Skip to contentSkip to conversation

x86: alternatives handling adjustments

PATCH In review Jan Beulich 2 participants 4 messages Nov 17, 2021 – Nov 18, 2021

lore
Share & tools
lore
#0 lore raw

cover x86: alternatives handling adjustments

The 1st patch here is incomplete and hence RFC, while the 2nd is merely a tiny bit of cleanup noticed on the way.

1: allow replacement code snippets to be merged
2: adjust alternative_vcall0()

Jan

#1 lore raw

RFC PATCH 1/2 x86/alternatives: allow replacement code snippets to be merged

Prior to b11380f6cd58 ("x86/spec-ctrl: Build with BRANCH_HARDEN lfences by default") LFENCE was the dominant entry. But even without it there's still quite a bit of redundancy, much of which can be eliminated by marking the sections as mergable. This of course also requires splitting them by "element" size.

Note that due to gas restrictions replacement code containing Jcc or PC-relative JMP cannot be made subject to merging. Hence the original assembly macros can't be altered, but replacement ones need to be introduced (and then used selectively).

Note that CR4_PV32_RESTORE could in principle also benefit from getting changed, but right now at least GNU ld doesn't merge sections with relocations. Since this is a per-section decision, not an element based one, marking the items for merging could thus get in the way of merging other same-sized entries originating from the same source files.

The sorting in the linker script additions is merely for cosmetic reasons, easing manual inspection of the resulting binary.

---
Obviously the new assembler macros could be merged with the existing ones, reducing redundancy. But I didn't want to do so right away, i.e. before knowing whether the basic approach is deemed viable (and worthwhile, considering it requires a relatively new gas).

For the same reason there are no C macro variants just yet (which overall would provide larger savings: in particular all altcalls can have their replacement insns folded).

