From 991f1167df58616f4275c290fd0aa14baf7861f8 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sun, 10 Mar 2024 19:55:30 -0400 Subject: kernel/amd64: trap: Add stack-segment fault ISR Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/machdep.c | 1 + sys/arch/amd64/amd64/trap.S | 10 ++++++++++ sys/arch/amd64/amd64/trap.c | 10 ++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) (limited to 'sys/arch/amd64') diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index 6342aab..e5fe83e 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -78,6 +78,7 @@ interrupts_init(void) idt_set_desc(0x8, IDT_TRAP_GATE_FLAGS, ISR(double_fault), 0); idt_set_desc(0xA, IDT_TRAP_GATE_FLAGS, ISR(invl_tss), 0); idt_set_desc(0xB, IDT_TRAP_GATE_FLAGS, ISR(segnp), 0); + idt_set_desc(0xC, IDT_TRAP_GATE_FLAGS, ISR(ss_fault), 0); idt_set_desc(0xD, IDT_TRAP_GATE_FLAGS, ISR(general_prot), 0); idt_set_desc(0xE, IDT_TRAP_GATE_FLAGS, ISR(page_fault), 0); idt_load(); diff --git a/sys/arch/amd64/amd64/trap.S b/sys/arch/amd64/amd64/trap.S index 5a77955..66dd2a9 100644 --- a/sys/arch/amd64/amd64/trap.S +++ b/sys/arch/amd64/amd64/trap.S @@ -144,3 +144,13 @@ nmi: /* TODO */ cli hlt + +.globl ss_fault +ss_fault: + push_trapframe_ec $TRAP_SS + + handle_trap + + /* TODO */ + cli + hlt diff --git a/sys/arch/amd64/amd64/trap.c b/sys/arch/amd64/amd64/trap.c index b73048d..c1cff56 100644 --- a/sys/arch/amd64/amd64/trap.c +++ b/sys/arch/amd64/amd64/trap.c @@ -44,7 +44,8 @@ static const char *trap_type[] = { [TRAP_SEGNP] = "segment not present", [TRAP_PROTFLT] = "general protection", [TRAP_PAGEFLT] = "page fault", - [TRAP_NMI] = "non-maskable interrupt" + [TRAP_NMI] = "non-maskable interrupt", + [TRAP_SS] = "stack-segment fault" }; static const int TRAP_COUNT = __ARRAY_COUNT(trap_type); @@ -54,12 +55,17 @@ dbg_errcode(struct trapframe *tf) { uint64_t ec = tf->error_code; - if (tf->trapno == TRAP_PAGEFLT) { + switch (tf->trapno) { + case TRAP_PAGEFLT: kprintf("bits (pwui): %c%c%c%c\n", __TEST(ec, __BIT(0)) ? 'p' : '-', __TEST(ec, __BIT(1)) ? 'w' : '-', __TEST(ec, __BIT(2)) ? 'u' : '-', __TEST(ec, __BIT(4)) ? 'i' : '-'); + break; + case TRAP_SS: + kprintf("ss: 0x%x\n", ec); + break; } } -- cgit v1.2.3 From 27cd9535664a83e7c9986dc8a1b0e1a80aee1629 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Mon, 11 Mar 2024 12:56:50 -0400 Subject: kernel/amd64: machdep: Enable SSE/SSE2 per core Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/machdep.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'sys/arch/amd64') diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index e5fe83e..d936912 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -84,6 +85,15 @@ interrupts_init(void) idt_load(); } +static bool +is_sse_supported(void) +{ + uint32_t edx, unused; + + __CPUID(0x00000001, unused, unused, unused, edx); + return __TEST(edx, __BIT(25)) && __TEST(edx, __BIT(26)); +} + void processor_halt(void) { @@ -150,6 +160,7 @@ processor_init(void) { /* Indicates what doesn't need to be init anymore */ static uint8_t init_flags = 0; + static uint64_t reg_tmp; struct cpu_info *cur_cpu; /* Create our cpu_info structure */ @@ -160,6 +171,21 @@ processor_init(void) /* Set %GS to cpu_info */ amd64_write_gs_base((uintptr_t)cur_cpu); + if (is_sse_supported) { + /* Enable SSE/SSE2 */ + reg_tmp = amd64_read_cr0(); + reg_tmp &= ~(__BIT(2)); + reg_tmp |= __BIT(1); + amd64_write_cr0(reg_tmp); + + /* Enable FXSAVE/FXRSTOR */ + reg_tmp = amd64_read_cr4(); + reg_tmp |= 3 << 9; + amd64_write_cr4(reg_tmp); + } else { + panic("SSE/SSE2 not supported!\n"); + } + CPU_INFO_LOCK(cur_cpu); init_tss(cur_cpu); -- cgit v1.2.3 From 5c9366f3e5f7448811e03d4ee5671a3408353e41 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Wed, 13 Mar 2024 19:00:08 -0400 Subject: kernel/amd64: machdep: Fix typo Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/machdep.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sys/arch/amd64') diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index d936912..c24f484 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -171,7 +171,7 @@ processor_init(void) /* Set %GS to cpu_info */ amd64_write_gs_base((uintptr_t)cur_cpu); - if (is_sse_supported) { + if (is_sse_supported()) { /* Enable SSE/SSE2 */ reg_tmp = amd64_read_cr0(); reg_tmp &= ~(__BIT(2)); -- cgit v1.2.3 From 10e2fc7e3c6a924923664cf2919393b7f7b37326 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Wed, 13 Mar 2024 21:20:25 -0400 Subject: kernel/amd64: spectre: Log only once Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/spectre.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'sys/arch/amd64') diff --git a/sys/arch/amd64/amd64/spectre.c b/sys/arch/amd64/amd64/spectre.c index 1247607..05aa557 100644 --- a/sys/arch/amd64/amd64/spectre.c +++ b/sys/arch/amd64/amd64/spectre.c @@ -62,13 +62,18 @@ __weak int try_spectre_mitigate(void) { uint64_t tmp; + static bool should_log = true; if (!__can_mitigate_spectre()) { KINFO("IBRS not supported; spectre mitigation NOT enabled\n"); return EXIT_FAILURE; } - KINFO("IBRS supported; spectre mitigation enabled\n"); + /* This is called per processor, only log once */ + if (should_log) { + KINFO("IBRS supported; spectre mitigation enabled\n"); + should_log = false; + } tmp = rdmsr(IA32_SPEC_CTL); tmp |= __BIT(0); /* IBRS */ -- cgit v1.2.3 From b40d31b6f6e56fdf3f3c4f4b4be36681fb829c6b Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Wed, 13 Mar 2024 21:24:43 -0400 Subject: kernel/amd64: trap: Update panic message We have a scheduler now, this old message does not make sense. So it is replaced. Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/trap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sys/arch/amd64') diff --git a/sys/arch/amd64/amd64/trap.c b/sys/arch/amd64/amd64/trap.c index c1cff56..f1e58f1 100644 --- a/sys/arch/amd64/amd64/trap.c +++ b/sys/arch/amd64/amd64/trap.c @@ -121,5 +121,5 @@ trap_handler(struct trapframe *tf) } regdump(tf); - panic("Caught pre-sched exception\n"); + panic("Halted\n"); } -- cgit v1.2.3 From 12d4b76edec3006c8dd131ee797e110d6f76cac9 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Wed, 13 Mar 2024 21:26:15 -0400 Subject: kernel/amd64: machdep: Remove extra whitespace Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/machdep.c | 1 - 1 file changed, 1 deletion(-) (limited to 'sys/arch/amd64') diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index c24f484..809e395 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -100,7 +100,6 @@ processor_halt(void) __ASMV("cli; hlt"); } - /* * Send char to serial for debugging purposes. */ -- cgit v1.2.3 From fc9c7bab5bb64dd2242e9e9dff98060d64af2a32 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Wed, 13 Mar 2024 21:27:39 -0400 Subject: kernel/amd64: machdep: Add pcb init code This commit adds a processor specific routine which sets up the Process Control Block for a thread Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/machdep.c | 25 +++++++++++++++++++++++++ sys/include/sys/machdep.h | 2 ++ sys/kern/kern_sched.c | 2 ++ 3 files changed, 29 insertions(+) (limited to 'sys/arch/amd64') diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index 809e395..0789f5a 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -154,6 +154,31 @@ intr_unmask(void) __ASMV("sti"); } +int +processor_init_pcb(struct proc *proc) +{ + struct pcb *pcb = &proc->pcb; + const uint16_t FPU_FCW = 0x33F; + const uint32_t SSE_MXCSR = 0x1F80; + + /* Allocate FPU save area, aligned on a 16 byte boundary */ + pcb->fpu_state = PHYS_TO_VIRT(vm_alloc_pageframe(1)); + if (pcb->fpu_state == 0) { + return -1; + } + + /* + * Setup x87 FPU control word and SSE MXCSR bits + * as per the sysv ABI + */ + __ASMV("fldcw %0\n" + "ldmxcsr %1" + :: "m" (FPU_FCW), + "m" (SSE_MXCSR) : "memory"); + + amd64_fxsave(pcb->fpu_state); + return 0; +} void processor_init(void) { diff --git a/sys/include/sys/machdep.h b/sys/include/sys/machdep.h index 713b7db..151d5f8 100644 --- a/sys/include/sys/machdep.h +++ b/sys/include/sys/machdep.h @@ -32,11 +32,13 @@ #include #include +#include #if defined(_KERNEL) #define MAXCPUS 32 +int processor_init_pcb(struct proc *proc); void processor_init(void); void processor_halt(void); void intr_mask(void); diff --git a/sys/kern/kern_sched.c b/sys/kern/kern_sched.c index 7be8241..f8f09c4 100644 --- a/sys/kern/kern_sched.c +++ b/sys/kern/kern_sched.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -264,6 +265,7 @@ sched_create_td(uintptr_t rip, char *argvp[], char *envp[], struct auxval auxv, td->cpu = NULL; /* Not yet assigned a core */ td->tf = tf; td->addrsp = vas; + processor_init_pcb(td); /* Setup trapframe */ if (!is_user) { -- cgit v1.2.3 From 39d4aa3a106f898d09b98e41ef44fb4891eda1ce Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Wed, 13 Mar 2024 21:40:06 -0400 Subject: kernel/amd64: machdep: Add context switch helper Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/machdep.c | 13 +++++++++++++ sys/include/sys/machdep.h | 1 + sys/kern/kern_sched.c | 4 ++++ 3 files changed, 18 insertions(+) (limited to 'sys/arch/amd64') diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index 0789f5a..5327189 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -179,6 +179,19 @@ processor_init_pcb(struct proc *proc) amd64_fxsave(pcb->fpu_state); return 0; } + +void +processor_switch_to(struct proc *old_td, struct proc *new_td) +{ + struct pcb *old_pcb = (old_td != NULL) ? &old_td->pcb : NULL; + struct pcb *new_pcb = &new_td->pcb; + + if (old_pcb != NULL) { + amd64_fxsave(old_pcb->fpu_state); + } + amd64_fxrstor(new_pcb->fpu_state); +} + void processor_init(void) { diff --git a/sys/include/sys/machdep.h b/sys/include/sys/machdep.h index 151d5f8..b6edf14 100644 --- a/sys/include/sys/machdep.h +++ b/sys/include/sys/machdep.h @@ -39,6 +39,7 @@ #define MAXCPUS 32 int processor_init_pcb(struct proc *proc); +void processor_switch_to(struct proc *old_td, struct proc *new_td); void processor_init(void); void processor_halt(void); void intr_mask(void); diff --git a/sys/kern/kern_sched.c b/sys/kern/kern_sched.c index f8f09c4..bc96146 100644 --- a/sys/kern/kern_sched.c +++ b/sys/kern/kern_sched.c @@ -311,6 +311,10 @@ sched_context_switch(struct trapframe *tf) sched_enqueue_td(td); } + /* Do architecture specific context switch logic */ + processor_switch_to(td, next_td); + + /* Done, switch out our vas and oneshot */ pmap_switch_vas(vm_get_ctx(), next_td->addrsp); sched_oneshot(); } -- cgit v1.2.3 From 86ee5ee30e2f98137da80434af788bf928f49d55 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Wed, 13 Mar 2024 23:28:44 -0400 Subject: kernel/amd64: machdep: Check for NULL instead of 0 Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/machdep.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sys/arch/amd64') diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index 5327189..7ad3e6a 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -163,7 +163,7 @@ processor_init_pcb(struct proc *proc) /* Allocate FPU save area, aligned on a 16 byte boundary */ pcb->fpu_state = PHYS_TO_VIRT(vm_alloc_pageframe(1)); - if (pcb->fpu_state == 0) { + if (pcb->fpu_state == NULL) { return -1; } -- cgit v1.2.3 From a2b2dbaa71278769e9dbf5ca611cd0d5f3b896ed Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Wed, 13 Mar 2024 23:30:21 -0400 Subject: kernel/amd64: machdep: Add processor_free_pcb() Add routine to deallocate resources within the process control block. Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/machdep.c | 13 +++++++++++++ sys/include/sys/machdep.h | 1 + 2 files changed, 14 insertions(+) (limited to 'sys/arch/amd64') diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index 7ad3e6a..fa6c1f5 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -180,6 +180,19 @@ processor_init_pcb(struct proc *proc) return 0; } +int +processor_free_pcb(struct proc *proc) +{ + struct pcb *pcb = &proc->pcb; + + if (pcb->fpu_state == NULL) { + return -1; + } + + vm_free_pageframe(VIRT_TO_PHYS(pcb->fpu_state), 1); + return 0; +} + void processor_switch_to(struct proc *old_td, struct proc *new_td) { diff --git a/sys/include/sys/machdep.h b/sys/include/sys/machdep.h index b6edf14..0c06374 100644 --- a/sys/include/sys/machdep.h +++ b/sys/include/sys/machdep.h @@ -39,6 +39,7 @@ #define MAXCPUS 32 int processor_init_pcb(struct proc *proc); +int processor_free_pcb(struct proc *proc); void processor_switch_to(struct proc *old_td, struct proc *new_td); void processor_init(void); void processor_halt(void); -- cgit v1.2.3 From 3e3ff0acccfbb1d251a9b9bacbf0851c9de69561 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 14 Mar 2024 21:02:10 -0400 Subject: kernel/amd64: pmap: Add pmap_free_vas() Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/pmap.c | 13 +++++++++++++ sys/include/vm/pmap.h | 6 ++++++ 2 files changed, 19 insertions(+) (limited to 'sys/arch/amd64') diff --git a/sys/arch/amd64/amd64/pmap.c b/sys/arch/amd64/amd64/pmap.c index 2760532..54d4ca3 100644 --- a/sys/arch/amd64/amd64/pmap.c +++ b/sys/arch/amd64/amd64/pmap.c @@ -228,6 +228,19 @@ pmap_switch_vas(struct vm_ctx *ctx, struct vas vas) : "memory"); } +/* + * TODO: During the mapping of a virtual address, a level + * may be allocated. This function does not handle the + * freeing of allocated levels. We should keep track + * of levels allocated and free them here. + */ +int +pmap_free_vas(struct vm_ctx *ctx, struct vas vas) +{ + vm_free_pageframe(vas.top_level, 1); + return 0; +} + struct vas pmap_read_vas(void) { diff --git a/sys/include/vm/pmap.h b/sys/include/vm/pmap.h index ebabd32..3380199 100644 --- a/sys/include/vm/pmap.h +++ b/sys/include/vm/pmap.h @@ -89,6 +89,12 @@ struct vas pmap_read_vas(void); */ int pmap_map(struct vm_ctx *, struct vas, vaddr_t, paddr_t, vm_prot_t); +/* + * Get rid of a virtual address space and free + * resources. + */ +int pmap_free_vas(struct vm_ctx *, struct vas); + /* * Unmap a page. */ -- cgit v1.2.3 From cec19cdab21e1eda7ae149792d1529bb03c0714b Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 14 Mar 2024 21:47:23 -0400 Subject: kernel: Add support for syscalls Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/machdep.c | 3 +++ sys/arch/amd64/amd64/syscall.S | 43 +++++++++++++++++++++++++++++++ sys/arch/amd64/amd64/syscall.c | 51 +++++++++++++++++++++++++++++++++++++ sys/include/sys/syscall.h | 58 ++++++++++++++++++++++++++++++++++++++++++ sys/kern/kern_syscall.c | 50 ++++++++++++++++++++++++++++++++++++ 5 files changed, 205 insertions(+) create mode 100644 sys/arch/amd64/amd64/syscall.S create mode 100644 sys/arch/amd64/amd64/syscall.c create mode 100644 sys/include/sys/syscall.h create mode 100644 sys/kern/kern_syscall.c (limited to 'sys/arch/amd64') diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index fa6c1f5..47d6dd0 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -57,6 +57,8 @@ __KERNEL_META("$Hyra$: machdep.c, Ian Marco Moffett, " #define INIT_FLAG_IOAPIC 0x00000001U #define INIT_FLAG_ACPI 0x00000002U +void syscall_isr(void); + static inline void init_tss(struct cpu_info *cur_cpu) { @@ -82,6 +84,7 @@ interrupts_init(void) idt_set_desc(0xC, IDT_TRAP_GATE_FLAGS, ISR(ss_fault), 0); idt_set_desc(0xD, IDT_TRAP_GATE_FLAGS, ISR(general_prot), 0); idt_set_desc(0xE, IDT_TRAP_GATE_FLAGS, ISR(page_fault), 0); + idt_set_desc(0x80, IDT_INT_GATE_USER, ISR(syscall_isr), 0); idt_load(); } diff --git a/sys/arch/amd64/amd64/syscall.S b/sys/arch/amd64/amd64/syscall.S new file mode 100644 index 0000000..fe70523 --- /dev/null +++ b/sys/arch/amd64/amd64/syscall.S @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + + #include + #include + +__KERNEL_META "$Hyra$: syscall.S, Ian Marco Moffett, \ + Syscall ISR code" + +.text +.globl syscall_isr +syscall_isr: + push_trapframe $0 + mov %rsp, %rdi + call __syscall + pop_trapframe + iretq diff --git a/sys/arch/amd64/amd64/syscall.c b/sys/arch/amd64/amd64/syscall.c new file mode 100644 index 0000000..9b1f988 --- /dev/null +++ b/sys/arch/amd64/amd64/syscall.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +void +__syscall(struct trapframe *tf) +{ + struct syscall_args args = { + .code = tf->rax, + .arg0 = tf->rdi, + .arg1 = tf->rsi, + .arg2 = tf->rcx, + .arg3 = tf->r8, + .arg4 = tf->r9, + .sp = tf->rsp, + .ret = tf->rax, + }; + + if (args.code < __MAX_SYSCALLS) { + g_syscall_table[tf->rax](&args); + } + + tf->rax = args.ret; +} diff --git a/sys/include/sys/syscall.h b/sys/include/sys/syscall.h new file mode 100644 index 0000000..98a57f2 --- /dev/null +++ b/sys/include/sys/syscall.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _SYS_SYSCALL_H_ +#define _SYS_SYSCALL_H_ + +#include +#if defined(_KERNEL) +#include +#endif + +/* Do not reorder */ +enum { + SYS_debug = 0, + SYS_exit, + __MAX_SYSCALLS +}; + +struct syscall_args { + uint64_t code; + uint64_t arg0, arg1, arg2, arg3, arg4; + uint64_t ip; + uint64_t sp; + uint64_t ret; +}; + +#if defined(_KERNEL) +extern void(*g_syscall_table[__MAX_SYSCALLS])(struct syscall_args *args); +void __syscall(struct trapframe *tf); +#endif + +#endif diff --git a/sys/kern/kern_syscall.c b/sys/kern/kern_syscall.c new file mode 100644 index 0000000..2c7215c --- /dev/null +++ b/sys/kern/kern_syscall.c @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +static void +sys_debug(struct syscall_args *args) +{ + /* TODO */ +} + +__noreturn static void +sys_exit(struct syscall_args *args) +{ + sched_exit(); + __builtin_unreachable(); +} + +void(*g_syscall_table[__MAX_SYSCALLS])(struct syscall_args *args) = { + sys_debug, + sys_exit, +}; -- cgit v1.2.3 From 3db86a30644d3eb3a964202dbfd7e91c432bda7b Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sat, 16 Mar 2024 12:42:25 -0400 Subject: kernel: syscall: Remove syscall_args.ret It is better to just return a value within the syscall handler and have that passed down to __syscall() like that Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/syscall.c | 7 ++----- sys/include/sys/syscall.h | 3 +-- sys/kern/kern_syscall.c | 8 +++++--- 3 files changed, 8 insertions(+), 10 deletions(-) (limited to 'sys/arch/amd64') diff --git a/sys/arch/amd64/amd64/syscall.c b/sys/arch/amd64/amd64/syscall.c index 9b1f988..e80fe94 100644 --- a/sys/arch/amd64/amd64/syscall.c +++ b/sys/arch/amd64/amd64/syscall.c @@ -39,13 +39,10 @@ __syscall(struct trapframe *tf) .arg2 = tf->rcx, .arg3 = tf->r8, .arg4 = tf->r9, - .sp = tf->rsp, - .ret = tf->rax, + .sp = tf->rsp }; if (args.code < __MAX_SYSCALLS) { - g_syscall_table[tf->rax](&args); + tf->rax = g_syscall_table[tf->rax](&args); } - - tf->rax = args.ret; } diff --git a/sys/include/sys/syscall.h b/sys/include/sys/syscall.h index 98a57f2..03eda0b 100644 --- a/sys/include/sys/syscall.h +++ b/sys/include/sys/syscall.h @@ -47,11 +47,10 @@ struct syscall_args { uint64_t arg0, arg1, arg2, arg3, arg4; uint64_t ip; uint64_t sp; - uint64_t ret; }; #if defined(_KERNEL) -extern void(*g_syscall_table[__MAX_SYSCALLS])(struct syscall_args *args); +extern uint64_t(*g_syscall_table[__MAX_SYSCALLS])(struct syscall_args *args); void __syscall(struct trapframe *tf); #endif diff --git a/sys/kern/kern_syscall.c b/sys/kern/kern_syscall.c index 2c7215c..5a88bbb 100644 --- a/sys/kern/kern_syscall.c +++ b/sys/kern/kern_syscall.c @@ -30,21 +30,23 @@ #include #include #include +#include -static void +static uint64_t sys_debug(struct syscall_args *args) { /* TODO */ + return 0; } -__noreturn static void +__noreturn static uint64_t sys_exit(struct syscall_args *args) { sched_exit(); __builtin_unreachable(); } -void(*g_syscall_table[__MAX_SYSCALLS])(struct syscall_args *args) = { +uint64_t(*g_syscall_table[__MAX_SYSCALLS])(struct syscall_args *args) = { sys_debug, sys_exit, }; -- cgit v1.2.3 From f166b2303a96a5e65e235139291df93b2b24bc3e Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sat, 16 Mar 2024 13:03:19 -0400 Subject: kernel,libc: syscall: Improve syscall code - Remove the SYS_debug syscall - First syscall starts at 1 Signed-off-by: Ian Moffett --- lib/libc/include/sys/syscall.h | 1 - sys/arch/amd64/amd64/syscall.c | 5 +++-- sys/include/sys/syscall.h | 3 +-- sys/kern/kern_syscall.c | 8 -------- 4 files changed, 4 insertions(+), 13 deletions(-) (limited to 'sys/arch/amd64') diff --git a/lib/libc/include/sys/syscall.h b/lib/libc/include/sys/syscall.h index b5e5fc2..34f762d 100644 --- a/lib/libc/include/sys/syscall.h +++ b/lib/libc/include/sys/syscall.h @@ -34,7 +34,6 @@ #include #endif -#define SYS_debug 0 #define SYS_exit 1 #if !defined(__ASSEMBLER__) diff --git a/sys/arch/amd64/amd64/syscall.c b/sys/arch/amd64/amd64/syscall.c index e80fe94..68235d5 100644 --- a/sys/arch/amd64/amd64/syscall.c +++ b/sys/arch/amd64/amd64/syscall.c @@ -42,7 +42,8 @@ __syscall(struct trapframe *tf) .sp = tf->rsp }; - if (args.code < __MAX_SYSCALLS) { - tf->rax = g_syscall_table[tf->rax](&args); + if (args.code < __MAX_SYSCALLS && args.code > 0) { + args.code -= 1; + tf->rax = g_syscall_table[args.code](&args); } } diff --git a/sys/include/sys/syscall.h b/sys/include/sys/syscall.h index 03eda0b..66dc5f3 100644 --- a/sys/include/sys/syscall.h +++ b/sys/include/sys/syscall.h @@ -37,8 +37,7 @@ /* Do not reorder */ enum { - SYS_debug = 0, - SYS_exit, + SYS_exit = 1, __MAX_SYSCALLS }; diff --git a/sys/kern/kern_syscall.c b/sys/kern/kern_syscall.c index 5a88bbb..b6e31d1 100644 --- a/sys/kern/kern_syscall.c +++ b/sys/kern/kern_syscall.c @@ -32,13 +32,6 @@ #include #include -static uint64_t -sys_debug(struct syscall_args *args) -{ - /* TODO */ - return 0; -} - __noreturn static uint64_t sys_exit(struct syscall_args *args) { @@ -47,6 +40,5 @@ sys_exit(struct syscall_args *args) } uint64_t(*g_syscall_table[__MAX_SYSCALLS])(struct syscall_args *args) = { - sys_debug, sys_exit, }; -- cgit v1.2.3