[RFC PATCH 0/1] xen/arm: smccc: preserve arguments before register setup

Jan Setje-Eilers posted 1 patch 5 days, 4 hours ago
xen/arch/arm/include/asm/smccc.h | 26 +++++++++++++++++++-------
1 file changed, 19 insertions(+), 7 deletions(-)
[RFC PATCH 0/1] xen/arm: smccc: preserve arguments before register setup
Posted by Jan Setje-Eilers 5 days, 4 hours ago
Hi Andrew,

While investigating an SMC forwarding failure after updating to Xen 4.22,
I found that firmware could receive different values from those passed to
arm_smccc_1_1_smc().

Several public Xen callers pass get_user_reg() calls directly to this
helper.  This includes the SCMI, ZynqMP EEMI, and i.MX forwarding paths.
The helper puts each value into its SMC argument register as the
expression is evaluated.  A later get_user_reg() call can then overwrite a
register prepared for an earlier argument.

The change in generated code appears after:

67bcf5eae709 ("xen/arm: Simplify type handling for SMCCC declarations")

I am not sure which behavior the helper is intended to provide. Should
arm_smccc_1_1_smc() continue to accept arguments such as get_user_reg()
calls, or should callers save those values in local variables first?

The attached patch is a candidate for the first option. It restores
temporary variables inside the helper, allowing all calls to finish before
x0-x7 are prepared for the SMC instruction. I am not able to validate the
public SCMI and ZynqMP paths on hardware, but their generated code now
finishes every get_user_reg() call before the final register setup and
smc. With this change, SMCs also work as intended on the device I'm
using.

If the intent is not to accept function calls as arguments, we should
probably fix the SCMI, ZynqMP EEMI, and i.MX callers.

Given the existing use cases, this seems like a bug, but your patch looks
intentional. It's very possible I'm missing the intended design. I would
appreciate your view on the intended contract.

Thanks for any advice or help with this.

Jan Setje-Eilers (1):
  xen/arm: smccc: preserve arguments before register setup

 xen/arch/arm/include/asm/smccc.h | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

-- 
2.47.3
Re: [RFC PATCH 0/1] xen/arm: smccc: preserve arguments before register setup
Posted by Andrew Cooper 4 days, 13 hours ago
On 29/08/2026 3:12 am, Jan Setje-Eilers wrote:
> Hi Andrew,
>
> While investigating an SMC forwarding failure after updating to Xen 4.22,
> I found that firmware could receive different values from those passed to
> arm_smccc_1_1_smc().
>
> Several public Xen callers pass get_user_reg() calls directly to this
> helper.  This includes the SCMI, ZynqMP EEMI, and i.MX forwarding paths.
> The helper puts each value into its SMC argument register as the
> expression is evaluated.  A later get_user_reg() call can then overwrite a
> register prepared for an earlier argument.
>
> The change in generated code appears after:
>
> 67bcf5eae709 ("xen/arm: Simplify type handling for SMCCC declarations")
>
> I am not sure which behavior the helper is intended to provide. Should
> arm_smccc_1_1_smc() continue to accept arguments such as get_user_reg()
> calls, or should callers save those values in local variables first?
>
> The attached patch is a candidate for the first option. It restores
> temporary variables inside the helper, allowing all calls to finish before
> x0-x7 are prepared for the SMC instruction. I am not able to validate the
> public SCMI and ZynqMP paths on hardware, but their generated code now
> finishes every get_user_reg() call before the final register setup and
> smc. With this change, SMCs also work as intended on the device I'm
> using.
>
> If the intent is not to accept function calls as arguments, we should
> probably fix the SCMI, ZynqMP EEMI, and i.MX callers.
>
> Given the existing use cases, this seems like a bug, but your patch looks
> intentional. It's very possible I'm missing the intended design. I would
> appreciate your view on the intended contract.
>
> Thanks for any advice or help with this.

Yes you're right.  My change was buggy.   It was actually part of a
mutli-stage cleanup across several patches.

I'll submit my own patch.  I'm afraid your AI has put in whitespace
errors where a straight revert would have gotten it correct, and left
the 0 case still buggy.

~Andrew

