next round of emulator cleanups
v2 cover next round of emulator cleanups
This is the next round of emulator cleanups. Make it even more detached from kvm. First patch introduces IO read cache which is needed to correctly emulate instructions that require more then one IO read exit during emulation.
ChangeLog;
v1->v2:
- add x86_emulator_ops callback to get cached segment base
- use double underscore on helper functions
- fold "remove unneeded initialization" patch
Gleb Natapov (23):
KVM: x86 emulator: introduce read cache.
KVM: x86 emulator: fix Move r/m16 to segment register decoding.
KVM: x86 emulator: cleanup xchg emulation.
KVM: x86 emulator: cleanup nop emulation
KVM: x86 emulator: handle "far address" source operand.
KVM: x86 emulator: add (set|get)_dr callbacks to x86_emulate_ops
KVM: x86 emulator: add (set|get)_msr callbacks to x86_emulate_ops
KVM: x86 emulator: add get_cached_segment_base() callback to
x86_emulate_ops.
KVM: x86 emulator: cleanup some direct calls into kvm to use existing
callbacks
KVM: x86 emulator: make set_cr() callback return error if it fails
KVM: x86 emulator: make (get|set)_dr() callback return error if it
fails
KVM: x86 emulator: fix X86EMUL_RETRY_INSTR and X86EMUL_CMPXCHG_FAILED
values
KVM: fill in run->mmio details in (read|write)_emulated function.
KVM: x86 emulator: x86_emulate_insn() return -1 only in case of
emulation failure
KVM: remove export of emulator_write_emulated().
KVM: do not inject #PF in (read|write)_emulated() callbacks
KVM: handle emulation failure case first.
KVM: x86 emulator: advance RIP outside x86 emulator code
KVM: x86 emulator: set RFLAGS outside x86 emulator code.
KVM: x86 emulator: use shadowed register in emulate_sysexit()
KVM: x86 exmulator: handle shadowed registers outside emulator.
KVM: x86 emulator: move interruptibility state tracking out of
emulator
KVM: x86 emulator: do not inject exception directly into vcpu
arch/x86/include/asm/kvm_emulate.h | 30 ++- arch/x86/include/asm/kvm_host.h | 9 - arch/x86/kvm/emulate.c | 559 ++++++++++++++++++++---------------- arch/x86/kvm/x86.c | 382 ++++++++++++++----------- 4 files changed, 549 insertions(+), 431 deletions(-)
PATCH v2 1/23 KVM: x86 emulator: introduce read cache.
Introduce read cache which is needed for instruction that require more then one exit to userspace. After returning from userspace the instruction will be re-executed with cached read value.
--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/kvm/emulate.c | 56 +++++++++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 14 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
@@ -186,6 +186,7 @@ struct decode_cache {unsigned long modrm_val;struct fetch_cache fetch;struct read_cache io_read; +struct read_cache mem_read; };struct x86_emulate_ctxt {
arch/x86/kvm/emulate.c +42 -14
98 lines not on this page · raw message
Signature · 1 line
1.6.5
PATCH v2 2/23 KVM: x86 emulator: fix Move r/m16 to segment register decoding.
In reply to Gleb NatapovThis instruction does not need generic decoding for its dst operand.
--- arch/x86/kvm/emulate.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kvm/emulate.c +1 -1
@@ -171,7 +171,7 @@ static u32 opcode_table[256] = { ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov, ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, DstMem | SrcReg | ModRM | Mov, ModRM | DstReg, - DstReg | SrcMem | ModRM | Mov, Group | Group1A, + ImplicitOps | SrcMem | ModRM, Group | Group1A,/* 0x90 - 0x97 */ DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, /* 0x98 - 0x9F */
Signature · 1 line
1.6.5
PATCH v2 3/23 KVM: x86 emulator: cleanup xchg emulation.
In reply to Gleb NatapovDst operand is already initialized during decoding stage. No need to reinitialize.
--- arch/x86/kvm/emulate.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +2 -2
@@ -2804,8 +2804,8 @@ special_insn:break ; } case 0x91 ... 0x97: /* xchg reg,rax */ - c->src.type = c->dst.type =OP_REG ; - c->src.bytes = c->dst.bytes = c->op_bytes; + c->src.type =OP_REG ; + c->src.bytes = c->op_bytes; c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX ]; c->src.val = *(c->src.ptr); goto xchg;
Signature · 1 line
1.6.5
PATCH v2 4/23 KVM: x86 emulator: cleanup nop emulation
In reply to Gleb NatapovMake it more explicit what we are checking for.
--- arch/x86/kvm/emulate.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +2 -2
@@ -2799,8 +2799,8 @@ special_insn: goto done;break ;case 0x90 :/* nop / xchg r8,rax */ -if (!(c->rex_prefix &1 )) {/* nop */ - c->dst.type =OP_NONE ; +if (c->dst.ptr == (unsigned long *)&c->regs[VCPU_REGS_RAX ]) { + c->dst.type =OP_NONE ;/* nop */ break ; } case 0x91 ... 0x97: /* xchg reg,rax */
Signature · 1 line
1.6.5
PATCH v2 5/23 KVM: x86 emulator: handle "far address" source operand.
In reply to Gleb Natapovljmp/lcall instruction operand contains address and segment. It can be 10 bytes long. Currently we decode it as two different operands. Fix it by introducing new kind of operand that can hold entire far address.
--- arch/x86/include/asm/kvm_emulate.h | 6 +++- arch/x86/kvm/emulate.c | 56 ++++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 25 deletions(-)
arch/x86/include/asm/kvm_emulate.h +5 -1
@@ -143,7 +143,11 @@ struct x86_emulate_ops {struct operand {enum {OP_REG ,OP_MEM ,OP_IMM ,OP_NONE } type;unsigned int bytes; -unsigned long val, orig_val, *ptr; +unsigned long orig_val, *ptr; +union { +unsigned long val; +char valptr[sizeof (unsigned long ) +2 ]; + }; };struct fetch_cache {
arch/x86/kvm/emulate.c +32 -24
124 lines not on this page · raw message
Signature · 1 line
1.6.5
PATCH v2 6/23 KVM: x86 emulator: add (set|get)_dr callbacks to x86_emulate_ops
In reply to Gleb NatapovAdd (set|get)_dr callbacks to x86_emulate_ops instead of calling them directly.
--- arch/x86/include/asm/kvm_emulate.h | 2 ++ arch/x86/include/asm/kvm_host.h | 4 ---- arch/x86/kvm/emulate.c | 7 +++++-- arch/x86/kvm/x86.c | 12 ++++++------ 4 files changed, 13 insertions(+), 12 deletions(-)
arch/x86/include/asm/kvm_emulate.h +2 -0
@@ -137,6 +137,8 @@ struct x86_emulate_ops {void (*set_cr)(int cr,ulong val,struct kvm_vcpu *vcpu);int (*cpl)(struct kvm_vcpu *vcpu);void (*set_rflags)(struct kvm_vcpu *vcpu,unsigned long rflags); +int (*get_dr)(int dr,unsigned long *dest,struct kvm_vcpu *vcpu); +int (*set_dr)(int dr,unsigned long value,struct kvm_vcpu *vcpu); };/* Type, address-of, and value of an instruction's operand. */
arch/x86/include/asm/kvm_host.h +0 -4
@@ -590,10 +590,6 @@ void kvm_emulate_cpuid(struct kvm_vcpu *vcpu);int kvm_emulate_halt (struct kvm_vcpu *vcpu);int emulate_invlpg (struct kvm_vcpu *vcpu,gva_t address);int emulate_clts (struct kvm_vcpu *vcpu); -int emulator_get_dr (struct x86_emulate_ctxt *ctxt,int dr, -unsigned long *dest); -int emulator_set_dr (struct x86_emulate_ctxt *ctxt,int dr, -unsigned long value);void kvm_get_segment (struct kvm_vcpu *vcpu,struct kvm_segment *var,int seg);int kvm_load_segment_descriptor (struct kvm_vcpu *vcpu,u16 selector,int seg);
arch/x86/kvm/emulate.c +5 -2
@@ -3132,7 +3132,7 @@ twobyte_insn:kvm_queue_exception (ctxt->vcpu,UD_VECTOR ); goto done; } -emulator_get_dr (ctxt, c->modrm_reg, &c->regs[c->modrm_rm]); + ops->get_dr (c->modrm_reg, &c->regs[c->modrm_rm], ctxt->vcpu); c->dst.type =OP_NONE ;/* no writeback */ break ;case 0x22 :/* mov reg, cr */
@@ -3145,7 +3145,10 @@ twobyte_insn:kvm_queue_exception (ctxt->vcpu,UD_VECTOR ); goto done; } -emulator_set_dr (ctxt, c->modrm_reg, c->regs[c->modrm_rm]); + + ops->set_dr (c->modrm_reg,c->regs[c->modrm_rm] & + ((ctxt->mode ==X86EMUL_MODE_PROT64 ) ? ~0ULL : ~0U ), + ctxt->vcpu); c->dst.type =OP_NONE ;/* no writeback */ break ;case 0x30 :
arch/x86/kvm/x86.c +6 -6
@@ -3558,16 +3558,14 @@ int emulate_clts(struct kvm_vcpu *vcpu)return X86EMUL_CONTINUE ; } -int emulator_get_dr (struct x86_emulate_ctxt *ctxt,int dr,unsigned long *dest) +int emulator_get_dr (int dr,unsigned long *dest,struct kvm_vcpu *vcpu) { -return kvm_get_dr (ctxt->vcpu, dr, dest); +return kvm_get_dr (vcpu, dr, dest); } -int emulator_set_dr (struct x86_emulate_ctxt *ctxt,int dr,unsigned long value) +int emulator_set_dr (int dr,unsigned long value,struct kvm_vcpu *vcpu) { -unsigned long mask = (ctxt->mode ==X86EMUL_MODE_PROT64 ) ? ~0ULL : ~0U ; - -return kvm_set_dr (ctxt->vcpu, dr, value & mask); +return kvm_set_dr (vcpu, dr, value); }void kvm_report_emulation_failure (struct kvm_vcpu *vcpu,const char *context)
@@ -3749,6 +3747,8 @@ static struct x86_emulate_ops emulate_ops = { .set_cr = emulator_set_cr, .cpl = emulator_get_cpl, .set_rflags = emulator_set_rflags, + .get_dr = emulator_get_dr, + .set_dr = emulator_set_dr, };static void cache_all_regs (struct kvm_vcpu *vcpu)
Signature · 1 line
1.6.5
PATCH v2 7/23 KVM: x86 emulator: add (set|get)_msr callbacks to x86_emulate_ops
In reply to Gleb NatapovAdd (set|get)_msr callbacks to x86_emulate_ops instead of calling them directly.
--- arch/x86/include/asm/kvm_emulate.h | 2 ++ arch/x86/kvm/emulate.c | 36 ++++++++++++++++++------------------ arch/x86/kvm/x86.c | 2 ++ 3 files changed, 22 insertions(+), 18 deletions(-)
arch/x86/include/asm/kvm_emulate.h +2 -0
@@ -139,6 +139,8 @@ struct x86_emulate_ops {void (*set_rflags)(struct kvm_vcpu *vcpu,unsigned long rflags);int (*get_dr)(int dr,unsigned long *dest,struct kvm_vcpu *vcpu);int (*set_dr)(int dr,unsigned long value,struct kvm_vcpu *vcpu); +int (*set_msr)(struct kvm_vcpu *vcpu,u32 msr_index,u64 data); +int (*get_msr)(struct kvm_vcpu *vcpu,u32 msr_index,u64 *pdata); };/* Type, address-of, and value of an instruction's operand. */
arch/x86/kvm/emulate.c +18 -18
@@ -1875,7 +1875,7 @@ setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, }static int -emulate_syscall (struct x86_emulate_ctxt *ctxt) +emulate_syscall (struct x86_emulate_ctxt *ctxt,struct x86_emulate_ops *ops) {struct decode_cache *c = &ctxt->decode;struct kvm_segment cs, ss;
@@ -1890,7 +1890,7 @@ emulate_syscall(struct x86_emulate_ctxt *ctxt)setup_syscalls_segments (ctxt, &cs, &ss); - kvm_x86_ops->get_msr (ctxt->vcpu,MSR_STAR , &msr_data); + ops->get_msr (ctxt->vcpu,MSR_STAR , &msr_data); msr_data >>=32 ; cs.selector = (u16)(msr_data &0xfffc ); ss.selector = (u16)(msr_data +8 );
@@ -1907,17 +1907,17 @@ emulate_syscall(struct x86_emulate_ctxt *ctxt)#ifdef CONFIG_X86_64 c->regs[VCPU_REGS_R11 ] = ctxt->eflags & ~EFLG_RF ; - kvm_x86_ops->get_msr (ctxt->vcpu, - ctxt->mode ==X86EMUL_MODE_PROT64 ? -MSR_LSTAR :MSR_CSTAR , &msr_data); + ops->get_msr (ctxt->vcpu, + ctxt->mode ==X86EMUL_MODE_PROT64 ? +MSR_LSTAR :MSR_CSTAR , &msr_data); c->eip = msr_data; - kvm_x86_ops->get_msr (ctxt->vcpu,MSR_SYSCALL_MASK , &msr_data); + ops->get_msr (ctxt->vcpu,MSR_SYSCALL_MASK , &msr_data); ctxt->eflags &= ~(msr_data |EFLG_RF );#endif }else {/* legacy mode */ - kvm_x86_ops->get_msr (ctxt->vcpu,MSR_STAR , &msr_data); + ops->get_msr (ctxt->vcpu,MSR_STAR , &msr_data); c->eip = (u32 )msr_data; ctxt->eflags &= ~(EFLG_VM |EFLG_IF |EFLG_RF );
@@ -1927,7 +1927,7 @@ emulate_syscall(struct x86_emulate_ctxt *ctxt) }static int -emulate_sysenter (struct x86_emulate_ctxt *ctxt) +emulate_sysenter (struct x86_emulate_ctxt *ctxt,struct x86_emulate_ops *ops) {struct decode_cache *c = &ctxt->decode;struct kvm_segment cs, ss;
@@ -1949,7 +1949,7 @@ emulate_sysenter(struct x86_emulate_ctxt *ctxt)setup_syscalls_segments (ctxt, &cs, &ss); - kvm_x86_ops->get_msr (ctxt->vcpu,MSR_IA32_SYSENTER_CS , &msr_data); + ops->get_msr (ctxt->vcpu,MSR_IA32_SYSENTER_CS , &msr_data);switch (ctxt->mode) {case X86EMUL_MODE_PROT32 :if ((msr_data &0xfffc ) ==0x0 ) {
@@ -1979,17 +1979,17 @@ emulate_sysenter(struct x86_emulate_ctxt *ctxt) kvm_x86_ops->set_segment (ctxt->vcpu, &cs,VCPU_SREG_CS ); kvm_x86_ops->set_segment (ctxt->vcpu, &ss,VCPU_SREG_SS ); - kvm_x86_ops->get_msr (ctxt->vcpu,MSR_IA32_SYSENTER_EIP , &msr_data); + ops->get_msr (ctxt->vcpu,MSR_IA32_SYSENTER_EIP , &msr_data); c->eip = msr_data; - kvm_x86_ops->get_msr (ctxt->vcpu,MSR_IA32_SYSENTER_ESP , &msr_data); + ops->get_msr (ctxt->vcpu,MSR_IA32_SYSENTER_ESP , &msr_data); c->regs[VCPU_REGS_RSP ] = msr_data;return X86EMUL_CONTINUE ; }static int -emulate_sysexit (struct x86_emulate_ctxt *ctxt) +emulate_sysexit (struct x86_emulate_ctxt *ctxt,struct x86_emulate_ops *ops) {struct decode_cache *c = &ctxt->decode;struct kvm_segment cs, ss;
@@ -2012,7 +2012,7 @@ emulate_sysexit(struct x86_emulate_ctxt *ctxt) cs.dpl =3 ; ss.dpl =3 ; - kvm_x86_ops->get_msr (ctxt->vcpu,MSR_IA32_SYSENTER_CS , &msr_data); + ops->get_msr (ctxt->vcpu,MSR_IA32_SYSENTER_CS , &msr_data);switch (usermode) {case X86EMUL_MODE_PROT32 : cs.selector = (u16)(msr_data +16 );
@@ -3099,7 +3099,7 @@ twobyte_insn: }break ;case 0x05 :/* syscall */ - rc =emulate_syscall (ctxt); + rc =emulate_syscall (ctxt, ops);if (rc !=X86EMUL_CONTINUE ) goto done;else
@@ -3155,7 +3155,7 @@ twobyte_insn:/* wrmsr */ msr_data = (u32 )c->regs[VCPU_REGS_RAX ] | ((u64 )c->regs[VCPU_REGS_RDX ] <<32 ); -if (kvm_set_msr (ctxt->vcpu, c->regs[VCPU_REGS_RCX ], msr_data)) { +if (ops->set_msr (ctxt->vcpu, c->regs[VCPU_REGS_RCX ], msr_data)) {kvm_inject_gp (ctxt->vcpu,0 ); goto done; }
@@ -3164,7 +3164,7 @@ twobyte_insn:break ;case 0x32 :/* rdmsr */ -if (kvm_get_msr (ctxt->vcpu, c->regs[VCPU_REGS_RCX ], &msr_data)) { +if (ops->get_msr (ctxt->vcpu, c->regs[VCPU_REGS_RCX ], &msr_data)) {kvm_inject_gp (ctxt->vcpu,0 ); goto done; }else {
@@ -3175,14 +3175,14 @@ twobyte_insn: c->dst.type =OP_NONE ;break ;case 0x34 :/* sysenter */ - rc =emulate_sysenter (ctxt); + rc =emulate_sysenter (ctxt, ops);if (rc !=X86EMUL_CONTINUE ) goto done;else goto writeback;break ;case 0x35 :/* sysexit */ - rc =emulate_sysexit (ctxt); + rc =emulate_sysexit (ctxt, ops);if (rc !=X86EMUL_CONTINUE ) goto done;else
arch/x86/kvm/x86.c +2 -0
@@ -3749,6 +3749,8 @@ static struct x86_emulate_ops emulate_ops = { .set_rflags = emulator_set_rflags, .get_dr = emulator_get_dr, .set_dr = emulator_set_dr, + .set_msr = kvm_set_msr, + .get_msr = kvm_get_msr, };static void cache_all_regs (struct kvm_vcpu *vcpu)
Signature · 1 line
1.6.5
PATCH v2 8/23 KVM: x86 emulator: add get_cached_segment_base() callback to x86_emulate_ops.
In reply to Gleb NatapovOn VMX it is expensive to call get_cached_descriptor() just to get segment base since multiple vmcs_reads are done instead of only one. Introduce new call back get_cached_segment_base() for efficiency.
--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/kvm/emulate.c | 13 +------------ arch/x86/kvm/x86.c | 7 +++++++ 3 files changed, 9 insertions(+), 12 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
@@ -132,6 +132,7 @@ struct x86_emulate_ops { int seg, struct kvm_vcpu *vcpu);u16 (*get_segment_selector)(int seg,struct kvm_vcpu *vcpu);void (*set_segment_selector)(u16 sel,int seg,struct kvm_vcpu *vcpu); +unsigned long (*get_cached_segment_base)(int seg,struct kvm_vcpu *vcpu);void (*get_gdt)(struct desc_ptr *dt,struct kvm_vcpu *vcpu);ulong (*get_cr)(int cr,struct kvm_vcpu *vcpu);void (*set_cr)(int cr,ulong val,struct kvm_vcpu *vcpu);
arch/x86/kvm/emulate.c +1 -12
@@ -2097,17 +2097,6 @@ static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,return true; } -static u32 get_cached_descriptor_base (struct x86_emulate_ctxt *ctxt, -struct x86_emulate_ops *ops, -int seg) -{ -struct desc_struct desc; -if (ops->get_cached_descriptor (&desc, seg, ctxt->vcpu)) -return get_desc_base (&desc); -else -return ~0 ; -} -static void save_state_to_tss16 (struct x86_emulate_ctxt *ctxt,struct x86_emulate_ops *ops,struct tss_segment_16 *tss)
@@ -2383,7 +2372,7 @@ static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,int ret;u16 old_tss_sel = ops->get_segment_selector (VCPU_SREG_TR , ctxt->vcpu);ulong old_tss_base = -get_cached_descriptor_base (ctxt, ops,VCPU_SREG_TR ); + ops->get_cached_segment_base (VCPU_SREG_TR , ctxt->vcpu);u32 desc_limit;/* FIXME: old_tss_base == ~0 ? */
arch/x86/kvm/x86.c +7 -0
@@ -3652,6 +3652,12 @@ static void emulator_get_gdt(struct desc_ptr *dt, struct kvm_vcpu *vcpu) kvm_x86_ops->get_gdt (vcpu, dt); } +static unsigned long emulator_get_cached_segment_base (int seg, +struct kvm_vcpu *vcpu) +{ +return get_segment_base (vcpu, seg); +} +static bool emulator_get_cached_descriptor (struct desc_struct *desc,int seg,struct kvm_vcpu *vcpu) {
@@ -3742,6 +3748,7 @@ static struct x86_emulate_ops emulate_ops = { .set_cached_descriptor = emulator_set_cached_descriptor, .get_segment_selector = emulator_get_segment_selector, .set_segment_selector = emulator_set_segment_selector, + .get_cached_segment_base = emulator_get_cached_segment_base, .get_gdt = emulator_get_gdt, .get_cr = emulator_get_cr, .set_cr = emulator_set_cr,
Signature · 1 line
1.6.5
PATCH v2 9/23 KVM: x86 emulator: cleanup some direct calls into kvm to use existing callbacks
In reply to Gleb NatapovUse callbacks from x86_emulate_ops to access segments instead of calling into kvm directly.
--- arch/x86/kvm/emulate.c | 200 +++++++++++++++++++++++++----------------------- 1 files changed, 105 insertions(+), 95 deletions(-)
arch/x86/kvm/emulate.c +105 -95
@@ -622,31 +622,35 @@ static void set_seg_override(struct decode_cache *c, int seg) c->seg_override = seg; } -static unsigned long seg_base (struct x86_emulate_ctxt *ctxt,int seg) +static unsigned long seg_base (struct x86_emulate_ctxt *ctxt, +struct x86_emulate_ops *ops,int seg) {if (ctxt->mode ==X86EMUL_MODE_PROT64 && seg <VCPU_SREG_FS )return 0 ; -return kvm_x86_ops->get_segment_base (ctxt->vcpu, seg); +return ops->get_cached_segment_base (seg, ctxt->vcpu); }static unsigned long seg_override_base (struct x86_emulate_ctxt *ctxt, +struct x86_emulate_ops *ops,struct decode_cache *c) {if (!c->has_seg_override)return 0 ; -return seg_base (ctxt, c->seg_override); +return seg_base (ctxt, ops, c->seg_override); } -static unsigned long es_base (struct x86_emulate_ctxt *ctxt) +static unsigned long es_base (struct x86_emulate_ctxt *ctxt, +struct x86_emulate_ops *ops) { -return seg_base (ctxt,VCPU_SREG_ES ); +return seg_base (ctxt, ops,VCPU_SREG_ES ); } -static unsigned long ss_base (struct x86_emulate_ctxt *ctxt) +static unsigned long ss_base (struct x86_emulate_ctxt *ctxt, +struct x86_emulate_ops *ops) { -return seg_base (ctxt,VCPU_SREG_SS ); +return seg_base (ctxt, ops,VCPU_SREG_SS ); } static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
@@ -941,7 +945,7 @@ x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)memset (c,0 ,sizeof (struct decode_cache )); c->eip = ctxt->eip; c->fetch.start = c->fetch.end = c->eip; - ctxt->cs_base =seg_base (ctxt,VCPU_SREG_CS ); + ctxt->cs_base =seg_base (ctxt, ops,VCPU_SREG_CS );memcpy (c->regs, ctxt->vcpu->arch.regs,sizeof c->regs);switch (mode) {
@@ -1065,7 +1069,7 @@ done_prefixes:set_seg_override (c,VCPU_SREG_DS );if (!(!c->twobyte && c->b ==0x8d )) - c->modrm_ea +=seg_override_base (ctxt, c); + c->modrm_ea +=seg_override_base (ctxt, ops, c);if (c->ad_bytes !=8 ) c->modrm_ea = (u32 )c->modrm_ea;
@@ -1161,7 +1165,7 @@ done_prefixes: c->src.type =OP_MEM ; c->src.bytes = (c->d & ByteOp) ?1 : c->op_bytes; c->src.ptr = (unsigned long *) -register_address (c,seg_override_base (ctxt, c), +register_address (c,seg_override_base (ctxt, ops, c), c->regs[VCPU_REGS_RSI ]); c->src.val =0 ;break ;
@@ -1257,7 +1261,7 @@ done_prefixes: c->dst.type =OP_MEM ; c->dst.bytes = (c->d & ByteOp) ?1 : c->op_bytes; c->dst.ptr = (unsigned long *) -register_address (c,es_base (ctxt), +register_address (c,es_base (ctxt, ops), c->regs[VCPU_REGS_RDI ]); c->dst.val =0 ;break ;
@@ -1516,7 +1520,8 @@ exception:return X86EMUL_PROPAGATE_FAULT ; } -static inline void emulate_push (struct x86_emulate_ctxt *ctxt) +static inline void emulate_push (struct x86_emulate_ctxt *ctxt, +struct x86_emulate_ops *ops) {struct decode_cache *c = &ctxt->decode;
@@ -1524,7 +1529,7 @@ static inline void emulate_push(struct x86_emulate_ctxt *ctxt) c->dst.bytes = c->op_bytes; c->dst.val = c->src.val;register_address_increment (c, &c->regs[VCPU_REGS_RSP ], -c->op_bytes); - c->dst.ptr = (void *)register_address (c,ss_base (ctxt), + c->dst.ptr = (void *)register_address (c,ss_base (ctxt, ops), c->regs[VCPU_REGS_RSP ]); }
@@ -1535,7 +1540,7 @@ static int emulate_pop(struct x86_emulate_ctxt *ctxt,struct decode_cache *c = &ctxt->decode;int rc; - rc =read_emulated (ctxt, ops,register_address (c,ss_base (ctxt), + rc =read_emulated (ctxt, ops,register_address (c,ss_base (ctxt, ops), c->regs[VCPU_REGS_RSP ]), dest, len);if (rc !=X86EMUL_CONTINUE )
@@ -1588,15 +1593,14 @@ static int emulate_popf(struct x86_emulate_ctxt *ctxt,return rc; } -static void emulate_push_sreg (struct x86_emulate_ctxt *ctxt,int seg) +static void emulate_push_sreg (struct x86_emulate_ctxt *ctxt, +struct x86_emulate_ops *ops,int seg) {struct decode_cache *c = &ctxt->decode; -struct kvm_segment segment; - kvm_x86_ops->get_segment (ctxt->vcpu, &segment, seg); + c->src.val = ops->get_segment_selector (seg, ctxt->vcpu); - c->src.val = segment.selector; -emulate_push (ctxt); +emulate_push (ctxt, ops); } static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
@@ -1614,7 +1618,8 @@ static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,return rc; } -static void emulate_pusha (struct x86_emulate_ctxt *ctxt) +static void emulate_pusha (struct x86_emulate_ctxt *ctxt, +struct x86_emulate_ops *ops) {struct decode_cache *c = &ctxt->decode;unsigned long old_esp = c->regs[VCPU_REGS_RSP ];
@@ -1624,7 +1629,7 @@ static void emulate_pusha(struct x86_emulate_ctxt *ctxt) (reg ==VCPU_REGS_RSP ) ? (c->src.val = old_esp) : (c->src.val = c->regs[reg]); -emulate_push (ctxt); +emulate_push (ctxt, ops); ++reg; } }
@@ -1726,14 +1731,14 @@ static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt, old_eip = c->eip; c->eip = c->src.val; c->src.val = old_eip; -emulate_push (ctxt); +emulate_push (ctxt, ops);break ; }case 4 :/* jmp abs */ c->eip = c->src.val;break ;case 6 :/* push */ -emulate_push (ctxt); +emulate_push (ctxt, ops);break ; }return X86EMUL_CONTINUE ;
@@ -1847,39 +1852,40 @@ static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask)static inline void setup_syscalls_segments (struct x86_emulate_ctxt *ctxt, -struct kvm_segment *cs,struct kvm_segment *ss) +struct x86_emulate_ops *ops,struct desc_struct *cs, +struct desc_struct *ss) { -memset (cs,0 ,sizeof (struct kvm_segment )); - kvm_x86_ops->get_segment (ctxt->vcpu, cs,VCPU_SREG_CS ); -memset (ss,0 ,sizeof (struct kvm_segment )); +memset (cs,0 ,sizeof (struct desc_struct )); + ops->get_cached_descriptor (cs,VCPU_SREG_CS , ctxt->vcpu); +memset (ss,0 ,sizeof (struct desc_struct )); cs->l =0 ;/* will be adjusted later */ - cs->base =0 ;/* flat segment */ +set_desc_base (cs,0 );/* flat segment */ cs->g =1 ;/* 4kb granularity */ - cs->limit =0xffffffff ;/* 4GB limit */ +set_desc_limit (cs,0xfffff );/* 4GB limit */ cs->type =0x0b ;/* Read, Execute, Accessed */ cs->s =1 ; cs->dpl =0 ;/* will be adjusted later */ - cs->present =1 ; - cs->db =1 ; + cs->p =1 ; + cs->d =1 ; - ss->unusable =0 ; - ss->base =0 ;/* flat segment */ - ss->limit =0xffffffff ;/* 4GB limit */ +set_desc_base (ss,0 );/* flat segment */ +set_desc_limit (ss,0xfffff );/* 4GB limit */ ss->g =1 ;/* 4kb granularity */ ss->s =1 ; ss->type =0x03 ;/* Read/Write, Accessed */ - ss->db =1 ;/* 32bit stack segment */ + ss->d =1 ;/* 32bit stack segment */ ss->dpl =0 ; - ss->present =1 ; + ss->p =1 ; }static int emulate_syscall (struct x86_emulate_ctxt *ctxt,struct x86_emulate_ops *ops) {struct decode_cache *c = &ctxt->decode; -struct kvm_segment cs, ss; +struct desc_struct cs, ss;u64 msr_data; +u16 cs_sel, ss_sel;/* syscall is not available in real mode */ if (ctxt->mode == X86EMUL_MODE_REAL ||
@@ -1888,19 +1894,21 @@ emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)return X86EMUL_PROPAGATE_FAULT ; } -setup_syscalls_segments (ctxt, &cs, &ss); +setup_syscalls_segments (ctxt, ops, &cs, &ss); ops->get_msr (ctxt->vcpu,MSR_STAR , &msr_data); msr_data >>=32 ; - cs.selector = (u16)(msr_data &0xfffc ); - ss.selector = (u16)(msr_data +8 ); + cs_sel = (u16)(msr_data &0xfffc ); + ss_sel = (u16)(msr_data +8 );if (is_long_mode (ctxt->vcpu)) { - cs.db =0 ; + cs.d =0 ; cs.l =1 ; } - kvm_x86_ops->set_segment (ctxt->vcpu, &cs,VCPU_SREG_CS ); - kvm_x86_ops->set_segment (ctxt->vcpu, &ss,VCPU_SREG_SS ); + ops->set_cached_descriptor (&cs,VCPU_SREG_CS , ctxt->vcpu); + ops->set_segment_selector (cs_sel,VCPU_SREG_CS , ctxt->vcpu); + ops->set_cached_descriptor (&ss,VCPU_SREG_SS , ctxt->vcpu); + ops->set_segment_selector (ss_sel,VCPU_SREG_SS , ctxt->vcpu); c->regs[VCPU_REGS_RCX ] = c->eip;if (is_long_mode (ctxt->vcpu)) {
@@ -1930,8 +1938,9 @@ static int emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) { struct decode_cache *c = &ctxt->decode; -struct kvm_segment cs, ss; +struct desc_struct cs, ss;u64 msr_data; +u16 cs_sel, ss_sel;/* inject #GP if in real mode */ if (ctxt->mode ==X86EMUL_MODE_REAL ) {
@@ -1947,7 +1956,7 @@ emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)return X86EMUL_PROPAGATE_FAULT ; } -setup_syscalls_segments (ctxt, &cs, &ss); +setup_syscalls_segments (ctxt, ops, &cs, &ss); ops->get_msr (ctxt->vcpu,MSR_IA32_SYSENTER_CS , &msr_data);switch (ctxt->mode) {
@@ -1966,18 +1975,20 @@ emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) } ctxt->eflags &= ~(EFLG_VM |EFLG_IF |EFLG_RF ); - cs.selector = (u16 )msr_data; - cs.selector &= ~SELECTOR_RPL_MASK ; - ss.selector = cs.selector +8 ; - ss.selector &= ~SELECTOR_RPL_MASK ; + cs_sel = (u16 )msr_data; + cs_sel &= ~SELECTOR_RPL_MASK ; + ss_sel = cs_sel +8 ; + ss_sel &= ~SELECTOR_RPL_MASK ;if (ctxt->mode ==X86EMUL_MODE_PROT64 ||is_long_mode (ctxt->vcpu)) { - cs.db =0 ; + cs.d =0 ; cs.l =1 ; } - kvm_x86_ops->set_segment (ctxt->vcpu, &cs,VCPU_SREG_CS ); - kvm_x86_ops->set_segment (ctxt->vcpu, &ss,VCPU_SREG_SS ); + ops->set_cached_descriptor (&cs,VCPU_SREG_CS , ctxt->vcpu); + ops->set_segment_selector (cs_sel,VCPU_SREG_CS , ctxt->vcpu); + ops->set_cached_descriptor (&ss,VCPU_SREG_SS , ctxt->vcpu); + ops->set_segment_selector (ss_sel,VCPU_SREG_SS , ctxt->vcpu); ops->get_msr (ctxt->vcpu,MSR_IA32_SYSENTER_EIP , &msr_data); c->eip = msr_data;
@@ -1992,9 +2003,10 @@ static int emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) { struct decode_cache *c = &ctxt->decode; -struct kvm_segment cs, ss; +struct desc_struct cs, ss;u64 msr_data;int usermode; +u16 cs_sel, ss_sel;/* inject #GP if in real mode or Virtual 8086 mode */ if (ctxt->mode == X86EMUL_MODE_REAL ||
@@ -2003,7 +2015,7 @@ emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)return X86EMUL_PROPAGATE_FAULT ; } -setup_syscalls_segments (ctxt, &cs, &ss); +setup_syscalls_segments (ctxt, ops, &cs, &ss);if ((c->rex_prefix &0x8 ) !=0x0 ) usermode =X86EMUL_MODE_PROT64 ;
@@ -2015,29 +2027,31 @@ emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) ops->get_msr (ctxt->vcpu,MSR_IA32_SYSENTER_CS , &msr_data);switch (usermode) {case X86EMUL_MODE_PROT32 : - cs.selector = (u16)(msr_data +16 ); + cs_sel = (u16)(msr_data +16 );if ((msr_data &0xfffc ) ==0x0 ) {kvm_inject_gp (ctxt->vcpu,0 );return X86EMUL_PROPAGATE_FAULT ; } - ss.selector = (u16)(msr_data +24 ); + ss_sel = (u16)(msr_data +24 );break ;case X86EMUL_MODE_PROT64 : - cs.selector = (u16)(msr_data +32 ); + cs_sel = (u16)(msr_data +32 );if (msr_data ==0x0 ) {kvm_inject_gp (ctxt->vcpu,0 );return X86EMUL_PROPAGATE_FAULT ; } - ss.selector = cs.selector +8 ; - cs.db =0 ; + ss_sel = cs_sel +8 ; + cs.d =0 ; cs.l =1 ;break ; } - cs.selector |=SELECTOR_RPL_MASK ; - ss.selector |=SELECTOR_RPL_MASK ; + cs_sel |=SELECTOR_RPL_MASK ; + ss_sel |=SELECTOR_RPL_MASK ; - kvm_x86_ops->set_segment (ctxt->vcpu, &cs,VCPU_SREG_CS ); - kvm_x86_ops->set_segment (ctxt->vcpu, &ss,VCPU_SREG_SS ); + ops->set_cached_descriptor (&cs,VCPU_SREG_CS , ctxt->vcpu); + ops->set_segment_selector (cs_sel,VCPU_SREG_CS , ctxt->vcpu); + ops->set_cached_descriptor (&ss,VCPU_SREG_SS , ctxt->vcpu); + ops->set_segment_selector (ss_sel,VCPU_SREG_SS , ctxt->vcpu); c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX ]; c->regs[VCPU_REGS_RSP ] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX ];
@@ -2061,25 +2075,25 @@ static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,struct x86_emulate_ops *ops, u16 port, u16 len) { - struct kvm_segment tr_seg; + struct desc_struct tr_seg;int r;u16 io_bitmap_ptr;u8 perm, bit_idx = port &0x7 ;unsigned mask = (1 << len) -1 ; -kvm_get_segment (ctxt->vcpu, &tr_seg,VCPU_SREG_TR ); -if (tr_seg.unusable) + ops->get_cached_descriptor (&tr_seg,VCPU_SREG_TR , ctxt->vcpu); +if (tr_seg.p)return false; -if (tr_seg.limit <103 ) +if (desc_limit_scaled (&tr_seg) <103 )return false; - r = ops->read_std (tr_seg.base +102 , &io_bitmap_ptr,2 , ctxt->vcpu, -NULL ); + r = ops->read_std (get_desc_base (&tr_seg) +102 , &io_bitmap_ptr,2 , + ctxt->vcpu,NULL );if (r !=X86EMUL_CONTINUE )return false; -if (io_bitmap_ptr + port/8 > tr_seg.limit) +if (io_bitmap_ptr + port/8 >desc_limit_scaled (&tr_seg))return false; - r = ops->read_std (tr_seg.base + io_bitmap_ptr + port/8 , &perm,1 , - ctxt->vcpu,NULL ); + r = ops->read_std (get_desc_base (&tr_seg) + io_bitmap_ptr + port/8 , + &perm,1 , ctxt->vcpu,NULL );if (r !=X86EMUL_CONTINUE )return false;if ((perm >> bit_idx) & mask)
@@ -2445,7 +2459,7 @@ static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, c->op_bytes = c->ad_bytes = (next_tss_desc.type &8 ) ?4 :2 ; c->lock_prefix =0 ; c->src.val = (unsigned long ) error_code; -emulate_push (ctxt); +emulate_push (ctxt, ops); }return ret;
@@ -2588,7 +2602,7 @@ special_insn:emulate_2op_SrcV ("add" , c->src, c->dst, ctxt->eflags);break ;case 0x06 :/* push es */ -emulate_push_sreg (ctxt,VCPU_SREG_ES ); +emulate_push_sreg (ctxt, ops,VCPU_SREG_ES );break ;case 0x07 :/* pop es */ rc =emulate_pop_sreg (ctxt, ops,VCPU_SREG_ES );
@@ -2600,14 +2614,14 @@ special_insn:emulate_2op_SrcV ("or" , c->src, c->dst, ctxt->eflags);break ;case 0x0e :/* push cs */ -emulate_push_sreg (ctxt,VCPU_SREG_CS ); +emulate_push_sreg (ctxt, ops,VCPU_SREG_CS );break ; case 0x10 ... 0x15: adc:/* adc */ emulate_2op_SrcV ("adc" , c->src, c->dst, ctxt->eflags);break ;case 0x16 :/* push ss */ -emulate_push_sreg (ctxt,VCPU_SREG_SS ); +emulate_push_sreg (ctxt, ops,VCPU_SREG_SS );break ;case 0x17 :/* pop ss */ rc =emulate_pop_sreg (ctxt, ops,VCPU_SREG_SS );
@@ -2619,7 +2633,7 @@ special_insn:emulate_2op_SrcV ("sbb" , c->src, c->dst, ctxt->eflags);break ;case 0x1e :/* push ds */ -emulate_push_sreg (ctxt,VCPU_SREG_DS ); +emulate_push_sreg (ctxt, ops,VCPU_SREG_DS );break ;case 0x1f :/* pop ds */ rc =emulate_pop_sreg (ctxt, ops,VCPU_SREG_DS );
@@ -2649,7 +2663,7 @@ special_insn:emulate_1op ("dec" , c->dst, ctxt->eflags);break ; case 0x50 ... 0x57: /* push reg */ -emulate_push (ctxt); +emulate_push (ctxt, ops);break ; case 0x58 ... 0x5f: /* pop reg */ pop_instruction:
@@ -2658,7 +2672,7 @@ special_insn: goto done;break ;case 0x60 :/* pusha */ -emulate_pusha (ctxt); +emulate_pusha (ctxt, ops);break ;case 0x61 :/* popa */ rc =emulate_popa (ctxt, ops);
@@ -2672,7 +2686,7 @@ special_insn:break ;case 0x68 :/* push imm */ case 0x6a :/* push imm8 */ -emulate_push (ctxt); +emulate_push (ctxt, ops);break ;case 0x6c :/* insb */ case 0x6d :/* insw/insd */
@@ -2752,18 +2766,13 @@ special_insn:break ; case 0x88 ... 0x8b: /* mov */ goto mov; -case 0x8c : {/* mov r/m, sreg */ -struct kvm_segment segreg; - -if (c->modrm_reg <=VCPU_SREG_GS ) -kvm_get_segment (ctxt->vcpu, &segreg, c->modrm_reg); -else { +case 0x8c :/* mov r/m, sreg */ +if (c->modrm_reg >VCPU_SREG_GS ) {kvm_queue_exception (ctxt->vcpu,UD_VECTOR ); goto done; } - c->dst.val = segreg.selector; + c->dst.val = ops->get_segment_selector (c->modrm_reg, ctxt->vcpu);break ; - }case 0x8d :/* lea r16/r32, m */ c->dst.val = c->modrm_ea;break ;
@@ -2804,7 +2813,7 @@ special_insn: goto xchg;case 0x9c :/* pushf */ c->src.val = (unsigned long ) ctxt->eflags; -emulate_push (ctxt); +emulate_push (ctxt, ops);break ;case 0x9d :/* popf */ c->dst.type =OP_REG ;
@@ -2872,7 +2881,7 @@ special_insn:long int rel = c->src.val; c->src.val = (unsigned long ) c->eip;jmp_rel (c, rel); -emulate_push (ctxt); +emulate_push (ctxt, ops);break ; }case 0xe9 :/* jmp rel */
@@ -2985,11 +2994,12 @@ writeback: c->dst.type = saved_dst_type;if ((c->d & SrcMask) == SrcSI) -string_addr_inc (ctxt,seg_override_base (ctxt, c),VCPU_REGS_RSI , - &c->src); +string_addr_inc (ctxt,seg_override_base (ctxt, ops, c), +VCPU_REGS_RSI , &c->src);if ((c->d & DstMask) == DstDI) -string_addr_inc (ctxt,es_base (ctxt),VCPU_REGS_RDI , &c->dst); +string_addr_inc (ctxt,es_base (ctxt, ops),VCPU_REGS_RDI , + &c->dst);if (c->rep_prefix && (c->d & String)) {struct read_cache *rc = &ctxt->decode.io_read;
@@ -3188,7 +3198,7 @@ twobyte_insn: c->dst.type =OP_NONE ;break ;case 0xa0 :/* push fs */ -emulate_push_sreg (ctxt,VCPU_SREG_FS ); +emulate_push_sreg (ctxt, ops,VCPU_SREG_FS );break ;case 0xa1 :/* pop fs */ rc =emulate_pop_sreg (ctxt, ops,VCPU_SREG_FS );
@@ -3207,7 +3217,7 @@ twobyte_insn:emulate_2op_cl ("shld" , c->src2, c->src, c->dst, ctxt->eflags);break ;case 0xa8 :/* push gs */ -emulate_push_sreg (ctxt,VCPU_SREG_GS ); +emulate_push_sreg (ctxt, ops,VCPU_SREG_GS );break ;case 0xa9 :/* pop gs */ rc =emulate_pop_sreg (ctxt, ops,VCPU_SREG_GS );
Signature · 1 line
1.6.5
PATCH v2 10/23 KVM: x86 emulator: make set_cr() callback return error if it fails
In reply to Gleb NatapovMake set_cr() callback return error if it fails instead of injecting #GP behind emulator's back.
--- arch/x86/include/asm/kvm_emulate.h | 2 +- arch/x86/kvm/emulate.c | 10 ++- arch/x86/kvm/x86.c | 148 ++++++++++++++++++------------------ 3 files changed, 84 insertions(+), 76 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -1
@@ -135,7 +135,7 @@ struct x86_emulate_ops {unsigned long (*get_cached_segment_base)(int seg,struct kvm_vcpu *vcpu);void (*get_gdt)(struct desc_ptr *dt,struct kvm_vcpu *vcpu);ulong (*get_cr)(int cr,struct kvm_vcpu *vcpu); -void (*set_cr)(int cr,ulong val,struct kvm_vcpu *vcpu); +int (*set_cr)(int cr,ulong val,struct kvm_vcpu *vcpu);int (*cpl)(struct kvm_vcpu *vcpu);void (*set_rflags)(struct kvm_vcpu *vcpu,unsigned long rflags);int (*get_dr)(int dr,unsigned long *dest,struct kvm_vcpu *vcpu);
arch/x86/kvm/emulate.c +8 -2
@@ -2272,7 +2272,10 @@ static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,struct decode_cache *c = &ctxt->decode;int ret; - ops->set_cr (3 , tss->cr3, ctxt->vcpu); +if (ops->set_cr (3 , tss->cr3, ctxt->vcpu)) { +kvm_inject_gp (ctxt->vcpu,0 ); +return X86EMUL_PROPAGATE_FAULT ; + } c->eip = tss->eip; ctxt->eflags = tss->eflags |2 ; c->regs[VCPU_REGS_RAX ] = tss->eax;
@@ -3135,7 +3138,10 @@ twobyte_insn: c->dst.type =OP_NONE ;/* no writeback */ break ;case 0x22 :/* mov reg, cr */ - ops->set_cr (c->modrm_reg, c->modrm_val, ctxt->vcpu); +if (ops->set_cr (c->modrm_reg, c->modrm_val, ctxt->vcpu)) { +kvm_inject_gp (ctxt->vcpu,0 ); + goto done; + } c->dst.type =OP_NONE ;break ;case 0x23 :/* mov from reg to dr */
arch/x86/kvm/x86.c +75 -73
244 lines not on this page · raw message
Signature · 1 line
1.6.5
PATCH v2 11/23 KVM: x86 emulator: make (get|set)_dr() callback return error if it fails
In reply to Gleb NatapovMake (get|set)_dr() callback return error if it fails instead of injecting exception behind emulator's back.
--- arch/x86/kvm/emulate.c | 11 ++++++-- arch/x86/kvm/x86.c | 63 ++++++++++++++++++++++++++++------------------- 2 files changed, 45 insertions(+), 29 deletions(-)
arch/x86/kvm/emulate.c +8 -3
@@ -3151,9 +3151,14 @@ twobyte_insn: goto done; } - ops->set_dr (c->modrm_reg,c->regs[c->modrm_rm] & - ((ctxt->mode ==X86EMUL_MODE_PROT64 ) ? ~0ULL : ~0U ), - ctxt->vcpu); +if (ops->set_dr (c->modrm_reg, c->regs[c->modrm_rm] & + ((ctxt->mode ==X86EMUL_MODE_PROT64 ) ? + ~0ULL : ~0U ), ctxt->vcpu) <0 ) { +/* #UD condition is already handled by the code above */ +kvm_inject_gp (ctxt->vcpu,0 ); + goto done; + } + c->dst.type =OP_NONE ;/* no writeback */ break ;case 0x30 :
arch/x86/kvm/x86.c +37 -26
119 lines not on this page · raw message
Signature · 1 line
1.6.5
PATCH v2 12/23 KVM: x86 emulator: fix X86EMUL_RETRY_INSTR and X86EMUL_CMPXCHG_FAILED values
In reply to Gleb NatapovCurrently X86EMUL_PROPAGATE_FAULT, X86EMUL_RETRY_INSTR and X86EMUL_CMPXCHG_FAILED have the same value so caller cannot distinguish why function such as emulator_cmpxchg_emulated() (which can return both X86EMUL_PROPAGATE_FAULT and X86EMUL_CMPXCHG_FAILED) failed.
--- arch/x86/include/asm/kvm_emulate.h | 5 +++-- 1 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_emulate.h +3 -2
@@ -51,8 +51,9 @@ struct x86_emulate_ctxt;#define X86EMUL_UNHANDLEABLE 1/* Terminate emulation but return success to the caller. */ #define X86EMUL_PROPAGATE_FAULT 2/* propagate a generated fault to guest */ -#define X86EMUL_RETRY_INSTR 2/* retry the instruction for some reason */ -#define X86EMUL_CMPXCHG_FAILED 2/* cmpxchg did not see expected value */ +#define X86EMUL_RETRY_INSTR 3/* retry the instruction for some reason */ +#define X86EMUL_CMPXCHG_FAILED 4/* cmpxchg did not see expected value */ +struct x86_emulate_ops {/* * read_std: Read bytes of standard (non-emulated/special) memory.
Signature · 1 line
1.6.5
PATCH v2 13/23 KVM: fill in run->mmio details in (read|write)_emulated function.
In reply to Gleb NatapovFill in run->mmio details in (read|write)_emulated function just like pio does. There is no point in filling only vcpu fields there just to copy them into vcpu->run a little bit later.
--- arch/x86/kvm/x86.c | 25 +++++++++---------------- 1 files changed, 9 insertions(+), 16 deletions(-)
arch/x86/kvm/x86.c +9 -16
@@ -3324,9 +3324,10 @@ mmio:trace_kvm_mmio (KVM_TRACE_MMIO_READ_UNSATISFIED , bytes, gpa,0 ); vcpu->mmio_needed =1 ; - vcpu->mmio_phys_addr = gpa; - vcpu->mmio_size = bytes; - vcpu->mmio_is_write =0 ; + vcpu->run->exit_reason =KVM_EXIT_MMIO ; + vcpu->run->mmio.phys_addr = vcpu->mmio_phys_addr = gpa; + vcpu->run->mmio.len = vcpu->mmio_size = bytes; + vcpu->run->mmio.is_write = vcpu->mmio_is_write =0 ;return X86EMUL_UNHANDLEABLE ; }
@@ -3374,10 +3375,11 @@ mmio:return X86EMUL_CONTINUE ; vcpu->mmio_needed =1 ; - vcpu->mmio_phys_addr = gpa; - vcpu->mmio_size = bytes; - vcpu->mmio_is_write =1 ; -memcpy (vcpu->mmio_data, val, bytes); + vcpu->run->exit_reason =KVM_EXIT_MMIO ; + vcpu->run->mmio.phys_addr = vcpu->mmio_phys_addr = gpa; + vcpu->run->mmio.len = vcpu->mmio_size = bytes; + vcpu->run->mmio.is_write = vcpu->mmio_is_write =1 ; +memcpy (vcpu->run->mmio.data, val, bytes);return X86EMUL_CONTINUE ; }
@@ -3788,7 +3790,6 @@ int emulate_instruction(struct kvm_vcpu *vcpu, {int r, shadow_mask;struct decode_cache *c; -struct kvm_run *run = vcpu->run;kvm_clear_exception_queue (vcpu); vcpu->arch.mmio_fault_cr2 = cr2;
@@ -3875,14 +3876,6 @@ restart:return EMULATE_DO_MMIO ; } -if (r || vcpu->mmio_is_write) { - run->exit_reason =KVM_EXIT_MMIO ; - run->mmio.phys_addr = vcpu->mmio_phys_addr; -memcpy (run->mmio.data, vcpu->mmio_data,8 ); - run->mmio.len = vcpu->mmio_size; - run->mmio.is_write = vcpu->mmio_is_write; - } -if (r) {if (kvm_mmu_unprotect_page_virt (vcpu, cr2)) goto done;
Signature · 1 line
1.6.5
PATCH v2 14/23 KVM: x86 emulator: x86_emulate_insn() return -1 only in case of emulation failure
In reply to Gleb NatapovCurrently emulator returns -1 when emulation failed or IO is needed. Caller tries to guess whether emulation failed by looking at other variables. Make it easier for caller to recognise error condition by always returning -1 in case of failure. For this new emulator internal return value X86EMUL_IO_NEEDED is introduced. It is used to distinguish between error condition (which returns X86EMUL_UNHANDLEABLE) and condition that requires IO exit to userspace to continue emulation.
--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/kvm/x86.c | 36 ++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 18 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
@@ -53,6 +53,7 @@ struct x86_emulate_ctxt;#define X86EMUL_PROPAGATE_FAULT 2/* propagate a generated fault to guest */ #define X86EMUL_RETRY_INSTR 3/* retry the instruction for some reason */ #define X86EMUL_CMPXCHG_FAILED 4/* cmpxchg did not see expected value */ +#define X86EMUL_IO_NEEDED 5/* IO is needed to complete emulation */ struct x86_emulate_ops {/*
arch/x86/kvm/x86.c +18 -18
@@ -3213,7 +3213,7 @@ static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, } ret =kvm_read_guest (vcpu->kvm, gpa, data, toread);if (ret <0 ) { - r =X86EMUL_UNHANDLEABLE ; + r =X86EMUL_IO_NEEDED ; goto out; }
@@ -3269,7 +3269,7 @@ static int kvm_write_guest_virt_system(gva_t addr, void *val, } ret =kvm_write_guest (vcpu->kvm, gpa, data, towrite);if (ret <0 ) { - r =X86EMUL_UNHANDLEABLE ; + r =X86EMUL_IO_NEEDED ; goto out; }
@@ -3329,7 +3329,7 @@ mmio: vcpu->run->mmio.len = vcpu->mmio_size = bytes; vcpu->run->mmio.is_write = vcpu->mmio_is_write =0 ; -return X86EMUL_UNHANDLEABLE ; +return X86EMUL_IO_NEEDED ; } int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
@@ -3801,8 +3801,6 @@ int emulate_instruction(struct kvm_vcpu *vcpu,*/ cache_all_regs (vcpu); - vcpu->mmio_is_write =0 ; -if (!(emulation_type &EMULTYPE_NO_DECODE )) {int cs_db, cs_l; kvm_x86_ops->get_cs_db_l_bits (vcpu, &cs_db, &cs_l);
@@ -3876,24 +3874,26 @@ restart:return EMULATE_DO_MMIO ; } -if (r) { -if (kvm_mmu_unprotect_page_virt (vcpu, cr2)) - goto done; -if (!vcpu->mmio_needed) { - ++vcpu->stat.insn_emulation_fail; -trace_kvm_emulate_insn_failed (vcpu); -kvm_report_emulation_failure (vcpu,"mmio" ); -return EMULATE_FAIL ; - } +if (vcpu->mmio_needed) { +if (vcpu->mmio_is_write) + vcpu->mmio_needed =0 ;return EMULATE_DO_MMIO ; } -if (vcpu->mmio_is_write) { - vcpu->mmio_needed =0 ; -return EMULATE_DO_MMIO ; +if (r) {/* emulation failed */ +/* +* if emulation was due to access to shadowed page table +* and it failed try to unshadow page and re-entetr the +* guest to let CPU execute the instruction. +*/ +if (kvm_mmu_unprotect_page_virt (vcpu, cr2)) +return EMULATE_DONE ; + +trace_kvm_emulate_insn_failed (vcpu); +kvm_report_emulation_failure (vcpu,"mmio" ); +return EMULATE_FAIL ; } -done:if (vcpu->arch.exception.pending) vcpu->arch.emulate_ctxt.restart = false;
Signature · 1 line
1.6.5
PATCH v2 15/23 KVM: remove export of emulator_write_emulated().
In reply to Gleb NatapovIt is not called directly outside of the file it's defined in anymore.
--- arch/x86/include/asm/kvm_host.h | 5 ----- arch/x86/kvm/x86.c | 1 - 2 files changed, 0 insertions(+), 6 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -5
@@ -627,11 +627,6 @@ void kvm_inject_nmi(struct kvm_vcpu *vcpu);void fx_init (struct kvm_vcpu *vcpu); -int emulator_write_emulated (unsigned long addr, -const void *val, -unsigned int bytes, -struct kvm_vcpu *vcpu); -void kvm_mmu_flush_tlb (struct kvm_vcpu *vcpu); void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new, int bytes,
arch/x86/kvm/x86.c +0 -1
@@ -3403,7 +3403,6 @@ int emulator_write_emulated(unsigned long addr, }return emulator_write_emulated_onepage (addr, val, bytes, vcpu); } -EXPORT_SYMBOL_GPL (emulator_write_emulated);#define CMPXCHG_TYPE (t, ptr, old, new) \ (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
Signature · 1 line
1.6.5
PATCH v2 16/23 KVM: do not inject #PF in (read|write)_emulated() callbacks
In reply to Gleb NatapovReturn error to x86 emulator instead of injection exception behind its back.
--- arch/x86/include/asm/kvm_emulate.h | 3 +++ arch/x86/kvm/emulate.c | 12 +++++++++++- arch/x86/kvm/x86.c | 28 ++++++++++++++-------------- 3 files changed, 28 insertions(+), 15 deletions(-)
arch/x86/include/asm/kvm_emulate.h +3 -0
@@ -94,6 +94,7 @@ struct x86_emulate_ops {int (*read_emulated)(unsigned long addr,void *val,unsigned int bytes, +unsigned int *error,struct kvm_vcpu *vcpu);/*
@@ -106,6 +107,7 @@ struct x86_emulate_ops {int (*write_emulated)(unsigned long addr,const void *val,unsigned int bytes, +unsigned int *error,struct kvm_vcpu *vcpu);/*
@@ -120,6 +122,7 @@ struct x86_emulate_ops {const void *old, const void *new, unsigned int bytes, + unsigned int *error, struct kvm_vcpu *vcpu); int (*pio_in_emulated)(int size, unsigned short port, void *val,
arch/x86/kvm/emulate.c +11 -1
@@ -1277,6 +1277,7 @@ static int read_emulated(struct x86_emulate_ctxt *ctxt, {int rc;struct read_cache *mc = &ctxt->decode.mem_read; +u32 err;while (size) {int n =min (size,8u );
@@ -1284,7 +1285,10 @@ static int read_emulated(struct x86_emulate_ctxt *ctxt,if (mc->pos < mc->end) goto read_cached; - rc = ops->read_emulated (addr, mc->data + mc->end, n, ctxt->vcpu); + rc = ops->read_emulated (addr, mc->data + mc->end, n, &err, + ctxt->vcpu); +if (rc ==X86EMUL_PROPAGATE_FAULT ) +kvm_inject_page_fault (ctxt->vcpu, addr, err);if (rc !=X86EMUL_CONTINUE )return rc; mc->end += n;
@@ -1789,6 +1793,7 @@ static inline int writeback(struct x86_emulate_ctxt *ctxt, {int rc;struct decode_cache *c = &ctxt->decode; +u32 err;switch (c->dst.type) {case OP_REG :
@@ -1817,13 +1822,18 @@ static inline int writeback(struct x86_emulate_ctxt *ctxt, &c->dst.orig_val, &c->dst.val, c->dst.bytes, + &err, ctxt->vcpu); else rc = ops->write_emulated ( (unsigned long )c->dst.ptr, &c->dst.val, c->dst.bytes, + &err, ctxt->vcpu); +if (rc ==X86EMUL_PROPAGATE_FAULT ) +kvm_inject_page_fault (ctxt->vcpu, + (unsigned long )c->dst.ptr, err);if (rc !=X86EMUL_CONTINUE )return rc;break ;
arch/x86/kvm/x86.c +14 -14
@@ -3284,10 +3284,10 @@ out:static int emulator_read_emulated (unsigned long addr,void *val,unsigned int bytes, +unsigned int *error_code,struct kvm_vcpu *vcpu) {gpa_t gpa; -u32 error_code;if (vcpu->mmio_read_completed) {memcpy (val, vcpu->mmio_data, bytes);
@@ -3297,12 +3297,10 @@ static int emulator_read_emulated(unsigned long addr,return X86EMUL_CONTINUE ; } - gpa =kvm_mmu_gva_to_gpa_read (vcpu, addr, &error_code); + gpa =kvm_mmu_gva_to_gpa_read (vcpu, addr, error_code); -if (gpa ==UNMAPPED_GVA ) { -kvm_inject_page_fault (vcpu, addr, error_code); +if (gpa ==UNMAPPED_GVA )return X86EMUL_PROPAGATE_FAULT ; - }/* For APIC access vmexit */ if ((gpa &PAGE_MASK ) ==APIC_DEFAULT_PHYS_BASE )
@@ -3347,17 +3345,15 @@ int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,static int emulator_write_emulated_onepage (unsigned long addr,const void *val,unsigned int bytes, +unsigned int *error_code,struct kvm_vcpu *vcpu) {gpa_t gpa; -u32 error_code; - gpa =kvm_mmu_gva_to_gpa_write (vcpu, addr, &error_code); + gpa =kvm_mmu_gva_to_gpa_write (vcpu, addr, error_code); -if (gpa ==UNMAPPED_GVA ) { -kvm_inject_page_fault (vcpu, addr, error_code); +if (gpa ==UNMAPPED_GVA )return X86EMUL_PROPAGATE_FAULT ; - }/* For APIC access vmexit */ if ((gpa &PAGE_MASK ) ==APIC_DEFAULT_PHYS_BASE )
@@ -3387,6 +3383,7 @@ mmio:int emulator_write_emulated (unsigned long addr,const void *val,unsigned int bytes, +unsigned int *error_code,struct kvm_vcpu *vcpu) {/* Crossing a page boundary? */
@@ -3394,14 +3391,16 @@ int emulator_write_emulated(unsigned long addr,int rc, now; now = -addr & ~PAGE_MASK ; - rc =emulator_write_emulated_onepage (addr, val, now, vcpu); + rc =emulator_write_emulated_onepage (addr, val, now, error_code, + vcpu);if (rc !=X86EMUL_CONTINUE )return rc; addr += now; val += now; bytes -= now; } -return emulator_write_emulated_onepage (addr, val, bytes, vcpu); +return emulator_write_emulated_onepage (addr, val, bytes, error_code, + vcpu); }#define CMPXCHG_TYPE (t, ptr, old, new) \
@@ -3418,6 +3417,7 @@ static int emulator_cmpxchg_emulated(unsigned long addr,const void *old, const void *new, unsigned int bytes, + unsigned int *error_code, struct kvm_vcpu *vcpu) { gpa_t gpa;
@@ -3471,7 +3471,7 @@ static int emulator_cmpxchg_emulated(unsigned long addr, emul_write:printk_once (KERN_WARNING "kvm: emulating exchange as write\n" ); -return emulator_write_emulated (addr, new, bytes, vcpu); +return emulator_write_emulated (addr, new, bytes, error_code, vcpu); }static int kernel_pio (struct kvm_vcpu *vcpu,void *pd)
@@ -4231,7 +4231,7 @@ int kvm_fix_hypercall(struct kvm_vcpu *vcpu) kvm_x86_ops->patch_hypercall (vcpu, instruction); -return emulator_write_emulated (rip, instruction,3 , vcpu); +return emulator_write_emulated (rip, instruction,3 ,NULL , vcpu); }void realmode_lgdt (struct kvm_vcpu *vcpu,u16 limit,unsigned long base)
Signature · 1 line
1.6.5
PATCH v2 17/23 KVM: handle emulation failure case first.
In reply to Gleb NatapovIf emulation failed return immediately.
--- arch/x86/kvm/x86.c | 31 +++++++++++++++---------------- 1 files changed, 15 insertions(+), 16 deletions(-)
arch/x86/kvm/x86.c +15 -16
@@ -3862,22 +3862,6 @@ int emulate_instruction(struct kvm_vcpu *vcpu, restart: r =x86_emulate_insn (&vcpu->arch.emulate_ctxt, &emulate_ops); - shadow_mask = vcpu->arch.emulate_ctxt.interruptibility; - -if (r ==0 ) - kvm_x86_ops->set_interrupt_shadow (vcpu, shadow_mask); - -if (vcpu->arch.pio.count) { -if (!vcpu->arch.pio.in) - vcpu->arch.pio.count =0 ; -return EMULATE_DO_MMIO ; - } - -if (vcpu->mmio_needed) { -if (vcpu->mmio_is_write) - vcpu->mmio_needed =0 ; -return EMULATE_DO_MMIO ; - }if (r) {/* emulation failed */ /*
@@ -3893,6 +3877,21 @@ restart:return EMULATE_FAIL ; } + shadow_mask = vcpu->arch.emulate_ctxt.interruptibility; + kvm_x86_ops->set_interrupt_shadow (vcpu, shadow_mask); + +if (vcpu->arch.pio.count) { +if (!vcpu->arch.pio.in) + vcpu->arch.pio.count =0 ; +return EMULATE_DO_MMIO ; + } + +if (vcpu->mmio_needed) { +if (vcpu->mmio_is_write) + vcpu->mmio_needed =0 ; +return EMULATE_DO_MMIO ; + } +if (vcpu->arch.exception.pending) vcpu->arch.emulate_ctxt.restart = false;
Signature · 1 line
1.6.5
PATCH v2 18/23 KVM: x86 emulator: advance RIP outside x86 emulator code
In reply to Gleb NatapovReturn new RIP as part of instruction emulation result instead of updating KVM's RIP from x86 emulator code.
--- arch/x86/kvm/emulate.c | 7 ++++--- arch/x86/kvm/x86.c | 4 +++- 2 files changed, 7 insertions(+), 4 deletions(-)
arch/x86/kvm/emulate.c +4 -3
@@ -2496,8 +2496,9 @@ int emulator_task_switch(struct x86_emulate_ctxt *ctxt,if (rc ==X86EMUL_CONTINUE ) {memcpy (ctxt->vcpu->arch.regs, c->regs,sizeof c->regs); -kvm_rip_write (ctxt->vcpu, c->eip); rc =writeback (ctxt, ops); +if (rc ==X86EMUL_CONTINUE ) + ctxt->eip = c->eip; }return (rc ==X86EMUL_UNHANDLEABLE ) ?-1 :0 ;
@@ -2554,7 +2555,7 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)if (address_mask (c, c->regs[VCPU_REGS_RCX ]) ==0 ) { string_done: ctxt->restart = false; -kvm_rip_write (ctxt->vcpu, c->eip); + ctxt->eip = c->eip; goto done; }/* The second termination condition only applies for REPE
@@ -3032,7 +3033,7 @@ writeback: ctxt->decode.mem_read.end =0 ;/* Commit shadow register state. */ memcpy (ctxt->vcpu->arch.regs, c->regs,sizeof c->regs); -kvm_rip_write (ctxt->vcpu, c->eip); + ctxt->eip = c->eip; ops->set_rflags (ctxt->vcpu, ctxt->eflags); done:
arch/x86/kvm/x86.c +3 -1
@@ -3879,6 +3879,7 @@ restart: shadow_mask = vcpu->arch.emulate_ctxt.interruptibility; kvm_x86_ops->set_interrupt_shadow (vcpu, shadow_mask); +kvm_rip_write (vcpu, vcpu->arch.emulate_ctxt.eip);if (vcpu->arch.pio.count) {if (!vcpu->arch.pio.in)
@@ -4882,7 +4883,8 @@ int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason,if (ret)return EMULATE_FAIL ; - + +kvm_rip_write (vcpu, vcpu->arch.emulate_ctxt.eip); kvm_x86_ops->set_rflags (vcpu, vcpu->arch.emulate_ctxt.eflags);return EMULATE_DONE ; }
Signature · 1 line
1.6.5
PATCH v2 19/23 KVM: x86 emulator: set RFLAGS outside x86 emulator code.
In reply to Gleb NatapovRemoves the need for set_flags() callback.
--- arch/x86/include/asm/kvm_emulate.h | 1 - arch/x86/kvm/emulate.c | 1 - arch/x86/kvm/x86.c | 7 +------ 3 files changed, 1 insertions(+), 8 deletions(-)
arch/x86/include/asm/kvm_emulate.h +0 -1
@@ -142,7 +142,6 @@ struct x86_emulate_ops {ulong (*get_cr)(int cr,struct kvm_vcpu *vcpu);int (*set_cr)(int cr,ulong val,struct kvm_vcpu *vcpu);int (*cpl)(struct kvm_vcpu *vcpu); -void (*set_rflags)(struct kvm_vcpu *vcpu,unsigned long rflags);int (*get_dr)(int dr,unsigned long *dest,struct kvm_vcpu *vcpu);int (*set_dr)(int dr,unsigned long value,struct kvm_vcpu *vcpu);int (*set_msr)(struct kvm_vcpu *vcpu,u32 msr_index,u64 data);
arch/x86/kvm/emulate.c +0 -1
@@ -3034,7 +3034,6 @@ writeback:/* Commit shadow register state. */ memcpy (ctxt->vcpu->arch.regs, c->regs,sizeof c->regs); ctxt->eip = c->eip; - ops->set_rflags (ctxt->vcpu, ctxt->eflags); done:return (rc ==X86EMUL_UNHANDLEABLE ) ?-1 :0 ;
arch/x86/kvm/x86.c +1 -6
@@ -3744,11 +3744,6 @@ static void emulator_set_segment_selector(u16 sel, int seg,kvm_set_segment (vcpu, &kvm_seg, seg); } -static void emulator_set_rflags (struct kvm_vcpu *vcpu,unsigned long rflags) -{ - kvm_x86_ops->set_rflags (vcpu, rflags); -} -static struct x86_emulate_ops emulate_ops = { .read_std = kvm_read_guest_virt_system, .write_std = kvm_write_guest_virt_system,
@@ -3767,7 +3762,6 @@ static struct x86_emulate_ops emulate_ops = { .get_cr = emulator_get_cr, .set_cr = emulator_set_cr, .cpl = emulator_get_cpl, - .set_rflags = emulator_set_rflags, .get_dr = emulator_get_dr, .set_dr = emulator_set_dr, .set_msr = kvm_set_msr,
@@ -3879,6 +3873,7 @@ restart: shadow_mask = vcpu->arch.emulate_ctxt.interruptibility; kvm_x86_ops->set_interrupt_shadow (vcpu, shadow_mask); + kvm_x86_ops->set_rflags (vcpu, vcpu->arch.emulate_ctxt.eflags);kvm_rip_write (vcpu, vcpu->arch.emulate_ctxt.eip);if (vcpu->arch.pio.count) {
Signature · 1 line
1.6.5
PATCH v2 20/23 KVM: x86 emulator: use shadowed register in emulate_sysexit()
In reply to Gleb Natapovemulate_sysexit() should use shadowed registers copy instead of looking into vcpu state directly.
--- arch/x86/kvm/emulate.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +2 -2
@@ -2063,8 +2063,8 @@ emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) ops->set_cached_descriptor (&ss,VCPU_SREG_SS , ctxt->vcpu); ops->set_segment_selector (ss_sel,VCPU_SREG_SS , ctxt->vcpu); - c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX ]; - c->regs[VCPU_REGS_RSP ] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX ]; + c->eip = c->regs[VCPU_REGS_RDX ]; + c->regs[VCPU_REGS_RSP ] = c->regs[VCPU_REGS_RCX ];return X86EMUL_CONTINUE ; }
Signature · 1 line
1.6.5
PATCH v2 21/23 KVM: x86 exmulator: handle shadowed registers outside emulator.
In reply to Gleb NatapovEmulator shouldn't access vcpu directly.
--- arch/x86/kvm/emulate.c | 15 --------------- arch/x86/kvm/x86.c | 16 +++++++++++++--- 2 files changed, 13 insertions(+), 18 deletions(-)
arch/x86/kvm/emulate.c +0 -15
@@ -941,12 +941,9 @@ x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)/* we cannot decode insn before we complete previous rep insn */ WARN_ON (ctxt->restart); -/* Shadow copy of register state. Committed on successful emulation. */ -memset (c,0 ,sizeof (struct decode_cache )); c->eip = ctxt->eip; c->fetch.start = c->fetch.end = c->eip; ctxt->cs_base =seg_base (ctxt, ops,VCPU_SREG_CS ); -memcpy (c->regs, ctxt->vcpu->arch.regs,sizeof c->regs);switch (mode) {case X86EMUL_MODE_REAL :
@@ -2486,16 +2483,13 @@ int emulator_task_switch(struct x86_emulate_ctxt *ctxt,struct decode_cache *c = &ctxt->decode;int rc; -memset (c,0 ,sizeof (struct decode_cache )); c->eip = ctxt->eip; -memcpy (c->regs, ctxt->vcpu->arch.regs,sizeof c->regs); c->dst.type =OP_NONE ; rc =emulator_do_task_switch (ctxt, ops, tss_selector, reason, has_error_code, error_code);if (rc ==X86EMUL_CONTINUE ) { -memcpy (ctxt->vcpu->arch.regs, c->regs,sizeof c->regs); rc =writeback (ctxt, ops);if (rc ==X86EMUL_CONTINUE ) ctxt->eip = c->eip;
@@ -2525,13 +2519,6 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) ctxt->interruptibility =0 ; ctxt->decode.mem_read.pos =0 ; -/* Shadow copy of register state. Committed on successful emulation. -* NOTE: we can copy them from vcpu as x86_decode_insn() doesn't -* modify them. -*/ - -memcpy (c->regs, ctxt->vcpu->arch.regs,sizeof c->regs); -if (ctxt->mode ==X86EMUL_MODE_PROT64 && (c->d & No64)) {kvm_queue_exception (ctxt->vcpu,UD_VECTOR ); goto done;
@@ -3031,8 +3018,6 @@ writeback:* without decoding */ ctxt->decode.mem_read.end =0 ; -/* Commit shadow register state. */ -memcpy (ctxt->vcpu->arch.regs, c->regs,sizeof c->regs); ctxt->eip = c->eip; done:
arch/x86/kvm/x86.c +13 -3
@@ -3782,7 +3782,7 @@ int emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type) {int r, shadow_mask; -struct decode_cache *c; +struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode;kvm_clear_exception_queue (vcpu); vcpu->arch.mmio_fault_cr2 = cr2;
@@ -3807,13 +3807,14 @@ int emulate_instruction(struct kvm_vcpu *vcpu, ? X86EMUL_MODE_VM86 : cs_l ?X86EMUL_MODE_PROT64 : cs_db ?X86EMUL_MODE_PROT32 :X86EMUL_MODE_PROT16 ; +memset (c,0 ,sizeof (struct decode_cache )); +memcpy (c->regs, vcpu->arch.regs,sizeof c->regs); r =x86_decode_insn (&vcpu->arch.emulate_ctxt, &emulate_ops);trace_kvm_emulate_insn_start (vcpu);/* Only allow emulation of specific instructions on #UD * (namely VMMCALL, sysenter, sysexit, syscall)*/ - c = &vcpu->arch.emulate_ctxt.decode;if (emulation_type &EMULTYPE_TRAP_UD ) {if (!c->twobyte)return EMULATE_FAIL ;
@@ -3854,6 +3855,10 @@ int emulate_instruction(struct kvm_vcpu *vcpu,return EMULATE_DONE ; } +/* this is needed for vmware backdor interface to work since it +changes registers values during IO operation */ +memcpy (c->regs, vcpu->arch.regs,sizeof c->regs); + restart: r =x86_emulate_insn (&vcpu->arch.emulate_ctxt, &emulate_ops);
@@ -3874,6 +3879,7 @@ restart: shadow_mask = vcpu->arch.emulate_ctxt.interruptibility; kvm_x86_ops->set_interrupt_shadow (vcpu, shadow_mask); kvm_x86_ops->set_rflags (vcpu, vcpu->arch.emulate_ctxt.eflags); +memcpy (vcpu->arch.regs, c->regs,sizeof c->regs);kvm_rip_write (vcpu, vcpu->arch.emulate_ctxt.eip);if (vcpu->arch.pio.count) {
@@ -4857,6 +4863,7 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,int kvm_task_switch (struct kvm_vcpu *vcpu,u16 tss_selector,int reason,bool has_error_code,u32 error_code) { +struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode;int cs_db, cs_l, ret;cache_all_regs (vcpu);
@@ -4871,6 +4878,8 @@ int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason, ? X86EMUL_MODE_VM86 : cs_l ?X86EMUL_MODE_PROT64 : cs_db ?X86EMUL_MODE_PROT32 :X86EMUL_MODE_PROT16 ; +memset (c,0 ,sizeof (struct decode_cache )); +memcpy (c->regs, vcpu->arch.regs,sizeof c->regs); ret = emulator_task_switch(&vcpu->arch.emulate_ctxt, &emulate_ops, tss_selector, reason, has_error_code,
@@ -4878,7 +4887,8 @@ int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason,if (ret)return EMULATE_FAIL ; - + +memcpy (vcpu->arch.regs, c->regs,sizeof c->regs);kvm_rip_write (vcpu, vcpu->arch.emulate_ctxt.eip); kvm_x86_ops->set_rflags (vcpu, vcpu->arch.emulate_ctxt.eflags);return EMULATE_DONE ;
Signature · 1 line
1.6.5
PATCH v2 22/23 KVM: x86 emulator: move interruptibility state tracking out of emulator
In reply to Gleb NatapovEmulator shouldn't access vcpu directly.
--- arch/x86/kvm/emulate.c | 19 ++----------------- arch/x86/kvm/x86.c | 20 +++++++++++++++++--- 2 files changed, 19 insertions(+), 20 deletions(-)
arch/x86/kvm/emulate.c +2 -17
@@ -1843,20 +1843,6 @@ static inline int writeback(struct x86_emulate_ctxt *ctxt,return X86EMUL_CONTINUE ; } -static void toggle_interruptibility (struct x86_emulate_ctxt *ctxt,u32 mask) -{ -u32 int_shadow = kvm_x86_ops->get_interrupt_shadow (ctxt->vcpu, mask); -/* -* an sti; sti; sequence only disable interrupts for the first -* instruction. So, if the last instruction, be it emulated or -* not, left the system with the INT_STI flag enabled, it -* means that the last instruction is an sti. We should not -* leave the flag on in this case. The same goes for mov ss -*/ -if (!(int_shadow & mask)) - ctxt->interruptibility = mask; -} - static inline void setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops, struct desc_struct *cs,
@@ -2516,7 +2502,6 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)int rc =X86EMUL_CONTINUE ;int saved_dst_type = c->dst.type; - ctxt->interruptibility =0 ; ctxt->decode.mem_read.pos =0 ;if (ctxt->mode ==X86EMUL_MODE_PROT64 && (c->d & No64)) {
@@ -2789,7 +2774,7 @@ special_insn: }if (c->modrm_reg ==VCPU_SREG_SS ) -toggle_interruptibility (ctxt,KVM_X86_SHADOW_INT_MOV_SS ); + ctxt->interruptibility =KVM_X86_SHADOW_INT_MOV_SS ; rc =load_segment_descriptor (ctxt, ops, sel, c->modrm_reg);
@@ -2958,7 +2943,7 @@ special_insn:if (emulator_bad_iopl (ctxt, ops))kvm_inject_gp (ctxt->vcpu,0 );else { -toggle_interruptibility (ctxt,KVM_X86_SHADOW_INT_STI ); + ctxt->interruptibility =KVM_X86_SHADOW_INT_STI ; ctxt->eflags |=X86_EFLAGS_IF ; c->dst.type =OP_NONE ;/* Disable writeback. */ }
arch/x86/kvm/x86.c +17 -3
@@ -3776,12 +3776,26 @@ static void cache_all_regs(struct kvm_vcpu *vcpu) vcpu->arch.regs_dirty = ~0 ; } +static void toggle_interruptibility (struct kvm_vcpu *vcpu,u32 mask) +{ +u32 int_shadow = kvm_x86_ops->get_interrupt_shadow (vcpu, mask); +/* +* an sti; sti; sequence only disable interrupts for the first +* instruction. So, if the last instruction, be it emulated or +* not, left the system with the INT_STI flag enabled, it +* means that the last instruction is an sti. We should not +* leave the flag on in this case. The same goes for mov ss +*/ +if (!(int_shadow & mask)) + kvm_x86_ops->set_interrupt_shadow (vcpu, mask); +} +int emulate_instruction (struct kvm_vcpu *vcpu,unsigned long cr2,u16 error_code,int emulation_type) { -int r, shadow_mask; +int r;struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode;kvm_clear_exception_queue (vcpu);
@@ -3809,6 +3823,7 @@ int emulate_instruction(struct kvm_vcpu *vcpu, ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;memset (c,0 ,sizeof (struct decode_cache ));memcpy (c->regs, vcpu->arch.regs,sizeof c->regs); + vcpu->arch.emulate_ctxt.interruptibility =0 ; r =x86_decode_insn (&vcpu->arch.emulate_ctxt, &emulate_ops);trace_kvm_emulate_insn_start (vcpu);
@@ -3876,8 +3891,7 @@ restart:return EMULATE_FAIL ; } - shadow_mask = vcpu->arch.emulate_ctxt.interruptibility; - kvm_x86_ops->set_interrupt_shadow (vcpu, shadow_mask); +toggle_interruptibility (vcpu, vcpu->arch.emulate_ctxt.interruptibility); kvm_x86_ops->set_rflags (vcpu, vcpu->arch.emulate_ctxt.eflags);memcpy (vcpu->arch.regs, c->regs,sizeof c->regs);kvm_rip_write (vcpu, vcpu->arch.emulate_ctxt.eip);
Signature · 1 line
1.6.5
PATCH v2 23/23 KVM: x86 emulator: do not inject exception directly into vcpu
In reply to Gleb NatapovReturn exception as a result of instruction emulation and handle injection in KVM code.
--- arch/x86/include/asm/kvm_emulate.h | 6 ++ arch/x86/kvm/emulate.c | 124 ++++++++++++++++++++++-------------- arch/x86/kvm/x86.c | 20 +++++- 3 files changed, 100 insertions(+), 50 deletions(-)
arch/x86/include/asm/kvm_emulate.h +6 -0
@@ -216,6 +216,12 @@ struct x86_emulate_ctxt {int interruptibility;bool restart;/* restart string instruction after writeback */ + +int exception;/* exception that happens during emulation or -1 */ +u32 error_code;/* error code for exception */ +bool error_code_valid; +unsigned long cr2;/* faulted address in case of #PF */ +/* decode cache */ struct decode_cache decode; };
arch/x86/kvm/emulate.c +77 -47
402 lines not on this page · raw message
arch/x86/kvm/x86.c +17 -3
@@ -3790,6 +3790,17 @@ static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask) kvm_x86_ops->set_interrupt_shadow (vcpu, mask); } +static void inject_emulated_exception (struct kvm_vcpu *vcpu) +{ +struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; +if (ctxt->exception ==PF_VECTOR ) +kvm_inject_page_fault (vcpu, ctxt->cr2, ctxt->error_code); +else if (ctxt->error_code_valid) +kvm_queue_exception_e (vcpu, ctxt->exception, ctxt->error_code); +else +kvm_queue_exception (vcpu, ctxt->exception); +} + int emulate_instruction(struct kvm_vcpu *vcpu, unsigned long cr2, u16 error_code,
@@ -3824,6 +3835,7 @@ int emulate_instruction(struct kvm_vcpu *vcpu,memset (c,0 ,sizeof (struct decode_cache ));memcpy (c->regs, vcpu->arch.regs,sizeof c->regs); vcpu->arch.emulate_ctxt.interruptibility =0 ; + vcpu->arch.emulate_ctxt.exception =-1 ; r =x86_decode_insn (&vcpu->arch.emulate_ctxt, &emulate_ops);trace_kvm_emulate_insn_start (vcpu);
@@ -3896,6 +3908,11 @@ restart:memcpy (vcpu->arch.regs, c->regs,sizeof c->regs);kvm_rip_write (vcpu, vcpu->arch.emulate_ctxt.eip); +if (vcpu->arch.emulate_ctxt.exception >=0 ) { +inject_emulated_exception (vcpu); +return EMULATE_DONE ; + } +if (vcpu->arch.pio.count) {if (!vcpu->arch.pio.in) vcpu->arch.pio.count =0 ;
@@ -3908,9 +3925,6 @@ restart:return EMULATE_DO_MMIO ; } -if (vcpu->arch.exception.pending) - vcpu->arch.emulate_ctxt.restart = false; -if (vcpu->arch.emulate_ctxt.restart) goto restart;
Signature · 1 line
1.6.5
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: qemu-kvm.0.12.2 aborts on linux
Quoted 10 lines · K D
Am using yahoo mail and my mails to this mailer gets rejected every time saying message has HTML content etc. Should I use some other mail tool? Below is my issue.
I am trying to get KVM/qemu running on linux. I compiled 2.6.27.10 by enabling "KVM", "KVM for intel" options at configure time. My box is running with this KVM enabled inside kernel. I also built qemu-kvm-0.12.2 using above kernel headers etc. I enabled virtualization in BIOS. I didn't try to install any guest from CD etc. I made a hard disk image, installed grub on it, copied kernel, initrd onto it. Now when I try to create Vm as below, it crashes with following backtrace. What could be going wrong?
Also when I try to say "-m 256" malloc (or
posix_memalign) fails with ENOMEM. So right now "-m 128", which is default, works. Why is that? I have 4G RAM in my setup and my native linux is using less than 1G. Is there some rlimits for qemu that I need to raise?Sounds like I'm doing some basic stuff wrong. I'm using bios, vapic, pxe-rtl bin straight from the qemu-kvm dir.
If I don't do '-nographic' its running into some malloc failure inside some vga routine. I pasted that backtrace too below.
What's your Linux distribution? First trace bellow shows that ptheard_create() failed which is strange. What ulimit -a shows?
Quoted 72 lines
Appreciate your help.
thanksqemu-system-x86_64 -hda
/dev/shm/vmhd.img-bios ./bios.bin --option-rom ./vapic.bin -curses -nographic -vga none -option-rom ./pxe-rtl8139.bin#0
0x414875a6in raise () from/lib/libc.so.6
(gdb) bt
#00x414875a6in raise () from/lib/libc.so.6
#10x4148ad18in abort () from/lib/libc.so.6
#20x080b4cb3in die2 (err=<value optimized out>,
what=0x81f2662 "pthread_create") at posix-aio-compat.c:80
#30x080b5682inthread_create(arg=<value optimized out>, start_routine=<value optimized out>, attr=<value optimized out>, thread=<value optimized out>) at posix-aio-compat.c:118
#4spawn_thread() at posix-aio-compat.c:379
#5qemu_paio_submit(aiocb=0x846b550) at posix-aio-compat.c:390 #60x080b57cbinpaio_submit(bs=0x843c008, fd=5, sector_num=0, qiov=0x84cefb8, nb_sectors=512, cb=0x81cb950 <dma_bdrv_cb>, opaque=0x84cef80, type=1) at posix-aio-compat.c:584
#70x080cc7b8inraw_aio_submit(type=<value optimized out>, opaque=<value optimized out>, cb=<value optimized out>, nb_sectors=<value optimized out>, qiov=<value optimized out>, sector_num=<value optimized out>, bs=<value optimized out>)
atblock/raw-posix.c:562
#8raw_aio_readv(bs=0x843c008, sector_num=0, qiov=0x84cefb8, nb_sectors=1, cb=0x81cb950 <dma_bdrv_cb>, opaque=0x84cef80) atblock/raw-posix.c:570#90x080b0593inbdrv_aio_readv(bs=0x843c008, sector_num=0, qiov=0x84cefb8, nb_sectors=1, cb=0x81cb950 <dma_bdrv_cb>, opaque=0x84cef80)
at block.c:1548
#100x081cbb26indma_bdrv_cb(opaque=0x84cef80, ret=0)
at /ws/pkoya-sjc/temp/qemu-kvm-0.12.2/dma-helpers.c:123
#110x081cbcdeindma_bdrv_io(bs=0x843c008, sg=0x846861c, sector_num=0, cb=0x8074320 <ide_read_dma_cb>, opaque=0x8468f1c, is_write=0)
at /ws/pkoya-sjc/temp/qemu-kvm-0.12.2/dma-helpers.c:167
#120x0807441binide_read_dma_cb(opaque=0x8468f1c, ret=0)
at /ws/pkoya-sjc/temp/qemu-kvm-0.12.2/hw/ide/core.c:597
#130x080760ecinbmdma_cmd_writeb(opaque=0x8468f1c, addr=49152, val=9) at /ws/pkoya-sjc/temp/qemu-kvm-0.12.2/hw/ide/pci.c:51
#140x080d9d5finioport_write(data=<value optimized out>, address=<value optimized out>, index=<value optimized out>) at ioport.c:80 #15cpu_outb(addr=6587, val=<value optimized out>) at ioport.c:198 #160xb60a3bc9in ?? ()
#170x0000c000in ?? ()
#180x00000009in ?? ()
#190x00000000in ?? ()
(gdb) qbacktrace without "-nographic"
============================
(gdb) bt
#00x414875a6in raise () from/lib/libc.so.6
#10x4148ad18in abort () from/lib/libc.so.6
#20x080b4c3cinqemu_memalign(alignment=4096, size=16777216) at osdep.c:96 #30x080b4c5ainqemu_vmalloc(size=16777216) at osdep.c:110
#40x08119995inqemu_ram_alloc(size=16777216)
at /devel/temp/qemu-kvm-0.12.2/exec.c:2550
#50x0807ffd0invga_common_init(s=0x84be7e4, vga_ram_size=16777216) at /devel/temp/qemu-kvm-0.12.2/hw/vga.c:2291
#60x080a1c4binpci_cirrus_vga_initfn(dev=0x84be618)
at /devel/temp/qemu-kvm-0.12.2/hw/cirrus_vga.c:3209
#70x0805e61einpci_qdev_init(qdev=0x84be618, base=0x8229700) at /devel/temp/qemu-kvm-0.12.2/hw/pci.c:1482
#80x080fa7eeinqdev_init(dev=0x84be618)
at /devel/temp/qemu-kvm-0.12.2/hw/qdev.c:242
#90x080fa885inqdev_init_nofail(dev=0x84be618)
at /devel/temp/qemu-kvm-0.12.2/hw/qdev.c:285
#100x0805d8cainpci_create_simple(bus=0x845ab58, devfn=-1, name=0x81ce0f8 "cirrus-vga")
at /devel/temp/qemu-kvm-0.12.2/hw/pci.c:1533
#110x080a2c71inpci_cirrus_vga_init(bus=0x845ab58)
at /devel/temp/qemu-kvm-0.12.2/hw/cirrus_vga.c:3235
#120x0808abc3inpc_init1(ram_size=<value optimized out>,
Quoted 9 lines
boot_device=0xbf9fea17 "cad", kernel_filename=0x0,
kernel_cmdline=0x81f82f8 "", initrd_filename=0x0,
cpu_model=0x81eca10 "qemu64", pci_enabled=1)
at /devel/temp/qemu-kvm-0.12.2/hw/pc.c:1149
#130x080518dbin main (argc=8, argv=0xbf9feaf4, envp=Cannot access memory at address0x7730)
at /devel/temp/qemu-kvm-0.12.2/vl.c:6055
(gdb) q
--
Gleb.
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: qemu-kvm.0.12.2 aborts on linux
Quoted 18 lines · K D
Here are my ulimits
$ ulimit -a core file size (blocks, -c) unlimited data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 71680 max locked memory (kbytes, -l) 32 max memory size (kbytes, -m) unlimited open files (-n) 1024 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 1024 virtual memory (kbytes, -v) 204800
Your virtual memory is limited to 200M. Make it unlimited.
Quoted 115 lines
file locks (-x) unlimited
$I'm using monta vista distribution. I went through qemu code and there is no place to raise rlimits. didn't want to touch it.
thanks for looking.
________________________________
From: Gleb Natapov <[email protected]>
To: K D <[email protected]>
Cc: [email protected]; [email protected]; [email protected]
Sent: Thu, April 29, 2010 3:17:06 AM
Subject: Re: qemu-kvm.0.12.2 aborts on linuxQuoted 10 lines · K D
Am using yahoo mail and my mails to this mailer gets rejected every time saying message has HTML content etc. Should I use some other mail tool? Below is my issue.
I am trying to get KVM/qemu running on linux. I compiled 2.6.27.10 by enabling "KVM", "KVM for intel" options at configure time. My box is running with this KVM enabled inside kernel. I also built qemu-kvm-0.12.2 using above kernel headers etc. I enabled virtualization in BIOS. I didn't try to install any guest from CD etc. I made a hard disk image, installed grub on it, copied kernel, initrd onto it. Now when I try to create Vm as below, it crashes with following backtrace. What could be going wrong?
Also when I try to say "-m 256" malloc (or
posix_memalign) fails with ENOMEM. So right now "-m 128", which is default, works. Why is that? I have 4G RAM in my setup and my native linux is using less than 1G. Is there some rlimits for qemu that I need to raise?Sounds like I'm doing some basic stuff wrong. I'm using bios, vapic, pxe-rtl bin straight from the qemu-kvm dir.
If I don't do '-nographic' its running into some malloc failure inside some vga routine. I pasted that backtrace too below.
What's your Linux distribution? First trace bellow shows that
ptheard_create()failed which is strange. What ulimit -a shows?Quoted 72 lines
Appreciate your help.
thanksqemu-system-x86_64 -hda
/dev/shm/vmhd.img-bios ./bios.bin --option-rom ./vapic.bin -curses -nographic -vga none -option-rom ./pxe-rtl8139.bin#0
0x414875a6in raise () from/lib/libc.so.6
(gdb) bt
#00x414875a6in raise () from/lib/libc.so.6
#10x4148ad18in abort () from/lib/libc.so.6
#20x080b4cb3in die2 (err=<value optimized out>,
what=0x81f2662 "pthread_create") at posix-aio-compat.c:80
#30x080b5682inthread_create(arg=<value optimized out>, start_routine=<value optimized out>, attr=<value optimized out>, thread=<value optimized out>) at posix-aio-compat.c:118
#4spawn_thread() at posix-aio-compat.c:379
#5qemu_paio_submit(aiocb=0x846b550) at posix-aio-compat.c:390 #60x080b57cbinpaio_submit(bs=0x843c008, fd=5, sector_num=0, qiov=0x84cefb8, nb_sectors=512, cb=0x81cb950 <dma_bdrv_cb>, opaque=0x84cef80, type=1) at posix-aio-compat.c:584
#70x080cc7b8inraw_aio_submit(type=<value optimized out>, opaque=<value optimized out>, cb=<value optimized out>, nb_sectors=<value optimized out>, qiov=<value optimized out>, sector_num=<value optimized out>, bs=<value optimized out>)
atblock/raw-posix.c:562
#8raw_aio_readv(bs=0x843c008, sector_num=0, qiov=0x84cefb8, nb_sectors=1, cb=0x81cb950 <dma_bdrv_cb>, opaque=0x84cef80) atblock/raw-posix.c:570#90x080b0593inbdrv_aio_readv(bs=0x843c008, sector_num=0, qiov=0x84cefb8, nb_sectors=1, cb=0x81cb950 <dma_bdrv_cb>, opaque=0x84cef80)
at block.c:1548
#100x081cbb26indma_bdrv_cb(opaque=0x84cef80, ret=0)
at /ws/pkoya-sjc/temp/qemu-kvm-0.12.2/dma-helpers.c:123
#110x081cbcdeindma_bdrv_io(bs=0x843c008, sg=0x846861c, sector_num=0, cb=0x8074320 <ide_read_dma_cb>, opaque=0x8468f1c, is_write=0)
at /ws/pkoya-sjc/temp/qemu-kvm-0.12.2/dma-helpers.c:167
#120x0807441binide_read_dma_cb(opaque=0x8468f1c, ret=0)
at /ws/pkoya-sjc/temp/qemu-kvm-0.12.2/hw/ide/core.c:597
#130x080760ecinbmdma_cmd_writeb(opaque=0x8468f1c, addr=49152, val=9) at /ws/pkoya-sjc/temp/qemu-kvm-0.12.2/hw/ide/pci.c:51
#140x080d9d5finioport_write(data=<value optimized out>, address=<value optimized out>, index=<value optimized out>) at ioport.c:80 #15cpu_outb(addr=6587, val=<value optimized out>) at ioport.c:198 #160xb60a3bc9in ?? ()
#170x0000c000in ?? ()
#180x00000009in ?? ()
#190x00000000in ?? ()
(gdb) qbacktrace without "-nographic"
============================
(gdb) bt
#00x414875a6in raise () from/lib/libc.so.6
#10x4148ad18in abort () from/lib/libc.so.6
#20x080b4c3cinqemu_memalign(alignment=4096, size=16777216) at osdep.c:96 #30x080b4c5ainqemu_vmalloc(size=16777216) at osdep.c:110
#40x08119995inqemu_ram_alloc(size=16777216)
at /devel/temp/qemu-kvm-0.12.2/exec.c:2550
#50x0807ffd0invga_common_init(s=0x84be7e4, vga_ram_size=16777216) at /devel/temp/qemu-kvm-0.12.2/hw/vga.c:2291
#60x080a1c4binpci_cirrus_vga_initfn(dev=0x84be618)
at /devel/temp/qemu-kvm-0.12.2/hw/cirrus_vga.c:3209
#70x0805e61einpci_qdev_init(qdev=0x84be618, base=0x8229700) at /devel/temp/qemu-kvm-0.12.2/hw/pci.c:1482
#80x080fa7eeinqdev_init(dev=0x84be618)
at /devel/temp/qemu-kvm-0.12.2/hw/qdev.c:242
#90x080fa885inqdev_init_nofail(dev=0x84be618)
at /devel/temp/qemu-kvm-0.12.2/hw/qdev.c:285
#100x0805d8cainpci_create_simple(bus=0x845ab58, devfn=-1, name=0x81ce0f8 "cirrus-vga")
at /devel/temp/qemu-kvm-0.12.2/hw/pci.c:1533
#110x080a2c71inpci_cirrus_vga_init(bus=0x845ab58)
at /devel/temp/qemu-kvm-0.12.2/hw/cirrus_vga.c:3235
#120x0808abc3inpc_init1(ram_size=<value optimized out>,Quoted 9 lines
boot_device=0xbf9fea17 "cad", kernel_filename=0x0,
kernel_cmdline=0x81f82f8 "", initrd_filename=0x0,
cpu_model=0x81eca10 "qemu64", pci_enabled=1)
at /devel/temp/qemu-kvm-0.12.2/hw/pc.c:1149
#130x080518dbin main (argc=8, argv=0xbf9feaf4, envp=Cannot access memory at address0x7730)
at /devel/temp/qemu-kvm-0.12.2/vl.c:6055
(gdb) q
Quoted 6 lines
--
Gleb.
--
Gleb.
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: qemu-kvm.0.12.2 aborts on linux
Quoted 2 lines · K D
After I added code to raise ulimits for qemu, I don't see any memory related issue. I'm trying to spawn VM on an embedded linux with no window manager etc. with '-curses' it goes into 'VGA Blank Mode' and it stops there. not sure what it is doing. any clues?
Haven't used '-curses' option for a long time. Have you provided bootable disk? Does your guest boots into graphical mode or text mode?
Quoted 158 lines
thanks for help.
________________________________
From: Gleb Natapov <[email protected]>
To: K D <[email protected]>
Cc: [email protected]; [email protected]; [email protected]
Sent: Sat, May 1, 2010 10:23:16 PM
Subject: Re: qemu-kvm.0.12.2 aborts on linuxQuoted 18 lines · K D
Here are my ulimits
$ ulimit -a core file size (blocks, -c) unlimited data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 71680 max locked memory (kbytes, -l) 32 max memory size (kbytes, -m) unlimited open files (-n) 1024 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 1024 virtual memory (kbytes, -v) 204800Your virtual memory is limited to 200M. Make it unlimited.
Quoted 115 lines
file locks (-x) unlimited
$I'm using monta vista distribution. I went through qemu code and there is no place to raise rlimits. didn't want to touch it.
thanks for looking.
________________________________
From: Gleb Natapov <[email protected]>
To: K D <[email protected]>
Cc: [email protected]; [email protected]; [email protected]
Sent: Thu, April 29, 2010 3:17:06 AM
Subject: Re: qemu-kvm.0.12.2 aborts on linuxQuoted 10 lines · K D
Am using yahoo mail and my mails to this mailer gets rejected every time saying message has HTML content etc. Should I use some other mail tool? Below is my issue.
I am trying to get KVM/qemu running on linux. I compiled 2.6.27.10 by enabling "KVM", "KVM for intel" options at configure time. My box is running with this KVM enabled inside kernel. I also built qemu-kvm-0.12.2 using above kernel headers etc. I enabled virtualization in BIOS. I didn't try to install any guest from CD etc. I made a hard disk image, installed grub on it, copied kernel, initrd onto it. Now when I try to create Vm as below, it crashes with following backtrace. What could be going wrong?
Also when I try to say "-m 256" malloc (or
posix_memalign) fails with ENOMEM. So right now "-m 128", which is default, works. Why is that? I have 4G RAM in my setup and my native linux is using less than 1G. Is there some rlimits for qemu that I need to raise?Sounds like I'm doing some basic stuff wrong. I'm using bios, vapic, pxe-rtl bin straight from the qemu-kvm dir.
If I don't do '-nographic' its running into some malloc failure inside some vga routine. I pasted that backtrace too below.
What's your Linux distribution? First trace bellow shows that
ptheard_create()failed which is strange. What ulimit -a shows?Quoted 72 lines
Appreciate your help.
thanksqemu-system-x86_64 -hda
/dev/shm/vmhd.img-bios ./bios.bin --option-rom ./vapic.bin -curses -nographic -vga none -option-rom ./pxe-rtl8139.bin#0
0x414875a6in raise () from/lib/libc.so.6
(gdb) bt
#00x414875a6in raise () from/lib/libc.so.6
#10x4148ad18in abort () from/lib/libc.so.6
#20x080b4cb3in die2 (err=<value optimized out>,
what=0x81f2662 "pthread_create") at posix-aio-compat.c:80
#30x080b5682inthread_create(arg=<value optimized out>, start_routine=<value optimized out>, attr=<value optimized out>, thread=<value optimized out>) at posix-aio-compat.c:118
#4spawn_thread() at posix-aio-compat.c:379
#5qemu_paio_submit(aiocb=0x846b550) at posix-aio-compat.c:390 #60x080b57cbinpaio_submit(bs=0x843c008, fd=5, sector_num=0, qiov=0x84cefb8, nb_sectors=512, cb=0x81cb950 <dma_bdrv_cb>, opaque=0x84cef80, type=1) at posix-aio-compat.c:584
#70x080cc7b8inraw_aio_submit(type=<value optimized out>, opaque=<value optimized out>, cb=<value optimized out>, nb_sectors=<value optimized out>, qiov=<value optimized out>, sector_num=<value optimized out>, bs=<value optimized out>)
atblock/raw-posix.c:562
#8raw_aio_readv(bs=0x843c008, sector_num=0, qiov=0x84cefb8, nb_sectors=1, cb=0x81cb950 <dma_bdrv_cb>, opaque=0x84cef80) atblock/raw-posix.c:570#90x080b0593inbdrv_aio_readv(bs=0x843c008, sector_num=0, qiov=0x84cefb8, nb_sectors=1, cb=0x81cb950 <dma_bdrv_cb>, opaque=0x84cef80)
at block.c:1548
#100x081cbb26indma_bdrv_cb(opaque=0x84cef80, ret=0)
at /ws/pkoya-sjc/temp/qemu-kvm-0.12.2/dma-helpers.c:123
#110x081cbcdeindma_bdrv_io(bs=0x843c008, sg=0x846861c, sector_num=0, cb=0x8074320 <ide_read_dma_cb>, opaque=0x8468f1c, is_write=0)
at /ws/pkoya-sjc/temp/qemu-kvm-0.12.2/dma-helpers.c:167
#120x0807441binide_read_dma_cb(opaque=0x8468f1c, ret=0)
at /ws/pkoya-sjc/temp/qemu-kvm-0.12.2/hw/ide/core.c:597
#130x080760ecinbmdma_cmd_writeb(opaque=0x8468f1c, addr=49152, val=9) at /ws/pkoya-sjc/temp/qemu-kvm-0.12.2/hw/ide/pci.c:51
#140x080d9d5finioport_write(data=<value optimized out>, address=<value optimized out>, index=<value optimized out>) at ioport.c:80 #15cpu_outb(addr=6587, val=<value optimized out>) at ioport.c:198 #160xb60a3bc9in ?? ()
#170x0000c000in ?? ()
#180x00000009in ?? ()
#190x00000000in ?? ()
(gdb) qbacktrace without "-nographic"
============================
(gdb) bt
#00x414875a6in raise () from/lib/libc.so.6
#10x4148ad18in abort () from/lib/libc.so.6
#20x080b4c3cinqemu_memalign(alignment=4096, size=16777216) at osdep.c:96 #30x080b4c5ainqemu_vmalloc(size=16777216) at osdep.c:110
#40x08119995inqemu_ram_alloc(size=16777216)
at /devel/temp/qemu-kvm-0.12.2/exec.c:2550
#50x0807ffd0invga_common_init(s=0x84be7e4, vga_ram_size=16777216) at /devel/temp/qemu-kvm-0.12.2/hw/vga.c:2291
#60x080a1c4binpci_cirrus_vga_initfn(dev=0x84be618)
at /devel/temp/qemu-kvm-0.12.2/hw/cirrus_vga.c:3209
#70x0805e61einpci_qdev_init(qdev=0x84be618, base=0x8229700) at /devel/temp/qemu-kvm-0.12.2/hw/pci.c:1482
#80x080fa7eeinqdev_init(dev=0x84be618)
at /devel/temp/qemu-kvm-0.12.2/hw/qdev.c:242
#90x080fa885inqdev_init_nofail(dev=0x84be618)
at /devel/temp/qemu-kvm-0.12.2/hw/qdev.c:285
#100x0805d8cainpci_create_simple(bus=0x845ab58, devfn=-1, name=0x81ce0f8 "cirrus-vga")
at /devel/temp/qemu-kvm-0.12.2/hw/pci.c:1533
#110x080a2c71inpci_cirrus_vga_init(bus=0x845ab58)
at /devel/temp/qemu-kvm-0.12.2/hw/cirrus_vga.c:3235
#120x0808abc3inpc_init1(ram_size=<value optimized out>,Quoted 9 lines
boot_device=0xbf9fea17 "cad", kernel_filename=0x0,
kernel_cmdline=0x81f82f8 "", initrd_filename=0x0,
cpu_model=0x81eca10 "qemu64", pci_enabled=1)
at /devel/temp/qemu-kvm-0.12.2/hw/pc.c:1149
#130x080518dbin main (argc=8, argv=0xbf9feaf4, envp=Cannot access memory at address0x7730)
at /devel/temp/qemu-kvm-0.12.2/vl.c:6055
(gdb) qQuoted 6 lines
--
Gleb.
Quoted 10 lines
--
Gleb.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
--
Gleb.
Quoted 5 lines · Gleb Natapov
This is the next round of emulator cleanups. Make it even more detached from kvm. First patch introduces IO read cache which is needed to correctly emulate instructions that require more then one IO read exit during emulation.
Signature · 1 line
error compiling committee.c: too many arguments to function
Avi Kivity
AppliedQuoted 6 lines · Gleb Natapov
This is the next round of emulator cleanups. Make it even more detached from kvm. First patch introduces IO read cache which is needed to correctly emulate instructions that require more then one IO read exit during emulation.
Applied, thanks.
Signature · 1 line
error compiling committee.c: too many arguments to function