Skip to contentSkip to conversation

handle multibyte NOPs

PATCH In review Igor Kovalenko 3 participants 9 messages Aug 18, 2006 – Aug 30, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 handle multibyte NOPs

Hi!

This patch adds handling of multi-byte NOPs, recent gcc/gas uses them for x86 code padding.
Patch checked with current opensuse-factory x86 guest installation which is built with new gcc/gas tools.

--
Kind Regards,
Igor V. Kovalenko

Index: target-i386/translate.c
===================================================================
RCS file: /cvsroot/qemu/qemu/target-i386/translate.c,v retrieving revision 1.59

translate.c +43 -0
diff -u -r1.59 translate.c
@@ -5795,6 +5795,49 @@
             goto illegal_op;
         }
         break;
+    case 0x11f:
+        /* multi-byte noop */
+
+        modrm = ldub_code(s->pc++);
+        rm  = modrm & 7;
+        mod = (modrm >> 6) & 3;
+        reg = (modrm >> 3) & 7;
+
+        if (reg != 0)
+        {
+            goto illegal_op;
+        }
+
+        if (rm == 0x04)
+        {
+            /* SIB byte follows */
+            s->pc += 1;
+        }
+
+        switch (mod)
+        {
+            case 0x00:
+                if (rm == 0x05)
+                {
+                    /* 32bit data follows */
+                    s->pc += 4;
+                }
+                /* else register is specified */
+                break;
+            case 0x01:
+                /* 8bit data follows */
+                s->pc += 1;
+                break;
+            case 0x10:
+                /* 32bit data follows */
+                s->pc += 4;
+                break;
+            case 0x11:
+            default:
+                /* register is specified */
+                break;
+        }
+        break;
     case 0x120: /* mov reg, crN */
     case 0x122: /* mov crN, reg */
         if (s->cpl != 0) {
#1 lore raw
Quoted 6 lines · Igor Kovalenko

Hi!

This patch adds handling of multi-byte NOPs, recent gcc/gas uses them for x86 code padding.
Patch checked with current opensuse-factory x86 guest installation which is built with new gcc/gas tools.

According to my amd64 documentation all of the opcodes 0f 19 through 0f 1f should be implemented as a NOP. Also, I suspect the "invalid" prefetch ops (0f 18) should also be implemented as nops.
The same implementation should be useable to all these opcodes.

Paul

#2 lore raw
Quoted 8 lines · Paul Brook
Quoted 3 lines · Igor Kovalenko

Hi!

This patch adds handling of multi-byte NOPs, recent gcc/gas uses them

for

Quoted 2 lines

x86 code padding.
Patch checked with current opensuse-factory x86 guest installation which

Quoted 9 lines

is

Quoted 1 line

built with new gcc/gas tools.

According to my amd64 documentation all of the opcodes 0f 19 through 0f 1f should be implemented as a NOP. Also, I suspect the "invalid" prefetch ops (0f 18) should also be implemented as nops.
The same implementation should be useable to all these opcodes.

Updated patch, extracted noop modrm processing into separate function and used it for 0f 18 case as well. I also removed reg sense from noop processing.

--
Kind Regards,
Igor V. Kovalenko

Index: target-i386/translate.c
===================================================================
RCS file: /cvsroot/qemu/qemu/target-i386/translate.c,v retrieving revision 1.59

translate.c +44 -1
diff -u -r1.59 translate.c
@@ -1615,6 +1615,43 @@
     *offset_ptr = disp;
 }
 
+static void gen_nop_modrm(DisasContext *s, int modrm)
+{
+    int rm, mod;
+    rm  = modrm & 7;
+    mod = (modrm >> 6) & 3;
+
+    if (rm == 0x04)
+    {
+        /* SIB byte follows */
+        s->pc += 1;
+    }
+
+    switch (mod)
+    {
+        case 0x00:
+            if (rm == 0x05)
+            {
+                /* 32bit data follows */
+                s->pc += 4;
+            }
+            /* else register is specified */
+            break;
+        case 0x01:
+            /* 8bit data follows */
+            s->pc += 1;
+            break;
+        case 0x10:
+            /* 32bit data follows */
+            s->pc += 4;
+            break;
+        case 0x11:
+        default:
+            /* register is specified */
+            break;
+    }
+}
+
 /* used for LEA and MOV AX, mem */
 static void gen_add_A0_ds_seg(DisasContext *s)
 {
@@ -5792,9 +5829,15 @@
             /* nothing more to do */
             break;
         default:
-            goto illegal_op;
+            gen_nop_modrm(s, modrm);
+            break;
         }
         break;
