KVM: arm64: Make CPU ID registers writable by userspace
Subsystems (5)
v6 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-rc7 with the patch [3] applied.
v6:
- Remove entries for all ID register entries from sys_reg_descs[],
and use id_reg_desc_table[] for ID registers instead. [Oliver]
- Remove sys_reg field from id_reg_info, add reg_desc (sys_reg_desc) field to id_reg_info, and rename 'id_reg_info' to 'id_reg_desc'.
- Merge the following three patches of v5 into one patch "KVM: arm64:
Make ID_AA64DFR0_EL1/ID_DFR0_EL1 writable" to accept userspace's request to set ID_AA64DFR0_EL1.PMUVER/ID_DFR0_EL1.PERFMON to 0xf (they are set to 0 though) even after applying the patch-10. [Oliver]
patch-10 KVM: arm64: Hide IMPLEMENTATION DEFINED PMU support for the guest patch-11 KVM: arm64: Make ID_AA64DFR0_EL1 writable"
patch-12 KVM: arm64: Make ID_DFR0_EL1 writable"
v5: https://lore.kernel.org/all/[email protected]/
- 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 (25):
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_desc
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_AA64DFR0_EL1/ID_DFR0_EL1 writable
KVM: arm64: Make MVFR1_EL1 writable
KVM: arm64: Add remaining ID registers to id_reg_desc_table
KVM: arm64: Use id_reg_desc_table for ID registers
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 | 5 +- 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 | 1637 +++++++++++++++-- arch/arm64/kvm/sys_regs_test.c | 1238 +++++++++++++ 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 | 1300 +++++++++++++ 19 files changed, 4324 insertions(+), 226 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: d1358550d36d8514b340f27a35ee0afc68f27cfc
Signature · 1 line
2.35.1.723.g4982287a31-goog
PATCH v6 1/25 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 · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v6 01/25] KVM: arm64: Introduce a validation function for an ID register
Hi Reiji,
Quoted 8 lines · Reiji Watanabe
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.
Quoted 5 lines
The validation is done using
arm64_ftr_bits_kvm, which is created fromarm64_ftr_regs, with some entries overwritten by entries fromarm64_ftr_bits_kvm_override.Signed-off-by Reiji Watanabe <[email protected]>
I have some concerns regarding the API between cpufeature and KVM that's being proposed here. It would appear that we are adding some of KVM's implementation details into the cpufeature code. In particular, I see that KVM's limitations on AA64DFR0 are being copied here.
Additionally, I think it would be preferable to expose this in a manner that does not require CONFIG_KVM guards in other parts of the kernel.
WDYT about having KVM keep its set of feature overrides and otherwise rely on the kernel's feature tables? I messed around with the idea a bit until I could get something workable (attached). I only compile tested this :)
--
Thanks,
Oliver
From e095ae6f138418bdb0981b6ef2c07930736aa2c2 Mon Sep 17 00:00:00 2001
From: Oliver Upton <[email protected]>
Date: Tue, 22 Mar 2022 07:22:28 +0000
Subject: [PATCH] arm64: cpufeature: Expose a helper for validating feature
registers
KVM is another part of the kernel that needs to deal with feature registers. Subsequent changes to KVM will make the feature ID registers writable from userspace, allowing VMMs to limit the exposed ISA to its guests. However, KVM does not have any good constructs for validating whether a particular register value constitutes a subset of the features supported by the system.
Add a helper to check that a feature register value is a subset of the system's safe value. Allow the caller to specify a set of overridden feature fields for the case where KVM is more restrictive than the kernel.
--- arch/arm64/include/asm/cpufeature.h | 8 +++ arch/arm64/kernel/cpufeature.c | 82 +++++++++++++++++++++++++++-- 2 files changed, 85 insertions(+), 5 deletions(-)
arch/arm64/include/asm/cpufeature.h +8 -0
@@ -63,6 +63,12 @@ struct arm64_ftr_bits {s64 safe_val;/* safe value for FTR_EXACT features */ }; +/* Terminator for an array of struct arm64_ftr_bits */ +#define ARM64_FTR_END \ + { \ + .width = 0, \ + } +/* * Describe the early feature override to the core override code: *
@@ -632,6 +638,8 @@ void check_local_cpu_capabilities(void);u64 read_sanitised_ftr_reg (u32 id);u64 __read_sysreg_by_encoding (u32 sys_id); +bool arm64_ftr_reg_valid (u32 sys_id,u64 val,const struct arm64_ftr_bits *override); +static inline bool cpu_supports_mixed_endian_el0 (void ) {return id_aa64mmfr0_mixed_endian_el0 (read_cpuid (ID_AA64MMFR0_EL1 ));
arch/arm64/kernel/cpufeature.c +77 -5
94 lines not on this page · raw message
Signature · 1 line
2.35.1.894.gb6a874cedc-goog
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v6 01/25] KVM: arm64: Introduce a validation function for an ID register
Hi Oliver,
Quoted 13 lines · Reiji Watanabe
Quoted 12 lines
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 fromarm64_ftr_regs, with some entries overwritten by entries fromarm64_ftr_bits_kvm_override.
Quoted 6 lines
Quoted 1 line
Signed-off-by Reiji Watanabe <[email protected]>I have some concerns regarding the API between cpufeature and KVM that's being proposed here. It would appear that we are adding some of KVM's implementation details into the cpufeature code. In particular, I see that KVM's limitations on AA64DFR0 are being copied here.
I assume "KVM's limitation details" you meant is about ftr_id_aa64dfr0_kvm.
Entries in arm64_ftr_bits_kvm_override shouldn't be added based on KVM's implementation. When cpufeature.c doesn't handle lower level of (or fewer) features as the "safe" value for fields, the field should be added to arm64_ftr_bits_kvm_override. As PMUVER and DEBUGVER are not treated as LOWER_SAFE, they were added in arm64_ftr_bits_kvm_override. Having said that, although ftr_id_aa64dfr0 has PMUVER as signed field, I didn't fix that in ftr_id_aa64dfr0_kvm, and the code abused that.... I should make PMUVER unsigned field, and fix cpufeature.c to validate the field based on its special ID scheme for cleaner abstraction. (And KVM should skip the cpufeature.c's PMUVER validation using
id_reg_desc's ignore_mask and have KVM validate it locally based on the KVM's special requirement)
Quoted 7 lines
Additionally, I think it would be preferable to expose this in a manner that does not require
CONFIG_KVMguards in other parts of the kernel.WDYT about having KVM keep its set of feature overrides and otherwise rely on the kernel's feature tables? I messed around with the idea a bit until I could get something workable (attached). I only compile tested this :)
Thanks for the proposal!
But, providing the overrides to arm64_ftr_reg_valid() means that its consumer knows implementation details of cpufeture.c (values of entries in arm64_ftr_regs[]), which is a similar abstraction problem I want to avoid (the default validation by cpufeature.c should be purely based on ID schemes even with this option).
Another option that I considered earlier was having a full set of arm64_ftr_bits in KVM for its validation. At the time, I thought statically) having a full set of arm64_ftr_bits in KVM is not good in terms of maintenance. But, considering that again, since most of fields are unsigned and lower safe fields, and KVM doesn't necessarily have to statically have a full set of arm64_ftr_bits (can dynamically generate during KVM's initialization), it may not be that bad. So, I am thinking of exploring this option.
More specifically, changes in cpufeature.c from patch-1 will be below and remove all other newly added codes from cpufeature.c. (Need more changes in KVM)
---
arch/arm64/kernel/cpufeature.c +0 -0
@@ -3357,9 +3357,9 @@ static const struct arm64_ftr_bits
*get_arm64_ftr_bits_kvm(u32 sys_id)
* scheme, the function checks if values of the fields in @val are the same
* as the ones in @limit.
*/
-int arm64_check_features_kvm(u32 sys_reg, u64 val, u64 limit) +int arm64_check_features(u32 sys_reg, const struct arm64_ftr_bits *ftrp,
+ u64 val, u64 limit)
{
- const struct arm64_ftr_bits *ftrp = get_arm64_ftr_bits_kvm(sys_reg);
u64 exposed_mask = 0;
if (!ftrp)---
Thanks,
Reiji
Re: [PATCH v6 01/25] KVM: arm64: Introduce a validation function for an ID register
Quoted 22 lines · Reiji Watanabe
Hi Oliver,
Quoted 13 lines · Reiji Watanabe
Quoted 12 lines
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 fromarm64_ftr_regs, with some entries overwritten by entries fromarm64_ftr_bits_kvm_override.Quoted 6 lines
Quoted 1 line
Signed-off-by Reiji Watanabe <[email protected]>I have some concerns regarding the API between cpufeature and KVM that's being proposed here. It would appear that we are adding some of KVM's implementation details into the cpufeature code. In particular, I see that KVM's limitations on AA64DFR0 are being copied here.
Quoted 7 lines
I assume "KVM's limitation details" you meant is about
ftr_id_aa64dfr0_kvm.
Entries inarm64_ftr_bits_kvm_overrideshouldn't be added based on KVM's implementation. Whencpufeature.cdoesn't handle lower level of (or fewer) features as the "safe" value for fields, the field should be added toarm64_ftr_bits_kvm_override. As PMUVER and DEBUGVER are not treated asLOWER_SAFE, they were added inarm64_ftr_bits_kvm_override.
I believe the fact that KVM is more permissive on PMUVER and DEBUGVER than cpufeature is in fact a detail of KVM, no? read_id_reg() already implicitly trusts the cpufeature code filtering and applies additional limitations on top of what we get back. Similarly, there are fields where KVM is more restrictive than cpufeature (ID_AA64DFR0_PMSVER).
Each of those constraints could theoretically be expressed as an arm64_ftr_bits structure within KVM.
Quoted 4 lines
Having said that, although
ftr_id_aa64dfr0has PMUVER as signed field, I didn't fix that inftr_id_aa64dfr0_kvm, and the code abused that.... I should make PMUVER unsigned field, and fixcpufeature.cto validate the field based on its special ID scheme for cleaner abstraction.
Good point. AA64DFR0 is an annoying register :)
Quoted 13 lines
(And KVM should skip the cpufeature.c's PMUVER validation using
id_reg_desc'signore_maskand have KVM validate it locally based on the KVM's special requirement)Quoted 7 lines
Additionally, I think it would be preferable to expose this in a manner that does not require
CONFIG_KVMguards in other parts of the kernel.WDYT about having KVM keep its set of feature overrides and otherwise rely on the kernel's feature tables? I messed around with the idea a bit until I could get something workable (attached). I only compile tested this :)
Quoted 6 lines
Thanks for the proposal!
But, providing the overrides toarm64_ftr_reg_valid()means that its consumer knows implementation details ofcpufeture.c(values of entries in arm64_ftr_regs[]), which is a similar abstraction problem I want to avoid (the default validation bycpufeature.cshould be purely based on ID schemes even with this option).
It is certainly a matter of where you choose to draw those lines. We already do bank on the implementation details of cpufeature.c quite heavily, its just stuffed away behind read_sanitised_ftr_reg(). Now we have KVM bits and pieces in cpufeature.c and might wind up forcing others to clean up our dirty laundry in the future.
It also seems to me that if I wanted to raise the permitted DEBUGVER for KVM, would I have to make a change outside of KVM.
Quoted 6 lines
Another option that I considered earlier was having a full set of
arm64_ftr_bitsin KVM for its validation. At the time, I thought statically) having a full set ofarm64_ftr_bitsin KVM is not good in terms of maintenance. But, considering that again, since most of fields are unsigned and lower safe fields, and KVM doesn't necessarily have to statically have a full set ofarm64_ftr_bits
I think the argument could be made for KVM having its own static + verbose cpufeature tables. We've already been bitten by scenarios where cpufeature exposes a feature that we simply do not virtualize in KVM. That really can become a game of whack-a-mole. commit 96f4f6809bee ("KVM: arm64: Don't advertise FEAT_SPE to guests") is a good example, and I can really see no end to these sorts of issues without an overhaul. We'd need to also find a way to leverage the existing infrasturcture for working out a system-wide safe value, but this time with KVM's table of registers.
KVM would then need to take a change to expose any new feature that has no involvement of EL2. Personally, I'd take that over the possibility of another unhandled feature slipping through and blowing up a guest kernel when running on newer hardware.
Quoted 1 line
(dynamically generate during KVM's initialization)
This was another one of my concerns with the current state of this patch. I found the register table construction at runtime hard to follow. I think it could be avoided with a helper that has a prescribed set of rules (caller-provided field definition takes precedence over the general one).
Sorry it took me a bit to comment on everything, needed to really sit down and wrap my head around the series.
--
Thanks,
Oliver
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v6 01/25] KVM: arm64: Introduce a validation function for an ID register
Hi Oliver,
Quoted 13 lines
Quoted 5 lines
Quoted 4 lines
I have some concerns regarding the API between cpufeature and KVM that's being proposed here. It would appear that we are adding some of KVM's implementation details into the cpufeature code. In particular, I see that KVM's limitations on AA64DFR0 are being copied here.
Quoted 7 lines
I assume "KVM's limitation details" you meant is about
ftr_id_aa64dfr0_kvm.
Entries inarm64_ftr_bits_kvm_overrideshouldn't be added based on KVM's implementation. Whencpufeature.cdoesn't handle lower level of (or fewer) features as the "safe" value for fields, the field should be added toarm64_ftr_bits_kvm_override. As PMUVER and DEBUGVER are not treated asLOWER_SAFE, they were added inarm64_ftr_bits_kvm_override.
Quoted 2 lines
I believe the fact that KVM is more permissive on PMUVER and DEBUGVER than cpufeature is in fact a detail of KVM, no?
read_id_reg()already
What cpufeature knows is that consumers of the validation function needs the validation of each field based on ID register schemes that are described in Arm ARM (basically lower safe).
As lower values of PMUVER/DEBUGVER indicates lower level of features or fewer level of features, those entries are to provide validation based on that. So, entries in arm64_ftr_bits_kvm_override will be added to adjust cpufeture's behavior based on ID register schemes, and KVM may or may not use them.
I need to remove the word "kvm" from variable/function/structure names and put more clear comments:)
Quoted 6 lines
implicitly trusts the cpufeature code filtering and applies additional limitations on top of what we get back. Similarly, there are fields where KVM is more restrictive than cpufeature (
ID_AA64DFR0_PMSVER).Each of those constraints could theoretically be expressed as an
arm64_ftr_bitsstructure within KVM.
It's not impossible but it's a bit tricky (With __arm64_ftr_reg_valid(), it might look straight forward, but I don't think that treats FTR_EXACT correctly. Please see update_cpu_ftr_reg).
Quoted 27 lines
Quoted 4 lines
Having said that, although
ftr_id_aa64dfr0has PMUVER as signed field, I didn't fix that inftr_id_aa64dfr0_kvm, and the code abused that.... I should make PMUVER unsigned field, and fixcpufeature.cto validate the field based on its special ID scheme for cleaner abstraction.Good point. AA64DFR0 is an annoying register :)
Quoted 13 lines
(And KVM should skip the cpufeature.c's PMUVER validation using
id_reg_desc'signore_maskand have KVM validate it locally based on the KVM's special requirement)Quoted 7 lines
Additionally, I think it would be preferable to expose this in a manner that does not require
CONFIG_KVMguards in other parts of the kernel.WDYT about having KVM keep its set of feature overrides and otherwise rely on the kernel's feature tables? I messed around with the idea a bit until I could get something workable (attached). I only compile tested this :)
Quoted 6 lines
Thanks for the proposal!
But, providing the overrides toarm64_ftr_reg_valid()means that its consumer knows implementation details ofcpufeture.c(values of entries in arm64_ftr_regs[]), which is a similar abstraction problem I want to avoid (the default validation bycpufeature.cshould be purely based on ID schemes even with this option).
Quoted 5 lines
It is certainly a matter of where you choose to draw those lines. We already do bank on the implementation details of
cpufeature.cquite heavily, its just stuffed away behindread_sanitised_ftr_reg(). Now we have KVM bits and pieces incpufeature.cand might wind up forcing others to clean up our dirty laundry in the future.
As I mentioned above, they aren't KVM specific.
Quoted 2 lines
It also seems to me that if I wanted to raise the permitted DEBUGVER for KVM, would I have to make a change outside of KVM.
Could you elaborate this a little more?
More specific concern I have about providing the override (with the existing arm64_ftr_bits) would be when field values of arm64_ftr_bits (i.e. LOWER_SAFE to EXACT) in cpufeature are changed due to kernel's implementation reasons, which might affect KVM (may need to pass extra override to arm64_ftr_reg_valid).
But, by having cpufeature provide the validation based on the ID register schemes, cpufeature should be changed to provide the same validation in that case (i.e. if DFR0.PERFMON is changed from LOWER_SAFE to EXACT like AA64DFR0.PMUVER, DFR0.PERFMON should be added in arm64_ftr_bits_kvm_override with LOWER_SAFE).
So, if I go with the option to provide override to cpufeature, IMHO it would be preferable for cpufeature to provide the validation based on ID schemes instead of with the current need-based policy (, which might get changed) for clear separation.
Quoted 9 lines
Quoted 6 lines
Another option that I considered earlier was having a full set of
arm64_ftr_bitsin KVM for its validation. At the time, I thought statically) having a full set ofarm64_ftr_bitsin KVM is not good in terms of maintenance. But, considering that again, since most of fields are unsigned and lower safe fields, and KVM doesn't necessarily have to statically have a full set ofarm64_ftr_bitsI think the argument could be made for KVM having its own static + verbose cpufeature tables. We've already been bitten by scenarios where
What does "verbose cpufeature tables" mean ?
Quoted 11 lines
cpufeature exposes a feature that we simply do not virtualize in KVM. That really can become a game of whack-a-mole. commit
96f4f6809bee("KVM: arm64: Don't advertiseFEAT_SPEto guests") is a good example, and I can really see no end to these sorts of issues without an overhaul. We'd need to also find a way to leverage the existing infrasturcture for working out a system-wide safe value, but this time with KVM's table of registers.
KVM would then need to take a change to expose any new feature that has no involvement of EL2. Personally, I'd take that over the possibility of another unhandled feature slipping through and blowing up a guest kernel when running on newer hardware.
Userspace with configurable ID registers would eliminate such problems on known systems, but I agree that KVM itself should prevent it. It will be inconvenient for some people, but it would be safer in general.
Quoted 7 lines
Quoted 1 line
(dynamically generate during KVM's initialization)
This was another one of my concerns with the current state of this patch. I found the register table construction at runtime hard to follow. I think it could be avoided with a helper that has a prescribed set of rules (caller-provided field definition takes precedence over the general one).
Sure, I will improve that if I continue to keep the current way. With the option of having a separate KVM's arm64_ftr_bits, the code will be very different, but I will keep that in mind.
Thanks,
Reiji
Re: [PATCH v6 01/25] KVM: arm64: Introduce a validation function for an ID register
Hi Reiji,
Quoted 27 lines · Reiji Watanabe
Hi Oliver,
Quoted 13 lines
Quoted 5 lines
Quoted 4 lines
I have some concerns regarding the API between cpufeature and KVM that's being proposed here. It would appear that we are adding some of KVM's implementation details into the cpufeature code. In particular, I see that KVM's limitations on AA64DFR0 are being copied here.
Quoted 7 lines
I assume "KVM's limitation details" you meant is about
ftr_id_aa64dfr0_kvm.
Entries inarm64_ftr_bits_kvm_overrideshouldn't be added based on KVM's implementation. Whencpufeature.cdoesn't handle lower level of (or fewer) features as the "safe" value for fields, the field should be added toarm64_ftr_bits_kvm_override. As PMUVER and DEBUGVER are not treated asLOWER_SAFE, they were added inarm64_ftr_bits_kvm_override.Quoted 2 lines
I believe the fact that KVM is more permissive on PMUVER and DEBUGVER than cpufeature is in fact a detail of KVM, no?
read_id_reg()alreadyWhat cpufeature knows is that consumers of the validation function needs the validation of each field based on ID register schemes that are described in Arm ARM (basically lower safe).
As lower values of PMUVER/DEBUGVER indicates lower level of features or fewer level of features, those entries are to provide validation based on that. So, entries inarm64_ftr_bits_kvm_overridewill be added to adjust cpufeture's behavior based on ID register schemes, and KVM may or may not use them.
Quoted 2 lines
I need to remove the word "kvm" from variable/function/structure names and put more clear comments:)
I'll admit I definitely drilled down on the fact that KVM is the only actual user of these, and not the fact that it was realigning the fields with the Arm ARM :)
Quoted 11 lines
Quoted 6 lines
implicitly trusts the cpufeature code filtering and applies additional limitations on top of what we get back. Similarly, there are fields where KVM is more restrictive than cpufeature (
ID_AA64DFR0_PMSVER).Each of those constraints could theoretically be expressed as an
arm64_ftr_bitsstructure within KVM.It's not impossible but it's a bit tricky (With
__arm64_ftr_reg_valid(), it might look straight forward, but I don't think that treatsFTR_EXACTcorrectly. Please seeupdate_cpu_ftr_reg).
Ah right. __arm64_ftr_reg_valid() needs to trust either the value that comes from the boot CPU, or ->safe_val if the cores are different in the system. And what does it mean if the caller specified FTR_EXACT?
I'll think on this more if I have any other suggestions.
[...]
Quoted 4 lines
Quoted 2 lines
It also seems to me that if I wanted to raise the permitted DEBUGVER for KVM, would I have to make a change outside of KVM.
Could you elaborate this a little more?
Urgh. Ignore me, I fixated to heavily on the SAFE_VAL you used for DEBUGVER, not the fact that it was LOWER_SAFE.
Quoted 11 lines
More specific concern I have about providing the override (with the existing
arm64_ftr_bits) would be when field values ofarm64_ftr_bits(i.e.LOWER_SAFEto EXACT) in cpufeature are changed due to kernel's implementation reasons, which might affect KVM (may need to pass extra override toarm64_ftr_reg_valid).
But, by having cpufeature provide the validation based on the ID register schemes, cpufeature should be changed to provide the same validation in that case (i.e. if DFR0.PERFMON is changed fromLOWER_SAFEto EXACT like AA64DFR0.PMUVER, DFR0.PERFMON should be added inarm64_ftr_bits_kvm_overridewithLOWER_SAFE).
Quoted 4 lines
So, if I go with the option to provide override to cpufeature, IMHO it would be preferable for cpufeature to provide the validation based on ID schemes instead of with the current need-based policy (, which might get changed) for clear separation.
Sounds good. Per your suggestion above, changing the naming/documentation around what is being added to cpufeature removes the confusion that it relates to KVM and really is a precise implementation of the rules in the Arm ARM.
Quoted 11 lines
Quoted 9 lines
Quoted 6 lines
Another option that I considered earlier was having a full set of
arm64_ftr_bitsin KVM for its validation. At the time, I thought statically) having a full set ofarm64_ftr_bitsin KVM is not good in terms of maintenance. But, considering that again, since most of fields are unsigned and lower safe fields, and KVM doesn't necessarily have to statically have a full set ofarm64_ftr_bitsI think the argument could be made for KVM having its own static + verbose cpufeature tables. We've already been bitten by scenarios where
What does "verbose cpufeature tables" mean ?
Currently KVM implements a sparsely-defined denylist on top of whatever we get back from read_sanitised_ftr_reg(). We do not have an absolute upper bound for all fields in the feature registers, so there are times where unsupported features leak through to the guest much like the SPE commit I mentioned below.
What I am suggesting is that KVM define an absolute limit on what it virtualizes for *all* fields, including what is presently RAZ. We have absolutely no idea whether or not we can virtualize new features that come in later revisions of the spec. It does mean we will need to raise those limits from time to time, but would rather do that than accidentally expose a feature we cannot virtualize.
Quoted 12 lines
Quoted 11 lines
cpufeature exposes a feature that we simply do not virtualize in KVM. That really can become a game of whack-a-mole. commit
96f4f6809bee("KVM: arm64: Don't advertiseFEAT_SPEto guests") is a good example, and I can really see no end to these sorts of issues without an overhaul. We'd need to also find a way to leverage the existing infrasturcture for working out a system-wide safe value, but this time with KVM's table of registers.
KVM would then need to take a change to expose any new feature that has no involvement of EL2. Personally, I'd take that over the possibility of another unhandled feature slipping through and blowing up a guest kernel when running on newer hardware.
Quoted 3 lines
Userspace with configurable ID registers would eliminate such problems on known systems, but I agree that KVM itself should prevent it. It will be inconvenient for some people, but it would be safer in general.
We cannot require userspace to write to these registers to run a guest given the fact that the present ABI doesn't. Given that fact, KVM is still responsible for having sane default values for these registers.
If a field that we do not handle implies a feature we do not virtualize on newer hardware, invariably our guest will see it and likely panic when it realizes the vCPU is out of spec.
Maybe the feature bits tables is a bit extreme given the fact that it does define the architected handling of each field. I think the upper bound on register values I mentioned above would do the trick and avoid copy/pasting a whole set of structures we don't desperately need.
Quoted 11 lines
Quoted 7 lines
Quoted 1 line
(dynamically generate during KVM's initialization)
This was another one of my concerns with the current state of this patch. I found the register table construction at runtime hard to follow. I think it could be avoided with a helper that has a prescribed set of rules (caller-provided field definition takes precedence over the general one).
Sure, I will improve that if I continue to keep the current way. With the option of having a separate KVM's
arm64_ftr_bits, the code will be very different, but I will keep that in mind.
arm64_ftr_bits might be a bit extreme in KVM after all, I'll retract that suggestion in favor of what I said above :)
Thanks for being patient working through all of this with me.
--
Best,
Oliver
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v6 01/25] KVM: arm64: Introduce a validation function for an ID register
Hi Oliver,
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
implicitly trusts the cpufeature code filtering and applies additional limitations on top of what we get back. Similarly, there are fields where KVM is more restrictive than cpufeature (
ID_AA64DFR0_PMSVER).Each of those constraints could theoretically be expressed as an
arm64_ftr_bitsstructure within KVM.
Quoted 8 lines
Quoted 4 lines
It's not impossible but it's a bit tricky (With
__arm64_ftr_reg_valid(), it might look straight forward, but I don't think that treatsFTR_EXACTcorrectly. Please seeupdate_cpu_ftr_reg).Ah right.
__arm64_ftr_reg_valid()needs to trust either the value that comes from the boot CPU, or ->safe_val if the cores are different in the system. And what does it mean if the caller specifiedFTR_EXACT?
What I was trying to say is __arm64_ftr_reg_valid() should check if the 'bits' are the same as 'safe_bits' and should treat 'bits' as safe if they are the same instead of calling arm64_ftr_field_valid() as update_cpu_ftr_reg does.
In my understanding, FTR_EXACT doesn't mean only 'ftrp->safe_val' is a safe value, but if two values are different, 'ftrp->safe_val' is the only safe value.
Quoted 37 lines
Quoted 11 lines
More specific concern I have about providing the override (with the existing
arm64_ftr_bits) would be when field values ofarm64_ftr_bits(i.e.LOWER_SAFEto EXACT) in cpufeature are changed due to kernel's implementation reasons, which might affect KVM (may need to pass extra override toarm64_ftr_reg_valid).
But, by having cpufeature provide the validation based on the ID register schemes, cpufeature should be changed to provide the same validation in that case (i.e. if DFR0.PERFMON is changed fromLOWER_SAFEto EXACT like AA64DFR0.PMUVER, DFR0.PERFMON should be added inarm64_ftr_bits_kvm_overridewithLOWER_SAFE).Quoted 4 lines
So, if I go with the option to provide override to cpufeature, IMHO it would be preferable for cpufeature to provide the validation based on ID schemes instead of with the current need-based policy (, which might get changed) for clear separation.
Sounds good. Per your suggestion above, changing the naming/documentation around what is being added to cpufeature removes the confusion that it relates to KVM and really is a precise implementation of the rules in the Arm ARM.
Quoted 12 lines
Quoted 11 lines
cpufeature exposes a feature that we simply do not virtualize in KVM. That really can become a game of whack-a-mole. commit
96f4f6809bee("KVM: arm64: Don't advertiseFEAT_SPEto guests") is a good example, and I can really see no end to these sorts of issues without an overhaul. We'd need to also find a way to leverage the existing infrasturcture for working out a system-wide safe value, but this time with KVM's table of registers.
KVM would then need to take a change to expose any new feature that has no involvement of EL2. Personally, I'd take that over the possibility of another unhandled feature slipping through and blowing up a guest kernel when running on newer hardware.Quoted 3 lines
Userspace with configurable ID registers would eliminate such problems on known systems, but I agree that KVM itself should prevent it. It will be inconvenient for some people, but it would be safer in general.
Quoted 7 lines
We cannot require userspace to write to these registers to run a guest given the fact that the present ABI doesn't. Given that fact, KVM is still responsible for having sane default values for these registers.
If a field that we do not handle implies a feature we do not virtualize on newer hardware, invariably our guest will see it and likely panic when it realizes the vCPU is out of spec.
I completely agree on this, and I will work on this separately from this series as it is a different issue from what this series tries to address.
Quoted 4 lines
Maybe the feature bits tables is a bit extreme given the fact that it does define the architected handling of each field. I think the upper bound on register values I mentioned above would do the trick and avoid copy/pasting a whole set of structures we don't desperately need.
Yes, I have been thinking the same (having a max register value for each register rather than having a max value for each field).
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 1 line
(dynamically generate during KVM's initialization)
This was another one of my concerns with the current state of this patch. I found the register table construction at runtime hard to follow. I think it could be avoided with a helper that has a prescribed set of rules (caller-provided field definition takes precedence over the general one).
Quoted 6 lines
Quoted 3 lines
Sure, I will improve that if I continue to keep the current way. With the option of having a separate KVM's
arm64_ftr_bits, the code will be very different, but I will keep that in mind.
arm64_ftr_bitsmight be a bit extreme in KVM after all, I'll retract that suggestion in favor of what I said above :)
Having a full set of arm64_ftr_bits in KVM I meant here is the one that I mentioned earlier, and I am inclined to go with the option rather than having cpufeature provide ID scheme based validation and the override validation. With that, KVM will still use a validation function that cpufeture will provide, but as it is done only based on arm64_ftr_bits provided by KVM (FTR_UNSIGNED + LOWER_SAFE entries will be generated during KVM init, and the other entries will be statically defined in KVM).
Compared to this separate arm64_ftr_bits option, there are two things that I don't like a bit about the ID scheme based validation option. One is special changes we will make in cpufeature to provide ID scheme based validation (like for ID_AA64DFR0_EL0.PMUVER) as needed won't be used by KVM when KVM needs another implementation (our efforts might be wasted). The other is (I think) it's pretty easy to forget to make changes in cpufeature to provide ID scheme based validation for new fields when needed.
Along with future changes to set the maximum limit for each ID register field, I would think this separate arm64_ftr_bits option could provide a safer way for KVM to control/maintain feature visibilities for the guest more independently with smaller maintenance cost.
The downside of having the separate arm64_ftr_bits will be that there are extra static arm64_ftr_bits entries that need to be defined for FTR_SIGNED + LOWER_SAFE. It's a so big deal though because there are just 6(?) of such entries, and I don't think we will newly have many of such entries.
Thanks,
Reiji
PATCH v6 2/25 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 · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v6 02/25] KVM: arm64: Save ID registers' sanitized value per guest
Hi Reiji,
Quoted 17 lines · Reiji Watanabe
Introduce id_regs[] in
kvm_archas a storage of guest's ID registers, and save ID registers' sanitized value in the array atKVM_CREATE_VM. Use the saved ones when ID registers are read by the guest or userspace (viaKVM_GET_ONE_REG).Signed-off-by Reiji Watanabe <[email protected]>--- 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 +0
-0
arch/arm64/include/asm/kvm_host.h struct kvm_s2_mmu { · line 106
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +3
-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.
Doesn't the Feature ID register scheme only apply to CRm={1-7}, op2={0-7}? I believe CRm=0, op2={1-4,7} are in fact UNDEFINED, not RAZ like the other ranges. Furthermore, the registers that are defined in that range do not go through the read_id_reg() plumbing.
Quoted 8 lines
arch/arm64/include/asm/kvm_host.h +4
-0+*/ +#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;
arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 149
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +3
-0@@ -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 ];
This is a decently large array. Should we embed it in kvm_arch or allocate at init?
[...]
arch/arm64/include/asm/kvm_host.h struct kvm_arch {
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +6
-0+ +/* +* Set the guest's ID registers that are defined in sys_reg_descs[] +* with ID_SANITISED() to the host's sanitized value. +*/ +void set_default_id_regs (struct kvm *kvm)
nit, more relevant if you take the above suggestion: maybe call it kvm_init_id_regs()?
arch/arm64/include/asm/kvm_host.h
Quoted 7 lines
arch/arm64/include/asm/kvm_host.h +7
-0+{ +int i; +u32 id; +const struct sys_reg_desc *rd; +u64 val; + +for (i =0 ; i <ARRAY_SIZE (sys_reg_descs); i++) {
You could avoid walking the entire system register table, since we already know the start and end values for the Feature ID register range.
maybe:
#define FEATURE_ID_RANGE_START SYS_ID_PFR0_EL1 #define FEATURE_ID_RANGE_END sys_reg(3, 0, 0, 7, 7)
u32 sys_reg;
for (sys_reg = FEATURE_ID_RANGE_START; sys_reg <= FEATURE_ID_RANGE_END; sys_reg++)
But, it depends on if this check is necessary:
arch/arm64/include/asm/kvm_host.h
Quoted 4 lines
arch/arm64/include/asm/kvm_host.h +4
-0+ rd = &sys_reg_descs[i]; +if (rd->access != access_id_reg) +/* Not ID register, or hidden/reserved ID register */ +continue ;
Which itself is dependent on whether KVM is going to sparsely or verbosely define its feature filtering tables per the other thread. So really only bother with this if that is the direction you're going.
--
Thanks,
Oliver
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v6 02/25] KVM: arm64: Save ID registers' sanitized value per guest
Hi Oliver,
Quoted 23 lines · Oliver Upton
Hi Reiji,
Quoted 20 lines · Reiji Watanabe
Introduce id_regs[] in
kvm_archas a storage of guest's ID registers, and save ID registers' sanitized value in the array atKVM_CREATE_VM. Use the saved ones when ID registers are read by the guest or userspace (viaKVM_GET_ONE_REG).Signed-off-by Reiji Watanabe <[email protected]>--- 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 +0
-0@@ -101,6 +101,13 @@ struct kvm_s2_mmu {struct kvm_arch_memory_slot { };
Quoted 8 lines
arch/arm64/include/asm/kvm_host.h struct kvm_s2_mmu { · line 106
Quoted 3 lines
arch/arm64/include/asm/kvm_host.h +3
-0+/* +* (Op0, Op1, CRn, CRm, Op2) of ID registers is (3, 0, 0, crm, op2), +* where 0<=crm<8, 0<=op2<8. Doesn't the Feature ID register scheme only apply to CRm={1-7}, op2={0-7}? I believe CRm=0, op2={1-4,7} are in fact UNDEFINED, not RAZ like the other ranges. Furthermore, the registers that are defined in that range do not go through the
read_id_reg()plumbing.
Will fix this.
Quoted 8 lines
Quoted 8 lines
arch/arm64/include/asm/kvm_host.h +4
-0+*/ +#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;
Quoted 10 lines
arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 149
Quoted 7 lines
arch/arm64/include/asm/kvm_host.h +3
-0@@ -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 ];This is a decently large array. Should we embed it in
kvm_archor allocate at init?
What is the reason why you think you might want to allocate it at init ?
Quoted 32 lines
[...]
arch/arm64/include/asm/kvm_host.h
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +6
-0+ +/* +* Set the guest's ID registers that are defined in sys_reg_descs[] +* with ID_SANITISED() to the host's sanitized value. +*/ +void set_default_id_regs (struct kvm *kvm)nit, more relevant if you take the above suggestion: maybe call it
kvm_init_id_regs()?arch/arm64/include/asm/kvm_host.h
Quoted 7 lines
arch/arm64/include/asm/kvm_host.h +7
-0+{ +int i; +u32 id; +const struct sys_reg_desc *rd; +u64 val; + +for (i =0 ; i <ARRAY_SIZE (sys_reg_descs); i++) {You could avoid walking the entire system register table, since we already know the start and end values for the Feature ID register range.> maybe:
#define FEATURE_ID_RANGE_START SYS_ID_PFR0_EL1 #define FEATURE_ID_RANGE_END sys_reg(3, 0, 0, 7, 7)u32
sys_reg;for (
sys_reg=FEATURE_ID_RANGE_START;sys_reg<=FEATURE_ID_RANGE_END; sys_reg++)But, it depends on if this check is necessary:
Quoted 8 lines
arch/arm64/include/asm/kvm_host.h
Quoted 4 lines
arch/arm64/include/asm/kvm_host.h +4
-0+ rd = &sys_reg_descs[i]; +if (rd->access != access_id_reg) +/* Not ID register, or hidden/reserved ID register */ +continue ;Which itself is dependent on whether KVM is going to sparsely or verbosely define its feature filtering tables per the other thread. So really only bother with this if that is the direction you're going.
Even just going through for ID register ranges, we should do the check to skip hidden/reserved ID registers (not to call read_sanitised_ftr_reg).
Yes, it's certainly possible to avoid walking the entire system register, and I will fix it. The reason why I didn't care it so much was just because the code (walking the entire system register) will be removed by the following patches:)
Thanks,
Reiji
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v6 02/25] KVM: arm64: Save ID registers' sanitized value per guest
Hi Reiji,
On Thu, Mar 24, 2022 at 09:23:10AM -0700, Reiji Watanabe wrote:
[...]
Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
+ */
arch/arm64/include/asm/kvm_host.h +3
-0+#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 */ Quoted 8 lines
arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 149
Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +3
-0bool mte_enabled;bool ran_once; + +/* ID registers for the guest. */ +u64 id_regs[KVM_ARM_ID_REG_MAX_NUM ];This is a decently large array. Should we embed it in
kvm_archor allocate at init?
Quoted 1 line
What is the reason why you think you might want to allocate it at init ?
Well, its a 512 byte array of mostly cold data. We're probably convinced that the guest is going to access these registers at most once per vCPU at boot.
For the vCPU context at least, we only allocate space for registers we actually care about (enum vcpu_sysreg). My impression of the feature register ranges is that there are a lot of registers which are RAZ, so I don't believe we need to make room for uninteresting values.
Additionally, struct kvm is visible to EL2 if running nVHE. I don't believe hyp will ever need to look at these register values.
[...]
Quoted 11 lines
Quoted 3 lines
Which itself is dependent on whether KVM is going to sparsely or verbosely define its feature filtering tables per the other thread. So really only bother with this if that is the direction you're going.
Even just going through for ID register ranges, we should do the check to skip hidden/reserved ID registers (not to call
read_sanitised_ftr_reg).Yes, it's certainly possible to avoid walking the entire system register, and I will fix it. The reason why I didn't care it so much was just because the code (walking the entire system register) will be removed by the following patches:)
Let me go through the series again and see how this flows. If there is a way to avoid rewriting code introduced earlier in the series I would suggest going that route.
--
Thanks,
Oliver
Re: [PATCH v6 02/25] KVM: arm64: Save ID registers' sanitized value per guest
Hi Oliver,
Quoted 24 lines
Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
+ */
arch/arm64/include/asm/kvm_host.h +3
-0+#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 */ Quoted 8 lines
arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 149
Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +3
-0bool mte_enabled;bool ran_once; + +/* ID registers for the guest. */ +u64 id_regs[KVM_ARM_ID_REG_MAX_NUM ];This is a decently large array. Should we embed it in
kvm_archor allocate at init?Quoted 1 line
What is the reason why you think you might want to allocate it at init ?
Well, its a 512 byte array of mostly cold data. We're probably convinced that the guest is going to access these registers at most once per vCPU at boot.
Quoted 7 lines
For the vCPU context at least, we only allocate space for registers we actually care about (enum
vcpu_sysreg). My impression of the feature register ranges is that there are a lot of registers which are RAZ, so I don't believe we need to make room for uninteresting values.Additionally, struct kvm is visible to EL2 if running nVHE. I don't believe hyp will ever need to look at these register values.
As saving/restoring breakpoint/watchpoint registers for guests might need a special handling when AA64DFR0_EL1.BRPs get changed, next version of the series will use the data in the array at EL2 nVHE. They are cold data, and almost half of the entries will be RAZ at the moment though:)
Thanks,
Reiji
Re: [PATCH v6 02/25] KVM: arm64: Save ID registers' sanitized value per guest
Quoted 31 lines · Reiji Watanabe
Hi Oliver,
Quoted 29 lines
Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
+ */
arch/arm64/include/asm/kvm_host.h +3
-0+#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 */ Quoted 8 lines
arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 149
Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +3
-0bool mte_enabled;bool ran_once; + +/* ID registers for the guest. */ +u64 id_regs[KVM_ARM_ID_REG_MAX_NUM ];This is a decently large array. Should we embed it in
kvm_archor allocate at init?Quoted 1 line
What is the reason why you think you might want to allocate it at init ?
Well, its a 512 byte array of mostly cold data. We're probably convinced that the guest is going to access these registers at most once per vCPU at boot.
For the vCPU context at least, we only allocate space for registers we actually care about (enum
vcpu_sysreg). My impression of the feature register ranges is that there are a lot of registers which are RAZ, so I don't believe we need to make room for uninteresting values.
Quoted 8 lines
Quoted 2 lines
Additionally, struct kvm is visible to EL2 if running nVHE. I don't believe hyp will ever need to look at these register values.
As saving/restoring breakpoint/watchpoint registers for guests might need a special handling when AA64DFR0_EL1.BRPs get changed, next version of the series will use the data in the array at EL2 nVHE. They are cold data, and almost half of the entries will be RAZ at the moment though:)
Shouldn't we always be doing a full context switch based on what registers are present in hardware? We probably don't want to leave host watchpoints/breakpoints visible to the guest.
Additionally, if we are narrowing the guest's view of the debug hardware, are we going to need to start trapping debug register accesses? Unexposed breakpoint registers are supposed to UNDEF if we obey the Arm ARM to the letter. Even if we decide to not care about unexposed regs, I believe certain combinations of ID_AA64DF0_EL1.{BRPs, CTX_CMPs} might require that we emulate.
--
Thanks,
Oliver
Re: [PATCH v6 02/25] KVM: arm64: Save ID registers' sanitized value per guest
Hi Oliver,
Quoted 42 lines · Oliver Upton
Quoted 31 lines · Reiji Watanabe
Hi Oliver,
Quoted 29 lines
Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
+ */
arch/arm64/include/asm/kvm_host.h +3
-0+#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 */ Quoted 8 lines
arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 149
Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +3
-0bool mte_enabled;bool ran_once; + +/* ID registers for the guest. */ +u64 id_regs[KVM_ARM_ID_REG_MAX_NUM ];This is a decently large array. Should we embed it in
kvm_archor allocate at init?Quoted 1 line
What is the reason why you think you might want to allocate it at init ?
Well, its a 512 byte array of mostly cold data. We're probably convinced that the guest is going to access these registers at most once per vCPU at boot.
For the vCPU context at least, we only allocate space for registers we actually care about (enum
vcpu_sysreg). My impression of the feature register ranges is that there are a lot of registers which are RAZ, so I don't believe we need to make room for uninteresting values.Quoted 8 lines
Quoted 2 lines
Additionally, struct kvm is visible to EL2 if running nVHE. I don't believe hyp will ever need to look at these register values.
As saving/restoring breakpoint/watchpoint registers for guests might need a special handling when AA64DFR0_EL1.BRPs get changed, next version of the series will use the data in the array at EL2 nVHE. They are cold data, and almost half of the entries will be RAZ at the moment though:)
Quoted 3 lines
Shouldn't we always be doing a full context switch based on what registers are present in hardware? We probably don't want to leave host watchpoints/breakpoints visible to the guest.
Correct, any host breakpoints/watchpoints won't be exposed to the guest. (When restoring breakpoints/watchpoints for the guest, physical
breakpoints that are not mapped to any virtual ones will be cleared)
Quoted 6 lines
Additionally, if we are narrowing the guest's view of the debug hardware, are we going to need to start trapping debug register accesses? Unexposed breakpoint registers are supposed to UNDEF if we obey the Arm ARM to the letter. Even if we decide to not care about unexposed regs, I believe certain combinations of ID_AA64DF0_EL1.{BRPs,
CTX_CMPs} might require that we emulate.
Exactly, we will need to start trapping debug registers when the special handling is needed, and yes, we will need a special handling when the guest accesses to invalid virtual breakpoints/watchpoints or use the invalid virtual breakpoints as linked breakpoints.
Thanks,
Reiji
PATCH v6 3/25 KVM: arm64: Introduce struct id_reg_desc
In reply to Reiji WatanabeThis patch lays the groundwork to make ID registers writable.
Introduce struct id_reg_desc 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. Use the id_reg_desc to do register specific initialization, validation of the ID register, etc. The id_reg_desc has reg_desc field (struct sys_reg_desc), which will be used instead of sys_reg_desc in sys_reg_descs[] for ID registers in the following patches (and then the entries in sys_reg_descs[] will be removed).
At present, changing an ID register from userspace is allowed only if the ID register has the id_reg_desc, but that will be changed by the following patches.
No ID register has the id_reg_desc yet, and the following patches will add them for all the ID registers currently in sys_reg_descs[].
kvm_set_id_reg_feature(), which is introduced in this patch, is going to be used by the following patch outside 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 | 3 +- arch/arm64/kvm/sys_regs.c | 299 ++++++++++++++++++++++++++++-- 3 files changed, 286 insertions(+), 17 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 +2 -1
@@ -1214,9 +1214,10 @@#define ICH_VTR_TDS_MASK (1 << ICH_VTR_TDS_SHIFT)#define ARM64_FEATURE_FIELD_BITS 4 +#define ARM64_FEATURE_FIELD_MASK GENMASK_ULL(ARM64_FEATURE_FIELD_BITS - 1, 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)) +#define ARM64_FEATURE_MASK (x) (ARM64_FEATURE_FIELD_MASK << x##_SHIFT)#ifdef __ASSEMBLY__
arch/arm64/kvm/sys_regs.c +283 -16
373 lines not on this page · raw message
Signature · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 4/25 KVM: arm64: Make ID_AA64PFR0_EL1 writable
In reply to Reiji WatanabeThis patch adds id_reg_desc 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 | 156 ++++++++++++++++++++------------ arch/arm64/kvm/vgic/vgic-init.c | 9 ++ 3 files changed, 107 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 +97 -59
187 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 · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 5/25 KVM: arm64: Make ID_AA64PFR1_EL1 writable
In reply to Reiji WatanabeThis patch adds id_reg_desc 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 · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 6/25 KVM: arm64: Make ID_AA64ISAR0_EL1 writable
In reply to Reiji WatanabeThis patch adds id_reg_desc 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 | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
arch/arm64/kvm/sys_regs.c +31 -0
@@ -389,6 +389,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_desc *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_desc (struct id_reg_desc *id_reg) {u64 limit = id_reg->vcpu_limit_val;
@@ -3167,6 +3190,11 @@ static struct id_reg_desc id_aa64pfr1_el1_desc = { .vcpu_mask = vcpu_mask_id_aa64pfr1_el1, }; +static struct id_reg_desc id_aa64isar0_el1_desc = { + .reg_desc =ID_SANITISED (ID_AA64ISAR0_EL1 ), + .validate = validate_id_aa64isar0_el1, +}; +#define ID_DESC (id_reg_name, id_reg_desc) \ [IDREG_IDX(SYS_##id_reg_name)] = (id_reg_desc)
@@ -3175,6 +3203,9 @@ static struct id_reg_desc *id_reg_desc_table[KVM_ARM_ID_REG_MAX_NUM] = {/* CRm=4 */ ID_DESC (ID_AA64PFR0_EL1 , &id_aa64pfr0_el1_desc),ID_DESC (ID_AA64PFR1_EL1 , &id_aa64pfr1_el1_desc), + +/* CRm=6 */ + ID_DESC(ID_AA64ISAR0_EL1, &id_aa64isar0_el1_desc), };static inline struct id_reg_desc *get_id_reg_desc (u32 id)
Signature · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 7/25 KVM: arm64: Make ID_AA64ISAR1_EL1 writable
In reply to Reiji WatanabeThis patch adds id_reg_desc 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
@@ -266,6 +266,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_desc {const struct sys_reg_desc reg_desc;
@@ -412,6 +430,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_desc *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_desc (struct id_reg_desc *id_reg) {u64 limit = id_reg->vcpu_limit_val;
@@ -449,8 +497,14 @@ static void init_id_aa64pfr1_el1_desc(struct id_reg_desc *id_reg) id_reg->vcpu_limit_val &= ~ARM64_FEATURE_MASK (ID_AA64PFR1_MTE ); } +static void init_id_aa64isar1_el1_desc (struct id_reg_desc *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_desc *idr) +const struct id_reg_desc *idr) {return vcpu_has_sve (vcpu) ?0 :ARM64_FEATURE_MASK (ID_AA64PFR0_SVE ); }
@@ -461,6 +515,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_desc *idr) +{ +return vcpu_has_ptrauth (vcpu) ?0 :PTRAUTH_MASK ; +} +static int validate_id_reg (struct kvm_vcpu *vcpu,const struct id_reg_desc *id_reg,u64 val) {
@@ -1394,13 +1454,6 @@ static u64 read_id_reg_with_encoding(const struct kvm_vcpu *vcpu, u32 id) val =read_kvm_id_reg (vcpu->kvm, id);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 );
@@ -3195,6 +3248,13 @@ static struct id_reg_desc id_aa64isar0_el1_desc = { .validate = validate_id_aa64isar0_el1, }; +static struct id_reg_desc id_aa64isar1_el1_desc = { + .reg_desc =ID_SANITISED (ID_AA64ISAR1_EL1 ), + .init = init_id_aa64isar1_el1_desc, + .validate = validate_id_aa64isar1_el1, + .vcpu_mask = vcpu_mask_id_aa64isar1_el1, +}; +#define ID_DESC (id_reg_name, id_reg_desc) \ [IDREG_IDX(SYS_##id_reg_name)] = (id_reg_desc)
@@ -3206,6 +3266,7 @@ static struct id_reg_desc *id_reg_desc_table[KVM_ARM_ID_REG_MAX_NUM] = {/* CRm=6 */ ID_DESC (ID_AA64ISAR0_EL1 , &id_aa64isar0_el1_desc), + ID_DESC(ID_AA64ISAR1_EL1, &id_aa64isar1_el1_desc), };static inline struct id_reg_desc *get_id_reg_desc (u32 id)
Signature · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 8/25 KVM: arm64: Make ID_AA64MMFR0_EL1 writable
In reply to Reiji WatanabeThis patch adds id_reg_desc 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 | 129 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+)
arch/arm64/kvm/sys_regs.c +129 -0
@@ -460,6 +460,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_desc *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_desc (struct id_reg_desc *id_reg) {u64 limit = id_reg->vcpu_limit_val;
@@ -3255,6 +3367,20 @@ static struct id_reg_desc id_aa64isar1_el1_desc = { .vcpu_mask = vcpu_mask_id_aa64isar1_el1, }; +static struct id_reg_desc id_aa64mmfr0_el1_desc = { + .reg_desc =ID_SANITISED (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, +}; +#define ID_DESC (id_reg_name, id_reg_desc) \ [IDREG_IDX(SYS_##id_reg_name)] = (id_reg_desc)
@@ -3267,6 +3393,9 @@ static struct id_reg_desc *id_reg_desc_table[KVM_ARM_ID_REG_MAX_NUM] = {/* CRm=6 */ ID_DESC (ID_AA64ISAR0_EL1 , &id_aa64isar0_el1_desc),ID_DESC (ID_AA64ISAR1_EL1 , &id_aa64isar1_el1_desc), + +/* CRm=7 */ + ID_DESC(ID_AA64MMFR0_EL1, &id_aa64mmfr0_el1_desc), };static inline struct id_reg_desc *get_id_reg_desc (u32 id)
Signature · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 9/25 KVM: arm64: Make ID_AA64DFR0_EL1/ID_DFR0_EL1 writable
In reply to Reiji WatanabeThis patch adds id_reg_desc for ID_AA64DFR0_EL1 and ID_DFR0_EL1 to make them writable by userspace.
Return an error if userspace tries to set PMUVER/PerfMon field of ID_AA64DFR0_EL1/ID_DFR0_EL1 to a value that conflicts with the PMU configuration.
When a value of ID_AA64DFR0_EL1.PMUVER or ID_DFR0_EL1.PERFMON on the host is 0xf, which means IMPLEMENTATION DEFINED PMU supported, KVM erroneously expose the value for the guest as it is even though KVM doesn't support it for the guest. In that case, since KVM should expose 0x0 (PMU is not implemented), change the initial value of ID_AA64DFR0_EL1.PMUVER and ID_DFR0_EL1.PERFMON for the guest to 0x0. If userspace requests KVM to set them to 0xf, which shouldn't be allowed as KVM doesn't support IMPLEMENTATION DEFINED PMU for the guest, ignore the request (set the fields to 0x0 instead) so that a live migration from the older kernel works fine.
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/include/asm/cpufeature.h | 2 +- arch/arm64/kvm/sys_regs.c | 143 +++++++++++++++++++++++----- 2 files changed, 122 insertions(+), 23 deletions(-)
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;
arch/arm64/kvm/sys_regs.c +121 -22
182 lines not on this page · raw message
Signature · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 10/25 KVM: arm64: Make MVFR1_EL1 writable
In reply to Reiji WatanabeThis patch adds id_reg_desc 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 | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
arch/arm64/kvm/sys_regs.c +38 -0
@@ -632,6 +632,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_desc *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_desc (struct id_reg_desc *id_reg) {u64 limit = id_reg->vcpu_limit_val;
@@ -3474,6 +3504,11 @@ static struct id_reg_desc id_dfr0_el1_desc = { .vcpu_mask = vcpu_mask_id_dfr0_el1, }; +static struct id_reg_desc mvfr1_el1_desc = { + .reg_desc =ID_SANITISED (MVFR1_EL1 ), + .validate = validate_mvfr1_el1, +}; +#define ID_DESC (id_reg_name, id_reg_desc) \ [IDREG_IDX(SYS_##id_reg_name)] = (id_reg_desc)
@@ -3482,6 +3517,9 @@ static struct id_reg_desc *id_reg_desc_table[KVM_ARM_ID_REG_MAX_NUM] = {/* CRm=1 */ ID_DESC (ID_DFR0_EL1 , &id_dfr0_el1_desc), +/* CRm=3 */ +ID_DESC (MVFR1_EL1 , &mvfr1_el1_desc), +/* CRm=4 */ ID_DESC (ID_AA64PFR0_EL1 , &id_aa64pfr0_el1_desc), ID_DESC(ID_AA64PFR1_EL1, &id_aa64pfr1_el1_desc),
Signature · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 11/25 KVM: arm64: Add remaining ID registers to id_reg_desc_table
In reply to Reiji WatanabeAdd hidden or reserved ID registers, and remaining ID registers, which don't require special handling, to id_reg_desc_table. Add 'flags' field to id_reg_desc, which is used to indicates hiddden or reserved registers. Since now id_reg_desc_init() is called even for hidden/reserved registers, change it to not do anything for them.
--- arch/arm64/kvm/sys_regs.c | 89 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 2 deletions(-)
arch/arm64/kvm/sys_regs.c +87 -2
@@ -284,6 +284,10 @@ static bool trap_raz_wi(struct kvm_vcpu *vcpu, (cpuid_feature_extract_unsigned_field (val,ID_AA64ISAR1_GPI_SHIFT ) >= \ID_AA64ISAR1_GPI_IMP_DEF ) +/* id_reg_desc flags field values */ +#define ID_DESC_REG_UNALLOC (1UL << 0) +#define ID_DESC_REG_HIDDEN (1UL << 1) +struct id_reg_desc {const struct sys_reg_desc reg_desc;
@@ -297,6 +301,9 @@ struct id_reg_desc {/* Fields that are not validated by arm64_check_features_kvm. */ u64 ignore_mask; +/* Miscellaneous flags */ +u64 flags; +/* An optional initialization function of the id_reg_desc */ void (*init)(struct id_reg_desc *id_reg);
@@ -333,8 +340,13 @@ struct id_reg_desc {static void id_reg_desc_init (struct id_reg_desc *id_reg) {u32 id =reg_to_encoding (&id_reg->reg_desc); -u64 val =read_sanitised_ftr_reg (id); +u64 val; + +if (id_reg->flags & (ID_DESC_REG_HIDDEN |ID_DESC_REG_UNALLOC )) +/* Nothing to do for a hidden/unalloc ID register */ +return ; + val =read_sanitised_ftr_reg (id); id_reg->vcpu_limit_val = val;if (id_reg->init) id_reg->init (id_reg);
@@ -3509,30 +3521,103 @@ static struct id_reg_desc mvfr1_el1_desc = { .validate = validate_mvfr1_el1, }; +#define ID_DESC_DEFAULT (name) \ + [IDREG_IDX(SYS_##name)] = &(struct id_reg_desc) { \ + .reg_desc = ID_SANITISED(name), \ + } + +#define ID_DESC_HIDDEN (name) \ + [IDREG_IDX(SYS_##name)] = &(struct id_reg_desc) { \ + .reg_desc = ID_HIDDEN(name), \ + .flags = ID_DESC_REG_HIDDEN, \ + } + +#define ID_DESC_UNALLOC (crm, op2) \ + [crm << 3 | op2] = &(struct id_reg_desc) { \ + .reg_desc = ID_UNALLOCATED(crm, op2), \ + .flags = ID_DESC_REG_UNALLOC, \ + } +#define ID_DESC (id_reg_name, id_reg_desc) \ [IDREG_IDX(SYS_##id_reg_name)] = (id_reg_desc) -/* A table for ID registers's information. */ +/* +* A table for ID registers's information. +* All entries in the table except ID_DESC_HIDDEN and ID_DESC_UNALLOC +* must have corresponding entries in arm64_ftr_regs[] in +* arch/arm64/kernel/cpufeature.c because read_sanitised_ftr_reg() is +* called for each of the ID registers. +*/ static struct id_reg_desc *id_reg_desc_table[KVM_ARM_ID_REG_MAX_NUM ] = {/* CRm=1 */ +ID_DESC_DEFAULT (ID_PFR0_EL1 ), +ID_DESC_DEFAULT (ID_PFR1_EL1 ),ID_DESC (ID_DFR0_EL1 , &id_dfr0_el1_desc), +ID_DESC_HIDDEN (ID_AFR0_EL1 ), +ID_DESC_DEFAULT (ID_MMFR0_EL1 ), +ID_DESC_DEFAULT (ID_MMFR1_EL1 ), +ID_DESC_DEFAULT (ID_MMFR2_EL1 ), +ID_DESC_DEFAULT (ID_MMFR3_EL1 ), + +/* CRm=2 */ +ID_DESC_DEFAULT (ID_ISAR0_EL1 ), +ID_DESC_DEFAULT (ID_ISAR1_EL1 ), +ID_DESC_DEFAULT (ID_ISAR2_EL1 ), +ID_DESC_DEFAULT (ID_ISAR3_EL1 ), +ID_DESC_DEFAULT (ID_ISAR4_EL1 ), +ID_DESC_DEFAULT (ID_ISAR5_EL1 ), +ID_DESC_DEFAULT (ID_MMFR4_EL1 ), +ID_DESC_DEFAULT (ID_ISAR6_EL1 ),/* CRm=3 */ +ID_DESC_DEFAULT (MVFR0_EL1 ),ID_DESC (MVFR1_EL1 , &mvfr1_el1_desc), +ID_DESC_DEFAULT (MVFR2_EL1 ), +ID_DESC_UNALLOC (3 ,3 ), +ID_DESC_DEFAULT (ID_PFR2_EL1 ), +ID_DESC_HIDDEN (ID_DFR1_EL1 ), +ID_DESC_DEFAULT (ID_MMFR5_EL1 ), +ID_DESC_UNALLOC (3 ,7 ),/* CRm=4 */ ID_DESC (ID_AA64PFR0_EL1 , &id_aa64pfr0_el1_desc),ID_DESC (ID_AA64PFR1_EL1 , &id_aa64pfr1_el1_desc), +ID_DESC_UNALLOC (4 ,2 ), +ID_DESC_UNALLOC (4 ,3 ), +ID_DESC_DEFAULT (ID_AA64ZFR0_EL1 ), +ID_DESC_UNALLOC (4 ,5 ), +ID_DESC_UNALLOC (4 ,6 ), +ID_DESC_UNALLOC (4 ,7 ),/* CRm=5 */ ID_DESC (ID_AA64DFR0_EL1 , &id_aa64dfr0_el1_desc), +ID_DESC_DEFAULT (ID_AA64DFR1_EL1 ), +ID_DESC_UNALLOC (5 ,2 ), +ID_DESC_UNALLOC (5 ,3 ), +ID_DESC_HIDDEN (ID_AA64AFR0_EL1 ), +ID_DESC_HIDDEN (ID_AA64AFR1_EL1 ), +ID_DESC_UNALLOC (5 ,6 ), +ID_DESC_UNALLOC (5 ,7 ),/* CRm=6 */ ID_DESC (ID_AA64ISAR0_EL1 , &id_aa64isar0_el1_desc),ID_DESC (ID_AA64ISAR1_EL1 , &id_aa64isar1_el1_desc), +ID_DESC_DEFAULT (ID_AA64ISAR2_EL1 ), +ID_DESC_UNALLOC (6 ,3 ), +ID_DESC_UNALLOC (6 ,4 ), +ID_DESC_UNALLOC (6 ,5 ), +ID_DESC_UNALLOC (6 ,6 ), +ID_DESC_UNALLOC (6 ,7 ),/* CRm=7 */ ID_DESC (ID_AA64MMFR0_EL1 , &id_aa64mmfr0_el1_desc), +ID_DESC_DEFAULT (ID_AA64MMFR1_EL1 ), +ID_DESC_DEFAULT (ID_AA64MMFR2_EL1 ), +ID_DESC_UNALLOC (7 ,3 ), +ID_DESC_UNALLOC (7 ,4 ), +ID_DESC_UNALLOC (7 ,5 ), +ID_DESC_UNALLOC (7 ,6 ), +ID_DESC_UNALLOC (7 ,7 ), };static inline struct id_reg_desc *get_id_reg_desc (u32 id)
Signature · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v6 11/25] KVM: arm64: Add remaining ID registers to id_reg_desc_table
Hi Reiji,
Quoted 7 lines · Reiji Watanabe
Add hidden or reserved ID registers, and remaining ID registers, which don't require special handling, to
id_reg_desc_table. Add 'flags' field toid_reg_desc, which is used to indicates hiddden or reserved registers. Since nowid_reg_desc_init()is called even for hidden/reserved registers, change it to not do anything for them.Signed-off-by Reiji Watanabe <[email protected]>
I think there is a very important detail of the series that probably should be highlighted. We are only allowing AArch64 feature registers to be configurable, right? AArch32 feature registers remain visible with their default values passed through to the guest. If you've already stated this as a precondition elsewhere then my apologies for the noise.
I don't know if adding support for this to AArch32 registers is necessarily the right step forward, either. 32 bit support is working just fine and IMO its OK to limit new KVM features to AArch64-only so long as it doesn't break 32 bit support. Marc of course is the authority on that, though :-)
If for any reason a guest uses a feature present in the AArch32 feature register but hidden from the AArch64 register, we could be in a particularly difficult position. Especially if we enabled traps based on the AArch64 value and UNDEF the guest.
One hack we could do is skip trap configuration if AArch32 is visible at either EL1 or EL0, but that may not be the most elegant solution. Otherwise, if we are AArch64-only at every EL then the definition of the AArch32 feature registers is architecturally UNKNOWN, so we can dodge the problem altogether. What are your thoughts?
--
Thanks,
Oliver
Re: [PATCH v6 11/25] KVM: arm64: Add remaining ID registers to id_reg_desc_table
Quoted 9 lines · Oliver Upton
Hi Reiji,
Quoted 6 lines · Reiji Watanabe
Add hidden or reserved ID registers, and remaining ID registers, which don't require special handling, to
id_reg_desc_table. Add 'flags' field toid_reg_desc, which is used to indicates hiddden or reserved registers. Since nowid_reg_desc_init()is called even for hidden/reserved registers, change it to not do anything for them.
Quoted 7 lines
Quoted 1 line
Signed-off-by Reiji Watanabe <[email protected]>I think there is a very important detail of the series that probably should be highlighted. We are only allowing AArch64 feature registers to be configurable, right? AArch32 feature registers remain visible with their default values passed through to the guest. If you've already stated this as a precondition elsewhere then my apologies for the noise.
Aren't AArch64 ID regs architecturally mapped to their AArch32 counterparts? They should show the same values. I'm not sure if it's a problem (and if KVM is faithful to that rule),
Quoted 21 lines
I don't know if adding support for this to AArch32 registers is necessarily the right step forward, either. 32 bit support is working just fine and IMO its OK to limit new KVM features to AArch64-only so long as it doesn't break 32 bit support. Marc of course is the authority on that, though :-)
If for any reason a guest uses a feature present in the AArch32 feature register but hidden from the AArch64 register, we could be in a particularly difficult position. Especially if we enabled traps based on the AArch64 value and UNDEF the guest.
One hack we could do is skip trap configuration if AArch32 is visible at either EL1 or EL0, but that may not be the most elegant solution. Otherwise, if we are AArch64-only at every EL then the definition of the AArch32 feature registers is architecturally UNKNOWN, so we can dodge the problem altogether. What are your thoughts?
--
Thanks,
Oliver
Re: [PATCH v6 11/25] KVM: arm64: Add remaining ID registers to id_reg_desc_table
Quoted 18 lines · Ricardo Koller
Quoted 9 lines · Oliver Upton
Hi Reiji,
Quoted 6 lines · Reiji Watanabe
Add hidden or reserved ID registers, and remaining ID registers, which don't require special handling, to
id_reg_desc_table. Add 'flags' field toid_reg_desc, which is used to indicates hiddden or reserved registers. Since nowid_reg_desc_init()is called even for hidden/reserved registers, change it to not do anything for them.Quoted 7 lines
Quoted 1 line
Signed-off-by Reiji Watanabe <[email protected]>I think there is a very important detail of the series that probably should be highlighted. We are only allowing AArch64 feature registers to be configurable, right? AArch32 feature registers remain visible with their default values passed through to the guest. If you've already stated this as a precondition elsewhere then my apologies for the noise.
Quoted 3 lines
Aren't AArch64 ID regs architecturally mapped to their AArch32 counterparts? They should show the same values. I'm not sure if it's a problem (and if KVM is faithful to that rule),
I believe it's a bit more subtle than that. The AArch32 feature registers are architecturally mapped to certain encodings accessible from AArch64. For example, ID_PFR0_EL1 is actually a 64 bit register where bits [31:0] map to the ID_PFR0 AArch32 register. ID_PFR0_EL1 is only accessible from AArch64 with the MRS instruction, and ID_PFR0 is only accessible from AArch32 with the MRC instruction. KVM just so happens to handle both of these reads from the same sys_reg_desc.
AFAIK, there does not exist a direct bit mapping between the ID_*_EL1 <-> ID_AA64*_EL1 registers. But hey, could be wrong :)
--
Thanks,
Oliver
Re: [PATCH v6 11/25] KVM: arm64: Add remaining ID registers to id_reg_desc_table
Quoted 31 lines · Oliver Upton
Quoted 18 lines · Ricardo Koller
Quoted 9 lines · Oliver Upton
Hi Reiji,
Quoted 6 lines · Reiji Watanabe
Add hidden or reserved ID registers, and remaining ID registers, which don't require special handling, to
id_reg_desc_table. Add 'flags' field toid_reg_desc, which is used to indicates hiddden or reserved registers. Since nowid_reg_desc_init()is called even for hidden/reserved registers, change it to not do anything for them.Quoted 7 lines
Quoted 1 line
Signed-off-by Reiji Watanabe <[email protected]>I think there is a very important detail of the series that probably should be highlighted. We are only allowing AArch64 feature registers to be configurable, right? AArch32 feature registers remain visible with their default values passed through to the guest. If you've already stated this as a precondition elsewhere then my apologies for the noise.
Quoted 3 lines
Aren't AArch64 ID regs architecturally mapped to their AArch32 counterparts? They should show the same values. I'm not sure if it's a problem (and if KVM is faithful to that rule),
I believe it's a bit more subtle than that. The AArch32 feature registers are architecturally mapped to certain encodings accessible from AArch64. For example,
ID_PFR0_EL1is actually a 64 bit register where bits [31:0] map to theID_PFR0AArch32 register.ID_PFR0_EL1is only accessible from AArch64 with the MRS instruction, andID_PFR0is only accessible from AArch32 with the MRC instruction. KVM just so happens to handle both of these reads from the samesys_reg_desc.
Quoted 2 lines
AFAIK, there does not exist a direct bit mapping between the ID_*_EL1 <-> ID_AA64*_EL1 registers. But hey, could be wrong :)
I think you are right. ID_PFR0_EL1[31:0] doesn't even have the same field as ID_AA64PFR0_EL1[31:0]. The only exception would be RAS which is at [31:28] on both, but it doesn't say anywhere that ID_PFR0_EL1.RAS maps architecturally to ID_AA64PFR0_EL1.RAS. So, I think we can assume it doesn't (?).
Thanks,
Ricardo
Quoted 4 lines
--
Thanks,
Oliver
Re: [PATCH v6 11/25] KVM: arm64: Add remaining ID registers to id_reg_desc_table
Quoted 32 lines · Ricardo Koller
Quoted 31 lines · Oliver Upton
Quoted 18 lines · Ricardo Koller
Quoted 9 lines · Oliver Upton
Hi Reiji,
Quoted 6 lines · Reiji Watanabe
Add hidden or reserved ID registers, and remaining ID registers, which don't require special handling, to
id_reg_desc_table. Add 'flags' field toid_reg_desc, which is used to indicates hiddden or reserved registers. Since nowid_reg_desc_init()is called even for hidden/reserved registers, change it to not do anything for them.Quoted 7 lines
Quoted 1 line
Signed-off-by Reiji Watanabe <[email protected]>I think there is a very important detail of the series that probably should be highlighted. We are only allowing AArch64 feature registers to be configurable, right? AArch32 feature registers remain visible with their default values passed through to the guest. If you've already stated this as a precondition elsewhere then my apologies for the noise.
Quoted 3 lines
Aren't AArch64 ID regs architecturally mapped to their AArch32 counterparts? They should show the same values. I'm not sure if it's a problem (and if KVM is faithful to that rule),
I believe it's a bit more subtle than that. The AArch32 feature registers are architecturally mapped to certain encodings accessible from AArch64. For example,
ID_PFR0_EL1is actually a 64 bit register where bits [31:0] map to theID_PFR0AArch32 register.ID_PFR0_EL1is only accessible from AArch64 with the MRS instruction, andID_PFR0is only accessible from AArch32 with the MRC instruction. KVM just so happens to handle both of these reads from the samesys_reg_desc.
Quoted 8 lines
Quoted 2 lines
AFAIK, there does not exist a direct bit mapping between the ID_*_EL1 <-> ID_AA64*_EL1 registers. But hey, could be wrong :)
I think you are right. ID_PFR0_EL1[31:0] doesn't even have the same field as ID_AA64PFR0_EL1[31:0]. The only exception would be RAS which is at [31:28] on both, but it doesn't say anywhere that ID_PFR0_EL1.RAS maps architecturally to ID_AA64PFR0_EL1.RAS. So, I think we can assume it doesn't (?).
Right, the feature registers are generally related (you will find fields of similar meaning), but figuring out that tangle and making it work is going to be a massive waste of time IMO. If we can say that our new feature configuration is AArch64-only, all potential bugs relating to AArch32 collapse :-)
--
Thanks,
Oliver
Re: [PATCH v6 11/25] KVM: arm64: Add remaining ID registers to id_reg_desc_table
Quoted 25 lines · Oliver Upton
Quoted 24 lines · Ricardo Koller
Quoted 23 lines · Oliver Upton
Quoted 21 lines · Ricardo Koller
Quoted 9 lines · Oliver Upton
Hi Reiji,
Quoted 6 lines · Reiji Watanabe
Add hidden or reserved ID registers, and remaining ID registers, which don't require special handling, to
id_reg_desc_table. Add 'flags' field toid_reg_desc, which is used to indicates hiddden or reserved registers. Since nowid_reg_desc_init()is called even for hidden/reserved registers, change it to not do anything for them.Quoted 7 lines
Quoted 1 line
Signed-off-by Reiji Watanabe <[email protected]>I think there is a very important detail of the series that probably should be highlighted. We are only allowing AArch64 feature registers to be configurable, right? AArch32 feature registers remain visible with their default values passed through to the guest. If you've already stated this as a precondition elsewhere then my apologies for the noise.
Aren't AArch64 ID regs architecturally mapped to their AArch32 counterparts? They should show the same values. I'm not sure if it's a problem (and if KVM is faithful to that rule),
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
I believe it's a bit more subtle than that. The AArch32 feature registers are architecturally mapped to certain encodings accessible from AArch64. For example,
ID_PFR0_EL1is actually a 64 bit register where bits [31:0] map to theID_PFR0AArch32 register.ID_PFR0_EL1is only accessible from AArch64 with the MRS instruction, andID_PFR0is only accessible from AArch32 with the MRC instruction. KVM just so happens to handle both of these reads from the samesys_reg_desc.
Ughhhhh.
We actually clear HCR_EL2.TID3 for AArch32 guests, so AArch32 EL1 reads straight from hardware. Considering the work we put in to make sure feature registers are consistent system-wide and the limitations on certain features, this is plain wrong.
I have a series that addresses this but need to go find some 32 bit hardware to test with :)
--
Thanks,
Oliver
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v6 11/25] KVM: arm64: Add remaining ID registers to id_reg_desc_table
In reply to Oliver UptonHi Oliver,
Quoted 29 lines · Oliver Upton
Hi Reiji,
Quoted 7 lines · Reiji Watanabe
Add hidden or reserved ID registers, and remaining ID registers, which don't require special handling, to
id_reg_desc_table. Add 'flags' field toid_reg_desc, which is used to indicates hiddden or reserved registers. Since nowid_reg_desc_init()is called even for hidden/reserved registers, change it to not do anything for them.Signed-off-by Reiji Watanabe <[email protected]>I think there is a very important detail of the series that probably should be highlighted. We are only allowing AArch64 feature registers to be configurable, right? AArch32 feature registers remain visible with their default values passed through to the guest. If you've already stated this as a precondition elsewhere then my apologies for the noise.
I don't know if adding support for this to AArch32 registers is necessarily the right step forward, either. 32 bit support is working just fine and IMO its OK to limit new KVM features to AArch64-only so long as it doesn't break 32 bit support. Marc of course is the authority on that, though :-)
If for any reason a guest uses a feature present in the AArch32 feature register but hidden from the AArch64 register, we could be in a particularly difficult position. Especially if we enabled traps based on the AArch64 value and UNDEF the guest.
Quoted 5 lines
One hack we could do is skip trap configuration if AArch32 is visible at either EL1 or EL0, but that may not be the most elegant solution. Otherwise, if we are AArch64-only at every EL then the definition of the AArch32 feature registers is architecturally UNKNOWN, so we can dodge the problem altogether. What are your thoughts?
Thank you so much for your review, Oliver!
For aarch32 guests (when KVM_ARM_VCPU_EL1_32BIT is configured), yes, the current series is problematic as you mentioned... I am thinking of disallowing configuring ID registers (keep ID registers immutable) for the aarch32 guests for now at least. (will document that)
For aarch64 guests that support EL0 aarch32, it would generally be a userspace bug if userspace sets inconsistent values in 32bit and 64bit ID registers. KVM doesn't provide a complete consistency checking for ID registers, but this could be added later as needed.
It might be a good idea to skip trap configuration to avoid being affected by the issue. On the other hand, this might provide a good opportunity to detect such userspace issue. As this could happen only with new userspace code that changes ID registers, I might rather prefer the latter?
Thanks,
Reiji
Re: [PATCH v6 11/25] KVM: arm64: Add remaining ID registers to id_reg_desc_table
Quoted 38 lines · Reiji Watanabe
Hi Oliver,
Quoted 29 lines · Oliver Upton
Hi Reiji,
Quoted 7 lines · Reiji Watanabe
Add hidden or reserved ID registers, and remaining ID registers, which don't require special handling, to
id_reg_desc_table. Add 'flags' field toid_reg_desc, which is used to indicates hiddden or reserved registers. Since nowid_reg_desc_init()is called even for hidden/reserved registers, change it to not do anything for them.Signed-off-by Reiji Watanabe <[email protected]>I think there is a very important detail of the series that probably should be highlighted. We are only allowing AArch64 feature registers to be configurable, right? AArch32 feature registers remain visible with their default values passed through to the guest. If you've already stated this as a precondition elsewhere then my apologies for the noise.
I don't know if adding support for this to AArch32 registers is necessarily the right step forward, either. 32 bit support is working just fine and IMO its OK to limit new KVM features to AArch64-only so long as it doesn't break 32 bit support. Marc of course is the authority on that, though :-)
If for any reason a guest uses a feature present in the AArch32 feature register but hidden from the AArch64 register, we could be in a particularly difficult position. Especially if we enabled traps based on the AArch64 value and UNDEF the guest.
Quoted 5 lines
One hack we could do is skip trap configuration if AArch32 is visible at either EL1 or EL0, but that may not be the most elegant solution. Otherwise, if we are AArch64-only at every EL then the definition of the AArch32 feature registers is architecturally UNKNOWN, so we can dodge the problem altogether. What are your thoughts?
Quoted 7 lines
Thank you so much for your review, Oliver!
For aarch32 guests (when
KVM_ARM_VCPU_EL1_32BITis configured), yes, the current series is problematic as you mentioned... I am thinking of disallowing configuring ID registers (keep ID registers immutable) for the aarch32 guests for now at least. (will document that)
That fixes it halfway, but the AArch64 views of the AArch32 feature registers have meaning even if AArch32 is defined at EL0. The only time they are architecturally UNKNOWN is if AArch32 is not implemented at any EL visible to the guest.
So, given that:
Quoted 4 lines
For aarch64 guests that support EL0 aarch32, it would generally be a userspace bug if userspace sets inconsistent values in 32bit and 64bit ID registers. KVM doesn't provide a complete consistency checking for ID registers, but this could be added later as needed.
I completely agree that there is a large set of things that can be swept under the rug of userspace bugs. If we are going to do that, we need to strongly assert that configurable feature registers is only for fully AArch64-only guests. Additionally, strong documentation around these expectations is required.
Mind you, these opinions are my own and IDK how others or Marc feel about it. My read of the situation w.r.t. the AArch32 registers is that it will become a mess to implement on top of the AArch64 registers. Given the fact that we aren't breaking AArch32 VMs, only augmenting behavior for AArch64, it seems OK.
But I would genuinely love to be wrong on this topic too. I just don't have perspective on AArch32 users so it is hard to really say whether this is something they need as well.
--
Thanks,
Oliver
Re: [PATCH v6 11/25] KVM: arm64: Add remaining ID registers to id_reg_desc_table
Quoted 60 lines · Oliver Upton
Quoted 38 lines · Reiji Watanabe
Hi Oliver,
Quoted 29 lines · Oliver Upton
Hi Reiji,
Quoted 7 lines · Reiji Watanabe
Add hidden or reserved ID registers, and remaining ID registers, which don't require special handling, to
id_reg_desc_table. Add 'flags' field toid_reg_desc, which is used to indicates hiddden or reserved registers. Since nowid_reg_desc_init()is called even for hidden/reserved registers, change it to not do anything for them.Signed-off-by Reiji Watanabe <[email protected]>I think there is a very important detail of the series that probably should be highlighted. We are only allowing AArch64 feature registers to be configurable, right? AArch32 feature registers remain visible with their default values passed through to the guest. If you've already stated this as a precondition elsewhere then my apologies for the noise.
I don't know if adding support for this to AArch32 registers is necessarily the right step forward, either. 32 bit support is working just fine and IMO its OK to limit new KVM features to AArch64-only so long as it doesn't break 32 bit support. Marc of course is the authority on that, though :-)
If for any reason a guest uses a feature present in the AArch32 feature register but hidden from the AArch64 register, we could be in a particularly difficult position. Especially if we enabled traps based on the AArch64 value and UNDEF the guest.
Quoted 5 lines
One hack we could do is skip trap configuration if AArch32 is visible at either EL1 or EL0, but that may not be the most elegant solution. Otherwise, if we are AArch64-only at every EL then the definition of the AArch32 feature registers is architecturally UNKNOWN, so we can dodge the problem altogether. What are your thoughts?
Quoted 7 lines
Thank you so much for your review, Oliver!
For aarch32 guests (when
KVM_ARM_VCPU_EL1_32BITis configured), yes, the current series is problematic as you mentioned... I am thinking of disallowing configuring ID registers (keep ID registers immutable) for the aarch32 guests for now at least. (will document that)That fixes it halfway, but the AArch64 views of the AArch32 feature registers have meaning even if AArch32 is defined at EL0. The only time they are architecturally UNKNOWN is if AArch32 is not implemented at any EL visible to the guest.
So, given that:
Quoted 4 lines
For aarch64 guests that support EL0 aarch32, it would generally be a userspace bug if userspace sets inconsistent values in 32bit and 64bit ID registers. KVM doesn't provide a complete consistency checking for ID registers, but this could be added later as needed.
Quoted 5 lines
I completely agree that there is a large set of things that can be swept under the rug of userspace bugs. If we are going to do that, we need to strongly assert that configurable feature registers is only for fully AArch64-only guests. Additionally, strong documentation around these expectations is required.
Just to be clear, what I meant "this could be added later" is that the feature consistency checking between 64bit ID registers and 32bit ID registers could be added later. It means configuring ID registers itself will be allowed for EL1 aarch64 guests including ones that support EL0 aarch32 (but not for EL1 aarch32 guests).
Quoted 5 lines
Mind you, these opinions are my own and IDK how others or Marc feel about it. My read of the situation w.r.t. the AArch32 registers is that it will become a mess to implement on top of the AArch64 registers. Given the fact that we aren't breaking AArch32 VMs, only augmenting behavior for AArch64, it seems OK.
Thank you for sharing your thoughts.
They look almost the same as what I've been thinking.
Thanks,
Reiji
Quoted 3 lines
But I would genuinely love to be wrong on this topic too. I just don't have perspective on AArch32 users so it is hard to really say whether this is something they need as well.
Re: [PATCH v6 11/25] KVM: arm64: Add remaining ID registers to id_reg_desc_table
Quoted 67 lines · Reiji Watanabe
Quoted 60 lines · Oliver Upton
Quoted 38 lines · Reiji Watanabe
Hi Oliver,
Quoted 29 lines · Oliver Upton
Hi Reiji,
Quoted 7 lines · Reiji Watanabe
Add hidden or reserved ID registers, and remaining ID registers, which don't require special handling, to
id_reg_desc_table. Add 'flags' field toid_reg_desc, which is used to indicates hiddden or reserved registers. Since nowid_reg_desc_init()is called even for hidden/reserved registers, change it to not do anything for them.Signed-off-by Reiji Watanabe <[email protected]>I think there is a very important detail of the series that probably should be highlighted. We are only allowing AArch64 feature registers to be configurable, right? AArch32 feature registers remain visible with their default values passed through to the guest. If you've already stated this as a precondition elsewhere then my apologies for the noise.
I don't know if adding support for this to AArch32 registers is necessarily the right step forward, either. 32 bit support is working just fine and IMO its OK to limit new KVM features to AArch64-only so long as it doesn't break 32 bit support. Marc of course is the authority on that, though :-)
If for any reason a guest uses a feature present in the AArch32 feature register but hidden from the AArch64 register, we could be in a particularly difficult position. Especially if we enabled traps based on the AArch64 value and UNDEF the guest.
Quoted 5 lines
One hack we could do is skip trap configuration if AArch32 is visible at either EL1 or EL0, but that may not be the most elegant solution. Otherwise, if we are AArch64-only at every EL then the definition of the AArch32 feature registers is architecturally UNKNOWN, so we can dodge the problem altogether. What are your thoughts?
Quoted 7 lines
Thank you so much for your review, Oliver!
For aarch32 guests (when
KVM_ARM_VCPU_EL1_32BITis configured), yes, the current series is problematic as you mentioned... I am thinking of disallowing configuring ID registers (keep ID registers immutable) for the aarch32 guests for now at least. (will document that)That fixes it halfway, but the AArch64 views of the AArch32 feature registers have meaning even if AArch32 is defined at EL0. The only time they are architecturally UNKNOWN is if AArch32 is not implemented at any EL visible to the guest.
So, given that:
Quoted 4 lines
For aarch64 guests that support EL0 aarch32, it would generally be a userspace bug if userspace sets inconsistent values in 32bit and 64bit ID registers. KVM doesn't provide a complete consistency checking for ID registers, but this could be added later as needed.
Quoted 5 lines
I completely agree that there is a large set of things that can be swept under the rug of userspace bugs. If we are going to do that, we need to strongly assert that configurable feature registers is only for fully AArch64-only guests. Additionally, strong documentation around these expectations is required.
Quoted 6 lines
Just to be clear, what I meant "this could be added later" is that the feature consistency checking between 64bit ID registers and 32bit ID registers could be added later. It means configuring ID registers itself will be allowed for EL1 aarch64 guests including ones that support EL0 aarch32 (but not for EL1 aarch32 guests).
I believe we are in agreement then. I don't believe there is any need to prohibit register accesses for the case of AArch32 EL1, given the fact that it is impossible to read the ID_AA64*_EL1 registers from the guest anyway.
We can go ahead and let userspace play with those registers as much as it wants, it delightfully has no affect :)
Quoted 16 lines
Quoted 5 lines
Mind you, these opinions are my own and IDK how others or Marc feel about it. My read of the situation w.r.t. the AArch32 registers is that it will become a mess to implement on top of the AArch64 registers. Given the fact that we aren't breaking AArch32 VMs, only augmenting behavior for AArch64, it seems OK.
Thank you for sharing your thoughts.
They look almost the same as what I've been thinking.Thanks,
ReijiQuoted 3 lines
But I would genuinely love to be wrong on this topic too. I just don't have perspective on AArch32 users so it is hard to really say whether this is something they need as well.
PATCH v6 12/25 KVM: arm64: Use id_reg_desc_table for ID registers
In reply to Reiji WatanabeUse id_reg_desc_table for ID registers instead of sys_reg_descs as id_reg_desc_table has all ID register entries that sys_reg_descs has, and remove the ID register entries from sys_reg_descs, which are no longer used.
--- arch/arm64/kvm/sys_regs.c | 201 +++++++++++++++++--------------------- 1 file changed, 92 insertions(+), 109 deletions(-)
arch/arm64/kvm/sys_regs.c +92 -109
@@ -337,6 +337,11 @@ struct id_reg_desc { const struct id_reg_desc *id_reg); }; +static inline struct id_reg_desc *sys_to_id_desc (const struct sys_reg_desc *r) +{ + return container_of(r, struct id_reg_desc, reg_desc); +} +static void id_reg_desc_init (struct id_reg_desc *id_reg) {u32 id =reg_to_encoding (&id_reg->reg_desc);
@@ -1696,23 +1701,15 @@ static u64 __read_id_reg(const struct kvm_vcpu *vcpu,return val; } -static u64 read_id_reg_with_encoding (const struct kvm_vcpu *vcpu,u32 id) +static u64 read_id_reg_with_encoding (const struct kvm_vcpu *vcpu,u32 encoding) { -u64 val; -const struct id_reg_desc *id_reg =get_id_reg_desc (id); - -if (id_reg) - val =__read_id_reg (vcpu, id_reg); -else - val =read_kvm_id_reg (vcpu->kvm, id); - -return val; +return __read_id_reg (vcpu,get_id_reg_desc (encoding)); }static u64 read_id_reg (const struct kvm_vcpu *vcpu,struct sys_reg_desc const *r,bool raz) { -return raz ?0 :read_id_reg_with_encoding (vcpu,reg_to_encoding (r)); +return raz ?0 :__read_id_reg (vcpu,sys_to_id_desc (r)); } static unsigned int id_visibility(const struct kvm_vcpu *vcpu,
@@ -1826,13 +1823,7 @@ static int __set_id_reg(struct kvm_vcpu *vcpu,if (raz)return -EINVAL ; -/* -* Don't allow to modify the register's value if the register doesn't -* have the id_reg_desc. -*/ - id_reg =get_id_reg_desc (encoding); -if (!id_reg) -return -EINVAL ; + id_reg =sys_to_id_desc (rd);/* * Skip the validation of AArch32 ID registers if the system doesn't
@@ -2055,83 +2046,6 @@ static const struct sys_reg_desc sys_reg_descs[] = { { SYS_DESC(SYS_MPIDR_EL1), NULL, reset_mpidr, MPIDR_EL1 }, -/* -* ID regs: all ID_SANITISED() entries here must have corresponding -* entries in arm64_ftr_regs[]. -*/ - -/* AArch64 mappings of the AArch32 ID registers */ -/* CRm=1 */ -ID_SANITISED (ID_PFR0_EL1 ), -ID_SANITISED (ID_PFR1_EL1 ), -ID_SANITISED (ID_DFR0_EL1 ), -ID_HIDDEN (ID_AFR0_EL1 ), -ID_SANITISED (ID_MMFR0_EL1 ), -ID_SANITISED (ID_MMFR1_EL1 ), -ID_SANITISED (ID_MMFR2_EL1 ), -ID_SANITISED (ID_MMFR3_EL1 ), - -/* CRm=2 */ -ID_SANITISED (ID_ISAR0_EL1 ), -ID_SANITISED (ID_ISAR1_EL1 ), -ID_SANITISED (ID_ISAR2_EL1 ), -ID_SANITISED (ID_ISAR3_EL1 ), -ID_SANITISED (ID_ISAR4_EL1 ), -ID_SANITISED (ID_ISAR5_EL1 ), -ID_SANITISED (ID_MMFR4_EL1 ), -ID_SANITISED (ID_ISAR6_EL1 ), - -/* CRm=3 */ -ID_SANITISED (MVFR0_EL1 ), -ID_SANITISED (MVFR1_EL1 ), -ID_SANITISED (MVFR2_EL1 ), -ID_UNALLOCATED (3 ,3 ), -ID_SANITISED (ID_PFR2_EL1 ), -ID_HIDDEN (ID_DFR1_EL1 ), -ID_SANITISED (ID_MMFR5_EL1 ), -ID_UNALLOCATED (3 ,7 ), - -/* AArch64 ID registers */ -/* CRm=4 */ -ID_SANITISED (ID_AA64PFR0_EL1 ), -ID_SANITISED (ID_AA64PFR1_EL1 ), -ID_UNALLOCATED (4 ,2 ), -ID_UNALLOCATED (4 ,3 ), -ID_SANITISED (ID_AA64ZFR0_EL1 ), -ID_UNALLOCATED (4 ,5 ), -ID_UNALLOCATED (4 ,6 ), -ID_UNALLOCATED (4 ,7 ), - -/* CRm=5 */ -ID_SANITISED (ID_AA64DFR0_EL1 ), -ID_SANITISED (ID_AA64DFR1_EL1 ), -ID_UNALLOCATED (5 ,2 ), -ID_UNALLOCATED (5 ,3 ), -ID_HIDDEN (ID_AA64AFR0_EL1 ), -ID_HIDDEN (ID_AA64AFR1_EL1 ), -ID_UNALLOCATED (5 ,6 ), -ID_UNALLOCATED (5 ,7 ), - -/* CRm=6 */ -ID_SANITISED (ID_AA64ISAR0_EL1 ), -ID_SANITISED (ID_AA64ISAR1_EL1 ), -ID_SANITISED (ID_AA64ISAR2_EL1 ), -ID_UNALLOCATED (6 ,3 ), -ID_UNALLOCATED (6 ,4 ), -ID_UNALLOCATED (6 ,5 ), -ID_UNALLOCATED (6 ,6 ), -ID_UNALLOCATED (6 ,7 ), - -/* CRm=7 */ -ID_SANITISED (ID_AA64MMFR0_EL1 ), -ID_SANITISED (ID_AA64MMFR1_EL1 ), -ID_SANITISED (ID_AA64MMFR2_EL1 ), -ID_UNALLOCATED (7 ,3 ), -ID_UNALLOCATED (7 ,4 ), -ID_UNALLOCATED (7 ,5 ), -ID_UNALLOCATED (7 ,6 ), -ID_UNALLOCATED (7 ,7 ), - { SYS_DESC(SYS_SCTLR_EL1), access_vm_reg, reset_val, SCTLR_EL1, 0x00C50078 }, { SYS_DESC(SYS_ACTLR_EL1), access_actlr, reset_actlr, ACTLR_EL1 }, { SYS_DESC(SYS_CPACR_EL1), NULL, reset_val, CPACR_EL1, 0 },
@@ -2946,12 +2860,38 @@ static bool is_imp_def_sys_reg(struct sys_reg_params *params)return params->Op0 ==3 && (params->CRn &0b1011 ) ==0b1011 ; } +static inline const struct sys_reg_desc * +find_id_reg (const struct sys_reg_params *params) +{ +u32 id =reg_to_encoding (params); +struct id_reg_desc *idr; + +if (!is_id_reg (id)) +return NULL ; + + idr =get_id_reg_desc (id); + +return idr ? &idr->reg_desc :NULL ; +} + +static const struct sys_reg_desc * +find_sys_reg (const struct sys_reg_params *params) +{ +const struct sys_reg_desc *r =NULL ; + + r =find_id_reg (params); +if (!r) + r =find_reg (params, sys_reg_descs,ARRAY_SIZE (sys_reg_descs)); + +return r; +} +static int emulate_sys_reg (struct kvm_vcpu *vcpu,struct sys_reg_params *params) {const struct sys_reg_desc *r; - r =find_reg (params, sys_reg_descs,ARRAY_SIZE (sys_reg_descs)); + r =find_sys_reg (params);if (likely (r)) {perform_access (vcpu, params, r);
@@ -2966,6 +2906,8 @@ static int emulate_sys_reg(struct kvm_vcpu *vcpu,return 1 ; } +static void kvm_reset_id_regs (struct kvm_vcpu *vcpu); +/** * kvm_reset_sys_regs - sets system registers to reset value * @vcpu: The VCPU pointer
@@ -2980,6 +2922,8 @@ void kvm_reset_sys_regs(struct kvm_vcpu *vcpu)for (i =0 ; i <ARRAY_SIZE (sys_reg_descs); i++)if (sys_reg_descs[i].reset) sys_reg_descs[i].reset (vcpu, &sys_reg_descs[i]); + +kvm_reset_id_regs (vcpu); }/**
@@ -3063,7 +3007,7 @@ static const struct sys_reg_desc *index_to_sys_reg_desc(struct kvm_vcpu *vcpu,if (!index_to_params (id, ¶ms))return NULL ; - r =find_reg (¶ms, sys_reg_descs,ARRAY_SIZE (sys_reg_descs)); + r =find_sys_reg (¶ms);/* Not saved in the sys_reg array and not otherwise accessible? */ if (r && !(r->reg || r->get_user))
@@ -3360,6 +3304,8 @@ static int walk_one_sys_reg(const struct kvm_vcpu *vcpu,return 0 ; } +static int walk_id_regs (struct kvm_vcpu *vcpu,u64 __user *uind); +/* Assumed ordered tables, see kvm_sys_reg_table_init. */ static int walk_sys_regs (struct kvm_vcpu *vcpu,u64 __user *uind) {
@@ -3375,6 +3321,12 @@ static int walk_sys_regs(struct kvm_vcpu *vcpu, u64 __user *uind)if (err)return err; } + + err =walk_id_regs (vcpu, uind); +if (err <0 ) +return err; + + total += err;return total; }
@@ -3625,6 +3577,25 @@ static inline struct id_reg_desc *get_id_reg_desc(u32 id)return id_reg_desc_table[IDREG_IDX (id)]; } +static int walk_id_regs (struct kvm_vcpu *vcpu,u64 __user *uind) +{ +const struct sys_reg_desc *sys_reg; +int err, i; +unsigned int total =0 ; + +for (i =0 ; i <ARRAY_SIZE (id_reg_desc_table); i++) { +if (!id_reg_desc_table[i]) +continue ; + + sys_reg = &id_reg_desc_table[i]->reg_desc; + err =walk_one_sys_reg (vcpu, sys_reg, &uind, &total); +if (err) +return err; + } + +return total; +} +static void id_reg_desc_init_all (void ) {int i;
@@ -3647,23 +3618,35 @@ void set_default_id_regs(struct kvm *kvm) {int i;u32 id; -const struct sys_reg_desc *rd; -u64 val;struct id_reg_desc *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 */ +for (i =0 ; i <ARRAY_SIZE (id_reg_desc_table); i++) { + idr = id_reg_desc_table[i]; +if (!idr) +continue ; + +if (idr->flags & (ID_DESC_REG_HIDDEN |ID_DESC_REG_UNALLOC )) +/* Nothing to do for hidden/unalloc registers */ continue ; - id =reg_to_encoding (rd); -if (WARN_ON_ONCE (!is_id_reg (id))) -/* Shouldn't happen */ + id =reg_to_encoding (&idr->reg_desc); +WARN_ON_ONCE (write_kvm_id_reg (kvm, id, idr->vcpu_limit_val)); + } +} + +static void kvm_reset_id_regs (struct kvm_vcpu *vcpu) +{ +int i; +const struct sys_reg_desc *r; +struct id_reg_desc *id_reg; + +for (i =0 ; i <ARRAY_SIZE (id_reg_desc_table); i++) { + id_reg = (struct id_reg_desc *)id_reg_desc_table[i]; +if (!id_reg)continue ; - idr =get_id_reg_desc (id); - val = idr ? idr->vcpu_limit_val :read_sanitised_ftr_reg (id); -WARN_ON_ONCE (write_kvm_id_reg (kvm, id, val)); + r = &id_reg->reg_desc; +if (r->reset) + r->reset (vcpu, r); } }
Signature · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 13/25 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 | 112 ++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+)
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 -0
130 lines not on this page · raw message
Signature · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 14/25 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:
@@ -7575,3 +7579,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
@@ -1135,6 +1135,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_XSAVE2 208#define KVM_CAP_SYS_ATTRIBUTES 209#define KVM_CAP_PPC_AIL_MODE_3 210 +#define KVM_CAP_ARM_ID_REG_CONFIGURABLE 211#ifdef KVM_CAP_IRQ_ROUTING
Signature · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 15/25 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 | 983 +++++++++++++++++++++++++++++++++ 4 files changed, 1002 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
@@ -3762,3 +3762,7 @@ static void kvm_reset_id_regs(struct kvm_vcpu *vcpu) r->reset (vcpu, r); } } + +#if IS_ENABLED (CONFIG_KVM_KUNIT_TEST ) +#include "sys_regs_test.c" +#endif
arch/arm64/kvm/sys_regs_test.c new file +983 -0
983 lines not on this page · raw message
Signature · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 16/25 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 · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 17/25 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 · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 18/25 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 | 111 ++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 5 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 +111 -0
153 lines not on this page · raw message
Signature · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 19/25 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
@@ -305,6 +305,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, +}; +/* id_reg_desc flags field values */ #define ID_DESC_REG_UNALLOC (1UL << 0)#define ID_DESC_REG_HIDDEN (1UL << 1)
@@ -853,6 +910,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
@@ -2155,14 +2224,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),
@@ -3598,6 +3667,11 @@ static struct id_reg_desc id_aa64pfr0_el1_desc = { .init = init_id_aa64pfr0_el1_desc, .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_desc id_aa64pfr1_el1_desc = {
Signature · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 20/25 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
@@ -345,6 +345,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 ,
@@ -362,6 +367,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, +}; +/* id_reg_desc flags field values */ #define ID_DESC_REG_UNALLOC (1UL << 0)#define ID_DESC_REG_HIDDEN (1UL << 1)
@@ -3682,6 +3696,10 @@ static struct id_reg_desc id_aa64pfr1_el1_desc = { .init = init_id_aa64pfr1_el1_desc, .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_desc id_aa64isar0_el1_desc = {
Signature · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 21/25 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
@@ -350,6 +350,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 ,
@@ -376,6 +400,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, +}; +/* id_reg_desc flags field values */ #define ID_DESC_REG_UNALLOC (1UL << 0)#define ID_DESC_REG_HIDDEN (1UL << 1)
@@ -3733,6 +3790,13 @@ static struct id_reg_desc id_aa64dfr0_el1_desc = { .init = init_id_aa64dfr0_el1_desc, .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_desc id_dfr0_el1_desc = {
Signature · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 22/25 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 | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)
arch/arm64/kvm/sys_regs.c +24 -3
@@ -374,6 +374,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 ,
@@ -433,6 +438,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, +}; +/* id_reg_desc flags field values */ #define ID_DESC_REG_UNALLOC (1UL << 0)#define ID_DESC_REG_HIDDEN (1UL << 1)
@@ -1003,10 +1017,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_with_encoding(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; }
@@ -3785,6 +3798,14 @@ static struct id_reg_desc id_aa64mmfr0_el1_desc = { .validate = validate_id_aa64mmfr0_el1, }; +static struct id_reg_desc id_aa64mmfr1_el1_desc = { + .reg_desc =ID_SANITISED (ID_AA64MMFR1_EL1 ), + .trap_features = &(const struct feature_config_ctrl *[]) { + &ftr_ctrl_lor, +NULL , + }, +}; +static struct id_reg_desc id_aa64dfr0_el1_desc = { .reg_desc =ID_SANITISED (ID_AA64DFR0_EL1 ), .init = init_id_aa64dfr0_el1_desc,
@@ -3901,7 +3922,7 @@ static struct id_reg_desc *id_reg_desc_table[KVM_ARM_ID_REG_MAX_NUM] = {/* CRm=7 */ ID_DESC (ID_AA64MMFR0_EL1 , &id_aa64mmfr0_el1_desc), -ID_DESC_DEFAULT (ID_AA64MMFR1_EL1 ), +ID_DESC (ID_AA64MMFR1_EL1 , &id_aa64mmfr1_el1_desc),ID_DESC_DEFAULT (ID_AA64MMFR2_EL1 ),ID_DESC_UNALLOC (7 ,3 ), ID_DESC_UNALLOC(7, 4),
Signature · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 23/25 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
@@ -284,6 +284,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_with_encoding (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.
@@ -379,6 +403,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 ,
@@ -447,6 +476,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, +}; +/* id_reg_desc flags field values */ #define ID_DESC_REG_UNALLOC (1UL << 0)#define ID_DESC_REG_HIDDEN (1UL << 1)
@@ -3782,6 +3817,10 @@ static struct id_reg_desc id_aa64isar1_el1_desc = { .init = init_id_aa64isar1_el1_desc, .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_desc id_aa64mmfr0_el1_desc = {
Signature · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 24/25 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 | 255 +++++++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+)
arch/arm64/kvm/sys_regs_test.c +255 -0
@@ -957,6 +957,258 @@ static void validate_id_reg_test(struct kunit *test) } } +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_desc with three entries in id_reg_desc->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_desc 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_desc */ + id_reg_sys_val =0x7777777777777777 ; + id =SYS_ID_AA64DFR0_EL1 ; +set_sys_desc ((struct sys_reg_desc *)&id_reg_data.reg_desc, 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_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),
@@ -971,6 +1223,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.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v6 25/25 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 | 1300 +++++++++++++++++ 4 files changed, 1303 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
@@ -102,6 +102,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 +1300 -0
1,300 lines not on this page · raw message
Signature · 6 lines
2.35.1.723.g4982287a31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm