Skip to contentSkip to conversation

x86/altcall: Switch to a simpler scheme

PATCH In review Andrew Cooper 3 participants 20 messages Apr 23, 2025

lore
Share & tools
lore
#0 lore raw

cover x86/altcall: Switch to a simpler scheme

See patch 7 for details. This simplifies _apply_alternatives() specifically by removing a special and quite complicated case, and encodes the metadata about altcalls differently and more efficiently.

https://gitlab.com/xen-project/hardware/xen-staging/-/pipelines/1780872355

Andrew Cooper (8):
x86/altcall: Split alternative-call.h out of alternative.h x86/altcall: Rename alternative_branches() to boot_apply_alt_calls() x86/alternatives: Rework information passing into
nmi_apply_alternatives()
x86/alternatives: Factor seal_endbr64() out of _apply_alternatives() x86/altcall: Introduce new simpler scheme
xen/livepatch: Support new altcall scheme
x86/altcall: Switch to simpler scheme
x86/alternatives: Simplify _apply_alternatives() now altcall is
separate

 .../eclair_analysis/ECLAIR/deviations.ecl     |   4 +-
 xen/arch/x86/alternative.c                    | 267 ++++++++++--------
 .../asm/{alternative.h => alternative-call.h} | 179 +-----------
 xen/arch/x86/include/asm/alternative.h        | 263 -----------------
 xen/arch/x86/include/asm/hvm/hvm.h            |   2 +-
 xen/arch/x86/setup.c                          |   3 +-
 xen/arch/x86/xen.lds.S                        |   4 +
 xen/common/core_parking.c                     |   4 +-
 xen/common/livepatch.c                        |  58 ++++
 xen/include/xen/alternative-call.h            |  24 +-
 10 files changed, 252 insertions(+), 556 deletions(-)
 copy xen/arch/x86/include/asm/{alternative.h => alternative-call.h} (63%)
Signature · 1 line
2.39.5
#1 lore raw

PATCH 1/8 x86/altcall: Split alternative-call.h out of alternative.h

... in preparation for changing how they're implemented.

Update the MISRA deviations with the new path.

No functional change.

---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: [email protected] <[email protected]>
CC: Nicola Vetrini <[email protected]>

In terms of naming, while tailcalls can technically be jumps, they're still usually reasoned about as being calls.

It appears that everywhere else which needs alternative_{v,}call() gets it transitively through hvm.h

---
 .../eclair_analysis/ECLAIR/deviations.ecl     |   4 +-
 .../asm/{alternative.h => alternative-call.h} | 171 +-----------
 xen/arch/x86/include/asm/alternative.h        | 262 ------------------
 xen/arch/x86/include/asm/hvm/hvm.h            |   2 +-
 xen/common/core_parking.c                     |   4 +-
 xen/include/xen/alternative-call.h            |  10 +-
 6 files changed, 16 insertions(+), 437 deletions(-)
 copy xen/arch/x86/include/asm/{alternative.h => alternative-call.h} (64%)
automation/eclair_analysis/ECLAIR/deviations.ecl +2 -2
index 2c8fb9271391..9c67358d4663 100644
@@ -414,8 +414,8 @@ of the short-circuit evaluation strategy of such logical operators."
 -doc_end
 
 -doc_begin="Macros alternative_v?call[0-9] use sizeof and typeof to check that the argument types match the corresponding parameter ones."
--config=MC3A2.R13.6,reports+={deliberate,"any_area(any_loc(any_exp(macro(^alternative_vcall[0-9]$))&&file(^xen/arch/x86/include/asm/alternative\\.h*$)))"}
--config=B.UNEVALEFF,reports+={deliberate,"any_area(any_loc(any_exp(macro(^alternative_v?call[0-9]$))&&file(^xen/arch/x86/include/asm/alterantive\\.h*$)))"}
+-config=MC3A2.R13.6,reports+={deliberate,"any_area(any_loc(any_exp(macro(^alternative_vcall[0-9]$))&&file(^xen/arch/x86/include/asm/alternative-call\\.h*$)))"}
+-config=B.UNEVALEFF,reports+={deliberate,"any_area(any_loc(any_exp(macro(^alternative_v?call[0-9]$))&&file(^xen/arch/x86/include/asm/alterantive-call\\.h*$)))"}
 -doc_end
 
 -doc_begin="Anything, no matter how complicated, inside the BUILD_BUG_ON macro is subject to a compile-time evaluation without relevant side effects."
xen/arch/x86/include/asm/alternative-call.h renamed from xen/arch/x86/include/asm/alternative.h +6 -165

184 lines not on this page · raw message

xen/arch/x86/include/asm/alternative.h +0 -262

268 lines not on this page · raw message

xen/arch/x86/include/asm/hvm/hvm.h +1 -1
index 963e8201130a..bf8bc2e100bd 100644
@@ -9,9 +9,9 @@
 #ifndef __ASM_X86_HVM_HVM_H__
 #define __ASM_X86_HVM_HVM_H__
 
+#include <xen/alternative-call.h>
 #include <xen/mm.h>
 
-#include <asm/alternative.h>
 #include <asm/asm_defns.h>
 #include <asm/current.h>
 #include <asm/x86_emulate.h>
xen/common/core_parking.c +2 -2
index a970ffeab8c3..7d6a18cdcf4c 100644
@@ -15,10 +15,10 @@
  *  General Public License for more details.
  */
 
-#include <xen/types.h>
+#include <xen/alternative-call.h>
 #include <xen/cpu.h>
-#include <xen/init.h>
 #include <xen/cpumask.h>
