Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/9] arm64: sysreg: More input checks in the sysreg generator
@ 2026-11-23  7:52 Fuad Tabba
  2026-11-23  7:52 ` [PATCH 1/9] arm64: sysreg: Map MPAM1_EL12 to MPAM1_EL1 Fuad Tabba
                   ` (8 more replies)
  0 siblings, 9 replies; 16+ messages in thread
From: Fuad Tabba @ 2026-11-23  7:52 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon
  Cc: Mark Rutland, Marc Zyngier, Oliver Upton, Mark Brown,
	Sascha Bischoff, Steffen Eiden, linux-arm-kernel, linux-kernel,
	Fuad Tabba

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
-- 
2.47.3



^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 1/9] arm64: sysreg: Map MPAM1_EL12 to MPAM1_EL1
  2026-11-23  7:52 [PATCH 0/9] arm64: sysreg: More input checks in the sysreg generator Fuad Tabba
@ 2026-11-23  7:52 ` Fuad Tabba
  2026-11-23  8:47   ` Steffen Eiden
  2026-11-23  7:52 ` [PATCH 2/9] arm64: sysreg: Remove the dead range check from parse_bitdef() Fuad Tabba
                   ` (7 subsequent siblings)
  8 siblings, 1 reply; 16+ messages in thread
From: Fuad Tabba @ 2026-11-23  7:52 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon
  Cc: Mark Rutland, Marc Zyngier, Oliver Upton, Mark Brown,
	Sascha Bischoff, Steffen Eiden, linux-arm-kernel, linux-kernel,
	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.

Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
---
 arch/arm64/tools/sysreg | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/arm64/tools/sysreg b/arch/arm64/tools/sysreg
