From 3dfa5a54f4d668a1a5a7141ff6ad5b79af7067c8 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 13 Oct 2019 18:25:22 +0300 Subject: [PATCH] Fix syscalls --- etc/make/amd64/platform.mk | 3 ++- sys/amd64/sys/sched.c | 28 ++++++++++++++++++++-- sys/amd64/syscall.c | 6 +++++ sys/amd64/syscall_s.S | 49 +++++++++++++++++++++++++++++++------- 4 files changed, 75 insertions(+), 11 deletions(-) create mode 100644 sys/amd64/syscall.c diff --git a/etc/make/amd64/platform.mk b/etc/make/amd64/platform.mk index e41400e..2fede30 100644 --- a/etc/make/amd64/platform.mk +++ b/etc/make/amd64/platform.mk @@ -25,7 +25,8 @@ OBJS+=$(O)/sys/amd64/hw/rs232.o \ $(O)/sys/amd64/mm/phys.o \ $(O)/sys/amd64/mm/vmalloc.o \ $(O)/sys/amd64/mm/pool.o \ - $(O)/sys/amd64/syscall_s.o + $(O)/sys/amd64/syscall_s.o \ + $(O)/sys/amd64/syscall.o kernel_OBJS=$(O)/sys/amd64/entry.o \ $(OBJS) diff --git a/sys/amd64/sys/sched.c b/sys/amd64/sys/sched.c index 1cb46f7..5ff37e6 100644 --- a/sys/amd64/sys/sched.c +++ b/sys/amd64/sys/sched.c @@ -19,10 +19,23 @@ static char t_stack3[IDLE_STACK * AMD64_MAX_SMP] = {0}; static struct thread t_idle[AMD64_MAX_SMP] = {0}; void func0(uintptr_t id, int state) { + uintptr_t a0 = 0, a1 = 0; uint16_t attr = (id + 0x2) << 8; uint16_t letter = '0' + id; *((uint16_t *) MM_VIRTUALIZE(0xB8000) + 80 * 0 + id) = state * attr | letter; + { + register uintptr_t rsp asm ("rsp"); + a0 = rsp; + } asm volatile ("syscall"); + { + register uintptr_t rsp asm ("rsp"); + a1 = rsp; + } + + if (a0 != a1) { + while (1); + } } void idle_func(uintptr_t id) { @@ -31,7 +44,6 @@ void idle_func(uintptr_t id) { uintptr_t a0 = 0, a1 = 0; *((uint16_t *) MM_VIRTUALIZE(0xB8000) + 80 * 1 + id) = (id + 'A') | ((id + 0x9) << 8); - { register uintptr_t rsp asm ("rsp"); a0 = rsp; @@ -48,7 +60,19 @@ void idle_func(uintptr_t id) { while (1) { func0(id, state); - asm volatile ("syscall"); + { + register uintptr_t rsp asm ("rsp"); + a0 = rsp; + } + asm volatile ("syscall"); + { + register uintptr_t rsp asm ("rsp"); + a1 = rsp; + } + + if (a0 != a1) { + while (1); + } state = !state; for (size_t i = 0; i < 10000000; ++i); } diff --git a/sys/amd64/syscall.c b/sys/amd64/syscall.c new file mode 100644 index 0000000..1f922ac --- /dev/null +++ b/sys/amd64/syscall.c @@ -0,0 +1,6 @@ +#include "sys/types.h" +#include "sys/debug.h" + +int amd64_syscall(uintptr_t rdi, uintptr_t rsi, uintptr_t rdx, uintptr_t rcx, uintptr_t r10, uintptr_t rax) { + return 0; +} diff --git a/sys/amd64/syscall_s.S b/sys/amd64/syscall_s.S index 30c9b9a..342b4c8 100644 --- a/sys/amd64/syscall_s.S +++ b/sys/amd64/syscall_s.S @@ -55,21 +55,54 @@ amd64_syscall_entry: movq get_cpu(0x08), %rsp movq (%rsp), %rsp - pushq %rax - movq get_cpu(0x20), %rax - pushq %rax + pushq %rcx + movq get_cpu(0x20), %rcx + pushq %rcx + pushq %r11 - movq $100000000, %rax + // Safe for interrupts now sti -1: - decq %rax - jnz 1b + // Userspace: + // %rdi %rsi %rdx %rcx %8 %r9 + // Syscall: + // %rdi %rsi %rdx %r10 %r8 %r9 + // %rax + // Now it's done like + // %rdi -> %rdi + // %rsi -> %rsi + // %rdx -> %rdx + // %r10 -> %rcx + // %r8 -> %r8 + // %rax -> %r9 + // FIXME: use %rax properly and + // have a table of system calls + + movq %r10, %rcx + movq %rax, %r9 + call amd64_syscall + + // Disable interrupts cli + popq %r11 popq %rdi - popq %rax + popq %rcx + + // We could've been interrupted by irq0, which could've + // scheduled a new task, which could've rewritten the TSS/RSP0 + // with weird values, so just fix that up + movq %rsp, %rdx + movq get_cpu(0x08), %rsi + movq %rdx, (%rsi) + movq get_cpu(0x18), %rsi + addq $(25 * 8), %rdx + movq %rdx, 4(%rsi) + movq %rdi, %rsp swapgs sysretq + +_fmt0: + .string "rsp = %p\n"