Skip to contentSkip to conversation

nested VMX: fix I/O port exit emulation

PATCH Superseded Jan Beulich 5 participants 17 messages Dec 3, 2013 – Dec 5, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nested VMX: fix I/O port exit emulation

For multi-byte operations all affected ports' bits in the bitmap need to be checked, not just the first port's one.

xen/arch/x86/hvm/vmx/vvmx.c +13 -7
@@ -2134,7 +2134,6 @@ int nvmx_n2_vmexit_handler(struct cpu_us
     struct nestedvcpu *nvcpu = &vcpu_nestedhvm(v);
     struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
     u32 ctrl;
-    u8 *bitmap;
 
     nvcpu->nv_vmexit_pending = 0;
     nvmx->intr.intr_info = 0;
@@ -2220,15 +2219,22 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
             __vmread(EXIT_QUALIFICATION, &qual);
-            port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;
+            for ( port = qual >> 16, size = (qual & 7) + 1; ; )
+            {
+                const u8 *bitmap = nvmx->iobitmap[port >> 15];
+
+                if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 7)) )
+                    nvcpu->nv_vmexit_pending = 1;
+                if ( !--size )
+                    break;
+                if ( !++port )
+                    nvcpu->nv_vmexit_pending = 1;
+            } while ( !nvcpu->nv_vmexit_pending );
             if ( !nvcpu->nv_vmexit_pending )
-               gdprintk(XENLOG_WARNING, "L0 PIO %x.\n", port);
+                printk(XENLOG_G_WARNING "L0 PIO %04x\n", port);
         }
         else if ( ctrl & CPU_BASED_UNCOND_IO_EXITING )
             nvcpu->nv_vmexit_pending = 1;

nested VMX: fix I/O port exit emulation

For multi-byte operations all affected ports' bits in the bitmap need to be checked, not just the first port's one.

xen/arch/x86/hvm/vmx/vvmx.c +13 -7
@@ -2134,7 +2134,6 @@ int nvmx_n2_vmexit_handler(struct cpu_us
     struct nestedvcpu *nvcpu = &vcpu_nestedhvm(v);
     struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
     u32 ctrl;
-    u8 *bitmap;
 
     nvcpu->nv_vmexit_pending = 0;
     nvmx->intr.intr_info = 0;
@@ -2220,15 +2219,22 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
             __vmread(EXIT_QUALIFICATION, &qual);
-            port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;
+            for ( port = qual >> 16, size = (qual & 7) + 1; ; )
+            {
+                const u8 *bitmap = nvmx->iobitmap[port >> 15];
+
+                if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 7)) )
+                    nvcpu->nv_vmexit_pending = 1;
+                if ( !--size )
+                    break;
+                if ( !++port )
+                    nvcpu->nv_vmexit_pending = 1;
+            } while ( !nvcpu->nv_vmexit_pending );
             if ( !nvcpu->nv_vmexit_pending )
-               gdprintk(XENLOG_WARNING, "L0 PIO %x.\n", port);
+                printk(XENLOG_G_WARNING "L0 PIO %04x\n", port);
         }
         else if ( ctrl & CPU_BASED_UNCOND_IO_EXITING )
             nvcpu->nv_vmexit_pending = 1;

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

#1 lore raw
Quoted 29 lines · Jan Beulich

For multi-byte operations all affected ports' bits in the bitmap need to be checked, not just the first port's one.

xen/arch/x86/hvm/vmx/vvmx.c +2 -6
@@ -2134,7 +2134,6 @@ int nvmx_n2_vmexit_handler(struct cpu_us
     struct nestedvcpu *nvcpu = &vcpu_nestedhvm(v);
     struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
     u32 ctrl;
-    u8 *bitmap;
 
     nvcpu->nv_vmexit_pending = 0;
     nvmx->intr.intr_info = 0;
@@ -2220,15 +2219,22 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
             __vmread(EXIT_QUALIFICATION, &qual);
-            port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;
+            for ( port = qual >> 16, size = (qual & 7) + 1; ; )

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_n2_vmexit_handler(struct cpu_us · line 2235

Quoted 10 lines
xen/arch/x86/hvm/vmx/vvmx.c +10 -0
+            {
+                const u8 *bitmap = nvmx->iobitmap[port >> 15];
+
+                if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 7)) )
+                    nvcpu->nv_vmexit_pending = 1;
+                if ( !--size )
+                    break;
+                if ( !++port )
+                    nvcpu->nv_vmexit_pending = 1;
+            } while ( !nvcpu->nv_vmexit_pending );

