From: Avi Kivity <avi@qumranet.com>
To: linux-kernel@vger.kernel.org, kvm-devel@lists.sourceforge.net
Cc: Laurent Vivier <Laurent.Vivier@bull.net>
Subject: [PATCH 07/50] KVM: Call x86_decode_insn() only when needed
Date: Sun, 23 Dec 2007 16:50:52 +0200 [thread overview]
Message-ID: <1198421495-31481-8-git-send-email-avi@qumranet.com> (raw)
In-Reply-To: <1198421495-31481-1-git-send-email-avi@qumranet.com>
From: Laurent Vivier <Laurent.Vivier@bull.net>
Move emulate_ctxt to kvm_vcpu to keep emulate context when we exit from kvm
module. Call x86_decode_insn() only when needed. Modify x86_emulate_insn() to
not modify the context if it must be re-entered.
Signed-off-by: Laurent Vivier <Laurent.Vivier@bull.net>
Signed-off-by: Avi Kivity <avi@qumranet.com>
---
drivers/kvm/kvm.h | 8 ++++-
drivers/kvm/kvm_main.c | 77 +++++++++++++++++++++++++-------------------
drivers/kvm/svm.c | 9 +++--
drivers/kvm/vmx.c | 9 +++--
drivers/kvm/x86_emulate.c | 24 ++++++++++++--
5 files changed, 82 insertions(+), 45 deletions(-)
diff --git a/drivers/kvm/kvm.h b/drivers/kvm/kvm.h
index da9c3aa..e885b19 100644
--- a/drivers/kvm/kvm.h
+++ b/drivers/kvm/kvm.h
@@ -207,6 +207,8 @@ enum {
VCPU_SREG_LDTR,
};
+#include "x86_emulate.h"
+
struct kvm_pio_request {
unsigned long count;
int cur_count;
@@ -380,6 +382,10 @@ struct kvm_vcpu {
int cpuid_nent;
struct kvm_cpuid_entry cpuid_entries[KVM_MAX_CPUID_ENTRIES];
+
+ /* emulate context */
+
+ struct x86_emulate_ctxt emulate_ctxt;
};
struct kvm_mem_alias {
@@ -555,7 +561,7 @@ enum emulation_result {
};
int emulate_instruction(struct kvm_vcpu *vcpu, struct kvm_run *run,
- unsigned long cr2, u16 error_code);
+ unsigned long cr2, u16 error_code, int no_decode);
void kvm_report_emulation_failure(struct kvm_vcpu *cvpu, const char *context);
void realmode_lgdt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
void realmode_lidt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
diff --git a/drivers/kvm/kvm_main.c b/drivers/kvm/kvm_main.c
index 39c54d5..fad3a08 100644
--- a/drivers/kvm/kvm_main.c
+++ b/drivers/kvm/kvm_main.c
@@ -1251,45 +1251,56 @@ struct x86_emulate_ops emulate_ops = {
int emulate_instruction(struct kvm_vcpu *vcpu,
struct kvm_run *run,
unsigned long cr2,
- u16 error_code)
+ u16 error_code,
+ int no_decode)
{
- struct x86_emulate_ctxt emulate_ctxt;
- int r;
- int cs_db, cs_l;
+ int r = 0;
vcpu->mmio_fault_cr2 = cr2;
kvm_x86_ops->cache_regs(vcpu);
- kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
-
- emulate_ctxt.vcpu = vcpu;
- emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
- emulate_ctxt.cr2 = cr2;
- emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
- ? X86EMUL_MODE_REAL : cs_l
- ? X86EMUL_MODE_PROT64 : cs_db
- ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
-
- if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
- emulate_ctxt.cs_base = 0;
- emulate_ctxt.ds_base = 0;
- emulate_ctxt.es_base = 0;
- emulate_ctxt.ss_base = 0;
- } else {
- emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
- emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
- emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
- emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
- }
-
- emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
- emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
-
vcpu->mmio_is_write = 0;
vcpu->pio.string = 0;
- r = x86_decode_insn(&emulate_ctxt, &emulate_ops);
+
+ if (!no_decode) {
+ int cs_db, cs_l;
+ kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
+
+ vcpu->emulate_ctxt.vcpu = vcpu;
+ vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
+ vcpu->emulate_ctxt.cr2 = cr2;
+ vcpu->emulate_ctxt.mode =
+ (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
+ ? X86EMUL_MODE_REAL : cs_l
+ ? X86EMUL_MODE_PROT64 : cs_db
+ ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
+
+ if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
+ vcpu->emulate_ctxt.cs_base = 0;
+ vcpu->emulate_ctxt.ds_base = 0;
+ vcpu->emulate_ctxt.es_base = 0;
+ vcpu->emulate_ctxt.ss_base = 0;
+ } else {
+ vcpu->emulate_ctxt.cs_base =
+ get_segment_base(vcpu, VCPU_SREG_CS);
+ vcpu->emulate_ctxt.ds_base =
+ get_segment_base(vcpu, VCPU_SREG_DS);
+ vcpu->emulate_ctxt.es_base =
+ get_segment_base(vcpu, VCPU_SREG_ES);
+ vcpu->emulate_ctxt.ss_base =
+ get_segment_base(vcpu, VCPU_SREG_SS);
+ }
+
+ vcpu->emulate_ctxt.gs_base =
+ get_segment_base(vcpu, VCPU_SREG_GS);
+ vcpu->emulate_ctxt.fs_base =
+ get_segment_base(vcpu, VCPU_SREG_FS);
+
+ r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
+ }
+
if (r == 0)
- r = x86_emulate_insn(&emulate_ctxt, &emulate_ops);
+ r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
if (vcpu->pio.string)
return EMULATE_DO_MMIO;
@@ -1313,7 +1324,7 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
}
kvm_x86_ops->decache_regs(vcpu);
- kvm_x86_ops->set_rflags(vcpu, emulate_ctxt.eflags);
+ kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
if (vcpu->mmio_is_write) {
vcpu->mmio_needed = 0;
@@ -2055,7 +2066,7 @@ static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
vcpu->mmio_read_completed = 1;
vcpu->mmio_needed = 0;
r = emulate_instruction(vcpu, kvm_run,
- vcpu->mmio_fault_cr2, 0);
+ vcpu->mmio_fault_cr2, 0, 1);
if (r == EMULATE_DO_MMIO) {
/*
* Read-modify-write. Back to userspace.
diff --git a/drivers/kvm/svm.c b/drivers/kvm/svm.c
index 5883f3e..a0eef78 100644
--- a/drivers/kvm/svm.c
+++ b/drivers/kvm/svm.c
@@ -960,7 +960,7 @@ static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
return 1;
}
er = emulate_instruction(&svm->vcpu, kvm_run, fault_address,
- error_code);
+ error_code, 0);
mutex_unlock(&kvm->lock);
switch (er) {
@@ -984,7 +984,7 @@ static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
{
int er;
- er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0);
+ er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, 0);
if (er != EMULATE_DONE)
inject_ud(&svm->vcpu);
@@ -1027,7 +1027,8 @@ static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
string = (io_info & SVM_IOIO_STR_MASK) != 0;
if (string) {
- if (emulate_instruction(&svm->vcpu, kvm_run, 0, 0) == EMULATE_DO_MMIO)
+ if (emulate_instruction(&svm->vcpu,
+ kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
return 0;
return 1;
}
@@ -1086,7 +1087,7 @@ static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
static int emulate_on_interception(struct vcpu_svm *svm,
struct kvm_run *kvm_run)
{
- if (emulate_instruction(&svm->vcpu, NULL, 0, 0) != EMULATE_DONE)
+ if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE)
pr_unimpl(&svm->vcpu, "%s: failed\n", __FUNCTION__);
return 1;
}
diff --git a/drivers/kvm/vmx.c b/drivers/kvm/vmx.c
index 77d061b..dcc0a84 100644
--- a/drivers/kvm/vmx.c
+++ b/drivers/kvm/vmx.c
@@ -1750,7 +1750,7 @@ static int handle_rmode_exception(struct kvm_vcpu *vcpu,
* Cause the #SS fault with 0 error code in VM86 mode.
*/
if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
- if (emulate_instruction(vcpu, NULL, 0, 0) == EMULATE_DONE)
+ if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
return 1;
return 0;
}
@@ -1787,7 +1787,7 @@ static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
}
if (is_invalid_opcode(intr_info)) {
- er = emulate_instruction(vcpu, kvm_run, 0, 0);
+ er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
if (er != EMULATE_DONE)
vmx_inject_ud(vcpu);
@@ -1812,7 +1812,7 @@ static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
return 1;
}
- er = emulate_instruction(vcpu, kvm_run, cr2, error_code);
+ er = emulate_instruction(vcpu, kvm_run, cr2, error_code, 0);
mutex_unlock(&vcpu->kvm->lock);
switch (er) {
@@ -1873,7 +1873,8 @@ static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
string = (exit_qualification & 16) != 0;
if (string) {
- if (emulate_instruction(vcpu, kvm_run, 0, 0) == EMULATE_DO_MMIO)
+ if (emulate_instruction(vcpu,
+ kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
return 0;
return 1;
}
diff --git a/drivers/kvm/x86_emulate.c b/drivers/kvm/x86_emulate.c
index 9290083..cab1719 100644
--- a/drivers/kvm/x86_emulate.c
+++ b/drivers/kvm/x86_emulate.c
@@ -913,10 +913,19 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
unsigned long cr2 = ctxt->cr2;
int no_wb = 0;
u64 msr_data;
+ unsigned long saved_eip = 0;
unsigned long _eflags = ctxt->eflags;
struct decode_cache *c = &ctxt->decode;
int rc = 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->regs, sizeof c->regs);
+ saved_eip = c->eip;
+
if ((c->d & ModRM) && (c->modrm_mod != 3))
cr2 = c->modrm_ea;
@@ -1250,7 +1259,11 @@ writeback:
ctxt->vcpu->rip = c->eip;
done:
- return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
+ if (rc == X86EMUL_UNHANDLEABLE) {
+ c->eip = saved_eip;
+ return -1;
+ }
+ return 0;
special_insn:
if (c->twobyte)
@@ -1305,8 +1318,10 @@ push:
register_address(ctxt->es_base,
c->regs[VCPU_REGS_RDI]),
c->rep_prefix,
- c->regs[VCPU_REGS_RDX]) == 0)
+ c->regs[VCPU_REGS_RDX]) == 0) {
+ c->eip = saved_eip;
return -1;
+ }
return 0;
case 0x6e: /* outsb */
case 0x6f: /* outsw/outsd */
@@ -1321,8 +1336,10 @@ push:
ctxt->ds_base,
c->regs[VCPU_REGS_RSI]),
c->rep_prefix,
- c->regs[VCPU_REGS_RDX]) == 0)
+ c->regs[VCPU_REGS_RDX]) == 0) {
+ c->eip = saved_eip;
return -1;
+ }
return 0;
case 0x70 ... 0x7f: /* jcc (short) */ {
int rel = insn_fetch(s8, 1, c->eip);
@@ -1711,5 +1728,6 @@ twobyte_special_insn:
cannot_emulate:
DPRINTF("Cannot emulate %02x\n", c->b);
+ c->eip = saved_eip;
return -1;
}
--
1.5.3.7
next prev parent reply other threads:[~2007-12-23 14:53 UTC|newest]
Thread overview: 52+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-12-23 14:50 [PATCH 00/50] KVM patch queue review for 2.6.25 merge window (part I) Avi Kivity
2007-12-23 14:50 ` [PATCH 01/50] KVM: x86 emulator: Add vmmcall/vmcall to x86_emulate (v3) Avi Kivity
2007-12-23 14:50 ` [PATCH 02/50] KVM: Refactor hypercall infrastructure (v3) Avi Kivity
2007-12-23 14:50 ` [PATCH 03/50] KVM: x86 emulator: remove unused functions Avi Kivity
2007-12-23 14:50 ` [PATCH 04/50] KVM: x86 emulator: move all x86_emulate_memop() to a structure Avi Kivity
2007-12-23 14:50 ` [PATCH 05/50] KVM: x86 emulator: move all decoding process to function x86_decode_insn() Avi Kivity
2007-12-23 14:50 ` [PATCH 06/50] KVM: emulate_instruction() calls now x86_decode_insn() and x86_emulate_insn() Avi Kivity
2007-12-23 14:50 ` Avi Kivity [this message]
2007-12-23 14:50 ` [PATCH 08/50] KVM: VMX: Further reduce efer reloads Avi Kivity
2007-12-23 14:50 ` [PATCH 09/50] KVM: Allow not-present guest page faults to bypass kvm Avi Kivity
2007-12-23 14:50 ` [PATCH 10/50] KVM: MMU: Make flooding detection work when guest page faults are bypassed Avi Kivity
2007-12-23 14:50 ` [PATCH 11/50] KVM: MMU: Ignore reserved bits in cr3 in non-pae mode Avi Kivity
2007-12-23 14:50 ` [PATCH 12/50] KVM: x86 emulator: split some decoding into functions for readability Avi Kivity
2007-12-23 14:50 ` [PATCH 13/50] KVM: x86 emulator: remove _eflags and use directly ctxt->eflags Avi Kivity
2007-12-23 14:50 ` [PATCH 14/50] KVM: x86 emulator: Remove no_wb, use dst.type = OP_NONE instead Avi Kivity
2007-12-23 14:51 ` [PATCH 15/50] KVM: x86_emulator: no writeback for bt Avi Kivity
2007-12-23 14:51 ` [PATCH 16/50] KVM: Purify x86_decode_insn() error case management Avi Kivity
2007-12-23 14:51 ` [PATCH 17/50] KVM: x86 emulator: Any legacy prefix after a REX prefix nullifies its effect Avi Kivity
2007-12-23 14:51 ` [PATCH 18/50] KVM: VMX: Don't clear the vmcs if the vcpu is not loaded on any processor Avi Kivity
2007-12-23 14:51 ` [PATCH 19/50] KVM: VMX: Simplify vcpu_clear() Avi Kivity
2007-12-23 14:51 ` [PATCH 20/50] KVM: Remove the usage of page->private field by rmap Avi Kivity
2007-12-23 14:51 ` [PATCH 21/50] KVM: Add general accessors to read and write guest memory Avi Kivity
2007-12-23 14:51 ` [PATCH 22/50] KVM: Allow dynamic allocation of the mmu shadow cache size Avi Kivity
2007-12-23 14:51 ` [PATCH 23/50] KVM: Add kvm_free_lapic() to pair with kvm_create_lapic() Avi Kivity
2007-12-23 14:51 ` [PATCH 24/50] KVM: Hoist kvm_create_lapic() into kvm_vcpu_init() Avi Kivity
2007-12-23 14:51 ` [PATCH 25/50] KVM: Remove gratuitous casts from lapic.c Avi Kivity
2007-12-23 14:51 ` [PATCH 26/50] KVM: CodingStyle cleanup Avi Kivity
2007-12-23 14:51 ` [PATCH 27/50] KVM: Support assigning userspace memory to the guest Avi Kivity
2007-12-23 18:16 ` [kvm-devel] " Avi Kivity
2007-12-23 14:51 ` [PATCH 28/50] KVM: Move x86 msr handling to new files x86.[ch] Avi Kivity
2007-12-23 14:51 ` [PATCH 29/50] KVM: MMU: Clean up MMU functions to take struct kvm when appropriate Avi Kivity
2007-12-23 14:51 ` [PATCH 30/50] KVM: MMU: More struct kvm_vcpu -> struct kvm cleanups Avi Kivity
2007-12-23 14:51 ` [PATCH 31/50] KVM: Move guest pte dirty bit management to the guest pagetable walker Avi Kivity
2007-12-23 14:51 ` [PATCH 32/50] KVM: MMU: Fix nx access bit for huge pages Avi Kivity
2007-12-23 14:51 ` [PATCH 33/50] KVM: MMU: Disable write access on clean large pages Avi Kivity
2007-12-23 14:51 ` [PATCH 34/50] KVM: MMU: Instantiate real-mode shadows as user writable shadows Avi Kivity
2007-12-23 14:51 ` [PATCH 35/50] KVM: MMU: Move dirty bit updates to a separate function Avi Kivity
2007-12-23 14:51 ` [PATCH 36/50] KVM: MMU: When updating the dirty bit, inform the mmu about it Avi Kivity
2007-12-23 14:51 ` [PATCH 37/50] KVM: Portability: split kvm_vcpu_ioctl Avi Kivity
2007-12-23 14:51 ` [PATCH 38/50] KVM: apic round robin cleanup Avi Kivity
2007-12-23 14:51 ` [PATCH 39/50] KVM: Add some \n in ioapic_debug() Avi Kivity
2007-12-23 14:51 ` [PATCH 40/50] KVM: Move apic timer interrupt backlog processing to common code Avi Kivity
2007-12-23 14:51 ` [PATCH 41/50] KVM: Rename KVM_TLB_FLUSH to KVM_REQ_TLB_FLUSH Avi Kivity
2007-12-23 14:51 ` [PATCH 42/50] KVM: x86 emulator: Implement emulation of instruction: inc & dec Avi Kivity
2007-12-23 14:51 ` [PATCH 43/50] KVM: MMU: Simplify page table walker Avi Kivity
2007-12-23 14:51 ` [PATCH 44/50] KVM: x86 emulator: cmc, clc, cli, sti Avi Kivity
2007-12-23 14:51 ` [PATCH 45/50] KVM: MMU: Add rmap_next(), a helper for walking kvm rmaps Avi Kivity
2007-12-23 14:51 ` [PATCH 46/50] KVM: MMU: Keep a reverse mapping of non-writable translations Avi Kivity
2007-12-23 14:51 ` [PATCH 47/50] KVM: MMU: Make gfn_to_page() always safe Avi Kivity
2007-12-23 14:51 ` [PATCH 48/50] KVM: MMU: Partial swapping of guest memory Avi Kivity
2007-12-23 14:51 ` [PATCH 49/50] KVM: Use virtual cpu accounting if available for guest times Avi Kivity
2007-12-23 14:51 ` [PATCH 50/50] KVM: Allocate userspace memory for older userspace Avi Kivity
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1198421495-31481-8-git-send-email-avi@qumranet.com \
--to=avi@qumranet.com \
--cc=Laurent.Vivier@bull.net \
--cc=kvm-devel@lists.sourceforge.net \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®