diff --git a/zephyr/test/userspace/ksem.c b/zephyr/test/userspace/ksem.c index d8f9e6f71666..6e74ee60c5d6 100644 --- a/zephyr/test/userspace/ksem.c +++ b/zephyr/test/userspace/ksem.c @@ -57,6 +57,67 @@ ZTEST(sof_boot, user_space) { test_user_thread(); test_user_thread_with_sem(); +} + +#include +#include + +struct sem_mem { + struct sys_sem sem; + uint8_t reserved[4096 - sizeof(struct sys_sem)]; +}; + +static struct sem_mem simple_sem __attribute__((aligned(4096))); +static struct k_mem_domain dp_mdom; + +static void sys_sem_function(void *p1, void *p2, void *p3) +{ + __ASSERT(k_is_user_context(), "isn't user"); + LOG_INF("SOF thread %s (%s)", + k_is_user_context() ? "UserSpace!" : "privileged mode.", + CONFIG_BOARD_TARGET); +#if 0 + /* This is the goal, but it hangs with this disabled too */ + sys_sem_give(&simple_sem.sem); +#endif +} - ztest_test_pass(); +static void test_user_thread_sys_sem(void) +{ + struct k_mem_partition mpart = { + .start = (uintptr_t)&simple_sem, + .size = 4096, + .attr = K_MEM_PARTITION_P_RW_U_RW/* | XTENSA_MMU_CACHED_WB*/, + }; + + k_mem_domain_init(&dp_mdom, 0, NULL); + sys_sem_init(&simple_sem.sem, 0, 1); + + k_thread_create(&user_thread, user_stack, USER_STACKSIZE, + sys_sem_function, NULL, NULL, NULL, + -1, K_USER, K_FOREVER); + k_mem_domain_add_partition(&dp_mdom, &mpart); + k_mem_domain_add_thread(&dp_mdom, &user_thread); + +#if 0 + /* + * Do we need to also grant access to the futex in sys_sem? But this + * isn't what's breaking execution, it breaks without user-space + * accessing the semaphore at all. + */ + k_thread_access_grant(&user_thread, &simple_sem.sem.futex); +#endif + + k_thread_start(&user_thread); +#if 0 + /* This is what doesn't work: enabling this line crashes the DSP */ + sys_sem_take(&simple_sem.sem, K_MSEC(20)); +#endif + k_thread_join(&user_thread, K_FOREVER); + k_mem_domain_remove_partition(&dp_mdom, &mpart); +} + +ZTEST(sof_boot, test_sys_sem) +{ + test_user_thread_sys_sem(); }