Skip to content

Commit 8ae6efd

Browse files
Dave MartinMarc Zyngier
authored andcommitted
KVM: arm64/sve: Clean up UAPI register ID definitions
Currently, the SVE register ID macros are not all defined in the same way, and advertise the fact that FFR maps onto the nonexistent predicate register P16. This is really just for kernel convenience, and may lead userspace into bad habits. Instead, this patch masks the ID macro arguments so that architecturally invalid register numbers will not be passed through any more, and uses a literal KVM_REG_ARM64_SVE_FFR_BASE macro to define KVM_REG_ARM64_SVE_FFR(), similarly to the way the _ZREG() and _PREG() macros are defined. Rather than plugging in magic numbers for the number of Z- and P- registers and the maximum possible number of register slices, this patch provides definitions for those too. Userspace is going to need them in any case, and it makes sense for them to come from <uapi/asm/kvm.h>. sve_reg_to_region() uses convenience constants that are defined in a different way, and also makes use of the fact that the FFR IDs are really contiguous with the P15 IDs, so this patch retains the existing convenience constants in guest.c, supplemented with a couple of sanity checks to check for consistency with the UAPI header. Fixes: e1c9c98 ("KVM: arm64/sve: Add SVE support to register access ioctl interface") Suggested-by: Andrew Jones <drjones@redhat.com> Signed-off-by: Dave Martin <Dave.Martin@arm.com> Reviewed-by: Andrew Jones <drjones@redhat.com> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent 700698a commit 8ae6efd

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <linux/psci.h>
3636
#include <linux/types.h>
3737
#include <asm/ptrace.h>
38+
#include <asm/sve_context.h>
3839

3940
#define __KVM_HAVE_GUEST_DEBUG
4041
#define __KVM_HAVE_IRQ_LINE
@@ -233,16 +234,29 @@ struct kvm_vcpu_events {
233234
/* Z- and P-regs occupy blocks at the following offsets within this range: */
234235
#define KVM_REG_ARM64_SVE_ZREG_BASE 0
235236
#define KVM_REG_ARM64_SVE_PREG_BASE 0x400
237+
#define KVM_REG_ARM64_SVE_FFR_BASE 0x600
236238

237-
#define KVM_REG_ARM64_SVE_ZREG(n, i) (KVM_REG_ARM64 | KVM_REG_ARM64_SVE | \
238-
KVM_REG_ARM64_SVE_ZREG_BASE | \
239-
KVM_REG_SIZE_U2048 | \
240-
((n) << 5) | (i))
241-
#define KVM_REG_ARM64_SVE_PREG(n, i) (KVM_REG_ARM64 | KVM_REG_ARM64_SVE | \
242-
KVM_REG_ARM64_SVE_PREG_BASE | \
243-
KVM_REG_SIZE_U256 | \
244-
((n) << 5) | (i))
245-
#define KVM_REG_ARM64_SVE_FFR(i) KVM_REG_ARM64_SVE_PREG(16, i)
239+
#define KVM_ARM64_SVE_NUM_ZREGS __SVE_NUM_ZREGS
240+
#define KVM_ARM64_SVE_NUM_PREGS __SVE_NUM_PREGS
241+
242+
#define KVM_ARM64_SVE_MAX_SLICES 32
243+
244+
#define KVM_REG_ARM64_SVE_ZREG(n, i) \
245+
(KVM_REG_ARM64 | KVM_REG_ARM64_SVE | KVM_REG_ARM64_SVE_ZREG_BASE | \
246+
KVM_REG_SIZE_U2048 | \
247+
(((n) & (KVM_ARM64_SVE_NUM_ZREGS - 1)) << 5) | \
248+
((i) & (KVM_ARM64_SVE_MAX_SLICES - 1)))
249+
250+
#define KVM_REG_ARM64_SVE_PREG(n, i) \
251+
(KVM_REG_ARM64 | KVM_REG_ARM64_SVE | KVM_REG_ARM64_SVE_PREG_BASE | \
252+
KVM_REG_SIZE_U256 | \
253+
(((n) & (KVM_ARM64_SVE_NUM_PREGS - 1)) << 5) | \
254+
((i) & (KVM_ARM64_SVE_MAX_SLICES - 1)))
255+
256+
#define KVM_REG_ARM64_SVE_FFR(i) \
257+
(KVM_REG_ARM64 | KVM_REG_ARM64_SVE | KVM_REG_ARM64_SVE_FFR_BASE | \
258+
KVM_REG_SIZE_U256 | \
259+
((i) & (KVM_ARM64_SVE_MAX_SLICES - 1)))
246260

247261
/* Vector lengths pseudo-register: */
248262
#define KVM_REG_ARM64_SVE_VLS (KVM_REG_ARM64 | KVM_REG_ARM64_SVE | \

‎arch/arm64/kvm/guest.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,15 @@ static int sve_reg_to_region(struct sve_state_reg_region *region,
325325

326326
size_t sve_state_size;
327327

328+
const u64 last_preg_id = KVM_REG_ARM64_SVE_PREG(SVE_NUM_PREGS - 1,
329+
SVE_NUM_SLICES - 1);
330+
331+
/* Verify that the P-regs and FFR really do have contiguous IDs: */
332+
BUILD_BUG_ON(KVM_REG_ARM64_SVE_FFR(0) != last_preg_id + 1);
333+
334+
/* Verify that we match the UAPI header: */
335+
BUILD_BUG_ON(SVE_NUM_SLICES != KVM_ARM64_SVE_MAX_SLICES);
336+
328337
/* Only the first slice ever exists, for now: */
329338
if ((reg->id & SVE_REG_SLICE_MASK) != 0)
330339
return -ENOENT;

0 commit comments

Comments
 (0)