+    case 0x119 ... 0x11f:
+        /* multi-byte noop */
+        modrm = ldub_code(s->pc++);
+        gen_nop_modrm(s, modrm);
+        break;
     case 0x120: /* mov reg, crN */
     case 0x122: /* mov crN, reg */
         if (s->cpl != 0) {
#3 lore raw
Quoted 18 lines · Igor Kovalenko
Quoted 8 lines · Paul Brook
Quoted 3 lines · Igor Kovalenko

Hi!

This patch adds handling of multi-byte NOPs, recent gcc/gas uses them

for

Quoted 2 lines

x86 code padding.
Patch checked with current opensuse-factory x86 guest installation which

Quoted 9 lines

is

Quoted 1 line

built with new gcc/gas tools.

According to my amd64 documentation all of the opcodes 0f 19 through 0f 1f should be implemented as a NOP. Also, I suspect the "invalid" prefetch ops (0f 18) should also be implemented as nops.
The same implementation should be useable to all these opcodes.

Quoted 3 lines

Updated patch, extracted noop modrm processing into separate function and used it for 0f 18 case as well. I also removed reg sense from noop processing.

Case labels in your `switch (mod)' statement are incorrect. I guess you wanted 0x to behave like 0b, while it works for 0 and 1, for everything else it's way of.

--
mailto:[email protected]

#4 lore raw
Quoted 24 lines · malc
Quoted 9 lines · Igor Kovalenko
Quoted 8 lines · Paul Brook
Quoted 3 lines · Igor Kovalenko

Hi!

This patch adds handling of multi-byte NOPs, recent gcc/gas uses them

for

Quoted 2 lines

x86 code padding.
Patch checked with current opensuse-factory x86 guest installation

which

Quoted 4 lines
Quoted 4 lines

is

Quoted 1 line

built with new gcc/gas tools.

According to my amd64 documentation all of the opcodes 0f 19 through 0f

1f

Quoted 1 line
Quoted 1 line

should be implemented as a NOP. Also, I suspect the "invalid" prefetch

ops

Quoted 4 lines
Quoted 4 lines

(0f 18) should also be implemented as nops.
The same implementation should be useable to all these opcodes.

Quoted 9 lines
Quoted 1 line

Updated patch, extracted noop modrm processing into separate function

and

Quoted 2 lines

used it for 0f 18 case as well. I also removed reg sense from noop processing.

Case labels in your `switch (mod)' statement are incorrect. I guess you wanted 0x to behave like 0b, while it works for 0 and 1, for everything else it's way of.

Right. Here is the real one with correct case labels.

Signature · 2 lines
Kind Regards,
Igor V. Kovalenko

Index: target-i386/translate.c
===================================================================
RCS file: /cvsroot/qemu/qemu/target-i386/translate.c,v retrieving revision 1.59

translate.c +44 -1
diff -u -r1.59 translate.c
@@ -1615,6 +1615,43 @@
     *offset_ptr = disp;
 }
 
+static void gen_nop_modrm(DisasContext *s, int modrm)
+{
+    int rm, mod;
+    rm  = modrm & 7;
+    mod = (modrm >> 6) & 3;
+
+    if (rm == 0x04)
+    {
+        /* SIB byte follows */
+        s->pc += 1;
+    }
+
+    switch (mod)
+    {
+        case 0:
+            if (rm == 0x05)
+            {
+                /* 32bit data follows */
+                s->pc += 4;
+            }
+            /* else register is specified */
+            break;
+        case 1:
+            /* 8bit data follows */
+            s->pc += 1;
+            break;
+        case 2:
+            /* 32bit data follows */
+            s->pc += 4;
+            break;
+        case 3:
+        default:
+            /* register is specified */
+            break;
+    }
+}
+
 /* used for LEA and MOV AX, mem */
 static void gen_add_A0_ds_seg(DisasContext *s)
 {
@@ -5792,9 +5829,15 @@
             /* nothing more to do */
             break;
         default:
-            goto illegal_op;
+            gen_nop_modrm(s, modrm);
+            break;
         }
         break;
