diff --git a/arch/powerpc/include/asm/bug.h b/arch/powerpc/include/asm/bug.h index 0db48977c70c38..6f0f652d934877 100644 --- a/arch/powerpc/include/asm/bug.h +++ b/arch/powerpc/include/asm/bug.h @@ -32,34 +32,38 @@ #endif /* verbose */ #else /* !__ASSEMBLER__ */ -/* _EMIT_BUG_ENTRY expects args %0,%1,%2,%3 to be FILE, LINE, flags and - sizeof(struct bug_entry), respectively */ #ifdef CONFIG_DEBUG_BUGVERBOSE -#define _EMIT_BUG_ENTRY \ - ".section __bug_table,\"aw\"\n" \ - "2: .4byte 1b - .\n" \ - " .4byte %0 - .\n" \ - " .short %1, %2\n" \ - ".org 2b+%3\n" \ - ".previous\n" +#define _EMIT_BUG_ENTRY(label, file, line, flags) \ + ".section __bug_table,\"aw\"\n" \ + "2: .4byte " #label " - .\n" \ + " .4byte " file " - .\n" \ + " .short " line ", " flags "\n" #else -#define _EMIT_BUG_ENTRY \ - ".section __bug_table,\"aw\"\n" \ - "2: .4byte 1b - .\n" \ - " .short %2\n" \ - ".org 2b+%3\n" \ - ".previous\n" +#define _EMIT_BUG_ENTRY(label, file, line, flags) \ + ".section __bug_table,\"aw\"\n" \ + "2: .4byte " #label " - .\n" \ + " .short " flags "\n" #endif #define BUG_ENTRY(cond_str, insn, flags, ...) \ __asm__ __volatile__( \ "1: " insn "\n" \ - _EMIT_BUG_ENTRY \ + _EMIT_BUG_ENTRY(1b, "%0", "%1", "%2") \ + ".org 2b+%3\n" \ + ".previous\n" \ : : "i" (WARN_CONDITION_STR(cond_str) __FILE__), "i" (__LINE__), \ "i" (flags), \ "i" (sizeof(struct bug_entry)), \ ##__VA_ARGS__) +#define ARCH_WARN_ASM(file, line, flags, size) \ + "1: twi 31, 0, 0\n" \ + _EMIT_BUG_ENTRY(1b, file, line, flags) \ + ".org 2b+" size "\n" \ + ".previous\n" + +#define ARCH_WARN_REACHABLE + /* * BUG_ON() and WARN_ON() do their best to cooperate with compile-time * optimisations. However depending on the complexity of the condition diff --git a/rust/kernel/Kconfig.test b/rust/kernel/Kconfig.test index e6a5c7a795f0fc..5a82f0812c890d 100644 --- a/rust/kernel/Kconfig.test +++ b/rust/kernel/Kconfig.test @@ -83,4 +83,17 @@ config RUST_BITFIELD_KUNIT_TEST If unsure, say N. +config RUST_BUG_POWERPC_KUNIT_TEST + bool "KUnit tests for powerpc ARCH_WARN_ASM bug table emission" if !KUNIT_ALL_TESTS + depends on PPC && GENERIC_BUG + default KUNIT_ALL_TESTS + help + This option enables KUnit tests that verify ARCH_WARN_ASM emits a + correct __bug_table entry on powerpc (both ppc32 and ppc64): the + bug_addr displacement must resolve back to the trap instruction so + that find_bug() can locate the entry — exactly as the real trap + handler does. + + If unsure, say N. + endif diff --git a/rust/kernel/bug.rs b/rust/kernel/bug.rs index 3566f0234ca403..da43e62ddcdd67 100644 --- a/rust/kernel/bug.rs +++ b/rust/kernel/bug.rs @@ -152,3 +152,174 @@ macro_rules! warn_on { cond }}; } + +// Test-only constants and file static referenced by the global_asm block below. +// +// global_asm! is file-scope and always emitted — LLVM cannot eliminate it, +// unlike asm! inside a function which is subject to dead-code removal. +// +// BUG_KUNIT_TRAP_ADDR is declared as a .global symbol entirely inside the +// global_asm! block so the .dc.a 1b relocation lands directly on it. +// A Rust static initialized to zero would end up in BSS; the linker does +// not apply relocations to BSS, so the address would stay zero at runtime. +#[cfg(CONFIG_RUST_BUG_POWERPC_KUNIT_TEST)] +mod test_statics { + use crate::bindings::{bug_entry, BUGFLAG_WARNING, TAINT_WARN}; + + pub(super) const FLAGS: u32 = BUGFLAG_WARNING | (TAINT_WARN << 8); + pub(super) const SIZE: usize = core::mem::size_of::(); + pub(super) const LINE: u32 = line!(); + + // Null-terminated source file name — the assembler references this symbol + // for the verbose file pointer in __bug_table, same as warn_flags!. + const _FILE: &[u8] = file!().as_bytes(); + #[no_mangle] + pub(super) static BUG_KUNIT_FILE: [u8; _FILE.len() + 1] = { + let mut bytes = [0u8; _FILE.len() + 1]; + let mut i = 0; + while i < _FILE.len() { + bytes[i] = _FILE[i]; + i += 1; + } + bytes + }; +} + +// Emit ARCH_WARN_ASM at file scope and capture the trap address. +// +// BUG_KUNIT_TRAP_ADDR is defined as a .global symbol right on top of the +// .dc.a 1b directive so the linker resolves the relocation directly into +// that symbol's storage — no BSS, no zero-init problem. +// .dc.a emits a pointer-width word (4 bytes on ppc32, 8 bytes on ppc64), +// matching the usize declaration on the Rust side. +#[cfg(all(CONFIG_RUST_BUG_POWERPC_KUNIT_TEST, CONFIG_PPC64))] +::core::arch::global_asm!( + concat!( + include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_warn_asm.rs")), + ".pushsection .data\n\t", + ".balign 8\n\t", + ".global BUG_KUNIT_TRAP_ADDR\n\t", + "BUG_KUNIT_TRAP_ADDR:\n\t", + ".dc.a 1b\n\t", + ".popsection\n", + ), + file = sym test_statics::BUG_KUNIT_FILE, + line = const test_statics::LINE, + flags = const test_statics::FLAGS, + size = const test_statics::SIZE, +); + +#[cfg(all(CONFIG_RUST_BUG_POWERPC_KUNIT_TEST, not(CONFIG_PPC64)))] +::core::arch::global_asm!( + concat!( + include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_warn_asm.rs")), + ".pushsection .data\n\t", + ".balign 4\n\t", + ".global BUG_KUNIT_TRAP_ADDR\n\t", + "BUG_KUNIT_TRAP_ADDR:\n\t", + ".dc.a 1b\n\t", + ".popsection\n", + ), + file = sym test_statics::BUG_KUNIT_FILE, + line = const test_statics::LINE, + flags = const test_statics::FLAGS, + size = const test_statics::SIZE, +); + +#[cfg(CONFIG_RUST_BUG_POWERPC_KUNIT_TEST)] +#[::kernel::macros::kunit_tests(rust_kernel_bug_powerpc)] +mod tests { + use crate::bindings; + + fn trap_addr() -> usize { + // BUG_KUNIT_TRAP_ADDR is a .global symbol defined in the global_asm! + // block above, placed in .data at the exact .dc.a 1b relocation word. + // The linker resolves it to the virtual address of the twi instruction + // before any Rust code runs, so reading it here is always safe. + extern "C" { + // .dc.a emits a pointer-width word: 4 bytes on ppc32, 8 on ppc64. + // usize matches the native pointer width on both. + static BUG_KUNIT_TRAP_ADDR: usize; + } + // SAFETY: read-only after link time, no concurrent mutation possible. + unsafe { BUG_KUNIT_TRAP_ADDR } + } + + /// The `__bug_table` entry emitted by `ARCH_WARN_ASM` must be locatable + /// via `find_bug()` using the trap instruction's address. A NULL result + /// means the `1b` label reference in `_EMIT_BUG_ENTRY` resolved to the + /// wrong address and the real trap handler would not recognise the site. + #[test] + fn bug_entry_found() { + // Non-zero proves the .dc.a relocation was resolved by the linker. + assert!(trap_addr() != 0); + + // SAFETY: find_bug() is always safe to call with any address; it + // simply walks __bug_table and returns NULL if nothing matches. + let entry = unsafe { bindings::find_bug(trap_addr()) }; + // Non-NULL proves the bug_addr displacement in _EMIT_BUG_ENTRY is correct. + assert!(!entry.is_null()); + } + + /// The emitted entry must be flagged as a warning (not a hard BUG). + #[test] + fn bug_entry_is_warning() { + assert!(trap_addr() != 0); + let entry = unsafe { bindings::find_bug(trap_addr()) }; + assert!(!entry.is_null()); + // SAFETY: entry is non-null and points to a valid bug_entry. + let flags = unsafe { (*entry).flags } as u32; + assert!(flags & bindings::BUGFLAG_WARNING != 0); + } + + /// With `CONFIG_DEBUG_BUGVERBOSE` the entry must record a non-null file + /// pointer pointing back into this source file. + #[test] + #[cfg(CONFIG_DEBUG_BUGVERBOSE)] + fn bug_entry_file() { + use core::ffi::CStr; + + assert!(trap_addr() != 0); + let entry = unsafe { bindings::find_bug(trap_addr()) }; + assert!(!entry.is_null()); + + let mut file_ptr: *const core::ffi::c_char = core::ptr::null(); + let mut line: u32 = 0; + // SAFETY: entry is non-null and valid; file_ptr and line are local + // variables passed as out-parameters. + unsafe { bindings::bug_get_file_line(entry, &mut file_ptr, &mut line) }; + + assert!(!file_ptr.is_null()); + // SAFETY: file_ptr is a null-terminated C string from BUG_KUNIT_FILE. + let file_str = unsafe { CStr::from_ptr(file_ptr) }.to_str().unwrap_or(""); + assert!(file_str.contains("bug")); + } + + /// With `CONFIG_DEBUG_BUGVERBOSE` the recorded line number must be + /// non-zero (a zero line would mean the asm operand was not substituted). + #[test] + #[cfg(CONFIG_DEBUG_BUGVERBOSE)] + fn bug_entry_line() { + assert!(trap_addr() != 0); + let entry = unsafe { bindings::find_bug(trap_addr()) }; + assert!(!entry.is_null()); + + let mut file_ptr: *const core::ffi::c_char = core::ptr::null(); + let mut line: u32 = 0; + // SAFETY: entry is non-null and valid. + unsafe { bindings::bug_get_file_line(entry, &mut file_ptr, &mut line) }; + + assert!(line != 0); + } + + /// The trap address stored in `__bug_table` must lie within the kernel + /// text segment. If the label reference in `_EMIT_BUG_ENTRY` resolved + /// to data or zero, `kernel_text_address()` would return false. + #[test] + fn bug_entry_addr_is_in_text() { + assert!(trap_addr() != 0); + // SAFETY: kernel_text_address() is always safe to call with any addr. + let in_text = unsafe { bindings::kernel_text_address(trap_addr()) }; + assert!(in_text != 0); + } +}