From 7967a9ab13214543041e4408086668b4db8d3e62 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Mon, 18 Apr 2022 21:10:57 +0300 Subject: start working on timer subsys --- include/apos/sp_tree.h | 10 ++--- include/apos/syscalls.h | 6 ++- include/apos/timer.h | 47 +++++++++++++++++++++++ include/apos/uapi.h | 99 +++++++++++++++++++++++++++++++++++++++---------- include/arch/timer.h | 15 ++++++++ 5 files changed, 151 insertions(+), 26 deletions(-) create mode 100644 include/apos/timer.h create mode 100644 include/arch/timer.h (limited to 'include') diff --git a/include/apos/sp_tree.h b/include/apos/sp_tree.h index 1c1525f..fe02e91 100644 --- a/include/apos/sp_tree.h +++ b/include/apos/sp_tree.h @@ -1,13 +1,13 @@ #ifndef SP_TREE_H #define SP_TREE_H -#define sp_root(r) (r.sp_r) -#define sp_left(n) (n->left) -#define sp_right(n) (n->right) +#define sp_root(r) ((r)->sp_r) +#define sp_left(n) ((n)->left) +#define sp_right(n) ((n)->right) #define sp_rparen(n) (sp_right(n)->parent) #define sp_lparen(n) (sp_left(n)->parent) -#define sp_paren(n) (n->parent) -#define sp_gparen(n) (n->parent->parent) +#define sp_paren(n) ((n)->parent) +#define sp_gparen(n) ((n)->parent->parent) #define sp_has_gparen(n) (sp_paren(n) && sp_gparen(n)) struct sp_node { diff --git a/include/apos/syscalls.h b/include/apos/syscalls.h index d2427cf..d9df89b 100644 --- a/include/apos/syscalls.h +++ b/include/apos/syscalls.h @@ -10,6 +10,10 @@ enum { SYS_REQ_FIXMEM, /* request memory at fixed address */ SYS_FREE_MEM, /* free memory */ + /* timers */ + SYS_REQ_TIMER, + SYS_FREE_TIMER, + /* IPC */ /* I really should try to find my notes about the server/client * structure of the OS, but these following syscalls are probably @@ -28,7 +32,7 @@ enum { /* 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 */ diff --git a/include/apos/timer.h b/include/apos/timer.h new file mode 100644 index 0000000..ea5bd03 --- /dev/null +++ b/include/apos/timer.h @@ -0,0 +1,47 @@ +#ifndef APOS_TIMER_H +#define APOS_TIMER_H + +#include + +typedef size_t ticks_t; +/* whichever time unit we're dealing with */ +typedef size_t tunit_t; + +struct timer { + id_t tid; + id_t cid; + + ticks_t ticks; +}; + +void init_timer(); + +/* set up timer interrupt ticks from now */ +id_t new_rel_timer(id_t tid, ticks_t ticks); +/* set up timer interrupt at ticks (from startup I suppose) */ +id_t new_abs_timer(id_t tid, ticks_t ticks); + +struct timer *newest_timer(); +struct timer *find_timer(id_t cid); +void remove_timer(struct timer *); + +ticks_t nsecs_to_ticks(tunit_t nsecs); + +static inline ticks_t usecs_to_ticks(tunit_t usecs) +{ + return nsecs_to_ticks(usecs * 1000); +} + +static inline ticks_t msecs_to_ticks(tunit_t msecs) +{ + return usecs_to_ticks(msecs * 1000); +} + +/* TODO: likely not a problem on 64bit systems, not sure how to handle situation on + * 32bit */ +static inline ticks_t secs_to_ticks(tunit_t secs) +{ + return msecs_to_ticks(secs * 1000); +} + +#endif /* APOS_TIMER_H */ diff --git a/include/apos/uapi.h b/include/apos/uapi.h index 74fd846..fb7dff7 100644 --- a/include/apos/uapi.h +++ b/include/apos/uapi.h @@ -1,29 +1,88 @@ #ifndef APOS_UAPI_H #define APOS_UAPI_H +#include #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); +typedef vm_t (*sys_t)(vm_t, vm_t, vm_t, vm_t); + +#define SYSCALL_DECLARE(name)\ + vm_t sys_##name(vm_t a, vm_t b, vm_t c, vm_t d); + +#define SYSCALL_DEFINE0(name)\ + static vm_t __##name();\ + vm_t sys_##name(vm_t a, vm_t b, vm_t c, vm_t d){ \ + UNUSED(a);\ + UNUSED(b);\ + UNUSED(c);\ + UNUSED(d);\ + return __##name();\ + }\ + static vm_t __##name + +#define SYSCALL_DEFINE1(name)\ + static vm_t __##name(vm_t);\ + vm_t sys_##name(vm_t a, vm_t b, vm_t c, vm_t d){ \ + UNUSED(b);\ + UNUSED(c);\ + UNUSED(d);\ + return __##name(a);\ + }\ + static vm_t __##name + +#define SYSCALL_DEFINE2(name)\ + static vm_t __##name(vm_t, vm_t);\ + vm_t sys_##name(vm_t a, vm_t b, vm_t c, vm_t d){ \ + UNUSED(c);\ + UNUSED(d);\ + return __##name(a, b);\ + }\ + static vm_t __##name + +#define SYSCALL_DEFINE3(name)\ + static vm_t __##name(vm_t, vm_t, vm_t);\ + vm_t sys_##name(vm_t a, vm_t b, vm_t c, vm_t d){ \ + UNUSED(d);\ + return __##name(a, b, c);\ + }\ + static vm_t __##name + +#define SYSCALL_DEFINE4(name)\ + static vm_t __##name(vm_t, vm_t, vm_t, vm_t);\ + vm_t sys_##name(vm_t a, vm_t b, vm_t c, vm_t d){ \ + return __##name(a, b, c, d);\ + }\ + static vm_t __##name + +/* memory */ +SYSCALL_DECLARE(req_mem); +SYSCALL_DECLARE(req_pmem); +SYSCALL_DECLARE(req_fixmem); +SYSCALL_DECLARE(req_sharedmem); +SYSCALL_DECLARE(free_mem); + +/* timers */ +SYSCALL_DECLARE(req_timer); +SYSCALL_DECLARE(free_timer); + +/* ipc */ +SYSCALL_DECLARE(ipc_server); +SYSCALL_DECLARE(ipc_req); +SYSCALL_DECLARE(ipc_resp); + +/* proc */ +SYSCALL_DECLARE(fork); +SYSCALL_DECLARE(exec); +SYSCALL_DECLARE(signal); +SYSCALL_DECLARE(switch); +SYSCALL_DECLARE(sync); + +/* conf */ +SYSCALL_DECLARE(conf); +SYSCALL_DECLARE(poweroff); + +vm_t syscall_dispatch(vm_t syscall, vm_t a, vm_t b, vm_t c, vm_t d); #endif /* APOS_UAPI_H */ diff --git a/include/arch/timer.h b/include/arch/timer.h new file mode 100644 index 0000000..6d63565 --- /dev/null +++ b/include/arch/timer.h @@ -0,0 +1,15 @@ +#ifndef APOS_ARCH_TIMER_H +#define APOS_ARCH_TIMER_H + +#include + +/* return hardware timer frequency */ +ticks_t stat_timer(); + +/* set up timer interrupt for absolute ticks */ +void set_timer(ticks_t ticks); + +/* current ticks */ +ticks_t current_ticks(); + +#endif /* APOS_ARCH_TIMER_H */ -- cgit v1.3