xen/arch/x86/arch.mk +5 -0
@@ -35,6 +35,11 @@ $(call as-option-add,CFLAGS,CC,\
 $(call as-option-add,CFLAGS,CC,\
     ".L1: .L2: .nops (.L2 - .L1)$$(comma)9",-DHAVE_AS_NOPS_DIRECTIVE)
 
+# Check to see whether the assembler supports insn emission to the absolute
+# section (via the .struct directive), to size them into an absolute symbol.
+$(call as-option-add,CFLAGS,CC,\
+    ".pushsection .text; .struct; lfence; .L0:; .if .L0 != 3; .error; .endif; .popsection",-DHAVE_AS_INSN_SIZE)
+
 CFLAGS += -mno-red-zone -fpic
 
 # Xen doesn't use SSE interally.  If the compiler supports it, also skip the
xen/arch/x86/x86_64/compat/entry.S +4 -4
@@ -12,7 +12,7 @@
 #include <irq_vectors.h>
 
 ENTRY(entry_int82)
-        ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
+        MERGE_ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
         pushq $0
         movl  $HYPERCALL_VECTOR, 4(%rsp)
         SAVE_ALL compat=1 /* DPL1 gate, restricted to 32bit PV guests only. */
@@ -248,7 +248,7 @@ ENTRY(compat_int80_direct_trap)
 compat_create_bounce_frame:
         ASSERT_INTERRUPTS_ENABLED
         mov   %fs,%edi
-        ALTERNATIVE "", stac, X86_FEATURE_XEN_SMAP
+        MERGE_ALTERNATIVE "", stac, X86_FEATURE_XEN_SMAP
         testb $2,UREGS_cs+8(%rsp)
         jz    1f
         /* Push new frame at registered guest-OS stack base. */
@@ -295,7 +295,7 @@ compat_create_bounce_frame:
         movl  TRAPBOUNCE_error_code(%rdx),%eax
 .Lft8:  movl  %eax,%fs:(%rsi)           # ERROR CODE
 1:
-        ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
+        MERGE_ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
         /* Rewrite our stack frame and return to guest-OS mode. */
         /* IA32 Ref. Vol. 3: TF, VM, RF and NT flags are cleared on trap. */
         andl  $~(X86_EFLAGS_VM|X86_EFLAGS_RF|\
@@ -341,7 +341,7 @@ compat_crash_page_fault_4:
         addl  $4,%esi
 compat_crash_page_fault:
 .Lft14: mov   %edi,%fs
-        ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
+        MERGE_ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
         movl  %esi,%edi
         call  show_page_walk
         jmp   dom_crash_sync_extable
xen/arch/x86/x86_64/entry.S +14 -14
@@ -249,7 +249,7 @@ iret_exit_to_guest:
  */
 ENTRY(lstar_enter)
 #ifdef CONFIG_XEN_SHSTK
-        ALTERNATIVE "", "setssbsy", X86_FEATURE_XEN_SHSTK
+        MERGE_ALTERNATIVE "", "setssbsy", X86_FEATURE_XEN_SHSTK
 #endif
         /* sti could live here when we don't switch page tables below. */
         movq  8(%rsp),%rax /* Restore %rax. */
@@ -286,7 +286,7 @@ ENTRY(lstar_enter)
 /* See lstar_enter for entry register state. */
 ENTRY(cstar_enter)
 #ifdef CONFIG_XEN_SHSTK
-        ALTERNATIVE "", "setssbsy", X86_FEATURE_XEN_SHSTK
+        MERGE_ALTERNATIVE "", "setssbsy", X86_FEATURE_XEN_SHSTK
 #endif
         /* sti could live here when we don't switch page tables below. */
         CR4_PV32_RESTORE
@@ -324,14 +324,14 @@ ENTRY(cstar_enter)
 
 ENTRY(sysenter_entry)
 #ifdef CONFIG_XEN_SHSTK
-        ALTERNATIVE "", "setssbsy", X86_FEATURE_XEN_SHSTK
+        MERGE_ALTERNATIVE "", "setssbsy", X86_FEATURE_XEN_SHSTK
 #endif
         /* sti could live here when we don't switch page tables below. */
         pushq $FLAT_USER_SS
         pushq $0
         pushfq
 GLOBAL(sysenter_eflags_saved)
-        ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
+        MERGE_ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
         pushq $3 /* ring 3 null cs */
         pushq $0 /* null rip */
         pushq $0
@@ -386,7 +386,7 @@ UNLIKELY_END(sysenter_gpf)
         jmp   .Lbounce_exception
 
 ENTRY(int80_direct_trap)
-        ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
+        MERGE_ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
         pushq $0
         movl  $0x80, 4(%rsp)
         SAVE_ALL
@@ -514,7 +514,7 @@ __UNLIKELY_END(create_bounce_frame_bad_s
 
         subq  $7*8,%rsi
         movq  UREGS_ss+8(%rsp),%rax
-        ALTERNATIVE "", stac, X86_FEATURE_XEN_SMAP
+        MERGE_ALTERNATIVE "", stac, X86_FEATURE_XEN_SMAP
         movq  VCPU_domain(%rbx),%rdi
         STORE_GUEST_STACK(rax,6)        # SS
         movq  UREGS_rsp+8(%rsp),%rax
@@ -552,7 +552,7 @@ __UNLIKELY_END(create_bounce_frame_bad_s
         STORE_GUEST_STACK(rax,1)        # R11
         movq  UREGS_rcx+8(%rsp),%rax
         STORE_GUEST_STACK(rax,0)        # RCX
-        ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
+        MERGE_ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
 
 #undef STORE_GUEST_STACK
 
@@ -594,11 +594,11 @@ domain_crash_page_fault_2x8:
 domain_crash_page_fault_1x8:
         addq  $8,%rsi
 domain_crash_page_fault_0x8:
-        ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
+        MERGE_ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
         movq  %rsi,%rdi
         call  show_page_walk
 ENTRY(dom_crash_sync_extable)
-        ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
+        MERGE_ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
         # Get out of the guest-save area of the stack.
         GET_STACK_END(ax)
         leaq  STACK_CPUINFO_FIELD(guest_cpu_user_regs)(%rax),%rsp
@@ -667,7 +667,7 @@ UNLIKELY_END(exit_cr3)
         iretq
 
 ENTRY(common_interrupt)
-        ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
+        MERGE_ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
         SAVE_ALL
 
         GET_STACK_END(14)
@@ -700,7 +700,7 @@ ENTRY(page_fault)
         movl  $TRAP_page_fault,4(%rsp)
 /* No special register assumptions. */
 GLOBAL(handle_exception)
-        ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
+        MERGE_ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
         SAVE_ALL
 
         GET_STACK_END(14)
@@ -728,7 +728,7 @@ handle_exception_saved:
         jz    exception_with_ints_disabled
 
 #if defined(CONFIG_PV32)
-        ALTERNATIVE_2 "jmp .Lcr4_pv32_done", \
+        MERGE_ALTERNATIVE_2 "jmp .Lcr4_pv32_done", \
             __stringify(mov VCPU_domain(%rbx), %rax), X86_FEATURE_XEN_SMEP, \
             __stringify(mov VCPU_domain(%rbx), %rax), X86_FEATURE_XEN_SMAP
 
@@ -908,7 +908,7 @@ ENTRY(entry_CP)
 ENTRY(double_fault)
         movl  $TRAP_double_fault,4(%rsp)
         /* Set AC to reduce chance of further SMAP faults */
-        ALTERNATIVE "", stac, X86_FEATURE_XEN_SMAP
+        MERGE_ALTERNATIVE "", stac, X86_FEATURE_XEN_SMAP
         SAVE_ALL
 
         GET_STACK_END(14)
@@ -942,7 +942,7 @@ ENTRY(nmi)
         pushq $0
         movl  $TRAP_nmi,4(%rsp)
 handle_ist_exception:
-        ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
+        MERGE_ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
         SAVE_ALL
 
         GET_STACK_END(14)
xen/arch/x86/xen.lds.S +2 -0
@@ -192,6 +192,8 @@ SECTIONS
         * the address and the length of them to patch the kernel safely.
         */
        *(.altinstr_replacement)
+       *(SORT(.altinstr_replacement.?))
+       *(SORT(.altinstr_replacement.*))
 
 #ifdef EFI /* EFI wants to merge all of .init.*  ELF doesn't. */
        . = ALIGN(SMP_CACHE_BYTES);
xen/include/asm-x86/alternative-asm.h +83 -0

95 lines not on this page · raw message

xen/include/asm-x86/spec_ctrl_asm.h +7 -7
@@ -228,19 +228,19 @@
 /* Use after a VMEXIT from an HVM guest. */
 #define SPEC_CTRL_ENTRY_FROM_HVM                                        \
     ALTERNATIVE "", DO_OVERWRITE_RSB, X86_FEATURE_SC_RSB_HVM;           \
-    ALTERNATIVE "", DO_SPEC_CTRL_ENTRY_FROM_HVM,                        \
+    MERGE_ALTERNATIVE "", DO_SPEC_CTRL_ENTRY_FROM_HVM,                  \
         X86_FEATURE_SC_MSR_HVM
 
 /* Use after an entry from PV context (syscall/sysenter/int80/int82/etc). */
 #define SPEC_CTRL_ENTRY_FROM_PV                                         \
     ALTERNATIVE "", DO_OVERWRITE_RSB, X86_FEATURE_SC_RSB_PV;            \
-    ALTERNATIVE "", __stringify(DO_SPEC_CTRL_ENTRY maybexen=0),         \
+    MERGE_ALTERNATIVE "", __stringify(DO_SPEC_CTRL_ENTRY maybexen=0),   \
         X86_FEATURE_SC_MSR_PV
 
 /* Use in interrupt/exception context.  May interrupt Xen or PV context. */
 #define SPEC_CTRL_ENTRY_FROM_INTR                                       \
     ALTERNATIVE "", DO_OVERWRITE_RSB, X86_FEATURE_SC_RSB_PV;            \
-    ALTERNATIVE "", __stringify(DO_SPEC_CTRL_ENTRY maybexen=1),         \
+    MERGE_ALTERNATIVE "", __stringify(DO_SPEC_CTRL_ENTRY maybexen=1),   \
         X86_FEATURE_SC_MSR_PV
 
 /* Use when exiting to Xen context. */
@@ -250,16 +250,16 @@
 
 /* Use when exiting to PV guest context. */
 #define SPEC_CTRL_EXIT_TO_PV                                            \
-    ALTERNATIVE "",                                                     \
+    MERGE_ALTERNATIVE "",                                               \
         DO_SPEC_CTRL_EXIT_TO_GUEST, X86_FEATURE_SC_MSR_PV;              \
-    ALTERNATIVE "", __stringify(verw CPUINFO_verw_sel(%rsp)),           \
+    MERGE_ALTERNATIVE "", __stringify(verw CPUINFO_verw_sel(%rsp)),     \
         X86_FEATURE_SC_VERW_PV
 
 /* Use when exiting to HVM guest context. */
 #define SPEC_CTRL_EXIT_TO_HVM                                           \
-    ALTERNATIVE "",                                                     \
+    MERGE_ALTERNATIVE "",                                               \
         DO_SPEC_CTRL_EXIT_TO_GUEST, X86_FEATURE_SC_MSR_HVM;             \
-    ALTERNATIVE "", __stringify(verw CPUINFO_verw_sel(%rsp)),           \
+    MERGE_ALTERNATIVE "", __stringify(verw CPUINFO_verw_sel(%rsp)),     \
         X86_FEATURE_SC_VERW_HVM
 
 /*
#2 lore raw

PATCH 2/2 x86/alternatives: adjust alternative_vcall0()

In reply to Jan Beulich

I'm puzzled about two inconsistencies with other alternative_vcall<N>()
here: There's a check missing that the supplied function pointer is actually pointing to a function taking no args. And there's a pointless pair of parentheses. Correct both.

Fixes 67d01cdb5518 ("x86: infrastructure to allow converting certain indirect calls to direct ones")
xen/include/asm-x86/alternative.h +2 -1
@@ -218,7 +218,8 @@ extern void alternative_branches(void);
 
 #define alternative_vcall0(func) ({             \
     ALT_CALL_NO_ARG1;                           \
-    ((void)alternative_callN(0, int, func));    \
+    (void)sizeof(func());                       \
+    (void)alternative_callN(0, int, func);      \
 })
 
 #define alternative_call0(func) ({              \
Reviewed-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] x86/alternatives: adjust alternative_vcall0()

Quoted 13 lines · Jan Beulich

I'm puzzled about two inconsistencies with other alternative_vcall<N>()
here: There's a check missing that the supplied function pointer is actually pointing to a function taking no args. And there's a pointless pair of parentheses. Correct both.

Fixes 67d01cdb5518 ("x86: infrastructure to allow converting certain indirect calls to direct ones")

xen/include/asm-x86/alternative.h extern void alternative_branches(void); · line 225

Quoted 9 lines
xen/include/asm-x86/alternative.h +2 -1
@@ -218,7 +218,8 @@ extern void alternative_branches(void);
 
  #define alternative_vcall0(func) ({             \
      ALT_CALL_NO_ARG1;                           \
-    ((void)alternative_callN(0, int, func));    \
+    (void)sizeof(func());                       \
+    (void)alternative_callN(0, int, func);      \
  })
 
  #define alternative_call0(func) ({              \