[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH for-4.20] hvmloader: Rework hypercall infrastructure


  • To: Jan Beulich <jbeulich@xxxxxxxx>
  • From: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
  • Date: Wed, 24 Jul 2024 18:24:04 +0100
  • Autocrypt: addr=andrew.cooper3@xxxxxxxxxx; keydata= xsFNBFLhNn8BEADVhE+Hb8i0GV6mihnnr/uiQQdPF8kUoFzCOPXkf7jQ5sLYeJa0cQi6Penp VtiFYznTairnVsN5J+ujSTIb+OlMSJUWV4opS7WVNnxHbFTPYZVQ3erv7NKc2iVizCRZ2Kxn srM1oPXWRic8BIAdYOKOloF2300SL/bIpeD+x7h3w9B/qez7nOin5NzkxgFoaUeIal12pXSR Q354FKFoy6Vh96gc4VRqte3jw8mPuJQpfws+Pb+swvSf/i1q1+1I4jsRQQh2m6OTADHIqg2E ofTYAEh7R5HfPx0EXoEDMdRjOeKn8+vvkAwhviWXTHlG3R1QkbE5M/oywnZ83udJmi+lxjJ5 YhQ5IzomvJ16H0Bq+TLyVLO/VRksp1VR9HxCzItLNCS8PdpYYz5TC204ViycobYU65WMpzWe LFAGn8jSS25XIpqv0Y9k87dLbctKKA14Ifw2kq5OIVu2FuX+3i446JOa2vpCI9GcjCzi3oHV e00bzYiHMIl0FICrNJU0Kjho8pdo0m2uxkn6SYEpogAy9pnatUlO+erL4LqFUO7GXSdBRbw5 gNt25XTLdSFuZtMxkY3tq8MFss5QnjhehCVPEpE6y9ZjI4XB8ad1G4oBHVGK5LMsvg22PfMJ ISWFSHoF/B5+lHkCKWkFxZ0gZn33ju5n6/FOdEx4B8cMJt+cWwARAQABzSlBbmRyZXcgQ29v cGVyIDxhbmRyZXcuY29vcGVyM0BjaXRyaXguY29tPsLBegQTAQgAJAIbAwULCQgHAwUVCgkI CwUWAgMBAAIeAQIXgAUCWKD95wIZAQAKCRBlw/kGpdefoHbdD/9AIoR3k6fKl+RFiFpyAhvO 59ttDF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xxxxx>
  • Delivery-date: Wed, 24 Jul 2024 17:24:17 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 17/07/2024 1:37 pm, Jan Beulich wrote:
> On 17.07.2024 13:12, Andrew Cooper wrote:
>> Right now, the hypercall page is at a hardcoded physical address, and making
>> hypercalls involves indirect calls to compile-time constant addresses.
>> e.g. HYPERCALL_memory_op is:
>>
>>   mov    $0x80180,%eax
>>   call   *%eax
>>
>> Instead, import the hypercall infrastructure from XTF to have 
>> hypercall_page[]
>> fully within the hvmloader image, and prepared at compile time rather than
>> runtime.  This uses direct calls, so HYPERCALL_memory_op now disassembles as:
>>
>>   call   132180 <HYPERCALL_memory_op>
>>
>> which is faster and clearer.
> Just to mention it - even with a fixed address using indirect calls shouldn't
> have been necessary (minus assembler bugs, that is).

Indeed.

