Skip to contentSkip to conversation

Emulate instructions in vm86 mode

RFC PATCH In review Bastian Blank 4 participants 18 messages Sep 7, 2006 – Nov 2, 2006

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 Emulate instructions in vm86 mode

Hi folks

This patch fixes the emulation of instructions in vm86 mode. It fetches them using cs and eip instead of only eip. This makes it at least possible to use the i945GM vesa bios from the running system.

I'm not sure if this is the correct fix or if we should call the gpf handler of the running system. At least the support in linux reports traps back to the userspace caller which is not possible if emulated in xen.

Bastian

Signature · 2 lines
Not one hundred percent efficient, of course ... but nothing ever is.
		-- Kirk, "Metamorphosis", stardate 3219.8

#! /bin/sh /usr/share/dpatch/dpatch-run
## vm86-support.dpatch by Bastian Blank <[email protected]> ##
## All lines beginning with `## DP:' are a description of the patch. ## DP: No description.

@DPATCH@

xen/arch/x86/traps.c +15 -13
diff -urNad xen-unstable~/xen/arch/x86/traps.c xen-unstable/xen/arch/x86/traps.c
@@ -1015,9 +1015,11 @@
     (admin_io_okay(_p, 4, _d, _r) ? outl(_v, _p) : ((void)0))
 
 /* Instruction fetch with error handling. */