index 997c62ab72e68..ebe5c5bd6fef6 100644
--- a/arch/arm64/tools/sysreg
+++ b/arch/arm64/tools/sysreg
@@ -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
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 2/9] arm64: sysreg: Remove the dead range check from parse_bitdef()
  2026-11-23  7:52 [PATCH 0/9] arm64: sysreg: More input checks in the sysreg generator Fuad Tabba
  2026-11-23  7:52 ` [PATCH 1/9] arm64: sysreg: Map MPAM1_EL12 to MPAM1_EL1 Fuad Tabba
@ 2026-11-23  7:52 ` Fuad Tabba
  2026-11-23  8:55   ` Steffen Eiden
  2026-11-23  7:52 ` [PATCH 3/9] arm64: sysreg: Reject a Sysreg and a SysregFields with the same name Fuad Tabba
                   ` (6 subsequent siblings)
  8 siblings, 1 reply; 16+ messages in thread
From: Fuad Tabba @ 2026-11-23  7:52 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon
  Cc: Mark Rutland, Marc Zyngier, Oliver Upton, Mark Brown,
	Sascha Bischoff, Steffen Eiden, linux-arm-kernel, linux-kernel,
	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 <fuad.tabba@linux.dev>
---
 arch/arm64/tools/gen-sysreg.awk | 2 --
 1 file changed, 2 deletions(-)

diff --git a/arch/arm64/tools/gen-sysreg.awk b/arch/arm64/tools/gen-sysreg.awk
index 53844f5c5e525..b299342463631 100755
--- a/arch/arm64/tools/gen-sysreg.awk
+++ b/arch/arm64/tools/gen-sysreg.awk
@@ -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
 }
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 3/9] arm64: sysreg: Reject a Sysreg and a SysregFields with the same name
  2026-11-23  7:52 [PATCH 0/9] arm64: sysreg: More input checks in the sysreg generator Fuad Tabba
  2026-11-23  7:52 ` [PATCH 1/9] arm64: sysreg: Map MPAM1_EL12 to MPAM1_EL1 Fuad Tabba
  2026-11-23  7:52 ` [PATCH 2/9] arm64: sysreg: Remove the dead range check from parse_bitdef() Fuad Tabba
@ 2026-11-23  7:52 ` Fuad Tabba
  2026-11-23 10:25   ` Steffen Eiden
  2026-11-23  7:52 ` [PATCH 4/9] arm64: sysreg: Don't run the end-of-file checks after an error Fuad Tabba
                   ` (5 subsequent siblings)
  8 siblings, 1 reply; 16+ messages in thread
From: Fuad Tabba @ 2026-11-23  7:52 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon
  Cc: Mark Rutland, Marc Zyngier, Oliver Upton, Mark Brown,
	Sascha Bischoff, Steffen Eiden, linux-arm-kernel, linux-kernel,
	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.

Suggested-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
---
 arch/arm64/tools/gen-sysreg.awk | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/arch/arm64/tools/gen-sysreg.awk b/arch/arm64/tools/gen-sysreg.awk
index b299342463631..8ce4981273dd3 100755
--- a/arch/arm64/tools/gen-sysreg.awk
+++ b/arch/arm64/tools/gen-sysreg.awk
@@ -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)
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 4/9] arm64: sysreg: Don't run the end-of-file checks after an error
  2026-11-23  7:52 [PATCH 0/9] arm64: sysreg: More input checks in the sysreg generator Fuad Tabba
                   ` (2 preceding siblings ...)
  2026-11-23  7:52 ` [PATCH 3/9] arm64: sysreg: Reject a Sysreg and a SysregFields with the same name Fuad Tabba
@ 2026-11-23  7:52 ` Fuad Tabba
  2026-11-23 10:29   ` Steffen Eiden
  2026-11-23  7:52 ` [PATCH 5/9] arm64: sysreg: Reject a Fields or Mapping line with an undefined target Fuad Tabba
                   ` (4 subsequent siblings)
  8 siblings, 1 reply; 16+ messages in thread
From: Fuad Tabba @ 2026-11-23  7:52 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon
  Cc: Mark Rutland, Marc Zyngier, Oliver Upton, Mark Brown,
	Sascha Bischoff, Steffen Eiden, linux-arm-kernel, linux-kernel,
	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 <fuad.tabba@linux.dev>
---
 arch/arm64/tools/gen-sysreg.awk | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/arm64/tools/gen-sysreg.awk b/arch/arm64/tools/gen-sysreg.awk
index 8ce4981273dd3..c4fa27cd9beff 100755
--- a/arch/arm64/tools/gen-sysreg.awk
+++ b/arch/arm64/tools/gen-sysreg.awk
@@ -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")
 
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 5/9] arm64: sysreg: Reject a Fields or Mapping line with an undefined target
  2026-11-23  7:52 [PATCH 0/9] arm64: sysreg: More input checks in the sysreg generator Fuad Tabba
                   ` (3 preceding siblings ...)
  2026-11-23  7:52 ` [PATCH 4/9] arm64: sysreg: Don't run the end-of-file checks after an error Fuad Tabba
@ 2026-11-23  7:52 ` Fuad Tabba
  2026-11-23 10:40   ` Steffen Eiden
  2026-11-23  7:52 ` [PATCH 6/9] arm64: sysreg: Reject an enum value not written at its field's width Fuad Tabba
                   ` (3 subsequent siblings)
  8 siblings, 1 reply; 16+ messages in thread
From: Fuad Tabba @ 2026-11-23  7:52 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon
  Cc: Mark Rutland, Marc Zyngier, Oliver Upton, Mark Brown,
	Sascha Bischoff, Steffen Eiden, linux-arm-kernel, linux-kernel,
	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.

Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
---
 arch/arm64/tools/gen-sysreg.awk | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/tools/gen-sysreg.awk b/arch/arm64/tools/gen-sysreg.awk
index c4fa27cd9beff..1843d283192d2 100755
--- a/arch/arm64/tools/gen-sysreg.awk
+++ b/arch/arm64/tools/gen-sysreg.awk
@@ -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"
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 6/9] arm64: sysreg: Reject an enum value not written at its field's width
  2026-11-23  7:52 [PATCH 0/9] arm64: sysreg: More input checks in the sysreg generator Fuad Tabba
                   ` (4 preceding siblings ...)
  2026-11-23  7:52 ` [PATCH 5/9] arm64: sysreg: Reject a Fields or Mapping line with an undefined target Fuad Tabba
@ 2026-11-23  7:52 ` Fuad Tabba
  2026-11-23 10:45   ` Steffen Eiden
  2026-11-23  7:52 ` [PATCH 7/9] arm64: sysreg: Reject a macro name defined twice Fuad Tabba
                   ` (2 subsequent siblings)
  8 siblings, 1 reply; 16+ messages in thread
From: Fuad Tabba @ 2026-11-23  7:52 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon
  Cc: Mark Rutland, Marc Zyngier, Oliver Upton, Mark Brown,
	Sascha Bischoff, Steffen Eiden, linux-arm-kernel, linux-kernel,
	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.

Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
---
 arch/arm64/tools/gen-sysreg.awk |  3 +++
 arch/arm64/tools/sysreg         | 14 +++++++-------
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/arch/arm64/tools/gen-sysreg.awk b/arch/arm64/tools/gen-sysreg.awk
index 1843d283192d2..cde526896ed96 100755
--- a/arch/arm64/tools/gen-sysreg.awk
+++ b/arch/arm64/tools/gen-sysreg.awk
@@ -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
diff --git a/arch/arm64/tools/sysreg b/arch/arm64/tools/sysreg
index ebe5c5bd6fef6..5b587a7544e40 100644
--- a/arch/arm64/tools/sysreg
+++ b/arch/arm64/tools/sysreg
@@ -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
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 7/9] arm64: sysreg: Reject a macro name defined twice
  2026-11-23  7:52 [PATCH 0/9] arm64: sysreg: More input checks in the sysreg generator Fuad Tabba
                   ` (5 preceding siblings ...)
  2026-11-23  7:52 ` [PATCH 6/9] arm64: sysreg: Reject an enum value not written at its field's width Fuad Tabba
@ 2026-11-23  7:52 ` Fuad Tabba
  2026-11-23  7:52 ` [PATCH 8/9] arm64: sysreg: Reject an out-of-range register encoding Fuad Tabba
  2026-11-23  7:52 ` [PATCH 9/9] arm64: sysreg: Reject a bit position written with a leading zero Fuad Tabba
  8 siblings, 0 replies; 16+ messages in thread
