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

From: Fuad Tabba

Date: Mon Nov 23 2026 - 02:53:37 EST


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@xxxxxxxxx>
---
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