aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/kvm
diff options
context:
space:
mode:
authorAvi Kivity <avi@redhat.com>2011-04-04 06:39:22 -0400
committerAvi Kivity <avi@redhat.com>2011-05-11 07:57:00 -0400
commitc4f035c60dad45ff8813550dc82540dbbc263df2 (patch)
tree371e0b43f62b8516870bb777dd967de5aa76b206 /arch/x86/kvm
parentaa97bb4891b1f1b35e7abef8d1e2bbd3dda07159 (diff)
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. Signed-off-by: Avi Kivity <avi@redhat.com> Signed-off-by: Joerg Roedel <joerg.roedel@amd.com> Signed-off-by: Avi Kivity <avi@redhat.com>
Diffstat (limited to 'arch/x86/kvm')
-rw-r--r--arch/x86/kvm/emulate.c26
-rw-r--r--arch/x86/kvm/x86.c9
2 files changed, 35 insertions, 0 deletions
diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index 2b6c24e572d4..a81486790ba8 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -104,6 +104,7 @@
104 104
105struct opcode { 105struct opcode {
106 u32 flags; 106 u32 flags;
107 u8 intercept;
107 union { 108 union {
108 int (*execute)(struct x86_emulate_ctxt *ctxt); 109 int (*execute)(struct x86_emulate_ctxt *ctxt);
109 struct opcode *group; 110 struct opcode *group;
@@ -2423,10 +2424,13 @@ static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2423} 2424}
2424 2425
2425#define D(_y) { .flags = (_y) } 2426#define D(_y) { .flags = (_y) }
2427#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
2426#define N D(0) 2428#define N D(0)
2427#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) } 2429#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2428#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) } 2430#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2429#define I(_f, _e) { .flags = (_f), .u.execute = (_e) } 2431#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
2432#define II(_f, _e, _i) \
2433 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
2430#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) } 2434#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
2431 2435
2432#define D2bv(_f) D((_f) | ByteOp), D(_f) 2436#define D2bv(_f) D((_f) | ByteOp), D(_f)
@@ -2867,6 +2871,7 @@ done_prefixes:
2867 } 2871 }
2868 2872
2869 c->execute = opcode.u.execute; 2873 c->execute = opcode.u.execute;
2874 c->intercept = opcode.intercept;
2870 2875
2871 /* Unrecognised? */ 2876 /* Unrecognised? */
2872 if (c->d == 0 || (c->d & Undefined)) 2877 if (c->d == 0 || (c->d & Undefined))
@@ -3116,12 +3121,26 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
3116 goto done; 3121 goto done;
3117 } 3122 }
3118 3123
3124 if (unlikely(ctxt->guest_mode) && c->intercept) {
3125 rc = ops->intercept(ctxt, c->intercept,
3126 X86_ICPT_PRE_EXCEPT);
3127 if (rc != X86EMUL_CONTINUE)
3128 goto done;
3129 }
3130
3119 /* Privileged instruction can be executed only in CPL=0 */ 3131 /* Privileged instruction can be executed only in CPL=0 */
3120 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) { 3132 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
3121 rc = emulate_gp(ctxt, 0); 3133 rc = emulate_gp(ctxt, 0);
3122 goto done; 3134 goto done;
3123 } 3135 }
3124 3136
3137 if (unlikely(ctxt->guest_mode) && c->intercept) {
3138 rc = ops->intercept(ctxt, c->intercept,
3139 X86_ICPT_POST_EXCEPT);
3140 if (rc != X86EMUL_CONTINUE)
3141 goto done;
3142 }
3143
3125 if (c->rep_prefix && (c->d & String)) { 3144 if (c->rep_prefix && (c->d & String)) {
3126 /* All REP prefixes have the same first termination condition */ 3145 /* All REP prefixes have the same first termination condition */
3127 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) { 3146 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
@@ -3160,6 +3179,13 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
3160 3179
3161special_insn: 3180special_insn:
3162 3181
3182 if (unlikely(ctxt->guest_mode) && c->intercept) {
3183 rc = ops->intercept(ctxt, c->intercept,
3184 X86_ICPT_POST_MEMACCESS);
3185 if (rc != X86EMUL_CONTINUE)
3186 goto done;
3187 }
3188
3163 if (c->execute) { 3189 if (c->execute) {
3164 rc = c->execute(ctxt); 3190 rc = c->execute(ctxt);
3165 if (rc != X86EMUL_CONTINUE) 3191 if (rc != X86EMUL_CONTINUE)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 5af66515337d..36786bbb4c09 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -4297,6 +4297,13 @@ static void emulator_put_fpu(struct x86_emulate_ctxt *ctxt)
4297 preempt_enable(); 4297 preempt_enable();
4298} 4298}
4299 4299
4300static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
4301 enum x86_intercept intercept,
4302 enum x86_intercept_stage stage)
4303{
4304 return X86EMUL_CONTINUE;
4305}
4306
4300static struct x86_emulate_ops emulate_ops = { 4307static struct x86_emulate_ops emulate_ops = {
4301 .read_std = kvm_read_guest_virt_system, 4308 .read_std = kvm_read_guest_virt_system,
4302 .write_std = kvm_write_guest_virt_system, 4309 .write_std = kvm_write_guest_virt_system,
@@ -4322,6 +4329,7 @@ static struct x86_emulate_ops emulate_ops = {
4322 .get_msr = kvm_get_msr, 4329 .get_msr = kvm_get_msr,
4323 .get_fpu = emulator_get_fpu, 4330 .get_fpu = emulator_get_fpu,
4324 .put_fpu = emulator_put_fpu, 4331 .put_fpu = emulator_put_fpu,
4332 .intercept = emulator_intercept,
4325}; 4333};
4326 4334
4327static void cache_all_regs(struct kvm_vcpu *vcpu) 4335static void cache_all_regs(struct kvm_vcpu *vcpu)
@@ -4376,6 +4384,7 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
4376 ? X86EMUL_MODE_VM86 : cs_l 4384 ? X86EMUL_MODE_VM86 : cs_l
4377 ? X86EMUL_MODE_PROT64 : cs_db 4385 ? X86EMUL_MODE_PROT64 : cs_db
4378 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; 4386 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
4387 vcpu->arch.emulate_ctxt.guest_mode = is_guest_mode(vcpu);
4379 memset(c, 0, sizeof(struct decode_cache)); 4388 memset(c, 0, sizeof(struct decode_cache));
4380 memcpy(c->regs, vcpu->arch.regs, sizeof c->regs); 4389 memcpy(c->regs, vcpu->arch.regs, sizeof c->regs);
4381} 4390}