KVM: Make the instruction emulator aware of Nested Virtualization v5
cover KVM: Make the instruction emulator aware of Nested Virtualization v5
Hi,
here is the next version of this patch-set. The changes to the previous post are limited to the cosmetic changes mentioned by Avi. The full function names are used now in the opcode table so that the #defines could be removed and the link between the opcode and the function for permission checking is now more clear.
Regards,
Joerg
Diffstat:
arch/x86/include/asm/kvm_emulate.h | 90 ++++++++ arch/x86/include/asm/kvm_host.h | 22 ++ arch/x86/kvm/emulate.c | 411 ++++++++++++++++++++++++++++++------ arch/x86/kvm/svm.c | 264 +++++++++++++++++++----- arch/x86/kvm/vmx.c | 9 + arch/x86/kvm/x86.c | 25 +-- 6 files changed, 695 insertions(+), 126 deletions(-)
Shortlog:
Avi Kivity (2):
KVM: x86 emulator: add framework for instruction intercepts
KVM: x86 emulator: add SVM intercepts
Joerg Roedel (13):
KVM: x86 emulator: Don't write-back cpu-state on X86EMUL_INTERCEPTED
KVM: x86 emulator: Add check_perm callback
KVM: x86 emulator: Add flag to check for protected mode instructions
KVM: x86: Add x86 callback for intercept check
KVM: SVM: Add intercept check for emulated cr accesses
KVM: SVM: Add intercept check for accessing dr registers
KVM: SVM: Add intercept checks for descriptor table accesses
KVM: SVM: Add intercept checks for SVM instructions
KVM: SVM: Add intercept checks for remaining group7 instructions
KVM: SVM: Add intercept checks for remaining twobyte instructions
KVM: SVM: Add intercept checks for one-byte instructions
KVM: SVM: Add checks for IO instructions
KVM: SVM: Remove nested sel_cr0_write handling code
PATCH 1/15 KVM: x86 emulator: add framework for instruction intercepts
From: Avi Kivity <[email protected]>
When running in guest mode, certain instructions can be intercepted by hardware. This also holds for nested guests running on emulated virtualization hardware, in particular instructions emulated by kvm itself.
This patch adds a framework for intercepting instructions. If an instruction is marked for interception, and if we're running in guest mode, a callback is called to check whether an intercept is needed or not. The callback is called at three points in time: immediately after beginning execution, after checking privilge exceptions, and after checking memory exception. This suits the different interception points defined for different instructions and for the various virtualization instruction sets.
In addition, a new X86EMUL_INTERCEPT is defined, which any callback or memory access may define, allowing the more complicated intercepts to be implemented in existing callbacks.
--- arch/x86/include/asm/kvm_emulate.h | 20 ++++++++++++++++++++ arch/x86/kvm/emulate.c | 26 ++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 9 +++++++++ 3 files changed, 55 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_emulate.h +20 -0
@@ -14,6 +14,8 @@#include <asm/desc_defs.h> struct x86_emulate_ctxt ; +enum x86_intercept ; +enum x86_intercept_stage ;struct x86_exception {u8 vector;
@@ -62,6 +64,7 @@ struct x86_exception {#define X86EMUL_RETRY_INSTR 3/* retry the instruction for some reason */ #define X86EMUL_CMPXCHG_FAILED 4/* cmpxchg did not see expected value */ #define X86EMUL_IO_NEEDED 5/* IO is needed to complete emulation */ +#define X86EMUL_INTERCEPTED 6/* Intercepted by nested VMCB/VMCS */ struct x86_emulate_ops {/*
@@ -158,6 +161,9 @@ struct x86_emulate_ops {int (*set_dr)(int dr,unsigned long value,struct kvm_vcpu *vcpu);int (*set_msr)(struct kvm_vcpu *vcpu,u32 msr_index,u64 data);int (*get_msr)(struct kvm_vcpu *vcpu,u32 msr_index,u64 *pdata); +int (*intercept)(struct x86_emulate_ctxt *ctxt, +enum x86_intercept intercept, +enum x86_intercept_stage stage); };/* Type, address-of, and value of an instruction's operand. */
@@ -197,6 +203,7 @@ struct read_cache {struct decode_cache {u8 twobyte;u8 b; +u8 intercept;u8 lock_prefix;u8 rep_prefix;u8 op_bytes;
@@ -238,6 +245,7 @@ struct x86_emulate_ctxt {/* interruptibility state, as a result of execution of STI or MOV SS */ int interruptibility; +bool guest_mode;/* guest running a nested guest */ bool perm_ok;/* do not check permissions if true */ bool only_vendor_specific_insn;
@@ -259,6 +267,18 @@ struct x86_emulate_ctxt {#define X86EMUL_MODE_PROT32 4/* 32-bit protected mode. */ #define X86EMUL_MODE_PROT64 8/* 64-bit (long) mode. */ +enum x86_intercept_stage { +X86_ICPT_PRE_EXCEPT , +X86_ICPT_POST_EXCEPT , +X86_ICPT_POST_MEMACCESS , +}; + +enum x86_intercept { + x86_intercept_none, + + nr_x86_intercepts +}; +/* Host execution mode. */ #if defined(CONFIG_X86_32 )#define X86EMUL_MODE_HOST X86EMUL_MODE_PROT32
arch/x86/kvm/emulate.c +26 -0
@@ -102,6 +102,7 @@struct opcode {u32 flags; +u8 intercept;union {int (*execute)(struct x86_emulate_ctxt *ctxt);struct opcode *group;
@@ -2326,10 +2327,13 @@ static int em_mov(struct x86_emulate_ctxt *ctxt) }#define D (_y) { .flags = (_y) } +#define DI (_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }#define N D(0)#define G (_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }#define GD (_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }#define I (_f, _e) { .flags = (_f), .u.execute = (_e) } +#define II (_f, _e, _i) \ + { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }#define D2bv (_f) D((_f) | ByteOp), D(_f)#define I2bv (_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
@@ -2745,6 +2749,7 @@ done_prefixes: } c->execute = opcode.u.execute; + c->intercept = opcode.intercept;/* Unrecognised? */ if (c->d ==0 || (c->d & Undefined))
@@ -2979,12 +2984,26 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt) goto done; } +if (unlikely (ctxt->guest_mode) && c->intercept) { + rc = ops->intercept (ctxt, c->intercept, +X86_ICPT_PRE_EXCEPT ); +if (rc !=X86EMUL_CONTINUE ) + goto done; + } +/* Privileged instruction can be executed only in CPL=0 */ if ((c->d & Priv) && ops->cpl (ctxt->vcpu)) { rc =emulate_gp (ctxt,0 ); goto done; } +if (unlikely (ctxt->guest_mode) && c->intercept) { + rc = ops->intercept (ctxt, c->intercept, +X86_ICPT_POST_EXCEPT ); +if (rc !=X86EMUL_CONTINUE ) + goto done; + } +if (c->rep_prefix && (c->d & String)) {/* All REP prefixes have the same first termination condition */ if (address_mask (c, c->regs[VCPU_REGS_RCX ]) ==0 ) {
@@ -3023,6 +3042,13 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt) special_insn: +if (unlikely (ctxt->guest_mode) && c->intercept) { + rc = ops->intercept (ctxt, c->intercept, +X86_ICPT_POST_MEMACCESS ); +if (rc !=X86EMUL_CONTINUE ) + goto done; + } +if (c->execute) { rc = c->execute (ctxt);if (rc !=X86EMUL_CONTINUE )
arch/x86/kvm/x86.c +9 -0
@@ -4242,6 +4242,13 @@ static void emulator_set_segment_selector(u16 sel, int seg,kvm_set_segment (vcpu, &kvm_seg, seg); } +static int emulator_intercept (struct x86_emulate_ctxt *ctxt, +enum x86_intercept intercept, +enum x86_intercept_stage stage) +{ +return X86EMUL_CONTINUE ; +} +static struct x86_emulate_ops emulate_ops = { .read_std = kvm_read_guest_virt_system, .write_std = kvm_write_guest_virt_system,
@@ -4265,6 +4272,7 @@ static struct x86_emulate_ops emulate_ops = { .set_dr = emulator_set_dr, .set_msr = kvm_set_msr, .get_msr = kvm_get_msr, + .intercept = emulator_intercept, };static void cache_all_regs (struct kvm_vcpu *vcpu)
@@ -4319,6 +4327,7 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu) ? X86EMUL_MODE_VM86 : cs_l ?X86EMUL_MODE_PROT64 : cs_db ?X86EMUL_MODE_PROT32 :X86EMUL_MODE_PROT16 ; + vcpu->arch.emulate_ctxt.guest_mode =is_guest_mode (vcpu);memset (c,0 ,sizeof (struct decode_cache ));memcpy (c->regs, vcpu->arch.regs,sizeof c->regs); }
Signature · 1 line
1.7.1
PATCH 2/15 KVM: x86 emulator: add SVM intercepts
In reply to Joerg RoedelFrom: Avi Kivity <[email protected]>
Add intercept codes for instructions defined by SVM as interceptable.
--- arch/x86/include/asm/kvm_emulate.h | 35 +++++++++++++++++++++++++++++++++++ arch/x86/kvm/emulate.c | 24 +++++++++++++----------- 2 files changed, 48 insertions(+), 11 deletions(-)
arch/x86/include/asm/kvm_emulate.h +35 -0
@@ -275,6 +275,41 @@ enum x86_intercept_stage {enum x86_intercept { x86_intercept_none, + x86_intercept_lmsw, + x86_intercept_smsw, + x86_intercept_lidt, + x86_intercept_sidt, + x86_intercept_lgdt, + x86_intercept_sgdt, + x86_intercept_lldt, + x86_intercept_sldt, + x86_intercept_ltr, + x86_intercept_str, + x86_intercept_rdtsc, + x86_intercept_rdpmc, + x86_intercept_pushf, + x86_intercept_popf, + x86_intercept_cpuid, + x86_intercept_rsm, + x86_intercept_iret, + x86_intercept_intn, + x86_intercept_invd, + x86_intercept_pause, + x86_intercept_hlt, + x86_intercept_invlpg, + x86_intercept_invlpga, + x86_intercept_vmrun, + x86_intercept_vmload, + x86_intercept_vmsave, + x86_intercept_vmmcall, + x86_intercept_stgi, + x86_intercept_clgi, + x86_intercept_skinit, + x86_intercept_rdtscp, + x86_intercept_icebp, + x86_intercept_wbinvd, + x86_intercept_monitor, + x86_intercept_mwait, nr_x86_intercepts };
arch/x86/kvm/emulate.c +13 -11
@@ -2371,15 +2371,15 @@ static struct opcode group5[] = { };static struct group_dual group7 = { { -N ,N ,D (ModRM | SrcMem | Priv),D (ModRM | SrcMem | Priv), -D (SrcNone | ModRM | DstMem | Mov),N , -D (SrcMem16 | ModRM | Mov | Priv), -D (SrcMem | ModRM | ByteOp | Priv | NoAccess), +N ,N ,DI (ModRM | SrcMem | Priv, lgdt),DI (ModRM | SrcMem | Priv, lidt), +DI (SrcNone | ModRM | DstMem | Mov, smsw),N , +DI (SrcMem16 | ModRM | Mov | Priv, lmsw), +DI (SrcMem | ModRM | ByteOp | Priv | NoAccess, invlpg), }, {D (SrcNone | ModRM | Priv | VendorSpecific),N ,N ,D (SrcNone | ModRM | Priv | VendorSpecific), -D (SrcNone | ModRM | DstMem | Mov),N , -D (SrcMem16 | ModRM | Mov | Priv),N , +DI (SrcNone | ModRM | DstMem | Mov, smsw),N , +DI (SrcMem16 | ModRM | Mov | Priv, lmsw),N , } };static struct opcode group8[] = {
@@ -2454,7 +2454,7 @@ static struct opcode opcode_table[256] = {/* 0x98 - 0x9F */ D (DstAcc | SrcNone),I (ImplicitOps | SrcAcc, em_cwd),I (SrcImmFAddr | No64, em_call_far),N , -D (ImplicitOps | Stack),D (ImplicitOps | Stack),N ,N , +DI (ImplicitOps | Stack, pushf),DI (ImplicitOps | Stack, popf),N ,N ,/* 0xA0 - 0xA7 */ I2bv (DstAcc | SrcMem | Mov | MemAbs, em_mov), I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
@@ -2477,7 +2477,8 @@ static struct opcode opcode_table[256] = {G (ByteOp, group11),G (0 , group11),/* 0xC8 - 0xCF */ N ,N ,N ,D (ImplicitOps | Stack), -D (ImplicitOps),D (SrcImmByte),D (ImplicitOps | No64),D (ImplicitOps), +D (ImplicitOps),DI (SrcImmByte, intn), +D (ImplicitOps | No64),DI (ImplicitOps, iret),/* 0xD0 - 0xD7 */ D2bv (DstMem | SrcOne | ModRM),D2bv (DstMem | ModRM), N, N, N, N,
@@ -2492,7 +2493,8 @@ static struct opcode opcode_table[256] = {D2bv (SrcNone | DstAcc),D2bv (SrcAcc | ImplicitOps),/* 0xF0 - 0xF7 */ N ,N ,N ,N , -D (ImplicitOps | Priv),D (ImplicitOps),G (ByteOp, group3),G (0 , group3), +DI (ImplicitOps | Priv, hlt),D (ImplicitOps), +G (ByteOp, group3),G (0 , group3),/* 0xF8 - 0xFF */ D (ImplicitOps),D (ImplicitOps),D (ImplicitOps),D (ImplicitOps), D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
@@ -2502,7 +2504,7 @@ static struct opcode twobyte_table[256] = {/* 0x00 - 0x0F */ N ,GD (0 , &group7),N ,N ,N ,D (ImplicitOps | VendorSpecific),D (ImplicitOps | Priv),N , -D (ImplicitOps | Priv),D (ImplicitOps | Priv),N ,N , +DI (ImplicitOps | Priv, invd),DI (ImplicitOps | Priv, wbinvd),N ,N ,N ,D (ImplicitOps | ModRM),N ,N ,/* 0x10 - 0x1F */ N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
@@ -2512,7 +2514,7 @@ static struct opcode twobyte_table[256] = {N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,/* 0x30 - 0x3F */ -D (ImplicitOps | Priv),I (ImplicitOps, em_rdtsc), +D (ImplicitOps | Priv),II (ImplicitOps, em_rdtsc, rdtsc),D (ImplicitOps | Priv),N ,D (ImplicitOps | VendorSpecific),D (ImplicitOps | Priv | VendorSpecific), N, N,
Signature · 1 line
1.7.1
PATCH 3/15 KVM: x86 emulator: Don't write-back cpu-state on X86EMUL_INTERCEPTED
In reply to Joerg RoedelThis patch prevents the changed CPU state to be written back when the emulator detected that the instruction was intercepted by the guest.
--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/kvm/emulate.c | 3 +++ arch/x86/kvm/x86.c | 3 +++ 3 files changed, 7 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
@@ -325,6 +325,7 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len);#define EMULATION_FAILED -1#define EMULATION_OK 0#define EMULATION_RESTART 1 +#define EMULATION_INTERCEPTED 2int x86_emulate_insn (struct x86_emulate_ctxt *ctxt); int emulator_task_switch(struct x86_emulate_ctxt *ctxt, u16 tss_selector, int reason,
arch/x86/kvm/emulate.c +3 -0
@@ -3455,6 +3455,9 @@ writeback: done:if (rc ==X86EMUL_PROPAGATE_FAULT ) ctxt->have_exception = true; +if (rc ==X86EMUL_INTERCEPTED ) +return EMULATION_INTERCEPTED ; +return (rc ==X86EMUL_UNHANDLEABLE ) ?EMULATION_FAILED :EMULATION_OK ; twobyte_insn:
arch/x86/kvm/x86.c +3 -0
@@ -4459,6 +4459,9 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, restart: r =x86_emulate_insn (&vcpu->arch.emulate_ctxt); +if (r ==EMULATION_INTERCEPTED ) +return EMULATE_DONE ; +if (r ==EMULATION_FAILED ) {if (reexecute_instruction (vcpu, cr2))return EMULATE_DONE ;
Signature · 1 line
1.7.1
PATCH 4/15 KVM: x86 emulator: Add check_perm callback
In reply to Joerg RoedelThis patch adds a check_perm callback for each opcode into the instruction emulator. This will be used to do all necessary permission checks on instructions before checking whether they are intercepted or not.
--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/kvm/emulate.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
@@ -216,6 +216,7 @@ struct decode_cache {u8 seg_override;unsigned int d;int (*execute)(struct x86_emulate_ctxt *ctxt); +int (*check_perm)(struct x86_emulate_ctxt *ctxt);unsigned long regs[NR_VCPU_REGS ];unsigned long eip;/* modrm */
arch/x86/kvm/emulate.c +16 -0
@@ -108,6 +108,7 @@ struct opcode {struct opcode *group;struct group_dual *gdual; } u; +int (*check_perm)(struct x86_emulate_ctxt *ctxt); };struct group_dual {
@@ -2328,14 +2329,21 @@ static int em_mov(struct x86_emulate_ctxt *ctxt)#define D (_y) { .flags = (_y) }#define DI (_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i } +#define DIP (_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \ + .check_perm = (_p) }#define N D(0)#define G (_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }#define GD (_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }#define I (_f, _e) { .flags = (_f), .u.execute = (_e) }#define II (_f, _e, _i) \ { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i } +#define IIP (_f, _e, _i, _p) \ + { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \ + .check_perm = (_p) }#define D2bv (_f) D((_f) | ByteOp), D(_f) +#define D2bvI (_f, _i) DI((_f) | ByteOp, _i), DI((_f), _i) +#define D2bvIP (_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP((_f), _i, _p)#define I2bv (_f, _e) I((_f) | ByteOp, _e), I(_f, _e)#define D6ALU (_f) D2bv((_f) | DstMem | SrcReg | ModRM), \
@@ -2751,6 +2759,7 @@ done_prefixes: } c->execute = opcode.u.execute; + c->check_perm = opcode.check_perm; c->intercept = opcode.intercept;/* Unrecognised? */
@@ -2999,6 +3008,13 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt) goto done; } +/* Do instruction specific permission checks */ +if (c->check_perm) { + rc = c->check_perm (ctxt); +if (rc !=X86EMUL_CONTINUE ) + goto done; + } +if (unlikely (ctxt->guest_mode) && c->intercept) { rc = ops->intercept (ctxt, c->intercept,X86_ICPT_POST_EXCEPT );
Signature · 1 line
1.7.1
PATCH 5/15 KVM: x86 emulator: Add flag to check for protected mode instructions
In reply to Joerg RoedelThis patch adds a flag for the opcoded to tag instruction which are only recognized in protected mode. The necessary check is added too.
--- arch/x86/include/asm/kvm_emulate.h | 4 ++++ arch/x86/kvm/emulate.c | 7 +++++++ 2 files changed, 11 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_emulate.h +4 -0
@@ -268,6 +268,10 @@ struct x86_emulate_ctxt {#define X86EMUL_MODE_PROT32 4/* 32-bit protected mode. */ #define X86EMUL_MODE_PROT64 8/* 64-bit (long) mode. */ +/* any protected mode */ +#define X86EMUL_MODE_PROT (X86EMUL_MODE_PROT16|X86EMUL_MODE_PROT32| \ + X86EMUL_MODE_PROT64) +enum x86_intercept_stage {X86_ICPT_PRE_EXCEPT ,X86_ICPT_POST_EXCEPT ,
arch/x86/kvm/emulate.c +7 -0
@@ -76,6 +76,7 @@#define Group (1<<14)/* Bits 3:5 of modrm byte extend opcode */ #define GroupDual (1<<15)/* Alternate decoding of mod == 3 */ /* Misc flags */ +#define Prot (1<<21)/* instruction generates #UD if not in prot-mode */ #define VendorSpecific (1<<22)/* Vendor specific instruction */ #define NoAccess (1<<23)/* Don't access memory (lea/invlpg/verr etc) */ #define Op3264 (1<<24)/* Operand is 64b in long mode, 32b otherwise */
@@ -3008,6 +3009,12 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt) goto done; } +/* Instruction can only be executed in protected mode */ +if ((c->d & Prot) && !(ctxt->mode &X86EMUL_MODE_PROT )) { + rc =emulate_ud (ctxt); + goto done; + } +/* Do instruction specific permission checks */ if (c->check_perm) { rc = c->check_perm (ctxt);
Signature · 1 line
1.7.1
PATCH 6/15 KVM: x86: Add x86 callback for intercept check
In reply to Joerg RoedelThis patch adds a callback into kvm_x86_ops so that svm and vmx code can do intercept checks on emulated instructions.
--- arch/x86/include/asm/kvm_emulate.h | 22 ++++++++++++++++++++-- arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/kvm/emulate.c | 32 ++++++++++++++++++++++++++------ arch/x86/kvm/svm.c | 9 +++++++++ arch/x86/kvm/vmx.c | 9 +++++++++ arch/x86/kvm/x86.c | 6 +++--- 6 files changed, 74 insertions(+), 11 deletions(-)
arch/x86/include/asm/kvm_emulate.h +20 -2
@@ -26,6 +26,24 @@ struct x86_exception { };/* +* This struct is used to carry enough information from the instruction +* decoder to main KVM so that a decision can be made whether the +* instruction needs to be intercepted or not. +*/ +struct x86_instruction_info { +u8 intercept;/* which intercept */ +u8 rep_prefix;/* rep prefix? */ +u8 modrm_mod;/* mod part of modrm */ +u8 modrm_reg;/* index of register used */ +u8 modrm_rm;/* rm part of modrm */ +u64 src_val;/* value of source operand */ +u8 src_bytes;/* size of source operand */ +u8 dst_bytes;/* size of destination operand */ +u8 ad_bytes;/* size of src/dst address */ +u64 next_rip;/* rip following the instruction */ +}; + +/* * x86_emulate_ops: * * These operations represent the instruction emulator's interface to memory.
@@ -161,8 +179,8 @@ struct x86_emulate_ops {int (*set_dr)(int dr,unsigned long value,struct kvm_vcpu *vcpu);int (*set_msr)(struct kvm_vcpu *vcpu,u32 msr_index,u64 data);int (*get_msr)(struct kvm_vcpu *vcpu,u32 msr_index,u64 *pdata); -int (*intercept)(struct x86_emulate_ctxt *ctxt, -enum x86_intercept intercept, +int (*intercept)(struct kvm_vcpu *vcpu, +struct x86_instruction_info *info,enum x86_intercept_stage stage); };
arch/x86/include/asm/kvm_host.h +7 -0
@@ -504,6 +504,8 @@ struct kvm_vcpu_stat {u32 nmi_injections; }; +struct x86_instruction_info ; +struct kvm_x86_ops {int (*cpu_has_kvm_support)(void );/* __init */ int (*disabled_by_bios)(void );/* __init */
@@ -591,6 +593,11 @@ struct kvm_x86_ops {void (*write_tsc_offset)(struct kvm_vcpu *vcpu,u64 offset);void (*get_exit_info)(struct kvm_vcpu *vcpu,u64 *info1,u64 *info2); + +int (*check_intercept)(struct kvm_vcpu *vcpu, +struct x86_instruction_info *info, +enum x86_intercept_stage stage); +const struct trace_print_flags *exit_reasons_str; };
arch/x86/kvm/emulate.c +26 -6
@@ -398,6 +398,26 @@ struct group_dual { (_eip) += (_size); \ }) +static int emulator_check_intercept (struct x86_emulate_ctxt *ctxt, +enum x86_intercept intercept, +enum x86_intercept_stage stage) +{ +struct x86_instruction_info info = { + .intercept = intercept, + .rep_prefix = ctxt->decode.rep_prefix, + .modrm_mod = ctxt->decode.modrm_mod, + .modrm_reg = ctxt->decode.modrm_reg, + .modrm_rm = ctxt->decode.modrm_rm, + .src_val = ctxt->decode.src.val64, + .src_bytes = ctxt->decode.src.bytes, + .dst_bytes = ctxt->decode.dst.bytes, + .ad_bytes = ctxt->decode.ad_bytes, + .next_rip = ctxt->eip, + }; + +return ctxt->ops->intercept (ctxt->vcpu, &info, stage); +} +static inline unsigned long ad_mask (struct decode_cache *c) {return (1UL << (c->ad_bytes <<3 )) -1 ;
@@ -2997,8 +3017,8 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt) }if (unlikely (ctxt->guest_mode) && c->intercept) { - rc = ops->intercept (ctxt, c->intercept, -X86_ICPT_PRE_EXCEPT ); + rc =emulator_check_intercept (ctxt, c->intercept, +X86_ICPT_PRE_EXCEPT );if (rc !=X86EMUL_CONTINUE ) goto done; }
@@ -3023,8 +3043,8 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt) }if (unlikely (ctxt->guest_mode) && c->intercept) { - rc = ops->intercept (ctxt, c->intercept, -X86_ICPT_POST_EXCEPT ); + rc =emulator_check_intercept (ctxt, c->intercept, +X86_ICPT_POST_EXCEPT );if (rc !=X86EMUL_CONTINUE ) goto done; }
@@ -3068,8 +3088,8 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt) special_insn:if (unlikely (ctxt->guest_mode) && c->intercept) { - rc = ops->intercept (ctxt, c->intercept, -X86_ICPT_POST_MEMACCESS ); + rc =emulator_check_intercept (ctxt, c->intercept, +X86_ICPT_POST_MEMACCESS );if (rc !=X86EMUL_CONTINUE ) goto done; }
arch/x86/kvm/svm.c +9 -0
@@ -3868,6 +3868,13 @@ static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)update_cr0_intercept (svm); } +static int svm_check_intercept (struct kvm_vcpu *vcpu, +struct x86_instruction_info *info, +enum x86_intercept_stage stage) +{ +return X86EMUL_CONTINUE ; +} +static struct kvm_x86_ops svm_x86_ops = { .cpu_has_kvm_support = has_svm, .disabled_by_bios = is_disabled,
@@ -3953,6 +3960,8 @@ static struct kvm_x86_ops svm_x86_ops = { .adjust_tsc_offset = svm_adjust_tsc_offset, .set_tdp_cr3 = set_tdp_cr3, + + .check_intercept = svm_check_intercept, };static int __init svm_init (void )
arch/x86/kvm/vmx.c +9 -0
@@ -4409,6 +4409,13 @@ static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) { } +static int vmx_check_intercept (struct kvm_vcpu *vcpu, +struct x86_instruction_info *info, +enum x86_intercept_stage stage) +{ +return X86EMUL_CONTINUE ; +} +static struct kvm_x86_ops vmx_x86_ops = { .cpu_has_kvm_support = cpu_has_kvm_support, .disabled_by_bios = vmx_disabled_by_bios,
@@ -4494,6 +4501,8 @@ static struct kvm_x86_ops vmx_x86_ops = { .adjust_tsc_offset = vmx_adjust_tsc_offset, .set_tdp_cr3 = vmx_set_cr3, + + .check_intercept = vmx_check_intercept, };static int __init vmx_init (void )
arch/x86/kvm/x86.c +3 -3
@@ -4242,11 +4242,11 @@ static void emulator_set_segment_selector(u16 sel, int seg,kvm_set_segment (vcpu, &kvm_seg, seg); } -static int emulator_intercept (struct x86_emulate_ctxt *ctxt, -enum x86_intercept intercept, +static int emulator_intercept (struct kvm_vcpu *vcpu, +struct x86_instruction_info *info,enum x86_intercept_stage stage) { -return X86EMUL_CONTINUE ; +return kvm_x86_ops->check_intercept (vcpu, info, stage); }static struct x86_emulate_ops emulate_ops = {
Signature · 1 line
1.7.1
PATCH 7/15 KVM: SVM: Add intercept check for emulated cr accesses
In reply to Joerg RoedelThis patch adds all necessary intercept checks for instructions that access the crX registers.
--- arch/x86/include/asm/kvm_emulate.h | 3 + arch/x86/include/asm/kvm_host.h | 15 +++++ arch/x86/kvm/emulate.c | 105 ++++++++++++++++++++++++++++++++---- arch/x86/kvm/svm.c | 81 +++++++++++++++++++++++++++- arch/x86/kvm/x86.c | 13 ----- 5 files changed, 192 insertions(+), 25 deletions(-)
arch/x86/include/asm/kvm_emulate.h +3 -0
@@ -298,6 +298,9 @@ enum x86_intercept_stage {enum x86_intercept { x86_intercept_none, + x86_intercept_cr_read, + x86_intercept_cr_write, + x86_intercept_clts, x86_intercept_lmsw, x86_intercept_smsw, x86_intercept_lidt,
arch/x86/include/asm/kvm_host.h +15 -0
@@ -34,10 +34,25 @@#define KVM_PIO_PAGE_OFFSET 1#define KVM_COALESCED_MMIO_PAGE_OFFSET 2 +#define CR0_RESERVED_BITS \ + (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ + | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \ + | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG)) +#define CR3_PAE_RESERVED_BITS ((X86_CR3_PWT | X86_CR3_PCD) - 1)#define CR3_NONPAE_RESERVED_BITS ((PAGE_SIZE-1) & ~(X86_CR3_PWT | X86_CR3_PCD))#define CR3_L_MODE_RESERVED_BITS (CR3_NONPAE_RESERVED_BITS | \ 0xFFFFFF0000000000ULL) +#define CR4_RESERVED_BITS \ + (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\ + | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \ + | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \ + | X86_CR4_OSXSAVE \ + | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE)) + +#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR) + +#define INVALID_PAGE (~(hpa_t)0)#define VALID_PAGE (x) ((x) != INVALID_PAGE)
arch/x86/kvm/emulate.c +94 -11
128 lines not on this page · raw message
arch/x86/kvm/svm.c +80 -1
91 lines not on this page · raw message
arch/x86/kvm/x86.c +0 -13
@@ -60,19 +60,6 @@#include <asm/div64.h> #define MAX_IO_MSRS 256 -#define CR0_RESERVED_BITS \ - (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ - | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \ - | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG)) -#define CR4_RESERVED_BITS \ - (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\ - | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \ - | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \ - | X86_CR4_OSXSAVE \ - | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE)) - -#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR) -#define KVM_MAX_MCE_BANKS 32#define KVM_MCE_CAP_SUPPORTED (MCG_CTL_P | MCG_SER_P)
Signature · 1 line
1.7.1
PATCH 8/15 KVM: SVM: Add intercept check for accessing dr registers
In reply to Joerg RoedelThis patch adds the intercept checks for instruction accessing the debug registers.
--- arch/x86/include/asm/kvm_emulate.h | 2 + arch/x86/kvm/emulate.c | 63 +++++++++++++++++++++++++++-------- arch/x86/kvm/svm.c | 6 +++ 3 files changed, 56 insertions(+), 15 deletions(-)
arch/x86/include/asm/kvm_emulate.h +2 -0
@@ -303,6 +303,8 @@ enum x86_intercept { x86_intercept_clts, x86_intercept_lmsw, x86_intercept_smsw, + x86_intercept_dr_read, + x86_intercept_dr_write, x86_intercept_lidt, x86_intercept_sidt, x86_intercept_lgdt,
arch/x86/kvm/emulate.c +48 -15
94 lines not on this page · raw message
arch/x86/kvm/svm.c +6 -0
@@ -3882,6 +3882,8 @@ static struct __x86_intercept { [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0), [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0), [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0), + [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0), + [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0), };#undef POST_EX
@@ -3939,6 +3941,10 @@ static int svm_check_intercept(struct kvm_vcpu *vcpu,break ; } +case SVM_EXIT_READ_DR0 : +case SVM_EXIT_WRITE_DR0 : + icpt_info.exit_code += info->modrm_reg; +break ;default :break ; }
Signature · 1 line
1.7.1
PATCH 9/15 KVM: SVM: Add intercept checks for descriptor table accesses
In reply to Joerg RoedelThis patch add intercept checks into the KVM instruction emulator to check for the 8 instructions that access the descriptor table addresses.
--- arch/x86/kvm/emulate.c | 14 ++++++++++++-- arch/x86/kvm/svm.c | 8 ++++++++ 2 files changed, 20 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +12 -2
@@ -2534,8 +2534,18 @@ static struct opcode group5[] = { D(SrcMem | ModRM | Stack), N, }; +static struct opcode group6[] = { +DI (ModRM | Prot, sldt), +DI (ModRM | Prot, str), +DI (ModRM | Prot | Priv, lldt), +DI (ModRM | Prot | Priv, ltr), +N ,N ,N ,N , +}; +static struct group_dual group7 = { { -N ,N ,DI (ModRM | SrcMem | Priv, lgdt),DI (ModRM | SrcMem | Priv, lidt), +DI (ModRM | Mov | DstMem | Priv, sgdt), +DI (ModRM | Mov | DstMem | Priv, sidt), +DI (ModRM | SrcMem | Priv, lgdt),DI (ModRM | SrcMem | Priv, lidt),DI (SrcNone | ModRM | DstMem | Mov, smsw),N ,DI (SrcMem16 | ModRM | Mov | Priv, lmsw),DI (SrcMem | ModRM | ByteOp | Priv | NoAccess, invlpg),
@@ -2666,7 +2676,7 @@ static struct opcode opcode_table[256] = {static struct opcode twobyte_table[256 ] = {/* 0x00 - 0x0F */ -N ,GD (0 , &group7),N ,N , +G (0 , group6),GD (0 , &group7),N ,N ,N ,D (ImplicitOps | VendorSpecific),DI (ImplicitOps | Priv, clts),N ,DI (ImplicitOps | Priv, invd),DI (ImplicitOps | Priv, wbinvd),N ,N ,N ,D (ImplicitOps | ModRM),N ,N ,
arch/x86/kvm/svm.c +8 -0
@@ -3884,6 +3884,14 @@ static struct __x86_intercept { [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0), [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0), [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0), + [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ), + [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ), + [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE), + [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE), + [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ), + [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ), + [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE), + [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE), };#undef POST_EX
Signature · 1 line
1.7.1
PATCH 10/15 KVM: SVM: Add intercept checks for SVM instructions
In reply to Joerg RoedelThis patch adds the necessary code changes in the instruction emulator and the extensions to svm.c to implement intercept checks for the svm instructions.
--- arch/x86/kvm/emulate.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/svm.c | 8 ++++++++ 2 files changed, 52 insertions(+), 1 deletions(-)
arch/x86/kvm/emulate.c +44 -1
@@ -75,6 +75,8 @@#define Stack (1<<13)/* Stack instruction (push/pop) */ #define Group (1<<14)/* Bits 3:5 of modrm byte extend opcode */ #define GroupDual (1<<15)/* Alternate decoding of mod == 3 */ +#define RMExt (1<<16)/* Opcode extension in ModRM r/m if mod == 3 */ +/* Misc flags */ #define Prot (1<<21)/* instruction generates #UD if not in prot-mode */ #define VendorSpecific (1<<22)/* Vendor specific instruction */
@@ -2483,11 +2485,35 @@ static int check_dr_write(struct x86_emulate_ctxt *ctxt)return check_dr_read (ctxt); } +static int check_svme (struct x86_emulate_ctxt *ctxt) +{ +u64 efer; + + ctxt->ops->get_msr (ctxt->vcpu,MSR_EFER , &efer); + +if (!(efer &EFER_SVME )) +return emulate_ud (ctxt); + +return X86EMUL_CONTINUE ; +} + +static int check_svme_pa (struct x86_emulate_ctxt *ctxt) +{ +u64 rax =kvm_register_read (ctxt->vcpu,VCPU_REGS_RAX ); + +/* Valid physical address? */ +if (rax &0xffff000000000000 ) +return emulate_gp (ctxt,0 ); + +return check_svme (ctxt); +} +#define D (_y) { .flags = (_y) }#define DI (_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }#define DIP (_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \ .check_perm = (_p) }#define N D(0) +#define EXT (_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }#define G (_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }#define GD (_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }#define I (_f, _e) { .flags = (_f), .u.execute = (_e) }
@@ -2506,6 +2532,16 @@ static int check_dr_write(struct x86_emulate_ctxt *ctxt)D2bv (((_f) | DstReg | SrcMem | ModRM) & ~Lock), \ D2bv(((_f) & ~Lock) | DstAcc | SrcImm) +static struct opcode group7_rm3[] = { +DIP (SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa), +DIP (SrcNone | ModRM | Prot , vmmcall, check_svme), +DIP (SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa), +DIP (SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa), +DIP (SrcNone | ModRM | Prot | Priv, stgi, check_svme), +DIP (SrcNone | ModRM | Prot | Priv, clgi, check_svme), +DIP (SrcNone | ModRM | Prot | Priv, skinit, check_svme), +DIP (SrcNone | ModRM | Prot | Priv, invlpga, check_svme), +};static struct opcode group1[] = {X7 (D (Lock)),N
@@ -2551,7 +2587,7 @@ static struct group_dual group7 = { { DI(SrcMem | ModRM | ByteOp | Priv | NoAccess, invlpg), }, {D (SrcNone | ModRM | Priv | VendorSpecific),N , -N ,D (SrcNone | ModRM | Priv | VendorSpecific), +N ,EXT (0 , group7_rm3),DI (SrcNone | ModRM | DstMem | Mov, smsw),N , DI(SrcMem16 | ModRM | Mov | Priv, lmsw), N, } };
@@ -2746,6 +2782,7 @@ static struct opcode twobyte_table[256] = {#undef G#undef GD#undef I +#undef EXT#undef D2bv#undef I2bv
@@ -2923,6 +2960,12 @@ done_prefixes: opcode = g_mod3[goffset]; else opcode = g_mod012[goffset]; + +if (opcode.flags & RMExt) { + goffset = c->modrm &7 ; + opcode = opcode.u.group[goffset]; + } + c->d |= opcode.flags; }
arch/x86/kvm/svm.c +8 -0
@@ -3892,6 +3892,14 @@ static struct __x86_intercept { [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ), [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE), [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE), + [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN), + [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL), + [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD), + [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE), + [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI), + [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI), + [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT), + [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA), };#undef POST_EX
Signature · 1 line
1.7.1
PATCH 11/15 KVM: SVM: Add intercept checks for remaining group7 instructions
In reply to Joerg RoedelThis patch implements the emulator intercept checks for the RDTSCP, MONITOR, and MWAIT instructions.
--- arch/x86/kvm/emulate.c | 25 +++++++++++++++++++++++-- arch/x86/kvm/svm.c | 7 +++++++ 2 files changed, 30 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +23 -2
@@ -2508,6 +2508,16 @@ static int check_svme_pa(struct x86_emulate_ctxt *ctxt)return check_svme (ctxt); } +static int check_rdtsc (struct x86_emulate_ctxt *ctxt) +{ +u64 cr4 = ctxt->ops->get_cr (4 , ctxt->vcpu); + +if (cr4 &X86_CR4_TSD && ctxt->ops->cpl (ctxt->vcpu)) +return emulate_ud (ctxt); + +return X86EMUL_CONTINUE ; +} +#define D (_y) { .flags = (_y) }#define DI (_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }#define DIP (_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
@@ -2532,6 +2542,12 @@ static int check_svme_pa(struct x86_emulate_ctxt *ctxt)D2bv (((_f) | DstReg | SrcMem | ModRM) & ~Lock), \ D2bv(((_f) & ~Lock) | DstAcc | SrcImm) +static struct opcode group7_rm1[] = { +DI (SrcNone | ModRM | Priv, monitor), +DI (SrcNone | ModRM | Priv, mwait), +N ,N ,N ,N ,N ,N , +}; +static struct opcode group7_rm3[] = {DIP (SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),DIP (SrcNone | ModRM | Prot , vmmcall, check_svme),
@@ -2543,6 +2559,11 @@ static struct opcode group7_rm3[] = { DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme), }; +static struct opcode group7_rm7[] = { +N , +DIP (SrcNone | ModRM, rdtscp, check_rdtsc), +N ,N ,N ,N ,N ,N , +};static struct opcode group1[] = {X7 (D (Lock)),N };
@@ -2586,10 +2607,10 @@ static struct group_dual group7 = { {DI (SrcMem16 | ModRM | Mov | Priv, lmsw),DI (SrcMem | ModRM | ByteOp | Priv | NoAccess, invlpg), }, { -D (SrcNone | ModRM | Priv | VendorSpecific),N , +D (SrcNone | ModRM | Priv | VendorSpecific),EXT (0 , group7_rm1),N ,EXT (0 , group7_rm3),DI (SrcNone | ModRM | DstMem | Mov, smsw),N , - DI(SrcMem16 | ModRM | Mov | Priv, lmsw), N, + DI(SrcMem16 | ModRM | Mov | Priv, lmsw), EXT(0, group7_rm7), } }; static struct opcode group8[] = {
arch/x86/kvm/svm.c +7 -0
@@ -3871,6 +3871,9 @@ static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)#define POST_EX (exit) { .exit_code = (exit), \ .stage = X86_ICPT_POST_EXCEPT, \ .valid = true } +#define POST_MEM (exit) { .exit_code = (exit), \ + .stage = X86_ICPT_POST_MEMACCESS, \ + .valid = true } static struct __x86_intercept { u32 exit_code;
@@ -3900,9 +3903,13 @@ static struct __x86_intercept { [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI), [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT), [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA), + [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP), + [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR), + [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT), };#undef POST_EX +#undef POST_MEM static int svm_check_intercept(struct kvm_vcpu *vcpu, struct x86_instruction_info *info,
Signature · 1 line
1.7.1
PATCH 12/15 KVM: SVM: Add intercept checks for remaining twobyte instructions
In reply to Joerg RoedelThis patch adds intercepts checks for the remaining twobyte instructions to the KVM instruction emulator.
--- arch/x86/include/asm/kvm_emulate.h | 2 ++ arch/x86/kvm/emulate.c | 25 ++++++++++++++++++------- arch/x86/kvm/svm.c | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_emulate.h +2 -0
@@ -338,6 +338,8 @@ enum x86_intercept { x86_intercept_wbinvd, x86_intercept_monitor, x86_intercept_mwait, + x86_intercept_rdmsr, + x86_intercept_wrmsr, nr_x86_intercepts };
arch/x86/kvm/emulate.c +18 -7
@@ -2336,12 +2336,9 @@ static int em_cwd(struct x86_emulate_ctxt *ctxt)static int em_rdtsc (struct x86_emulate_ctxt *ctxt) { -unsigned cpl = ctxt->ops->cpl (ctxt->vcpu);struct decode_cache *c = &ctxt->decode;u64 tsc =0 ; -if (cpl >0 && (ctxt->ops->get_cr (4 , ctxt->vcpu) &X86_CR4_TSD )) -return emulate_gp (ctxt,0 ); ctxt->ops->get_msr (ctxt->vcpu,MSR_IA32_TSC , &tsc); c->regs[VCPU_REGS_RAX ] = (u32 )tsc; c->regs[VCPU_REGS_RDX ] = tsc >>32 ;
@@ -2518,6 +2515,18 @@ static int check_rdtsc(struct x86_emulate_ctxt *ctxt)return X86EMUL_CONTINUE ; } +static int check_rdpmc (struct x86_emulate_ctxt *ctxt) +{ +u64 cr4 = ctxt->ops->get_cr (4 , ctxt->vcpu); +u64 rcx =kvm_register_read (ctxt->vcpu,VCPU_REGS_RCX ); + +if ((!(cr4 &X86_CR4_PCE ) && ctxt->ops->cpl (ctxt->vcpu)) || + (rcx >3 )) +return emulate_gp (ctxt,0 ); + +return X86EMUL_CONTINUE ; +} +#define D (_y) { .flags = (_y) }#define DI (_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }#define DIP (_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
@@ -2747,8 +2756,10 @@ static struct opcode twobyte_table[256] = {N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,/* 0x30 - 0x3F */ -D (ImplicitOps | Priv),II (ImplicitOps, em_rdtsc, rdtsc), -D (ImplicitOps | Priv),N , +DI (ImplicitOps | Priv, wrmsr), +IIP (ImplicitOps, em_rdtsc, rdtsc, check_rdtsc), +DI (ImplicitOps | Priv, rdmsr), +DIP (ImplicitOps | Priv, rdpmc, check_rdpmc),D (ImplicitOps | VendorSpecific),D (ImplicitOps | Priv | VendorSpecific),N ,N , N, N, N, N, N, N, N, N,
@@ -2766,12 +2777,12 @@ static struct opcode twobyte_table[256] = {X16 (D (ByteOp | DstMem | SrcNone | ModRM| Mov)),/* 0xA0 - 0xA7 */ D (ImplicitOps | Stack),D (ImplicitOps | Stack), -N ,D (DstMem | SrcReg | ModRM | BitOp), +DI (ImplicitOps, cpuid),D (DstMem | SrcReg | ModRM | BitOp),D (DstMem | SrcReg | Src2ImmByte | ModRM),D (DstMem | SrcReg | Src2CL | ModRM),N ,N ,/* 0xA8 - 0xAF */ D (ImplicitOps | Stack),D (ImplicitOps | Stack), -N ,D (DstMem | SrcReg | ModRM | BitOp | Lock), +DI (ImplicitOps, rsm),D (DstMem | SrcReg | ModRM | BitOp | Lock),D (DstMem | SrcReg | Src2ImmByte | ModRM),D (DstMem | SrcReg | Src2CL | ModRM), D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
arch/x86/kvm/svm.c +19 -0
@@ -3868,6 +3868,9 @@ static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)update_cr0_intercept (svm); } +#define PRE_EX (exit) { .exit_code = (exit), \ + .stage = X86_ICPT_PRE_EXCEPT, \ + .valid = true }#define POST_EX (exit) { .exit_code = (exit), \ .stage = X86_ICPT_POST_EXCEPT, \ .valid = true }
@@ -3906,8 +3909,18 @@ static struct __x86_intercept { [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP), [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR), [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT), + [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG), + [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD), + [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD), + [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR), + [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC), + [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR), + [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC), + [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID), + [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM), }; +#undef PRE_EX#undef POST_EX#undef POST_MEM
@@ -3968,6 +3981,12 @@ static int svm_check_intercept(struct kvm_vcpu *vcpu,case SVM_EXIT_WRITE_DR0 : icpt_info.exit_code += info->modrm_reg;break ; +case SVM_EXIT_MSR : +if (info->intercept == x86_intercept_wrmsr) + vmcb->control.exit_info_1 =1 ; +else + vmcb->control.exit_info_1 =0 ; +break ;default :break ; }
Signature · 1 line
1.7.1
PATCH 13/15 KVM: SVM: Add intercept checks for one-byte instructions
In reply to Joerg RoedelThis patch add intercept checks for emulated one-byte instructions to the KVM instruction emulation path.
--- arch/x86/kvm/emulate.c | 4 ++-- arch/x86/kvm/svm.c | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +2 -2
@@ -2690,7 +2690,7 @@ static struct opcode opcode_table[256] = {D (DstMem | SrcNone | ModRM | Mov),D (ModRM | SrcMem | NoAccess | DstReg),D (ImplicitOps | SrcMem16 | ModRM),G (0 , group1A),/* 0x90 - 0x97 */ -X8 (D (SrcAcc | DstReg)), +DI (SrcAcc | DstReg, pause),X7 (D (SrcAcc | DstReg)),/* 0x98 - 0x9F */ D (DstAcc | SrcNone),I (ImplicitOps | SrcAcc, em_cwd), I(SrcImmFAddr | No64, em_call_far), N,
@@ -2732,7 +2732,7 @@ static struct opcode opcode_table[256] = {D (SrcImmFAddr | No64),D (SrcImmByte | ImplicitOps),D2bv (SrcNone | DstAcc),D2bv (SrcAcc | ImplicitOps),/* 0xF0 - 0xF7 */ -N ,N ,N ,N , +N ,DI (ImplicitOps, icebp),N ,N ,DI (ImplicitOps | Priv, hlt),D (ImplicitOps),G (ByteOp, group3),G (0 , group3), /* 0xF8 - 0xFF */
arch/x86/kvm/svm.c +14 -0
@@ -3918,6 +3918,13 @@ static struct __x86_intercept { [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC), [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID), [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM), + [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE), + [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF), + [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF), + [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT), + [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET), + [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP), + [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT), };#undef PRE_EX
@@ -3987,6 +3994,13 @@ static int svm_check_intercept(struct kvm_vcpu *vcpu,else vmcb->control.exit_info_1 =0 ;break ; +case SVM_EXIT_PAUSE : +/* +* We get this for NOP only, but pause +* is rep not, check this here +*/ +if (info->rep_prefix !=REPE_PREFIX ) + goto out;default :break ; }
Signature · 1 line
1.7.1
PATCH 14/15 KVM: SVM: Add checks for IO instructions
In reply to Joerg RoedelThis patch adds code to check for IOIO intercepts on instructions decoded by the KVM instruction emulator.
--- arch/x86/include/asm/kvm_emulate.h | 4 +++ arch/x86/kvm/emulate.c | 44 +++++++++++++++++++++++------------ arch/x86/kvm/svm.c | 36 +++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 15 deletions(-)
arch/x86/include/asm/kvm_emulate.h +4 -0
@@ -340,6 +340,10 @@ enum x86_intercept { x86_intercept_mwait, x86_intercept_rdmsr, x86_intercept_wrmsr, + x86_intercept_in, + x86_intercept_ins, + x86_intercept_out, + x86_intercept_outs, nr_x86_intercepts };
arch/x86/kvm/emulate.c +29 -15
83 lines not on this page · raw message
arch/x86/kvm/svm.c +36 -0
@@ -3925,6 +3925,10 @@ static struct __x86_intercept { [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET), [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP), [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT), + [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO), + [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO), + [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO), + [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO), };#undef PRE_EX
@@ -4001,6 +4005,38 @@ static int svm_check_intercept(struct kvm_vcpu *vcpu,*/ if (info->rep_prefix !=REPE_PREFIX ) goto out; +case SVM_EXIT_IOIO : { +u64 exit_info; +u32 bytes; + + exit_info = (vcpu->arch.regs[VCPU_REGS_RDX ] &0xffff ) <<16 ; + +if (info->intercept == x86_intercept_in || + info->intercept == x86_intercept_ins) { + exit_info |=SVM_IOIO_TYPE_MASK ; + bytes = info->src_bytes; + }else { + bytes = info->dst_bytes; + } + +if (info->intercept == x86_intercept_outs || + info->intercept == x86_intercept_ins) + exit_info |=SVM_IOIO_STR_MASK ; + +if (info->rep_prefix) + exit_info |=SVM_IOIO_REP_MASK ; + + bytes =min (bytes,4u ); + + exit_info |= bytes <<SVM_IOIO_SIZE_SHIFT ; + + exit_info |= (u32 )info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT -1 ); + + vmcb->control.exit_info_1 = exit_info; + vmcb->control.exit_info_2 = info->next_rip; + +break ; + }default :break ; }
Signature · 1 line
1.7.1
PATCH 15/15 KVM: SVM: Remove nested sel_cr0_write handling code
In reply to Joerg RoedelThis patch removes all the old code which handled the nested selective cr0 write intercepts. This code was only in place as a work-around until the instruction emulator is capable of doing the same. This is the case with this patch-set and so the code can be removed.
--- arch/x86/kvm/svm.c | 78 +++++++++++++++++---------------------------------- 1 files changed, 26 insertions(+), 52 deletions(-)
arch/x86/kvm/svm.c +26 -52
@@ -93,14 +93,6 @@ struct nested_state {/* A VMEXIT is required but not yet emulated */ bool exit_required; -/* -* If we vmexit during an instruction emulation we need this to restore -* the l1 guest rip after the emulation -*/ -unsigned long vmexit_rip; -unsigned long vmexit_rsp; -unsigned long vmexit_rax; -/* cache for intercepts of the guest */ u32 intercept_cr;u32 intercept_dr;
@@ -1362,31 +1354,6 @@ static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) {struct vcpu_svm *svm =to_svm (vcpu); -if (is_guest_mode (vcpu)) { -/* -* We are here because we run in nested mode, the host kvm -* intercepts cr0 writes but the l1 hypervisor does not. -* But the L1 hypervisor may intercept selective cr0 writes. -* This needs to be checked here. -*/ -unsigned long old, new; - -/* Remove bits that would trigger a real cr0 write intercept */ - old = vcpu->arch.cr0 &SVM_CR0_SELECTIVE_MASK ; - new = cr0 &SVM_CR0_SELECTIVE_MASK ; - -if (old == new) { -/* cr0 write with ts and mp unchanged */ - svm->vmcb->control.exit_code =SVM_EXIT_CR0_SEL_WRITE ; -if (nested_svm_exit_handled (svm) ==NESTED_EXIT_DONE ) { - svm->nested.vmexit_rip =kvm_rip_read (vcpu); - svm->nested.vmexit_rsp =kvm_register_read (vcpu,VCPU_REGS_RSP ); - svm->nested.vmexit_rax =kvm_register_read (vcpu,VCPU_REGS_RAX ); -return ; - } - } - } -#ifdef CONFIG_X86_64 if (vcpu->arch.efer &EFER_LME ) {if (!is_paging (vcpu) && (cr0 &X86_CR0_PG )) {
@@ -2673,6 +2640,29 @@ static int emulate_on_interception(struct vcpu_svm *svm)return emulate_instruction (&svm->vcpu,0 ) ==EMULATE_DONE ; } +bool check_selective_cr0_intercepted (struct vcpu_svm *svm,unsigned long val) +{ +unsigned long cr0 = svm->vcpu.arch.cr0; +bool ret = false; +u64 intercept; + + intercept = svm->nested.intercept; + +if (!is_guest_mode (&svm->vcpu) || + (!(intercept & (1ULL <<INTERCEPT_SELECTIVE_CR0 )))) +return false; + + cr0 &= ~SVM_CR0_SELECTIVE_MASK ; + val &= ~SVM_CR0_SELECTIVE_MASK ; + +if (cr0 ^ val) { + svm->vmcb->control.exit_code =SVM_EXIT_CR0_SEL_WRITE ; + ret = (nested_svm_exit_handled (svm) ==NESTED_EXIT_DONE ); + } + +return ret; +} +#define CR_VALID (1ULL << 63)static int cr_interception (struct vcpu_svm *svm)
@@ -2696,7 +2686,8 @@ static int cr_interception(struct vcpu_svm *svm) val =kvm_register_read (&svm->vcpu, reg);switch (cr) {case 0 : - err =kvm_set_cr0 (&svm->vcpu, val); +if (!check_selective_cr0_intercepted (svm, val)) + err =kvm_set_cr0 (&svm->vcpu, val);break ;case 3 : err =kvm_set_cr3 (&svm->vcpu, val);
@@ -2741,23 +2732,6 @@ static int cr_interception(struct vcpu_svm *svm)return 1 ; } -static int cr0_write_interception (struct vcpu_svm *svm) -{ -struct kvm_vcpu *vcpu = &svm->vcpu; -int r; - - r =cr_interception (svm); - -if (svm->nested.vmexit_rip) { -kvm_register_write (vcpu,VCPU_REGS_RIP , svm->nested.vmexit_rip); -kvm_register_write (vcpu,VCPU_REGS_RSP , svm->nested.vmexit_rsp); -kvm_register_write (vcpu,VCPU_REGS_RAX , svm->nested.vmexit_rax); - svm->nested.vmexit_rip =0 ; - } - -return r; -} -static int dr_interception (struct vcpu_svm *svm) {int reg, dr;
@@ -3045,7 +3019,7 @@ static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = { [SVM_EXIT_READ_CR4] = cr_interception, [SVM_EXIT_READ_CR8] = cr_interception, [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, - [SVM_EXIT_WRITE_CR0] = cr0_write_interception, + [SVM_EXIT_WRITE_CR0] = cr_interception, [SVM_EXIT_WRITE_CR3] = cr_interception, [SVM_EXIT_WRITE_CR4] = cr_interception, [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Signature · 1 line
1.7.1
Avi Kivity
AppliedQuoted 7 lines · Joerg Roedel
Hi,
here is the next version of this patch-set. The changes to the previous post are limited to the cosmetic changes mentioned by Avi. The full function names are used now in the opcode table so that the #defines could be removed and the link between the opcode and the function for permission checking is now more clear.
Applied, thanks.
Signature · 1 line
error compiling committee.c: too many arguments to function