From: Fuad Tabba @ 2026-11-23  7:52 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon
  Cc: Mark Rutland, Marc Zyngier, Oliver Upton, Mark Brown,
	Sascha Bischoff, Steffen Eiden, linux-arm-kernel, linux-kernel,
	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.

Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
---
 arch/arm64/tools/gen-sysreg.awk | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/arm64/tools/gen-sysreg.awk b/arch/arm64/tools/gen-sysreg.awk
index cde526896ed96..0bd6aab8aae0f 100755
--- a/arch/arm64/tools/gen-sysreg.awk
+++ b/arch/arm64/tools/gen-sysreg.awk
@@ -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
 }
 
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 8/9] arm64: sysreg: Reject an out-of-range register encoding
  2026-11-23  7:52 [PATCH 0/9] arm64: sysreg: More input checks in the sysreg generator Fuad Tabba
                   ` (6 preceding siblings ...)
  2026-11-23  7:52 ` [PATCH 7/9] arm64: sysreg: Reject a macro name defined twice Fuad Tabba
@ 2026-11-23  7:52 ` Fuad Tabba
  2026-11-23  7:52 ` [PATCH 9/9] arm64: sysreg: Reject a bit position written with a leading zero Fuad Tabba
  8 siblings, 0 replies; 16+ messages in thread
From: Fuad Tabba @ 2026-11-23  7:52 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon
  Cc: Mark Rutland, Marc Zyngier, Oliver Upton, Mark Brown,
	Sascha Bischoff, Steffen Eiden, linux-arm-kernel, linux-kernel,
	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 <fuad.tabba@linux.dev>
---
 arch/arm64/tools/gen-sysreg.awk | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/arch/arm64/tools/gen-sysreg.awk b/arch/arm64/tools/gen-sysreg.awk
index 0bd6aab8aae0f..057dc98082b5e 100755
--- a/arch/arm64/tools/gen-sysreg.awk
+++ b/arch/arm64/tools/gen-sysreg.awk
@@ -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 ")")
 
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 9/9] arm64: sysreg: Reject a bit position written with a leading zero
  2026-11-23  7:52 [PATCH 0/9] arm64: sysreg: More input checks in the sysreg generator Fuad Tabba
                   ` (7 preceding siblings ...)
  2026-11-23  7:52 ` [PATCH 8/9] arm64: sysreg: Reject an out-of-range register encoding Fuad Tabba
