Skip to contentSkip to conversation

arm64: sysreg: More input checks in the sysreg generator

PATCH In review Fuad Tabba 3 participants 29 messages Nov 23 arm64

lore
Share & tools
lore

Fuad Tabba

author
#0 lore raw

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
author
#1 lore raw

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
index 997c62ab72e68..ebe5c5bd6fef6 100644
@@ -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
author
#4 lore raw

PATCH 2/9 arm64: sysreg: Remove the dead range check from parse_bitdef()

In reply to 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.

---
 arch/arm64/tools/gen-sysreg.awk | 2 --
 1 file changed, 2 deletions(-)
arch/arm64/tools/gen-sysreg.awk +0 -2
index 53844f5c5e525..b299342463631 100755
@@ -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
author
#7 lore raw

PATCH 3/9 arm64: sysreg: Reject a Sysreg and a SysregFields with the same name

In reply to 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 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
index b299342463631..8ce4981273dd3 100755
@@ -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
author
#10 lore raw

PATCH 4/9 arm64: sysreg: Don't run the end-of-file checks after an error

In reply to 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.

---
 arch/arm64/tools/gen-sysreg.awk | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm64/tools/gen-sysreg.awk +5 -0
index 8ce4981273dd3..c4fa27cd9beff 100755
@@ -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
author
#13 lore raw

PATCH 5/9 arm64: sysreg: Reject a Fields or Mapping line with an undefined target

In reply to 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_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
index c4fa27cd9beff..1843d283192d2 100755
@@ -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
author
#16 lore raw

PATCH 6/9 arm64: sysreg: Reject an enum value not written at its field's width

In reply to 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_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
index 1843d283192d2..cde526896ed96 100755
@@ -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
index ebe5c5bd6fef6..5b587a7544e40 100644
@@ -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
author
#20 lore raw

PATCH 7/9 arm64: sysreg: Reject a macro name defined twice

In reply to 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_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
index cde526896ed96..0bd6aab8aae0f 100755
@@ -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
author
#23 lore raw

PATCH 8/9 arm64: sysreg: Reject an out-of-range register encoding

In reply to 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.

---
 arch/arm64/tools/gen-sysreg.awk | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/arm64/tools/gen-sysreg.awk +12 -0
index 0bd6aab8aae0f..057dc98082b5e 100755
@@ -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
author
#26 lore raw

PATCH 9/9 arm64: sysreg: Reject a bit position written with a leading zero

In reply to 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 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
index 057dc98082b5e..01b463e271cef 100755
@@ -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
Reviewed-by on v1 1/9
#2 lore raw

Re: [PATCH 1/9] arm64: sysreg: Map MPAM1_EL12 to MPAM1_EL1

In reply to Fuad Tabba
Quoted 10 lines · Fuad Tabba

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.

Reviewed-by on v1 2/9
#5 lore raw

Re: [PATCH 2/9] arm64: sysreg: Remove the dead range check from parse_bitdef()

In reply to Fuad Tabba
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.

Reviewed-by on v1 3/9
#8 lore raw

Re: [PATCH 3/9] arm64: sysreg: Reject a Sysreg and a SysregFields with the same name

In reply to Fuad Tabba
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 as FOO_RES0 and 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.

Reviewed-by on v1 4/9
#11 lore raw

Re: [PATCH 4/9] arm64: sysreg: Don't run the end-of-file checks after an error

In reply to Fuad Tabba
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.

Reviewed-by on v1 5/9
#14 lore raw

Re: [PATCH 5/9] arm64: sysreg: Reject a Fields or Mapping line with an undefined target

In reply to Fuad Tabba
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_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.

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_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.

#17 lore raw

Re: [PATCH 6/9] arm64: sysreg: Reject an enum value not written at its field's width

In reply to Fuad Tabba
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_EL1 and TRFCR_EL2 whose values have four digits. Write them with two digits, which leaves the numbers unchanged.

Quoted 6 lines
---
 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
index 1843d283192d2..cde526896ed96 100755
@@ -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
index ebe5c5bd6fef6..5b587a7544e40 100644
@@ -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
author
#18 lore raw

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_EL1 and TRFCR_EL2 whose values have four digits. Write them with two digits, which leaves the numbers unchanged.

Quoted 6 lines
---
 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 script

Then the fix is split apart from the enforcement.

Ack.

Thanks for the reviews!
/fuad

Reviewed-by on v1 7/9
#21 lore raw

Re: [PATCH 7/9] arm64: sysreg: Reject a macro name defined twice

In reply to Fuad Tabba
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_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.

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.

Reviewed-by on v1 8/9
#24 lore raw

Re: [PATCH 8/9] arm64: sysreg: Reject an out-of-range register encoding

In reply to Fuad Tabba
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.

#27 lore raw

Re: [PATCH 9/9] arm64: sysreg: Reject a bit position written with a leading zero

In reply to Fuad Tabba
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 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 +0 -0
index 057dc98082b5e..01b463e271cef 100755

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 -0
 	if (msb != next_bit)
 		fatal(reg "." field " starts at " msb " not " next_bit)
 	if (63 < msb || msb < 0)
Signature · 1 line
2.47.3
author
#28 lore raw

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
index 057dc98082b5e..01b463e271cef 100755
@@ -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

Reviewed-by on v1 1/9
#3 lore raw

Re: [PATCH 1/9] arm64: sysreg: Map MPAM1_EL12 to MPAM1_EL1

In reply to Fuad Tabba
Quoted 6 lines · Fuad Tabba

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.

Reviewed-by on v1 2/9
#6 lore raw

Re: [PATCH 2/9] arm64: sysreg: Remove the dead range check from parse_bitdef()

In reply to Fuad Tabba
Quoted 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.

Reviewed-by on v1 3/9
#9 lore raw

Re: [PATCH 3/9] arm64: sysreg: Reject a Sysreg and a SysregFields with the same name

In reply to Fuad Tabba
Quoted 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 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.

Reviewed-by on v1 4/9
#12 lore raw

Re: [PATCH 4/9] arm64: sysreg: Don't run the end-of-file checks after an error

In reply to Fuad Tabba
Quoted 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.

Reviewed-by on v1 6/9
#19 lore raw

Re: [PATCH 6/9] arm64: sysreg: Reject an enum value not written at its field's width

In reply to Fuad Tabba
Quoted 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_EL1 and TRFCR_EL2 whose values have four digits. Write them with two digits, which leaves the numbers unchanged.

Reviewed-by on v1 7/9
#22 lore raw

Re: [PATCH 7/9] arm64: sysreg: Reject a macro name defined twice

In reply to Fuad Tabba
Quoted 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.

Reviewed-by on v1 8/9
#25 lore raw

Re: [PATCH 8/9] arm64: sysreg: Reject an out-of-range register encoding

In reply to Fuad Tabba
Quoted 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.

Reviewed-by on v1 5/9
#15 lore raw

Re: [PATCH 5/9] arm64: sysreg: Reject a Fields or Mapping line with an undefined target

In reply to Fuad Tabba
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_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.

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_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.