Repository navigation
posix: host stubs for the Zephyr atomic, cache and assert APIs #11583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8762199
06ba392
9063ab7
56fc1ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* SPDX-License-Identifier: BSD-3-Clause */ | ||
| /* | ||
| * Copyright(c) 2026 Intel Corporation. All rights reserved. | ||
| * | ||
| * Stub for <zephyr/cache.h> used by the non-Zephyr host builds (the host | ||
| * library and the testbench), which compile the shared code against | ||
| * posix/include. Zephyr builds, native_sim included, use the Zephyr header. | ||
| * | ||
| * The host has no cache to maintain, so these return -ENOTSUP, like Zephyr | ||
| * built without CONFIG_CACHE_MANAGEMENT. | ||
| */ | ||
|
|
||
| #ifndef __POSIX_ZEPHYR_CACHE_H__ | ||
| #define __POSIX_ZEPHYR_CACHE_H__ | ||
|
|
||
| #include <errno.h> | ||
| #include <stddef.h> | ||
|
|
||
| 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__ */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* SPDX-License-Identifier: BSD-3-Clause */ | ||
| /* | ||
| * Copyright(c) 2026 Intel Corporation. All rights reserved. | ||
| * | ||
| * Stub for <zephyr/sys/__assert.h> used by the non-Zephyr host builds (the host | ||
| * library and the testbench), which compile the shared code against | ||
| * posix/include. Zephyr builds, native_sim included, use the Zephyr header. | ||
| * | ||
| * The host checks its assertions in every build, as assert() does: a failed | ||
| * assertion prints the condition and any message, then panics with | ||
| * SOF_IPC_PANIC_ASSERT. | ||
| */ | ||
|
|
||
| #ifndef __POSIX_ZEPHYR_SYS___ASSERT_H__ | ||
| #define __POSIX_ZEPHYR_SYS___ASSERT_H__ | ||
|
|
||
| #include <rtos/panic.h> | ||
| #include <stdarg.h> | ||
| #include <stddef.h> | ||
| #include <stdio.h> | ||
|
|
||
| /* | ||
| * 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__ */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* SPDX-License-Identifier: BSD-3-Clause */ | ||
| /* | ||
| * Copyright(c) 2026 Intel Corporation. All rights reserved. | ||
| * | ||
| * Stub for <zephyr/sys/atomic.h> used by the non-Zephyr host builds (the host | ||
| * library and the testbench), which compile the shared code against | ||
| * posix/include. Zephyr builds, native_sim included, use the Zephyr header. | ||
| * | ||
| * 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 <arch/atomic.h> | ||
| #include <stdint.h> | ||
|
|
||
| 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__ */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ | |
| * Ranjani Sridharan <ranjani.sridharan@linux.intel.com> | ||
| */ | ||
|
|
||
| #ifdef __SOF_ATOMIC_H__ | ||
| #if defined(__SOF_ATOMIC_H__) || defined(__POSIX_ZEPHYR_SYS_ATOMIC_H__) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't follow this. This host abstraction is only used by testbench and cmocka, should not be needed for Zephyr posix builds (SOF fuzzer build, ztests)
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto here |
||
|
|
||
| #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__ */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| cmake_minimum_required(VERSION 3.20.0) | ||
|
|
||
| find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
| project(test_host_stubs) | ||
|
|
||
| set(SOF_ROOT "${PROJECT_SOURCE_DIR}/../../../..") | ||
|
|
||
| include(${SOF_ROOT}/scripts/cmake/misc.cmake) | ||
|
|
||
| # SOF has two kinds of host build. The Zephyr ones (these ztests, the IPC | ||
| # fuzzer) use Zephyr itself. The non-Zephyr host library and testbench | ||
| # compile the shared code against posix/include, which has stubs of some | ||
| # Zephyr APIs. This test checks those stubs against Zephyr: ztest and | ||
| # native_sim are only the harness. | ||
| # | ||
| # host_stubs.c is compiled the way the non-Zephyr host builds compile the | ||
| # shared code: with the host include paths only, so that <zephyr/...> | ||
| # resolves to posix/include, not to Zephyr. | ||
| add_library(host_stubs STATIC host_stubs.c) | ||
| target_include_directories(host_stubs PRIVATE | ||
| ${SOF_ROOT}/posix/include | ||
| ${SOF_ROOT}/src/kernel/arch/host/include | ||
| ${SOF_ROOT}/src/include | ||
| ) | ||
| target_compile_options(host_stubs PRIVATE -Wall -Werror) | ||
| if(NOT CONFIG_64BIT) | ||
| target_compile_options(host_stubs PRIVATE -m32) | ||
| endif() | ||
| sof_append_relative_path_definitions(host_stubs) | ||
|
|
||
| target_include_directories(app PRIVATE ${SOF_ROOT}/src/include) | ||
| target_sources(app PRIVATE test_host_stubs_ztest.c) | ||
| target_link_libraries(app PRIVATE host_stubs) |
Uh oh!
There was an error while loading. Please reload this page.