99 * use it, and a user thread that corrupts its metadata can only fault
1010 * itself. Also checks that the module resource system calls reject LL
1111 * modules and forged allocation contexts.
12+ *
13+ * Tests for the fast_get area of the LL memory region: LL user threads
14+ * get shared read-only copies with fast_get_ll(), freed copies are reused
15+ * without stale data, only the LL and IPC user threads may make the calls,
16+ * and kernel code does not release LL copies with fast_put(). LL modules
17+ * fall back to a private copy when the area is full.
1218 */
1319
1420#include <sof/boot_test.h>
1521#include <sof/audio/component.h>
1622#include <sof/audio/module_adapter/module/generic.h>
23+ #include <sof/lib/fast-get.h>
1724#include <sof/schedule/ll_schedule_domain.h>
25+ #include <sof/userspace/syscall_caller.h>
1826#include <rtos/alloc.h>
1927
2028#include <zephyr/cache.h>
@@ -144,14 +152,17 @@ static void user_function(void *p1, void *p2, void *p3)
144152 zassert_unreachable ("user thread did not fault" );
145153}
146154
147- static void run_case (enum user_heap_test_case test_case , bool fault_allowed )
155+ /* @infra: run as the IPC user thread, otherwise as another LL domain thread */
156+ static void run_user (k_thread_entry_t entry , int test_case , bool infra , bool fault_allowed )
148157{
149158 int ret ;
150159
151160 k_thread_create (& user_thread , user_stack , USER_STACKSIZE ,
152- user_function , (void * )(uintptr_t )test_case , NULL , NULL ,
161+ entry , (void * )(uintptr_t )test_case , NULL , NULL ,
153162 -1 , K_USER , K_FOREVER );
154163 k_mem_domain_add_thread (zephyr_ll_mem_domain (), & user_thread );
164+ if (infra )
165+ sof_sysuser_thread_set (SOF_SYSUSER_IPC , PLATFORM_PRIMARY_CORE_ID , & user_thread );
155166
156167 if (fault_allowed )
157168 sof_boot_test_set_fault_valid (& user_thread );
@@ -160,10 +171,17 @@ static void run_case(enum user_heap_test_case test_case, bool fault_allowed)
160171 ret = k_thread_join (& user_thread , K_FOREVER );
161172
162173 sof_boot_test_set_fault_valid (NULL );
174+ if (infra )
175+ sof_sysuser_thread_set (SOF_SYSUSER_IPC , PLATFORM_PRIMARY_CORE_ID , NULL );
163176
164177 zassert_equal (ret , 0 , "user thread join failed: %d" , ret );
165178}
166179
180+ static void run_case (enum user_heap_test_case test_case , bool fault_allowed )
181+ {
182+ run_user (user_function , test_case , false, fault_allowed );
183+ }
184+
167185static void * ll_user_heap_setup (void )
168186{
169187 k_mem_domain_add_partition (zephyr_ll_mem_domain (), & ll_user_heap_test_part );
@@ -300,13 +318,234 @@ ZTEST(userspace_ll_user_heap, mod_user_rejects_forged_heap)
300318ZTEST_SUITE (userspace_ll_user_heap , sof_boot_test_standalone_predicate , ll_user_heap_setup ,
301319 NULL , NULL , ll_user_heap_teardown );
302320
321+ /* without DRAM execution fast_get_ll() is a passthrough and there are no copies */
322+ #define HAVE_LL_FAST_GET_COPIES (CONFIG_FAST_GET && CONFIG_COLD_STORE_EXECUTE_DRAM)
323+
324+ #if HAVE_LL_FAST_GET_COPIES
325+ #define DATA_SIZE 1000
326+ /* more than the fast_get area can hold */
327+ #define FG_MAX_BLOCKS (CONFIG_SOF_ZEPHYR_LL_FG_AREA_SIZE / 256 + 1)
328+
329+ /* source data and results, the user threads read and write them */
330+ K_APP_BMEM (ll_user_heap_test_part ) static uint8_t data_a [DATA_SIZE ];
331+ K_APP_BMEM (ll_user_heap_test_part ) static uint8_t data_b [DATA_SIZE ];
332+ K_APP_BMEM (ll_user_heap_test_part ) static const void * fg_ptr [2 ];
333+
334+ /* kernel-only, a user thread must not get a copy of it */
335+ static uint8_t kernel_only_data [DATA_SIZE ];
336+
337+ static void * fg_blocks [FG_MAX_BLOCKS ];
338+
339+ enum fast_get_test_case {
340+ /* get data_a twice, check the copies, keep them in fg_ptr */
341+ FG_GET_SHARED ,
342+ /* put the copies in fg_ptr */
343+ FG_PUT_KEPT ,
344+ /* get data_a, check it and put it, then the same for data_b */
345+ FG_GET_PUT_A_THEN_B ,
346+ /* write to a copy */
347+ FG_WRITE_COPY ,
348+ /* get a copy of kernel memory */
349+ FG_GET_KERNEL_DATA ,
350+ /* get data_a with the module interface, keep it in fg_ptr[0] */
351+ FG_MOD_GET ,
352+ /* put fg_ptr[0] with the module interface */
353+ FG_MOD_PUT ,
354+ /* free the module resources */
355+ FG_MOD_FREE_ALL ,
356+ };
357+
358+ static void check_copy (const void * copy , const uint8_t * data )
359+ {
360+ zassert_not_null (copy );
361+ zassert_not_equal (copy , data );
362+ zassert_true (sys_cache_is_ptr_cached ((void * )copy ));
363+ zassert_mem_equal (copy , data , DATA_SIZE );
364+ }
365+
366+ static void fast_get_user_function (void * p1 , void * p2 , void * p3 )
367+ {
368+ enum fast_get_test_case test_case = (enum fast_get_test_case )(uintptr_t )p1 ;
369+ const void * ptr ;
370+
371+ zassert_true (k_is_user_context (), "isn't user" );
372+
373+ switch (test_case ) {
374+ case FG_GET_SHARED :
375+ fg_ptr [0 ] = fast_get_ll (data_a , DATA_SIZE );
376+ check_copy (fg_ptr [0 ], data_a );
377+ fg_ptr [1 ] = fast_get_ll (data_a , DATA_SIZE );
378+ zassert_equal (fg_ptr [1 ], fg_ptr [0 ], "copy not shared" );
379+ /* a size that does not match the shared copy */
380+ zassert_is_null (fast_get_ll (data_a , DATA_SIZE / 2 ));
381+ return ;
382+ case FG_PUT_KEPT :
383+ fast_put_ll (fg_ptr [0 ]);
384+ fast_put_ll (fg_ptr [1 ]);
385+ return ;
386+ case FG_GET_PUT_A_THEN_B :
387+ /* reads data_a into the cache, a later copy may reuse the lines */
388+ fg_ptr [0 ] = fast_get_ll (data_a , DATA_SIZE );
389+ check_copy (fg_ptr [0 ], data_a );
390+ fast_put_ll (fg_ptr [0 ]);
391+ fg_ptr [1 ] = fast_get_ll (data_b , DATA_SIZE );
392+ check_copy (fg_ptr [1 ], data_b );
393+ fast_put_ll (fg_ptr [1 ]);
394+ return ;
395+ case FG_WRITE_COPY :
396+ ptr = fast_get_ll (data_a , DATA_SIZE );
397+ zassert_not_null (ptr );
398+ fg_ptr [0 ] = ptr ;
399+ * (volatile uint8_t * )ptr = 0 ;
400+ break ;
401+ case FG_GET_KERNEL_DATA :
402+ (void )fast_get_ll (kernel_only_data , DATA_SIZE );
403+ break ;
404+ case FG_MOD_GET :
405+ fg_ptr [0 ] = mod_fast_get (& fake_mod , data_a , DATA_SIZE );
406+ check_copy (fg_ptr [0 ], data_a );
407+ return ;
408+ case FG_MOD_PUT :
409+ mod_fast_put (& fake_mod , fg_ptr [0 ]);
410+ return ;
411+ case FG_MOD_FREE_ALL :
412+ mod_free_all (& fake_mod );
413+ return ;
414+ default :
415+ zassert_unreachable ("unknown test case" );
416+ }
417+
418+ zassert_unreachable ("user thread did not fault" );
419+ }
420+
421+ static void run_fg_case (enum fast_get_test_case test_case , bool infra , bool fault_allowed )
422+ {
423+ run_user (fast_get_user_function , test_case , infra , fault_allowed );
424+ }
425+
426+ static void * ll_fast_get_setup (void )
427+ {
428+ int i ;
429+
430+ for (i = 0 ; i < DATA_SIZE ; i ++ ) {
431+ data_a [i ] = i ;
432+ data_b [i ] = ~i ;
433+ }
434+
435+ return ll_user_heap_setup ();
436+ }
437+
438+ ZTEST (userspace_ll_fast_get , shared_copy )
439+ {
440+ size_t used = zephyr_ll_fg_used ();
441+
442+ run_fg_case (FG_GET_SHARED , true, false);
443+ zassert_true (zephyr_ll_fg_contains (fg_ptr [0 ], DATA_SIZE ));
444+ zassert_equal (zephyr_ll_fg_used (), used + ROUND_UP (DATA_SIZE , 256 ));
445+
446+ /* kernel code does not release LL copies with fast_put() */
447+ fast_put (NULL , NULL , fg_ptr [0 ]);
448+ zassert_equal (zephyr_ll_fg_used (), used + ROUND_UP (DATA_SIZE , 256 ));
449+
450+ /* the copy is freed with its last user */
451+ run_fg_case (FG_PUT_KEPT , true, false);
452+ zassert_equal (zephyr_ll_fg_used (), used );
453+ }
454+
455+ ZTEST (userspace_ll_fast_get , reuse_without_stale_data )
456+ {
457+ size_t used = zephyr_ll_fg_used ();
458+
459+ run_fg_case (FG_GET_PUT_A_THEN_B , true, false);
460+ zassert_equal (fg_ptr [1 ], fg_ptr [0 ], "freed copy not reused" );
461+ zassert_equal (zephyr_ll_fg_used (), used );
462+ }
463+
464+ ZTEST (userspace_ll_fast_get , copy_read_only )
465+ {
466+ size_t used = zephyr_ll_fg_used ();
467+
468+ run_fg_case (FG_WRITE_COPY , true, true);
469+ zassert_mem_equal (fg_ptr [0 ], data_a , DATA_SIZE );
470+
471+ /* the faulting thread kept its reference */
472+ fast_put_ll (fg_ptr [0 ]);
473+ zassert_equal (zephyr_ll_fg_used (), used );
474+ }
475+
476+ ZTEST (userspace_ll_fast_get , caller_checks )
477+ {
478+ size_t used = zephyr_ll_fg_used ();
479+
480+ /* an LL domain thread that is not the LL or IPC user thread */
481+ if (IS_ENABLED (CONFIG_SOF_USERSPACE_SYSCALL_CALLER_CHECK ))
482+ run_fg_case (FG_GET_SHARED , false, true);
483+ /* memory that the caller cannot read */
484+ run_fg_case (FG_GET_KERNEL_DATA , true, true);
485+
486+ zassert_equal (zephyr_ll_fg_used (), used );
487+ }
488+
489+ ZTEST (userspace_ll_fast_get , mod_fast_get_falls_back )
490+ {
491+ size_t used = zephyr_ll_fg_used ();
492+ int n ;
493+
494+ memset (& fake_mod , 0 , sizeof (fake_mod ));
495+ memset (& fake_dev , 0 , sizeof (fake_dev ));
496+ fake_mod .dev = & fake_dev ;
497+ fake_dev .mod = & fake_mod ;
498+ fake_mod .priv .resources .alloc = & fake_alloc ;
499+ fake_dev .ipc_config .proc_domain = COMP_PROCESSING_DOMAIN_LL ;
500+ fake_alloc .heap = zephyr_ll_user_heap ();
501+ fake_alloc .vreg = NULL ;
502+ mod_resource_init (& fake_mod );
503+
504+ run_fg_case (FG_MOD_GET , true, false);
505+ zassert_true (zephyr_ll_fg_contains (fg_ptr [0 ], DATA_SIZE ));
506+ run_fg_case (FG_MOD_PUT , true, false);
507+ zassert_equal (zephyr_ll_fg_used (), used );
508+
509+ /* fill the area */
510+ for (n = 0 ; n < FG_MAX_BLOCKS ; n ++ ) {
511+ fg_blocks [n ] = zephyr_ll_fg_alloc (256 );
512+ if (!fg_blocks [n ])
513+ break ;
514+ }
515+ zassert_true (n < FG_MAX_BLOCKS , "fast_get area not full" );
516+
517+ run_fg_case (FG_MOD_GET , true, false);
518+ zassert_false (zephyr_ll_fg_contains (fg_ptr [0 ], 1 ), "copy in the full area" );
519+ run_fg_case (FG_MOD_PUT , true, false);
520+
521+ while (n -- )
522+ zephyr_ll_fg_free (fg_blocks [n ], 256 );
523+ zassert_equal (zephyr_ll_fg_used (), used );
524+
525+ run_fg_case (FG_MOD_FREE_ALL , true, false);
526+ }
527+
528+ static bool ll_fast_get_predicate (const void * state )
529+ {
530+ return CONFIG_SOF_ZEPHYR_LL_FG_AREA_SIZE && sof_boot_test_standalone_predicate (state );
531+ }
532+
533+ ZTEST_SUITE (userspace_ll_fast_get , ll_fast_get_predicate , ll_fast_get_setup ,
534+ NULL , NULL , ll_user_heap_teardown );
535+
536+ #endif /* HAVE_LL_FAST_GET_COPIES */
537+
538+
303539/**
304540 * SOF main has booted up and IPC handling is stopped.
305541 * Run test suites with ztest_run_all.
306542 */
307543static int run_tests (void )
308544{
309545 ztest_run_test_suite (userspace_ll_user_heap , false, 1 , 1 , NULL );
546+ #if HAVE_LL_FAST_GET_COPIES && CONFIG_SOF_ZEPHYR_LL_FG_AREA_SIZE
547+ ztest_run_test_suite (userspace_ll_fast_get , false, 1 , 1 , NULL );
548+ #endif
310549 return 0 ;
311550}
312551
0 commit comments