From nobody Sat Feb 7 08:55:12 2026 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 B794513DBB6 for ; Wed, 27 Nov 2024 19:04:04 +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=1732734246; cv=none; b=fmRx2gPw/5IlDJidzQeDAWhJ4ZjYRChjN5ikP2IuRVHAA6RPb0mHihuOEoRFFM21Slcx/CoG99DpETm0SkHvTKYQB/4JGAE4HzxdasLux3iJATKtKpKnvat8cVj5iuQ0KvMxPGXbaUkM030eOvemcJRJcakp+ALsuNvKsxv+VPs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734246; c=relaxed/simple; bh=3SqJF/VnlSgG+GODyAWRjTdYXkhTouAgU12k3nHcgX8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SEqbdR7fwXIPTMatK6Xezye20C6o/RNuj8oiPMqxr95OnFip/tIV/UgWWGFWiiy+CmFVwjPTOTWChBdrQVqRoCLXu64OnAwUSyilXxCA05FJluLUXX1mhAg35bGxApLPYwHMETFU4scatFGB7fxoUwQ6FSR65rpUKMZmkKLnf+0= 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=ajAa6zng; 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="ajAa6zng" 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=wV5Ch2f/rekMRnAGWDqppVh4gVRb99fHt2UOyp/e2N0=; b=ajAa6zngHwcn7K5Aa3M5D9KTC6 VAB7nGe2pyUne/Xv4EIWvecFnKgLWcBZKiF9bpSwKnlTffGJaoZ/n01mLHxEokcdPcwnBG0V10NxN jPcm6sBdKJ9i56VFVyOalcgWe7cPe/Si9MZkZNBD9rWgjAKUiSKnm6gdbw+o8J2GncyHSDYsZF554 d+/yjrKqBbp3NrwuDCkCf9LhTuxwOMId3TNlFMztgIpzG5FBQrFzpMqPlJ4iYNViiBrjmvtl4YLWv OLaVAKRkX6aJYfVH8Y9MVRghUVid/D6OrVEPo70Xyk91CYXJqPvXb4/vCxU7ZQPLNUI6xz/imUdt2 IOTEwGtA==; 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 1tGNKF-00000001Sub-18GV; Wed, 27 Nov 2024 19:03:47 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKD-00000000BiA-3OxZ; Wed, 27 Nov 2024 19:03:45 +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: [RFC PATCH v4 01/20] x86/kexec: Restore GDT on return from preserve_context kexec Date: Wed, 27 Nov 2024 19:00:15 +0000 Message-ID: <20241127190343.44916-2-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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 restore_processor_state() function explicitly states that "the asm code that gets us here will have restored a usable GDT". That wasn't true in the case of returning from a preserve_context kexec. Make it so. Without this, the kernel was depending on the called function to reload a GDT which is appropriate for the kernel before returning. Test program: #include #include #include #include #include #include #include #include int main (void) { struct kexec_segment segment =3D {}; unsigned char purgatory[] =3D { 0x66, 0xba, 0xf8, 0x03, // mov $0x3f8, %dx 0xb0, 0x42, // mov $0x42, %al 0xee, // outb %al, (%dx) 0xc3, // ret }; int ret; segment.buf =3D &purgatory; segment.bufsz =3D sizeof(purgatory); segment.mem =3D (void *)0x400000; segment.memsz =3D 0x1000; ret =3D syscall(__NR_kexec_load, 0x400000, 1, &segment, KEXEC_PRESERVE_CON= TEXT); if (ret) { perror("kexec_load"); exit(1); } ret =3D syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LIN= UX_REBOOT_CMD_KEXEC); if (ret) { perror("kexec reboot"); exit(1); } printf("Success\n"); return 0; } Signed-off-by: David Woodhouse Cc: stable@vger.kernel.org --- arch/x86/kernel/relocate_kernel_64.S | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index e9e88c342f75..1236f25fc8d1 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -242,6 +242,13 @@ SYM_CODE_START_LOCAL_NOALIGN(virtual_mapped) movq CR0(%r8), %r8 movq %rax, %cr3 movq %r8, %cr0 + +#ifdef CONFIG_KEXEC_JUMP + /* Saved in save_processor_state. */ + movq $saved_context, %rax + lgdt saved_context_gdt_desc(%rax) +#endif + movq %rbp, %rax =20 popf --=20 2.47.0 From nobody Sat Feb 7 08:55:12 2026 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 B8449200B95 for ; Wed, 27 Nov 2024 19:04:04 +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=1732734247; cv=none; b=VknseQFJWE9yxmTKxOtcq+J5g4g5lFU+DxlmQz3hJkLLXw1Enlouy2vgNZY6lEV5RdGSkc8iqNaN1TZD+epAi1SD4dr/Skh7Rn5g9/j0i0wAK021OGUUicpcMEAewV3sifWu20VJxxHQ4krEPI4iLhlv6sEP6gkkmYNjr0v3/fc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734247; c=relaxed/simple; bh=YEYgduFbx3hxPviKlbaHo3vgA2xGogrBHXSzn5W5fyc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=W1vU183fRc/WeJ4s51P+5nNvzP4Ou6faBqv+Bug+kB+Xl3U9TwDws7iJ6J61fn0goYk1MqmkDDdpWlq+9x0B8gha0G6MM1XgesEJUecpMWXcfKS3w5eDI+OxlpInoN2JOlMsIk7ltHMyZQVcTF+6mPbo1Kypcf+CtwHmbTlJ4xs= 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=oozLZbru; 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="oozLZbru" 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=G8KdxSUjA1BoxOPlX8D0AVdJHTOMU4u9sPbtmHj+Y10=; b=oozLZbrufgQLvjMvST9Tk0OoKX F2Y6DFOreM8CdHo3b6KM9q3DvDFNCM6NHxvnGFFWrqxJkP6gmQdl9RVPP4JY4rtmegp8a6X7Dmwde Wse0mPybjCz99WPJYJsHyT928/VaWmqTtEO4IDpkBvwhlxTm3i9VI3cfl3nxODEMyq3vv9DhAt0hx ncH0P1AWEumIdUc1qCf9y65HRZmR4q+K4TW4aoILXPp/JTL/6pnJ9XJx4NLmayRv+Y5+cn8/8SFMP pqkxc+peG8tIhv3iGcx92fctxfId6z6jX1pvuYdVqRkuOMiuY4VK7IdLGG7V5sf7kBGXX4OeVfoXZ zG73p7qg==; 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 1tGNKF-00000001Suc-17d4; Wed, 27 Nov 2024 19:03:47 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKD-00000000BiE-3dWr; Wed, 27 Nov 2024 19:03:45 +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: [RFC PATCH v4 02/20] x86/kexec: Clean up and document register use in relocate_kernel_64.S Date: Wed, 27 Nov 2024 19:00:16 +0000 Message-ID: <20241127190343.44916-3-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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 Add more comments explaining what each register contains, and save the preserve_context flag to a non-clobbered register sooner, to keep things simpler. Signed-off-by: David Woodhouse Acked-by: Kai Huang --- arch/x86/kernel/relocate_kernel_64.S | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index 1236f25fc8d1..92478e2e254f 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -100,6 +100,9 @@ SYM_CODE_START_NOALIGN(relocate_kernel) movq %r10, CP_PA_SWAP_PAGE(%r11) movq %rdi, CP_PA_BACKUP_PAGES_MAP(%r11) =20 + /* Save the preserve_context to %r11 as swap_pages clobbers %rcx. */ + movq %rcx, %r11 + /* Switch to the identity mapped page tables */ movq %r9, %cr3 =20 @@ -116,6 +119,14 @@ SYM_CODE_END(relocate_kernel) =20 SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) UNWIND_HINT_END_OF_STACK + /* + * %rdi indirection page + * %rdx start address + * %r11 preserve_context + * %r12 host_mem_enc_active + * %r13 original CR4 when relocate_kernel() was invoked + */ + /* set return address to 0 if not preserving context */ pushq $0 /* store the start address on the stack */ @@ -170,8 +181,6 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) wbinvd .Lsme_off: =20 - /* Save the preserve_context to %r11 as swap_pages clobbers %rcx. */ - movq %rcx, %r11 call swap_pages =20 /* @@ -183,13 +192,14 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) movq %cr3, %rax movq %rax, %cr3 =20 + testq %r11, %r11 /* preserve_context */ + jnz .Lrelocate + /* * set all of the registers to known values * leave %rsp alone */ =20 - testq %r11, %r11 - jnz .Lrelocate xorl %eax, %eax xorl %ebx, %ebx xorl %ecx, %ecx --=20 2.47.0 From nobody Sat Feb 7 08:55:12 2026 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 25A852036FE for ; Wed, 27 Nov 2024 19:04:09 +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=1732734251; cv=none; b=UduJlPHzE670tpNvvDxy0afJD5oj+cfzttUQacmLGac4EzBjHebhQ5HG3rskiJBXZKiVSmhsV+i5AqgXnjhscFY3ksydwZ9tt95sArZzX3zPAkFgLaHaL3KDzgfRX5mEhPaOV/7m2mxpTmUesodc9cfP6dbBZuXDhr5fC2Hd9CQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734251; c=relaxed/simple; bh=0IXqucUxMBmTpYcXB8raPtO79+38AarfDo7A5uJ9hsA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TqWNNNyRJjDHHamZS1672dvhK4O1m3ye+eE4QjSnFYbjINaj9N3RinxkZ+bf1y5PyEE4wttx7SseMyjJ2nN4QxMKhCQnelfdng+QeoAt5vmnl7dLb+KTQwEn9d3OZToqQwGp4Ya4CHm0ndOHxRqN6ZjxkMGkNtIq+q1JVZWByaI= 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=CKL0Pnq6; 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="CKL0Pnq6" 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=fLZisNRVnIE7XYTDFZ5u5GD/dt85ViU8LD82f5fJODE=; b=CKL0Pnq6GJ1us0i0716sVxK93k GFKtUYNrh1URKFI+lnoi/NEdvJaeZFY/0VysNqn0jaMbJ9O6ALODaWllLzervIVGFpUKBaLcgg0mC qAlhQWKbO6liMmKDoRWKyWiBJJtctPwlkHU/+/8yoSmDfmgbQpc5+6hzoymzlEfNFYyrJ6psWfxLW w6dvX9l034i+XaEdwJSQwr6SrnHQByTws4kWc9nTOzxrveL7PgQ071+9vuAQNwNTWttYJIWl6cXDm LgmrDP0ygEEohMlmhR9i/JY73YqG5GSRx1L9gutbSrYdahF94zvBbtvLab627pNVVFUKceM6DK5Xv ApvNSYiw==; 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 1tGNKC-00000001cW3-3qom; Wed, 27 Nov 2024 19:03:46 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKD-00000000BiI-3sev; Wed, 27 Nov 2024 19:03:45 +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: [RFC PATCH v4 03/20] x86/kexec: Use named labels in swap_pages in relocate_kernel_64.S Date: Wed, 27 Nov 2024 19:00:17 +0000 Message-ID: <20241127190343.44916-4-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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 Make the code a little more readable. Signed-off-by: David Woodhouse Acked-by: Kai Huang --- arch/x86/kernel/relocate_kernel_64.S | 30 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index 92478e2e254f..fea650f92606 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -279,31 +279,31 @@ SYM_CODE_START_LOCAL_NOALIGN(swap_pages) movq %rdi, %rcx /* Put the indirection_page in %rcx */ xorl %edi, %edi xorl %esi, %esi - jmp 1f + jmp .Lstart /* Should start with an indirection record */ =20 -0: /* top, read another word for the indirection page */ +.Lloop: /* top, read another word for the indirection page */ =20 movq (%rbx), %rcx addq $8, %rbx -1: +.Lstart: testb $0x1, %cl /* is it a destination page? */ - jz 2f + jz .Lnotdest movq %rcx, %rdi andq $0xfffffffffffff000, %rdi - jmp 0b -2: + jmp .Lloop +.Lnotdest: testb $0x2, %cl /* is it an indirection page? */ - jz 2f + jz .Lnotind movq %rcx, %rbx andq $0xfffffffffffff000, %rbx - jmp 0b -2: + jmp .Lloop +.Lnotind: testb $0x4, %cl /* is it the done indicator? */ - jz 2f - jmp 3f -2: + jz .Lnotdone + jmp .Ldone +.Lnotdone: testb $0x8, %cl /* is it the source indicator? */ - jz 0b /* Ignore it otherwise */ + jz .Lloop /* Ignore it otherwise */ movq %rcx, %rsi /* For ever source page do a copy */ andq $0xfffffffffffff000, %rsi =20 @@ -328,8 +328,8 @@ SYM_CODE_START_LOCAL_NOALIGN(swap_pages) rep ; movsq =20 lea PAGE_SIZE(%rax), %rsi - jmp 0b -3: + jmp .Lloop +.Ldone: ANNOTATE_UNRET_SAFE ret int3 --=20 2.47.0 From nobody Sat Feb 7 08:55:12 2026 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 4A7631F5FD for ; Wed, 27 Nov 2024 19:04:04 +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=1732734245; cv=none; b=nuUIZuHgF0gB7mkNFsiuLhVWCpe9+qbBqx9m9/b/QHSI3XS9Y2/ClRnJqjtKr7SgWcqyypUOfoPa4axkdCbrB5nJLUyAZAC2+Muhsnc6FDdDakKDtJZm9zgudSMXRf8bKy5MfxNqjMdY5ZKbpzirJFyBgyWuydw/lCt4ng9HM9Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734245; c=relaxed/simple; bh=Jszb8KFN5GzjUkCQ/IcBcaFlVglAOixLKBZmKX8ltN8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sv922MtpFJSfiLahRmbEZTBxhV4ZKvz8pBIAKLL4FhKqQBDE4aWYXqrI4XLPtj3qO2uzs8+Yv4AQ23KtTWXKRj0a+zZaSBsQ+uuDouT3QAT931PfLIwbs6VGiPX9PuO6xbbcxG2BYwk33Ss9+ORXvP3JibE1v5I0mM5+7TfKsBo= 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=ah2JlbFU; 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="ah2JlbFU" 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=AbThuJMqNCEqi5OCJ2hHO8LahRmj/ISCryb2ADbdx9M=; b=ah2JlbFUdHK5RQ1AnFOXYqBoiM tXKSBRgBzcq16hLM8IfYjNoRPIMC90FYEnffspwc3822yDnFXeAP8s7HfB90kVpEgg1Wr3VSX+hGC W7qZZPUcbEuGr1HoATjsjOT3fy/ppfYZk97D4+Xcc0cJ6AjeRxctXO/5zTxHwGEcRb9g37BWk5xjk pA9NnhXAR0SLiT0Y/SveMSlviSlpvRnxTFhjLft2fBR+KmrKJxPl2BiBJxOh9llUCmOTaWWSAEKnz +Rriu2nSpH4Fy+Uiu+7BwV/eRZalbMkyiC1MFOSpzDDsagkWf8LKrsX9D827x/15pxM/1gpL11FV4 b9H90kWg==; 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 1tGNKF-00000001Sud-188j; Wed, 27 Nov 2024 19:03:47 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKD-00000000BiM-47rP; Wed, 27 Nov 2024 19:03:46 +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: [RFC PATCH v4 04/20] x86/kexec: Only swap pages for preserve_context mode Date: Wed, 27 Nov 2024 19:00:18 +0000 Message-ID: <20241127190343.44916-5-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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 There's no need to swap pages (which involves three memcopies for each page) in the plain kexec case. Just do a single copy from source to destination page. Signed-off-by: David Woodhouse --- arch/x86/kernel/relocate_kernel_64.S | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index fea650f92606..50cc33f2ecb7 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -310,6 +310,9 @@ SYM_CODE_START_LOCAL_NOALIGN(swap_pages) movq %rdi, %rdx /* Save destination page to %rdx */ movq %rsi, %rax /* Save source page to %rax */ =20 + testq %r11, %r11 /* Only actually swap for preserve_context */ + jz .Lnoswap + /* copy source page to swap page */ movq %r10, %rdi movl $512, %ecx @@ -324,6 +327,7 @@ SYM_CODE_START_LOCAL_NOALIGN(swap_pages) /* copy swap page to destination page */ movq %rdx, %rdi movq %r10, %rsi +.Lnoswap: movl $512, %ecx rep ; movsq =20 --=20 2.47.0 From nobody Sat Feb 7 08:55:12 2026 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 5A07113D508 for ; Wed, 27 Nov 2024 19:04:04 +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=1732734246; cv=none; b=c6kZLY7G4k0e6I1FHp0tsUPHjjQZuISTO4Uuw5armSVK6NYRqR7ObuiKL6iGLNTXzOROs1z4HGCmpluevp8rdghwAanvktR3RRj5iuWfJoHO2gLMI8d4OEAvj/bdfsxCAXr2W0x/qPjlUellq06SOrfJl756sn3TrgrwEWNgi/E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734246; c=relaxed/simple; bh=YJXrns6qDSqiRjj29QGazSPtjrIYWURjaZhzu/au2Co=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ub0W4DMSu/6jfC0zkHrrwlCgg/7ZJ8a82WNGcF/jy9aUjvGDNQtLhqYwBxRHUw2SjJKBEsnVsNJ59jxNLkqE6bcdXOdDiKat3hmZEe+qIQdxW012H6Fl2hlHIEeITkZeHwhXikCrjg7+rYs5H5QIMPokibkhggxKcbh6dwQjhXE= 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=hwA8++2g; 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="hwA8++2g" 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=3+iHS09x/bUbYczSnY4CdvRzG4X74WBD5o3VDFQQU2k=; b=hwA8++2g3fZuKG6TcF6+2Z8MXR vv1srmp07bw1OZBEmjW7Nyr999NAwYCUCnBgDbKLTc47bA6aMV6nYY66kYP/E6YX5OktmHI8d9+2M ylzFzpUUlQMsmPpOo5VuK1/W/gHWLtDZ71xcC1aZwmdsSZvnYKsNM/4kyces0cIZhgcZ0/8R4smES AYvF/9fSPjO+/xXh0XV01Pv8TpinHEuBzZIZVupR2ZVfLTQGOzQNa/podDMGBjImaHGQ1hzwYJ7Cv n/iDPvNfHCONX2ddihN7KZ+VeOB2Lx1EpKxp5Cp8bQr24l96y0WyqCNNXc5S8ItYHFmp2x3wf/CZL ZoVwJzgA==; 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 1tGNKF-00000001Sue-188L; Wed, 27 Nov 2024 19:03:47 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKE-00000000BiQ-0B6E; Wed, 27 Nov 2024 19:03:46 +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: [RFC PATCH v4 05/20] x86/mm: Add _PAGE_NOPTISHADOW bit to avoid updating userspace page tables Date: Wed, 27 Nov 2024 19:00:19 +0000 Message-ID: <20241127190343.44916-6-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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 set_p4d() and set_pgd() functions (in 4-level or 5-level page table set= ups respectively) assume that the root page table is actually a 8KiB allocation, with the userspace root immediately after the kernel root page table (so th= at the former can enforce NX on on all the subordinate pages, which are actual= ly shared). However, users of the kernel_ident_mapping_init() code do not give it an 8K= iB allocation for its PGD. Both swsusp_arch_resume() and acpi_mp_setup_reset() allocate only a single 4KiB page. The kexec code on x86_64 currently gets away with it purely by chance, because it allocates 8KiB for its "control code page" and then actually uses the first half for the PGD, then copies t= he actual trampoline code into the second half only after the identmap code has finished scribbling over it. Fix this by defining a _PAGE_NOPTISHADOW bit (which can use the same bit as _PAGE_SAVED_DIRTY since one is only for the PGD/P4D root and the other is exclusively for leaf PTEs.). This instructs __pti_set_user_pgtbl() not to write to the userspace 'shadow' PGD. Strictly, the _PAGE_NOPTISHADOW bit doesn't need to be written out to the actual page tables; since __pti_set_user_pgtbl() returns the value to be written to the kernel page table, it could be filtered out. But there seems to be no benefit to actually doing so. Cc: stable@kernel.org Signed-off-by: David Woodhouse --- arch/x86/include/asm/pgtable_types.h | 8 ++++++-- arch/x86/mm/ident_map.c | 6 +++--- arch/x86/mm/pti.c | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/pgtable_types.h b/arch/x86/include/asm/pg= table_types.h index 6f82e75b6149..4b804531b03c 100644 --- a/arch/x86/include/asm/pgtable_types.h +++ b/arch/x86/include/asm/pgtable_types.h @@ -36,10 +36,12 @@ #define _PAGE_BIT_DEVMAP _PAGE_BIT_SOFTW4 =20 #ifdef CONFIG_X86_64 -#define _PAGE_BIT_SAVED_DIRTY _PAGE_BIT_SOFTW5 /* Saved Dirty bit */ +#define _PAGE_BIT_SAVED_DIRTY _PAGE_BIT_SOFTW5 /* Saved Dirty bit (leaf) */ +#define _PAGE_BIT_NOPTISHADOW _PAGE_BIT_SOFTW5 /* No PTI shadow (root PGD)= */ #else /* Shared with _PAGE_BIT_UFFD_WP which is not supported on 32 bit */ -#define _PAGE_BIT_SAVED_DIRTY _PAGE_BIT_SOFTW2 /* Saved Dirty bit */ +#define _PAGE_BIT_SAVED_DIRTY _PAGE_BIT_SOFTW2 /* Saved Dirty bit (leaf) */ +#define _PAGE_BIT_NOPTISHADOW _PAGE_BIT_SOFTW2 /* No PTI shadow (root PGD)= */ #endif =20 /* If _PAGE_BIT_PRESENT is clear, we use these: */ @@ -139,6 +141,8 @@ =20 #define _PAGE_PROTNONE (_AT(pteval_t, 1) << _PAGE_BIT_PROTNONE) =20 +#define _PAGE_NOPTISHADOW (_AT(pteval_t, 1) << _PAGE_BIT_NOPTISHADOW) + /* * Set of bits not changed in pte_modify. The pte's * protection key is treated like _PAGE_RW, for diff --git a/arch/x86/mm/ident_map.c b/arch/x86/mm/ident_map.c index 437e96fb4977..5ab7bd2f1983 100644 --- a/arch/x86/mm/ident_map.c +++ b/arch/x86/mm/ident_map.c @@ -174,7 +174,7 @@ static int ident_p4d_init(struct x86_mapping_info *info= , p4d_t *p4d_page, if (result) return result; =20 - set_p4d(p4d, __p4d(__pa(pud) | info->kernpg_flag)); + set_p4d(p4d, __p4d(__pa(pud) | info->kernpg_flag | _PAGE_NOPTISHADOW)); } =20 return 0; @@ -218,14 +218,14 @@ int kernel_ident_mapping_init(struct x86_mapping_info= *info, pgd_t *pgd_page, if (result) return result; if (pgtable_l5_enabled()) { - set_pgd(pgd, __pgd(__pa(p4d) | info->kernpg_flag)); + set_pgd(pgd, __pgd(__pa(p4d) | info->kernpg_flag | _PAGE_NOPTISHADOW)); } else { /* * With p4d folded, pgd is equal to p4d. * The pgd entry has to point to the pud page table in this case. */ pud_t *pud =3D pud_offset(p4d, 0); - set_pgd(pgd, __pgd(__pa(pud) | info->kernpg_flag)); + set_pgd(pgd, __pgd(__pa(pud) | info->kernpg_flag | _PAGE_NOPTISHADOW)); } } =20 diff --git a/arch/x86/mm/pti.c b/arch/x86/mm/pti.c index 851ec8f1363a..5f0d579932c6 100644 --- a/arch/x86/mm/pti.c +++ b/arch/x86/mm/pti.c @@ -132,7 +132,7 @@ pgd_t __pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd) * Top-level entries added to init_mm's usermode pgd after boot * will not be automatically propagated to other mms. */ - if (!pgdp_maps_userspace(pgdp)) + if (!pgdp_maps_userspace(pgdp) || (pgd.pgd & _PAGE_NOPTISHADOW)) return pgd; =20 /* --=20 2.47.0 From nobody Sat Feb 7 08:55:12 2026 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 25B5E203700 for ; Wed, 27 Nov 2024 19:04:09 +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=1732734253; cv=none; b=QNbXHLopOY5icyUnDvMI5PB9XUhzZd20BzZGA+AJTaakKDZzyO90T58DY+TzRxqCVGZ6Lo1NcPvTIO48tn+40AywK2U7HH7MJukIJ1NuYIJmRhTY1K9tyQnSrkEGg1w8WogNv+iu+Q5qk31MZz6dUUFwnFG4reN2qfAEf/rrrFk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734253; c=relaxed/simple; bh=rGGczQPPXwpG5n/Ef/sXDs2fngcI7S7YYqN9aWusfSM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=F39zztIbJSWdPtC5o6OpPTnHt8pc8r9Mbj+iKglfbR8G8vOsWz/ZWmTc177QYvcYy11j66hCPe8T8IourC/ZJiWrsIWLvSQc8Du8dfvdjBG+0XAzvl2KQvU6R5SqGgRpKX58DwfuZaE57EHJmptkeFoNs1pFLAKUdHhfUjFlURQ= 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=wGBHZk4n; 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="wGBHZk4n" 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=PDgTOq7AS1Q2FfXttd9WQ6MOg0yX2l6kaJBCPuUCN9Q=; b=wGBHZk4nQf+3LZW+4YM/s0Ogig lbsBurkO+TD044YTth/BBC6fkJ91UHYerZOr5aGf/LE9xDK0+v229ijaosmG18tt1MsVJIswPjNvm tnvDMY24CzJz9yTQpGRzkadOkHWEqdFg9esRAlmbhAYKNTYx4sB3iqKms5D7X4eZmoCAb0sWQ6jkA qLUv6FPgjKIPFNmgOFliCNTVtt/QPRPket3qRz4hA4JpP+VF8CKfyAv0a5BdxzmVqP1h3gS0zPN+z 1uDaAWhvqP+PFoOkbjYxDt0WwM+J7rbQKMLgybxmrhhAgxZfVevLl/4TbEVglG9YYN3rB+AFq6/ZL 8BTWAn6g==; 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 1tGNKD-00000001cW6-0OR3; Wed, 27 Nov 2024 19:03:46 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKE-00000000BiU-0QFk; Wed, 27 Nov 2024 19:03:46 +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: [RFC PATCH v4 06/20] x86/kexec: Allocate PGD for x86_64 transition page tables separately Date: Wed, 27 Nov 2024 19:00:20 +0000 Message-ID: <20241127190343.44916-7-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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 There's no good reason for this to be part of the control_code_page; just allocate it separately on x86_64 like i386 does. Signed-off-by: David Woodhouse --- arch/x86/include/asm/kexec.h | 18 +++++++++--- arch/x86/kernel/machine_kexec_64.c | 45 ++++++++++++++++-------------- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/arch/x86/include/asm/kexec.h b/arch/x86/include/asm/kexec.h index ae5482a2f0ca..ccb8ff37fa9d 100644 --- a/arch/x86/include/asm/kexec.h +++ b/arch/x86/include/asm/kexec.h @@ -16,6 +16,7 @@ # define PAGES_NR 4 #endif =20 +# define KEXEC_CONTROL_PAGE_SIZE 4096 # define KEXEC_CONTROL_CODE_MAX_SIZE 2048 =20 #ifndef __ASSEMBLY__ @@ -43,7 +44,6 @@ struct kimage; /* Maximum address we can use for the control code buffer */ # define KEXEC_CONTROL_MEMORY_LIMIT TASK_SIZE =20 -# define KEXEC_CONTROL_PAGE_SIZE 4096 =20 /* The native architecture */ # define KEXEC_ARCH KEXEC_ARCH_386 @@ -58,9 +58,6 @@ struct kimage; /* Maximum address we can use for the control pages */ # define KEXEC_CONTROL_MEMORY_LIMIT (MAXMEM-1) =20 -/* Allocate one page for the pdp and the second for the code */ -# define KEXEC_CONTROL_PAGE_SIZE (4096UL + 4096UL) - /* The native architecture */ # define KEXEC_ARCH KEXEC_ARCH_X86_64 #endif @@ -145,6 +142,19 @@ struct kimage_arch { }; #else struct kimage_arch { + /* + * This is a kimage control page, as it must not overlap with either + * source or destination address ranges. + */ + pgd_t *pgd; + /* + * The virtual mapping of the control code page itself is used only + * during the transition, while the current kernel's pages are all + * in place. Thus the intermediate page table pages used to map it + * are not control pages, but instead just normal pages obtained + * with get_zeroed_page(). And have to be tracked (below) so that + * they can be freed. + */ p4d_t *p4d; pud_t *pud; pmd_t *pmd; diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_k= exec_64.c index 9c9ac606893e..7223c38a8708 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -146,7 +146,8 @@ static void free_transition_pgtable(struct kimage *imag= e) image->arch.pte =3D NULL; } =20 -static int init_transition_pgtable(struct kimage *image, pgd_t *pgd) +static int init_transition_pgtable(struct kimage *image, pgd_t *pgd, + unsigned long control_page) { pgprot_t prot =3D PAGE_KERNEL_EXEC_NOENC; unsigned long vaddr, paddr; @@ -157,7 +158,7 @@ static int init_transition_pgtable(struct kimage *image= , pgd_t *pgd) pte_t *pte; =20 vaddr =3D (unsigned long)relocate_kernel; - paddr =3D __pa(page_address(image->control_code_page)+PAGE_SIZE); + paddr =3D control_page; pgd +=3D pgd_index(vaddr); if (!pgd_present(*pgd)) { p4d =3D (p4d_t *)get_zeroed_page(GFP_KERNEL); @@ -216,7 +217,7 @@ static void *alloc_pgt_page(void *data) return p; } =20 -static int init_pgtable(struct kimage *image, unsigned long start_pgtable) +static int init_pgtable(struct kimage *image, unsigned long control_page) { struct x86_mapping_info info =3D { .alloc_pgt_page =3D alloc_pgt_page, @@ -225,12 +226,12 @@ static int init_pgtable(struct kimage *image, unsigne= d long start_pgtable) .kernpg_flag =3D _KERNPG_TABLE_NOENC, }; unsigned long mstart, mend; - pgd_t *level4p; int result; int i; =20 - level4p =3D (pgd_t *)__va(start_pgtable); - clear_page(level4p); + image->arch.pgd =3D alloc_pgt_page(image); + if (!image->arch.pgd) + return -ENOMEM; =20 if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) { info.page_flag |=3D _PAGE_ENC; @@ -244,8 +245,8 @@ static int init_pgtable(struct kimage *image, unsigned = long start_pgtable) mstart =3D pfn_mapped[i].start << PAGE_SHIFT; mend =3D pfn_mapped[i].end << PAGE_SHIFT; =20 - result =3D kernel_ident_mapping_init(&info, - level4p, mstart, mend); + result =3D kernel_ident_mapping_init(&info, image->arch.pgd, + mstart, mend); if (result) return result; } @@ -260,8 +261,8 @@ static int init_pgtable(struct kimage *image, unsigned = long start_pgtable) mstart =3D image->segment[i].mem; mend =3D mstart + image->segment[i].memsz; =20 - result =3D kernel_ident_mapping_init(&info, - level4p, mstart, mend); + result =3D kernel_ident_mapping_init(&info, image->arch.pgd, + mstart, mend); =20 if (result) return result; @@ -271,15 +272,19 @@ static int init_pgtable(struct kimage *image, unsigne= d long start_pgtable) * Prepare EFI systab and ACPI tables for kexec kernel since they are * not covered by pfn_mapped. */ - result =3D map_efi_systab(&info, level4p); + result =3D map_efi_systab(&info, image->arch.pgd); if (result) return result; =20 - result =3D map_acpi_tables(&info, level4p); + result =3D map_acpi_tables(&info, image->arch.pgd); if (result) return result; =20 - return init_transition_pgtable(image, level4p); + /* + * This must be last because the intermediate page table pages it + * allocates will not be control pages and may overlap the image. + */ + return init_transition_pgtable(image, image->arch.pgd, control_page); } =20 static void load_segments(void) @@ -296,14 +301,14 @@ static void load_segments(void) =20 int machine_kexec_prepare(struct kimage *image) { - unsigned long start_pgtable; + unsigned long control_page; int result; =20 /* Calculate the offsets */ - start_pgtable =3D page_to_pfn(image->control_code_page) << PAGE_SHIFT; + control_page =3D page_to_pfn(image->control_code_page) << PAGE_SHIFT; =20 /* Setup the identity mapped 64bit page table */ - result =3D init_pgtable(image, start_pgtable); + result =3D init_pgtable(image, control_page); if (result) return result; =20 @@ -357,13 +362,12 @@ void machine_kexec(struct kimage *image) #endif } =20 - control_page =3D page_address(image->control_code_page) + PAGE_SIZE; + control_page =3D page_address(image->control_code_page); __memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE); =20 page_list[PA_CONTROL_PAGE] =3D virt_to_phys(control_page); page_list[VA_CONTROL_PAGE] =3D (unsigned long)control_page; - page_list[PA_TABLE_PAGE] =3D - (unsigned long)__pa(page_address(image->control_code_page)); + page_list[PA_TABLE_PAGE] =3D (unsigned long)__pa(image->arch.pgd); =20 if (image->type =3D=3D KEXEC_TYPE_DEFAULT) page_list[PA_SWAP_PAGE] =3D (page_to_pfn(image->swap_page) @@ -573,8 +577,7 @@ static void kexec_mark_crashkres(bool protect) =20 /* Don't touch the control code page used in crash_kexec().*/ control =3D PFN_PHYS(page_to_pfn(kexec_crash_image->control_code_page)); - /* Control code page is located in the 2nd page. */ - kexec_mark_range(crashk_res.start, control + PAGE_SIZE - 1, protect); + kexec_mark_range(crashk_res.start, control - 1, protect); control +=3D KEXEC_CONTROL_PAGE_SIZE; kexec_mark_range(control, crashk_res.end, protect); } --=20 2.47.0 From nobody Sat Feb 7 08:55:12 2026 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 75E08200BB5 for ; Wed, 27 Nov 2024 19:04:05 +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=1732734246; cv=none; b=otFPHTMGpLd/L8NTTJR46ji1u9jZJ11I9b9+x+J60Yqao07ak+iUVWgWYWbVeGhwQkhivhkHIVySKdl1iaYSvR29Z7y4mm61iwzpO/klmzDhfQHytV1yU6udPzYb0AtuKYtEtb3stGMzgB64Y2QQq5/s7AHjCE1RC0wTlucBPKM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734246; c=relaxed/simple; bh=FW6hMMPLyhwrarG5r0mql4WfDEvwbKU+qaIi/tENEuY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Q1jDuL5iI9n7012YONPigYKUciuo+Mffx6fmukqN8CMaVwOqlUB/2NWTf8GeJ/I1Pc+sue9cRZ1ItqK58LDRxWkP7LyUPepKeLgClYE/1sWW6lSY+euv9Bt6MELbkFsjN0/8sjMdjf2OkpSRIgFuxtqW0GgHbwX+2KKi8jjBbNk= 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=QTHP0tu7; 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="QTHP0tu7" 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=0nLJBUlnC0lJdbTn9E+DJez294BNmSeUZNgs1SMEfHk=; b=QTHP0tu7JkZrCncyFGKJwfhkRm 4kgZwolFREmY9Q0yPxR6t8QU8dlxFpifkLwvbrKa4iTOsF4HkoWjTrmVvtZ+pXQmw4VhN4gw/Fm/D aCXp3rGDgv23xrXpXbvd1Z2JwBx/Z82vYYlPcE4/nr6SHk6kjpbj1kGEt7jzXJ40RfjodPFxBchB/ GqTnBWJuNaFVP5YrtpchS8naUcb6zc+n+flv0d0+pDoEX5CT731wbXbefvmVGWmWXb6FG4BdLYlez T59Kc8iH/t8IcaDSe1SRUCYYIhnfqw86AEL+bitFLVqVvI9s7TdBLODjyqM/rt/+LsZ7dxOVcPU4Z 48M+TI7A==; 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 1tGNKF-00000001Suf-188l; Wed, 27 Nov 2024 19:03:47 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKE-00000000BiY-0fVq; Wed, 27 Nov 2024 19:03:46 +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: [RFC PATCH v4 07/20] x86/kexec: Copy control page into place in machine_kexec_prepare() Date: Wed, 27 Nov 2024 19:00:21 +0000 Message-ID: <20241127190343.44916-8-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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 There's no need for this to wait until the actual machine_kexec() invocatio= n; future changes will need to make the control page read-only and executable, so all writes should be completed before machine_kexec_prepare() returns. Signed-off-by: David Woodhouse --- arch/x86/kernel/machine_kexec_64.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_k= exec_64.c index 7223c38a8708..3a4cbac1a0c6 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -301,17 +301,16 @@ static void load_segments(void) =20 int machine_kexec_prepare(struct kimage *image) { - unsigned long control_page; + void *control_page =3D page_address(image->control_code_page); int result; =20 - /* Calculate the offsets */ - control_page =3D page_to_pfn(image->control_code_page) << PAGE_SHIFT; - /* Setup the identity mapped 64bit page table */ - result =3D init_pgtable(image, control_page); + result =3D init_pgtable(image, __pa(control_page)); if (result) return result; =20 + __memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE); + return 0; } =20 @@ -363,7 +362,6 @@ void machine_kexec(struct kimage *image) } =20 control_page =3D page_address(image->control_code_page); - __memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE); =20 page_list[PA_CONTROL_PAGE] =3D virt_to_phys(control_page); page_list[VA_CONTROL_PAGE] =3D (unsigned long)control_page; --=20 2.47.0 From nobody Sat Feb 7 08:55:12 2026 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 25BB1203702 for ; Wed, 27 Nov 2024 19:04:09 +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=1732734251; cv=none; b=f2Rmka3p42qHQzY3nUvQN195LTbhvWwTuDPb24fz/ipu7jWuYLoYXUKVGEyPB4joH3MGXGFV8prSNIZVM02HSAtT+gZFayvKLhugrscbRRCylBTztrgaND7l5QcyTSyOthqHWwrSC9ASvBTstkXRhCwYWXzZwErGkRH6d3IWvgU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734251; c=relaxed/simple; bh=h9T1Us79rG5zNlNiqErNkmovUuy+vDBk3u4+TWO3n5k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=keMZ3Wp0AnqrA/PsEffHktx3vVu1XR6ZeQGDBblyMyVihgsPr98kc2Oawr+JDtEPsvKiO0jAJjrfpQAIPWXu8Ku4xmHYd8vep18+8RtrzBcAznQxvVRIfqSuYoD2ZeNC8ttOO6NyfShTml05Jg7CiuJfMTmOJU8/FNLKBgecP6g= 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=LO17THms; 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="LO17THms" 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=DHyuUfLXwZ8dNR2vFEDqrk7NrFvfuBvjwmzlKRQQKmc=; b=LO17THmsBo2+MWcMiFMpu9YNGf 84GcC+kHYVRJcCizWpCcvaRrR3r8dGfFC1Ip+RLLf/sdn+r/Gwwj93rLTl4yz6bEsl0nkTge6/7AS WlCdqCaj46+hCgpRpFCH+H1/NLUp4vMDVCEoyHnc7jw8pSbf8mbNRH7EiZAqLxO+xl46H3DClYABl sAjxm2gQUxJer4foG4c74UnWBqxCFzUQnzHsJfOohJ94/4Am8X2zqTIad9tU553JSpzPNOfpsMY2b HtP1Mwkg9qt0vozvYrkklAlFzs6ezk8RQ30LBXyazl+Qwoj2CZ+wftUWGqKjxIx711WqJfjF2XNhF 5i4KXkQg==; 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 1tGNKD-00000001cW8-0lEo; Wed, 27 Nov 2024 19:03:46 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKE-00000000Bic-0uvz; Wed, 27 Nov 2024 19:03:46 +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: [RFC PATCH v4 08/20] x86/kexec: Invoke copy of relocate_kernel() instead of the original Date: Wed, 27 Nov 2024 19:00:22 +0000 Message-ID: <20241127190343.44916-9-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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 This currently calls set_memory_x() from machine_kexec_prepare() just like the 32-bit version does. That's actually a bit earlier than I'd like, as it leaves the page RWX all the time the image is even *loaded*. Subsequent commits will eliminate all the writes to the page between the point it's marked executable in machine_kexec_prepare() the time that relocate_kernel() is running and has switched to the identmap %cr3, so that it can be ROX. But that can't happen until it's moved to the .data section of the kernel, and *that* can't happen until we start executing the copy instead of executing it in place in the kernel .text. So break the circular dependency in those commits by letting it be RWX for now. Signed-off-by: David Woodhouse --- arch/x86/kernel/machine_kexec_64.c | 30 ++++++++++++++++++++++------ arch/x86/kernel/relocate_kernel_64.S | 5 ++++- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_k= exec_64.c index 3a4cbac1a0c6..9567347f7a9b 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -157,7 +157,12 @@ static int init_transition_pgtable(struct kimage *imag= e, pgd_t *pgd, pmd_t *pmd; pte_t *pte; =20 - vaddr =3D (unsigned long)relocate_kernel; + /* + * For the transition to the identity mapped page tables, the control + * code page also needs to be mapped at the virtual address it starts + * off running from. + */ + vaddr =3D (unsigned long)__va(control_page); paddr =3D control_page; pgd +=3D pgd_index(vaddr); if (!pgd_present(*pgd)) { @@ -311,11 +316,17 @@ int machine_kexec_prepare(struct kimage *image) =20 __memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE); =20 + set_memory_x((unsigned long)control_page, 1); + return 0; } =20 void machine_kexec_cleanup(struct kimage *image) { + void *control_page =3D page_address(image->control_code_page); + + set_memory_nx((unsigned long)control_page, 1); + free_transition_pgtable(image); } =20 @@ -325,6 +336,11 @@ void machine_kexec_cleanup(struct kimage *image) */ void machine_kexec(struct kimage *image) { + unsigned long (*relocate_kernel_ptr)(unsigned long indirection_page, + unsigned long page_list, + unsigned long start_address, + unsigned int preserve_context, + unsigned int host_mem_enc_active); unsigned long page_list[PAGES_NR]; unsigned int host_mem_enc_active; int save_ftrace_enabled; @@ -371,6 +387,8 @@ void machine_kexec(struct kimage *image) page_list[PA_SWAP_PAGE] =3D (page_to_pfn(image->swap_page) << PAGE_SHIFT); =20 + relocate_kernel_ptr =3D control_page; + /* * The segment registers are funny things, they have both a * visible and an invisible part. Whenever the visible part is @@ -390,11 +408,11 @@ void machine_kexec(struct kimage *image) native_gdt_invalidate(); =20 /* now call it */ - image->start =3D relocate_kernel((unsigned long)image->head, - (unsigned long)page_list, - image->start, - image->preserve_context, - host_mem_enc_active); + image->start =3D relocate_kernel_ptr((unsigned long)image->head, + (unsigned long)page_list, + image->start, + image->preserve_context, + host_mem_enc_active); =20 #ifdef CONFIG_KEXEC_JUMP if (image->preserve_context) diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index 50cc33f2ecb7..b48bd82843fd 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -39,6 +39,7 @@ #define CP_PA_TABLE_PAGE DATA(0x20) #define CP_PA_SWAP_PAGE DATA(0x28) #define CP_PA_BACKUP_PAGES_MAP DATA(0x30) +#define CP_VA_CONTROL_PAGE DATA(0x38) =20 .text .align PAGE_SIZE @@ -99,6 +100,7 @@ SYM_CODE_START_NOALIGN(relocate_kernel) movq %r9, CP_PA_TABLE_PAGE(%r11) movq %r10, CP_PA_SWAP_PAGE(%r11) movq %rdi, CP_PA_BACKUP_PAGES_MAP(%r11) + movq %r11, CP_VA_CONTROL_PAGE(%r11) =20 /* Save the preserve_context to %r11 as swap_pages clobbers %rcx. */ movq %rcx, %r11 @@ -235,7 +237,8 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) movq %rax, %cr3 lea PAGE_SIZE(%r8), %rsp call swap_pages - movq $virtual_mapped, %rax + movq CP_VA_CONTROL_PAGE(%r8), %rax + addq $(virtual_mapped - relocate_kernel), %rax pushq %rax ANNOTATE_UNRET_SAFE ret --=20 2.47.0 From nobody Sat Feb 7 08:55:12 2026 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 2805D203703 for ; Wed, 27 Nov 2024 19:04:09 +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=1732734251; cv=none; b=A8osQiq1PGIo2h17MZHKVzwtn4r8zblt2whTHUmxQIuYswu4xD+KiiVi8KhH5nP1S30ZvWJiLDr1zxJzDvcwt/h/eoEwjsaFVSeKLBQL1hyu2U/CMa8vJvCxhYBtayi8pEHMOrJeiuYkkfYLO+Hq7AXz9doAook9TVfRW9YV5nE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734251; c=relaxed/simple; bh=RgZAS7eRpj8HVUhSNf7gSSClH/Y1bldeh41m+XiqIXE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ebKARn3f02kaG6+V+XkDzqjkF+LLa6W2IahjtjlNXbcdUrD+kcuNyuk4OXq18AYnhvDyXb0bmQ2Sofx8/UsGjUfcEfKsYwxLfmpIc9kA0j35ispTxAE+Y+0dsAFB5XmJgJGmu6kM5XZGvd25ZKeT2ghtVc95SPuSs6V1Niz8wr4= 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=Wn/uYhEd; 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="Wn/uYhEd" 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=50BPCyL9LIGHSpurJ68Tq+6jNVpqvvXZVWUSqaOyZlA=; b=Wn/uYhEdkk9I16azIWbeCOgE5Q WZ3ZbpRZetorvKyyCqcvnjFF2EPMVvOhuu0xJgvg0HMIRmTTRXV8ggqPk+2OpeCBF87jtsP35H5pN D4yn9QOCcrTSN/N+5J101xcWN8tE0EfHeht/IleOrA0nhUTyeFRBM2TV7Vp+aRF3Fk/lgSX67d+up pxJVgiCW9v3kgLFx4s5kfQk2EHv3gQhz3fV2HuydUAm6Vzu1UmyHUwNm/pdLh44SChf4YSQc7JfmX /9CCXbD0VGuWp81iMUjGzDQmeJYfxugOZz6C7GbUhc97cW6z+BrdRmLw5yPI8EsF0yS3RoPwkWsDE WSQA8HJg==; 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 1tGNKD-00000001cW9-0xjs; Wed, 27 Nov 2024 19:03:46 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKE-00000000Bih-143V; Wed, 27 Nov 2024 19:03:46 +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: [RFC PATCH v4 09/20] x86/kexec: Move relocate_kernel to kernel .data section Date: Wed, 27 Nov 2024 19:00:23 +0000 Message-ID: <20241127190343.44916-10-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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 Now that the copy is executed instead of the original, the relocate_kernel page can live in the kernel's .text section. This will allow subsequent commits to actually add real data to it and clean up the code somewhat as well as making the control page ROX. Signed-off-by: David Woodhouse --- arch/x86/include/asm/sections.h | 1 + arch/x86/kernel/callthunks.c | 6 ++++++ arch/x86/kernel/machine_kexec_64.c | 4 +++- arch/x86/kernel/relocate_kernel_64.S | 7 +------ arch/x86/kernel/vmlinux.lds.S | 15 ++++++++++++++- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/sections.h b/arch/x86/include/asm/section= s.h index 3fa87e5e11ab..30e8ee7006f9 100644 --- a/arch/x86/include/asm/sections.h +++ b/arch/x86/include/asm/sections.h @@ -5,6 +5,7 @@ #include #include =20 +extern char __relocate_kernel_start[], __relocate_kernel_end[]; extern char __brk_base[], __brk_limit[]; extern char __end_rodata_aligned[]; =20 diff --git a/arch/x86/kernel/callthunks.c b/arch/x86/kernel/callthunks.c index 465647456753..51c3e0049152 100644 --- a/arch/x86/kernel/callthunks.c +++ b/arch/x86/kernel/callthunks.c @@ -139,9 +139,15 @@ static bool skip_addr(void *dest) return true; #endif #ifdef CONFIG_KEXEC_CORE +# ifdef CONFIG_X86_64 + if (dest >=3D (void *)__relocate_kernel_start && + dest < (void *)__relocate_kernel_end) + return true; +# else if (dest >=3D (void *)relocate_kernel && dest < (void*)relocate_kernel + KEXEC_CONTROL_CODE_MAX_SIZE) return true; +# endif #endif #ifdef CONFIG_XEN if (dest >=3D (void *)hypercall_page && diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_k= exec_64.c index 9567347f7a9b..23dffdc070dd 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -307,6 +307,8 @@ static void load_segments(void) int machine_kexec_prepare(struct kimage *image) { void *control_page =3D page_address(image->control_code_page); + unsigned long reloc_start =3D (unsigned long)__relocate_kernel_start; + unsigned long reloc_end =3D (unsigned long)__relocate_kernel_end; int result; =20 /* Setup the identity mapped 64bit page table */ @@ -314,7 +316,7 @@ int machine_kexec_prepare(struct kimage *image) if (result) return result; =20 - __memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE); + __memcpy(control_page, __relocate_kernel_start, reloc_end - reloc_start); =20 set_memory_x((unsigned long)control_page, 1); =20 diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index b48bd82843fd..01138f862c59 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -41,10 +41,8 @@ #define CP_PA_BACKUP_PAGES_MAP DATA(0x30) #define CP_VA_CONTROL_PAGE DATA(0x38) =20 - .text - .align PAGE_SIZE + .section .text.relocate_kernel,"ax"; .code64 -SYM_CODE_START_NOALIGN(relocate_range) SYM_CODE_START_NOALIGN(relocate_kernel) UNWIND_HINT_END_OF_STACK ANNOTATE_NOENDBR @@ -341,6 +339,3 @@ SYM_CODE_START_LOCAL_NOALIGN(swap_pages) ret int3 SYM_CODE_END(swap_pages) - - .skip KEXEC_CONTROL_CODE_MAX_SIZE - (. - relocate_kernel), 0xcc -SYM_CODE_END(relocate_range); diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S index b8c5741d2fb4..1ff23a4bbf03 100644 --- a/arch/x86/kernel/vmlinux.lds.S +++ b/arch/x86/kernel/vmlinux.lds.S @@ -28,6 +28,7 @@ #include #include #include +#include =20 #undef i386 /* in case the preprocessor is a 32bit one */ =20 @@ -95,7 +96,18 @@ const_pcpu_hot =3D pcpu_hot; #define BSS_DECRYPTED =20 #endif - +#if defined(CONFIG_X86_64) && defined(CONFIG_KEXEC_CORE) +#define KEXEC_RELOCATE_KERNEL \ + . =3D ALIGN(0x100); \ + __relocate_kernel_start =3D .; \ + *(.text.relocate_kernel); \ + __relocate_kernel_end =3D .; + +ASSERT(__relocate_kernel_end - __relocate_kernel_start <=3D KEXEC_CONTROL_= CODE_MAX_SIZE, + "relocate_kernel code too large!") +#else +#define KEXEC_RELOCATE_KERNEL +#endif PHDRS { text PT_LOAD FLAGS(5); /* R_E */ data PT_LOAD FLAGS(6); /* RW_ */ @@ -181,6 +193,7 @@ SECTIONS =20 DATA_DATA CONSTRUCTORS + KEXEC_RELOCATE_KERNEL =20 /* rarely changed data like cpu maps */ READ_MOSTLY_DATA(INTERNODE_CACHE_BYTES) --=20 2.47.0 From nobody Sat Feb 7 08:55:12 2026 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 2E9CD203707 for ; Wed, 27 Nov 2024 19:04:09 +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=1732734251; cv=none; b=Oy7dIvbMnpi5hGckqdcLyv75O7S5QslRZgy12VozMO3m3W3dCH2x6M5Faxo45ndIDJp6CpN728p/ZGD0JV3RaN6im8JTRd1ogqaryLg86uI05e9fwR7Ku3IG6UHuB20rwAZILXFp4FvHynVQBcfD4ZRdIajzMv7hIDiQwA9v7l4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734251; c=relaxed/simple; bh=SweqAFxvSlcY1+bpXMP9GrCXeMEKwMhpkbWKkzINeek=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=N650S+3iGT7YxcrUQet8cCyaPEMjXkg/J3zbPp7LSqImQ1EZJjlVIXHQewZDxcv/zzi2YGv2jpXXwa8x/3oc5/e76cKYy/cdFcO49IkcYXHHUEAxIanKuPymAwviXq5G2dXgSm2ztHd/qRnhtZSi4rp6P1pevjrxugni6KrgJcQ= 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=la/KWTaa; 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="la/KWTaa" 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=gpANgpfLxcrLK0BYFWO4SdTCw+lDSFs1KZi61ixjGCI=; b=la/KWTaaALiK7mLgP7h/1ZVRv2 gG9zh1A0wryicYerxb6yc7VFXTArNRNhguAiTuUakt43c2HBJPjw9N5QJ8W5bSiZp1zxIs1xQHuk7 YNQhSfxX1LiE6y4SEahMiektH7dXeVILWbFCxMD5ig6pZlKKVnl2Z+PQl6fL1UJ5N3tfWkAqGTxnS BAmbeaOak7CVAoeFg8wzfEb2wJxjz3zrYcCEFAGtlvKq+VL6vqiizxyLDztI79JnR5159jFopurAQ 0Khmjn6B2pTa5RjP9cbgOwRlrYRXyOPlTbY1F4/U0Ja5i90CHApXRnETr1ztibBkmTJmWWmDM7rII EUhLru3g==; 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 1tGNKD-00000001cWA-1HtL; Wed, 27 Nov 2024 19:03:46 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKE-00000000Bil-1MWj; Wed, 27 Nov 2024 19:03:46 +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: [RFC PATCH v4 10/20] x86/kexec: Add data section to relocate_kernel Date: Wed, 27 Nov 2024 19:00:24 +0000 Message-ID: <20241127190343.44916-11-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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 Now that the relocate_kernel page is handled sanely by a linker script we can have actual data, and just use %rip-relative addressing to access it. Signed-off-by: David Woodhouse --- arch/x86/kernel/machine_kexec_64.c | 8 +++- arch/x86/kernel/relocate_kernel_64.S | 62 ++++++++++++++-------------- arch/x86/kernel/vmlinux.lds.S | 1 + 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_k= exec_64.c index 23dffdc070dd..63dca5c595f6 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -343,6 +343,7 @@ void machine_kexec(struct kimage *image) unsigned long start_address, unsigned int preserve_context, unsigned int host_mem_enc_active); + unsigned long reloc_start =3D (unsigned long)__relocate_kernel_start; unsigned long page_list[PAGES_NR]; unsigned int host_mem_enc_active; int save_ftrace_enabled; @@ -389,7 +390,12 @@ void machine_kexec(struct kimage *image) page_list[PA_SWAP_PAGE] =3D (page_to_pfn(image->swap_page) << PAGE_SHIFT); =20 - relocate_kernel_ptr =3D control_page; + /* + * Allow for the possibility that relocate_kernel might not be at + * the very start of the page. + */ + relocate_kernel_ptr =3D control_page + (unsigned long)relocate_kernel - + reloc_start; =20 /* * The segment registers are funny things, they have both a diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index 01138f862c59..469af51589ee 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -23,23 +23,21 @@ #define PAGE_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY) =20 /* - * control_page + KEXEC_CONTROL_CODE_MAX_SIZE - * ~ control_page + PAGE_SIZE are used as data storage and stack for - * jumping back + * The .text.relocate_kernel and .data.relocate_kernel sections are copied + * into the control page, and the remainder of the page is used as the sta= ck. */ -#define DATA(offset) (KEXEC_CONTROL_CODE_MAX_SIZE+(offset)) =20 + .section .data.relocate_kernel,"a"; /* Minimal CPU state */ -#define RSP DATA(0x0) -#define CR0 DATA(0x8) -#define CR3 DATA(0x10) -#define CR4 DATA(0x18) - -/* other data */ -#define CP_PA_TABLE_PAGE DATA(0x20) -#define CP_PA_SWAP_PAGE DATA(0x28) -#define CP_PA_BACKUP_PAGES_MAP DATA(0x30) -#define CP_VA_CONTROL_PAGE DATA(0x38) +SYM_DATA_LOCAL(saved_rsp, .quad 0) +SYM_DATA_LOCAL(saved_cr0, .quad 0) +SYM_DATA_LOCAL(saved_cr3, .quad 0) +SYM_DATA_LOCAL(saved_cr4, .quad 0) + /* other data */ +SYM_DATA_LOCAL(va_control_page, .quad 0) +SYM_DATA_LOCAL(pa_table_page, .quad 0) +SYM_DATA_LOCAL(pa_swap_page, .quad 0) +SYM_DATA_LOCAL(pa_backup_pages_map, .quad 0) =20 .section .text.relocate_kernel,"ax"; .code64 @@ -63,14 +61,13 @@ SYM_CODE_START_NOALIGN(relocate_kernel) pushq %r15 pushf =20 - movq PTR(VA_CONTROL_PAGE)(%rsi), %r11 - movq %rsp, RSP(%r11) + movq %rsp, saved_rsp(%rip) movq %cr0, %rax - movq %rax, CR0(%r11) + movq %rax, saved_cr0(%rip) movq %cr3, %rax - movq %rax, CR3(%r11) + movq %rax, saved_cr3(%rip) movq %cr4, %rax - movq %rax, CR4(%r11) + movq %rax, saved_cr4(%rip) =20 /* Save CR4. Required to enable the right paging mode later. */ movq %rax, %r13 @@ -83,10 +80,11 @@ SYM_CODE_START_NOALIGN(relocate_kernel) movq %r8, %r12 =20 /* - * get physical address of control page now + * get physical and virtual address of control page now * this is impossible after page table switch */ movq PTR(PA_CONTROL_PAGE)(%rsi), %r8 + movq PTR(VA_CONTROL_PAGE)(%rsi), %r11 =20 /* get physical address of page table now too */ movq PTR(PA_TABLE_PAGE)(%rsi), %r9 @@ -95,10 +93,10 @@ SYM_CODE_START_NOALIGN(relocate_kernel) movq PTR(PA_SWAP_PAGE)(%rsi), %r10 =20 /* save some information for jumping back */ - movq %r9, CP_PA_TABLE_PAGE(%r11) - movq %r10, CP_PA_SWAP_PAGE(%r11) - movq %rdi, CP_PA_BACKUP_PAGES_MAP(%r11) - movq %r11, CP_VA_CONTROL_PAGE(%r11) + movq %r9, pa_table_page(%rip) + movq %r10, pa_swap_page(%rip) + movq %rdi, pa_backup_pages_map(%rip) + movq %r11, va_control_page(%rip) =20 /* Save the preserve_context to %r11 as swap_pages clobbers %rcx. */ movq %rcx, %r11 @@ -229,13 +227,13 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) /* get the re-entry point of the peer system */ movq 0(%rsp), %rbp leaq relocate_kernel(%rip), %r8 - movq CP_PA_SWAP_PAGE(%r8), %r10 - movq CP_PA_BACKUP_PAGES_MAP(%r8), %rdi - movq CP_PA_TABLE_PAGE(%r8), %rax + movq pa_swap_page(%rip), %r10 + movq pa_backup_pages_map(%rip), %rdi + movq pa_table_page(%rip), %rax movq %rax, %cr3 lea PAGE_SIZE(%r8), %rsp call swap_pages - movq CP_VA_CONTROL_PAGE(%r8), %rax + movq va_control_page(%rip), %rax addq $(virtual_mapped - relocate_kernel), %rax pushq %rax ANNOTATE_UNRET_SAFE @@ -246,11 +244,11 @@ SYM_CODE_END(identity_mapped) SYM_CODE_START_LOCAL_NOALIGN(virtual_mapped) UNWIND_HINT_END_OF_STACK ANNOTATE_NOENDBR // RET target, above - movq RSP(%r8), %rsp - movq CR4(%r8), %rax + movq saved_rsp(%rip), %rsp + movq saved_cr4(%rip), %rax movq %rax, %cr4 - movq CR3(%r8), %rax - movq CR0(%r8), %r8 + movq saved_cr3(%rip), %rax + movq saved_cr0(%rip), %r8 movq %rax, %cr3 movq %r8, %cr0 =20 diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S index 1ff23a4bbf03..5d036fab1251 100644 --- a/arch/x86/kernel/vmlinux.lds.S +++ b/arch/x86/kernel/vmlinux.lds.S @@ -101,6 +101,7 @@ const_pcpu_hot =3D pcpu_hot; . =3D ALIGN(0x100); \ __relocate_kernel_start =3D .; \ *(.text.relocate_kernel); \ + *(.data.relocate_kernel); \ __relocate_kernel_end =3D .; =20 ASSERT(__relocate_kernel_end - __relocate_kernel_start <=3D KEXEC_CONTROL_= CODE_MAX_SIZE, --=20 2.47.0 From nobody Sat Feb 7 08:55:12 2026 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 30844203708 for ; Wed, 27 Nov 2024 19:04:09 +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=1732734252; cv=none; b=bHM29rajUadqXx650vqEwEnlSGrLQWjmNrDKumw17tE0I14C+hwih1yzs5cLuzbaHZlGjm168kHCW0QD6uPbx6jp81THhT+J9KS8zeD0qq6azXxSfcYJrr36p9DvTMNpwswO8vSmAMBlumr0sy7vNA76uI2Mi9piMNDMm/lHiOk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734252; c=relaxed/simple; bh=kUbjanEXjddFgktas16TK/9UQEactJwhG2rx0T5+mxY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qcFPoA+2+18ggegencd9l2OhR/TZNchISqyV+QPFZGnQHHVTjqzKAw4LjNAdWJj0AcqDUVt4Oe8BFIznZBu8tucGMd57rTc4ZUc8YKVxObleYWcqUxc5vJrj6LGB8etwG128pyLCXgHaI3WiMN10m0CdOCREJsBrjFjX10t7BE4= 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=ZhEQPA9v; 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="ZhEQPA9v" 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=y4llxN1150m5VJE0hmL7LsJF75dL//a2jb4zHWMLskg=; b=ZhEQPA9v058lCWo6BA0DdXpU8t 4cO4Y4bJ182spc8JTetBfGW/mk0wHAjb14b5Dyd2pyiTm6K3Co4sXcm3Hcodux6oDlzSpwgpnwwcX dY2FQKoev3uaIVEO6UVDrfo+g3eAxI2LLGefgaPijYH1BRag77BW/uYp72e4X1RurD2F9Whu8Mn0L CM+HI5E1OUZFtn0PPboNX1SWymJ8LsnXXfdeBykm+XRQbNWOTNbPzae/6AUTk9tNR3RCQ7217h5b5 jb++0Xcn0RMJg7Hupw04+UCPLXcSpFw0SYFnrg2CsHEw+XzHYVWtLeWPUGnZLOEknZUfsMNldjtie CmyB0DCQ==; 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 1tGNKD-00000001cWB-1XV7; Wed, 27 Nov 2024 19:03:46 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKE-00000000Biq-1bsr; Wed, 27 Nov 2024 19:03:46 +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: [RFC PATCH v4 11/20] x86/kexec: Drop page_list argument from relocate_kernel() Date: Wed, 27 Nov 2024 19:00:25 +0000 Message-ID: <20241127190343.44916-12-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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 The kernel's virtual mapping of the relocate_kernel page currently needs to be RWX because it is written to before the %cr3 switch. Now that the relocate_kernel page has its own .data section and local variables, it can also have *global* variables. So eliminate the separate page_list argument, and write the same information directly to variables in the relocate_kernel page instead. This way, the relocate_kernel code itself doesn't need to copy it. Signed-off-by: David Woodhouse --- arch/x86/include/asm/kexec.h | 12 ++++------ arch/x86/kernel/machine_kexec_64.c | 18 ++++++-------- arch/x86/kernel/relocate_kernel_64.S | 36 ++++++++++------------------ 3 files changed, 24 insertions(+), 42 deletions(-) diff --git a/arch/x86/include/asm/kexec.h b/arch/x86/include/asm/kexec.h index ccb8ff37fa9d..48e4f44f794f 100644 --- a/arch/x86/include/asm/kexec.h +++ b/arch/x86/include/asm/kexec.h @@ -8,12 +8,6 @@ # define PA_PGD 2 # define PA_SWAP_PAGE 3 # define PAGES_NR 4 -#else -# define PA_CONTROL_PAGE 0 -# define VA_CONTROL_PAGE 1 -# define PA_TABLE_PAGE 2 -# define PA_SWAP_PAGE 3 -# define PAGES_NR 4 #endif =20 # define KEXEC_CONTROL_PAGE_SIZE 4096 @@ -60,6 +54,10 @@ struct kimage; =20 /* The native architecture */ # define KEXEC_ARCH KEXEC_ARCH_X86_64 + +extern unsigned long kexec_va_control_page; +extern unsigned long kexec_pa_table_page; +extern unsigned long kexec_pa_swap_page; #endif =20 /* @@ -122,7 +120,7 @@ relocate_kernel(unsigned long indirection_page, #else unsigned long relocate_kernel(unsigned long indirection_page, - unsigned long page_list, + unsigned long pa_control_page, unsigned long start_address, unsigned int preserve_context, unsigned int host_mem_enc_active); diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_k= exec_64.c index 63dca5c595f6..c9fd60f8f806 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -315,6 +315,11 @@ int machine_kexec_prepare(struct kimage *image) result =3D init_pgtable(image, __pa(control_page)); if (result) return result; + kexec_va_control_page =3D (unsigned long)control_page; + kexec_pa_table_page =3D (unsigned long)__pa(image->arch.pgd); + + if (image->type =3D=3D KEXEC_TYPE_DEFAULT) + kexec_pa_swap_page =3D page_to_pfn(image->swap_page) << PAGE_SHIFT; =20 __memcpy(control_page, __relocate_kernel_start, reloc_end - reloc_start); =20 @@ -339,12 +344,11 @@ void machine_kexec_cleanup(struct kimage *image) void machine_kexec(struct kimage *image) { unsigned long (*relocate_kernel_ptr)(unsigned long indirection_page, - unsigned long page_list, + unsigned long pa_control_page, unsigned long start_address, unsigned int preserve_context, unsigned int host_mem_enc_active); unsigned long reloc_start =3D (unsigned long)__relocate_kernel_start; - unsigned long page_list[PAGES_NR]; unsigned int host_mem_enc_active; int save_ftrace_enabled; void *control_page; @@ -382,14 +386,6 @@ void machine_kexec(struct kimage *image) =20 control_page =3D page_address(image->control_code_page); =20 - page_list[PA_CONTROL_PAGE] =3D virt_to_phys(control_page); - page_list[VA_CONTROL_PAGE] =3D (unsigned long)control_page; - page_list[PA_TABLE_PAGE] =3D (unsigned long)__pa(image->arch.pgd); - - if (image->type =3D=3D KEXEC_TYPE_DEFAULT) - page_list[PA_SWAP_PAGE] =3D (page_to_pfn(image->swap_page) - << PAGE_SHIFT); - /* * Allow for the possibility that relocate_kernel might not be at * the very start of the page. @@ -417,7 +413,7 @@ void machine_kexec(struct kimage *image) =20 /* now call it */ image->start =3D relocate_kernel_ptr((unsigned long)image->head, - (unsigned long)page_list, + virt_to_phys(control_page), image->start, image->preserve_context, host_mem_enc_active); diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index 469af51589ee..16f123527406 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -34,9 +34,9 @@ SYM_DATA_LOCAL(saved_cr0, .quad 0) SYM_DATA_LOCAL(saved_cr3, .quad 0) SYM_DATA_LOCAL(saved_cr4, .quad 0) /* other data */ -SYM_DATA_LOCAL(va_control_page, .quad 0) -SYM_DATA_LOCAL(pa_table_page, .quad 0) -SYM_DATA_LOCAL(pa_swap_page, .quad 0) +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) =20 .section .text.relocate_kernel,"ax"; @@ -46,7 +46,7 @@ SYM_CODE_START_NOALIGN(relocate_kernel) ANNOTATE_NOENDBR /* * %rdi indirection_page - * %rsi page_list + * %rsi pa_control_page * %rdx start address * %rcx preserve_context * %r8 host_mem_enc_active @@ -79,31 +79,19 @@ SYM_CODE_START_NOALIGN(relocate_kernel) /* Save SME active flag */ movq %r8, %r12 =20 - /* - * get physical and virtual address of control page now - * this is impossible after page table switch - */ - movq PTR(PA_CONTROL_PAGE)(%rsi), %r8 - movq PTR(VA_CONTROL_PAGE)(%rsi), %r11 - - /* get physical address of page table now too */ - movq PTR(PA_TABLE_PAGE)(%rsi), %r9 - - /* get physical address of swap page now */ - movq PTR(PA_SWAP_PAGE)(%rsi), %r10 - - /* save some information for jumping back */ - movq %r9, pa_table_page(%rip) - movq %r10, pa_swap_page(%rip) + /* save indirection list for jumping back */ movq %rdi, pa_backup_pages_map(%rip) - movq %r11, va_control_page(%rip) =20 /* Save the preserve_context to %r11 as swap_pages clobbers %rcx. */ movq %rcx, %r11 =20 /* Switch to the identity mapped page tables */ + movq kexec_pa_table_page(%rip), %r9 movq %r9, %cr3 =20 + /* Physical address of control page */ + movq %rsi, %r8 + /* setup a new stack at the end of the physical control page */ lea PAGE_SIZE(%r8), %rsp =20 @@ -227,13 +215,13 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) /* get the re-entry point of the peer system */ movq 0(%rsp), %rbp leaq relocate_kernel(%rip), %r8 - movq pa_swap_page(%rip), %r10 + movq kexec_pa_swap_page(%rip), %r10 movq pa_backup_pages_map(%rip), %rdi - movq pa_table_page(%rip), %rax + movq kexec_pa_table_page(%rip), %rax movq %rax, %cr3 lea PAGE_SIZE(%r8), %rsp call swap_pages - movq va_control_page(%rip), %rax + movq kexec_va_control_page(%rip), %rax addq $(virtual_mapped - relocate_kernel), %rax pushq %rax ANNOTATE_UNRET_SAFE --=20 2.47.0 From nobody Sat Feb 7 08:55:12 2026 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 25AD92036FF for ; Wed, 27 Nov 2024 19:04:09 +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=1732734251; cv=none; b=rLeKWwJglhjlU3JJmNF/SDuLmGCYlydYsSa6n16Q96sBbqUZ1SLbyE3GsBu4RolF0vwjwN9xI6Zdjcy1jKjIkowgOfiDzpWyQPh6TzmAtuW2F99AOF82whrqCxL4wW0VYaHqd8ec369rTx5AS+UwBO7xihMu2QgkBJgvAPeyvPo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734251; c=relaxed/simple; bh=+pmdgCkhVTXULc+zNZF3mqNKSLVfJPhAcX/3S8Yjupo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fRywbbrCAflNlEoPC+Uw77d1uNdYTqb4lYWrLILBhqtrQDfsOOMiXH62LWRc+pzyD/RtYEPhskQf6Nc8cy7JwgQq7AnlGTeIkWOEV+umb2uB5hfFzudTT0TIINwohLIiH9ksdxYlZ/PUc9/pirJanWh66CVOvPg1JRrIgIuCqSI= 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=dE4JF530; 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="dE4JF530" 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=1uC9ZsG8+iSDsihXzKgc5JDj2Gbt5KFaLk2mOsYAE18=; b=dE4JF530jNRnvReAd0VikcoJ8O bC33xLei1yRXs1wphmdArKG6YaNTh5gsBZqUnfD9WXzUAeZqNL5dfq+g9sv6jQ8KaaNBY6a9LFX5E Qw1i2KY8nyHUVFOdjzZSmsstOhpwYK8EquiRGjbLuel2Mlu32fA1G0HLiVe6643dwj26Up2k/sDU5 d6hVznEL8ZFAPCp8cKicl864rrc/VyHi1oXghgb2RhTo8IfP6T7VF3xbCR+os5aC8HwTvukBoHPJM TdMUWQ/fFGNGDBAfEc6EgDdtaWYlJCxmiUUfl3UQL/hvpIJ0Y/LvHzSrFisX/LaTxWFtKP6tvQshH 0TXP+OgQ==; 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 1tGNKD-00000001cWD-1fSS; Wed, 27 Nov 2024 19:03:46 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKE-00000000Biu-1qv5; Wed, 27 Nov 2024 19:03:46 +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: [RFC PATCH v4 12/20] x86/kexec: Eliminate writes through kernel mapping of relocate_kernel page Date: Wed, 27 Nov 2024 19:00:26 +0000 Message-ID: <20241127190343.44916-13-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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 All writes to the relocate_kernel control page are now done *after* the %cr3 switch via simple %rip-relative addressing, which means the DATA() macro with its pointer arithmetic can also now be removed. Signed-off-by: David Woodhouse --- arch/x86/kernel/relocate_kernel_64.S | 29 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index 16f123527406..288dfc08c63d 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -61,21 +61,24 @@ SYM_CODE_START_NOALIGN(relocate_kernel) pushq %r15 pushf =20 - movq %rsp, saved_rsp(%rip) - movq %cr0, %rax - movq %rax, saved_cr0(%rip) - movq %cr3, %rax - movq %rax, saved_cr3(%rip) - movq %cr4, %rax - movq %rax, saved_cr4(%rip) - - /* Save CR4. Required to enable the right paging mode later. */ - movq %rax, %r13 - /* zero out flags, and disable interrupts */ pushq $0 popfq =20 + /* Switch to the identity mapped page tables */ + movq %cr3, %rax + movq kexec_pa_table_page(%rip), %r9 + movq %r9, %cr3 + + /* Save %rsp and CRs. */ + movq %rsp, saved_rsp(%rip) + movq %rax, saved_cr3(%rip) + movq %cr0, %rax + movq %rax, saved_cr0(%rip) + /* Leave CR4 in %r13 to enable the right paging mode later. */ + movq %cr4, %r13 + movq %r13, saved_cr4(%rip) + /* Save SME active flag */ movq %r8, %r12 =20 @@ -85,10 +88,6 @@ SYM_CODE_START_NOALIGN(relocate_kernel) /* Save the preserve_context to %r11 as swap_pages clobbers %rcx. */ movq %rcx, %r11 =20 - /* Switch to the identity mapped page tables */ - movq kexec_pa_table_page(%rip), %r9 - movq %r9, %cr3 - /* Physical address of control page */ movq %rsi, %r8 =20 --=20 2.47.0 From nobody Sat Feb 7 08:55:12 2026 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 28541203704 for ; Wed, 27 Nov 2024 19:04:09 +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=1732734251; cv=none; b=mz7WTdBCUyP7O8sSrqTbrv5fwAwVGAEjOGe0xIr3xOqbKJF9TLIgQkfiP+cqotOsoKHDR2L8j0Kpy+d4biVKRje8b0oC4jF7bCAtps8pugXMSbZaJ1Bh0msFBBL+BvJndRFBID+PpN6FrsooxRFVg7mrmZTTl43OvSjO6TQZv+c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734251; c=relaxed/simple; bh=MjVsLckdiCBNZq9iVaa/SPkl4U5dQcUJNFlUyW9YUYc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FsyM8fvcB5fdqvLRTFHrv4OssNpBzzrqOLnj0UcM/C2UBjl7nOKxmCKVKyw4SYbhmuYtwKy6JbjlkOtcmQBIvnnD79k6vBP6VnlHvP+M1RSrweZHW8FHnxmP7+53UqJrxByH8a6fllHoBvUvREhYv+C6TVrfx0ZJUg2BFK2c0EE= 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=KWl4g9w6; 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="KWl4g9w6" 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=B+cdGMrXlLMQ5gF/V+Me3rwZRfmBgngjB82bVhN99Q4=; b=KWl4g9w6uuVvVNBdyAo0iQzqrh A5Tg6tGQ1WazTsXKLaVaCAZK5fn790dTl88FZD53ZBp6NYqXaa1CNwSWi6D4hMCk5chl71FsgVbBz NhIvqpsVhLM0PPIqIqE68HbwzYi1h+X7wlvm3U5JFZi2ZQW4cgWGn110ssZvflrb8fX25GA91DDz8 bJTGaPpzlA/L3ataInSZl+Ik8/R5VSnCMfQzIw21tKcbwazADTRPYbbBTtnaSj1gNqTQIbmRnbkdE AlOCtVecMo/NL9BG05CGVWNMeEW9JNHObE19bnch2kX0u3RfN6wbOtCPcQyRCezmcEfJVrlawkz7G etN4e9kw==; 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 1tGNKD-00000001cWE-1s5a; Wed, 27 Nov 2024 19:03:46 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKE-00000000Biy-205D; Wed, 27 Nov 2024 19:03:46 +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: [RFC PATCH v4 13/20] x86/kexec: Clean up register usage in relocate_kernel() Date: Wed, 27 Nov 2024 19:00:27 +0000 Message-ID: <20241127190343.44916-14-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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 The memory encryption flag is passed in %r8 because that's where the calling convention puts it. Instead of moving it to %r12 and then using %r8 for other things, just leave it in %r8 and use other registers instead. Signed-off-by: David Woodhouse --- arch/x86/kernel/relocate_kernel_64.S | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index 288dfc08c63d..b24198eb1fe9 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -79,24 +79,18 @@ SYM_CODE_START_NOALIGN(relocate_kernel) movq %cr4, %r13 movq %r13, saved_cr4(%rip) =20 - /* Save SME active flag */ - movq %r8, %r12 - /* save indirection list for jumping back */ movq %rdi, pa_backup_pages_map(%rip) =20 /* Save the preserve_context to %r11 as swap_pages clobbers %rcx. */ movq %rcx, %r11 =20 - /* Physical address of control page */ - movq %rsi, %r8 - /* setup a new stack at the end of the physical control page */ - lea PAGE_SIZE(%r8), %rsp + lea PAGE_SIZE(%rsi), %rsp =20 /* jump to identity mapped page */ - addq $(identity_mapped - relocate_kernel), %r8 - pushq %r8 + addq $(identity_mapped - relocate_kernel), %rsi + pushq %rsi ANNOTATE_UNRET_SAFE ret int3 @@ -107,8 +101,9 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) /* * %rdi indirection page * %rdx start address + * %r8 host_mem_enc_active + * %r9 page table page * %r11 preserve_context - * %r12 host_mem_enc_active * %r13 original CR4 when relocate_kernel() was invoked */ =20 @@ -161,7 +156,7 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) * entries that will conflict with the now unencrypted memory * used by kexec. Flush the caches before copying the kernel. */ - testq %r12, %r12 + testq %r8, %r8 jz .Lsme_off wbinvd .Lsme_off: --=20 2.47.0 From nobody Sat Feb 7 08:55:12 2026 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 B83F1200119 for ; Wed, 27 Nov 2024 19:04:04 +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=1732734246; cv=none; b=fu/ND5UAxpow73Zvx9y8H7lV2x9ta7ervxzIDF64wNa+q4fNvMI/PWZmaDq09Cw30GOugEaTJsGTxp7Lpts7qAkAXxJ+rsaf6UeYVIog9watEFis0A3wL/OYQ9lJ2B7l4ZS5XkvmaXa7WxasVN288NQnP4KMJsL0oYGZk4T/fPk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734246; c=relaxed/simple; bh=HZMzJO0ERgKBSdgHCinISLLLJ2P1/lAUKPmv9/37Fgs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SaxrlgbeORZgfHfePh+32JWfXI1+tZyyiZd069EMUI+ks3zJaxJ9yHk/aQi9g7VtvT8YciEzcder6pOJJ7RBPJ7nGIAmdGeDNOJrywslwnoyWFWGw+J2Kf80nZy+Bu4oFqQrUJgQwI0vhuxQesq/cQ7IDWH5fMpYi4TLsudMFP8= 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=VrHBbErr; 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="VrHBbErr" 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=Bu60bcDvZzloSAXCkbCegV/lwySuet3eE7e1Gl1k8gk=; b=VrHBbErrDgCmTebG6pTRQKsfBK ivsQF0WiwGKvaxQZGgicLI185iWwM2QVj8/ROA7y2WScYLTbph0q18oXEO2OatPjYIhgSanOUz7HX Svp77FKyy3Kf4VVhhhBsOVQjBo4Cy9312AyQtAUhhN+oRmOrHI4xzvnR5leyRr/JvIh7Vf3zHLqIb vGKcgWfxCi/LpZfXsivlNV1T4PK/JQfQmQ2hnX8Oefd10fC+dPDNpOstaoL2ngUnVL3VFrd5EL8Nw YsuNGbO0n+yomlabePM85oSU8+FQFF4e62vDjOOk+KckKN3na2wgx+sAqhQKBS6jF2phYum+dJiYd Dkc8Q6BA==; 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 1tGNKF-00000001Sug-18A7; Wed, 27 Nov 2024 19:03:47 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKE-00000000Bj2-28rJ; Wed, 27 Nov 2024 19:03:46 +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: [RFC PATCH v4 14/20] x86/kexec: Mark relocate_kernel page as ROX instead of RWX Date: Wed, 27 Nov 2024 19:00:28 +0000 Message-ID: <20241127190343.44916-15-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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 All writes to the page now happen before it gets marked as executable (or after it's already switched to the identmap page tables where it's OK to be RWX). Signed-off-by: David Woodhouse --- arch/x86/kernel/machine_kexec_64.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_k= exec_64.c index c9fd60f8f806..9232ad1562c8 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -323,7 +323,7 @@ int machine_kexec_prepare(struct kimage *image) =20 __memcpy(control_page, __relocate_kernel_start, reloc_end - reloc_start); =20 - set_memory_x((unsigned long)control_page, 1); + set_memory_rox((unsigned long)control_page, 1); =20 return 0; } @@ -333,6 +333,7 @@ void machine_kexec_cleanup(struct kimage *image) void *control_page =3D page_address(image->control_code_page); =20 set_memory_nx((unsigned long)control_page, 1); + set_memory_rw((unsigned long)control_page, 1); =20 free_transition_pgtable(image); } --=20 2.47.0 From nobody Sat Feb 7 08:55:12 2026 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 B7EBA1FF604 for ; Wed, 27 Nov 2024 19:04:04 +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=1732734246; cv=none; b=VrW9yPi869RjXLvBj0ZNWdf559F44RtaVWo2Qlcfu8A4Yk+M+xX60MDpFt5UQbJGEnRI/gfjFuc6LAupx0+eYPha2BVS6ZtXGw15fJuuMHBVK3b6aDlMuiH99HtxTzm9gJuMenTIHFNUZ/CRZxfW3DZ18qRBXYyaLTezaVZj8IM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734246; c=relaxed/simple; bh=4G1dwuKi5j5hukJsKtHQTnOK312oh/BUWKz5OC4SG7Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FDx1dnhS3/c0TSxsMJdu8B3hXjh+7vT6ZAmBK15uwLpvf/47Jxvrtw2AcCOAN2js5mS0uzdyg2VJ4RXuTDoX/S2neaN0/w/9grxPc+/yQW8UB7XjUxWULH4VnqxcVCt5+AeubIDpOly4gP5URDK6HbvCN5T2gOqNEZsa0qSpWwM= 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=cIpbPcdh; 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="cIpbPcdh" 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=n1NxLTesaxf1CmVmrYGt0DtpmfeNYvqyA55PeXFrYQI=; b=cIpbPcdhiv/PxCBrA0udVBYnUb jd6ot3jhec8XiieDBZTWAPnUeNeFCD3UDuRMG/y2mzrEK75n29vfsgS4GobkTq6RnUrhNjNtj1I6N 1C5cf7nVS3T+bi1e/pByQuKlAq/K1uUE0iJJ4bQKsLBTLQYjkSiw4SlMv0MFdDxd64aJVrVfe5W3a xbrr6uSzejIarUWPhENLx4HgcL7MehRPW+i3qC/PohAnOjFBjel5Ga4+FZEvtjVDid1ViAdV0z3NV 7ZPnp732MaB2zlOkhuHr0WeExPfHFaP5LHXzumzFo2aq2FB2r1+KJmtEX2ocPPkJ70qPDNaY/OFsN 4sxXSfVg==; 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 1tGNKF-00000001Suh-1855; Wed, 27 Nov 2024 19:03:47 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKE-00000000Bj6-2Qt6; Wed, 27 Nov 2024 19:03:46 +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: [RFC PATCH v4 15/20] x86/kexec: Add CONFIG_KEXEC_DEBUG option Date: Wed, 27 Nov 2024 19:00:29 +0000 Message-ID: <20241127190343.44916-16-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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 does nothing yet. Signed-off-by: David Woodhouse --- arch/x86/Kconfig.debug | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/x86/Kconfig.debug b/arch/x86/Kconfig.debug index 74777a97e394..9dde32865a9b 100644 --- a/arch/x86/Kconfig.debug +++ b/arch/x86/Kconfig.debug @@ -56,6 +56,14 @@ config EARLY_PRINTK_USB_XDBC You should normally say N here, unless you want to debug early crashes or need a very simple printk logging facility. =20 +config KEXEC_DEBUG + bool "Debug kexec transition" + depends on KEXEC_CORE && EARLY_PRINTK + help + Faults during kexec can be difficult to debug. This installs exception + handlers and attempts to report faults through the same serial port as + used for early_printk. + config EFI_PGT_DUMP bool "Dump the EFI pagetable" depends on EFI --=20 2.47.0 From nobody Sat Feb 7 08:55:12 2026 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 2B42C201255 for ; Wed, 27 Nov 2024 19:04:05 +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=1732734247; cv=none; b=rHMrIurQYaIygoqPYAo7Rfbe6jSVV/8rMob/mY0Af80GWATLf8X3TDkcJhZHNE6TlyBCg45XrrU7MmwLzUew9etCsQALIC8y8KKxiz93waOlWgIbOt1h+UdSrD68O93QvHYk60Pi8UkkHeeuvxtyTmJF88XLihaBLNXYbp+xbEI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734247; c=relaxed/simple; bh=623G9e0jeZ2RMFMd1YHMdaz5Es5e+KKYpgx8j02dSxU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YHDuC2QNFbsQbwpF2D7bLl02+GBXXajpMz51jBdrXqXuzpzX2z6d3f6OybdE7XTOvsl0OAhvvziBMZmu9MP2fwQ1lSuyoI6nm4iHUz6Y09wUrQxVP8CGM+848ieumX4Xm/fQc7KUo8L7brPCFGT6Tle8MJgSgD3l81F+oV5+rGw= 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=Vi6zJACz; 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="Vi6zJACz" 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=J05THXe+YRacmOzA8KfGrmyEonAyJGQ1gSBRrJydwjs=; b=Vi6zJACzVw41uFSjDRal8XnX3X OlG6Ex20Hgc+bEduVJcdQhYqUgvCcd31ZBngWdAAvvpGtoL7z+LazP4EB8D7ErYWUjbQJaH1OYd/V 9PoMEp9vXcfJN8p33AHUYf2I16NT2p2iN6JOg7RUTI4NYEdiINrKhClES9K5wqdrV6xL/IRnFSSna XypqcMPNm5wfWLO0x9JM8JfHwDb+1QMwB/327Q+AVsGNcw3Rf5lw/8oQ4Ujl2j5yhGIS24VEd4Eyu Ivb9S2lMHbWZL4Ad3kj0ZfBk4EWHDYXbBIB9295YhutnPxsjOWD0QgIQkIMYcSaVL09epqmZDO1p3 vbCR9vmg==; 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 1tGNKF-00000001Sui-17Li; Wed, 27 Nov 2024 19:03:47 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKE-00000000BjB-2caS; Wed, 27 Nov 2024 19:03:46 +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: [RFC PATCH v4 16/20] x86/kexec: Debugging support: load a GDT Date: Wed, 27 Nov 2024 19:00:30 +0000 Message-ID: <20241127190343.44916-17-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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 There are some failure modes which lead to triple-faults in the relocate_kernel function, which is fairly much undebuggable for normal mortals. Adding a GDT in the relocate_kernel environment is step 1 towards being able to catch faults and do something more useful. Signed-off-by: David Woodhouse --- arch/x86/kernel/relocate_kernel_64.S | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index b24198eb1fe9..6c6bfdccfe6a 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -39,6 +39,18 @@ 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) =20 +#ifdef CONFIG_KEXEC_DEBUG + .balign 16 +SYM_DATA_START_LOCAL(kexec_debug_gdt) + .word kexec_debug_gdt_end - kexec_debug_gdt - 1 + .long 0 + .word 0 + .quad 0x00cf9a000000ffff /* __KERNEL32_CS */ + .quad 0x00af9a000000ffff /* __KERNEL_CS */ + .quad 0x00cf92000000ffff /* __KERNEL_DS */ +SYM_DATA_END_LABEL(kexec_debug_gdt, SYM_L_LOCAL, kexec_debug_gdt_end) +#endif /* CONFIG_KEXEC_DEBUG */ + .section .text.relocate_kernel,"ax"; .code64 SYM_CODE_START_NOALIGN(relocate_kernel) @@ -112,6 +124,21 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) /* store the start address on the stack */ pushq %rdx =20 +#ifdef CONFIG_KEXEC_DEBUG + /* Create a GDTR (16 bits limit, 64 bits addr) on stack */ + leaq kexec_debug_gdt(%rip), %rax + pushq %rax + pushw (%rax) + + /* Load the GDT, put the stack back */ + lgdt (%rsp) + addq $10, %rsp + + /* Test that we can load segments */ + movq %ds, %rax + movq %rax, %ds +#endif /* CONFIG_KEXEC_DEBUG */ + /* * Clear X86_CR4_CET (if it was set) such that we can clear CR0_WP * below. --=20 2.47.0 From nobody Sat Feb 7 08:55:12 2026 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 31AAF203709 for ; Wed, 27 Nov 2024 19:04:09 +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=1732734251; cv=none; b=nDMZGxT66eCRBntfaEpv9+PqdkmnBojx3+Xp/RsZrryDHIS3MasnKPbHGn7Q1btMr203kHnvvdXmVp1uZYZRvefcqJVnqy4T4GB0GkMdxpEcuJdzyo2CLdFi5+ip/CtbjTiBDsFS5RToFxr1iWLhSgT5NtrBcRVFLNZb0I2Zki4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734251; c=relaxed/simple; bh=N1lnkJP7eh1x4zNxsuAjBfSiDW9J3oCxh6NldftiY3A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pGQFy5ZZNG+BvVid3XoR06WqUW+xVeAmTNYfkok77rUoz4+uAxzzHDmDcL2nx3lmbPKTng5lBwCYJx0qTCmChkcojg67q0oI+WnxQ1SU/ZiuL7dY6Z/hhIPbW/O18sqAhYfn0wUbEw6rKWsC4J3/JmgZw1BESzSYo1I07bs6YMM= 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=sR1O/exR; 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="sR1O/exR" 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=nloRe6XBn3UjxvOHw+WXz1+0xd962X4eY+Ev2n27Wpg=; b=sR1O/exRqDSW4EZJLvxXsVy5oK E1g6MODbE9QFttjX2KRTN+g3wIt2FCmVUoldQOlRe4INnjW/NriWIDcX0e0C5rSQMdewl9Zv+30NS vKcUXG5eYQeRO3igYxmolfkbHyrmFjLCviyMaVjDcfd8I0k0x8dV2kcGIG2Vqkmpnn8IrcuXb1GwE QUCJVyNN3Wh4Lt4bwrTDpYTtKl05Rvm9zfm/A/eMpxFInNAYpT1E9i9uW/zZIBnHwzHfVhRNVNCHg jW7FMThoQdpYhDf97zQWu8S2fUHufJ6/d1IoTAf+6OpwU/+E4Yry78IPmvpnEQT32VwZf5+jAKCfQ QGh4WLbQ==; 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 1tGNKD-00000001cWH-2dYs; Wed, 27 Nov 2024 19:03:46 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKE-00000000BjG-2oPa; Wed, 27 Nov 2024 19:03:46 +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: [RFC PATCH v4 17/20] x86/kexec: Debugging support: Load an IDT and basic exception entry points Date: Wed, 27 Nov 2024 19:00:31 +0000 Message-ID: <20241127190343.44916-18-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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/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 48e4f44f794f..c14b0a2704c0 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 9232ad1562c8..f9a68c6ec815 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 6c6bfdccfe6a..2179f691a45b 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"; @@ -108,6 +114,11 @@ SYM_CODE_START_NOALIGN(relocate_kernel) int3 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 /* @@ -137,6 +148,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 /* @@ -346,3 +366,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 From nobody Sat Feb 7 08:55:12 2026 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 B7F2A1FF7BC for ; Wed, 27 Nov 2024 19:04:04 +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=1732734247; cv=none; b=XOUpvbAUJ/p0aWqaCrBWIwFWpWCqXOYGokfBqMwB6qyIu97h5RRFQlyf3pJLcQUjKlfe7N8mhLWIBMPHQEA2T9RXFOzFjzXW1q48JlxYU+SGt895KxoOf7kYVrHpDQGdOI280gYxpq/1vpCHjzpjpYNBKCZLukMH4fKPePKDtvY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734247; c=relaxed/simple; bh=Uw3Y4djbQj+oGnM4V8NZ8aD+cLeBaycYn3PSbrmSV2I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CHrwqA/npleMUt+RIDx9r7AU9E/lXFd3gnMMKtBoFGyiw2u/vyKVsyGt+39XcGCF08M6Br+Dj1gxDVlss9x1FyddDMIv4rin+Fn2beJAUXDHeJE2/nzefAhE21XvjC+Ud2LOp2Alk5aYHEPrJFHOKOp0b3boi71gmCAgf0ib9Jk= 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=Mj7+sGdO; 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="Mj7+sGdO" 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=HZSTsH9jPmSjfGOA2jGMGLovn1Q55JGddszKWUZDjoo=; b=Mj7+sGdOscCDZGphhrrgLTyyRk 7E73fFbQ2jcn6CZoAgC0onGv6QK4VraOTWFd96mO4tuK6p6Hlgt/Tl6ubX4wBmA8JTHJ5gg6s0tXe 9yYa0ri6NTzuD6toBOGPhGbmC9VIcxIIf9SI7t+Ju4iXNGpdjnCoTTvKNlXRWE9fQyzI8ZpejSSgn 4NHRUIjIdJFIULjoZE4++QNyE7eJ/fh52K/QKattt5Szn58t1Ox/cF5Y1y149MAUIGcuVo0BdLtjM 3EfVZKBn21gdgtyBS3AAioYGlcu7V+VN+nS94gHG3+vCluiLyjWIm1HAmT67hW7ZbjDMNSt6BmbFN OUkCPVAA==; 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 1tGNKF-00000001Suj-17gP; Wed, 27 Nov 2024 19:03:47 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKE-00000000BjK-2yJ6; Wed, 27 Nov 2024 19:03:46 +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: [RFC PATCH v4 18/20] x86/kexec: Debugging support: Dump registers on exception Date: Wed, 27 Nov 2024 19:00:32 +0000 Message-ID: <20241127190343.44916-19-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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 | 104 ++++++++++++++++++++++++--- 1 file changed, 96 insertions(+), 8 deletions(-) diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index 2179f691a45b..cd349b6d34a7 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -114,11 +114,6 @@ SYM_CODE_START_NOALIGN(relocate_kernel) int3 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 /* @@ -368,6 +363,68 @@ SYM_CODE_START_LOCAL_NOALIGN(swap_pages) SYM_CODE_END(swap_pages) =20 #ifdef CONFIG_KEXEC_DEBUG +/* + * 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_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 @@ -408,11 +465,42 @@ SYM_CODE_END(kexec_debug_exc_vectors) =20 SYM_CODE_START_LOCAL_NOALIGN(exc_handler) pushq %rax + pushq %rbx + pushq %rcx pushq %rdx - movw $0x3f8, %dx - movb $'A', %al - outb %al, %dx + pushq %rsi + + /* Set up %rdx/%rsi for debug output */ + pr_setup + + /* rip and exception info */ + print_reg 'E', 'x', 'c', ':', 40(%rsp) + print_reg 'E', 'r', 'r', ':', 48(%rsp) + print_reg 'r', 'i', 'p', ':', 54(%rsp) + + /* We spilled these to the stack */ + print_reg 'r', 'a', 'x', ':', 32(%rsp) + print_reg 'r', 'b', 'x', ':', 24(%rsp) + print_reg 'r', 'c', 'x', ':', 16(%rsp) + print_reg 'r', 'd', 'x', ':', 8(%rsp) + + /* Other registers */ + print_reg 'r', 's', 'i', ':', (%rsp) + 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 + + popq %rsi popq %rdx + popq %rcx + popq %rbx popq %rax =20 /* Only return from int3 */ --=20 2.47.0 From nobody Sat Feb 7 08:55:12 2026 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 B7F7F1FF7DF for ; Wed, 27 Nov 2024 19:04:04 +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=1732734246; cv=none; b=A5BYwTolX+qqr3RV6G4QzWDiDGUpU1DA7mIGnGredpTUl1aw+HMjj8QRIlwssjE0heXtrZNOL4WbSbms3LqlJUPYAQqqQr9jeQ0ri4ids3YHwXjmw+A4J8fcUYqh/DZIZmpk9oxbJYOgu6pPH1M4VFMeXu714t4xnraCZ5DF3G8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734246; c=relaxed/simple; bh=QJxzlm/nn8Yq8Tsj0Zor4e8z8UaVsFNQs4U9DwetKYs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BLoeigsdjoKiI6TAjjTiKasL2yiAywdl1eUJYYXML+6K/JlJB9zbauulBxXIFs73h2GdyvCzkJTzecaKVGLCk9/d+z0zTftUuPOnnJEiQqRsFmebIzxJtCfy/jutT1ID87k0mNdHJu+i2tmGpn8u+IrumMdVAjw8EBCH+0O6rAc= 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=j+btQi7C; 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="j+btQi7C" 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=uDB0B/y8BEIJAdSME/L53tegScvP1ehfXftaZ5ENwBE=; b=j+btQi7CSOsAF3Gyz7jFumIZOU KeEFCMmvjqMLuQeNRJTuchluRVGy+eur5LdE4UJbyXwOEadgZN4KTqsD9XB8/0sWMQUNHyiryNi3A 7jJXxoMJ/YJ001YgGCPGwHbk77IVw98cOSx1+4Hiy6+5FVhKveEmtSE15ambQzMRPNuMequKw7Aat CN4vqpQtTBR4H9bYJECES/lA6Je9LFsA5OFaN3G70abwOHTFpw6Krw3pNxbxDQrAc1pkrFzDk8TLO 6JiMbTb6amoMjaHor7IeII2h42eBoV7oegiM316Le4bAJ33sUTu83mFkF7p/qsxFmk7GBQ8o5myql gdoEcNeQ==; 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 1tGNKF-00000001Suk-17q2; Wed, 27 Nov 2024 19:03:47 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKE-00000000BjP-38M8; Wed, 27 Nov 2024 19:03:46 +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: [RFC PATCH v4 19/20] x86/kexec: Add 8250 serial port output Date: Wed, 27 Nov 2024 19:00:33 +0000 Message-ID: <20241127190343.44916-20-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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 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 | 37 +++++++++++++++++++++++----- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/kexec.h b/arch/x86/include/asm/kexec.h index c14b0a2704c0..f37d2d9fda3f 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..bf06866ee90a 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); + +#ifdef CONFIG_KEXEC_DEBUG + 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 cd349b6d34a7..01a31e4a0664 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -38,6 +38,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 #ifdef CONFIG_KEXEC_DEBUG .balign 16 @@ -364,23 +365,47 @@ SYM_CODE_END(swap_pages) =20 #ifdef CONFIG_KEXEC_DEBUG /* - * 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 + 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_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.47.0 From nobody Sat Feb 7 08:55:12 2026 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 B7E4F1FE45C for ; Wed, 27 Nov 2024 19:04:04 +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=1732734246; cv=none; b=G+AwEVGvv8Zcu5Dk1bEV1HD8/O7M056KLNfYwBGpa0oMUnBXqabb+Ua6wJjTCSpCDeOzJe4mcDKUigNrO5axnEZOKImP0wN42BVW6sQS2L8fp7YDVI3/35EcDJkMEFCekmREOlTlZhhKo0VRC+YcEtdhAHBRevmSpn9LQn2yn6U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732734246; c=relaxed/simple; bh=D+JloHBMt0oC8r70FErtjAFnRxiGxH8EaihhtPNVCeI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uJmomyjQDBljsuFJ0a1/JASVU2CY6VOKxQD5+i6tvSzEQCkhR3ZhXb7yHJAKfV3ZitlZPO9jA5ZL6p3JH5PquO/sR4/7AEtENDgAdvy1EGHLkpV/TuSNsaC5FUzjUjMOAfCOqrg5aMT/fBbtvK+RVA4QnZJpdxqgQ6249im7xTE= 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=c6yI3hXu; 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="c6yI3hXu" 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=r3AYqthkhqE/hpsX8OlsrIm8YbRRATEjfUq8IVBi2fg=; b=c6yI3hXuTHaVrvxd1yVnHBVNij 96A37jM/iCQUDKVKAlMQN8W8+Yr5+O/MCkQEvvGDHyzlceX95tnVQ54UTGjTo95FsiCzt8tHlqxjc c7qG6T1XRoLA4sUABomX1yNBJDmi7TQO5p1nmcSXdtWaeC40ulDx7Om9isCCmryt1PDt9CFinKN6D unqbwGZsLVdRZlU1D5ss+qnhvrbroS/4iJgXIL6lrH23QI6ZibbR1wH16QYsNmVFcgNrYbypb3sVK etkrF/ftkKgYc/ACKnfRrGfJFJ1B6aUf/mUu/cX/rfcI0BSjIRQHATtN5B0LP33wtqiSTfY2TMOKx wN42K1VA==; 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 1tGNKF-00000001Sul-17l5; Wed, 27 Nov 2024 19:03:47 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tGNKE-00000000BjT-3JcL; Wed, 27 Nov 2024 19:03:46 +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: [RFC PATCH v4 20/20] [DO NOT MERGE] x86/kexec: Add int3 in kexec path for testing Date: Wed, 27 Nov 2024 19:00:34 +0000 Message-ID: <20241127190343.44916-21-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127190343.44916-1-dwmw2@infradead.org> References: <20241127190343.44916-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/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 01a31e4a0664..ff8a813a9f9b 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -152,7 +152,7 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) lidt (%rsp) addq $10, %rsp =20 - //int3 + int3 #endif /* CONFIG_KEXEC_DEBUG */ =20 /* --=20 2.47.0