You have a rather odd looking "for () { } while ()" loop, which appears to be a while loop with no body and a constant loop condition. Is this intended?

~Andrew

Quoted 11 lines
xen/arch/x86/hvm/vmx/vvmx.c +1 -1
             if ( !nvcpu->nv_vmexit_pending )
-               gdprintk(XENLOG_WARNING, "L0 PIO %x.\n", port);
+                printk(XENLOG_G_WARNING "L0 PIO %04x\n", port);
         }
         else if ( ctrl & CPU_BASED_UNCOND_IO_EXITING )
             nvcpu->nv_vmexit_pending = 1;
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 37 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 35 lines · Jan Beulich

For multi-byte operations all affected ports' bits in the bitmap need to be checked, not just the first port's one.

xen/arch/x86/hvm/vmx/vvmx.c +8 -6
@@ -2134,7 +2134,6 @@ int nvmx_n2_vmexit_handler(struct cpu_us
     struct nestedvcpu *nvcpu = &vcpu_nestedhvm(v);
     struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
     u32 ctrl;
-    u8 *bitmap;
 
     nvcpu->nv_vmexit_pending = 0;
     nvmx->intr.intr_info = 0;
@@ -2220,15 +2219,22 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
             __vmread(EXIT_QUALIFICATION, &qual);
-            port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;
+            for ( port = qual >> 16, size = (qual & 7) + 1; ; )
+            {
+                const u8 *bitmap = nvmx->iobitmap[port >> 15];
+
+                if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 7)) )
+                    nvcpu->nv_vmexit_pending = 1;
+                if ( !--size )
Quoted 8 lines

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_n2_vmexit_handler(struct cpu_us · line 2235

Quoted 4 lines
xen/arch/x86/hvm/vmx/vvmx.c +4 -0
+                    break;
+                if ( !++port )
+                    nvcpu->nv_vmexit_pending = 1;
+            } while ( !nvcpu->nv_vmexit_pending );

You have a rather odd looking "for () { } while ()" loop, which appears to be a while loop with no body and a constant loop condition. Is this intended?

Oops, no, of course not.

Jan

#3 lore raw

PATCH v2 1/1 nested VMX: fix I/O port exit emulation

For multi-byte operations all affected ports' bits in the bitmap need to be checked, not just the first port's one.

---
v2: Fix loop construct.

xen/arch/x86/hvm/vmx/vvmx.c +13 -6
@@ -2134,7 +2134,6 @@ int nvmx_n2_vmexit_handler(struct cpu_us
     struct nestedvcpu *nvcpu = &vcpu_nestedhvm(v);
     struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
     u32 ctrl;
-    u8 *bitmap;
 
     nvcpu->nv_vmexit_pending = 0;
     nvmx->intr.intr_info = 0;
@@ -2220,15 +2219,23 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
             __vmread(EXIT_QUALIFICATION, &qual);
             port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;
+            size = (qual & 7) + 1;
+            do {
+                const u8 *bitmap = nvmx->iobitmap[port >> 15];
+
+                if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 7)) )
+                    nvcpu->nv_vmexit_pending = 1;
+                if ( !--size )
+                    break;
+                if ( !++port )
+                    nvcpu->nv_vmexit_pending = 1;
+            } while ( !nvcpu->nv_vmexit_pending );
             if ( !nvcpu->nv_vmexit_pending )
-               gdprintk(XENLOG_WARNING, "L0 PIO %x.\n", port);
+                printk(XENLOG_G_WARNING "L0 PIO %04x\n", port);
         }
         else if ( ctrl & CPU_BASED_UNCOND_IO_EXITING )
             nvcpu->nv_vmexit_pending = 1;

