diff --git a/posix/include/rtos/atomic.h b/posix/include/rtos/atomic.h index 6d16b6b6c9e6..c01635031fec 100644 --- a/posix/include/rtos/atomic.h +++ b/posix/include/rtos/atomic.h @@ -8,32 +8,18 @@ #ifndef __SOF_ATOMIC_H__ #define __SOF_ATOMIC_H__ -#include +#include #include +/* SOF names for atomic_set() and atomic_get(), as in zephyr/include/rtos/atomic.h */ static inline void atomic_init(atomic_t *a, int32_t value) { - arch_atomic_init(a, value); + atomic_set(a, value); } static inline int32_t atomic_read(const atomic_t *a) { - return arch_atomic_read(a); -} - -static inline void atomic_set(atomic_t *a, int32_t value) -{ - arch_atomic_set(a, value); -} - -static inline int32_t atomic_add(atomic_t *a, int32_t value) -{ - return arch_atomic_add(a, value); -} - -static inline int32_t atomic_sub(atomic_t *a, int32_t value) -{ - return arch_atomic_sub(a, value); + return atomic_get(a); } #endif /* __SOF_ATOMIC_H__ */ diff --git a/posix/include/rtos/panic.h b/posix/include/rtos/panic.h index e4aca1661f56..2ba24eed48bc 100644 --- a/posix/include/rtos/panic.h +++ b/posix/include/rtos/panic.h @@ -38,6 +38,12 @@ void __panic(uint32_t p, const char *filename, uint32_t linenum) SOF_NORETURN; #define sof_panic(x) __panic((x), (RELATIVE_FILE), (__LINE__)) +/* + * Zephyr k_panic() takes no panic code. Report SOF_IPC_PANIC_ASSERT, the code + * of a failed assert(): Zephyr also ends a failed assertion with k_panic(). + */ +#define k_panic() sof_panic(SOF_IPC_PANIC_ASSERT) + /* runtime assertion */ #ifndef assert #define assert(cond) (void)((cond) || (sof_panic(SOF_IPC_PANIC_ASSERT), 0)) diff --git a/posix/include/zephyr/cache.h b/posix/include/zephyr/cache.h new file mode 100644 index 000000000000..59313da05cf8 --- /dev/null +++ b/posix/include/zephyr/cache.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Copyright(c) 2026 Intel Corporation. All rights reserved. + * + * Stub for used by the host builds. The host has no cache to + * maintain, so these return -ENOTSUP, like Zephyr built without + * CONFIG_CACHE_MANAGEMENT (native_sim, for example). + */ + +#ifndef __POSIX_ZEPHYR_CACHE_H__ +#define __POSIX_ZEPHYR_CACHE_H__ + +#include +#include + +static inline int sys_cache_data_flush_range(void *addr, size_t size) +{ + (void)addr; + (void)size; + return -ENOTSUP; +} + +static inline int sys_cache_data_invd_range(void *addr, size_t size) +{ + (void)addr; + (void)size; + return -ENOTSUP; +} + +static inline int sys_cache_data_flush_and_invd_range(void *addr, size_t size) +{ + (void)addr; + (void)size; + return -ENOTSUP; +} + +static inline int sys_cache_instr_invd_range(void *addr, size_t size) +{ + (void)addr; + (void)size; + return -ENOTSUP; +} + +#endif /* __POSIX_ZEPHYR_CACHE_H__ */ diff --git a/posix/include/zephyr/sys/__assert.h b/posix/include/zephyr/sys/__assert.h new file mode 100644 index 000000000000..4e625e41c4f1 --- /dev/null +++ b/posix/include/zephyr/sys/__assert.h @@ -0,0 +1,50 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Copyright(c) 2026 Intel Corporation. All rights reserved. + * + * Stub for used by the host builds. The host checks + * its assertions in every build, as assert() does: a failed assertion prints + * the condition and the message, then panics with SOF_IPC_PANIC_ASSERT. + */ + +#ifndef __POSIX_ZEPHYR_SYS___ASSERT_H__ +#define __POSIX_ZEPHYR_SYS___ASSERT_H__ + +#include +#include +#include +#include + +/* + * Print a failed assertion. The variable arguments are the optional message + * format and its arguments, then NULL: NULL alone means there is no message. + */ +static inline void posix_assert_print(const char *test, const char *file, int line, ...) +{ + const char *fmt; + va_list ap; + + fprintf(stderr, "ASSERTION FAIL [%s] @ %s:%d\n", test, file, line); + va_start(ap, line); + fmt = va_arg(ap, const char *); + if (fmt) { + fprintf(stderr, "\t"); + vfprintf(stderr, fmt, ap); + fprintf(stderr, "\n"); + } + va_end(ap); +} + +/* as in Zephyr, the message format and its arguments are optional */ +#define __ASSERT(test, ...) \ + do { \ + if (!(test)) { \ + posix_assert_print(#test, RELATIVE_FILE, __LINE__, \ + ##__VA_ARGS__, NULL); \ + sof_panic(SOF_IPC_PANIC_ASSERT); \ + } \ + } while (0) + +#define __ASSERT_NO_MSG(test) __ASSERT(test) + +#endif /* __POSIX_ZEPHYR_SYS___ASSERT_H__ */ diff --git a/posix/include/zephyr/sys/atomic.h b/posix/include/zephyr/sys/atomic.h new file mode 100644 index 000000000000..6a6843aa3d61 --- /dev/null +++ b/posix/include/zephyr/sys/atomic.h @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Copyright(c) 2026 Intel Corporation. All rights reserved. + * + * Stub for used by the host builds. The functions have + * the Zephyr names and return values: atomic_set(), atomic_add() and + * atomic_sub() return the previous value. + */ + +#ifndef __POSIX_ZEPHYR_SYS_ATOMIC_H__ +#define __POSIX_ZEPHYR_SYS_ATOMIC_H__ + +#include +#include + +typedef int32_t atomic_val_t; + +static inline atomic_val_t atomic_get(const atomic_t *target) +{ + return arch_atomic_read(target); +} + +static inline atomic_val_t atomic_set(atomic_t *target, atomic_val_t value) +{ + return arch_atomic_swap(target, value); +} + +static inline atomic_val_t atomic_add(atomic_t *target, atomic_val_t value) +{ + return arch_atomic_add(target, value); +} + +static inline atomic_val_t atomic_sub(atomic_t *target, atomic_val_t value) +{ + return arch_atomic_sub(target, value); +} + +#endif /* __POSIX_ZEPHYR_SYS_ATOMIC_H__ */ diff --git a/src/kernel/arch/host/include/arch/atomic.h b/src/kernel/arch/host/include/arch/atomic.h index 797ba5f5e1d4..180a0ba69140 100644 --- a/src/kernel/arch/host/include/arch/atomic.h +++ b/src/kernel/arch/host/include/arch/atomic.h @@ -6,7 +6,7 @@ * Ranjani Sridharan */ -#ifdef __SOF_ATOMIC_H__ +#if defined(__SOF_ATOMIC_H__) || defined(__POSIX_ZEPHYR_SYS_ATOMIC_H__) #ifndef __ARCH_ATOMIC_H__ #define __ARCH_ATOMIC_H__ @@ -24,24 +24,20 @@ static inline int32_t arch_atomic_read(const atomic_t *a) return __atomic_load_n(&a->value, __ATOMIC_SEQ_CST); } -static inline void arch_atomic_set(atomic_t *a, int32_t value) +/* The swap, add and sub functions return the previous value, like Zephyr */ +static inline int32_t arch_atomic_swap(atomic_t *a, int32_t value) { - __atomic_store_n(&a->value, value, __ATOMIC_SEQ_CST); -} - -static inline void arch_atomic_init(atomic_t *a, int32_t value) -{ - __atomic_store_n(&a->value, value, __ATOMIC_SEQ_CST); + return __atomic_exchange_n(&a->value, value, __ATOMIC_SEQ_CST); } static inline int32_t arch_atomic_add(atomic_t *a, int32_t value) { - return __atomic_add_fetch(&a->value, value, __ATOMIC_SEQ_CST); + return __atomic_fetch_add(&a->value, value, __ATOMIC_SEQ_CST); } static inline int32_t arch_atomic_sub(atomic_t *a, int32_t value) { - return __atomic_sub_fetch(&a->value, value, __ATOMIC_SEQ_CST); + return __atomic_fetch_sub(&a->value, value, __ATOMIC_SEQ_CST); } #elif defined __XCC__ @@ -57,14 +53,15 @@ static inline int32_t arch_atomic_read(const atomic_t *a) return a->value; } -static inline void arch_atomic_set(atomic_t *a, int32_t value) +static inline int32_t arch_atomic_swap(atomic_t *a, int32_t value) { - a->value = value; -} + int32_t tmp; -static inline void arch_atomic_init(atomic_t *a, int32_t value) -{ - arch_atomic_set(a, value); + lock(); + tmp = a->value; + a->value = value; + unlock(); + return tmp; } static inline int32_t arch_atomic_add(atomic_t *a, int32_t value) @@ -99,6 +96,6 @@ static inline int32_t arch_atomic_sub(atomic_t *a, int32_t value) #else -#error "This file shouldn't be included from outside of sof/atomic.h" +#error "This file shouldn't be included from outside of rtos/atomic.h or zephyr/sys/atomic.h" -#endif /* __SOF_ATOMIC_H__ */ +#endif /* __SOF_ATOMIC_H__ || __POSIX_ZEPHYR_SYS_ATOMIC_H__ */ diff --git a/test/ztest/unit/posix_stubs/CMakeLists.txt b/test/ztest/unit/posix_stubs/CMakeLists.txt new file mode 100644 index 000000000000..35297d624f05 --- /dev/null +++ b/test/ztest/unit/posix_stubs/CMakeLists.txt @@ -0,0 +1,28 @@ +# SPDX-License-Identifier: BSD-3-Clause + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(test_posix_stubs) + +set(SOF_ROOT "${PROJECT_SOURCE_DIR}/../../../..") + +include(${SOF_ROOT}/scripts/cmake/misc.cmake) + +# The stubs are compiled as the host builds see them: with the host include +# paths only, so resolves to posix/include, not to Zephyr. +add_library(posix_stubs STATIC posix_stubs.c) +target_include_directories(posix_stubs PRIVATE + ${SOF_ROOT}/posix/include + ${SOF_ROOT}/src/kernel/arch/host/include + ${SOF_ROOT}/src/include +) +target_compile_options(posix_stubs PRIVATE -Wall -Werror) +if(NOT CONFIG_64BIT) + target_compile_options(posix_stubs PRIVATE -m32) +endif() +sof_append_relative_path_definitions(posix_stubs) + +target_include_directories(app PRIVATE ${SOF_ROOT}/src/include) +target_sources(app PRIVATE test_posix_stubs_ztest.c) +target_link_libraries(app PRIVATE posix_stubs) diff --git a/test/ztest/unit/posix_stubs/posix_stubs.c b/test/ztest/unit/posix_stubs/posix_stubs.c new file mode 100644 index 000000000000..0ed91425dd52 --- /dev/null +++ b/test/ztest/unit/posix_stubs/posix_stubs.c @@ -0,0 +1,111 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Uses the host (posix) stubs of the Zephyr APIs. Built with the host include +// paths only, see CMakeLists.txt. + +#include +#include +#include +#include +#include + +#include +#include + +#include "posix_stubs.h" + +static jmp_buf panic_env; +/* volatile: written between setjmp() and longjmp() */ +static volatile uint32_t panic_code; + +/* the host __panic() aborts, return to the caller of the stub instead */ +void __panic(uint32_t p, const char *filename, uint32_t linenum) +{ + (void)filename; + (void)linenum; + panic_code = p; + longjmp(panic_env, 1); +} + +void posix_stub_atomic_seq(struct stub_atomic_seq *seq) +{ + atomic_t a; + + atomic_init(&a, STUB_ATOMIC_INIT); + seq->read_after_init = atomic_read(&a); + seq->set_ret = atomic_set(&a, STUB_ATOMIC_SET); + seq->add_ret = atomic_add(&a, STUB_ATOMIC_ADD); + seq->sub_ret = atomic_sub(&a, STUB_ATOMIC_SUB); + seq->get_final = atomic_get(&a); +} + +void posix_stub_cache(int ret[STUB_CACHE_FUNCS]) +{ + static char buf[64]; + + ret[0] = sys_cache_data_flush_range(buf, sizeof(buf)); + ret[1] = sys_cache_data_invd_range(buf, sizeof(buf)); + ret[2] = sys_cache_data_flush_and_invd_range(buf, sizeof(buf)); + ret[3] = sys_cache_instr_invd_range(buf, sizeof(buf)); +} + +uint32_t posix_stub_assert_no_msg(int cond) +{ + panic_code = 0; + if (!setjmp(panic_env)) { + __ASSERT_NO_MSG(cond); + } + return panic_code; +} + +uint32_t posix_stub_assert_msg(int cond) +{ + panic_code = 0; + if (!setjmp(panic_env)) { + __ASSERT(cond, "condition is %d", cond); + } + return panic_code; +} + +uint32_t posix_stub_assert_no_fmt(int cond) +{ + panic_code = 0; + if (!setjmp(panic_env)) { + __ASSERT(cond); + } + return panic_code; +} + +uint32_t posix_stub_assert_var_fmt(int cond) +{ + const char *fmt = "condition is %d"; + + panic_code = 0; + if (!setjmp(panic_env)) { + __ASSERT(cond, fmt, cond); + } + return panic_code; +} + +uint32_t posix_stub_k_panic(void) +{ + panic_code = 0; + if (!setjmp(panic_env)) { + k_panic(); + } + return panic_code; +} + +int posix_stub_assert_eval_count(void) +{ + /* volatile: written between setjmp() and longjmp() */ + volatile int count = 0; + + panic_code = 0; + if (!setjmp(panic_env)) { + __ASSERT(++count > 0, "count %d", count); + } + return count; +} diff --git a/test/ztest/unit/posix_stubs/posix_stubs.h b/test/ztest/unit/posix_stubs/posix_stubs.h new file mode 100644 index 000000000000..0a27cfbbb9e0 --- /dev/null +++ b/test/ztest/unit/posix_stubs/posix_stubs.h @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Copyright(c) 2026 Intel Corporation. + * + * Interface between the ztest code and posix_stubs.c, which is compiled with + * the host headers. Only plain C types cross it. + */ + +#ifndef __TEST_POSIX_STUBS_H__ +#define __TEST_POSIX_STUBS_H__ + +#include + +/* values returned by one sequence of atomic operations */ +struct stub_atomic_seq { + int32_t read_after_init; + int32_t set_ret; + int32_t add_ret; + int32_t sub_ret; + int32_t get_final; +}; + +#define STUB_ATOMIC_INIT 5 +#define STUB_ATOMIC_SET 10 +#define STUB_ATOMIC_ADD 3 +#define STUB_ATOMIC_SUB 4 + +/* number of sys_cache_*() functions in the stub */ +#define STUB_CACHE_FUNCS 4 + +void posix_stub_atomic_seq(struct stub_atomic_seq *seq); +void posix_stub_cache(int ret[STUB_CACHE_FUNCS]); + +/* return 0 if nothing panicked, else the panic code */ +uint32_t posix_stub_assert_no_msg(int cond); +uint32_t posix_stub_assert_msg(int cond); +/* __ASSERT() without a message, and with a format that is not a literal */ +uint32_t posix_stub_assert_no_fmt(int cond); +uint32_t posix_stub_assert_var_fmt(int cond); +uint32_t posix_stub_k_panic(void); + +/* number of times __ASSERT() evaluates its condition */ +int posix_stub_assert_eval_count(void); + +#endif /* __TEST_POSIX_STUBS_H__ */ diff --git a/test/ztest/unit/posix_stubs/prj.conf b/test/ztest/unit/posix_stubs/prj.conf new file mode 100644 index 000000000000..d34c7781cd0a --- /dev/null +++ b/test/ztest/unit/posix_stubs/prj.conf @@ -0,0 +1,2 @@ +CONFIG_ZTEST=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n diff --git a/test/ztest/unit/posix_stubs/test_posix_stubs_ztest.c b/test/ztest/unit/posix_stubs/test_posix_stubs_ztest.c new file mode 100644 index 000000000000..439a55bac7a2 --- /dev/null +++ b/test/ztest/unit/posix_stubs/test_posix_stubs_ztest.c @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// The host builds of the shared code use the posix/include stubs of the +// Zephyr APIs. Check that the stubs behave like the Zephyr implementations, +// which this test is linked with. + +#include +#include +#include +#include + +#include + +#include "posix_stubs.h" + +/* the same sequence as posix_stub_atomic_seq(), with the Zephyr atomics */ +static void zephyr_atomic_seq(struct stub_atomic_seq *seq) +{ + atomic_t a; + + atomic_set(&a, STUB_ATOMIC_INIT); + seq->read_after_init = atomic_get(&a); + seq->set_ret = atomic_set(&a, STUB_ATOMIC_SET); + seq->add_ret = atomic_add(&a, STUB_ATOMIC_ADD); + seq->sub_ret = atomic_sub(&a, STUB_ATOMIC_SUB); + seq->get_final = atomic_get(&a); +} + +ZTEST(posix_stubs, test_atomic_like_zephyr) +{ + struct stub_atomic_seq host, zephyr; + + posix_stub_atomic_seq(&host); + zephyr_atomic_seq(&zephyr); + + /* set, add and sub return the previous value */ + zassert_equal(host.read_after_init, STUB_ATOMIC_INIT); + zassert_equal(host.set_ret, STUB_ATOMIC_INIT); + zassert_equal(host.add_ret, STUB_ATOMIC_SET); + zassert_equal(host.sub_ret, STUB_ATOMIC_SET + STUB_ATOMIC_ADD); + zassert_equal(host.get_final, STUB_ATOMIC_SET + STUB_ATOMIC_ADD - STUB_ATOMIC_SUB); + + zassert_equal(host.read_after_init, zephyr.read_after_init); + zassert_equal(host.set_ret, zephyr.set_ret); + zassert_equal(host.add_ret, zephyr.add_ret); + zassert_equal(host.sub_ret, zephyr.sub_ret); + zassert_equal(host.get_final, zephyr.get_final); +} + +ZTEST(posix_stubs, test_cache_like_zephyr) +{ + static char buf[64]; + int host[STUB_CACHE_FUNCS]; + int zephyr[STUB_CACHE_FUNCS] = { + sys_cache_data_flush_range(buf, sizeof(buf)), + sys_cache_data_invd_range(buf, sizeof(buf)), + sys_cache_data_flush_and_invd_range(buf, sizeof(buf)), + sys_cache_instr_invd_range(buf, sizeof(buf)), + }; + + posix_stub_cache(host); + + for (int i = 0; i < STUB_CACHE_FUNCS; i++) { + /* native_sim has no cache management, like the host */ + zassert_equal(host[i], -ENOTSUP, "function %d", i); + zassert_equal(host[i], zephyr[i], "function %d", i); + } +} + +ZTEST(posix_stubs, test_assert_pass) +{ + zassert_equal(posix_stub_assert_no_msg(1), 0); + zassert_equal(posix_stub_assert_msg(1), 0); + zassert_equal(posix_stub_assert_no_fmt(1), 0); + zassert_equal(posix_stub_assert_var_fmt(1), 0); +} + +ZTEST(posix_stubs, test_assert_fail_panics) +{ + zassert_equal(posix_stub_assert_no_msg(0), SOF_IPC_PANIC_ASSERT); + zassert_equal(posix_stub_assert_msg(0), SOF_IPC_PANIC_ASSERT); + zassert_equal(posix_stub_assert_no_fmt(0), SOF_IPC_PANIC_ASSERT); + zassert_equal(posix_stub_assert_var_fmt(0), SOF_IPC_PANIC_ASSERT); +} + +ZTEST(posix_stubs, test_assert_evaluates_once) +{ + zassert_equal(posix_stub_assert_eval_count(), 1); +} + +ZTEST(posix_stubs, test_k_panic) +{ + zassert_equal(posix_stub_k_panic(), SOF_IPC_PANIC_ASSERT); +} + +ZTEST_SUITE(posix_stubs, NULL, NULL, NULL, NULL, NULL); diff --git a/test/ztest/unit/posix_stubs/testcase.yaml b/test/ztest/unit/posix_stubs/testcase.yaml new file mode 100644 index 000000000000..e1a119cd0b3d --- /dev/null +++ b/test/ztest/unit/posix_stubs/testcase.yaml @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# Checks that the host (posix) stubs of Zephyr APIs behave like Zephyr + +tests: + sof.unit.posix_stubs: + tags: unit + platform_allow: native_sim + integration_platforms: + - native_sim + build_only: false