Skip to content

Latest commit

 

History

History

userspace

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

SOF User/Kernel Split — Architecture and Conventions

This is the canonical reference for how Sound Open Firmware (SOF) is divided between kernel (supervisor) context and user (unprivileged) context on top of Zephyr userspace. It defines the conventions that make the split evident in the source layout and easy to maintain.

Status: convention proposal. The execution model and the syscall/marker/Kconfig facts below reflect the code as it is today. The directory taxonomy and the per-directory "Runs in:" banners are the proposed mechanism for making the split visible; they are being rolled out incrementally.

Why this exists

SOF supports several privilege configurations. The default build targets the Linux SOF driver: on Intel ACE3.x and newer targets (e.g. ptl, wcl, nvl), app/CMakeLists.txt automatically includes app/ll_userspace_overlay.conf, which enables CONFIG_USERSPACE and CONFIG_SOF_USERSPACE_LL so the entire LL pipeline and the module-facing IPC run in user threads. With app/internal_ci_overlay.conf the LL userspace overlay is not included and, on PTL (intel_adsp_ace30_ptl), CONFIG_SOF_USERSPACE_PROXY is enabled instead, running individual audio modules in isolated user threads via the proxy container. The kernel keeps the OS/HAL, boot, drivers, and the syscall validators in both cases.

The privilege boundary is real, but nothing in the source layout signals which side a given file belongs to. This document makes that explicit.

Execution model

graph TD
    subgraph Kernel [Kernel / supervisor context - ring 0]
        Boot[Boot & init]
        Drv[Drivers / HAL]
        Dispatch[IPC dispatch]
        LLk[LL scheduler core + domain setup]
        Vrfy["Syscall validators (z_vrfy_*)"]
    end

    subgraph User [User context - ring 3, MMU-isolated]
        LLu[LL pipeline thread]
        DP[DP module threads]
        Mods[Audio modules / copiers]
        IPCu[Module-facing IPC handler]
    end

    Dispatch -->|forwards cmd| IPCu
    LLk -->|starts| LLu
    LLu -->|"syscalls"| Vrfy
    DP -->|"syscalls"| Vrfy
    IPCu -->|"syscalls"| Vrfy
    Vrfy -->|"z_impl_* (validated)"| Drv
Loading
  • Kernel context owns hardware, boot, interrupt handling, memory-domain construction, and the validating half of every syscall (z_vrfy_*).
  • User context runs the audio application: LL pipeline processing, DP modules, copiers, and the module-facing IPC path.
  • The two contexts cross the boundary in two ways:
    1. Syscalls — user code calls a kernel service (see The boundary is the syscall set).
    2. Kernel-to-user shared-memory handoff — the kernel writes IPC message words to shared ipc_user state, signals the user thread via a granted k_event, and waits on a semaphore for the result (ipc_user_forward_cmd() in ipc/ipc-common.c). The user thread wakes, processes the message, and posts the semaphore back.

Directory taxonomy — where code runs

Every src/ subdirectory falls into one of four tiers. This is the primary mechanism for making the split evident: the tier tells you where a directory's code runs before you read a line of it. Top-level directories carry a "Runs in:" banner in their README.md to declare their tier. The banner rollout is in progress; directories in the table that still lack a banner or a README are noted. The src/include/ tree is not classified here because its subdirectories span all tiers.

Tier Meaning Directories
Kernel-only OS integration, HAL, boot, inter-core. Never executes in a user thread. Must not be reached directly from user code — only via a syscall. kernel/ (see its README)
Boundary / bridge Owns the syscalls and the thread / memory-domain setup. Split internally: some translation units run kernel-side, some user-side (e.g. handler-kernel.c vs handler-user.c). schedule/, ipc/, debug/debug_stream/, and (in the Zephyr integration tree) zephyr/syscall/
Application Audio pipeline / component logic that runs in user threads under userspace configs. audio/
Shared library Context-agnostic code linked into whichever context calls it. Must stay "user-safe": no privileged operations (no direct MMIO, no spinlocks held across syscalls, no raw kernel object access) so it is safe when pulled into a user build. math/, module/
Mixed A directory whose files span more than one tier; see its own README for per-file classification. lib/