The very proto-form of XTF to investigate XSA-106 was done in HVMLoader
because I needed something HVM based (rather than the MiniOS based PV
work I'd been doing up to that point).

Starting XTF from scratch was a very deliberate decision so as not to
starting with technical debt such as this...

>
>> Remove the loop over multiple hypercall pages.  It was long ago realised to 
>> be
>> an unworkable design, and eax fixed in the ABI to 1.
>>
>> Pass -z noexecstack to LD to stop newer bintuils complaining about the 
>> absence
>> of .note.GNU-stack.  hvmloader is not a regular binary, and in fact its stack
>> is always executable owing to operating in unpaged mode.
>>
>> Signed-off-by: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
>> ---
>> CC: Jan Beulich <JBeulich@xxxxxxxx>
>> CC: Roger Pau Monné <roger.pau@xxxxxxxxxx>
>>
>> There doesn't appear to be any sensible AFLAGS infrastructure to set
>> -D__ASSEMBLY__.  Opecoding it once seemed like the least bad option.
> I agree.
>
>> ---
>>  tools/firmware/hvmloader/Makefile         |   3 +
>>  tools/firmware/hvmloader/config.h         |   1 -
>>  tools/firmware/hvmloader/hvmloader.c      |   7 +-
>>  tools/firmware/hvmloader/hvmloader.lds    |   4 +-
>>  tools/firmware/hvmloader/hypercall.h      | 121 ++++++----------------
>>  tools/firmware/hvmloader/hypercall_page.S |  67 ++++++++++++
>>  6 files changed, 105 insertions(+), 98 deletions(-)
>>  create mode 100644 tools/firmware/hvmloader/hypercall_page.S
> May I ask that the new file use a hyphen in place of the underscore?
>
>> @@ -142,8 +141,7 @@ static void init_hypercalls(void)
>>  
>>      /* Fill in hypercall transfer pages. */
>>      cpuid(base + 2, &eax, &ebx, &ecx, &edx);
>> -    for ( i = 0; i < eax; i++ )
>> -        wrmsr(ebx, HYPERCALL_PHYSICAL_ADDRESS + (i << 12) + i);
>> +    wrmsr(ebx, (unsigned long)hypercall_page);
> Convert the comment to singular then, too?
>
>> --- a/tools/firmware/hvmloader/hvmloader.lds
>> +++ b/tools/firmware/hvmloader/hvmloader.lds
>> @@ -7,9 +7,9 @@ SECTIONS
>>     * NB: there's no need to use the AT keyword in order to set the LMA, by
>>     * default the linker will use VMA = LMA unless specified otherwise.
>>     */
>> -  .text : { *(.text) *(.text.*) }
>> +  .text : { *(.text) *(.text.*)}
> Likely merely a leftover from some experimentation?

Ah yes.  I originally had .text.hcall which doesn't work properly after
.text.*

But I also didn't want it disassembled by default, hence ending up in data.

>
>> --- a/tools/firmware/hvmloader/hypercall.h
>> +++ b/tools/firmware/hvmloader/hypercall.h
>> @@ -35,148 +35,91 @@
>>  #include <xen/xen.h>
>>  #include "config.h"
>>  
>> -#define hcall_addr(name)                                                \
>> -    ((unsigned long)HYPERCALL_PHYSICAL_ADDRESS + __HYPERVISOR_##name * 32)
>> -
>> -#define _hypercall0(type, name)                 \
>> -({                                              \
>> -    long __res;                                 \
>> -    asm volatile (                              \
>> -        "call *%%eax"                           \
>> -        : "=a" (__res)                          \
>> -        : "0" (hcall_addr(name))                \
>> -        : "memory" );                           \
>> -    (type)__res;                                \
>> -})
>> -
>> -#define _hypercall1(type, name, a1)             \
>> -({                                              \
>> -    long __res, __ign1;                         \
>> -    asm volatile (                              \
>> -        "call *%%eax"                           \
>> -        : "=a" (__res), "=b" (__ign1)           \
>> -        : "0" (hcall_addr(name)),               \
>> -          "1" ((long)(a1))                      \
>> -        : "memory" );                           \
>> -    (type)__res;                                \
>> -})
>> -
>> -#define _hypercall2(type, name, a1, a2)                 \
>> -({                                                      \
>> -    long __res, __ign1, __ign2;                         \
>> -    asm volatile (                                      \
>> -        "call *%%eax"                                   \
>> -        : "=a" (__res), "=b" (__ign1), "=c" (__ign2)    \
>> -        : "0" (hcall_addr(name)),                       \
>> -          "1" ((long)(a1)), "2" ((long)(a2))            \
>> -        : "memory" );                                   \
>> -    (type)__res;                                        \
>> -})
>> -
>> -#define _hypercall3(type, name, a1, a2, a3)             \
>> -({                                                      \
>> -    long __res, __ign1, __ign2, __ign3;                 \
>> -    asm volatile (                                      \
>> -        "call *%%eax"                                   \
>> -        : "=a" (__res), "=b" (__ign1), "=c" (__ign2),   \
>> -          "=d" (__ign3)                                 \
>> -        : "0" (hcall_addr(name)),                       \
>> -          "1" ((long)(a1)), "2" ((long)(a2)),           \
>> -          "3" ((long)(a3))                              \
>> -        : "memory" );                                   \
>> -    (type)__res;                                        \
>> -})
>> -
>> -#define _hypercall4(type, name, a1, a2, a3, a4)         \
>> -({                                                      \
>> -    long __res, __ign1, __ign2, __ign3, __ign4;         \
>> -    asm volatile (                                      \
>> -        "call *%%eax"                                   \
>> -        : "=a" (__res), "=b" (__ign1), "=c" (__ign2),   \
>> -          "=d" (__ign3), "=S" (__ign4)                  \
>> -        : "0" (hcall_addr(name)),                       \
>> -          "1" ((long)(a1)), "2" ((long)(a2)),           \
>> -          "3" ((long)(a3)), "4" ((long)(a4))            \
>> -        : "memory" );                                   \
>> -    (type)__res;                                        \
>> -})
>> -
>> -#define _hypercall5(type, name, a1, a2, a3, a4, a5)     \
>> -({                                                      \
>> -    long __res, __ign1, __ign2, __ign3, __ign4, __ign5; \
>> -    asm volatile (                                      \
>> -        "call *%%eax"                                   \
>> -        : "=a" (__res), "=b" (__ign1), "=c" (__ign2),   \
>> -          "=d" (__ign3), "=S" (__ign4), "=D" (__ign5)   \
>> -        : "0" (hcall_addr(name)),                       \
>> -          "1" ((long)(a1)), "2" ((long)(a2)),           \
>> -          "3" ((long)(a3)), "4" ((long)(a4)),           \
>> -          "5" ((long)(a5))                              \
>> -        : "memory" );                                   \
>> -    (type)__res;                                        \
>> -})
>> +extern const char hypercall_page[];
>> +
>> +#define _hypercall2(type, hcall, a1, a2)                                \
>> +    ({                                                                  \
>> +        long res, _a1 = (long)(a1), _a2 = (long)(a2);                   \
>> +        asm volatile (                                                  \
>> +            "call hypercall_page + %c[offset]"                          \
>> +            : "=a" (res), "+b" (_a1), "+c" (_a2)                        \
>> +            : [offset] "i" (hcall * 32)                                 \
>> +            : "memory" );                                               \
>> +        (type)res;                                                      \
>> +    })
>> +
>> +#define _hypercall3(type, hcall, a1, a2, a3)                            \
>> +    ({                                                                  \
>> +        long res, _a1 = (long)(a1), _a2 = (long)(a2), _a3 = (long)(a3); \
>> +        asm volatile (                                                  \
>> +            "call hypercall_page + %c[offset]"                          \
>> +            : "=a" (res), "+b" (_a1), "+c" (_a2), "+d" (_a3)            \
>> +            : [offset] "i" (hcall * 32)                                 \
>> +            : "memory" );                                               \
>> +        (type)res;                                                      \
>> +    })
> Not having _hypercall0() and _hypercall1() anymore is certainly a little
> odd. If one needed to use such a hypercall, even if only for debugging,
> the extra work needed (every time) would be larger than necessary. I'm
> definitely less worried about _hypercall4() and _hypercall5().
>
> In any event the removal of any wrappers could do with mentioning in the
> description, to indicate it's deliberate (and why).

I suppose leaving 0 and 1 behind is fine.  That's easy enough.

>
>>  static inline int
>>  hypercall_sched_op(
>>      int cmd, void *arg)
>>  {
>> -    return _hypercall2(int, sched_op, cmd, arg);
>> +    return _hypercall2(int, __HYPERVISOR_sched_op, cmd, arg);
>>  }
> I know you don't really like token concatenation in cases like these ones,
> but these adjustments all don't look as if they were necessary right here.
> The new macros use the macro parameter only in ways where token pasting
> would continue to work, afaics. And in the new assembly file you use very
> similar token pasting anyway.

That's because my judgement is not about simply tokenisation (or not). 
It's about not using ## when it is likely to interfere with
grep/cscope/tags/etc.

The assembly file both isn't really interesting to find this way, and
needs ## in order to work the way it does (differing prefixes in the
hypercall names).

>
>> --- /dev/null
>> +++ b/tools/firmware/hvmloader/hypercall_page.S
>> @@ -0,0 +1,67 @@
>> +#include <xen/xen.h>
>> +
>> +        .section ".hcall", "aw"
> Why "aw"? I'd have expected "awx" if you really think the writable part
> needs expressing here, or else "ax". Otherwise I think a brief comment as
> wanted as to the absence of x for something that is executable.

It's because it's merged with .data (so it doesn't pollute the
disassembly).  "awx" causes a section flags mismatch.

> Also may I ask that you add @progbits?

Ok.

>
>> +        .align 4096
> PAGE_SIZE? And then again ...
>
>> +        .globl hypercall_page
>> +hypercall_page:
>> +         /* Poisoned with `ret` for safety before hypercalls are set up. */
>> +        .fill 4096, 1, 0xc3
>> +        .type hypercall_page, STT_OBJECT
>> +        .size hypercall_page, 4096
> ... here?

HVMLoader doesn't have a suitable constant, and doesn't have _AC().

Although we probably can just get away with (1 << PAGE_SHIFT) and drop
the ul.

>
> As to the "poisoning" - how does RET provide any safety? If a call happened
> early, the uncertainty of what %eax is set to would make it unpredictable
> how such a caller would further behave. Imo better to crash / hang in such
> a case, perhaps by using INT3 instead.
>
> I notice that matches earlier behavior, but there the comment at least
> validly said "rendering them no-ops" (yet still without considering
> possible problems resulting from doing so).

That's a complicated one.  I can't remember why I chose that exact
phraseology, but it's not really about accidentally-too-early case, it's
about error handling.

HVMLoader doesn't have an IDT, so any exception is fatal.  But that's
also something that ideally wants fixing (if it weren't for the fact
that it's more likely that I'll complete plans to remove hvmloader
completely before having time to do an IDT).

But for code which does have a panic() function, there's console_io
(logging) and sched_op (SHUTDOWN_crash) which you want to use just in
case they do work, before moving onto other methods of terminating.


>
>> +#define DECLARE_HYPERCALL(name)                                             
>>     \
>> +        .globl HYPERCALL_ ## name;                                          
>>     \
>> +        .type  HYPERCALL_ ## name, STT_FUNC;                                
>>     \
>> +        .size  HYPERCALL_ ## name, 32;                                      
>>     \
>> +        .set   HYPERCALL_ ## name, hypercall_page + __HYPERVISOR_ ## name * 
>> 32
>> +
>> +DECLARE_HYPERCALL(set_trap_table)
>> +DECLARE_HYPERCALL(mmu_update)
>> +DECLARE_HYPERCALL(set_gdt)
>> +DECLARE_HYPERCALL(stack_switch)
>> +DECLARE_HYPERCALL(set_callbacks)
>> +DECLARE_HYPERCALL(fpu_taskswitch)
>> +DECLARE_HYPERCALL(sched_op_compat)
>> +DECLARE_HYPERCALL(platform_op)
>> +DECLARE_HYPERCALL(set_debugreg)
>> +DECLARE_HYPERCALL(get_debugreg)
>> +DECLARE_HYPERCALL(update_descriptor)
>> +DECLARE_HYPERCALL(memory_op)
>> +DECLARE_HYPERCALL(multicall)
>> +DECLARE_HYPERCALL(update_va_mapping)
>> +DECLARE_HYPERCALL(set_timer_op)
>> +DECLARE_HYPERCALL(event_channel_op_compat)
>> +DECLARE_HYPERCALL(xen_version)
>> +DECLARE_HYPERCALL(console_io)
>> +DECLARE_HYPERCALL(physdev_op_compat)
>> +DECLARE_HYPERCALL(grant_table_op)
>> +DECLARE_HYPERCALL(vm_assist)
>> +DECLARE_HYPERCALL(update_va_mapping_otherdomain)
>> +DECLARE_HYPERCALL(iret)
>> +DECLARE_HYPERCALL(vcpu_op)
>> +DECLARE_HYPERCALL(set_segment_base)
>> +DECLARE_HYPERCALL(mmuext_op)
>> +DECLARE_HYPERCALL(xsm_op)
>> +DECLARE_HYPERCALL(nmi_op)
>> +DECLARE_HYPERCALL(sched_op)
>> +DECLARE_HYPERCALL(callback_op)
>> +DECLARE_HYPERCALL(xenoprof_op)
>> +DECLARE_HYPERCALL(event_channel_op)
>> +DECLARE_HYPERCALL(physdev_op)
>> +DECLARE_HYPERCALL(hvm_op)
>> +DECLARE_HYPERCALL(sysctl)
>> +DECLARE_HYPERCALL(domctl)
>> +DECLARE_HYPERCALL(kexec_op)
>> +DECLARE_HYPERCALL(tmem_op)
> We're not going to ever need this in hvmloader, are we? There are quite a
> few more that I'd suggest to leave out, but this one stands out for no
> longer existing even in the hypervisor.

I suspect I can trim this to just what HVMLoader needs.  It's not as if
we're expecting a major change in functionality.

~Andrew



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.