+#include <xen/init.h>
 #include <xen/param.h>
 
 #include <asm/smp.h>
xen/include/xen/alternative-call.h +5 -5
index 62672b732431..39339c3f0f76 100644
@@ -13,10 +13,10 @@
  *
  * For architectures to support:
  *
- * - Implement alternative_{,v}call() in asm/alternative.h.  Code generation
- *   requirements are to emit a function pointer call at build time, and stash
- *   enough metadata to simplify the call at boot once the implementation has
- *   been resolved.
+ * - Implement alternative_{,v}call() in asm/alternative-call.h.  Code
+ *   generation requirements are to emit a function pointer call at build
+ *   time, and stash enough metadata to simplify the call at boot once the
+ *   implementation has been resolved.
  * - Select ALTERNATIVE_CALL in Kconfig.
  *
  * To use:
@@ -48,7 +48,7 @@
 
 #ifdef CONFIG_ALTERNATIVE_CALL
 
-#include <asm/alternative.h>
+#include <asm/alternative-call.h>
 
 #ifdef CONFIG_LIVEPATCH
 /* Must keep for livepatches to resolve alternative calls. */
Signature · 1 line
2.39.5
#3 lore raw

PATCH 2/8 x86/altcall: Rename alternative_branches() to boot_apply_alt_calls()

In reply to Andrew Cooper

The alternatives APIs are not great; rename alternative_branches() to be more precise. Centralise the declaration in xen/alternative-call.h, in the expectation that x86 won't be the only user in the long term.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/alternative.c             |  2 +-
 xen/arch/x86/include/asm/alternative.h |  1 -
 xen/arch/x86/setup.c                   |  3 ++-
 xen/include/xen/alternative-call.h     | 10 +++++++++-
 4 files changed, 12 insertions(+), 4 deletions(-)
xen/arch/x86/alternative.c +1 -1
index 1ba35cb9ede9..d1a3b7ea7ca6 100644
@@ -493,7 +493,7 @@ void __init alternative_instructions(void)
     _alternative_instructions(false);
 }
 
