Skip to content

Commit

Permalink
usermode: handle usermode exceptions
Browse files Browse the repository at this point in the history
Exceptions occuring in usermode terminate a user task, but are not fatal
for the kernel.

Signed-off-by: Pawel Wieczorkiewicz <[email protected]>
  • Loading branch information
wipawel committed Jul 22, 2022
1 parent fd33011 commit dd62f2e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 9 additions & 2 deletions arch/x86/traps.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@
#include <drivers/keyboard.h>
#include <drivers/pit.h>
#include <drivers/serial.h>
#include <errno.h>
#include <extables.h>
#include <ktf.h>
#include <mm/regions.h>
#include <percpu.h>
#include <segment.h>
#include <symbols.h>
#include <traps.h>
#include <usermode.h>

#include <mm/vmm.h>
#include <usermode.h>

extern void asm_interrupt_handler_uart(void);
extern void asm_interrupt_handler_keyboard(void);
Expand Down Expand Up @@ -298,7 +299,7 @@ static bool extables_fixup(struct cpu_regs *regs) {
void do_exception(struct cpu_regs *regs) {
static char ec_str[32], panic_str[128];

if (extables_fixup(regs))
if (!from_usermode(regs->cs) && extables_fixup(regs))
return;

dump_regs(regs);
Expand All @@ -312,5 +313,11 @@ void do_exception(struct cpu_regs *regs) {
exception_names[regs->vector], ec_str, regs->cs, regs->_ASM_IP, regs->ss,
regs->_ASM_SP);

/* Handle user tasks' exceptions */
if (from_usermode(regs->cs)) {
printk("Task exception: %s\n", panic_str);
goto_syscall_exit(-EFAULT);
}

panic(panic_str);
}
5 changes: 5 additions & 0 deletions include/usermode.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ static inline bool from_usermode(uint16_t cs) {
return (cs & 0x3) != 0;
}

static inline void goto_syscall_exit(long exit_code) {
swapgs();
asm volatile("jmp syscall_handler" ::"A"(SYSCALL_EXIT), "D"(exit_code));
}

/* External declarations */

extern unsigned long enter_usermode(task_func_t fn, void *fn_arg,
Expand Down

0 comments on commit dd62f2e

Please sign in to comment.