syscall: change handling of exceptions and traps

This commit is contained in:
Michael Shaw 2023-11-21 19:19:14 -05:00
commit 0a0235612e
5 changed files with 59 additions and 18 deletions

View file

@ -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 */

View file

@ -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_ */

56
init/trap.c Normal file
View file

@ -0,0 +1,56 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/slab.h>
#include <kernel/proc.h>
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();
}
}

View file

@ -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

View file

@ -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)