@ 2026-11-23  7:52 ` Fuad Tabba
  8 siblings, 0 replies; 16+ messages in thread
From: Fuad Tabba @ 2026-11-23  7:52 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon
  Cc: Mark Rutland, Marc Zyngier, Oliver Upton, Mark Brown,
	Sascha Bischoff, Steffen Eiden, linux-arm-kernel, linux-kernel,
	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.

Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
---
 arch/arm64/tools/gen-sysreg.awk | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/tools/gen-sysreg.awk b/arch/arm64/tools/gen-sysreg.awk
index 057dc98082b5e..01b463e271cef 100755
--- a/arch/arm64/tools/gen-sysreg.awk
+++ b/arch/arm64/tools/gen-sysreg.awk
@@ -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)
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/9] arm64: sysreg: Map MPAM1_EL12 to MPAM1_EL1
  2026-11-23  7:52 ` [PATCH 1/9] arm64: sysreg: Map MPAM1_EL12 to MPAM1_EL1 Fuad Tabba
@ 2026-11-23  8:47   ` Steffen Eiden
  0 siblings, 0 replies; 16+ messages in thread
From: Steffen Eiden @ 2026-11-23  8:47 UTC (permalink / raw)
  To: Fuad Tabba
  Cc: Catalin Marinas, Will Deacon, Mark Rutland, Marc Zyngier,
	Oliver Upton, Mark Brown, Sascha Bischoff, linux-arm-kernel,
	linux-kernel, Fuad Tabba

On Mon, Nov 23, 2026 at 07:52:11AM +0000, Fuad Tabba wrote:
> 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.
> 
> Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>

Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
 


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 2/9] arm64: sysreg: Remove the dead range check from parse_bitdef()
  2026-11-23  7:52 ` [PATCH 2/9] arm64: sysreg: Remove the dead range check from parse_bitdef() Fuad Tabba
@ 2026-11-23  8:55   ` Steffen Eiden
  0 siblings, 0 replies; 16+ messages in thread
From: Steffen Eiden @ 2026-11-23  8:55 UTC (permalink / raw)
  To: Fuad Tabba
  Cc: Catalin Marinas, Will Deacon, Mark Rutland, Marc Zyngier,
	Oliver Upton, Mark Brown, Sascha Bischoff, linux-arm-kernel,
	linux-kernel, Fuad Tabba

On Mon, Nov 23, 2026 at 07:52:12AM +0000, Fuad Tabba wrote:
> 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 <fuad.tabba@linux.dev>

Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
 


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 3/9] arm64: sysreg: Reject a Sysreg and a SysregFields with the same name
  2026-11-23  7:52 ` [PATCH 3/9] arm64: sysreg: Reject a Sysreg and a SysregFields with the same name Fuad Tabba
@ 2026-11-23 10:25   ` Steffen Eiden
  0 siblings, 0 replies; 16+ messages in thread
From: Steffen Eiden @ 2026-11-23 10:25 UTC (permalink / raw)
  To: Fuad Tabba
  Cc: Catalin Marinas, Will Deacon, Mark Rutland, Marc Zyngier,
	Oliver Upton, Mark Brown, Sascha Bischoff, linux-arm-kernel,
	linux-kernel, Fuad Tabba

On Mon, Nov 23, 2026 at 07:52:13AM +0000, Fuad Tabba wrote:
> 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.
> 
> Suggested-by: Mark Rutland <mark.rutland@arm.com>
> Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>

Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 4/9] arm64: sysreg: Don't run the end-of-file checks after an error
  2026-11-23  7:52 ` [PATCH 4/9] arm64: sysreg: Don't run the end-of-file checks after an error Fuad Tabba
@ 2026-11-23 10:29   ` Steffen Eiden
  0 siblings, 0 replies; 16+ messages in thread
From: Steffen Eiden @ 2026-11-23 10:29 UTC (permalink / raw)
  To: Fuad Tabba
  Cc: Catalin Marinas, Will Deacon, Mark Rutland, Marc Zyngier,
	Oliver Upton, Mark Brown, Sascha Bischoff, linux-arm-kernel,
	linux-kernel, Fuad Tabba

On Mon, Nov 23, 2026 at 07:52:14AM +0000, Fuad Tabba wrote:
> 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 <fuad.tabba@linux.dev>

Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 5/9] arm64: sysreg: Reject a Fields or Mapping line with an undefined target
  2026-11-23  7:52 ` [PATCH 5/9] arm64: sysreg: Reject a Fields or Mapping line with an undefined target Fuad Tabba