nested VMX: fix I/O port exit emulation

For multi-byte operations all affected ports' bits in the bitmap need to be checked, not just the first port's one.

---
v2: Fix loop construct.

xen/arch/x86/hvm/vmx/vvmx.c +13 -6
@@ -2134,7 +2134,6 @@ int nvmx_n2_vmexit_handler(struct cpu_us
     struct nestedvcpu *nvcpu = &vcpu_nestedhvm(v);
     struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
     u32 ctrl;
-    u8 *bitmap;
 
     nvcpu->nv_vmexit_pending = 0;
     nvmx->intr.intr_info = 0;
@@ -2220,15 +2219,23 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
             __vmread(EXIT_QUALIFICATION, &qual);
             port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;
+            size = (qual & 7) + 1;
+            do {
+                const u8 *bitmap = nvmx->iobitmap[port >> 15];
+
+                if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 7)) )
+                    nvcpu->nv_vmexit_pending = 1;
+                if ( !--size )
+                    break;
+                if ( !++port )
+                    nvcpu->nv_vmexit_pending = 1;
+            } while ( !nvcpu->nv_vmexit_pending );
             if ( !nvcpu->nv_vmexit_pending )
-               gdprintk(XENLOG_WARNING, "L0 PIO %x.\n", port);
+                printk(XENLOG_G_WARNING "L0 PIO %04x\n", port);
         }
         else if ( ctrl & CPU_BASED_UNCOND_IO_EXITING )
             nvcpu->nv_vmexit_pending = 1;

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

#4 lore raw
Quoted 21 lines · Jan Beulich

For multi-byte operations all affected ports' bits in the bitmap need to be checked, not just the first port's one.

---
v2: Fix loop construct.

xen/arch/x86/hvm/vmx/vvmx.c +0 -1
@@ -2134,7 +2134,6 @@ int nvmx_n2_vmexit_handler(struct cpu_us
     struct nestedvcpu *nvcpu = &vcpu_nestedhvm(v);
     struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
     u32 ctrl;
-    u8 *bitmap;
 
     nvcpu->nv_vmexit_pending = 0;
     nvmx->intr.intr_info = 0;
@@ -2220,15 +2219,23 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_n2_vmexit_handler(struct cpu_us · line 2226

Quoted 10 lines
xen/arch/x86/hvm/vmx/vvmx.c +2 -4
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
             __vmread(EXIT_QUALIFICATION, &qual);
             port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;
+            size = (qual & 7) + 1;

This should be (qual & 3) for the correct size (Bit 3 is the direction). Is it worth also verifying that ((qual & 3) != 2)?

~Andrew

Quoted 19 lines
xen/arch/x86/hvm/vmx/vvmx.c +11 -1
+            do {
+                const u8 *bitmap = nvmx->iobitmap[port >> 15];
+
+                if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 7)) )
+                    nvcpu->nv_vmexit_pending = 1;
+                if ( !--size )
+                    break;
+                if ( !++port )
+                    nvcpu->nv_vmexit_pending = 1;
+            } while ( !nvcpu->nv_vmexit_pending );
             if ( !nvcpu->nv_vmexit_pending )
-               gdprintk(XENLOG_WARNING, "L0 PIO %x.\n", port);
+                printk(XENLOG_G_WARNING "L0 PIO %04x\n", port);
         }
         else if ( ctrl & CPU_BASED_UNCOND_IO_EXITING )
             nvcpu->nv_vmexit_pending = 1;
