mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Avi Kivity <avi@qumranet.com>
To: avi@qumranet.com, linux-kernel@vger.kernel.org
Subject: [PATCH 10/13] KVM: less common exit handlers
Date: Mon, 23 Oct 2006 13:31:16 -0000	[thread overview]
Message-ID: <20061023133116.CB16C250143@cleopatra.q> (raw)
In-Reply-To: <453CC390.9080508@qumranet.com>

Add exit handlers for msrs, debug registers, and cpuid.

Signed-off-by: Yaniv Kamay <yaniv@qumranet.com>
Signed-off-by: Avi Kivity <avi@qumranet.com>

Index: linux-2.6/drivers/kvm/kvm_main.c
===================================================================
--- linux-2.6.orig/drivers/kvm/kvm_main.c
+++ linux-2.6/drivers/kvm/kvm_main.c
@@ -2122,6 +2122,113 @@ static int handle_cr(struct kvm_vcpu *vc
 	return 0;
 }
 
+static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
+{
+	u64 exit_qualification;
+	unsigned long val;
+	int dr, reg;
+
+	/*
+	 * FIXME: this code assumes the host is debugging the guest.
+	 *        need to deal with guest debugging itself too.
+	 */
+	exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
+	dr = exit_qualification & 7;
+	reg = (exit_qualification >> 8) & 15;
+	vcpu_load_rsp_rip(vcpu);
+	if (exit_qualification & 16) {
+		/* mov from dr */
+		switch (dr) {
+		case 6:
+			val = 0xffff0ff0;
+			break;
+		case 7:
+			val = 0x400;
+			break;
+		default:
+			val = 0;
+		}
+		vcpu->regs[reg] = val;
+	} else {
+		/* mov to dr */
+	}
+	vcpu_put_rsp_rip(vcpu);
+	skip_emulated_instruction(vcpu);
+	return 1;
+}
+
+static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
+{
+	kvm_run->exit_reason = KVM_EXIT_CPUID;
+	return 0;
+}
+
+static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
+{
+	u32 ecx = vcpu->regs[VCPU_REGS_RCX];
+	struct vmx_msr_entry *msr = find_msr_entry(vcpu, ecx);
+	u64 data;
+
+#ifdef KVM_DEBUG
+	if (guest_cpl() != 0) {
+		vcpu_printf(vcpu, "%s: not supervisor\n", __FUNCTION__);
+		inject_gp(vcpu);
+		return 1;
+	}
+#endif
+
+	switch (ecx) {
+#ifdef __x86_64__
+	case MSR_FS_BASE:
+		data = vmcs_readl(GUEST_FS_BASE);
+		break;
+	case MSR_GS_BASE:
+		data = vmcs_readl(GUEST_GS_BASE);
+		break;
+#endif
+	case MSR_IA32_SYSENTER_CS:
+		data = vmcs_read32(GUEST_SYSENTER_CS);
+		break;
+	case MSR_IA32_SYSENTER_EIP:
+		data = vmcs_read32(GUEST_SYSENTER_EIP);
+		break;
+	case MSR_IA32_SYSENTER_ESP:
+		data = vmcs_read32(GUEST_SYSENTER_ESP);
+		break;
+	case MSR_IA32_MC0_CTL:
+	case MSR_IA32_MCG_STATUS:
+	case MSR_IA32_MCG_CAP:
+	case MSR_IA32_MC0_MISC:
+	case MSR_IA32_MC0_MISC+4:
+	case MSR_IA32_MC0_MISC+8:
+	case MSR_IA32_MC0_MISC+12:
+	case MSR_IA32_MC0_MISC+16:
+	case MSR_IA32_UCODE_REV:
+		/* MTRR registers */
+	case 0xfe:
+	case 0x200 ... 0x2ff:
+		data = 0;
+		break;
+	case MSR_IA32_APICBASE:
+		data = vcpu->apic_base;
+		break;
+	default:
+		if (msr) {
+			data = msr->data;
+			break;
+		}
+		printk(KERN_ERR "kvm: unhandled rdmsr: %x\n", ecx);
+		inject_gp(vcpu);
+		return 1;
+	}
+
+	/* FIXME: handling of bits 32:63 of rax, rdx */
+	vcpu->regs[VCPU_REGS_RAX] = data & -1u;
+	vcpu->regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
+	skip_emulated_instruction(vcpu);
+	return 1;
+}
+
 #ifdef __x86_64__
 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
 
@@ -2175,6 +2282,78 @@ static void __set_efer(struct kvm_vcpu *
 }
 #endif
 
