Enable writable for idregs DFR0,PFR0, MMFR{0,1,2, 3}
v6 cover Enable writable for idregs DFR0,PFR0, MMFR{0,1,2, 3}
This patch series enable userspace writable for below idregs:ID_AA64DFR0_EL1, ID_DFR0_EL1, ID_AA64PFR0_EL1, ID_AA64MMFR{0, 1, 2, 3}_EL1.
It is based on v6.5-rc1 which contains infrastructure for writable idregs.
A selftest is added to verify that KVM handles the writings from user space correctly.
A relevant patch from Oliver is picked from [3].
---
* v5 -> v6
- Override the type of field AA64DFR0_EL1_DebugVer to be FTR_LOWER_SAFE by the discussion of Oliver and Suraj.
* v4 -> v5
- Rebase on v6.4-rc1 which contains infrastructure for writable idregs.
- Use guest ID registers values for the sake of emulation.
- Added a selftest to verify idreg userspace writing.
* v3 -> v4
- Rebase on v11 of writable idregs series at [2].
* v2 -> v3
- Rebase on v6 of writable idregs series.
- Enable writable for ID_AA64PFR0_EL1 and ID_AA64MMFR{0, 1, 2}_EL1.
* v1 -> v2
- Rebase on latest patch series [1] of enabling writable ID register.
[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]
[3] https://lore.kernel.org/kvmarm/[email protected]
[v1] https://lore.kernel.org/all/[email protected]
[v2] https://lore.kernel.org/all/[email protected]
[v3] https://lore.kernel.org/all/[email protected]
[v4] https://lore.kernel.org/all/[email protected]
[v5] https://lore.kernel.org/all/[email protected]
---
Jing Zhang (4):
KVM: arm64: Use guest ID register values for the sake of emulation
KVM: arm64: Enable writable for ID_AA64PFR0_EL1
KVM: arm64: Enable writable for ID_AA64MMFR{0, 1, 2, 3}_EL1
KVM: arm64: selftests: Test for setting ID register from usersapce
Oliver Upton (1):
KVM: arm64: Reject attempts to set invalid debug arch version
arch/arm64/kvm/sys_regs.c | 80 +++++++-- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/aarch64/set_id_regs.c | 163 ++++++++++++++++++ 3 files changed, 230 insertions(+), 14 deletions(-) create mode 100644 tools/testing/selftests/kvm/aarch64/set_id_regs.c
base-commit: 06c2afb862f9da8dc5efa4b6076a0e48c3fbaaa5
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
PATCH v6 1/5 KVM: arm64: Use guest ID register values for the sake of emulation
Since KVM now supports per-VM ID registers, use per-VM ID register values for the sake of emulation for DBGDIDR and LORegion.
--- arch/arm64/kvm/sys_regs.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/kvm/sys_regs.c +3 -3
@@ -379,7 +379,7 @@ static bool trap_loregion(struct kvm_vcpu *vcpu,struct sys_reg_params *p, const struct sys_reg_desc *r) { - u64 val = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1); + u64 val = IDREG(vcpu->kvm, SYS_ID_AA64MMFR1_EL1);u32 sr =reg_to_encoding (r);if (!(val & (0xfUL <<ID_AA64MMFR1_EL1_LO_SHIFT ))) {
@@ -2429,8 +2429,8 @@ static bool trap_dbgdidr(struct kvm_vcpu *vcpu,if (p->is_write) {return ignore_write (vcpu, p); }else { -u64 dfr =read_sanitised_ftr_reg (SYS_ID_AA64DFR0_EL1 ); -u64 pfr =read_sanitised_ftr_reg (SYS_ID_AA64PFR0_EL1 ); +u64 dfr =IDREG (vcpu->kvm,SYS_ID_AA64DFR0_EL1 ); +u64 pfr =IDREG (vcpu->kvm,SYS_ID_AA64PFR0_EL1 );u32 el3 = !!cpuid_feature_extract_unsigned_field (pfr,ID_AA64PFR0_EL1_EL3_SHIFT ); p->regval = ((((dfr >> ID_AA64DFR0_EL1_WRPs_SHIFT) & 0xf) << 28) |
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
PATCH v6 2/5 KVM: arm64: Reject attempts to set invalid debug arch version
In reply to Jing ZhangFrom: Oliver Upton <[email protected]>
The debug architecture is mandatory in ARMv8, so KVM should not allow userspace to configure a vCPU with less than that. Of course, this isn't handled elegantly by the generic ID register plumbing, as the respective ID register fields have a nonzero starting value.
Add an explicit check for debug versions less than v8 of the architecture.
--- arch/arm64/kvm/sys_regs.c | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-)
arch/arm64/kvm/sys_regs.c +31 -3
@@ -1216,8 +1216,14 @@ static s64 kvm_arm64_ftr_safe_value(u32 id, const struct arm64_ftr_bits *ftrp,/* Some features have different safe value type in KVM than host features */ switch (id) {case SYS_ID_AA64DFR0_EL1 : -if (kvm_ftr.shift == ID_AA64DFR0_EL1_PMUVer_SHIFT) +switch (kvm_ftr.shift) { +case ID_AA64DFR0_EL1_PMUVer_SHIFT: kvm_ftr.type =FTR_LOWER_SAFE ; +break ; +case ID_AA64DFR0_EL1_DebugVer_SHIFT: + kvm_ftr.type =FTR_LOWER_SAFE ; +break ; + }break ;case SYS_ID_DFR0_EL1 :if (kvm_ftr.shift == ID_DFR0_EL1_PerfMon_SHIFT)
@@ -1469,14 +1475,22 @@ static u64 read_sanitised_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,return val; } +#define ID_REG_LIMIT_FIELD_ENUM (val, reg, field, limit) \ +({ \ + u64 __f_val = FIELD_GET(reg##_##field##_MASK, val); \ + (val) &= ~reg##_##field##_MASK; \ + (val) |= FIELD_PREP(reg##_##field##_MASK, \ + min(__f_val, (u64)reg##_##field##_##limit)); \ + (val); \ +}) +static u64 read_sanitised_id_aa64dfr0_el1 (struct kvm_vcpu *vcpu,const struct sys_reg_desc *rd) {u64 val =read_sanitised_ftr_reg (SYS_ID_AA64DFR0_EL1 );/* Limit debug to ARMv8.0 */ - val &= ~ID_AA64DFR0_EL1_DebugVer_MASK; - val |=SYS_FIELD_PREP_ENUM (ID_AA64DFR0_EL1 , DebugVer,IMP ); + val =ID_REG_LIMIT_FIELD_ENUM (val,ID_AA64DFR0_EL1 , DebugVer,V8P8 );/* * Only initialize the PMU version if the vCPU was configured with one.
@@ -1496,6 +1510,7 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,const struct sys_reg_desc *rd, u64 val) { + u8 debugver = SYS_FIELD_GET(ID_AA64DFR0_EL1, DebugVer, val);u8 pmuver =SYS_FIELD_GET (ID_AA64DFR0_EL1 , PMUVer, val);/*
@@ -1515,6 +1530,13 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) val &= ~ID_AA64DFR0_EL1_PMUVer_MASK; +/* +* ID_AA64DFR0_EL1.DebugVer is one of those awkward fields with a +* nonzero minimum safe value. +*/ +if (debugver < ID_AA64DFR0_EL1_DebugVer_IMP) +return -EINVAL ; +return set_id_reg (vcpu, rd, val); }
@@ -1528,6 +1550,8 @@ static u64 read_sanitised_id_dfr0_el1(struct kvm_vcpu *vcpu,if (kvm_vcpu_has_pmu (vcpu)) val |=SYS_FIELD_PREP (ID_DFR0_EL1 , PerfMon, perfmon); + val =ID_REG_LIMIT_FIELD_ENUM (val,ID_DFR0_EL1 , CopDbg, Debugv8p8); +return val; }
@@ -1536,6 +1560,7 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, u64 val) {u8 perfmon =SYS_FIELD_GET (ID_DFR0_EL1 , PerfMon, val); +u8 copdbg =SYS_FIELD_GET (ID_DFR0_EL1 , CopDbg, val);if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF) { val &= ~ID_DFR0_EL1_PerfMon_MASK;
@@ -1551,6 +1576,9 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,if (perfmon !=0 && perfmon < ID_DFR0_EL1_PerfMon_PMUv3)return -EINVAL ; +if (copdbg < ID_DFR0_EL1_CopDbg_Armv8) +return -EINVAL ; +return set_id_reg (vcpu, rd, val); }
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
PATCH v6 3/5 KVM: arm64: Enable writable for ID_AA64PFR0_EL1
In reply to Jing ZhangAll valid fields in ID_AA64PFR0_EL1 are writable from usrespace with this change.
--- arch/arm64/kvm/sys_regs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/sys_regs.c +1 -1
@@ -2042,7 +2042,7 @@ static const struct sys_reg_desc sys_reg_descs[] = { .get_user = get_id_reg, .set_user = set_id_reg, .reset = read_sanitised_id_aa64pfr0_el1, - .val = ID_AA64PFR0_EL1_CSV2_MASK | ID_AA64PFR0_EL1_CSV3_MASK, }, + .val = GENMASK(63, 0), },ID_SANITISED (ID_AA64PFR1_EL1 ),ID_UNALLOCATED (4 ,2 ), ID_UNALLOCATED(4,3),
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
PATCH v6 4/5 KVM: arm64: Enable writable for ID_AA64MMFR{0, 1, 2, 3}_EL1
In reply to Jing ZhangEnable writable from userspace for ID_AA64MMFR{0, 1, 2, 3}_EL1. Added a macro for defining general writable idregs.
--- arch/arm64/kvm/sys_regs.c | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-)
arch/arm64/kvm/sys_regs.c +31 -7
@@ -1346,9 +1346,6 @@ static u64 __kvm_read_sanitised_id_reg(const struct kvm_vcpu *vcpu, val &= ~ARM64_FEATURE_MASK (ID_AA64ISAR2_EL1_WFxT); val &= ~ARM64_FEATURE_MASK (ID_AA64ISAR2_EL1_MOPS );break ; -case SYS_ID_AA64MMFR2_EL1 : - val &= ~ID_AA64MMFR2_EL1_CCIDX_MASK ; -break ;case SYS_ID_MMFR4_EL1 : val &= ~ARM64_FEATURE_MASK (ID_MMFR4_EL1_CCIDX );break ;
@@ -1582,6 +1579,18 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,return set_id_reg (vcpu, rd, val); } +static u64 read_sanitised_id_aa64mmfr2_el1 (struct kvm_vcpu *vcpu, +const struct sys_reg_desc *rd) +{ +u64 val; +u32 id =reg_to_encoding (rd); + + val =read_sanitised_ftr_reg (id); + val &= ~ID_AA64MMFR2_EL1_CCIDX_MASK ; + +return val; +} +/* * cpufeature ID register user accessors *
@@ -1856,6 +1865,16 @@ static unsigned int elx2_visibility(const struct kvm_vcpu *vcpu, .val = 0, \ } +#define ID_SANITISED_WRITABLE (name) { \ + SYS_DESC(SYS_##name), \ + .access = access_id_reg, \ + .get_user = get_id_reg, \ + .set_user = set_id_reg, \ + .visibility = id_visibility, \ + .reset = kvm_read_sanitised_id_reg, \ + .val = GENMASK(63, 0), \ +} +/* sys_reg_desc initialiser for known cpufeature ID registers */ #define AA32_ID_SANITISED (name) { \ SYS_DESC(SYS_##name), \
@@ -2077,10 +2096,15 @@ static const struct sys_reg_desc sys_reg_descs[] = {ID_UNALLOCATED (6 ,7 ),/* CRm=7 */ -ID_SANITISED (ID_AA64MMFR0_EL1 ), -ID_SANITISED (ID_AA64MMFR1_EL1 ), -ID_SANITISED (ID_AA64MMFR2_EL1 ), -ID_SANITISED (ID_AA64MMFR3_EL1 ), +ID_SANITISED_WRITABLE (ID_AA64MMFR0_EL1 ), +ID_SANITISED_WRITABLE (ID_AA64MMFR1_EL1 ), + { SYS_DESC(SYS_ID_AA64MMFR2_EL1), + .access = access_id_reg, + .get_user = get_id_reg, + .set_user = set_id_reg, + .reset = read_sanitised_id_aa64mmfr2_el1, + .val = GENMASK(63, 0), }, +ID_SANITISED_WRITABLE (ID_AA64MMFR3_EL1 ),ID_UNALLOCATED (7 ,4 ),ID_UNALLOCATED (7 ,5 ), ID_UNALLOCATED(7,6),
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
PATCH v6 5/5 KVM: arm64: selftests: Test for setting ID register from usersapce
In reply to Jing ZhangAdd a test to verify setting ID registers from userapce is handled correctly by KVM.
--- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/aarch64/set_id_regs.c | 163 ++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 tools/testing/selftests/kvm/aarch64/set_id_regs.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -144,6 +144,7 @@ TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list TEST_GEN_PROGS_aarch64 += aarch64/hypercalls TEST_GEN_PROGS_aarch64 += aarch64/page_fault_test TEST_GEN_PROGS_aarch64 += aarch64/psci_test +TEST_GEN_PROGS_aarch64 += aarch64/set_id_regs TEST_GEN_PROGS_aarch64 += aarch64/smccc_filter TEST_GEN_PROGS_aarch64 += aarch64/vcpu_width_config TEST_GEN_PROGS_aarch64 += aarch64/vgic_init
tools/testing/selftests/kvm/aarch64/set_id_regs.c new file +163 -0
163 lines not on this page · raw message
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Please ignore this post of v6 which missed a commit. I'll send out the correct v6 soon.
Thanks,
Jing
Quoted 64 lines · Jing Zhang
This patch series enable userspace writable for below idregs:
ID_AA64DFR0_EL1,ID_DFR0_EL1,ID_AA64PFR0_EL1, ID_AA64MMFR{0, 1, 2, 3}_EL1.It is based on v6.5-rc1 which contains infrastructure for writable idregs.
A selftest is added to verify that KVM handles the writings from user space correctly.
A relevant patch from Oliver is picked from [3].
---
* v5 -> v6
- Override the type of fieldAA64DFR0_EL1_DebugVerto beFTR_LOWER_SAFEby the discussion of Oliver and Suraj.* v4 -> v5
- Rebase on v6.4-rc1 which contains infrastructure for writable idregs.
- Use guest ID registers values for the sake of emulation.
- Added a selftest to verify idreg userspace writing.* v3 -> v4
- Rebase on v11 of writable idregs series at [2].* v2 -> v3
- Rebase on v6 of writable idregs series.
- Enable writable forID_AA64PFR0_EL1and ID_AA64MMFR{0, 1, 2}_EL1.* v1 -> v2
- Rebase on latest patch series [1] of enabling writable ID register.[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]
[3] https://lore.kernel.org/kvmarm/[email protected][v1] https://lore.kernel.org/all/[email protected]
[v2] https://lore.kernel.org/all/[email protected]
[v3] https://lore.kernel.org/all/[email protected]
[v4] https://lore.kernel.org/all/[email protected]
[v5] https://lore.kernel.org/all/[email protected]---
Jing Zhang (4):
KVM: arm64: Use guest ID register values for the sake of emulation
KVM: arm64: Enable writable forID_AA64PFR0_EL1
KVM: arm64: Enable writable for ID_AA64MMFR{0, 1, 2, 3}_EL1
KVM: arm64: selftests: Test for setting ID register from usersapceOliver Upton (1):
KVM: arm64: Reject attempts to set invalid debug arch versionarch/arm64/kvm/sys_regs.c | 80 +++++++-- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/aarch64/set_id_regs.c | 163 ++++++++++++++++++ 3 files changed, 230 insertions(+), 14 deletions(-) create mode 100644 tools/testing/selftests/kvm/aarch64/set_id_regs.cbase-commit:
06c2afb862f9da8dc5efa4b6076a0e48c3fbaaa5--
2.41.0.255.g8b1d071c50-goog