arm64/sysreg: Introduce Prefix descriptor and generated ICH_VMCR_EL2 support
v2 cover arm64/sysreg: Introduce Prefix descriptor and generated ICH_VMCR_EL2 support
This series introduces support for conditional field encodings in the sysreg description framework and migrates the vGIC-v3 code to use generated definitions for ICH_VMCR_EL2, in part as an example of how the Prefix descriptor can be used. In addition, it fixes an issue with the tracking of incomplete system register efinitions.
The first patch addresses an issue where next_bit was checked for > 0 instead of >= 0 to determine if all 64-bits had been defined. The result was that the generator didn't catch cases where bit 0 of a system register was not defined. Thankfully, there were no such cases.
The next patch adds the Prefix descriptor, allowing sysreg definitions to be prefixed with an optional condition (e.g., GICv3 vs GICv5). The Prefix/EndPrefix construct enables generation of prefixed field encodings without affecting legacy definitions which remain unchanged. This forms the basis for supporting feature-dependent register layouts.
The third patch adds the generated description for ICH_VMCR_EL2, including both its GICv3 and GICv5 variants. This register was previously defined manually in the KVM GICv3 code; moving it into the sysreg framework ensures consistency and reduces duplication.
Finally, the last patch updates the KVM vGIC-v3 implementation to use the generated ICH_VMCR_EL2 definitions. This replaces and removes the hand-written definitions, with no functional change to behaviour.
Together, these patches complete the migration of ICH_VMCR_EL2 to the sysreg framework and establish the infrastructure needed to describe registers with multiple field encodings.
Thanks,
Sascha
Sascha Bischoff (4):
arm64/sysreg: Fix checks for incomplete sysreg definitions arm64/sysreg: Support feature-specific fields with 'Prefix' descriptor arm64/sysreg: Add ICH_VMCR_EL2
KVM: arm64: gic-v3: Switch vGIC-v3 to use generated ICH_VMCR_EL2
arch/arm64/include/asm/sysreg.h | 21 ---- arch/arm64/kvm/hyp/vgic-v3-sr.c | 64 ++++++------- arch/arm64/kvm/vgic/vgic-v3-nested.c | 8 +- arch/arm64/kvm/vgic/vgic-v3.c | 42 ++++---- arch/arm64/tools/gen-sysreg.awk | 137 ++++++++++++++++++--------- arch/arm64/tools/sysreg | 21 ++++ 6 files changed, 162 insertions(+), 131 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/4 arm64/sysreg: Fix checks for incomplete sysreg definitions
The checks for incomplete sysreg definitions were checking if the next_bit was greater than 0, which is incorrect and missed occasions where bit 0 hasn't been defined for a sysreg. The reason is that next_bit is -1 when all bits have been processed (LSB - 1).
Change the checks to use >= 0, instead. Also, set next_bit in Mapping to -1 instead of 0 to match these new checks.
There are no changes to the generated sysreg definitons as part of this change, and conveniently no definitions lack definitions for bit 0.
--- arch/arm64/tools/gen-sysreg.awk | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/tools/gen-sysreg.awk +3 -3
@@ -129,7 +129,7 @@ $1 == "SysregFields" && block_current() == "Root" { $1 == "EndSysregFields" && block_current() == "SysregFields" { expect_fields(1) - if (next_bit > 0) + if (next_bit >= 0) fatal("Unspecified bits in " reg) define(reg "_RES0", "(" res0 ")")
@@ -180,7 +180,7 @@ $1 == "Sysreg" && block_current() == "Root" { $1 == "EndSysreg" && block_current() == "Sysreg" { expect_fields(1) - if (next_bit > 0) + if (next_bit >= 0) fatal("Unspecified bits in " reg) if (res0 != null)
@@ -217,7 +217,7 @@ $1 == "EndSysreg" && block_current() == "Sysreg" { print "/* For " reg " fields see " $2 " */" print "" - next_bit = 0 + next_bit = -1 res0 = null res1 = null unkn = null
Signature · 1 line
2.34.1
Re: [PATCH v2 1/4] arm64/sysreg: Fix checks for incomplete sysreg definitions
Quoted 4 lines · Sascha Bischoff
The checks for incomplete sysreg definitions were checking if the
next_bitwas greater than 0, which is incorrect and missed occasions where bit 0 hasn't been defined for a sysreg. The reason is thatnext_bitis -1 when all bits have been processed (LSB - 1).
PATCH v2 2/4 arm64/sysreg: Support feature-specific fields with 'Prefix' descriptor
In reply to Sascha BischoffSome system register field encodings change based on, for example the in-use architecture features, or the context in which they are accessed. In order to support these different field encodings, introduce the Prefix descriptor (Prefix, EndPrefix) for describing such sysregs.
The Prefix descriptor can be used in the following way:
Sysreg EXAMPLE 0 1 2 3 4
Prefix FEAT_A
Field 63:0 Foo
EndPrefix
Prefix FEAT_B
Field 63:1 Bar
Res0 0
EndPrefix
Field 63:0 Baz
EndSysreg
This will generate a single set of system register encodings (REG_, SYS_, ...), and then generate three sets of field definitions for the system register called EXAMPLE. The first set is prefixed by FEAT_A, e.g. FEAT_A_EXAMPLE_Foo. The second set is prefixed by FEAT_B, e.g., FEAT_B_EXAMPLE_Bar. The third set is not given a prefix at all, e.g. EXAMPLE_BAZ. For each set, a corresponding set of defines for Res0, Res1, and Unkn is generated.
The intent for the final prefix-less fields is to describe default or legacy field encodings. This ensure that prefixed encodings can be added to already-present sysregs without affecting existing legacy code. Prefixed fields must be defined before those without a prefix, and this is checked by the generator. This ensures consisnt ordering within the sysregs definitions.
The Prefix descriptor can be used within Sysreg or SysregFields blocks. Field, Res0, Res1, Unkn, Rax, SignedEnum, Enum can all be used within a Prefix block. Fields and Mapping can not. Fields that vary with features must be described as part of a SysregFields block, instead. Mappings, which are just a code comment, make little sense in this context, and have hence not been included.
There are no changes to the generated system register definitions as part of this change.
--- arch/arm64/tools/gen-sysreg.awk | 131 +++++++++++++++++++++----------- 1 file changed, 87 insertions(+), 44 deletions(-)
arch/arm64/tools/gen-sysreg.awk +87 -44
@@ -44,21 +44,38 @@ function expect_fields(nf) { # Print a CPP macro definition, padded with spaces so that the macro bodies # line up in a column -function define(name, val) { - printf "%-56s%s\n", "#define " name, val +function define(prefix, name, val) { + printf "%-56s%s\n", "#define " prefix name, val +} + +# Same as above, but without a prefix +function define_reg(name, val) { + define(null, name, val) } # Print standard BITMASK/SHIFT/WIDTH CPP definitions for a field -function define_field(reg, field, msb, lsb) { - define(reg "_" field, "GENMASK(" msb ", " lsb ")") - define(reg "_" field "_MASK", "GENMASK(" msb ", " lsb ")") - define(reg "_" field "_SHIFT", lsb) - define(reg "_" field "_WIDTH", msb - lsb + 1) +function define_field(prefix, reg, field, msb, lsb) { + define(prefix, reg "_" field, "GENMASK(" msb ", " lsb ")") + define(prefix, reg "_" field "_MASK", "GENMASK(" msb ", " lsb ")") + define(prefix, reg "_" field "_SHIFT", lsb) + define(prefix, reg "_" field "_WIDTH", msb - lsb + 1) } # Print a field _SIGNED definition for a field -function define_field_sign(reg, field, sign) { - define(reg "_" field "_SIGNED", sign) +function define_field_sign(prefix, reg, field, sign) { + define(prefix, reg "_" field "_SIGNED", sign) +} + +# Print the Res0, Res1, Unkn masks +function define_resx_unkn(prefix, reg, res0, res1, unkn) { + if (res0 != null) + define(prefix, reg "_RES0", "(" res0 ")") + if (res1 != null) + define(prefix, reg "_RES1", "(" res1 ")") + if (unkn != null) + define(prefix, reg "_UNKN", "(" unkn ")") + if (res0 != null || res1 != null || unkn != null) + print "" } # Parse a "<msb>[:<lsb>]" string into the global variables @msb and @lsb
@@ -132,10 +149,7 @@ $1 == "EndSysregFields" && block_current() == "SysregFields" { if (next_bit >= 0) fatal("Unspecified bits in " reg) - define(reg "_RES0", "(" res0 ")") - define(reg "_RES1", "(" res1 ")") - define(reg "_UNKN", "(" unkn ")") - print "" + define_resx_unkn(prefix, reg, res0, res1, unkn) reg = null res0 = null
@@ -162,17 +176,18 @@ $1 == "Sysreg" && block_current() == "Root" { res1 = "UL(0)" unkn = "UL(0)" - define("REG_" reg, "S" op0 "_" op1 "_C" crn "_C" crm "_" op2) - define("SYS_" reg, "sys_reg(" op0 ", " op1 ", " crn ", " crm ", " op2 ")") + define_reg("REG_" reg, "S" op0 "_" op1 "_C" crn "_C" crm "_" op2) + define_reg("SYS_" reg, "sys_reg(" op0 ", " op1 ", " crn ", " crm ", " op2 ")") - define("SYS_" reg "_Op0", op0) - define("SYS_" reg "_Op1", op1) - define("SYS_" reg "_CRn", crn) - define("SYS_" reg "_CRm", crm) - define("SYS_" reg "_Op2", op2) + define_reg("SYS_" reg "_Op0", op0) + define_reg("SYS_" reg "_Op1", op1) + define_reg("SYS_" reg "_CRn", crn) + define_reg("SYS_" reg "_CRm", crm) + define_reg("SYS_" reg "_Op2", op2) print "" + prefix = null next_bit = 63 next
@@ -183,14 +198,7 @@ $1 == "EndSysreg" && block_current() == "Sysreg" { if (next_bit >= 0) fatal("Unspecified bits in " reg) - if (res0 != null) - define(reg "_RES0", "(" res0 ")") - if (res1 != null) - define(reg "_RES1", "(" res1 ")") - if (unkn != null) - define(reg "_UNKN", "(" unkn ")") - if (res0 != null || res1 != null || unkn != null) - print "" + define_resx_unkn(prefix, reg, res0, res1, unkn) reg = null op0 = null
@@ -201,6 +209,7 @@ $1 == "EndSysreg" && block_current() == "Sysreg" { res0 = null res1 = null unkn = null + prefix = null block_pop() next
@@ -225,8 +234,7 @@ $1 == "EndSysreg" && block_current() == "Sysreg" { next } - -$1 == "Res0" && (block_current() == "Sysreg" || block_current() == "SysregFields") { +$1 == "Res0" && (block_current() == "Sysreg" || block_current() == "SysregFields" || block_current() == "Prefix") { expect_fields(2) parse_bitdef(reg, "RES0", $2) field = "RES0_" msb "_" lsb
@@ -236,7 +244,7 @@ $1 == "Res0" && (block_current() == "Sysreg" || block_current() == "SysregFields next } -$1 == "Res1" && (block_current() == "Sysreg" || block_current() == "SysregFields") { +$1 == "Res1" && (block_current() == "Sysreg" || block_current() == "SysregFields" || block_current() == "Prefix") { expect_fields(2) parse_bitdef(reg, "RES1", $2) field = "RES1_" msb "_" lsb
@@ -246,7 +254,7 @@ $1 == "Res1" && (block_current() == "Sysreg" || block_current() == "SysregFields next } -$1 == "Unkn" && (block_current() == "Sysreg" || block_current() == "SysregFields") { +$1 == "Unkn" && (block_current() == "Sysreg" || block_current() == "SysregFields" || block_current() == "Prefix") { expect_fields(2) parse_bitdef(reg, "UNKN", $2) field = "UNKN_" msb "_" lsb
@@ -256,58 +264,58 @@ $1 == "Unkn" && (block_current() == "Sysreg" || block_current() == "SysregFields next } -$1 == "Field" && (block_current() == "Sysreg" || block_current() == "SysregFields") { +$1 == "Field" && (block_current() == "Sysreg" || block_current() == "SysregFields" || block_current() == "Prefix") { expect_fields(3) field = $3 parse_bitdef(reg, field, $2) - define_field(reg, field, msb, lsb) + define_field(prefix, reg, field, msb, lsb) print "" next } -$1 == "Raz" && (block_current() == "Sysreg" || block_current() == "SysregFields") { +$1 == "Raz" && (block_current() == "Sysreg" || block_current() == "SysregFields" || block_current() == "Prefix") { expect_fields(2) parse_bitdef(reg, field, $2) next } -$1 == "SignedEnum" && (block_current() == "Sysreg" || block_current() == "SysregFields") { +$1 == "SignedEnum" && (block_current() == "Sysreg" || block_current() == "SysregFields" || block_current() == "Prefix") { block_push("Enum") expect_fields(3) field = $3 parse_bitdef(reg, field, $2) - define_field(reg, field, msb, lsb) - define_field_sign(reg, field, "true") + define_field(prefix, reg, field, msb, lsb) + define_field_sign(prefix, reg, field, "true") next } -$1 == "UnsignedEnum" && (block_current() == "Sysreg" || block_current() == "SysregFields") { +$1 == "UnsignedEnum" && (block_current() == "Sysreg" || block_current() == "SysregFields" || block_current() == "Prefix") { block_push("Enum") expect_fields(3) field = $3 parse_bitdef(reg, field, $2) - define_field(reg, field, msb, lsb) - define_field_sign(reg, field, "false") + define_field(prefix, reg, field, msb, lsb) + define_field_sign(prefix, reg, field, "false") next } -$1 == "Enum" && (block_current() == "Sysreg" || block_current() == "SysregFields") { +$1 == "Enum" && (block_current() == "Sysreg" || block_current() == "SysregFields" || block_current() == "Prefix") { block_push("Enum") expect_fields(3) field = $3 parse_bitdef(reg, field, $2) - define_field(reg, field, msb, lsb) + define_field(prefix, reg, field, msb, lsb) next }
@@ -329,7 +337,42 @@ $1 == "EndEnum" && block_current() == "Enum" { val = $1 name = $2 - define(reg "_" field "_" name, "UL(" val ")") + define(prefix, reg "_" field "_" name, "UL(" val ")") + next +} + +$1 == "Prefix" && (block_current() == "Sysreg" || block_current() == "SysregFields") { + block_push("Prefix") + + expect_fields(2) + + if (next_bit < 63) + fatal("Prefixed fields must precede non-prefixed fields (" reg ")") + + prefix = $2 "_" + res0 = "UL(0)" + res1 = "UL(0)" + unkn = "UL(0)" + next_bit = 63 + + next +} + +$1 == "EndPrefix" && block_current() == "Prefix" { + expect_fields(1) + if (next_bit >= 0) + fatal("Unspecified bits in prefix " prefix " for " reg) + + define_resx_unkn(prefix, reg, res0, res1, unkn) + + prefix = null + res0 = "UL(0)" + res1 = "UL(0)" + unkn = "UL(0)" + next_bit = 63 + + block_pop() + next }
Signature · 1 line
2.34.1
Re: [PATCH v2 2/4] arm64/sysreg: Support feature-specific fields with 'Prefix' descriptor
Quoted 12 lines · Sascha Bischoff
The Prefix descriptor can be used in the following way:
Sysreg EXAMPLE 0 1 2 3 4
PrefixFEAT_A
Field 63:0 Foo
EndPrefix
PrefixFEAT_B
Field 63:1 Bar
Res0 0
EndPrefix
Field 63:0 Baz
EndSysreg
This seems like a reasonable solution to the practical problem, and the implementation seems OK:
I think trying to do too much more would open up far too many cans of worms to be reasonable to block things on.
One nit:
arch/arm64/tools/gen-sysreg.awk $1 == "EndSysregFields" && block_current() == "SysregFields" { · line 152
Quoted 10 lines
# Parse a "<msb>[:<lsb>]" string into the global variables @msb and @lsb
arch/arm64/tools/gen-sysreg.awk +1
-4@@ -132,10 +149,7 @@ $1 == "EndSysregFields" && block_current() == "SysregFields" { if (next_bit >= 0) fatal("Unspecified bits in " reg) - define(reg "_RES0", "(" res0 ")") - define(reg "_RES1", "(" res1 ")") - define(reg "_UNKN", "(" unkn ")") - print "" + define_resx_unkn(prefix, reg, res0, res1, unkn)
This refactoring into a function seems like it could usefully have been a separate patch.
PATCH v2 3/4 arm64/sysreg: Add ICH_VMCR_EL2
In reply to Sascha BischoffAdd the ICH_VMCR_EL2 register, which is required for the upcoming GICv5 KVM support. This register has two different field encodings, based on if it is used for GICv3 or GICv5-based VMs. The GICv5-specific field encodings are generated with a FEAT_GCIE prefix.
This register is already described in the GICv3 KVM code directly. This will be ported across to use the generated encodings as part of an upcoming change.
--- arch/arm64/tools/sysreg | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
arch/arm64/tools/sysreg +21 -0
@@ -4668,6 +4668,27 @@ Field 1 V3 Field 0 En EndSysreg +Sysreg ICH_VMCR_EL2 3 4 12 11 7 +Prefix FEAT_GCIE +Res0 63:32 +Field 31:27 VPMR +Res0 26:1 +Field 0 EN +EndPrefix +Res0 63:32 +Field 31:24 VPMR +Field 23:21 VBPR0 +Field 20:18 VBPR1 +Res0 17:10 +Field 9 VEOIM +Res0 8:5 +Field 4 VCBPR +Field 3 VFIQEn +Field 2 VAckCtl +Field 1 VENG1 +Field 0 VENG0 +EndSysreg + Sysreg CONTEXTIDR_EL2 3 4 13 0 1 Fields CONTEXTIDR_ELx EndSysreg
Signature · 1 line
2.34.1
PATCH v2 4/4 KVM: arm64: gic-v3: Switch vGIC-v3 to use generated ICH_VMCR_EL2
In reply to Sascha BischoffThe VGIC-v3 code relied on hand-written definitions for the ICH_VMCR_EL2 register. This register, and the associated fields, is now generated as part of the sysreg framework. Move to using the generated definitions instead of the hand-written ones.
There are no functional changes as part of this change.
--- arch/arm64/include/asm/sysreg.h | 21 --------- arch/arm64/kvm/hyp/vgic-v3-sr.c | 64 ++++++++++++---------------- arch/arm64/kvm/vgic/vgic-v3-nested.c | 8 ++-- arch/arm64/kvm/vgic/vgic-v3.c | 42 +++++++++--------- 4 files changed, 51 insertions(+), 84 deletions(-)
arch/arm64/include/asm/sysreg.h +0 -21
@@ -571,7 +571,6 @@#define SYS_ICC_SRE_EL2 sys_reg(3, 4, 12, 9, 5)#define SYS_ICH_EISR_EL2 sys_reg(3, 4, 12, 11, 3)#define SYS_ICH_ELRSR_EL2 sys_reg(3, 4, 12, 11, 5) -#define SYS_ICH_VMCR_EL2 sys_reg(3, 4, 12, 11, 7)#define __SYS__LR0_EL2 (x) sys_reg(3, 4, 12, 12, x)#define SYS_ICH_LR0_EL2 __SYS__LR0_EL2(0)
@@ -999,26 +998,6 @@#define ICH_LR_PRIORITY_SHIFT 48#define ICH_LR_PRIORITY_MASK (0xffULL << ICH_LR_PRIORITY_SHIFT) -/* ICH_VMCR_EL2 bit definitions */ -#define ICH_VMCR_ACK_CTL_SHIFT 2 -#define ICH_VMCR_ACK_CTL_MASK (1 << ICH_VMCR_ACK_CTL_SHIFT) -#define ICH_VMCR_FIQ_EN_SHIFT 3 -#define ICH_VMCR_FIQ_EN_MASK (1 << ICH_VMCR_FIQ_EN_SHIFT) -#define ICH_VMCR_CBPR_SHIFT 4 -#define ICH_VMCR_CBPR_MASK (1 << ICH_VMCR_CBPR_SHIFT) -#define ICH_VMCR_EOIM_SHIFT 9 -#define ICH_VMCR_EOIM_MASK (1 << ICH_VMCR_EOIM_SHIFT) -#define ICH_VMCR_BPR1_SHIFT 18 -#define ICH_VMCR_BPR1_MASK (7 << ICH_VMCR_BPR1_SHIFT) -#define ICH_VMCR_BPR0_SHIFT 21 -#define ICH_VMCR_BPR0_MASK (7 << ICH_VMCR_BPR0_SHIFT) -#define ICH_VMCR_PMR_SHIFT 24 -#define ICH_VMCR_PMR_MASK (0xffUL << ICH_VMCR_PMR_SHIFT) -#define ICH_VMCR_ENG0_SHIFT 0 -#define ICH_VMCR_ENG0_MASK (1 << ICH_VMCR_ENG0_SHIFT) -#define ICH_VMCR_ENG1_SHIFT 1 -#define ICH_VMCR_ENG1_MASK (1 << ICH_VMCR_ENG1_SHIFT) -/* * Permission Indirection Extension (PIE) permission encodings. * Encodings with the _O suffix, have overlays applied (Permission Overlay Extension).
arch/arm64/kvm/hyp/vgic-v3-sr.c +28 -36
178 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic-v3-nested.c +4 -4
@@ -199,16 +199,16 @@ u64 vgic_v3_get_misr(struct kvm_vcpu *vcpu)if ((hcr &ICH_HCR_EL2_NPIE ) && !mi_state.pend) reg |=ICH_MISR_EL2_NP ; -if ((hcr & ICH_HCR_EL2_VGrp0EIE) && (vmcr &ICH_VMCR_ENG0_MASK )) +if ((hcr & ICH_HCR_EL2_VGrp0EIE) && (vmcr &ICH_VMCR_EL2_VENG0_MASK )) reg |= ICH_MISR_EL2_VGrp0E; -if ((hcr & ICH_HCR_EL2_VGrp0DIE) && !(vmcr &ICH_VMCR_ENG0_MASK )) +if ((hcr & ICH_HCR_EL2_VGrp0DIE) && !(vmcr &ICH_VMCR_EL2_VENG0_MASK )) reg |= ICH_MISR_EL2_VGrp0D; -if ((hcr & ICH_HCR_EL2_VGrp1EIE) && (vmcr &ICH_VMCR_ENG1_MASK )) +if ((hcr & ICH_HCR_EL2_VGrp1EIE) && (vmcr &ICH_VMCR_EL2_VENG1_MASK )) reg |= ICH_MISR_EL2_VGrp1E; -if ((hcr & ICH_HCR_EL2_VGrp1DIE) && !(vmcr &ICH_VMCR_ENG1_MASK )) +if ((hcr & ICH_HCR_EL2_VGrp1DIE) && !(vmcr &ICH_VMCR_EL2_VENG1_MASK )) reg |= ICH_MISR_EL2_VGrp1D;return reg;
arch/arm64/kvm/vgic/vgic-v3.c +19 -23
67 lines not on this page · raw message
Signature · 1 line
2.34.1