Commit 4b85080f authored by Marc Zyngier's avatar Marc Zyngier

KVM: arm64: vgic: Consolidate userspace access for base address setting

Align kvm_vgic_addr() with the rest of the code by moving the
userspace accesses into it. kvm_vgic_addr() is also made static.
Signed-off-by: default avatarMarc Zyngier <maz@kernel.org>
parent 9f968c92
...@@ -76,8 +76,7 @@ int kvm_set_legacy_vgic_v2_addr(struct kvm *kvm, struct kvm_arm_device_addr *dev ...@@ -76,8 +76,7 @@ int kvm_set_legacy_vgic_v2_addr(struct kvm *kvm, struct kvm_arm_device_addr *dev
/** /**
* kvm_vgic_addr - set or get vgic VM base addresses * kvm_vgic_addr - set or get vgic VM base addresses
* @kvm: pointer to the vm struct * @kvm: pointer to the vm struct
* @type: the VGIC addr type, one of KVM_VGIC_V[23]_ADDR_TYPE_XXX * @attr: pointer to the attribute being retrieved/updated
* @addr: pointer to address value
* @write: if true set the address in the VM address space, if false read the * @write: if true set the address in the VM address space, if false read the
* address * address
* *
...@@ -89,15 +88,22 @@ int kvm_set_legacy_vgic_v2_addr(struct kvm *kvm, struct kvm_arm_device_addr *dev ...@@ -89,15 +88,22 @@ int kvm_set_legacy_vgic_v2_addr(struct kvm *kvm, struct kvm_arm_device_addr *dev
* overlapping regions in case of a virtual GICv3 here, since we don't know * overlapping regions in case of a virtual GICv3 here, since we don't know
* the number of VCPUs yet, so we defer this check to map_resources(). * the number of VCPUs yet, so we defer this check to map_resources().
*/ */
int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write) static int kvm_vgic_addr(struct kvm *kvm, struct kvm_device_attr *attr, bool write)
{ {
int r = 0; u64 __user *uaddr = (u64 __user *)attr->addr;
struct vgic_dist *vgic = &kvm->arch.vgic; struct vgic_dist *vgic = &kvm->arch.vgic;
phys_addr_t *addr_ptr, alignment, size; phys_addr_t *addr_ptr, alignment, size;
u64 undef_value = VGIC_ADDR_UNDEF; u64 undef_value = VGIC_ADDR_UNDEF;
u64 addr;
int r;
/* Reading a redistributor region addr implies getting the index */
if (write || attr->attr == KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION)
if (get_user(addr, uaddr))
return -EFAULT;
mutex_lock(&kvm->lock); mutex_lock(&kvm->lock);
switch (type) { switch (attr->attr) {
case KVM_VGIC_V2_ADDR_TYPE_DIST: case KVM_VGIC_V2_ADDR_TYPE_DIST:
r = vgic_check_type(kvm, KVM_DEV_TYPE_ARM_VGIC_V2); r = vgic_check_type(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
addr_ptr = &vgic->vgic_dist_base; addr_ptr = &vgic->vgic_dist_base;
...@@ -123,7 +129,7 @@ int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write) ...@@ -123,7 +129,7 @@ int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
if (r) if (r)
break; break;
if (write) { if (write) {
r = vgic_v3_set_redist_base(kvm, 0, *addr, 0); r = vgic_v3_set_redist_base(kvm, 0, addr, 0);
goto out; goto out;
} }
rdreg = list_first_entry_or_null(&vgic->rd_regions, rdreg = list_first_entry_or_null(&vgic->rd_regions,
...@@ -143,14 +149,12 @@ int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write) ...@@ -143,14 +149,12 @@ int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
if (r) if (r)
break; break;
index = *addr & KVM_VGIC_V3_RDIST_INDEX_MASK; index = addr & KVM_VGIC_V3_RDIST_INDEX_MASK;
if (write) { if (write) {
gpa_t base = *addr & KVM_VGIC_V3_RDIST_BASE_MASK; gpa_t base = addr & KVM_VGIC_V3_RDIST_BASE_MASK;
u32 count = (*addr & KVM_VGIC_V3_RDIST_COUNT_MASK) u32 count = FIELD_GET(KVM_VGIC_V3_RDIST_COUNT_MASK, addr);
>> KVM_VGIC_V3_RDIST_COUNT_SHIFT; u8 flags = FIELD_GET(KVM_VGIC_V3_RDIST_FLAGS_MASK, addr);
u8 flags = (*addr & KVM_VGIC_V3_RDIST_FLAGS_MASK)
>> KVM_VGIC_V3_RDIST_FLAGS_SHIFT;
if (!count || flags) if (!count || flags)
r = -EINVAL; r = -EINVAL;
...@@ -166,9 +170,9 @@ int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write) ...@@ -166,9 +170,9 @@ int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
goto out; goto out;
} }
*addr = index; addr = index;
*addr |= rdreg->base; addr |= rdreg->base;
*addr |= (u64)rdreg->count << KVM_VGIC_V3_RDIST_COUNT_SHIFT; addr |= (u64)rdreg->count << KVM_VGIC_V3_RDIST_COUNT_SHIFT;
goto out; goto out;
} }
default: default:
...@@ -179,15 +183,19 @@ int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write) ...@@ -179,15 +183,19 @@ int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
goto out; goto out;
if (write) { if (write) {
r = vgic_check_iorange(kvm, *addr_ptr, *addr, alignment, size); r = vgic_check_iorange(kvm, *addr_ptr, addr, alignment, size);
if (!r) if (!r)
*addr_ptr = *addr; *addr_ptr = addr;
} else { } else {
*addr = *addr_ptr; addr = *addr_ptr;
} }
out: out:
mutex_unlock(&kvm->lock); mutex_unlock(&kvm->lock);
if (!r && !write)
r = put_user(addr, uaddr);
return r; return r;
} }
...@@ -197,17 +205,9 @@ static int vgic_set_common_attr(struct kvm_device *dev, ...@@ -197,17 +205,9 @@ static int vgic_set_common_attr(struct kvm_device *dev,
int r; int r;
switch (attr->group) { switch (attr->group) {
case KVM_DEV_ARM_VGIC_GRP_ADDR: { case KVM_DEV_ARM_VGIC_GRP_ADDR:
u64 __user *uaddr = (u64 __user *)(long)attr->addr; r = kvm_vgic_addr(dev->kvm, attr, true);
u64 addr;
unsigned long type = (unsigned long)attr->attr;
if (get_user(addr, uaddr))
return -EFAULT;
r = kvm_vgic_addr(dev->kvm, type, &addr, true);
return (r == -ENODEV) ? -ENXIO : r; return (r == -ENODEV) ? -ENXIO : r;
}
case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: { case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
u32 __user *uaddr = (u32 __user *)(long)attr->addr; u32 __user *uaddr = (u32 __user *)(long)attr->addr;
u32 val; u32 val;
...@@ -260,22 +260,9 @@ static int vgic_get_common_attr(struct kvm_device *dev, ...@@ -260,22 +260,9 @@ static int vgic_get_common_attr(struct kvm_device *dev,
int r = -ENXIO; int r = -ENXIO;
switch (attr->group) { switch (attr->group) {
case KVM_DEV_ARM_VGIC_GRP_ADDR: { case KVM_DEV_ARM_VGIC_GRP_ADDR:
u64 __user *uaddr = (u64 __user *)(long)attr->addr; r = kvm_vgic_addr(dev->kvm, attr, false);
u64 addr; return (r == -ENODEV) ? -ENXIO : r;
unsigned long type = (unsigned long)attr->attr;
if (get_user(addr, uaddr))
return -EFAULT;
r = kvm_vgic_addr(dev->kvm, type, &addr, false);
if (r)
return (r == -ENODEV) ? -ENXIO : r;
if (put_user(addr, uaddr))
return -EFAULT;
break;
}
case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: { case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
u32 __user *uaddr = (u32 __user *)(long)attr->addr; u32 __user *uaddr = (u32 __user *)(long)attr->addr;
......
...@@ -364,7 +364,6 @@ struct vgic_cpu { ...@@ -364,7 +364,6 @@ struct vgic_cpu {
extern struct static_key_false vgic_v2_cpuif_trap; extern struct static_key_false vgic_v2_cpuif_trap;
extern struct static_key_false vgic_v3_cpuif_trap; extern struct static_key_false vgic_v3_cpuif_trap;
int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write);
int kvm_set_legacy_vgic_v2_addr(struct kvm *kvm, struct kvm_arm_device_addr *dev_addr); int kvm_set_legacy_vgic_v2_addr(struct kvm *kvm, struct kvm_arm_device_addr *dev_addr);
void kvm_vgic_early_init(struct kvm *kvm); void kvm_vgic_early_init(struct kvm *kvm);
int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu); int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment