Skip to content

Commit 9ceb1b6

Browse files
committed
test: userspace: add a sys_sem test
Add a sys_sem test between kernel- and user-space. Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent 79855f3 commit 9ceb1b6

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

zephyr/test/userspace/ksem.c

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,67 @@ ZTEST(sof_boot, user_space)
5757
{
5858
test_user_thread();
5959
test_user_thread_with_sem();
60+
}
61+
62+
#include <zephyr/sys/sem.h>
63+
#include <zephyr/app_memory/mem_domain.h>
64+
65+
struct sem_mem {
66+
struct sys_sem sem;
67+
uint8_t reserved[4096 - sizeof(struct sys_sem)];
68+
};
69+
70+
static struct sem_mem simple_sem __attribute__((aligned(4096)));
71+
static struct k_mem_domain dp_mdom;
72+
73+
static void sys_sem_function(void *p1, void *p2, void *p3)
74+
{
75+
__ASSERT(k_is_user_context(), "isn't user");
76+
LOG_INF("SOF thread %s (%s)",
77+
k_is_user_context() ? "UserSpace!" : "privileged mode.",
78+
CONFIG_BOARD_TARGET);
79+
#if 0
80+
/* This is the goal, but it hangs with this disabled too */
81+
sys_sem_give(&simple_sem.sem);
82+
#endif
83+
}
6084

61-
ztest_test_pass();
85+
static void test_user_thread_sys_sem(void)
86+
{
87+
struct k_mem_partition mpart = {
88+
.start = (uintptr_t)&simple_sem,
89+
.size = 4096,
90+
.attr = K_MEM_PARTITION_P_RW_U_RW/* | XTENSA_MMU_CACHED_WB*/,
91+
};
92+
93+
k_mem_domain_init(&dp_mdom, 0, NULL);
94+
sys_sem_init(&simple_sem.sem, 0, 1);
95+
96+
k_thread_create(&user_thread, user_stack, USER_STACKSIZE,
97+
sys_sem_function, NULL, NULL, NULL,
98+
-1, K_USER, K_FOREVER);
99+
k_mem_domain_add_partition(&dp_mdom, &mpart);
100+
k_mem_domain_add_thread(&dp_mdom, &user_thread);
101+
102+
#if 0
103+
/*
104+
* Do we need to also grant access to the futex in sys_sem? But this
105+
* isn't what's breaking execution, it breaks without user-space
106+
* accessing the semaphore at all.
107+
*/
108+
k_thread_access_grant(&user_thread, &simple_sem.sem.futex);
109+
#endif
110+
111+
k_thread_start(&user_thread);
112+
#if 0
113+
/* This is what doesn't work: enabling this line crashes the DSP */
114+
sys_sem_take(&simple_sem.sem, K_MSEC(20));
115+
#endif
116+
k_thread_join(&user_thread, K_FOREVER);
117+
k_mem_domain_remove_partition(&dp_mdom, &mpart);
118+
}
119+
120+
ZTEST(sof_boot, test_sys_sem)
121+
{
122+
test_user_thread_sys_sem();
62123
}

0 commit comments

Comments
 (0)