-#define insn_fetch(_type, _size, _ptr)                                      \
-({  unsigned long _rc, _x;                                                  \
-    if ( (_rc = copy_from_user(&_x, (_type *)eip, sizeof(_type))) != 0 )    \
+#define insn_fetch(_type, _size, cs, eip)                                   \
+({  unsigned long _rc, _x, _ptr = eip;                                      \
+    if ( vm86_mode(regs) )                                                  \
+        _ptr += cs << 4;                                                    \
+    if ( (_rc = copy_from_user(&_x, (_type *)_ptr, sizeof(_type))) != 0 )   \
     {                                                                       \
         propagate_page_fault(eip + sizeof(_type) - _rc, 0);                 \
         return EXCRET_fault_fixed;                                          \
@@ -1027,7 +1029,7 @@
 static int emulate_privileged_op(struct cpu_user_regs *regs)
 {
     struct vcpu *v = current;
-    unsigned long *reg, eip = regs->eip, res;
+    unsigned long *reg, eip = regs->eip, cs = regs->cs, res;
     u8 opcode, modrm_reg = 0, modrm_rm = 0, rep_prefix = 0;
     unsigned int port, i, op_bytes = 4, data, rc;
     u32 l, h;
@@ -1035,7 +1037,7 @@
     /* Legacy prefixes. */
     for ( i = 0; i < 8; i++ )
     {
-        switch ( opcode = insn_fetch(u8, 1, eip) )
+        switch ( opcode = insn_fetch(u8, 1, cs, eip) )
         {
         case 0x66: /* operand-size override */
             op_bytes ^= 6; /* switch between 2/4 bytes */
@@ -1067,7 +1069,7 @@
         modrm_rm  = (opcode & 1) << 3;  /* REX.B */
 
         /* REX.W and REX.X do not need to be decoded. */
-        opcode = insn_fetch(u8, 1, eip);
+        opcode = insn_fetch(u8, 1, cs, eip);
     }
 #endif
     
@@ -1149,7 +1151,7 @@
     case 0xe4: /* IN imm8,%al */
         op_bytes = 1;
     case 0xe5: /* IN imm8,%eax */
-        port = insn_fetch(u8, 1, eip);
+        port = insn_fetch(u8, 1, cs, eip);
     exec_in:
         if ( !guest_io_okay(port, op_bytes, v, regs) )
             goto fail;
@@ -1178,7 +1180,7 @@
     case 0xe6: /* OUT %al,imm8 */
         op_bytes = 1;
     case 0xe7: /* OUT %eax,imm8 */
-        port = insn_fetch(u8, 1, eip);
+        port = insn_fetch(u8, 1, cs, eip);
     exec_out:
         if ( !guest_io_okay(port, op_bytes, v, regs) )
             goto fail;
@@ -1227,7 +1229,7 @@
         goto fail;
 
     /* Privileged (ring 0) instructions. */
-    opcode = insn_fetch(u8, 1, eip);
+    opcode = insn_fetch(u8, 1, cs, eip);
     switch ( opcode )
     {
     case 0x06: /* CLTS */
@@ -1245,7 +1247,7 @@
         break;
 
     case 0x20: /* MOV CR?,<reg> */
-        opcode = insn_fetch(u8, 1, eip);
+        opcode = insn_fetch(u8, 1, cs, eip);
         modrm_reg |= (opcode >> 3) & 7;
         modrm_rm  |= (opcode >> 0) & 7;
         reg = decode_register(modrm_rm, regs, 0);
@@ -1279,7 +1281,7 @@
         break;
 
     case 0x21: /* MOV DR?,<reg> */
-        opcode = insn_fetch(u8, 1, eip);
+        opcode = insn_fetch(u8, 1, cs, eip);
         modrm_reg |= (opcode >> 3) & 7;
         modrm_rm  |= (opcode >> 0) & 7;
         reg = decode_register(modrm_rm, regs, 0);
@@ -1289,7 +1291,7 @@
         break;
 
     case 0x22: /* MOV <reg>,CR? */
-        opcode = insn_fetch(u8, 1, eip);
+        opcode = insn_fetch(u8, 1, cs, eip);
         modrm_reg |= (opcode >> 3) & 7;
         modrm_rm  |= (opcode >> 0) & 7;
         reg = decode_register(modrm_rm, regs, 0);
@@ -1329,7 +1331,7 @@
         break;
 
     case 0x23: /* MOV <reg>,DR? */
-        opcode = insn_fetch(u8, 1, eip);
+        opcode = insn_fetch(u8, 1, cs, eip);
         modrm_reg |= (opcode >> 3) & 7;
         modrm_rm  |= (opcode >> 0) & 7;
         reg = decode_register(modrm_rm, regs, 0);

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

#1 lore raw
Quoted 8 lines · Bastian Blank

This patch fixes the emulation of instructions in vm86 mode. It fetches them using cs and eip instead of only eip. This makes it at least possible to use the i945GM vesa bios from the running system.

I'm not sure if this is the correct fix or if we should call the gpf handler of the running system. At least the support in linux reports traps back to the userspace caller which is not possible if emulated in xen.

There are also data operands of some instructions that may need fixing (INS/OUTS). What happens if you just bail to the guest's GPF handler if in vm86 mode? I think the libint10 library in userspace will emulate all these instructions itself.

-- Keir

#2 lore raw
Quoted 7 lines · Keir Fraser
Quoted 4 lines

I'm not sure if this is the correct fix or if we should call the gpf handler of the running system. At least the support in linux reports traps back to the userspace caller which is not possible if emulated in xen.

There are also data operands of some instructions that may need fixing (INS/OUTS). What happens if you just bail to the guest's GPF handler if in vm86 mode?

I don't know how to do that.

There is some code to pass it to the guest in do_general_protection, but I don't know why it fails than.

Bastian

Signature · 2 lines
You're dead, Jim.
		-- McCoy, "Amok Time", stardate 3372.7

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

#3 lore raw
Quoted 4 lines · Bastian Blank
Quoted 2 lines

(INS/OUTS). What happens if you just bail to the guest's GPF handler if in vm86 mode?

I don't know how to do that.

Just add 'if (vm86_mode(regs)) goto fail' to the top of emulate_privileged_op(). This will cause Xen to fall back to propagating the fault to the guest's GPF handler.

-- Keir

Quoted 2 lines

There is some code to pass it to the guest in do_general_protection, but I don't know why it fails than.

#4 lore raw
Quoted 7 lines · Keir Fraser
Quoted 3 lines · Bastian Blank
Quoted 2 lines

(INS/OUTS). What happens if you just bail to the guest's GPF handler if in vm86 mode?

I don't know how to do that.

Just add 'if (vm86_mode(regs)) goto fail' to the top of emulate_privileged_op(). This will cause Xen to fall back to propagating the fault to the guest's GPF handler.

Seems to work partialy. The i810 driver can init the hardware but I get weird artefacts. It seems that some of the memory which is used for the cursor is overwriten by the real video output or so. Therefor I think that one of the segment registers which the bios uses is overwriten somewhere in this gpf handling.

Hmm, the trap bounce code clears ds and es.

Bastian

Signature · 2 lines
Schshschshchsch.
		-- The Gorn, "Arena", stardate 3046.2

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

#5 lore raw
Quoted 7 lines · Bastian Blank

Seems to work partialy. The i810 driver can init the hardware but I get weird artefacts. It seems that some of the memory which is used for the cursor is overwriten by the real video output or so. Therefor I think that one of the segment registers which the bios uses is overwriten somewhere in this gpf handling.

Hmm, the trap bounce code clears ds and es.

The trap bounce code clears fs and gs too, just as native hardware does on an exception while in vm86 mode. The real values are saved on the exception stack frame (hopefully!).

-- Keir

#6 lore raw
Quoted 3 lines · Keir Fraser

The trap bounce code clears fs and gs too, just as native hardware does on an exception while in vm86 mode. The real values are saved on the exception stack frame (hopefully!).

Hmm, the code write 0 for this values to the exception frame in VM86 mode, lets check if this is the problem.

Bastian

Signature · 2 lines
Punishment becomes ineffective after a certain point.  Men become insensitive.
		-- Eneg, "Patterns of Force", stardate 2534.7

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

#7 lore raw
Quoted 6 lines · Bastian Blank
Quoted 3 lines · Keir Fraser

The trap bounce code clears fs and gs too, just as native hardware does on an exception while in vm86 mode. The real values are saved on the exception stack frame (hopefully!).

Hmm, the code write 0 for this values to the exception frame in VM86 mode, lets check if this is the problem.

It seems that this is the problem.

The following patch always bounces traps in vm86 mode to the guest kernel and don't longer clears the segment register values in the exception frame.

Bastian

Signature · 2 lines
The sooner our happiness together begins, the longer it will last.
		-- Miramanee, "The Paradise Syndrome", stardate 4842.6

#! /bin/sh /usr/share/dpatch/dpatch-run
## vm86-support-2.dpatch by Bastian Blank <[email protected]> ##
## All lines beginning with `## DP:' are a description of the patch. ## DP: No description.

@DPATCH@

xen/arch/x86/traps.c +3 -0
diff -urNad xen-unstable~/xen/arch/x86/traps.c xen-unstable/xen/arch/x86/traps.c
@@ -1032,6 +1032,9 @@
     unsigned int port, i, op_bytes = 4, data, rc;
     u32 l, h;
 
+    if ( vm86_mode(regs) )
+        goto fail;
+
     /* Legacy prefixes. */
     for ( i = 0; i < 8; i++ )
     {
xen/arch/x86/x86_32/entry.S +1 -11
diff -urNad xen-unstable~/xen/arch/x86/x86_32/entry.S xen-unstable/xen/arch/x86/x86_32/entry.S
@@ -331,15 +331,7 @@
 1:      testb $TBF_FAILSAFE,%cl
         jz   2f
         subl $16,%esi                # add DS/ES/FS/GS to failsafe stack frame
-        testl $X86_EFLAGS_VM,UREGS_eflags+4(%esp)
-        jz   nvm86_2
-        xorl %eax,%eax               # VM86: we write zero selector values
-FLT18:  movl %eax,%gs:(%esi) 
-FLT19:  movl %eax,%gs:4(%esi)
-FLT20:  movl %eax,%gs:8(%esi) 
-FLT21:  movl %eax,%gs:12(%esi)
-        jmp  2f
-nvm86_2:movl UREGS_ds+4(%esp),%eax   # non-VM86: write real selector values
+        movl UREGS_ds+4(%esp),%eax
 FLT22:  movl %eax,%gs:(%esi) 
         movl UREGS_es+4(%esp),%eax
 FLT23:  movl %eax,%gs:4(%esi)
@@ -375,8 +367,6 @@
         .long FLT12,domain_crash_synchronous , FLT13,domain_crash_synchronous
         .long FLT14,domain_crash_synchronous , FLT15,domain_crash_synchronous
         .long FLT16,domain_crash_synchronous , FLT17,domain_crash_synchronous
-        .long FLT18,domain_crash_synchronous , FLT19,domain_crash_synchronous
-        .long FLT20,domain_crash_synchronous , FLT21,domain_crash_synchronous
         .long FLT22,domain_crash_synchronous , FLT23,domain_crash_synchronous
         .long FLT24,domain_crash_synchronous , FLT25,domain_crash_synchronous
 .previous

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

#8 lore raw
Quoted 10 lines · Bastian Blank
Quoted 2 lines

Hmm, the code write 0 for this values to the exception frame in VM86 mode, lets check if this is the problem.

It seems that this is the problem.

The following patch always bounces traps in vm86 mode to the guest kernel and don't longer clears the segment register values in the exception frame.

That is only on the 'failsafe' stackframe path. It is okay to write zero values there because we already wrote the real values lower down on the stack frame (at labels FLT7 to FLT10). In fact you probably shouldn't end up taking the 'failsafe' path anyway, so the code you remove wouldn't normally be executed at all unless something in the guest is buggy.

-- Keir

#9 lore raw
Quoted 5 lines · Keir Fraser

That is only on the 'failsafe' stackframe path. It is okay to write zero values there because we already wrote the real values lower down on the stack frame (at labels FLT7 to FLT10). In fact you probably shouldn't end up taking the 'failsafe' path anyway, so the code you remove wouldn't normally be executed at all unless something in the guest is buggy.

Yep, saw that. So I have to recheck that.

Bastian

Signature · 3 lines
	"That unit is a woman."
	"A mass of conflicting impulses."
		-- Spock and Nomad, "The Changeling", stardate 3541.9

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

#10 lore raw
Quoted 8 lines · Bastian Blank
Quoted 5 lines · Keir Fraser

That is only on the 'failsafe' stackframe path. It is okay to write zero values there because we already wrote the real values lower down on the stack frame (at labels FLT7 to FLT10). In fact you probably shouldn't end up taking the 'failsafe' path anyway, so the code you remove wouldn't normally be executed at all unless something in the guest is buggy.

Yep, saw that. So I have to recheck that.

It works correctly with the patch but not without. Can't say why.

Bastian

Signature · 2 lines
You!  What PLANET is this!
		-- McCoy, "The City on the Edge of Forever", stardate 3134.0

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

#11 lore raw
Quoted 9 lines · Bastian Blank
Quoted 7 lines
Quoted 5 lines

That is only on the 'failsafe' stackframe path. It is okay to write zero values there because we already wrote the real values lower down on the stack frame (at labels FLT7 to FLT10). In fact you probably shouldn't end up taking the 'failsafe' path anyway, so the code you remove wouldn't normally be executed at all unless something in the guest is buggy.

Yep, saw that. So I have to recheck that.

It works correctly with the patch but not without. Can't say why.

That doesn't seem to make sense. It's probably worthwhile working out why you are taking that 'failsafe' path through create_bounce_frame. If you run a debug build of Xen, do you get lines of the form 'Pre-exception: xxxxxx -> xxxxxx' in 'xm dmesg' or on the Xen emergency console?

-- Keir

#12 lore raw
Quoted 4 lines · Keir Fraser

That doesn't seem to make sense. It's probably worthwhile working out why you are taking that 'failsafe' path through create_bounce_frame. If you run a debug build of Xen, do you get lines of the form 'Pre-exception: xxxxxx -> xxxxxx' in 'xm dmesg' or on the Xen emergency console?

Nope. In fact I'm unable to say why this happens. Is it possible that you at least merge the non-failsafe part of the patch (i.e. the if vm86_mode). It makes most things working.

Bastian

Signature · 2 lines
A princess should not be afraid -- not with a brave knight to protect her.
		-- McCoy, "Shore Leave", stardate 3025.3

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

#13 lore raw
Quoted 9 lines · Bastian Blank
Quoted 4 lines · Keir Fraser

That doesn't seem to make sense. It's probably worthwhile working out why you are taking that 'failsafe' path through create_bounce_frame. If you run a debug build of Xen, do you get lines of the form 'Pre-exception: xxxxxx -> xxxxxx' in 'xm dmesg' or on the Xen emergency console?

Nope. In fact I'm unable to say why this happens. Is it possible that you at least merge the non-failsafe part of the patch (i.e. the if vm86_mode). It makes most things working.

I already applied your original patch (which adds cs<<4 to eip if in vm86 mode). It was obviously the right thing to do.

-- Keir

#14 lore raw
Quoted 2 lines · Keir Fraser

I already applied your original patch (which adds cs<<4 to eip if in vm86 mode). It was obviously the right thing to do.

Ah, thanks.

Bastian

Signature · 3 lines
... The prejudices people feel about each other disappear when they get
to know each other.
		-- Kirk, "Elaan of Troyius", stardate 4372.5

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

#15 lore raw
In reply to Keir Fraser
Quoted 8 lines · * Keir Fraser ([email protected]) wrote:
Quoted 6 lines · Bastian Blank
Quoted 4 lines · Keir Fraser

That doesn't seem to make sense. It's probably worthwhile working out why you are taking that 'failsafe' path through create_bounce_frame. If you run a debug build of Xen, do you get lines of the form 'Pre-exception: xxxxxx -> xxxxxx' in 'xm dmesg' or on the Xen emergency console?

Quoted 6 lines
Quoted 3 lines

Nope. In fact I'm unable to say why this happens. Is it possible that you at least merge the non-failsafe part of the patch (i.e. the if vm86_mode). It makes most things working.

I already applied your original patch (which adds cs<<4 to eip if in vm86 mode). It was obviously the right thing to do.

This is breaking machines (with at least 845G) when running video bios. Appears it can't set the video memory size appropriately, and therefore X exits since it doesn't have any modes that will fit in the default 320k. Both backing the change out and kicking back to userspace using if(vm86_mode(regs)) goto fail; allow X to run again. I've tested various combos of patched and unpatched hv on 845, 945, and 965 to find that backing out the patch breaks X on 945. Bastian, any further ideas on the failsafe path?

thanks,
-chris

#16 lore raw
Quoted 17 lines
Quoted 1 line
Quoted 1 line

Chris Wright <[email protected]> 02.11.06 12:53 >>>

Quoted 8 lines · * Keir Fraser ([email protected]) wrote:
Quoted 6 lines · Bastian Blank
Quoted 4 lines · Keir Fraser

That doesn't seem to make sense. It's probably worthwhile working out why you are taking that 'failsafe' path through create_bounce_frame. If you run a debug build of Xen, do you get lines of the form 'Pre-exception: xxxxxx -> xxxxxx' in 'xm dmesg' or on the Xen emergency console?

Quoted 6 lines
Quoted 3 lines

Nope. In fact I'm unable to say why this happens. Is it possible that you at least merge the non-failsafe part of the patch (i.e. the if vm86_mode). It makes most things working.

I already applied your original patch (which adds cs<<4 to eip if in vm86 mode). It was obviously the right thing to do.

Quoted 8 lines

This is breaking machines (with at least 845G) when running video bios. Appears it can't set the video memory size appropriately, and therefore X exits since it doesn't have any modes that will fit in the default 320k. Both backing the change out and kicking back to userspace using if(vm86_mode(regs)) goto fail; allow X to run again. I've tested various combos of patched and unpatched hv on 845, 945, and 965 to find that backing out the patch breaks X on 945. Bastian, any further ideas on the failsafe path?

Hmm, that would then perhaps also cause problems with the enhanced emulation code I have pending in my 32on64 queue, unless the problem is due to masked problems in the old code (ie when not adding the CS base for vm86 mode emulation). Any chance you dug into why exactly the more correct (but still incomplete) code doesn't work anymore (namely, what instruction(s) caused problems)?

Jan

#17 lore raw
Quoted 6 lines · * Jan Beulich ([email protected]) wrote:

Hmm, that would then perhaps also cause problems with the enhanced emulation code I have pending in my 32on64 queue, unless the problem is due to masked problems in the old code (ie when not adding the CS base for vm86 mode emulation). Any chance you dug into why exactly the more correct (but still incomplete) code doesn't work anymore (namely, what instruction(s) caused problems)?

I haven't figured out yet where the underlying problem is. I had a working theory until i tested on the 945, so I still have some digging to do.

thanks,
-chris