Skip to content

Commit a87a364

Browse files
committed
Merge branch kvm-arm64/writable-id-regs into kvmarm/next
* kvm-arm64/writable-id-regs: : Writable ID registers, courtesy of Jing Zhang : : This series significantly expands the architectural feature set that : userspace can manipulate via the ID registers. A new ioctl is defined : that makes the mutable fields in the ID registers discoverable to : userspace. KVM: selftests: Avoid using forced target for generating arm64 headers tools headers arm64: Fix references to top srcdir in Makefile KVM: arm64: selftests: Test for setting ID register from usersapce tools headers arm64: Update sysreg.h with kernel sources KVM: selftests: Generate sysreg-defs.h and add to include path perf build: Generate arm64's sysreg-defs.h and add to include path tools: arm64: Add a Makefile for generating sysreg-defs.h KVM: arm64: Document vCPU feature selection UAPIs KVM: arm64: Allow userspace to change ID_AA64ZFR0_EL1 KVM: arm64: Allow userspace to change ID_AA64PFR0_EL1 KVM: arm64: Allow userspace to change ID_AA64MMFR{0-2}_EL1 KVM: arm64: Allow userspace to change ID_AA64ISAR{0-2}_EL1 KVM: arm64: Bump up the default KVM sanitised debug version to v8p8 KVM: arm64: Reject attempts to set invalid debug arch version KVM: arm64: Advertise selected DebugVer in DBGDIDR.Version KVM: arm64: Use guest ID register values for the sake of emulation KVM: arm64: Document KVM_ARM_GET_REG_WRITABLE_MASKS KVM: arm64: Allow userspace to get the writable masks for feature ID registers Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
2 parents 54b44ad + 70c7b70 commit a87a364

File tree

20 files changed

+1095
-699
lines changed

20 files changed

+1095
-699
lines changed

Documentation/virt/kvm/api.rst

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3370,6 +3370,8 @@ return indicates the attribute is implemented. It does not necessarily
33703370
indicate that the attribute can be read or written in the device's
33713371
current state. "addr" is ignored.
33723372

3373+
.. _KVM_ARM_VCPU_INIT:
3374+
33733375
4.82 KVM_ARM_VCPU_INIT
33743376
----------------------
33753377

@@ -6070,6 +6072,56 @@ writes to the CNTVCT_EL0 and CNTPCT_EL0 registers using the SET_ONE_REG
60706072
interface. No error will be returned, but the resulting offset will not be
60716073
applied.
60726074

6075+
.. _KVM_ARM_GET_REG_WRITABLE_MASKS:
6076+
6077+
4.139 KVM_ARM_GET_REG_WRITABLE_MASKS
6078+
-------------------------------------------
6079+
6080+
:Capability: KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES
6081+
:Architectures: arm64
6082+
:Type: vm ioctl
6083+
:Parameters: struct reg_mask_range (in/out)
6084+
:Returns: 0 on success, < 0 on error
6085+
6086+
6087+
::
6088+
6089+
#define KVM_ARM_FEATURE_ID_RANGE 0
6090+
#define KVM_ARM_FEATURE_ID_RANGE_SIZE (3 * 8 * 8)
6091+
6092+
struct reg_mask_range {
6093+
__u64 addr; /* Pointer to mask array */
6094+
__u32 range; /* Requested range */
6095+
__u32 reserved[13];
6096+
};
6097+
6098+
This ioctl copies the writable masks for a selected range of registers to
6099+
userspace.
6100+
6101+
The ``addr`` field is a pointer to the destination array where KVM copies
6102+
the writable masks.
6103+
6104+
The ``range`` field indicates the requested range of registers.
6105+
``KVM_CHECK_EXTENSION`` for the ``KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES``
6106+
capability returns the supported ranges, expressed as a set of flags. Each
6107+
flag's bit index represents a possible value for the ``range`` field.
6108+
All other values are reserved for future use and KVM may return an error.
6109+
6110+
The ``reserved[13]`` array is reserved for future use and should be 0, or
6111+
KVM may return an error.
6112+
6113+
KVM_ARM_FEATURE_ID_RANGE (0)
6114+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6115+
6116+
The Feature ID range is defined as the AArch64 System register space with
6117+
op0==3, op1=={0, 1, 3}, CRn==0, CRm=={0-7}, op2=={0-7}.
6118+
6119+
The mask returned array pointed to by ``addr`` is indexed by the macro
6120+
``ARM64_FEATURE_ID_RANGE_IDX(op0, op1, crn, crm, op2)``, allowing userspace
6121+
to know what fields can be changed for the system register described by
6122+
``op0, op1, crn, crm, op2``. KVM rejects ID register values that describe a
6123+
superset of the features supported by the system.
6124+
60736125
5. The kvm_run structure
60746126
========================
60756127

