vVMX: VMX insn handling
v2 cover vVMX: VMX insn handling
Originally, while dealing with some other patch, I merely noticed the issue addressed by patch 1. That quickly grew then, though.
1: adjust reg_read() for 32-bit guests
2: adjust reg_write() for 32-bit guests
3: prefer hvm_long_mode_active() in decode_vmx_inst() 4: operand size in decode_vmx_inst()
5: address size in decode_vmx_inst()
Jan
PATCH v2 1/5 vVMX: adjust reg_read() for 32-bit guests
Using the full 64-bit register values is wrong in this case; especially soon after a mode switch from long mode to 32-bit one upper halves of registers may continue to be non-zero.
---
Note that the affected VMX insns are invalid to use from compatibility mode, and hence the more expensive vmx_guest_x86_mode() doesn't need using here. (VMCALL and VMFUNC, which are permitted in compatibility mode, aren't taking this path. In fact both aren't dealt with at all [explicitly] in vvmx.c.)
---
v2: Add code comment.
xen/arch/x86/hvm/vmx/vvmx.c +12 -1
@@ -360,7 +360,18 @@ enum vmx_insn_errno set_vvmcs_real_safe(static unsigned long reg_read (struct cpu_user_regs *regs,unsigned int index) { -return *decode_gpr (regs, index); +unsigned long val = *decode_gpr (regs, index); + +/* +* Outside of 64-bit mode, zero-extend the result from 32 bits, like +* hardware would. +* NB: A long-mode check is sufficient here, as insns this logic is used +* for will #UD in compatibility mode (and hence not make it here). +*/ +if ( !hvm_long_mode_active (current) ) + val = (uint32_t )val; + +return val; } static void reg_write(struct cpu_user_regs *regs,
PATCH v2 2/5 vVMX: adjust reg_write() for 32-bit guests
In reply to Jan BeulichUsing the full 64-bit register values is slightly wrong in this case; 32-bit writes of registers would normally zero-extend the value to 64 bits. The difference may be observable after switching (back) to 64-bit mode (even if as per the spec upper halves of registers are undefined after a mode switch, in reality they retain their values).
---
Note that the sole affected VMX insn (VMREAD) is invalid to use from compatibility mode, and hence the more expensive vmx_guest_x86_mode() doesn't need using here.
---
v2: Add code comment.
xen/arch/x86/hvm/vmx/vvmx.c +8 -0
@@ -378,6 +378,14 @@ static void reg_write(struct cpu_user_re unsigned int index, unsigned long value) { +/* +* Outside of 64-bit mode, make sure we don't store non-zero upper halves +* in GPRs. +* NB: A long-mode check is sufficient here, as insns this logic is used +* for will #UD in compatibility mode (and hence not make it here). +*/ +if ( !hvm_long_mode_active (current) ) + value = (uint32_t )value; *decode_gpr (regs, index) = value; }
PATCH v2 3/5 vVMX: prefer hvm_long_mode_active() in decode_vmx_inst()
In reply to Jan BeulichAll affected VMX insns are invalid to use from compatibility mode, and hence the more expensive vmx_guest_x86_mode() doesn't need using here.
xen/arch/x86/hvm/vmx/vvmx.c +1 -1
@@ -430,7 +430,7 @@ static int decode_vmx_inst(struct cpu_us }else { -bool mode_64bit =vmx_guest_x86_mode (v) ==X86_MODE_64BIT ; +bool mode_64bit =hvm_long_mode_active (v); decode->type =VMX_INST_MEMREG_TYPE_MEMORY ;
PATCH v2 4/5 vVMX: operand size in decode_vmx_inst()
In reply to Jan BeulichAddress size is entirely irrelevant to operand size determination; For VMREAD and VMWRITE outside of 64-bit mode operand size is 32 bits, while in 64-bit mode it's (naturally) 64 bits. For all other insns it's 64 bits (a physical address) or 128 bits (INVEPT, INVVPID). To limit the amount of change here, keep the latter at reading only 64 bits from guest space.
---
Beyond the wrong operand handling for INVEPT and INVVPID, the latter also doesn't even have the part read checked to have bits 16 and above all clear.
xen/arch/x86/hvm/vmx/vvmx.c +15 -12
@@ -411,13 +411,13 @@ static inline u32 __n2_secondary_exec_costatic int decode_vmx_inst (struct cpu_user_regs *regs,struct vmx_inst_decoded *decode, -unsigned long *poperandS) +unsigned long *poperandS,unsigned int size) {struct vcpu *v = current;union vmx_inst_info info;struct segment_register seg;unsigned long base, index, seg_base, disp, offset; -int scale, size; +unsigned int scale;__vmread (VMX_INSTRUCTION_INFO , &offset); info.word = offset;
@@ -449,7 +449,8 @@ static int decode_vmx_inst(struct cpu_us__vmread (EXIT_QUALIFICATION , &disp); - size =1 << (info.fields.addr_size +1 ); +if ( !size ) + size =4 << mode_64bit; offset = base + index * scale + disp; base = !mode_64bit || info.fields.segment >= x86_seg_fs ?
@@ -464,7 +465,9 @@ static int decode_vmx_inst(struct cpu_usif ( poperandS !=NULL ) {pagefault_info_t pfinfo; -int rc =hvm_copy_from_guest_linear (poperandS, base, size, +int rc =hvm_copy_from_guest_linear (poperandS, base, + min_t(unsigned int, size, +sizeof (*poperandS)),0 , &pfinfo);if ( rc == HVMTRANS_bad_linear_to_gfn )
@@ -1561,7 +1564,7 @@ static int nvmx_handle_vmxon(struct cpu_uint32_t nvmcs_revid;int rc; - rc =decode_vmx_inst (regs, &decode, &gpa); + rc =decode_vmx_inst (regs, &decode, &gpa,sizeof (gpa));if ( rc !=X86EMUL_OKAY )return rc;
@@ -1788,7 +1791,7 @@ static int nvmx_handle_vmptrld(struct cpunsigned long gpa =0 ;int rc; - rc =decode_vmx_inst (regs, &decode, &gpa); + rc =decode_vmx_inst (regs, &decode, &gpa,sizeof (gpa));if ( rc !=X86EMUL_OKAY )return rc;
@@ -1865,7 +1868,7 @@ static int nvmx_handle_vmptrst(struct cpunsigned long gpa =0 ;int rc; - rc =decode_vmx_inst (regs, &decode, &gpa); + rc =decode_vmx_inst (regs, &decode, &gpa,sizeof (gpa));if ( rc !=X86EMUL_OKAY )return rc;
@@ -1891,7 +1894,7 @@ static int nvmx_handle_vmclear(struct cpvoid *vvmcs;int rc; - rc =decode_vmx_inst (regs, &decode, &gpa); + rc =decode_vmx_inst (regs, &decode, &gpa,sizeof (gpa));if ( rc !=X86EMUL_OKAY )return rc;
@@ -1953,7 +1956,7 @@ static int nvmx_handle_vmread(struct cpuu64 value =0 ;int rc; - rc =decode_vmx_inst (regs, &decode,NULL ); + rc =decode_vmx_inst (regs, &decode,NULL ,0 );if ( rc !=X86EMUL_OKAY )return rc;
@@ -1996,7 +1999,7 @@ static int nvmx_handle_vmwrite(struct cpenum vmx_insn_errno err;int rc; - rc =decode_vmx_inst (regs, &decode, &operand); + rc =decode_vmx_inst (regs, &decode, &operand,0 );if ( rc !=X86EMUL_OKAY )return rc;
@@ -2038,7 +2041,7 @@ static int nvmx_handle_invept(struct cpuunsigned long eptp;int ret; -if ( (ret =decode_vmx_inst (regs, &decode, &eptp)) !=X86EMUL_OKAY ) +if ( (ret =decode_vmx_inst (regs, &decode, &eptp,16 )) !=X86EMUL_OKAY )return ret; switch ( reg_read(regs, decode.reg2) )
@@ -2066,7 +2069,7 @@ static int nvmx_handle_invvpid(struct cpunsigned long vpid;int ret; -if ( (ret =decode_vmx_inst (regs, &decode, &vpid)) !=X86EMUL_OKAY ) +if ( (ret =decode_vmx_inst (regs, &decode, &vpid,16 )) !=X86EMUL_OKAY )return ret; switch ( reg_read(regs, decode.reg2) )
PATCH v2 5/5 vVMX: address size in decode_vmx_inst()
In reply to Jan BeulichWhile the original use of the address size file in the instruction info provided was wrong, it still wants using: The offset into the designated segment still may need truncating accordingly.
xen/arch/x86/hvm/vmx/vvmx.c +7 -0
@@ -453,6 +453,13 @@ static int decode_vmx_inst(struct cpu_us size =4 << mode_64bit; offset = base + index * scale + disp; +switch ( info.fields.addr_size ) + { +case 0 : offset = (uint16_t )offset;break ; +case 1 : offset = (uint32_t )offset;break ; +default :break ; + } + base = !mode_64bit || info.fields.segment >= x86_seg_fs ? seg_base + offset : offset; if ( offset + size - 1 < offset ||