Skip to content

Commit ce01e4e

Browse files
committed
KVM: arm-vgic: Set base addr through device API
Support setting the distributor and cpu interface base addresses in the VM physical address space through the KVM_{SET,GET}_DEVICE_ATTR API in addition to the ARM specific API. This has the added benefit of being able to share more code in user space and do things in a uniform manner. Also deprecate the older API at the same time, but backwards compatibility will be maintained. Acked-by: Marc Zyngier <marc.zyngier@arm.com> Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>
1 parent 7330672 commit ce01e4e

6 files changed

Lines changed: 96 additions & 15 deletions

File tree

‎Documentation/virtual/kvm/api.txt‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2391,7 +2391,8 @@ struct kvm_reg_list {
23912391
This ioctl returns the guest registers that are supported for the
23922392
KVM_GET_ONE_REG/KVM_SET_ONE_REG calls.
23932393

2394-
4.85 KVM_ARM_SET_DEVICE_ADDR
2394+
2395+
4.85 KVM_ARM_SET_DEVICE_ADDR (deprecated)
23952396

23962397
Capability: KVM_CAP_ARM_SET_DEVICE_ADDR
23972398
Architectures: arm, arm64
@@ -2429,6 +2430,10 @@ must be called after calling KVM_CREATE_IRQCHIP, but before calling
24292430
KVM_RUN on any of the VCPUs. Calling this ioctl twice for any of the
24302431
base addresses will return -EEXIST.
24312432

2433+
Note, this IOCTL is deprecated and the more flexible SET/GET_DEVICE_ATTR API
2434+
should be used instead.
2435+
2436+
24322437
4.86 KVM_PPC_RTAS_DEFINE_TOKEN
24332438

24342439
Capability: KVM_CAP_PPC_RTAS

‎Documentation/virtual/kvm/devices/arm-vgic.txt‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,14 @@ Only one VGIC instance may be instantiated through either this API or the
88
legacy KVM_CREATE_IRQCHIP api. The created VGIC will act as the VM interrupt
99
controller, requiring emulated user-space devices to inject interrupts to the
1010
VGIC instead of directly to CPUs.
11+
12+
Groups:
13+
KVM_DEV_ARM_VGIC_GRP_ADDR
14+
Attributes:
15+
KVM_VGIC_V2_ADDR_TYPE_DIST (rw, 64-bit)
16+
Base address in the guest physical address space of the GIC distributor
17+
register mappings.
18+
19+
KVM_VGIC_V2_ADDR_TYPE_CPU (rw, 64-bit)
20+
Base address in the guest physical address space of the GIC virtual cpu
21+
interface register mappings.

‎arch/arm/include/uapi/asm/kvm.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ struct kvm_arch_memory_slot {
163163
#define KVM_REG_ARM_VFP_FPINST 0x1009
164164
#define KVM_REG_ARM_VFP_FPINST2 0x100A
165165

166+
/* Device Control API: ARM VGIC */
167+
#define KVM_DEV_ARM_VGIC_GRP_ADDR 0
166168

167169
/* KVM_IRQ_LINE irq field index values */
168170
#define KVM_ARM_IRQ_TYPE_SHIFT 24

‎arch/arm/kvm/arm.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
776776
case KVM_ARM_DEVICE_VGIC_V2:
777777
if (!vgic_present)
778778
return -ENXIO;
779-
return kvm_vgic_set_addr(kvm, type, dev_addr->addr);
779+
return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
780780
default:
781781
return -ENODEV;
782782
}

‎include/kvm/arm_vgic.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ struct kvm_run;
144144
struct kvm_exit_mmio;
145145

146146
#ifdef CONFIG_KVM_ARM_VGIC
147-
int kvm_vgic_set_addr(struct kvm *kvm, unsigned long type, u64 addr);
147+
int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write);
148148
int kvm_vgic_hyp_init(void);
149149
int kvm_vgic_init(struct kvm *kvm);
150150
int kvm_vgic_create(struct kvm *kvm);

‎virt/kvm/arm/vgic.c‎

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,12 @@ static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
14951495
{
14961496
int ret;
14971497

1498+
if (addr & ~KVM_PHYS_MASK)
1499+
return -E2BIG;
1500+
1501+
if (addr & (SZ_4K - 1))
1502+
return -EINVAL;
1503+
14981504
if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
14991505
return -EEXIST;
15001506
if (addr + size < addr)
@@ -1507,26 +1513,41 @@ static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
15071513
return ret;
15081514
}
15091515

