arm64: sysreg: More input checks in the sysreg generator
cover arm64: sysreg: More input checks in the sysreg generator
Hi folks,
After finding yet another bug in the sysreg file, I think it's time to stop filling potholes and have the generator reject them instead.
The bug is in MPAM1_EL12. Its Fields line refers to a SysregFields block that doesn't exist, so its RES0, RES1 and UNKN masks expand to undefined names. The masks aren't used yet. Patch 1 fixes it.
The rest of the series adds checks to gen-sysreg.awk for input it accepts while the kernel still builds: a Sysreg and a SysregFields with the same name (Mark's suggestion [1]), a Fields or Mapping target that isn't defined, an enum value not written at its field's width, a macro defined twice, and an encoding or a bit position that's out of range or has a leading zero. Two patches prepare for them. One removes a range check that can never fail, and the other stops the end-of-file checks from running after an error.
With gawk, mawk and busybox awk, the generated header is the same before and after the series, apart from the MPAM1_EL12 lines and the number of digits in the TRFCR_ELx.TS values.
Based on Linux 7.4-rc3 (aebb727447640).
Cheers,
/fuad
[1] https://lore.kernel.org/all/aLF9mSCs1B2gk8BU@J2N7QTR9R3/
Fuad Tabba (9):
arm64: sysreg: Map MPAM1_EL12 to MPAM1_EL1
arm64: sysreg: Remove the dead range check from parse_bitdef()
arm64: sysreg: Reject a Sysreg and a SysregFields with the same name
arm64: sysreg: Don't run the end-of-file checks after an error
arm64: sysreg: Reject a Fields or Mapping line with an undefined
target
arm64: sysreg: Reject an enum value not written at its field's width
arm64: sysreg: Reject a macro name defined twice
arm64: sysreg: Reject an out-of-range register encoding
arm64: sysreg: Reject a bit position written with a leading zero
arch/arm64/tools/gen-sysreg.awk | 48 ++++++++++++++++++++++++++------- arch/arm64/tools/sysreg | 16 +++++------ 2 files changed, 47 insertions(+), 17 deletions(-)
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
Signature · 1 line
2.47.3
PATCH 1/9 arm64: sysreg: Map MPAM1_EL12 to MPAM1_EL1
MPAM1_EL12 is the EL12 alias of MPAM1_EL1, but its entry in the sysreg file is "Fields MPAM1_ELx", and the file doesn't have an MPAM1_ELx block. Since commit 6ae7fab7d9d33 ("arm64: sysreg: Emit RESx/UNKN values for Mapping/Fields definitions"), the generator's MPAM1_EL12_RES0, _RES1 and _UNKN expand to MPAM1_ELx_RES0 and the like, which aren't defined. They aren't used yet, but the first user would fail to build.
Describe the register as a mapping of MPAM1_EL1.
--- arch/arm64/tools/sysreg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/tools/sysreg +1 -1
@@ -5501,7 +5501,7 @@ Field 63:0 ADDR EndSysreg Sysreg MPAM1_EL12 3 5 10 5 0 -Fields MPAM1_ELx +Mapping MPAM1_EL1 EndSysreg Sysreg CONTEXTIDR_EL12 3 5 13 0 1
Signature · 1 line
2.47.3
Re: [PATCH 1/9] arm64: sysreg: Map MPAM1_EL12 to MPAM1_EL1
Quoted 10 lines · Fuad Tabba
MPAM1_EL12is the EL12 alias ofMPAM1_EL1, but its entry in the sysreg file is "FieldsMPAM1_ELx", and the file doesn't have anMPAM1_ELxblock. Since commit6ae7fab7d9d33("arm64: sysreg: Emit RESx/UNKN values for Mapping/Fields definitions"), the generator'sMPAM1_EL12_RES0,_RES1and_UNKNexpand toMPAM1_ELx_RES0and the like, which aren't defined. They aren't used yet, but the first user would fail to build.Describe the register as a mapping of
MPAM1_EL1.Signed-off-by Fuad Tabba <[email protected]>
Re: [PATCH 1/9] arm64: sysreg: Map MPAM1_EL12 to MPAM1_EL1
In reply to Fuad TabbaQuoted 6 lines · Fuad Tabba
MPAM1_EL12is the EL12 alias ofMPAM1_EL1, but its entry in the sysreg file is "FieldsMPAM1_ELx", and the file doesn't have anMPAM1_ELxblock. Since commit6ae7fab7d9d33("arm64: sysreg: Emit RESx/UNKN values for Mapping/Fields definitions"), the generator'sMPAM1_EL12_RES0,_RES1and_UNKNexpand toMPAM1_ELx_RES0and the like, which aren't defined. They aren't used yet, but the first user would fail to build.
PATCH 2/9 arm64: sysreg: Remove the dead range check from parse_bitdef()
In reply to Fuad Tabbaparse_bitdef() ends by testing "low > high", but the script doesn't assign either variable, so the test compares two empty values and never fires. The "msb < lsb" test just above it already rejects an inverted range.
Remove the dead test.
No functional change intended.
--- arch/arm64/tools/gen-sysreg.awk | 2 -- 1 file changed, 2 deletions(-)
arch/arm64/tools/gen-sysreg.awk +0 -2
@@ -100,8 +100,6 @@ function parse_bitdef(reg, field, bitdef, _bits) fatal(reg "." field " invalid low bit in '" bitdef "'") if (msb < lsb) fatal(reg "." field " invalid bit-range '" bitdef "'") - if (low > high) - fatal(reg "." field " has invalid range " high "-" low) next_bit = lsb - 1 }
Signature · 1 line
2.47.3
Re: [PATCH 2/9] arm64: sysreg: Remove the dead range check from parse_bitdef()
Quoted 10 lines · Fuad Tabba
parse_bitdef()ends by testing "low > high", but the script doesn't assign either variable, so the test compares two empty values and never fires. The "msb < lsb" test just above it already rejects an inverted range.Remove the dead test.
No functional change intended.
Signed-off-by Fuad Tabba <[email protected]>
Re: [PATCH 2/9] arm64: sysreg: Remove the dead range check from parse_bitdef()
In reply to Fuad TabbaQuoted 6 lines · Fuad Tabba
parse_bitdef()ends by testing "low > high", but the script doesn't assign either variable, so the test compares two empty values and never fires. The "msb < lsb" test just above it already rejects an inverted range.Remove the dead test.
PATCH 3/9 arm64: sysreg: Reject a Sysreg and a SysregFields with the same name
In reply to Fuad TabbaThe generator rejects two Sysreg blocks, or two SysregFields blocks, with the same name, but tracks the two kinds separately. Both kinds name their macros for the block, so a Sysreg FOO and a SysregFields FOO that each have a field A both define FOO_A, as well as FOO_RES0 and the other masks, and the compiler warns only where the definitions differ.
Track both kinds of name in defined_regs, so that a block is rejected when either kind already has its name, and leave the kind out of the error.
--- arch/arm64/tools/gen-sysreg.awk | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/tools/gen-sysreg.awk +4 -4
@@ -137,9 +137,9 @@ $1 == "SysregFields" && block_current() == "Root" { res1 = "UL(0)" unkn = "UL(0)" - if (reg in defined_fields) - fatal("Duplicate SysregFields definition for " reg) - defined_fields[reg] = 1 + if (reg in defined_regs) + fatal("Duplicate definition for " reg) + defined_regs[reg] = 1 next_bit = 63
@@ -181,7 +181,7 @@ $1 == "Sysreg" && block_current() == "Root" { unkn = "UL(0)" if (reg in defined_regs) - fatal("Duplicate Sysreg definition for " reg) + fatal("Duplicate definition for " reg) defined_regs[reg] = 1 define_reg("REG_" reg, "S" op0 "_" op1 "_C" crn "_C" crm "_" op2)
Signature · 1 line
2.47.3
Re: [PATCH 3/9] arm64: sysreg: Reject a Sysreg and a SysregFields with the same name
Quoted 6 lines · Fuad Tabba
The generator rejects two Sysreg blocks, or two SysregFields blocks, with the same name, but tracks the two kinds separately. Both kinds name their macros for the block, so a Sysreg FOO and a SysregFields FOO that each have a field A both define
FOO_A, as well asFOO_RES0and the other masks, and the compiler warns only where the definitions differ.
Quoted 6 lines
Track both kinds of name in
defined_regs, so that a block is rejected when either kind already has its name, and leave the kind out of the error.Suggested-by Mark Rutland <[email protected]>Signed-off-by Fuad Tabba <[email protected]>
Re: [PATCH 3/9] arm64: sysreg: Reject a Sysreg and a SysregFields with the same name
In reply to Fuad TabbaQuoted 9 lines · Fuad Tabba
The generator rejects two Sysreg blocks, or two SysregFields blocks, with the same name, but tracks the two kinds separately. Both kinds name their macros for the block, so a Sysreg FOO and a SysregFields FOO that each have a field A both define
FOO_A, as well asFOO_RES0and the other masks, and the compiler warns only where the definitions differ.Track both kinds of name in
defined_regs, so that a block is rejected when either kind already has its name, and leave the kind out of the error.
PATCH 4/9 arm64: sysreg: Don't run the end-of-file checks after an error
In reply to Fuad Tabbafatal() ends the run with exit, but awk still runs the END rule after it, and END checks the file as though it had been read to the end. An error inside a block leaves that block open, so the error is followed by a second, bogus one about a missing terminator.
Skip the END checks once fatal() has run.
--- arch/arm64/tools/gen-sysreg.awk | 5 +++++ 1 file changed, 5 insertions(+)
arch/arm64/tools/gen-sysreg.awk +5 -0
@@ -19,6 +19,7 @@ function fatal(msg) { } printf "\n" + __fatal = 1 exit 1 }
@@ -116,6 +117,10 @@ BEGIN { } END { + # fatal() exits before the whole file is read, and exit still runs END. + if (__fatal) + exit 1 + if (__current_block_depth != 0) fatal("Missing terminator for " block_current() " block")
Signature · 1 line
2.47.3
Re: [PATCH 4/9] arm64: sysreg: Don't run the end-of-file checks after an error
Quoted 8 lines · Fuad Tabba
fatal()ends the run with exit, but awk still runs the END rule after it, and END checks the file as though it had been read to the end. An error inside a block leaves that block open, so the error is followed by a second, bogus one about a missing terminator.Skip the END checks once
fatal()has run.Signed-off-by Fuad Tabba <[email protected]>
Re: [PATCH 4/9] arm64: sysreg: Don't run the end-of-file checks after an error
In reply to Fuad TabbaQuoted 6 lines · Fuad Tabba
fatal()ends the run with exit, but awk still runs the END rule after it, and END checks the file as though it had been read to the end. An error inside a block leaves that block open, so the error is followed by a second, bogus one about a missing terminator.Skip the END checks once
fatal()has run.
PATCH 5/9 arm64: sysreg: Reject a Fields or Mapping line with an undefined target
In reply to Fuad TabbaA Sysreg block can use another block's layout. "Fields X" names a SysregFields block and "Mapping X" another Sysreg. The generator emits the register's RESx and UNKN masks as X_RES0, X_RES1 and X_UNKN, but it doesn't check that X exists. A misspelt or missing X gives macros that expand to undefined names, and the build fails only once something uses them. MPAM1_EL12 had such a line.
Check every Fields and Mapping target once the whole file is read, since a target may be defined after the register that names it (MPAM1_EL1 follows MPAM1_EL12). Either kind of block defines the masks, so the check accepts either kind as the target of a Fields or a Mapping line. Give fatal() an optional line number, so that the error is reported at the Fields or Mapping line.
--- arch/arm64/tools/gen-sysreg.awk | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
arch/arm64/tools/gen-sysreg.awk +9 -2
@@ -9,8 +9,8 @@ function block_current() { } # Log an error and terminate -function fatal(msg) { - print "Error at " NR ": " msg > "/dev/stderr" +function fatal(msg, line) { + print "Error at " (line ? line : NR) ": " msg > "/dev/stderr" printf "Current block nesting:"
@@ -124,6 +124,11 @@ END { if (__current_block_depth != 0) fatal("Missing terminator for " block_current() " block") + # A Fields or Mapping target may be defined after its user. + for (line in refs) + if (!(refs[line] in defined_regs)) + fatal("Undefined Fields or Mapping target " refs[line], line) + print "#endif /* __ASM_SYSREG_DEFS_H */" }
@@ -241,6 +246,8 @@ $1 == "EndSysreg" && block_current() == "Sysreg" { print "/* For " reg " fields see " $2 " */" print "" + refs[NR] = $2 + next_bit = -1 res0 = $2 "_RES0" res1 = $2 "_RES1"
Signature · 1 line
2.47.3
Re: [PATCH 5/9] arm64: sysreg: Reject a Fields or Mapping line with an undefined target
Quoted 7 lines · Fuad Tabba
A Sysreg block can use another block's layout. "Fields X" names a SysregFields block and "Mapping X" another Sysreg. The generator emits the register's RESx and UNKN masks as
X_RES0,X_RES1andX_UNKN, but it doesn't check that X exists. A misspelt or missing X gives macros that expand to undefined names, and the build fails only once something uses them.MPAM1_EL12had such a line.
Quoted 8 lines
Check every Fields and Mapping target once the whole file is read, since a target may be defined after the register that names it (
MPAM1_EL1followsMPAM1_EL12). Either kind of block defines the masks, so the check accepts either kind as the target of a Fields or a Mapping line. Givefatal()an optional line number, so that the error is reported at the Fields or Mapping line.Signed-off-by Fuad Tabba <[email protected]>
Re: [PATCH 5/9] arm64: sysreg: Reject a Fields or Mapping line with an undefined target
In reply to Fuad TabbaQuoted 7 lines · Fuad Tabba
A Sysreg block can use another block's layout. "Fields X" names a SysregFields block and "Mapping X" another Sysreg. The generator emits the register's RESx and UNKN masks as
X_RES0,X_RES1andX_UNKN, but it doesn't check that X exists. A misspelt or missing X gives macros that expand to undefined names, and the build fails only once something uses them.MPAM1_EL12had such a line.
Quoted 6 lines
Check every Fields and Mapping target once the whole file is read, since a target may be defined after the register that names it (
MPAM1_EL1followsMPAM1_EL12). Either kind of block defines the masks, so the check accepts either kind as the target of a Fields or a Mapping line. Givefatal()an optional line number, so that the error is reported at the Fields or Mapping line.
PATCH 6/9 arm64: sysreg: Reject an enum value not written at its field's width
In reply to Fuad TabbaThe generator emits each enum value as written and doesn't check it against the width of its field, so a value wider than the field defines a constant the field can't hold. The duplicate value check compares the strings as written, so 0b01 and 0b001 in the same field pass as distinct values, and the rule's pattern, an unanchored /0b[01]+/, accepts 0b12 too.
Require each value to be 0b followed by exactly as many binary digits as its field has bits. The one enum written otherwise is TS, a two-bit field of TRFCR_EL1 and TRFCR_EL2 whose values have four digits. Write them with two digits, which leaves the numbers unchanged.
--- arch/arm64/tools/gen-sysreg.awk | 3 +++ arch/arm64/tools/sysreg | 14 +++++++------- 2 files changed, 10 insertions(+), 7 deletions(-)
arch/arm64/tools/gen-sysreg.awk +3 -0
@@ -367,6 +367,9 @@ $1 == "EndEnum" && block_current() == "Enum" { val = $1 name = $2 + if (val !~ /^0b[01]+$/ || length(val) - 2 != msb - lsb + 1) + fatal("Enum value " val " for " name " is not " (msb - lsb + 1) " binary digits") + if (val in seen_enum_vals) fatal("Duplicate Enum value " val " for " name) seen_enum_vals[val] = 1
arch/arm64/tools/sysreg +7 -7
@@ -2919,9 +2919,9 @@ EndSysreg Sysreg TRFCR_EL1 3 0 1 2 1 Res0 63:7 UnsignedEnum 6:5 TS - 0b0001 VIRTUAL - 0b0010 GUEST_PHYSICAL - 0b0011 PHYSICAL + 0b01 VIRTUAL + 0b10 GUEST_PHYSICAL + 0b11 PHYSICAL EndEnum Res0 4:2 Field 1 ExTRE
@@ -4300,10 +4300,10 @@ EndSysreg Sysreg TRFCR_EL2 3 4 1 2 1 Res0 63:7 UnsignedEnum 6:5 TS - 0b0000 USE_TRFCR_EL1_TS - 0b0001 VIRTUAL - 0b0010 GUEST_PHYSICAL - 0b0011 PHYSICAL + 0b00 USE_TRFCR_EL1_TS + 0b01 VIRTUAL + 0b10 GUEST_PHYSICAL + 0b11 PHYSICAL EndEnum Res0 4 Field 3 CX
Signature · 1 line
2.47.3
Re: [PATCH 6/9] arm64: sysreg: Reject an enum value not written at its field's width
Quoted 12 lines · Fuad Tabba
The generator emits each enum value as written and doesn't check it against the width of its field, so a value wider than the field defines a constant the field can't hold. The duplicate value check compares the strings as written, so 0b01 and 0b001 in the same field pass as distinct values, and the rule's pattern, an unanchored /0b[01]+/, accepts 0b12 too.
Require each value to be 0b followed by exactly as many binary digits as its field has bits. The one enum written otherwise is TS, a two-bit field of
TRFCR_EL1andTRFCR_EL2whose values have four digits. Write them with two digits, which leaves the numbers unchanged.
Quoted 6 lines
Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/tools/gen-sysreg.awk | 3 +++ arch/arm64/tools/sysreg | 14 +++++++------- 2 files changed, 10 insertions(+), 7 deletions(-)
Lets split this patch into two:
1. Fix the bitwidth of the Enums
2. Add the new check to the awk script
Then the fix is split apart from the enforcement.
The actual changes LGTM.
Steffen
Quoted 49 lines
arch/arm64/tools/gen-sysreg.awk +3
-0@@ -367,6 +367,9 @@ $1 == "EndEnum" && block_current() == "Enum" { val = $1 name = $2 + if (val !~ /^0b[01]+$/ || length(val) - 2 != msb - lsb + 1) + fatal("Enum value " val " for " name " is not " (msb - lsb + 1) " binary digits") + if (val in seen_enum_vals) fatal("Duplicate Enum value " val " for " name) seen_enum_vals[val] = 1arch/arm64/tools/sysreg +7
-7@@ -2919,9 +2919,9 @@ EndSysreg Sysreg TRFCR_EL1 3 0 1 2 1 Res0 63:7 UnsignedEnum 6:5 TS - 0b0001 VIRTUAL - 0b0010 GUEST_PHYSICAL - 0b0011 PHYSICAL + 0b01 VIRTUAL + 0b10 GUEST_PHYSICAL + 0b11 PHYSICAL EndEnum Res0 4:2 Field 1 ExTRE@@ -4300,10 +4300,10 @@ EndSysreg Sysreg TRFCR_EL2 3 4 1 2 1 Res0 63:7 UnsignedEnum 6:5 TS - 0b0000 USE_TRFCR_EL1_TS - 0b0001 VIRTUAL - 0b0010 GUEST_PHYSICAL - 0b0011 PHYSICAL + 0b00 USE_TRFCR_EL1_TS + 0b01 VIRTUAL + 0b10 GUEST_PHYSICAL + 0b11 PHYSICAL EndEnum Res0 4 Field 3 CXSignature · 1 line
2.47.3
Re: [PATCH 6/9] arm64: sysreg: Reject an enum value not written at its field's width
Hi Steffen,
Quoted 21 lines · Steffen Eiden
Quoted 12 lines · Fuad Tabba
The generator emits each enum value as written and doesn't check it against the width of its field, so a value wider than the field defines a constant the field can't hold. The duplicate value check compares the strings as written, so 0b01 and 0b001 in the same field pass as distinct values, and the rule's pattern, an unanchored /0b[01]+/, accepts 0b12 too.
Require each value to be 0b followed by exactly as many binary digits as its field has bits. The one enum written otherwise is TS, a two-bit field of
TRFCR_EL1andTRFCR_EL2whose values have four digits. Write them with two digits, which leaves the numbers unchanged.Quoted 6 lines
Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/tools/gen-sysreg.awk | 3 +++ arch/arm64/tools/sysreg | 14 +++++++------- 2 files changed, 10 insertions(+), 7 deletions(-)
Quoted 6 lines
Lets split this patch into two:
1. Fix the bitwidth of the Enums
2. Add the new check to the awk scriptThen the fix is split apart from the enforcement.
Ack.
Thanks for the reviews!
/fuad
Re: [PATCH 6/9] arm64: sysreg: Reject an enum value not written at its field's width
In reply to Fuad TabbaQuoted 7 lines · Fuad Tabba
The generator emits each enum value as written and doesn't check it against the width of its field, so a value wider than the field defines a constant the field can't hold. The duplicate value check compares the strings as written, so 0b01 and 0b001 in the same field pass as distinct values, and the rule's pattern, an unanchored /0b[01]+/, accepts 0b12 too.
Quoted 6 lines
Require each value to be 0b followed by exactly as many binary digits as its field has bits. The one enum written otherwise is TS, a two-bit field of
TRFCR_EL1andTRFCR_EL2whose values have four digits. Write them with two digits, which leaves the numbers unchanged.Signed-off-by Fuad Tabba <[email protected]>
PATCH 7/9 arm64: sysreg: Reject a macro name defined twice
In reply to Fuad TabbaThe generator rejects a duplicate register, SysregFields block, prefix or enum value, but not two definitions of the same macro. Two fields called F in REG make it emit REG_F twice with different masks, which the compiler reports only as a redefinition warning against the generated header, in every file that includes it. Two values of one enum sharing a name do the same, and so do names that collide only once the generator joins them: a field A_B beside value B of field A, a field F_MASK beside F, or a field B_C in FOO beside a field C in FOO_B.
Record every macro name the generator defines, and fail on the second definition. This covers a duplicate block or prefix too, but their own checks stay, since they report the duplicate at the block's own line, by block name.
--- arch/arm64/tools/gen-sysreg.awk | 4 ++++ 1 file changed, 4 insertions(+)
arch/arm64/tools/gen-sysreg.awk +4 -0
@@ -46,6 +46,10 @@ function expect_fields(nf) { # Print a CPP macro definition, padded with spaces so that the macro bodies # line up in a column function define(prefix, name, val) { + if ((prefix name) in defined_macros) + fatal("Duplicate macro " prefix name) + defined_macros[prefix name] = 1 + printf "%-56s%s\n", "#define " prefix name, val }
Signature · 1 line
2.47.3
Re: [PATCH 7/9] arm64: sysreg: Reject a macro name defined twice
Quoted 9 lines · Fuad Tabba
The generator rejects a duplicate register, SysregFields block, prefix or enum value, but not two definitions of the same macro. Two fields called F in REG make it emit
REG_Ftwice with different masks, which the compiler reports only as a redefinition warning against the generated header, in every file that includes it. Two values of one enum sharing a name do the same, and so do names that collide only once the generator joins them: a fieldA_Bbeside value B of field A, a fieldF_MASKbeside F, or a fieldB_Cin FOO beside a field C inFOO_B.
Quoted 6 lines
Record every macro name the generator defines, and fail on the second definition. This covers a duplicate block or prefix too, but their own checks stay, since they report the duplicate at the block's own line, by block name.
Signed-off-by Fuad Tabba <[email protected]>
Re: [PATCH 7/9] arm64: sysreg: Reject a macro name defined twice
In reply to Fuad TabbaQuoted 4 lines · Fuad Tabba
Record every macro name the generator defines, and fail on the second definition. This covers a duplicate block or prefix too, but their own checks stay, since they report the duplicate at the block's own line, by block name.
PATCH 8/9 arm64: sysreg: Reject an out-of-range register encoding
In reply to Fuad TabbaThe generator copies a register's op0, op1, CRn, CRm and op2 into SYS_<reg> without checking them. sys_reg() shifts each into place without masking it, so a value too wide for its field spills into the next one. SYS_<reg> then names a different register's encoding, and the build doesn't report it.
Require each of the five to be a decimal number that fits its field (two bits for op0, three for op1 and op2, four for CRn and CRm). Reject a leading zero too, since C reads such a value as octal.
--- arch/arm64/tools/gen-sysreg.awk | 12 ++++++++++++ 1 file changed, 12 insertions(+)
arch/arm64/tools/gen-sysreg.awk +12 -0
@@ -43,6 +43,12 @@ function expect_fields(nf) { fatal(NF " fields found where " nf " expected") } +# Check that one field of a register's encoding is a number in [0, max] +function check_encoding(reg, name, val, max) { + if (val !~ /^(0|[1-9][0-9]*)$/ || val > max) + fatal(reg " has invalid " name " " val) +} + # Print a CPP macro definition, padded with spaces so that the macro bodies # line up in a column function define(prefix, name, val) {
@@ -198,6 +204,12 @@ $1 == "Sysreg" && block_current() == "Root" { fatal("Duplicate definition for " reg) defined_regs[reg] = 1 + check_encoding(reg, "op0", op0, 3) + check_encoding(reg, "op1", op1, 7) + check_encoding(reg, "CRn", crn, 15) + check_encoding(reg, "CRm", crm, 15) + check_encoding(reg, "op2", op2, 7) + define_reg("REG_" reg, "S" op0 "_" op1 "_C" crn "_C" crm "_" op2) define_reg("SYS_" reg, "sys_reg(" op0 ", " op1 ", " crn ", " crm ", " op2 ")")
Signature · 1 line
2.47.3
Re: [PATCH 8/9] arm64: sysreg: Reject an out-of-range register encoding
Quoted 11 lines · Fuad Tabba
The generator copies a register's op0, op1, CRn, CRm and op2 into SYS_<reg> without checking them.
sys_reg()shifts each into place without masking it, so a value too wide for its field spills into the next one. SYS_<reg> then names a different register's encoding, and the build doesn't report it.Require each of the five to be a decimal number that fits its field (two bits for op0, three for op1 and op2, four for CRn and CRm). Reject a leading zero too, since C reads such a value as octal.
Signed-off-by Fuad Tabba <[email protected]>
Re: [PATCH 8/9] arm64: sysreg: Reject an out-of-range register encoding
In reply to Fuad TabbaQuoted 9 lines · Fuad Tabba
The generator copies a register's op0, op1, CRn, CRm and op2 into SYS_<reg> without checking them.
sys_reg()shifts each into place without masking it, so a value too wide for its field spills into the next one. SYS_<reg> then names a different register's encoding, and the build doesn't report it.Require each of the five to be a decimal number that fits its field (two bits for op0, three for op1 and op2, four for CRn and CRm). Reject a leading zero too, since C reads such a value as octal.
PATCH 9/9 arm64: sysreg: Reject a bit position written with a leading zero
In reply to Fuad Tabbaparse_bitdef() reads a field's msb and lsb as decimal and checks the layout with them, but the generator copies the digits into GENMASK() unchanged, and C reads a leading zero as octal. "Field 63:010 A" is accepted as bits 63 to 10, with the next field starting at bit 9, while its mask is GENMASK(63, 010), bits 63 to 8, which overlaps that field.
Require both ends of a bit range to be decimal numbers without a leading zero.
--- arch/arm64/tools/gen-sysreg.awk | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/tools/gen-sysreg.awk +2 -1
@@ -102,7 +102,8 @@ function parse_bitdef(reg, field, bitdef, _bits) fatal("invalid bit-range definition '" bitdef "'") } - + if (msb !~ /^(0|[1-9][0-9]*)$/ || lsb !~ /^(0|[1-9][0-9]*)$/) + fatal(reg "." field " invalid bit-range '" bitdef "'") if (msb != next_bit) fatal(reg "." field " starts at " msb " not " next_bit) if (63 < msb || msb < 0)
Signature · 1 line
2.47.3
Re: [PATCH 9/9] arm64: sysreg: Reject a bit position written with a leading zero
Quoted 19 lines · Fuad Tabba
parse_bitdef()reads a field's msb and lsb as decimal and checks the layout with them, but the generator copies the digits intoGENMASK()unchanged, and C reads a leading zero as octal. "Field 63:010 A" is accepted as bits 63 to 10, with the next field starting at bit 9, while its mask isGENMASK(63, 010), bits 63 to 8, which overlaps that field.Require both ends of a bit range to be decimal numbers without a leading zero.
Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/tools/gen-sysreg.awk | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/arm64/tools/gen-sysreg.awk +0
-0
arch/arm64/tools/gen-sysreg.awk function parse_bitdef(reg, field, bitdef, _bits) · line 106
Quoted 6 lines
arch/arm64/tools/gen-sysreg.awk +2
-1@@ -102,7 +102,8 @@ function parse_bitdef(reg, field, bitdef, _bits) fatal("invalid bit-range definition '" bitdef "'") } - + if (msb !~ /^(0|[1-9][0-9]*)$/ || lsb !~ /^(0|[1-9][0-9]*)$/) + fatal(reg "." field " invalid bit-range '" bitdef "'")
The test is good but IMO the error message should be more precise to what you are actually checking. sth like:
fatal(reg "." field " non-decimal bit-range '" bitdef "'")
Steffen
Quoted 6 lines
arch/arm64/tools/gen-sysreg.awk +0
-0if (msb != next_bit) fatal(reg "." field " starts at " msb " not " next_bit) if (63 < msb || msb < 0)Signature · 1 line
2.47.3
Re: [PATCH 9/9] arm64: sysreg: Reject a bit position written with a leading zero
On Mon, 23 Nov 2026 at 11:22, Steffen Eiden <[email protected]> wrote:
[...]
Quoted 8 lines
Quoted 8 lines
arch/arm64/tools/gen-sysreg.awk +0
-0@@ -102,7 +102,8 @@ function parse_bitdef(reg, field, bitdef, _bits) fatal("invalid bit-range definition '" bitdef "'") }
Quoted 8 lines
arch/arm64/tools/gen-sysreg.awk function parse_bitdef(reg, field, bitdef, _bits) · line 106
Quoted 3 lines
arch/arm64/tools/gen-sysreg.awk +2
-1- + if (msb !~ /^(0|[1-9][0-9]*)$/ || lsb !~ /^(0|[1-9][0-9]*)$/) + fatal(reg "." field " invalid bit-range '" bitdef "'")The test is good but IMO the error message should be more precise to what you are actually checking. sth like:
fatal(reg "." field " non-decimal bit-range '" bitdef "'")
Ack.
Thanks again,
/fuad