KVM: arm64: Implement API for vGICv3 live migration
Subsystems (5)
v2 cover KVM: arm64: Implement API for vGICv3 live migration
This patchset adds necessary userspace API in order to support vGICv3 live migration. GICv3 registers are accessed using device attribute ioctls, similar to GICv2.
v1 => v2:
- Do not use generic register get/set API for CPU interface, use only device attributes.
- Introduce size specifier for distributor and redistributor register accesses, do not assume size any more.
- Lots of refactor and reusable code extraction.
- Added forgotten documentation
Pavel Fedin (5):
KVM: arm/arm64: Refactor vGIC attributes handling code
KVM: arm64: Implement vGICv3 distributor and redistributor access from
userspace
KVM: arm64: Refactor system register handlers
KVM: arm64: Introduce find_reg_by_id()
Implement vGICv3 CPU interface access
Documentation/virtual/kvm/devices/arm-vgic.txt | 72 +++++- arch/arm64/include/uapi/asm/kvm.h | 9 + arch/arm64/kvm/sys_regs.c | 73 +++--- arch/arm64/kvm/sys_regs.h | 8 +- arch/arm64/kvm/sys_regs_generic_v8.c | 2 +- include/linux/irqchip/arm-gic-v3.h | 18 +- virt/kvm/arm/vgic-v2-emul.c | 126 ++------- virt/kvm/arm/vgic-v3-emul.c | 342 ++++++++++++++++++++++++- virt/kvm/arm/vgic.c | 77 ++++++ virt/kvm/arm/vgic.h | 4 + 10 files changed, 573 insertions(+), 158 deletions(-)
Signature · 1 line
2.4.4
PATCH v2 1/5 KVM: arm/arm64: Refactor vGIC attributes handling code
Separate all implementation-independent code in vgic_attr_regs_access() and move it to vgic.c. This will allow to reuse this code for vGICv3 implementation.
--- virt/kvm/arm/vgic-v2-emul.c | 126 +++++++++----------------------------------- virt/kvm/arm/vgic.c | 77 +++++++++++++++++++++++++++ virt/kvm/arm/vgic.h | 4 ++ 3 files changed, 107 insertions(+), 100 deletions(-)
virt/kvm/arm/vgic-v2-emul.c +26 -100
@@ -661,97 +661,38 @@ static const struct vgic_io_range vgic_cpu_ranges[] = { }, }; -static int vgic_attr_regs_access (struct kvm_device *dev, -struct kvm_device_attr *attr, -u32 *reg,bool is_write) +static int vgic_v2_attr_regs_access (struct kvm_device *dev, +struct kvm_device_attr *attr, +__le32 *data,bool is_write) { -const struct vgic_io_range *r =NULL , *ranges; +const struct vgic_io_range *ranges;phys_addr_t offset; -int ret, cpuid, c; -struct kvm_vcpu *vcpu, *tmp_vcpu; -struct vgic_dist *vgic; +int cpuid; +struct vgic_dist *vgic = &dev->kvm->arch.vgic;struct kvm_exit_mmio mmio; -u32 data; offset = attr->attr &KVM_DEV_ARM_VGIC_OFFSET_MASK ; cpuid = (attr->attr &KVM_DEV_ARM_VGIC_CPUID_MASK ) >>KVM_DEV_ARM_VGIC_CPUID_SHIFT ; -mutex_lock (&dev->kvm->lock); - - ret =vgic_init (dev->kvm); -if (ret) - goto out; - -if (cpuid >=atomic_read (&dev->kvm->online_vcpus)) { - ret = -EINVAL ; - goto out; - } - - vcpu =kvm_get_vcpu (dev->kvm, cpuid); - vgic = &dev->kvm->arch.vgic; - - mmio.len =4 ; - mmio.is_write = is_write; - mmio.data = &data; -if (is_write) -mmio_data_write (&mmio, ~0 , *reg);switch (attr->group) {case KVM_DEV_ARM_VGIC_GRP_DIST_REGS : - mmio.phys_addr = vgic->vgic_dist_base + offset; + mmio.phys_addr = vgic->vgic_dist_base; ranges = vgic_dist_ranges;break ;case KVM_DEV_ARM_VGIC_GRP_CPU_REGS : - mmio.phys_addr = vgic->vgic_cpu_base + offset; + mmio.phys_addr = vgic->vgic_cpu_base; ranges = vgic_cpu_ranges;break ;default : -BUG (); +return -ENXIO ; } - r =vgic_find_range (ranges,4 , offset); -if (unlikely (!r || !r->handle_mmio)) { - ret = -ENXIO ; - goto out; - } - - -spin_lock (&vgic->lock); - -/* -* Ensure that no other VCPU is running by checking the vcpu->cpu -* field. If no other VPCUs are running we can safely access the VGIC -* state, because even if another VPU is run after this point, that -* VCPU will not touch the vgic state, because it will block on -* getting the vgic->lock in kvm_vgic_sync_hwstate(). -*/ -kvm_for_each_vcpu (c, tmp_vcpu, dev->kvm) { -if (unlikely (tmp_vcpu->cpu !=-1 )) { - ret = -EBUSY ; - goto out_vgic_unlock; - } - } - -/* -* Move all pending IRQs from the LRs on all VCPUs so the pending -* state can be properly represented in the register state accessible -* through this API. -*/ -kvm_for_each_vcpu (c, tmp_vcpu, dev->kvm) -vgic_unqueue_irqs (tmp_vcpu); - - offset -= r->base; - r->handle_mmio (vcpu, &mmio, offset); - -if (!is_write) - *reg =mmio_data_read (&mmio, ~0 ); + mmio.is_write = is_write; + mmio.data = data; - ret =0 ; -out_vgic_unlock: -spin_unlock (&vgic->lock); -out: -mutex_unlock (&dev->kvm->lock); -return ret; +return vgic_attr_regs_access (dev, ranges, &mmio, offset,sizeof (data), + cpuid); }static int vgic_v2_create (struct kvm_device *dev,u32 type)
@@ -767,53 +708,38 @@ static void vgic_v2_destroy(struct kvm_device *dev)static int vgic_v2_set_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { +u32 __user *uaddr = (u32 __user *)(long )attr->addr; +u32 reg; +__le32 data;int ret; ret =vgic_set_common_attr (dev, attr);if (ret != -ENXIO )return ret; -switch (attr->group) { -case KVM_DEV_ARM_VGIC_GRP_DIST_REGS : -case KVM_DEV_ARM_VGIC_GRP_CPU_REGS : { -u32 __user *uaddr = (u32 __user *)(long )attr->addr; -u32 reg; - -if (get_user (reg, uaddr)) -return -EFAULT ; - -return vgic_attr_regs_access (dev, attr, ®, true); - } - - } +if (get_user (reg, uaddr)) +return -EFAULT ; -return -ENXIO ; + data =cpu_to_le32 (reg); +return vgic_v2_attr_regs_access (dev, attr, &data, true); }static int vgic_v2_get_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { +u32 __user *uaddr = (u32 __user *)(long )attr->addr; +__le32 data =0 ;int ret; ret =vgic_get_common_attr (dev, attr);if (ret != -ENXIO )return ret; -switch (attr->group) { -case KVM_DEV_ARM_VGIC_GRP_DIST_REGS : -case KVM_DEV_ARM_VGIC_GRP_CPU_REGS : { -u32 __user *uaddr = (u32 __user *)(long )attr->addr; -u32 reg =0 ; - - ret =vgic_attr_regs_access (dev, attr, ®, false); -if (ret) -return ret; -return put_user (reg, uaddr); - } - - } + ret =vgic_v2_attr_regs_access (dev, attr, &data, false); +if (ret) +return ret; -return -ENXIO ; +return put_user (le32_to_cpu (data), uaddr); } static int vgic_v2_has_attr(struct kvm_device *dev,
virt/kvm/arm/vgic.c +77 -0
83 lines not on this page · raw message
virt/kvm/arm/vgic.h +4 -0
@@ -132,6 +132,10 @@ void vgic_kick_vcpus(struct kvm *kvm);int vgic_has_attr_regs (const struct vgic_io_range *ranges,phys_addr_t offset);int vgic_set_common_attr (struct kvm_device *dev,struct kvm_device_attr *attr);int vgic_get_common_attr (struct kvm_device *dev,struct kvm_device_attr *attr); +int vgic_attr_regs_access (struct kvm_device *dev, +const struct vgic_io_range *ranges, +struct kvm_exit_mmio *mmio,phys_addr_t offset, +int len,int cpuid);int vgic_init (struct kvm *kvm);void vgic_v2_init_emulation (struct kvm *kvm);
Signature · 1 line
2.4.4
Re: [PATCH v2 1/5] KVM: arm/arm64: Refactor vGIC attributes handling code
Hi Pavel,
Quoted 113 lines · Pavel Fedin
Separate all implementation-independent code in
vgic_attr_regs_access()and move it tovgic.c. This will allow to reuse this code for vGICv3 implementation.Signed-off-by Pavel Fedin <[email protected]>--- virt/kvm/arm/vgic-v2-emul.c | 126 +++++++++----------------------------------- virt/kvm/arm/vgic.c | 77 +++++++++++++++++++++++++++ virt/kvm/arm/vgic.h | 4 ++ 3 files changed, 107 insertions(+), 100 deletions(-)virt/kvm/arm/vgic-v2-emul.c +10
-66@@ -661,97 +661,38 @@ static const struct vgic_io_range vgic_cpu_ranges[] = { }, }; -static int vgic_attr_regs_access (struct kvm_device *dev, -struct kvm_device_attr *attr, -u32 *reg,bool is_write) +static int vgic_v2_attr_regs_access (struct kvm_device *dev, +struct kvm_device_attr *attr, +__le32 *data,bool is_write) { -const struct vgic_io_range *r =NULL , *ranges; +const struct vgic_io_range *ranges;phys_addr_t offset; -int ret, cpuid, c; -struct kvm_vcpu *vcpu, *tmp_vcpu; -struct vgic_dist *vgic; +int cpuid; +struct vgic_dist *vgic = &dev->kvm->arch.vgic;struct kvm_exit_mmio mmio; -u32 data; offset = attr->attr &KVM_DEV_ARM_VGIC_OFFSET_MASK ; cpuid = (attr->attr &KVM_DEV_ARM_VGIC_CPUID_MASK ) >>KVM_DEV_ARM_VGIC_CPUID_SHIFT ; -mutex_lock (&dev->kvm->lock); - - ret =vgic_init (dev->kvm); -if (ret) - goto out; - -if (cpuid >=atomic_read (&dev->kvm->online_vcpus)) { - ret = -EINVAL ; - goto out; - } - - vcpu =kvm_get_vcpu (dev->kvm, cpuid); - vgic = &dev->kvm->arch.vgic; - - mmio.len =4 ; - mmio.is_write = is_write; - mmio.data = &data; -if (is_write) -mmio_data_write (&mmio, ~0 , *reg);switch (attr->group) {case KVM_DEV_ARM_VGIC_GRP_DIST_REGS : - mmio.phys_addr = vgic->vgic_dist_base + offset; + mmio.phys_addr = vgic->vgic_dist_base; ranges = vgic_dist_ranges;break ;case KVM_DEV_ARM_VGIC_GRP_CPU_REGS : - mmio.phys_addr = vgic->vgic_cpu_base + offset; + mmio.phys_addr = vgic->vgic_cpu_base; ranges = vgic_cpu_ranges;break ;default : -BUG (); +return -ENXIO ; } - r =vgic_find_range (ranges,4 , offset); -if (unlikely (!r || !r->handle_mmio)) { - ret = -ENXIO ; - goto out; - } - - -spin_lock (&vgic->lock); - -/* -* Ensure that no other VCPU is running by checking the vcpu->cpu -* field. If no other VPCUs are running we can safely access the VGIC -* state, because even if another VPU is run after this point, that -* VCPU will not touch the vgic state, because it will block on -* getting the vgic->lock in kvm_vgic_sync_hwstate(). -*/ -kvm_for_each_vcpu (c, tmp_vcpu, dev->kvm) { -if (unlikely (tmp_vcpu->cpu !=-1 )) { - ret = -EBUSY ; - goto out_vgic_unlock; - } - } - -/* -* Move all pending IRQs from the LRs on all VCPUs so the pending -* state can be properly represented in the register state accessible -* through this API. -*/ -kvm_for_each_vcpu (c, tmp_vcpu, dev->kvm) -vgic_unqueue_irqs (tmp_vcpu); - - offset -= r->base; - r->handle_mmio (vcpu, &mmio, offset); - -if (!is_write) - *reg =mmio_data_read (&mmio, ~0 ); + mmio.is_write = is_write;
virt/kvm/arm/vgic-v2-emul.c static const struct vgic_io_range vgic_cpu_ranges[] = { · line 695
Quoted 10 lines
virt/kvm/arm/vgic-v2-emul.c +3
-6+ mmio.data = data; - ret =0 ; -out_vgic_unlock: -spin_unlock (&vgic->lock); -out: -mutex_unlock (&dev->kvm->lock); -return ret; +return vgic_attr_regs_access (dev, ranges, &mmio, offset,sizeof (data), + cpuid);
Isn't the len parameter redundant here? I see that you don't initialize mmio.len (which is a bit scary, btw), so can't you just use that field?
virt/kvm/arm/vgic-v2-emul.c static void vgic_v2_destroy(struct kvm_device *dev) · line 713
Quoted 10 lines
virt/kvm/arm/vgic-v2-emul.c +3
-0}static int vgic_v2_create (struct kvm_device *dev,u32 type)@@ -767,53 +708,38 @@ static void vgic_v2_destroy(struct kvm_device *dev)static int vgic_v2_set_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { +u32 __user *uaddr = (u32 __user *)(long )attr->addr; +u32 reg; +__le32 data;
That (and other parts of this patch) sneak in some endianness handling, which I'd like to be mentioned in the commit message, but preferably be in a separate patch. The commit message here talks only about refactoring.
Quoted 14 lines
virt/kvm/arm/vgic-v2-emul.c +0
-8int ret; ret =vgic_set_common_attr (dev, attr);if (ret != -ENXIO )return ret; -switch (attr->group) { -case KVM_DEV_ARM_VGIC_GRP_DIST_REGS : -case KVM_DEV_ARM_VGIC_GRP_CPU_REGS : { -u32 __user *uaddr = (u32 __user *)(long )attr->addr; -u32 reg; - -if (get_user (reg, uaddr)) -return -EFAULT ;
virt/kvm/arm/vgic-v2-emul.c static void vgic_v2_destroy(struct kvm_device *dev) · line 723
Quoted 10 lines
virt/kvm/arm/vgic-v2-emul.c +3
-6- -return vgic_attr_regs_access (dev, attr, ®, true); - } - - } +if (get_user (reg, uaddr)) +return -EFAULT ; -return -ENXIO ; + data =cpu_to_le32 (reg);
Again changes to the code that exceed refactoring. Please move that to a separate patch and explain them.
Quoted 93 lines
virt/kvm/arm/vgic-v2-emul.c +7
-14+return vgic_v2_attr_regs_access (dev, attr, &data, true); }static int vgic_v2_get_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { +u32 __user *uaddr = (u32 __user *)(long )attr->addr; +__le32 data =0 ;int ret; ret =vgic_get_common_attr (dev, attr);if (ret != -ENXIO )return ret; -switch (attr->group) { -case KVM_DEV_ARM_VGIC_GRP_DIST_REGS : -case KVM_DEV_ARM_VGIC_GRP_CPU_REGS : { -u32 __user *uaddr = (u32 __user *)(long )attr->addr; -u32 reg =0 ; - - ret =vgic_attr_regs_access (dev, attr, ®, false); -if (ret) -return ret; -return put_user (reg, uaddr); - } - - } + ret =vgic_v2_attr_regs_access (dev, attr, &data, false); +if (ret) +return ret; -return -ENXIO ; +return put_user (le32_to_cpu (data), uaddr); } static int vgic_v2_has_attr(struct kvm_device *dev,virt/kvm/arm/vgic.c +49
-0@@ -2410,6 +2410,83 @@ int vgic_has_attr_regs(const struct vgic_io_range *ranges, phys_addr_t offset)return -ENXIO ; } +int vgic_attr_regs_access (struct kvm_device *dev, +const struct vgic_io_range *ranges, +struct kvm_exit_mmio *mmio,phys_addr_t offset, +int len,int cpuid) +{ +const struct vgic_io_range *r; +int ret, c; +struct kvm_vcpu *vcpu, *tmp_vcpu; +struct vgic_dist *vgic; + + r =vgic_find_range (ranges, len, offset); + +if (unlikely (!r || !r->handle_mmio)) +return -ENXIO ; + +mutex_lock (&dev->kvm->lock); + + ret =vgic_init (dev->kvm); +if (ret) + goto out; + +if (cpuid >=atomic_read (&dev->kvm->online_vcpus)) { + ret = -EINVAL ; + goto out; + } + + vcpu =kvm_get_vcpu (dev->kvm, cpuid); + vgic = &dev->kvm->arch.vgic; + +spin_lock (&vgic->lock); + +/* +* Ensure that no other VCPU is running by checking the vcpu->cpu +* field. If no other VPCUs are running we can safely access the VGIC +* state, because even if another VPU is run after this point, that +* VCPU will not touch the vgic state, because it will block on +* getting the vgic->lock in kvm_vgic_sync_hwstate(). +*/ +kvm_for_each_vcpu (c, tmp_vcpu, dev->kvm) { +if (unlikely (tmp_vcpu->cpu !=-1 )) { + ret = -EBUSY ; + goto out_vgic_unlock; + } + } + +/* +* Move all pending IRQs from the LRs on all VCPUs so the pending +* state can be properly represented in the register state accessible +* through this API.
virt/kvm/arm/vgic.c int vgic_has_attr_regs(const struct vgic_io_range *ranges, phys_addr_t offset) · line 2471
Quoted 10 lines
virt/kvm/arm/vgic.c +10
-0+*/ +kvm_for_each_vcpu (c, tmp_vcpu, dev->kvm) +vgic_unqueue_irqs (tmp_vcpu); + +/* +* Unfortunately our MMIO handlers can process only up to 32 bits per +* access. For 64-bit registers we have to split up the operation. +*/ + mmio->len =sizeof (u32); + mmio->phys_addr += offset;
I find it a bit awkward to change the mmio struct which has been setup by the caller. For a possible solution, see the next comment.
virt/kvm/arm/vgic.c int vgic_has_attr_regs(const struct vgic_io_range *ranges, phys_addr_t offset) · line 2480
Quoted 9 lines
virt/kvm/arm/vgic.c +9
-0+ offset -= r->base; + r->handle_mmio (vcpu, mmio, offset); + +if (len ==sizeof (u64)) { + mmio->data +=sizeof (u32); + mmio->phys_addr +=sizeof (u32); + offset +=sizeof (u32); + r->handle_mmio (vcpu, mmio, offset); + }
Can't you just call call_range_handler(vcpu, mmio, offset, r) here instead, which does the 64-bit split for you?
Or if there is something that prevents this, refactor this function to be used from both places?
But again, I'd prefer that this patch be pure refactoring, and any changes to the code be in (a) separate patch(es).
64 bit preparations are clearly not just refactoring.
Cheers,
Andre.
Quoted 28 lines
virt/kvm/arm/vgic.c +9
-0+ + ret =0 ; +out_vgic_unlock: +spin_unlock (&vgic->lock); +out: +mutex_unlock (&dev->kvm->lock); +return ret; +} +static void vgic_init_maintenance_interrupt (void *info) {enable_percpu_irq (vgic->maint_irq,0 );virt/kvm/arm/vgic.h +4
-0@@ -132,6 +132,10 @@ void vgic_kick_vcpus(struct kvm *kvm);int vgic_has_attr_regs (const struct vgic_io_range *ranges,phys_addr_t offset);int vgic_set_common_attr (struct kvm_device *dev,struct kvm_device_attr *attr);int vgic_get_common_attr (struct kvm_device *dev,struct kvm_device_attr *attr); +int vgic_attr_regs_access (struct kvm_device *dev, +const struct vgic_io_range *ranges, +struct kvm_exit_mmio *mmio,phys_addr_t offset, +int len,int cpuid);int vgic_init (struct kvm *kvm);void vgic_v2_init_emulation (struct kvm *kvm);
RE: [PATCH v2 1/5] KVM: arm/arm64: Refactor vGIC attributes handling code
Hello!
Quoted 2 lines
Isn't the len parameter redundant here? I see that you don't initialize mmio.len (which is a bit scary, btw), so can't you just use that field?
This was because of split below. I did not know about call_range_handler(), and now i will redo
this.
Quoted 3 lines
That (and other parts of this patch) sneak in some endianness handling, which I'd like to be mentioned in the commit message, but preferably be in a separate patch. The commit message here talks only about refactoring.
These come from mmio_data_read() and mmio_data_write() in original vgic_attr_regs_access().
These inlines cannot be used with arbitrary data length, so i opened them up (they contain endianness conversion plus masking which isn't used in our case) and moved endianness conversion to load/store part.
If i make this a separate patch, it will be two lines patch. Does it worth that? In the next respin
i'd better add this explanation to commit message. Would it be OK?
Kind regards,
Pavel Fedin
Expert Engineer
Samsung Electronics Research center Russia
Re: [PATCH v2 1/5] KVM: arm/arm64: Refactor vGIC attributes handling code
Hi,
Quoted 12 lines · Pavel Fedin
Hello!
Quoted 2 lines
Isn't the len parameter redundant here? I see that you don't initialize mmio.len (which is a bit scary, btw), so can't you just use that field?
This was because of split below. I did not know about
call_range_handler(), and now i will redo
this.Quoted 3 lines
That (and other parts of this patch) sneak in some endianness handling, which I'd like to be mentioned in the commit message, but preferably be in a separate patch. The commit message here talks only about refactoring.
Quoted 8 lines
These come from
mmio_data_read()andmmio_data_write()in originalvgic_attr_regs_access().
These inlines cannot be used with arbitrary data length, so i opened them up (they contain endianness conversion plus masking which isn't used in our case) and moved endianness conversion to load/store part.
If i make this a separate patch, it will be two lines patch. Does it worth that? In the next respin
i'd better add this explanation to commit message. Would it be OK?From a review (and later bisecting) point of view separate patches would
be better. Ideally the refactoring does not introduce any change except code moving around.
Cheers,
Andre.
PATCH v2 2/5 KVM: arm64: Implement vGICv3 distributor and redistributor access from userspace
In reply to Pavel FedinThe access is done similar to vGICv2, using KVM_DEV_ARM_VGIC_GRP_DIST_REGS and KVM_DEV_ARM_VGIC_GRP_REDIST_REGS with KVM_SET_DEVICE_ATTR and KVM_GET_DEVICE_ATTR ioctls.
Some registers are 64-bit wide according to the specification. KVM_DEV_ARM_VGIC_64BIT flag is introduced, allowing to perform full 64-bit accesses.
--- Documentation/virtual/kvm/devices/arm-vgic.txt | 34 ++++++++- arch/arm64/include/uapi/asm/kvm.h | 2 + virt/kvm/arm/vgic-v3-emul.c | 96 ++++++++++++++++++++++---- 3 files changed, 115 insertions(+), 17 deletions(-)
Documentation/virtual/kvm/devices/arm-vgic.txt +31 -3
@@ -43,10 +43,13 @@ Groups: KVM_DEV_ARM_VGIC_GRP_DIST_REGS Attributes: The attr field of kvm_device_attr encodes two values: - bits: | 63 .... 40 | 39 .. 32 | 31 .... 0 | - values: | reserved | cpu id | offset | + bits: | 63 | 62 .. 40 | 39 .. 32 | 31 .... 0 | + values: | size | reserved | cpu id | offset | All distributor regs are (rw, 32-bit) + For GICv3 some regs are also (rw, 64-bit) according to the specification. + In order to perform full 64-bit access 'size' bit should be set to 1. + KVM_DEV_ARM_VGIC_64BIT flag value is provided for this purpose. The offset is relative to the "Distributor base address" as defined in the GICv2 specs. Getting or setting such a register has the same effect as
@@ -54,9 +57,34 @@ Groups: specified with cpu id field. Note that most distributor fields are not banked, but return the same value regardless of the cpu id used to access the register. + + Limitations: + - Priorities are not implemented, and registers are RAZ/WI + Errors: + -ENODEV: Getting or setting this register is not yet supported + -EBUSY: One or more VCPUs are running + + KVM_DEV_ARM_VGIC_GRP_REDIST_REGS + Attributes: + The attr field of kvm_device_attr encodes two values: + bits: | 63 | 62 .. 40 | 39 .. 32 | 31 .... 0 | + values: | size | reserved | cpu id | offset | + + All redistributor regs are (rw, 32-bit) + For GICv3 some regs are also (rw, 64-bit) according to the specification. + In order to perform full 64-bit access 'size' bit should be set to 1. + KVM_DEV_ARM_VGIC_64BIT flag value is provided for this purpose. + + + The offset is relative to the "Redistributor base address" as defined in + the GICv3 specs. Getting or setting such a register has the same effect as + reading or writing the register on the actual hardware from the cpu + specified with cpu id field. Note that most distributor fields are not + banked, but return the same value regardless of the cpu id used to access + the register. + Limitations: - Priorities are not implemented, and registers are RAZ/WI - - Currently only implemented for KVM_DEV_TYPE_ARM_VGIC_V2. Errors: -ENODEV: Getting or setting this register is not yet supported -EBUSY: One or more VCPUs are running
arch/arm64/include/uapi/asm/kvm.h +2 -0
@@ -196,6 +196,7 @@ struct kvm_arch_memory_slot {#define KVM_DEV_ARM_VGIC_GRP_ADDR 0#define KVM_DEV_ARM_VGIC_GRP_DIST_REGS 1#define KVM_DEV_ARM_VGIC_GRP_CPU_REGS 2 +#define KVM_DEV_ARM_VGIC_64BIT (1ULL << 63)#define KVM_DEV_ARM_VGIC_CPUID_SHIFT 32#define KVM_DEV_ARM_VGIC_CPUID_MASK (0xffULL << KVM_DEV_ARM_VGIC_CPUID_SHIFT)#define KVM_DEV_ARM_VGIC_OFFSET_SHIFT 0
@@ -203,6 +204,7 @@ struct kvm_arch_memory_slot {#define KVM_DEV_ARM_VGIC_GRP_NR_IRQS 3#define KVM_DEV_ARM_VGIC_GRP_CTRL 4#define KVM_DEV_ARM_VGIC_CTRL_INIT 0 +#define KVM_DEV_ARM_VGIC_GRP_REDIST_REGS 5/* KVM_IRQ_LINE irq field index values */ #define KVM_ARM_IRQ_TYPE_SHIFT 24
virt/kvm/arm/vgic-v3-emul.c +82 -14
131 lines not on this page · raw message
Signature · 1 line
2.4.4
Re: [PATCH v2 2/5] KVM: arm64: Implement vGICv3 distributor and redistributor access from userspace
Quoted 10 lines · Pavel Fedin
The access is done similar to vGICv2, using
KVM_DEV_ARM_VGIC_GRP_DIST_REGSandKVM_DEV_ARM_VGIC_GRP_REDIST_REGSwithKVM_SET_DEVICE_ATTRandKVM_GET_DEVICE_ATTRioctls.Some registers are 64-bit wide according to the specification.
KVM_DEV_ARM_VGIC_64BITflag is introduced, allowing to perform full 64-bit accesses.Signed-off-by Pavel Fedin <[email protected]>---
Documentation/virtual/kvm/devices/arm-vgic.txt Groups: · line 71
Quoted 5 lines
Documentation/virtual/kvm/devices/arm-vgic.txt +5
-0+ KVM_DEV_ARM_VGIC_GRP_REDIST_REGS + Attributes: + The attr field of kvm_device_attr encodes two values: + bits: | 63 | 62 .. 40 | 39 .. 32 | 31 .... 0 | + values: | size | reserved | cpu id | offset |
We should avoid imposing an accidental limit on the maximum number of CPUs in the userspace API. GICv3 doesn't have a limit at 256 CPUs, so I think we should define an attr format which lets us specify a complete affinity specification.
thanks
-- PMM
RE: [PATCH v2 2/5] KVM: arm64: Implement vGICv3 distributor and redistributor access from userspace
Hello!
Quoted 9 lines
Documentation/virtual/kvm/devices/arm-vgic.txt Groups: · line 71
Quoted 5 lines
Documentation/virtual/kvm/devices/arm-vgic.txt +5
-0+ KVM_DEV_ARM_VGIC_GRP_REDIST_REGS + Attributes: + The attr field of kvm_device_attr encodes two values: + bits: | 63 | 62 .. 40 | 39 .. 32 | 31 .... 0 | + values: | size | reserved | cpu id | offset |We should avoid imposing an accidental limit on the maximum number of CPUs in the userspace API. GICv3 doesn't have a limit at 256 CPUs
Ops, my fault, forgot. :(
However, it seems to be very simple. "cpu id" is actually an index, not a real affinity ID (see http://lxr.free-electrons.com/source/include/linux/kvm_host.h#L427). Would it be OK just to enlarge KVM_DEV_ARM_VGIC_CPUID_MASK?
bits: | 63 | 62 .. 32 | 31 .... 0 |
values: | size | cpu id | offset |I think 31 bits is more than enough for CPU index. And, since id is actually an index, may be we should fix up docs?
Kind regards,
Pavel Fedin
Expert Engineer
Samsung Electronics Research center Russia
PATCH v2 3/5 KVM: arm64: Refactor system register handlers
In reply to Pavel FedinReplace Rt with data pointer in struct sys_reg_params. This will allow to reuse system register handling code in implementation of vGICv3 CPU interface access API. Additionally, got rid of "massive hack" in kvm_handle_cp_64().
--- arch/arm64/kvm/sys_regs.c | 51 +++++++++++++++++------------------- arch/arm64/kvm/sys_regs.h | 4 +-- arch/arm64/kvm/sys_regs_generic_v8.c | 2 +- 3 files changed, 27 insertions(+), 30 deletions(-)
arch/arm64/kvm/sys_regs.c +24 -27
@@ -102,7 +102,7 @@ static bool access_vm_reg(struct kvm_vcpu *vcpu,BUG_ON (!p->is_write); - val = *vcpu_reg (vcpu, p->Rt); + val = *p->val;if (!p->is_aarch32) {vcpu_sys_reg (vcpu, r->reg) = val; }else {
@@ -125,13 +125,10 @@ static bool access_gic_sgi(struct kvm_vcpu *vcpu,const struct sys_reg_params *p, const struct sys_reg_desc *r) { - u64 val; - if (!p->is_write) return read_from_write_only(vcpu, p); - val = *vcpu_reg (vcpu, p->Rt); -vgic_v3_dispatch_sgi (vcpu, val); + vgic_v3_dispatch_sgi(vcpu, *p->val);return true; }
@@ -153,7 +150,7 @@ static bool trap_oslsr_el1(struct kvm_vcpu *vcpu,if (p->is_write) {return ignore_write (vcpu, p); }else { - *vcpu_reg (vcpu, p->Rt) = (1 <<3 ); + *p->val = (1 <<3 );return true; } }
@@ -167,7 +164,7 @@ static bool trap_dbgauthstatus_el1(struct kvm_vcpu *vcpu, }else {u32 val; asmvolatile ("mrs %0, dbgauthstatus_el1" :"=r" (val)); - *vcpu_reg (vcpu, p->Rt) = val; + *p->val = val;return true; } }
@@ -204,10 +201,10 @@ static bool trap_debug_regs(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) {if (p->is_write) { -vcpu_sys_reg (vcpu, r->reg) = *vcpu_reg (vcpu, p->Rt); +vcpu_sys_reg (vcpu, r->reg) = *p->val; vcpu->arch.debug_flags |=KVM_ARM64_DEBUG_DIRTY ; }else { - *vcpu_reg (vcpu, p->Rt) =vcpu_sys_reg (vcpu, r->reg); + *p->val =vcpu_sys_reg (vcpu, r->reg); }trace_trap_reg (__func__, r->reg, p->is_write, *vcpu_reg (vcpu, p->Rt));
@@ -704,10 +701,10 @@ static bool trap_dbgidr(struct kvm_vcpu *vcpu,u64 pfr =read_cpuid (ID_AA64PFR0_EL1 );u32 el3 = !!((pfr >>12 ) &0xf ); - *vcpu_reg (vcpu, p->Rt) = ((((dfr >>20 ) &0xf ) <<28 ) | - (((dfr >>12 ) &0xf ) <<24 ) | - (((dfr >>28 ) &0xf ) <<20 ) | - (6 <<16 ) | (el3 <<14 ) | (el3 <<12 )); + *p->val = ((((dfr >>20 ) &0xf ) <<28 ) | + (((dfr >>12 ) &0xf ) <<24 ) | + (((dfr >>28 ) &0xf ) <<20 ) | + (6 <<16 ) | (el3 <<14 ) | (el3 <<12 ));return true; } }
@@ -717,10 +714,10 @@ static bool trap_debug32(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) {if (p->is_write) { -vcpu_cp14 (vcpu, r->reg) = *vcpu_reg (vcpu, p->Rt); +vcpu_cp14 (vcpu, r->reg) = *p->val; vcpu->arch.debug_flags |=KVM_ARM64_DEBUG_DIRTY ; }else { - *vcpu_reg (vcpu, p->Rt) =vcpu_cp14 (vcpu, r->reg); + *p->val =vcpu_cp14 (vcpu, r->reg); }return true;
@@ -1069,12 +1066,14 @@ static int kvm_handle_cp_64(struct kvm_vcpu *vcpu, {struct sys_reg_params params;u32 hsr =kvm_vcpu_get_hsr (vcpu); +int Rt = (hsr >>5 ) &0xf ;int Rt2 = (hsr >>10 ) &0xf ; +unsigned long val; params.is_aarch32 = true; params.is_32bit = false; params.CRm = (hsr >>1 ) &0xf ; - params.Rt = (hsr >>5 ) &0xf ; + params.val = &val; params.is_write = ((hsr &1 ) ==0 ); params.Op0 =0 ;
@@ -1083,15 +1082,12 @@ static int kvm_handle_cp_64(struct kvm_vcpu *vcpu, params.CRn =0 ;/* -* Massive hack here. Store Rt2 in the top 32bits so we only -* have one register to deal with. As we use the same trap +* Make a 64-bit value out of Rt and Rt2. As we use the same trap * backends between AArch32 and AArch64, we get away with it. */ if (params.is_write) { -u64 val = *vcpu_reg (vcpu, params.Rt); - val &=0xffffffff ; + val = *vcpu_reg (vcpu, Rt) &0xffffffff ; val |= *vcpu_reg (vcpu, Rt2) <<32 ; - *vcpu_reg (vcpu, params.Rt) = val; }if (!emulate_cp (vcpu, ¶ms, target_specific, nr_specific))
@@ -1102,11 +1098,10 @@ static int kvm_handle_cp_64(struct kvm_vcpu *vcpu,unhandled_cp_access (vcpu, ¶ms); out: -/* Do the opposite hack for the read side */ +/* Split up the value between registers for the read side */ if (!params.is_write) { -u64 val = *vcpu_reg (vcpu, params.Rt); - val >>=32 ; - *vcpu_reg (vcpu, Rt2) = val; + *vcpu_reg (vcpu, Rt) = val &0xffffffff ; + *vcpu_reg (vcpu, Rt2) = val >>32 ; }return 1 ;
@@ -1125,11 +1120,12 @@ static int kvm_handle_cp_32(struct kvm_vcpu *vcpu, {struct sys_reg_params params;u32 hsr =kvm_vcpu_get_hsr (vcpu); +int Rt = (hsr >>5 ) &0xf ; params.is_aarch32 = true; params.is_32bit = true; params.CRm = (hsr >>1 ) &0xf ; - params.Rt = (hsr >>5 ) &0xf ; + params.val =vcpu_reg (vcpu, Rt); params.is_write = ((hsr &1 ) ==0 ); params.CRn = (hsr >>10 ) &0xf ; params.Op0 =0 ;
@@ -1237,6 +1233,7 @@ int kvm_handle_sys_reg(struct kvm_vcpu *vcpu, struct kvm_run *run) {struct sys_reg_params params;unsigned long esr =kvm_vcpu_get_hsr (vcpu); +int Rt = (esr >>5 ) &0x1f ;trace_kvm_handle_sys_reg (esr);
@@ -1247,7 +1244,7 @@ int kvm_handle_sys_reg(struct kvm_vcpu *vcpu, struct kvm_run *run) params.CRn = (esr >>10 ) &0xf ; params.CRm = (esr >>1 ) &0xf ; params.Op2 = (esr >>17 ) &0x7 ; - params.Rt = (esr >>5 ) &0x1f ; + params.val =vcpu_reg (vcpu, Rt); params.is_write = !(esr &1 );return emulate_sys_reg (vcpu, ¶ms);
arch/arm64/kvm/sys_regs.h +2 -2
@@ -28,7 +28,7 @@ struct sys_reg_params {u8 CRn;u8 CRm;u8 Op2; -u8 Rt; +u_long *val;bool is_write;bool is_aarch32;bool is_32bit;/* Only valid if is_aarch32 is true */
@@ -79,7 +79,7 @@ static inline bool ignore_write(struct kvm_vcpu *vcpu,static inline bool read_zero (struct kvm_vcpu *vcpu,const struct sys_reg_params *p) { - *vcpu_reg (vcpu, p->Rt) =0 ; + *p->val =0 ;return true; }
arch/arm64/kvm/sys_regs_generic_v8.c +1 -1
@@ -37,7 +37,7 @@ static bool access_actlr(struct kvm_vcpu *vcpu,if (p->is_write)return ignore_write (vcpu, p); - *vcpu_reg (vcpu, p->Rt) =vcpu_sys_reg (vcpu,ACTLR_EL1 ); + *p->val =vcpu_sys_reg (vcpu,ACTLR_EL1 );return true; }
Signature · 1 line
2.4.4
PATCH v2 4/5 KVM: arm64: Introduce find_reg_by_id()
In reply to Pavel FedinIn order to implement vGICv3 CPU interface access, we will need to perform table lookup of system registers. We would need both index_to_params() and find_reg() exported for that purpose, but instead we export a single function which combines them both.
--- arch/arm64/kvm/sys_regs.c | 22 +++++++++++++++------- arch/arm64/kvm/sys_regs.h | 4 ++++ 2 files changed, 19 insertions(+), 7 deletions(-)
arch/arm64/kvm/sys_regs.c +15 -7
@@ -1283,6 +1283,17 @@ static bool index_to_params(u64 id, struct sys_reg_params *params) } } +const struct sys_reg_desc *find_reg_by_id (u64 id, +struct sys_reg_params *params, +const struct sys_reg_desc table[], +unsigned int num) +{ +if (!index_to_params (id, params)) +return NULL ; + +return find_reg (params, table, num); +} +/* Decode an index value, and find the sys_reg_desc entry. */ static const struct sys_reg_desc *index_to_sys_reg_desc (struct kvm_vcpu *vcpu,u64 id)
@@ -1410,10 +1421,8 @@ static int get_invariant_sys_reg(u64 id, void __user *uaddr)struct sys_reg_params params;const struct sys_reg_desc *r; -if (!index_to_params (id, ¶ms)) -return -ENOENT ; - - r =find_reg (¶ms, invariant_sys_regs,ARRAY_SIZE (invariant_sys_regs)); + r =find_reg_by_id (id, ¶ms, invariant_sys_regs, +ARRAY_SIZE (invariant_sys_regs));if (!r)return -ENOENT ;
@@ -1427,9 +1436,8 @@ static int set_invariant_sys_reg(u64 id, void __user *uaddr)int err;u64 val =0 ;/* Make sure high bits are 0 for 32-bit regs */ -if (!index_to_params (id, ¶ms)) -return -ENOENT ; - r =find_reg (¶ms, invariant_sys_regs,ARRAY_SIZE (invariant_sys_regs)); + r =find_reg_by_id (id, ¶ms, invariant_sys_regs, +ARRAY_SIZE (invariant_sys_regs));if (!r)return -ENOENT ;
arch/arm64/kvm/sys_regs.h +4 -0
@@ -136,6 +136,10 @@ static inline int cmp_sys_reg(const struct sys_reg_desc *i1,return i1->Op2 - i2->Op2; } +const struct sys_reg_desc *find_reg_by_id (u64 id, +struct sys_reg_params *params, +const struct sys_reg_desc table[], +unsigned int num);#define Op0 (_x) .Op0 = _x#define Op1 (_x) .Op1 = _x
Signature · 1 line
2.4.4
PATCH v2 5/5 Implement vGICv3 CPU interface access
In reply to Pavel FedinThe access is done similar to GICv2, using KVM_DEV_ARM_VGIC_GRP_CPU_REGS group, however attribute ID encodes corresponding system register. Access size is always 64 bits.
Since CPU interface state actually affects only a single vCPU, no vGIC locking is done. Just made sure that the vCPU is not running.
--- Documentation/virtual/kvm/devices/arm-vgic.txt | 38 +++- arch/arm64/include/uapi/asm/kvm.h | 7 + include/linux/irqchip/arm-gic-v3.h | 18 +- virt/kvm/arm/vgic-v3-emul.c | 246 +++++++++++++++++++++++++ 4 files changed, 305 insertions(+), 4 deletions(-)
Documentation/virtual/kvm/devices/arm-vgic.txt +36 -2
@@ -89,7 +89,7 @@ Groups: -ENODEV: Getting or setting this register is not yet supported -EBUSY: One or more VCPUs are running - KVM_DEV_ARM_VGIC_GRP_CPU_REGS + KVM_DEV_ARM_VGIC_GRP_CPU_REGS (vGICv2) Attributes: The attr field of kvm_device_attr encodes two values: bits: | 63 .... 40 | 39 .. 32 | 31 .... 0 |
@@ -117,11 +117,45 @@ Groups: Limitations: - Priorities are not implemented, and registers are RAZ/WI - - Currently only implemented for KVM_DEV_TYPE_ARM_VGIC_V2. Errors: -ENODEV: Getting or setting this register is not yet supported -EBUSY: One or more VCPUs are running + KVM_DEV_ARM_VGIC_GRP_CPU_REGS (vGICv3) + Attributes: + The attr field of kvm_device_attr encodes the following values: + bits: | 63 .. 56 | 55 .. 48 | 47 ... 40 | 39 .. 32 | 31 .. 0 | + values: | arch | size | reserved | cpu id | reg id | + + All CPU interface regs are (rw, 64-bit). The only supported size value is + KVM_REG_SIZE_U64. + + Arch, size and reg id fields actually encode system register to be + accessed. Normally these values are obtained using ARM64_SYS_REG() macro. + Getting or setting such a register has the same effect as reading or + writing the register on the actual hardware. + + The Active Priorities Registers AP0Rn and AP1Rn are implementation defined, + so we set a fixed format for our implementation that fits with the model of + a "GICv3 implementation without the security extensions" which we present + to the guest. This interface always exposes four register APR[0-3] + describing the maximum possible 128 preemption levels. The semantics of the + register indicates if any interrupts in a given preemption level are in the + active state by setting the corresponding bit. + + Thus, preemption level X has one or more active interrupts if and only if: + + APRn[X mod 32] == 0b1, where n = X / 32 + + Bits for undefined preemption levels are RAZ/WI. + + Limitations: + - Priorities are not implemented, and registers are RAZ/WI + Errors: + -ENODEV: Getting or setting this register is not yet supported + -EBUSY: One or more VCPUs are running + + KVM_DEV_ARM_VGIC_GRP_NR_IRQS Attributes: A value describing the number of interrupts (SGI, PPI and SPI) for
arch/arm64/include/uapi/asm/kvm.h +7 -0
@@ -201,6 +201,13 @@ struct kvm_arch_memory_slot {#define KVM_DEV_ARM_VGIC_CPUID_MASK (0xffULL << KVM_DEV_ARM_VGIC_CPUID_SHIFT)#define KVM_DEV_ARM_VGIC_OFFSET_SHIFT 0#define KVM_DEV_ARM_VGIC_OFFSET_MASK (0xffffffffULL << KVM_DEV_ARM_VGIC_OFFSET_SHIFT) +#define KVM_DEV_ARM_VGIC_REG_MASK (KVM_REG_SIZE_MASK | \ + KVM_REG_ARM64_SYSREG_OP0_MASK | \ + KVM_REG_ARM64_SYSREG_OP1_MASK | \ + KVM_REG_ARM64_SYSREG_CRN_MASK | \ + KVM_REG_ARM64_SYSREG_CRM_MASK | \ + KVM_REG_ARM64_SYSREG_OP2_MASK) +#define KVM_DEV_ARM_VGIC_GRP_NR_IRQS 3#define KVM_DEV_ARM_VGIC_GRP_CTRL 4#define KVM_DEV_ARM_VGIC_CTRL_INIT 0
include/linux/irqchip/arm-gic-v3.h +16 -2
@@ -259,8 +259,14 @@/* * CPU interface registers */ -#define ICC_CTLR_EL1_EOImode_drop_dir (0U << 1) -#define ICC_CTLR_EL1_EOImode_drop (1U << 1) +#define ICC_CTLR_EL1_CBPR_SHIFT 0 +#define ICC_CTLR_EL1_EOImode_SHIFT 1 +#define ICC_CTLR_EL1_EOImode_drop_dir (0U << ICC_CTLR_EL1_EOImode_SHIFT) +#define ICC_CTLR_EL1_EOImode_drop (1U << ICC_CTLR_EL1_EOImode_SHIFT) +#define ICC_CTLR_EL1_PRIbits_MASK (7U << 8) +#define ICC_CTLR_EL1_IDbits_MASK (7U << 11) +#define ICC_CTLR_EL1_SEIS (1U << 14) +#define ICC_CTLR_EL1_A3V (1U << 15)#define ICC_SRE_EL1_SRE (1U << 0)/*
@@ -285,6 +291,14 @@#define ICH_VMCR_CTLR_SHIFT 0#define ICH_VMCR_CTLR_MASK (0x21f << ICH_VMCR_CTLR_SHIFT) +#define ICH_VMCR_ENG0_SHIFT 0 +#define ICH_VMCR_ENG0 (1 << ICH_VMCR_ENG0_SHIFT) +#define ICH_VMCR_ENG1_SHIFT 1 +#define ICH_VMCR_ENG1 (1 << ICH_VMCR_ENG1_SHIFT) +#define ICH_VMCR_CBPR_SHIFT 4 +#define ICH_VMCR_CBPR (1 << ICH_VMCR_CBPR_SHIFT) +#define ICH_VMCR_EOIM_SHIFT 9 +#define ICH_VMCR_EOIM (1 << ICH_VMCR_EOIM_SHIFT)#define ICH_VMCR_BPR1_SHIFT 18#define ICH_VMCR_BPR1_MASK (7 << ICH_VMCR_BPR1_SHIFT)#define ICH_VMCR_BPR0_SHIFT 21
virt/kvm/arm/vgic-v3-emul.c +246 -0
264 lines not on this page · raw message
Signature · 1 line
2.4.4