Repository navigation
Conversation
The host builds of the shared code use the SOF atomic names from posix/include/rtos/atomic.h. To switch that code to the Zephyr names, the host needs a <zephyr/sys/atomic.h> that behaves like Zephyr's. Add the stub with atomic_get(), atomic_set(), atomic_add() and atomic_sub(). Like Zephyr, atomic_set(), atomic_add() and atomic_sub() return the previous value. The GCC version of the host atomics returned the new value from add and sub, while the xt-xcc version (for the xt-run testbench) already returned the previous one. Make the GCC version return the previous value too, and replace the set and init functions with a swap that returns it. No host caller uses these return values. rtos/atomic.h now keeps only atomic_read() and atomic_init() on top of the stub, as on Zephyr. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Shared code calls the SOF cache wrappers because the host has no <zephyr/cache.h>. Add a stub with sys_cache_data_flush_range(), sys_cache_data_invd_range(), sys_cache_data_flush_and_invd_range() and sys_cache_instr_invd_range(). The host has no cache to maintain, so they return -ENOTSUP, like Zephyr built without CONFIG_CACHE_MANAGEMENT. The cached and uncached pointer conversions are not stubbed yet: the host currently reports every pointer as uncached, which does not map directly onto the Zephyr calls. They come with their first user. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Shared code uses assert() and sof_panic() because the host has neither <zephyr/sys/__assert.h> nor k_panic(). Code that already uses the Zephyr names has to be hidden from the host builds, such as the coherent API checks. Add <zephyr/sys/__assert.h> with __ASSERT() and __ASSERT_NO_MSG(). 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. Add k_panic() to rtos/panic.h, which already defines sof_panic(). It takes no panic code, so it reports SOF_IPC_PANIC_ASSERT, the code of a failed assert(): Zephyr also ends a failed assertion with k_panic(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
The host builds use the posix/include stubs instead of the Zephyr headers, and no test covered them. Add a test that compiles a source file the way the host builds do, with only the host include paths, so that <zephyr/...> resolves to the stubs. The ztest code calls it and compares the results with the Zephyr implementations it is linked with: - atomic_set(), atomic_add() and atomic_sub() return the previous value, and the values match Zephyr's; - the cache functions return -ENOTSUP, as on native_sim; - __ASSERT() and __ASSERT_NO_MSG() pass on a true condition, panic with SOF_IPC_PANIC_ASSERT on a false one, and evaluate the condition once; - k_panic() panics with SOF_IPC_PANIC_ASSERT. The test catches panics by replacing the host __panic() with a longjmp() back to the test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The assertion stub rejects calls without a message or with a format-string variable, causing shared code using these supported Zephyr forms to fail compilation in host builds.
1 open issue
What changed in this PR
Adds host-side Zephyr API stubs to support the migration away from SOF wrappers in #11517. Shared-code conversions remain for follow-up PRs; firmware behavior is unchanged.
Changes:
- Adds atomic stubs and aligns host atomic operations with Zephyr’s previous-value return semantics.
- Adds cache, assertion, and
k_panic()stubs. - Adds native_sim tests for atomic/cache compatibility and host assertion/panic behavior.
| File | Description |
|---|---|
test/ztest/unit/posix_stubs/testcase.yaml |
Registers the native_sim test suite. |
test/ztest/unit/posix_stubs/test_posix_stubs_ztest.c |
Checks stub results and panic behavior. |
test/ztest/unit/posix_stubs/prj.conf |
Configures the standalone ztest application. |
test/ztest/unit/posix_stubs/posix_stubs.h |
Defines the interface between host and Zephyr test code. |
test/ztest/unit/posix_stubs/posix_stubs.c |
Exercises host stubs and intercepts panics. |
test/ztest/unit/posix_stubs/CMakeLists.txt |
Compiles stub tests with isolated host include paths. |
src/kernel/arch/host/include/arch/atomic.h |
Adds atomic exchange and returns previous values. |
posix/include/zephyr/sys/atomic.h |
Exposes Zephyr-named atomic operations. |
posix/include/zephyr/sys/__assert.h |
Adds host assertion macros and diagnostics. |
posix/include/zephyr/cache.h |
Adds cache-range stubs returning -ENOTSUP. |
posix/include/rtos/panic.h |
Adds the host k_panic() mapping. |
posix/include/rtos/atomic.h |
Retains SOF compatibility wrappers over the new stubs. |
Review effort: Balanced
Comment on lines
+16
to
+20
| #define __ASSERT(test, fmt, ...) \ | ||
| do { \ | ||
| if (!(test)) { \ | ||
| fprintf(stderr, "ASSERTION FAIL [%s] @ %s:%d\n\t" fmt "\n", \ | ||
| #test, RELATIVE_FILE, __LINE__, ##__VA_ARGS__); \ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Add host stubs for the Zephyr atomic, cache and assert APIs, so that shared code can use the Zephyr names instead of the SOF wrappers. Part of #11517 (step 3, "Host stubs with Zephyr names").
The host builds (host library, testbench) compile
src/audio,src/ipc,src/libandsrc/moduleagainstposix/includeinstead of the Zephyr headers. Shared code can only switch to a Zephyr name when the host has a stub with the same name and the same behaviour. This PR adds those stubs and a test that compares them with Zephyr. The shared code is converted in follow-up PRs.Patches
posix:<zephyr/sys/atomic.h>stub:atomic_get(),atomic_set(),atomic_add()andatomic_sub(). As in Zephyr,atomic_set(),atomic_add()andatomic_sub()return the previous value.rtos/atomic.hkeeps onlyatomic_read()andatomic_init()on top of the stub, aszephyr/include/rtos/atomic.hdoes on Zephyr.posix:<zephyr/cache.h>stub: the foursys_cache_*_range()functions. The host has no cache to maintain, so they return-ENOTSUP, like Zephyr built withoutCONFIG_CACHE_MANAGEMENT.posix: assert andk_panic()stubs:<zephyr/sys/__assert.h>with__ASSERT()and__ASSERT_NO_MSG(). The host checks its assertions in every build, asassert()does: a failed assertion prints the condition and the message, then panics withSOF_IPC_PANIC_ASSERT.k_panic()inrtos/panic.h. It takes no panic code, so it reportsSOF_IPC_PANIC_ASSERT, the code of a failedassert().test: ztest:posix_stubs: compiles one source file the way the host builds do, with only the host include paths, so that<zephyr/...>resolves to the stubs. The ztest code calls it and compares the results with the Zephyr implementations it is linked with:-ENOTSUP, as on native_sim;SOF_IPC_PANIC_ASSERTon a false one, and evaluate the condition once;k_panic()panics withSOF_IPC_PANIC_ASSERT.Not in this PR
atomic_read/atomic_init, cache wrappers,assert()): follow-up PRs, one per API.sys_cache_cached_ptr_get()and friends: the host reports every pointer as uncached (is_uncached()is 1), which does not map directly onto the Zephyr calls. They come with their first user.<zephyr/pm/policy.h>: it needs a host implementation file for its lock counter, so it comes with the IPC handler change that uses it.Functional change
posix/includeor on the host arch headers (checked withninja -t depsin every configured firmware build of the test matrix).Testing
posix_stubstest (6 of 6 cases).atomic_add()returns the new value again.posix/include,src/kernel/arch/host/includeandsrc/includeon its include path.__XCC__defined).testbench.ymlsteps in the SOF docker image (SOF alsa-lib/alsa-utils at the CI commits, test topologies, testbench build,host-testbench.sh): all 19 component tests pass.🤖 Generated with Claude Code