diff mbox

[04/15] KVM: x86 emulator: Add check_perm callback

Message ID 1301667024-29420-5-git-send-email-joerg.roedel@amd.com (mailing list archive)
State New, archived
Headers show

Commit Message

Joerg Roedel April 1, 2011, 2:10 p.m. UTC
None

Comments

Avi Kivity April 3, 2011, 12:35 p.m. UTC | #1
On 04/01/2011 05:10 PM, Joerg Roedel wrote:
> This 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.
>
>
> @@ -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);

I originally mean to use a group-like structure to have check_perm only 
when needed, but I guess this is a premature optimization. #define D(_y) 
{ .flags = (_y) }
>   #define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
> +#define DIP(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i, \
> +		      .check_perm = em_check_perm_##_i }

Sorry, this (and all the #defines which follow) are just obfuscating.  I 
set a bad example here, but the following patches show there is nothing 
gained by the ## games.  Please use the full function name.
Joerg Roedel April 4, 2011, 10:23 a.m. UTC | #2
On Sun, Apr 03, 2011 at 08:35:28AM -0400, Avi Kivity wrote:
> On 04/01/2011 05:10 PM, Joerg Roedel wrote:
> > This 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.
> >
> >
> > @@ -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);
> 
> I originally mean to use a group-like structure to have check_perm only 
> when needed, but I guess this is a premature optimization. #define D(_y) 
> { .flags = (_y) }
> >   #define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
> > +#define DIP(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i, \
> > +		      .check_perm = em_check_perm_##_i }
> 
> Sorry, this (and all the #defines which follow) are just obfuscating.  I 
> set a bad example here, but the following patches show there is nothing 
> gained by the ## games.  Please use the full function name.

Yeah, this became a bit obfuscating in the end. My intention was to keep
the opcode tables clean and readable, but I changed it now and added the
check-functions directly removing all the #defines.

	Joerg
diff mbox

Patch

diff --git a/arch/x86/include/asm/kvm_emulate.h b/arch/x86/include/asm/kvm_emulate.h
index 366de63..a891051 100644
--- a/arch/x86/include/asm/kvm_emulate.h
+++ b/arch/x86/include/asm/kvm_emulate.h
@@ -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 */
diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index b05e50d..f87d7f8 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -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) { .flags = (_y), .intercept = x86_intercept_##_i, \
+		      .check_perm = em_check_perm_##_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 IIP(_f, _e, _i) \
+	{ .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
+	  .check_perm = em_check_perm_##_i }
 
 #define D2bv(_f)      D((_f) | ByteOp), D(_f)
+#define D2bvI(_f, _i) DI((_f) | ByteOp, _i), DI((_f), _i)
+#define D2bvIP(_f, _i) DIP((_f) | ByteOp, _i), DIP((_f), _i)
 #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);