aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/kvm/emulate.c
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/emulate.c
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/emulate.c')
-rw-r--r--arch/x86/kvm/emulate.c26
1 files changed, 26 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)