Skip to contentSkip to conversation

x86: fix rdrand asm()

PATCH Jan Beulich 3 participants 4 messages Sep 25, 2013

lore
Share & tools
lore
#0 lore raw

Just learned the hard way that at least for non-volatile asm()s gcc indeed does what the documentation says: It may move it across jumps (i.e. ahead of the cpu_has() check). While the documentation claims that this can also happen for volatile asm()s, if that was the case we'd have many more problems in our code (and e,g, Linux would too).

xen/include/asm-x86/random.h +1 -1
@@ -8,7 +8,7 @@ static inline unsigned int arch_get_rand
     unsigned int val = 0;
 
     if ( cpu_has(&current_cpu_data, X86_FEATURE_RDRAND) )
-        asm ( ".byte 0x0f,0xc7,0xf0" : "+a" (val) );
+        __asm__ __volatile__ ( ".byte 0x0f,0xc7,0xf0" : "+a" (val) );
 
     return val;
 }

x86: fix rdrand asm()

Just learned the hard way that at least for non-volatile asm()s gcc indeed does what the documentation says: It may move it across jumps (i.e. ahead of the cpu_has() check). While the documentation claims that this can also happen for volatile asm()s, if that was the case we'd have many more problems in our code (and e,g, Linux would too).

xen/include/asm-x86/random.h +1 -1
@@ -8,7 +8,7 @@ static inline unsigned int arch_get_rand
     unsigned int val = 0;
 
     if ( cpu_has(&current_cpu_data, X86_FEATURE_RDRAND) )
-        asm ( ".byte 0x0f,0xc7,0xf0" : "+a" (val) );
+        __asm__ __volatile__ ( ".byte 0x0f,0xc7,0xf0" : "+a" (val) );
 
     return val;
 }

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Reviewed-by
#1 lore raw
Quoted 11 lines · Jan Beulich

Just learned the hard way that at least for non-volatile asm()s gcc indeed does what the documentation says: It may move it across jumps (i.e. ahead of the cpu_has() check). While the documentation claims that this can also happen for volatile asm()s, if that was the case we'd have many more problems in our code (and e,g, Linux would too).

xen/include/asm-x86/random.h static inline unsigned int arch_get_rand · line 11

Quoted 5 lines
xen/include/asm-x86/random.h +1 -1
@@ -8,7 +8,7 @@ static inline unsigned int arch_get_rand
     unsigned int val = 0;
 
     if ( cpu_has(&current_cpu_data, X86_FEATURE_RDRAND) )
-        asm ( ".byte 0x0f,0xc7,0xf0" : "+a" (val) );
+        __asm__ __volatile__ ( ".byte 0x0f,0xc7,0xf0" : "+a" (val) );

Any reason for using the double underscore versions? They have identical meanings, and the prevailing style does appear to be without.

Either way,

Quoted 8 lines
xen/include/asm-x86/random.h +0 -0
     return val;
 }
 
Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#2 lore raw
Quoted 15 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 13 lines · Jan Beulich

Just learned the hard way that at least for non-volatile asm()s gcc indeed does what the documentation says: It may move it across jumps (i.e. ahead of the cpu_has() check). While the documentation claims that this can also happen for volatile asm()s, if that was the case we'd have many more problems in our code (and e,g, Linux would too).

xen/include/asm-x86/random.h +0 -0
@@ -8,7 +8,7 @@ static inline unsigned int arch_get_rand
     unsigned int val = 0;
 
Quoted 6 lines

xen/include/asm-x86/random.h static inline unsigned int arch_get_rand · line 11

Quoted 3 lines
xen/include/asm-x86/random.h +1 -1
     if ( cpu_has(&current_cpu_data, X86_FEATURE_RDRAND) )
-        asm ( ".byte 0x0f,0xc7,0xf0" : "+a" (val) );
+        __asm__ __volatile__ ( ".byte 0x0f,0xc7,0xf0" : "+a" (val) );

Any reason for using the double underscore versions? They have identical meanings, and the prevailing style does appear to be without.

In header I prefer to use the name space safe variant with the underscores, while in .c files I'd use the ones without. From a strict POV that ought to be necessary for public headers only (where we don't have any asm()s anyway), but I'd like to do it this way as long as we're not really consistent with this throughout the tree.

Jan

Acked-by
#3 lore raw
In reply to Jan Beulich
Quoted 11 lines · Jan Beulich

Just learned the hard way that at least for non-volatile asm()s gcc indeed does what the documentation says: It may move it across jumps (i.e. ahead of the cpu_has() check). While the documentation claims that this can also happen for volatile asm()s, if that was the case we'd have many more problems in our code (and e,g, Linux would too).

xen/include/asm-x86/random.h static inline unsigned int arch_get_rand · line 11

Quoted 5 lines
xen/include/asm-x86/random.h +1 -1
@@ -8,7 +8,7 @@ static inline unsigned int arch_get_rand
     unsigned int val = 0;
 
     if ( cpu_has(&current_cpu_data, X86_FEATURE_RDRAND) )
-        asm ( ".byte 0x0f,0xc7,0xf0" : "+a" (val) );
+        __asm__ __volatile__ ( ".byte 0x0f,0xc7,0xf0" : "+a" (val) );

Although not consistently applied, we use 'asm volatile' rather than '__asm__ __volatile__' generally. So we should here. Apart from that

Quoted 5 lines
xen/include/asm-x86/random.h +0 -0
     return val;
 }