From patchwork Mon Oct 22 06:51:49 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoffer Dall X-Patchwork-Id: 1623831 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id E63D7400E8 for ; Mon, 22 Oct 2012 06:51:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755317Ab2JVGvl (ORCPT ); Mon, 22 Oct 2012 02:51:41 -0400 Received: from mail-vc0-f174.google.com ([209.85.220.174]:53511 "EHLO mail-vc0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755002Ab2JVGvk (ORCPT ); Mon, 22 Oct 2012 02:51:40 -0400 Received: by mail-vc0-f174.google.com with SMTP id fk26so2577234vcb.19 for ; Sun, 21 Oct 2012 23:51:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=subject:to:from:date:message-id:in-reply-to:references:user-agent :mime-version:content-type:content-transfer-encoding :x-gm-message-state; bh=ybT5PrGt+BnXFNNZoEprICflQVU9Oe+l4xFAlqs1cAk=; b=jkhAUQIsF31SpwFdFihqrBp/bWrZc77uhaLw+AhQXYTlTJ4cM8AJqd0CPqGCpvhbXL coA63J2LYvqkRb6oqJiIc7B4NOOUpFieHuiqcSzigrscE/CnHCGG2TRKz61PT1lCvAdH t6H4BBdb0rDciiRDZFBDqq1N/1WE5uUkNYlL982J4/jqvx/5CJo3rr0SJtCbOIEkz6rH Y4ZiruHr9I1rR7BMM0t8QuxEmRuSphu9vatF6p18CREWTehzZ8pJ0SZTxj14zn2Y1fue TV7rYDTAnRXCCh3pmhcjxtkXMy2Ooh1FnXU/YTEoB3ORQlUNeXrzS7KdrcEAHjht9FWI 5wkQ== Received: by 10.58.161.41 with SMTP id xp9mr14050906veb.56.1350888700507; Sun, 21 Oct 2012 23:51:40 -0700 (PDT) Received: from [127.0.1.1] (pool-72-80-83-148.nycmny.fios.verizon.net. [72.80.83.148]) by mx.google.com with ESMTPS id f10sm9500771vdk.5.2012.10.21.23.51.39 (version=TLSv1/SSLv3 cipher=OTHER); Sun, 21 Oct 2012 23:51:39 -0700 (PDT) Subject: [PATCH v3 05/13] ARM: KVM: VGIC accept vcpu and dist base addresses from user space To: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu From: Christoffer Dall Date: Mon, 22 Oct 2012 02:51:49 -0400 Message-ID: <20121022065149.18672.42872.stgit@ubuntu> In-Reply-To: <20121022065104.18672.52989.stgit@ubuntu> References: <20121022065104.18672.52989.stgit@ubuntu> User-Agent: StGit/0.15 MIME-Version: 1.0 X-Gm-Message-State: ALoCoQnxKakQDf5cGQR4ocrlgrWfU8e6Ndz4vMJrWnVk/vdyhzsKq4TXE2t/5sZNkxLuY0jZs5Bl Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org User space defines the model to emulate to a guest and should therefore decide which addresses are used for both the virtual CPU interface directly mapped in the guest physical address space and for the emulated distributor interface, which is mapped in software by the in-kernel VGIC support. Signed-off-by: Christoffer Dall --- arch/arm/include/asm/kvm_mmu.h | 2 + arch/arm/include/asm/kvm_vgic.h | 9 ++++++ arch/arm/kvm/arm.c | 16 ++++++++++ arch/arm/kvm/vgic.c | 61 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 1 deletion(-) -- 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/arm/include/asm/kvm_mmu.h b/arch/arm/include/asm/kvm_mmu.h index 9bd0508..0800531 100644 --- a/arch/arm/include/asm/kvm_mmu.h +++ b/arch/arm/include/asm/kvm_mmu.h @@ -26,6 +26,8 @@ * To save a bit of memory and to avoid alignment issues we assume 39-bit IPA * for now, but remember that the level-1 table must be aligned to its size. */ +#define KVM_PHYS_SHIFT (38) +#define KVM_PHYS_MASK ((1ULL << KVM_PHYS_SHIFT) - 1) #define PTRS_PER_PGD2 512 #define PGD2_ORDER get_order(PTRS_PER_PGD2 * sizeof(pgd_t)) diff --git a/arch/arm/include/asm/kvm_vgic.h b/arch/arm/include/asm/kvm_vgic.h index b444ecf..9ca8d21 100644 --- a/arch/arm/include/asm/kvm_vgic.h +++ b/arch/arm/include/asm/kvm_vgic.h @@ -20,6 +20,9 @@ #define __ASM_ARM_KVM_VGIC_H struct vgic_dist { + /* Distributor and vcpu interface mapping in the guest */ + phys_addr_t vgic_dist_base; + phys_addr_t vgic_cpu_base; }; struct vgic_cpu { @@ -31,6 +34,7 @@ struct kvm_run; struct kvm_exit_mmio; #ifdef CONFIG_KVM_ARM_VGIC +int kvm_vgic_set_addr(struct kvm *kvm, unsigned long type, u64 addr); bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run, struct kvm_exit_mmio *mmio); @@ -40,6 +44,11 @@ static inline int kvm_vgic_hyp_init(void) return 0; } +static inline int kvm_vgic_set_addr(struct kvm *kvm, unsigned long type, u64 addr) +{ + return 0; +} + static inline int kvm_vgic_init(struct kvm *kvm) { return 0; diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c index a57b107..f92b4ec 100644 --- a/arch/arm/kvm/arm.c +++ b/arch/arm/kvm/arm.c @@ -61,6 +61,8 @@ static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1); static u8 kvm_next_vmid; static DEFINE_SPINLOCK(kvm_vmid_lock); +static bool vgic_present; + static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu) { BUG_ON(preemptible()); @@ -824,7 +826,19 @@ int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) static int kvm_vm_ioctl_set_device_address(struct kvm *kvm, struct kvm_device_address *dev_addr) { - return -ENODEV; + unsigned long dev_id, type; + + dev_id = (dev_addr->id & KVM_DEVICE_ID_MASK) >> KVM_DEVICE_ID_SHIFT; + type = (dev_addr->id & KVM_DEVICE_TYPE_MASK) >> KVM_DEVICE_TYPE_SHIFT; + + switch (dev_id) { + case KVM_ARM_DEVICE_VGIC_V2: + if (!vgic_present) + return -ENXIO; + return kvm_vgic_set_addr(kvm, type, dev_addr->addr); + default: + return -ENODEV; + } } long kvm_arch_vm_ioctl(struct file *filp, diff --git a/arch/arm/kvm/vgic.c b/arch/arm/kvm/vgic.c index 26ada3b..f85b275 100644 --- a/arch/arm/kvm/vgic.c +++ b/arch/arm/kvm/vgic.c @@ -22,6 +22,13 @@ #include #include +#define VGIC_ADDR_UNDEF (-1) +#define IS_VGIC_ADDR_UNDEF(_x) ((_x) == (typeof(_x))VGIC_ADDR_UNDEF) + +#define VGIC_DIST_SIZE 0x1000 +#define VGIC_CPU_SIZE 0x2000 + + #define ACCESS_READ_VALUE (1 << 0) #define ACCESS_READ_RAZ (0 << 0) #define ACCESS_READ_MASK(x) ((x) & (1 << 0)) @@ -136,3 +143,57 @@ bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run, struct kvm_exi { return KVM_EXIT_MMIO; } + +static bool vgic_ioaddr_overlap(struct kvm *kvm) +{ + phys_addr_t dist = kvm->arch.vgic.vgic_dist_base; + phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base; + + if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu)) + return false; + if ((dist <= cpu && dist + VGIC_DIST_SIZE > cpu) || + (cpu <= dist && cpu + VGIC_CPU_SIZE > dist)) + return true; + return false; +} + +int kvm_vgic_set_addr(struct kvm *kvm, unsigned long type, u64 addr) +{ + int r = 0; + struct vgic_dist *vgic = &kvm->arch.vgic; + + if (addr & ~KVM_PHYS_MASK) + return -E2BIG; + + if (addr & ~PAGE_MASK) + return -EINVAL; + + mutex_lock(&kvm->lock); + switch (type) { + case KVM_VGIC_V2_ADDR_TYPE_DIST: + if (!IS_VGIC_ADDR_UNDEF(vgic->vgic_dist_base)) + return -EEXIST; + if (addr + VGIC_DIST_SIZE < addr) + return -EINVAL; + kvm->arch.vgic.vgic_dist_base = addr; + break; + case KVM_VGIC_V2_ADDR_TYPE_CPU: + if (!IS_VGIC_ADDR_UNDEF(vgic->vgic_cpu_base)) + return -EEXIST; + if (addr + VGIC_CPU_SIZE < addr) + return -EINVAL; + kvm->arch.vgic.vgic_cpu_base = addr; + break; + default: + r = -ENODEV; + } + + if (vgic_ioaddr_overlap(kvm)) { + kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF; + kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF; + return -EINVAL; + } + + mutex_unlock(&kvm->lock); + return r; +}