Skip to contentSkip to conversation

x86/idt: Minor improvements to _update_gate_addr_lower()

PATCH In review Andrew Cooper 2 participants 4 messages May 12, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/idt: Minor improvements to _update_gate_addr_lower()

After some experimentation, using .a/.b makes far better logic than using the named fields, as both Clang and GCC spill idte to the stack when named fields are used.

GCC seems to do something very daft for the addr1 field. It takes addr, shifts it by 32, then ANDs with 0xffff0000000000000UL, which requires manifesting a MOVABS.

Clang follows the C, whereby it ANDs with $imm32, then shifts, avoiding the MOVABS entirely.

No functional change.

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

I'm disappointed about how poor the code generation is when assigning to named fields, but I suppose it is a harder problem for the compiler to figure out.

add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-24 (-24)

Function                                     old     new   delta
machine_kexec                                356     348      -8
traps_init                                   434     418     -16
---
 xen/arch/x86/include/asm/idt.h | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
xen/arch/x86/include/asm/idt.h +7 -6
index f613d5693e0e..b5e570a77fae 100644
@@ -92,15 +92,16 @@ static inline void _set_gate_lower(idt_entry_t *gate, unsigned long type,
  * Update the lower half handler of an IDT entry, without changing any other
  * configuration.
  */
-static inline void _update_gate_addr_lower(idt_entry_t *gate, void *addr)
+static inline void _update_gate_addr_lower(idt_entry_t *gate, void *_addr)
 {
+    unsigned long addr = (unsigned long)_addr;
+    unsigned int addr1 = addr & 0xffff0000U; /* GCC force better codegen. */
     idt_entry_t idte;
-    idte.a = gate->a;
 
-    idte.b = ((unsigned long)(addr) >> 32);
-    idte.a &= 0x0000FFFFFFFF0000ULL;
-    idte.a |= (((unsigned long)(addr) & 0xFFFF0000UL) << 32) |
-        ((unsigned long)(addr) & 0xFFFFUL);
+    idte.b = addr >> 32;
+    idte.a = gate->a & 0x0000ffffffff0000UL;
+    idte.a |= (unsigned long)addr1 << 32;
+    idte.a |= addr & 0xffff;
 
     _write_gate_lower(gate, &idte);
 }
Signature · 1 line
2.39.5
#1 lore raw

xen/arch/x86/include/asm/idt.h static inline void _set_gate_lower(idt_entry_t *gate, unsigned long type, · line 95

Quoted 8 lines · Andrew Cooper
xen/arch/x86/include/asm/idt.h +1 -1
@@ -92,15 +92,16 @@ static inline void _set_gate_lower(idt_entry_t *gate, unsigned long type,
  * Update the lower half handler of an IDT entry, without changing any other
  * configuration.
  */
-static inline void _update_gate_addr_lower(idt_entry_t *gate, void *addr)
+static inline void _update_gate_addr_lower(idt_entry_t *gate, void *_addr)

Considering comment and name of the function, ...

xen/arch/x86/include/asm/idt.h static inline void _set_gate_lower(idt_entry_t *gate, unsigned long type, · line 101

Quoted 11 lines
xen/arch/x86/include/asm/idt.h +3 -5
 {
+    unsigned long addr = (unsigned long)_addr;
+    unsigned int addr1 = addr & 0xffff0000U; /* GCC force better codegen. */
     idt_entry_t idte;
-    idte.a = gate->a;
 
-    idte.b = ((unsigned long)(addr) >> 32);
-    idte.a &= 0x0000FFFFFFFF0000ULL;
-    idte.a |= (((unsigned long)(addr) & 0xFFFF0000UL) << 32) |
-        ((unsigned long)(addr) & 0xFFFFUL);
+    idte.b = addr >> 32;

... doesn't this line want dropping altogether? Or at best be an assertion?

Jan

Quoted 6 lines
xen/arch/x86/include/asm/idt.h +3 -0
+    idte.a = gate->a & 0x0000ffffffff0000UL;
+    idte.a |= (unsigned long)addr1 << 32;
+    idte.a |= addr & 0xffff;
 
     _write_gate_lower(gate, &idte);
 }
#2 lore raw
Quoted 17 lines · Jan Beulich

xen/arch/x86/include/asm/idt.h static inline void _set_gate_lower(idt_entry_t *gate, unsigned long type, · line 95

Quoted 8 lines · Andrew Cooper
xen/arch/x86/include/asm/idt.h +1 -1
@@ -92,15 +92,16 @@ static inline void _set_gate_lower(idt_entry_t *gate, unsigned long type,
  * Update the lower half handler of an IDT entry, without changing any other
  * configuration.
  */
-static inline void _update_gate_addr_lower(idt_entry_t *gate, void *addr)
+static inline void _update_gate_addr_lower(idt_entry_t *gate, void *_addr)

Considering comment and name of the function, ...

Quoted 6 lines
xen/arch/x86/include/asm/idt.h +2 -1
 {
+    unsigned long addr = (unsigned long)_addr;
+    unsigned int addr1 = addr & 0xffff0000U; /* GCC force better codegen. */
     idt_entry_t idte;
-    idte.a = gate->a;
 
Quoted 6 lines

xen/arch/x86/include/asm/idt.h static inline void _set_gate_lower(idt_entry_t *gate, unsigned long type, · line 101

Quoted 5 lines
xen/arch/x86/include/asm/idt.h +1 -4
-    idte.b = ((unsigned long)(addr) >> 32);
-    idte.a &= 0x0000FFFFFFFF0000ULL;
-    idte.a |= (((unsigned long)(addr) & 0xFFFF0000UL) << 32) |
-        ((unsigned long)(addr) & 0xFFFFUL);
+    idte.b = addr >> 32;

... doesn't this line want dropping altogether? Or at best be an assertion?

That's what _write_gate_lower() does, hence why
_update_gate_addr_lower() needs to calculate .b.

~Andrew

#3 lore raw
Quoted 23 lines · Andrew Cooper
Quoted 22 lines · Jan Beulich

xen/arch/x86/include/asm/idt.h static inline void _set_gate_lower(idt_entry_t *gate, unsigned long type, · line 95

Quoted 8 lines · Andrew Cooper
xen/arch/x86/include/asm/idt.h +1 -1
@@ -92,15 +92,16 @@ static inline void _set_gate_lower(idt_entry_t *gate, unsigned long type,
  * Update the lower half handler of an IDT entry, without changing any other
  * configuration.
  */
-static inline void _update_gate_addr_lower(idt_entry_t *gate, void *addr)
+static inline void _update_gate_addr_lower(idt_entry_t *gate, void *_addr)

Considering comment and name of the function, ...

Quoted 11 lines
xen/arch/x86/include/asm/idt.h +3 -5
 {
+    unsigned long addr = (unsigned long)_addr;
+    unsigned int addr1 = addr & 0xffff0000U; /* GCC force better codegen. */
     idt_entry_t idte;
-    idte.a = gate->a;
 
-    idte.b = ((unsigned long)(addr) >> 32);
-    idte.a &= 0x0000FFFFFFFF0000ULL;
-    idte.a |= (((unsigned long)(addr) & 0xFFFF0000UL) << 32) |
-        ((unsigned long)(addr) & 0xFFFFUL);
+    idte.b = addr >> 32;
Quoted 4 lines
Quoted 1 line

... doesn't this line want dropping altogether? Or at best be an assertion?

That's what _write_gate_lower() does, hence why
_update_gate_addr_lower() needs to calculate .b.

To satisfy that, idte.b = gate.b would be all we need (i.e. just like _set_gate_lower() does). Or else I think the "lower" would need dropping from comment and name here.

Jan