From 1dec2052ada3a43a6598861afa53eafbae949b4d Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Mon, 30 Nov 2026 21:55:59 +0000 Subject: [PATCH] zephyr: use the Zephyr assert and panic macros in kernel-only code On Zephyr, rtos/panic.h defines assert() as __ASSERT_NO_MSG() and sof_panic() as k_panic(), dropping the SOF panic code. Use the Zephyr macros directly in the code that only Zephyr builds: the kernel side of the userspace proxy, debug stream, telemetry, IDC, init, the library manager, the schedulers and zephyr/. Continuation lines are re-aligned and no lines are added or removed, so __LINE__ in verbose asserts does not change. The shared code also builds on the host, where assert() and sof_panic() report the SOF panic code, so it keeps them until the host has Zephyr assert and panic stubs. No functional change: the generated code is the same. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Liam Girdwood --- .../module_adapter/library/userspace_proxy.c | 4 ++-- .../debug/debug_stream/debug_stream_slot.c | 6 +++--- .../debug/telemetry/performance_monitor.c | 2 +- src/kernel/idc/zephyr_idc.c | 6 +++--- src/kernel/init/init.c | 4 ++-- src/kernel/library_manager/lib_manager.c | 2 +- src/kernel/library_manager/lib_notification.c | 2 +- src/kernel/schedule/schedule-kernel.c | 2 +- .../zephyr_dp_schedule_application-kernel.c | 4 ++-- .../zephyr_dp_schedule_thread-kernel.c | 4 ++-- src/kernel/schedule/zephyr_ll-kernel.c | 6 +++--- src/kernel/schedule/zephyr_twb_schedule.c | 2 +- src/schedule/zephyr_dp_schedule_thread.c | 2 +- src/schedule/zephyr_ll.c | 18 +++++++++--------- zephyr/include/rtos/task.h | 4 ++-- zephyr/lib/alloc.c | 2 +- zephyr/schedule.c | 6 +++--- 17 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/kernel/audio/module_adapter/library/userspace_proxy.c b/src/kernel/audio/module_adapter/library/userspace_proxy.c index a1c8faeca4dd..c68de22d9238 100644 --- a/src/kernel/audio/module_adapter/library/userspace_proxy.c +++ b/src/kernel/audio/module_adapter/library/userspace_proxy.c @@ -74,7 +74,7 @@ struct k_work_user *userspace_proxy_register_ipc_handler(struct processing_modul if (user_ctx) { tr_dbg(&userspace_proxy_tr, "Set DP event %p for module %p", (void *)event, (void *)mod); - assert(user_ctx->work_item); + __ASSERT_NO_MSG(user_ctx->work_item); user_ctx->dp_event = event; user_ctx->work_item->event = event; @@ -271,7 +271,7 @@ static int userspace_proxy_invoke(struct userspace_context *user_ctx, uint32_t c goto done; } #else - assert(event); + __ASSERT_NO_MSG(event); k_event_post(event, DP_TASK_EVENT_IPC); #endif diff --git a/src/kernel/debug/debug_stream/debug_stream_slot.c b/src/kernel/debug/debug_stream/debug_stream_slot.c index f501339d6612..c28ae56d31be 100644 --- a/src/kernel/debug/debug_stream/debug_stream_slot.c +++ b/src/kernel/debug/debug_stream/debug_stream_slot.c @@ -108,16 +108,16 @@ static int debug_stream_slot_send_sized(struct debug_stream_record *rec, uint32_ ret = memcpy_s(&buf->data[record_start], buf_remain * sizeof(uint32_t), rec, buf_remain * sizeof(uint32_t)); - assert(!ret); + __ASSERT_NO_MSG(!ret); ret = memcpy_s(&buf->data[0], desc.buf_words * sizeof(uint32_t), ((uint32_t *) rec) + buf_remain, rec_remain * sizeof(uint32_t)); - assert(!ret); + __ASSERT_NO_MSG(!ret); } else { int ret; ret = memcpy_s(&buf->data[record_start], buf_remain * sizeof(uint32_t), rec, record_size * sizeof(uint32_t)); - assert(!ret); + __ASSERT_NO_MSG(!ret); } /* Write record size again after the record */ buf->data[buf->w_ptr] = record_size + 1; diff --git a/src/kernel/debug/telemetry/performance_monitor.c b/src/kernel/debug/telemetry/performance_monitor.c index 54802a3eff2f..fce4ab0c0384 100644 --- a/src/kernel/debug/telemetry/performance_monitor.c +++ b/src/kernel/debug/telemetry/performance_monitor.c @@ -698,7 +698,7 @@ __cold int io_perf_monitor_release_slot(struct io_perf_data_item *item) /* we assign data items ourselves so neither of those should ever fail */ ret = perf_bitmap_free(&self->io_performance_data_bitmap, idx); - assert(!ret); + __ASSERT_NO_MSG(!ret); return ret; } diff --git a/src/kernel/idc/zephyr_idc.c b/src/kernel/idc/zephyr_idc.c index 12727da6dd34..e5023cba649f 100644 --- a/src/kernel/idc/zephyr_idc.c +++ b/src/kernel/idc/zephyr_idc.c @@ -86,7 +86,7 @@ static void idc_handler(struct k_p4wq_work *work) if (msg->size == sizeof(int)) { const int idc_handler_memcpy_err __unused = memcpy_s(&payload, sizeof(payload), msg->payload, msg->size); - assert(!idc_handler_memcpy_err); + __ASSERT_NO_MSG(!idc_handler_memcpy_err); } idc->received_msg.core = msg->core; @@ -153,7 +153,7 @@ static int idc_send_msg_kernel(struct idc_msg *msg, uint32_t mode) } idc_send_memcpy_err = memcpy_s(msg_cp, sizeof(*msg_cp), msg, sizeof(*msg)); - assert(!idc_send_memcpy_err); + __ASSERT_NO_MSG(!idc_send_memcpy_err); /* Same priority as the IPC thread which is an EDF task and under Zephyr */ work->priority = CONFIG_EDF_THREAD_PRIORITY; @@ -164,7 +164,7 @@ static int idc_send_msg_kernel(struct idc_msg *msg, uint32_t mode) if (msg->payload) { idc_send_memcpy_err = memcpy_s(payload->data, sizeof(payload->data), msg->payload, msg->size); - assert(!idc_send_memcpy_err); + __ASSERT_NO_MSG(!idc_send_memcpy_err); /* Sending a message to another core, write back local payload cache */ sys_cache_data_flush_range(payload->data, MIN(sizeof(payload->data), msg->size)); diff --git a/src/kernel/init/init.c b/src/kernel/init/init.c index 46386c242f98..03f57ae4e2e3 100644 --- a/src/kernel/init/init.c +++ b/src/kernel/init/init.c @@ -9,7 +9,7 @@ * initialisation functions. */ -#include +#include #include #include #include @@ -245,7 +245,7 @@ __cold static int primary_core_init(int argc, char *argv[], struct sof *sof) /* init the platform */ if (platform_init(sof) < 0) - sof_panic(SOF_IPC_PANIC_PLATFORM); + k_panic(); #if CONFIG_AMS if (ams_init()) diff --git a/src/kernel/library_manager/lib_manager.c b/src/kernel/library_manager/lib_manager.c index 8cdbce800b51..fb355f2fa832 100644 --- a/src/kernel/library_manager/lib_manager.c +++ b/src/kernel/library_manager/lib_manager.c @@ -528,7 +528,7 @@ __cold static void lib_manager_update_sof_ctx(void *base_addr, uint32_t lib_id) if (!ctx) { tr_err(&lib_manager_tr, "allocation failed"); - sof_panic(SOF_IPC_PANIC_IPC); + k_panic(); } ctx->base_addr = base_addr; diff --git a/src/kernel/library_manager/lib_notification.c b/src/kernel/library_manager/lib_notification.c index 0db5b2a08778..0eff8e6b779d 100644 --- a/src/kernel/library_manager/lib_notification.c +++ b/src/kernel/library_manager/lib_notification.c @@ -95,7 +95,7 @@ void lib_notif_msg_clean(bool leave_one_handle) /* Free all unused elements except the last one */ list_for_item_safe(list_elem, list_tmp, &lib_notif->list) { msg_pool_elem = container_of(list_elem, struct ipc_lib_msg, list); - assert(msg_pool_elem->msg); + __ASSERT_NO_MSG(msg_pool_elem->msg); if (list_is_empty(&msg_pool_elem->msg->list)) { k_spinlock_key_t key; diff --git a/src/kernel/schedule/schedule-kernel.c b/src/kernel/schedule/schedule-kernel.c index af9dd1ac99f3..1e873daa893c 100644 --- a/src/kernel/schedule/schedule-kernel.c +++ b/src/kernel/schedule/schedule-kernel.c @@ -90,7 +90,7 @@ __cold void scheduler_init(int type, const struct scheduler_ops *ops, void *data sch = scheduler_alloc(heap, sizeof(*sch)); if (!sch) { tr_err(&sch_tr, "allocation failed"); - sof_panic(SOF_IPC_PANIC_IPC); + k_panic(); } memset(sch, 0, sizeof(*sch)); list_init(&sch->list); diff --git a/src/kernel/schedule/zephyr_dp_schedule_application-kernel.c b/src/kernel/schedule/zephyr_dp_schedule_application-kernel.c index e839d32f716b..5355fcf5412f 100644 --- a/src/kernel/schedule/zephyr_dp_schedule_application-kernel.c +++ b/src/kernel/schedule/zephyr_dp_schedule_application-kernel.c @@ -262,8 +262,8 @@ int scheduler_dp_task_init(struct task **task, const struct sof_uuid_entry *uid, int ret; /* must be called on the same core the task will be bound to */ - assert(cpu_get_id() == core); - assert(!k_is_user_context()); + __ASSERT_NO_MSG(cpu_get_id() == core); + __ASSERT_NO_MSG(!k_is_user_context()); /* * allocate memory diff --git a/src/kernel/schedule/zephyr_dp_schedule_thread-kernel.c b/src/kernel/schedule/zephyr_dp_schedule_thread-kernel.c index 05c85ad98b1e..9968b26c9ac6 100644 --- a/src/kernel/schedule/zephyr_dp_schedule_thread-kernel.c +++ b/src/kernel/schedule/zephyr_dp_schedule_thread-kernel.c @@ -54,7 +54,7 @@ int scheduler_dp_task_init(struct task **task, int ret; /* must be called on the same core the task will be binded to */ - assert(cpu_get_id() == core); + __ASSERT_NO_MSG(cpu_get_id() == core); /* * allocate memory @@ -152,7 +152,7 @@ int scheduler_dp_task_init(struct task **task, #if IS_ENABLED(CONFIG_SOF_USERSPACE_MOD_IPC_BY_DP_THREAD) pdata->ipc_work_item = userspace_proxy_register_ipc_handler(mod, pdata->event); - assert(pdata->ipc_work_item); + __ASSERT_NO_MSG(pdata->ipc_work_item); #endif } #endif /* CONFIG_USERSPACE */ diff --git a/src/kernel/schedule/zephyr_ll-kernel.c b/src/kernel/schedule/zephyr_ll-kernel.c index 028cae362b55..3110388ff7a0 100644 --- a/src/kernel/schedule/zephyr_ll-kernel.c +++ b/src/kernel/schedule/zephyr_ll-kernel.c @@ -202,7 +202,7 @@ struct k_thread *zephyr_ll_init_context(void *data, struct task *task) return NULL; } - assert(!k_is_user_context()); + __ASSERT_NO_MSG(!k_is_user_context()); k_thread_access_grant(zephyr_domain_thread_tid(sch->ll_domain), sch->lock, pdata->sem_p); tr_dbg(&ll_tr, "granting access to lock %p for thread %p", sch->lock, @@ -215,7 +215,7 @@ struct k_thread *zephyr_ll_init_context(void *data, struct task *task) void user_ll_grant_access(struct k_thread *thread, int core) { - assert(core < CONFIG_CORE_COUNT && zephyr_ll_klocks[core] != NULL); + __ASSERT_NO_MSG(core < CONFIG_CORE_COUNT && zephyr_ll_klocks[core] != NULL); k_thread_access_grant(thread, zephyr_ll_klocks[core]); } @@ -297,7 +297,7 @@ __cold int zephyr_ll_scheduler_init(struct ll_schedule_domain *domain) zephyr_ll_free(sch); return -ENOMEM; } - assert(core < CONFIG_CORE_COUNT && zephyr_ll_locks[core] == NULL); + __ASSERT_NO_MSG(core < CONFIG_CORE_COUNT && zephyr_ll_locks[core] == NULL); zephyr_ll_locks[core] = sch->lock; zephyr_ll_klocks[core] = sch->lock; k_mutex_init(sch->lock); diff --git a/src/kernel/schedule/zephyr_twb_schedule.c b/src/kernel/schedule/zephyr_twb_schedule.c index d7dd71b9b78e..a9c3694200b5 100644 --- a/src/kernel/schedule/zephyr_twb_schedule.c +++ b/src/kernel/schedule/zephyr_twb_schedule.c @@ -404,7 +404,7 @@ int scheduler_twb_task_init(struct task **task, } /* must be called on the same core the task will be binded to */ - assert(cpu_get_id() == core); + __ASSERT_NO_MSG(cpu_get_id() == core); if (thread_priority < 0) { tr_err(&twb_tr, "non preemptible priority"); diff --git a/src/schedule/zephyr_dp_schedule_thread.c b/src/schedule/zephyr_dp_schedule_thread.c index 998d1485dacc..e8b494e192fe 100644 --- a/src/schedule/zephyr_dp_schedule_thread.c +++ b/src/schedule/zephyr_dp_schedule_thread.c @@ -155,7 +155,7 @@ void dp_thread_fn(void *p1, void *p2, void *p3) #if IS_ENABLED(CONFIG_SOF_USERSPACE_MOD_IPC_BY_DP_THREAD) if (event & DP_TASK_EVENT_IPC) { - assert(task_pdata->ipc_work_item); + __ASSERT_NO_MSG(task_pdata->ipc_work_item); userspace_proxy_worker_handler(task_pdata->ipc_work_item); } #endif diff --git a/src/schedule/zephyr_ll.c b/src/schedule/zephyr_ll.c index 95f4cd17620d..73704f1b961c 100644 --- a/src/schedule/zephyr_ll.c +++ b/src/schedule/zephyr_ll.c @@ -59,7 +59,7 @@ static inline void zephyr_ll_lock_acquired(int core) static inline void zephyr_ll_lock_releasing(int core) { - assert(zephyr_ll_lock_owner[core] == k_current_get()); + __ASSERT_NO_MSG(zephyr_ll_lock_owner[core] == k_current_get()); if (--zephyr_ll_lock_depth[core] == 0) zephyr_ll_lock_owner[core] = NULL; } @@ -67,8 +67,8 @@ static inline void zephyr_ll_lock_releasing(int core) #ifdef CONFIG_ASSERT void user_ll_assert_locked(int core) { - assert(core < CONFIG_CORE_COUNT && - zephyr_ll_lock_owner[core] == k_current_get()); + __ASSERT_NO_MSG(core < CONFIG_CORE_COUNT && + zephyr_ll_lock_owner[core] == k_current_get()); } #endif /* CONFIG_ASSERT */ #endif /* CONFIG_SOF_USERSPACE_LL */ @@ -550,9 +550,9 @@ struct task *zephyr_ll_task_alloc(void) */ void user_ll_lock_sched(int core) { - assert(core >= 0 && core < CONFIG_CORE_COUNT && zephyr_ll_locks[core] != NULL); + __ASSERT_NO_MSG(core >= 0 && core < CONFIG_CORE_COUNT && zephyr_ll_locks[core] != NULL); int __maybe_unused ret = k_mutex_lock(zephyr_ll_locks[core], K_FOREVER); - assert(!ret); + __ASSERT_NO_MSG(!ret); zephyr_ll_lock_acquired(core); } @@ -561,17 +561,17 @@ void user_ll_lock_sched(int core) */ void user_ll_unlock_sched(int core) { - assert(core >= 0 && core < CONFIG_CORE_COUNT && zephyr_ll_locks[core] != NULL); + __ASSERT_NO_MSG(core >= 0 && core < CONFIG_CORE_COUNT && zephyr_ll_locks[core] != NULL); zephyr_ll_lock_releasing(core); int __maybe_unused ret = k_mutex_unlock(zephyr_ll_locks[core]); - assert(!ret); + __ASSERT_NO_MSG(!ret); } unsigned int user_ll_unlock_sched_all(int core) { unsigned int depth, i; - assert(core >= 0 && core < CONFIG_CORE_COUNT); + __ASSERT_NO_MSG(core >= 0 && core < CONFIG_CORE_COUNT); if (zephyr_ll_lock_owner[core] != k_current_get()) return 0; @@ -610,7 +610,7 @@ int zephyr_ll_task_init(struct task *task, * schedule_task_init() must be run on target core, see * sof/zephyr/schedule.c:arch_schedulers_get() */ - assert(cpu_get_id() == core); + __ASSERT_NO_MSG(cpu_get_id() == core); ret = schedule_task_init(task, uid, type, priority, run, data, core, flags); diff --git a/zephyr/include/rtos/task.h b/zephyr/include/rtos/task.h index 563b4b999145..f50d6f74528e 100644 --- a/zephyr/include/rtos/task.h +++ b/zephyr/include/rtos/task.h @@ -126,7 +126,7 @@ static inline bool task_is_active(struct task *task) */ static inline enum task_state task_run(struct task *task) { - assert(task->ops.run); + __ASSERT_NO_MSG(task->ops.run); return task->ops.run(task->data); } @@ -148,7 +148,7 @@ static inline void task_complete(struct task *task) */ static inline uint64_t task_get_deadline(struct task *task) { - assert(task->ops.get_deadline); + __ASSERT_NO_MSG(task->ops.get_deadline); return task->ops.get_deadline(task->data); } diff --git a/zephyr/lib/alloc.c b/zephyr/lib/alloc.c index 28b84a53ec1d..dfef930ea096 100644 --- a/zephyr/lib/alloc.c +++ b/zephyr/lib/alloc.c @@ -344,7 +344,7 @@ static void *virtual_heap_alloc(struct vmh_heap *heap, uint32_t flags, size_t by return NULL; } - assert(align == 0 || IS_ALIGNED(mem, align)); + __ASSERT_NO_MSG(align == 0 || IS_ALIGNED(mem, align)); if (flags & SOF_MEM_FLAG_COHERENT) mem = sys_cache_uncached_ptr_get((__sparse_force void __sparse_cache *)mem); diff --git a/zephyr/schedule.c b/zephyr/schedule.c index dea2bd10ab5f..afd4a2c99368 100644 --- a/zephyr/schedule.c +++ b/zephyr/schedule.c @@ -40,7 +40,7 @@ static struct schedulers *_ku_schedulers[CONFIG_CORE_COUNT]; struct schedulers **arch_schedulers_get(void) { #ifdef __ZEPHYR__ - assert(!k_is_user_context()); + __ASSERT_NO_MSG(!k_is_user_context()); #endif return _k_schedulers + cpu_get_id(); } @@ -58,7 +58,7 @@ EXPORT_SYMBOL(arch_schedulers_get); struct schedulers **arch_user_schedulers_get(void) { #ifdef CONFIG_SOF_USERSPACE_LL - assert(!k_is_user_context()); + __ASSERT_NO_MSG(!k_is_user_context()); return _ku_schedulers + cpu_get_id(); #else return NULL; @@ -76,7 +76,7 @@ void arch_user_schedulers_publish(void) #ifdef CONFIG_SOF_USERSPACE_LL unsigned int core = cpu_get_id(); - assert(!k_is_user_context()); + __ASSERT_NO_MSG(!k_is_user_context()); _u_schedulers[core] = _ku_schedulers[core]; #endif }