@@ -57,6 +57,62 @@ 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 sem1 ;
67+ struct sys_sem sem2 ;
68+ uint8_t reserved [4096 - 2 * sizeof (struct sys_sem )];
69+ };
70+
71+ static struct sem_mem simple_sem __attribute__((aligned (4096 )));
72+ static struct k_mem_domain dp_mdom ;
73+
74+ static void sys_sem_function (void * p1 , void * p2 , void * p3 )
75+ {
76+ __ASSERT (k_is_user_context (), "isn't user" );
77+ /* This is the goal, but it hangs with this disabled too */
78+ sys_sem_give (& simple_sem .sem1 );
79+ int ret = sys_sem_take (& simple_sem .sem2 , K_MSEC (20 ));
80+
81+ LOG_INF ("SOF thread %s (%s) sem %p: %d" ,
82+ k_is_user_context () ? "UserSpace!" : "privileged mode." ,
83+ CONFIG_BOARD_TARGET , & simple_sem , ret );
84+ }
85+
86+ static void test_user_thread_sys_sem (void )
87+ {
88+ struct k_mem_partition mpart = {
89+ .start = (uintptr_t )& simple_sem ,
90+ .size = 4096 ,
91+ .attr = K_MEM_PARTITION_P_RW_U_RW /* | XTENSA_MMU_CACHED_WB*/ ,
92+ };
6093
61- ztest_test_pass ();
94+ k_mem_domain_init (& dp_mdom , 0 , NULL );
95+ sys_sem_init (& simple_sem .sem1 , 0 , 1 );
96+ sys_sem_init (& simple_sem .sem2 , 0 , 1 );
97+
98+ k_thread_create (& user_thread , user_stack , USER_STACKSIZE ,
99+ sys_sem_function , NULL , NULL , NULL ,
100+ -1 , K_USER , K_FOREVER );
101+ k_mem_domain_add_partition (& dp_mdom , & mpart );
102+ k_mem_domain_add_thread (& dp_mdom , & user_thread );
103+
104+ k_thread_start (& user_thread );
105+
106+ /* This is what doesn't work: enabling this line crashes the DSP */
107+ zassert_ok (sys_sem_take (& simple_sem .sem1 , K_MSEC (20 )));
108+
109+ sys_sem_give (& simple_sem .sem2 );
110+
111+ k_thread_join (& user_thread , K_FOREVER );
112+ k_mem_domain_remove_partition (& dp_mdom , & mpart );
113+ }
114+
115+ ZTEST (sof_boot , test_sys_sem )
116+ {
117+ test_user_thread_sys_sem ();
62118}
0 commit comments