@ 2026-11-23 10:40   ` Steffen Eiden
  0 siblings, 0 replies; 16+ messages in thread
From: Steffen Eiden @ 2026-11-23 10:40 UTC (permalink / raw)
  To: Fuad Tabba
  Cc: Catalin Marinas, Will Deacon, Mark Rutland, Marc Zyngier,
	Oliver Upton, Mark Brown, Sascha Bischoff, linux-arm-kernel,
	linux-kernel, Fuad Tabba

On Mon, Nov 23, 2026 at 07:52:15AM +0000, Fuad Tabba wrote:
> 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.
> 
> Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>

Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
 


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 6/9] arm64: sysreg: Reject an enum value not written at its field's width
  2026-11-23  7:52 ` [PATCH 6/9] arm64: sysreg: Reject an enum value not written at its field's width Fuad Tabba
@ 2026-11-23 10:45   ` Steffen Eiden
  0 siblings, 0 replies; 16+ messages in thread
From: Steffen Eiden @ 2026-11-23 10:45 UTC (permalink / raw)
  To: Fuad Tabba
  Cc: Catalin Marinas, Will Deacon, Mark Rutland, Marc Zyngier,
	Oliver Upton, Mark Brown, Sascha Bischoff, linux-arm-kernel,
	linux-kernel, Fuad Tabba

On Mon, Nov 23, 2026 at 07:52:16AM +0000, Fuad Tabba wrote:
> 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.
> 
> Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
> ---
>  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


> diff --git a/arch/arm64/tools/gen-sysreg.awk b/arch/arm64/tools/gen-sysreg.awk
> index 1843d283192d2..cde526896ed96 100755
> --- a/arch/arm64/tools/gen-sysreg.awk
> +++ b/arch/arm64/tools/gen-sysreg.awk
> @@ -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
> diff --git a/arch/arm64/tools/sysreg b/arch/arm64/tools/sysreg
> index ebe5c5bd6fef6..5b587a7544e40 100644
> --- a/arch/arm64/tools/sysreg
> +++ b/arch/arm64/tools/sysreg
> @@ -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
> -- 
> 2.47.3
> 


^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2026-11-23 10:46 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23  7:52 [PATCH 0/9] arm64: sysreg: More input checks in the sysreg generator Fuad Tabba
2026-11-23  7:52 ` [PATCH 1/9] arm64: sysreg: Map MPAM1_EL12 to MPAM1_EL1 Fuad Tabba
2026-11-23  8:47   ` Steffen Eiden
2026-11-23  7:52 ` [PATCH 2/9] arm64: sysreg: Remove the dead range check from parse_bitdef() Fuad Tabba
2026-11-23  8:55   ` Steffen Eiden
2026-11-23  7:52 ` [PATCH 3/9] arm64: sysreg: Reject a Sysreg and a SysregFields with the same name Fuad Tabba
2026-11-23 10:25   ` Steffen Eiden
2026-11-23  7:52 ` [PATCH 4/9] arm64: sysreg: Don't run the end-of-file checks after an error Fuad Tabba
2026-11-23 10:29   ` Steffen Eiden
2026-11-23  7:52 ` [PATCH 5/9] arm64: sysreg: Reject a Fields or Mapping line with an undefined target Fuad Tabba
2026-11-23 10:40   ` Steffen Eiden
2026-11-23  7:52 ` [PATCH 6/9] arm64: sysreg: Reject an enum value not written at its field's width Fuad Tabba
2026-11-23 10:45   ` Steffen Eiden
2026-11-23  7:52 ` [PATCH 7/9] arm64: sysreg: Reject a macro name defined twice Fuad Tabba
2026-11-23  7:52 ` [PATCH 8/9] arm64: sysreg: Reject an out-of-range register encoding Fuad Tabba
2026-11-23  7:52 ` [PATCH 9/9] arm64: sysreg: Reject a bit position written with a leading zero Fuad Tabba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox