Skip to contentSkip to conversation

x86/entry: More cleanup

PATCH In review Andrew Cooper 2 participants 8 messages Feb 26, 2024

lore
Share & tools
lore
#0 lore raw

cover x86/entry: More cleanup

Misc adjustments and cleanup. No functional change.

Andrew Cooper (3):
x86/entry: Adjustments to "reduce assembly code size of entry points" x86/entry: Simplify expressions in compat_restore_all_guest() x86/entry: Introduce EFRAME_* constants

 xen/arch/x86/include/asm/asm_defns.h | 12 +++++
 xen/arch/x86/x86_64/asm-offsets.c    | 17 +++++++
 xen/arch/x86/x86_64/compat/entry.S   | 12 ++---
 xen/arch/x86/x86_64/entry.S          | 76 +++++++++++++---------------
 4 files changed, 69 insertions(+), 48 deletions(-)

base-commit: 8de3afc0b402bc17f65093a53e5870862707a8c7

Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/3 x86/entry: Adjustments to "reduce assembly code size of entry points"

Some retroactive review, for if I'd got to the patch in time.

* The new ASM-friendly BUILD_BUG_ON() should be in a header file.
* entry_int82() wants the movl->movb treatment too.

Fixes c144b9e32427 ("x86: Reduce assembly code size of entry points")

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

Fixes just to link the two patches.

---
 xen/arch/x86/include/asm/asm_defns.h | 12 ++++++++++++
 xen/arch/x86/x86_64/compat/entry.S   |  2 +-
 xen/arch/x86/x86_64/entry.S          |  8 --------
 3 files changed, 13 insertions(+), 9 deletions(-)
xen/arch/x86/include/asm/asm_defns.h +12 -0
index a9a6c21c76cd..f18a11b36198 100644
@@ -56,6 +56,18 @@ register unsigned long current_stack_pointer asm("rsp");
 #define ASSERT_INTERRUPTS_DISABLED \
     ASSERT_INTERRUPT_STATUS(z, "INTERRUPTS DISABLED")
 
+#ifdef __ASSEMBLY__
+
+.macro BUILD_BUG_ON condstr cond:vararg
+        .if \cond
+        .error "Condition \"\condstr\" not satisfied"
+        .endif
+.endm
+/* preprocessor macro to make error message more user friendly */
+#define BUILD_BUG_ON(cond) BUILD_BUG_ON #cond cond
+
+#endif /* __ASSEMBLY__ */
+
 #ifdef __ASSEMBLY__
 # define _ASM_EX(p) p-.
 #else
xen/arch/x86/x86_64/compat/entry.S +1 -1
index d4f0e4804090..93fbbeb4ae18 100644
@@ -15,7 +15,7 @@ FUNC(entry_int82)
         ENDBR64
         ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
         pushq $0
-        movl  $HYPERCALL_VECTOR, 4(%rsp)
+        movb  $HYPERCALL_VECTOR, 4(%rsp)
         SAVE_ALL compat=1 /* DPL1 gate, restricted to 32bit PV guests only. */
 
         SPEC_CTRL_ENTRY_FROM_PV /* Req: %rsp=regs/cpuinfo, %rdx=0, Clob: acd */
xen/arch/x86/x86_64/entry.S +0 -8
index a7bd8f0ca5b1..f8938b0b42fd 100644
@@ -22,14 +22,6 @@
 #endif
 .endm
 
-.macro BUILD_BUG_ON condstr cond:vararg
-        .if \cond
-        .error "Condition \"\condstr\" not satisfied"
-        .endif
-.endm
-/* preprocessor macro to make error message more user friendly */
-#define BUILD_BUG_ON(cond) BUILD_BUG_ON #cond cond
-
 #ifdef CONFIG_PV
 /* %rbx: struct vcpu */
 FUNC_LOCAL(switch_to_kernel)
Signature · 1 line
2.30.2
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] x86/entry: Adjustments to "reduce assembly code size of entry points"

Quoted 7 lines · Andrew Cooper

Some retroactive review, for if I'd got to the patch in time.

* The new ASM-friendly BUILD_BUG_ON() should be in a header file.
* entry_int82() wants the movl->movb treatment too.

Fixes c144b9e32427 ("x86: Reduce assembly code size of entry points")
Quoted 1 line

Fixes just to link the two patches.

Hmm, I see.

Quoted 9 lines
xen/arch/x86/include/asm/asm_defns.h +3 -0
@@ -56,6 +56,18 @@ register unsigned long current_stack_pointer asm("rsp");
 #define ASSERT_INTERRUPTS_DISABLED \
     ASSERT_INTERRUPT_STATUS(z, "INTERRUPTS DISABLED")
 
