Skip to contentSkip to conversation

xen/x86: Implement enable_nmis() in C

PATCH In review Andrew Cooper 2 participants 4 messages Mar 15, 2018 – Mar 16, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/x86: Implement enable_nmis() in C

I don't recall why I chose to implement this in assembly to begin with, but it can happily live in a static inline instead, and only has two callers.

Doing so reduces the quantity of code in .text.entry.

---
Looking at the disassembly, this change causes machine_kexec to avoid spilling any registers onto the stack.

This patch is intended as to simplify "x86/XPTI: reduce .text.entry" by not having enable_nmis to worry about at all.

---
 xen/arch/x86/x86_64/entry.S     | 16 ----------------
 xen/include/asm-x86/processor.h | 17 ++++++++++++++++-
 2 files changed, 16 insertions(+), 17 deletions(-)
xen/arch/x86/x86_64/entry.S +0 -16
index f4e1b80..f6ba31c 100644
@@ -854,22 +854,6 @@ ENTRY(machine_check)
         movl  $TRAP_machine_check,4(%rsp)
         jmp   handle_ist_exception
 
-/* Enable NMIs.  No special register assumptions. Only %rax is not preserved. */
-ENTRY(enable_nmis)
-        movq  %rsp, %rax /* Grab RSP before pushing */
-
-        /* Set up stack frame */
-        pushq $0               /* SS */
-        pushq %rax             /* RSP */
-        pushfq                 /* RFLAGS */
-        pushq $__HYPERVISOR_CS /* CS */
-        leaq  1f(%rip),%rax
-        pushq %rax             /* RIP */
-
-        iretq /* Disable the hardware NMI latch */
-1:
-        retq
-
 /* No op trap handler.  Required for kexec crash path. */
 GLOBAL(trap_nop)
         iretq
xen/include/asm-x86/processor.h +16 -1
index 01bc89f..2cfd2c3 100644
@@ -498,7 +498,22 @@ DECLARE_TRAP_HANDLER(entry_int82);
 #undef DECLARE_TRAP_HANDLER
 
 void trap_nop(void);
-void enable_nmis(void);
+
+static inline void enable_nmis(void)
+{
+    unsigned long tmp;
+
+    asm volatile ( "mov %%rsp, %[sp]     \n\t"
+                   "push %[ss]           \n\t"
+                   "push %[sp]           \n\t"
+                   "pushf                \n\t"
+                   "push %[cs]           \n\t"
+                   "lea 1f(%%rip), %[ip] \n\t"
+                   "push %[ip]           \n\t"
+                   "iretq; 1:            \n\t"
+                   : [sp] "=r" (tmp), [ip] "=r" (tmp)
+                   : [ss] "i" (__HYPERVISOR_DS), [cs] "i" (__HYPERVISOR_CS) );
+}
 
 void sysenter_entry(void);
 void sysenter_eflags_saved(void);
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 15.03.18 at 17:43, <[email protected]> wrote:
xen/include/asm-x86/processor.h +3 -0
+static inline void enable_nmis(void)
+{
+    unsigned long tmp;

xen/include/asm-x86/processor.h DECLARE_TRAP_HANDLER(entry_int82); · line 514

Quoted 10 lines
xen/include/asm-x86/processor.h +10 -0
+
+    asm volatile ( "mov %%rsp, %[sp]     \n\t"
+                   "push %[ss]           \n\t"
+                   "push %[sp]           \n\t"
+                   "pushf                \n\t"
+                   "push %[cs]           \n\t"
+                   "lea 1f(%%rip), %[ip] \n\t"
+                   "push %[ip]           \n\t"
+                   "iretq; 1:            \n\t"
+                   : [sp] "=r" (tmp), [ip] "=r" (tmp)

Strictly speaking this needs to be "=&r" in both cases. That'll guarantee the compiler to pick two distinct registers (not sure how that ends up being with the code you have), which is more than we need want. How about having just a single [tmp] output?

With at least the missing & added

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 20 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 15.03.18 at 17:43, <[email protected]> wrote:
xen/include/asm-x86/processor.h +3 -0
+static inline void enable_nmis(void)
+{
+    unsigned long tmp;

xen/include/asm-x86/processor.h DECLARE_TRAP_HANDLER(entry_int82); · line 514

Quoted 10 lines
xen/include/asm-x86/processor.h +10 -0
+
+    asm volatile ( "mov %%rsp, %[sp]     \n\t"
+                   "push %[ss]           \n\t"
+                   "push %[sp]           \n\t"
+                   "pushf                \n\t"
+                   "push %[cs]           \n\t"
+                   "lea 1f(%%rip), %[ip] \n\t"
+                   "push %[ip]           \n\t"
+                   "iretq; 1:            \n\t"
+                   : [sp] "=r" (tmp), [ip] "=r" (tmp)

Strictly speaking this needs to be "=&r" in both cases. That'll guarantee the compiler to pick two distinct registers (not sure how that ends up being with the code you have), which is more than we need want. How about having just a single [tmp] output?

Quoted 2 lines

With at least the missing & added

In this case, everything works fine even if the compiler picks the same register.  GCC 7.3 picks %rax for sp and %rdx for.

Then again, we can get away with a single tmp, so I'll switch to that.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 15.03.18 at 18:07, <[email protected]> wrote:
Quoted 20 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 15.03.18 at 17:43, <[email protected]> wrote:
xen/include/asm-x86/processor.h +3 -0
+static inline void enable_nmis(void)
+{
+    unsigned long tmp;

xen/include/asm-x86/processor.h DECLARE_TRAP_HANDLER(entry_int82); · line 514

Quoted 10 lines
xen/include/asm-x86/processor.h +10 -0
+
+    asm volatile ( "mov %%rsp, %[sp]     \n\t"
+                   "push %[ss]           \n\t"
+                   "push %[sp]           \n\t"
+                   "pushf                \n\t"
+                   "push %[cs]           \n\t"
+                   "lea 1f(%%rip), %[ip] \n\t"
+                   "push %[ip]           \n\t"
+                   "iretq; 1:            \n\t"
+                   : [sp] "=r" (tmp), [ip] "=r" (tmp)

Strictly speaking this needs to be "=&r" in both cases. That'll guarantee the compiler to pick two distinct registers (not sure how that ends up being with the code you have), which is more than we need want. How about having just a single [tmp] output?

Quoted 5 lines
Quoted 2 lines

With at least the missing & added

In this case, everything works fine even if the compiler picks the same register. GCC 7.3 picks %rax for sp and %rdx for.

Well, my remark was meant the other way around - I _wanted_ the same register to be used, and assumed the compiler would (pointlessly) pick separate ones.

Quoted 1 line

Then again, we can get away with a single tmp, so I'll switch to that.

One more remark (without meaning to retract my R-b) - considering the big asm() and almost noting else in the function, I question whether implementing something like this in C is actual a benefit. I don't think the goal should be to reduce .S file size just for the size reduction benefit alone. Otherwise we could move everything into C files, wrapping things into giant asm()-s. I would sort of guess that this was the reason for the function here to have been put in entry.S in the first place.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel