Big big real mode improvements
v3 cover Big big real mode improvements
This patchset fixes several bugs in the emulator and in vmx invalid guest state emulation. They are sufficient to boot several Linux and Windows guests, so the last patch turns vmx invalid guest state emulation on by default.
The first patch is a little hacky; we should try to emulate svm here by making cpl a state, not a cache.
Changes from v2:
- none, resend since a previous patchset accidentally got mixed in
Changes from v1:
- folded addendum patchset (BSWAP..LTR) into this one
- fix stack_size() miscalculation for 64-bit, ENTER patch
- generate #GP when loading NULL to SS, if CPL!=RPL (noted by Kevin)
- fix SS base for ESP/EBP addressing without SIB
- more testing
Avi Kivity (26):
KVM: VMX: Return correct CPL during transition to protected mode
KVM: Split cpuid register access from computation
KVM: x86 emulator: change ->get_cpuid() accessor to use the x86
semantics
KVM: x86 emulator: emulate cpuid
KVM: x86 emulator: allow loading null SS in long mode
KVM: x86 emulator: fix LIDT/LGDT in long mode
KVM: VMX: Relax check on unusable segment
KVM: VMX: Limit iterations with emulator_invalid_guest_state
KVM: x86 emulator: emulate LEAVE
KVM: x86 emulator: initialize memop
KVM: Fix SS default ESP/EBP based addressing
KVM: x86 emulator: emulate SGDT/SIDT
KVM: VMX: Fix interrupt exit condition during emulation
KVM: VMX: Continue emulating after batch exhausted
KVM: x86 emulator: emulate LAHF
KVM: x86 emulator: fix byte-sized MOVZX/MOVSX
KVM: x86 emulator: split push logic from push opcode emulation
KVM: x86 emulator: implement ENTER
KVM: VMX: Stop invalid guest state emulation on pending event
KVM: VMX: Improve error reporting during invalid guest state
emulation
KVM: x86 emulator: emulate BSWAP
KVM: x86 emulator: emulate LLDT
KVM: x86 emulator: make read_segment_descriptor() return the address
KVM: x86 emulator: make loading TR set the busy bit
KVM: x86 emulator: implement LTR
KVM: VMX: Emulate invalid guest state by default
arch/x86/include/asm/kvm_emulate.h | 6 +- arch/x86/kvm/cpuid.c | 38 ++++-- arch/x86/kvm/cpuid.h | 1 + arch/x86/kvm/emulate.c | 273 +++++++++++++++++++++++++++++-------- arch/x86/kvm/vmx.c | 38 ++++-- arch/x86/kvm/x86.c | 20 +-- 6 files changed, 276 insertions(+), 100 deletions(-)
Signature · 1 line
1.7.11
PATCH v3 1/26 KVM: VMX: Return correct CPL during transition to protected mode
In protected mode, the CPL is defined as the lower two bits of CS, as set by the last far jump. But during the transition to protected mode, there is no last far jump, so we need to return zero (the inherited real mode CPL).
Fix by reading CPL from the cache during the transition. This isn't 100% correct since we don't set the CPL cache on a far jump, but since protected mode transition will always jump to a segment with RPL=0, it will always work.
--- arch/x86/kvm/vmx.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx.c +13 -2
@@ -3175,11 +3175,22 @@ static int __vmx_get_cpl(struct kvm_vcpu *vcpu)static int vmx_get_cpl (struct kvm_vcpu *vcpu) { +struct vcpu_vmx *vmx =to_vmx (vcpu); + +/* +* If we enter real mode with cs.sel & 3 != 0, the normal CPL calculations +* fail; use the cache instead. +*/ +if (unlikely (vmx->emulation_required && emulate_invalid_guest_state)) { +return vmx->cpl; + } +if (!test_bit (VCPU_EXREG_CPL , (ulong *)&vcpu->arch.regs_avail)) {__set_bit (VCPU_EXREG_CPL , (ulong *)&vcpu->arch.regs_avail); -to_vmx (vcpu)->cpl =__vmx_get_cpl (vcpu); + vmx->cpl =__vmx_get_cpl (vcpu); } -return to_vmx (vcpu)->cpl; + +return vmx->cpl; }
Signature · 1 line
1.7.11
PATCH v3 2/26 KVM: Split cpuid register access from computation
In reply to Avi KivityIntroduce kvm_cpuid() to perform the leaf limit check and calculate register values, and let kvm_emulate_cpuid() just handle reading and writing the registers from/to the vcpu. This allows us to reuse kvm_cpuid() in a context where directly reading and writing registers is not desired.
--- arch/x86/kvm/cpuid.c | 38 ++++++++++++++++++++++++-------------- arch/x86/kvm/cpuid.h | 1 + 2 files changed, 25 insertions(+), 14 deletions(-)
arch/x86/kvm/cpuid.c +24 -14
@@ -639,33 +639,43 @@ static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu,return kvm_find_cpuid_entry (vcpu, maxlevel->eax, index); } -void kvm_emulate_cpuid (struct kvm_vcpu *vcpu) +void kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx) { -u32 function, index; +u32 function = *eax, index = *ecx;struct kvm_cpuid_entry2 *best; - function =kvm_register_read (vcpu,VCPU_REGS_RAX ); - index =kvm_register_read (vcpu,VCPU_REGS_RCX ); -kvm_register_write (vcpu,VCPU_REGS_RAX ,0 ); -kvm_register_write (vcpu,VCPU_REGS_RBX ,0 ); -kvm_register_write (vcpu,VCPU_REGS_RCX ,0 ); -kvm_register_write (vcpu,VCPU_REGS_RDX ,0 ); best =kvm_find_cpuid_entry (vcpu, function, index);if (!best) best =check_cpuid_limit (vcpu, function, index);if (best) { -kvm_register_write (vcpu,VCPU_REGS_RAX , best->eax); -kvm_register_write (vcpu,VCPU_REGS_RBX , best->ebx); -kvm_register_write (vcpu,VCPU_REGS_RCX , best->ecx); -kvm_register_write (vcpu,VCPU_REGS_RDX , best->edx); - } - kvm_x86_ops->skip_emulated_instruction (vcpu); + *eax = best->eax; + *ebx = best->ebx; + *ecx = best->ecx; + *edx = best->edx; + }else + *eax = *ebx = *ecx = *edx =0 ; +trace_kvm_cpuid (function,kvm_register_read (vcpu,VCPU_REGS_RAX ),kvm_register_read (vcpu,VCPU_REGS_RBX ),kvm_register_read (vcpu,VCPU_REGS_RCX ),kvm_register_read (vcpu,VCPU_REGS_RDX )); } + +void kvm_emulate_cpuid (struct kvm_vcpu *vcpu) +{ +u32 function, eax, ebx, ecx, edx; + + function = eax =kvm_register_read (vcpu,VCPU_REGS_RAX ); + ecx =kvm_register_read (vcpu,VCPU_REGS_RCX ); +kvm_cpuid (vcpu, &eax, &ebx, &ecx, &edx); +kvm_register_write (vcpu,VCPU_REGS_RAX , eax); +kvm_register_write (vcpu,VCPU_REGS_RBX , ebx); +kvm_register_write (vcpu,VCPU_REGS_RCX , ecx); +kvm_register_write (vcpu,VCPU_REGS_RDX , edx); + kvm_x86_ops->skip_emulated_instruction (vcpu); +trace_kvm_cpuid (function, eax, ebx, ecx, edx); +}EXPORT_SYMBOL_GPL (kvm_emulate_cpuid);
arch/x86/kvm/cpuid.h +1 -0
@@ -17,6 +17,7 @@ int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,int kvm_vcpu_ioctl_get_cpuid2 (struct kvm_vcpu *vcpu,struct kvm_cpuid2 *cpuid,struct kvm_cpuid_entry2 __user *entries); +void kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx);static inline bool guest_cpuid_has_xsave (struct kvm_vcpu *vcpu)
Signature · 1 line
1.7.11
Re: [PATCH v3 02/26] KVM: Split cpuid register access from computation
Quoted 63 lines · Avi Kivity
Introduce
kvm_cpuid()to perform the leaf limit check and calculate register values, and letkvm_emulate_cpuid()just handle reading and writing the registers from/to the vcpu. This allows us to reusekvm_cpuid()in a context where directly reading and writing registers is not desired.Signed-off-by Avi Kivity <[email protected]>--- arch/x86/kvm/cpuid.c | 38 ++++++++++++++++++++++++-------------- arch/x86/kvm/cpuid.h | 1 + 2 files changed, 25 insertions(+), 14 deletions(-)arch/x86/kvm/cpuid.c +14
-14@@ -639,33 +639,43 @@ static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu,return kvm_find_cpuid_entry (vcpu, maxlevel->eax, index); } -void kvm_emulate_cpuid (struct kvm_vcpu *vcpu) +void kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx) { -u32 function, index; +u32 function = *eax, index = *ecx;struct kvm_cpuid_entry2 *best; - function =kvm_register_read (vcpu,VCPU_REGS_RAX ); - index =kvm_register_read (vcpu,VCPU_REGS_RCX ); -kvm_register_write (vcpu,VCPU_REGS_RAX ,0 ); -kvm_register_write (vcpu,VCPU_REGS_RBX ,0 ); -kvm_register_write (vcpu,VCPU_REGS_RCX ,0 ); -kvm_register_write (vcpu,VCPU_REGS_RDX ,0 ); best =kvm_find_cpuid_entry (vcpu, function, index);if (!best) best =check_cpuid_limit (vcpu, function, index);if (best) { -kvm_register_write (vcpu,VCPU_REGS_RAX , best->eax); -kvm_register_write (vcpu,VCPU_REGS_RBX , best->ebx); -kvm_register_write (vcpu,VCPU_REGS_RCX , best->ecx); -kvm_register_write (vcpu,VCPU_REGS_RDX , best->edx); - } - kvm_x86_ops->skip_emulated_instruction (vcpu); + *eax = best->eax; + *ebx = best->ebx; + *ecx = best->ecx; + *edx = best->edx; + }else + *eax = *ebx = *ecx = *edx =0 ; +trace_kvm_cpuid (function,kvm_register_read (vcpu,VCPU_REGS_RAX ),kvm_register_read (vcpu,VCPU_REGS_RBX ),kvm_register_read (vcpu,VCPU_REGS_RCX ),kvm_register_read (vcpu,VCPU_REGS_RDX )); } + +void kvm_emulate_cpuid (struct kvm_vcpu *vcpu) +{ +u32 function, eax, ebx, ecx, edx; +
arch/x86/kvm/cpuid.c static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu, · line 680
Quoted 10 lines
arch/x86/kvm/cpuid.c +10
-0+ function = eax =kvm_register_read (vcpu,VCPU_REGS_RAX ); + ecx =kvm_register_read (vcpu,VCPU_REGS_RCX ); +kvm_cpuid (vcpu, &eax, &ebx, &ecx, &edx); +kvm_register_write (vcpu,VCPU_REGS_RAX , eax); +kvm_register_write (vcpu,VCPU_REGS_RBX , ebx); +kvm_register_write (vcpu,VCPU_REGS_RCX , ecx); +kvm_register_write (vcpu,VCPU_REGS_RDX , edx); + kvm_x86_ops->skip_emulated_instruction (vcpu); +trace_kvm_cpuid (function, eax, ebx, ecx, edx); +}
Tracing is duplicated. Is that intented?
Re: [PATCH v3 02/26] KVM: Split cpuid register access from computation
Quoted 68 lines · Marcelo Tosatti
Quoted 67 lines · Avi Kivity
Introduce
kvm_cpuid()to perform the leaf limit check and calculate register values, and letkvm_emulate_cpuid()just handle reading and writing the registers from/to the vcpu. This allows us to reusekvm_cpuid()in a context where directly reading and writing registers is not desired.Signed-off-by Avi Kivity <[email protected]>--- arch/x86/kvm/cpuid.c | 38 ++++++++++++++++++++++++-------------- arch/x86/kvm/cpuid.h | 1 + 2 files changed, 25 insertions(+), 14 deletions(-)arch/x86/kvm/cpuid.c +18
-14@@ -639,33 +639,43 @@ static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu,return kvm_find_cpuid_entry (vcpu, maxlevel->eax, index); } -void kvm_emulate_cpuid (struct kvm_vcpu *vcpu) +void kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx) { -u32 function, index; +u32 function = *eax, index = *ecx;struct kvm_cpuid_entry2 *best; - function =kvm_register_read (vcpu,VCPU_REGS_RAX ); - index =kvm_register_read (vcpu,VCPU_REGS_RCX ); -kvm_register_write (vcpu,VCPU_REGS_RAX ,0 ); -kvm_register_write (vcpu,VCPU_REGS_RBX ,0 ); -kvm_register_write (vcpu,VCPU_REGS_RCX ,0 ); -kvm_register_write (vcpu,VCPU_REGS_RDX ,0 ); best =kvm_find_cpuid_entry (vcpu, function, index);if (!best) best =check_cpuid_limit (vcpu, function, index);if (best) { -kvm_register_write (vcpu,VCPU_REGS_RAX , best->eax); -kvm_register_write (vcpu,VCPU_REGS_RBX , best->ebx); -kvm_register_write (vcpu,VCPU_REGS_RCX , best->ecx); -kvm_register_write (vcpu,VCPU_REGS_RDX , best->edx); - } - kvm_x86_ops->skip_emulated_instruction (vcpu); + *eax = best->eax; + *ebx = best->ebx; + *ecx = best->ecx; + *edx = best->edx; + }else + *eax = *ebx = *ecx = *edx =0 ; +trace_kvm_cpuid (function,kvm_register_read (vcpu,VCPU_REGS_RAX ),kvm_register_read (vcpu,VCPU_REGS_RBX ),kvm_register_read (vcpu,VCPU_REGS_RCX ),kvm_register_read (vcpu,VCPU_REGS_RDX )); } + +void kvm_emulate_cpuid (struct kvm_vcpu *vcpu) +{ +u32 function, eax, ebx, ecx, edx; + + function = eax =kvm_register_read (vcpu,VCPU_REGS_RAX ); + ecx =kvm_register_read (vcpu,VCPU_REGS_RCX ); +kvm_cpuid (vcpu, &eax, &ebx, &ecx, &edx); +kvm_register_write (vcpu,VCPU_REGS_RAX , eax);
Quoted 8 lines
arch/x86/kvm/cpuid.c static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu, · line 680
Quoted 6 lines
arch/x86/kvm/cpuid.c +6
-0+kvm_register_write (vcpu,VCPU_REGS_RBX , ebx); +kvm_register_write (vcpu,VCPU_REGS_RCX , ecx); +kvm_register_write (vcpu,VCPU_REGS_RDX , edx); + kvm_x86_ops->skip_emulated_instruction (vcpu); +trace_kvm_cpuid (function, eax, ebx, ecx, edx); +}Tracing is duplicated. Is that intented?
The first instance is bogus, since it's reading vcpu registers which might not have been the input to the function. I'll remove it.
Signature · 1 line
error compiling committee.c: too many arguments to function
PATCH v3 2/26 KVM: Split cpuid register access from computatio
Introduce kvm_cpuid() to perform the leaf limit check and calculate register values, and let kvm_emulate_cpuid() just handle reading and writing the registers from/to the vcpu. This allows us to reuse kvm_cpuid() in a context where directly reading and writing registers is not desired.
---
v3.1: drop duplicate trace
arch/x86/kvm/cpuid.c | 40 ++++++++++++++++++++++------------------ arch/x86/kvm/cpuid.h | 1 + 2 files changed, 23 insertions(+), 18 deletions(-)
arch/x86/kvm/cpuid.c +22 -18
@@ -640,33 +640,37 @@ static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu,return kvm_find_cpuid_entry (vcpu, maxlevel->eax, index); } -void kvm_emulate_cpuid (struct kvm_vcpu *vcpu) +void kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx) { -u32 function, index; +u32 function = *eax, index = *ecx;struct kvm_cpuid_entry2 *best; - function =kvm_register_read (vcpu,VCPU_REGS_RAX ); - index =kvm_register_read (vcpu,VCPU_REGS_RCX ); -kvm_register_write (vcpu,VCPU_REGS_RAX ,0 ); -kvm_register_write (vcpu,VCPU_REGS_RBX ,0 ); -kvm_register_write (vcpu,VCPU_REGS_RCX ,0 ); -kvm_register_write (vcpu,VCPU_REGS_RDX ,0 ); best =kvm_find_cpuid_entry (vcpu, function, index);if (!best) best =check_cpuid_limit (vcpu, function, index);if (best) { -kvm_register_write (vcpu,VCPU_REGS_RAX , best->eax); -kvm_register_write (vcpu,VCPU_REGS_RBX , best->ebx); -kvm_register_write (vcpu,VCPU_REGS_RCX , best->ecx); -kvm_register_write (vcpu,VCPU_REGS_RDX , best->edx); - } + *eax = best->eax; + *ebx = best->ebx; + *ecx = best->ecx; + *edx = best->edx; + }else + *eax = *ebx = *ecx = *edx =0 ; +} + +void kvm_emulate_cpuid (struct kvm_vcpu *vcpu) +{ +u32 function, eax, ebx, ecx, edx; + + function = eax =kvm_register_read (vcpu,VCPU_REGS_RAX ); + ecx =kvm_register_read (vcpu,VCPU_REGS_RCX ); +kvm_cpuid (vcpu, &eax, &ebx, &ecx, &edx); +kvm_register_write (vcpu,VCPU_REGS_RAX , eax); +kvm_register_write (vcpu,VCPU_REGS_RBX , ebx); +kvm_register_write (vcpu,VCPU_REGS_RCX , ecx); +kvm_register_write (vcpu,VCPU_REGS_RDX , edx); kvm_x86_ops->skip_emulated_instruction (vcpu); -trace_kvm_cpuid (function, -kvm_register_read (vcpu,VCPU_REGS_RAX ), -kvm_register_read (vcpu,VCPU_REGS_RBX ), -kvm_register_read (vcpu,VCPU_REGS_RCX ), -kvm_register_read (vcpu,VCPU_REGS_RDX )); +trace_kvm_cpuid (function, eax, ebx, ecx, edx); }EXPORT_SYMBOL_GPL (kvm_emulate_cpuid);
arch/x86/kvm/cpuid.h +1 -0
@@ -17,6 +17,7 @@ int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,int kvm_vcpu_ioctl_get_cpuid2 (struct kvm_vcpu *vcpu,struct kvm_cpuid2 *cpuid,struct kvm_cpuid_entry2 __user *entries); +void kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx);static inline bool guest_cpuid_has_xsave (struct kvm_vcpu *vcpu)
Signature · 1 line
1.7.11
PATCH v3 3/26 KVM: x86 emulator: change ->get_cpuid() accessor to use the x86 semantics
In reply to Avi KivityInstead of getting an exact leaf, follow the spec and fall back to the last main leaf instead. This lets us easily emulate the cpuid instruction in the emulator.
--- arch/x86/include/asm/kvm_emulate.h | 4 +-- arch/x86/kvm/emulate.c | 53 +++++++++++++++++++------------------- arch/x86/kvm/x86.c | 20 ++------------ 3 files changed, 30 insertions(+), 47 deletions(-)
arch/x86/include/asm/kvm_emulate.h +2 -2
@@ -192,8 +192,8 @@ struct x86_emulate_ops {struct x86_instruction_info *info, enum x86_intercept_stage stage); -bool (*get_cpuid)(struct x86_emulate_ctxt *ctxt, -u32 *eax,u32 *ebx,u32 *ecx,u32 *edx); +void (*get_cpuid)(struct x86_emulate_ctxt *ctxt, +u32 *eax,u32 *ebx,u32 *ecx,u32 *edx); }; typedef u32 __attribute__((vector_size(16))) sse128_t;
arch/x86/kvm/emulate.c +26 -27
66 lines not on this page · raw message
arch/x86/kvm/x86.c +2 -18
@@ -4297,26 +4297,10 @@ static int emulator_intercept(struct x86_emulate_ctxt *ctxt,return kvm_x86_ops->check_intercept (emul_to_vcpu (ctxt), info, stage); } -static bool emulator_get_cpuid (struct x86_emulate_ctxt *ctxt, +static void emulator_get_cpuid (struct x86_emulate_ctxt *ctxt,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx) { -struct kvm_cpuid_entry2 *cpuid =NULL ; - -if (eax && ecx) - cpuid =kvm_find_cpuid_entry (emul_to_vcpu (ctxt), - *eax, *ecx); - -if (cpuid) { - *eax = cpuid->eax; - *ecx = cpuid->ecx; -if (ebx) - *ebx = cpuid->ebx; -if (edx) - *edx = cpuid->edx; -return true; - } - -return false; +kvm_cpuid (emul_to_vcpu (ctxt), eax, ebx, ecx, edx); }static struct x86_emulate_ops emulate_ops = {
Signature · 1 line
1.7.11
PATCH v3 4/26 KVM: x86 emulator: emulate cpuid
In reply to Avi KivityOpcode 0F A2.
Used by Linux during the mode change trampoline while in a state that is not virtualizable on vmx without unrestricted_guest, so we need to emulate it is emulate_invalid_guest_state=1.
--- arch/x86/kvm/emulate.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +15 -1
@@ -3142,6 +3142,20 @@ static int em_bsr(struct x86_emulate_ctxt *ctxt)return X86EMUL_CONTINUE ; } +static int em_cpuid (struct x86_emulate_ctxt *ctxt) +{ +u32 eax, ebx, ecx, edx; + + eax = ctxt->regs[VCPU_REGS_RAX ]; + ecx = ctxt->regs[VCPU_REGS_RCX ]; + ctxt->ops->get_cpuid (ctxt, &eax, &ebx, &ecx, &edx); + ctxt->regs[VCPU_REGS_RAX ] = eax; + ctxt->regs[VCPU_REGS_RBX ] = ebx; + ctxt->regs[VCPU_REGS_RCX ] = ecx; + ctxt->regs[VCPU_REGS_RDX ] = edx; +return X86EMUL_CONTINUE ; +} +static bool valid_cr (int nr) {switch (nr) {
@@ -3634,7 +3648,7 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)X16 (D (ByteOp | DstMem | SrcNone | ModRM| Mov)),/* 0xA0 - 0xA7 */ I (Stack | Src2FS, em_push_sreg),I (Stack | Src2FS, em_pop_sreg), -DI (ImplicitOps, cpuid),I (DstMem | SrcReg | ModRM | BitOp, em_bt), +II (ImplicitOps, em_cpuid, cpuid),I (DstMem | SrcReg | ModRM | BitOp, em_bt),D (DstMem | SrcReg | Src2ImmByte | ModRM), D(DstMem | SrcReg | Src2CL | ModRM), N, N,/* 0xA8 - 0xAF */
Signature · 1 line
1.7.11
PATCH v3 5/26 KVM: x86 emulator: allow loading null SS in long mode
In reply to Avi KivityNull SS is valid in long mode; allow loading it.
--- arch/x86/kvm/emulate.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
arch/x86/kvm/emulate.c +8 -4
@@ -1324,8 +1324,14 @@ static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, goto load; } -/* NULL selector is not valid for TR, CS and SS */ -if ((seg ==VCPU_SREG_CS || seg ==VCPU_SREG_SS || seg ==VCPU_SREG_TR ) + rpl = selector &3 ; + cpl = ctxt->ops->cpl (ctxt); + +/* NULL selector is not valid for TR, CS and SS (except for long mode) */ +if ((seg ==VCPU_SREG_CS + || (seg ==VCPU_SREG_SS + && (ctxt->mode !=X86EMUL_MODE_PROT64 || rpl != cpl)) + || seg ==VCPU_SREG_TR ) && null_selector) goto exception;
@@ -1352,9 +1358,7 @@ static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, goto exception; } - rpl = selector &3 ; dpl = seg_desc.dpl; - cpl = ctxt->ops->cpl (ctxt);switch (seg) {case VCPU_SREG_SS :
Signature · 1 line
1.7.11
PATCH v3 6/26 KVM: x86 emulator: fix LIDT/LGDT in long mode
In reply to Avi KivityThe operand size for these instructions is 8 bytes in long mode, even without a REX prefix. Set it explicitly.
Triggered while booting Linux with emulate_invalid_guest_state=1.
--- arch/x86/kvm/emulate.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/kvm/emulate.c +4 -0
@@ -2997,6 +2997,8 @@ static int em_lgdt(struct x86_emulate_ctxt *ctxt)struct desc_ptr desc_ptr;int rc; +if (ctxt->mode ==X86EMUL_MODE_PROT64 ) + ctxt->op_bytes =8 ; rc =read_descriptor (ctxt, ctxt->src.addr.mem, &desc_ptr.size, &desc_ptr.address, ctxt->op_bytes);
@@ -3024,6 +3026,8 @@ static int em_lidt(struct x86_emulate_ctxt *ctxt)struct desc_ptr desc_ptr;int rc; +if (ctxt->mode ==X86EMUL_MODE_PROT64 ) + ctxt->op_bytes =8 ; rc =read_descriptor (ctxt, ctxt->src.addr.mem, &desc_ptr.size, &desc_ptr.address, ctxt->op_bytes);
Signature · 1 line
1.7.11
PATCH v3 7/26 KVM: VMX: Relax check on unusable segment
In reply to Avi KivitySome userspace (e.g. QEMU 1.1) munge the d and g bits of segment descriptors, causing us not to recognize them as unusable segments with emulate_invalid_guest_state=1. Relax the check by testing for segment not present (a non-present segment cannot be usable).
--- arch/x86/kvm/vmx.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
arch/x86/kvm/vmx.c +1 -3
@@ -3198,7 +3198,7 @@ static u32 vmx_segment_access_rights(struct kvm_segment *var) {u32 ar; -if (var->unusable) +if (var->unusable || !var->present) ar =1 <<16 ;else { ar = var->type &15 ;
@@ -3210,8 +3210,6 @@ static u32 vmx_segment_access_rights(struct kvm_segment *var) ar |= (var->db &1 ) <<14 ; ar |= (var->g &1 ) <<15 ; } -if (ar ==0 )/* a 0 value means unusable */ - ar =AR_UNUSABLE_MASK ;return ar; }
Signature · 1 line
1.7.11
PATCH v3 8/26 KVM: VMX: Limit iterations with emulator_invalid_guest_state
In reply to Avi KivityOtherwise, if the guest ends up looping, we never exit the srcu critical section, which causes synchronize_srcu() to hang.
--- arch/x86/kvm/vmx.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx.c +2 -1
@@ -4977,11 +4977,12 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)int ret =1 ;u32 cpu_exec_ctrl;bool intr_window_requested; +unsigned count =130 ; cpu_exec_ctrl =vmcs_read32 (CPU_BASED_VM_EXEC_CONTROL ); intr_window_requested = cpu_exec_ctrl &CPU_BASED_VIRTUAL_INTR_PENDING ; -while (!guest_state_valid (vcpu)) { +while (!guest_state_valid (vcpu) && count-- !=0 ) {if (intr_window_requested && (kvm_get_rflags (&vmx->vcpu) &X86_EFLAGS_IF ))return handle_interrupt_window (&vmx->vcpu);
Signature · 1 line
1.7.11
PATCH v3 9/26 KVM: x86 emulator: emulate LEAVE
In reply to Avi KivityOpcode c9; used by some variants of Windows during boot, in big real mode.
--- arch/x86/kvm/emulate.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +24 -1
@@ -433,11 +433,27 @@ static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,return ctxt->ops->intercept (ctxt, &info, stage); } +static void assign_masked (ulong *dest,ulong src,ulong mask) +{ + *dest = (*dest & ~mask) | (src & mask); +} +static inline unsigned long ad_mask (struct x86_emulate_ctxt *ctxt) {return (1UL << (ctxt->ad_bytes <<3 )) -1 ; } +static ulong stack_mask (struct x86_emulate_ctxt *ctxt) +{ +u16 sel; +struct desc_struct ss; + +if (ctxt->mode ==X86EMUL_MODE_PROT64 ) +return ~0UL ; + ctxt->ops->get_segment (ctxt, &sel, &ss,NULL ,VCPU_SREG_SS ); +return ~0U >> ((ss.d ^1 ) *16 );/* d=0: 0xffff; d=1: 0xffffffff */ +} +/* Access/update address held in a register, based on addressing mode. */ static inline unsigned long address_mask (struct x86_emulate_ctxt *ctxt,unsigned long reg)
@@ -1560,6 +1576,13 @@ static int em_popf(struct x86_emulate_ctxt *ctxt)return emulate_popf (ctxt, &ctxt->dst.val, ctxt->op_bytes); } +static int em_leave (struct x86_emulate_ctxt *ctxt) +{ +assign_masked (&ctxt->regs[VCPU_REGS_RSP ], ctxt->regs[VCPU_REGS_RBP ], +stack_mask (ctxt)); +return emulate_pop (ctxt, &ctxt->regs[VCPU_REGS_RBP ], ctxt->op_bytes); +} +static int em_push_sreg (struct x86_emulate_ctxt *ctxt) {int seg = ctxt->src2.val;
@@ -3582,7 +3605,7 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)I (DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),G (ByteOp, group11),G (0 , group11),/* 0xC8 - 0xCF */ -N ,N ,N ,I (ImplicitOps | Stack, em_ret_far), +N ,I (Stack, em_leave),N ,I (ImplicitOps | Stack, em_ret_far),D (ImplicitOps),DI (SrcImmByte, intn),D (ImplicitOps | No64),II (ImplicitOps, em_iret, iret), /* 0xD0 - 0xD7 */
Signature · 1 line
1.7.11
PATCH v3 10/26 KVM: x86 emulator: initialize memop
In reply to Avi Kivitymemop is not initialized; this can lead to a two-byte operation following a 4-byte operation to see garbage values. Usually truncation fixes things fot us later on, but at least in one case (call abs) it doesn't.
Fix by moving memop to the auto-initialized field area.
--- arch/x86/include/asm/kvm_emulate.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/kvm_emulate.h +1 -1
@@ -280,9 +280,9 @@ struct x86_emulate_ctxt {u8 modrm_seg;bool rip_relative;unsigned long _eip; +struct operand memop;/* Fields above regs are cleared together. */ unsigned long regs[NR_VCPU_REGS ]; -struct operand memop;struct operand *memopp;struct fetch_cache fetch;struct read_cache io_read;
Signature · 1 line
1.7.11
PATCH v3 11/26 KVM: Fix SS default ESP/EBP based addressing
In reply to Avi KivityWe correctly default to SS when BP is used as a base in 16-bit address mode, but we don't do that for 32-bit mode.
Fix by adjusting the default to SS when either ESP or EBP is used as the base register.
--- arch/x86/kvm/emulate.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
arch/x86/kvm/emulate.c +14 -3
@@ -974,6 +974,12 @@ static void decode_register_operand(struct x86_emulate_ctxt *ctxt, op->orig_val = op->val; } +static void adjust_modrm_seg (struct x86_emulate_ctxt *ctxt,int base_reg) +{ +if (base_reg ==VCPU_REGS_RSP || base_reg ==VCPU_REGS_RBP ) + ctxt->modrm_seg =VCPU_SREG_SS ; +} +static int decode_modrm (struct x86_emulate_ctxt *ctxt,struct operand *op) {
@@ -1077,15 +1083,20 @@ static int decode_modrm(struct x86_emulate_ctxt *ctxt,if ((base_reg &7 ) ==5 && ctxt->modrm_mod ==0 ) modrm_ea +=insn_fetch (s32, ctxt); -else +else { modrm_ea += ctxt->regs[base_reg]; +adjust_modrm_seg (ctxt, base_reg); + }if (index_reg !=4 ) modrm_ea += ctxt->regs[index_reg] << scale; }else if ((ctxt->modrm_rm &7 ) ==5 && ctxt->modrm_mod ==0 ) {if (ctxt->mode ==X86EMUL_MODE_PROT64 ) ctxt->rip_relative =1 ; - }else - modrm_ea += ctxt->regs[ctxt->modrm_rm]; + }else { + base_reg = ctxt->modrm_rm; + modrm_ea += ctxt->regs[base_reg]; +adjust_modrm_seg (ctxt, base_reg); + }switch (ctxt->modrm_mod) {case 0 :if (ctxt->modrm_rm ==5 )
Signature · 1 line
1.7.11
PATCH v3 12/26 KVM: x86 emulator: emulate SGDT/SIDT
In reply to Avi KivityOpcodes 0F 01 /0 and 0F 01 /1
--- arch/x86/kvm/emulate.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +31 -2
@@ -3026,6 +3026,35 @@ static int em_vmcall(struct x86_emulate_ctxt *ctxt)return X86EMUL_CONTINUE ; } +static int emulate_store_desc_ptr (struct x86_emulate_ctxt *ctxt, +void (*get)(struct x86_emulate_ctxt *ctxt, +struct desc_ptr *ptr)) +{ +struct desc_ptr desc_ptr; + +if (ctxt->mode ==X86EMUL_MODE_PROT64 ) + ctxt->op_bytes =8 ; +get (ctxt, &desc_ptr); +if (ctxt->op_bytes ==2 ) { + ctxt->op_bytes =4 ; + desc_ptr.address &=0x00ffffff ; + } +/* Disable writeback. */ + ctxt->dst.type =OP_NONE ; +return segmented_write (ctxt, ctxt->dst.addr.mem, + &desc_ptr,2 + ctxt->op_bytes); +} + +static int em_sgdt (struct x86_emulate_ctxt *ctxt) +{ +return emulate_store_desc_ptr (ctxt, ctxt->ops->get_gdt); +} + +static int em_sidt (struct x86_emulate_ctxt *ctxt) +{ +return emulate_store_desc_ptr (ctxt, ctxt->ops->get_idt); +} +static int em_lgdt (struct x86_emulate_ctxt *ctxt) {struct desc_ptr desc_ptr;
@@ -3485,8 +3514,8 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt) };static struct group_dual group7 = { { -DI (Mov | DstMem | Priv, sgdt), -DI (Mov | DstMem | Priv, sidt), +II (Mov | DstMem | Priv, em_sgdt, sgdt), +II (Mov | DstMem | Priv, em_sidt, sidt),II (SrcMem | Priv, em_lgdt, lgdt),II (SrcMem | Priv, em_lidt, lidt),II (SrcNone | DstMem | Mov, em_smsw, smsw),N ,
Signature · 1 line
1.7.11
PATCH v3 13/26 KVM: VMX: Fix interrupt exit condition during emulation
In reply to Avi KivityChecking EFLAGS.IF is incorrect as we might be in interrupt shadow. If that is the case, the main loop will notice that and not inject the interrupt, causing an endless loop.
Fix by using vmx_interrupt_allowed() to check if we can inject an interrupt instead.
--- arch/x86/kvm/vmx.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kvm/vmx.c +1 -2
@@ -4983,8 +4983,7 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) intr_window_requested = cpu_exec_ctrl &CPU_BASED_VIRTUAL_INTR_PENDING ;while (!guest_state_valid (vcpu) && count-- !=0 ) { -if (intr_window_requested - && (kvm_get_rflags (&vmx->vcpu) &X86_EFLAGS_IF )) +if (intr_window_requested &&vmx_interrupt_allowed (vcpu))return handle_interrupt_window (&vmx->vcpu); err =emulate_instruction (vcpu,0 );
Signature · 1 line
1.7.11
PATCH v3 14/26 KVM: VMX: Continue emulating after batch exhausted
In reply to Avi KivityIf we return early from an invalid guest state emulation loop, make sure we return to it later if the guest state is still invalid.
--- arch/x86/kvm/vmx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx.c +1 -1
@@ -5002,7 +5002,7 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)schedule (); } - vmx->emulation_required =0 ; + vmx->emulation_required = !guest_state_valid (vcpu); out:return ret; }
Signature · 1 line
1.7.11
PATCH v3 15/26 KVM: x86 emulator: emulate LAHF
In reply to Avi KivityOpcode 9F.
--- arch/x86/kvm/emulate.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +8 -1
@@ -3227,6 +3227,13 @@ static int em_cpuid(struct x86_emulate_ctxt *ctxt)return X86EMUL_CONTINUE ; } +static int em_lahf (struct x86_emulate_ctxt *ctxt) +{ + ctxt->regs[VCPU_REGS_RAX ] &= ~0xff00UL ; + ctxt->regs[VCPU_REGS_RAX ] |= (ctxt->eflags &0xff ) <<8 ; +return X86EMUL_CONTINUE ; +} +static bool valid_cr (int nr) {switch (nr) {
@@ -3622,7 +3629,7 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)D (DstAcc | SrcNone),I (ImplicitOps | SrcAcc, em_cwd),I (SrcImmFAddr | No64, em_call_far),N ,II (ImplicitOps | Stack, em_pushf, pushf), -II (ImplicitOps | Stack, em_popf, popf),N ,N , +II (ImplicitOps | Stack, em_popf, popf),N ,I (ImplicitOps, em_lahf),/* 0xA0 - 0xA7 */ I2bv (DstAcc | SrcMem | Mov | MemAbs, em_mov), I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Signature · 1 line
1.7.11
PATCH v3 16/26 KVM: x86 emulator: fix byte-sized MOVZX/MOVSX
In reply to Avi KivityCommit 2adb5ad9fe1 removed ByteOp from MOVZX/MOVSX, replacing them by SrcMem8, but neglected to fix the dependency in the emulation code on ByteOp. This caused the instruction not to have any effect in some circumstances.
Fix by replacing the check for ByteOp with the equivalent src.op_bytes == 1.
--- arch/x86/kvm/emulate.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +2 -2
@@ -4517,12 +4517,12 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)break ; case 0xb6 ... 0xb7: /* movzx */ ctxt->dst.bytes = ctxt->op_bytes; - ctxt->dst.val = (ctxt->d & ByteOp) ? (u8 ) ctxt->src.val + ctxt->dst.val = (ctxt->src.bytes ==1 ) ? (u8 ) ctxt->src.val : (u16 ) ctxt->src.val;break ; case 0xbe ... 0xbf: /* movsx */ ctxt->dst.bytes = ctxt->op_bytes; - ctxt->dst.val = (ctxt->d & ByteOp) ? (s8 ) ctxt->src.val : + ctxt->dst.val = (ctxt->src.bytes ==1 ) ? (s8 ) ctxt->src.val : (s16 ) ctxt->src.val;break ; case 0xc0 ... 0xc1: /* xadd */
Signature · 1 line
1.7.11
PATCH v3 17/26 KVM: x86 emulator: split push logic from push opcode emulation
In reply to Avi KivityThis allows us to reuse the code without populating ctxt->src and overriding ctxt->op_bytes.
--- arch/x86/kvm/emulate.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
arch/x86/kvm/emulate.c +8 -3
@@ -1505,17 +1505,22 @@ static int writeback(struct x86_emulate_ctxt *ctxt)return X86EMUL_CONTINUE ; } -static int em_push (struct x86_emulate_ctxt *ctxt) +static int push (struct x86_emulate_ctxt *ctxt,void *data,int bytes) {struct segmented_address addr; -register_address_increment (ctxt, &ctxt->regs[VCPU_REGS_RSP ], -ctxt->op_bytes); +register_address_increment (ctxt, &ctxt->regs[VCPU_REGS_RSP ], -bytes); addr.ea =register_address (ctxt, ctxt->regs[VCPU_REGS_RSP ]); addr.seg =VCPU_SREG_SS ; +return segmented_write (ctxt, addr, data, bytes); +} + +static int em_push (struct x86_emulate_ctxt *ctxt) +{/* Disable writeback. */ ctxt->dst.type =OP_NONE ; -return segmented_write (ctxt, addr, &ctxt->src.val, ctxt->op_bytes); +return push (ctxt, &ctxt->src.val, ctxt->op_bytes); } static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Signature · 1 line
1.7.11
PATCH v3 18/26 KVM: x86 emulator: implement ENTER
In reply to Avi KivityOpcode C8.
Only ENTER with lexical nesting depth 0 is implemented, since others are very rare. We'll fail emulation if nonzero lexical depth is used so data is not corrupted.
--- arch/x86/kvm/emulate.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +27 -1
@@ -454,6 +454,11 @@ static ulong stack_mask(struct x86_emulate_ctxt *ctxt)return ~0U >> ((ss.d ^1 ) *16 );/* d=0: 0xffff; d=1: 0xffffffff */ } +static int stack_size (struct x86_emulate_ctxt *ctxt) +{ +return (__fls (stack_mask (ctxt)) +1 ) >>3 ; +} +/* Access/update address held in a register, based on addressing mode. */ static inline unsigned long address_mask (struct x86_emulate_ctxt *ctxt,unsigned long reg)
@@ -1592,6 +1597,26 @@ static int em_popf(struct x86_emulate_ctxt *ctxt)return emulate_popf (ctxt, &ctxt->dst.val, ctxt->op_bytes); } +static int em_enter (struct x86_emulate_ctxt *ctxt) +{ +int rc; +unsigned frame_size = ctxt->src.val; +unsigned nesting_level = ctxt->src2.val &31 ; + +if (nesting_level) +return X86EMUL_UNHANDLEABLE ; + + rc =push (ctxt, &ctxt->regs[VCPU_REGS_RBP ],stack_size (ctxt)); +if (rc !=X86EMUL_CONTINUE ) +return rc; +assign_masked (&ctxt->regs[VCPU_REGS_RBP ], ctxt->regs[VCPU_REGS_RSP ], +stack_mask (ctxt)); +assign_masked (&ctxt->regs[VCPU_REGS_RSP ], + ctxt->regs[VCPU_REGS_RSP ] - frame_size, +stack_mask (ctxt)); +return X86EMUL_CONTINUE ; +} +static int em_leave (struct x86_emulate_ctxt *ctxt) { assign_masked(&ctxt->regs[VCPU_REGS_RSP], ctxt->regs[VCPU_REGS_RBP],
@@ -3657,7 +3682,8 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)I (DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),G (ByteOp, group11),G (0 , group11),/* 0xC8 - 0xCF */ -N ,I (Stack, em_leave),N ,I (ImplicitOps | Stack, em_ret_far), +I (Stack | SrcImmU16 | Src2ImmByte, em_enter),I (Stack, em_leave), +N ,I (ImplicitOps | Stack, em_ret_far),D (ImplicitOps),DI (SrcImmByte, intn),D (ImplicitOps | No64),II (ImplicitOps, em_iret, iret), /* 0xD0 - 0xD7 */
Signature · 1 line
1.7.11
PATCH v3 19/26 KVM: VMX: Stop invalid guest state emulation on pending event
In reply to Avi KivityProcess the event, possibly injecting an interrupt, before continuing.
--- arch/x86/kvm/vmx.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/vmx.c +3 -0
@@ -4986,6 +4986,9 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)if (intr_window_requested &&vmx_interrupt_allowed (vcpu))return handle_interrupt_window (&vmx->vcpu); +if (test_bit (KVM_REQ_EVENT , &vcpu->requests)) +return 1 ; + err =emulate_instruction (vcpu,0 );if (err ==EMULATE_DO_MMIO ) {
Signature · 1 line
1.7.11
PATCH v3 20/26 KVM: VMX: Improve error reporting during invalid guest state emulation
In reply to Avi KivityIf instruction emulation fails, report it properly to userspace.
--- arch/x86/kvm/vmx.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx.c +5 -1
@@ -4996,8 +4996,12 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) goto out; } -if (err !=EMULATE_DONE ) +if (err !=EMULATE_DONE ) { + vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; + vcpu->run->internal.suberror =KVM_INTERNAL_ERROR_EMULATION ; + vcpu->run->internal.ndata =0 ;return 0 ; + }if (signal_pending (current)) goto out;
Signature · 1 line
1.7.11
PATCH v3 21/26 KVM: x86 emulator: emulate BSWAP
In reply to Avi KivityOpcodes 0F C8 - 0F CF.
Used by the SeaBIOS cdrom code (though not in big real mode).
--- arch/x86/kvm/emulate.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +18 -2
@@ -3264,6 +3264,21 @@ static int em_lahf(struct x86_emulate_ctxt *ctxt)return X86EMUL_CONTINUE ; } +static int em_bswap (struct x86_emulate_ctxt *ctxt) +{ +switch (ctxt->op_bytes) { +#ifdef CONFIG_X86_64 +case 8 : + asm("bswap %0" :"+r" (ctxt->dst.val)); +break ; +#endif +default : + asm("bswap %0" :"+r" (*(u32 *)&ctxt->dst.val)); +break ; + } +return X86EMUL_CONTINUE ; +} +static bool valid_cr (int nr) {switch (nr) {
@@ -3780,11 +3795,12 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)I (DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),I (DstReg | SrcMem | ModRM, em_bsf),I (DstReg | SrcMem | ModRM, em_bsr),D (DstReg | SrcMem8 | ModRM | Mov),D (DstReg | SrcMem16 | ModRM | Mov), -/* 0xC0 - 0xCF */ +/* 0xC0 - 0xC7 */ D2bv (DstMem | SrcReg | ModRM | Lock),N ,D (DstMem | SrcReg | ModRM | Mov),N ,N ,N ,GD (0 , &group9), -N ,N ,N ,N ,N ,N ,N ,N , +/* 0xC8 - 0xCF */ +X8 (I (DstReg, em_bswap)),/* 0xD0 - 0xDF */ N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N , /* 0xE0 - 0xEF */
Signature · 1 line
1.7.11
PATCH v3 22/26 KVM: x86 emulator: emulate LLDT
In reply to Avi KivityOpcode 0F 00 /2. Used by isolinux durign the protected mode transition.
--- arch/x86/kvm/emulate.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +10 -1
@@ -3015,6 +3015,15 @@ static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)return load_segment_descriptor (ctxt, sel, ctxt->modrm_reg); } +static int em_lldt (struct x86_emulate_ctxt *ctxt) +{ +u16 sel = ctxt->src.val; + +/* Disable writeback. */ + ctxt->dst.type =OP_NONE ; +return load_segment_descriptor (ctxt, sel,VCPU_SREG_LDTR ); +} +static int em_invlpg (struct x86_emulate_ctxt *ctxt) {int rc;
@@ -3560,7 +3569,7 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)static struct opcode group6[] = {DI (Prot, sldt),DI (Prot, str), -DI (Prot | Priv, lldt), +II (Prot | Priv | SrcMem16, em_lldt, lldt),DI (Prot | Priv, ltr),N ,N ,N ,N , };
Signature · 1 line
1.7.11
PATCH v3 23/26 KVM: x86 emulator: make read_segment_descriptor() return the address
In reply to Avi KivitySome operations want to modify the descriptor later on, so save the address for future use.
--- arch/x86/kvm/emulate.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
arch/x86/kvm/emulate.c +8 -5
@@ -1296,7 +1296,8 @@ static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,/* allowed just for 8 bytes segments */ static int read_segment_descriptor (struct x86_emulate_ctxt *ctxt, -u16 selector,struct desc_struct *desc) +u16 selector,struct desc_struct *desc, +ulong *desc_addr_p) {struct desc_ptr dt;u16 index = selector >>3 ;
@@ -1307,7 +1308,7 @@ static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,if (dt.size < index *8 +7 )return emulate_gp (ctxt, selector &0xfffc ); - addr = dt.address + index *8 ; + *desc_addr_p = addr = dt.address + index *8 ;return ctxt->ops->read_std (ctxt, addr, desc,sizeof *desc, &ctxt->exception); }
@@ -1339,6 +1340,7 @@ static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,unsigned err_vec =GP_VECTOR ;u32 err_code =0 ;bool null_selector = !(selector & ~0x3 );/* 0000-0003 are null */ +ulong desc_addr;int ret;memset (&seg_desc,0 ,sizeof seg_desc);
@@ -1374,7 +1376,7 @@ static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,if (null_selector)/* for NULL selector skip all following checks */ goto load; - ret =read_segment_descriptor (ctxt, selector, &seg_desc); + ret =read_segment_descriptor (ctxt, selector, &seg_desc, &desc_addr);if (ret !=X86EMUL_CONTINUE )return ret;
@@ -2614,13 +2616,14 @@ static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,ulong old_tss_base = ops->get_cached_segment_base (ctxt,VCPU_SREG_TR );u32 desc_limit; +ulong desc_addr;/* FIXME: old_tss_base == ~0 ? */ - ret =read_segment_descriptor (ctxt, tss_selector, &next_tss_desc); + ret =read_segment_descriptor (ctxt, tss_selector, &next_tss_desc, &desc_addr);if (ret !=X86EMUL_CONTINUE )return ret; - ret =read_segment_descriptor (ctxt, old_tss_sel, &curr_tss_desc); + ret =read_segment_descriptor (ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);if (ret !=X86EMUL_CONTINUE )return ret;
Signature · 1 line
1.7.11
PATCH v3 24/26 KVM: x86 emulator: make loading TR set the busy bit
In reply to Avi KivityGuest software doesn't actually depend on it, but vmx will refuse us entry if we don't. Set the bit in both the cached segment and memory, just to be nice.
--- arch/x86/kvm/emulate.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +7 -1
@@ -1335,7 +1335,7 @@ static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,static int load_segment_descriptor (struct x86_emulate_ctxt *ctxt,u16 selector,int seg) { -struct desc_struct seg_desc; +struct desc_struct seg_desc, old_desc;u8 dpl, rpl, cpl;unsigned err_vec =GP_VECTOR ;u32 err_code =0 ;
@@ -1422,6 +1422,12 @@ static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,case VCPU_SREG_TR :if (seg_desc.s || (seg_desc.type !=1 && seg_desc.type !=9 )) goto exception; + old_desc = seg_desc; + seg_desc.type |=2 ;/* busy */ + ret = ctxt->ops->cmpxchg_emulated (ctxt, desc_addr, &old_desc, &seg_desc, +sizeof (seg_desc), &ctxt->exception); +if (ret !=X86EMUL_CONTINUE ) +return ret;break ;case VCPU_SREG_LDTR :if (seg_desc.s || seg_desc.type !=2 )
Signature · 1 line
1.7.11
PATCH v3 25/26 KVM: x86 emulator: implement LTR
In reply to Avi KivityOpcode 0F 00 /3. Encountered during Windows XP secondary processor bringup.
--- arch/x86/kvm/emulate.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +10 -1
@@ -3033,6 +3033,15 @@ static int em_lldt(struct x86_emulate_ctxt *ctxt)return load_segment_descriptor (ctxt, sel,VCPU_SREG_LDTR ); } +static int em_ltr (struct x86_emulate_ctxt *ctxt) +{ +u16 sel = ctxt->src.val; + +/* Disable writeback. */ + ctxt->dst.type =OP_NONE ; +return load_segment_descriptor (ctxt, sel,VCPU_SREG_TR ); +} +static int em_invlpg (struct x86_emulate_ctxt *ctxt) {int rc;
@@ -3579,7 +3588,7 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)DI (Prot, sldt),DI (Prot, str),II (Prot | Priv | SrcMem16, em_lldt, lldt), -DI (Prot | Priv, ltr), +II (Prot | Priv | SrcMem16, em_ltr, ltr), N, N, N, N, };
Signature · 1 line
1.7.11
PATCH v3 26/26 KVM: VMX: Emulate invalid guest state by default
In reply to Avi KivityOur emulation should be complete enough that we can emulate guests while they are in big real mode, or in a mode transition that is not virtualizable without unrestricted guest support.
--- arch/x86/kvm/vmx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx.c +1 -1
@@ -74,7 +74,7 @@static bool __read_mostly enable_ept_ad_bits =1 ;module_param_named (eptad, enable_ept_ad_bits, bool,S_IRUGO ); -static bool __read_mostly emulate_invalid_guest_state =0 ; +static bool __read_mostly emulate_invalid_guest_state = true;module_param (emulate_invalid_guest_state, bool,S_IRUGO );static bool __read_mostly vmm_exclusive =1 ;
Signature · 1 line
1.7.11