#5 lore raw
Quoted 8 lines
Quoted 8 lines
Quoted 1 line · Andrew Cooper
xen/arch/x86/hvm/vmx/vvmx.c +1 -1
@@ -2220,15 +2219,23 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
Quoted 9 lines

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_n2_vmexit_handler(struct cpu_us · line 2226

Quoted 6 lines
xen/arch/x86/hvm/vmx/vvmx.c +1 -3
             __vmread(EXIT_QUALIFICATION, &qual);
             port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;
+            size = (qual & 7) + 1;

This should be (qual & 3) for the correct size (Bit 3 is the direction).

Right - bit 3 is the direction. Bits 0..2 are the size. Hence the mask ought to be 7.

Quoted 1 line

Is it worth also verifying that ((qual & 3) != 2)?

I don't think so - there's no harm to our code here if an undefined value was there.

Jan

Reviewed-by
#6 lore raw
Quoted 16 lines · Jan Beulich
Quoted 8 lines
Quoted 8 lines
Quoted 1 line · Andrew Cooper
xen/arch/x86/hvm/vmx/vvmx.c +1 -1
@@ -2220,15 +2219,23 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
Quoted 8 lines

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_n2_vmexit_handler(struct cpu_us · line 2226

Quoted 6 lines
xen/arch/x86/hvm/vmx/vvmx.c +1 -3
             __vmread(EXIT_QUALIFICATION, &qual);
             port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;
+            size = (qual & 7) + 1;

This should be (qual & 3) for the correct size (Bit 3 is the direction).

Quoted 2 lines

Right - bit 3 is the direction. Bits 0..2 are the size. Hence the mask ought to be 7.

D'oh - I cant count, even with the manual for reference.

Quoted 7 lines
Quoted 2 lines

Is it worth also verifying that ((qual & 3) != 2)?

I don't think so - there's no harm to our code here if an undefined value was there.

Jan

#7 lore raw
In reply to Jan Beulich

Jan Beulich wrote on 2013-12-03:

Quoted 30 lines

For multi-byte operations all affected ports' bits in the bitmap need to be checked, not just the first port's one.

---
v2: Fix loop construct.

xen/arch/x86/hvm/vmx/vvmx.c +1 -5
@@ -2134,7 +2134,6 @@ int nvmx_n2_vmexit_handler(struct cpu_us
     struct nestedvcpu *nvcpu = &vcpu_nestedhvm(v);
     struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
     u32 ctrl;
-    u8 *bitmap;
 
     nvcpu->nv_vmexit_pending = 0;
     nvmx->intr.intr_info = 0;
@@ -2220,15 +2219,23 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
             __vmread(EXIT_QUALIFICATION, &qual);
             port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_n2_vmexit_handler(struct cpu_us · line 2235

Quoted 10 lines
xen/arch/x86/hvm/vmx/vvmx.c +10 -0
+            size = (qual & 7) + 1;
+            do {
+                const u8 *bitmap = nvmx->iobitmap[port >> 15];
+
+                if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 7)) )
+                    nvcpu->nv_vmexit_pending = 1;
+                if ( !--size )
+                    break;
+                if ( !++port )
+                    nvcpu->nv_vmexit_pending = 1;

If port overflow, will it cause vmexit or maybe other fault like GP or just be ignored? Also, you need to check the DF bit to know the string direction before updating the port.

Best regards,
Yang

#8 lore raw
Quoted 43 lines · Yang Z

Jan Beulich wrote on 2013-12-03:

Quoted 30 lines

For multi-byte operations all affected ports' bits in the bitmap need to be checked, not just the first port's one.

---
v2: Fix loop construct.

xen/arch/x86/hvm/vmx/vvmx.c +1 -5
@@ -2134,7 +2134,6 @@ int nvmx_n2_vmexit_handler(struct cpu_us
     struct nestedvcpu *nvcpu = &vcpu_nestedhvm(v);
     struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
     u32 ctrl;
-    u8 *bitmap;
 
     nvcpu->nv_vmexit_pending = 0;
     nvmx->intr.intr_info = 0;
@@ -2220,15 +2219,23 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
             __vmread(EXIT_QUALIFICATION, &qual);
             port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_n2_vmexit_handler(struct cpu_us · line 2235

Quoted 10 lines
xen/arch/x86/hvm/vmx/vvmx.c +10 -0
+            size = (qual & 7) + 1;
+            do {
+                const u8 *bitmap = nvmx->iobitmap[port >> 15];
+
+                if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 7)) )
+                    nvcpu->nv_vmexit_pending = 1;
+                if ( !--size )
+                    break;
+                if ( !++port )
+                    nvcpu->nv_vmexit_pending = 1;

If port overflow, will it cause vmexit or maybe other fault like GP or just be ignored? Also, you need to check the DF bit to know the string direction before updating the port.

Quoted 4 lines

Best regards,
Yang

What does the direction flag have to do with the port(s) used as the target of an ins/outs instruction? I was under the impression that it solely acted as an increment/decrement on si/di.

~Andrew

#9 lore raw

Andrew Cooper wrote on 2013-12-04:

Quoted 47 lines · Yang Z
Quoted 35 lines

Jan Beulich wrote on 2013-12-03:

Quoted 34 lines

For multi-byte operations all affected ports' bits in the bitmap need to be checked, not just the first port's one.

---
v2: Fix loop construct.

xen/arch/x86/hvm/vmx/vvmx.c +5 -5
@@ -2134,7 +2134,6 @@ int nvmx_n2_vmexit_handler(struct cpu_us
     struct nestedvcpu *nvcpu = &vcpu_nestedhvm(v);
     struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
     u32 ctrl;
-    u8 *bitmap;
 
     nvcpu->nv_vmexit_pending = 0;
     nvmx->intr.intr_info = 0;
@@ -2220,15 +2219,23 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
             __vmread(EXIT_QUALIFICATION, &qual);
             port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;
+            size = (qual & 7) + 1;
+            do {
+                const u8 *bitmap = nvmx->iobitmap[port >> 15];
+
Quoted 8 lines

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_n2_vmexit_handler(struct cpu_us · line 2235

Quoted 6 lines
xen/arch/x86/hvm/vmx/vvmx.c +6 -0
+                if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 7)) )
+                    nvcpu->nv_vmexit_pending = 1;
+                if ( !--size )
+                    break;
+                if ( !++port )
+                    nvcpu->nv_vmexit_pending = 1;

If port overflow, will it cause vmexit or maybe other fault like GP or just be

ignored? Also, you need to check the DF bit to know the string direction before updating the port.

Quoted 1 line
Quoted 9 lines
Quoted 4 lines

Best regards,
Yang

What does the direction flag have to do with the port(s) used as the target of an ins/outs instruction? I was under the impression that it solely acted as an increment/decrement on si/di.

Uh.. It seems DF only impact the memory location, ioport is not affected, See what SDM said. After the byte, word, or doubleword is transfer from the I/O port to the memory location, the DI/EDI/RDI register is incremented or decremented automatically according to the setting of the DF flag in the EFLAGS register. (If the DF flag is 0, the (E)DI register is incremented; if the DF flag is 1, the (E)DI register is decremented.) The (E)DI register is incremented or decremented by 1 for byte operations, by 2 for word operations, or by 4 for doubleword operations.

Quoted 1 line

~Andrew

Best regards,
Yang

#10 lore raw
In reply to Zhang, Yang Z
Quoted 19 lines
Quoted 1 line
Quoted 1 line · Yang Z

Jan Beulich wrote on 2013-12-03:

Quoted 17 lines
xen/arch/x86/hvm/vmx/vvmx.c +6 -4
@@ -2220,15 +2219,23 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
             __vmread(EXIT_QUALIFICATION, &qual);
             port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;
+            size = (qual & 7) + 1;
+            do {
+                const u8 *bitmap = nvmx->iobitmap[port >> 15];
+
+                if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 7)) )
Quoted 8 lines

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_n2_vmexit_handler(struct cpu_us · line 2235

Quoted 5 lines
xen/arch/x86/hvm/vmx/vvmx.c +5 -0
+                    nvcpu->nv_vmexit_pending = 1;
+                if ( !--size )
+                    break;
+                if ( !++port )
+                    nvcpu->nv_vmexit_pending = 1;

If port overflow, will it cause vmexit or maybe other fault like GP or just be ignored?

The documentation is explicit here: It causes a VM exit. And hence the emulation is doing so too.

Quoted 2 lines

Also, you need to check the DF bit to know the string direction before updating the port.

I think Andrew already sufficiently clarified that part.

Jan

#11 lore raw

Jan Beulich wrote on 2013-12-04:

Quoted 28 lines
Quoted 19 lines
Quoted 1 line
Quoted 1 line · Yang Z

Jan Beulich wrote on 2013-12-03:

Quoted 17 lines
xen/arch/x86/hvm/vmx/vvmx.c +6 -4
@@ -2220,15 +2219,23 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
             __vmread(EXIT_QUALIFICATION, &qual);
             port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;
+            size = (qual & 7) + 1;
+            do {
+                const u8 *bitmap = nvmx->iobitmap[port >> 15];
+
+                if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 7)) )
Quoted 8 lines

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_n2_vmexit_handler(struct cpu_us · line 2235

Quoted 5 lines
xen/arch/x86/hvm/vmx/vvmx.c +5 -0
+                    nvcpu->nv_vmexit_pending = 1;
+                if ( !--size )
+                    break;
+                if ( !++port )
+                    nvcpu->nv_vmexit_pending = 1;

If port overflow, will it cause vmexit or maybe other fault like GP or just be ignored?

Quoted 2 lines

The documentation is explicit here: It causes a VM exit. And hence the emulation is doing so too.

Ok. BTW, which chapter tells this? I didn't find the corresponding chapter in Intel SDM. :(

Quoted 7 lines
Quoted 3 lines

Also, you need to check the DF bit to know the string direction before updating the port.

I think Andrew already sufficiently clarified that part.

Jan

Best regards,
Yang

#12 lore raw
Quoted 30 lines
Quoted 1 line
Quoted 1 line · Yang Z

Jan Beulich wrote on 2013-12-04:

Quoted 28 lines
Quoted 27 lines
Quoted 1 line
Quoted 1 line · Yang Z

Jan Beulich wrote on 2013-12-03:

Quoted 12 lines
xen/arch/x86/hvm/vmx/vvmx.c +1 -4
@@ -2220,15 +2219,23 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
             __vmread(EXIT_QUALIFICATION, &qual);
             port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_n2_vmexit_handler(struct cpu_us · line 2235

Quoted 10 lines
xen/arch/x86/hvm/vmx/vvmx.c +10 -0
+            size = (qual & 7) + 1;
+            do {
+                const u8 *bitmap = nvmx->iobitmap[port >> 15];
+
+                if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 7)) )
+                    nvcpu->nv_vmexit_pending = 1;
+                if ( !--size )
+                    break;
+                if ( !++port )
+                    nvcpu->nv_vmexit_pending = 1;