Documentation/virt/kvm/arm/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ ARM
1111
hypercalls
1212
pvtime
1313
ptp_kvm
14+
vcpu-features
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.. SPDX-License-Identifier: GPL-2.0
2+
3+
===============================
4+
vCPU feature selection on arm64
5+
===============================
6+
7+
KVM/arm64 provides two mechanisms that allow userspace to configure
8+
the CPU features presented to the guest.
9+
10+
KVM_ARM_VCPU_INIT
11+
=================
12+
13+
The ``KVM_ARM_VCPU_INIT`` ioctl accepts a bitmap of feature flags
14+
(``struct kvm_vcpu_init::features``). Features enabled by this interface are
15+
*opt-in* and may change/extend UAPI. See :ref:`KVM_ARM_VCPU_INIT` for complete
16+
documentation of the features controlled by the ioctl.
17+
18+
Otherwise, all CPU features supported by KVM are described by the architected
19+
ID registers.
20+
21+
The ID Registers
22+
================
23+
24+
The Arm architecture specifies a range of *ID Registers* that describe the set
25+
of architectural features supported by the CPU implementation. KVM initializes
26+
the guest's ID registers to the maximum set of CPU features supported by the
27+
system. The ID register values may be VM-scoped in KVM, meaning that the
28+
values could be shared for all vCPUs in a VM.
29+
30+
KVM allows userspace to *opt-out* of certain CPU features described by the ID
31+
registers by writing values to them via the ``KVM_SET_ONE_REG`` ioctl. The ID
32+
registers are mutable until the VM has started, i.e. userspace has called
33+
``KVM_RUN`` on at least one vCPU in the VM. Userspace can discover what fields
34+
are mutable in the ID registers using the ``KVM_ARM_GET_REG_WRITABLE_MASKS``.
35+
See the :ref:`ioctl documentation <KVM_ARM_GET_REG_WRITABLE_MASKS>` for more
36+
details.
37+
38+
Userspace is allowed to *limit* or *mask* CPU features according to the rules
39+
outlined by the architecture in DDI0487J.a D19.1.3 'Principles of the ID
40+
scheme for fields in ID register'. KVM does not allow ID register values that
41+
exceed the capabilities of the system.
42+
43+
.. warning::
44+
It is **strongly recommended** that userspace modify the ID register values
45+
before accessing the rest of the vCPU's CPU register state. KVM may use the
46+
ID register values to control feature emulation. Interleaving ID register
47+
modification with other system register accesses may lead to unpredictable
48+
behavior.

arch/arm64/include/asm/kvm_host.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,8 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
11081108
struct kvm_arm_copy_mte_tags *copy_tags);
11091109
int kvm_vm_ioctl_set_counter_offset(struct kvm *kvm,
11101110
struct kvm_arm_counter_offset *offset);
1111+
int kvm_vm_ioctl_get_reg_writable_masks(struct kvm *kvm,
1112+
struct reg_mask_range *range);
11111113

11121114
/* Guest/host FPSIMD coordination helpers */
11131115
int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,38 @@ struct kvm_smccc_filter {
505505
#define KVM_HYPERCALL_EXIT_SMC (1U << 0)
506506
#define KVM_HYPERCALL_EXIT_16BIT (1U << 1)
507507

508+
/*
509+
* Get feature ID registers userspace writable mask.
510+
*
511+
* From DDI0487J.a, D19.2.66 ("ID_AA64MMFR2_EL1, AArch64 Memory Model
512+
* Feature Register 2"):
513+
*
514+
* "The Feature ID space is defined as the System register space in
515+
* AArch64 with op0==3, op1=={0, 1, 3}, CRn==0, CRm=={0-7},
516+
* op2=={0-7}."
517+
*
518+
* This covers all currently known R/O registers that indicate
519+
* anything useful feature wise, including the ID registers.
520+
*
521+
* If we ever need to introduce a new range, it will be described as
522+
* such in the range field.
523+
*/
524+
#define KVM_ARM_FEATURE_ID_RANGE_IDX(op0, op1, crn, crm, op2) \
525+
({ \
526+
__u64 __op1 = (op1) & 3; \
527+
__op1 -= (__op1 == 3); \
528+
(__op1 << 6 | ((crm) & 7) << 3 | (op2)); \
529+
})
530+
531+
#define KVM_ARM_FEATURE_ID_RANGE 0
532+
#define KVM_ARM_FEATURE_ID_RANGE_SIZE (3 * 8 * 8)
533+
534+
struct reg_mask_range {
535+
__u64 addr; /* Pointer to mask array */
536+
__u32 range; /* Requested range */
537+
__u32 reserved[13];
538+
};
539+
508540
#endif
509541

510542
#endif /* __ARM_KVM_H__ */

arch/arm64/kvm/arm.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
318318
case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
319319
r = kvm_supported_block_sizes();
320320
break;
321+
case KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES:
322+
r = BIT(0);
323+
break;
321324
default:
322325
r = 0;
323326
}
@@ -1713,6 +1716,13 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
17131716

17141717
return kvm_vm_set_attr(kvm, &attr);
17151718
}
1719+
case KVM_ARM_GET_REG_WRITABLE_MASKS: {
1720+
struct reg_mask_range range;
1721+
1722+
if (copy_from_user(&range, argp, sizeof(range)))
1723+
return -EFAULT;
1724+
return kvm_vm_ioctl_get_reg_writable_masks(kvm, &range);
1725+
}
17161726
default:
17171727
return -EINVAL;
17181728
}

0 commit comments

Comments
 (0)