Re: [RFC PATCH 0/1] xen/arm: smccc: preserve arguments before register setup
Posted by Andrew Cooper 4 days, 11 hours ago
On 29/08/2026 5:53 pm, Andrew Cooper wrote:
> On 29/08/2026 3:12 am, Jan Setje-Eilers wrote:
>> Hi Andrew,
>>
>> While investigating an SMC forwarding failure after updating to Xen 4.22,
>> I found that firmware could receive different values from those passed to
>> arm_smccc_1_1_smc().
>>
>> Several public Xen callers pass get_user_reg() calls directly to this
>> helper.  This includes the SCMI, ZynqMP EEMI, and i.MX forwarding paths.
>> The helper puts each value into its SMC argument register as the
>> expression is evaluated.  A later get_user_reg() call can then overwrite a
>> register prepared for an earlier argument.
>>
>> The change in generated code appears after:
>>
>> 67bcf5eae709 ("xen/arm: Simplify type handling for SMCCC declarations")
>>
>> I am not sure which behavior the helper is intended to provide. Should
>> arm_smccc_1_1_smc() continue to accept arguments such as get_user_reg()
>> calls, or should callers save those values in local variables first?
>>
>> The attached patch is a candidate for the first option. It restores
>> temporary variables inside the helper, allowing all calls to finish before
>> x0-x7 are prepared for the SMC instruction. I am not able to validate the
>> public SCMI and ZynqMP paths on hardware, but their generated code now
>> finishes every get_user_reg() call before the final register setup and
>> smc. With this change, SMCs also work as intended on the device I'm
>> using.
>>
>> If the intent is not to accept function calls as arguments, we should
>> probably fix the SCMI, ZynqMP EEMI, and i.MX callers.
>>
>> Given the existing use cases, this seems like a bug, but your patch looks
>> intentional. It's very possible I'm missing the intended design. I would
>> appreciate your view on the intended contract.
>>
>> Thanks for any advice or help with this.
> Yes you're right.  My change was buggy.   It was actually part of a
> mutli-stage cleanup across several patches.
>
> I'll submit my own patch.  I'm afraid your AI has put in whitespace
> errors where a straight revert would have gotten it correct, and left
> the 0 case still buggy.

In fact there's another bug.  The SMCCC ABI states that certain
registers are clobbered, yet these are missing from asm().

It's also sad that this macro maze goes to the effort of making a
function-like-call which can return 4 registers by value, just to then
force it all to be spilled to the stack anyway.

~Andrew

Re: [RFC PATCH 0/1] xen/arm: smccc: preserve arguments before register setup
Posted by Jan Setje-Eilers 4 days, 8 hours ago
On 8/29/26 11:19, Andrew Cooper wrote:
> On 29/08/2026 5: 53 pm, Andrew Cooper wrote: > On 29/08/2026 3: 12 am,
> Jan Setje-Eilers wrote: >> Hi Andrew, >> >> While investigating an SMC
> forwarding failure after updating to Xen 4. 22, >> I found that firmware
> could
> 
> 
> On 29/08/2026 5:53 pm, Andrew Cooper wrote:
>> On 29/08/2026 3:12 am, Jan Setje-Eilers wrote:
>>> Hi Andrew,
>>>
>>> While investigating an SMC forwarding failure after updating to Xen 4.22,
>>> I found that firmware could receive different values from those passed to
>>> arm_smccc_1_1_smc().
>>>
>>> Several public Xen callers pass get_user_reg() calls directly to this
>>> helper.  This includes the SCMI, ZynqMP EEMI, and i.MX forwarding paths.
>>> The helper puts each value into its SMC argument register as the
>>> expression is evaluated.  A later get_user_reg() call can then overwrite a
>>> register prepared for an earlier argument.
>>>
>>> The change in generated code appears after:
>>>
>>> 67bcf5eae709 ("xen/arm: Simplify type handling for SMCCC declarations")
>>>
>>> I am not sure which behavior the helper is intended to provide. Should
>>> arm_smccc_1_1_smc() continue to accept arguments such as get_user_reg()
>>> calls, or should callers save those values in local variables first?
>>>
>>> The attached patch is a candidate for the first option. It restores
>>> temporary variables inside the helper, allowing all calls to finish before
>>> x0-x7 are prepared for the SMC instruction. I am not able to validate the
>>> public SCMI and ZynqMP paths on hardware, but their generated code now
>>> finishes every get_user_reg() call before the final register setup and
>>> smc. With this change, SMCs also work as intended on the device I'm
>>> using.
>>>
>>> If the intent is not to accept function calls as arguments, we should
>>> probably fix the SCMI, ZynqMP EEMI, and i.MX callers.
>>>
>>> Given the existing use cases, this seems like a bug, but your patch looks
>>> intentional. It's very possible I'm missing the intended design. I would
>>> appreciate your view on the intended contract.
>>>
>>> Thanks for any advice or help with this.
>> Yes you're right.  My change was buggy.   It was actually part of a
>> mutli-stage cleanup across several patches.
>>
>> I'll submit my own patch.  I'm afraid your AI has put in whitespace
>> errors where a straight revert would have gotten it correct, and left
>> the 0 case still buggy.

 Thank you!! Yes, a revert also makes sense.

 Heh, the bulk of my AI usage was actually to try to make sure I hit all
the project rules and formatting since I'm much less used to some of the
style here. Sigh. :)

> In fact there's another bug.  The SMCCC ABI states that certain
> registers are clobbered, yet these are missing from asm().
> 
> It's also sad that this macro maze goes to the effort of making a
> function-like-call which can return 4 registers by value, just to then
> force it all to be spilled to the stack anyway.

 The other way to do this, if we're actually trying to minimize
instructions,
would actually be to create another interface that does something like
get_user_reg() as part of the invocation. Then that could be a single
assembly implementation.

-jan