From a12e50e5a7dae52ed8bd5a24cc6576371e20985b Mon Sep 17 00:00:00 2001 From: Kimplul Date: Mon, 27 Dec 2021 20:40:44 +0200 Subject: Started work on syscalls --- Makefile | 2 +- TODO.txt | 3 +++ arch/riscv/include/tcb.h | 9 +++++++++ arch/riscv/kernel/cpu.c | 6 ++++++ arch/riscv/kernel/main.c | 8 +------- common/tcb.c | 16 ++++++++++++++++ common/uapi/conf.c | 15 +++++++++++++++ common/uapi/ipc.c | 28 ++++++++++++++++++++++++++++ common/uapi/mem.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ common/uapi/proc.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ common/vmem.c | 12 +++++++++--- config.h | 1 + include/apos/cpu.h | 7 +++++++ include/apos/syscalls.h | 36 ++++++++++++++++++++++++++++++++++++ include/apos/tcb.h | 9 ++++++--- include/apos/uapi.h | 29 +++++++++++++++++++++++++++++ include/apos/utils.h | 2 ++ include/apos/vmem.h | 1 + 18 files changed, 262 insertions(+), 14 deletions(-) create mode 100644 arch/riscv/include/tcb.h create mode 100644 arch/riscv/kernel/cpu.c create mode 100644 common/uapi/conf.c create mode 100644 common/uapi/ipc.c create mode 100644 common/uapi/mem.c create mode 100644 common/uapi/proc.c create mode 100644 include/apos/cpu.h create mode 100644 include/apos/syscalls.h create mode 100644 include/apos/uapi.h diff --git a/Makefile b/Makefile index 939e5e1..adcb405 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ OBJCOPY ?= objcopy # This makes sure .bss is loaded into the binary OBJCOPY_FLAGS ?= -Obinary --set-section-flags .bss=alloc,load,contents -KERNEL_SOURCES != echo common/*.c lib/*.c +KERNEL_SOURCES != echo common/*.c common/uapi/*.c lib/*.c CLEANUP := build deps.mk kernel.* init.* apos.bin CLEANUP_CMD := INIT_SOURCES := diff --git a/TODO.txt b/TODO.txt index dd2898a..a552121 100644 --- a/TODO.txt +++ b/TODO.txt @@ -16,3 +16,6 @@ acceptable by Unix standards. + Alignment can maybe still be used, though more in the sense of using larger pages? YES, CHECK common/vmem.c:map_fill_region! Should be implemented there as well ++ check elf.c "skip while testing" comment, remove when sufficiently pleased +with the system. Christ, what a terrible way to develop software :D ++ Start working on syscalls etc. diff --git a/arch/riscv/include/tcb.h b/arch/riscv/include/tcb.h new file mode 100644 index 0000000..d1e0717 --- /dev/null +++ b/arch/riscv/include/tcb.h @@ -0,0 +1,9 @@ +#ifndef ARCH_RISCV_TCB_H +#define ARCH_RISCV_TCB_H + +struct arch_tcbd { + /* empty for now, but should probably be filled with stuff like register + * saving of something */ +}; + +#endif /* ARCH_RISCV_TCB_H */ diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c new file mode 100644 index 0000000..34b7a32 --- /dev/null +++ b/arch/riscv/kernel/cpu.c @@ -0,0 +1,6 @@ +#include + +unsigned cpu_id() +{ + return 0; /* VERY MUCH TEMP */ +} diff --git a/arch/riscv/kernel/main.c b/arch/riscv/kernel/main.c index 1964514..e519697 100644 --- a/arch/riscv/kernel/main.c +++ b/arch/riscv/kernel/main.c @@ -144,7 +144,7 @@ static struct pm_layout_t setup_pmem(void *fdt) /* mark init stack, at the moment always mapped to 2M */ mark_used(PM_STACK_BASE, MM_MPAGE); - /* mark kernel, at the moment it is always mapped to a 2M partition */ + /* mark kernel, at the moment with a 2M partition */ mark_used(PM_KERN, MM_MPAGE); /* mark fdt and initrd */ @@ -269,12 +269,6 @@ static void init_proc(void *fdt, struct vm_branch_t *b) __asm__("mv " QUOTE(reg) ", %0" :: "rK" (reg) : );\ } -static void update_stack() -{ - __va_reg(sp); - __va_reg(gp); -} - void __main main(void *fdt) { __va_reg(sp); diff --git a/common/tcb.c b/common/tcb.c index 5b00524..8a1e95e 100644 --- a/common/tcb.c +++ b/common/tcb.c @@ -3,6 +3,7 @@ #include static struct sp_root t_root = (struct sp_root){0}; +static struct tcb *__tcb_cache[MAX_CPUS] = {0}; #define tcb_container(x) \ container_of(x, struct tcb, sp_n) @@ -53,3 +54,18 @@ struct tcb *threads_find(id_t tid) return 0; } + +struct tcb *get_tcb(id_t pid) +{ + /* TODO: figure out how exactly thread IDs are related to process IDs, + * and figure out mapping between them. */ + return 0; +} + +struct tcb *cur_tcb() +{ + /* TODO: probably keep an array of processor ID's somewhere where each + * ID has a corresponding 'currently executing thread ID' field, and + * reutrn that. */ + return __tcb_cache[cpu_id()]; +} diff --git a/common/uapi/conf.c b/common/uapi/conf.c new file mode 100644 index 0000000..01c4ada --- /dev/null +++ b/common/uapi/conf.c @@ -0,0 +1,15 @@ +#include + +vm_t sys_conf(vm_t param, vm_t val, vm_t u0, vm_t u1) +{ + UNUSED(u0); UNUSED(u1); + /* no parameters supported atm */ + return 0; +} + +vm_t sys_poweroff(vm_t type, vm_t u0, vm_t u1, vm_t u2) +{ + UNUSED(u0); UNUSED(u1); UNUSED(u2); + /* powering off not supported yet, you're stuck here >:D */ + return 0; +} diff --git a/common/uapi/ipc.c b/common/uapi/ipc.c new file mode 100644 index 0000000..5abbaeb --- /dev/null +++ b/common/uapi/ipc.c @@ -0,0 +1,28 @@ +#include +#include + +vm_t sys_ipc_server(vm_t callback, vm_t u0, vm_t u1, vm_t u2) +{ + UNUSED(u0); UNUSED(u1); UNUSED(u2); + struct tcb *r = cur_tcb(); + if(r->callback) /* server can't be reinitialized */ + return 1; + + r->callback = callback; + return 0; +} + +vm_t sys_ipc_req(vm_t pid, vm_t d0, vm_t d1, vm_t d2) +{ + struct tcb *t = get_tcb(pid); + /* something like jump_to_callback(t, d0, d1, d2) */ + return 0; +} + +vm_t sys_ipc_resp(vm_t pid, vm_t ret, vm_t u0, vm_t u1) +{ + UNUSED(u0); UNUSED(u1); + struct tcb *r = get_tcb(pid); + /* something like return_from_callback(t, r) */ + return 0; /* oh yeah probably unreachable? */ +} diff --git a/common/uapi/mem.c b/common/uapi/mem.c new file mode 100644 index 0000000..34ddda2 --- /dev/null +++ b/common/uapi/mem.c @@ -0,0 +1,45 @@ +#include +#include +#include + +vm_t sys_req_mem(vm_t size, vm_t flags, vm_t u0, vm_t u1) +{ + UNUSED(u0); UNUSED(u1); + /* proc_tcb should give the tcb of the TID currently running */ + struct tcb *r = cur_tcb(); + return alloc_uvmem(r, size, flags); +} + +vm_t sys_req_fixmem(vm_t start, vm_t size, vm_t flags, vm_t u0) +{ + UNUSED(u0); + struct tcb *r = cur_tcb(); + return alloc_fixed_uvmem(r, start, size, flags); +} + +vm_t sys_free_mem(vm_t start, vm_t u0, vm_t u1, vm_t u2) +{ + UNUSED(u0); UNUSED(u1); UNUSED(u2); + struct tcb *r = cur_tcb(); + free_uvmem(r, start); + return 0; +} + +vm_t sys_req_pmem(vm_t paddr, vm_t size, vm_t flags, vm_t u0) +{ + UNUSED(u0); + /* this will require some pondering, but essentially this syscall should + * only be used for device access, so any addresses requested should be + * outside the RAM area, and I'll probably have to implement some method + * that keeps track of used regions outside of RAM. We'll see. + */ + return 0; +} + +vm_t sys_req_sharedmem(vm_t pid, vm_t start, vm_t size, vm_t flags) +{ + /* take memory in PID's vaddr and map it somewhere in our own memory + * region. + */ + return 0; +} diff --git a/common/uapi/proc.c b/common/uapi/proc.c new file mode 100644 index 0000000..dd05bad --- /dev/null +++ b/common/uapi/proc.c @@ -0,0 +1,47 @@ +#include + +/* Not entirely sure how I should handle forks/execs etc, mostly whether I + * should allow forks/execs to be called directly or only though the process + * manager. Probably though the process manager, although that will add in a + * slight bit of delay. + * + * I suppose I could add in a runtime parameter + * that would allow them to be called directly, and then the process manager + * would have to periodically ask the kernel about all threads it is aware of + * via sys_sync. Dunno. + */ +vm_t sys_fork(vm_t pid, vm_t u0, vm_t u1, vm_t u2) +{ + UNUSED(u0); UNUSED(u1); UNUSED(u2); + /* TODO: create new thread in the same process family */ + return 0; +} + +vm_t sys_exec(vm_t pid, vm_t bin, vm_t argc, vm_t argv) +{ + /* TODO: execute new process */ + return 0; +} + +vm_t sys_signal(vm_t pid, vm_t signal, vm_t u0, vm_t u1) +{ + UNUSED(u0); UNUSED(u1); + /* TODO: signals? */ + return 0; +} + +vm_t sys_switch(vm_t pid, vm_t u0, vm_t u1, vm_t u2) +{ + UNUSED(u0); UNUSED(u1); UNUSED(u2); + /* TODO: switch to process */ + return 0; +} + +vm_t sys_sync(vm_t buf, vm_t size, vm_t u0, vm_t u1) +{ + UNUSED(u0); UNUSED(u1); + /* check that only the process manager can use this syscall, otherwise + * just dump process info into the buffer (I guess, not sure if this + * will be quite required */ + return 0; +} diff --git a/common/vmem.c b/common/vmem.c index 229963c..ffe9b32 100644 --- a/common/vmem.c +++ b/common/vmem.c @@ -449,10 +449,16 @@ vm_t map_fill_region(struct vm_branch_t *b, vm_t start, size_t bytes, uint8_t fl return start; } -vm_t alloc_uvmem(struct tcb *t, size_t s, uint8_t flags) +vm_t alloc_uvmem(struct tcb *t, size_t size, uint8_t flags) { - vm_t v = alloc_region(&t->sp_r, s, &s); - return map_fill_region(t->b_r, v, s, flags); + vm_t v = alloc_region(&t->sp_r, size, &size); + return map_fill_region(t->b_r, v, size, flags); +} + +vm_t alloc_fixed_uvmem(struct tcb *t, vm_t start, size_t size, uint8_t flags) +{ + vm_t v = alloc_fixed_region(&t->sp_r, start, size, &size); + return map_fill_region(t->b_r, v, size, flags); } void free_uvmem(struct tcb *t, vm_t a) diff --git a/config.h b/config.h index e69de29..a5f9562 100644 --- a/config.h +++ b/config.h @@ -0,0 +1 @@ +#define MAX_CPUS 16 diff --git a/include/apos/cpu.h b/include/apos/cpu.h new file mode 100644 index 0000000..a55fbad --- /dev/null +++ b/include/apos/cpu.h @@ -0,0 +1,7 @@ +#ifndef APOS_CPU_H +#define APOS_CPU_H + +unsigned cpu_id(); +/* TODO: add more cpu handling functions */ + +#endif /* APOS_CPU_H */ diff --git a/include/apos/syscalls.h b/include/apos/syscalls.h new file mode 100644 index 0000000..d2427cf --- /dev/null +++ b/include/apos/syscalls.h @@ -0,0 +1,36 @@ +#ifndef APOS_SYSCALLS_H +#define APOS_SYSCALLS_H + +/* enum for now, possibly macros in the future once I get an approximate idea of + * which syscalls are necessary etc. */ +enum { + /* memory management */ + SYS_REQ_MEM, /* request memory from anywhere */ + SYS_REQ_PMEM, /* request physical address */ + SYS_REQ_FIXMEM, /* request memory at fixed address */ + SYS_FREE_MEM, /* free memory */ + + /* IPC */ + /* I really should try to find my notes about the server/client + * structure of the OS, but these following syscalls are probably + * required */ + SYS_IPC_SERVER, /* inform kernel that process should be treated as a server */ + SYS_IPC_REQ, /* IPC request to server */ + SYS_IPC_RESP, /* IPC response from server */ + + /* process management */ + SYS_FORK, /* duplicate process */ + SYS_EXEC, /* execute new binary in process space */ + SYS_SIGNAL, /* send signal to process (kill etc.) */ + SYS_SWITCH, /* switch running process */ + SYS_SYNC, /* used by init process to synchronize running processes, maybe not used */ + + /* kernel management */ + SYS_CONF, /* config system parameters (stack size etc.) */ + SYS_POWEROFF, /* shutdown/reboot etc. */ +} + +/* function declarations should be somewhere else, this file could be used in + * userspace applications as well */ + +#endif /* APOS_SYSCALLS_H */ diff --git a/include/apos/tcb.h b/include/apos/tcb.h index f987b43..714d750 100644 --- a/include/apos/tcb.h +++ b/include/apos/tcb.h @@ -4,6 +4,7 @@ struct tcb; struct sp_reg_root; +#include #include #include #include @@ -18,18 +19,20 @@ struct sp_reg_root { struct tcb { struct sp_node sp_n; + struct sp_reg_root sp_r; + struct arch_tcbd tcbd; id_t pid; id_t tid; + vm_t callback; vm_t stack; - vm_t heap; - vm_t bin; struct vm_branch_t *b_r; - struct sp_reg_root sp_r; }; void threads_insert(struct tcb *t); +struct tcb *cur_tcb(); +struct tcb *get_tcb(id_t tid); #endif /* APOS_TCB_H */ diff --git a/include/apos/uapi.h b/include/apos/uapi.h new file mode 100644 index 0000000..74fd846 --- /dev/null +++ b/include/apos/uapi.h @@ -0,0 +1,29 @@ +#ifndef APOS_UAPI_H +#define APOS_UAPI_H + +#include + +/* syscall function type, let's start with four arguments and see where that + * goes */ +typedef vm_t (*sys_t)(vm_t a0, vm_t a1, vm_t a2, vm_t a3); + +vm_t sys_req_mem(vm_t size, vm_t flags, vm_t u0, vm_t u1); +vm_t sys_req_pmem(vm_t paddr, vm_t size, vm_t flags, vm_t u0); +vm_t sys_req_fixmem(vm_t start, vm_t size, vm_t flags, vm_t u0); +vm_t sys_req_sharedmem(vm_t pid, vm_t start, vm_t size, vm_t flags); +vm_t sys_free_mem(vm_t start, vm_t u0, vm_t u1, vm_t u2); + +vm_t sys_ipc_server(vm_t callback, vm_t u0, vm_t u1, vm_t u2); +vm_t sys_ipc_req(vm_t tgt_pid, vm_t d0, vm_t d1, vm_t d2); +vm_t sys_ipc_resp(vm_t tgt_pid, vm_t ret, vm_t u0, vm_t u1); + +vm_t sys_fork(vm_t pid, vm_t u1, vm_t u2, vm_t u3); +vm_t sys_exec(vm_t pid, vm_t bin, vm_t argc, vm_t argv); +vm_t sys_signal(vm_t pid, vm_t signal, vm_t u0, vm_t u1); +vm_t sys_switch(vm_t pid, vm_t u0, vm_t u1, vm_t u2); +vm_t sys_sync(vm_t buf, vm_t size, vm_t u1, vm_t u2); + +vm_t sys_conf(vm_t param, vm_t val, vm_t u0, vm_t u1); +vm_t sys_poweroff(vm_t type, vm_t u0, vm_t u1, vm_t u2); + +#endif /* APOS_UAPI_H */ diff --git a/include/apos/utils.h b/include/apos/utils.h index eff473f..c2cff2c 100644 --- a/include/apos/utils.h +++ b/include/apos/utils.h @@ -19,6 +19,8 @@ #define QUOTE2(x) #x #define QUOTE(x) QUOTE2(x) +#define UNUSED(x) ((void)(x)) + #include #if __has_builtin(__builtin_offsetof) diff --git a/include/apos/vmem.h b/include/apos/vmem.h index b7522d2..93d2ed7 100644 --- a/include/apos/vmem.h +++ b/include/apos/vmem.h @@ -54,6 +54,7 @@ vm_t alloc_fixed_region(struct sp_reg_root *r, vm_t start, size_t size, size_t * void free_region(struct sp_reg_root *r, vm_t start); vm_t alloc_uvmem(struct tcb *r, size_t size, uint8_t flags); +vm_t alloc_fixed_uvmem(struct tcb *r, vm_t start, size_t size, uint8_t flags); void free_uvmem(struct tcb *r, vm_t a); vm_t map_fill_region(struct vm_branch_t *b, vm_t start, size_t bytes, uint8_t flags); -- cgit v1.3