If port overflow, will it cause vmexit or maybe other fault like GP or just be ignored?

Quoted 5 lines
Quoted 2 lines

The documentation is explicit here: It causes a VM exit. And hence the emulation is doing so too.

Ok. BTW, which chapter tells this? I didn't find the corresponding chapter in Intel SDM. :(

"25.1.3 Instructions That Cause VM Exits Conditionally" says

"● IN, INS/INSB/INSW/INSD, OUT, OUTS/OUTSB/OUTSW/OUTSD. The behavior of each of these instructions is determined by the settings of the “unconditional I/O exiting” and “use I/O bitmaps” VM-execution controls:
— If both controls are 0, the instruction executes normally.
— If the “unconditional I/O exiting” VM-execution control is 1 and the “use I/O bitmaps” VM-execution control
is 0, the instruction causes a VM exit.
— If the “use I/O bitmaps” VM-execution control is 1, the instruction causes a VM exit if it attempts to access an I/O port corresponding to a bit set to 1 in the appropriate I/O bitmap (see Section 24.6.4). If an I/O operation “wraps around” the 16-bit I/O-port space (accesses ports FFFFH and 0000H), the I/O instruction causes a VM exit (the “unconditional I/O exiting” VM-execution control is ignored if the “use I/O bitmaps” VM-execution control is 1)."

