Skip to content

Commit

Permalink
Streamline all sys_xxx() functions
Browse files Browse the repository at this point in the history
In preparation to generate most of what's in syscalls.c today, make all
sys_xxx() functions to adhere to the same pattern: extract parameters
from trap_frame if applicable, and tail-call proc_xxx().

In some cases, like poweroff() and gpio_do_syscall() that makes no sense
at all, but let's have that this way for easier upcoming code generation.
  • Loading branch information
rtfb committed Apr 21, 2024
1 parent e08339a commit 3692188
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 38 deletions.
12 changes: 9 additions & 3 deletions include/proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ uint32_t proc_fork();
// proc_exit implements the exit syscall. It will remove the process from the
// table, release all the resources taken by the process and call the
// scheduler.
void proc_exit();
regsize_t proc_exit();

// proc_execv implements the exec system call. The 'v' suffix means the
// arguments are passed vectorized, in an array of pointers to strings. The
Expand Down Expand Up @@ -239,8 +239,14 @@ int32_t proc_dup(uint32_t fd);

uint32_t proc_lsdir(char const *dir, dirent_t *dirents, regsize_t size);

void* proc_pgalloc();
void proc_pgfree(void *page);
regsize_t proc_pgalloc();
regsize_t proc_pgfree(void *page);

regsize_t proc_getpid();
regsize_t proc_pipe(uint32_t *fds);
regsize_t proc_sysinfo();
regsize_t proc_gpio(uint32_t pin_num, uint32_t enable, uint32_t value);
regsize_t proc_restart();

