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

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


  • To: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Wed, 17 Jul 2024 14:37:40 +0200
  • Autocrypt: addr=jbeulich@xxxxxxxx; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL
  • Cc: Roger Pau Monné <roger.pau@xxxxxxxxxx>, Xen-devel <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • Delivery-date: Wed, 17 Jul 2024 12:38:14 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

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).

> 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?

> --- 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).

>  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.

> --- /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.

Also may I ask that you add @progbits?

> +        .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?

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).

> +#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.

Jan

> +DECLARE_HYPERCALL(argo_op)
> +DECLARE_HYPERCALL(xenpmu_op)
> +
> +DECLARE_HYPERCALL(arch_0)
> +DECLARE_HYPERCALL(arch_1)
> +DECLARE_HYPERCALL(arch_2)
> +DECLARE_HYPERCALL(arch_3)
> +DECLARE_HYPERCALL(arch_4)
> +DECLARE_HYPERCALL(arch_5)
> +DECLARE_HYPERCALL(arch_6)
> +DECLARE_HYPERCALL(arch_7)




 


Rackspace

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