Skip to contentSkip to conversation

KVM: x86: use proper port value when checking io instruction permission

PATCH Marcelo Tosatti 4 participants 23 messages May 24, 2011 – May 30, 2011

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Commit fa4491a6b667304 moved the permission check for io instructions to the ->check_perm callback. It failed to copy the port value from RDX register for string and "in,out ax,dx" instructions. Fix it.

Fixes FC8.32 installation.

arch/x86/kvm/emulate.c +18 -0
index 3bc6b7a..df354a4 100644
@@ -2944,6 +2944,15 @@ static int check_perm_in(struct x86_emulate_ctxt *ctxt)
 {
 	struct decode_cache *c = &ctxt->decode;
 
+	switch (c->b) {
+	case 0x6c: /* insb */
+	case 0x6d: /* insw/insd */
+	case 0xec: /* in al,dx */
+	case 0xed: /* in (e/r)ax,dx */
+		c->src.val = c->regs[VCPU_REGS_RDX];
+		break;
+	}
+
 	c->dst.bytes = min(c->dst.bytes, 4u);
 	if (!emulator_io_permited(ctxt, c->src.val, c->dst.bytes))
 		return emulate_gp(ctxt, 0);
@@ -2955,6 +2964,15 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)
 {
 	struct decode_cache *c = &ctxt->decode;
 
+	switch (c->b) {
+	case 0x6e: /* outsb */
+	case 0x6f: /* outsw/outsd */
+	case 0xee: /* out dx,al */
+	case 0xef: /* out dx,(e/r)ax */
+		c->dst.val = c->regs[VCPU_REGS_RDX];
+		break;
+	}
+
 	c->src.bytes = min(c->src.bytes, 4u);
 	if (!emulator_io_permited(ctxt, c->dst.val, c->src.bytes))
 		return emulate_gp(ctxt, 0);
#1 lore raw
Quoted 35 lines · Marcelo Tosatti

Commit fa4491a6b667304 moved the permission check for io instructions to the ->check_perm callback. It failed to copy the port value from RDX register for string and "in,out ax,dx" instructions. Fix it.

Fixes FC8.32 installation.

arch/x86/kvm/emulate.c +11 -0
index 3bc6b7a..df354a4 100644
@@ -2944,6 +2944,15 @@ static int check_perm_in(struct x86_emulate_ctxt *ctxt)
 {
 	struct decode_cache *c = &ctxt->decode;
 
+	switch (c->b) {
+	case 0x6c: /* insb */
+	case 0x6d: /* insw/insd */
+	case 0xec: /* in al,dx */
+	case 0xed: /* in (e/r)ax,dx */
+		c->src.val = c->regs[VCPU_REGS_RDX];
+		break;
+	}
+
 	c->dst.bytes = min(c->dst.bytes, 4u);
 	if (!emulator_io_permited(ctxt, c->src.val, c->dst.bytes))
 		return emulate_gp(ctxt, 0);
@@ -2955,6 +2964,15 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)
 {
 	struct decode_cache *c = &ctxt->decode;
 
+	switch (c->b) {
+	case 0x6e: /* outsb */

arch/x86/kvm/emulate.c static int check_perm_out(struct x86_emulate_ctxt *ctxt) · line 2978

Quoted 10 lines
arch/x86/kvm/emulate.c +7 -0
+	case 0x6f: /* outsw/outsd */
+	case 0xee: /* out dx,al */
+	case 0xef: /* out dx,(e/r)ax */
+		c->dst.val = c->regs[VCPU_REGS_RDX];
+		break;
+	}
+
 	c->src.bytes = min(c->src.bytes, 4u);
 	if (!emulator_io_permited(ctxt, c->dst.val, c->src.bytes))
 		return emulate_gp(ctxt, 0);

I'd rather do it at decoding stage by adding SrcDX/DstDX.

--
Gleb.

#2 lore raw
Quoted 7 lines · Gleb Natapov
Quoted 6 lines · Marcelo Tosatti

Commit fa4491a6b667304 moved the permission check for io instructions to the ->check_perm callback. It failed to copy the port value from RDX register for string and "in,out ax,dx" instructions. Fix it.

Fixes FC8.32 installation.

Ouch.

Quoted 16 lines
Quoted 4 lines

@@ -2955,6 +2964,15 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)
{
struct decode_cache *c =&ctxt->decode;

Quoted 12 lines

+ switch (c->b) {
+ case 0x6e: /* outsb */
+ case 0x6f: /* outsw/outsd */
+ case 0xee: /* out dx,al */
+ case 0xef: /* out dx,(e/r)ax */
+ c->dst.val = c->regs[VCPU_REGS_RDX];
+ break;
+ }
+
c->src.bytes = min(c->src.bytes, 4u);
if (!emulator_io_permited(ctxt, c->dst.val, c->src.bytes))
return emulate_gp(ctxt, 0);

Quoted 2 lines

I'd rather do it at decoding stage by adding SrcDX/DstDX.

Note we haven't decoded operands yet. And this doesn't fix in $imm8, %al.

Maybe we need an additional check site after operands are fetched.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#3 lore raw
Quoted 16 lines · Avi Kivity
Quoted 16 lines
Quoted 16 lines

@@ -2955,6 +2964,15 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)
{
struct decode_cache *c =&ctxt->decode;

+ switch (c->b) {
+ case 0x6e: /* outsb */
+ case 0x6f: /* outsw/outsd */
+ case 0xee: /* out dx,al */
+ case 0xef: /* out dx,(e/r)ax */
+ c->dst.val = c->regs[VCPU_REGS_RDX];
+ break;
+ }
+
c->src.bytes = min(c->src.bytes, 4u);
if (!emulator_io_permited(ctxt, c->dst.val, c->src.bytes))
return emulate_gp(ctxt, 0);

Quoted 5 lines
Quoted 2 lines

I'd rather do it at decoding stage by adding SrcDX/DstDX.

Note we haven't decoded operands yet. And this doesn't fix in $imm8, %al.

We haven't? check_perm is called from x86_emulate_insn() and operands are decode in x86_decode_insn(). So $imm8, %al should work now. Or am I missing something?

Quoted 5 lines

Maybe we need an additional check site after operands are fetched.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.

--
Gleb.

#4 lore raw
Quoted 5 lines · Gleb Natapov
Quoted 2 lines

Note we haven't decoded operands yet. And this doesn't fix in $imm8, %al.

We haven't? check_perm is called from x86_emulate_insn() and operands are decode in x86_decode_insn(). So $imm8, %al should work now. Or am I missing something?

No, you're right. SrcDX seems the simplest fix then.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#5 lore raw

KVM: x86: use proper port value when checking io instruction permission (v2)

In reply to Gleb Natapov

Commit fa4491a6b667304 moved the permission check for io instructions to the ->check_perm callback. It failed to copy the port value from RDX register for string and "in,out ax,dx" instructions.

Fix it by reading RDX register at decode stage when appropriate.

Fixes FC8.32 installation.

arch/x86/include/asm/kvm_emulate.h +1 -1
index ab09ba2..a448212 100644
@@ -243,7 +243,7 @@ struct decode_cache {
 	struct operand dst;
 	bool has_seg_override;
 	u8 seg_override;
-	unsigned int d;
+	u64 d;
 	int (*execute)(struct x86_emulate_ctxt *ctxt);
 	int (*check_perm)(struct x86_emulate_ctxt *ctxt);
 	unsigned long regs[NR_VCPU_REGS];
arch/x86/kvm/emulate.c +62 -48

151 lines not on this page · raw message

#6 lore raw

Re: KVM: x86: use proper port value when checking io instruction permission (v2)

Quoted 9 lines · Marcelo Tosatti

Commit fa4491a6b667304 moved the permission check for io instructions to the ->check_perm callback. It failed to copy the port value from RDX register for string and "in,out ax,dx" instructions.

Fix it by reading RDX register at decode stage when appropriate.

Fixes FC8.32 installation.

+#define Sse (1<<18) /* SSE Vector instruction */

19/20 are still available, no need to go 64-bit just yet.

arch/x86/kvm/emulate.c done_prefixes: · line 3587

Quoted 6 lines

/* Misc flags */

arch/x86/kvm/emulate.c +3 -1
-#define Prot        (1<<21) /* instruction generates #UD if not in prot-mode */
 
+	case SrcDX:
+		c->src.type = OP_REG;
+		c->src.bytes = c->op_bytes;

Needs to be 2. Otherwise we'll see extra bits from edx, or lose bits from dx if it's a 1-byte instruction.

arch/x86/kvm/emulate.c done_prefixes: · line 3662

Quoted 13 lines
arch/x86/kvm/emulate.c +6 -0
+		c->src.addr.reg =&c->regs[VCPU_REGS_RDX];
+		fetch_register_operand(&c->src);
+		break;
  	}
 
  	if (rc != X86EMUL_CONTINUE)
@@ -3649,6 +3657,12 @@ done_prefixes:
  		c->dst.addr.mem.seg = VCPU_SREG_ES;
  		c->dst.val = 0;
  		break;
+	case DstDX:
+		c->dst.type = OP_REG;
+		c->dst.bytes = c->op_bytes;

2 again.

arch/x86/kvm/emulate.c done_prefixes: · line 3668

Quoted 6 lines
arch/x86/kvm/emulate.c +3 -0
+		c->dst.addr.reg =&c->regs[VCPU_REGS_RDX];
+		fetch_register_operand(&c->dst);
+		break;
  	case ImplicitOps:
  		/* Special instructions do their own operand decoding. */
  	default:

We also need to unify Src/Dst decode eventually.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#7 lore raw

Re: KVM: x86: use proper port value when checking io instruction permission (v2)

Quoted 16 lines · Avi Kivity
Quoted 9 lines · Marcelo Tosatti

Commit fa4491a6b667304 moved the permission check for io instructions to the ->check_perm callback. It failed to copy the port value from RDX register for string and "in,out ax,dx" instructions.

Fix it by reading RDX register at decode stage when appropriate.

Fixes FC8.32 installation.

+#define Sse (1<<18) /* SSE Vector instruction */

19/20 are still available, no need to go 64-bit just yet.

Quoted 3 lines

/* Misc flags */
-#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */

Quoted 7 lines

arch/x86/kvm/emulate.c done_prefixes: · line 3587

Quoted 3 lines
arch/x86/kvm/emulate.c +3 -0
+	case SrcDX:
+		c->src.type = OP_REG;
+		c->src.bytes = c->op_bytes;

Needs to be 2. Otherwise we'll see extra bits from edx, or lose bits from dx if it's a 1-byte instruction.

But those extra bits will be dropped by check_perm_in() anyway. Can c->op_bytes ever be 1?

Quoted 16 lines

arch/x86/kvm/emulate.c done_prefixes: · line 3662

Quoted 13 lines
arch/x86/kvm/emulate.c +6 -0
+		c->src.addr.reg =&c->regs[VCPU_REGS_RDX];
+		fetch_register_operand(&c->src);
+		break;
 	}
 
 	if (rc != X86EMUL_CONTINUE)
@@ -3649,6 +3657,12 @@ done_prefixes:
 		c->dst.addr.mem.seg = VCPU_SREG_ES;
 		c->dst.val = 0;
 		break;
+	case DstDX:
+		c->dst.type = OP_REG;
+		c->dst.bytes = c->op_bytes;

2 again.

Quoted 12 lines

arch/x86/kvm/emulate.c done_prefixes: · line 3668

Quoted 6 lines
arch/x86/kvm/emulate.c +3 -0
+		c->dst.addr.reg =&c->regs[VCPU_REGS_RDX];
+		fetch_register_operand(&c->dst);
+		break;
 	case ImplicitOps:
 		/* Special instructions do their own operand decoding. */
 	default:

We also need to unify Src/Dst decode eventually.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.

--
Gleb.

#8 lore raw

Re: KVM: x86: use proper port value when checking io instruction permission (v2)

Quoted 9 lines · Gleb Natapov
Quoted 8 lines

arch/x86/kvm/emulate.c done_prefixes: · line 3587

Quoted 4 lines
arch/x86/kvm/emulate.c +3 -0
+	case SrcDX:
+		c->src.type = OP_REG;
+		c->src.bytes = c->op_bytes;

Needs to be 2. Otherwise we'll see extra bits from edx, or lose bits from dx if it's a 1-byte instruction.

But those extra bits will be dropped by check_perm_in() anyway.

It isn't nice to depend on it.

btw, Marcelo, the patch should also make use of the decode during execution:

    case 0xef: /* out dx,(e/r)ax */
        c->dst.val = c->regs[VCPU_REGS_RDX];

^^ can drop

    do_io_out:
        ops->pio_out_emulated(ctxt, c->src.bytes, c->dst.val,
&c->src.val, 1);
        c->dst.type = OP_NONE;    /* Disable writeback. */
        break;
Quoted 2 lines

Can
c->op_bytes ever be 1?

in %dx, %al

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#9 lore raw

Re: KVM: x86: use proper port value when checking io instruction permission (v2)

Quoted 6 lines · Avi Kivity
Quoted 3 lines

Can
c->op_bytes ever be 1?

in %dx, %al

er, that doesn't change op_bytes. Still, op_bytes is irrelevant for SrcDX, the 16-bit version is always used.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#10 lore raw

Re: KVM: x86: use proper port value when checking io instruction permission (v2)

Quoted 9 lines · Avi Kivity
Quoted 6 lines · Avi Kivity
Quoted 3 lines

Can
c->op_bytes ever be 1?

in %dx, %al

er, that doesn't change op_bytes.

Yep.

Quoted 3 lines

Still, op_bytes is irrelevant for
SrcDX, the 16-bit version is always used.

If SrcDX/DstDX will be used only for decoding in/out instruction then yes. Otherwise it is nice to have more general decoder.

--
Gleb.

maintainer
#11 lore raw

Re: KVM: x86: use proper port value when checking io instruction permission (v2)

Quoted 5 lines · Gleb Natapov
Quoted 2 lines

Still, op_bytes is irrelevant for
SrcDX, the 16-bit version is always used.

If SrcDX/DstDX will be used only for decoding in/out instruction then yes. Otherwise it is nice to have more general decoder.

Not counting instructions that read/write many registers (rdmsr/wrmsr, mul/imul/div/idiv, rdtsc, etc.), I think the only other instruction with an implicit DstDX is cwd/cdq/cqo. Since cwd/cdq/cqo needs c->dst.bytes = c->src.bytes (not op_bytes) I think DstDX is not really reusable beyond port instructions.

Paolo

#12 lore raw

Re: KVM: x86: use proper port value when checking io instruction permission (v2)

Quoted 4 lines · Paolo Bonzini
Quoted 3 lines · Gleb Natapov
Quoted 2 lines

Still, op_bytes is irrelevant for
SrcDX, the 16-bit version is always used.

Quoted 9 lines
Quoted 2 lines

If SrcDX/DstDX will be used only for decoding in/out instruction then yes. Otherwise it is nice to have more general decoder.

Not counting instructions that read/write many registers (rdmsr/wrmsr, mul/imul/div/idiv, rdtsc, etc.), I think the only other instruction with an implicit DstDX is cwd/cdq/cqo. Since cwd/cdq/cqo needs c->dst.bytes = c->src.bytes (not op_bytes) I think DstDX is not really reusable beyond port instructions.

Why would c->dst.bytes != c->src.bytes for cwd/cdq/cqo if we'll set c->dst.bytes to op_bytes during decode?

--
Gleb.

maintainer
#13 lore raw

Re: KVM: x86: use proper port value when checking io instruction permission (v2)

Quoted 2 lines · Gleb Natapov

Why would c->dst.bytes != c->src.bytes for cwd/cdq/cqo if we'll set c->dst.bytes to op_bytes during decode?

Duh, you're right, cwd/cdq/cqo uses SrcAcc which has

                c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;

so in practice c->src.bytes = c->op_bytes.

I still find it confusing that DstDX would use c->op_bytes without honoring ByteOp unlike pretty much everything else; but yes, there is a possible use of DstDX outside in/out.

Paolo

maintainer
#15 lore raw

Re: KVM: x86: use proper port value when checking io instruction permission (v2)

In reply to Gleb Natapov
Quoted 2 lines · Gleb Natapov

We can make it honor ByteOp. There will be no instruction that will specify DstDX | ByteOp though.

"in %dx, %al" and "out %al, %dx" will via D2bv.

Paolo

#16 lore raw

Re: KVM: x86: use proper port value when checking io instruction permission (v2)

Quoted 6 lines · Paolo Bonzini
Quoted 2 lines · Gleb Natapov

We can make it honor ByteOp. There will be no instruction that will specify DstDX | ByteOp though.

"in %dx, %al" and "out %al, %dx" will via D2bv.

Yeah. Should ignore ByteOp then.

--
Gleb.

#14 lore raw

Re: KVM: x86: use proper port value when checking io instruction permission (v2)

In reply to Paolo Bonzini
Quoted 6 lines · Paolo Bonzini
Quoted 2 lines · Gleb Natapov

Why would c->dst.bytes != c->src.bytes for cwd/cdq/cqo if we'll set c->dst.bytes to op_bytes during decode?

Duh, you're right, cwd/cdq/cqo uses SrcAcc which has

Quoted 8 lines
                c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;

so in practice c->src.bytes = c->op_bytes.

I still find it confusing that DstDX would use c->op_bytes without honoring ByteOp unlike pretty much everything else; but yes, there is a possible use of DstDX outside in/out.

We can make it honor ByteOp. There will be no instruction that will specify DstDX | ByteOp though.

--
Gleb.

#17 lore raw

Re: KVM: x86: use proper port value when checking io instruction permission (v2)

In reply to Paolo Bonzini
Quoted 6 lines · Paolo Bonzini
Quoted 5 lines · Gleb Natapov
Quoted 2 lines

Still, op_bytes is irrelevant for
SrcDX, the 16-bit version is always used.

If SrcDX/DstDX will be used only for decoding in/out instruction then yes. Otherwise it is nice to have more general decoder.

Yes, the use of op_bytes instead of 2 had that in mind.

Quoted 7 lines

Not counting instructions that read/write many registers (rdmsr/wrmsr, mul/imul/div/idiv, rdtsc, etc.), I think the only other instruction with an implicit DstDX is cwd/cdq/cqo. Since cwd/cdq/cqo needs c->dst.bytes = c->src.bytes (not op_bytes) I think DstDX is not really reusable beyond port instructions.

Paolo

OK, will switch to 2 then, thanks.

#18 lore raw

KVM: x86: use proper port value when checking io instruction permission (v3)

In reply to Avi Kivity

Commit fa4491a6b667304 moved the permission check for io instructions to the ->check_perm callback. It failed to copy the port value from RDX register for string and "in,out ax,dx" instructions.

Fix it by reading RDX register at decode stage when appropriate.

Fixes FC8.32 installation.

arch/x86/kvm/emulate.c +20 -8
index 3bc6b7a..fc3d2d9 100644
@@ -47,7 +47,7 @@
 #define DstDI       (5<<1)	/* Destination is in ES:(E)DI */
 #define DstMem64    (6<<1)	/* 64bit memory operand */
 #define DstImmUByte (7<<1)	/* 8-bit unsigned immediate operand */
-#define DstMask     (7<<1)
+#define DstMask     ((7<<1) | (1<<18))
 /* Source operand type. */
 #define SrcNone     (0<<4)	/* No source operand. */
 #define SrcReg      (1<<4)	/* Register operand. */
@@ -64,7 +64,7 @@
 #define SrcMemFAddr (0xc<<4)	/* Source is far address in memory */
 #define SrcAcc      (0xd<<4)	/* Source Accumulator */
 #define SrcImmU16   (0xe<<4)    /* Immediate operand, unsigned, 16 bits */
-#define SrcMask     (0xf<<4)
+#define SrcMask     ((0xf<<4) | (1<<19))
 /* Generic ModRM decode. */
 #define ModRM       (1<<8)
 /* Destination is only written; never read. */
@@ -79,6 +79,8 @@
 #define Prefix      (3<<14)     /* Instruction varies with 66/f2/f3 prefix */
 #define RMExt       (4<<14)     /* Opcode extension in ModRM r/m if mod == 3 */
 #define Sse         (1<<17)     /* SSE Vector instruction */
+#define DstDX       (1<<18)	/* Destination is in DX register */
+#define SrcDX       (1<<19)	/* Source is in DX register */
 /* Misc flags */
 #define Prot        (1<<21) /* instruction generates #UD if not in prot-mode */
 #define VendorSpecific (1<<22) /* Vendor specific instruction */
@@ -3124,8 +3126,8 @@ static struct opcode opcode_table[256] = {
 	I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
 	I(SrcImmByte | Mov | Stack, em_push),
 	I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
-	D2bvIP(DstDI | Mov | String, ins, check_perm_in), /* insb, insw/insd */
-	D2bvIP(SrcSI | ImplicitOps | String, outs, check_perm_out), /* outsb, outsw/outsd */
+	D2bvIP(DstDI | SrcDX | Mov | String, ins, check_perm_in), /* insb, insw/insd */
+	D2bvIP(SrcSI | DstDX | String, outs, check_perm_out), /* outsb, outsw/outsd */
 	/* 0x70 - 0x7F */
 	X16(D(SrcImmByte)),
 	/* 0x80 - 0x87 */
@@ -3182,8 +3184,8 @@ static struct opcode opcode_table[256] = {
 	/* 0xE8 - 0xEF */
 	D(SrcImm | Stack), D(SrcImm | ImplicitOps),
 	D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
-	D2bvIP(SrcNone | DstAcc,     in,  check_perm_in),
-	D2bvIP(SrcAcc | ImplicitOps, out, check_perm_out),
+	D2bvIP(SrcDX | DstAcc, in,  check_perm_in),
+	D2bvIP(SrcAcc | DstDX, out, check_perm_out),
 	/* 0xF0 - 0xF7 */
 	N, DI(ImplicitOps, icebp), N, N,
 	DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
@@ -3580,6 +3582,12 @@ done_prefixes:
 		memop.bytes = c->op_bytes + 2;
 		goto srcmem_common;
 		break;
+	case SrcDX:
+		c->src.type = OP_REG;
+		c->src.bytes = 2;
+		c->src.addr.reg = &c->regs[VCPU_REGS_RDX];
+		fetch_register_operand(&c->src);
+		break;
 	}
 
 	if (rc != X86EMUL_CONTINUE)
@@ -3649,6 +3657,12 @@ done_prefixes:
 		c->dst.addr.mem.seg = VCPU_SREG_ES;
 		c->dst.val = 0;
 		break;
+	case DstDX:
+		c->dst.type = OP_REG;
+		c->dst.bytes = 2;
+		c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
+		fetch_register_operand(&c->dst);
+		break;
 	case ImplicitOps:
 		/* Special instructions do their own operand decoding. */
 	default:
@@ -3993,7 +4007,6 @@ special_insn:
 		break;
 	case 0xec: /* in al,dx */
 	case 0xed: /* in (e/r)ax,dx */
-		c->src.val = c->regs[VCPU_REGS_RDX];
 	do_io_in:
 		if (!pio_in_emulated(ctxt, c->dst.bytes, c->src.val,
 				     &c->dst.val))
@@ -4001,7 +4014,6 @@ special_insn:
 		break;
 	case 0xee: /* out dx,al */
 	case 0xef: /* out dx,(e/r)ax */
-		c->dst.val = c->regs[VCPU_REGS_RDX];
 	do_io_out:
 		ops->pio_out_emulated(ctxt, c->src.bytes, c->dst.val,
 				      &c->src.val, 1);
#19 lore raw

Re: KVM: x86: use proper port value when checking io instruction permission (v3)

Quoted 13 lines · Marcelo Tosatti

Commit fa4491a6b667304 moved the permission check for io instructions to the ->check_perm callback. It failed to copy the port value from RDX register for string and "in,out ax,dx" instructions.

Fix it by reading RDX register at decode stage when appropriate.

Fixes FC8.32 installation.

arch/x86/kvm/emulate.c +0 -0
index 3bc6b7a..fc3d2d9 100644

arch/x86/kvm/emulate.c line 50

Quoted 5 lines
arch/x86/kvm/emulate.c +1 -1
@@ -47,7 +47,7 @@
  #define DstDI       (5<<1)	/* Destination is in ES:(E)DI */
  #define DstMem64    (6<<1)	/* 64bit memory operand */
  #define DstImmUByte (7<<1)	/* 8-bit unsigned immediate operand */
-#define DstMask     (7<<1)
+#define DstMask     ((7<<1) | (1<<18))

Ouch, why??

My comment about bits 19-20 was only about the shifting of of Prot etc. from 21+ to 22+.

Signature · 1 line
error compiling committee.c: too many arguments to function
#20 lore raw

KVM: x86: use proper port value when checking io instruction permission (v4)

Commit fa4491a6b667304 moved the permission check for io instructions to the ->check_perm callback. It failed to copy the port value from RDX register for string and "in,out ax,dx" instructions.

Fix it by reading RDX register at decode stage when appropriate.

Fixes FC8.32 installation.

arch/x86/kvm/emulate.c +47 -35
index 3bc6b7a..01c295f 100644
@@ -47,38 +47,40 @@
 #define DstDI       (5<<1)	/* Destination is in ES:(E)DI */
 #define DstMem64    (6<<1)	/* 64bit memory operand */
 #define DstImmUByte (7<<1)	/* 8-bit unsigned immediate operand */
-#define DstMask     (7<<1)
+#define DstDX       (8<<1)	/* Destination is in DX register */
+#define DstMask     (0xf<<1)
 /* Source operand type. */
-#define SrcNone     (0<<4)	/* No source operand. */
-#define SrcReg      (1<<4)	/* Register operand. */
-#define SrcMem      (2<<4)	/* Memory operand. */
-#define SrcMem16    (3<<4)	/* Memory operand (16-bit). */
-#define SrcMem32    (4<<4)	/* Memory operand (32-bit). */
-#define SrcImm      (5<<4)	/* Immediate operand. */
-#define SrcImmByte  (6<<4)	/* 8-bit sign-extended immediate operand. */
-#define SrcOne      (7<<4)	/* Implied '1' */
-#define SrcImmUByte (8<<4)      /* 8-bit unsigned immediate operand. */
-#define SrcImmU     (9<<4)      /* Immediate operand, unsigned */
-#define SrcSI       (0xa<<4)	/* Source is in the DS:RSI */
-#define SrcImmFAddr (0xb<<4)	/* Source is immediate far address */
-#define SrcMemFAddr (0xc<<4)	/* Source is far address in memory */
-#define SrcAcc      (0xd<<4)	/* Source Accumulator */
-#define SrcImmU16   (0xe<<4)    /* Immediate operand, unsigned, 16 bits */
-#define SrcMask     (0xf<<4)
+#define SrcNone     (0<<5)	/* No source operand. */
+#define SrcReg      (1<<5)	/* Register operand. */
+#define SrcMem      (2<<5)	/* Memory operand. */
+#define SrcMem16    (3<<5)	/* Memory operand (16-bit). */
+#define SrcMem32    (4<<5)	/* Memory operand (32-bit). */
+#define SrcImm      (5<<5)	/* Immediate operand. */
+#define SrcImmByte  (6<<5)	/* 8-bit sign-extended immediate operand. */
+#define SrcOne      (7<<5)	/* Implied '1' */
+#define SrcImmUByte (8<<5)      /* 8-bit unsigned immediate operand. */
+#define SrcImmU     (9<<5)      /* Immediate operand, unsigned */
+#define SrcSI       (0xa<<5)	/* Source is in the DS:RSI */
+#define SrcImmFAddr (0xb<<5)	/* Source is immediate far address */
+#define SrcMemFAddr (0xc<<5)	/* Source is far address in memory */
+#define SrcAcc      (0xd<<5)	/* Source Accumulator */
+#define SrcImmU16   (0xe<<5)    /* Immediate operand, unsigned, 16 bits */
+#define SrcDX       (0xf<<5)	/* Source is in DX register */
+#define SrcMask     (0xf<<5)
 /* Generic ModRM decode. */
-#define ModRM       (1<<8)
+#define ModRM       (1<<9)
 /* Destination is only written; never read. */
-#define Mov         (1<<9)
-#define BitOp       (1<<10)
-#define MemAbs      (1<<11)      /* Memory operand is absolute displacement */
-#define String      (1<<12)     /* String instruction (rep capable) */
-#define Stack       (1<<13)     /* Stack instruction (push/pop) */
-#define GroupMask   (7<<14)     /* Opcode uses one of the group mechanisms */
-#define Group       (1<<14)     /* Bits 3:5 of modrm byte extend opcode */
-#define GroupDual   (2<<14)     /* Alternate decoding of mod == 3 */
-#define Prefix      (3<<14)     /* Instruction varies with 66/f2/f3 prefix */
-#define RMExt       (4<<14)     /* Opcode extension in ModRM r/m if mod == 3 */
-#define Sse         (1<<17)     /* SSE Vector instruction */
+#define Mov         (1<<10)
+#define BitOp       (1<<11)
+#define MemAbs      (1<<12)      /* Memory operand is absolute displacement */
+#define String      (1<<13)     /* String instruction (rep capable) */
+#define Stack       (1<<14)     /* Stack instruction (push/pop) */
+#define GroupMask   (7<<15)     /* Opcode uses one of the group mechanisms */
+#define Group       (1<<15)     /* Bits 3:5 of modrm byte extend opcode */
+#define GroupDual   (2<<15)     /* Alternate decoding of mod == 3 */
+#define Prefix      (3<<15)     /* Instruction varies with 66/f2/f3 prefix */
+#define RMExt       (4<<15)     /* Opcode extension in ModRM r/m if mod == 3 */
+#define Sse         (1<<18)     /* SSE Vector instruction */
 /* Misc flags */
 #define Prot        (1<<21) /* instruction generates #UD if not in prot-mode */
 #define VendorSpecific (1<<22) /* Vendor specific instruction */
@@ -3124,8 +3126,8 @@ static struct opcode opcode_table[256] = {
 	I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
 	I(SrcImmByte | Mov | Stack, em_push),
 	I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
-	D2bvIP(DstDI | Mov | String, ins, check_perm_in), /* insb, insw/insd */
-	D2bvIP(SrcSI | ImplicitOps | String, outs, check_perm_out), /* outsb, outsw/outsd */
+	D2bvIP(DstDI | SrcDX | Mov | String, ins, check_perm_in), /* insb, insw/insd */
+	D2bvIP(SrcSI | DstDX | String, outs, check_perm_out), /* outsb, outsw/outsd */
 	/* 0x70 - 0x7F */
 	X16(D(SrcImmByte)),
 	/* 0x80 - 0x87 */
@@ -3182,8 +3184,8 @@ static struct opcode opcode_table[256] = {
 	/* 0xE8 - 0xEF */
 	D(SrcImm | Stack), D(SrcImm | ImplicitOps),
 	D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
-	D2bvIP(SrcNone | DstAcc,     in,  check_perm_in),
-	D2bvIP(SrcAcc | ImplicitOps, out, check_perm_out),
+	D2bvIP(SrcDX | DstAcc, in,  check_perm_in),
+	D2bvIP(SrcAcc | DstDX, out, check_perm_out),
 	/* 0xF0 - 0xF7 */
 	N, DI(ImplicitOps, icebp), N, N,
 	DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
@@ -3580,6 +3582,12 @@ done_prefixes:
 		memop.bytes = c->op_bytes + 2;
 		goto srcmem_common;
 		break;
+	case SrcDX:
+		c->src.type = OP_REG;
+		c->src.bytes = 2;
+		c->src.addr.reg = &c->regs[VCPU_REGS_RDX];
+		fetch_register_operand(&c->src);
+		break;
 	}
 
 	if (rc != X86EMUL_CONTINUE)
@@ -3649,6 +3657,12 @@ done_prefixes:
 		c->dst.addr.mem.seg = VCPU_SREG_ES;
 		c->dst.val = 0;
 		break;
+	case DstDX:
+		c->dst.type = OP_REG;
+		c->dst.bytes = 2;
+		c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
+		fetch_register_operand(&c->dst);
+		break;
 	case ImplicitOps:
 		/* Special instructions do their own operand decoding. */
 	default:
@@ -3993,7 +4007,6 @@ special_insn:
 		break;
 	case 0xec: /* in al,dx */
 	case 0xed: /* in (e/r)ax,dx */
-		c->src.val = c->regs[VCPU_REGS_RDX];
 	do_io_in:
 		if (!pio_in_emulated(ctxt, c->dst.bytes, c->src.val,
 				     &c->dst.val))
@@ -4001,7 +4014,6 @@ special_insn:
 		break;
 	case 0xee: /* out dx,al */
 	case 0xef: /* out dx,(e/r)ax */
-		c->dst.val = c->regs[VCPU_REGS_RDX];
 	do_io_out:
 		ops->pio_out_emulated(ctxt, c->src.bytes, c->dst.val,
 				      &c->src.val, 1);
#22 lore raw

Re: KVM: x86: use proper port value when checking io instruction permission (v3)

In reply to Avi Kivity
Quoted 22 lines · Avi Kivity
Quoted 13 lines · Marcelo Tosatti

Commit fa4491a6b667304 moved the permission check for io instructions to the ->check_perm callback. It failed to copy the port value from RDX register for string and "in,out ax,dx" instructions.

Fix it by reading RDX register at decode stage when appropriate.

Fixes FC8.32 installation.

arch/x86/kvm/emulate.c +0 -0
index 3bc6b7a..fc3d2d9 100644

arch/x86/kvm/emulate.c line 50

Quoted 5 lines
arch/x86/kvm/emulate.c +1 -1
@@ -47,7 +47,7 @@
 #define DstDI       (5<<1)	/* Destination is in ES:(E)DI */
 #define DstMem64    (6<<1)	/* 64bit memory operand */
 #define DstImmUByte (7<<1)	/* 8-bit unsigned immediate operand */
-#define DstMask     (7<<1)
+#define DstMask     ((7<<1) | (1<<18))

Ouch, why??

Quoted 2 lines

My comment about bits 19-20 was only about the shifting of of Prot etc. from 21+ to 22+.

Oh, OK, please check v4.

#21 lore raw

Re: KVM: x86: use proper port value when checking io instruction permission (v4)

In reply to Marcelo Tosatti
Quoted 8 lines · Marcelo Tosatti

Commit fa4491a6b667304 moved the permission check for io instructions to the ->check_perm callback. It failed to copy the port value from RDX register for string and "in,out ax,dx" instructions.

Fix it by reading RDX register at decode stage when appropriate.

Fixes FC8.32 installation.

Looks good.

We should really unify Src/Dst encoding and fetching.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.