Jan

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

#13 lore raw

Jan Beulich wrote on 2013-12-04:

Quoted 39 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line · Yang Z

Jan Beulich wrote on 2013-12-04:

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

On 04.12.13 at 02:51, "Zhang, Yang Z" <[email protected]>

Quoted 27 lines · wrote:
Quoted 27 lines
Quoted 26 lines

Jan Beulich wrote on 2013-12-03:

Quoted 12 lines
xen/arch/x86/hvm/vmx/vvmx.c +1 -4
@@ -2220,15 +2219,23 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
             __vmread(EXIT_QUALIFICATION, &qual);
             port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_n2_vmexit_handler(struct cpu_us · line 2235

Quoted 10 lines
xen/arch/x86/hvm/vmx/vvmx.c +10 -0
+            size = (qual & 7) + 1;
+            do {
+                const u8 *bitmap = nvmx->iobitmap[port >> 15];
+
+                if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 7)) )
+                    nvcpu->nv_vmexit_pending = 1;
+                if ( !--size )
+                    break;
+                if ( !++port )
+                    nvcpu->nv_vmexit_pending = 1;

If port overflow, will it cause vmexit or maybe other fault like GP or just be ignored?

Quoted 5 lines
Quoted 2 lines

The documentation is explicit here: It causes a VM exit. And hence the emulation is doing so too.