+    case 0x119 ... 0x11f:
+        /* multi-byte noop */
+        modrm = ldub_code(s->pc++);
+        gen_nop_modrm(s, modrm);
+        break;
     case 0x120: /* mov reg, crN */
     case 0x122: /* mov crN, reg */
         if (s->cpl != 0) {
#5 lore raw

On Mon, 21 Aug 2006, Igor Kovalenko wrote:

<snip>

Quoted 1 line

Right. Here is the real one with correct case labels.

Sorry, forgot to mention that atop of the hex/bin problem the code also suffers from lack of proper 16/64 bit modrm decoding.

--
mailto:[email protected]

#6 lore raw
Quoted 9 lines · malc

On Mon, 21 Aug 2006, Igor Kovalenko wrote:

<snip>

Quoted 1 line

Right. Here is the real one with correct case labels.

Sorry, forgot to mention that atop of the hex/bin problem the code also suffers from lack of proper 16/64 bit modrm decoding.

seems to me that 16 and 64 bit cases does not apply here

Signature · 2 lines
Kind Regards,
Igor V. Kovalenko
#7 lore raw
Quoted 6 lines · Igor Kovalenko
Quoted 5 lines · malc

On Mon, 21 Aug 2006, Igor Kovalenko wrote:

<snip>

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Right. Here is the real one with correct case labels.

Sorry, forgot to mention that atop of the hex/bin problem the code also suffers from lack of proper 16/64 bit modrm decoding.

seems to me that 16 and 64 bit cases does not apply here

Well, quick experiment in vm86 environment suggests that i'm right at least for 16bit case.

<moo.asm>

        xor ax, ax
        int 16h
        db 0xf, 0x19, 5, 0xde, 0xad
        db 0xf, 0x19, 5, 0xbe, 0xef
        mov ah, 9
        mov dx, erm
        add dx, 0x100
        int 21h
        xor ax, ax
        int 16h
        ret

erm db "moo$"
</moo.asm>

nasm -o moo.com moo.asm
dosemu moo.com

64bit case should be investigated by someone who possesses 64bit OS.

--
mailto:[email protected]

#8 lore raw
Quoted 32 lines · malc
Quoted 6 lines · Igor Kovalenko
Quoted 5 lines · malc

On Mon, 21 Aug 2006, Igor Kovalenko wrote:

<snip>

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Right. Here is the real one with correct case labels.

Sorry, forgot to mention that atop of the hex/bin problem the code also suffers from lack of proper 16/64 bit modrm decoding.

seems to me that 16 and 64 bit cases does not apply here

Well, quick experiment in vm86 environment suggests that i'm right at least for 16bit case.

<moo.asm>

         xor ax, ax
         int 16h
         db 0xf, 0x19, 5, 0xde, 0xad
         db 0xf, 0x19, 5, 0xbe, 0xef
         mov ah, 9
         mov dx, erm
         add dx, 0x100
         int 21h
         xor ax, ax
         int 16h
         ret
Quoted 8 lines

erm db "moo$"
</moo.asm>

nasm -o moo.com moo.asm
dosemu moo.com

64bit case should be investigated by someone who possesses 64bit OS.

Here is the updated patch, should implement 16/32/64 modes according to public intel docs.
Operand size is taken from DisassContext->dflag which is set beforehand; I assume it is decoded correctly wrt appropriate instruction prefixes.

Signature · 2 lines
Kind Regards,
Igor V. Kovalenko

Index: target-i386/translate.c
===================================================================
RCS file: /cvsroot/qemu/qemu/target-i386/translate.c,v retrieving revision 1.59

translate.c +64 -1
diff -u -r1.59 translate.c
@@ -1615,6 +1615,63 @@
     *offset_ptr = disp;
 }
 
+static void gen_nop_modrm(DisasContext *s, int modrm)
+{
+    int rm, mod;
+    rm  = modrm & 7;
+    mod = (modrm >> 6) & 3;
+
+    switch (s->dflag)
+    {
+        case 0:
+            /* 16 bit */
+            if (mod == 0 && rm == 6)
+            {
+                /* 16 bit data follows */
+                s->pc += 2;
+            }
+            else if (mod < 3)
+            {
+                /* 0, 8 or 16 bit data follows */
+                s->pc += mod;
+            }
+            break;
+
+        case 2:
+            /* 64 bit, modr/m size does not change, use 32 bit case */
+        case 1:
+            /*32 bit*/
+            if (mod == 0 && rm == 5)
+            {
+               /* 32 bit data follows */
+               s->pc += 4;
+            }
+            else
+            {
+                if (mod < 3 && rm == 4)
+                {
+                    /* SIB byte follows */
+                    s->pc += 1;
+                }
+
+                if (mod == 1)
+                {
+                    /* 8 bit data follows */
+                    s->pc += 1;
+                }
+                else if (mod == 1)
+                {
+                    /* 32 bit data follows */
+                    s->pc += 4;
+                }
+            }
+            break;
+        default:
+            /* undefined */
+            break;
+    }
+}
+
 /* used for LEA and MOV AX, mem */
 static void gen_add_A0_ds_seg(DisasContext *s)
 {
@@ -5792,9 +5849,15 @@
             /* nothing more to do */
             break;
         default:
-            goto illegal_op;
+            gen_nop_modrm(s, modrm);
+            break;
         }
         break;
+    case 0x119 ... 0x11f:
+        /* multi-byte noop */
+        modrm = ldub_code(s->pc++);
+        gen_nop_modrm(s, modrm);
+        break;
     case 0x120: /* mov reg, crN */
     case 0x122: /* mov crN, reg */
         if (s->cpl != 0) {