From: Andre Przywara <andre.przywara@amd.com>
To: <avi@redhat.com>
Cc: <kvm@vger.kernel.org>, <mtosatti@redhat.com>,
Andre Przywara <andre.przywara@amd.com>
Subject: [PATCH 2/5] kvm/svm: enhance MOV CR intercept handler
Date: Tue, 7 Dec 2010 11:59:43 +0100 [thread overview]
Message-ID: <1291719586-22533-3-git-send-email-andre.przywara@amd.com> (raw)
In-Reply-To: <1291719586-22533-1-git-send-email-andre.przywara@amd.com>
Newer SVM implementations provide the GPR number in the VMCB, so
that the emulation path is no longer necesarry to handle CR
register access intercepts. Implement the handling in svm.c and
use it when the info is provided.
Signed-off-by: Andre Przywara <andre.przywara@amd.com>
---
arch/x86/include/asm/svm.h | 2 +
arch/x86/kvm/svm.c | 74 +++++++++++++++++++++++++++++++++++++++-----
2 files changed, 68 insertions(+), 8 deletions(-)
diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h
index 11dbca7..589fc25 100644
--- a/arch/x86/include/asm/svm.h
+++ b/arch/x86/include/asm/svm.h
@@ -256,6 +256,8 @@ struct __attribute__ ((__packed__)) vmcb {
#define SVM_EXITINFOSHIFT_TS_REASON_JMP 38
#define SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE 44
+#define SVM_EXITINFO_REG_MASK 0x0F
+
#define SVM_EXIT_READ_CR0 0x000
#define SVM_EXIT_READ_CR3 0x003
#define SVM_EXIT_READ_CR4 0x004
diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c
index c573e2d..b7233fd 100644
--- a/arch/x86/kvm/svm.c
+++ b/arch/x86/kvm/svm.c
@@ -2594,12 +2594,70 @@ static int emulate_on_interception(struct vcpu_svm *svm)
return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
}
+static int cr_interception(struct vcpu_svm *svm)
+{
+ int reg, cr;
+ unsigned long val;
+
+ if (!boot_cpu_has(SVM_FEATURE_DECODE_ASSIST) ||
+ (svm->vmcb->control.exit_info_1 & (1ULL << 63)) == 0)
+ return emulate_on_interception(svm);
+
+ reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
+ cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
+ if (cr > 15) { /* mov to cr */
+ val = kvm_register_read(&svm->vcpu, reg);
+ switch (cr & 0x0F) {
+ case 0:
+ kvm_set_cr0(&svm->vcpu, val);
+ break;
+ case 3:
+ kvm_set_cr3(&svm->vcpu, val);
+ break;
+ case 4:
+ kvm_set_cr4(&svm->vcpu, val);
+ break;
+ case 8:
+ kvm_set_cr8(&svm->vcpu, val & 0xfUL);
+ break;
+ default:
+ WARN(1, "unhandled write to CR%d", cr & 0x0F);
+ return EMULATE_FAIL;
+ }
+ } else { /* mov from cr */
+ switch (cr & 0x0F) {
+ case 0:
+ val = kvm_read_cr0(&svm->vcpu);
+ break;
+ case 2:
+ val = svm->vcpu.arch.cr2;
+ break;
+ case 3:
+ val = svm->vcpu.arch.cr3;
+ break;
+ case 4:
+ val = kvm_read_cr4(&svm->vcpu);
+ break;
+ case 8:
+ val = kvm_get_cr8(&svm->vcpu);
+ break;
+ default:
+ WARN(1, "unhandled read from CR%d", cr & 0x0F);
+ return EMULATE_FAIL;
+ }
+ kvm_register_write(&svm->vcpu, reg, val);
+ }
+ skip_emulated_instruction(&svm->vcpu);
+
+ return 1;
+}
+
static int cr0_write_interception(struct vcpu_svm *svm)
{
struct kvm_vcpu *vcpu = &svm->vcpu;
int r;
- r = emulate_instruction(&svm->vcpu, 0, 0, 0);
+ r = cr_interception(svm);
if (svm->nested.vmexit_rip) {
kvm_register_write(vcpu, VCPU_REGS_RIP, svm->nested.vmexit_rip);
@@ -2617,7 +2675,7 @@ static int cr8_write_interception(struct vcpu_svm *svm)
u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
/* instruction emulation calls kvm_set_cr8() */
- emulate_instruction(&svm->vcpu, 0, 0, 0);
+ cr_interception(svm);
if (irqchip_in_kernel(svm->vcpu.kvm)) {
clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
return 1;
@@ -2864,14 +2922,14 @@ static int pause_interception(struct vcpu_svm *svm)
}
static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
- [SVM_EXIT_READ_CR0] = emulate_on_interception,
- [SVM_EXIT_READ_CR3] = emulate_on_interception,
- [SVM_EXIT_READ_CR4] = emulate_on_interception,
- [SVM_EXIT_READ_CR8] = emulate_on_interception,
+ [SVM_EXIT_READ_CR0] = cr_interception,
+ [SVM_EXIT_READ_CR3] = cr_interception,
+ [SVM_EXIT_READ_CR4] = cr_interception,
+ [SVM_EXIT_READ_CR8] = cr_interception,
[SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
[SVM_EXIT_WRITE_CR0] = cr0_write_interception,
- [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
- [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
+ [SVM_EXIT_WRITE_CR3] = cr_interception,
+ [SVM_EXIT_WRITE_CR4] = cr_interception,
[SVM_EXIT_WRITE_CR8] = cr8_write_interception,
[SVM_EXIT_READ_DR0] = emulate_on_interception,
[SVM_EXIT_READ_DR1] = emulate_on_interception,
--
1.6.4
next prev parent reply other threads:[~2010-12-07 10:54 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-12-07 10:59 [PATCH 0/5] kvm/svm: implement new DecodeAssist features Andre Przywara
2010-12-07 10:59 ` [PATCH 1/5] kvm/svm: add new SVM feature bit names Andre Przywara
2010-12-07 10:59 ` Andre Przywara [this message]
2010-12-07 13:24 ` [PATCH 2/5] kvm/svm: enhance MOV CR intercept handler Avi Kivity
2010-12-07 14:30 ` Andre Przywara
2010-12-07 14:41 ` Avi Kivity
2010-12-07 10:59 ` [PATCH 3/5] kvm/svm: enhance mov DR " Andre Przywara
2010-12-07 11:02 ` Alexander Graf
2010-12-07 13:14 ` Avi Kivity
2010-12-07 13:25 ` Avi Kivity
2010-12-07 10:59 ` [PATCH 4/5] kvm/svm: implement enhanced INVLPG intercept Andre Przywara
2010-12-07 13:27 ` Avi Kivity
2010-12-07 10:59 ` [PATCH 5/5] kvm/svm: copy instruction bytes from VMCB Andre Przywara
2010-12-07 13:33 ` Avi Kivity
-- strict thread matches above, loose matches on Subject: below --
2010-12-10 13:51 [PATCH -v2 0/5] kvm/svm: implement new DecodeAssist features Andre Przywara
2010-12-10 13:51 ` [PATCH 2/5] kvm/svm: enhance MOV CR intercept handler Andre Przywara
2010-12-13 12:13 ` Avi Kivity
2010-12-20 11:56 ` Marcelo Tosatti
2010-12-20 13:20 ` Andre Przywara
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=1291719586-22533-3-git-send-email-andre.przywara@amd.com \
--to=andre.przywara@amd.com \
--cc=avi@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=mtosatti@redhat.com \
/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