Skip to contentSkip to conversation

x86/HVM: mis adjustments

PATCH New Jan Beulich 2 participants 13 messages Jun 8, 2016 – Jun 17, 2016

lore
Share & tools
lore
#1 lore raw

PATCH 1/2 x86/HVM: constify hvm_virtual_to_linear_addr()'s segment register parameter

... to clarify to callers that they don't need to fear the pointed to data changing (which will be made use of subsequently).

xen/arch/x86/hvm/hvm.c +1 -1
@@ -2411,7 +2411,7 @@ int hvm_set_cr4(unsigned long value, boo
 
 bool_t hvm_virtual_to_linear_addr(
     enum x86_segment seg,
-    struct segment_register *reg,
+    const struct segment_register *reg,
     unsigned long offset,
     unsigned int bytes,
     enum hvm_access_type access_type,
xen/include/asm-x86/hvm/hvm.h +1 -1
@@ -469,7 +469,7 @@ enum hvm_access_type {
 };
 bool_t hvm_virtual_to_linear_addr(
     enum x86_segment seg,
-    struct segment_register *reg,
+    const struct segment_register *reg,
     unsigned long offset,
     unsigned int bytes,
     enum hvm_access_type access_type,

x86/HVM: constify hvm_virtual_to_linear_addr()'s segment register parameter

... to clarify to callers that they don't need to fear the pointed to data changing (which will be made use of subsequently).

xen/arch/x86/hvm/hvm.c +1 -1
@@ -2411,7 +2411,7 @@ int hvm_set_cr4(unsigned long value, boo
 
 bool_t hvm_virtual_to_linear_addr(
     enum x86_segment seg,
-    struct segment_register *reg,
+    const struct segment_register *reg,
     unsigned long offset,
     unsigned int bytes,
     enum hvm_access_type access_type,
xen/include/asm-x86/hvm/hvm.h +1 -1
@@ -469,7 +469,7 @@ enum hvm_access_type {
 };
 bool_t hvm_virtual_to_linear_addr(
     enum x86_segment seg,
-    struct segment_register *reg,
+    const struct segment_register *reg,
     unsigned long offset,
     unsigned int bytes,
     enum hvm_access_type access_type,

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

Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] x86/HVM: constify hvm_virtual_to_linear_addr()'s segment register parameter

Quoted 4 lines · Jan Beulich

... to clarify to callers that they don't need to fear the pointed to data changing (which will be made use of subsequently).

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

#3 lore raw

PATCH 2/2 x86/HVM: re-order operations in hvm_ud_intercept()

In reply to Jan Beulich

Don't fetch CS explicitly, leverage the fact that hvm_emulate_prepare() already does (and that hvm_virtual_to_linear_addr() doesn't alter it).

At once increase the length passed to hvm_virtual_to_linear_addr() by
one: There definitely needs to be at least one more opcode byte, and we can avoid missing a wraparound case this way.

xen/arch/x86/hvm/hvm.c +7 -8
@@ -3834,19 +3834,20 @@ void hvm_ud_intercept(struct cpu_user_re
 {
     struct hvm_emulate_ctxt ctxt;
 
+    hvm_emulate_prepare(&ctxt, regs);
+
     if ( opt_hvm_fep )
     {
         struct vcpu *cur = current;
-        struct segment_register cs;
+        const struct segment_register *cs = &ctxt.seg_reg[x86_seg_cs];
         unsigned long addr;
         char sig[5]; /* ud2; .ascii "xen" */
 
-        hvm_get_segment_register(cur, x86_seg_cs, &cs);
-        if ( hvm_virtual_to_linear_addr(x86_seg_cs, &cs, regs->eip,
-                                        sizeof(sig), hvm_access_insn_fetch,
+        if ( hvm_virtual_to_linear_addr(x86_seg_cs, cs, regs->eip,
+                                        sizeof(sig) + 1, hvm_access_insn_fetch,
                                         (hvm_long_mode_enabled(cur) &&
-                                         cs.attr.fields.l) ? 64 :
-                                        cs.attr.fields.db ? 32 : 16, &addr) &&
+                                         cs->attr.fields.l) ? 64 :
+                                        cs->attr.fields.db ? 32 : 16, &addr) &&
              (hvm_fetch_from_guest_virt_nofault(sig, addr, sizeof(sig),
                                                 0) == HVMCOPY_okay) &&
              (memcmp(sig, "\xf\xbxen", sizeof(sig)) == 0) )
@@ -3856,8 +3857,6 @@ void hvm_ud_intercept(struct cpu_user_re
         }
     }
 
-    hvm_emulate_prepare(&ctxt, regs);
-
     switch ( hvm_emulate_one(&ctxt) )
     {
     case X86EMUL_UNHANDLEABLE:

x86/HVM: re-order operations in hvm_ud_intercept()

Don't fetch CS explicitly, leverage the fact that hvm_emulate_prepare() already does (and that hvm_virtual_to_linear_addr() doesn't alter it).

At once increase the length passed to hvm_virtual_to_linear_addr() by
one: There definitely needs to be at least one more opcode byte, and we can avoid missing a wraparound case this way.

xen/arch/x86/hvm/hvm.c +7 -8
@@ -3834,19 +3834,20 @@ void hvm_ud_intercept(struct cpu_user_re
 {
     struct hvm_emulate_ctxt ctxt;
 
+    hvm_emulate_prepare(&ctxt, regs);
+
     if ( opt_hvm_fep )
     {
         struct vcpu *cur = current;
-        struct segment_register cs;
+        const struct segment_register *cs = &ctxt.seg_reg[x86_seg_cs];
         unsigned long addr;
         char sig[5]; /* ud2; .ascii "xen" */
 
-        hvm_get_segment_register(cur, x86_seg_cs, &cs);
-        if ( hvm_virtual_to_linear_addr(x86_seg_cs, &cs, regs->eip,
-                                        sizeof(sig), hvm_access_insn_fetch,
+        if ( hvm_virtual_to_linear_addr(x86_seg_cs, cs, regs->eip,
+                                        sizeof(sig) + 1, hvm_access_insn_fetch,
                                         (hvm_long_mode_enabled(cur) &&
-                                         cs.attr.fields.l) ? 64 :
-                                        cs.attr.fields.db ? 32 : 16, &addr) &&
+                                         cs->attr.fields.l) ? 64 :
+                                        cs->attr.fields.db ? 32 : 16, &addr) &&
              (hvm_fetch_from_guest_virt_nofault(sig, addr, sizeof(sig),
                                                 0) == HVMCOPY_okay) &&
              (memcmp(sig, "\xf\xbxen", sizeof(sig)) == 0) )
@@ -3856,8 +3857,6 @@ void hvm_ud_intercept(struct cpu_user_re
         }
     }
 
-    hvm_emulate_prepare(&ctxt, regs);
-
     switch ( hvm_emulate_one(&ctxt) )
     {
     case X86EMUL_UNHANDLEABLE:

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

#4 lore raw

Re: [PATCH 2/2] x86/HVM: re-order operations in hvm_ud_intercept()

Quoted 8 lines · Jan Beulich

Don't fetch CS explicitly, leverage the fact that hvm_emulate_prepare() already does (and that hvm_virtual_to_linear_addr() doesn't alter it).

At once increase the length passed to hvm_virtual_to_linear_addr() by
one: There definitely needs to be at least one more opcode byte, and we can avoid missing a wraparound case this way.

I looked into this when you suggested it, but it latches the wrong eip in the emulation state, and you will end up re-emulating the ud2a instruction, rather than the following instruction.

I would be tempted just to leave this code in its current condition.

~Andrew

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

#5 lore raw

Re: [PATCH 2/2] x86/HVM: re-order operations in hvm_ud_intercept()

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 09.06.16 at 13:34, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich

Don't fetch CS explicitly, leverage the fact that hvm_emulate_prepare() already does (and that hvm_virtual_to_linear_addr() doesn't alter it).

At once increase the length passed to hvm_virtual_to_linear_addr() by
one: There definitely needs to be at least one more opcode byte, and we can avoid missing a wraparound case this way.

Quoted 5 lines
Quoted 1 line

I looked into this when you suggested it, but it latches the wrong eip in the emulation state, and you will end up re-emulating the ud2a instruction, rather than the following instruction.

Where is there any latching of eip? All hvm_emulate_prepare() does is storing the regs pointer.

Jan

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

#6 lore raw

Re: [PATCH 2/2] x86/HVM: re-order operations in hvm_ud_intercept()

Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 09.06.16 at 13:34, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich

Don't fetch CS explicitly, leverage the fact that hvm_emulate_prepare() already does (and that hvm_virtual_to_linear_addr() doesn't alter it).

At once increase the length passed to hvm_virtual_to_linear_addr() by
one: There definitely needs to be at least one more opcode byte, and we can avoid missing a wraparound case this way.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

I looked into this when you suggested it, but it latches the wrong eip in the emulation state, and you will end up re-emulating the ud2a instruction, rather than the following instruction.

Where is there any latching of eip? All hvm_emulate_prepare() does is storing the regs pointer.

Oh - so it does. I clearly looked over it too quickly.

What wraparound issue are you referring to? Adding 1 will cause incorrect behaviour when the emulation prefix ends at the segment limit.

~Andrew

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

#7 lore raw

Re: [PATCH 2/2] x86/HVM: re-order operations in hvm_ud_intercept()

Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 09.06.16 at 16:06, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 09.06.16 at 13:34, <[email protected]> wrote:
Quoted 8 lines · Jan Beulich

Don't fetch CS explicitly, leverage the fact that hvm_emulate_prepare() already does (and that hvm_virtual_to_linear_addr() doesn't alter it).

At once increase the length passed to hvm_virtual_to_linear_addr() by
one: There definitely needs to be at least one more opcode byte, and we can avoid missing a wraparound case this way.

I looked into this when you suggested it, but it latches the wrong eip in the emulation state, and you will end up re-emulating the ud2a instruction, rather than the following instruction.

Quoted 7 lines
Quoted 2 lines

Where is there any latching of eip? All hvm_emulate_prepare() does is storing the regs pointer.

Oh - so it does. I clearly looked over it too quickly.

What wraparound issue are you referring to? Adding 1 will cause incorrect behaviour when the emulation prefix ends at the segment limit.

I don't think so: The prefix together with the actual instruction encoding should be viewed as a single instruction, and it crossing the segment limit should #GP. It wrapping at the prefix/encoding boundary is the case that I'm specifically referring to (this case should also #GP, but wouldn't without this adjustment).

Jan

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

#8 lore raw

Re: [PATCH 2/2] x86/HVM: re-order operations in hvm_ud_intercept()

Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 09.06.16 at 16:06, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 09.06.16 at 13:34, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich

Don't fetch CS explicitly, leverage the fact that hvm_emulate_prepare() already does (and that hvm_virtual_to_linear_addr() doesn't alter it).

At once increase the length passed to hvm_virtual_to_linear_addr() by
one: There definitely needs to be at least one more opcode byte, and we can avoid missing a wraparound case this way.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

I looked into this when you suggested it, but it latches the wrong eip in the emulation state, and you will end up re-emulating the ud2a instruction, rather than the following instruction.

Where is there any latching of eip? All hvm_emulate_prepare() does is storing the regs pointer.

Oh - so it does. I clearly looked over it too quickly.

Quoted 7 lines
Quoted 2 lines

What wraparound issue are you referring to? Adding 1 will cause incorrect behaviour when the emulation prefix ends at the segment limit.

I don't think so: The prefix together with the actual instruction encoding should be viewed as a single instruction, and it crossing the segment limit should #GP. It wrapping at the prefix/encoding boundary is the case that I'm specifically referring to (this case should also #GP, but wouldn't without this adjustment).

But the force emulation prefix specifically doesn't behave like other prefixes.

It doesn't count towards the 15 byte instruction limit, and if the emulated instruction does fault, we want the fault pointing at the emulated instruction, not the force prefix. We should avoid making any link.

~Andrew

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

#9 lore raw

Re: [PATCH 2/2] x86/HVM: re-order operations in hvm_ud_intercept()

Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 09.06.16 at 16:27, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 09.06.16 at 16:06, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 09.06.16 at 13:34, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich

Don't fetch CS explicitly, leverage the fact that hvm_emulate_prepare() already does (and that hvm_virtual_to_linear_addr() doesn't alter it).

At once increase the length passed to hvm_virtual_to_linear_addr() by
one: There definitely needs to be at least one more opcode byte, and we can avoid missing a wraparound case this way.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

I looked into this when you suggested it, but it latches the wrong eip in the emulation state, and you will end up re-emulating the ud2a instruction, rather than the following instruction.

Where is there any latching of eip? All hvm_emulate_prepare() does is storing the regs pointer.

Oh - so it does. I clearly looked over it too quickly.

Quoted 7 lines
Quoted 2 lines

What wraparound issue are you referring to? Adding 1 will cause incorrect behaviour when the emulation prefix ends at the segment limit.

I don't think so: The prefix together with the actual instruction encoding should be viewed as a single instruction, and it crossing the segment limit should #GP. It wrapping at the prefix/encoding boundary is the case that I'm specifically referring to (this case should also #GP, but wouldn't without this adjustment).

Quoted 7 lines

But the force emulation prefix specifically doesn't behave like other prefixes.

It doesn't count towards the 15 byte instruction limit, and if the emulated instruction does fault, we want the fault pointing at the emulated instruction, not the force prefix. We should avoid making any link.

Well, are you saying placing such a prefix right below the boundary of a flat segment is _expected_ to get the instruction at address 0 emulated? I don't think I could buy that. The patch makes no other connection between prefix and actual insn. And #GP because of such a boundary condition should imo point at the prefix; only all faults associated with the actual insn should point there.

Jan

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

#10 lore raw

Ping: Re: [PATCH 2/2] x86/HVM: re-order operations in hvm_ud_intercept()

Quoted 38 lines
Quoted 30 lines
Quoted 2 lines · On 09.06.16 at 17:05, <[email protected]> wrote:
Quoted 1 line · On 09.06.16 at 16:27, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 09.06.16 at 16:06, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 09.06.16 at 13:34, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich

Don't fetch CS explicitly, leverage the fact that hvm_emulate_prepare() already does (and that hvm_virtual_to_linear_addr() doesn't alter it).

At once increase the length passed to hvm_virtual_to_linear_addr() by
one: There definitely needs to be at least one more opcode byte, and we can avoid missing a wraparound case this way.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

I looked into this when you suggested it, but it latches the wrong eip in the emulation state, and you will end up re-emulating the ud2a instruction, rather than the following instruction.

Where is there any latching of eip? All hvm_emulate_prepare() does is storing the regs pointer.

Oh - so it does. I clearly looked over it too quickly.

Quoted 7 lines
Quoted 2 lines

What wraparound issue are you referring to? Adding 1 will cause incorrect behaviour when the emulation prefix ends at the segment limit.

I don't think so: The prefix together with the actual instruction encoding should be viewed as a single instruction, and it crossing the segment limit should #GP. It wrapping at the prefix/encoding boundary is the case that I'm specifically referring to (this case should also #GP, but wouldn't without this adjustment).

Quoted 7 lines

But the force emulation prefix specifically doesn't behave like other prefixes.

It doesn't count towards the 15 byte instruction limit, and if the emulated instruction does fault, we want the fault pointing at the emulated instruction, not the force prefix. We should avoid making any link.

Quoted 6 lines

Well, are you saying placing such a prefix right below the boundary of a flat segment is _expected_ to get the instruction at address 0 emulated? I don't think I could buy that. The patch makes no other connection between prefix and actual insn. And #GP because of such a boundary condition should imo point at the prefix; only all faults associated with the actual insn should point there.

Ping?

Jan

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

Reviewed-by
#11 lore raw

Re: [PATCH 2/2] x86/HVM: re-order operations in hvm_ud_intercept()

In reply to Jan Beulich
Quoted 35 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 09.06.16 at 16:27, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 09.06.16 at 16:06, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 09.06.16 at 13:34, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich

Don't fetch CS explicitly, leverage the fact that hvm_emulate_prepare() already does (and that hvm_virtual_to_linear_addr() doesn't alter it).

At once increase the length passed to hvm_virtual_to_linear_addr() by
one: There definitely needs to be at least one more opcode byte, and we can avoid missing a wraparound case this way.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

I looked into this when you suggested it, but it latches the wrong eip in the emulation state, and you will end up re-emulating the ud2a instruction, rather than the following instruction.

Where is there any latching of eip? All hvm_emulate_prepare() does is storing the regs pointer.

Oh - so it does. I clearly looked over it too quickly.

Quoted 7 lines
Quoted 2 lines

What wraparound issue are you referring to? Adding 1 will cause incorrect behaviour when the emulation prefix ends at the segment limit.

I don't think so: The prefix together with the actual instruction encoding should be viewed as a single instruction, and it crossing the segment limit should #GP. It wrapping at the prefix/encoding boundary is the case that I'm specifically referring to (this case should also #GP, but wouldn't without this adjustment).

Quoted 7 lines

But the force emulation prefix specifically doesn't behave like other prefixes.

It doesn't count towards the 15 byte instruction limit, and if the emulated instruction does fault, we want the fault pointing at the emulated instruction, not the force prefix. We should avoid making any link.

Quoted 6 lines

Well, are you saying placing such a prefix right below the boundary of a flat segment is _expected_ to get the instruction at address 0 emulated? I don't think I could buy that. The patch makes no other connection between prefix and actual insn. And #GP because of such a boundary condition should imo point at the prefix; only all faults associated with the actual insn should point there.

Ok. That sounds reasonable. Would it be possible to add a small comment to the code? Even with the commit message, I was confused as to the nature of the +1.

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

#12 lore raw

Re: [PATCH 2/2] x86/HVM: re-order operations in hvm_ud_intercept()

Quoted 44 lines
Quoted 1 line
Quoted 1 line · On 17.06.16 at 11:37, <[email protected]> wrote:
Quoted 35 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 09.06.16 at 16:27, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 09.06.16 at 16:06, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 09.06.16 at 13:34, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich

Don't fetch CS explicitly, leverage the fact that hvm_emulate_prepare() already does (and that hvm_virtual_to_linear_addr() doesn't alter it).

At once increase the length passed to hvm_virtual_to_linear_addr() by
one: There definitely needs to be at least one more opcode byte, and we can avoid missing a wraparound case this way.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

I looked into this when you suggested it, but it latches the wrong eip in the emulation state, and you will end up re-emulating the ud2a instruction, rather than the following instruction.

Where is there any latching of eip? All hvm_emulate_prepare() does is storing the regs pointer.

Oh - so it does. I clearly looked over it too quickly.

Quoted 7 lines
Quoted 2 lines

What wraparound issue are you referring to? Adding 1 will cause incorrect behaviour when the emulation prefix ends at the segment limit.

I don't think so: The prefix together with the actual instruction encoding should be viewed as a single instruction, and it crossing the segment limit should #GP. It wrapping at the prefix/encoding boundary is the case that I'm specifically referring to (this case should also #GP, but wouldn't without this adjustment).

Quoted 7 lines

But the force emulation prefix specifically doesn't behave like other prefixes.

It doesn't count towards the 15 byte instruction limit, and if the emulated instruction does fault, we want the fault pointing at the emulated instruction, not the force prefix. We should avoid making any link.

Quoted 6 lines

Well, are you saying placing such a prefix right below the boundary of a flat segment is _expected_ to get the instruction at address 0 emulated? I don't think I could buy that. The patch makes no other connection between prefix and actual insn. And #GP because of such a boundary condition should imo point at the prefix; only all faults associated with the actual insn should point there.

Quoted 3 lines

Ok. That sounds reasonable. Would it be possible to add a small comment to the code? Even with the commit message, I was confused as to the nature of the +1.

+ /*

+         * Note that in the call below we pass 1 more than the signature
+         * size, to guard against the overall code sequence wrapping between
+         * "prefix" and actual instruction. There's necessarily at least one
+         * actual instruction byte required, so this won't cause failure on
+         * legitimate uses.
+         */
Quoted 1 line

Thanks, Jan

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