1510-
int kvm_vgic_set_addr(struct kvm *kvm, unsigned long type, u64 addr)
1516+
/**
1517+
* kvm_vgic_addr - set or get vgic VM base addresses
1518+
* @kvm: pointer to the vm struct
1519+
* @type: the VGIC addr type, one of KVM_VGIC_V2_ADDR_TYPE_XXX
1520+
* @addr: pointer to address value
1521+
* @write: if true set the address in the VM address space, if false read the
1522+
* address
1523+
*
1524+
* Set or get the vgic base addresses for the distributor and the virtual CPU
1525+
* interface in the VM physical address space. These addresses are properties
1526+
* of the emulated core/SoC and therefore user space initially knows this
1527+
* information.
1528+
*/
1529+
int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
15111530
{
15121531
int r = 0;
15131532
struct vgic_dist *vgic = &kvm->arch.vgic;
15141533

1515-
if (addr & ~KVM_PHYS_MASK)
1516-
return -E2BIG;
1517-
1518-
if (addr & (SZ_4K - 1))
1519-
return -EINVAL;
1520-
15211534
mutex_lock(&kvm->lock);
15221535
switch (type) {
15231536
case KVM_VGIC_V2_ADDR_TYPE_DIST:
1524-
r = vgic_ioaddr_assign(kvm, &vgic->vgic_dist_base,
1525-
addr, KVM_VGIC_V2_DIST_SIZE);
1537+
if (write) {
1538+
r = vgic_ioaddr_assign(kvm, &vgic->vgic_dist_base,
1539+
*addr, KVM_VGIC_V2_DIST_SIZE);
1540+
} else {
1541+
*addr = vgic->vgic_dist_base;
1542+
}
15261543
break;
15271544
case KVM_VGIC_V2_ADDR_TYPE_CPU:
1528-
r = vgic_ioaddr_assign(kvm, &vgic->vgic_cpu_base,
1529-
addr, KVM_VGIC_V2_CPU_SIZE);
1545+
if (write) {
1546+
r = vgic_ioaddr_assign(kvm, &vgic->vgic_cpu_base,
1547+
*addr, KVM_VGIC_V2_CPU_SIZE);
1548+
} else {
1549+
*addr = vgic->vgic_cpu_base;
1550+
}
15301551
break;
15311552
default:
15321553
r = -ENODEV;
@@ -1538,16 +1559,58 @@ int kvm_vgic_set_addr(struct kvm *kvm, unsigned long type, u64 addr)
15381559

15391560
static int vgic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
15401561
{
1562+
int r;
1563+
1564+
switch (attr->group) {
1565+
case KVM_DEV_ARM_VGIC_GRP_ADDR: {
1566+
u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1567+
u64 addr;
1568+
unsigned long type = (unsigned long)attr->attr;
1569+
1570+
if (copy_from_user(&addr, uaddr, sizeof(addr)))
1571+
return -EFAULT;
1572+
1573+
r = kvm_vgic_addr(dev->kvm, type, &addr, true);
1574+
return (r == -ENODEV) ? -ENXIO : r;
1575+
}
1576+
}
1577+
15411578
return -ENXIO;
15421579
}
15431580

15441581
static int vgic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
15451582
{
1546-
return -ENXIO;
1583+
int r = -ENXIO;
1584+
1585+
switch (attr->group) {
1586+
case KVM_DEV_ARM_VGIC_GRP_ADDR: {
1587+
u64 __user *uaddr = (u64 __user *)(long)attr->addr;
1588+
u64 addr;
1589+
unsigned long type = (unsigned long)attr->attr;
1590+
1591+
r = kvm_vgic_addr(dev->kvm, type, &addr, false);
1592+
if (r)
1593+
return (r == -ENODEV) ? -ENXIO : r;
1594+
1595+
if (copy_to_user(uaddr, &addr, sizeof(addr)))
1596+
return -EFAULT;
1597+
}
1598+
}
1599+
1600+
return r;
15471601
}
15481602

15491603
static int vgic_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
15501604
{
1605+
switch (attr->group) {
1606+
case KVM_DEV_ARM_VGIC_GRP_ADDR:
1607+
switch (attr->attr) {
1608+
case KVM_VGIC_V2_ADDR_TYPE_DIST:
1609+
case KVM_VGIC_V2_ADDR_TYPE_CPU:
1610+
return 0;
1611+
}
1612+
break;
1613+
}
15511614
return -ENXIO;
15521615
}
15531616

0 commit comments

Comments
 (0)