Skip to contentSkip to conversation

x86: correct asm() constraints when dealing with immediate selector values

PATCH In review Jan Beulich 2 participants 3 messages Sep 13, 2021 – Jun 28, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86: correct asm() constraints when dealing with immediate selector values

asm() constraints need to fit both the intended insn(s) which the respective operands are going to be used with as well as the actual kind of value specified. "m" (alone) together with a constant, however, leads to gcc saying

error: memory input <N> is not directly addressable

while clang complains

error: invalid lvalue in asm input for constraint 'm'

And rightly so - in order to access a memory operand, an address needs to be specified to the insn. In some cases it might be possible for a compiler to synthesize a memory operand holding the requested constant, but I think any solution there would have sharp edges.

If "m" alone doesn't work with constants, it is at best pointless (and perhaps misleading or even risky - the compiler might decide to actually pick "m" and not try very hard to find a suitable register) to specify it alongside "r". And indeed clang does, oddly enough despite its objection to "m" alone. Which means there the change also improves the generated code.

While there also switch the two operand case to using named operands.

---
v2: Use named operands in do_double_fault().

xen/arch/x86/cpu/amd.c +1 -1
@@ -736,7 +736,7 @@ void __init detect_zen2_null_seg_behavio
 	uint64_t base;
 
 	wrmsrl(MSR_FS_BASE, 1);
-	asm volatile ( "mov %0, %%fs" :: "rm" (0) );
+	asm volatile ( "mov %0, %%fs" :: "r" (0) );
 	rdmsrl(MSR_FS_BASE, base);
 
 	if (base == 0)
xen/arch/x86/x86_64/traps.c +2 -1
@@ -248,7 +248,8 @@ void do_double_fault(struct cpu_user_reg
 
     console_force_unlock();
 
-    asm ( "lsll %1, %0" : "=r" (cpu) : "rm" (PER_CPU_SELECTOR) );
+    asm ( "lsll %[sel], %[limit]" : [limit] "=r" (cpu)
+                                  : [sel] "r" (PER_CPU_SELECTOR) );
 
     /* Find information saved during fault and dump it to the console. */
     printk("*** DOUBLE FAULT ***\n");
#1 lore raw

Ping: [PATCH v2] x86: correct asm() constraints when dealing with immediate selector values

Quoted 23 lines · Jan Beulich

asm() constraints need to fit both the intended insn(s) which the respective operands are going to be used with as well as the actual kind of value specified. "m" (alone) together with a constant, however, leads to gcc saying

error: memory input <N> is not directly addressable

while clang complains

error: invalid lvalue in asm input for constraint 'm'

And rightly so - in order to access a memory operand, an address needs to be specified to the insn. In some cases it might be possible for a compiler to synthesize a memory operand holding the requested constant, but I think any solution there would have sharp edges.

If "m" alone doesn't work with constants, it is at best pointless (and perhaps misleading or even risky - the compiler might decide to actually pick "m" and not try very hard to find a suitable register) to specify it alongside "r". And indeed clang does, oddly enough despite its objection to "m" alone. Which means there the change also improves the generated code.

Quoted 5 lines

While there also switch the two operand case to using named operands.

---
v2: Use named operands in do_double_fault().

This has been pending for over 9 months. May I ask for feedback?

Thanks, Jan

Quoted 23 lines
xen/arch/x86/cpu/amd.c +1 -1
@@ -736,7 +736,7 @@ void __init detect_zen2_null_seg_behavio
 	uint64_t base;
 
 	wrmsrl(MSR_FS_BASE, 1);
-	asm volatile ( "mov %0, %%fs" :: "rm" (0) );
+	asm volatile ( "mov %0, %%fs" :: "r" (0) );
 	rdmsrl(MSR_FS_BASE, base);
 
 	if (base == 0)
xen/arch/x86/x86_64/traps.c +2 -1
@@ -248,7 +248,8 @@ void do_double_fault(struct cpu_user_reg
 
     console_force_unlock();
 
-    asm ( "lsll %1, %0" : "=r" (cpu) : "rm" (PER_CPU_SELECTOR) );
+    asm ( "lsll %[sel], %[limit]" : [limit] "=r" (cpu)
+                                  : [sel] "r" (PER_CPU_SELECTOR) );
 
     /* Find information saved during fault and dump it to the console. */
     printk("*** DOUBLE FAULT ***\n");
Acked-by on v2 1/1
#2 lore raw
In reply to Jan Beulich
Quoted 23 lines · Jan Beulich

asm() constraints need to fit both the intended insn(s) which the respective operands are going to be used with as well as the actual kind of value specified. "m" (alone) together with a constant, however, leads to gcc saying

error: memory input <N> is not directly addressable

while clang complains

error: invalid lvalue in asm input for constraint 'm'

And rightly so - in order to access a memory operand, an address needs to be specified to the insn. In some cases it might be possible for a compiler to synthesize a memory operand holding the requested constant, but I think any solution there would have sharp edges.

If "m" alone doesn't work with constants, it is at best pointless (and perhaps misleading or even risky - the compiler might decide to actually pick "m" and not try very hard to find a suitable register) to specify it alongside "r". And indeed clang does, oddly enough despite its objection to "m" alone. Which means there the change also improves the generated code.

Quoted 3 lines

While there also switch the two operand case to using named operands.

Thanks, and sorry for the delay.

Roger.