Skip to contentSkip to conversation

x86/apic: Fix asm() constraints in TMICT calculation

PATCH In review Andrew Cooper 2 participants 4 messages Apr 20 – Apr 21

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/apic: Fix asm() constraints in TMICT calculation

The encoded MUL is 64 bits, so writes %rdx too. At a minimum, this needs expressing as a clobber.

Also fix a logical disconnect between 'overflow' being the carry flag not the overflow flag. CF and OF are always the same for MUL instructions, so use the flag which matches the variable name.

Fixes d5c70a51bfbe ("x86/APIC: handle overflow in TMICT calculation")

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

Only affects 4.20 (and earlier) where __builtin_umull_overflow() can't be used.

I've kept this form because it produces best code generation for GCCs which support flag outputs.

An alternative would be to capture product_hi and check the nonzero-ness, as that's how OF/CF are produced in hardware, which would be better code generation on very old GCCs.

---
 xen/arch/x86/apic.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
xen/arch/x86/apic.c +4 -3
index daf597ed44b7..764ee1e98f77 100644
@@ -1317,9 +1317,10 @@ int reprogram_timer(s_time_t timeout)
 
         apic_tmict = UINT32_MAX;
         asm ( "mul %[expire]\n\t"
-              ASM_FLAG_OUT(, "setc %[cf]")
-              : "=a" (product), [cf] ASM_FLAG_OUT("=@ccc", "=qm") (overflow)
-              : "0" ((unsigned long)bus_scale), [expire] "r" (expire) );
+              ASM_FLAG_OUT(, "seto %[of]")
+              : "=a" (product), [of] ASM_FLAG_OUT("=@cco", "=qm") (overflow)
+              : "0" ((unsigned long)bus_scale), [expire] "r" (expire)
+              : "rdx" );
         if ( !overflow &&
              (product >>= BUS_SCALE_SHIFT) < apic_tmict )
             apic_tmict = product;

base-commit: 5f7054258c6937b74aee411f16db5eb54ce9fda1

Signature · 1 line
2.39.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 2 lines · Andrew Cooper

The encoded MUL is 64 bits, so writes %rdx too. At a minimum, this needs expressing as a clobber.

I'm embarrassed of missing this.

Quoted 3 lines

Also fix a logical disconnect between 'overflow' being the carry flag not the overflow flag. CF and OF are always the same for MUL instructions, so use the flag which matches the variable name.

I don't mind this too much, but the use of CF was deliberate: Imo OF is relevant to signed arithmetic only, whereas CF is the flag to use with unsigned operations.

Quoted 2 lines
Fixes d5c70a51bfbe ("x86/APIC: handle overflow in TMICT calculation")
Quoted 6 lines

I've kept this form because it produces best code generation for GCCs which support flag outputs.

An alternative would be to capture product_hi and check the nonzero-ness, as that's how OF/CF are produced in hardware, which would be better code generation on very old GCCs.

We could fit both, by further widening the use of ASM_FLAG_OUT().

xen/arch/x86/apic.c int reprogram_timer(s_time_t timeout) · line 1321

Quoted 11 lines
xen/arch/x86/apic.c +2 -3
@@ -1317,9 +1317,10 @@ int reprogram_timer(s_time_t timeout)
 
         apic_tmict = UINT32_MAX;
         asm ( "mul %[expire]\n\t"
-              ASM_FLAG_OUT(, "setc %[cf]")
-              : "=a" (product), [cf] ASM_FLAG_OUT("=@ccc", "=qm") (overflow)
-              : "0" ((unsigned long)bus_scale), [expire] "r" (expire) );
+              ASM_FLAG_OUT(, "seto %[of]")
+              : "=a" (product), [of] ASM_FLAG_OUT("=@cco", "=qm") (overflow)

Noticing only now - for the non-flag-output case this should be "=&a". With

xen/arch/x86/apic.c int reprogram_timer(s_time_t timeout) · line 1322

Quoted 1 line
xen/arch/x86/apic.c +1 -0
+              : "0" ((unsigned long)bus_scale), [expire] "r" (expire)

... %rax also being an input, there's no risk of the compiler using the register for the other input, but still. Would you mind making that adjustment as well, while at it?

Jan

#2 lore raw
Quoted 8 lines · Jan Beulich
Quoted 2 lines · Andrew Cooper

The encoded MUL is 64 bits, so writes %rdx too. At a minimum, this needs expressing as a clobber.

I'm embarrassed of missing this.

Quoted 3 lines

Also fix a logical disconnect between 'overflow' being the carry flag not the overflow flag. CF and OF are always the same for MUL instructions, so use the flag which matches the variable name.

Quoted 7 lines

I don't mind this too much, but the use of CF was deliberate: Imo OF is relevant to signed arithmetic only, whereas CF is the flag to use with unsigned operations.

Quoted 2 lines
Fixes d5c70a51bfbe ("x86/APIC: handle overflow in TMICT calculation")

Thanks.  I could rename the variable to carry instead then?  Either works.

Quoted 22 lines
Quoted 7 lines

I've kept this form because it produces best code generation for GCCs which support flag outputs.

An alternative would be to capture product_hi and check the nonzero-ness, as that's how OF/CF are produced in hardware, which would be better code generation on very old GCCs.

We could fit both, by further widening the use of ASM_FLAG_OUT().

xen/arch/x86/apic.c int reprogram_timer(s_time_t timeout) · line 1321

Quoted 11 lines
xen/arch/x86/apic.c +2 -3
@@ -1317,9 +1317,10 @@ int reprogram_timer(s_time_t timeout)
 
         apic_tmict = UINT32_MAX;
         asm ( "mul %[expire]\n\t"
-              ASM_FLAG_OUT(, "setc %[cf]")
-              : "=a" (product), [cf] ASM_FLAG_OUT("=@ccc", "=qm") (overflow)
-              : "0" ((unsigned long)bus_scale), [expire] "r" (expire) );
+              ASM_FLAG_OUT(, "seto %[of]")
+              : "=a" (product), [of] ASM_FLAG_OUT("=@cco", "=qm") (overflow)

Noticing only now - for the non-flag-output case this should be "=&a". With

Quoted 4 lines

xen/arch/x86/apic.c int reprogram_timer(s_time_t timeout) · line 1322

Quoted 1 line
xen/arch/x86/apic.c +1 -0
+              : "0" ((unsigned long)bus_scale), [expire] "r" (expire)

... %rax also being an input, there's no risk of the compiler using the register for the other input, but still. Would you mind making that adjustment as well, while at it?

Ok.

~Andrew

#3 lore raw
Quoted 13 lines · Andrew Cooper
Quoted 12 lines · Jan Beulich
Quoted 2 lines · Andrew Cooper

The encoded MUL is 64 bits, so writes %rdx too. At a minimum, this needs expressing as a clobber.

I'm embarrassed of missing this.

Quoted 3 lines

Also fix a logical disconnect between 'overflow' being the carry flag not the overflow flag. CF and OF are always the same for MUL instructions, so use the flag which matches the variable name.

I don't mind this too much, but the use of CF was deliberate: Imo OF is relevant to signed arithmetic only, whereas CF is the flag to use with unsigned operations.

Quoted 5 lines
Quoted 3 lines
Quoted 2 lines
Fixes d5c70a51bfbe ("x86/APIC: handle overflow in TMICT calculation")

Thanks.  I could rename the variable to carry instead then?  Either works.

I'd slightly prefer that alternative, yes.

Thanks, Jan