From patchwork Thu Jun 18 12:47:26 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beth Kon X-Patchwork-Id: 31112 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n5ICkkJ4003326 for ; Thu, 18 Jun 2009 12:46:46 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758466AbZFRMqG (ORCPT ); Thu, 18 Jun 2009 08:46:06 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1758133AbZFRMqF (ORCPT ); Thu, 18 Jun 2009 08:46:05 -0400 Received: from e33.co.us.ibm.com ([32.97.110.151]:46055 "EHLO e33.co.us.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753897AbZFRMqD (ORCPT ); Thu, 18 Jun 2009 08:46:03 -0400 Received: from d03relay02.boulder.ibm.com (d03relay02.boulder.ibm.com [9.17.195.227]) by e33.co.us.ibm.com (8.13.1/8.13.1) with ESMTP id n5ICi2g1031598 for ; Thu, 18 Jun 2009 06:44:02 -0600 Received: from d03av02.boulder.ibm.com (d03av02.boulder.ibm.com [9.17.195.168]) by d03relay02.boulder.ibm.com (8.13.8/8.13.8/NCO v9.2) with ESMTP id n5ICk4mK215536 for ; Thu, 18 Jun 2009 06:46:04 -0600 Received: from d03av02.boulder.ibm.com (loopback [127.0.0.1]) by d03av02.boulder.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id n5ICk302015556 for ; Thu, 18 Jun 2009 06:46:04 -0600 Received: from localhost.localdomain (sig-9-48-52-90.mts.ibm.com [9.48.52.90]) by d03av02.boulder.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id n5ICk1le015403; Thu, 18 Jun 2009 06:46:03 -0600 From: Beth Kon To: avi@redhat.com Cc: kvm@vger.kernel.org, Beth Kon Subject: [PATCH 2/2][RFC] Kernel changes for HPET legacy mode (v7) Date: Thu, 18 Jun 2009 08:47:26 -0400 Message-Id: <1245329246-17526-3-git-send-email-eak@us.ibm.com> X-Mailer: git-send-email 1.5.4.3 In-Reply-To: <1245329246-17526-1-git-send-email-eak@us.ibm.com> References: <1245329246-17526-1-git-send-email-eak@us.ibm.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org When kvm is in hpet_legacy_mode, the hpet is providing the timer interrupt and the pit should not be. So in legacy mode, the pit timer is destroyed, but the *state* of the pit is maintained. So if kvm or the guest tries to modify the state of the pit, this modification is accepted, *except* that the timer isn't actually started. When we exit hpet_legacy_mode, the current state of the pit (which is up to date since we've been accepting modifications) is used to restart the pit timer. The saved_mode code in kvm_pit_load_count temporarily changes mode to 0xff in order to destroy the timer, but then restores the actual value, again maintaining "current" state of the pit for possible later reenablement. changes from v6: - Added ioctl interface for legacy mode in order not to break the abi. Signed-off-by: Beth Kon --- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/arch/x86/include/asm/kvm.h b/arch/x86/include/asm/kvm.h index 708b9c3..25cae50 100644 --- a/arch/x86/include/asm/kvm.h +++ b/arch/x86/include/asm/kvm.h @@ -18,6 +18,7 @@ #define __KVM_HAVE_GUEST_DEBUG #define __KVM_HAVE_MSIX #define __KVM_HAVE_MCE +#define __KVM_HAVE_HPET_LEGACY_MODE /* Architectural interrupt line count. */ #define KVM_NR_INTERRUPTS 256 diff --git a/arch/x86/kvm/i8254.c b/arch/x86/kvm/i8254.c index 331705f..02de293 100644 --- a/arch/x86/kvm/i8254.c +++ b/arch/x86/kvm/i8254.c @@ -329,21 +329,32 @@ static void pit_load_count(struct kvm *kvm, int channel, u32 val) case 1: /* FIXME: enhance mode 4 precision */ case 4: - create_pit_timer(ps, val, 0); + if (!ps->hpet_legacy_mode) + create_pit_timer(ps, val, 0); break; case 2: case 3: - create_pit_timer(ps, val, 1); + if (!ps->hpet_legacy_mode) + create_pit_timer(ps, val, 1); break; default: destroy_pit_timer(&ps->pit_timer); } } -void kvm_pit_load_count(struct kvm *kvm, int channel, u32 val) +void kvm_pit_load_count(struct kvm *kvm, int channel, u32 val, int hpet_legacy_start) { + u8 saved_mode; mutex_lock(&kvm->arch.vpit->pit_state.lock); - pit_load_count(kvm, channel, val); + if (hpet_legacy_start) { + /* save existing mode for later reenablement */ + saved_mode = kvm->arch.vpit->pit_state.channels[0].mode; + kvm->arch.vpit->pit_state.channels[0].mode = 0xff; /* disable timer */ + pit_load_count(kvm, channel, val); + kvm->arch.vpit->pit_state.channels[0].mode = saved_mode; + } else { + pit_load_count(kvm, channel, val); + } mutex_unlock(&kvm->arch.vpit->pit_state.lock); } @@ -548,6 +559,7 @@ void kvm_pit_reset(struct kvm_pit *pit) struct kvm_kpit_channel_state *c; mutex_lock(&pit->pit_state.lock); + pit->pit_state.hpet_legacy_mode = 0; for (i = 0; i < 3; i++) { c = &pit->pit_state.channels[i]; c->mode = 0xff; diff --git a/arch/x86/kvm/i8254.h b/arch/x86/kvm/i8254.h index b267018..b5967ca 100644 --- a/arch/x86/kvm/i8254.h +++ b/arch/x86/kvm/i8254.h @@ -21,6 +21,7 @@ struct kvm_kpit_channel_state { struct kvm_kpit_state { struct kvm_kpit_channel_state channels[3]; + u8 hpet_legacy_mode; struct kvm_timer pit_timer; bool is_periodic; u32 speaker_data_on; @@ -49,7 +50,7 @@ struct kvm_pit { #define KVM_PIT_CHANNEL_MASK 0x3 void kvm_inject_pit_timer_irqs(struct kvm_vcpu *vcpu); -void kvm_pit_load_count(struct kvm *kvm, int channel, u32 val); +void kvm_pit_load_count(struct kvm *kvm, int channel, u32 val, int hpet_legacy_start); struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags); void kvm_free_pit(struct kvm *kvm); void kvm_pit_reset(struct kvm_pit *pit); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 6025e5b..8562eeb 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1134,6 +1134,7 @@ int kvm_dev_ioctl_check_extension(long ext) case KVM_CAP_ASSIGN_DEV_IRQ: case KVM_CAP_IRQFD: case KVM_CAP_PIT2: + case KVM_CAP_HPET_LEGACY_MODE: r = 1; break; case KVM_CAP_COALESCED_MMIO: @@ -1986,7 +1987,24 @@ static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps) int r = 0; memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state)); - kvm_pit_load_count(kvm, 0, ps->channels[0].count); + kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0); + return r; +} + +static int kvm_vm_ioctl_get_hpet_legacy_mode(struct kvm *kvm, u8 *mode) +{ + int r = 0; + *mode = kvm->arch.vpit->pit_state.hpet_legacy_mode; + return r; +} + +static int kvm_vm_ioctl_set_hpet_legacy_mode(struct kvm *kvm, u8 *mode) +{ + int r = 0, start = 0; + if (kvm->arch.vpit->pit_state.hpet_legacy_mode == 0 && *mode == 1) + start = 1; + kvm->arch.vpit->pit_state.hpet_legacy_mode = *mode; + kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start); return r; } @@ -2047,6 +2065,7 @@ long kvm_arch_vm_ioctl(struct file *filp, struct kvm_pit_state ps; struct kvm_memory_alias alias; struct kvm_pit_config pit_config; + u8 hpet_legacy_mode; } u; switch (ioctl) { @@ -2227,6 +2246,32 @@ long kvm_arch_vm_ioctl(struct file *filp, r = 0; break; } + case KVM_GET_HPET_LEGACY_MODE: { + r = -ENXIO; + if (!kvm->arch.vpit) + goto out; + r = kvm_vm_ioctl_get_hpet_legacy_mode(kvm, &u.hpet_legacy_mode); + if (r) + goto out; + r = -EFAULT; + if (copy_to_user(argp, &u.ps, sizeof(u.hpet_legacy_mode))) + goto out; + r = 0; + break; + } + case KVM_SET_HPET_LEGACY_MODE: { + r = -EFAULT; + if (copy_from_user(&u.hpet_legacy_mode, argp, sizeof(u.hpet_legacy_mode))) + goto out; + r = -ENXIO; + if (!kvm->arch.vpit) + goto out; + r = kvm_vm_ioctl_set_hpet_legacy_mode(kvm, &u.hpet_legacy_mode); + if (r) + goto out; + r = 0; + break; + } case KVM_REINJECT_CONTROL: { struct kvm_reinject_control control; r = -EFAULT; diff --git a/include/linux/kvm.h b/include/linux/kvm.h index 38ff31e..202cf6e 100644 --- a/include/linux/kvm.h +++ b/include/linux/kvm.h @@ -438,6 +438,9 @@ struct kvm_trace_rec { #define KVM_CAP_PIT2 33 #endif #define KVM_CAP_SET_BOOT_CPU_ID 34 +#ifdef __KVM_HAVE_HPET_LEGACY_MODE +#define KVM_CAP_HPET_LEGACY_MODE 35 +#endif #ifdef KVM_CAP_IRQ_ROUTING @@ -613,6 +616,9 @@ struct kvm_debug_guest { #define KVM_IA64_VCPU_GET_STACK _IOR(KVMIO, 0x9a, void *) #define KVM_IA64_VCPU_SET_STACK _IOW(KVMIO, 0x9b, void *) +#define KVM_GET_HPET_LEGACY_MODE _IOR(KVMIO, 0x9c, __u8) +#define KVM_SET_HPET_LEGACY_MODE _IOWR(KVMIO, 0x9d, __u8) + #define KVM_TRC_INJ_VIRQ (KVM_TRC_HANDLER + 0x02) #define KVM_TRC_REDELIVER_EVT (KVM_TRC_HANDLER + 0x03) #define KVM_TRC_PEND_INTR (KVM_TRC_HANDLER + 0x04)