Kernel-only code lives under src/kernel/, which Zephyr builds into a library of its own, sof_kernel, instead of modules_sof. Source files that mix kernel and user code are split, and their kernel halves moved there, separately.

Classification is grep-verifiable — see Verifying the taxonomy.

zephyr/syscall/ is the model to grow toward. The Zephyr integration tree already collects the validating half of several syscalls in one place — zephyr/syscall/cpu.c, vregion.c, dai.c, sof_dma.c each hold the z_vrfy_* wrappers for their subsystem, physically separated from the z_impl_* logic (in zephyr/lib/*.c and src/kernel/lib/*.c). This is exactly the boundary-tier separation this document promotes; the migration opportunity is to move the still-scattered validators (IPC, module management, library manager, scheduling, debug) toward the same convention.

Mixed files are split: the syscall implementations of lib/dma.c and lib/dai.c, for example, are in kernel/lib/dma-kernel.c and dai-kernel.c. When a single directory genuinely mixes tiers, its README states the exception explicitly.

The boundary is the syscall set

Zephyr crosses the user/kernel boundary with system calls declared __syscall in a header and implemented as two functions:

  • z_vrfy_<name>() — kernel-side validator. Runs in supervisor mode, validates every argument coming from user space (K_SYSCALL_MEMORY_READ/WRITE, K_SYSCALL_OBJ, k_usermode_from/to_copy), then calls the implementation. This is the trust boundary.
  • z_impl_<name>() — the actual logic. Callable directly from kernel code (fast path) or from the validator when the call originates in user space.

These pairs are the entire user/kernel API surface. The table below summarises every SOF syscall by subsystem:

Subsystem Syscalls Declared in Validator (z_vrfy_) / logic (z_impl_)
IPC ipc_msg_send, ipc_msg_list_remove, ipc_msg_reply, ipc_compound_pre_start, ipc_compound_post_start, ipc_wait_for_compound_msg, send_resource_notif sof/ipc/msg.h, sof/ipc/ipc_reply.h, ipc4/handler.h, ipc4/notification.h scattered: ipc/ipc-common.c, kernel/ipc/ipc3/helper.c, kernel/ipc/ipc4/handler-kernel.c, kernel/ipc/ipc4/notification.c
Virtual regions vregion_create_map, vregion_set_interim, vregion_interim_get, vregion_get, vregion_put, vregion_alloc_align, vregion_alloc_coherent_align, vregion_free sof/lib/vregion.h zephyr/syscall/vregion.c / zephyr/lib/vregion.c
DMA sof_dma_get, sof_dma_put, sof_dma_get_attribute, sof_dma_request_channel, sof_dma_release_channel, sof_dma_config, sof_dma_start, sof_dma_stop, sof_dma_get_status, sof_dma_reload, sof_dma_suspend, sof_dma_resume sof/lib/sof_dma.h zephyr/syscall/sof_dma.c / kernel/lib/dma-kernel.c (get/put); remaining 10 are static inline z_impl_* in sof/lib/sof_dma.h
DAI dai_get_device sof/lib/dai-zephyr.h zephyr/syscall/dai.c / kernel/lib/dai-kernel.c
CPU cpu_get_id sof/lib/cpu.h zephyr/syscall/cpu.c / zephyr/lib/cpu.c
Module management mod_dp_balloc_align, mod_dp_alloc_ext, mod_dp_free, mod_dp_free_all, mod_dp_data_blob_handler_new, mod_dp_data_blob_set, mod_dp_fast_get sof/audio/module_adapter/module/generic.h scattered: audio/module_adapter/module/generic.c
Library manager lib_manager_free_module sof/lib_manager.h scattered: kernel/library_manager/lib_manager.c
Scheduling zephyr_ll_task_sem_alloc, zephyr_ll_task_sem_free, scheduler_dp_internal_free, scheduler_dp_ll_tick, scheduler_dp_kick sof/schedule/ll_schedule_domain.h, sof/schedule/dp_schedule.h scattered: kernel/schedule/zephyr_ll-kernel.c, kernel/schedule/zephyr_dp_schedule*-kernel.c
Debug debug_stream_slot_send_record user/debug_stream_slot.h kernel/debug/debug_stream/debug_stream_slot.c
KPB kpb_notifier_schedule sof/audio/kpb.h zephyr/syscall/kpb.c / audio/kpb.c
fast_get fast_get_ll, fast_put_ll sof/lib/fast-get.h scattered: zephyr/lib/fast-get.c

Allowed callers

Zephyr lets any user thread invoke any syscall. With CONFIG_SOF_USERSPACE_SYSCALL_CALLER_CHECK (default y with SOF_USERSPACE_LL), each validator first calls SOF_SYSCALL_CALLER_CHECK() from sof/userspace/syscall_caller.h. That kills any calling thread that is not registered with an allowed role. The registry is kernel-only; never identify the caller from user-writable data such as ipc_user->thread[].

Allowed callers Syscalls
Any user thread, including DP module threads cpu_get_id, all module management syscalls (mod_dp_*), ipc_msg_send, ipc_msg_list_remove, send_resource_notif, kpb_notifier_schedule
IPC user thread only ipc_compound_pre_start, ipc_compound_post_start, ipc_wait_for_compound_msg, lib_manager_free_module
LL user thread only scheduler_dp_ll_tick, scheduler_dp_kick
LL or IPC user thread, or the owner DP thread of the region (lifetime allocator only) vregion_alloc_align, vregion_alloc_coherent_align, vregion_free
LL or IPC user thread all other SOF syscalls

With CONFIG_SOF_USERSPACE_APPLICATION, the DP task list and task memory are writable by DP threads, so the kernel does not walk them. The DP readiness and deadline calculation runs in the LL user thread (scheduler_dp_ll_post_tick()), which passes its results to scheduler_dp_kick(); the kernel takes the DP thread and event objects from its own task records. scheduler_dp_ll_tick is not built when CONFIG_SOF_USERSPACE_LL is also set.

DP modules only get the syscalls needed to implement the module interface. That includes sending notifications: modules queue messages from mod_ipc_msg_init() with ipc_msg_send() and free them with mod_ipc_msg_free(), and dp_task_run() reports processing errors with send_resource_notif(). DP modules have no access to the other IPC syscalls or to DMA, DAI or scheduler syscalls.

The kernel creates the vregion of a DP module and passes it to scheduler_dp_task_init(), which keeps a reference in the kernel-only task record and builds the heap partitions of the DP memory domain from the bounds in the kernel-only struct vregion, not from the allocation context in module memory. It also records the DP thread as the owner of the region (vregion_owner_set()), until the task is freed. The vregion allocation syscalls accept the owner for the lifetime allocator of its own region only (vregion_owner_check()): the kernel never runs the interim heap for a DP thread, and any other DP thread is killed.

Modules call the module resource functions without the dp prefix (mod_alloc_ext(), mod_free(), mod_fast_get() and so on), which are plain functions. For an LL module in the LL or IPC user thread they run in user mode, on the module's resource pool and the LL user heap, under a sys_mutex in LL memory. That path is only taken for the LL modules that module_adapter_mem_alloc() registered in a list in LL memory when it created them in user mode (mod_res_user_add()), not by the processing domain: the comp_dev and module objects of a DP module are in DP memory, so a DP thread could make its module look like an LL module. mod_fast_get() then gets a shared read-only copy with the fast_get_ll() syscall, see below, or makes a private copy when the fast_get area is full. For DP modules, and in kernel code, they call the mod_dp_* syscalls. Those validators reject LL modules and DP modules whose allocation context has no live vregion, and pin the vregion during the call. They still read the context from DP memory, so a DP thread that rewrites it during the call is not stopped.

A message sent by a user thread is never linked into the kernel send queue. ipc_msg_send() copies its header and payload (at most CONFIG_SOF_IPC_USER_MSG_MAX_SIZE bytes) once into one of CONFIG_SOF_IPC_USER_MSG_SLOTS kernel-owned slots and queues the slot. The slot is found again by the address of the user message and the memory domain of the sender, so sending a queued message again updates the queued copy, and ipc_msg_list_remove() only removes messages of the caller's own domain. msg->list and msg->callback of a user message are not used. With CONFIG_SOF_USERSPACE_LL, the send queue, its lock and the IPC processing task are kept in kernel-only struct ipc_kstate, not in the user-writable struct ipc; kernel code accesses them with ipc_ks().

The zephyr/syscall/*.c files hold the validators in one place per subsystem; the rows marked scattered keep their z_vrfy_ next to the kernel logic and are the migration targets.

Most syscall declarations are gated by #if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION); outside that guard (non-Zephyr host-test and ztest builds) the same name is a plain macro or inline calling z_impl_* directly. Non-userspace Zephyr builds (no CONFIG_USERSPACE) still use the __syscall declaration path, which Zephyr compiles to a direct z_impl_* call without the privilege-elevation trap.

LL memory region

With CONFIG_SOF_USERSPACE_LL, the memory of the LL and IPC user threads is one region of CONFIG_SOF_ZEPHYR_SYS_USER_HEAP_SIZE bytes, allocated from the kernel heap at boot (src/kernel/schedule/zephyr_ll_user.c) and split into page-aligned parts:

Part Mapping in the LL memory domain Allocator
Kernel RO area (CONFIG_SOF_ZEPHYR_LL_KAREA_RO_SIZE) read-only zephyr_ll_karea_alloc(ZEPHYR_LL_KAREA_RO, ...)
fast_get area (CONFIG_SOF_ZEPHYR_LL_FG_AREA_SIZE) read-only, cached alias only zephyr_ll_fg_alloc(), zephyr_ll_fg_free()
Kernel RW area (CONFIG_SOF_ZEPHYR_LL_KAREA_RW_SIZE) read-write zephyr_ll_karea_alloc(ZEPHYR_LL_KAREA_RW, ...)
LL user heap (the rest) read-write sof_heap_alloc(sof_sys_user_heap_get(), ...), in user mode only

The kernel areas are for objects that kernel code creates and LL user threads access. Their allocator is a bump allocator with kernel-only state and no free, so use it for objects that live until the firmware is reset. The Xtensa MMU has one write permission for both rings, so the RO area is also read-only to kernel code running in the LL memory domain, such as system calls from LL threads: only kernel threads can write objects there.

Objects in the kernel areas:

  • RO: the user scheduler list heads and descriptors (struct schedule_data, including the scheduler ops), the LL allocation context ipc->ll_alloc, and the drivers of the loadable modules (struct comp_driver, including the component ops), which are never unregistered. Kernel code finds the user scheduler lists through a kernel-only copy of the per-core list heads.
  • RW: struct ipc_user, the IPC data buffer ipc->comp_data, the LL scheduler state (struct zephyr_ll), the LL scheduling domain and its Zephyr domain data, the DP scheduler data, the LL tasks that kernel threads create at boot together with their task data, and the DMA channel arrays. A channel array is allocated on the first sof_dma_get() of its controller and kept in the kernel-only struct sof_dma_kinfo for the next one.

DP modules are created by kernel code, but their allocation context, pin format arrays and configuration buffers are in the module's own vregion, released with it, and the IPC user thread adds the component to the component list. So creating a DP module does not allocate from the LL user heap in kernel code.

The LL timer interrupt and the LL thread setup and teardown use kernel-only copies of the domain data pointer, the per-core semaphores and the LL threads, not the user-writable domain data.

Chain DMA messages are forwarded to the IPC user thread like the pipeline messages, so the chain DMA component is created, run and freed in user space. The kernel only looks up the driver, and takes the runtime idle PM lock of a started chain itself, tracked in a kernel-only mask of host DMA channels. The IPC gateway is not supported and is disabled in the LL overlay.

User threads can write the dma[] array, so kernel code drives DMA through the sof_dma_*() wrappers, which take the Zephyr device from struct sof_dma_kinfo (sof_dma_kdev()), and never calls the Zephyr DMA API on dma->z_dev.

The LL user heap keeps its sys_heap metadata in user-writable memory, so its allocator runs in user mode only (schedule/zephyr_ll_user_heap.c). The sys_heap object, its sys_mutex lock and the usage counters are in the system user partition. The kernel initialises the heap at boot and does not parse it after that: in kernel mode, including system calls, sof_heap_alloc() and sof_heap_free() refuse the LL user heap and log the caller, and telemetry reads only the usage counters, as numbers. In user mode they call the allocator directly, with no system call, and kill the thread for any other heap. A user thread that corrupts the heap can only make LL and IPC user threads fault. zephyr_ll_user_heap() returns a kernel-only struct k_heap that is never initialised: its address only selects the LL user heap.

Each pipeline also has a virtual region (vregion) for itself and its LL modules, mapped read-write into the LL memory domain. Allocations made before the pipeline is complete come from the lifetime allocator of the region, whose state is kernel-only, through the vregion_* syscalls. The interim heap, used after that, keeps its sys_heap metadata in the region, so it runs in user mode too (lib/vregion_user.c). The pipeline regions are created with VREGION_FLAG_USER_HEAP: vregion_set_interim() then only reserves the rest of the region for the interim heap, and kernel code, including the vregion_* syscalls, refuses to allocate from or free to that range and logs the caller. When the pipeline is complete, the IPC user thread gets the range with the vregion_interim_get() syscall, which only the LL and IPC user threads may call, and initialises the heap in it. The heap header at the start of the range holds the sys_heap object and the usage counters. Telemetry copies the counters as numbers in system calls of the LL and IPC user threads, as kernel threads must not access the region memory. The user threads find the header of a region in a table in the system user partition, under a sys_mutex there, so sof_ctx_alloc(), sof_ctx_free() and the object pools allocate from the heap with no syscall, through vregion_user_alloc_align() and vregion_user_free(). The heap is removed when the pipeline is freed, after its modules. The kernel still runs the interim heap of the DP module regions, which are mapped into the LL memory domain too.

The fast_get area holds the fast_get() copies of the LL modules, such as SRC coefficients. LL and IPC user threads get them with the fast_get_ll() and fast_put_ll() syscalls, and all users of the same data share a copy. The allocator is a bitmap of 256-byte blocks in kernel memory, and the entries of the copies are in kernel memory too, marked as LL copies, which fast_get() and fast_put() never use. Only the cached alias of the area is mapped into the LL memory domain, so kernel code writes a copy through the uncached alias, also in the syscall, and invalidates the cached alias on the calling core at every get, as the core may still hold lines of an earlier copy at the same address. LL threads cannot change a shared copy. A user thread can release copies that other LL modules use, which only affects LL modules.

DP modules keep their copies in their own memory: small copies in the module vregion, and large copies from vpage_alloc() instead of the heap of the module allocation context, which is the LL user heap, mapped read-only into the domain of each user. Kernel code still follows some pointers stored in user-writable memory, for example task->sch and the domain ops.

Memory-partition markers

User threads can only touch memory that is mapped into their memory domain. SOF exposes shared globals to user threads through Zephyr application memory partitions. The markers live in rtos/userspace_helper.h:

Marker Partition Availability Use for
APP_TASK_DATA / APP_TASK_BSS common_partition CONFIG_USERSPACE Data shared with the userspace proxy worker thread and similar kernel-managed user tasks; not mapped into DP module domains in SOF_USERSPACE_APPLICATION mode
APP_SYSUSER_DATA / APP_SYSUSER_BSS sysuser_partition CONFIG_SOF_USERSPACE_LL Data shared with the LL user thread and the LL userspace IPC thread
K_APP_DMEM(part) / K_APP_BMEM(part) kernel-defined partition CONFIG_USERSPACE Per-subsystem partitions defined and mapped by kernel code (e.g. ipc_context_part in ipc-common.c); not intended for use by user components directly

When userspace is disabled, all four SOF markers expand to nothing, so annotated globals are ordinary variables in non-userspace builds.

Rule: any global that a kernel-managed user thread reads or writes must carry a partition marker at its declaration site. Conversely, marked data is user-writable, so kernel code must never trust it — re-validate on every use. DP modules running under SOF_USERSPACE_APPLICATION access data through their per-module memory domain set up by the proxy or DP thread infrastructure, not through APP_TASK_* / APP_SYSUSER_* markers. See kernel/schedule/zephyr_ll_user.c: zephyr_ll_user_heap() returns the address of a kernel-only heap handle rather than a pointer stored in user-writable memory.

Kconfig hierarchy

The userspace behaviour is assembled from a small option tree (source of truth: zephyr/Kconfig).

graph TD
    US["USERSPACE (Zephyr MMU)"]
    US --> DMA[SOF_USERSPACE_INTERFACE_DMA]
    US --> VREG["SOF_USERSPACE_INTERFACE_VREGION (needs SOF_VREGIONS)"]
    US --> LL[SOF_USERSPACE_LL]
    LL -.selects.-> DMA
    LL -.selects if SOF_VREGIONS.-> VREG
    US --> SHEAP[SOF_USERSPACE_USE_SHARED_HEAP]
    US --> DHEAP[SOF_USERSPACE_USE_DRIVER_HEAP]
    PROXY[SOF_USERSPACE_PROXY]
    PROXY -.selects.-> SHEAP
    PROXY -.selects.-> DHEAP
    PROXY --> MODIPC[SOF_USERSPACE_MOD_IPC_BY_DP_THREAD]
    APP["SOF_USERSPACE_APPLICATION (auto = USERSPACE && !PROXY, needs SOF_VREGIONS)"]
Loading

Key options:

  • SOF_USERSPACE_LL — run LL pipelines in user threads (depends on USERSPACE; selects the DMA/VREGION interfaces). This is what ll_userspace_overlay.conf turns on.
  • SOF_USERSPACE_PROXY — run modules inside a proxy container that forwards kernel-side calls to user-privileged module code (selects the driver + shared heaps).
  • SOF_USERSPACE_APPLICATION — not user-settable; a shortcut for USERSPACE && !SOF_USERSPACE_PROXY (depends on SOF_VREGIONS), used to switch the DP scheduler between the _application and _thread implementations in schedule/CMakeLists.txt.
  • SOF_USERSPACE — a separate WIP flag for userspace modules; do not confuse with USERSPACE (the Zephyr option) or SOF_USERSPACE_LL.

Rules for placing new code

  1. Pick the tier first. OS/HAL/boot → a kernel-only directory. Audio application logic → audio/. Reusable pure code → a shared-library directory (keep it user-safe).
  2. Cross the boundary only via a syscall. Add a __syscall declaration, put the z_vrfy_ validator in the boundary tier — preferably in zephyr/syscall/<subsystem>.c alongside the existing ones — and validate every argument. Start the validator with SOF_SYSCALL_CALLER_CHECK() and add the syscall to the allowed callers table. Read each field of a user structure once (k_usermode_from_copy()), and never let the kernel follow, free through or link a pointer that lives in user-writable memory.
  3. Mark shared globals. Any global a kernel-managed user thread (LL user thread, userspace IPC thread, proxy worker) reads/writes gets APP_TASK_*, APP_SYSUSER_*, or a K_APP_* partition marker — and the kernel re-validates it. DP modules in application mode use their per-module domain instead; see the DP scheduler and userspace proxy for how that is wired.
  4. Grant kernel objects explicitly. User threads need k_object_access_grant() / k_thread_access_grant() for each kernel object they use.
  5. Split by suffix inside boundary directories. Where a subsystem has both sides, keep the existing -kernel / -user file split so the two contexts stay physically separate.

Verifying the taxonomy

The tier of any directory can be confirmed from its source signals:

# Boundary code: declares or implements syscalls
grep -rl 'z_vrfy_\|__syscall' src/<dir>

# User-visible data: partition markers
grep -rl 'APP_SYSUSER\|APP_TASK\|K_APP_BMEM\|K_APP_DMEM\|K_APPMEM_PARTITION' src/<dir>

# User threads / object grants / domains
grep -rl 'K_USER\|_access_grant\|mem_domain' src/<dir>

A kernel-only directory returns nothing for all three. A boundary directory hits the first (and usually the third). The application/shared tiers hit markers but should not host z_vrfy_ validators.

Related files