Ok. BTW, which chapter tells this? I didn't find the corresponding chapter in Intel SDM. :(

"25.1.3 Instructions That Cause VM Exits Conditionally" says

Quoted 14 lines

"● IN, INS/INSB/INSW/INSD, OUT, OUTS/OUTSB/OUTSW/OUTSD. The behavior of each of these instructions is determined by the settings of the “unconditional I/O exiting” and “use I/O bitmaps” VM-execution controls:
— If both controls are 0, the instruction executes normally. — If the “unconditional I/O exiting” VM-execution control is 1 and the “use I/O bitmaps” VM-execution control is 0, the instruction causes a VM exit. — If the “use I/O bitmaps” VM-execution control is 1, the instruction causes a VM exit if it attempts to access an I/O port corresponding to a bit set to 1 in the appropriate I/O bitmap (see Section 24.6.4). If an I/O operation “wraps around” the 16-bit I/O-port space (accesses ports FFFFH and 0000H), the I/O instruction causes a VM exit (the “unconditional I/O exiting” VM-execution control is ignored if the “use I/O bitmaps” VM-execution control is 1)."

Got it. Thanks.

Quoted 1 line

Jan

Best regards,
Yang

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

#14 lore raw
In reply to Jan Beulich

Acked-by Eddie Dong <[email protected]>

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, December 03, 2013 10:07 PM
To: xen-devel
Cc: Matthew Daley; Andrew Cooper; Dong, Eddie; Nakajima, Jun
Subject: [PATCH v2] nested VMX: fix I/O port exit emulation

For multi-byte operations all affected ports' bits in the bitmap need to be checked, not just the first port's one.

---
v2: Fix loop construct.

xen/arch/x86/hvm/vmx/vvmx.c +13 -6
@@ -2134,7 +2134,6 @@ int nvmx_n2_vmexit_handler(struct cpu_us
     struct nestedvcpu *nvcpu = &vcpu_nestedhvm(v);
     struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
     u32 ctrl;
-    u8 *bitmap;
 
     nvcpu->nv_vmexit_pending = 0;
     nvmx->intr.intr_info = 0;
@@ -2220,15 +2219,23 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
             __vmread(EXIT_QUALIFICATION, &qual);
             port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;
+            size = (qual & 7) + 1;
+            do {
+                const u8 *bitmap = nvmx->iobitmap[port >> 15];
+
+                if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 7)) )
+                    nvcpu->nv_vmexit_pending = 1;
+                if ( !--size )
+                    break;
+                if ( !++port )
+                    nvcpu->nv_vmexit_pending = 1;
+            } while ( !nvcpu->nv_vmexit_pending );
             if ( !nvcpu->nv_vmexit_pending )
-               gdprintk(XENLOG_WARNING, "L0 PIO %x.\n", port);
+                printk(XENLOG_G_WARNING "L0 PIO %04x\n", port);
         }
         else if ( ctrl & CPU_BASED_UNCOND_IO_EXITING )
             nvcpu->nv_vmexit_pending = 1;
#15 lore raw
In reply to Jan Beulich
Quoted 38 lines · Jan Beulich

For multi-byte operations all affected ports' bits in the bitmap need to be checked, not just the first port's one.

