Skip to content

Commit 4c5f35b

Browse files
authored
Zephyr User Mode Support (#3650)
Add support for the [Zephyr Usermode](https://docs.zephyrproject.org/latest/kernel/usermode/index.html) to the Zephyr port. The following changes are applied: - Fix `signbit`, check if it is defined already and only implement it, if not - Introduce `sys_mutex` and `sys_sem` in favour of `k_mutex` and `k_sem`, when `CONFIG_USERMODE` is enabled - Remove the installation of the `_stdout_hook_iwasm()` when `CONFIG_USERMODE` is enabled, otherwise this causes MPU errors since the std hook is in the kernel space - Add a thread name for debugging
1 parent 22df091 commit 4c5f35b

3 files changed

Lines changed: 105 additions & 58 deletions

File tree

core/shared/platform/zephyr/platform_internal.h

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (C) 2019 Intel Corporation. All rights reserved.
3+
* SPDX-FileCopyrightText: 2024 Siemens AG (For Zephyr usermode changes)
34
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
45
*/
56

@@ -18,7 +19,6 @@
1819
#include <misc/printk.h>
1920
#endif
2021
#else /* else of KERNEL_VERSION_NUMBER < 0x030200 */
21-
#include <zephyr/kernel.h>
2222
#include <zephyr/sys/printk.h>
2323
#endif /* end of KERNEL_VERSION_NUMBER < 0x030200 */
2424

@@ -37,19 +37,26 @@
3737
#endif
3838

3939
#if KERNEL_VERSION_NUMBER < 0x030200 /* version 3.2.0 */
40+
#include <zephyr.h>
4041
#include <net/net_pkt.h>
4142
#include <net/net_if.h>
4243
#include <net/net_ip.h>
4344
#include <net/net_core.h>
4445
#include <net/net_context.h>
4546
#else /* else of KERNEL_VERSION_NUMBER < 0x030200 */
47+
#include <zephyr/kernel.h>
4648
#include <zephyr/net/net_pkt.h>
4749
#include <zephyr/net/net_if.h>
4850
#include <zephyr/net/net_ip.h>
4951
#include <zephyr/net/net_core.h>
5052
#include <zephyr/net/net_context.h>
5153
#endif /* end of KERNEL_VERSION_NUMBER < 0x030200 */
5254

55+
#ifdef CONFIG_USERSPACE
56+
#include <zephyr/sys/mutex.h>
57+
#include <zephyr/sys/sem.h>
58+
#endif /* end of CONFIG_USERSPACE */
59+
5360
#if KERNEL_VERSION_NUMBER >= 0x030300 /* version 3.3.0 */
5461
#include <zephyr/cache.h>
5562
#endif /* end of KERNEL_VERSION_NUMBER > 0x030300 */
@@ -64,18 +71,47 @@
6471
#endif
6572
#endif
6673

74+
#ifdef signbit /* probably since Zephyr v3.5.0 a new picolib is included */
75+
#define BH_HAS_SIGNBIT 1
76+
#endif
77+
6778
#ifndef BH_PLATFORM_ZEPHYR
6879
#define BH_PLATFORM_ZEPHYR
6980
#endif
7081

82+
// Synchronization primitives for usermode
83+
#ifdef CONFIG_USERSPACE
84+
#define mutex_t struct sys_mutex
85+
#define mutex_init(mtx) sys_mutex_init(mtx)
86+
#define mutex_lock(mtx, timeout) sys_mutex_lock(mtx, timeout)
87+
#define mutex_unlock(mtx) sys_mutex_unlock(mtx)
88+
89+
#define sem_t struct sys_sem
90+
#define sem_init(sem, init_count, limit) sys_sem_init(sem, init_count, limit)
91+
#define sem_give(sem) sys_sem_give(sem)
92+
#define sem_take(sem, timeout) sys_sem_take(sem, timeout)
93+
#define sem_count_get(sem) sys_sem_count_get(sem)
94+
#else /* else of CONFIG_USERSPACE */
95+
#define mutex_t struct k_mutex
96+
#define mutex_init(mtx) k_mutex_init(mtx)
97+
#define mutex_lock(mtx, timeout) k_mutex_lock(mtx, timeout)
98+
#define mutex_unlock(mtx) k_mutex_unlock(mtx)
99+
100+
#define sem_t struct k_sem
101+
#define sem_init(sem, init_count, limit) k_sem_init(sem, init_count, limit)
102+
#define sem_give(sem) k_sem_give(sem)
103+
#define sem_take(sem, timeout) k_sem_take(sem, timeout)
104+
#define sem_count_get(sem) k_sem_count_get(sem)
105+
#endif /* end of CONFIG_USERSPACE */
106+
71107
#define BH_APPLET_PRESERVED_STACK_SIZE (2 * BH_KB)
72108

73109
/* Default thread priority */
74110
#define BH_THREAD_DEFAULT_PRIORITY 7
75111

76112
typedef struct k_thread korp_thread;
77113
typedef korp_thread *korp_tid;
78-
typedef struct k_mutex korp_mutex;
114+
typedef mutex_t korp_mutex;
79115
typedef unsigned int korp_sem;
80116

81117
/* korp_rwlock is used in platform_api_extension.h,
@@ -87,7 +123,7 @@ typedef struct {
87123
struct os_thread_wait_node;
88124
typedef struct os_thread_wait_node *os_thread_wait_list;
89125
typedef struct korp_cond {
90-
struct k_mutex wait_list_lock;
126+
mutex_t wait_list_lock;
91127
os_thread_wait_list thread_wait_list;
92128
} korp_cond;
93129

@@ -120,11 +156,14 @@ float fmaxf(float x, float y);
120156
float rintf(float x);
121157
float fabsf(float x);
122158
float truncf(float x);
123-
int signbit(double x);
124159
int isnan(double x);
125160
double pow(double x, double y);
126161
double scalbn(double x, int n);
127162

163+
#ifndef BH_HAS_SIGNBIT
164+
int signbit(double x);
165+
#endif
166+
128167
unsigned long long int strtoull(const char *nptr, char **endptr, int base);
129168
double strtod(const char *nptr, char **endptr);
130169
float strtof(const char *nptr, char **endptr);

core/shared/platform/zephyr/zephyr_platform.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (C) 2019 Intel Corporation. All rights reserved.
3+
* SPDX-FileCopyrightText: 2024 Siemens AG (For Zephyr usermode changes)
34
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
45
*/
56

@@ -35,12 +36,14 @@ disable_mpu_rasr_xn(void)
3536
#endif /* end of CONFIG_ARM_MPU */
3637
#endif
3738

39+
#ifndef CONFIG_USERSPACE
3840
static int
3941
_stdout_hook_iwasm(int c)
4042
{
4143
printk("%c", (char)c);
4244
return 1;
4345
}
46+
#endif
4447

4548
int
4649
os_thread_sys_init();
@@ -51,9 +54,11 @@ os_thread_sys_destroy();
5154
int
5255
bh_platform_init()
5356
{
57+
#ifndef CONFIG_USERSPACE
5458
extern void __stdout_hook_install(int (*hook)(int));
5559
/* Enable printf() in Zephyr */
5660
__stdout_hook_install(_stdout_hook_iwasm);
61+
#endif
5762

5863
#if WASM_ENABLE_AOT != 0
5964
#ifdef CONFIG_ARM_MPU

0 commit comments

Comments
 (0)