Skip to content

posix: host stubs for the Zephyr atomic, cache and assert APIs - #11583

Draft
lgirdwood wants to merge 4 commits into
thesofproject:mainfrom
lgirdwood:topic/zn-rtos-posix-stubs
Draft

lgirdwood wants to merge 4 commits into
thesofproject:mainfrom
lgirdwood:topic/zn-rtos-posix-stubs

Conversation

@lgirdwood

Copy link
Copy Markdown
Member

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/lib and src/module against posix/include instead 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

  1. posix: <zephyr/sys/atomic.h> stub: atomic_get(), atomic_set(), atomic_add() and atomic_sub(). As in Zephyr, atomic_set(), atomic_add() and atomic_sub() return the previous value.
    • The GCC host atomics returned the new value from add and sub, while the xt-xcc ones (xt-run testbench) returned the previous one. Both now return the previous value. No host caller uses these return values.
    • rtos/atomic.h keeps only atomic_read() and atomic_init() on top of the stub, as zephyr/include/rtos/atomic.h does on Zephyr.
  2. posix: <zephyr/cache.h> stub: the four sys_cache_*_range() functions. The host has no cache to maintain, so they return -ENOTSUP, like Zephyr built without CONFIG_CACHE_MANAGEMENT.
  3. posix: assert and k_panic() stubs:
    • <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.
    • k_panic() in rtos/panic.h. It takes no panic code, so it reports SOF_IPC_PANIC_ASSERT, the code of a failed assert().
  4. 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:
    • the atomic return values match Zephyr's;
    • the cache functions return -ENOTSUP, as on native_sim;
    • the asserts 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.

Not in this PR

  • The shared code conversions (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

  • Host: the GCC atomic add and sub now return the previous value. No caller uses the value.
  • Firmware: none. No firmware build depends on posix/include or on the host arch headers (checked with ninja -t deps in every configured firmware build of the test matrix).

Testing

  • ZTest: 38 of 38 configurations pass, run as in CI (native_sim, host LLVM, ASan and LSan), including the new posix_stubs test (6 of 6 cases).
    • The new test fails when the host atomic_add() returns the new value again.
    • The stub source compiles with only posix/include, src/kernel/arch/host/include and src/include on its include path.
  • Host: the host library builds. Both atomic implementations compile (GCC, and the xt-xcc one with __XCC__ defined).
  • Testbench: the CI testbench.yml steps 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

lrgirdwo and others added 4 commits December 1, 2026 16:04
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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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__); \
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants