From 0a0235612e5faa0b3d342284571921384e308300 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Tue, 21 Nov 2023 19:19:14 -0500 Subject: [PATCH] syscall: change handling of exceptions and traps --- dev/pic.c | 17 ------------- include/kernel/proc.h | 1 + init/trap.c | 56 +++++++++++++++++++++++++++++++++++++++++++ sched/entry.S | 2 +- sched/syscall.c | 1 + 5 files changed, 59 insertions(+), 18 deletions(-) create mode 100644 init/trap.c diff --git a/dev/pic.c b/dev/pic.c index 9aec220..4ea5ddc 100644 --- a/dev/pic.c +++ b/dev/pic.c @@ -32,23 +32,6 @@ void pic_eoi(uint8_t irq) outb(PIC1_CMD, 0x20); } -void exception(int vector, int error) -{ - kprintf(LOG_WARN "EXCEPTION: %x, error code: %d\n", vector, error); - pid_t pid = getpid(); - - struct proc *proc = proc_find(pid); - - if (proc->is_kernel) { - kprintf(LOG_ERROR "Kernel process %d crashed\n", pid); - panic(); - } else { - proc_term(pid); - kprintf(LOG_WARN "Process %d terminated\n", pid); - yield(); - } -} - void pic_init() { /* disable the PIC */ diff --git a/include/kernel/proc.h b/include/kernel/proc.h index e864ea3..5092982 100644 --- a/include/kernel/proc.h +++ b/include/kernel/proc.h @@ -60,5 +60,6 @@ void proc_term(pid_t pid); struct proc *proc_find(pid_t pid); void proc_set_current(pid_t pid); void proc_init(unsigned num_cpus); +void schedule(); #endif /* _PROC_H_ */ diff --git a/init/trap.c b/init/trap.c new file mode 100644 index 0000000..36b9b84 --- /dev/null +++ b/init/trap.c @@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include + +static const char *exception_names[] = { + "Divide-by-zero Error", + "Debug", + "Non-maskable Interrupt", + "Breakpoint", + "Overflow", + "Bound Range Exceeded", + "Invalid Opcode", + "Device Not Available", + "Double Fault", + "Coprocessor Segment Overrun", + "Invalid TSS", + "Segment Not Present", + "Stack-Segment Fault", + "General Protection Fault", + "Page Fault", + "Reserved", + "x87 Floating-Point Exception", + "Alignment Check", + "Machine Check", + "SIMD Floating-Point Exception", + "Virtualization Exception", + "Invalid exception", + "Invalid exception", + "Invalid exception", + "Invalid exception", + "Invalid exception", + "Invalid exception", + "Invalid exception", + "Security Exception", + "Reserved", + "Triple Fault", +}; + +void exception(int vector, int error) +{ + pid_t pid = getpid(); + + struct proc *proc = proc_find(pid); + + if (proc->is_kernel) { + kprintf(LOG_ERROR "Kernel process %d crashed: %s(%xh)\n", pid, exception_names[vector], error); + panic(); + } else { + proc_term(pid); + kprintf("Process %d terminated: %s(%xh)\n", pid, exception_names[vector], error); + proc_set_current(0); + + schedule(); + } +} diff --git a/sched/entry.S b/sched/entry.S index f002ac3..7bc5c40 100644 --- a/sched/entry.S +++ b/sched/entry.S @@ -201,7 +201,7 @@ gate_syscall: movq 40(%rax), %rsi /* arguments */ call syscall - call schedule + iretq /* void return_from_irq(struct procregs *regs, paddr_t cr3); */ .global _return_to_user diff --git a/sched/syscall.c b/sched/syscall.c index ddebd14..37a149b 100644 --- a/sched/syscall.c +++ b/sched/syscall.c @@ -35,6 +35,7 @@ void sys_exit(int status) kprintf("(syscall)Process %d exited with status 0x%x\n", getpid(), status); proc_set_current(0); + schedule(); } static void syscall_insert(uint64_t syscall_no, syscall_t syscall)