Emulator intercept frameworks
cover Emulator intercept frameworks
This patchset defines a framework for intercepting emulated instructions. It takes a middle ground to the two previous proposals: putting everything in an arch callback, or putting everything in the emulator. Instead, it marks each interceptable instruction with an intercept code that it passes to an arch callback. The intercept code can then be used to index a table that looks up the correct VMCB bit and exit reason (for the case of SVM).
Joerg, if this looks right to you, please integrate it into your emulator intercept patchset.
Avi Kivity (2):
KVM: x86 emulator: add framework for instruction intercepts
KVM: x86 emulator: add SVM intercepts
arch/x86/include/asm/kvm_emulate.h | 55 ++++++++++++++++++++++++++++++++++++ arch/x86/kvm/emulate.c | 50 +++++++++++++++++++++++++------- arch/x86/kvm/x86.c | 9 ++++++ 3 files changed, 103 insertions(+), 11 deletions(-)
PATCH 1/2 KVM: x86 emulator: add framework for instruction intercepts
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 have_exception;
@@ -258,6 +266,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
@@ -101,6 +101,7 @@struct opcode {u32 flags; +u8 intercept;union {int (*execute)(struct x86_emulate_ctxt *ctxt);struct opcode *group;
@@ -2319,10 +2320,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)
@@ -2733,6 +2737,7 @@ done_prefixes: } c->execute = opcode.u.execute; + c->intercept = opcode.intercept;/* Unrecognised? */ if (c->d ==0 || (c->d & Undefined))
@@ -2964,12 +2969,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 ) {
@@ -3008,6 +3027,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
@@ -4207,6 +4207,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,
@@ -4230,6 +4237,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)
@@ -4284,6 +4292,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/2 KVM: x86 emulator: add SVM intercepts
In reply to Avi KivityAdd 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
@@ -274,6 +274,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
@@ -2364,14 +2364,14 @@ 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),N ,N ,D (SrcNone | ModRM | Priv), -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[] = {
@@ -2446,7 +2446,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),
@@ -2469,7 +2469,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,
@@ -2484,7 +2485,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),
@@ -2494,7 +2496,7 @@ static struct opcode twobyte_table[256] = {/* 0x00 - 0x0F */ N ,GD (0 , &group7),N ,N ,N ,D (ImplicitOps),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,
@@ -2504,7 +2506,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),D (ImplicitOps | Priv),N ,N , N, N, N, N, N, N, N, N,
Signature · 1 line
1.7.1
Re: [PATCH 2/2] KVM: x86 emulator: add SVM intercepts
Quoted 2 lines · Avi Kivity
Add intercept codes for instructions defined by SVM as interceptable.
Note: this doesn't handle pause, aka 'rep nop', since we don't yet specialize decode based on rep prefixes. When we get it (needed for sse) we can add the decode, till then it doesn't pause any security issue since pause has no side effects.
Signature · 1 line
error compiling committee.c: too many arguments to function
Hi Avi,
Quoted 6 lines · Avi Kivity
This patchset defines a framework for intercepting emulated instructions. It takes a middle ground to the two previous proposals: putting everything in an arch callback, or putting everything in the emulator. Instead, it marks each interceptable instruction with an intercept code that it passes to an arch callback. The intercept code can then be used to index a table that looks up the correct VMCB bit and exit reason (for the case of SVM).
Thanks for that infrastructure work. This interface looks indeed better than my original one. Most of the instruction decoding stuff (which was the code duplication between the x86_emulate_insn function) can be removed now.
Quoted 2 lines
Joerg, if this looks right to you, please integrate it into your emulator intercept patchset.
Will do. It will probably take until January before I am done with this. There is some other stuff to do first.
Joerg
Signature · 5 lines
AMD Operating System Research Center Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach General Managers: Alberto Bozzo, Andrew Bowd Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
Quoted 6 lines · Joerg
Quoted 3 lines
Joerg, if this looks right to you, please integrate it into your emulator intercept patchset.
Will do. It will probably take until January before I am done with this. There is some other stuff to do first.
Any progress? I'd like to do some more emulator work, but don't want to impact this patch needlessly.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 10 lines · Avi Kivity
Quoted 6 lines · Joerg
Quoted 3 lines
Joerg, if this looks right to you, please integrate it into your emulator intercept patchset.
Will do. It will probably take until January before I am done with this. There is some other stuff to do first.
Any progress? I'd like to do some more emulator work, but don't want to impact this patch needlessly.
Not yet. I currently struggle with the KVMs tsc and pvclock code to get tsc-scaling supported. But this will be next. If your work can't wait just implement it and I take care of forward-porting these patches.
Joerg
Signature · 5 lines
AMD Operating System Research Center Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach General Managers: Alberto Bozzo, Andrew Bowd Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
Quoted 9 lines · Joerg
Quoted 8 lines · Avi Kivity
Quoted 6 lines · Joerg
Quoted 3 lines
Joerg, if this looks right to you, please integrate it into your emulator intercept patchset.
Will do. It will probably take until January before I am done with this. There is some other stuff to do first.
Quoted 6 lines
Quoted 2 lines
Any progress? I'd like to do some more emulator work, but don't want to impact this patch needlessly.
Not yet. I currently struggle with the KVMs tsc and pvclock code to get tsc-scaling supported. But this will be next. If your work can't wait just implement it and I take care of forward-porting these patches.
Nothing urgent, it can wait for a bit.
Signature · 1 line
error compiling committee.c: too many arguments to function