From: Chao Liu <[email protected]> Some machines model firmware services which use vendor SMC function IDs while QEMU provides PSCI through the same SMC conduit. There is currently no way for such a machine to take over a call before the PSCI path handles it.
Add a small registry for SMC function ID handlers. Machines can register a handler which inspects the call and returns whether it was handled, without changing the behavior of existing callers. Signed-off-by: Chao Liu <[email protected]> Signed-off-by: Yifan Wang <[email protected]> --- MAINTAINERS | 1 + include/hw/arm/arm-smc.h | 31 ++++++++++++++++ target/arm/helper.c | 80 ++++++++++++++++++++++++++++++++++++++++ target/arm/internals.h | 3 ++ 4 files changed, 115 insertions(+) create mode 100644 include/hw/arm/arm-smc.h diff --git a/MAINTAINERS b/MAINTAINERS index fa0c6d0c9b..db1a62a0cb 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -222,6 +222,7 @@ F: tests/tcg/arm/ F: tests/tcg/aarch64/ F: tests/qtest/arm-cpu-features.c F: hw/arm/ +F: include/hw/arm/arm-smc.h F: hw/cpu/a*mpcore.c F: include/hw/cpu/a*mpcore.h F: docs/system/target-arm.rst diff --git a/include/hw/arm/arm-smc.h b/include/hw/arm/arm-smc.h new file mode 100644 index 0000000000..e27d64218f --- /dev/null +++ b/include/hw/arm/arm-smc.h @@ -0,0 +1,31 @@ +/* + * ARM SMC call dispatch + * + * Board and SoC models can register handlers for vendor or firmware + * SMC calling convention function IDs. Under TCG, dispatch follows the + * architectural trap checks and precedes exception delivery and QEMU's + * PSCI emulation, so firmware services can share the SMC conduit with PSCI. + * + * Copyright (C) 2026 Process Mission + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef HW_ARM_ARM_SMC_H +#define HW_ARM_ARM_SMC_H + +typedef bool (*ARMSCMCHandler)(const uint64_t args[8], uint64_t *ret, + void *opaque); + +/* + * Register a handler for one SMCCC function ID. The handler returns + * true when it has handled the call and set *ret. args contains the + * caller's x0-x7 or zero-extended r0-r7; the ARM dispatcher writes *ret + * to x0/r0 and preserves the other registers. Returns 0 on success and + * a negative value if the function ID is already registered. + */ +int arm_register_smc_handler(uint64_t func_id, ARMSCMCHandler handler, + void *opaque); +void arm_unregister_smc_handler(uint64_t func_id, ARMSCMCHandler handler); + +#endif /* HW_ARM_ARM_SMC_H */ diff --git a/target/arm/helper.c b/target/arm/helper.c index f413b15eb7..61645fefa6 100644 --- a/target/arm/helper.c +++ b/target/arm/helper.c @@ -36,6 +36,7 @@ #include "cpregs.h" #include "target/arm/gtimer.h" #include "qemu/plugin.h" +#include "hw/arm/arm-smc.h" static void switch_mode(CPUARMState *env, int mode); #ifndef CONFIG_USER_ONLY @@ -9815,6 +9816,78 @@ static void arm_cpu_do_interrupt_aarch64(CPUState *cs) new_el, env->pc, pstate_read(env)); } +typedef struct ARMSCMCHandlerEntry { + uint64_t func_id; + ARMSCMCHandler handler; + void *opaque; + QTAILQ_ENTRY(ARMSCMCHandlerEntry) node; +} ARMSCMCHandlerEntry; + +static QTAILQ_HEAD(, ARMSCMCHandlerEntry) arm_smc_handlers = + QTAILQ_HEAD_INITIALIZER(arm_smc_handlers); + +int arm_register_smc_handler(uint64_t func_id, ARMSCMCHandler handler, + void *opaque) +{ + ARMSCMCHandlerEntry *entry; + + QTAILQ_FOREACH(entry, &arm_smc_handlers, node) { + if (entry->func_id == func_id) { + return -1; + } + } + + entry = g_new0(ARMSCMCHandlerEntry, 1); + entry->func_id = func_id; + entry->handler = handler; + entry->opaque = opaque; + QTAILQ_INSERT_TAIL(&arm_smc_handlers, entry, node); + + return 0; +} + +void arm_unregister_smc_handler(uint64_t func_id, ARMSCMCHandler handler) +{ + ARMSCMCHandlerEntry *entry, *next; + + QTAILQ_FOREACH_SAFE(entry, &arm_smc_handlers, node, next) { + if (entry->func_id == func_id && entry->handler == handler) { + QTAILQ_REMOVE(&arm_smc_handlers, entry, node); + g_free(entry); + return; + } + } +} + +bool arm_smc_handle_call(ARMCPU *cpu) +{ + CPUARMState *env = &cpu->env; + uint64_t func_id = is_a64(env) ? env->xregs[0] : env->regs[0]; + ARMSCMCHandlerEntry *entry; + + QTAILQ_FOREACH(entry, &arm_smc_handlers, node) { + uint64_t args[8], ret; + + if (entry->func_id != func_id) { + continue; + } + for (size_t i = 0; i < ARRAY_SIZE(args); i++) { + args[i] = is_a64(env) ? env->xregs[i] : env->regs[i]; + } + if (!entry->handler(args, &ret, entry->opaque)) { + return false; + } + if (is_a64(env)) { + env->xregs[0] = ret; + } else { + env->regs[0] = ret; + } + return true; + } + + return false; +} + /* * Do semihosting call and set the appropriate return value. All the * permission and validity checks have been done at translate time. @@ -9873,6 +9946,13 @@ void arm_cpu_do_interrupt(CPUState *cs) env->exception.syndrome); } + if (tcg_enabled() && cs->exception_index == EXCP_SMC && + arm_smc_handle_call(cpu)) { + qemu_log_mask(CPU_LOG_INT, "...handled as SMC handler call\n"); + qemu_plugin_vcpu_hostcall_cb(cs, last_pc); + return; + } + if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) { arm_handle_psci_call(cpu); qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n"); diff --git a/target/arm/internals.h b/target/arm/internals.h index 033d6d66da..4c6825b201 100644 --- a/target/arm/internals.h +++ b/target/arm/internals.h @@ -717,6 +717,9 @@ bool arm_is_psci_call(ARMCPU *cpu, int excp_type); /* Actually handle a PSCI call */ void arm_handle_psci_call(ARMCPU *cpu); +/* Dispatch a registered SMC handler call, if any. */ +bool arm_smc_handle_call(ARMCPU *cpu); + /** * arm_clear_exclusive: clear the exclusive monitor * @env: CPU env -- 2.53.0
