diff --git a/app/os_linux_overlay.conf b/app/os_linux_overlay.conf index 18024b4e6106..34a272b2a9da 100644 --- a/app/os_linux_overlay.conf +++ b/app/os_linux_overlay.conf @@ -25,3 +25,5 @@ CONFIG_LOG_RUNTIME_DEFAULT_LEVEL=3 # Disable additional VMH buffers to use memory for virtual regions. CONFIG_VIRTUAL_HEAP_EXTENDED=n + +CONFIG_MTRACE_LOG_CACHE_BUF_SIZE=32768 \ No newline at end of file diff --git a/app/prj.conf b/app/prj.conf index b989ac6d1ac4..be69aa346ac4 100644 --- a/app/prj.conf +++ b/app/prj.conf @@ -24,7 +24,7 @@ CONFIG_LOG_MODE_DEFERRED=y CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD=1 # Frontend buffer must be large enough to cover all # typical bursts of log messages. -CONFIG_LOG_BUFFER_SIZE=4096 +CONFIG_LOG_BUFFER_SIZE=16384 # Use stack that is sufficient for SOF backends CONFIG_LOG_PROCESS_THREAD_STACK_SIZE=4096 # Use Linux kernel style timestamp format diff --git a/src/audio/module_adapter/module/generic.c b/src/audio/module_adapter/module/generic.c index 42a8356f0e68..317ff1266009 100644 --- a/src/audio/module_adapter/module/generic.c +++ b/src/audio/module_adapter/module/generic.c @@ -137,7 +137,11 @@ __cold int module_init(struct processing_module *mod) ret = scheduler_dp_thread_ipc(mod, SOF_IPC4_MOD_INIT_INSTANCE, ¶m); } else #endif + { + comp_info(dev, "module_init: calling module init callback"); ret = interface->init(mod); + comp_info(dev, "module_init: module init callback returned %d", ret); + } if (ret) { comp_err(dev, "error %d: module specific init failed", ret); diff --git a/src/audio/module_adapter/module_adapter.c b/src/audio/module_adapter/module_adapter.c index b0075453e434..782de7abb85d 100644 --- a/src/audio/module_adapter/module_adapter.c +++ b/src/audio/module_adapter/module_adapter.c @@ -355,6 +355,10 @@ __cold struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv, goto err; } + comp_info(dev, "module adapter: proc_domain=%u period=%u", + (unsigned int)dev->ipc_config.proc_domain, + (unsigned int)dev->period); + module_adapter_reset_data(dst); dev->state = COMP_STATE_READY; diff --git a/src/audio/volume/volume_ipc4.c b/src/audio/volume/volume_ipc4.c index 7ca9b90c1ef6..fc4cbb38d701 100644 --- a/src/audio/volume/volume_ipc4.c +++ b/src/audio/volume/volume_ipc4.c @@ -31,6 +31,9 @@ LOG_MODULE_DECLARE(volume, CONFIG_SOF_LOG_LEVEL); #include "volume.h" +/* Debug only: remove to disable the deliberate fault in volume_init() */ +#define VOLUME_INJECT_FAULT 1 + void set_volume_process(struct vol_data *cd, struct comp_dev *dev, bool source_or_sink) { cd->scale_vol = vol_get_processing_function(dev, cd); @@ -115,8 +118,17 @@ int volume_init(struct processing_module *mod) uint8_t channel_cfg; uint8_t channel; bool all_channels; + + +#ifdef VOLUME_INJECT_FAULT + /* Debug only: deliberately write to NULL to raise a DSP exception */ + comp_err(dev, "volume_init: injecting NULL pointer write fault"); + *(volatile uint32_t *)NULL = 0xdeadbeef; +#endif + uint32_t instance_id = IPC4_INST_ID(dev_comp_id(dev)); + if (instance_id >= IPC4_MAX_PEAK_VOL_REG_SLOTS) { comp_err(dev, "instance_id %u out of array bounds.", instance_id); return -EINVAL; @@ -147,24 +159,34 @@ int volume_init(struct processing_module *mod) return -EINVAL; } + comp_info(dev, "volume_init: config validated, channels %u all_channels %u", + channels_count, (unsigned int)all_channels); + + comp_info(dev, "volume_init: allocating vol_data, size %zu", sizeof(struct vol_data)); cd = mod_zalloc(mod, sizeof(struct vol_data)); - if (!cd) + if (!cd) { + comp_err(dev, "volume_init: vol_data allocation failed"); return -ENOMEM; + } + comp_info(dev, "volume_init: vol_data allocated %p", cd); /* * malloc memory to store current volume 4 times to ensure the address * is 8-byte aligned for multi-way xtensa intrinsic operations. */ + comp_info(dev, "volume_init: allocating volume buffer, size %zu", vol_size); cd->vol = mod_alloc_align(mod, vol_size, SOF_FRAME_BYTE_ALIGN); if (!cd->vol) { mod_free(mod, cd); comp_err(dev, "Failed to allocate %d", vol_size); return -ENOMEM; } + comp_info(dev, "volume_init: volume buffer allocated %p", cd->vol); /* malloc memory to store temp peak volume 4 times to ensure the address * is 8-byte aligned for multi-way xtensa intrinsic operations. */ + comp_info(dev, "volume_init: allocating peak buffer, size %zu", vol_size); cd->peak_vol = mod_zalloc(mod, vol_size); if (!cd->peak_vol) { mod_free(mod, cd->vol); @@ -172,9 +194,11 @@ int volume_init(struct processing_module *mod) comp_err(dev, "Failed to allocate %d for peak_vol", vol_size); return -ENOMEM; } + comp_info(dev, "volume_init: peak buffer allocated %p", cd->peak_vol); md->private = cd; + comp_info(dev, "volume_init: initializing channels"); for (channel = 0; channel < channels_count; channel++) { channel_cfg = all_channels ? 0 : channel; @@ -188,11 +212,15 @@ int volume_init(struct processing_module *mod) /* set muted as false*/ cd->muted[channel] = false; + comp_info(dev, "volume_init: channel %u initialized, target volume %#x", + (unsigned int)channel, target_volume[channel]); } + comp_info(dev, "volume_init: initializing ramp"); init_ramp(cd, vol->config[0].curve_duration, target_volume[0]); volume_set_ramp_channel_counter(cd, channels_count); + comp_info(dev, "volume_init: ramp initialized"); cd->mailbox_offset = offsetof(struct ipc4_fw_registers, peak_vol_regs); cd->mailbox_offset += instance_id * sizeof(struct ipc4_peak_volume_regs); @@ -200,8 +228,10 @@ int volume_init(struct processing_module *mod) cd->attenuation = 0; cd->is_passthrough = false; + comp_info(dev, "volume_init: mailbox offset %#x, resetting state", cd->mailbox_offset); volume_reset_state(cd); + comp_info(dev, "volume_init: completed"); return 0; } @@ -477,4 +507,3 @@ int volume_peak_prepare(struct vol_data *cd, struct processing_module *mod) return 0; } - diff --git a/src/include/sof/ipc/common.h b/src/include/sof/ipc/common.h index dab67d29d1de..382f76826bcb 100644 --- a/src/include/sof/ipc/common.h +++ b/src/include/sof/ipc/common.h @@ -91,6 +91,14 @@ struct ipc_user_xcore { struct ipc_user { struct k_thread *thread[IPC_DSP_CORE_COUNT]; + /** @brief Per-core helper thread objects used for INIT_INSTANCE processing */ + struct k_thread *init_thread[IPC_DSP_CORE_COUNT]; + /** @brief Per-core stacks for the INIT_INSTANCE helper threads */ + k_thread_stack_t *init_stack[IPC_DSP_CORE_COUNT]; + /** @brief Core the currently forwarded command is executed on */ + unsigned int cmd_core; + /** @brief Result of the INIT_INSTANCE helper thread */ + int init_result; struct k_sem *sem; struct k_event *event[IPC_DSP_CORE_COUNT]; /** @brief Copy of IPC4 message primary word forwarded to user thread */ diff --git a/src/ipc/ipc-common.c b/src/ipc/ipc-common.c index a7e4d57dbdf5..d46ff486a07f 100644 --- a/src/ipc/ipc-common.c +++ b/src/ipc/ipc-common.c @@ -294,7 +294,7 @@ void ipc_user_thread_fn(void *p1, void *p2, void *p3) IPC_USER_EVENT_XCORE, false, K_FOREVER); - LOG_DBG("IPC user wake, mask %u", mask); + LOG_INF("IPC user wake, mask %u", mask); /* idc_cmd_user() is only built with SMP, there are no requests without */ if (IS_ENABLED(CONFIG_SMP) && (mask & IPC_USER_EVENT_XCORE)) { diff --git a/src/ipc/ipc4/handler-user.c b/src/ipc/ipc4/handler-user.c index 232e81800201..cf60b58ac28f 100644 --- a/src/ipc/ipc4/handler-user.c +++ b/src/ipc/ipc4/handler-user.c @@ -1050,6 +1050,64 @@ __cold int ipc4_process_large_config_set(struct ipc4_message_request *ipc4) } #ifdef CONFIG_SOF_USERSPACE_LL +/** + * @brief Helper thread entry performing SOF_IPC4_MOD_INIT_INSTANCE. + * + * Runs in user space on the same core as the IPC user thread that spawned + * it. The IPC message words and driver pointer are taken from the shared + * @c ipc_user context (@p p1); the result is stored in + * @c ipc_user->init_result. + * + * @param p1 struct ipc_user pointer + * @param p2 unused + * @param p3 unused + */ +static void ipc_user_init_instance_fn(void *p1, void *p2, void *p3) +{ + struct ipc_user *ipc_user = p1; + struct ipc4_message_request msg; + const struct comp_driver *drv = ipc_user->init_drv; + struct comp_dev *dev; + int result; + + ARG_UNUSED(p2); + ARG_UNUSED(p3); + + msg.primary.dat = ipc_user->ipc_msg_pri; + msg.extension.dat = ipc_user->ipc_msg_ext; + + /* User thread creates the component: drv->ops.create() runs in + * user-space so untrusted module code does not execute with kernel + * privileges. init_drv is the original kernel pointer. + */ + tr_info(&ipc_tr, "INIT_INSTANCE: driver received %p", drv); + ipc_user->init_drv = NULL; + if (!drv) { + tr_err(&ipc_tr, "INIT_INSTANCE: driver missing"); + ipc_user->init_result = IPC4_MOD_NOT_INITIALIZED; + return; + } + + dev = comp_new_ipc4_user(&msg, drv); + if (!dev) { + tr_err(&ipc_tr, "INIT_INSTANCE: component creation failed"); + ipc_user->init_result = IPC4_MOD_NOT_INITIALIZED; + return; + } + + result = ipc4_add_comp_dev(dev); + if (result != IPC4_SUCCESS) { + tr_err(&ipc_tr, "INIT_INSTANCE: component registration failed %d", result); + ipc_user->init_result = result; + return; + } + + comp_update_ibs_obs_cpc(dev); + + ipc_user->init_result = 0; + tr_info(&ipc_tr, "INIT_INSTANCE: completed"); +} + /* * IPC4 implementation of the user-thread dispatch hook. Runs in the * user-space IPC thread; reconstructs the IPC4 message from the forwarded @@ -1064,6 +1122,8 @@ int ipc_user_thread_dispatch(struct ipc_user *ipc_user) msg.primary.dat = ipc_user->ipc_msg_pri; msg.extension.dat = ipc_user->ipc_msg_ext; + tr_info(&ipc_tr, "user dispatch: message primary type %#x", msg.primary.r.type); + ipc_user->reply_ext = 0; if (msg.primary.r.msg_tgt == SOF_IPC4_MESSAGE_TARGET_MODULE_MSG) { @@ -1097,34 +1157,45 @@ int ipc_user_thread_dispatch(struct ipc_user *ipc_user) break; } case SOF_IPC4_MOD_INIT_INSTANCE: { - /* User thread creates the component — - * drv->ops.create() runs in user-space so untrusted - * module code does not execute with kernel privileges. - * - * init_drv = original kernel pointer + /* + * Run the instance creation in a helper thread pinned to + * the same core as this IPC user thread, then join it. + * Message words and init_drv are already in ipc_user. */ - const struct comp_driver *drv = ipc_user->init_drv; + unsigned int core = ipc_user->cmd_core; + struct k_thread *thread; - ipc_user->init_drv = NULL; - if (!drv) { + if (core >= CONFIG_CORE_COUNT || !ipc_user->init_thread[core]) { result = IPC4_MOD_NOT_INITIALIZED; break; } - struct comp_dev *dev = comp_new_ipc4_user(&msg, drv); - - if (!dev) { + thread = ipc_user->init_thread[core]; + ipc_user->init_result = IPC4_MOD_NOT_INITIALIZED; + + k_thread_create(thread, ipc_user->init_stack[core], + CONFIG_SOF_IPC_USER_THREAD_STACK_SIZE, + ipc_user_init_instance_fn, ipc_user, NULL, NULL, + -1, K_USER | K_INHERIT_PERMS, K_FOREVER); + result = sof_thread_cpu_pin(thread, core); + if (result) { + tr_err(&ipc_tr, "INIT_INSTANCE: cpu pin failed %d", result); + /* never started: abort so the objects can be reused */ + k_thread_abort(thread); result = IPC4_MOD_NOT_INITIALIZED; break; } + k_thread_name_set(thread, "ipc_init"); + k_thread_start(thread); - result = ipc4_add_comp_dev(dev); - if (result != IPC4_SUCCESS) + result = k_thread_join(thread, K_FOREVER); + if (result) { + tr_err(&ipc_tr, "INIT_INSTANCE: join failed %d", result); + result = IPC4_MOD_NOT_INITIALIZED; break; + } - comp_update_ibs_obs_cpc(dev); - - result = 0; + result = ipc_user->init_result; break; } case SOF_IPC4_MOD_DELETE_INSTANCE: { diff --git a/src/kernel/ipc/ipc-common-kernel.c b/src/kernel/ipc/ipc-common-kernel.c index 832a14a1a9de..c46521aa8313 100644 --- a/src/kernel/ipc/ipc-common-kernel.c +++ b/src/kernel/ipc/ipc-common-kernel.c @@ -469,6 +469,10 @@ SOF_DEFINE_REG_UUID(sec_core_init); static K_THREAD_STACK_ARRAY_DEFINE(ipc_user_stack, CONFIG_CORE_COUNT, CONFIG_SOF_IPC_USER_THREAD_STACK_SIZE); +/* Stacks for the per-core helper threads spawned by the IPC user thread */ +static K_THREAD_STACK_ARRAY_DEFINE(ipc_init_stack, CONFIG_CORE_COUNT, + CONFIG_SOF_IPC_USER_THREAD_STACK_SIZE); + /* * Kernel-only copies of the IPC user thread state. struct ipc_user and the * ipc->ipc_user_pdata pointer are both writable by user threads, so kernel @@ -513,7 +517,7 @@ int ipc_user_forward_cmd(uint32_t primary, uint32_t extension, unsigned int core pdata->ipc_msg_pri = primary; pdata->ipc_msg_ext = extension; pdata->ipc = ipc; - + pdata->cmd_core = core; /* Prevent host completion until user thread finishes */ key = k_spin_lock(&ipc_ks(ipc)->lock); ipc_ks(ipc)->task_mask |= IPC_TASK_IN_THREAD; @@ -545,6 +549,7 @@ __cold static int ipc_user_init_thread(struct ipc_user *ipc_user, unsigned int c { char thread_name[] = "ll_user0"; struct k_thread *thread; + struct k_thread *init_thread; struct k_event *event; int ret; @@ -570,6 +575,13 @@ __cold static int ipc_user_init_thread(struct ipc_user *ipc_user, unsigned int c goto e_event; } + init_thread = k_object_alloc(K_OBJ_THREAD); + if (!init_thread) { + LOG_ERR("user IPC init thread alloc failed"); + ret = -ENOMEM; + goto e_thread; + } + k_thread_create(thread, ipc_user_stack[core], CONFIG_SOF_IPC_USER_THREAD_STACK_SIZE, ipc_user_thread_fn, ipc_user, UINT_TO_POINTER(core), NULL, @@ -584,6 +596,7 @@ __cold static int ipc_user_init_thread(struct ipc_user *ipc_user, unsigned int c * and signal completion on the primary core semaphore */ k_thread_access_grant(thread, ipc_user_ksem, event); + k_thread_access_grant(thread, init_thread, ipc_init_stack[core]); user_grant_dai_access_all(thread); user_grant_dma_access_all(thread); k_mem_domain_add_thread(zephyr_ll_mem_domain(), thread); @@ -597,9 +610,11 @@ __cold static int ipc_user_init_thread(struct ipc_user *ipc_user, unsigned int c /* for ipc_user_thread_fn() and ipc_thread_user() in user context */ ipc_user->event[core] = event; ipc_user->thread[core] = thread; - + ipc_user->init_thread[core] = init_thread; + ipc_user->init_stack[core] = ipc_init_stack[core]; return 0; - +e_thread: + k_object_free(thread); e_event: k_object_free(event); diff --git a/zephyr/include/sof/lib/cpu.h b/zephyr/include/sof/lib/cpu.h index a47b93898973..5537750ae73c 100644 --- a/zephyr/include/sof/lib/cpu.h +++ b/zephyr/include/sof/lib/cpu.h @@ -50,6 +50,21 @@ void cpu_notify_state_exit(enum pm_state state); */ #if defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) __syscall int cpu_get_id(void); + +struct k_thread; + +/** + * @brief Pin a (not yet started) thread to a single DSP core. + * + * k_thread_cpu_pin() is a plain kernel function that takes the scheduler + * lock and therefore cannot be called from user mode. This system call + * lets a user-mode thread pin a helper thread it has just created. + * + * @param thread Thread object the caller has access to + * @param cpu Target core id + * @return 0 on success, negative error code otherwise + */ +__syscall int sof_thread_cpu_pin(struct k_thread *thread, int cpu); #endif /* let the compiler optimise when in single core mode */ diff --git a/zephyr/syscall/cpu.c b/zephyr/syscall/cpu.c index ee1990db8fd4..6a389c761735 100644 --- a/zephyr/syscall/cpu.c +++ b/zephyr/syscall/cpu.c @@ -20,3 +20,27 @@ static inline int z_vrfy_cpu_get_id(void) return z_impl_cpu_get_id(); } #include + +/** + * @brief Supervisor implementation of sof_thread_cpu_pin(). + */ +int z_impl_sof_thread_cpu_pin(struct k_thread *thread, int cpu) +{ + return k_thread_cpu_pin(thread, cpu); +} + +/** + * @brief Userspace verification wrapper for sof_thread_cpu_pin(). + * + * The caller must hold access to an initialized thread object and the + * core id must be valid. + */ +static inline int z_vrfy_sof_thread_cpu_pin(struct k_thread *thread, int cpu) +{ + K_OOPS(K_SYSCALL_OBJ(thread, K_OBJ_THREAD)); + K_OOPS(K_SYSCALL_VERIFY_MSG(cpu >= 0 && cpu < CONFIG_MP_MAX_NUM_CPUS, + "invalid cpu %d", cpu)); + + return z_impl_sof_thread_cpu_pin(thread, cpu); +} +#include diff --git a/zephyr/wrapper.c b/zephyr/wrapper.c index 7dcac306cb60..8809188e2fa9 100644 --- a/zephyr/wrapper.c +++ b/zephyr/wrapper.c @@ -30,6 +30,10 @@ /* Zephyr includes */ #include +#if defined(CONFIG_XTENSA) && defined(CONFIG_USERSPACE) +/* Zephyr arch-internal header, from zephyr/arch/xtensa/include */ +#include +#endif #include #include #include @@ -364,6 +368,29 @@ void k_sys_fatal_error_handler(unsigned int reason, } #endif +#if defined(CONFIG_XTENSA) && defined(CONFIG_USERSPACE) + /* + * A kernel oops raised from a syscall verifier (K_OOPS) or + * k_oops() arrives with a syscall stack frame, not an exception + * frame, so xtensa_exception_is_user() cannot validate it and + * would wrongly halt the system. If the faulting thread is a + * user thread, the oops is the user thread's fault: just abort + * that thread (z_fatal_error() does so once we return). + */ + if (reason == K_ERR_KERNEL_OOPS && _current && + k_thread_is_user_thread(_current)) { + LOG_ERR("ERROR: kernel oops from user thread %p; aborting thread.", + _current); + return; + } + + if (reason != K_ERR_KERNEL_PANIC && xtensa_exception_is_user(esf)) { + LOG_ERR("ERROR: Unexpected fatal error %u in Xtensa user mode; " + "aborting thread.\n", reason); + return; + } +#endif + /* flush and switch to immediate mode */ LOG_PANIC(); @@ -372,6 +399,8 @@ void k_sys_fatal_error_handler(unsigned int reason, ipc_send_panic_notification(); #endif + + #if defined(CONFIG_ARCH_POSIX) || defined(CONFIG_ZEPHYR_POSIX) LOG_ERR("Halting emulation");