From aaa23fffd02fb49cdbc56a480dbb5a8fa95bff38 Mon Sep 17 00:00:00 2001 From: Aleksa Vuckovic Date: Sat, 25 Feb 2023 05:43:37 +0100 Subject: x86_64_regs.S: (push/pop)_(callee/caller)_regs --- kernel/include/x86_64_regs.S | 78 ++++++++++++++++++++++++++++++++++++++++++ kernel/src/cpu/irq_stub.S | 60 ++++---------------------------- kernel/src/mem/pmm.c | 7 ---- kernel/src/scheduler/ap_init.S | 1 - kernel/src/scheduler/switch.S | 18 ++++++++++ kernel/src/sys/syscall_asm.S | 38 ++------------------ 6 files changed, 106 insertions(+), 96 deletions(-) create mode 100644 kernel/include/x86_64_regs.S create mode 100644 kernel/src/scheduler/switch.S (limited to 'kernel') diff --git a/kernel/include/x86_64_regs.S b/kernel/include/x86_64_regs.S new file mode 100644 index 0000000..7b72d76 --- /dev/null +++ b/kernel/include/x86_64_regs.S @@ -0,0 +1,78 @@ +/* isr stack */ +/* ss:rsp (original rsp) -> rflags -> cs -> rip */ + +/* callee saved registers: rbx, rbp, rsp, r12, r13, r14, r15 */ +/* isr saved registers: rsp, rip, rflags, cs, ss */ + +/* if the code will stay on same thread of execution use push/pop_caller_saved + * else use push/pop_callee_saved with push/pop_caller_saved + */ + +/* push 0x30 bytes to stack */ +.macro push_callee_saved + push %rbx + push %rbp + push %r12 + push %r13 + push %r14 + push %r15 +.endm + +/* pop 0x30 bytes from stack */ +.macro pop_callee_saved + push %rbx + push %rbp + push %r12 + push %r13 + push %r14 + push %r15 +.endm + +/* push 0x50 bytes to stack */ +.macro push_caller_saved + push %rax + push %rcx + push %rdx + push %rsi + push %rdi + push %r8 + push %r9 + push %r10 + push %r11 + mov %ds, %ax + shl $16, %rax + mov %es, %ax + shl $16, %rax + mov %fs, %ax + shl $16, %rax + mov %gs, %ax + push %rax + mov $0x10, %ax + mov %ax, %ds + mov %ax, %ss + mov %ax, %es + mov %ax, %fs + mov %ax, %gs + mov 0x48(%rsp), %rax +.endm + +/* pop 0x50 bytes from stack */ +.macro pop_caller_saved + pop %rax + mov %ax, %gs + shr $16, %rax + mov %ax, %fs + shr $16, %rax + mov %ax, %es + shr $16, %rax + mov %ax, %ds + pop %r11 + pop %r10 + pop %r9 + pop %r8 + pop %rdi + pop %rsi + pop %rdx + pop %rcx + pop %rax +.endm diff --git a/kernel/src/cpu/irq_stub.S b/kernel/src/cpu/irq_stub.S index 3ba4360..33d87e1 100644 --- a/kernel/src/cpu/irq_stub.S +++ b/kernel/src/cpu/irq_stub.S @@ -1,61 +1,15 @@ .extern panic_rsp -.macro pushall - push %rax - push %rbx - push %rcx - push %rdx - push %rsi - push %rdi - push %r8 - push %r9 - push %r10 - push %r11 - push %r12 - push %r13 - push %r14 - push %r15 - mov %ds, %ax - push %rax - mov %es, %ax - push %rax - mov %ss, %ax - push %rax - mov $0x10, %ax - mov %ax, %ds - mov %ax, %es - mov 0x80(%rsp), %rax -.endm - -.macro popall - pop %rax - pop %rax - mov %ax, %es - pop %rax - mov %ax, %ds - pop %r15 - pop %r14 - pop %r13 - pop %r12 - pop %r11 - pop %r10 - pop %r9 - pop %r8 - pop %rdi - pop %rsi - pop %rdx - pop %rcx - pop %rbx - pop %rax -.endm +#include "x86_64_regs.S" .macro isrstub - pushall - mov 0x88(%rsp), %rsi + push_caller_saved + /* get error code from stack */ + mov 0x50(%rsp), %rsi mov %rsp, panic_rsp cld call isr_def_handler - popall + pop_caller_saved add $8, %rsp iretq .endm @@ -78,12 +32,12 @@ isr\number: .macro irq number .global irq\number irq\number: - pushall + push_caller_saved mov $\number, %rdi mov %rsp, panic_rsp cld call irq_def_handler - popall + pop_caller_saved iretq .endm diff --git a/kernel/src/mem/pmm.c b/kernel/src/mem/pmm.c index e8245f2..f0b4884 100644 --- a/kernel/src/mem/pmm.c +++ b/kernel/src/mem/pmm.c @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -33,12 +32,6 @@ void init_pmm() } } all_mem_cnt = free_mem_cnt; - - // print addr of every free block of memory - //list_t* pok; - //list_for_each(pok, (&pmm_list)) { - // serial_printf("0x%x\n", pok); - //} } void memory_usage() diff --git a/kernel/src/scheduler/ap_init.S b/kernel/src/scheduler/ap_init.S index 47efc6f..ac758c8 100644 --- a/kernel/src/scheduler/ap_init.S +++ b/kernel/src/scheduler/ap_init.S @@ -1,6 +1,5 @@ .section .apinit, "a" - .SET stack_top, 0x3008000 .SET stack_off, 0x8000 diff --git a/kernel/src/scheduler/switch.S b/kernel/src/scheduler/switch.S new file mode 100644 index 0000000..f4d0e79 --- /dev/null +++ b/kernel/src/scheduler/switch.S @@ -0,0 +1,18 @@ +#include "x86_64_regs.S" + +.global context_switch +context_switch: + /* switch rsp to old task's %rsp */ + + /* save all old task's registers */ + push_caller_saved + push_callee_saved + + /* save old task's rsp & rip */ + call schedule + /* get new task's rsp & rip */ + + /* restore all new task's registers */ + pop_callee_saved + pop_caller_saved + iretq diff --git a/kernel/src/sys/syscall_asm.S b/kernel/src/sys/syscall_asm.S index 2e3c0bb..8287e95 100644 --- a/kernel/src/sys/syscall_asm.S +++ b/kernel/src/sys/syscall_asm.S @@ -1,47 +1,15 @@ .extern syscall_handler -.macro pushall - push %rax - push %rbx - push %rcx - push %rdx - push %rsi - push %rdi - push %r8 - push %r9 - push %r10 - push %r11 - push %r12 - push %r13 - push %r14 - push %r15 -.endm - -.macro popall - pop %r15 - pop %r14 - pop %r13 - pop %r12 - pop %r11 - pop %r10 - pop %r9 - pop %r8 - pop %rdi - pop %rsi - pop %rdx - pop %rcx - pop %rbx - pop %rax -.endm +#include "x86_64_regs.S" .global __syscall __syscall: pushq %rcx pushq %r11 - pushall + push_caller_saved call syscall_handler - popall + pop_caller_saved popfq popq %rcx -- cgit v1.2.3