diff --git a/kernel/Makefile b/kernel/Makefile index 7bedae7..23c3529 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -11,6 +11,7 @@ CFLAGS += -I. \ -I$(ROOT)/std/include \ -I./std \ -I./flanterm/src \ + -I$(ROOT)/share \ -DPRINTF_INCLUDE_CONFIG_H=1 \ -DLFS_NO_ASSERT \ -DLFS_NO_DEBUG \ diff --git a/kernel/fs/portlfs/portlfs.c b/kernel/fs/portlfs/portlfs.c index a789c51..743f809 100644 --- a/kernel/fs/portlfs/portlfs.c +++ b/kernel/fs/portlfs/portlfs.c @@ -2,7 +2,7 @@ #include #include "fs/littlefs/lfs.h" #include "vfs/vfs.h" -#include "errors.h" +#include "hdrs/errors.h" #include "kprintf.h" #include "dlmalloc/malloc.h" #include "hal/hal.h" diff --git a/kernel/hal/x86_64/intr.c b/kernel/hal/x86_64/intr.c index 29df735..4fab54a 100644 --- a/kernel/hal/x86_64/intr.c +++ b/kernel/hal/x86_64/intr.c @@ -12,7 +12,7 @@ #include "pit.h" #include "proc/proc.h" #include "syscall/syscall.h" -#include "errors.h" +#include "hdrs/errors.h" void hal_intr_disable(void) { asm volatile("cli"); @@ -165,7 +165,7 @@ void hal_syscalldispatch(IntrStackFrame *frame) { int32_t ret = fn(frame->regs.rdi, frame->regs.rsi, frame->regs.rdx, frame->regs.r10, frame->regs.r8, frame->regs.r9); - if (sysnum == SYS_QUITPROC) { + if (ret == E_DOSCHEDULING) { proc_sched((void *)frame); } frame->regs.rax = *(uint64_t *)&ret; diff --git a/kernel/proc/proc.c b/kernel/proc/proc.c index c0081ab..6a86fc2 100644 --- a/kernel/proc/proc.c +++ b/kernel/proc/proc.c @@ -9,7 +9,7 @@ #include "util/util.h" #include "kprintf.h" #include "elf.h" -#include "errors.h" +#include "hdrs/errors.h" #include "vfs/vfs.h" #include "bootinfo/bootinfo.h" diff --git a/kernel/proc/proc.h b/kernel/proc/proc.h index 1245896..1f3acee 100644 --- a/kernel/proc/proc.h +++ b/kernel/proc/proc.h @@ -50,6 +50,7 @@ Proc *proc_spawnkern(void (*ent)(void), char *name); Proc *proc_spawnuser(char *mountpoint, char *path); void proc_sched(void *cpustate); void proc_killself(void); +void proc_kill(Proc *proc); #define PROC_DIE() \ do { \ diff --git a/kernel/storedev/ramsd.c b/kernel/storedev/ramsd.c index cadfa3c..531bc0d 100644 --- a/kernel/storedev/ramsd.c +++ b/kernel/storedev/ramsd.c @@ -1,7 +1,7 @@ #include #include #include "spinlock/spinlock.h" -#include "errors.h" +#include "hdrs/errors.h" #include "dlmalloc/malloc.h" #include "ramsd.h" #include "storedev.h" diff --git a/kernel/storedev/storedev.c b/kernel/storedev/storedev.c index 57ce702..3dd52c4 100644 --- a/kernel/storedev/storedev.c +++ b/kernel/storedev/storedev.c @@ -3,7 +3,7 @@ #include "storedev.h" #include "spinlock/spinlock.h" #include "kprintf.h" -#include "errors.h" +#include "hdrs/errors.h" #include "dlmalloc/malloc.h" #include "ramsd.h" #include "util/util.h" diff --git a/kernel/syscall/processctl.c b/kernel/syscall/processctl.c new file mode 100644 index 0000000..4d8d087 --- /dev/null +++ b/kernel/syscall/processctl.c @@ -0,0 +1,53 @@ +#include +#include "syscall.h" +#include "proc/proc.h" +#include "spinlock/spinlock.h" +#include "hdrs/errors.h" +#include "util/util.h" + +#define PID_SELF_MAGIC 0x5E1F + +enum { + PCTL_KILL = 0, +}; + +typedef struct { + +} ProcessCtl; + +int32_t SYSCALL3(sys_processctl, pid1, cmd1, optsptr1) { + uint64_t pid = pid1; + uint64_t cmd = cmd1; + ProcessCtl *pctl = (ProcessCtl *)(void *)optsptr1; + int32_t ret = E_OK; + + spinlock_acquire(&PROCS.spinlock); + + if (pid == PID_SELF_MAGIC) { + pid = PROCS.current->pid; + } + + Proc *proc = NULL; + LL_FINDPROP(PROCS.procs, proc, pid, pid); + + if (proc == NULL) { + ret = E_INVALIDARGUMENT; + goto done; + } + + switch (cmd) { + case PCTL_KILL: { + proc_kill(proc); + ret = E_DOSCHEDULING; + goto done; + } break; + default: { + ret = E_INVALIDARGUMENT; + goto done; + } break; + } + +done: + spinlock_release(&PROCS.spinlock); + return ret; +} diff --git a/kernel/syscall/processctl.h b/kernel/syscall/processctl.h new file mode 100644 index 0000000..2019498 --- /dev/null +++ b/kernel/syscall/processctl.h @@ -0,0 +1,9 @@ +#ifndef SYSCALL_PROCESSCTL_H_ +#define SYSCALL_PROCESSCTL_H_ + +#include +#include "syscall.h" + +int32_t SYSCALL3(sys_processctl, pid1, cmd1, optsptr1); + +#endif // SYSCALL_PROCESSCTL_H_ diff --git a/kernel/syscall/syscall.c b/kernel/syscall/syscall.c index cc01f2f..5288798 100644 --- a/kernel/syscall/syscall.c +++ b/kernel/syscall/syscall.c @@ -1,9 +1,9 @@ #include #include "syscall.h" -#include "errors.h" -#include "dlmalloc/malloc.h" +#include "hdrs/errors.h" #include "kprintf.h" -#include "proc/proc.h" +#include "processctl.h" +#include "hdrs/syscall.h" int32_t SYSCALL1(sys_debugprint, string) { char *p = (char *)string; @@ -11,12 +11,7 @@ int32_t SYSCALL1(sys_debugprint, string) { return E_OK; } -int32_t SYSCALL0(sys_quitproc) { - proc_killself(); - return E_OK; -} - SyscallFn SYSCALL_TABLE[SYSCALLS_MAX] = { [SYS_DEBUGPRINT] = &sys_debugprint, - [SYS_QUITPROC] = &sys_quitproc, + [SYS_PROCESSCTL] = &sys_processctl, }; diff --git a/kernel/syscall/syscall.h b/kernel/syscall/syscall.h index 2079533..4fa8903 100644 --- a/kernel/syscall/syscall.h +++ b/kernel/syscall/syscall.h @@ -69,11 +69,6 @@ uint64_t arg6 \ ) -enum { - SYS_DEBUGPRINT = 1, - SYS_QUITPROC = 2, -}; - typedef int32_t (*SyscallFn)(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t); extern SyscallFn SYSCALL_TABLE[SYSCALLS_MAX]; diff --git a/kernel/vfs/vfs.c b/kernel/vfs/vfs.c index a275c07..d08299d 100644 --- a/kernel/vfs/vfs.c +++ b/kernel/vfs/vfs.c @@ -7,7 +7,7 @@ #include "util/util.h" #include "hshtb.h" #include "assert.h" -#include "errors.h" +#include "hdrs/errors.h" #include "fs/portlfs/portlfs.h" #include "storedev/storedev.h" #include "baseimg/baseimg.h" diff --git a/kernel/errors.h b/share/hdrs/errors.h similarity index 85% rename from kernel/errors.h rename to share/hdrs/errors.h index da810c6..ded8db8 100644 --- a/kernel/errors.h +++ b/share/hdrs/errors.h @@ -11,6 +11,8 @@ enum { E_TODO = -6, E_BADIO = -7, E_BADSYSCALL = -8, + E_DOSCHEDULING = -9, + E_INVALIDARGUMENT = -10, }; #endif // ERRORS_H_ diff --git a/share/hdrs/syscall.h b/share/hdrs/syscall.h new file mode 100644 index 0000000..5583673 --- /dev/null +++ b/share/hdrs/syscall.h @@ -0,0 +1,9 @@ +#ifndef SHARE_HDRS_SYSCALL_H_ +#define SHARE_HDRS_SYSCALL_H_ + +enum { + SYS_DEBUGPRINT = 1, + SYS_PROCESSCTL = 2, +}; + +#endif // SHARE_HDRS_SYSCALL_H_ diff --git a/ulib/Makefile b/ulib/Makefile index b8c7573..33cbef5 100644 --- a/ulib/Makefile +++ b/ulib/Makefile @@ -11,7 +11,7 @@ SRCFILES := $(call GRABSRC, \ system \ ) -CFLAGS += -isystem $(ROOT)/ulib -isystem $(ROOT)/std/include +CFLAGS += -isystem $(ROOT)/share/hdrs -isystem $(ROOT)/ulib -isystem $(ROOT)/std/include ASFILES := $(call GET_ASFILES, $(SRCFILES)) CFILES := $(call GET_CFILES, $(SRCFILES)) diff --git a/ulib/_start.S b/ulib/_start.S index c09c7eb..fcea48a 100644 --- a/ulib/_start.S +++ b/ulib/_start.S @@ -6,5 +6,7 @@ _start: call _premain - mov $SYS_QUITPROC, %rax + movq $2, %rax // sys processctl + movq $0x5E1F, %rdi // self magic num + movq $0, %rsi // kill cmd int $0x80 diff --git a/ulib/libulib.a b/ulib/libulib.a index da94968..2a45b30 100644 Binary files a/ulib/libulib.a and b/ulib/libulib.a differ diff --git a/ulib/syscall/syscall.h b/ulib/syscall/syscall.h index 1c4ccdd..3708321 100644 --- a/ulib/syscall/syscall.h +++ b/ulib/syscall/syscall.h @@ -1,9 +1,6 @@ #ifndef ULIB_SYSCALL_SYSCALL_H_ #define ULIB_SYSCALL_SYSCALL_H_ -#define SYS_DEBUGPRINT 1 -#define SYS_QUITPROC 2 - #if !defined(__ASSEMBLER__) uint64_t syscall(uint64_t num, uint64_t arg1, uint64_t arg2, diff --git a/ulib/system/system.c b/ulib/system/system.c index 7d44bcf..ba176bb 100644 --- a/ulib/system/system.c +++ b/ulib/system/system.c @@ -1,9 +1,13 @@ #include #include #include - +#include void sys_debugprint(const char *string) { syscall(SYS_DEBUGPRINT, (uint64_t)string, 0, 0, 0, 0, 0); } +int32_t sys_processctl(uint64_t pid, uint64_t cmd, void *extra) { + return syscall(SYS_PROCESSCTL, pid, cmd, (uint64_t)extra, 0, 0, 0); +} + diff --git a/ulib/system/system.h b/ulib/system/system.h index 30149b0..bd63bc6 100644 --- a/ulib/system/system.h +++ b/ulib/system/system.h @@ -1,6 +1,9 @@ #ifndef ULIB_SYSTEM_SYSTEM_H_ #define ULIB_SYSTEM_SYSTEM_H_ +#include + void sys_debugprint(const char *string); +int32_t sys_processctl(uint64_t pid, uint64_t cmd, void *extra); #endif // ULIB_SYSTEM_SYSTEM_H_