From nobody Thu Sep 24 17:02:50 2026 Received: from mail-pz2-f41.google.com (mail-pz2-f41.google.com [74.125.228.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3996D49F12F for ; Tue, 22 Sep 2026 05:50:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.228.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790056245; cv=none; b=eVhNlf2uMAKKGeP5cJ2MhkEwdV3pC3jvXfzwOgkdpOEqYUNRGHzXg2C6NWOhvuoHpq/Et/+uht5yTQc1DCGhhd4Jx0ykXQXAv5PHa/E3pqQosuHEwctBOHHhI+XO+7+CPslS0dw8wT9Z/zmdYutMYCQ9+rxLlExhFw4E2fBZR6o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790056245; c=relaxed/simple; bh=Jhq62GU8LvxuE6TM7ziiSU8QyEtvcFjcdIwqIWkz9g0=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; b=m5/6yUNXhiAd2wbkXfVGF0MpwIovZE4uJoodZ+0noch2XkgO2TrHxgqXvl7beL3VHyzpkOGzGuWjcWlw9dFIBDpPGRtfJLO2/uop5gWBb6J4zjq/syGuJJqGgE5vGVBFnjthUrGuU5wlvxFRSKdmfdHTDcpewYzEkPep1nUIEfs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ICaVWLKO; arc=none smtp.client-ip=74.125.228.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ICaVWLKO" Received: by mail-pz2-f41.google.com with SMTP id d2e1a72fcca58-85469b2e1d5so3813013b3a.1 for ; Mon, 21 Sep 2026 22:50:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790056241; x=1790661041; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:message-id:date :subject:to:from:from:to:cc:subject:date:message-id:reply-to :content-type; bh=rKB+Q6LIx1d58ckFivalSd71pp5dS1NppOZBiMZAHqE=; b=ICaVWLKOmFdfuhnybBFyuHsyh7QaPrtmKO8epuHKid+KMfVcBHqsuKmPo0g7Vt7WZE tdDt9mllF5vKNpEqUgotyQGGYYYte2zemakkxik+1u5D+G5LpH1TevxpTO7v/HdtDVch 7bv6SougHdIk9jbAjCMWmDotc3GC3dY5HbE1naEuEujQBlGZGjxdcPsmPRDlc8oOvzU2 fw9pxDLKlOnl7zuqicU7k4MD3XsTsxxVPMZdoKf5GJ5onIumyqWNk35arHZXUyFu9/Km sVGsNK9ugrfp9zviF/Mj/eLqrRt9W/CjbMyhQn8GMOuKo1uBfYgXsR0fLkUx/tKYznob +JAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790056241; x=1790661041; h=content-transfer-encoding:content-type:mime-version:message-id:date :subject:to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=rKB+Q6LIx1d58ckFivalSd71pp5dS1NppOZBiMZAHqE=; b=GrG9RM0Uug3cVZDOtuoXxfadcsYN7eeS9TN47tbzrhKM6MVYrMYZQrAzzPXbF8n8KK ykBCmV1OC7HzciXlRSLgAWJQ7zCDPmCUofSB0duLSd/meie/XkhDjy0viy6uW0EKA00c 95Fa5QUID/SU0nonffiBcuYC0oSikAW6X4fPhmtcSP2aGI3VfUpU+nuF/QOI/OqVryj0 rtXkV9gSdoqJ96jdlltqS7njub/OZu2XDafpKi0oHUFs7unZiAFDqaFIx/WQWaHUOvR5 GGLneOrXp6+lGJ/4Bn+wdSkh1DPqMi5vofNAMRMecxwUfHQbsZhaXEbB8Iq8vghMXklt T/mQ== X-Forwarded-Encrypted: i=1; AKwUvBz9sqvRlRDrJJMKr+pcjwZZYI+wqHZVGvc0jylZyHKA3H3OdOzOG5X4KpH6GmxDc/YCMCu3xEhG9NckR0Y=@vger.kernel.org X-Gm-Message-State: AFuF++kG/AihB6voH8wQhWYucN3IxZPJcndLGkWzkrJIB2xCzZfV+mjr iC3n8LLLCpDCqr7icCh7d1ksBoWhmJGI55r+roLXL0yzdOTihsp74TQd X-Gm-Gg: AYBFou1PxRu1SKk7AwRKU+GPYNaR+UjdUmWh4gVLtJLjjt0FNSkH3N07Jua3xIbecc1 00TfDqJak7PyuogVj3a6EZ1/0VXsUQFoMd+VA77U7r0gUUUTzn5z16dWy3Yxf6qSfPm2UUqPMWw ci9KEjUILAAWY1jLB79Z9ldISZURK8fPb+zfHGUjIpqKjasE5r60wbCMUk6npEjBISzdW8L5UxF 45FCSMltxJV8M+hGirr4Uj60RvqZXJen0ZliIQiK4J7G41Cfm4Ok2DjT3NmMtldrOPh4ZKKno1A XnXat2MedZpbnK0QDShI4sS/TfJI3rS7D1madqs9nMFXzcYiBgwqlzjcGAnVrH2vPcA3VxG6IaC Noi6hq+aIBGTFVTY/D416mWuUaD6KSJfSeseBn82A30iO5bYiqy5xHbWdfDvgZuehYhch74oo08 Fjq0T2AvafwjEMqlD/DeGds+mRQ4BPnE39f94q7h5mVsB1sJVy6ScW9ci5bo8WYmnvuqnDRYW2g hsHPSGyhfb59pHzxHkjmDmWjWlgEO218s3DM5rZZ7ewSzQ8PZE/ X-Received: by 2002:a05:6a20:4322:b0:3dd:a197:cf1d with SMTP id adf61e73a8af0-3ddec84e50emr85313637.65.1790056241253; Mon, 21 Sep 2026 22:50:41 -0700 (PDT) Received: from li-1a3e774c-28e4-11b2-a85c-acc9f2883e29.bl1-in.ibm.com ([129.41.58.4]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-33e613154bdsm2074809eec.24.2026.09.21.22.50.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Sep 2026 22:50:40 -0700 (PDT) From: "Mukesh Kumar Chaurasiya (IBM)" To: catalin.marinas@arm.com, will@kernel.org, mark.rutland@arm.com, maddy@linux.ibm.com, mpe@ellerman.id.au, npiggin@gmail.com, chleroy@kernel.org, ritesh.list@gmail.com, sshegde@linux.ibm.com, pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, hca@linux.ibm.com, gor@linux.ibm.com, agordeev@linux.ibm.com, borntraeger@linux.ibm.com, svens@linux.ibm.com, tglx@kernel.org, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, ojeda@kernel.org, boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, dakr@kernel.org, daniel.almeida@collabora.com, tamird@kernel.org, acourbot@nvidia.com, work@onurozkan.dev, nathan@kernel.org, ndesaulniers@google.com, morbo@google.com, justinstitt@google.com, mkchauras@gmail.com, jszhang@kernel.org, japo@linux.ibm.com, peterz@infradead.org, jpoimboe@kernel.org, seanjc@google.com, pmladek@suse.com, thuth@redhat.com, ynorov@nvidia.com, joelagnelf@nvidia.com, david@davidgow.net, fujita.tomonori@gmail.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Subject: [RFC] rust: kernel: Add KUnit tests for ARCH_WARN_ASM bug table emission Date: Tue, 22 Sep 2026 11:20:00 +0530 Message-ID: <20260922054959.955093-2-mkchauras@gmail.com> X-Mailer: git-send-email 2.55.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Verify that the __bug_table entry emitted by ARCH_WARN_ASM has a correct bug_addr displacement =E2=80=94 i.e. the arch's trap label reference resolv= es to the trap instruction =E2=80=94 by calling find_bug() with the exact virtual= address of the trap, mirroring what the real trap handler does. To support all architectures, each arch that implements ARCH_WARN_ASM now defines ARCH_WARN_ASM_TRAP_LABEL, a string constant naming the local label at which the trap instruction is placed: x86 "1" (ud2 at label 1:) powerpc "1" (twi at label 1:) riscv "1" (ebreak at label 1:) arm64 "14471" (brk placed at 14471: by __BUG_ENTRY_END) s390 "0" (mc at label 0:) The label is used consistently: in ARCH_WARN_ASM itself, in the bug_addr back-reference inside __BUG_ENTRY / _EMIT_BUG_ENTRY, and in the new generated_arch_warn_asm_trap_label.rs template, which the C preprocessor expands at build time into a .rs file included by the test's global_asm! block. global_asm! is used instead of asm! because LLVM eliminates asm! blocks in dead branches; global_asm! is file-scope and always emitted. BUG_KUNIT_TRAP_ADDR is defined as a .global symbol in .data on top of a .dc.a {trap_label}b relocation so the linker resolves the trap address into it at link time. A Rust static initialized to zero would land in BSS where relocations are not applied. The global_asm! string mirrors warn_flags!() exactly =E2=80=94 same "/* {si= ze} */" prefix, same generated_arch_warn_asm.rs and generated_arch_reachable_asm.rs includes, same operands =E2=80=94 plus the .pushsection/.dc.a/.popsection t= ail. This keeps the test structurally in sync with the macro it exercises. The CONFIG_RUST_BUG_WARN_ASM_KUNIT_TEST option depends on GENERIC_BUG and excludes ARM, LOONGARCH, and UML (which use a different warn_flags! path without ARCH_WARN_ASM). Five tests are included in the rust_kernel_bug_warn_asm suite: bug_entry_found - find_bug() returns non-NULL for the trap address, proving bug_addr is correct bug_entry_is_warning - the emitted entry has BUGFLAG_WARNING set bug_entry_file - bug_get_file_line() returns the correct source file (CONFIG_DEBUG_BUGVERBOSE only) bug_entry_line - recorded line number is non-zero, confirming the {line} operand was substituted correctly (CONFIG_DEBUG_BUGVERBOSE only) bug_entry_addr_is_in_text - kernel_text_address() confirms the trap address lies in kernel text Tested (pass:5 fail:0 skip:0) on: x86_64 - QEMU x86_64 powerpc - QEMU pseries ppc64le (pseries_le_defconfig) riscv - QEMU riscv64 arm64 - QEMU aarch64 s390 - QEMU s390x Signed-off-by: Mukesh Kumar Chaurasiya (IBM) --- Note: I sent this out as a powerpc specific tests earlier. During review process some people wanted these to be arch independent. Hence sending this out as an RFC. Earlier version: https://lore.kernel.org/all/20260915090453.1227034-3-mkcha= uras@gmail.com/ arch/arm64/include/asm/asm-bug.h | 3 + arch/powerpc/include/asm/bug.h | 22 +- arch/riscv/include/asm/bug.h | 5 +- arch/s390/include/asm/bug.h | 4 +- arch/x86/include/asm/bug.h | 5 +- rust/Makefile | 2 +- rust/kernel/.gitignore | 1 + rust/kernel/Kconfig.test | 15 ++ rust/kernel/bug.rs | 191 ++++++++++++++++++ .../generated_arch_warn_asm_trap_label.rs.S | 7 + 10 files changed, 239 insertions(+), 16 deletions(-) create mode 100644 rust/kernel/generated_arch_warn_asm_trap_label.rs.S diff --git a/arch/arm64/include/asm/asm-bug.h b/arch/arm64/include/asm/asm-= bug.h index a5f13801b784..e0694543115e 100644 --- a/arch/arm64/include/asm/asm-bug.h +++ b/arch/arm64/include/asm/asm-bug.h @@ -60,6 +60,9 @@ _BUGVERBOSE_LOCATION(__FILE__, __LINE__) \ ".short " flags ";" \ __stringify(__BUG_ENTRY_END) =20 +/* Trap instruction is placed at label 14471: by __BUG_ENTRY_END */ +#define ARCH_WARN_ASM_TRAP_LABEL "14471" + #define ARCH_WARN_ASM(file, line, flags, size) \ __BUG_ENTRY_STRING(file, line, flags) \ __stringify(brk BUG_BRK_IMM) diff --git a/arch/powerpc/include/asm/bug.h b/arch/powerpc/include/asm/bug.h index bf31ee1e902a..f0333c564d4f 100644 --- a/arch/powerpc/include/asm/bug.h +++ b/arch/powerpc/include/asm/bug.h @@ -32,6 +32,8 @@ #endif /* verbose */ =20 #else /* !__ASSEMBLER__ */ +#define ARCH_WARN_ASM_TRAP_LABEL "1" + #ifdef CONFIG_DEBUG_BUGVERBOSE #define _EMIT_BUG_ENTRY(bug_entry, trap, file, line, flags) \ ".section __bug_table,\"aw\"\n" \ @@ -45,21 +47,21 @@ " .short " flags "\n" #endif =20 -#define BUG_ENTRY(cond_str, insn, flags, ...) \ - __asm__ __volatile__( \ - "1: " insn "\n" \ - _EMIT_BUG_ENTRY(2, 1b, "%0", "%1", "%2") \ - ".org 2b+%3\n" \ - ".previous\n" \ +#define BUG_ENTRY(cond_str, insn, flags, ...) \ + __asm__ __volatile__( \ + ARCH_WARN_ASM_TRAP_LABEL ": " insn "\n" \ + _EMIT_BUG_ENTRY(2, 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__) =20 -#define ARCH_WARN_ASM(file, line, flags, size) \ - "1: twi 31, 0, 0\n" \ - _EMIT_BUG_ENTRY(2, 1b, file, line, flags) \ - ".org 2b+" size "\n" \ +#define ARCH_WARN_ASM(file, line, flags, size) \ + ARCH_WARN_ASM_TRAP_LABEL ": twi 31, 0, 0\n" \ + _EMIT_BUG_ENTRY(2, 1b, file, line, flags) \ + ".org 2b+" size "\n" \ ".previous\n" =20 #define ARCH_WARN_REACHABLE diff --git a/arch/riscv/include/asm/bug.h b/arch/riscv/include/asm/bug.h index 699c0cf3e4ef..def6b0d153f2 100644 --- a/arch/riscv/include/asm/bug.h +++ b/arch/riscv/include/asm/bug.h @@ -29,7 +29,8 @@ =20 typedef u32 bug_insn_t; =20 -#define __BUG_ENTRY_ADDR RISCV_INT " 1b - ." +#define ARCH_WARN_ASM_TRAP_LABEL "1" +#define __BUG_ENTRY_ADDR RISCV_INT " " ARCH_WARN_ASM_TRAP_LABEL "b - ." #define __BUG_ENTRY_FILE(file) RISCV_INT " " file " - ." =20 #ifdef CONFIG_DEBUG_BUGVERBOSE @@ -47,7 +48,7 @@ typedef u32 bug_insn_t; #ifdef CONFIG_GENERIC_BUG =20 #define ARCH_WARN_ASM(file, line, flags, size) \ - "1:\n\t" \ + ARCH_WARN_ASM_TRAP_LABEL ":\n\t" \ "ebreak\n" \ ".pushsection __bug_table,\"aw\"\n\t" \ "2:\n\t" \ diff --git a/arch/s390/include/asm/bug.h b/arch/s390/include/asm/bug.h index 2010342c97b1..3c0fa1acc92a 100644 --- a/arch/s390/include/asm/bug.h +++ b/arch/s390/include/asm/bug.h @@ -26,9 +26,11 @@ #define WARN_CONDITION_STR(cond_str) "" #endif =20 +#define ARCH_WARN_ASM_TRAP_LABEL "0" + #define __BUG_ENTRY(format, file, line, flags, size) \ " .section __bug_table,\"aw\"\n" \ - "1: .long 0b - . # bug_entry::bug_addr\n"\ + "1: .long " ARCH_WARN_ASM_TRAP_LABEL "b - . # bug_entry::bug_addr\n" \ " .long " format " - . # bug_entry::format\n" \ __BUG_ENTRY_VERBOSE(file, line) \ " .short "flags" # bug_entry::flags\n" \ diff --git a/arch/x86/include/asm/bug.h b/arch/x86/include/asm/bug.h index 23ab05438269..6cca8880660e 100644 --- a/arch/x86/include/asm/bug.h +++ b/arch/x86/include/asm/bug.h @@ -62,8 +62,9 @@ extern void __WARN_trap(struct bug_entry *bug, ...); #define HAVE_ARCH_BUG_FORMAT_ARGS #endif =20 +#define ARCH_WARN_ASM_TRAP_LABEL "1" #define __BUG_ENTRY(format, file, line, flags) \ - "\t.long 1b - ." "\t# bug_entry::bug_addr\n" \ + "\t.long " ARCH_WARN_ASM_TRAP_LABEL "b - ." "\t# bug_entry::bug_addr\n" \ __BUG_ENTRY_FORMAT(format) \ __BUG_ENTRY_VERBOSE(file, line) \ "\t.word " flags "\t# bug_entry::flags\n" @@ -101,7 +102,7 @@ do { \ "99:\n" \ "\t.string \"\"\n" \ ".popsection\n" \ - "1:\t " ASM_UD2 "\n" \ + ARCH_WARN_ASM_TRAP_LABEL ":\t " ASM_UD2 "\n" \ _BUG_FLAGS_ASM("99b", file, line, flags, size, "") =20 #else diff --git a/rust/Makefile b/rust/Makefile index da1a7409d984..8f253d516495 100644 --- a/rust/Makefile +++ b/rust/Makefile @@ -46,7 +46,7 @@ obj-$(CONFIG_RUST_KERNEL_DOCTESTS) +=3D doctests_kernel_g= enerated_kunit.o =20 always-$(subst y,$(CONFIG_RUST),$(CONFIG_JUMP_LABEL)) +=3D kernel/generate= d_arch_static_branch_asm.rs ifndef CONFIG_UML -always-$(subst y,$(CONFIG_RUST),$(CONFIG_BUG)) +=3D kernel/generated_arch_= warn_asm.rs kernel/generated_arch_reachable_asm.rs +always-$(subst y,$(CONFIG_RUST),$(CONFIG_BUG)) +=3D kernel/generated_arch_= warn_asm.rs kernel/generated_arch_reachable_asm.rs kernel/generated_arch_wa= rn_asm_trap_label.rs endif =20 # Avoids running `$(RUSTC)` when it may not be available. diff --git a/rust/kernel/.gitignore b/rust/kernel/.gitignore index f636ad95aaf3..05bb00bbb83c 100644 --- a/rust/kernel/.gitignore +++ b/rust/kernel/.gitignore @@ -3,3 +3,4 @@ /generated_arch_static_branch_asm.rs /generated_arch_warn_asm.rs /generated_arch_reachable_asm.rs +/generated_arch_warn_asm_trap_label.rs diff --git a/rust/kernel/Kconfig.test b/rust/kernel/Kconfig.test index e6a5c7a795f0..8cd7c8f91b07 100644 --- a/rust/kernel/Kconfig.test +++ b/rust/kernel/Kconfig.test @@ -83,4 +83,19 @@ config RUST_BITFIELD_KUNIT_TEST =20 If unsure, say N. =20 +config RUST_BUG_WARN_ASM_KUNIT_TEST + bool "KUnit tests for ARCH_WARN_ASM bug table emission" if !KUNIT_ALL_TES= TS + depends on GENERIC_BUG && !ARM && !LOONGARCH && !UML + default KUNIT_ALL_TESTS + help + This option enables KUnit tests that verify ARCH_WARN_ASM emits a + correct __bug_table entry: the bug_addr displacement must resolve + back to the trap instruction so that find_bug() can locate the + entry =E2=80=94 exactly as the real trap handler does. + + Supported on all architectures that implement ARCH_WARN_ASM + (x86, powerpc, riscv, arm64, s390). + + If unsure, say N. + endif diff --git a/rust/kernel/bug.rs b/rust/kernel/bug.rs index 3566f0234ca4..9e9b6d3a68b7 100644 --- a/rust/kernel/bug.rs +++ b/rust/kernel/bug.rs @@ -152,3 +152,194 @@ 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 =E2=80=94 LLVM cannot elim= inate 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 {trap_label}b 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_WARN_ASM_KUNIT_TEST)] +mod test_statics { + use crate::bindings::{bug_entry, BUGFLAG_WARNING, TAINT_WARN}; + + pub(super) const FLAGS: u32 =3D BUGFLAG_WARNING | (TAINT_WARN << 8); + pub(super) const SIZE: usize =3D core::mem::size_of::(); + + // LINE and BUG_KUNIT_FILE are only referenced by the {file}/{line} + // operands in global_asm!, which are only present when + // CONFIG_DEBUG_BUGVERBOSE is set (the non-verbose _EMIT_BUG_ENTRY + // drops the file/line fields from the bug table entry entirely). + #[cfg(CONFIG_DEBUG_BUGVERBOSE)] + pub(super) const LINE: u32 =3D line!(); + + // Null-terminated source file name =E2=80=94 the assembler references= this symbol + // for the verbose file pointer in __bug_table, same as warn_flags!. + #[cfg(CONFIG_DEBUG_BUGVERBOSE)] + const _FILE: &[u8] =3D file!().as_bytes(); + #[cfg(CONFIG_DEBUG_BUGVERBOSE)] + #[no_mangle] + pub(super) static BUG_KUNIT_FILE: [u8; _FILE.len() + 1] =3D { + let mut bytes =3D [0u8; _FILE.len() + 1]; + let mut i =3D 0; + while i < _FILE.len() { + bytes[i] =3D _FILE[i]; + i +=3D 1; + } + bytes + }; +} + +// Emit ARCH_WARN_ASM at file scope and capture the trap address. +// +// The asm string mirrors warn_flags!() exactly =E2=80=94 same "/* {size} = */" prefix, +// same generated_arch_warn_asm.rs and generated_arch_reachable_asm.rs inc= ludes, +// same operands =E2=80=94 so the two stay in sync as the macro evolves. = The only +// addition is the .pushsection/.dc.a/.popsection tail that records the tr= ap +// address into BUG_KUNIT_TRAP_ADDR at link time. +// +// BUG_KUNIT_TRAP_ADDR is a .global symbol in .data whose value is set by a +// .dc.a relocation to the arch's trap label (from +// generated_arch_warn_asm_trap_label.rs); .dc.a self-aligns to pointer wi= dth +// so no .balign is needed. +#[cfg(all(CONFIG_RUST_BUG_WARN_ASM_KUNIT_TEST, CONFIG_DEBUG_BUGVERBOSE))] +::core::arch::global_asm!( + concat!( + "/* {size} */", + include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_war= n_asm.rs")), + include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_rea= chable_asm.rs")), + "\n\t.pushsection .data\n\t", + ".global BUG_KUNIT_TRAP_ADDR\n\t", + "BUG_KUNIT_TRAP_ADDR:\n\t", + ".dc.a ", + include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_war= n_asm_trap_label.rs")), + "b\n\t", + ".popsection\n", + ), + file =3D sym test_statics::BUG_KUNIT_FILE, + line =3D const test_statics::LINE, + flags =3D const test_statics::FLAGS, + size =3D const test_statics::SIZE, +); + +#[cfg(all(CONFIG_RUST_BUG_WARN_ASM_KUNIT_TEST, not(CONFIG_DEBUG_BUGVERBOSE= )))] +::core::arch::global_asm!( + concat!( + "/* {size} */", + include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_war= n_asm.rs")), + include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_rea= chable_asm.rs")), + "\n\t.pushsection .data\n\t", + ".global BUG_KUNIT_TRAP_ADDR\n\t", + "BUG_KUNIT_TRAP_ADDR:\n\t", + ".dc.a ", + include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_war= n_asm_trap_label.rs")), + "b\n\t", + ".popsection\n", + ), + flags =3D const test_statics::FLAGS, + size =3D const test_statics::SIZE, +); + +#[cfg(CONFIG_RUST_BUG_WARN_ASM_KUNIT_TEST)] +#[::kernel::macros::kunit_tests(rust_kernel_bug_warn_asm)] +mod tests { + use crate::bindings; + + fn trap_addr() -> usize { + // BUG_KUNIT_TRAP_ADDR is a .global symbol defined in the global_a= sm! + // block above, placed in .data at the exact .dc.a 1b relocation w= ord. + // The linker resolves it to the virtual address of the twi instru= ction + // 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 pp= c64. + // usize matches the native pointer width on both. + static BUG_KUNIT_TRAP_ADDR: usize; + } + // SAFETY: read-only after link time, no concurrent mutation possi= ble. + unsafe { BUG_KUNIT_TRAP_ADDR } + } + + /// The `__bug_table` entry emitted by `ARCH_WARN_ASM` must be locatab= le + /// via `find_bug()` using the trap instruction's address. A NULL res= ult + /// means the `1b` label reference in `_EMIT_BUG_ENTRY` resolved to the + /// wrong address and the real trap handler would not recognise the si= te. + #[test] + fn bug_entry_found() { + // Non-zero proves the .dc.a relocation was resolved by the linker. + assert!(trap_addr() !=3D 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 =3D 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() !=3D 0); + let entry =3D unsafe { bindings::find_bug(trap_addr()) }; + assert!(!entry.is_null()); + // SAFETY: entry is non-null and points to a valid bug_entry. + let flags =3D unsafe { (*entry).flags } as u32; + assert!(flags & bindings::BUGFLAG_WARNING !=3D 0); + } + + /// With `CONFIG_DEBUG_BUGVERBOSE` the entry must record a non-null fi= le + /// pointer pointing back into this source file. + #[test] + #[cfg(CONFIG_DEBUG_BUGVERBOSE)] + fn bug_entry_file() { + use kernel::str::CStrExt; + + assert!(trap_addr() !=3D 0); + let entry =3D unsafe { bindings::find_bug(trap_addr()) }; + assert!(!entry.is_null()); + + let mut file_ptr: *const kernel::ffi::c_char =3D core::ptr::null(); + let mut line: u32 =3D 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 li= ne) }; + + assert!(!file_ptr.is_null()); + // SAFETY: file_ptr is a non-null, null-terminated C string from B= UG_KUNIT_FILE, + // valid for the lifetime of this function. + let file_str =3D unsafe { kernel::prelude::CStr::from_char_ptr(fil= e_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 substitut= ed). + #[test] + #[cfg(CONFIG_DEBUG_BUGVERBOSE)] + fn bug_entry_line() { + assert!(trap_addr() !=3D 0); + let entry =3D unsafe { bindings::find_bug(trap_addr()) }; + assert!(!entry.is_null()); + + let mut file_ptr: *const kernel::ffi::c_char =3D core::ptr::null(); + let mut line: u32 =3D 0; + // SAFETY: entry is non-null and valid. + unsafe { bindings::bug_get_file_line(entry, &mut file_ptr, &mut li= ne) }; + + assert!(line !=3D 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() !=3D 0); + // SAFETY: kernel_text_address() is always safe to call with any a= ddr. + let in_text =3D unsafe { bindings::kernel_text_address(trap_addr()= ) }; + assert!(in_text !=3D 0); + } +} diff --git a/rust/kernel/generated_arch_warn_asm_trap_label.rs.S b/rust/ker= nel/generated_arch_warn_asm_trap_label.rs.S new file mode 100644 index 000000000000..d7bed8751f10 --- /dev/null +++ b/rust/kernel/generated_arch_warn_asm_trap_label.rs.S @@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include + +// Cut here. + +::kernel::concat_literals!(ARCH_WARN_ASM_TRAP_LABEL) --=20 2.55.0