+#ifdef __ASSEMBLY__
+
+.macro BUILD_BUG_ON condstr cond:vararg

xen/arch/x86/include/asm/asm_defns.h register unsigned long current_stack_pointer asm("rsp"); · line 71

Quoted 10 lines
xen/arch/x86/include/asm/asm_defns.h +9 -0
+        .if \cond
+        .error "Condition \"\condstr\" not satisfied"
+        .endif
+.endm
+/* preprocessor macro to make error message more user friendly */
+#define BUILD_BUG_ON(cond) BUILD_BUG_ON #cond cond
+
+#endif /* __ASSEMBLY__ */
+
 #ifdef __ASSEMBLY__

Minor remark: Seeing these back to back is slightly odd. A little higher up there's an "#ifndef __ASSEMBLY__" (producing e.g. ASM_CALL_CONSTRAINT) in an #else of which this would imo be a pretty good fit. Further down where SUBSECTION_LBL() is defined would also look to be more suitable than adding yet another #ifdef.

Jan

#3 lore raw

PATCH 2/3 x86/entry: Simplify expressions in compat_restore_all_guest()

In reply to Andrew Cooper

compat_restore_all_guest() already has SPEC_CTRL_EXIT_TO_PV with a documented requirement for %rsp to be both regs and cpuinfo.

Use the now-normal annotations and simplify the expressions which happen to be a subtraction of 0.

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

---
 xen/arch/x86/x86_64/compat/entry.S | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
xen/arch/x86/x86_64/compat/entry.S +5 -5
index 93fbbeb4ae18..727ab65290de 100644
@@ -117,19 +117,19 @@ compat_process_trap:
         jmp   compat_test_all_events
 END(compat_test_all_events)
 
-/* %rbx: struct vcpu, interrupts disabled */
+/* %rbx: struct vcpu, interrupts disabled, %rsp=regs/cpuinfo */
 FUNC(compat_restore_all_guest)
         ASSERT_INTERRUPTS_DISABLED
         mov   $~(X86_EFLAGS_IOPL | X86_EFLAGS_VM), %r11d
         and   UREGS_eflags(%rsp),%r11d
 
-.macro alt_cr4_pv32
+.macro alt_cr4_pv32 /* %rsp=regs/cpuinfo */
         testb $3,UREGS_cs(%rsp)
         jpe   2f
-        mov   CPUINFO_cr4-CPUINFO_guest_cpu_user_regs(%rsp), %rax
+        mov   CPUINFO_cr4(%rsp), %rax
         and   $~XEN_CR4_PV32_BITS, %rax
 1:
-        mov   %rax, CPUINFO_cr4-CPUINFO_guest_cpu_user_regs(%rsp)
+        mov   %rax, CPUINFO_cr4(%rsp)
         mov   %rax, %cr4
         /*
          * An NMI or MCE may have occurred between the previous two
@@ -141,7 +141,7 @@ FUNC(compat_restore_all_guest)
          * loop to cause a live lock: If NMIs/MCEs occurred at that
          * high a rate, we'd be live locked anyway.
          */
-        cmp   %rax, CPUINFO_cr4-CPUINFO_guest_cpu_user_regs(%rsp)
+        cmp   %rax, CPUINFO_cr4(%rsp)
         jne   1b
 2:
 .endm
Signature · 1 line
2.30.2
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] x86/entry: Simplify expressions in compat_restore_all_guest()

Quoted 7 lines · Andrew Cooper

compat_restore_all_guest() already has SPEC_CTRL_EXIT_TO_PV with a documented requirement for %rsp to be both regs and cpuinfo.

Use the now-normal annotations and simplify the expressions which happen to be a subtraction of 0.

#5 lore raw

PATCH 3/3 x86/entry: Introduce EFRAME_* constants

In reply to Andrew Cooper

restore_all_guest() does a lot of manipulation of the stack after popping the GPRs, and uses raw %rsp displacements to do so. Also, almost all entrypaths use raw %rsp displacements prior to pushing GPRs.

Provide better mnemonics, to aid readability and reduce the chance of errors when editing.

No functional change. The resulting binary is identical.

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

---
 xen/arch/x86/x86_64/asm-offsets.c  | 17 ++++++++
 xen/arch/x86/x86_64/compat/entry.S |  2 +-
 xen/arch/x86/x86_64/entry.S        | 68 +++++++++++++++---------------
 3 files changed, 52 insertions(+), 35 deletions(-)