+#define MSR_IA32_TIME_STAMP_COUNTER 0x10
+
+static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
+{
+	u32 ecx = vcpu->regs[VCPU_REGS_RCX];
+	struct vmx_msr_entry *msr;
+	u64 data = (vcpu->regs[VCPU_REGS_RAX] & -1u)
+		| ((u64)(vcpu->regs[VCPU_REGS_RDX] & -1u) << 32);
+
+#ifdef KVM_DEBUG
+	if (guest_cpl() != 0) {
+		vcpu_printf(vcpu, "%s: not supervisor\n", __FUNCTION__);
+		inject_gp(vcpu);
+		return 1;
+	}
+#endif
+
+	switch (ecx) {
+#ifdef __x86_64__
+	case MSR_FS_BASE:
+		vmcs_writel(GUEST_FS_BASE, data);
+		break;
+	case MSR_GS_BASE:
+		vmcs_writel(GUEST_GS_BASE, data);
+		break;
+#endif
+	case MSR_IA32_SYSENTER_CS:
+		vmcs_write32(GUEST_SYSENTER_CS, data);
+		break;
+	case MSR_IA32_SYSENTER_EIP:
+		vmcs_write32(GUEST_SYSENTER_EIP, data);
+		break;
+	case MSR_IA32_SYSENTER_ESP:
+		vmcs_write32(GUEST_SYSENTER_ESP, data);
+		break;
+#ifdef __x86_64
+	case MSR_EFER:
+		set_efer(vcpu, data);
+		return 1;
+	case MSR_IA32_MC0_STATUS:
+		printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n"
+			    , __FUNCTION__, data);
+		break;
+#endif
+	case MSR_IA32_TIME_STAMP_COUNTER: {
+		u64 tsc;
+
+		rdtscll(tsc);
+		vmcs_write64(TSC_OFFSET, data - tsc);
+		break;
+	}
+	case MSR_IA32_UCODE_REV:
+	case MSR_IA32_UCODE_WRITE:
+	case 0x200 ... 0x2ff: /* MTRRs */
+		break;
+	case MSR_IA32_APICBASE:
+		vcpu->apic_base = data;
+		break;
+	default:
+		msr = find_msr_entry(vcpu, ecx);
+		if (msr) {
+			msr->data = data;
+			break;
+		}
+		printk(KERN_ERR "kvm: unhandled wrmsr: %x\n", ecx);
+		inject_gp(vcpu);
+		return 1;
+	}
+	skip_emulated_instruction(vcpu);
+	return 1;
+}
+
 static int handle_interrupt_window(struct kvm_vcpu *vcpu,
 				   struct kvm_run *kvm_run)
 {
@@ -2207,6 +2386,10 @@ static int (*kvm_vmx_exit_handlers[])(st
 	[EXIT_REASON_IO_INSTRUCTION]          = handle_io,
 	[EXIT_REASON_INVLPG]                  = handle_invlpg,
 	[EXIT_REASON_CR_ACCESS]               = handle_cr,
+	[EXIT_REASON_DR_ACCESS]               = handle_dr,
+	[EXIT_REASON_CPUID]                   = handle_cpuid,
+	[EXIT_REASON_MSR_READ]                = handle_rdmsr,
+	[EXIT_REASON_MSR_WRITE]               = handle_wrmsr,
 	[EXIT_REASON_PENDING_INTERRUPT]       = handle_interrupt_window,
 	[EXIT_REASON_HLT]                     = handle_halt,
 };

  parent reply	other threads:[~2006-10-23 13:33 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-10-23 13:28 [PATCH 0/7] KVM: Kernel-based Virtual Machine (v2) Avi Kivity
2006-10-23 13:29 ` [PATCH 1/13] KVM: userspace interface Avi Kivity
2006-10-24 12:51   ` Muli Ben-Yehuda
2006-10-24 12:56     ` Avi Kivity
2006-10-24 12:59       ` Muli Ben-Yehuda
2006-10-23 13:29 ` [PATCH 2/13] KVM: Intel virtual mode extensions definitions Avi Kivity
2006-10-23 13:30 ` [PATCH 3/13] KVM: kvm data structures Avi Kivity
2006-10-23 13:30 ` [PATCH 4/13] KVM: random accessors and constants Avi Kivity
2006-10-23 13:30 ` [PATCH 5/13] KVM: virtualization infrastructure Avi Kivity
2006-10-23 19:35   ` Arnd Bergmann
2006-10-23 20:28     ` Avi Kivity
2006-10-23 20:35       ` Arnd Bergmann
2006-10-23 20:39         ` Avi Kivity
2006-10-24 12:03         ` Avi Kivity
2006-10-24  5:19           ` Andi Kleen
2006-10-24 13:43             ` [PATCH] x86: Extract segment descriptor definitions for use outside of x86_64 Avi Kivity
2006-10-24 14:10               ` Andi Kleen
2006-10-23 13:30 ` [PATCH 6/13] KVM: memory slot management Avi Kivity
2006-10-23 13:30 ` [PATCH 7/13] KVM: vcpu creation and maintenance Avi Kivity
2006-10-23 13:30 ` [PATCH 8/13] KVM: vcpu execution loop Avi Kivity
     [not found]   ` <200610232141.45802.arnd@arndb.de>
2006-10-23 20:16     ` Avi Kivity
2006-10-23 20:29       ` Arnd Bergmann
2006-10-23 20:37         ` Avi Kivity
2006-10-23 21:02           ` Antonio Vargas
2006-10-23 21:11             ` Avi Kivity
2006-10-23 22:08               ` Antonio Vargas
2006-10-23 22:18           ` Arnd Bergmann
2006-10-23 13:31 ` [PATCH 9/13] KVM: define exit handlers Avi Kivity
2006-10-24  1:05   ` Anthony Liguori
2006-10-24  7:23     ` Avi Kivity
2006-10-23 13:31 ` Avi Kivity [this message]
2006-10-23 13:31 ` [PATCH 11/13] KVM: mmu Avi Kivity
2006-10-23 13:31 ` [PATCH 12/13] KVM: x86 emulator Avi Kivity
2006-10-23 13:31 ` [PATCH 13/13] KVM: plumbing Avi Kivity
2006-10-23 13:44 ` [PATCH 0/7] KVM: Kernel-based Virtual Machine (v2) Avi Kivity
2006-10-23 15:38 ` [PATCH 0/13] KVM: qemu patch Avi Kivity
2006-10-26 17:19 [PATCH 0/13] KVM: Kernel-based Virtual Machine (v3) Avi Kivity
2006-10-26 17:31 ` [PATCH 10/13] KVM: less common exit handlers 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=20061023133116.CB16C250143@cleopatra.q \
    --to=avi@qumranet.com \
    --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®