Re: [PATCH] x86/acrn: Improve ACRN hypercalls

From: Uros Bizjak
Date: Thu Aug 04 2022 - 14:56:34 EST


On Thu, Aug 4, 2022 at 8:41 PM Dave Hansen <dave.hansen@xxxxxxxxx> wrote:
>
> On 8/4/22 11:03, Uros Bizjak wrote:
> > As explained in section 6.47.5.2, "Specifying Registers for Local Variables"
> > of the GCC info documentation, the correct way to specify register for
> > input operands when calling Extended 'asm' is to define a local register
> > variable and associate it with a specified register:
> >
> > register unsigned long r8 asm ("r8") = hcall_id;
>
> IIRC, that's what the ACRN folks proposed first. But, it's more fragile
> because you can't, for instance, put a printk() in that function between
> the variable definition and assebly.

Yes, this is also stated in the documentation. The solution is to:

--quote--
register int *p1 asm ("r0") = ...;
register int *p2 asm ("r1") = ...;
register int *result asm ("r0");
asm ("sysint" : "=r" (result) : "0" (p1), "r" (p2));

_Warning:_ In the above example, be aware that a register (for example
'r0') can be call-clobbered by subsequent code, including function calls
and library calls for arithmetic operators on other variables (for
example the initialization of 'p2'). In this case, use temporary
variables for expressions between the register assignments:

int t1 = ...;
register int *p1 asm ("r0") = ...;
register int *p2 asm ("r1") = t1;
register int *result asm ("r0");
asm ("sysint" : "=r" (result) : "0" (p1), "r" (p2));
--/quote--

The %r8 is not preserved across function calls, so your statement
above is correct. But as long as there is no function call *between*
the variable definition and the assembly, the approach with the local
register variable works without any problems. It is even something GCC
itself has used in its library for years.

Uros.