From nobody Wed Feb 5 17:03:21 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 3F6C9482EF for ; Wed, 15 Jan 2025 19:14: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=1736968484; cv=none; b=IF6qPbssUgcbmBDzN3PeNg3tb9W58fhPlLpkhj+rY1SG//OLFhHJDvUGWuV0TiKUP9gopiJRCujyb9fVo9sTZ5TLC6kg3Kwecna+ZJ4TjL6gz3HGuM16d4u4Pli6acYN6neh7f30e7vNfrdskSRbnExpMdF1OIURyCDc1iS/hIs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736968484; c=relaxed/simple; bh=KX2zDbp6BlEvBsyiSKnIFwdDwhiybqszDV6WGNqoUjQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZNkbrZmRqpO7ndvjE9gLUWFCL8zpp4nAU1DbMxCJlXZ1i872P+awTwhBq5/trpQOMyPVHbEEquGHUN2Q+BdadUB+3kBCU+JrhYFJVPVeY2ixOKahCS9GYmYRyOPuogJGLXkxjGkdrMIckVCnhG6W3/VYe41Nd7PL+plhSFGd3SM= 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=O1G029fJ; 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="O1G029fJ" 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=0Lfnf5OQV/O8wFKeVcDBQagu82mNw6Nx1vxvLmuyV88=; b=O1G029fJGrVbz1TpuH8Q5lS7O/ xX029hUh7UlzrN6qDj3NkLOtdGsiP1CFat0uSvH7Kxo8pwEdTUTJ5VAK+lODrDNlz+BhWehmSIlAu Zo4652XtGwzYXLIFDFAPDJ34jlAbkMjnoHulVXDPLkuqtxQkO3WPRLRPy7ltk9aDBVjbTWYViivqw 3K+nlFN75OCUcK2Dj/w9oJhz+CZVO1e7vS7XfyRAtBB9RQD1h2OiAbb/DFwRUmBkX1nh11py0xiAr X3M/sm9gslMQ/SfDsr+wEhY9rNlVFE4bVBxk9o3QFNRWyEIH5KPDFu1JsCmpZIB9l4oinbodCb/aJ n5juTMOQ==; 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 1tY8qS-0000000AvMb-07Gl; Wed, 15 Jan 2025 19:14:29 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tY8qO-00000002Uw1-3wfb; Wed, 15 Jan 2025 19:14:24 +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 v6 3/7] x86/kexec: Debugging support: Load an IDT and basic exception entry points Date: Wed, 15 Jan 2025 19:09:32 +0000 Message-ID: <20250115191423.587774-4-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250115191423.587774-1-dwmw2@infradead.org> References: <20250115191423.587774-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 | 23 ++++++++ arch/x86/kernel/relocate_kernel_64.S | 82 ++++++++++++++++++++++++++++ 3 files changed, 110 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..535438375568 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -304,6 +304,26 @@ static void load_segments(void) ); } =20 +static void prepare_debug_idt(unsigned long control_page, unsigned long ve= c_ofs) +{ +#ifdef CONFIG_KEXEC_DEBUG + 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; + } +#endif +} + int machine_kexec_prepare(struct kimage *image) { void *control_page =3D page_address(image->control_code_page); @@ -321,6 +341,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 c62f03808f18..f5b134a81827 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -49,6 +49,12 @@ SYM_DATA_START_LOCAL(kexec_debug_gdt) .quad 0x00af9a000000ffff /* __KERNEL_CS */ .quad 0x00cf92000000ffff /* __KERNEL_DS */ SYM_DATA_END_LABEL(kexec_debug_gdt, SYM_L_LOCAL, kexec_debug_gdt_end) + + .balign 8 +SYM_DATA_START(kexec_debug_idt) + .skip 0x100, 0x00 +SYM_DATA_END(kexec_debug_idt) + #endif /* CONFIG_KEXEC_DEBUG */ =20 .section .text..relocate_kernel,"ax"; @@ -113,6 +119,11 @@ SYM_CODE_START_NOALIGN(relocate_kernel) jmp *%rsi SYM_CODE_END(relocate_kernel) =20 +#ifdef DEBUG + UNWIND_HINT_UNDEFINED + .balign 0x100 /* relocate_kernel will be overwritten with an IDT */ +#endif + SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) UNWIND_HINT_END_OF_STACK /* @@ -140,6 +151,15 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) /* Test that we can load segments */ movq %ds, %rax movq %rax, %ds + + /* 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 #endif /* CONFIG_KEXEC_DEBUG */ =20 /* @@ -367,3 +387,65 @@ SYM_CODE_START_LOCAL_NOALIGN(swap_pages) ret int3 SYM_CODE_END(swap_pages) + +#ifdef CONFIG_KEXEC_DEBUG +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) + 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) +#endif /* CONFIG_KEXEC_DEBUG */ --=20 2.47.0