-void __init alternative_branches(void)
+void __init boot_apply_alt_calls(void)
 {
     local_irq_disable();
     _alternative_instructions(true);
xen/arch/x86/include/asm/alternative.h +0 -1
index 2d2ace97f794..29c3d724b07f 100644
@@ -29,7 +29,6 @@ extern void add_nops(void *insns, unsigned int len);
 /* Similar to alternative_instructions except it can be run with IRQs enabled. */
 extern int apply_alternatives(struct alt_instr *start, struct alt_instr *end);
 extern void alternative_instructions(void);
-extern void alternative_branches(void);
 
 #define alt_orig_len       "(.LXEN%=_orig_e - .LXEN%=_orig_s)"
 #define alt_pad_len        "(.LXEN%=_orig_p - .LXEN%=_orig_e)"
xen/arch/x86/setup.c +2 -1
index cf1ea040dd90..25189541244d 100644
@@ -1,4 +1,5 @@
 #include <xen/acpi.h>
+#include <xen/alternative-call.h>
 #include <xen/bitops.h>
 #include <xen/console.h>
 #include <xen/cpu.h>
@@ -2082,7 +2083,7 @@ void asmlinkage __init noreturn __start_xen(void)
 
     do_presmp_initcalls();
 
-    alternative_branches();
+    boot_apply_alt_calls();
 
     /*
      * NB: when running as a PV shim VCPUOP_up/down is wired to the shim
xen/include/xen/alternative-call.h +9 -1
index 39339c3f0f76..3c855bfa44f5 100644
@@ -17,6 +17,8 @@
  *   generation requirements are to emit a function pointer call at build
  *   time, and stash enough metadata to simplify the call at boot once the
  *   implementation has been resolved.
+ * - Implement boot_apply_alt_calls() to convert the function pointer calls
+ *   into direct calls on boot.
  * - Select ALTERNATIVE_CALL in Kconfig.
  *
  * To use:
@@ -57,7 +59,13 @@
 # define __alt_call_maybe_initdata __initdata
 #endif
 
-#else
+/*
+ * Devirtualise the alternative_{,v}call()'s on boot.  Convert still-NULL
+ * function pointers into traps.
+ */
+void boot_apply_alt_calls(void);
+
+#else /* CONFIG_ALTERNATIVE_CALL */
 
 #define alternative_call(func, args...)  (func)(args)
 #define alternative_vcall(func, args...) (func)(args)
Signature · 1 line
2.39.5
Acked-by on v1 2/8
#4 lore raw

Re: [PATCH 2/8] x86/altcall: Rename alternative_branches() to boot_apply_alt_calls()

Quoted 7 lines · Andrew Cooper

The alternatives APIs are not great; rename alternative_branches() to be more precise. Centralise the declaration in xen/alternative-call.h, in the expectation that x86 won't be the only user in the long term.

No functional change.

#5 lore raw

PATCH 3/8 x86/alternatives: Rework information passing into nmi_apply_alternatives()

In reply to Andrew Cooper

nmi_apply_alternatives() is soon going to need to dispatch to multiple functions, and a force parameter is not a good way of passing information.

Introduce ALT_INSNS and ALT_CALLS to pass in at the top level to select the operation(s) desired. They represent what will happen when we've separated the altcalls out of the general alternative instructions infrastructure, although in the short term we still need to synthesise the force parameter for _apply_alternatives().

Move two externs to reduce their scope a little.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/alternative.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
xen/arch/x86/alternative.c +9 -7
index d1a3b7ea7ca6..9aa591b364a4 100644
@@ -19,8 +19,6 @@
 
 #define MAX_PATCH_LEN (255-1)
 
-extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
-
 #ifdef K8_NOP1
 static const unsigned char k8nops[] init_or_livepatch_const = {
     K8_NOP1,
@@ -387,9 +385,13 @@ int apply_alternatives(struct alt_instr *start, struct alt_instr *end)
 }
 #endif
 
+#define ALT_INSNS (1U << 0)
+#define ALT_CALLS (1U << 1)
 static unsigned int __initdata alt_todo;
 static unsigned int __initdata alt_done;
 
+extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
+
 /*
  * At boot time, we patch alternatives in NMI context.  This means that the
  * active NMI-shadow will defer any further NMIs, removing the slim race
@@ -419,7 +421,7 @@ static int __init cf_check nmi_apply_alternatives(
         flush_local(FLUSH_TLB_GLOBAL);
 
         rc = _apply_alternatives(__alt_instructions, __alt_instructions_end,
-                                 alt_done);
+                                 alt_todo == ALT_CALLS);
         if ( rc )
             panic("Unable to apply alternatives: %d\n", rc);
 
@@ -442,7 +444,7 @@ static int __init cf_check nmi_apply_alternatives(
  * This routine is called with local interrupt disabled and used during
  * bootup.
  */
-static void __init _alternative_instructions(bool force)
+static void __init _alternative_instructions(unsigned int what)
 {
     unsigned int i;
     nmi_callback_t *saved_nmi_callback;
@@ -460,7 +462,7 @@ static void __init _alternative_instructions(bool force)
     ASSERT(!local_irq_is_enabled());
 
     /* Set what operation to perform /before/ setting the callback. */
-    alt_todo = 1u << force;
+    alt_todo = what;
     barrier();
 
     /*
@@ -490,12 +492,12 @@ static void __init _alternative_instructions(bool force)
 void __init alternative_instructions(void)
 {
     arch_init_ideal_nops();
-    _alternative_instructions(false);
+    _alternative_instructions(ALT_INSNS);
 }
 
 void __init boot_apply_alt_calls(void)
 {
     local_irq_disable();
-    _alternative_instructions(true);
+    _alternative_instructions(ALT_CALLS);
     local_irq_enable();
 }
Signature · 1 line
2.39.5
Reviewed-by on v1 3/8
#6 lore raw

Re: [PATCH 3/8] x86/alternatives: Rework information passing into nmi_apply_alternatives()

Quoted 9 lines · Andrew Cooper

nmi_apply_alternatives() is soon going to need to dispatch to multiple functions, and a force parameter is not a good way of passing information.

Introduce ALT_INSNS and ALT_CALLS to pass in at the top level to select the operation(s) desired. They represent what will happen when we've separated the altcalls out of the general alternative instructions infrastructure, although in the short term we still need to synthesise the force parameter for _apply_alternatives().

Quoted 5 lines

Move two externs to reduce their scope a little.

No functional change.

#7 lore raw

PATCH 4/8 x86/alternatives: Factor seal_endbr64() out of _apply_alternatives()

In reply to Andrew Cooper

We are going to need to reposition the call in a change with several moving parts.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/alternative.c | 70 ++++++++++++++++++++++----------------
 1 file changed, 40 insertions(+), 30 deletions(-)
xen/arch/x86/alternative.c +40 -30
index 9aa591b364a4..4b9f8d860153 100644
@@ -162,6 +162,44 @@ text_poke(void *addr, const void *opcode, size_t len)
 extern void *const __initdata_cf_clobber_start[];
 extern void *const __initdata_cf_clobber_end[];
 
+/*
+ * In CET-IBT enabled builds, clobber endbr64 instructions after altcall has
+ * finished optimising all indirect branches to direct ones.
+ */
+static void __init seal_endbr64(void)
+{
+    void *const *val;
+    unsigned int clobbered = 0;
+
+    if ( !cpu_has_xen_ibt )
+        return;
+
+    /*
+     * This is some minor structure (ab)use.  We walk the entire contents
+     * of .init.{ro,}data.cf_clobber as if it were an array of pointers.
+     *
+     * If the pointer points into .text, and at an endbr64 instruction,
+     * nop out the endbr64.  This causes the pointer to no longer be a
+     * legal indirect branch target under CET-IBT.  This is a
+     * defence-in-depth measure, to reduce the options available to an
+     * adversary who has managed to hijack a function pointer.
+     */
+    for ( val = __initdata_cf_clobber_start;
+          val < __initdata_cf_clobber_end;
+          val++ )
+    {
+        void *ptr = *val;
+
+        if ( !is_kernel_text(ptr) || !is_endbr64(ptr) )
+            continue;
+
+        place_endbr64_poison(ptr);
+        clobbered++;
+    }
+
+    printk("altcall: Optimised away %u endbr64 instructions\n", clobbered);
+}
+
 /*
  * Replace instructions with better alternatives for this CPU type.
  * This runs before SMP is initialized to avoid SMP problems with
@@ -344,36 +382,8 @@ static int init_or_livepatch _apply_alternatives(struct alt_instr *start,
      * Clobber endbr64 instructions now that altcall has finished optimising
      * all indirect branches to direct ones.
      */
-    if ( force && cpu_has_xen_ibt && system_state < SYS_STATE_active )
-    {
-        void *const *val;
-        unsigned int clobbered = 0;
-
-        /*
-         * This is some minor structure (ab)use.  We walk the entire contents
-         * of .init.{ro,}data.cf_clobber as if it were an array of pointers.
-         *
-         * If the pointer points into .text, and at an endbr64 instruction,
-         * nop out the endbr64.  This causes the pointer to no longer be a
-         * legal indirect branch target under CET-IBT.  This is a
-         * defence-in-depth measure, to reduce the options available to an
-         * adversary who has managed to hijack a function pointer.
-         */
-        for ( val = __initdata_cf_clobber_start;
-              val < __initdata_cf_clobber_end;
-              val++ )
-        {
-            void *ptr = *val;
-
-            if ( !is_kernel_text(ptr) || !is_endbr64(ptr) )
-                continue;
-
-            place_endbr64_poison(ptr);
-            clobbered++;
-        }
-
-        printk("altcall: Optimised away %u endbr64 instructions\n", clobbered);
-    }
+    if ( force && system_state < SYS_STATE_active )
+        seal_endbr64();
 
     return 0;
 }
Signature · 1 line
2.39.5
#9 lore raw

PATCH 5/8 x86/altcall: Introduce new simpler scheme

In reply to Andrew Cooper

Encoding altcalls as regular alternatives leads to an unreasonable amount of complexity in _apply_alternatives().

Introduce apply_alt_calls(), and an .alt_call_sites section which simply tracks the source address (relative, to save on space). That's literally all that is needed in order to devirtualise the function pointers.

apply_alt_calls() is mostly as per _apply_alternatives(), except the size is known to be 6 bytes. Drop the logic for JMP *RIPREL, as there's no support for tailcall optimisations, nor a feasbile plan on how to introduce support. Pad with a redundant prefix to avoid needing a separate NOP on the end.

Wire it up in nmi_apply_alternatives(), although the section is empty at this juncture so nothing happens in practice.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

Finding a 6-byte UD instruction that is distinct from ud2 turns out to be quite challengning. The easy way involves a length changing prefix, which is best avoided. Suggestions for alternative patterns welcome.

---
 xen/arch/x86/alternative.c                  | 94 +++++++++++++++++++++
 xen/arch/x86/include/asm/alternative-call.h |  7 ++
 xen/arch/x86/xen.lds.S                      |  4 +
 3 files changed, 105 insertions(+)
xen/arch/x86/alternative.c +94 -0
index 4b9f8d860153..f6594e21a14c 100644
@@ -388,6 +388,92 @@ static int init_or_livepatch _apply_alternatives(struct alt_instr *start,
     return 0;
 }
 
+/*
+ * At build time, alternative calls are emitted as:
+ *   ff 15 xx xx xx xx  =>  call *disp32(%rip)
+ *
+ * During boot, we devirtualise by editing to:
+ *   2e e8 xx xx xx xx  =>  cs call disp32
+ *
+ * or, if the function pointer is still NULL, poison to:
+ *   0f 0b 0f 0b 0f 0b  =>  ud2a (x3)
+ */
+static int init_or_livepatch apply_alt_calls(
+    const struct alt_call *start, const struct alt_call *end)
+{
+    const struct alt_call *a;
+
+    for ( a = start; a < end; a++ )
+    {
+        const uint8_t *dest;
+        uint8_t buf[6], *orig = ALT_CALL_PTR(a);
+        long disp;
+
+        /* It's likely that this won't change, but check just to be safe. */
+        BUILD_BUG_ON(ALT_CALL_LEN(a) != 6);
+
+        if ( orig[0] != 0xff || orig[1] != 0x15 )
+        {
+            printk(XENLOG_ERR
+                   "Altcall for %ps [%6ph] not CALL *RIPREL\n",
+                   orig, orig);
+            return -EINVAL;
+        }
+
+        disp = *(int32_t *)(orig + 2);
+        dest = *(const void **)(orig + 6 + disp);
+
+        if ( dest )
+        {
+            /*
+             * When building for CET-IBT, all function pointer targets
+             * should have an endbr64 instruction.
+             *
+             * If this is not the case, leave a warning because
+             * something is probably wrong with the build.  A CET-IBT
+             * enabled system might have exploded already.
+             *
+             * Otherwise, skip the endbr64 instruction.  This is a
+             * marginal perf improvement which saves on instruction
+             * decode bandwidth.
+             */
+            if ( IS_ENABLED(CONFIG_XEN_IBT) )
+            {
+                if ( is_endbr64(dest) )
+                    dest += ENDBR64_LEN;
+                else
+                    printk(XENLOG_WARNING
+                           "Altcall %ps dest %ps has no endbr64\n",
+                           orig, dest);
+            }
+
+            disp = dest - (orig + 6);
+            ASSERT(disp == (int32_t)disp);
+
+            buf[0] = 0x2e;
+            buf[1] = 0xe8;
+            *(int32_t *)(buf + 2) = disp;
+        }
+        else
+        {
+            /*
+             * The function pointer is still NULL.  Seal the whole call, as
+             * it's not used.
+             */
+            buf[0] = 0x0f;
+            buf[1] = 0x0b;
+            buf[2] = 0x0f;
+            buf[3] = 0x0b;
+            buf[4] = 0x0f;
+            buf[5] = 0x0b;
+        }
+
+        text_poke(orig, buf, sizeof(buf));
+    }
+
+    return 0;
+}
+
 #ifdef CONFIG_LIVEPATCH
 int apply_alternatives(struct alt_instr *start, struct alt_instr *end)
 {
@@ -401,6 +487,7 @@ static unsigned int __initdata alt_todo;
 static unsigned int __initdata alt_done;
 
 extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
+extern struct alt_call __alt_call_sites_start[], __alt_call_sites_end[];
 
 /*
  * At boot time, we patch alternatives in NMI context.  This means that the
@@ -435,6 +522,13 @@ static int __init cf_check nmi_apply_alternatives(
         if ( rc )
             panic("Unable to apply alternatives: %d\n", rc);
 
+        if ( alt_todo & ALT_CALLS )
+        {
+            rc = apply_alt_calls(__alt_call_sites_start, __alt_call_sites_end);
+            if ( rc )
+                panic("Unable to apply alternative calls: %d\n", rc);
+        }
+
         /*
          * Reinstate perms on .text to be RX.  This also cleans out the dirty
          * bits, which matters when CET Shstk is active.
xen/arch/x86/include/asm/alternative-call.h +7 -0
index 828ea32a9625..49a04a7cc45b 100644
@@ -4,6 +4,13 @@
 
 #include <asm/alternative.h>
 
+/* Simply the relative position of the source call. */
+struct alt_call {
+    int32_t offset;
+};
+#define ALT_CALL_PTR(a) ((void *)&(a)->offset + (a)->offset)
+#define ALT_CALL_LEN(a) (6)
+
 /*
  * Machinery to allow converting indirect to direct calls, when the called
  * function is determined once at boot and later never changed.
xen/arch/x86/xen.lds.S +4 -0
index d4dd6434c466..53bafc98a536 100644
@@ -260,6 +260,10 @@ SECTIONS
         __alt_instructions = .;
         *(.altinstructions)
         __alt_instructions_end = .;
+        . = ALIGN(4);
+        __alt_call_sites_start = .;
+        *(.alt_call_sites)
+        __alt_call_sites_end = .;
 
        LOCK_PROFILE_DATA
 
Signature · 1 line
2.39.5
Reviewed-by on v1 5/8
#10 lore raw

Re: [PATCH 5/8] x86/altcall: Introduce new simpler scheme

Quoted 12 lines · Andrew Cooper

Encoding altcalls as regular alternatives leads to an unreasonable amount of complexity in _apply_alternatives().

Introduce apply_alt_calls(), and an .alt_call_sites section which simply tracks the source address (relative, to save on space). That's literally all that is needed in order to devirtualise the function pointers.

apply_alt_calls() is mostly as per _apply_alternatives(), except the size is known to be 6 bytes. Drop the logic for JMP *RIPREL, as there's no support for tailcall optimisations, nor a feasbile plan on how to introduce support. Pad with a redundant prefix to avoid needing a separate NOP on the end.

Quoted 4 lines

Wire it up in nmi_apply_alternatives(), although the section is empty at this juncture so nothing happens in practice.

Quoted 3 lines

Finding a 6-byte UD instruction that is distinct from ud2 turns out to be quite challengning. The easy way involves a length changing prefix, which is best avoided. Suggestions for alternative patterns welcome.

(Intel syntax, sorry.)

	ud0	edi, [edi+edi-1]
	ud1	edi, [edi+edi-1]

	ud0	edi, cs:[rdi+rdi-1]
	ud1	edi, cs:[rdi+rdi-1]

Jan

#11 lore raw

PATCH 6/8 xen/livepatch: Support new altcall scheme

In reply to Andrew Cooper

The new altcall scheme uses an .alt_call_sites section. Wire this up in very much the same way as the .altinstructions section, although there is less sanity checking necessary.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Ross Lagerwall <[email protected]>

---
 xen/arch/x86/alternative.c         |  6 ++++
 xen/common/livepatch.c             | 58 ++++++++++++++++++++++++++++++
 xen/include/xen/alternative-call.h |  8 +++--
 3 files changed, 70 insertions(+), 2 deletions(-)
xen/arch/x86/alternative.c +6 -0
index f6594e21a14c..22af224f08f7 100644
@@ -479,6 +479,12 @@ int apply_alternatives(struct alt_instr *start, struct alt_instr *end)
 {
     return _apply_alternatives(start, end, true);
 }
+
+int livepatch_apply_alt_calls(const struct alt_call *start,
+                              const struct alt_call *end)
+{
+    return apply_alt_calls(start, end);
+}
 #endif
 
 #define ALT_INSNS (1U << 0)
xen/common/livepatch.c +58 -0

64 lines not on this page · raw message

xen/include/xen/alternative-call.h +6 -2
index 3c855bfa44f5..767c2149bce7 100644
@@ -17,8 +17,8 @@
  *   generation requirements are to emit a function pointer call at build
  *   time, and stash enough metadata to simplify the call at boot once the
  *   implementation has been resolved.
- * - Implement boot_apply_alt_calls() to convert the function pointer calls
- *   into direct calls on boot.
+ * - Implement {boot,livepatch}_apply_alt_calls() to convert the function
+ *   pointer calls into direct calls on boot/livepatch.
  * - Select ALTERNATIVE_CALL in Kconfig.
  *
  * To use:
@@ -65,6 +65,10 @@
  */
 void boot_apply_alt_calls(void);
 
+/* As per boot_apply_alt_calls() but for a livepatch. */
+int livepatch_apply_alt_calls(const struct alt_call *start,
+                              const struct alt_call *end);
+
 #else /* CONFIG_ALTERNATIVE_CALL */
 
 #define alternative_call(func, args...)  (func)(args)
Signature · 1 line
2.39.5
Reviewed-by on v1 6/8
#13 lore raw

Re: [PATCH 6/8] xen/livepatch: Support new altcall scheme

In reply to Andrew Cooper
Quoted 5 lines · Andrew Cooper

The new altcall scheme uses an .alt_call_sites section. Wire this up in very much the same way as the .altinstructions section, although there is less sanity checking necessary.

One nit/comment below.

Quoted 36 lines

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Ross Lagerwall <[email protected]>

---
 xen/arch/x86/alternative.c         |  6 ++++
 xen/common/livepatch.c             | 58 ++++++++++++++++++++++++++++++
 xen/include/xen/alternative-call.h |  8 +++--
 3 files changed, 70 insertions(+), 2 deletions(-)
xen/arch/x86/alternative.c +6 -0
index f6594e21a14c..22af224f08f7 100644
@@ -479,6 +479,12 @@ int apply_alternatives(struct alt_instr *start, struct alt_instr *end)
 {
     return _apply_alternatives(start, end, true);
 }
+
+int livepatch_apply_alt_calls(const struct alt_call *start,
+                              const struct alt_call *end)
+{
+    return apply_alt_calls(start, end);
+}
 #endif
 
 #define ALT_INSNS (1U << 0)
xen/common/livepatch.c +1 -0
index 6ce77bf021b7..be9b7e367553 100644
@@ -905,6 +905,64 @@ static int prepare_payload(struct payload *payload,
 #endif
     }
 
+    sec = livepatch_elf_sec_by_name(elf, ".alt_call_sites");

xen/common/livepatch.c static int prepare_payload(struct payload *payload, · line 918

Quoted 10 lines
xen/common/livepatch.c +10 -0
+    if ( sec )
+    {
+#ifdef CONFIG_ALTERNATIVE_CALL
+        const struct alt_call *a, *start, *end;
+
+        if ( !section_ok(elf, sec, sizeof(*a)) )
+            return -EINVAL;
+
+        /* Tolerate an empty .alt_call_sites section... */
+        if ( sec->sec->sh_size == 0 )

You could possibly move this check to the outer if condition, and avoid the alt_call_done label?

As even in the !CONFIG_ALTERNATIVE_CALL case skipping an empty section would be OK.

Thanks, Roger.

#14 lore raw

Re: [PATCH 6/8] xen/livepatch: Support new altcall scheme

Quoted 7 lines · Roger Pau Monné
Quoted 5 lines · Andrew Cooper

The new altcall scheme uses an .alt_call_sites section. Wire this up in very much the same way as the .altinstructions section, although there is less sanity checking necessary.

Thanks.

Quoted 52 lines

One nit/comment below.

Quoted 36 lines

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Ross Lagerwall <[email protected]>

---
 xen/arch/x86/alternative.c         |  6 ++++
 xen/common/livepatch.c             | 58 ++++++++++++++++++++++++++++++
 xen/include/xen/alternative-call.h |  8 +++--
 3 files changed, 70 insertions(+), 2 deletions(-)
xen/arch/x86/alternative.c +6 -0
index f6594e21a14c..22af224f08f7 100644
@@ -479,6 +479,12 @@ int apply_alternatives(struct alt_instr *start, struct alt_instr *end)
 {
     return _apply_alternatives(start, end, true);
 }
+
+int livepatch_apply_alt_calls(const struct alt_call *start,
+                              const struct alt_call *end)
+{
+    return apply_alt_calls(start, end);
+}
 #endif
 
 #define ALT_INSNS (1U << 0)
xen/common/livepatch.c +1 -0
index 6ce77bf021b7..be9b7e367553 100644
@@ -905,6 +905,64 @@ static int prepare_payload(struct payload *payload,
 #endif
     }
 
+    sec = livepatch_elf_sec_by_name(elf, ".alt_call_sites");

xen/common/livepatch.c static int prepare_payload(struct payload *payload, · line 918

Quoted 10 lines
xen/common/livepatch.c +10 -0
+    if ( sec )
+    {
+#ifdef CONFIG_ALTERNATIVE_CALL
+        const struct alt_call *a, *start, *end;
+
+        if ( !section_ok(elf, sec, sizeof(*a)) )
+            return -EINVAL;
+
+        /* Tolerate an empty .alt_call_sites section... */
+        if ( sec->sec->sh_size == 0 )

You could possibly move this check to the outer if condition, and avoid the alt_call_done label?

Quoted 2 lines

As even in the !CONFIG_ALTERNATIVE_CALL case skipping an empty section would be OK.

.altinstructions is like this.  It was put in as part of e74360e4ba4a, I believe because it was decided that an empty section wasn't wanted.

We can revisit the decision, but the logic should be consistent.

~Andrew

#15 lore raw

PATCH 7/8 x86/altcall: Switch to simpler scheme

In reply to Andrew Cooper

With all the infrastructure in place, switch from using ALTERNATIVE() to simply populating .alt_call_sites.

Before, _apply_alternatives() would devirtualise in two passes; the first being opportunistic, and the second (signified by the force parameter) sealing any call with a still-NULL function pointer.

Now, all devirtualising is performed together, at the point in time of the second pass previously. The call to seal_endbr64() needs delaying until after apply_alt_calls() is complete, or we have a narrow window with real indirect branches and no ENDBR64 instructions.

Under the hood, the following changes are happening:

  Section                Old size   New size   Change (%)
  .alt_call_sites               0    0x00730   +0x0730
  .altinstructions        0x1350a    0x11fe0   -0x152a (-7%)
  .altinstr_replacement   0x015f2    0x00e35   -0x07bd (-23%)

The changes aren't quite equal because inlining is affected by the smaller asm() block. Nevertheless, the metadata is held in 1/3 of the space, and there are no CALL instructions held in the replacement section any more.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/alternative.c                  | 9 ++-------
 xen/arch/x86/include/asm/alternative-call.h | 9 ++++++---
 2 files changed, 8 insertions(+), 10 deletions(-)
xen/arch/x86/alternative.c +2 -7
index 22af224f08f7..047bfc6e424b 100644
@@ -378,13 +378,6 @@ static int init_or_livepatch _apply_alternatives(struct alt_instr *start,
         text_poke(orig, buf, total_len);
     }
 
-    /*
-     * Clobber endbr64 instructions now that altcall has finished optimising
-     * all indirect branches to direct ones.
-     */
-    if ( force && system_state < SYS_STATE_active )
-        seal_endbr64();
-
     return 0;
 }
 
@@ -533,6 +526,8 @@ static int __init cf_check nmi_apply_alternatives(
             rc = apply_alt_calls(__alt_call_sites_start, __alt_call_sites_end);
             if ( rc )
                 panic("Unable to apply alternative calls: %d\n", rc);
+
+            seal_endbr64();
         }
 
         /*
xen/arch/x86/include/asm/alternative-call.h +6 -3
index 49a04a7cc45b..bbc49a5274d9 100644
@@ -2,7 +2,8 @@
 #ifndef X86_ALTERNATIVE_CALL_H
 #define X86_ALTERNATIVE_CALL_H
 
-#include <asm/alternative.h>
+#include <xen/macros.h>
+#include <xen/stdint.h>
 
 /* Simply the relative position of the source call. */
 struct alt_call {
@@ -86,8 +87,10 @@ struct alt_call {
     rettype ret_;                                                  \
     register unsigned long r10_ asm("r10");                        \
     register unsigned long r11_ asm("r11");                        \
-    asm volatile (ALTERNATIVE("call *%c[addr](%%rip)", "call .",   \
-                              X86_FEATURE_ALWAYS)                  \
+    asm volatile ("1: call *%c[addr](%%rip)\n\t"                   \
+                  ".pushsection .alt_call_sites, \"a\", @progbits\n\t"  \
+                  ".long 1b - .\n\t"                               \
+                  ".popsection"                                    \
                   : ALT_CALL ## n ## _OUT, "=a" (ret_),            \
                     "=r" (r10_), "=r" (r11_) ASM_CALL_CONSTRAINT   \
                   : [addr] "i" (&(func)), "g" (func)               \
Signature · 1 line
2.39.5
Reviewed-by on v1 7/8
#16 lore raw

Re: [PATCH 7/8] x86/altcall: Switch to simpler scheme

Quoted 17 lines · Andrew Cooper
xen/arch/x86/alternative.c +0 -7
@@ -378,13 +378,6 @@ static int init_or_livepatch _apply_alternatives(struct alt_instr *start,
         text_poke(orig, buf, total_len);
     }
 
-    /*
-     * Clobber endbr64 instructions now that altcall has finished optimising
-     * all indirect branches to direct ones.
-     */
-    if ( force && system_state < SYS_STATE_active )
-        seal_endbr64();
-
     return 0;
 }
 

xen/arch/x86/alternative.c static int __init cf_check nmi_apply_alternatives( · line 531

Quoted 6 lines
xen/arch/x86/alternative.c +2 -0
@@ -533,6 +526,8 @@ static int __init cf_check nmi_apply_alternatives(
             rc = apply_alt_calls(__alt_call_sites_start, __alt_call_sites_end);
             if ( rc )
                 panic("Unable to apply alternative calls: %d\n", rc);
+
+            seal_endbr64();
         }

Are you deliberately losing the comment? Other than this:

Jan

#17 lore raw

Re: [PATCH 7/8] x86/altcall: Switch to simpler scheme

Quoted 17 lines · Jan Beulich
Quoted 16 lines · Andrew Cooper
xen/arch/x86/alternative.c +0 -7
@@ -378,13 +378,6 @@ static int init_or_livepatch _apply_alternatives(struct alt_instr *start,
         text_poke(orig, buf, total_len);
     }
 
-    /*
-     * Clobber endbr64 instructions now that altcall has finished optimising
-     * all indirect branches to direct ones.
-     */
-    if ( force && system_state < SYS_STATE_active )
-        seal_endbr64();
-
     return 0;
 }
 
Quoted 8 lines

xen/arch/x86/alternative.c static int __init cf_check nmi_apply_alternatives( · line 531

Quoted 7 lines
xen/arch/x86/alternative.c +2 -0
@@ -533,6 +526,8 @@ static int __init cf_check nmi_apply_alternatives(
             rc = apply_alt_calls(__alt_call_sites_start, __alt_call_sites_end);
             if ( rc )
                 panic("Unable to apply alternative calls: %d\n", rc);
+
+            seal_endbr64();
         }

Are you deliberately losing the comment?

Yes, counterbalancing the comment gained in patch 4.

Quoted 2 lines

Other than this:

Thanks,

~Andrew

#18 lore raw

PATCH 8/8 x86/alternatives: Simplify _apply_alternatives() now altcall is separate

In reply to Andrew Cooper

With altcall handled separately, the special case in _apply_alternatives() is unused and can be dropped. The force parameter (used to signify the seal pass) can be removed too.

In turn, nmi_apply_alternatives() no longer needs to call _apply_alternatives() on the second pass.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/alternative.c | 94 ++++----------------------------------
 1 file changed, 10 insertions(+), 84 deletions(-)
xen/arch/x86/alternative.c +10 -84
index 047bfc6e424b..43b009888c02 100644
@@ -206,14 +206,9 @@ static void __init seal_endbr64(void)
  * self modifying code. This implies that asymmetric systems where
  * APs have less capabilities than the boot processor are not handled.
  * Tough. Make sure you disable such features by hand.
- *
- * The caller will set the "force" argument to true for the final
- * invocation, such that no CALLs/JMPs to NULL pointers will be left
- * around. See also the further comment below.
  */
 static int init_or_livepatch _apply_alternatives(struct alt_instr *start,
-                                                 struct alt_instr *end,
-                                                 bool force)
+                                                 struct alt_instr *end)
 {
     struct alt_instr *a, *base;
 
@@ -274,10 +269,7 @@ static int init_or_livepatch _apply_alternatives(struct alt_instr *start,
 
         /* Skip patch sites already handled during the first pass. */
         if ( a->priv )
-        {
-            ASSERT(force);
             continue;
-        }
 
         /* If there is no replacement to make, see about optimising the nops. */
         if ( !boot_cpu_has(a->cpuid) )
@@ -301,76 +293,7 @@ static int init_or_livepatch _apply_alternatives(struct alt_instr *start,
 
         /* 0xe8/0xe9 are relative branches; fix the offset. */
         if ( a->repl_len >= 5 && (*buf & 0xfe) == 0xe8 )
-        {
-            /*
-             * Detect the special case of indirect-to-direct branch patching:
-             * - replacement is a direct CALL/JMP (opcodes 0xE8/0xE9; already
-             *   checked above),
-             * - replacement's displacement is -5 (pointing back at the very
-             *   insn, which makes no sense in a real replacement insn),
-             * - original is an indirect CALL/JMP (opcodes 0xFF/2 or 0xFF/4)
-             *   using RIP-relative addressing.
-             * Some branch destinations may still be NULL when we come here
-             * the first time. Defer patching of those until the post-presmp-
-             * initcalls re-invocation (with force set to true). If at that
-             * point the branch destination is still NULL, insert "UD2; UD0"
-             * (for ease of recognition) instead of CALL/JMP.
-             */
-            if ( a->cpuid == X86_FEATURE_ALWAYS &&
-                 *(int32_t *)(buf + 1) == -5 &&
-                 a->orig_len >= 6 &&
-                 orig[0] == 0xff &&
-                 orig[1] == (*buf & 1 ? 0x25 : 0x15) )
-            {
-                long disp = *(int32_t *)(orig + 2);
-                const uint8_t *dest = *(void **)(orig + 6 + disp);
-
-                if ( dest )
-                {
-                    /*
-                     * When building for CET-IBT, all function pointer targets
-                     * should have an endbr64 instruction.
-                     *
-                     * If this is not the case, leave a warning because
-                     * something is probably wrong with the build.  A CET-IBT
-                     * enabled system might have exploded already.
-                     *
-                     * Otherwise, skip the endbr64 instruction.  This is a
-                     * marginal perf improvement which saves on instruction
-                     * decode bandwidth.
-                     */
-                    if ( IS_ENABLED(CONFIG_XEN_IBT) )
-                    {
-                        if ( is_endbr64(dest) )
-                            dest += ENDBR64_LEN;
-                        else
-                            printk(XENLOG_WARNING
-                                   "altcall %ps dest %ps has no endbr64\n",
-                                   orig, dest);
-                    }
-
-                    disp = dest - (orig + 5);
-                    ASSERT(disp == (int32_t)disp);
-                    *(int32_t *)(buf + 1) = disp;
-                }
-                else if ( force )
-                {
-                    buf[0] = 0x0f;
-                    buf[1] = 0x0b;
-                    buf[2] = 0x0f;
-                    buf[3] = 0xff;
-                    buf[4] = 0xff;
-                }
-                else
-                    continue;
-            }
-            else if ( force && system_state < SYS_STATE_active )
-                ASSERT_UNREACHABLE();
-            else
-                *(int32_t *)(buf + 1) += repl - orig;
-        }
-        else if ( force && system_state < SYS_STATE_active  )
-            ASSERT_UNREACHABLE();
+            *(int32_t *)(buf + 1) += repl - orig;
 
         a->priv = 1;
 
@@ -470,7 +393,7 @@ static int init_or_livepatch apply_alt_calls(
 #ifdef CONFIG_LIVEPATCH
 int apply_alternatives(struct alt_instr *start, struct alt_instr *end)
 {
-    return _apply_alternatives(start, end, true);
+    return _apply_alternatives(start, end);
 }
 
 int livepatch_apply_alt_calls(const struct alt_call *start,
@@ -516,10 +439,13 @@ static int __init cf_check nmi_apply_alternatives(
                                  PAGE_HYPERVISOR_RWX);
         flush_local(FLUSH_TLB_GLOBAL);
 
-        rc = _apply_alternatives(__alt_instructions, __alt_instructions_end,
-                                 alt_todo == ALT_CALLS);
-        if ( rc )
-            panic("Unable to apply alternatives: %d\n", rc);
+        if ( alt_todo & ALT_INSNS )
+        {
+            rc = _apply_alternatives(__alt_instructions,
+                                     __alt_instructions_end);
+            if ( rc )
+                panic("Unable to apply alternatives: %d\n", rc);
+        }
 
         if ( alt_todo & ALT_CALLS )
         {
Signature · 1 line
2.39.5
Reviewed-by on v1 8/8
#19 lore raw

Re: [PATCH 8/8] x86/alternatives: Simplify _apply_alternatives() now altcall is separate

Quoted 10 lines · Andrew Cooper

With altcall handled separately, the special case in _apply_alternatives() is unused and can be dropped. The force parameter (used to signify the seal pass) can be removed too.

In turn, nmi_apply_alternatives() no longer needs to call _apply_alternatives() on the second pass.

No functional change.