Skip to content

Commit

Permalink
KVM: x86: Emulator does not calculate address correctly
Browse files Browse the repository at this point in the history
In long-mode, when the address size is 4 bytes, the linear address is not
truncated as the emulator mistakenly does.  Instead, the offset within the
segment (the ea field) should be truncated according to the address size.

As Intel SDM says: "In 64-bit mode, the effective address components are added
and the effective address is truncated ... before adding the full 64-bit
segment base."

Signed-off-by: Nadav Amit <namit@cs.technion.ac.il>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
Nadav Amit authored and Paolo Bonzini committed Nov 3, 2014
1 parent 6bdf066 commit 518547b
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions arch/x86/kvm/emulate.c
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,8 @@ static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
u16 sel;
unsigned cpl;

la = seg_base(ctxt, addr.seg) + addr.ea;
la = seg_base(ctxt, addr.seg) +
(fetch || ctxt->ad_bytes == 8 ? addr.ea : (u32)addr.ea);
*max_size = 0;
switch (ctxt->mode) {
case X86EMUL_MODE_PROT64:
Expand Down Expand Up @@ -717,7 +718,7 @@ static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
}
break;
}
if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
if (ctxt->mode != X86EMUL_MODE_PROT64)
la &= (u32)-1;
if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
return emulate_gp(ctxt, 0);
Expand Down

0 comments on commit 518547b

Please sign in to comment.