Skip to contentSkip to conversation

x86 emulation: fix bswap

PATCH In review Jan Beulich 5 participants 7 messages Feb 15, 2007 – Feb 16, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86 emulation: fix bswap

REX.R needs to be decoded and since bswap on a 16-bit operand is undefined, it is best to have hardware execute this so the emulation result matches hardware behavior. Since it is simple to do, faster, and smaller, also let hardware do 32- and 64-bit ones.

xen/arch/x86/x86_emulate.c +7 -17
Index: 2007-02-07/xen/arch/x86/x86_emulate.c
===================================================================
@@ -2326,32 +2326,22 @@ x86_emulate(
 
     case 0xc8 ... 0xcf: /* bswap */
         dst.type  = OP_REG;
-        dst.reg   = decode_register(b & 7, &_regs, 0);
+        dst.reg   = decode_register(
+            (b & 7) | ((rex_prefix & 1) << 3), &_regs, 0);
         dst.val = *dst.reg;
         switch ( dst.bytes = op_bytes )
         {
         case 2:
-            dst.val = (((dst.val & 0x00FFUL) << 8) |
-                       ((dst.val & 0xFF00UL) >> 8));
+            __asm__("data16 bswap %k0" : "+r" (dst.val));
             break;
         case 4:
-            dst.val = (((dst.val & 0x000000FFUL) << 24) |
-                       ((dst.val & 0x0000FF00UL) <<  8) |
-                       ((dst.val & 0x00FF0000UL) >>  8) |
-                       ((dst.val & 0xFF000000UL) >> 24));
-            break;
 #ifdef __x86_64__
-        case 8:
-            dst.val = (((dst.val & 0x00000000000000FFUL) << 56) |
-                       ((dst.val & 0x000000000000FF00UL) << 40) |
-                       ((dst.val & 0x0000000000FF0000UL) << 24) |
-                       ((dst.val & 0x00000000FF000000UL) <<  8) |
-                       ((dst.val & 0x000000FF00000000UL) >>  8) |
-                       ((dst.val & 0x0000FF0000000000UL) >> 24) |
-                       ((dst.val & 0x00FF000000000000UL) >> 40) |
-                       ((dst.val & 0xFF00000000000000UL) >> 56));
+            __asm__("bswap %k0" : "+r" (dst.val));
             break;
+        case 8:
 #endif
+            __asm__("bswap %0" : "+r" (dst.val));
+            break;
         }
         break;
     }
#1 lore raw
Quoted 4 lines · Jan Beulich

REX.R needs to be decoded and since bswap on a 16-bit operand is undefined, it is best to have hardware execute this so the emulation result matches hardware behavior. Since it is simple to do, faster, and smaller, also let hardware do 32- and 64-bit ones.

Can 'undefined' behaviour include e.g. #UD?

-- Keir

#2 lore raw
Quoted 9 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 15.02.07 11:33 >>>

Quoted 4 lines · Jan Beulich

REX.R needs to be decoded and since bswap on a 16-bit operand is undefined, it is best to have hardware execute this so the emulation result matches hardware behavior. Since it is simple to do, faster, and smaller, also let hardware do 32- and 64-bit ones.

Can 'undefined' behaviour include e.g. #UD?

I don't think so.

Jan

#3 lore raw
In reply to Keir Fraser
Quoted 13 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Keir Fraser
Sent: 15 February 2007 10:33
To: Jan Beulich; [email protected]
Subject: Re: [Xen-devel] [PATCH] x86 emulation: fix bswap

Quoted 1 line · Jan Beulich

REX.R needs to be decoded and since bswap on a 16-bit

Quoted 8 lines

operand is undefined,

Quoted 1 line

it is best to have hardware execute this so the emulation

result matches

Quoted 1 line

hardware behavior. Since it is simple to do, faster, and

smaller, also let

Quoted 1 line

hardware do 32- and 64-bit ones.

Can 'undefined' behaviour include e.g. #UD?

BSWAP isn't allowed to #UD ("Exceptions: None") according to Vol 3 of AMD64 Programmers Manual. A 16-bit prefix to that instruction is just not defined - it will do SOMETHING, but it won't crash the system, just "randomly store data in your target register" [I haven't tried it, and it may well vary depending on model of processor as well as of course differences between different companies processors]. I would expect the usage of 66 0F C8 <reg> to be extremely unusual - there is the "correct" way to do it with XCHG AL,AH and related instructions.

If anything is going to trap, it will be on the next instruction, when the data is garbled.

I just wrote a little bit of code to test it:
int x = 0x12345678;
__asm__(".byte 0x66; bswap %0": "=r"(x): "0"(x))
printf("x=%x", x);

Prints 12340000, so the data is "zerod". (and it looks like the code generated by gcc is correct!).

--
Mats

Quoted 12 lines

-- Keir

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

#4 lore raw
Quoted 7 lines · Mats

I just wrote a little bit of code to test it:
int x = 0x12345678;
__asm__(".byte 0x66; bswap %0": "=r"(x): "0"(x))
printf("x=%x", x);

Prints 12340000, so the data is "zerod". (and it looks like the code generated by gcc is correct!).

Same behaviour as on an Intel CPU. I am quite inclined to do that directly in the emulator, with a comment explaining why, rather than do an undefined operation. That just seems unnecessarily scary. Direct 32- and 64-bit BSWAP is okay though and does reduce the code size.

-- Keir

#5 lore raw
Quoted 23 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Keir Fraser
Sent: 15 February 2007 11:54
To: Petersson, Mats; Jan Beulich; [email protected]
Subject: Re: [Xen-devel] [PATCH] x86 emulation: fix bswap

Quoted 7 lines · Mats

I just wrote a little bit of code to test it:
int x = 0x12345678;
__asm__(".byte 0x66; bswap %0": "=r"(x): "0"(x))
printf("x=%x", x);

Prints 12340000, so the data is "zerod". (and it looks like the code generated by gcc is correct!).

Quoted 7 lines

Same behaviour as on an Intel CPU. I am quite inclined to do that directly
in the emulator, with a comment explaining why, rather than do an undefined
operation. That just seems unnecessarily scary. Direct 32- and 64-bit BSWAP
is okay though and does reduce the code size.

Whilst this is following the (sampled) architecture, wouldn't it be just as easy to just IGNORE that case?

It's not going to occur unless:
1. Someone likes to play games with code (it's easier to do "mov $0, %ax" or "xor %ax,%ax" if that's what you actually want to do). And it's not impossible that for example a 486, 386 or the latest quad-core [or processors of other manufacturers (via, national->amd->discontinued)] does something completely different on the same instruction, so it's not a reliable way to clear that register.
2. Some code has gone astray and is executing random data - well, that's going to crash pretty soon anyways!
3. Bugs in compiler/assembler.

[Those are listed in order of importance/likelyhood to be needed, in my personal opinion].

--
Mats

Quoted 11 lines

-- Keir

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

#6 lore raw

Error with vfb Xen 3.0.4

In reply to Petersson, Mats

We get this while booting our DOM-0.
Somehow /dev/fb is not configured.

Quoted 3 lines
Quoted 3 lines

Vfb "Trying to show message with id 'ERROR_Video_Default' Vfb Using image /home/user/prism/errors/video/default.png' Vfb Execing

Is this a vfb problem or we have missed something?

-Kaushik