xen/arch/x86/x86_64/asm-offsets.c +17 -0
index fee0edc61abb..4cc23cd032c1 100644
@@ -51,6 +51,23 @@ void __dummy__(void)
     OFFSET(UREGS_kernel_sizeof, struct cpu_user_regs, es);
     BLANK();
 
+    /*
+     * EFRAME_* is for the entry/exit logic where %rsp is pointing at
+     * UREGS_error_code and GPRs are still guest values.
+     */
+#define OFFSET_EF(sym, mem)                                             \
+    DEFINE(sym, offsetof(struct cpu_user_regs, mem) -                   \
+                offsetof(struct cpu_user_regs, error_code))
+
+    OFFSET_EF(EFRAME_entry_vector,    entry_vector);
+    OFFSET_EF(EFRAME_rip,             rip);
+    OFFSET_EF(EFRAME_cs,              cs);
+    OFFSET_EF(EFRAME_eflags,          eflags);
+    OFFSET_EF(EFRAME_rsp,             rsp);
+    BLANK();
+
+#undef OFFSET_EF
+
     OFFSET(VCPU_processor, struct vcpu, processor);
     OFFSET(VCPU_domain, struct vcpu, domain);
     OFFSET(VCPU_vcpu_info, struct vcpu, vcpu_info_area.map);
xen/arch/x86/x86_64/compat/entry.S +1 -1
index 727ab65290de..2f8fe5ebfbe4 100644
@@ -15,7 +15,7 @@ FUNC(entry_int82)
         ENDBR64
         ALTERNATIVE "", clac, X86_FEATURE_XEN_SMAP
         pushq $0
-        movb  $HYPERCALL_VECTOR, 4(%rsp)
+        movb  $HYPERCALL_VECTOR, EFRAME_entry_vector(%rsp)
         SAVE_ALL compat=1 /* DPL1 gate, restricted to 32bit PV guests only. */
 
         SPEC_CTRL_ENTRY_FROM_PV /* Req: %rsp=regs/cpuinfo, %rdx=0, Clob: acd */
xen/arch/x86/x86_64/entry.S +34 -34

236 lines not on this page · raw message

Signature · 1 line
2.30.2
Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] x86/entry: Introduce EFRAME_* constants

Quoted 10 lines · Andrew Cooper

restore_all_guest() does a lot of manipulation of the stack after popping the GPRs, and uses raw %rsp displacements to do so. Also, almost all entrypaths use raw %rsp displacements prior to pushing GPRs.

Provide better mnemonics, to aid readability and reduce the chance of errors when editing.

No functional change. The resulting binary is identical.

with one small request:

xen/arch/x86/x86_64/asm-offsets.c void __dummy__(void) · line 57

Quoted 10 lines
xen/arch/x86/x86_64/asm-offsets.c +4 -0
@@ -51,6 +51,23 @@ void __dummy__(void)
     OFFSET(UREGS_kernel_sizeof, struct cpu_user_regs, es);
     BLANK();
 
+    /*
+     * EFRAME_* is for the entry/exit logic where %rsp is pointing at
+     * UREGS_error_code and GPRs are still guest values.
+     */

"still/already" or some such to match "entry/exit"?

Jan

#7 lore raw

Re: [PATCH 3/3] x86/entry: Introduce EFRAME_* constants

Quoted 5 lines · Jan Beulich
Quoted 4 lines · Andrew Cooper

restore_all_guest() does a lot of manipulation of the stack after popping the GPRs, and uses raw %rsp displacements to do so. Also, almost all entrypaths use raw %rsp displacements prior to pushing GPRs.

Quoted 7 lines
Quoted 6 lines

Provide better mnemonics, to aid readability and reduce the chance of errors when editing.

No functional change. The resulting binary is identical.

Thanks.

Quoted 8 lines

with one small request:

Quoted 6 lines
xen/arch/x86/x86_64/asm-offsets.c +0 -0
@@ -51,6 +51,23 @@ void __dummy__(void)
     OFFSET(UREGS_kernel_sizeof, struct cpu_user_regs, es);
     BLANK();
 
Quoted 5 lines

xen/arch/x86/x86_64/asm-offsets.c void __dummy__(void) · line 57

Quoted 4 lines
xen/arch/x86/x86_64/asm-offsets.c +4 -0
+    /*
+     * EFRAME_* is for the entry/exit logic where %rsp is pointing at
+     * UREGS_error_code and GPRs are still guest values.
+     */

"still/already" or some such to match "entry/exit"?

Ok.

~Andrew