---
v2: Fix loop construct.

xen/arch/x86/hvm/vmx/vvmx.c +8 -5
@@ -2134,7 +2134,6 @@ int nvmx_n2_vmexit_handler(struct cpu_us
     struct nestedvcpu *nvcpu = &vcpu_nestedhvm(v);
     struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
     u32 ctrl;
-    u8 *bitmap;
 
     nvcpu->nv_vmexit_pending = 0;
     nvmx->intr.intr_info = 0;
@@ -2220,15 +2219,23 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
             __vmread(EXIT_QUALIFICATION, &qual);
             port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;
+            size = (qual & 7) + 1;
+            do {
+                const u8 *bitmap = nvmx->iobitmap[port >> 15];
+
+                if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 7)) )
+                    nvcpu->nv_vmexit_pending = 1;
+                if ( !--size )

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_n2_vmexit_handler(struct cpu_us · line 2241

Quoted 10 lines
xen/arch/x86/hvm/vmx/vvmx.c +5 -1
+                    break;
+                if ( !++port )
+                    nvcpu->nv_vmexit_pending = 1;
+            } while ( !nvcpu->nv_vmexit_pending );
             if ( !nvcpu->nv_vmexit_pending )
-               gdprintk(XENLOG_WARNING, "L0 PIO %x.\n", port);
+                printk(XENLOG_G_WARNING "L0 PIO %04x\n", port);
         }
         else if ( ctrl & CPU_BASED_UNCOND_IO_EXITING )
             nvcpu->nv_vmexit_pending = 1;

Can you use #define's for the bit operations, please? That makes the code more readable and avoids copy & paste errors.

Christoph

#16 lore raw
Quoted 45 lines
Quoted 1 line
Quoted 1 line · Christoph
Quoted 43 lines · Jan Beulich

For multi-byte operations all affected ports' bits in the bitmap need to be checked, not just the first port's one.

---
v2: Fix loop construct.

xen/arch/x86/hvm/vmx/vvmx.c +12 -6
@@ -2134,7 +2134,6 @@ int nvmx_n2_vmexit_handler(struct cpu_us
     struct nestedvcpu *nvcpu = &vcpu_nestedhvm(v);
     struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
     u32 ctrl;
-    u8 *bitmap;
 
     nvcpu->nv_vmexit_pending = 0;
     nvmx->intr.intr_info = 0;
@@ -2220,15 +2219,23 @@ int nvmx_n2_vmexit_handler(struct cpu_us
         if ( ctrl & CPU_BASED_ACTIVATE_IO_BITMAP )
         {
             unsigned long qual;
-            u16 port;
+            u16 port, size;
 
             __vmread(EXIT_QUALIFICATION, &qual);
             port = qual >> 16;
-            bitmap = nvmx->iobitmap[port >> 15];
-            if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 0x7)) )
-                nvcpu->nv_vmexit_pending = 1;
+            size = (qual & 7) + 1;
+            do {
+                const u8 *bitmap = nvmx->iobitmap[port >> 15];
+
+                if ( bitmap[(port & 0x7fff) >> 3] & (1 << (port & 7)) )
+                    nvcpu->nv_vmexit_pending = 1;
+                if ( !--size )
+                    break;
+                if ( !++port )
+                    nvcpu->nv_vmexit_pending = 1;
+            } while ( !nvcpu->nv_vmexit_pending );
             if ( !nvcpu->nv_vmexit_pending )
-               gdprintk(XENLOG_WARNING, "L0 PIO %x.\n", port);
Quoted 8 lines

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_n2_vmexit_handler(struct cpu_us · line 2241

Quoted 5 lines
xen/arch/x86/hvm/vmx/vvmx.c +1 -0
+                printk(XENLOG_G_WARNING "L0 PIO %04x\n", port);
         }
         else if ( ctrl & CPU_BASED_UNCOND_IO_EXITING )
             nvcpu->nv_vmexit_pending = 1;

Can you use #define's for the bit operations, please? That makes the code more readable and avoids copy & paste errors.

That would be a separate cleanup patch, as there's code in vmx.c also wanting to then use such #define-s.

Jan