uint32_t proc_detach();
int32_t proc_isopen(int32_t fd);
Expand Down
4 changes: 2 additions & 2 deletions include/syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ typedef struct wait_cond_s {

// TODO: unify the return values of syscalls

void sys_restart();
void sys_exit();
regsize_t sys_restart();
regsize_t sys_exit();
uint32_t sys_fork();
int32_t sys_read();
int32_t sys_write();
Expand Down
46 changes: 42 additions & 4 deletions src/proc.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "bakedinfs.h"
#include "drivers/uart/uart.h"
#include "errno.h"
#include "gpio.h"
#include "kernel.h"
#include "mem.h"
#include "pagealloc.h"
#include "pipe.h"
#include "pmp.h"
#include "proc.h"
#include "programs.h"
Expand Down Expand Up @@ -484,7 +486,7 @@ void patch_proc_sp(process_t *proc, regsize_t sp) {
proc->ctx.regs[REG_SP] = sp;
}

void proc_exit() {
regsize_t proc_exit() {
process_t* proc = myproc();
release_page(proc->stack_page);
#if CONFIG_MMU
Expand All @@ -510,6 +512,7 @@ void proc_exit() {
proc_table.num_procs--;
release(&proc_table.lock);
swtch(&proc->ctx, &thiscpu()->context);
return 0;
}

// check_exited_children iterates over process table looking for zombie
Expand Down Expand Up @@ -802,7 +805,7 @@ int32_t proc_dup(uint32_t fd) {
return newfd;
}

void* proc_pgalloc() {
regsize_t proc_pgalloc() {
process_t* proc = myproc();
void *page = allocate_page("user", proc->pid, PAGE_USERMEM);
if (!page) {
Expand All @@ -811,13 +814,14 @@ void* proc_pgalloc() {
#if CONFIG_MMU
map_page_sv39(proc->upagetable, page, USR_VIRT(page), PERM_UDATA, proc->pid);
#endif
return (void*)USR_VIRT(page);
return USR_VIRT(page);
}

void proc_pgfree(void *page) {
regsize_t proc_pgfree(void *page) {
process_t* proc = myproc();
page = va2pa(proc->upagetable, page);
release_page(page);
return 0;
}

// proc_detach detaches the current process from its parent. The parent will no
Expand Down Expand Up @@ -907,3 +911,37 @@ uint32_t proc_lsdir(char const *dir, dirent_t *dirents, regsize_t size) {
}
return status;
}

regsize_t proc_getpid() {
return myproc()->pid;
}

regsize_t proc_pipe(uint32_t *fds) {
return pipe_open(fds);
}

regsize_t proc_sysinfo() {
sysinfo_t* info = (sysinfo_t*)trap_frame.regs[REG_A0];
process_t *proc = myproc();
info = va2pa(proc->upagetable, info);
acquire(&proc_table.lock);
info->procs = proc_table.num_procs;
release(&proc_table.lock);

acquire(&paged_memory.lock);
info->totalram = paged_memory.num_pages;
info->freeram = count_free_pages();
info->unclaimed_start = paged_memory.unclaimed_start;
info->unclaimed_end = paged_memory.unclaimed_end;
release(&paged_memory.lock);
return 0;
}

regsize_t proc_gpio(uint32_t pin_num, uint32_t enable, uint32_t value) {
return gpio_do_syscall(pin_num, enable, value);
}

regsize_t proc_restart() {
poweroff();
return 0;
}
39 changes: 10 additions & 29 deletions src/syscalls.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// Entry points for all the syscalls here

#include "gpio.h"
#include "pagealloc.h"
#include "pipe.h"
#include "proc.h"
#include "syscalls.h"
#include "vm.h"

// for fun let's pretend syscall table is kinda like 32bit Linux on x86,
// /usr/include/asm/unistd_32.h: __NR_restart_syscall 0, __NR_exit 1, _NR_fork 2, __NR_read 3, __NR_write 4
Expand Down Expand Up @@ -39,12 +35,12 @@ void *syscall_vector[] _text = {
};
int syscall_vector_len _text = SYS_NR_lsdir;

void sys_restart() {
poweroff();
regsize_t sys_restart() {
return proc_restart();
}

void sys_exit() {
proc_exit();
regsize_t sys_exit() {
return proc_exit();
}

uint32_t sys_fork() {
Expand Down Expand Up @@ -88,7 +84,7 @@ uint32_t sys_execv() {
}

uint32_t sys_getpid() {
return myproc()->pid;
return proc_getpid();
}

uint32_t sys_dup() {
Expand All @@ -98,25 +94,11 @@ uint32_t sys_dup() {

uint32_t sys_pipe() {
uint32_t *fds = (uint32_t*)trap_frame.regs[REG_A0];
return pipe_open(fds);
return proc_pipe(fds);
}

// TODO: move implementation to proc layer
uint32_t sys_sysinfo() {
sysinfo_t* info = (sysinfo_t*)trap_frame.regs[REG_A0];
process_t *proc = myproc();
info = va2pa(proc->upagetable, info);
acquire(&proc_table.lock);
info->procs = proc_table.num_procs;
release(&proc_table.lock);

acquire(&paged_memory.lock);
info->totalram = paged_memory.num_pages;
info->freeram = count_free_pages();
info->unclaimed_start = paged_memory.unclaimed_start;
info->unclaimed_end = paged_memory.unclaimed_end;
release(&paged_memory.lock);
return 0;
return proc_sysinfo();
}

uint32_t sys_sleep() {
Expand All @@ -137,20 +119,19 @@ uint32_t sys_pinfo() {
}

regsize_t sys_pgalloc() {
return (regsize_t)proc_pgalloc();
return proc_pgalloc();
}

regsize_t sys_pgfree() {
void *page = (void*)trap_frame.regs[REG_A0];
proc_pgfree(page);
return 0;
return proc_pgfree(page);
}

uint32_t sys_gpio() {
uint32_t pin_num = (uint32_t)trap_frame.regs[REG_A0];
uint32_t enable = (uint32_t)trap_frame.regs[REG_A1];
uint32_t value = (uint32_t)trap_frame.regs[REG_A2];
return gpio_do_syscall(pin_num, enable, value);
return proc_gpio(pin_num, enable, value);
}

uint32_t sys_detach() {
Expand Down

0 comments on commit 3692188

Please sign in to comment.