Skip to content

Commit 6211b64

Browse files
committed
zephyr: tests: userspace: add test for cache ops via syscalls
Add simple tests that call cache invalidate and flush from a user-space thread. This does not currently have any negative tests (attempt to invalidate an buffer the user space thread does not have access to), as these currently result in kernel oops on xtensa. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
1 parent a561fea commit 6211b64

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

zephyr/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ endif()
66

77
if (CONFIG_ACE_VERSION_3_0)
88
zephyr_library_sources_ifdef(CONFIG_SOF_BOOT_TEST
9+
userspace/cache.c
910
userspace/local_lock.c
1011
)
1112
endif()

zephyr/test/userspace/cache.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
/*
3+
* Copyright(c) 2025 Intel Corporation.
4+
*/
5+
6+
#include <sof/boot_test.h>
7+
8+
#include <zephyr/kernel.h>
9+
#include <zephyr/cache.h>
10+
#include <zephyr/ztest.h>
11+
#include <zephyr/logging/log.h>
12+
13+
LOG_MODULE_DECLARE(sof_boot_test, LOG_LEVEL_DBG);
14+
15+
#define USER_STACKSIZE 2048
16+
17+
static struct k_thread user_thread;
18+
static K_THREAD_STACK_DEFINE(user_stack, USER_STACKSIZE);
19+
20+
static void user_function(void *p1, void *p2, void *p3)
21+
{
22+
char stack_buf[64];
23+
int ret;
24+
25+
__ASSERT(k_is_user_context(), "isn't user");
26+
27+
LOG_INF("SOF thread %s (%s)",
28+
k_is_user_context() ? "UserSpace!" : "privileged mode.",
29+
CONFIG_BOARD_TARGET);
30+
31+
ret = sys_cache_data_flush_range(stack_buf, sizeof(stack_buf));
32+
__ASSERT(!ret, "cache flush of stack buffer failed");
33+
34+
ret = sys_cache_data_invd_range(stack_buf, sizeof(stack_buf));
35+
__ASSERT(!ret, "cache invalidate of stack buffer failed");
36+
}
37+
38+
static void test_user_thread_cache(void)
39+
{
40+
k_thread_create(&user_thread, user_stack, USER_STACKSIZE,
41+
user_function, NULL, NULL, NULL,
42+
-1, K_USER, K_MSEC(0));
43+
k_thread_join(&user_thread, K_FOREVER);
44+
}
45+
46+
ZTEST(sof_boot, user_space_cache)
47+
{
48+
test_user_thread_cache();
49+
50+
ztest_test_pass();
51+
}

0 commit comments

Comments
 (0)