KVM: arm64: Make CPU ID registers writable by userspace
Subsystems (5)
v5 cover KVM: arm64: Make CPU ID registers writable by userspace
In KVM/arm64, values of ID registers for a guest are mostly same as its host's values except for bits for feature that KVM doesn't support and for opt-in features that userspace didn't configure. Userspace can use KVM_SET_ONE_REG to a set ID register value, but it fails if userspace attempts to modify the register value.
This patch series adds support to allow userspace to modify a value of ID registers (as long as KVM can support features that are indicated in the registers) so userspace can have more control of configuring and unconfiguring features for guests. We need this because we would like to expose a uniform set/level of features for a group of guests on systems with different ARM CPUs. Since some features are not binary in nature (e.g. AA64DFR0_EL1.BRPs fields indicate number of breakpoints minus 1), using KVM_ARM_VCPU_INIT to control such features is inconvenient.
The patch series is for both VHE and non-VHE, except for protected VMs, which have a different way of configuring ID registers based on its different requirements [1].
There was a patch series that tried to achieve the same thing [2]. A few snippets of codes in this series were inspired by or came from [2].
The initial value of ID registers for a vCPU will be the host's value with bits cleared for unsupported features and for opt-in features that were not configured. So, the initial value userspace can see (via KVM_GET_ONE_REG) is the upper limit that can be set for the register. Any requests to change the value that conflicts with opt-in features' configuration will fail (e.g. if KVM_ARM_VCPU_PMU_V3 is configured by KVM_ARM_VCPU_INIT, ID_AA64DFR0_EL1.PMUVER cannot be set to zero. Otherwise, the initial value of ID_AA64DFR0_EL1.PMUVER will be zero, and cannot be changed from zero).
When a guest tries to use a CPU feature that is not exposed to the guest, trapping it (to emulate a real CPU's behavior) would generally be a desirable behavior (when it is possible with no or little side effects). The later patches in the series add codes for this. Only features that can be trapped independently will be trapped by this series though.
This series adds kunit tests for new functions in sys_regs.c (except for trivial ones), and these tests are enabled with a new configuration option 'CONFIG_KVM_KUNIT_TEST'.
The series is based on v5.17-rc3 with the patch [3] applied.
v5:
- Change the return value of kcalloc failure of init_arm64_ftr_bits_kvm to -ENOMEM from ENOMEM. [Fuad]
- Call init_arm64_ftr_bits_kvm from init_cpu_features(). [Ricardo, Fuad]
- Move is_id_reg() in arch/arm64/kvm/sys_regs.c. [Fuad]
- Remove frac_ftr_check from feature_frac [Fuad]
- Rename kvm_id_regs_consistency_check() [Fuad]
- Add feature_config_ctrl for ID_AA64DFR0_TRACEVER [Fuad]
- Move changes for kvm_set_id_reg_feature and __modify_kvm_id_reg from
patch-4 to patch-3. [Fuad]
- Comment additions and fixes [Fuad]
- Rename arm64_check_features() [Ricardo]
- Run arm64_check_features_kvm() for the default guest value [Ricardo]
- Add ID_AA64MMFR1_EL1.HAFDBS validation
- Cosmetic fixes
v4: https://lore.kernel.org/all/[email protected]/
- Make ID registers storage per VM instead of per vCPU. [Marc]
- Implement arm64_check_features() in arch/arm64/kernel/cpufeature.c by using existing codes in the file. [Marc]
- Use a configuration function to enable traps for disabled features. [Marc]
- Document ID registers become immutable after the first KVM_RUN [Eric]
- Update ID_AA64PFR0.GIC at the point where a GICv3 is created. [Marc]
- Get TGranX's bit position by substracting 12 from TGranX_2's bit position. [Eric]
- Don't validate AArch32 ID registers when the system doesn't support
32bit EL0. [Eric]
- Add/fixes comments for patches. [Eric]
- Made bug fixes/improvements of the selftest. [Eric]
- Added .kunitconfig for arm64 KUnit tests
v3: https://lore.kernel.org/all/[email protected]/
- Remove ID register consistency checking across vCPUs. [Oliver]
- Change KVM_CAP_ARM_ID_REG_WRITABLE to KVM_CAP_ARM_ID_REG_CONFIGURABLE. [Oliver]
- Add KUnit testing for ID register validation and trap initialization.
- Change read_id_reg() to take care of ID_AA64PFR0_EL1.GIC.
- Add a helper of read_id_reg() (__read_id_reg()) and use the helper instead of directly using __vcpu_sys_reg().
- Change not to run kvm_id_regs_consistency_check() and kvm_vcpu_init_traps() for protected VMs.
- Update selftest to remove test cases for ID register consistency. checking across vCPUs and to add test cases for ID_AA64PFR0_EL1.GIC.
v2: https://lore.kernel.org/all/[email protected]/
- Remove unnecessary line breaks. [Andrew]
- Use @params for comments. [Andrew]
- Move arm64_check_features to arch/arm64/kvm/sys_regs.c and change that KVM specific feature check function. [Andrew]
- Remove unnecessary raz handling from __set_id_reg. [Andrew]
- Remove sys_val field from the initial id_reg_info and add it in the later patch. [Andrew]
- Call id_reg->init() from id_reg_info_init(). [Andrew]
- Fix cpuid_feature_cap_perfmon_field() to convert 0xf to 0x0 (and use it in the following patches).
- Change kvm_vcpu_first_run_init to set has_run_once to false when kvm_id_regs_consistency_check() fails.
- Add a patch to introduce id_reg_info for ID_AA64MMFR0_EL1, which requires special validity checking for TGran*_2 fields.
- Add patches to introduce id_reg_info for ID_DFR1_EL1 and ID_MMFR0_EL1, which are required due to arm64_check_features implementation change.
- Add a new argument, which is a pointer to id_reg_info, for id_reg_info's validate().
v1: https://lore.kernel.org/all/[email protected]/
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
Reiji Watanabe (27):
KVM: arm64: Introduce a validation function for an ID register
KVM: arm64: Save ID registers' sanitized value per guest
KVM: arm64: Introduce struct id_reg_info
KVM: arm64: Make ID_AA64PFR0_EL1 writable
KVM: arm64: Make ID_AA64PFR1_EL1 writable
KVM: arm64: Make ID_AA64ISAR0_EL1 writable
KVM: arm64: Make ID_AA64ISAR1_EL1 writable
KVM: arm64: Make ID_AA64MMFR0_EL1 writable
KVM: arm64: Make ID_AA64MMFR1_EL1 writable
KVM: arm64: Hide IMPLEMENTATION DEFINED PMU support for the guest
KVM: arm64: Make ID_AA64DFR0_EL1 writable
KVM: arm64: Make ID_DFR0_EL1 writable
KVM: arm64: Make MVFR1_EL1 writable
KVM: arm64: Make ID registers without id_reg_info writable
KVM: arm64: Add consistency checking for frac fields of ID registers
KVM: arm64: Introduce KVM_CAP_ARM_ID_REG_CONFIGURABLE capability
KVM: arm64: Add kunit test for ID register validation
KVM: arm64: Use vcpu->arch cptr_el2 to track value of cptr_el2 for VHE
KVM: arm64: Use vcpu->arch.mdcr_el2 to track value of mdcr_el2
KVM: arm64: Introduce framework to trap disabled features
KVM: arm64: Trap disabled features of ID_AA64PFR0_EL1
KVM: arm64: Trap disabled features of ID_AA64PFR1_EL1
KVM: arm64: Trap disabled features of ID_AA64DFR0_EL1
KVM: arm64: Trap disabled features of ID_AA64MMFR1_EL1
KVM: arm64: Trap disabled features of ID_AA64ISAR1_EL1
KVM: arm64: Add kunit test for trap initialization
KVM: arm64: selftests: Introduce id_reg_test
Documentation/virt/kvm/api.rst | 12 + arch/arm64/include/asm/cpufeature.h | 3 +- arch/arm64/include/asm/kvm_arm.h | 32 + arch/arm64/include/asm/kvm_host.h | 15 + arch/arm64/include/asm/sysreg.h | 3 + arch/arm64/kernel/cpufeature.c | 229 +++ arch/arm64/kvm/.kunitconfig | 4 + arch/arm64/kvm/Kconfig | 11 + arch/arm64/kvm/arm.c | 24 +- arch/arm64/kvm/debug.c | 13 +- arch/arm64/kvm/hyp/vhe/switch.c | 14 +- arch/arm64/kvm/sys_regs.c | 1392 +++++++++++++++-- arch/arm64/kvm/sys_regs_test.c | 1280 +++++++++++++++ arch/arm64/kvm/vgic/vgic-init.c | 9 + include/uapi/linux/kvm.h | 1 + tools/arch/arm64/include/asm/sysreg.h | 1 + tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/aarch64/id_reg_test.c | 1239 +++++++++++++++ 19 files changed, 4138 insertions(+), 146 deletions(-) create mode 100644 arch/arm64/kvm/.kunitconfig create mode 100644 arch/arm64/kvm/sys_regs_test.c create mode 100644 tools/testing/selftests/kvm/aarch64/id_reg_test.c
base-commit: 7a0ca515b9f15b68f22ec0feacd868b2c810b0af
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
PATCH v5 1/27 KVM: arm64: Introduce a validation function for an ID register
Introduce arm64_check_features(), which does a basic validity checking of an ID register value against the register's limit value, which is generally the host's sanitized value.
This function will be used by the following patches to check if an ID register value that userspace tries to set for a guest can be supported on the host.
The validation is done using arm64_ftr_bits_kvm, which is created from arm64_ftr_regs, with some entries overwritten by entries from arm64_ftr_bits_kvm_override.
--- arch/arm64/include/asm/cpufeature.h | 1 + arch/arm64/kernel/cpufeature.c | 229 ++++++++++++++++++++++++++++ 2 files changed, 230 insertions(+)
arch/arm64/include/asm/cpufeature.h +1 -0
@@ -631,6 +631,7 @@ void check_local_cpu_capabilities(void);u64 read_sanitised_ftr_reg (u32 id);u64 __read_sysreg_by_encoding (u32 sys_id); +int arm64_check_features_kvm (u32 sys_reg,u64 val,u64 limit);static inline bool cpu_supports_mixed_endian_el0 (void ) {
arch/arm64/kernel/cpufeature.c +229 -0
244 lines not on this page · raw message
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
PATCH v5 2/27 KVM: arm64: Save ID registers' sanitized value per guest
In reply to Reiji WatanabeIntroduce id_regs[] in kvm_arch as a storage of guest's ID registers, and save ID registers' sanitized value in the array at KVM_CREATE_VM. Use the saved ones when ID registers are read by the guest or userspace (via KVM_GET_ONE_REG).
--- arch/arm64/include/asm/kvm_host.h | 12 ++++++ arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/sys_regs.c | 65 ++++++++++++++++++++++++------- 3 files changed, 63 insertions(+), 15 deletions(-)
arch/arm64/include/asm/kvm_host.h +12 -0
@@ -101,6 +101,13 @@ struct kvm_s2_mmu {struct kvm_arch_memory_slot { }; +/* +* (Op0, Op1, CRn, CRm, Op2) of ID registers is (3, 0, 0, crm, op2), +* where 0<=crm<8, 0<=op2<8. +*/ +#define KVM_ARM_ID_REG_MAX_NUM 64 +#define IDREG_IDX (id) ((sys_reg_CRm(id) << 3) | sys_reg_Op2(id)) +struct kvm_arch {struct kvm_s2_mmu mmu;
@@ -137,6 +144,9 @@ struct kvm_arch {/* Memory Tagging Extension enabled for the guest */ bool mte_enabled;bool ran_once; + +/* ID registers for the guest. */ +u64 id_regs[KVM_ARM_ID_REG_MAX_NUM ]; };struct kvm_vcpu_fault_info {
@@ -736,6 +746,8 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,long kvm_vm_ioctl_mte_copy_tags (struct kvm *kvm,struct kvm_arm_copy_mte_tags *copy_tags); +void set_default_id_regs (struct kvm *kvm); +/* Guest/host FPSIMD coordination helpers */ int kvm_arch_vcpu_run_map_fp (struct kvm_vcpu *vcpu);void kvm_arch_vcpu_load_fp (struct kvm_vcpu *vcpu);
arch/arm64/kvm/arm.c +1 -0
@@ -156,6 +156,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) kvm->arch.max_vcpus =kvm_arm_default_max_vcpus ();set_default_spectre (kvm); +set_default_id_regs (kvm);return ret; out_free_stage2_pgd:
arch/arm64/kvm/sys_regs.c +50 -15
106 lines not on this page · raw message
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
PATCH v5 3/27 KVM: arm64: Introduce struct id_reg_info
In reply to Reiji WatanabeThis patch lays the groundwork to make ID registers writable.
Introduce struct id_reg_info for an ID register to manage the register specific control of its value for the guest, and provide set of functions commonly used for ID registers to make them writable.
The id_reg_info is used to do register specific initialization, validation of the ID register and etc. Not all ID registers must have the id_reg_info. ID registers that don't have the id_reg_info are handled in a common way that is applied to all ID registers.
At present, changing an ID register from userspace is allowed only if the ID register has the id_reg_info, but that will be changed by the following patches.
No ID register has the structure yet and the following patches will add the id_reg_info for some ID registers.
kvm_set_id_reg_feature(), which is introduced in this patch, is going to be used by the following patch outsdie from sys_regs.c when an ID register field needs to be updated.
--- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/include/asm/sysreg.h | 1 + arch/arm64/kvm/sys_regs.c | 280 ++++++++++++++++++++++++++++-- 3 files changed, 268 insertions(+), 14 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -747,6 +747,7 @@ long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, struct kvm_arm_copy_mte_tags *copy_tags);void set_default_id_regs (struct kvm *kvm); +int kvm_set_id_reg_feature (struct kvm *kvm,u32 id,u8 field_shift,u8 fval);/* Guest/host FPSIMD coordination helpers */ int kvm_arch_vcpu_run_map_fp (struct kvm_vcpu *vcpu);
arch/arm64/include/asm/sysreg.h +1 -0
@@ -1214,6 +1214,7 @@#define ICH_VTR_TDS_MASK (1 << ICH_VTR_TDS_SHIFT)#define ARM64_FEATURE_FIELD_BITS 4 +#define ARM64_FEATURE_FIELD_MASK ((1ull << ARM64_FEATURE_FIELD_BITS) - 1)/* Create a mask for the feature bits of the specified feature. */ #define ARM64_FEATURE_MASK (x) (GENMASK_ULL(x##_SHIFT + ARM64_FEATURE_FIELD_BITS - 1, x##_SHIFT))
arch/arm64/kvm/sys_regs.c +266 -14
345 lines not on this page · raw message
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
Re: [PATCH v5 03/27] KVM: arm64: Introduce struct id_reg_info
Quoted 15 lines · Reiji Watanabe
This patch lays the groundwork to make ID registers writable.
Introduce struct
id_reg_infofor an ID register to manage the register specific control of its value for the guest, and provide set of functions commonly used for ID registers to make them writable.The
id_reg_infois used to do register specific initialization, validation of the ID register and etc. Not all ID registers must have theid_reg_info. ID registers that don't have theid_reg_infoare handled in a common way that is applied to all ID registers.At present, changing an ID register from userspace is allowed only if the ID register has the
id_reg_info, but that will be changed by the following patches.
Quoted 5 lines
No ID register has the structure yet and the following patches will add the
id_reg_infofor some ID registers.
kvm_set_id_reg_feature(), which is introduced in this patch, is going to be used by the following patch outsdie fromsys_regs.c
typo: outside
Quoted 26 lines
when an ID register field needs to be updated.
Signed-off-by Reiji Watanabe <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/include/asm/sysreg.h | 1 + arch/arm64/kvm/sys_regs.c | 280 ++++++++++++++++++++++++++++-- 3 files changed, 268 insertions(+), 14 deletions(-)arch/arm64/include/asm/kvm_host.h +1
-0@@ -747,6 +747,7 @@ long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, struct kvm_arm_copy_mte_tags *copy_tags);void set_default_id_regs (struct kvm *kvm); +int kvm_set_id_reg_feature (struct kvm *kvm,u32 id,u8 field_shift,u8 fval);/* Guest/host FPSIMD coordination helpers */ int kvm_arch_vcpu_run_map_fp (struct kvm_vcpu *vcpu);arch/arm64/include/asm/sysreg.h +0
-0
arch/arm64/include/asm/sysreg.h line 1217
Quoted 4 lines
arch/arm64/include/asm/sysreg.h +1
-0@@ -1214,6 +1214,7 @@#define ICH_VTR_TDS_MASK (1 << ICH_VTR_TDS_SHIFT)#define ARM64_FEATURE_FIELD_BITS 4 +#define ARM64_FEATURE_FIELD_MASK ((1ull << ARM64_FEATURE_FIELD_BITS) - 1)
nit: use GENMASK_ULL()
arch/arm64/include/asm/sysreg.h line 1220
Quoted 2 lines
arch/arm64/include/asm/sysreg.h +0
-0/* Create a mask for the feature bits of the specified feature. */ #define ARM64_FEATURE_MASK (x) (GENMASK_ULL(x##_SHIFT + ARM64_FEATURE_FIELD_BITS - 1, x##_SHIFT))
nit: make use of the newly-minted ARM64_FEATURE_FIELD_MASK in this macro.
Quoted 72 lines
arch/arm64/kvm/sys_regs.c +64
-0@@ -265,6 +265,113 @@ static bool trap_raz_wi(struct kvm_vcpu *vcpu,return read_zero (vcpu, p); } +struct id_reg_info { +/* Register ID */ +u32 sys_reg; + +/* +* Limit value of the register for a vcpu. The value is the sanitized +* system value with bits set/cleared for unsupported features for the +* guest. +*/ +u64 vcpu_limit_val; + +/* Fields that are not validated by arm64_check_features_kvm. */ +u64 ignore_mask; + +/* An optional initialization function of the id_reg_info */ +void (*init)(struct id_reg_info *id_reg); + +/* +* This is an optional ID register specific validation function. When +* userspace tries to set the ID register, arm64_check_features_kvm() +* will check if the requested value indicates any features that cannot +* be supported by KVM on the host. But, some ID register fields need +* a special checking, and this function can be used for such fields. +* e.g. When SVE is configured for a vCPU by KVM_ARM_VCPU_INIT, +* ID_AA64PFR0_EL1.SVE shouldn't be set to 0 for the vCPU. +* The validation function for ID_AA64PFR0_EL1 could be used to check +* the field is consistent with SVE configuration. +*/ +int (*validate)(struct kvm_vcpu *vcpu,const struct id_reg_info *id_reg, +u64 val); + +/* +* Return a bitmask of the vCPU's ID register fields that are not +* synced with saved (per VM) ID register value, which usually +* indicates opt-in CPU features that are not configured for the vCPU. +* ID registers are saved per VM, but some opt-in CPU features can +* be configured per vCPU. The saved (per VM) values for such +* features are for vCPUs with the features (and zero for +* vCPUs without the features). +* Return value of this function is used to handle such fields +* for per vCPU ID register read/write request with saved per VM +* ID register. See the __write_id_reg's comment for more detail. +*/ +u64 (*vcpu_mask)(const struct kvm_vcpu *vcpu, +const struct id_reg_info *id_reg); +}; + +static void id_reg_info_init (struct id_reg_info *id_reg) +{ +u64 val =read_sanitised_ftr_reg (id_reg->sys_reg); + + id_reg->vcpu_limit_val = val; +if (id_reg->init) + id_reg->init (id_reg); + +/* +* id_reg->init() might update id_reg->vcpu_limit_val. +* Make sure that id_reg->vcpu_limit_val, which will be the default +* register value for guests, is a safe value to use for guests +* on the host. +*/ +WARN_ON_ONCE (arm64_check_features_kvm (id_reg->sys_reg, + id_reg->vcpu_limit_val, val)); +}
arch/arm64/kvm/sys_regs.c static bool trap_raz_wi(struct kvm_vcpu *vcpu, · line 341
Quoted 10 lines
arch/arm64/kvm/sys_regs.c +10
-0+ +/* +* An ID register that needs special handling to control the value for the +* guest must have its own id_reg_info in id_reg_info_table. +* (i.e. the reset value is different from the host's sanitized value, +* the value is affected by opt-in features, some fields need specific +* validation, etc.) +*/ +#define GET_ID_REG_INFO (id) (id_reg_info_table[IDREG_IDX(id)]) +static struct id_reg_info *id_reg_info_table[KVM_ARM_ID_REG_MAX_NUM ] = {};
It seems a bit peculiar that we effectively have two arrays that describe feature ID registers now. IMO, it might be a bit cleaner to sweep all relevant data into this array and just remove them from the general system register array altogether.
We are already adding a lot of special-case handling for feature ID registers, and that is only bound to increase as the architecture is extended upon further.
Quoted 278 lines
arch/arm64/kvm/sys_regs.c +191
-14+static int validate_id_reg (struct kvm_vcpu *vcpu,u32 id,u64 val) +{ +const struct id_reg_info *id_reg =GET_ID_REG_INFO (id); +u64 limit, tmp_val; +int err; + +if (id_reg) { + limit = id_reg->vcpu_limit_val; +/* +* Replace the fields that are indicated in ignore_mask with +* the value in the limit to not have arm64_check_features_kvm() +* check the field in @val. +*/ + tmp_val = val & ~id_reg->ignore_mask; + tmp_val |= (limit & id_reg->ignore_mask); + }else { + limit =read_sanitised_ftr_reg (id); + tmp_val = val; + } + +/* Check if the value indicates any feature that is not in the limit. */ + err =arm64_check_features_kvm (id, tmp_val, limit); +if (err) +return err; + +if (id_reg && id_reg->validate) +/* Run the ID register specific validity check. */ + err = id_reg->validate (vcpu, id_reg, val); + +return err; +} +/* * ARMv8.1 mandates at least a trivial LORegion implementation, where all the * RW registers are RES0 (which we can implement as RAZ/WI). On an ARMv8.0 @@ -1068,9 +1175,91 @@ static bool is_id_reg(u32 id) sys_reg_CRm(id) < 8); } +static u64 read_kvm_id_reg (struct kvm *kvm,u32 id) +{ +return kvm->arch.id_regs[IDREG_IDX (id)]; +} + +static int __modify_kvm_id_reg (struct kvm *kvm,u32 id,u64 val, +u64 preserve_mask) +{ +u64 old, new; + +lockdep_assert_held (&kvm->lock); + + old = kvm->arch.id_regs[IDREG_IDX (id)]; + +/* Preserve the value at the bit position set in preserve_mask */ + new = old & preserve_mask; + new |= (val & ~preserve_mask); + +/* Don't allow to modify ID register value after KVM_RUN on any vCPUs */ +if (kvm->arch.ran_once && new != old) +return -EBUSY ; + +WRITE_ONCE (kvm->arch.id_regs[IDREG_IDX (id)], new); + +return 0 ; +} + +static int modify_kvm_id_reg (struct kvm *kvm,u32 id,u64 val, +u64 preserve_mask) +{ +int ret; + +mutex_lock (&kvm->lock); + ret =__modify_kvm_id_reg (kvm, id, val, preserve_mask); +mutex_unlock (&kvm->lock); + +return ret; +} + +static int write_kvm_id_reg (struct kvm *kvm,u32 id,u64 val) +{ +return modify_kvm_id_reg (kvm, id, val,0 ); +} + +/* +* KVM basically forces all vCPUs of the guest to have a uniform value for +* each ID register (it means KVM_SET_ONE_REG for a vCPU affects all +* the vCPUs of the guest), and the id_regs[] of kvm_arch holds values +* of ID registers for the guest. However, there is an exception for +* ID register fields corresponding to CPU features that can be +* configured per vCPU by KVM_ARM_VCPU_INIT, or etc (e.g. PMUv3, SVE, etc). +* For such fields, all vCPUs that have the feature will have a non-zero +* uniform value, which can be updated by userspace, but the vCPUs that +* don't have the feature will have zero for the fields. +* Values that @id_regs holds are for vCPUs that have such features. So, +* to get the ID register value for a vCPU that doesn't have those features, +* the corresponding fields in id_regs[] needs to be cleared. +* A bitmask of the fields are provided by id_reg_info's vcpu_mask(), and +* __write_id_reg() and __read_id_reg() take care of those fields using +* the bitmask. +*/ +static int __write_id_reg (struct kvm_vcpu *vcpu,u32 id,u64 val) +{ +const struct id_reg_info *id_reg =GET_ID_REG_INFO (id); +u64 mask =0 ; + +if (id_reg && id_reg->vcpu_mask) + mask = id_reg->vcpu_mask (vcpu, id_reg); + +/* +* Update the ID register for the guest with @val, except for fields +* that are set in the mask, which indicates fields for opt-in +* features that are not configured for the vCPU. +*/ +return modify_kvm_id_reg (vcpu->kvm, id, val, mask); +} +static u64 __read_id_reg (const struct kvm_vcpu *vcpu,u32 id) { -u64 val = vcpu->kvm->arch.id_regs[IDREG_IDX (id)]; +const struct id_reg_info *id_reg =GET_ID_REG_INFO (id); +u64 val =read_kvm_id_reg (vcpu->kvm, id); + +if (id_reg && id_reg->vcpu_mask) +/* Clear fields for opt-in features that are not configured. */ + val &= ~(id_reg->vcpu_mask (vcpu, id_reg));switch (id) {case SYS_ID_AA64PFR0_EL1 :@@ -1229,12 +1418,7 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,return 0 ; } -/* -* cpufeature ID register user accessors -* -* For now, these registers are immutable for userspace, so for set_id_reg() -* we don't allow the effective value to be changed. -*/ +/* cpufeature ID register user accessors */ static int __get_id_reg (const struct kvm_vcpu *vcpu,const struct sys_reg_desc *rd,void __user *uaddr,bool raz)@@ -1245,11 +1429,31 @@ static int __get_id_reg(const struct kvm_vcpu *vcpu,return reg_to_user (uaddr, &val, id); } -static int __set_id_reg (const struct kvm_vcpu *vcpu, +/* +* Check if the given id indicates AArch32 ID register encoding. +*/ +static bool is_aarch32_id_reg (u32 id) +{ +u32 crm, op2; + +if (!is_id_reg (id)) +return false; + + crm =sys_reg_CRm (id); + op2 =sys_reg_Op2 (id); +if (crm ==1 || crm ==2 || (crm ==3 && (op2 !=3 && op2 !=7 ))) +/* AArch32 ID register */ +return true; + +return false; +} + +static int __set_id_reg (struct kvm_vcpu *vcpu,const struct sys_reg_desc *rd,void __user *uaddr,bool raz) {const u64 id =sys_reg_to_index (rd); +u32 encoding =reg_to_encoding (rd);int err;u64 val;@@ -1257,11 +1461,28 @@ static int __set_id_reg(const struct kvm_vcpu *vcpu,if (err)return err; -/* This is what we mean by invariant: you can't change it. */ -if (val !=read_id_reg (vcpu, rd, raz)) +if (val ==read_id_reg (vcpu, rd, raz)) +/* The value is same as the current value. Nothing to do. */ +return 0 ; + +/* +* Don't allow to modify the register's value if the register is raz, +* or the reg doesn't have the id_reg_info. +*/ +if (raz || !GET_ID_REG_INFO (encoding))return -EINVAL ; -return 0 ; +/* +* Skip the validation of AArch32 ID registers if the system doesn't +* 32bit EL0 (their value are UNKNOWN). +*/ +if (system_supports_32bit_el0 () || !is_aarch32_id_reg (encoding)) { + err =validate_id_reg (vcpu, encoding, val); +if (err) +return err; + } + +return __write_id_reg (vcpu, encoding, val); } static int get_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,@@ -2823,6 +3044,20 @@ int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)return write_demux_regids (uindices); } +static void id_reg_info_init_all (void ) +{ +int i; +struct id_reg_info *id_reg; + +for (i =0 ; i <ARRAY_SIZE (id_reg_info_table); i++) { + id_reg = (struct id_reg_info *)id_reg_info_table[i]; +if (!id_reg) +continue ; + +id_reg_info_init (id_reg); + } +} +void kvm_sys_reg_table_init (void ) {unsigned int i;@@ -2857,6 +3092,8 @@ void kvm_sys_reg_table_init(void)break ;/* Clear all higher bits. */ cache_levels &= (1 << (i*3 ))-1 ; + +id_reg_info_init_all (); }/* @@ -2869,11 +3106,12 @@ void set_default_id_regs(struct kvm *kvm)u32 id;const struct sys_reg_desc *rd;u64 val; +struct id_reg_info *idr;for (i =0 ; i <ARRAY_SIZE (sys_reg_descs); i++) { rd = &sys_reg_descs[i];if (rd->access != access_id_reg) -/* Not ID register, or hidden/reserved ID register */ +/* Not ID register or hidden/reserved ID register */ continue ; id =reg_to_encoding (rd);@@ -2881,7 +3119,21 @@ void set_default_id_regs(struct kvm *kvm)/* Shouldn't happen */ continue ; - val =read_sanitised_ftr_reg (id); - kvm->arch.id_regs[IDREG_IDX (id)] = val; + idr =GET_ID_REG_INFO (id); + val = idr ? idr->vcpu_limit_val :read_sanitised_ftr_reg (id); +WARN_ON_ONCE (write_kvm_id_reg (kvm, id, val)); } } + +/* +* Update the ID register's field with @fval for the guest. +* The caller is expected to hold the kvm->lock. +* This will not fail unless any vCPUs in the guest have started. +*/ +int kvm_set_id_reg_feature (struct kvm *kvm,u32 id,u8 field_shift,u8 fval) +{ +u64 val = ((u64 )fval &ARM64_FEATURE_FIELD_MASK ) << field_shift; +u64 preserve_mask = ~(ARM64_FEATURE_FIELD_MASK << field_shift); + +return __modify_kvm_id_reg (kvm, id, val, preserve_mask); +}Signature · 1 line
2.35.1.265.g69c8d7142f-goog
--
Thanks,
Oliver
Re: [PATCH v5 03/27] KVM: arm64: Introduce struct id_reg_info
Hi Oliver,
Thank you for the review!
Quoted 17 lines · Oliver Upton
Quoted 15 lines · Reiji Watanabe
This patch lays the groundwork to make ID registers writable.
Introduce struct
id_reg_infofor an ID register to manage the register specific control of its value for the guest, and provide set of functions commonly used for ID registers to make them writable.The
id_reg_infois used to do register specific initialization, validation of the ID register and etc. Not all ID registers must have theid_reg_info. ID registers that don't have theid_reg_infoare handled in a common way that is applied to all ID registers.At present, changing an ID register from userspace is allowed only if the ID register has the
id_reg_info, but that will be changed by the following patches.
Quoted 7 lines
Quoted 5 lines
No ID register has the structure yet and the following patches will add the
id_reg_infofor some ID registers.
kvm_set_id_reg_feature(), which is introduced in this patch, is going to be used by the following patch outsdie fromsys_regs.ctypo: outside
I will fix that.
Quoted 34 lines
Quoted 27 lines
when an ID register field needs to be updated.
Signed-off-by Reiji Watanabe <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/include/asm/sysreg.h | 1 + arch/arm64/kvm/sys_regs.c | 280 ++++++++++++++++++++++++++++-- 3 files changed, 268 insertions(+), 14 deletions(-)arch/arm64/include/asm/kvm_host.h +1
-0@@ -747,6 +747,7 @@ long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, struct kvm_arm_copy_mte_tags *copy_tags);void set_default_id_regs (struct kvm *kvm); +int kvm_set_id_reg_feature (struct kvm *kvm,u32 id,u8 field_shift,u8 fval);/* Guest/host FPSIMD coordination helpers */ int kvm_arch_vcpu_run_map_fp (struct kvm_vcpu *vcpu);arch/arm64/include/asm/sysreg.h +0
-0arch/arm64/include/asm/sysreg.h line 1217
Quoted 4 lines
arch/arm64/include/asm/sysreg.h +1
-0@@ -1214,6 +1214,7 @@#define ICH_VTR_TDS_MASK (1 << ICH_VTR_TDS_SHIFT)#define ARM64_FEATURE_FIELD_BITS 4 +#define ARM64_FEATURE_FIELD_MASK ((1ull << ARM64_FEATURE_FIELD_BITS) - 1)nit: use
GENMASK_ULL()
Quoted 5 lines
arch/arm64/include/asm/sysreg.h line 1220
Quoted 2 lines
arch/arm64/include/asm/sysreg.h +0
-0/* Create a mask for the feature bits of the specified feature. */ #define ARM64_FEATURE_MASK (x) (GENMASK_ULL(x##_SHIFT + ARM64_FEATURE_FIELD_BITS - 1, x##_SHIFT))nit: make use of the newly-minted
ARM64_FEATURE_FIELD_MASKin this macro.
Thank you for catching those. I will fix them.
Quoted 80 lines
Quoted 80 lines
arch/arm64/kvm/sys_regs.c +71
-0@@ -265,6 +265,113 @@ static bool trap_raz_wi(struct kvm_vcpu *vcpu,return read_zero (vcpu, p); } +struct id_reg_info { +/* Register ID */ +u32 sys_reg; + +/* +* Limit value of the register for a vcpu. The value is the sanitized +* system value with bits set/cleared for unsupported features for the +* guest. +*/ +u64 vcpu_limit_val; + +/* Fields that are not validated by arm64_check_features_kvm. */ +u64 ignore_mask; + +/* An optional initialization function of the id_reg_info */ +void (*init)(struct id_reg_info *id_reg); + +/* +* This is an optional ID register specific validation function. When +* userspace tries to set the ID register, arm64_check_features_kvm() +* will check if the requested value indicates any features that cannot +* be supported by KVM on the host. But, some ID register fields need +* a special checking, and this function can be used for such fields. +* e.g. When SVE is configured for a vCPU by KVM_ARM_VCPU_INIT, +* ID_AA64PFR0_EL1.SVE shouldn't be set to 0 for the vCPU. +* The validation function for ID_AA64PFR0_EL1 could be used to check +* the field is consistent with SVE configuration. +*/ +int (*validate)(struct kvm_vcpu *vcpu,const struct id_reg_info *id_reg, +u64 val); + +/* +* Return a bitmask of the vCPU's ID register fields that are not +* synced with saved (per VM) ID register value, which usually +* indicates opt-in CPU features that are not configured for the vCPU. +* ID registers are saved per VM, but some opt-in CPU features can +* be configured per vCPU. The saved (per VM) values for such +* features are for vCPUs with the features (and zero for +* vCPUs without the features). +* Return value of this function is used to handle such fields +* for per vCPU ID register read/write request with saved per VM +* ID register. See the __write_id_reg's comment for more detail. +*/ +u64 (*vcpu_mask)(const struct kvm_vcpu *vcpu, +const struct id_reg_info *id_reg); +}; + +static void id_reg_info_init (struct id_reg_info *id_reg) +{ +u64 val =read_sanitised_ftr_reg (id_reg->sys_reg); + + id_reg->vcpu_limit_val = val; +if (id_reg->init) + id_reg->init (id_reg); + +/* +* id_reg->init() might update id_reg->vcpu_limit_val. +* Make sure that id_reg->vcpu_limit_val, which will be the default +* register value for guests, is a safe value to use for guests +* on the host. +*/ +WARN_ON_ONCE (arm64_check_features_kvm (id_reg->sys_reg, + id_reg->vcpu_limit_val, val)); +} + +/* +* An ID register that needs special handling to control the value for the +* guest must have its own id_reg_info in id_reg_info_table. +* (i.e. the reset value is different from the host's sanitized value, +* the value is affected by opt-in features, some fields need specific +* validation, etc.)
Quoted 8 lines
arch/arm64/kvm/sys_regs.c static bool trap_raz_wi(struct kvm_vcpu *vcpu, · line 341
Quoted 3 lines
arch/arm64/kvm/sys_regs.c +3
-0+*/ +#define GET_ID_REG_INFO (id) (id_reg_info_table[IDREG_IDX(id)]) +static struct id_reg_info *id_reg_info_table[KVM_ARM_ID_REG_MAX_NUM ] = {};It seems a bit peculiar that we effectively have two arrays that describe feature ID registers now. IMO, it might be a bit cleaner to sweep all relevant data into this array and just remove them from the general system register array altogether.
Yes, having a separate array from sys_regs might be a bit cleaner. But, then, ID registers will have a little less sharing of lower level infrastructure, which might potentially introduce a bit higher maintenance cost for the lower level infrastructure.
It won't be too big, so I will consider that.
Regards,
Reiji
Quoted 287 lines
We are already adding a lot of special-case handling for feature ID registers, and that is only bound to increase as the architecture is extended upon further.
Quoted 276 lines
arch/arm64/kvm/sys_regs.c +191
-15+static int validate_id_reg (struct kvm_vcpu *vcpu,u32 id,u64 val) +{ +const struct id_reg_info *id_reg =GET_ID_REG_INFO (id); +u64 limit, tmp_val; +int err; + +if (id_reg) { + limit = id_reg->vcpu_limit_val; +/* +* Replace the fields that are indicated in ignore_mask with +* the value in the limit to not have arm64_check_features_kvm() +* check the field in @val. +*/ + tmp_val = val & ~id_reg->ignore_mask; + tmp_val |= (limit & id_reg->ignore_mask); + }else { + limit =read_sanitised_ftr_reg (id); + tmp_val = val; + } + +/* Check if the value indicates any feature that is not in the limit. */ + err =arm64_check_features_kvm (id, tmp_val, limit); +if (err) +return err; + +if (id_reg && id_reg->validate) +/* Run the ID register specific validity check. */ + err = id_reg->validate (vcpu, id_reg, val); + +return err; +} +/* * ARMv8.1 mandates at least a trivial LORegion implementation, where all the * RW registers are RES0 (which we can implement as RAZ/WI). On an ARMv8.0 @@ -1068,9 +1175,91 @@ static bool is_id_reg(u32 id) sys_reg_CRm(id) < 8); } +static u64 read_kvm_id_reg (struct kvm *kvm,u32 id) +{ +return kvm->arch.id_regs[IDREG_IDX (id)]; +} + +static int __modify_kvm_id_reg (struct kvm *kvm,u32 id,u64 val, +u64 preserve_mask) +{ +u64 old, new; + +lockdep_assert_held (&kvm->lock); + + old = kvm->arch.id_regs[IDREG_IDX (id)]; + +/* Preserve the value at the bit position set in preserve_mask */ + new = old & preserve_mask; + new |= (val & ~preserve_mask); + +/* Don't allow to modify ID register value after KVM_RUN on any vCPUs */ +if (kvm->arch.ran_once && new != old) +return -EBUSY ; + +WRITE_ONCE (kvm->arch.id_regs[IDREG_IDX (id)], new); + +return 0 ; +} + +static int modify_kvm_id_reg (struct kvm *kvm,u32 id,u64 val, +u64 preserve_mask) +{ +int ret; + +mutex_lock (&kvm->lock); + ret =__modify_kvm_id_reg (kvm, id, val, preserve_mask); +mutex_unlock (&kvm->lock); + +return ret; +} + +static int write_kvm_id_reg (struct kvm *kvm,u32 id,u64 val) +{ +return modify_kvm_id_reg (kvm, id, val,0 ); +} + +/* +* KVM basically forces all vCPUs of the guest to have a uniform value for +* each ID register (it means KVM_SET_ONE_REG for a vCPU affects all +* the vCPUs of the guest), and the id_regs[] of kvm_arch holds values +* of ID registers for the guest. However, there is an exception for +* ID register fields corresponding to CPU features that can be +* configured per vCPU by KVM_ARM_VCPU_INIT, or etc (e.g. PMUv3, SVE, etc). +* For such fields, all vCPUs that have the feature will have a non-zero +* uniform value, which can be updated by userspace, but the vCPUs that +* don't have the feature will have zero for the fields. +* Values that @id_regs holds are for vCPUs that have such features. So, +* to get the ID register value for a vCPU that doesn't have those features, +* the corresponding fields in id_regs[] needs to be cleared. +* A bitmask of the fields are provided by id_reg_info's vcpu_mask(), and +* __write_id_reg() and __read_id_reg() take care of those fields using +* the bitmask. +*/ +static int __write_id_reg (struct kvm_vcpu *vcpu,u32 id,u64 val) +{ +const struct id_reg_info *id_reg =GET_ID_REG_INFO (id); +u64 mask =0 ; + +if (id_reg && id_reg->vcpu_mask) + mask = id_reg->vcpu_mask (vcpu, id_reg); + +/* +* Update the ID register for the guest with @val, except for fields +* that are set in the mask, which indicates fields for opt-in +* features that are not configured for the vCPU. +*/ +return modify_kvm_id_reg (vcpu->kvm, id, val, mask); +} +static u64 __read_id_reg (const struct kvm_vcpu *vcpu,u32 id) { -u64 val = vcpu->kvm->arch.id_regs[IDREG_IDX (id)]; +const struct id_reg_info *id_reg =GET_ID_REG_INFO (id); +u64 val =read_kvm_id_reg (vcpu->kvm, id); + +if (id_reg && id_reg->vcpu_mask) +/* Clear fields for opt-in features that are not configured. */ + val &= ~(id_reg->vcpu_mask (vcpu, id_reg));switch (id) {case SYS_ID_AA64PFR0_EL1 :@@ -1229,12 +1418,7 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,return 0 ; } -/* -* cpufeature ID register user accessors -* -* For now, these registers are immutable for userspace, so for set_id_reg() -* we don't allow the effective value to be changed. -*/ +/* cpufeature ID register user accessors */ static int __get_id_reg (const struct kvm_vcpu *vcpu,const struct sys_reg_desc *rd,void __user *uaddr,bool raz)@@ -1245,11 +1429,31 @@ static int __get_id_reg(const struct kvm_vcpu *vcpu,return reg_to_user (uaddr, &val, id); } -static int __set_id_reg (const struct kvm_vcpu *vcpu, +/* +* Check if the given id indicates AArch32 ID register encoding. +*/ +static bool is_aarch32_id_reg (u32 id) +{ +u32 crm, op2; + +if (!is_id_reg (id)) +return false; + + crm =sys_reg_CRm (id); + op2 =sys_reg_Op2 (id); +if (crm ==1 || crm ==2 || (crm ==3 && (op2 !=3 && op2 !=7 ))) +/* AArch32 ID register */ +return true; + +return false; +} + +static int __set_id_reg (struct kvm_vcpu *vcpu,const struct sys_reg_desc *rd,void __user *uaddr,bool raz) {const u64 id =sys_reg_to_index (rd); +u32 encoding =reg_to_encoding (rd);int err;u64 val;@@ -1257,11 +1461,28 @@ static int __set_id_reg(const struct kvm_vcpu *vcpu,if (err)return err; -/* This is what we mean by invariant: you can't change it. */ -if (val !=read_id_reg (vcpu, rd, raz)) +if (val ==read_id_reg (vcpu, rd, raz)) +/* The value is same as the current value. Nothing to do. */ +return 0 ; + +/* +* Don't allow to modify the register's value if the register is raz, +* or the reg doesn't have the id_reg_info. +*/ +if (raz || !GET_ID_REG_INFO (encoding))return -EINVAL ; -return 0 ; +/* +* Skip the validation of AArch32 ID registers if the system doesn't +* 32bit EL0 (their value are UNKNOWN). +*/ +if (system_supports_32bit_el0 () || !is_aarch32_id_reg (encoding)) { + err =validate_id_reg (vcpu, encoding, val); +if (err) +return err; + } + +return __write_id_reg (vcpu, encoding, val); } static int get_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,@@ -2823,6 +3044,20 @@ int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)return write_demux_regids (uindices); } +static void id_reg_info_init_all (void ) +{ +int i; +struct id_reg_info *id_reg; + +for (i =0 ; i <ARRAY_SIZE (id_reg_info_table); i++) { + id_reg = (struct id_reg_info *)id_reg_info_table[i]; +if (!id_reg) +continue ; + +id_reg_info_init (id_reg); + } +} +void kvm_sys_reg_table_init (void ) {unsigned int i;@@ -2857,6 +3092,8 @@ void kvm_sys_reg_table_init(void)break ;/* Clear all higher bits. */ cache_levels &= (1 << (i*3 ))-1 ; + +id_reg_info_init_all (); }/* @@ -2869,11 +3106,12 @@ void set_default_id_regs(struct kvm *kvm)u32 id;const struct sys_reg_desc *rd;u64 val; +struct id_reg_info *idr;for (i =0 ; i <ARRAY_SIZE (sys_reg_descs); i++) { rd = &sys_reg_descs[i];if (rd->access != access_id_reg) -/* Not ID register, or hidden/reserved ID register */ +/* Not ID register or hidden/reserved ID register */ continue ; id =reg_to_encoding (rd);@@ -2881,7 +3119,21 @@ void set_default_id_regs(struct kvm *kvm)/* Shouldn't happen */ continue ; - val =read_sanitised_ftr_reg (id); - kvm->arch.id_regs[IDREG_IDX (id)] = val; + idr =GET_ID_REG_INFO (id); + val = idr ? idr->vcpu_limit_val :read_sanitised_ftr_reg (id); +WARN_ON_ONCE (write_kvm_id_reg (kvm, id, val)); } } + +/* +* Update the ID register's field with @fval for the guest. +* The caller is expected to hold the kvm->lock. +* This will not fail unless any vCPUs in the guest have started. +*/ +int kvm_set_id_reg_feature (struct kvm *kvm,u32 id,u8 field_shift,u8 fval) +{ +u64 val = ((u64 )fval &ARM64_FEATURE_FIELD_MASK ) << field_shift; +u64 preserve_mask = ~(ARM64_FEATURE_FIELD_MASK << field_shift); + +return __modify_kvm_id_reg (kvm, id, val, preserve_mask); +} --Quoted 2 lines
2.35.1.265.g69c8d7142f-goog
--
Thanks,
Oliver
PATCH v5 4/27 KVM: arm64: Make ID_AA64PFR0_EL1 writable
In reply to Reiji WatanabeThis patch adds id_reg_info for ID_AA64PFR0_EL1 to make it writable by userspace.
Return an error if userspace tries to set SVE/GIC field of the register to a value that conflicts with SVE/GIC configuration for the guest. SIMD/FP/SVE fields of the requested value are validated according to Arm ARM.
--- arch/arm64/include/asm/sysreg.h | 1 + arch/arm64/kvm/sys_regs.c | 150 +++++++++++++++++++------------- arch/arm64/kvm/vgic/vgic-init.c | 9 ++ 3 files changed, 101 insertions(+), 59 deletions(-)
arch/arm64/include/asm/sysreg.h +1 -0
@@ -813,6 +813,7 @@#define ID_AA64PFR0_ASIMD_SUPPORTED 0x0#define ID_AA64PFR0_ELx_64BIT_ONLY 0x1#define ID_AA64PFR0_ELx_32BIT_64BIT 0x2 +#define ID_AA64PFR0_GIC3 0x1/* id_aa64pfr1 */ #define ID_AA64PFR1_MPAMFRAC_SHIFT 16
arch/arm64/kvm/sys_regs.c +91 -59
180 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic-init.c +9 -0
@@ -117,6 +117,15 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)else INIT_LIST_HEAD (&kvm->arch.vgic.rd_regions); +if (type ==KVM_DEV_TYPE_ARM_VGIC_V3 ) +/* +* Set ID_AA64PFR0_EL1.GIC to 1. This shouldn't fail unless +* any vCPU in the guest have started. +*/ +WARN_ON_ONCE (kvm_set_id_reg_feature (kvm,SYS_ID_AA64PFR0_EL1 , +ID_AA64PFR0_GIC3 , +ID_AA64PFR0_GIC_SHIFT )); + out_unlock:unlock_all_vcpus (kvm);return ret;
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
PATCH v5 5/27 KVM: arm64: Make ID_AA64PFR1_EL1 writable
In reply to Reiji WatanabeThis patch adds id_reg_info for ID_AA64PFR1_EL1 to make it writable by userspace.
Return an error if userspace tries to set MTE field of the register to a value that conflicts with KVM_CAP_ARM_MTE configuration for the guest.
Skip fractional feature fields validation at present and they will be handled by the following patches.
--- arch/arm64/include/asm/sysreg.h | 1 + arch/arm64/kvm/sys_regs.c | 42 +++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-)
arch/arm64/include/asm/sysreg.h +1 -0
@@ -816,6 +816,7 @@#define ID_AA64PFR0_GIC3 0x1/* id_aa64pfr1 */ +#define ID_AA64PFR1_CSV2FRAC_SHIFT 32#define ID_AA64PFR1_MPAMFRAC_SHIFT 16#define ID_AA64PFR1_RASFRAC_SHIFT 12#define ID_AA64PFR1_MTE_SHIFT 8
arch/arm64/kvm/sys_regs.c +38 -4
78 lines not on this page · raw message
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
PATCH v5 6/27 KVM: arm64: Make ID_AA64ISAR0_EL1 writable
In reply to Reiji WatanabeThis patch adds id_reg_info for ID_AA64ISAR0_EL1 to make it writable by userspace.
Updating sm3, sm4, sha1, sha2 and sha3 fields are allowed only if values of those fields follow Arm ARM.
--- arch/arm64/kvm/sys_regs.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)
arch/arm64/kvm/sys_regs.c +29 -0
@@ -387,6 +387,29 @@ static int validate_id_aa64pfr1_el1(struct kvm_vcpu *vcpu,return 0 ; } +static int validate_id_aa64isar0_el1 (struct kvm_vcpu *vcpu, +const struct id_reg_info *id_reg,u64 val) +{ +unsigned int sm3, sm4, sha1, sha2, sha3; + +/* Run consistency checkings according to Arm ARM */ + sm3 =cpuid_feature_extract_unsigned_field (val,ID_AA64ISAR0_SM3_SHIFT ); + sm4 =cpuid_feature_extract_unsigned_field (val,ID_AA64ISAR0_SM4_SHIFT ); +if (sm3 != sm4) +return -EINVAL ; + + sha1 =cpuid_feature_extract_unsigned_field (val,ID_AA64ISAR0_SHA1_SHIFT ); + sha2 =cpuid_feature_extract_unsigned_field (val,ID_AA64ISAR0_SHA2_SHIFT ); +if ((sha1 ==0 ) ^ (sha2 ==0 )) +return -EINVAL ; + + sha3 =cpuid_feature_extract_unsigned_field (val,ID_AA64ISAR0_SHA3_SHIFT ); +if (((sha2 ==2 ) ^ (sha3 ==1 )) || (!sha1 && sha3)) +return -EINVAL ; + +return 0 ; +} +static void init_id_aa64pfr0_el1_info (struct id_reg_info *id_reg) {u64 limit = id_reg->vcpu_limit_val;
@@ -454,6 +477,11 @@ static struct id_reg_info id_aa64pfr1_el1_info = { .vcpu_mask = vcpu_mask_id_aa64pfr1_el1, }; +static struct id_reg_info id_aa64isar0_el1_info = { + .sys_reg =SYS_ID_AA64ISAR0_EL1 , + .validate = validate_id_aa64isar0_el1, +}; +/* * An ID register that needs special handling to control the value for the * guest must have its own id_reg_info in id_reg_info_table.
@@ -465,6 +493,7 @@ static struct id_reg_info id_aa64pfr1_el1_info = {static struct id_reg_info *id_reg_info_table[KVM_ARM_ID_REG_MAX_NUM ] = { [IDREG_IDX (SYS_ID_AA64PFR0_EL1 )] = &id_aa64pfr0_el1_info, [IDREG_IDX (SYS_ID_AA64PFR1_EL1 )] = &id_aa64pfr1_el1_info, + [IDREG_IDX (SYS_ID_AA64ISAR0_EL1 )] = &id_aa64isar0_el1_info, };static int validate_id_reg (struct kvm_vcpu *vcpu,u32 id,u64 val)
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
PATCH v5 7/27 KVM: arm64: Make ID_AA64ISAR1_EL1 writable
In reply to Reiji WatanabeThis patch adds id_reg_info for ID_AA64ISAR1_EL1 to make it writable by userspace.
Return an error if userspace tries to set PTRAUTH related fields of the register to values that conflict with PTRAUTH configuration, which was configured by KVM_ARM_VCPU_INIT, for the guest.
--- arch/arm64/kvm/sys_regs.c | 77 +++++++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 8 deletions(-)
arch/arm64/kvm/sys_regs.c +69 -8
@@ -265,6 +265,24 @@ static bool trap_raz_wi(struct kvm_vcpu *vcpu,return read_zero (vcpu, p); } +#define PTRAUTH_MASK (ARM64_FEATURE_MASK(ID_AA64ISAR1_APA) | \ + ARM64_FEATURE_MASK(ID_AA64ISAR1_API) | \ + ARM64_FEATURE_MASK(ID_AA64ISAR1_GPA) | \ + ARM64_FEATURE_MASK(ID_AA64ISAR1_GPI)) + +#define aa64isar1_has_apa (val) \ + (cpuid_feature_extract_unsigned_field(val, ID_AA64ISAR1_APA_SHIFT) >= \ + ID_AA64ISAR1_APA_ARCHITECTED) +#define aa64isar1_has_api (val) \ + (cpuid_feature_extract_unsigned_field(val, ID_AA64ISAR1_API_SHIFT) >= \ + ID_AA64ISAR1_API_IMP_DEF) +#define aa64isar1_has_gpa (val) \ + (cpuid_feature_extract_unsigned_field(val, ID_AA64ISAR1_GPA_SHIFT) >= \ + ID_AA64ISAR1_GPA_ARCHITECTED) +#define aa64isar1_has_gpi (val) \ + (cpuid_feature_extract_unsigned_field(val, ID_AA64ISAR1_GPI_SHIFT) >= \ + ID_AA64ISAR1_GPI_IMP_DEF) +struct id_reg_info {/* Register ID */ u32 sys_reg;
@@ -410,6 +428,36 @@ static int validate_id_aa64isar0_el1(struct kvm_vcpu *vcpu,return 0 ; } +static int validate_id_aa64isar1_el1 (struct kvm_vcpu *vcpu, +const struct id_reg_info *id_reg,u64 val) +{ +bool has_gpi, has_gpa, has_api, has_apa; +bool generic, address; + + has_gpi =aa64isar1_has_gpi (val); + has_gpa =aa64isar1_has_gpa (val); + has_api =aa64isar1_has_api (val); + has_apa =aa64isar1_has_apa (val); +if ((has_gpi && has_gpa) || (has_api && has_apa)) +return -EINVAL ; + + generic = has_gpi || has_gpa; + address = has_api || has_apa; +/* +* Since the current KVM guest implementation works by enabling +* both address/generic pointer authentication features, +* return an error if they conflict. +*/ +if (generic ^ address) +return -EPERM ; + +/* Check if there is a conflict with a request via KVM_ARM_VCPU_INIT */ +if (vcpu_has_ptrauth (vcpu) ^ (generic && address)) +return -EPERM ; + +return 0 ; +} +static void init_id_aa64pfr0_el1_info (struct id_reg_info *id_reg) {u64 limit = id_reg->vcpu_limit_val;
@@ -447,8 +495,14 @@ static void init_id_aa64pfr1_el1_info(struct id_reg_info *id_reg) id_reg->vcpu_limit_val &= ~ARM64_FEATURE_MASK (ID_AA64PFR1_MTE ); } +static void init_id_aa64isar1_el1_info (struct id_reg_info *id_reg) +{ +if (!system_has_full_ptr_auth ()) + id_reg->vcpu_limit_val &= ~PTRAUTH_MASK ; +} +static u64 vcpu_mask_id_aa64pfr0_el1 (const struct kvm_vcpu *vcpu, -const struct id_reg_info *idr) +const struct id_reg_info *idr) {return vcpu_has_sve (vcpu) ?0 :ARM64_FEATURE_MASK (ID_AA64PFR0_SVE ); }
@@ -459,6 +513,12 @@ static u64 vcpu_mask_id_aa64pfr1_el1(const struct kvm_vcpu *vcpu,return kvm_has_mte (vcpu->kvm) ?0 : (ARM64_FEATURE_MASK (ID_AA64PFR1_MTE )); } +static u64 vcpu_mask_id_aa64isar1_el1 (const struct kvm_vcpu *vcpu, +const struct id_reg_info *idr) +{ +return vcpu_has_ptrauth (vcpu) ?0 :PTRAUTH_MASK ; +} +static struct id_reg_info id_aa64pfr0_el1_info = { .sys_reg =SYS_ID_AA64PFR0_EL1 , .ignore_mask =ARM64_FEATURE_MASK (ID_AA64PFR0_GIC ),
@@ -482,6 +542,13 @@ static struct id_reg_info id_aa64isar0_el1_info = { .validate = validate_id_aa64isar0_el1, }; +static struct id_reg_info id_aa64isar1_el1_info = { + .sys_reg =SYS_ID_AA64ISAR1_EL1 , + .init = init_id_aa64isar1_el1_info, + .validate = validate_id_aa64isar1_el1, + .vcpu_mask = vcpu_mask_id_aa64isar1_el1, +}; +/* * An ID register that needs special handling to control the value for the * guest must have its own id_reg_info in id_reg_info_table.
@@ -494,6 +561,7 @@ static struct id_reg_info *id_reg_info_table[KVM_ARM_ID_REG_MAX_NUM] = { [IDREG_IDX(SYS_ID_AA64PFR0_EL1)] = &id_aa64pfr0_el1_info, [IDREG_IDX(SYS_ID_AA64PFR1_EL1)] = &id_aa64pfr1_el1_info, [IDREG_IDX(SYS_ID_AA64ISAR0_EL1)] = &id_aa64isar0_el1_info, + [IDREG_IDX(SYS_ID_AA64ISAR1_EL1)] = &id_aa64isar1_el1_info, };static int validate_id_reg (struct kvm_vcpu *vcpu,u32 id,u64 val)
@@ -1418,13 +1486,6 @@ static u64 __read_id_reg(const struct kvm_vcpu *vcpu, u32 id) val &= ~(id_reg->vcpu_mask (vcpu, id_reg));switch (id) { -case SYS_ID_AA64ISAR1_EL1 : -if (!vcpu_has_ptrauth (vcpu)) - val &= ~(ARM64_FEATURE_MASK (ID_AA64ISAR1_APA ) | -ARM64_FEATURE_MASK (ID_AA64ISAR1_API ) | -ARM64_FEATURE_MASK (ID_AA64ISAR1_GPA ) | -ARM64_FEATURE_MASK (ID_AA64ISAR1_GPI )); -break ;case SYS_ID_AA64DFR0_EL1 :/* Limit debug to ARMv8.0 */ val &= ~ARM64_FEATURE_MASK (ID_AA64DFR0_DEBUGVER );
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
PATCH v5 8/27 KVM: arm64: Make ID_AA64MMFR0_EL1 writable
In reply to Reiji WatanabeThis patch adds id_reg_info for ID_AA64MMFR0_EL1 to make it writable by userspace.
Since ID_AA64MMFR0_EL1 stage 2 granule size fields don't follow the standard ID scheme, we need a special handling to validate those fields.
--- arch/arm64/kvm/sys_regs.c | 127 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+)
arch/arm64/kvm/sys_regs.c +127 -0
@@ -458,6 +458,118 @@ static int validate_id_aa64isar1_el1(struct kvm_vcpu *vcpu,return 0 ; } +/* +* Check if the requested stage2 translation granule size indicated in +* @mmfr0 is also indicated in @mmfr0_lim. +* If TGranX_2 field is zero, the value must be validated based on TGranX +* field because that indicates the feature support is identified in +* TGranX field. +* This function relies on the fact TGranX fields are validated before +* through arm64_check_features_kvm. +*/ +static int aa64mmfr0_tgran2_check (int field,u64 mmfr0,u64 mmfr0_lim) +{ +s64 tgran2, lim_tgran2, rtgran1; +int f1; +bool is_signed; + + tgran2 =cpuid_feature_extract_unsigned_field (mmfr0, field); + lim_tgran2 =cpuid_feature_extract_unsigned_field (mmfr0_lim, field); +if (tgran2 && lim_tgran2) +/* +* We don't need to check TGranX field. We can simply +* compare tgran2 and lim_tgran2. +*/ +return (tgran2 > lim_tgran2) ? -E2BIG :0 ; + +if (tgran2 == lim_tgran2) +/* +* Both of them are zero. Since TGranX in @mmfr0 is already +* validated by arm64_check_features_kvm, tgran2 must be fine. +*/ +return 0 ; + +/* +* Either tgran2 or lim_tgran2 is zero. +* Need stage1 granule size to validate tgran2. +*/ + +/* +* Get TGranX's bit position by subtracting 12 from TGranX_2's bit +* position. +*/ + f1 = field -12 ; + +/* TGran4/TGran64 is signed and TGran16 is unsigned field. */ + is_signed = (f1 ==ID_AA64MMFR0_TGRAN16_SHIFT ) ? false : true; + +/* +* If tgran2 == 0 (&& lim_tgran2 != 0), the requested stage2 granule +* size is indicated in the stage1 granule size field of @mmfr0. +* So, validate the stage1 granule size against the stage2 limit +* granule size. +* If lim_tgran2 == 0 (&& tgran2 != 0), the stage2 limit granule size +* is indicated in the stage1 granule size field of @mmfr0_lim. +* So, validate the requested stage2 granule size against the stage1 +* limit granule size. +*/ + +/* Get the relevant stage1 granule size to validate tgran2 */ +if (tgran2 ==0 ) +/* The requested stage1 granule size */ + rtgran1 =cpuid_feature_extract_field (mmfr0, f1, is_signed); +else /* lim_tgran2 == 0 */ +/* The stage1 limit granule size */ + rtgran1 =cpuid_feature_extract_field (mmfr0_lim, f1, is_signed); + +/* +* Adjust the value of rtgran1 to compare with stage2 granule size, +* which indicates: 1: Not supported, 2: Supported, etc. +*/ +if (is_signed) +/* For signed, -1: Not supported, 0: Supported, etc. */ + rtgran1 +=0x2 ; +else +/* For unsigned, 0: Not supported, 1: Supported, etc. */ + rtgran1 +=0x1 ; + +if ((tgran2 ==0 ) && (rtgran1 > lim_tgran2)) +/* +* The requested stage1 granule size (== the requested stage2 +* granule size) is larger than the stage2 limit granule size. +*/ +return -E2BIG ; +else if ((lim_tgran2 ==0 ) && (tgran2 > rtgran1)) +/* +* The requested stage2 granule size is larger than the stage1 +* limit granulze size (== the stage2 limit granule size). +*/ +return -E2BIG ; + +return 0 ; +} + +static int validate_id_aa64mmfr0_el1 (struct kvm_vcpu *vcpu, +const struct id_reg_info *id_reg,u64 val) +{ +u64 limit = id_reg->vcpu_limit_val; +int ret; + + ret =aa64mmfr0_tgran2_check (ID_AA64MMFR0_TGRAN4_2_SHIFT , val, limit); +if (ret) +return ret; + + ret =aa64mmfr0_tgran2_check (ID_AA64MMFR0_TGRAN64_2_SHIFT , val, limit); +if (ret) +return ret; + + ret =aa64mmfr0_tgran2_check (ID_AA64MMFR0_TGRAN16_2_SHIFT , val, limit); +if (ret) +return ret; + +return 0 ; +} +static void init_id_aa64pfr0_el1_info (struct id_reg_info *id_reg) {u64 limit = id_reg->vcpu_limit_val;
@@ -549,6 +661,20 @@ static struct id_reg_info id_aa64isar1_el1_info = { .vcpu_mask = vcpu_mask_id_aa64isar1_el1, }; +static struct id_reg_info id_aa64mmfr0_el1_info = { + .sys_reg =SYS_ID_AA64MMFR0_EL1 , +/* +* When TGranX_2 value is 0, validity of the value depend on TGranX +* value, and TGranX_2 value must be validated against TGranX value, +* which is done by validate_id_aa64mmfr0_el1. +* So, skip the regular validity checking for TGranX_2 fields. +*/ + .ignore_mask =ARM64_FEATURE_MASK (ID_AA64MMFR0_TGRAN4_2 ) | +ARM64_FEATURE_MASK (ID_AA64MMFR0_TGRAN64_2 ) | +ARM64_FEATURE_MASK (ID_AA64MMFR0_TGRAN16_2 ), + .validate = validate_id_aa64mmfr0_el1, +}; +/* * An ID register that needs special handling to control the value for the * guest must have its own id_reg_info in id_reg_info_table.
@@ -562,6 +688,7 @@ static struct id_reg_info *id_reg_info_table[KVM_ARM_ID_REG_MAX_NUM] = { [IDREG_IDX(SYS_ID_AA64PFR1_EL1)] = &id_aa64pfr1_el1_info, [IDREG_IDX(SYS_ID_AA64ISAR0_EL1)] = &id_aa64isar0_el1_info, [IDREG_IDX(SYS_ID_AA64ISAR1_EL1)] = &id_aa64isar1_el1_info, + [IDREG_IDX(SYS_ID_AA64MMFR0_EL1)] = &id_aa64mmfr0_el1_info, };static int validate_id_reg (struct kvm_vcpu *vcpu,u32 id,u64 val)
Signature · 6 lines
2.35.1.265.g69c8d7142f-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v5 9/27 KVM: arm64: Make ID_AA64MMFR1_EL1 writable
In reply to Reiji WatanabeThis patch adds id_reg_info for ID_AA64MMFR1_EL1 to make it writable by userspace.
Hardware update of Access flag and/or Dirty state in translation table needs to be disabled for the guest to let userspace set ID_AA64MMFR1_EL1.HAFDBS field to a lower value. It requires trapping the guest's accessing TCR_EL1, which KVM doesn't always do (in order to trap it without FEAT_FGT, HCR_EL2.TVM needs to be set to 1, which also traps many other virtual memory control registers).
So, userspace won't be allowed to modify the value of the HAFDBS field.
--- arch/arm64/kvm/sys_regs.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
arch/arm64/kvm/sys_regs.c +30 -0
@@ -570,6 +570,30 @@ static int validate_id_aa64mmfr0_el1(struct kvm_vcpu *vcpu,return 0 ; } +static int validate_id_aa64mmfr1_el1 (struct kvm_vcpu *vcpu, +const struct id_reg_info *id_reg,u64 val) +{ +u64 limit = id_reg->vcpu_limit_val; +unsigned int hafdbs, lim_hafdbs; + + hafdbs =cpuid_feature_extract_unsigned_field (val,ID_AA64MMFR1_HADBS_SHIFT ); + lim_hafdbs =cpuid_feature_extract_unsigned_field (limit,ID_AA64MMFR1_HADBS_SHIFT ); + +/* +* Don't allow userspace to modify the value of HAFDBS. +* Hardware update of Access flag and/or Dirty state in translation +* table needs to be disabled for the guest to let userspace set +* HAFDBS field to a lower value. It requires trapping the guest's +* accessing TCR_EL1, which KVM doesn't always do (in order to trap +* it without FEAT_FGT, HCR_EL2.TVM needs to be set to 1, which also +* traps many other virtual memory control registers). +*/ +if (hafdbs != lim_hafdbs) +return -EINVAL ; + +return 0 ; +} +static void init_id_aa64pfr0_el1_info (struct id_reg_info *id_reg) {u64 limit = id_reg->vcpu_limit_val;
@@ -675,6 +699,11 @@ static struct id_reg_info id_aa64mmfr0_el1_info = { .validate = validate_id_aa64mmfr0_el1, }; +static struct id_reg_info id_aa64mmfr1_el1_info = { + .sys_reg =SYS_ID_AA64MMFR1_EL1 , + .validate = validate_id_aa64mmfr1_el1, +}; +/* * An ID register that needs special handling to control the value for the * guest must have its own id_reg_info in id_reg_info_table.
@@ -689,6 +718,7 @@ static struct id_reg_info *id_reg_info_table[KVM_ARM_ID_REG_MAX_NUM] = { [IDREG_IDX(SYS_ID_AA64ISAR0_EL1)] = &id_aa64isar0_el1_info, [IDREG_IDX(SYS_ID_AA64ISAR1_EL1)] = &id_aa64isar1_el1_info, [IDREG_IDX(SYS_ID_AA64MMFR0_EL1)] = &id_aa64mmfr0_el1_info, + [IDREG_IDX(SYS_ID_AA64MMFR1_EL1)] = &id_aa64mmfr1_el1_info, };static int validate_id_reg (struct kvm_vcpu *vcpu,u32 id,u64 val)
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
Re: [PATCH v5 09/27] KVM: arm64: Make ID_AA64MMFR1_EL1 writable
Hi Reiji,
Quoted 34 lines · Reiji Watanabe
This patch adds
id_reg_infoforID_AA64MMFR1_EL1to make it writable by userspace.Hardware update of Access flag and/or Dirty state in translation table needs to be disabled for the guest to let userspace set ID_AA64MMFR1_EL1.HAFDBS field to a lower value. It requires trapping the guest's accessing
TCR_EL1, which KVM doesn't always do (in order to trap it withoutFEAT_FGT, HCR_EL2.TVM needs to be set to 1, which also traps many other virtual memory control registers).
So, userspace won't be allowed to modify the value of the HAFDBS field.Signed-off-by Reiji Watanabe <[email protected]>--- arch/arm64/kvm/sys_regs.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)arch/arm64/kvm/sys_regs.c +10
-0@@ -570,6 +570,30 @@ static int validate_id_aa64mmfr0_el1(struct kvm_vcpu *vcpu,return 0 ; } +static int validate_id_aa64mmfr1_el1 (struct kvm_vcpu *vcpu, +const struct id_reg_info *id_reg,u64 val) +{ +u64 limit = id_reg->vcpu_limit_val; +unsigned int hafdbs, lim_hafdbs; + + hafdbs =cpuid_feature_extract_unsigned_field (val,ID_AA64MMFR1_HADBS_SHIFT ); + lim_hafdbs =cpuid_feature_extract_unsigned_field (limit,ID_AA64MMFR1_HADBS_SHIFT ); + +/*
arch/arm64/kvm/sys_regs.c static int validate_id_aa64mmfr0_el1(struct kvm_vcpu *vcpu, · line 592
Quoted 10 lines
arch/arm64/kvm/sys_regs.c +10
-0+* Don't allow userspace to modify the value of HAFDBS. +* Hardware update of Access flag and/or Dirty state in translation +* table needs to be disabled for the guest to let userspace set +* HAFDBS field to a lower value. It requires trapping the guest's +* accessing TCR_EL1, which KVM doesn't always do (in order to trap +* it without FEAT_FGT, HCR_EL2.TVM needs to be set to 1, which also +* traps many other virtual memory control registers). +*/ +if (hafdbs != lim_hafdbs) +return -EINVAL ;
Are we going to require that any hidden feature be trappable going forward? It doesn't seem to me like there's much risk to userspace hiding any arbitrary feature so long as identity remains architectural.
Another example of this is AArch32 at EL0. Without FGT, there is no precise trap for KVM to intervene and prevent an AArch32 EL0. Nonetheless, userspace might still want to hide this from its guests even if a misbehaved guest could still use it.
--
Thanks,
Oliver
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v5 09/27] KVM: arm64: Make ID_AA64MMFR1_EL1 writable
Hi Oliver,
Quoted 44 lines · Oliver Upton
Hi Reiji,
Quoted 40 lines · Reiji Watanabe
This patch adds
id_reg_infoforID_AA64MMFR1_EL1to make it writable by userspace.Hardware update of Access flag and/or Dirty state in translation table needs to be disabled for the guest to let userspace set ID_AA64MMFR1_EL1.HAFDBS field to a lower value. It requires trapping the guest's accessing
TCR_EL1, which KVM doesn't always do (in order to trap it withoutFEAT_FGT, HCR_EL2.TVM needs to be set to 1, which also traps many other virtual memory control registers).
So, userspace won't be allowed to modify the value of the HAFDBS field.Signed-off-by Reiji Watanabe <[email protected]>--- arch/arm64/kvm/sys_regs.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)arch/arm64/kvm/sys_regs.c +16
-0@@ -570,6 +570,30 @@ static int validate_id_aa64mmfr0_el1(struct kvm_vcpu *vcpu,return 0 ; } +static int validate_id_aa64mmfr1_el1 (struct kvm_vcpu *vcpu, +const struct id_reg_info *id_reg,u64 val) +{ +u64 limit = id_reg->vcpu_limit_val; +unsigned int hafdbs, lim_hafdbs; + + hafdbs =cpuid_feature_extract_unsigned_field (val,ID_AA64MMFR1_HADBS_SHIFT ); + lim_hafdbs =cpuid_feature_extract_unsigned_field (limit,ID_AA64MMFR1_HADBS_SHIFT ); + +/* +* Don't allow userspace to modify the value of HAFDBS. +* Hardware update of Access flag and/or Dirty state in translation +* table needs to be disabled for the guest to let userspace set +* HAFDBS field to a lower value. It requires trapping the guest's +* accessing TCR_EL1, which KVM doesn't always do (in order to trap +* it without FEAT_FGT, HCR_EL2.TVM needs to be set to 1, which also
Quoted 8 lines
arch/arm64/kvm/sys_regs.c static int validate_id_aa64mmfr0_el1(struct kvm_vcpu *vcpu, · line 592
Quoted 4 lines
arch/arm64/kvm/sys_regs.c +4
-0+* traps many other virtual memory control registers). +*/ +if (hafdbs != lim_hafdbs) +return -EINVAL ;Are we going to require that any hidden feature be trappable going forward? It doesn't seem to me like there's much risk to userspace hiding any arbitrary feature so long as identity remains architectural.
That wasn't my intention, and I will drop this patch. (I wonder why I thought this was needed...)
Thank you for catching this !
Regards,
Reiji
Quoted 9 lines
Another example of this is AArch32 at EL0. Without FGT, there is no precise trap for KVM to intervene and prevent an AArch32 EL0. Nonetheless, userspace might still want to hide this from its guests even if a misbehaved guest could still use it.
--
Thanks,
Oliver
PATCH v5 10/27 KVM: arm64: Hide IMPLEMENTATION DEFINED PMU support for the guest
In reply to Reiji WatanabeWhen ID_AA64DFR0_EL1.PMUVER or ID_DFR0_EL1.PERFMON is 0xf, which means IMPLEMENTATION DEFINED PMU supported, KVM unconditionally expose the value for the guest as it is. Since KVM doesn't support IMPLEMENTATION DEFINED PMU for the guest, in that case KVM should expose 0x0 (PMU is not implemented) instead.
Change cpuid_feature_cap_perfmon_field() to update the field value to 0x0 when it is 0xf.
--- arch/arm64/include/asm/cpufeature.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/cpufeature.h +1 -1
@@ -553,7 +553,7 @@ cpuid_feature_cap_perfmon_field(u64 features, int field, u64 cap)/* Treat IMPLEMENTATION DEFINED functionality as unimplemented */ if (val ==ID_AA64DFR0_PMUVER_IMP_DEF ) - val =0 ; +return (features & ~mask);if (val > cap) { features &= ~mask;
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
Re: [PATCH v5 10/27] KVM: arm64: Hide IMPLEMENTATION DEFINED PMU support for the guest
Hi Reiji,
Quoted 8 lines · Reiji Watanabe
When ID_AA64DFR0_EL1.PMUVER or ID_DFR0_EL1.PERFMON is
0xf, which means IMPLEMENTATION DEFINED PMU supported, KVM unconditionally expose the value for the guest as it is. Since KVM doesn't support IMPLEMENTATION DEFINED PMU for the guest, in that case KVM should expose0x0(PMU is not implemented) instead.Change
cpuid_feature_cap_perfmon_field()to update the field value to0x0when it is0xf.
Definitely agree with the change in this patch. Do we need to tolerate writes of 0xf for ABI compatibility (even if it is nonsensical)? Otherwise a guest with IMP_DEF PMU cannot be migrated to a newer kernel.
--
Thanks,
Oliver
Re: [PATCH v5 10/27] KVM: arm64: Hide IMPLEMENTATION DEFINED PMU support for the guest
Hi Oliver,
Thank you for the review!
Quoted 10 lines · Oliver Upton
Hi Reiji,
Quoted 6 lines · Reiji Watanabe
When ID_AA64DFR0_EL1.PMUVER or ID_DFR0_EL1.PERFMON is
0xf, which means IMPLEMENTATION DEFINED PMU supported, KVM unconditionally expose the value for the guest as it is. Since KVM doesn't support IMPLEMENTATION DEFINED PMU for the guest, in that case KVM should expose0x0(PMU is not implemented) instead.
Quoted 6 lines
Quoted 2 lines
Change
cpuid_feature_cap_perfmon_field()to update the field value to0x0when it is0xf.Definitely agree with the change in this patch. Do we need to tolerate writes of
0xffor ABI compatibility (even if it is nonsensical)? Otherwise a guest withIMP_DEFPMU cannot be migrated to a newer kernel.
Hmm, yes, I think KVM should tolerate writes of 0xf so that we can avoid the migration failure. I will make this change in v6.
Since ID registers are immutable with the current KVM, I think a live migration failure to a newer kernel happens when the newer kernel/KVM supports more CPU features (or when an ID register field is newly masked or capped by KVM, etc). So, I would assume such migration breakage on KVM/ARM has been introduced from time to time though.
Thanks,
Reiji
Re: [PATCH v5 10/27] KVM: arm64: Hide IMPLEMENTATION DEFINED PMU support for the guest
Quoted 22 lines · Reiji Watanabe
Hi Oliver,
Thank you for the review!
Quoted 10 lines · Oliver Upton
Hi Reiji,
Quoted 6 lines · Reiji Watanabe
When ID_AA64DFR0_EL1.PMUVER or ID_DFR0_EL1.PERFMON is
0xf, which means IMPLEMENTATION DEFINED PMU supported, KVM unconditionally expose the value for the guest as it is. Since KVM doesn't support IMPLEMENTATION DEFINED PMU for the guest, in that case KVM should expose0x0(PMU is not implemented) instead.Quoted 6 lines
Quoted 2 lines
Change
cpuid_feature_cap_perfmon_field()to update the field value to0x0when it is0xf.Definitely agree with the change in this patch. Do we need to tolerate writes of
0xffor ABI compatibility (even if it is nonsensical)? Otherwise a guest withIMP_DEFPMU cannot be migrated to a newer kernel.
Quoted 9 lines
Hmm, yes, I think KVM should tolerate writes of
0xfso that we can avoid the migration failure. I will make this change in v6.Since ID registers are immutable with the current KVM, I think a live migration failure to a newer kernel happens when the newer kernel/KVM supports more CPU features (or when an ID register field is newly masked or capped by KVM, etc). So, I would assume such migration breakage on KVM/ARM has been introduced from time to time though.
Indeed it has, but IMO migration breakage should really be avoided at all costs. End of the day, its ABI breakage.
Unless folks feel otherwise, I would be in favor of just ignoring the IMP_DEF write and setting the field value to NI instead. Allows VMs to migrate onto newer kernels and fixes the KVM bug at the same time.
--
Oliver
PATCH v5 11/27 KVM: arm64: Make ID_AA64DFR0_EL1 writable
In reply to Reiji WatanabeThis patch adds id_reg_info for ID_AA64DFR0_EL1 to make it writable by userspace.
Return an error if userspace tries to set PMUVER field of the register to a value that conflicts with the PMU configuration.
Since number of context-aware breakpoints must be no more than number of supported breakpoints according to Arm ARM, return an error if userspace tries to set CTX_CMPS field to such value.
--- arch/arm64/kvm/sys_regs.c | 83 +++++++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 12 deletions(-)
arch/arm64/kvm/sys_regs.c +71 -12
@@ -594,6 +594,45 @@ static int validate_id_aa64mmfr1_el1(struct kvm_vcpu *vcpu,return 0 ; } +static bool id_reg_has_pmu (u64 val,u64 shift,unsigned int min) +{ +unsigned int pmu =cpuid_feature_extract_unsigned_field (val, shift); + +/* +* Treat IMPLEMENTATION DEFINED functionality as unimplemented for +* ID_AA64DFR0_EL1.PMUVer/ID_DFR0_EL1.PerfMon. +*/ +if (pmu ==0xf ) + pmu =0 ; + +return (pmu >= min); +} + +static int validate_id_aa64dfr0_el1 (struct kvm_vcpu *vcpu, +const struct id_reg_info *id_reg,u64 val) +{ +unsigned int brps, ctx_cmps; +bool vcpu_pmu, dfr0_pmu; + + brps =cpuid_feature_extract_unsigned_field (val,ID_AA64DFR0_BRPS_SHIFT ); + ctx_cmps =cpuid_feature_extract_unsigned_field (val,ID_AA64DFR0_CTX_CMPS_SHIFT ); + +/* +* Number of context-aware breakpoints can be no more than number of +* supported breakpoints. +*/ +if (ctx_cmps > brps) +return -EINVAL ; + + vcpu_pmu =kvm_vcpu_has_pmu (vcpu); + dfr0_pmu =id_reg_has_pmu (val,ID_AA64DFR0_PMUVER_SHIFT ,ID_AA64DFR0_PMUVER_8_0 ); +/* Check if there is a conflict with a request via KVM_ARM_VCPU_INIT */ +if (vcpu_pmu ^ dfr0_pmu) +return -EPERM ; + +return 0 ; +} +static void init_id_aa64pfr0_el1_info (struct id_reg_info *id_reg) {u64 limit = id_reg->vcpu_limit_val;
@@ -637,8 +676,25 @@ static void init_id_aa64isar1_el1_info(struct id_reg_info *id_reg) id_reg->vcpu_limit_val &= ~PTRAUTH_MASK ; } +static void init_id_aa64dfr0_el1_info (struct id_reg_info *id_reg) +{ +u64 limit = id_reg->vcpu_limit_val; + +/* Limit guests to PMUv3 for ARMv8.4 */ + limit =cpuid_feature_cap_perfmon_field (limit,ID_AA64DFR0_PMUVER_SHIFT , +ID_AA64DFR0_PMUVER_8_4 ); +/* Limit debug to ARMv8.0 */ + limit &= ~ARM64_FEATURE_MASK (ID_AA64DFR0_DEBUGVER ); + limit |= (FIELD_PREP (ARM64_FEATURE_MASK (ID_AA64DFR0_DEBUGVER ),6 )); + +/* Hide SPE from guests */ + limit &= ~ARM64_FEATURE_MASK (ID_AA64DFR0_PMSVER ); + + id_reg->vcpu_limit_val = limit; +} +static u64 vcpu_mask_id_aa64pfr0_el1 (const struct kvm_vcpu *vcpu, -const struct id_reg_info *idr) +const struct id_reg_info *idr) {return vcpu_has_sve (vcpu) ?0 :ARM64_FEATURE_MASK (ID_AA64PFR0_SVE ); }
@@ -655,6 +711,12 @@ static u64 vcpu_mask_id_aa64isar1_el1(const struct kvm_vcpu *vcpu,return vcpu_has_ptrauth (vcpu) ?0 :PTRAUTH_MASK ; } +static u64 vcpu_mask_id_aa64dfr0_el1 (const struct kvm_vcpu *vcpu, +const struct id_reg_info *idr) +{ +return kvm_vcpu_has_pmu (vcpu) ?0 :ARM64_FEATURE_MASK (ID_AA64DFR0_PMUVER ); +} +static struct id_reg_info id_aa64pfr0_el1_info = { .sys_reg =SYS_ID_AA64PFR0_EL1 , .ignore_mask =ARM64_FEATURE_MASK (ID_AA64PFR0_GIC ),
@@ -704,6 +766,13 @@ static struct id_reg_info id_aa64mmfr1_el1_info = { .validate = validate_id_aa64mmfr1_el1, }; +static struct id_reg_info id_aa64dfr0_el1_info = { + .sys_reg =SYS_ID_AA64DFR0_EL1 , + .init = init_id_aa64dfr0_el1_info, + .validate = validate_id_aa64dfr0_el1, + .vcpu_mask = vcpu_mask_id_aa64dfr0_el1, +}; +/* * An ID register that needs special handling to control the value for the * guest must have its own id_reg_info in id_reg_info_table.
@@ -715,6 +784,7 @@ static struct id_reg_info id_aa64mmfr1_el1_info = {static struct id_reg_info *id_reg_info_table[KVM_ARM_ID_REG_MAX_NUM ] = { [IDREG_IDX (SYS_ID_AA64PFR0_EL1 )] = &id_aa64pfr0_el1_info, [IDREG_IDX (SYS_ID_AA64PFR1_EL1 )] = &id_aa64pfr1_el1_info, + [IDREG_IDX (SYS_ID_AA64DFR0_EL1 )] = &id_aa64dfr0_el1_info, [IDREG_IDX (SYS_ID_AA64ISAR0_EL1 )] = &id_aa64isar0_el1_info, [IDREG_IDX (SYS_ID_AA64ISAR1_EL1 )] = &id_aa64isar1_el1_info, [IDREG_IDX (SYS_ID_AA64MMFR0_EL1 )] = &id_aa64mmfr0_el1_info,
@@ -1643,17 +1713,6 @@ static u64 __read_id_reg(const struct kvm_vcpu *vcpu, u32 id) val &= ~(id_reg->vcpu_mask (vcpu, id_reg));switch (id) { -case SYS_ID_AA64DFR0_EL1 : -/* Limit debug to ARMv8.0 */ - val &= ~ARM64_FEATURE_MASK (ID_AA64DFR0_DEBUGVER ); - val |=FIELD_PREP (ARM64_FEATURE_MASK (ID_AA64DFR0_DEBUGVER ),6 ); -/* Limit guests to PMUv3 for ARMv8.4 */ - val =cpuid_feature_cap_perfmon_field (val, -ID_AA64DFR0_PMUVER_SHIFT , -kvm_vcpu_has_pmu (vcpu) ?ID_AA64DFR0_PMUVER_8_4 :0 ); -/* Hide SPE from guests */ - val &= ~ARM64_FEATURE_MASK (ID_AA64DFR0_PMSVER ); -break ;case SYS_ID_DFR0_EL1 :/* Limit guests to PMUv3 for ARMv8.4 */ val = cpuid_feature_cap_perfmon_field(val,
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
PATCH v5 12/27 KVM: arm64: Make ID_DFR0_EL1 writable
In reply to Reiji WatanabeThis patch adds id_reg_info for ID_DFR0_EL1 to make it writable by userspace.
Return an error if userspace tries to set PerfMon field of the register to a value that conflicts with the PMU configuration.
--- arch/arm64/kvm/sys_regs.c | 55 ++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 10 deletions(-)
arch/arm64/kvm/sys_regs.c +45 -10
@@ -633,6 +633,27 @@ static int validate_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,return 0 ; } +static int validate_id_dfr0_el1 (struct kvm_vcpu *vcpu, +const struct id_reg_info *id_reg,u64 val) +{ +bool vcpu_pmu, dfr0_pmu; +unsigned int perfmon; + + perfmon =cpuid_feature_extract_unsigned_field (val,ID_DFR0_PERFMON_SHIFT ); +if (perfmon ==1 || perfmon ==2 ) +/* PMUv1 or PMUv2 is not allowed on ARMv8. */ +return -EINVAL ; + + vcpu_pmu =kvm_vcpu_has_pmu (vcpu); + dfr0_pmu =id_reg_has_pmu (val,ID_DFR0_PERFMON_SHIFT ,ID_DFR0_PERFMON_8_0 ); + +/* Check if there is a conflict with a request via KVM_ARM_VCPU_INIT */ +if (vcpu_pmu ^ dfr0_pmu) +return -EPERM ; + +return 0 ; +} +static void init_id_aa64pfr0_el1_info (struct id_reg_info *id_reg) {u64 limit = id_reg->vcpu_limit_val;
@@ -693,8 +714,17 @@ static void init_id_aa64dfr0_el1_info(struct id_reg_info *id_reg) id_reg->vcpu_limit_val = limit; } +static void init_id_dfr0_el1_info (struct id_reg_info *id_reg) +{ +/* Limit guests to PMUv3 for ARMv8.4 */ + id_reg->vcpu_limit_val = +cpuid_feature_cap_perfmon_field (id_reg->vcpu_limit_val, +ID_DFR0_PERFMON_SHIFT , +ID_DFR0_PERFMON_8_4 ); +} +static u64 vcpu_mask_id_aa64pfr0_el1 (const struct kvm_vcpu *vcpu, -const struct id_reg_info *idr) +const struct id_reg_info *idr) {return vcpu_has_sve (vcpu) ?0 :ARM64_FEATURE_MASK (ID_AA64PFR0_SVE ); }
@@ -717,6 +747,12 @@ static u64 vcpu_mask_id_aa64dfr0_el1(const struct kvm_vcpu *vcpu,return kvm_vcpu_has_pmu (vcpu) ?0 :ARM64_FEATURE_MASK (ID_AA64DFR0_PMUVER ); } +static u64 vcpu_mask_id_dfr0_el1 (const struct kvm_vcpu *vcpu, +const struct id_reg_info *idr) +{ +return kvm_vcpu_has_pmu (vcpu) ?0 :ARM64_FEATURE_MASK (ID_DFR0_PERFMON ); +} +static struct id_reg_info id_aa64pfr0_el1_info = { .sys_reg =SYS_ID_AA64PFR0_EL1 , .ignore_mask =ARM64_FEATURE_MASK (ID_AA64PFR0_GIC ),
@@ -773,6 +809,13 @@ static struct id_reg_info id_aa64dfr0_el1_info = { .vcpu_mask = vcpu_mask_id_aa64dfr0_el1, }; +static struct id_reg_info id_dfr0_el1_info = { + .sys_reg =SYS_ID_DFR0_EL1 , + .init = init_id_dfr0_el1_info, + .validate = validate_id_dfr0_el1, + .vcpu_mask = vcpu_mask_id_dfr0_el1, +}; +/* * An ID register that needs special handling to control the value for the * guest must have its own id_reg_info in id_reg_info_table.
@@ -782,6 +825,7 @@ static struct id_reg_info id_aa64dfr0_el1_info = {*/ #define GET_ID_REG_INFO (id) (id_reg_info_table[IDREG_IDX(id)])static struct id_reg_info *id_reg_info_table[KVM_ARM_ID_REG_MAX_NUM ] = { + [IDREG_IDX (SYS_ID_DFR0_EL1 )] = &id_dfr0_el1_info, [IDREG_IDX (SYS_ID_AA64PFR0_EL1 )] = &id_aa64pfr0_el1_info, [IDREG_IDX (SYS_ID_AA64PFR1_EL1 )] = &id_aa64pfr1_el1_info, [IDREG_IDX (SYS_ID_AA64DFR0_EL1 )] = &id_aa64dfr0_el1_info,
@@ -1712,15 +1756,6 @@ static u64 __read_id_reg(const struct kvm_vcpu *vcpu, u32 id)/* Clear fields for opt-in features that are not configured. */ val &= ~(id_reg->vcpu_mask (vcpu, id_reg)); -switch (id) { -case SYS_ID_DFR0_EL1 : -/* Limit guests to PMUv3 for ARMv8.4 */ - val =cpuid_feature_cap_perfmon_field (val, -ID_DFR0_PERFMON_SHIFT , -kvm_vcpu_has_pmu (vcpu) ?ID_DFR0_PERFMON_8_4 :0 ); -break ; - } -return val; }
Signature · 6 lines
2.35.1.265.g69c8d7142f-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v5 13/27 KVM: arm64: Make MVFR1_EL1 writable
In reply to Reiji WatanabeThis patch adds id_reg_info for MVFR1_EL1 to make it writable by userspace.
There are only a few valid combinations of values that can be set for FPHP and SIMDHP fields according to Arm ARM. Return an error when userspace tries to set those fields to values that don't match any of the valid combinations.
--- arch/arm64/kvm/sys_regs.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
arch/arm64/kvm/sys_regs.c +36 -0
@@ -654,6 +654,36 @@ static int validate_id_dfr0_el1(struct kvm_vcpu *vcpu,return 0 ; } +static int validate_mvfr1_el1 (struct kvm_vcpu *vcpu, +const struct id_reg_info *id_reg,u64 val) +{ +unsigned int fphp, simdhp; +struct fphp_simdhp { +unsigned int fphp; +unsigned int simdhp; + }; +/* Permitted fphp/simdhp value combinations according to Arm ARM */ +struct fphp_simdhp valid_fphp_simdhp[3 ] = {{0 ,0 }, {2 ,1 }, {3 ,2 }}; +int i; +bool is_valid_fphp_simdhp = false; + + fphp =cpuid_feature_extract_unsigned_field (val,MVFR1_FPHP_SHIFT ); + simdhp =cpuid_feature_extract_unsigned_field (val,MVFR1_SIMDHP_SHIFT ); + +for (i =0 ; i <ARRAY_SIZE (valid_fphp_simdhp); i++) { +if (valid_fphp_simdhp[i].fphp == fphp && + valid_fphp_simdhp[i].simdhp == simdhp) { + is_valid_fphp_simdhp = true; +break ; + } + } + +if (!is_valid_fphp_simdhp) +return -EINVAL ; + +return 0 ; +} +static void init_id_aa64pfr0_el1_info (struct id_reg_info *id_reg) {u64 limit = id_reg->vcpu_limit_val;
@@ -816,6 +846,11 @@ static struct id_reg_info id_dfr0_el1_info = { .vcpu_mask = vcpu_mask_id_dfr0_el1, }; +static struct id_reg_info mvfr1_el1_info = { + .sys_reg =SYS_MVFR1_EL1 , + .validate = validate_mvfr1_el1, +}; +/* * An ID register that needs special handling to control the value for the * guest must have its own id_reg_info in id_reg_info_table.
@@ -826,6 +861,7 @@ static struct id_reg_info id_dfr0_el1_info = {#define GET_ID_REG_INFO (id) (id_reg_info_table[IDREG_IDX(id)])static struct id_reg_info *id_reg_info_table[KVM_ARM_ID_REG_MAX_NUM ] = { [IDREG_IDX (SYS_ID_DFR0_EL1 )] = &id_dfr0_el1_info, + [IDREG_IDX (SYS_MVFR1_EL1 )] = &mvfr1_el1_info, [IDREG_IDX (SYS_ID_AA64PFR0_EL1 )] = &id_aa64pfr0_el1_info, [IDREG_IDX (SYS_ID_AA64PFR1_EL1 )] = &id_aa64pfr1_el1_info, [IDREG_IDX (SYS_ID_AA64DFR0_EL1 )] = &id_aa64dfr0_el1_info,
Signature · 6 lines
2.35.1.265.g69c8d7142f-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v5 14/27 KVM: arm64: Make ID registers without id_reg_info writable
In reply to Reiji WatanabeMake ID registers that don't have id_reg_info writable.
--- arch/arm64/kvm/sys_regs.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
arch/arm64/kvm/sys_regs.c +2 -5
@@ -1909,11 +1909,8 @@ static int __set_id_reg(struct kvm_vcpu *vcpu,/* The value is same as the current value. Nothing to do. */ return 0 ; -/* -* Don't allow to modify the register's value if the register is raz, -* or the reg doesn't have the id_reg_info. -*/ -if (raz || !GET_ID_REG_INFO (encoding)) +/* Don't allow to modify the register's value if the register is raz. */ +if (raz)return -EINVAL ;/*
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
PATCH v5 15/27 KVM: arm64: Add consistency checking for frac fields of ID registers
In reply to Reiji WatanabeFeature fractional field of an ID register cannot be simply validated at KVM_SET_ONE_REG because its validity depends on its (main) feature field value, which could be in a different ID register (and might be set later).
Validate fractional fields at the first KVM_RUN instead.
--- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/arm.c | 3 + arch/arm64/kvm/sys_regs.c | 115 +++++++++++++++++++++++++++++- 3 files changed, 116 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -748,6 +748,7 @@ long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,void set_default_id_regs (struct kvm *kvm);int kvm_set_id_reg_feature (struct kvm *kvm,u32 id,u8 field_shift,u8 fval); +int kvm_id_regs_check_frac_fields (const struct kvm_vcpu *vcpu);/* Guest/host FPSIMD coordination helpers */ int kvm_arch_vcpu_run_map_fp (struct kvm_vcpu *vcpu);
arch/arm64/kvm/arm.c +3 -0
@@ -599,6 +599,9 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)if (likely (vcpu_has_run_once (vcpu)))return 0 ; +if (!kvm_vm_is_protected (kvm) &&kvm_id_regs_check_frac_fields (vcpu)) +return -EPERM ; +kvm_arm_vcpu_init_debug (vcpu);if (likely (irqchip_in_kernel (kvm))) {
arch/arm64/kvm/sys_regs.c +112 -3
137 lines not on this page · raw message
Signature · 6 lines
2.35.1.265.g69c8d7142f-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v5 16/27 KVM: arm64: Introduce KVM_CAP_ARM_ID_REG_CONFIGURABLE capability
In reply to Reiji WatanabeIntroduce a new capability KVM_CAP_ARM_ID_REG_CONFIGURABLE to indicate that ID registers are writable by userspace.
--- Documentation/virt/kvm/api.rst | 12 ++++++++++++ arch/arm64/kvm/arm.c | 1 + include/uapi/linux/kvm.h | 1 + 3 files changed, 14 insertions(+)
Documentation/virt/kvm/api.rst +12 -0
@@ -2607,6 +2607,10 @@ EINVAL. After the vcpu's SVE configuration is finalized, further attempts to write this register will fail with EPERM. +The arm64 ID registers (where Op0=3, Op1=0, CRn=0, 0<=CRm<8, 0<=Op2<8) +are allowed to set by userspace if KVM_CAP_ARM_ID_REG_CONFIGURABLE is +available. They become immutable after calling KVM_RUN on any of the +vcpus in the guest (modifying values of those registers will fail). MIPS registers are mapped using the lower 32 bits. The upper 16 of that is the register group type:
@@ -7561,3 +7565,11 @@ The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset of the result of KVM_CHECK_EXTENSION. KVM will forward to userspace the hypercalls whose corresponding bit is in the argument, and return ENOSYS for the others. + +8.35 KVM_CAP_ARM_ID_REG_CONFIGURABLE +------------------------------------ + +:Architectures: arm64 + +This capability indicates that userspace can modify the ID registers +via KVM_SET_ONE_REG ioctl.
arch/arm64/kvm/arm.c +1 -0
@@ -210,6 +210,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_SET_GUEST_DEBUG :case KVM_CAP_VCPU_ATTRIBUTES :case KVM_CAP_PTP_KVM : +case KVM_CAP_ARM_ID_REG_CONFIGURABLE : r =1 ;break ;case KVM_CAP_SET_GUEST_DEBUG2 :
include/uapi/linux/kvm.h +1 -0
@@ -1134,6 +1134,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_VM_GPA_BITS 207#define KVM_CAP_XSAVE2 208#define KVM_CAP_SYS_ATTRIBUTES 209 +#define KVM_CAP_ARM_ID_REG_CONFIGURABLE 210#ifdef KVM_CAP_IRQ_ROUTING
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
PATCH v5 17/27 KVM: arm64: Add kunit test for ID register validation
In reply to Reiji WatanabeAdd kunit tests for functions that are used for validation of ID registers, CONFIG_KVM_KUNIT_TEST option to enable the tests, and .kunitconfig to run the kunit tests.
One line change below is needed in the default arm64.py to fully run all of those kunit tests.
-----------------------------------------------------------------------
$ diff tools/testing/kunit/qemu_configs/arm64.py arm64_kvm_min.py 12c12 < extra_qemu_params=['-machine virt', '-cpu cortex-a57']) ---
Quoted 1 line
extra_qemu_params=['-M virt,virtualization=on,mte=on', '-cpu max,sve=on'])
-----------------------------------------------------------------------
The outputs from the tests are:
-----------------------------------------------------------------------
$ tools/testing/kunit/kunit.py run --timeout=60 --jobs=`nproc --all` \
--arch=arm64 --cross_compile=aarch64-linux-gnu- \
--qemu_config arm64_kvm_min.py \
--kunitconfig=arch/arm64/kvm/.kunitconfig
[20:02:52] Configuring KUnit Kernel ...
[20:02:52] Building KUnit Kernel ...
Populating config with:
$ make ARCH=arm64 olddefconfig CROSS_COMPILE=aarch64-linux-gnu- O=.kunit
Building with:
$ make ARCH=arm64 --jobs=96 CROSS_COMPILE=aarch64-linux-gnu- O=.kunit
[20:02:59] Starting KUnit Kernel (1/1)...
[20:02:59] ============================================================
Running tests with:
$ qemu-system-aarch64 -nodefaults -m 1024 -kernel .kunit/arch/arm64/boot/Image.gz -append 'mem=1G console=tty kunit_shutdown=halt console=ttyAMA0 kunit_shutdown=reboot' -no-reboot -nographic -serial stdio -M virt,virtualization=on,mte=on -cpu max,sve=on
[20:03:00] ========== kvm-sys-regs-test-suite (14 subtests) ===========
[20:03:00] [PASSED] vcpu_id_reg_feature_frac_check_test
[20:03:00] [PASSED] validate_id_aa64mmfr0_tgran2_test
[20:03:01] [PASSED] validate_id_aa64mmfr0_tgran2_test
[20:03:01] [PASSED] validate_id_aa64mmfr0_tgran2_test
[20:03:01] [PASSED] validate_id_aa64pfr0_el1_test
[20:03:01] [PASSED] validate_id_aa64pfr1_el1_test
[20:03:01] [PASSED] validate_id_aa64isar0_el1_test
[20:03:01] [PASSED] validate_id_aa64isar1_el1_test
[20:03:01] [PASSED] validate_id_aa64mmfr0_el1_test
[20:03:01] [PASSED] validate_id_aa64mmfr1_el1_test
[20:03:01] [PASSED] validate_id_aa64dfr0_el1_test
[20:03:01] [PASSED] validate_id_dfr0_el1_test
[20:03:01] [PASSED] validate_mvfr1_el1_test
[20:03:01] [PASSED] validate_id_reg_test
[20:03:01] ============= [PASSED] kvm-sys-regs-test-suite =============
[20:03:01] ============================================================
[20:03:01] Testing complete. Passed: 14, Failed: 0, Crashed: 0, Skipped: 0, Errors: 0
[20:03:01] Elapsed time: 8.534s total, 0.003s configuring, 6.962s building, 1.569s running
-------------------------------------------------------------------------- arch/arm64/kvm/.kunitconfig | 4 + arch/arm64/kvm/Kconfig | 11 + arch/arm64/kvm/sys_regs.c | 4 + arch/arm64/kvm/sys_regs_test.c | 1018 ++++++++++++++++++++++++++++++++ 4 files changed, 1037 insertions(+) create mode 100644 arch/arm64/kvm/.kunitconfig create mode 100644 arch/arm64/kvm/sys_regs_test.c
arch/arm64/kvm/.kunitconfig new file +4 -0
@@ -0,0 +1,4 @@ +CONFIG_KUNIT=y +CONFIG_VIRTUALIZATION=y +CONFIG_KVM=y +CONFIG_KVM_KUNIT_TEST=y
arch/arm64/kvm/Kconfig +11 -0
@@ -56,4 +56,15 @@ config NVHE_EL2_DEBUG If unsure, say N. +config KVM_KUNIT_TEST + bool "KUnit tests for KVM on ARM64 processors" if !KUNIT_ALL_TESTS + depends on KVM && KUNIT + default KUNIT_ALL_TESTS + help + Say Y here to enable KUnit tests for the KVM on ARM64. + Only useful for KVM/ARM development and are not for inclusion into + a production build. + + If unsure, say N. + endif # VIRTUALIZATION
arch/arm64/kvm/sys_regs.c +4 -0
@@ -3686,3 +3686,7 @@ int kvm_set_id_reg_feature(struct kvm *kvm, u32 id, u8 field_shift, u8 fval)return __modify_kvm_id_reg (kvm, id, val, preserve_mask); } + +#if IS_ENABLED (CONFIG_KVM_KUNIT_TEST ) +#include "sys_regs_test.c" +#endif
arch/arm64/kvm/sys_regs_test.c new file +1018 -0
1,018 lines not on this page · raw message
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
PATCH v5 18/27 KVM: arm64: Use vcpu->arch cptr_el2 to track value of cptr_el2 for VHE
In reply to Reiji WatanabeTrack the baseline guest value for cptr_el2 in struct kvm_vcpu_arch for VHE. Use this value when setting cptr_el2 for the guest.
Currently this value is unchanged, but the following patches will set trapping bits based on features supported for the guest.
No functional change intended.
--- arch/arm64/include/asm/kvm_arm.h | 16 ++++++++++++++++ arch/arm64/kvm/arm.c | 5 ++++- arch/arm64/kvm/hyp/vhe/switch.c | 14 ++------------ 3 files changed, 22 insertions(+), 13 deletions(-)
arch/arm64/include/asm/kvm_arm.h +16 -0
@@ -288,6 +288,22 @@ GENMASK(19, 14) | \ BIT(11)) +/* +* With VHE (HCR.E2H == 1), accesses to CPACR_EL1 are routed to +* CPTR_EL2. In general, CPACR_EL1 has the same layout as CPTR_EL2, +* except for some missing controls, such as TAM. +* In this case, CPTR_EL2.TAM has the same position with or without +* VHE (HCR.E2H == 1) which allows us to use here the CPTR_EL2.TAM +* shift value for trapping the AMU accesses. +*/ +#define CPTR_EL2_VHE_GUEST_DEFAULT (CPACR_EL1_TTA | CPTR_EL2_TAM) + +/* +* Bits that are copied from vcpu->arch.cptr_el2 to set cptr_el2 for +* guest with VHE. +*/ +#define CPTR_EL2_VHE_GUEST_TRACKED_MASK (CPACR_EL1_TTA | CPTR_EL2_TAM) +/* Hyp Debug Configuration Register bits */ #define MDCR_EL2_E2TB_MASK (UL(0x3))#define MDCR_EL2_E2TB_SHIFT (UL(24))
arch/arm64/kvm/arm.c +4 -1
@@ -1182,7 +1182,10 @@ static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu, }vcpu_reset_hcr (vcpu); - vcpu->arch.cptr_el2 =CPTR_EL2_DEFAULT ; +if (has_vhe ()) + vcpu->arch.cptr_el2 =CPTR_EL2_VHE_GUEST_DEFAULT ; +else + vcpu->arch.cptr_el2 =CPTR_EL2_DEFAULT ;/* * Handle the "start in power-off" case.
arch/arm64/kvm/hyp/vhe/switch.c +2 -12
@@ -37,20 +37,10 @@ static void __activate_traps(struct kvm_vcpu *vcpu)___activate_traps (vcpu); val =read_sysreg (cpacr_el1); - val |=CPACR_EL1_TTA ; + val &= ~CPTR_EL2_VHE_GUEST_TRACKED_MASK ; + val |= (vcpu->arch.cptr_el2 &CPTR_EL2_VHE_GUEST_TRACKED_MASK ); val &= ~CPACR_EL1_ZEN ; -/* -* With VHE (HCR.E2H == 1), accesses to CPACR_EL1 are routed to -* CPTR_EL2. In general, CPACR_EL1 has the same layout as CPTR_EL2, -* except for some missing controls, such as TAM. -* In this case, CPTR_EL2.TAM has the same position with or without -* VHE (HCR.E2H == 1) which allows us to use here the CPTR_EL2.TAM -* shift value for trapping the AMU accesses. -*/ - - val |=CPTR_EL2_TAM ; -if (update_fp_enabled (vcpu)) {if (vcpu_has_sve (vcpu)) val |=CPACR_EL1_ZEN ;
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
PATCH v5 19/27 KVM: arm64: Use vcpu->arch.mdcr_el2 to track value of mdcr_el2
In reply to Reiji WatanabeTrack the baseline guest value for mdcr_el2 in struct kvm_vcpu_arch. Use this value when setting mdcr_el2 for the guest.
Currently this value is unchanged, but the following patches will set trapping bits based on features supported for the guest.
No functional change intended.
--- arch/arm64/include/asm/kvm_arm.h | 16 ++++++++++++++++ arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/debug.c | 13 ++++--------- 3 files changed, 21 insertions(+), 9 deletions(-)
arch/arm64/include/asm/kvm_arm.h +16 -0
@@ -333,6 +333,22 @@ BIT(18) | \ GENMASK(16, 15)) +/* +* The default value for the guest below also clears MDCR_EL2_E2PB_MASK +* and MDCR_EL2_E2TB_MASK to disable guest access to the profiling and +* trace buffers. +*/ +#define MDCR_GUEST_FLAGS_DEFAULT \ + (MDCR_EL2_TPM | MDCR_EL2_TPMS | MDCR_EL2_TTRF | \ + MDCR_EL2_TPMCR | MDCR_EL2_TDRA | MDCR_EL2_TDOSA) + +/* Bits that are copied from vcpu->arch.mdcr_el2 to set mdcr_el2 for guest. */ +#define MDCR_GUEST_FLAGS_TRACKED_MASK \ + (MDCR_EL2_TPM | MDCR_EL2_TPMS | MDCR_EL2_TTRF | \ + MDCR_EL2_TPMCR | MDCR_EL2_TDRA | MDCR_EL2_TDOSA | \ + (MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT)) + +/* For compatibility with fault code shared with 32-bit */ #define FSC_FAULT ESR_ELx_FSC_FAULT#define FSC_ACCESS ESR_ELx_FSC_ACCESS
arch/arm64/kvm/arm.c +1 -0
@@ -1182,6 +1182,7 @@ static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu, }vcpu_reset_hcr (vcpu); + vcpu->arch.mdcr_el2 =MDCR_GUEST_FLAGS_DEFAULT ;if (has_vhe ()) vcpu->arch.cptr_el2 =CPTR_EL2_VHE_GUEST_DEFAULT ;else
arch/arm64/kvm/debug.c +4 -9
@@ -84,16 +84,11 @@ void kvm_arm_init_debug(void)static void kvm_arm_setup_mdcr_el2 (struct kvm_vcpu *vcpu) {/* -* This also clears MDCR_EL2_E2PB_MASK and MDCR_EL2_E2TB_MASK -* to disable guest access to the profiling and trace buffers +* Keep the vcpu->arch.mdcr_el2 bits that are specified by +* MDCR_GUEST_FLAGS_TRACKED_MASK. */ - vcpu->arch.mdcr_el2 =__this_cpu_read (mdcr_el2) &MDCR_EL2_HPMN_MASK ; - vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM | -MDCR_EL2_TPMS | -MDCR_EL2_TTRF | -MDCR_EL2_TPMCR | -MDCR_EL2_TDRA | -MDCR_EL2_TDOSA ); + vcpu->arch.mdcr_el2 &=MDCR_GUEST_FLAGS_TRACKED_MASK ; + vcpu->arch.mdcr_el2 |=__this_cpu_read (mdcr_el2) &MDCR_EL2_HPMN_MASK ;/* Is the VM being debugged by userspace? */ if (vcpu->guest_debug)
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
PATCH v5 20/27 KVM: arm64: Introduce framework to trap disabled features
In reply to Reiji WatanabeWhen a CPU feature that is supported on the host is not exposed to its guest, emulating a real CPU's behavior (by trapping or disabling guest's using the feature) is generally a desirable behavior (when it's possible without any or little side effect).
Introduce feature_config_ctrl structure, which manages feature information to program configuration register to trap or disable the feature when the feature is not exposed to the guest, and functions that uses the structure to activate the vcpu's trapping the feature. Those codes don't update trap configuration registers themselves (HCR_EL2, etc) but values for the registers in kvm_vcpu_arch at the first KVM_RUN.
At present, no feature has feature_config_ctrl yet and the following patches will add the feature_config_ctrl for some features.
--- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/arm.c | 13 ++-- arch/arm64/kvm/sys_regs.c | 112 ++++++++++++++++++++++++++++-- 3 files changed, 117 insertions(+), 9 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -749,6 +749,7 @@ long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,void set_default_id_regs (struct kvm *kvm);int kvm_set_id_reg_feature (struct kvm *kvm,u32 id,u8 field_shift,u8 fval);int kvm_id_regs_check_frac_fields (const struct kvm_vcpu *vcpu); +void kvm_vcpu_init_traps (struct kvm_vcpu *vcpu);/* Guest/host FPSIMD coordination helpers */ int kvm_arch_vcpu_run_map_fp (struct kvm_vcpu *vcpu);
arch/arm64/kvm/arm.c +8 -5
@@ -631,13 +631,16 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)static_branch_inc (&userspace_irqchip_in_use); } -/* -* Initialize traps for protected VMs. -* NOTE: Move to run in EL2 directly, rather than via a hypercall, once -* the code is in place for first run initialization at EL2. -*/ +/* Initialize traps for the guest. */ if (kvm_vm_is_protected (kvm)) +/* +* NOTE: Move to run in EL2 directly, rather than via a +* hypercall, once the code is in place for first run +* initialization at EL2. +*/ kvm_call_hyp_nvhe (__pkvm_vcpu_init_traps, vcpu); +else +kvm_vcpu_init_traps (vcpu);mutex_lock (&kvm->lock); kvm->arch.ran_once = true;
arch/arm64/kvm/sys_regs.c +108 -4
156 lines not on this page · raw message
Signature · 6 lines
2.35.1.265.g69c8d7142f-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v5 21/27 KVM: arm64: Trap disabled features of ID_AA64PFR0_EL1
In reply to Reiji WatanabeAdd feature_config_ctrl for RAS and AMU, which are indicated in ID_AA64PFR0_EL1, to program configuration registers to trap guest's using those features when they are not exposed to the guest.
Introduce trap_ras_regs() to change a behavior of guest's access to the registers, which is currently raz/wi, depending on the feature's availability for the guest (and inject undefined instruction exception when guest's RAS register access are trapped and RAS is not exposed to the guest). In order to keep the current visibility of the RAS registers from userspace (always visible), a visibility function for RAS registers is not added.
--- arch/arm64/kvm/sys_regs.c | 90 +++++++++++++++++++++++++++++++++++---- 1 file changed, 82 insertions(+), 8 deletions(-)
arch/arm64/kvm/sys_regs.c +82 -8
@@ -304,6 +304,63 @@ struct feature_config_ctrl {void (*trap_activate)(struct kvm_vcpu *vcpu); }; +enum vcpu_config_reg { +VCPU_HCR_EL2 =1 , +VCPU_MDCR_EL2 , +VCPU_CPTR_EL2 , +}; + +static void feature_trap_activate (struct kvm_vcpu *vcpu, +enum vcpu_config_reg cfg_reg, +u64 cfg_set,u64 cfg_clear) +{ +u64 *reg_ptr, reg_val; + +switch (cfg_reg) { +case VCPU_HCR_EL2 : + reg_ptr = &vcpu->arch.hcr_el2; +break ; +case VCPU_MDCR_EL2 : + reg_ptr = &vcpu->arch.mdcr_el2; +break ; +case VCPU_CPTR_EL2 : + reg_ptr = &vcpu->arch.cptr_el2; +break ; + } + +/* Clear/Set fields that are indicated by cfg_clear/cfg_set. */ + reg_val = (*reg_ptr & ~cfg_clear); + reg_val |= cfg_set; + *reg_ptr = reg_val; +} + +static void feature_ras_trap_activate (struct kvm_vcpu *vcpu) +{ +feature_trap_activate (vcpu,VCPU_HCR_EL2 ,HCR_TERR |HCR_TEA ,HCR_FIEN ); +} + +static void feature_amu_trap_activate (struct kvm_vcpu *vcpu) +{ +feature_trap_activate (vcpu,VCPU_CPTR_EL2 ,CPTR_EL2_TAM ,0 ); +} + +/* For ID_AA64PFR0_EL1 */ +static struct feature_config_ctrl ftr_ctrl_ras = { + .ftr_reg =SYS_ID_AA64PFR0_EL1 , + .ftr_shift =ID_AA64PFR0_RAS_SHIFT , + .ftr_min =ID_AA64PFR0_RAS_V1 , + .ftr_signed =FTR_UNSIGNED , + .trap_activate = feature_ras_trap_activate, +}; + +static struct feature_config_ctrl ftr_ctrl_amu = { + .ftr_reg =SYS_ID_AA64PFR0_EL1 , + .ftr_shift =ID_AA64PFR0_AMU_SHIFT , + .ftr_min =ID_AA64PFR0_AMU , + .ftr_signed =FTR_UNSIGNED , + .trap_activate = feature_amu_trap_activate, +}; +struct id_reg_info {/* Register ID */ u32 sys_reg;
@@ -816,6 +873,11 @@ static struct id_reg_info id_aa64pfr0_el1_info = { .init = init_id_aa64pfr0_el1_info, .validate = validate_id_aa64pfr0_el1, .vcpu_mask = vcpu_mask_id_aa64pfr0_el1, + .trap_features = &(const struct feature_config_ctrl *[]) { + &ftr_ctrl_ras, + &ftr_ctrl_amu, +NULL , + }, };static struct id_reg_info id_aa64pfr1_el1_info = {
@@ -945,6 +1007,18 @@ static inline bool vcpu_feature_is_available(struct kvm_vcpu *vcpu,return feature_avail (ctrl, val); } +static bool trap_ras_regs (struct kvm_vcpu *vcpu, +struct sys_reg_params *p, +const struct sys_reg_desc *r) +{ +if (!vcpu_feature_is_available (vcpu, &ftr_ctrl_ras)) { +kvm_inject_undefined (vcpu); +return false; + } + +return trap_raz_wi (vcpu, p, r); +} +/* * ARMv8.1 mandates at least a trivial LORegion implementation, where all the * RW registers are RES0 (which we can implement as RAZ/WI). On an ARMv8.0
@@ -2316,14 +2390,14 @@ static const struct sys_reg_desc sys_reg_descs[] = { { SYS_DESC(SYS_AFSR1_EL1), access_vm_reg, reset_unknown, AFSR1_EL1 }, { SYS_DESC(SYS_ESR_EL1), access_vm_reg, reset_unknown, ESR_EL1 }, - { SYS_DESC(SYS_ERRIDR_EL1), trap_raz_wi }, - { SYS_DESC(SYS_ERRSELR_EL1), trap_raz_wi }, - { SYS_DESC(SYS_ERXFR_EL1), trap_raz_wi }, - { SYS_DESC(SYS_ERXCTLR_EL1), trap_raz_wi }, - { SYS_DESC(SYS_ERXSTATUS_EL1), trap_raz_wi }, - { SYS_DESC(SYS_ERXADDR_EL1), trap_raz_wi }, - { SYS_DESC(SYS_ERXMISC0_EL1), trap_raz_wi }, - { SYS_DESC(SYS_ERXMISC1_EL1), trap_raz_wi }, + { SYS_DESC(SYS_ERRIDR_EL1), trap_ras_regs }, + { SYS_DESC(SYS_ERRSELR_EL1), trap_ras_regs }, + { SYS_DESC(SYS_ERXFR_EL1), trap_ras_regs }, + { SYS_DESC(SYS_ERXCTLR_EL1), trap_ras_regs }, + { SYS_DESC(SYS_ERXSTATUS_EL1), trap_ras_regs }, + { SYS_DESC(SYS_ERXADDR_EL1), trap_ras_regs }, + { SYS_DESC(SYS_ERXMISC0_EL1), trap_ras_regs }, + { SYS_DESC(SYS_ERXMISC1_EL1), trap_ras_regs },MTE_REG (TFSR_EL1 ), MTE_REG(TFSRE0_EL1),
Signature · 6 lines
2.35.1.265.g69c8d7142f-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v5 22/27 KVM: arm64: Trap disabled features of ID_AA64PFR1_EL1
In reply to Reiji WatanabeAdd feature_config_ctrl for MTE, which is indicated in ID_AA64PFR1_EL1, to program configuration register to trap the guest's using the feature when it is not exposed to the guest.
--- arch/arm64/kvm/sys_regs.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
arch/arm64/kvm/sys_regs.c +18 -0
@@ -344,6 +344,11 @@ static void feature_amu_trap_activate(struct kvm_vcpu *vcpu)feature_trap_activate (vcpu,VCPU_CPTR_EL2 ,CPTR_EL2_TAM ,0 ); } +static void feature_mte_trap_activate (struct kvm_vcpu *vcpu) +{ +feature_trap_activate (vcpu,VCPU_HCR_EL2 ,HCR_TID5 ,HCR_DCT |HCR_ATA ); +} +/* For ID_AA64PFR0_EL1 */ static struct feature_config_ctrl ftr_ctrl_ras = { .ftr_reg =SYS_ID_AA64PFR0_EL1 ,
@@ -361,6 +366,15 @@ static struct feature_config_ctrl ftr_ctrl_amu = { .trap_activate = feature_amu_trap_activate, }; +/* For ID_AA64PFR1_EL1 */ +static struct feature_config_ctrl ftr_ctrl_mte = { + .ftr_reg =SYS_ID_AA64PFR1_EL1 , + .ftr_shift =ID_AA64PFR1_MTE_SHIFT , + .ftr_min =ID_AA64PFR1_MTE_EL0 , + .ftr_signed =FTR_UNSIGNED , + .trap_activate = feature_mte_trap_activate, +}; +struct id_reg_info {/* Register ID */ u32 sys_reg;
@@ -885,6 +899,10 @@ static struct id_reg_info id_aa64pfr1_el1_info = { .init = init_id_aa64pfr1_el1_info, .validate = validate_id_aa64pfr1_el1, .vcpu_mask = vcpu_mask_id_aa64pfr1_el1, + .trap_features = &(const struct feature_config_ctrl *[]) { + &ftr_ctrl_mte, +NULL , + }, };static struct id_reg_info id_aa64isar0_el1_info = {
Signature · 6 lines
2.35.1.265.g69c8d7142f-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v5 23/27 KVM: arm64: Trap disabled features of ID_AA64DFR0_EL1
In reply to Reiji WatanabeAdd feature_config_ctrl for PMUv3, PMS and TraceFilt, which are indicated in ID_AA64DFR0_EL1, to program configuration registers to trap guest's using those features when they are not exposed to the guest.
--- arch/arm64/kvm/sys_regs.c | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+)
arch/arm64/kvm/sys_regs.c +64 -0
@@ -349,6 +349,30 @@ static void feature_mte_trap_activate(struct kvm_vcpu *vcpu)feature_trap_activate (vcpu,VCPU_HCR_EL2 ,HCR_TID5 ,HCR_DCT |HCR_ATA ); } +static void feature_trace_trap_activate (struct kvm_vcpu *vcpu) +{ +if (has_vhe ()) +feature_trap_activate (vcpu,VCPU_CPTR_EL2 ,CPACR_EL1_TTA ,0 ); +else +feature_trap_activate (vcpu,VCPU_CPTR_EL2 ,CPTR_EL2_TTA ,0 ); +} + +static void feature_pmuv3_trap_activate (struct kvm_vcpu *vcpu) +{ +feature_trap_activate (vcpu,VCPU_MDCR_EL2 ,MDCR_EL2_TPM ,0 ); +} + +static void feature_pms_trap_activate (struct kvm_vcpu *vcpu) +{ +feature_trap_activate (vcpu,VCPU_MDCR_EL2 ,MDCR_EL2_TPMS , +MDCR_EL2_E2PB_MASK <<MDCR_EL2_E2PB_SHIFT ); +} + +static void feature_tracefilt_trap_activate (struct kvm_vcpu *vcpu) +{ +feature_trap_activate (vcpu,VCPU_MDCR_EL2 ,MDCR_EL2_TTRF ,0 ); +} +/* For ID_AA64PFR0_EL1 */ static struct feature_config_ctrl ftr_ctrl_ras = { .ftr_reg =SYS_ID_AA64PFR0_EL1 ,
@@ -375,6 +399,39 @@ static struct feature_config_ctrl ftr_ctrl_mte = { .trap_activate = feature_mte_trap_activate, }; +/* For ID_AA64DFR0_EL1 */ +static struct feature_config_ctrl ftr_ctrl_trace = { + .ftr_reg =SYS_ID_AA64DFR0_EL1 , + .ftr_shift =ID_AA64DFR0_TRACEVER_SHIFT , + .ftr_min =1 , + .ftr_signed =FTR_UNSIGNED , + .trap_activate = feature_trace_trap_activate, +}; + +static struct feature_config_ctrl ftr_ctrl_pmuv3 = { + .ftr_reg =SYS_ID_AA64DFR0_EL1 , + .ftr_shift =ID_AA64DFR0_PMUVER_SHIFT , + .ftr_min =ID_AA64DFR0_PMUVER_8_0 , + .ftr_signed =FTR_UNSIGNED , + .trap_activate = feature_pmuv3_trap_activate, +}; + +static struct feature_config_ctrl ftr_ctrl_pms = { + .ftr_reg =SYS_ID_AA64DFR0_EL1 , + .ftr_shift =ID_AA64DFR0_PMSVER_SHIFT , + .ftr_min =ID_AA64DFR0_PMSVER_8_2 , + .ftr_signed =FTR_UNSIGNED , + .trap_activate = feature_pms_trap_activate, +}; + +static struct feature_config_ctrl ftr_ctrl_tracefilt = { + .ftr_reg =SYS_ID_AA64DFR0_EL1 , + .ftr_shift =ID_AA64DFR0_TRACE_FILT_SHIFT , + .ftr_min =1 , + .ftr_signed =FTR_UNSIGNED , + .trap_activate = feature_tracefilt_trap_activate, +}; +struct id_reg_info {/* Register ID */ u32 sys_reg;
@@ -941,6 +998,13 @@ static struct id_reg_info id_aa64dfr0_el1_info = { .init = init_id_aa64dfr0_el1_info, .validate = validate_id_aa64dfr0_el1, .vcpu_mask = vcpu_mask_id_aa64dfr0_el1, + .trap_features = &(const struct feature_config_ctrl *[]) { + &ftr_ctrl_trace, + &ftr_ctrl_pmuv3, + &ftr_ctrl_pms, + &ftr_ctrl_tracefilt, +NULL , + }, };static struct id_reg_info id_dfr0_el1_info = {
Signature · 6 lines
2.35.1.265.g69c8d7142f-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v5 24/27 KVM: arm64: Trap disabled features of ID_AA64MMFR1_EL1
In reply to Reiji WatanabeAdd feature_config_ctrl for LORegions, which is indicated in ID_AA64MMFR1_EL1, to program configuration register to trap guest's using the feature when it is not exposed to the guest.
Change trap_loregion() to use vcpu_feature_is_available() to simplify checking of the feature's availability.
--- arch/arm64/kvm/sys_regs.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
arch/arm64/kvm/sys_regs.c +19 -2
@@ -373,6 +373,11 @@ static void feature_tracefilt_trap_activate(struct kvm_vcpu *vcpu)feature_trap_activate (vcpu,VCPU_MDCR_EL2 ,MDCR_EL2_TTRF ,0 ); } +static void feature_lor_trap_activate (struct kvm_vcpu *vcpu) +{ +feature_trap_activate (vcpu,VCPU_HCR_EL2 ,HCR_TLOR ,0 ); +} +/* For ID_AA64PFR0_EL1 */ static struct feature_config_ctrl ftr_ctrl_ras = { .ftr_reg =SYS_ID_AA64PFR0_EL1 ,
@@ -432,6 +437,15 @@ static struct feature_config_ctrl ftr_ctrl_tracefilt = { .trap_activate = feature_tracefilt_trap_activate, }; +/* For ID_AA64MMFR1_EL1 */ +static struct feature_config_ctrl ftr_ctrl_lor = { + .ftr_reg =SYS_ID_AA64MMFR1_EL1 , + .ftr_shift =ID_AA64MMFR1_LOR_SHIFT , + .ftr_min =1 , + .ftr_signed =FTR_UNSIGNED , + .trap_activate = feature_lor_trap_activate, +}; +struct id_reg_info {/* Register ID */ u32 sys_reg;
@@ -991,6 +1005,10 @@ static struct id_reg_info id_aa64mmfr0_el1_info = {static struct id_reg_info id_aa64mmfr1_el1_info = { .sys_reg =SYS_ID_AA64MMFR1_EL1 , .validate = validate_id_aa64mmfr1_el1, + .trap_features = &(const struct feature_config_ctrl *[]) { + &ftr_ctrl_lor, +NULL , + }, };static struct id_reg_info id_aa64dfr0_el1_info = {
@@ -1111,10 +1129,9 @@ static bool trap_loregion(struct kvm_vcpu *vcpu,struct sys_reg_params *p, const struct sys_reg_desc *r) { - u64 val = __read_id_reg(vcpu, SYS_ID_AA64MMFR1_EL1); u32 sr = reg_to_encoding(r); -if (!(val & (0xfUL <<ID_AA64MMFR1_LOR_SHIFT ))) { +if (!vcpu_feature_is_available (vcpu, &ftr_ctrl_lor)) {kvm_inject_undefined (vcpu);return false; }
Signature · 6 lines
2.35.1.265.g69c8d7142f-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v5 25/27 KVM: arm64: Trap disabled features of ID_AA64ISAR1_EL1
In reply to Reiji WatanabeAdd feature_config_ctrl for PTRAUTH, which is indicated in ID_AA64ISAR1_EL1, to program configuration register to trap guest's using the feature when it is not exposed to the guest.
--- arch/arm64/kvm/sys_regs.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)
arch/arm64/kvm/sys_regs.c +39 -0
@@ -283,6 +283,30 @@ static bool trap_raz_wi(struct kvm_vcpu *vcpu, (cpuid_feature_extract_unsigned_field (val,ID_AA64ISAR1_GPI_SHIFT ) >= \ID_AA64ISAR1_GPI_IMP_DEF ) +/* +* Return true if ptrauth needs to be trapped. +* (i.e. if ptrauth is supported on the host but not exposed to the guest) + */ +static bool vcpu_need_trap_ptrauth (struct kvm_vcpu *vcpu) +{ +u64 val; +bool generic, address; + +if (!system_has_full_ptr_auth ()) +/* The feature is not supported. */ +return false; + + val =__read_id_reg (vcpu,SYS_ID_AA64ISAR1_EL1 ); + generic =aa64isar1_has_gpi (val) ||aa64isar1_has_gpa (val); + address =aa64isar1_has_api (val) ||aa64isar1_has_apa (val); +if (generic && address) +/* The feature is available. */ +return false; + +/* The feature is supported but hidden. */ +return true; +} +/* * Feature information to program configuration register to trap or disable * guest's using a feature when the feature is not exposed to the guest.
@@ -378,6 +402,11 @@ static void feature_lor_trap_activate(struct kvm_vcpu *vcpu)feature_trap_activate (vcpu,VCPU_HCR_EL2 ,HCR_TLOR ,0 ); } +static void feature_ptrauth_trap_activate (struct kvm_vcpu *vcpu) +{ +feature_trap_activate (vcpu,VCPU_HCR_EL2 ,0 ,HCR_API |HCR_APK ); +} +/* For ID_AA64PFR0_EL1 */ static struct feature_config_ctrl ftr_ctrl_ras = { .ftr_reg =SYS_ID_AA64PFR0_EL1 ,
@@ -446,6 +475,12 @@ static struct feature_config_ctrl ftr_ctrl_lor = { .trap_activate = feature_lor_trap_activate, }; +/* For SYS_ID_AA64ISAR1_EL1 */ +static struct feature_config_ctrl ftr_ctrl_ptrauth = { + .ftr_need_trap = vcpu_need_trap_ptrauth, + .trap_activate = feature_ptrauth_trap_activate, +}; +struct id_reg_info {/* Register ID */ u32 sys_reg;
@@ -986,6 +1021,10 @@ static struct id_reg_info id_aa64isar1_el1_info = { .init = init_id_aa64isar1_el1_info, .validate = validate_id_aa64isar1_el1, .vcpu_mask = vcpu_mask_id_aa64isar1_el1, + .trap_features = &(const struct feature_config_ctrl *[]) { + &ftr_ctrl_ptrauth, +NULL , + }, };static struct id_reg_info id_aa64mmfr0_el1_info = {
Signature · 6 lines
2.35.1.265.g69c8d7142f-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v5 26/27 KVM: arm64: Add kunit test for trap initialization
In reply to Reiji WatanabeAdd KUnit tests for functions in arch/arm64/kvm/sys_regs_test.c that activates traps for disabled features.
--- arch/arm64/kvm/sys_regs_test.c | 262 +++++++++++++++++++++++++++++++++ 1 file changed, 262 insertions(+)
arch/arm64/kvm/sys_regs_test.c +262 -0
@@ -991,6 +991,265 @@ static void validate_id_reg_test(struct kunit *test)GET_ID_REG_INFO (id) = original_idr; } +struct trap_config_test { +u64 set; +u64 clear; +u64 prev_val; +u64 expect_val; +}; + +struct trap_config_test trap_params[] = { + {0x30000800000 ,0 ,0 ,0x30000800000 }, + {0 ,0x30000800000 ,0 ,0 }, + {0x30000800000 ,0 , (u64)-1 , (u64)-1 }, + {0 ,0x30000800000 , (u64)-1 , (u64 )0xfffffcffff7fffff }, +}; + +static void trap_case_to_desc (struct trap_config_test *t,char *desc) +{ +snprintf (desc,KUNIT_PARAM_DESC_SIZE , +"trap - set:0x%llx, clear:0x%llx, prev_val:0x%llx\n" , + t->set, t->clear, t->prev_val); +} + +KUNIT_ARRAY_PARAM (trap, trap_params, trap_case_to_desc); + +/* Tests for feature_trap_activate(). */ +static void feature_trap_activate_test (struct kunit *test) +{ +struct kvm_vcpu *vcpu; +const struct trap_config_test *trap = test->param_value; + + vcpu =test_kvm_vcpu_init (test); +KUNIT_ASSERT_TRUE (test, vcpu); + +/* Test for HCR_EL2 */ + vcpu->arch.hcr_el2 = trap->prev_val; +feature_trap_activate (vcpu,VCPU_HCR_EL2 , trap->set, trap->clear); +KUNIT_EXPECT_EQ (test, vcpu->arch.hcr_el2, trap->expect_val); + +/* Test for MDCR_EL2 */ + vcpu->arch.mdcr_el2 = trap->prev_val; +feature_trap_activate (vcpu,VCPU_MDCR_EL2 , trap->set, trap->clear); +KUNIT_EXPECT_EQ (test, vcpu->arch.mdcr_el2, trap->expect_val); + +/* Test for CPTR_EL2 */ + vcpu->arch.cptr_el2 = trap->prev_val; +feature_trap_activate (vcpu,VCPU_CPTR_EL2 , trap->set, trap->clear); +KUNIT_EXPECT_EQ (test, vcpu->arch.cptr_el2, trap->expect_val); +} + +static u64 test_trap_set0; +static u64 test_trap_clear0; +static void test_trap_activate0 (struct kvm_vcpu *vcpu) +{ +feature_trap_activate (vcpu,VCPU_HCR_EL2 , + test_trap_set0, test_trap_clear0); +} + +static u64 test_trap_set1; +static u64 test_trap_clear1; +static void test_trap_activate1 (struct kvm_vcpu *vcpu) +{ +feature_trap_activate (vcpu,VCPU_HCR_EL2 , + test_trap_set1, test_trap_clear1); +} + +static u64 test_trap_set2; +static u64 test_trap_clear2; +static void test_trap_activate2 (struct kvm_vcpu *vcpu) +{ +feature_trap_activate (vcpu,VCPU_HCR_EL2 , + test_trap_set2, test_trap_clear2); +} + + +static void setup_feature_config_ctrl (struct feature_config_ctrl *config, +u32 id,int shift,int min,bool sign, +void *fn) +{ +memset (config,0 ,sizeof (*config)); + config->ftr_reg = id; + config->ftr_shift = shift; + config->ftr_min = min; + config->ftr_signed = sign; + config->trap_activate = fn; +} + +/* +* Tests for id_reg_features_trap_activate. +* Setup a id_reg_info with three entries in id_reg_info->trap_features[]. +* Check if the config register is updated to enable trap for the disabled +* features. +*/ +static void id_reg_features_trap_activate_test (struct kunit *test) +{ +struct kvm_vcpu *vcpu; +u32 id; +u64 cfg_set, cfg_clear, id_reg_sys_val, id_reg_val; +struct id_reg_info id_reg_data; +struct feature_config_ctrl config0, config1, config2; +struct feature_config_ctrl *trap_features[] = { + &config0, &config1, &config2,NULL , + }; + + vcpu =test_kvm_vcpu_init (test); +KUNIT_EXPECT_TRUE (test, vcpu); +if (!vcpu) +return ; + +/* Setup id_reg_info */ + id_reg_sys_val =0x7777777777777777 ; + id =SYS_ID_AA64DFR0_EL1 ; + id_reg_data.sys_reg = id; + id_reg_data.sys_val = id_reg_sys_val; + id_reg_data.vcpu_limit_val = (u64)-1 ; + id_reg_data.trap_features = + (const struct feature_config_ctrl *(*)[])trap_features; + +/* Setup the 1st feature_config_ctrl */ + test_trap_set0 =0x3 ; + test_trap_clear0 =0x0 ; +setup_feature_config_ctrl (&config0, id,60 ,2 ,FTR_UNSIGNED , + &test_trap_activate0); + +/* Setup the 2nd feature_config_ctrl */ + test_trap_set1 =0x30000040 ; + test_trap_clear1 =0x40000000 ; +setup_feature_config_ctrl (&config1, id,0 ,1 ,FTR_UNSIGNED , + &test_trap_activate1); + +/* Setup the 3rd feature_config_ctrl */ + test_trap_set2 =0x30000000800 ; + test_trap_clear2 =0x40000000000 ; +setup_feature_config_ctrl (&config2, id,4 ,0 ,FTR_SIGNED , + &test_trap_activate2); + +#define ftr_dis (cfg) \ + ((u64)(((cfg)->ftr_min - 1) & 0xf) << (cfg)->ftr_shift) + +#define ftr_en (cfg) \ + ((u64)(cfg)->ftr_min << (cfg)->ftr_shift) + +/* Test with features enabled for config0, 1 and 2 */ + id_reg_val =ftr_en (&config0) |ftr_en (&config1) |ftr_en (&config2); +write_kvm_id_reg (vcpu->kvm, id, id_reg_val); + vcpu->arch.hcr_el2 =0 ; +id_reg_features_trap_activate (vcpu, &id_reg_data); +KUNIT_EXPECT_EQ (test, vcpu->arch.hcr_el2,0 ); + + +/* Test with features disabled for config0 only */ + id_reg_val =ftr_dis (&config0) |ftr_en (&config1) |ftr_en (&config2); +write_kvm_id_reg (vcpu->kvm, id, id_reg_val); + vcpu->arch.hcr_el2 =0 ; + cfg_set = test_trap_set0; + cfg_clear = test_trap_clear0; + +id_reg_features_trap_activate (vcpu, &id_reg_data); +KUNIT_EXPECT_EQ (test, vcpu->arch.hcr_el2 & cfg_set, cfg_set); +KUNIT_EXPECT_EQ (test, vcpu->arch.hcr_el2 & cfg_clear,0 ); + + +/* Test with features disabled for config0 and config1 */ + id_reg_val =ftr_dis (&config0) |ftr_dis (&config1) |ftr_en (&config2); +write_kvm_id_reg (vcpu->kvm, id, id_reg_val); + vcpu->arch.hcr_el2 =0 ; + + cfg_set = test_trap_set0 | test_trap_set1; + cfg_clear = test_trap_clear0 | test_trap_clear1; + +id_reg_features_trap_activate (vcpu, &id_reg_data); +KUNIT_EXPECT_EQ (test, vcpu->arch.hcr_el2 & cfg_set, cfg_set); +KUNIT_EXPECT_EQ (test, vcpu->arch.hcr_el2 & cfg_clear,0 ); + + +/* Test with features disabled for config0, config1, and config2 */ + id_reg_val =ftr_dis (&config0) |ftr_dis (&config1) |ftr_dis (&config2); +write_kvm_id_reg (vcpu->kvm, id, id_reg_val); + vcpu->arch.hcr_el2 =0 ; + + cfg_set = test_trap_set0 | test_trap_set1 | test_trap_set2; + cfg_clear = test_trap_clear0 | test_trap_clear1 | test_trap_clear2; + +id_reg_features_trap_activate (vcpu, &id_reg_data); +KUNIT_EXPECT_EQ (test, vcpu->arch.hcr_el2 & cfg_set, cfg_set); +KUNIT_EXPECT_EQ (test, vcpu->arch.hcr_el2 & cfg_clear,0 ); + + +/* Test with id_reg_info == NULL */ + vcpu->arch.hcr_el2 =0 ; +id_reg_features_trap_activate (vcpu,NULL ); +KUNIT_EXPECT_EQ (test, vcpu->arch.hcr_el2,0 ); + + +/* Test with id_reg_data.trap_features = NULL */ + id_reg_data.trap_features =NULL ; + vcpu->arch.hcr_el2 =0 ; +id_reg_features_trap_activate (vcpu, &id_reg_data); +KUNIT_EXPECT_EQ (test, vcpu->arch.hcr_el2,0 ); + +} + +/* Tests for vcpu_need_trap_ptrauth(). */ +static void vcpu_need_trap_ptrauth_test (struct kunit *test) +{ +struct kvm_vcpu *vcpu; +u32 id =SYS_ID_AA64ISAR1_EL1 ; + + vcpu =test_kvm_vcpu_init (test); +KUNIT_EXPECT_TRUE (test, vcpu); +if (!vcpu) +return ; + +if (system_has_full_ptr_auth ()) { +/* Tests with PTRAUTH disabled vCPU */ +write_kvm_id_reg (vcpu->kvm, id,0x0 ); +KUNIT_EXPECT_TRUE (test,vcpu_need_trap_ptrauth (vcpu)); + +/* GPI = 1, API = 1 */ +write_kvm_id_reg (vcpu->kvm, id,0x10000100 ); +KUNIT_EXPECT_TRUE (test,vcpu_need_trap_ptrauth (vcpu)); + +/* GPI = 1, APA = 1 */ +write_kvm_id_reg (vcpu->kvm, id,0x10000010 ); +KUNIT_EXPECT_TRUE (test,vcpu_need_trap_ptrauth (vcpu)); + +/* GPA = 1, API = 1 */ +write_kvm_id_reg (vcpu->kvm, id,0x01000100 ); +KUNIT_EXPECT_TRUE (test,vcpu_need_trap_ptrauth (vcpu)); + +/* GPA = 1, APA = 1 */ +write_kvm_id_reg (vcpu->kvm, id,0x01000010 ); +KUNIT_EXPECT_TRUE (test,vcpu_need_trap_ptrauth (vcpu)); + + +/* Tests with PTRAUTH enabled vCPU */ + vcpu->arch.flags |=KVM_ARM64_GUEST_HAS_PTRAUTH ; + +write_kvm_id_reg (vcpu->kvm, id,0x0 ); +KUNIT_EXPECT_TRUE (test,vcpu_need_trap_ptrauth (vcpu)); + +/* GPI = 1, API = 1 */ +write_kvm_id_reg (vcpu->kvm, id,0x10000100 ); +KUNIT_EXPECT_FALSE (test,vcpu_need_trap_ptrauth (vcpu)); + +/* GPI = 1, APA = 1 */ +write_kvm_id_reg (vcpu->kvm, id,0x10000010 ); +KUNIT_EXPECT_FALSE (test,vcpu_need_trap_ptrauth (vcpu)); + +/* GPA = 1, API = 1 */ +write_kvm_id_reg (vcpu->kvm, id,0x01000100 ); +KUNIT_EXPECT_FALSE (test,vcpu_need_trap_ptrauth (vcpu)); + +/* GPA = 1, APA = 1 */ +write_kvm_id_reg (vcpu->kvm, id,0x01000010 ); +KUNIT_EXPECT_FALSE (test,vcpu_need_trap_ptrauth (vcpu)); + }else { +KUNIT_EXPECT_FALSE (test,vcpu_need_trap_ptrauth (vcpu)); + } +} +static struct kunit_case kvm_sys_regs_test_cases[] = {KUNIT_CASE_PARAM (vcpu_id_reg_feature_frac_check_test, frac_gen_params),KUNIT_CASE_PARAM (validate_id_aa64mmfr0_tgran2_test, tgran4_2_gen_params),
@@ -1006,6 +1265,9 @@ static struct kunit_case kvm_sys_regs_test_cases[] = {KUNIT_CASE (validate_id_dfr0_el1_test),KUNIT_CASE (validate_mvfr1_el1_test),KUNIT_CASE (validate_id_reg_test), +KUNIT_CASE (vcpu_need_trap_ptrauth_test), +KUNIT_CASE_PARAM (feature_trap_activate_test, trap_gen_params), + KUNIT_CASE(id_reg_features_trap_activate_test), {} };
Signature · 6 lines
2.35.1.265.g69c8d7142f-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v5 27/27 KVM: arm64: selftests: Introduce id_reg_test
In reply to Reiji WatanabeIntroduce a test for aarch64 to validate basic behavior of KVM_GET_ONE_REG and KVM_SET_ONE_REG for ID registers.
This test runs only when KVM_CAP_ARM_ID_REG_CONFIGURABLE is supported.
--- tools/arch/arm64/include/asm/sysreg.h | 1 + tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/aarch64/id_reg_test.c | 1239 +++++++++++++++++ 4 files changed, 1242 insertions(+) create mode 100644 tools/testing/selftests/kvm/aarch64/id_reg_test.c
tools/arch/arm64/include/asm/sysreg.h +1 -0
@@ -793,6 +793,7 @@#define ID_AA64PFR0_ELx_32BIT_64BIT 0x2/* id_aa64pfr1 */ +#define ID_AA64PFR1_CSV2FRAC_SHIFT 32#define ID_AA64PFR1_MPAMFRAC_SHIFT 16#define ID_AA64PFR1_RASFRAC_SHIFT 12#define ID_AA64PFR1_MTE_SHIFT 8
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -2,6 +2,7 @@ /aarch64/arch_timer /aarch64/debug-exceptions /aarch64/get-reg-list +/aarch64/id_reg_test /aarch64/psci_cpu_on_test /aarch64/vgic_init /aarch64/vgic_irq
tools/testing/selftests/kvm/Makefile +1 -0
@@ -103,6 +103,7 @@ TEST_GEN_PROGS_x86_64 += system_counter_offset_test TEST_GEN_PROGS_aarch64 += aarch64/arch_timer TEST_GEN_PROGS_aarch64 += aarch64/debug-exceptions TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list +TEST_GEN_PROGS_aarch64 += aarch64/id_reg_test TEST_GEN_PROGS_aarch64 += aarch64/psci_cpu_on_test TEST_GEN_PROGS_aarch64 += aarch64/vgic_init TEST_GEN_PROGS_aarch64 += aarch64/vgic_irq
tools/testing/selftests/kvm/aarch64/id_reg_test.c new file +1239 -0
1,239 lines not on this page · raw message
Signature · 6 lines
2.35.1.265.g69c8d7142f-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm