kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Will Deacon <will.deacon@arm.com>
To: Christoffer Dall <c.dall@virtualopensystems.com>
Cc: "kvm@vger.kernel.org" <kvm@vger.kernel.org>,
	"linux-arm-kernel@lists.infradead.org"
	<linux-arm-kernel@lists.infradead.org>,
	"kvmarm@lists.cs.columbia.edu" <kvmarm@lists.cs.columbia.edu>
Subject: Re: [PATCH v4 05/13] ARM: KVM: VGIC accept vcpu and dist base addresses from user space
Date: Wed, 28 Nov 2012 13:11:39 +0000	[thread overview]
Message-ID: <20121128131139.GG21671@mudshark.cambridge.arm.com> (raw)
In-Reply-To: <20121110154451.3061.74235.stgit@chazy-air>

On Sat, Nov 10, 2012 at 03:44:51PM +0000, Christoffer Dall wrote:
> 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 <c.dall@virtualopensystems.com>
> ---
>  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(-)
> 
> 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)

Seems a bit small...

> +#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 426828a..3ac1aab 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());
> @@ -825,7 +827,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 <linux/io.h>
>  #include <asm/kvm_emulate.h>
>  
> +#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

These defines might be useful to userspace so that they don't request the
distributor and the cpu interface to be place too close together (been there,
done that :).

> +
> +
>  #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;

Just return the predicate that you're testing.

> +}
> +
> +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;

I think somebody else pointed out the missing mutex_unlocks on the failure
paths.

> +		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;

Perhaps we could put all the address checking in one place, so that the wrapping
round zero checks and the overlap checks can be in the same function?

> +	}
> +
> +	mutex_unlock(&kvm->lock);
> +	return r;
> +}

Will

  parent reply	other threads:[~2012-11-28 13:12 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-10 15:44 [PATCH v4 00/13] KVM/ARM vGIC support Christoffer Dall
2012-11-10 15:44 ` [PATCH v4 01/13] KVM: ARM: Introduce KVM_SET_DEVICE_ADDRESS ioctl Christoffer Dall
2012-11-10 15:44 ` [PATCH v4 02/13] ARM: KVM: Keep track of currently running vcpus Christoffer Dall
2012-11-28 12:47   ` Will Deacon
2012-11-28 13:15     ` Marc Zyngier
2012-11-30 22:39     ` Christoffer Dall
2012-11-10 15:44 ` [PATCH v4 03/13] ARM: KVM: Initial VGIC infrastructure support Christoffer Dall
2012-11-28 12:49   ` Will Deacon
2012-11-28 13:09     ` Marc Zyngier
2012-11-28 14:13       ` Will Deacon
2012-12-01  2:19     ` Christoffer Dall
2012-11-10 15:44 ` [PATCH v4 04/13] ARM: KVM: Initial VGIC MMIO support code Christoffer Dall
2012-11-12  8:54   ` Dong Aisheng
2012-11-13 13:32     ` Christoffer Dall
2012-11-28 13:09   ` Will Deacon
2012-11-28 13:44     ` Marc Zyngier
2012-11-10 15:44 ` [PATCH v4 05/13] ARM: KVM: VGIC accept vcpu and dist base addresses from user space Christoffer Dall
2012-11-12  8:56   ` Dong Aisheng
2012-11-13 13:35     ` Christoffer Dall
2012-11-28 13:11   ` Will Deacon [this message]
2012-11-28 13:22     ` [kvmarm] " Marc Zyngier
2012-12-01  2:52     ` Christoffer Dall
2012-12-01 15:57       ` Christoffer Dall
2012-12-03 10:40       ` Will Deacon
2012-11-10 15:44 ` [PATCH v4 06/13] ARM: KVM: VGIC distributor handling Christoffer Dall
2012-11-12  9:29   ` Dong Aisheng
2012-11-13 13:38     ` Christoffer Dall
2012-11-28 13:21   ` Will Deacon
2012-11-28 14:35     ` Marc Zyngier
2012-11-10 15:45 ` [PATCH v4 07/13] ARM: KVM: VGIC virtual CPU interface management Christoffer Dall
2012-12-03 13:23   ` Will Deacon
2012-12-03 14:11     ` Marc Zyngier
2012-12-03 14:34       ` Will Deacon
2012-12-03 15:24         ` Marc Zyngier
2012-12-03 14:54       ` Christoffer Dall
2012-11-10 15:45 ` [PATCH v4 08/13] ARM: KVM: vgic: retire queued, disabled interrupts Christoffer Dall
2012-12-03 13:24   ` Will Deacon
2012-11-10 15:45 ` [PATCH v4 09/13] ARM: KVM: VGIC interrupt injection Christoffer Dall
2012-12-03 13:25   ` Will Deacon
2012-12-03 14:21     ` Marc Zyngier
2012-12-03 14:58       ` Christoffer Dall
2012-12-03 19:13       ` Christoffer Dall
2012-12-03 19:22         ` Marc Zyngier
2012-11-10 15:45 ` [PATCH v4 10/13] ARM: KVM: VGIC control interface world switch Christoffer Dall
2012-12-03 13:31   ` Will Deacon
2012-12-03 14:26     ` Marc Zyngier
2012-11-10 15:45 ` [PATCH v4 11/13] ARM: KVM: VGIC initialisation code Christoffer Dall
2012-12-05 10:43   ` Will Deacon
2012-11-10 15:45 ` [PATCH v4 12/13] ARM: KVM: vgic: reduce the number of vcpu kick Christoffer Dall
2012-12-05 10:43   ` Will Deacon
2012-12-05 10:58     ` Russell King - ARM Linux
2012-12-05 12:17       ` Marc Zyngier
2012-12-05 12:29         ` Russell King - ARM Linux
2012-12-05 13:40           ` Marc Zyngier
2012-12-05 15:55             ` Russell King - ARM Linux
2012-12-05 11:16   ` Russell King - ARM Linux
2012-11-10 15:45 ` [PATCH v4 13/13] ARM: KVM: Add VGIC configuration option Christoffer Dall
2012-11-10 19:52   ` Sergei Shtylyov

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=20121128131139.GG21671@mudshark.cambridge.arm.com \
    --to=will.deacon@arm.com \
    --cc=c.dall@virtualopensystems.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.cs.columbia.edu \
    --cc=linux-arm-kernel@lists.infradead.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;
as well as URLs for NNTP newsgroup(s).