Skip to content

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

Merged
lgirdwood merged 4 commits into
thesofproject:mainfrom
lgirdwood:topic/zn-rtos-posix-stubs
Dec 3, 2026
Merged

lgirdwood merged 4 commits into
thesofproject:mainfrom
lgirdwood:topic/zn-rtos-posix-stubs

Conversation

@lgirdwood

@lgirdwood lgirdwood commented Dec 1, 2026 •

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").

SOF has two kinds of host build. The Zephyr ones (the native_sim ztests, the IPC fuzzer) use Zephyr itself and are not affected by this PR. The non-Zephyr host builds (the host library and the 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 posix/include 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(). As in Zephyr, the __ASSERT() message is optional and its format does not have to be a string literal. 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.
    • 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: host_stubs: compiles one source file the way the non-Zephyr host builds compile the shared code, with only the host include paths, so that <zephyr/...> resolves to the stubs. The ztest code, built normally against Zephyr, calls it and compares the results with the Zephyr implementations. ztest and native_sim are only the harness; the Zephyr builds do not depend on the stubs. The test checks that:
    • 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, with a literal message, a non-literal format and no message;
    • 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 host_stubs test (6 of 6 cases).
    • The new test fails when the host atomic_add() returns the new value again.
    • The test source that uses the stubs 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.
  • PTL hardware: firmware built from this branch, signed and deployed with the sof-hda-generic topology: boots, and the SOF smoke tests pass (11 of 11: analog playback at 48 kHz S16/S24/S32 and 44.1 kHz, deep-buffer playback, analog capture S16/S32, full duplex, concurrent analog and deep-buffer playback, 10 start/stop cycles, 3 runtime D3 suspend/resume cycles with playback).

🤖 Generated with Claude Code

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 thread posix/include/zephyr/sys/__assert.h Outdated
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__); \

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in e506959: __ASSERT(test, ...) now takes an optional message, like Zephyr. The format and its arguments go to a small vfprintf() helper instead of being concatenated, so a format that is not a literal works too. The posix_stubs test now covers __ASSERT() with no message and with a variable format, in both the passing and the failing case.

@intel-sofci

intel-sofci commented Dec 2, 2026 •

Copy link
Copy Markdown

PR 11583: test results

Run date: 2026-12-03 14:25 UTC

Tested commit: 56fc1eeb3d6c567ab0ec2470a05b407ba2ec4230

mtl pass rate lnl pass rate ptl pass rate wcl pass rate nvl pass rate

@kv2019i kv2019i left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not sure this series is correct. Are we dropping testbench and cmocka? If yes, then we should be able to use existing Zephyr posix layer to provide these functions.

The SOF side src/kernel/src is only needed for non-Zephyr builds like testbench and cmocka. I don't think we should make Zephyr posix builds depend on these.

UPDATE: ok, it seems the SOF host layer does already use the posix includes, so that's why they are mixer. I think the series is correct, although especially the last ztest commit is adding to the confusion between two host builds we support (one with Zephyr and one without).

Comment thread posix/include/zephyr/sys/atomic.h Resolved
*/

#ifdef __SOF_ATOMIC_H__
#if defined(__SOF_ATOMIC_H__) || defined(__POSIX_ZEPHYR_SYS_ATOMIC_H__)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ditto here

@@ -0,0 +1,28 @@
# SPDX-License-Identifier: BSD-3-Clause

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This commit message has issues. Zephyr ztest builds should use Zephyr posix runtime and should not need these wrappers.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ok, this is specifically implementing a Zephyr ztest to implement the stubs that are not really needed by host builds that use Zephyr.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed, the naming and the commit message mixed up the two host builds. In 56fc1ee:

  • The test is now host_stubs instead of posix_stubs, so it no longer reads like the Zephyr posix arch.
  • The commit message and a comment at the top of CMakeLists.txt say what it is: the Zephyr host builds (these ztests, the IPC fuzzer) use Zephyr itself, and the test only uses ztest and native_sim as a harness to check the posix/include stubs of the non-Zephyr host library and testbench. No Zephyr build depends on the stubs.

@kv2019i kv2019i left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'll change my -1 to +1 , see updated comments

@lgirdwood

Copy link
Copy Markdown
Member Author

Not sure this series is correct. Are we dropping testbench and cmocka? If yes, then we should be able to use existing Zephyr posix layer to provide these functions.

The SOF side src/kernel/src is only needed for non-Zephyr builds like testbench and cmocka. I don't think we should make Zephyr posix builds depend on these.

cmocka has gone. testbench is going to sof-utils (as FW repo will be FW code only), and via west testbench will sync the headers/source from sof repo. There is transitional changes as we move, but all non FW files will eventually be elsewhere when done.

lrgirdwo and others added 4 commits December 3, 2026 11:50
The non-Zephyr host builds (the host library and the testbench) compile
the shared code against posix/include, which gives it the SOF atomic
names from rtos/atomic.h. To switch that code to the Zephyr names, these
builds need a <zephyr/sys/atomic.h> that behaves like Zephyr's. Zephyr
builds, native_sim included, keep using the Zephyr header.

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>
In the non-Zephyr host builds (the host library and the testbench),
shared code calls the SOF cache wrappers because posix/include 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.
Zephyr builds, native_sim included, keep using the Zephyr header.

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 non-Zephyr host
builds (the host library and the testbench) have neither
<zephyr/sys/__assert.h> nor k_panic() in posix/include. Code that
already uses the Zephyr names has to be hidden from these builds, such
as the coherent API checks.

Add <zephyr/sys/__assert.h> with __ASSERT() and __ASSERT_NO_MSG(). As
in Zephyr, the __ASSERT() message is optional and its format does not
have to be a string literal. 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.

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().

Zephyr builds, native_sim included, keep using the Zephyr macros.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
SOF has two kinds of host build. The Zephyr ones, such as the native_sim
ztests and the IPC fuzzer, use Zephyr itself and never see the stubs in
posix/include. The non-Zephyr host library and testbench compile the
shared code against posix/include, and nothing tested its stubs of the
Zephyr APIs.

Add a test that compiles one source file the way the non-Zephyr host
builds compile the shared code: with the host include paths only, so
that <zephyr/...> resolves to the stubs. The ztest code, built normally
against Zephyr, calls it and compares the results with the Zephyr
implementations. ztest and native_sim are only the harness here; the
Zephyr builds do not depend on the stubs.

The test checks that:
- 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 and panic
  with SOF_IPC_PANIC_ASSERT on a false one, with a literal message, a
  format that is not a literal and no message, and evaluate the
  condition once;
- k_panic() panics with SOF_IPC_PANIC_ASSERT.

It 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>
@lgirdwood
lgirdwood force-pushed the topic/zn-rtos-posix-stubs branch from e506959 to 56fc1ee Compare December 3, 2026 12:04
@lgirdwood

Copy link
Copy Markdown
Member Author

CI unrelated

@lgirdwood
lgirdwood merged commit 5c00bfa into thesofproject:main Dec 3, 2026
45 of 47 checks passed
@lgirdwood
lgirdwood deleted the topic/zn-rtos-posix-stubs branch December 3, 2026 20:31
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.

5 participants