From 76c04a21ee5ec3f137b815c1b1ad0ea7acd0b542 Mon Sep 17 00:00:00 2001 From: Andrew Boie Date: Wed, 27 Sep 2017 14:45:10 -0700 Subject: [PATCH] kernel: implement some more system calls These are needed to demonstrate the Philosophers demo with threads running in user mode. Signed-off-by: Andrew Boie --- include/kernel.h | 8 ++++---- kernel/sched.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- kernel/sys_clock.c | 16 +++++++++++++++- 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/include/kernel.h b/include/kernel.h index a34d27a2ccf..76b3553e371 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -599,7 +599,7 @@ extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry, * * @return N/A */ -extern void k_sleep(s32_t duration); +__syscall void k_sleep(s32_t duration); /** * @brief Cause the current thread to busy wait. @@ -640,7 +640,7 @@ extern void k_wakeup(k_tid_t thread); * * @return ID of current thread. */ -extern k_tid_t k_current_get(void); +__syscall k_tid_t k_current_get(void); /** * @brief Cancel thread performing a delayed start. @@ -779,7 +779,7 @@ struct _static_thread_data { * * @return Priority of @a thread. */ -extern int k_thread_priority_get(k_tid_t thread); +__syscall int k_thread_priority_get(k_tid_t thread); /** * @brief Set a thread's priority. @@ -1414,7 +1414,7 @@ static inline void k_disable_sys_clock_always_on(void) * * @return Current uptime. */ -extern u32_t k_uptime_get_32(void); +__syscall u32_t k_uptime_get_32(void); /** * @brief Get elapsed time. diff --git a/kernel/sched.c b/kernel/sched.c index 3dab1da1c04..0de0d60dd77 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -10,6 +10,7 @@ #include #include #include +#include /* the only struct _kernel instance */ struct _kernel _kernel = {0}; @@ -257,11 +258,26 @@ int __must_switch_threads(void) #endif } -int k_thread_priority_get(k_tid_t thread) +int _impl_k_thread_priority_get(k_tid_t thread) { return thread->base.prio; } +#ifdef CONFIG_USERSPACE +u32_t _handler_k_thread_priority_get(u32_t arg1, u32_t arg2, u32_t arg3, + u32_t arg4, u32_t arg5, u32_t arg6, + void *ssf) +{ + struct k_thread *thread; + + _SYSCALL_ARG1; + + thread = (struct k_thread *)arg1; + _SYSCALL_IS_OBJ(thread, K_OBJ_THREAD, 0, ssf); + return (u32_t)_impl_k_thread_priority_get(thread); +} +#endif + void k_thread_priority_set(k_tid_t tid, int prio) { /* @@ -322,7 +338,7 @@ void k_yield(void) } } -void k_sleep(s32_t duration) +void _impl_k_sleep(s32_t duration) { #ifdef CONFIG_MULTITHREADING /* volatile to guarantee that irq_lock() is executed after ticks is @@ -352,6 +368,19 @@ void k_sleep(s32_t duration) #endif } +#ifdef CONFIG_USERSPACE +u32_t _handler_k_sleep(u32_t arg1, u32_t arg2, u32_t arg3, + u32_t arg4, u32_t arg5, u32_t arg6, void *ssf) +{ + _SYSCALL_ARG1; + + _SYSCALL_VERIFY(arg1 != K_FOREVER, ssf); + _impl_k_sleep(arg1); + + return 0; +} +#endif + void k_wakeup(k_tid_t thread) { int key = irq_lock(); @@ -376,11 +405,21 @@ void k_wakeup(k_tid_t thread) } } -k_tid_t k_current_get(void) +k_tid_t _impl_k_current_get(void) { return _current; } +#ifdef CONFIG_USERSPACE +u32_t _handler_k_current_get(u32_t arg1, u32_t arg2, u32_t arg3, u32_t arg4, + u32_t arg5, u32_t arg6, void *ssf) +{ + _SYSCALL_ARG0; + + return (u32_t)_impl_k_current_get(); +} +#endif + #ifdef CONFIG_TIMESLICING extern s32_t _time_slice_duration; /* Measured in ms */ extern s32_t _time_slice_elapsed; /* Measured in ms */ diff --git a/kernel/sys_clock.c b/kernel/sys_clock.c index f459f70267b..0671a123be2 100644 --- a/kernel/sys_clock.c +++ b/kernel/sys_clock.c @@ -12,6 +12,7 @@ #include #include #include +#include #ifdef CONFIG_SYS_CLOCK_EXISTS #ifdef _NON_OPTIMIZED_TICKS_PER_SEC @@ -68,7 +69,7 @@ u32_t _tick_get_32(void) } FUNC_ALIAS(_tick_get_32, sys_tick_get_32, u32_t); -u32_t k_uptime_get_32(void) +u32_t _impl_k_uptime_get_32(void) { #ifdef CONFIG_TICKLESS_KERNEL __ASSERT(_sys_clock_always_on, @@ -77,6 +78,19 @@ u32_t k_uptime_get_32(void) return __ticks_to_ms(_tick_get_32()); } +#ifdef CONFIG_USERSPACE +u32_t _handler_k_uptime_get_32(u32_t arg1, u32_t arg2, u32_t arg3, + u32_t arg4, u32_t arg5, u32_t arg6, void *ssf) +{ + _SYSCALL_ARG0; + +#ifdef CONFIG_TICKLESS_KERNEL + _SYSCALL_VERIFY(_sys_clock_always_on, ssf); +#endif + return _impl_k_uptime_get_32(); +} +#endif + /** * * @brief Return the current system tick count