public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Avi Kivity <avi-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
To: kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
Cc: akpm-3NddpPZAyC0@public.gmane.org,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: [PATCH 10/14] KVM: less common exit handlers
Date: Sun, 05 Nov 2006 20:38:35 -0000	[thread overview]
Message-ID: <20061105203835.BAF202500A7@cleopatra.q> (raw)
In-Reply-To: <454E4941.7000108-atKUWr5tajBWk0Htik3J/w@public.gmane.org>

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

Signed-off-by: Yaniv Kamay <yaniv-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
Signed-off-by: Avi Kivity <avi-atKUWr5tajBWk0Htik3J/w@public.gmane.org>

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
@@ -2161,6 +2161,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__
 
 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
@@ -2195,6 +2302,78 @@ static void set_efer(struct kvm_vcpu *vc
 
 #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)
 {
@@ -2227,6 +2406,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,
 };

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

  parent reply	other threads:[~2006-11-05 20:38 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-11-05 20:27 [PATCH 0/14] KVM: Kernel-based Virtual Machine (v4) Avi Kivity
2006-11-05 20:30 ` [PATCH 2/14] KVM: Intel virtual mode extensions definitions Avi Kivity
     [not found] ` <454E4941.7000108-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2006-11-05 20:29   ` [PATCH 1/14] KVM: userspace interface Avi Kivity
2006-11-06 10:03     ` Arjan van de Ven
     [not found]       ` <1162807420.3160.186.camel-NIQFrBLA1CpScpXdPBN83iCwEArCW2h5@public.gmane.org>
2006-11-06 10:28         ` Avi Kivity
     [not found]           ` <454F0E4A.7030001-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2006-11-06 10:32             ` Arjan van de Ven
     [not found]               ` <1162809128.3160.201.camel-NIQFrBLA1CpScpXdPBN83iCwEArCW2h5@public.gmane.org>
2006-11-06 10:47                 ` Avi Kivity
2006-11-05 20:31   ` [PATCH 3/14] KVM: kvm data structures Avi Kivity
2006-11-05 20:32   ` [PATCH 4/14] KVM: random accessors and constants Avi Kivity
2006-11-05 20:33   ` [PATCH 5/14] KVM: virtualization infrastructure Avi Kivity
2006-11-05 20:34   ` [PATCH 6/14] KVM: memory slot management Avi Kivity
2006-11-05 20:35   ` [PATCH 7/14] KVM: vcpu creation and maintenance Avi Kivity
2006-11-05 20:36   ` [PATCH 8/14] KVM: vcpu execution loop Avi Kivity
2006-11-05 20:37   ` [PATCH 9/14] KVM: define exit handlers Avi Kivity
2006-11-05 20:38   ` Avi Kivity [this message]
2006-11-05 20:39   ` [PATCH 11/14] KVM: mmu Avi Kivity
2006-11-05 20:40   ` [PATCH 12/14] KVM: x86 emulator Avi Kivity
2006-11-07 12:49     ` Pavel Machek
2006-11-07 12:55       ` Avi Kivity
     [not found]         ` <4550823E.2070108-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2006-11-07 13:00           ` Arjan van de Ven
     [not found]             ` <1162904459.3138.142.camel-NIQFrBLA1CpScpXdPBN83iCwEArCW2h5@public.gmane.org>
2006-11-07 13:22               ` Avi Kivity
     [not found]                 ` <4550889C.2020708-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2006-11-07 13:35                   ` Miguel Ojeda
2006-11-07 13:44                   ` Arjan van de Ven
2006-11-07 13:12         ` Hesse, Christian
     [not found]           ` <200611071412.07196.mail-8oMOrB1mGocUSW6y5lq3GQ@public.gmane.org>
2006-11-08 16:54             ` David Bristow
2006-11-05 20:41   ` [PATCH 13/14] KVM: plumbing Avi Kivity
2006-11-05 20:42   ` [PATCH 14/14] KVM: Dynamically determine which msrs to load and save Avi Kivity
2006-11-07 16:59   ` [PATCH 0/14] KVM: Kernel-based Virtual Machine (v4) Yinghai Lu
     [not found]     ` <86802c440611070859g5bb3c8b0q6b05b4ef2782d682-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2006-11-07 19:56       ` Avi Kivity
2006-11-08  4:44   ` Andrew Morton
     [not found]     ` <20061107204440.090450ea.akpm-3NddpPZAyC0@public.gmane.org>
2006-11-08  4:51       ` Roland Dreier
     [not found]         ` <adafycuh77b.fsf-FYB4Gu1CFyUAvxtiuMwx3w@public.gmane.org>
2006-11-08  7:14           ` Avi Kivity
     [not found]             ` <455183EA.2020405-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2006-11-08  7:33               ` Andrew Morton
     [not found]                 ` <20061107233323.c984fa9b.akpm-3NddpPZAyC0@public.gmane.org>
2006-11-08  8:07                   ` Avi Kivity
     [not found]                     ` <45519033.3060409-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2006-11-08  8:30                       ` Andrew Morton
2006-11-08  9:39                       ` Arjan van de Ven
     [not found]                         ` <1162978754.3138.266.camel-NIQFrBLA1CpScpXdPBN83iCwEArCW2h5@public.gmane.org>
2006-11-08  9:54                           ` Avi Kivity
2006-11-08 10:01                             ` Arjan van de Ven
     [not found]                               ` <1162980101.3138.276.camel-NIQFrBLA1CpScpXdPBN83iCwEArCW2h5@public.gmane.org>
2006-11-08 10:10                                 ` Avi Kivity
2006-11-08 18:26             ` Roland Dreier

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=20061105203835.BAF202500A7@cleopatra.q \
    --to=avi-atkuwr5tajbwk0htik3j/w@public.gmane.org \
    --cc=akpm-3NddpPZAyC0@public.gmane.org \
    --cc=kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org \
    --cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.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