From nobody Wed Dec 17 19:00:28 2025 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 236A71FFC60 for ; Fri, 14 Mar 2025 17:32:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741973563; cv=none; b=NZpCGNbllHkR/fX2Q+b9EPbYNsAi88NNypNuOckEWe14ilS1YOVsQUeHpRnsmmZxwijoGPnZVc2q+xmNma9YKA0vLuvqDPI/NSwZvKYcBTTA48FdlqXm/veIO58weZOXEZhDqTZR3FiJoF9SIegjLHu1cCKH0IM3yd5TZLy/olU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741973563; c=relaxed/simple; bh=KcZmg1zIUwYl+d2uu76ow2WhOsy6u7gEqnTVmBZKyz8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HyXmhopg8bvGcvY4xRAR6nd6PZGhdsVkEzNIoS8usgQ+wMaXjMoj5CxDLJ51NvUm5qE4lFE574fA9/NyRhJym7Gyxp+epZITzUjoLiJii4Aqml4N9PfFDjwYUWOvFqz0zJrHIydOfpwZkLfHAJZC7T7y6njZ8x76DxjYRr3ZSsc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=desiato.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=CaR08mpK; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=desiato.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="CaR08mpK" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=I5Awq6moaztxY22uQpLVPuBkTzBbIxEtYFjSbRAN49Y=; b=CaR08mpK+sw0slWVjMplYOL9ou 8OaStTEcVYPeMmXoOSQ/y4t41ZXejoGL40x32rlJ0ltjUw56aQmTN/JaSd+NloQNKQ5Yu6JYSfMPv tn3exLJQlbxB+sJrb8hLhaqkGute3MV3eG2vrQ2HCCJANlAQ4WS2/pj5ehW0b2MXTFRE0Khjaisyp 4hYUfM0+cjyIkVU4ouKmHo/FXmq3sQAhMUQjepR4nrTWKm5M4kGs/cxOBmE5vdQHGKchsfUBLm4md QraBeazfY3zr9/M+msg5KTr7knguUpW/LR3z+KA9oKD+Lxbmk97QE8kmdv5Y8zOB4mCNLMucnn4o4 Oono+19A==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by desiato.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tt8tb-00000002vUC-3fT3; Fri, 14 Mar 2025 17:32:32 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tt8tY-0000000CsVj-3Mn6; Fri, 14 Mar 2025 17:32:28 +0000 From: David Woodhouse To: kexec@lists.infradead.org Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , David Woodhouse , "Kirill A . Shutemov" , Kai Huang , Nikolay Borisov , linux-kernel@vger.kernel.org, Simon Horman , Dave Young , Peter Zijlstra , jpoimboe@kernel.org, bsz@amazon.de Subject: [PATCH v8 1/7] x86/kexec: Debugging support: Load an IDT and basic exception entry points Date: Fri, 14 Mar 2025 17:27:33 +0000 Message-ID: <20250314173226.3062535-2-dwmw2@infradead.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250314173226.3062535-1-dwmw2@infradead.org> References: <20250314173226.3062535-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html Content-Type: text/plain; charset="utf-8" From: David Woodhouse Signed-off-by: David Woodhouse --- arch/x86/include/asm/kexec.h | 5 ++ arch/x86/kernel/machine_kexec_64.c | 21 ++++++++ arch/x86/kernel/relocate_kernel_64.S | 77 ++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) diff --git a/arch/x86/include/asm/kexec.h b/arch/x86/include/asm/kexec.h index 8ad187462b68..ec7636f4f86a 100644 --- a/arch/x86/include/asm/kexec.h +++ b/arch/x86/include/asm/kexec.h @@ -8,6 +8,9 @@ # define PA_PGD 2 # define PA_SWAP_PAGE 3 # define PAGES_NR 4 +#else +/* Size of each exception handler referenced by the IDT */ +# define KEXEC_DEBUG_EXC_HANDLER_SIZE 6 /* pushi, pushi, 2-byte jmp */ #endif =20 # define KEXEC_CONTROL_PAGE_SIZE 4096 @@ -58,6 +61,8 @@ struct kimage; extern unsigned long kexec_va_control_page; extern unsigned long kexec_pa_table_page; extern unsigned long kexec_pa_swap_page; +extern gate_desc kexec_debug_idt[]; +extern unsigned char kexec_debug_exc_vectors[]; #endif =20 /* diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_k= exec_64.c index a68f5a0a9f37..faf2fb1cd9f6 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -304,6 +304,24 @@ static void load_segments(void) ); } =20 +static void prepare_debug_idt(unsigned long control_page, unsigned long ve= c_ofs) +{ + gate_desc idtentry =3D { 0 }; + int i; + + idtentry.bits.p =3D 1; + idtentry.bits.type =3D GATE_TRAP; + idtentry.segment =3D __KERNEL_CS; + idtentry.offset_low =3D (control_page & 0xFFFF) + vec_ofs; + idtentry.offset_middle =3D (control_page >> 16) & 0xFFFF; + idtentry.offset_high =3D control_page >> 32; + + for (i =3D 0; i < 16; i++) { + kexec_debug_idt[i] =3D idtentry; + idtentry.offset_low +=3D KEXEC_DEBUG_EXC_HANDLER_SIZE; + } +} + int machine_kexec_prepare(struct kimage *image) { void *control_page =3D page_address(image->control_code_page); @@ -321,6 +339,9 @@ int machine_kexec_prepare(struct kimage *image) if (image->type =3D=3D KEXEC_TYPE_DEFAULT) kexec_pa_swap_page =3D page_to_pfn(image->swap_page) << PAGE_SHIFT; =20 + prepare_debug_idt((unsigned long)__pa(control_page), + (unsigned long)kexec_debug_exc_vectors - reloc_start); + __memcpy(control_page, __relocate_kernel_start, reloc_end - reloc_start); =20 set_memory_rox((unsigned long)control_page, 1); diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index ac058971a382..b07c55019ce5 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -50,6 +50,11 @@ SYM_DATA_START_LOCAL(kexec_debug_gdt) .quad 0x00cf92000000ffff /* __KERNEL_DS */ SYM_DATA_END_LABEL(kexec_debug_gdt, SYM_L_LOCAL, kexec_debug_gdt_end) =20 + .balign 8 +SYM_DATA_START(kexec_debug_idt) + .skip 0x100, 0x00 +SYM_DATA_END(kexec_debug_idt) + .section .text..relocate_kernel,"ax"; .code64 SYM_CODE_START_NOALIGN(relocate_kernel) @@ -139,6 +144,15 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) movq %ds, %rax movq %rax, %ds =20 + /* Now an IDTR on the stack to load the IDT the kernel created */ + leaq kexec_debug_idt(%rip), %rsi + pushq %rsi + pushw $0xff + lidt (%rsp) + addq $10, %rsp + + //int3 + /* * Clear X86_CR4_CET (if it was set) such that we can clear CR0_WP * below. @@ -364,3 +378,66 @@ SYM_CODE_START_LOCAL_NOALIGN(swap_pages) ret int3 SYM_CODE_END(swap_pages) + +SYM_CODE_START_NOALIGN(kexec_debug_exc_vectors) + /* Each of these is 6 bytes. */ +.macro vec_err exc + UNWIND_HINT_ENTRY + . =3D kexec_debug_exc_vectors + (\exc * KEXEC_DEBUG_EXC_HANDLER_SIZE) + nop + nop + pushq $\exc + jmp exc_handler +.endm + +.macro vec_noerr exc + UNWIND_HINT_ENTRY + . =3D kexec_debug_exc_vectors + (\exc * KEXEC_DEBUG_EXC_HANDLER_SIZE) + pushq $0 + pushq $\exc + jmp exc_handler +.endm + + ANNOTATE_NOENDBR + vec_noerr 0 // #DE + vec_noerr 1 // #DB + vec_noerr 2 // #NMI + vec_noerr 3 // #BP + vec_noerr 4 // #OF + vec_noerr 5 // #BR + vec_noerr 6 // #UD + vec_noerr 7 // #NM + vec_err 8 // #DF + vec_noerr 9 + vec_err 10 // #TS + vec_err 11 // #NP + vec_err 12 // #SS + vec_err 13 // #GP + vec_err 14 // #PF + vec_noerr 15 +SYM_CODE_END(kexec_debug_exc_vectors) + +SYM_CODE_START_LOCAL_NOALIGN(exc_handler) + /* No need for ret mitigations during kexec */ + VALIDATE_UNRET_END + + pushq %rax + pushq %rdx + movw $0x3f8, %dx + movb $'A', %al + outb %al, %dx + popq %rdx + popq %rax + + /* Only return from int3 */ + cmpq $3, (%rsp) + jne .Ldie + + addq $16, %rsp + iretq + +.Ldie: + hlt + jmp .Ldie + +SYM_CODE_END(exc_handler) --=20 2.48.1 From nobody Wed Dec 17 19:00:28 2025 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2363A1FDE35 for ; Fri, 14 Mar 2025 17:32:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741973564; cv=none; b=lVafhpUlWDjbKgG8aM/TJ0atdG7qd/5+L0mqrLYgVQINAbc+sZIz3kYDq8zqPF1zcB/HL68VVgIkLtgp+RMVjwatELtKOw5MwHd6ZvAFr0zMsSFoQcUogyt3/0VF2gaUT9hyZwgXdmX7DXVVJlNKyI8xMo6SUqnQTKBn/H95cLM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741973564; c=relaxed/simple; bh=3KV2A40aE8hziNQ20v2xlelA3ksEf/i1QIUUg5cXLdw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iBgzddzjfymNq2p9UoQ/jFVlGxaIgbkUPB0SO3oWNHjHy9PFGES94Wog778qNrB1AOzBRATHWYivAOLBqtuPb84mL/YIsUDSd8LkGhQPiXcmznblg+4ycpaBGnlIp2uQPC+enY3TjHs5P6wlBHFIxBXM+uzrLMSwjYfafWPEkSk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=desiato.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=qwvTc2uY; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=desiato.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="qwvTc2uY" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=XiU2A8Hf++dNsHgUZJbi/FS+hirzyzKI9tlEDEk267E=; b=qwvTc2uY27wXLSW8SnjfbZSPNf XAx+8qyapF9t8uM9JwqFtY4otTLYNWpp74MkZJFNsDshKBXbD4iPWBhriarGMwWdct1OTlUfFtuPy E0frzIt+40jJPGf8Ogc2tLscxKdFROaavwx5bOS5Qte33Z7bAkSzkEnJHXB/J+fWlFaaMlWhuGbOl 0bGReU5nf9pB/mgIKtpfgHAxfpZiBz/ji2hY9F8nRlcq6R3jazyZMKHOWVt41vJudQEU3Iu5jxRgm JtZCcXiUb+K/kvxk2oH2d1Zq0dkrctHQnr9KmwV7QTD1Mix3WDWvZ6WXsZ1vV0mTvl8gsaCxQ+4xT 0vht+9Uw==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by desiato.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tt8tb-00000002vUB-3fgc; Fri, 14 Mar 2025 17:32:32 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tt8tY-0000000CsVp-3WmP; Fri, 14 Mar 2025 17:32:28 +0000 From: David Woodhouse To: kexec@lists.infradead.org Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , David Woodhouse , "Kirill A . Shutemov" , Kai Huang , Nikolay Borisov , linux-kernel@vger.kernel.org, Simon Horman , Dave Young , Peter Zijlstra , jpoimboe@kernel.org, bsz@amazon.de Subject: [PATCH v8 2/7] x86/kexec: Debugging support: Dump registers on exception Date: Fri, 14 Mar 2025 17:27:34 +0000 Message-ID: <20250314173226.3062535-3-dwmw2@infradead.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250314173226.3062535-1-dwmw2@infradead.org> References: <20250314173226.3062535-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html Content-Type: text/plain; charset="utf-8" From: David Woodhouse The actual serial output function is a no-op for now. Signed-off-by: David Woodhouse --- arch/x86/kernel/relocate_kernel_64.S | 121 +++++++++++++++++++++++++-- 1 file changed, 115 insertions(+), 6 deletions(-) diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index b07c55019ce5..c2f73998d1b6 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -379,6 +379,69 @@ SYM_CODE_START_LOCAL_NOALIGN(swap_pages) int3 SYM_CODE_END(swap_pages) =20 +/* + * Generic 'print character' routine (as yet unimplemented) + * - %al: Character to be printed (may clobber %rax) + * - %rdx: MMIO address or port. + */ +SYM_CODE_START_LOCAL_NOALIGN(pr_char) + UNWIND_HINT_FUNC + ANNOTATE_NOENDBR + ANNOTATE_UNRET_SAFE + ret +SYM_CODE_END(pr_char) + +/* + * Load pr_char function pointer into %rsi and load %rdx with whatever + * that function wants to see there (typically port/MMIO address). + */ +.macro pr_setup + /* No output; pr_char just returns */ + leaq pr_char(%rip), %rsi +.endm + +/* Print the nybble in %bl, clobber %rax */ +SYM_CODE_START_LOCAL_NOALIGN(pr_nybble) + UNWIND_HINT_FUNC + movb %bl, %al + nop + andb $0x0f, %al + addb $0x30, %al + cmpb $0x3a, %al + jb 1f + addb $('a' - '0' - 10), %al + ANNOTATE_RETPOLINE_SAFE +1: jmp *%rsi +SYM_CODE_END(pr_nybble) + +SYM_CODE_START_LOCAL_NOALIGN(pr_qword) + UNWIND_HINT_FUNC + movq $16, %rcx +1: rolq $4, %rbx + call pr_nybble + loop 1b + movb $'\n', %al + ANNOTATE_RETPOLINE_SAFE + jmp *%rsi +SYM_CODE_END(pr_qword) + +.macro print_reg a, b, c, d, r + movb $\a, %al + ANNOTATE_RETPOLINE_SAFE + call *%rsi + movb $\b, %al + ANNOTATE_RETPOLINE_SAFE + call *%rsi + movb $\c, %al + ANNOTATE_RETPOLINE_SAFE + call *%rsi + movb $\d, %al + ANNOTATE_RETPOLINE_SAFE + call *%rsi + movq \r, %rbx + call pr_qword +.endm + SYM_CODE_START_NOALIGN(kexec_debug_exc_vectors) /* Each of these is 6 bytes. */ .macro vec_err exc @@ -422,17 +485,63 @@ SYM_CODE_START_LOCAL_NOALIGN(exc_handler) VALIDATE_UNRET_END =20 pushq %rax + pushq %rbx + pushq %rcx pushq %rdx - movw $0x3f8, %dx - movb $'A', %al - outb %al, %dx - popq %rdx - popq %rax + pushq %rsi + + /* Stack frame */ +#define EXC_SS 0x58 /* Architectural... */ +#define EXC_RSP 0x50 +#define EXC_EFLAGS 0x48 +#define EXC_CS 0x40 +#define EXC_RIP 0x38 +#define EXC_ERRORCODE 0x30 /* Either architectural or zero pushed by handl= er */ +#define EXC_EXCEPTION 0x28 /* Pushed by handler entry point */ +#define EXC_RAX 0x20 /* Pushed just above in exc_handler */ +#define EXC_RBX 0x18 +#define EXC_RCX 0x10 +#define EXC_RDX 0x08 +#define EXC_RSI 0x00 + + /* Set up %rdx/%rsi for debug output */ + pr_setup + + /* rip and exception info */ + print_reg 'E', 'x', 'c', ':', EXC_EXCEPTION(%rsp) + print_reg 'E', 'r', 'r', ':', EXC_ERRORCODE(%rsp) + print_reg 'r', 'i', 'p', ':', EXC_RIP(%rsp) + print_reg 'r', 's', 'p', ':', EXC_RSP(%rsp) + + /* We spilled these to the stack */ + print_reg 'r', 'a', 'x', ':', EXC_RAX(%rsp) + print_reg 'r', 'b', 'x', ':', EXC_RBX(%rsp) + print_reg 'r', 'c', 'x', ':', EXC_RCX(%rsp) + print_reg 'r', 'd', 'x', ':', EXC_RDX(%rsp) + print_reg 'r', 's', 'i', ':', EXC_RSI(%rsp) + + /* Other registers untouched */ + print_reg 'r', 'd', 'i', ':', %rdi + print_reg 'r', '8', ' ', ':', %r8 + print_reg 'r', '9', ' ', ':', %r9 + print_reg 'r', '1', '0', ':', %r10 + print_reg 'r', '1', '1', ':', %r11 + print_reg 'r', '1', '2', ':', %r12 + print_reg 'r', '1', '3', ':', %r13 + print_reg 'r', '1', '4', ':', %r14 + print_reg 'r', '1', '5', ':', %r15 + print_reg 'c', 'r', '2', ':', %cr2 =20 /* Only return from int3 */ - cmpq $3, (%rsp) + cmpq $3, EXC_EXCEPTION(%rsp) jne .Ldie =20 + popq %rsi + popq %rdx + popq %rcx + popq %rbx + popq %rax + addq $16, %rsp iretq =20 --=20 2.48.1 From nobody Wed Dec 17 19:00:28 2025 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BDCBF204874 for ; Fri, 14 Mar 2025 17:36:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741973811; cv=none; b=r0YsiRhAwFTOJwNq/JfEKpYW2DDnv/xW6WqhCtS8Nh/w3+7Y2ioz2nhv7rEiHAxZWxJEQjWEwXdNPRYkYbBviYAw4qSDhy8eaUrjSdF1qOIGIJE2cEbNsTRQ+s0qsNT4vzOHmyikaIwQ65AMR2zf3lmEnS1qHnIl9Rr++BdqjoY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741973811; c=relaxed/simple; bh=lTGzeLVixRHII7hxGC67buyIQ540f9vcxOWP+E177FE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T3C2xwa1eHCUKHcz7tkfFIOTTJDvGtyf6QfXvQuPe1SNIg2+L+mrl+JL8T/J7xsZm+A11kqRLtYNxRoA4WebMVvzFSKfPbwEk0SX0kSm7nN+dflDcKESUbagfbxvbOKRfDjqF8EYCRoZzSIQwitFbm5pK+ZHlT3yjFf9cqN1GEI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=casper.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=SCemwOhK; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=casper.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="SCemwOhK" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=uCEBfgh97r1ZRaD9/vr5HcRSBvjPstuhIMv+FvxUqfo=; b=SCemwOhKOdG1TiIcHIp/uPMkD1 IfQc5tSzVMyRqG8K5tODkP0HZ/GP5yP3OxbAZCjb9p3MjCc1kMqtLo2nF6EkAPlJP1F1bHeGNkZf7 eWDeVly2kq5x/hgtcYWH7qeG3AcFQBCTKIcHKl34VMAQcjXDemAE5gZcnfDQ4elUlb9E4ShogY5i8 l1cbwi2qiovVCHeT6ul2LThuQ1uC0/FjR4VgTyF+iZOp25xMECZK/2X6glcSXVhfApfpIIg4vD24y 5u/zFhg1saAgL7FWiGhTOZ7/MHc45edRI59rFcv040vRP3diA4199qTYeKKULaCef7TSBn1c+tD8N 03DgWDWQ==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tt8ta-00000003tcq-2ro6; Fri, 14 Mar 2025 17:32:35 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tt8tY-0000000CsVv-3rUi; Fri, 14 Mar 2025 17:32:28 +0000 From: David Woodhouse To: kexec@lists.infradead.org Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , David Woodhouse , "Kirill A . Shutemov" , Kai Huang , Nikolay Borisov , linux-kernel@vger.kernel.org, Simon Horman , Dave Young , Peter Zijlstra , jpoimboe@kernel.org, bsz@amazon.de Subject: [PATCH v8 3/7] x86/kexec: Add 8250 serial port output Date: Fri, 14 Mar 2025 17:27:35 +0000 Message-ID: <20250314173226.3062535-4-dwmw2@infradead.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250314173226.3062535-1-dwmw2@infradead.org> References: <20250314173226.3062535-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Content-Type: text/plain; charset="utf-8" From: David Woodhouse If a serial port was configured for early_printk, use it for debug output from the relocate_kernel exception handler too. Signed-off-by: David Woodhouse --- arch/x86/include/asm/kexec.h | 1 + arch/x86/kernel/early_printk.c | 6 +++++ arch/x86/kernel/relocate_kernel_64.S | 39 +++++++++++++++++++++++----- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/kexec.h b/arch/x86/include/asm/kexec.h index ec7636f4f86a..8cbdb6fd10c2 100644 --- a/arch/x86/include/asm/kexec.h +++ b/arch/x86/include/asm/kexec.h @@ -63,6 +63,7 @@ extern unsigned long kexec_pa_table_page; extern unsigned long kexec_pa_swap_page; extern gate_desc kexec_debug_idt[]; extern unsigned char kexec_debug_exc_vectors[]; +extern uint16_t kexec_debug_8250_port; #endif =20 /* diff --git a/arch/x86/kernel/early_printk.c b/arch/x86/kernel/early_printk.c index 44f937015e1e..e98e064089cd 100644 --- a/arch/x86/kernel/early_printk.c +++ b/arch/x86/kernel/early_printk.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include #include +#include #include #include #include @@ -141,6 +142,11 @@ static __init void early_serial_hw_init(unsigned divis= or) serial_out(early_serial_base, DLL, divisor & 0xff); serial_out(early_serial_base, DLH, (divisor >> 8) & 0xff); serial_out(early_serial_base, LCR, c & ~DLAB); + +#if defined(CONFIG_KEXEC_CORE) && defined(CONFIG_X86_64) + if (serial_in =3D=3D io_serial_in) + kexec_debug_8250_port =3D early_serial_base; +#endif } =20 #define DEFAULT_BAUD 9600 diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index c2f73998d1b6..1f8c8890798c 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -39,6 +39,7 @@ SYM_DATA(kexec_va_control_page, .quad 0) SYM_DATA(kexec_pa_table_page, .quad 0) SYM_DATA(kexec_pa_swap_page, .quad 0) SYM_DATA_LOCAL(pa_backup_pages_map, .quad 0) +SYM_DATA(kexec_debug_8250_port, .word 0) =20 .balign 16 SYM_DATA_START_LOCAL(kexec_debug_gdt) @@ -380,24 +381,50 @@ SYM_CODE_START_LOCAL_NOALIGN(swap_pages) SYM_CODE_END(swap_pages) =20 /* - * Generic 'print character' routine (as yet unimplemented) + * Generic 'print character' routine * - %al: Character to be printed (may clobber %rax) * - %rdx: MMIO address or port. */ -SYM_CODE_START_LOCAL_NOALIGN(pr_char) +#define XMTRDY 0x20 + +#define TXR 0 /* Transmit register (WRITE) */ +#define LSR 5 /* Line Status */ + +SYM_CODE_START_LOCAL_NOALIGN(pr_char_8250) UNWIND_HINT_FUNC ANNOTATE_NOENDBR + addw $LSR, %dx + xchg %al, %ah +.Lxmtrdy_loop: + inb %dx, %al + testb $XMTRDY, %al + jnz .Lready + rep nop + jmp .Lxmtrdy_loop + +.Lready: + subw $LSR, %dx + xchg %al, %ah + outb %al, %dx +pr_char_null: + ANNOTATE_NOENDBR + ANNOTATE_UNRET_SAFE ret -SYM_CODE_END(pr_char) +SYM_CODE_END(pr_char_8250) =20 /* * Load pr_char function pointer into %rsi and load %rdx with whatever * that function wants to see there (typically port/MMIO address). */ -.macro pr_setup - /* No output; pr_char just returns */ - leaq pr_char(%rip), %rsi +.macro pr_setup + leaq pr_char_8250(%rip), %rsi + movw kexec_debug_8250_port(%rip), %dx + testw %dx, %dx + jnz 1f + + leaq pr_char_null(%rip), %rsi +1: .endm =20 /* Print the nybble in %bl, clobber %rax */ --=20 2.48.1 From nobody Wed Dec 17 19:00:28 2025 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7262D13B58A for ; Fri, 14 Mar 2025 17:32:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741973565; cv=none; b=u9aoyNjMnv3FzMQBqriZg9HM15OyWBlym9PpuHhSX+YwnIr8V+AQGoGFNmcmsvfXkgx7aHDhya2kWZ4oUVdkXs5wgPEhUuM30fzefyriJrcnPoYgYB/TJDXIzpc0CUlkiIflqHUWaWf0OH+Xs+8nbv0vrSOFitnznb5PuYMpQ68= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741973565; c=relaxed/simple; bh=LgrQqVRbDq2Xk2FY6PHUIYWjD4ZdkluKRjznLW3IR2I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=P3KGRePjywX0NH3Rmev6HrXhHzTA9duhhN5jK7Ggly2wi3qD0y9W9zFGF7ZlHcVq0CLs8UBuHAVdx+lgbnmTb25HRkkkaxxSFBg+M5opM1o0JVQHQIlrXkOf8yxtrbErT07DjCNuXYtAbNFjVBcGLsjt94c/OdDi4YsqkfNEZs8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=desiato.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=cHK7Unys; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=desiato.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="cHK7Unys" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=ehlcQleY3ftncpfhgztEIr+jQwi7JRqC2t6K+XfXKRM=; b=cHK7Unys7pXOpHDcJMbgSAOTiT 3QbVEB8KdccON7QRwMyrMyxj1DN/oKmfiAPmUKFomzdElETiOl9tjV07TaRb4UMxi4yjkaLzJwOWJ xFl6wcqBVSLrmi5ceaSNZ0a3KJTcclbWDnI/49p89hnz9m+wXM+91pP0lWWwvacUini2vwzoqocmt AmgwC2E7UMQCs9DRm1xBm6zfZQY1DFlBFWk/xHnGND0xy0MGFbdOW9eANDNYmWQQezEjAGo3kXqW+ 4U3IT2c5Nv2oUugYWYycqPOPUx5NBQ+oT8AGbN7dw6wPxFhh+COPkUcjnrxHqaOHZyIiZd/UZtOMO bd1KC6lA==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by desiato.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tt8tb-00000002vU8-3fiC; Fri, 14 Mar 2025 17:32:32 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tt8tY-0000000CsVz-4Bsv; Fri, 14 Mar 2025 17:32:29 +0000 From: David Woodhouse To: kexec@lists.infradead.org Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , David Woodhouse , "Kirill A . Shutemov" , Kai Huang , Nikolay Borisov , linux-kernel@vger.kernel.org, Simon Horman , Dave Young , Peter Zijlstra , jpoimboe@kernel.org, bsz@amazon.de Subject: [PATCH v8 4/7] x86/kexec: Add 8250 MMIO serial port output Date: Fri, 14 Mar 2025 17:27:36 +0000 Message-ID: <20250314173226.3062535-5-dwmw2@infradead.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250314173226.3062535-1-dwmw2@infradead.org> References: <20250314173226.3062535-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html Content-Type: text/plain; charset="utf-8" From: David Woodhouse This supports the same 32-bit MMIO-mapped 8250 as the early_printk code. It's not clear why the early_printk code supports this form and only this form; the actual runtime 8250_pci doesn't seem to support it. But having hacked up QEMU to expose such a device, early_printk does work with it, and now so does the kexec debug code. Signed-off-by: David Woodhouse --- arch/x86/include/asm/kexec.h | 1 + arch/x86/kernel/early_printk.c | 3 +++ arch/x86/kernel/machine_kexec_64.c | 17 +++++++++++++++++ arch/x86/kernel/relocate_kernel_64.S | 22 ++++++++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/arch/x86/include/asm/kexec.h b/arch/x86/include/asm/kexec.h index 8cbdb6fd10c2..5081d0b9e290 100644 --- a/arch/x86/include/asm/kexec.h +++ b/arch/x86/include/asm/kexec.h @@ -64,6 +64,7 @@ extern unsigned long kexec_pa_swap_page; extern gate_desc kexec_debug_idt[]; extern unsigned char kexec_debug_exc_vectors[]; extern uint16_t kexec_debug_8250_port; +extern unsigned long kexec_debug_8250_mmio32; #endif =20 /* diff --git a/arch/x86/kernel/early_printk.c b/arch/x86/kernel/early_printk.c index e98e064089cd..eb43c51a93a7 100644 --- a/arch/x86/kernel/early_printk.c +++ b/arch/x86/kernel/early_printk.c @@ -296,6 +296,9 @@ static __init void early_pci_serial_init(char *s) /* WARNING! assuming the address is always in the first 4G */ early_serial_base =3D (unsigned long)early_ioremap(bar0 & PCI_BASE_ADDRESS_MEM_MASK, 0x10); +#if defined(CONFIG_KEXEC_CORE) && defined(CONFIG_X86_64) + kexec_debug_8250_mmio32 =3D bar0 & PCI_BASE_ADDRESS_MEM_MASK; +#endif write_pci_config(bus, slot, func, PCI_COMMAND, cmdreg|PCI_COMMAND_MEMORY); } diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_k= exec_64.c index faf2fb1cd9f6..a2e00a814708 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -76,6 +76,19 @@ map_acpi_tables(struct x86_mapping_info *info, pgd_t *le= vel4p) static int map_acpi_tables(struct x86_mapping_info *info, pgd_t *level4p) = { return 0; } #endif =20 +static int map_mmio_serial(struct x86_mapping_info *info, pgd_t *level4p) +{ + unsigned long mstart, mend; + + if (!kexec_debug_8250_mmio32) + return 0; + + mstart =3D kexec_debug_8250_mmio32 & PAGE_MASK; + mend =3D (kexec_debug_8250_mmio32 + PAGE_SIZE + 23) & PAGE_MASK; + pr_info("Map PCI serial at %lx - %lx\n", mstart, mend); + return kernel_ident_mapping_init(info, level4p, mstart, mend); +} + #ifdef CONFIG_KEXEC_FILE const struct kexec_file_ops * const kexec_file_loaders[] =3D { &kexec_bzImage64_ops, @@ -285,6 +298,10 @@ static int init_pgtable(struct kimage *image, unsigned= long control_page) if (result) return result; =20 + result =3D map_mmio_serial(&info, image->arch.pgd); + if (result) + return result; + /* * This must be last because the intermediate page table pages it * allocates will not be control pages and may overlap the image. diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index 1f8c8890798c..cf736bbaec98 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -39,6 +39,7 @@ SYM_DATA(kexec_va_control_page, .quad 0) SYM_DATA(kexec_pa_table_page, .quad 0) SYM_DATA(kexec_pa_swap_page, .quad 0) SYM_DATA_LOCAL(pa_backup_pages_map, .quad 0) +SYM_DATA(kexec_debug_8250_mmio32, .quad 0) SYM_DATA(kexec_debug_8250_port, .word 0) =20 .balign 16 @@ -413,6 +414,22 @@ pr_char_null: ret SYM_CODE_END(pr_char_8250) =20 +SYM_CODE_START_LOCAL_NOALIGN(pr_char_8250_mmio32) + UNWIND_HINT_FUNC + ANNOTATE_NOENDBR +.Lxmtrdy_loop_mmio: + movb (LSR*4)(%rdx), %ah + testb $XMTRDY, %ah + jnz .Lready_mmio + rep nop + jmp .Lxmtrdy_loop_mmio + +.Lready_mmio: + movb %al, (%rdx) + ANNOTATE_UNRET_SAFE + ret +SYM_CODE_END(pr_char_8250_mmio32) + /* * Load pr_char function pointer into %rsi and load %rdx with whatever * that function wants to see there (typically port/MMIO address). @@ -423,6 +440,11 @@ SYM_CODE_END(pr_char_8250) testw %dx, %dx jnz 1f =20 + leaq pr_char_8250_mmio32(%rip), %rsi + movq kexec_debug_8250_mmio32(%rip), %rdx + testq %rdx, %rdx + jnz 1f + leaq pr_char_null(%rip), %rsi 1: .endm --=20 2.48.1 From nobody Wed Dec 17 19:00:28 2025 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 46ADC2046A5 for ; Fri, 14 Mar 2025 17:36:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741973768; cv=none; b=lH629s9fpgsrR3qWywVwY0Z151WvBk467puncRy1NYbGLVLCcZ3k4MZ46kGbStDl7NioFRrRQHaYKQUkpf2FreQdpBF6rEMidrqZXQbkHOVc1hAOTxpUUx9gM7V0Rl11/4PZTDqxlltJ8T7lRq0b/pWyF/t01ml+tWIQKlv/fnE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741973768; c=relaxed/simple; bh=eu1vahZq0TwHF+eFm6KndnNqayo/tNR4UGGuRmNnaZ0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=k2PjWliHllI9QJSk1w5LLRrf0Ao/PPSlE44G3E41fW7VuMp+1q035GndRnuuBBMDaXRE0za4Dqz4udsH2sS3V1Mz4ex+hvSWjhh+lv/pP4u7fXuS10H9jc5kp7m2qUESlk8yxDEMOYJxf+A2LU91LYAh/KBpsiZpYBd3YAsWPYo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=gUQO3Uty; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="gUQO3Uty" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=YA4cz3IqKYs8g/T34NdCWeOqKjRd9LcnyNkGC/9OpFU=; b=gUQO3UtyUMXfkoHY35Due7617c GQLNYIyJZ6cu5vT2UBXPDrWAmV0dG5RR+gk9dgwnutpLoljUUiHv1KXha68ASFKh0eQNsXvQPAkfH BV1pE5gV3kckJ9Zk1oa3GEJ+izc7bY1jHUqAgvXynaas1/9dxJfzLEaXDZmxG6n1s+tJZ3jiuEijP b8XpJxWBW4H0RvHS53dLW0+99NsJEiOpxTP69TJTY5pu24vCAipSQc6EjaxKhaVDe9kXF6NKwdnAT YfAJTMjs3RrHcwM8Er7/iWGVjdvRl9e5uXtsW8WVB/mYlzFTzEQPITJLsN0j4hmn/DiQozPaJDNrx YqcHAo9Q==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tt8ta-00000003tcs-2mto; Fri, 14 Mar 2025 17:32:35 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tt8tZ-0000000CsW8-0KEO; Fri, 14 Mar 2025 17:32:29 +0000 From: David Woodhouse To: kexec@lists.infradead.org Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , David Woodhouse , "Kirill A . Shutemov" , Kai Huang , Nikolay Borisov , linux-kernel@vger.kernel.org, Simon Horman , Dave Young , Peter Zijlstra , jpoimboe@kernel.org, bsz@amazon.de Subject: [PATCH v8 5/7] x86/kexec: Invalidate GDT/IDT from relocate_kernel() instead of earlier Date: Fri, 14 Mar 2025 17:27:37 +0000 Message-ID: <20250314173226.3062535-6-dwmw2@infradead.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250314173226.3062535-1-dwmw2@infradead.org> References: <20250314173226.3062535-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Content-Type: text/plain; charset="utf-8" From: David Woodhouse Commit 2114796ca041 ("x86/kexec: Mark machine_kexec() with __nocfi") marked the machine_kexec() function with __nocfi because I failed to make objtool happy when actually *providing* CFI information for the relocate_kernel() function that it calls, and __nocfi was the easy workaround. When that eventually gets fixed, the check on calling the relocate_kernel() function pointer may trap. Obviously that should never happen unless the function prototype gets messed up, but that's exactly what the CFI check is for. So it would be kind of helpful if the IDT was still in place and the trap could be *handled*. It can remain valid until the moment that %cr3 gets replaced, so shift the invalidation down into relocate_kernel() itself. This is a useful cleanup regardless of making CFI work, so do it now. Signed-off-by: David Woodhouse --- arch/x86/kernel/machine_kexec_64.c | 10 ++-------- arch/x86/kernel/relocate_kernel_64.S | 9 +++++++-- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_k= exec_64.c index a2e00a814708..7abc7aa0261b 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -434,16 +434,10 @@ void __nocfi machine_kexec(struct kimage *image) * with from a table in memory. At no other time is the * descriptor table in memory accessed. * - * I take advantage of this here by force loading the - * segments, before I zap the gdt with an invalid value. + * Take advantage of this here by force loading the segments, + * before the GDT is zapped with an invalid value. */ load_segments(); - /* - * The gdt & idt are now invalid. - * If you want to load them you must set up your own idt & gdt. - */ - native_idt_invalidate(); - native_gdt_invalidate(); =20 /* now call it */ image->start =3D relocate_kernel_ptr((unsigned long)image->head, diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index cf736bbaec98..4f8b7d318025 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -79,8 +79,13 @@ SYM_CODE_START_NOALIGN(relocate_kernel) pushq %r15 pushf =20 - /* zero out flags, and disable interrupts */ - pushq $0 + /* Invalidate GDT/IDT, zero out flags */ + pushq $0 + pushq $0 + + lidt (%rsp) + lgdt (%rsp) + addq $8, %rsp popfq =20 /* Switch to the identity mapped page tables */ --=20 2.48.1 From nobody Wed Dec 17 19:00:28 2025 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0B22920468F for ; Fri, 14 Mar 2025 17:36:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741973810; cv=none; b=ILl5aBS9qOqkiloLZY1OMeW5MHxSU/7JK+BGNU+r9cevkay4ExV9Grbu6WoWhiVclowq5INx9LshEicFh4HxPqX8QiWXrm0OGXHjC73+5PV+Mc7XkVV+mvnPiFeoTzsN19Z4So7y+ZgHJhtwkz/elrw4G6Iqbnlih9KHrs62DQw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741973810; c=relaxed/simple; bh=Dsh4VAnH63YeGPJh9gsY2uUxVF7+sifxwgt6NZTC81s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XBJLTQM05k3P8yr5yIi3d1sp+QbZ7/I9EMHRFseOt8Mq92GoREQgQmo3D3Lsw9t0tjtYFRgOAD5uZ2fI3vuZ2EP6uf7qnoEJmQhTLmbfq+UZ7r0jxP2+bv0w151APtu4dTovjuln/KhCcIbfSDp/06/awyqi7HXoWoMo51+F7vY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=casper.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=diV4G5/I; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=casper.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="diV4G5/I" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=cqwZ/H/FVUiSZsArJsLCTQnCYNemua1WIXyd2bx7EII=; b=diV4G5/IEhJa8Q+vOrcbuKfaWR IHIuwgnvh8u5e1wr06gVYGwT3/CARtlqM2HGhbWNscS9xs2uFnzpjjRzKAuVxAgTWzlXWIklsDQWP lmcLnZGJnqRimalPkLl2g/bzS1hWe5WMvsSXfdTP16JX1g6OlJPPtvcBLtZsxwtsBaNDKyDxNTILL P3yUge1q1Jw+i1p+gr+4/oWNUz++hOPqhEnRUYciGwKsmoO3f/JkJMbM7e5OqIuEwLORT+xe6NrEx vY2wKo9KVH5SR5SPBK2kxwyQAqHgWRKYCTasm+RJzXLhTOJI1siUs7CnDEmfXkVSti3kMz9Jk+yp5 cNjzFTig==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tt8ta-00000003tcr-2nQ2; Fri, 14 Mar 2025 17:32:35 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tt8tZ-0000000CsWE-0VrL; Fri, 14 Mar 2025 17:32:29 +0000 From: David Woodhouse To: kexec@lists.infradead.org Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , David Woodhouse , "Kirill A . Shutemov" , Kai Huang , Nikolay Borisov , linux-kernel@vger.kernel.org, Simon Horman , Dave Young , Peter Zijlstra , jpoimboe@kernel.org, bsz@amazon.de Subject: [PATCH v8 6/7] [DO NOT MERGE] x86/kexec: Add int3 in kexec path for testing Date: Fri, 14 Mar 2025 17:27:38 +0000 Message-ID: <20250314173226.3062535-7-dwmw2@infradead.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250314173226.3062535-1-dwmw2@infradead.org> References: <20250314173226.3062535-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Content-Type: text/plain; charset="utf-8" From: David Woodhouse Signed-off-by: David Woodhouse --- arch/x86/kernel/relocate_kernel_64.S | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index 4f8b7d318025..228301ac66e9 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -158,7 +158,7 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) lidt (%rsp) addq $10, %rsp =20 - //int3 + int3 =20 /* * Clear X86_CR4_CET (if it was set) such that we can clear CR0_WP --=20 2.48.1 From nobody Wed Dec 17 19:00:28 2025 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7268C201002 for ; Fri, 14 Mar 2025 17:32:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741973565; cv=none; b=hwRFDVsImxSbMeR22Z00kNtgae1oe5cwoktJiUlKEDPelkO9bGl0iI8z1RFrpeljZexRIJoMUUtwkbccuj8D6H/E5phm1AuJi0crrrY6G7tbskQ8cn1nsGRCz9idbHHiuh8L7VWQgCIgXofvhu7JVgJNholXV6PI9qvv8rKxKFs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741973565; c=relaxed/simple; bh=vAy0a/6TMYOoP2YK49H3iPUzcwOqZD68MfTy3bsmu/8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Aiq5iFkxbrOkhKnSLLIh66xB/gbMBY6mXSJJbfhN+wn19rZA1Nm1iw6DqIgIIumxeFFNmAfnp4cgZZ/0ykv92henzJtSUjwRC2zqHHcQKocJQiG6/gFgMsfkATLeXy1iHKo8cb6LCDKZ7u2CRJxl4sBeoXxdXZoMZR2CW0eGKlo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=desiato.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=h8NSWW96; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=desiato.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="h8NSWW96" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=7ZEU4ooB9JbXRgCaEf2/WOsbQmZpDXUsz2616q1hGqk=; b=h8NSWW9622lyf+TkR66+QUn3pB vnD9qJh9YuRa82X3/xejTJyv4mUvkP02rihXQkbmK9S7TSOHAABGDtKRRHlDRYrdEU4RlAO/8S3fE DcDjjNZLB5FK7pL7gWtdVbWck58neWnKh6XbdGCIWD6Pv/qO6tNprEj3VN1ZIBdPkFfb8JyBxEtIB 6/evZ7cZ3tJC0hGc1NaK6/wjYhN/TtnpM9KV5whneVvCSs2tMHyluC+6Q71rzWr883CJu2uvYXgGr 5S44TQNl/dX2pQ8l22tfnJP5WyVBCDevxUUtj5gBd/PzlSSYlvJ7mjBfb0v+/vDhpb+m0T2CVAw8R 4V0G+bcw==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by desiato.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tt8tb-00000002vUA-3fSj; Fri, 14 Mar 2025 17:32:32 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tt8tZ-0000000CsWL-0pvp; Fri, 14 Mar 2025 17:32:29 +0000 From: David Woodhouse To: kexec@lists.infradead.org Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , David Woodhouse , "Kirill A . Shutemov" , Kai Huang , Nikolay Borisov , linux-kernel@vger.kernel.org, Simon Horman , Dave Young , Peter Zijlstra , jpoimboe@kernel.org, bsz@amazon.de Subject: [PATCH v8 7/7] [DO NOT MERGE] x86/kexec: Add CFI type information to relocate_kernel() Date: Fri, 14 Mar 2025 17:27:39 +0000 Message-ID: <20250314173226.3062535-8-dwmw2@infradead.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250314173226.3062535-1-dwmw2@infradead.org> References: <20250314173226.3062535-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html Content-Type: text/plain; charset="utf-8" From: David Woodhouse A previous commit added __nocfi to machine_kexec() because it makes an indirect call to relocate_kernel() which lacked CFI type information, and caused the system to crash. Use SYM_TYPED_FUNC_START() to ensure that the type information is present, and remove the __nocfi tag. I still can't make objtool happy with this in both GCC and Clang builds at the same time, so not yet for merging; only included in this series to nerd-snipe the objtool maintainers. Signed-off-by: David Woodhouse --- arch/x86/kernel/machine_kexec_64.c | 2 +- arch/x86/kernel/relocate_kernel_64.S | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_k= exec_64.c index 7abc7aa0261b..84f59f18dcb6 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -380,7 +380,7 @@ void machine_kexec_cleanup(struct kimage *image) * Do not allocate memory (or fail in any way) in machine_kexec(). * We are past the point of no return, committed to rebooting now. */ -void __nocfi machine_kexec(struct kimage *image) +void machine_kexec(struct kimage *image) { unsigned long reloc_start =3D (unsigned long)__relocate_kernel_start; relocate_kernel_fn *relocate_kernel_ptr; diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index 228301ac66e9..dbcef3430285 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -5,6 +5,7 @@ */ =20 #include +#include #include #include #include @@ -59,8 +60,9 @@ SYM_DATA_END(kexec_debug_idt) =20 .section .text..relocate_kernel,"ax"; .code64 -SYM_CODE_START_NOALIGN(relocate_kernel) +SYM_TYPED_FUNC_START(relocate_kernel) UNWIND_HINT_END_OF_STACK + UNWIND_HINT_FUNC ANNOTATE_NOENDBR /* * %rdi indirection_page --=20 2.48.1