Skip to content

Commit 18b8f57

Browse files
James-A-Clarkwilldeacon
authored andcommitted
arm64: sysreg: Generate C compiler warnings on {read,write}_sysreg_s arguments
Evaluate the register before the asm section so that the C compiler generates warnings when there is an issue with the register argument. This will prevent possible future issues such as the one seen here [1] where a missing bracket caused the shift and addition operators to be evaluated in the wrong order, but no warning was emitted. The GNU assembler has no warning for when expressions evaluate differently to C due to different operator precedence, but the C compiler has some warnings that may suggest something is wrong. For example in this case the following warning would have been emitted: error: operator '>>' has lower precedence than '+'; '+' will be evaluated first [-Werror,-Wshift-op-parentheses] There are currently no existing warnings that need to be fixed. [1]: https://lore.kernel.org/linux-perf-users/20230728162011.GA22050@willie-the-truck/ Signed-off-by: James Clark <james.clark@arm.com> Link: https://lore.kernel.org/r/20230815140639.614769-1-james.clark@arm.com Signed-off-by: Will Deacon <will@kernel.org>
1 parent 5cd474e commit 18b8f57

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

arch/arm64/include/asm/sysreg.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,15 +803,21 @@
803803
/*
804804
* For registers without architectural names, or simply unsupported by
805805
* GAS.
806+
*
807+
* __check_r forces warnings to be generated by the compiler when
808+
* evaluating r which wouldn't normally happen due to being passed to
809+
* the assembler via __stringify(r).
806810
*/
807811
#define read_sysreg_s(r) ({ \
808812
u64 __val; \
813+
u32 __maybe_unused __check_r = (u32)(r); \
809814
asm volatile(__mrs_s("%0", r) : "=r" (__val)); \
810815
__val; \
811816
})
812817

813818
#define write_sysreg_s(v, r) do { \
814819
u64 __val = (u64)(v); \
820+
u32 __maybe_unused __check_r = (u32)(r); \
815821
asm volatile(__msr_s(r, "%x0") : : "rZ" (__val)); \
816822
} while (0)
817823

0 commit comments

Comments
 (0)