signals: handle SIGBUS, SIGSEGV, SIGILL etc

This commit is contained in:
Matheus
2024-05-29 11:06:15 -03:00
parent d689766a44
commit c81bdac897
4 changed files with 33 additions and 4 deletions
+18
View File
@@ -7,6 +7,21 @@
#define EFER_SYSCALLENABLE 1
void arch_syscall_entry();
static void illisr(isr_t *self, context_t *ctx) {
if (ARCH_CONTEXT_ISUSER(ctx)) {
signal_signalthread(_cpu()->thread, SIGILL, true);
} else {
_panic("Invalid Opcode", ctx);
}
}
static void div0isr(isr_t *self, context_t *ctx) {
if (ARCH_CONTEXT_ISUSER(ctx)) {
signal_signalthread(_cpu()->thread, SIGFPE, true);
} else {
_panic("Division by 0", ctx);
}
}
void cpu_initstate() {
arch_apic_initap();
@@ -49,4 +64,7 @@ void cpu_initstate() {
"mov %%rax, %%cr0;"
: : : "rax"
);
interrupt_register(0, div0isr, NULL, IPL_IGNORE);
interrupt_register(6, illisr, NULL, IPL_IGNORE);
}
+9 -1
View File
@@ -278,10 +278,17 @@ static void pfisr(isr_t *self, context_t *ctx) {
if (ARCH_CONTEXT_ISUSER(ctx))
signal_signalthread(_cpu()->thread, SIGSEGV, true);
else
_panic("Page fault", ctx);
_panic("Page Fault", ctx);
}
}
static void gpfisr(isr_t *self, context_t *ctx) {
if (ARCH_CONTEXT_ISUSER(ctx))
signal_signalthread(_cpu()->thread, SIGSEGV, true);
else
_panic("General Protection Fault", ctx);
}
void arch_mmu_init() {
template = pmm_allocpage(PMM_SECTION_DEFAULT);
__assert(template);
@@ -326,6 +333,7 @@ void arch_mmu_init() {
void arch_mmu_apswitch() {
arch_mmu_switch(FROM_HHDM(template));
interrupt_register(13, gpfisr, NULL, IPL_IGNORE);
interrupt_register(14, pfisr, NULL, IPL_IGNORE);
interrupt_register(0xfe, arch_mmu_tlbipi, ARCH_EOI, IPL_IGNORE);
}
+3 -2
View File
@@ -363,8 +363,9 @@ bool vmm_pagefault(void *addr, bool user, int actions) {
page_t *res = NULL;
int error = vmmcache_getpage(range->vnode, range->offset + mapoffset, &res);
if (error == ENXIO) {
// TODO if error returned == ENXIO, raise an urgent SIGBUS on the calling thread
__assert(!"TODO: sigbus");
// address is past the last page of the file
signal_signalthread(_cpu()->thread, SIGBUS, true);
status = true;
} else if (error) {
printf("vmm: error on vmmcache_getpage(): %d\n", error);
status = false;
+3 -1
View File
@@ -325,6 +325,7 @@ bool signal_check(struct thread_t *thread, context_t *context, bool syscall, uin
goto leave;
}
// if not, just sleep. the sigcont won't be sent until THREAD_LEAVE and preparesleep is protecting it from a lost wakeup
context_t savecontext = *context; // save context as it could be overwritten if, for example, its the thread->context one
sched_preparesleep(false);
thread->signals.stopped = true;
THREAD_LEAVE(thread);
@@ -334,6 +335,7 @@ bool signal_check(struct thread_t *thread, context_t *context, bool syscall, uin
// make sure a SIGCONT happened
__assert(SIGNAL_GET(&thread->signals.pending, SIGCONT));
__assert(thread->signals.stopped == false);
*context = savecontext; // restore saved context
// the stopped variable is already set by the signaling function
// see if theres any other signal to deal with (such as the SIGCONT handler)
return true;
@@ -352,7 +354,7 @@ bool signal_check(struct thread_t *thread, context_t *context, bool syscall, uin
// get where in memory to put the frame
void *stack = altstack ? altstack : (void *)CTX_SP(context);
#if ARCH_SIGNAL_STACK_GROWS_DOWNWARDS == 1
stack = (void *)(((uintptr_t)stack - ARCH_SIGNAL_REDZONE_SIZE - sizeof(sigframe_t)) & ~0xf);
stack = (void *)(((uintptr_t)stack - ARCH_SIGNAL_REDZONE_SIZE - sizeof(sigframe_t)) & ~0xfl);
#else
#error unsupported
#endif