diff --git a/src/drivers/nulldrv/main.s b/src/drivers/nulldrv/main.s index 6b3dacd..53dfffb 100644 --- a/src/drivers/nulldrv/main.s +++ b/src/drivers/nulldrv/main.s @@ -20,6 +20,9 @@ _start: mov rax, 1 ; DEBUG syscall syscall ; int 0xee + cmp r13, 0 + je .doexit + cmp r12, 1 je .dosend jne .doreceive @@ -59,3 +62,7 @@ _start: mov rdi, 1 ; source is pid 2 syscall ; int 0xee jmp .preloop + +.doexit + mov rax, 9 ; EXIT syscall + syscall diff --git a/src/kernel/process.cpp b/src/kernel/process.cpp index 5e38927..cc91a38 100644 --- a/src/kernel/process.cpp +++ b/src/kernel/process.cpp @@ -4,6 +4,13 @@ #include "scheduler.h" +void +process::exit(uint32_t code) +{ + return_code = code; + flags -= process_flags::running; +} + pid_t process::fork(uintptr_t in_rsp) { diff --git a/src/kernel/process.h b/src/kernel/process.h index 50dfee5..8bda077 100644 --- a/src/kernel/process.h +++ b/src/kernel/process.h @@ -57,6 +57,10 @@ struct process uintptr_t kernel_stack; size_t kernel_stack_size; + /// Terminate this process. + /// \arg code The return code to exit with. + void exit(unsigned code); + /// Copy this process. /// \arg in_rsp The RSP of the calling process /// \returns Returns the child's pid to the parent, and diff --git a/src/kernel/syscall.cpp b/src/kernel/syscall.cpp index dee61db..1da14f3 100644 --- a/src/kernel/syscall.cpp +++ b/src/kernel/syscall.cpp @@ -127,12 +127,18 @@ syscall_dispatch(uintptr_t return_rsp, cpu_state ®s) cons->set_color(); pid_t pid = p->fork(return_rsp); - if (pid == scheduler::get().current()->pid) - pid = 0; regs.rax = pid; } break; + case syscall::exit: + cons->set_color(11); + cons->printf("\nProcess %u: Received EXIT syscall\n", p->pid); + cons->set_color(); + p->exit(regs.rdi); + return_rsp = s.schedule(return_rsp); + break; + default: cons->set_color(9); cons->printf("\nReceived unknown syscall: %02x\n", call); diff --git a/src/kernel/syscall.h b/src/kernel/syscall.h index 7de9b22..fb2f08e 100644 --- a/src/kernel/syscall.h +++ b/src/kernel/syscall.h @@ -15,6 +15,7 @@ enum class syscall : uint64_t send = 0x0006, receive = 0x0007, fork = 0x0008, + exit = 0x0009, last_syscall };