From patchwork Wed Jun 7 07:23:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104300 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp75288vqr; Wed, 7 Jun 2023 00:37:02 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4NU6otY4bBD7O+UzOJk8p37tmP/1c7pR+k9Qv4jWWL+VtgWrsMZ1CdVOWMPpzPZwuJmo8F X-Received: by 2002:a05:6a20:12cf:b0:118:5d5a:f26d with SMTP id v15-20020a056a2012cf00b001185d5af26dmr250012pzg.59.1686123422718; Wed, 07 Jun 2023 00:37:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123422; cv=none; d=google.com; s=arc-20160816; b=m9sCkJgVFKwjt0OZGzGO0BgaRp/j0kh7x0mUTUtOyaKQUPXAE6y3uRHfKZfwJIqhav 8vGtmWAIl+cGHBNM/N2tj0+MhoYp70sKMKEygYQPltDljOMqd9JhwbZNWQNa79Uum+Al SaPH7iYFP9inaV8Ehk+ZsCW155La44GaqCi2icX7Sp2vM5ZTjSZFhxoySHusvSu6jyU2 Xak5Nn8W+46wxHIGkHxfAjMqEuWd8pvo7Hvoo6l3JX1SP/7+Fvk9kkX96wjNtr9epgg9 GwxTMBc3uKi6lVY6kJlPhPKwidZ7iqfKAFwLLQZk+DA169vsNnIIJvGKboJbNcfSi0Ol +UqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=u8IvhatF++wRhzyU5M2j0CfkceCNheQ6ZML+xgLkmTo=; b=TSb3abhX37OdDB1LzAg5hIg6lbXqNckSEJQhIhRyVhWlE/P/5V/CFl1agO20yyCWyr ioTU9Jh54+YlQGt1Zb9tRmHRaK1zQTu5C1EJI1/wKEds60HqDjOKpn8usQif3iNgVxKx ClxltQFkqdmCV3oJRc8a1TexuBtO8KepN4XeTnY4gJGYw4FdI3HJrqqYTFeIGW3cvPNq 4WnLqdcMROvYF1JgqPPsUu8pNTs2jpjdlMeH5wHQuUOXkYWdESXPSJOT5z9JXcSX9SB0 8NfcB+z8SxpSIwg3NfVQwJa3/R5GWyh2mw5N1NwY7cXjcsxDOXEx7fxJFECnXloYy2Y+ a3sQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=pWwvL1E8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 5-20020a17090a018500b0024e29660f61si724444pjc.90.2023.06.07.00.36.49; Wed, 07 Jun 2023 00:37:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=pWwvL1E8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239170AbjFGH01 (ORCPT + 99 others); Wed, 7 Jun 2023 03:26:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239160AbjFGHZx (ORCPT ); Wed, 7 Jun 2023 03:25:53 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B4B891FED; Wed, 7 Jun 2023 00:24:45 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 2A0ED63542; Wed, 7 Jun 2023 07:24:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BE859C433A0; Wed, 7 Jun 2023 07:23:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122640; bh=0wgPR8cosROASbxhizaWEh3zQ4j+H/DEB9t5hilAPDo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pWwvL1E8pkq/VHu7SKqTWzB5IsBgx4UbU58H2oaGOtkSndAPzouOdgkZ46eMTGO8L gekBQ9RF8fjOVy+m7hXAmWVgYCLKNQyFbVaddTwSCU5Iaw78exac+o0Fxrngck8VEc ikpqE/Jx6aG9KdVljqYBO3V6TQm5FPF23NdVe/IpMZup1d7Ce1xg267r9l4UuhH1ve JOk8S+A0T/E87x2VpzCaDSSvkKaJ0UE9KIbtkoN29JU27f2wOxKpkaa+FK/0zdfSwT OFsElmTqK0gQOdinmyHj8imQUwnxS4YL8LGxYnmZX2cbT+5mZFO2boaudaPomPzhfY gaDmlKOTzxHwg== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 01/20] x86/efistub: Branch straight to kernel entry point from C code Date: Wed, 7 Jun 2023 09:23:23 +0200 Message-Id: <20230607072342.4054036-2-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1927; i=ardb@kernel.org; h=from:subject; bh=0wgPR8cosROASbxhizaWEh3zQ4j+H/DEB9t5hilAPDo=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBILVkwumLTU3H5j/cIH/66+vkV6UeBj/PLN5wZM6TP 89vcTMLdpSyMIhxMMiKKbIIzP77bufpiVK1zrNkYeawMoEMYeDiFICJ3GRi+B+p8rz94KHqOPOD idd2cLMx/C6+sVndsE9l34VujQ92byMZ/inqvZv5UkxSInIp75QN//crGk/65nbjcTpnhXpMZYS cITcA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028554030479389?= X-GMAIL-MSGID: =?utf-8?q?1768028554030479389?= Instead of returning to the calling code in assembler that does nothing more than perform an indirect call with the boot_params pointer in register ESI/RSI, perform the jump directly from the EFI stub C code. This will allow the asm entrypoint code to be dropped entirely in subsequent patches. Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/x86-stub.c | 21 ++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c index 220be75a5cdc1f4c..d4a730f053bdcbfa 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -803,10 +803,19 @@ static efi_status_t exit_boot(struct boot_params *boot_params, void *handle) return EFI_SUCCESS; } +static void __noreturn enter_kernel(unsigned long kernel_addr, + struct boot_params *boot_params) +{ + /* enter decompressed kernel with boot_params pointer in RSI/ESI */ + asm("jmp *%0"::"r"(kernel_addr), "S"(boot_params)); + + unreachable(); +} + /* - * On success, we return the address of startup_32, which has potentially been - * relocated by efi_relocate_kernel. - * On failure, we exit to the firmware via efi_exit instead of returning. + * On success, this routine will jump to the relocated image directly and never + * return. On failure, it will exit to the firmware via efi_exit() instead of + * returning. */ asmlinkage unsigned long efi_main(efi_handle_t handle, efi_system_table_t *sys_table_arg, @@ -950,7 +959,11 @@ asmlinkage unsigned long efi_main(efi_handle_t handle, goto fail; } - return bzimage_addr; + if (IS_ENABLED(CONFIG_X86_64)) + /* add offset of startup_64() */ + bzimage_addr += 0x200; + + enter_kernel(bzimage_addr, boot_params); fail: efi_err("efi_main() failed!\n"); From patchwork Wed Jun 7 07:23:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104306 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp75610vqr; Wed, 7 Jun 2023 00:37:43 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4gOGy54HPxkZuRxz1bhGHRYlHtwjcAyBXfS8gER8B+fnm2rOH3j8LA9+MPS58JtD1P+oKP X-Received: by 2002:a05:6808:9ac:b0:39a:a146:8bf1 with SMTP id e12-20020a05680809ac00b0039aa1468bf1mr4940487oig.3.1686123463630; Wed, 07 Jun 2023 00:37:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123463; cv=none; d=google.com; s=arc-20160816; b=lHKRiWU7bfeZCKb97ZUSNF5RllC+6mhpoI3qy2mAlZPbyguXwUweal2NPpBGNVQuES Y7c0hMbM1qQ6JMOVRgbrZUY6r2BnRQhtl44UKNObZFeK0vNm7e7C76QKkU2tACXgXtt5 zLBOYpAY4aE6lBoVkEQfDfskT2HjfOaNNJWkfsBXdNcublPNQz1jmyURb78LOcvDec/A bVyZzQdSXdzzYqHXjDVbRV0fQF0Vpe3SimEN3d0m7a52DviwAXOHehGbCKYogFaaYG9e ezmOTNuNY8vrUr6V+D9S0+UR3eOe2rDlQTW8Ql9MGsrBwi9WrLxd6Q1Y4zeSn+BLdD9i dCYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=bQnC4cBm6/Kqw55cMYddOy2m5QCOrzk3S/3UjHiOaLQ=; b=MlkRZFSp1ZXkuqF4P/mZdvwoFrrou8fmUODE13SnETkjQE+BdgH0Mzou76dgAuMo0o gPKTQZU1jzrqgfvhRCYSCnKEabljJfyHJO0gQtwgT79GJijM7g6rvybZxrcQPaFbr6ZM clo1Sua5BwZCnBQT+ELvQvVAJUON7TtTbGrP420KpfkWDRlGkwac+W4GAZNkOSs0emlv vbdYKBjhareR9sj3d81drnHhtW9Yv+5ZTIbOb7Blq2WqpWVNsDmjXsS80azsD0QWWmN7 RsIQWn5dEx20CMXPEfFfrkQUYv/8vFqD7T+jern/w7/YA98OHnswAwi0laccnuQ3g7BM 0Tsw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PMy25v4G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ik8-20020a170902ab0800b001a687bb33f5si8344997plb.434.2023.06.07.00.37.30; Wed, 07 Jun 2023 00:37:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PMy25v4G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239099AbjFGH0Q (ORCPT + 99 others); Wed, 7 Jun 2023 03:26:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237868AbjFGHZs (ORCPT ); Wed, 7 Jun 2023 03:25:48 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 307D926B8; Wed, 7 Jun 2023 00:24:48 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 65A5F61158; Wed, 7 Jun 2023 07:24:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 05BD3C433D2; Wed, 7 Jun 2023 07:24:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122644; bh=CkxTuOnRk4Y7C4ZgcPNao1u1GfsuntANuWAV4i13qLA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PMy25v4GG9+Ja9Bb96Nluii5OIwk55WuuQX7qDg7t5jFS0Hx5+OX7Jw4xm+TFgG0/ EK5cbd5c5sNCpSKZ8ThwMv9L53yizemFCeFm8x9yLPLeG8b/D8zOMqdLySdclP2Ya4 5OERtCbemLyLAy6CxaB608eNBsFazsyIQIz8i3kfeMalc70ufng3srHT/gyvLyNsCV w0FVKboMBo6uWFt/klHQGLGZgrlF7Dw2mmTQeag1uUAhb9Yk1FB5XXD+Weowj7HmhX TrjTY503vQrUytexJAArVadYvR3eZjMeFrrppDqhjoz32ETsi1dH/yzz7sSc+Pd0qc 9wd11LoAP8ZFQ== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 02/20] x86/efistub: Simplify and clean up handover entry code Date: Wed, 7 Jun 2023 09:23:24 +0200 Message-Id: <20230607072342.4054036-3-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=7453; i=ardb@kernel.org; h=from:subject; bh=CkxTuOnRk4Y7C4ZgcPNao1u1GfsuntANuWAV4i13qLA=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBIG1xdWG85es/hlI6M/Z1RGYpx0vedDecdulYdWP4r YN3zr/uKGVhEONgkBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABP5eIqRodP0FQNjOP9v89I0 zdSmhrNcL2/Z7dj8mfcI+88dT+cxnGZk2HlIO35xrvZZq7Wn/+rc6/ql/9B+n7TI0hJ3j9naHp/ reAA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028596652876154?= X-GMAIL-MSGID: =?utf-8?q?1768028596652876154?= Now that the EFI entry code in assembler is only used by the optional and deprecated EFI handover protocol, and given that the EFI stub C code no longer returns to it, most of it can simply be dropped. While at it, clarify the symbol naming, by merging efi_main() and efi_stub_entry(), making the latter the shared entry point for all different boot modes that enter via the EFI stub. The efi32_stub_entry() and efi64_stub_entry() names are referenced explicitly by the tooling that populates the setup header, so these must be retained, but can be emitted as aliases of efi_stub_entry() where appropriate. Signed-off-by: Ard Biesheuvel --- Documentation/arch/x86/boot.rst | 2 +- arch/x86/boot/compressed/efi_mixed.S | 22 +++++++++++--------- arch/x86/boot/compressed/head_32.S | 11 ---------- arch/x86/boot/compressed/head_64.S | 12 ++--------- drivers/firmware/efi/libstub/x86-stub.c | 20 ++++++++++++++---- 5 files changed, 31 insertions(+), 36 deletions(-) diff --git a/Documentation/arch/x86/boot.rst b/Documentation/arch/x86/boot.rst index 33520ecdb37abfda..cdbca15a4fc23833 100644 --- a/Documentation/arch/x86/boot.rst +++ b/Documentation/arch/x86/boot.rst @@ -1417,7 +1417,7 @@ execution context provided by the EFI firmware. The function prototype for the handover entry point looks like this:: - efi_main(void *handle, efi_system_table_t *table, struct boot_params *bp) + efi_stub_entry(void *handle, efi_system_table_t *table, struct boot_params *bp) 'handle' is the EFI image handle passed to the boot loader by the EFI firmware, 'table' is the EFI system table - these are the first two diff --git a/arch/x86/boot/compressed/efi_mixed.S b/arch/x86/boot/compressed/efi_mixed.S index 4ca70bf93dc0bdcd..dcc562c8f7f35162 100644 --- a/arch/x86/boot/compressed/efi_mixed.S +++ b/arch/x86/boot/compressed/efi_mixed.S @@ -26,8 +26,8 @@ * When booting in 64-bit mode on 32-bit EFI firmware, startup_64_mixed_mode() * is the first thing that runs after switching to long mode. Depending on * whether the EFI handover protocol or the compat entry point was used to - * enter the kernel, it will either branch to the 64-bit EFI handover - * entrypoint at offset 0x390 in the image, or to the 64-bit EFI PE/COFF + * enter the kernel, it will either branch to the common 64-bit EFI stub + * entrypoint efi_stub_entry() directly, or via the 64-bit EFI PE/COFF * entrypoint efi_pe_entry(). In the former case, the bootloader must provide a * struct bootparams pointer as the third argument, so the presence of such a * pointer is used to disambiguate. @@ -37,21 +37,23 @@ * | efi32_pe_entry |---->| | | +-----------+--+ * +------------------+ | | +------+----------------+ | * | startup_32 |---->| startup_64_mixed_mode | | - * +------------------+ | | +------+----------------+ V - * | efi32_stub_entry |---->| | | +------------------+ - * +------------------+ +------------+ +---->| efi64_stub_entry | - * +-------------+----+ - * +------------+ +----------+ | - * | startup_64 |<----| efi_main |<--------------+ - * +------------+ +----------+ + * +------------------+ | | +------+----------------+ | + * | efi32_stub_entry |---->| | | | + * +------------------+ +------------+ | | + * V | + * +------------+ +----------------+ | + * | startup_64 |<----| efi_stub_entry |<--------+ + * +------------+ +----------------+ */ SYM_FUNC_START(startup_64_mixed_mode) lea efi32_boot_args(%rip), %rdx mov 0(%rdx), %edi mov 4(%rdx), %esi +#ifdef CONFIG_EFI_HANDOVER_PROTOCOL mov 8(%rdx), %edx // saved bootparams pointer test %edx, %edx - jnz efi64_stub_entry + jnz efi_stub_entry +#endif /* * efi_pe_entry uses MS calling convention, which requires 32 bytes of * shadow space on the stack even if all arguments are passed in diff --git a/arch/x86/boot/compressed/head_32.S b/arch/x86/boot/compressed/head_32.S index 987ae727cf9f0d04..8876ffe30e9a4819 100644 --- a/arch/x86/boot/compressed/head_32.S +++ b/arch/x86/boot/compressed/head_32.S @@ -150,17 +150,6 @@ SYM_FUNC_START(startup_32) jmp *%eax SYM_FUNC_END(startup_32) -#ifdef CONFIG_EFI_STUB -SYM_FUNC_START(efi32_stub_entry) - add $0x4, %esp - movl 8(%esp), %esi /* save boot_params pointer */ - call efi_main - /* efi_main returns the possibly relocated address of startup_32 */ - jmp *%eax -SYM_FUNC_END(efi32_stub_entry) -SYM_FUNC_ALIAS(efi_stub_entry, efi32_stub_entry) -#endif - .text SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index 03c4328a88cbd5d0..71c1f40a7ac067b9 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -523,19 +523,11 @@ trampoline_return: jmp *%rax SYM_CODE_END(startup_64) -#ifdef CONFIG_EFI_STUB -#ifdef CONFIG_EFI_HANDOVER_PROTOCOL +#if IS_ENABLED(CONFIG_EFI_MIXED) && IS_ENABLED(CONFIG_EFI_HANDOVER_PROTOCOL) .org 0x390 -#endif SYM_FUNC_START(efi64_stub_entry) - and $~0xf, %rsp /* realign the stack */ - movq %rdx, %rbx /* save boot_params pointer */ - call efi_main - movq %rbx,%rsi - leaq rva(startup_64)(%rax), %rax - jmp *%rax + jmp efi_stub_entry SYM_FUNC_END(efi64_stub_entry) -SYM_FUNC_ALIAS(efi_stub_entry, efi64_stub_entry) #endif .text diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c index d4a730f053bdcbfa..4e01278c3f3d55f3 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -817,9 +817,9 @@ static void __noreturn enter_kernel(unsigned long kernel_addr, * return. On failure, it will exit to the firmware via efi_exit() instead of * returning. */ -asmlinkage unsigned long efi_main(efi_handle_t handle, - efi_system_table_t *sys_table_arg, - struct boot_params *boot_params) +void __noreturn efi_stub_entry(efi_handle_t handle, + efi_system_table_t *sys_table_arg, + struct boot_params *boot_params) { unsigned long bzimage_addr = (unsigned long)startup_32; unsigned long buffer_start, buffer_end; @@ -965,7 +965,19 @@ asmlinkage unsigned long efi_main(efi_handle_t handle, enter_kernel(bzimage_addr, boot_params); fail: - efi_err("efi_main() failed!\n"); + efi_err("efi_stub_entry() failed!\n"); efi_exit(handle, status); } + +#ifdef CONFIG_EFI_HANDOVER_PROTOCOL +#ifndef CONFIG_EFI_MIXED +extern __alias(efi_stub_entry) +void efi32_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg, + struct boot_params *boot_params); + +extern __alias(efi_stub_entry) +void efi64_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg, + struct boot_params *boot_params); +#endif +#endif From patchwork Wed Jun 7 07:23:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104303 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp75432vqr; Wed, 7 Jun 2023 00:37:20 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7RAAsm1dRjzB9v1nDAzL/a0FSAD9AFM5yAWS6tPOiBWk2fTLObNyX743BUfJICpMqXEjRd X-Received: by 2002:a0d:d1c6:0:b0:55d:626c:b62f with SMTP id t189-20020a0dd1c6000000b0055d626cb62fmr4696119ywd.51.1686123439932; Wed, 07 Jun 2023 00:37:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123439; cv=none; d=google.com; s=arc-20160816; b=GWd90sis1AhK+F3/J32XnZQSiIDUPvyo3zZNDmW1/P80xZ07txjFtxZab10Bnwaz2G oWpZLMRlZBr767SvgCbMyNz+jpT1iMp1sJSSjfB+GwiIaRYIYFssW6EVdLbMDjVMcJrY KUXF6XsQvNaEp37ysCOEmMPc3Xyuj6VpYgudbiFntl152d7FjqvWq+52cRITjoozaK6T kXy0Pa2SpF2DeJZVFP1R5MC/k7h/jQ32+VBKeM0q1G4aSCbrKso5WGio9qIgbiH1MTre cCKHLsP2xG02Exd+wueVEgQ0s4ve8jXiKlbdmWZF2AInJIXahMpbAXFI1vH5gqxVALi0 BEZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XJ255OBwItkgl/RrDZ4/XIccKLHanS1q8evPlqMyMy0=; b=E+cb3aJ/XILAS+CRg+tYtM+68gb/a2Xd/diH5Uucw9yPDKouItBOc5xLtooXyTgwg/ fu5saiKi78CkeGjZdeJ0/G82XKI/ye1k5rjZEo5i6ss3YaVqcnnwNMmlxAbE7P/VIE5T G4WCTkFoqyEgsxeMfWM9wXCdDh/N1Rlh5xPpRFUTx1V9M1h8Sv5/Gmqn2u3D+viFwU8s DenNm1hrouU2PW0U0WpeqF5XjUncT2a3RqzjtRndw261Moa4zPQz3byagx71JvCtM1ku bWgFYIYZH1744IUIzkav+ylDwsNk+uCj9mXwllljtXowrNHkLddgnFZjXN0SzCKrcut3 sWaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=gbNyxJgI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g2-20020aa796a2000000b0065dba8cc3a6si3764595pfk.376.2023.06.07.00.37.07; Wed, 07 Jun 2023 00:37:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=gbNyxJgI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239197AbjFGHZZ (ORCPT + 99 others); Wed, 7 Jun 2023 03:25:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239302AbjFGHYz (ORCPT ); Wed, 7 Jun 2023 03:24:55 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52C691BD3; Wed, 7 Jun 2023 00:24:14 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 9FB4461587; Wed, 7 Jun 2023 07:24:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 452C9C4339E; Wed, 7 Jun 2023 07:24:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122649; bh=+pOeFG4I2M0NxWTqt2DlFeFWBXmc5I0TflwHlC5RHXA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gbNyxJgII6oV/sDxiUDD0sac72ZQt7Kqef0c8ooJ39V/oNSjBboXYXF87mt1XogBA 3aYvQb5VFCxGO4V9J0wWkrODPVWbopBNPshHQTM3//VL6nujl4Kvs0u6GGIGI4qyzJ sOjHHn4ew8IDefnhTKurOw8PWdmLyc1UsDbyhEd2Xg559ai570tqnugXvhEtv3wIu+ fpnmfncUc/E85AGpC1A9c5CvjQVD/PkhrAdW5yxLJX0zWuWYlKlo6TCz5k25m2jJdt xEDfvp5LqEKkr91a0Ss+cOE50yYh9oT1XUjV8TDWe+D6tPu7VgJYcVVNKvwT3uW3mP l72G78g+Shq+g== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 03/20] x86/decompressor: Avoid magic offsets for EFI handover entrypoint Date: Wed, 7 Jun 2023 09:23:25 +0200 Message-Id: <20230607072342.4054036-4-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3376; i=ardb@kernel.org; h=from:subject; bh=+pOeFG4I2M0NxWTqt2DlFeFWBXmc5I0TflwHlC5RHXA=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBIP1Nh4/k5gqHKBtjoxNfX9xeE5N2szXBxHFW0OH12 3TCd2h2lLIwiHEwyIopsgjM/vtu5+mJUrXOs2Rh5rAygQxh4OIUgIkU8TIybJ51tOW7kaxFx04D fyutr04uNQo9m7s6JFwFVD/OWddzguG/n3pLnLdO4jHXpLUCZ1h23HtwSFl75opVnK7LZ8499E+ EFQA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028571984393211?= X-GMAIL-MSGID: =?utf-8?q?1768028571984393211?= The native 32-bit or 64-bit EFI handover protocol entrypoint offset relative to the respective startup_32/64 address is described in boot_params as handover_offset, so that the special Linux/x86 aware EFI loader can find it there. When mixed mode is enabled, this single field has to describe this offset for both the 32-bit and 64-bit entrypoints, so their respective relative offsets have to be identical. Given that startup_32 and startup_64 are 0x200 bytes apart, and the EFI handover entrypoint resides at a fixed offset, the 32-bit and 64-bit versions of those entrypoints must be exactly 0x200 bytes apart as well. Currently, hard-coded fixed offsets are used to ensure this, but it is sufficient to emit the 64-bit entrypoint 0x200 bytes after the 32-bit one, wherever it happens to reside. This allows this code (which is now EFI mixed mode specific) to be moved into efi_mixed.S and out of the startup code in head_64.S. Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/efi_mixed.S | 20 +++++++++++++++++++- arch/x86/boot/compressed/head_64.S | 18 ------------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/arch/x86/boot/compressed/efi_mixed.S b/arch/x86/boot/compressed/efi_mixed.S index dcc562c8f7f35162..9308b595f6f0a5de 100644 --- a/arch/x86/boot/compressed/efi_mixed.S +++ b/arch/x86/boot/compressed/efi_mixed.S @@ -140,6 +140,16 @@ SYM_FUNC_START(__efi64_thunk) SYM_FUNC_END(__efi64_thunk) .code32 +#ifdef CONFIG_EFI_HANDOVER_PROTOCOL +SYM_FUNC_START(efi32_stub_entry) + add $0x4, %esp /* Discard return address */ + popl %ecx + popl %edx + popl %esi + jmp efi32_entry +SYM_FUNC_END(efi32_stub_entry) +#endif + /* * EFI service pointer must be in %edi. * @@ -220,7 +230,7 @@ SYM_FUNC_END(efi_enter32) * stub may still exit and return to the firmware using the Exit() EFI boot * service.] */ -SYM_FUNC_START(efi32_entry) +SYM_FUNC_START_LOCAL(efi32_entry) call 1f 1: pop %ebx @@ -320,6 +330,14 @@ SYM_FUNC_START(efi32_pe_entry) RET SYM_FUNC_END(efi32_pe_entry) +#ifdef CONFIG_EFI_HANDOVER_PROTOCOL + .org efi32_stub_entry + 0x200 + .code64 +SYM_FUNC_START_NOALIGN(efi64_stub_entry) + jmp efi_stub_entry +SYM_FUNC_END(efi64_stub_entry) +#endif + .section ".rodata" /* EFI loaded image protocol GUID */ .balign 4 diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index 71c1f40a7ac067b9..9f90661744741210 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -294,17 +294,6 @@ SYM_FUNC_START(startup_32) lret SYM_FUNC_END(startup_32) -#if IS_ENABLED(CONFIG_EFI_MIXED) && IS_ENABLED(CONFIG_EFI_HANDOVER_PROTOCOL) - .org 0x190 -SYM_FUNC_START(efi32_stub_entry) - add $0x4, %esp /* Discard return address */ - popl %ecx - popl %edx - popl %esi - jmp efi32_entry -SYM_FUNC_END(efi32_stub_entry) -#endif - .code64 .org 0x200 SYM_CODE_START(startup_64) @@ -523,13 +512,6 @@ trampoline_return: jmp *%rax SYM_CODE_END(startup_64) -#if IS_ENABLED(CONFIG_EFI_MIXED) && IS_ENABLED(CONFIG_EFI_HANDOVER_PROTOCOL) - .org 0x390 -SYM_FUNC_START(efi64_stub_entry) - jmp efi_stub_entry -SYM_FUNC_END(efi64_stub_entry) -#endif - .text SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) From patchwork Wed Jun 7 07:23:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104312 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp75898vqr; Wed, 7 Jun 2023 00:38:24 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4Cr49XfJkfqua69L/pTM4wl7eP4snISrrtBwYOspnCMiPD23decAO9sZgB+oO0B+ogjy+n X-Received: by 2002:a05:6a21:900c:b0:10b:e90d:13a2 with SMTP id tq12-20020a056a21900c00b0010be90d13a2mr2094667pzb.36.1686123503935; Wed, 07 Jun 2023 00:38:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123503; cv=none; d=google.com; s=arc-20160816; b=frNt9b4bYMioHwh/u8rc6cue4uaLBmaxahLmenoi5iGhNnbj4vAA4vUMSqyYAc6miF 8OK67zGoRYVtTFZfM++TKhqz9J17Z6CVoJPP61xZdR6/Oz5+zO9buKGuJJjGJPb+Kt0y qiOi1JEHbZuv1o8S6s6w5ACooNe6w2mLY1xEvGr/V85umdapIBtRAAV5/ovcxAv0/ei/ ay5raWyorSms+PV1APH8K/0czY91Bfj3/3+YusoiF89GOQki2MMTUebKJ+nxbHjGhcgy Cg6i0GkjXeWmZp5zfJxtL8FA67V+KR6mBoOpnVRXFlO4tZFwD3+oGHkPCEfDbURkX0rA lUyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=A7psBbiOfZAd0QbGmLtYpHQhUjDQ0NxJS83UjKOqeO0=; b=SYlueQSQHjGJxJ7EkXrji6K8M6TxtLu7HV8EWERDKM+I4LaoQJYCZNKxp+rlPoXaJV HMyHRhDlk30dft6Q5MC3neAjaQq41lJsY+u84R9O0Y3hyiEu7E7bKSxriFRGM/bxh92K 7RvOIT4VnzpdaPNCHVjGQPdKr6FFOBoU2/YXKZvzwAq2waNurjx8maWxXP2FFnbJl5mf ewbs+uq6M8pksl5TeZMqsHIqPzSgqHv0laEkLzpEebyJEFddFDzA5qm8kOdAlY9E88ik Z+g2jhrzkyICXDfLBwsejPdiQpLk/ULDspK25VEX9576+3JLsezorkUXKUDs0G2jEZWF yBMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Gz9GzW/y"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u8-20020a170902e5c800b001a59439dbfesi8903231plf.529.2023.06.07.00.38.10; Wed, 07 Jun 2023 00:38:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Gz9GzW/y"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235467AbjFGHZ3 (ORCPT + 99 others); Wed, 7 Jun 2023 03:25:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53538 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239310AbjFGHY4 (ORCPT ); Wed, 7 Jun 2023 03:24:56 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D8591BDC; Wed, 7 Jun 2023 00:24:15 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E03F063B5C; Wed, 7 Jun 2023 07:24:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7F9FAC4339C; Wed, 7 Jun 2023 07:24:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122653; bh=noxdmhKmlrx1dvn8Z1FgJIl9KblkeEYhFso88MlTjTc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Gz9GzW/yVz3UEVoG3d5J3Tht2msZHcUDWORT+8B2L/+tpheo3+tg+5DTHOMjgUz34 SVH6ENb7Q51NZz+D/h4SJNNomCLVMCr8ijDQdWIpTsKiM2MCpgvqHDp94oVUoLNKDV KuASwIfmpChq8x9vaNMeEohedXelKnTJNalhXIm8SYiLZay3nKodR6ecl16UXuadpX olVXXuj1J1jMoIYeJQ/KVQTKLePSQCsRSGsFUCN7ALPT2cHM8u8Ah6UFpdNsC9Fb83 XgMt+ESgqiq2kYUXAPmbPidtpaQ9G7vZCJKYQ5H+biYJrmiRDsNygQzHsOeBQrFybF XeoDwnKnpq47g== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 04/20] x86/efistub: Clear BSS in EFI handover protocol entrypoint Date: Wed, 7 Jun 2023 09:23:26 +0200 Message-Id: <20230607072342.4054036-5-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2971; i=ardb@kernel.org; h=from:subject; bh=noxdmhKmlrx1dvn8Z1FgJIl9KblkeEYhFso88MlTjTc=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBILPzyK+u+TI+Te90fv28ss82munuVsX4QJsdS5YYK 504fHtJRykLgxgHg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZjIzUZGhnOC4iXn96iuMw0O 4TQPyTkjlnN+TsFd/5TnjxbGTqlQPMnIcCor5P+fj4z71CryBPfxdC043fm3cNfmCcYq7pLuUWq vOAA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028639098595075?= X-GMAIL-MSGID: =?utf-8?q?1768028639098595075?= The so-called EFI handover protocol is value-add from the distros that permits a loader to simply copy a PE kernel image into memory and call an alternative entrypoint that is described by an embedded boot_params structure. Most implementations of this protocol do not bother to check the PE header for minimum alignment, section placement, etc, and therefore also don't clear the image's BSS, or even allocate enough memory for it. Allocating more memory on the fly is rather difficult, but at least clear the BSS region explicitly when entering in this manner, so that the EFI stub code does not get confused by global variables that were not zero-initialized correctly. When booting in mixed mode, this BSS clearing must occur before any global state is created, so clear it in the 32-bit asm entry point. Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/efi_mixed.S | 14 +++++++++++++- drivers/firmware/efi/libstub/x86-stub.c | 13 +++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/arch/x86/boot/compressed/efi_mixed.S b/arch/x86/boot/compressed/efi_mixed.S index 9308b595f6f0a5de..8a02a151806df14c 100644 --- a/arch/x86/boot/compressed/efi_mixed.S +++ b/arch/x86/boot/compressed/efi_mixed.S @@ -142,6 +142,18 @@ SYM_FUNC_END(__efi64_thunk) .code32 #ifdef CONFIG_EFI_HANDOVER_PROTOCOL SYM_FUNC_START(efi32_stub_entry) + call 1f +1: popl %ecx + + /* Clear BSS */ + xorl %eax, %eax + leal (_bss - 1b)(%ecx), %edi + leal (_ebss - 1b)(%ecx), %ecx + subl %edi, %ecx + shrl $2, %ecx + cld + rep stosl + add $0x4, %esp /* Discard return address */ popl %ecx popl %edx @@ -334,7 +346,7 @@ SYM_FUNC_END(efi32_pe_entry) .org efi32_stub_entry + 0x200 .code64 SYM_FUNC_START_NOALIGN(efi64_stub_entry) - jmp efi_stub_entry + jmp efi_handover_entry SYM_FUNC_END(efi64_stub_entry) #endif diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c index 4e01278c3f3d55f3..517cd68ea86cb7f4 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -971,12 +971,21 @@ void __noreturn efi_stub_entry(efi_handle_t handle, } #ifdef CONFIG_EFI_HANDOVER_PROTOCOL +void efi_handover_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg, + struct boot_params *boot_params) +{ + extern char _bss[], _ebss[]; + + memset(_bss, 0, _ebss - _bss); + efi_stub_entry(handle, sys_table_arg, boot_params); +} + #ifndef CONFIG_EFI_MIXED -extern __alias(efi_stub_entry) +extern __alias(efi_handover_entry) void efi32_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg, struct boot_params *boot_params); -extern __alias(efi_stub_entry) +extern __alias(efi_handover_entry) void efi64_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg, struct boot_params *boot_params); #endif From patchwork Wed Jun 7 07:23:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104296 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp75039vqr; Wed, 7 Jun 2023 00:36:22 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ45mMAp7f6MBTvatXmnP6ntlfMnt54m8GcDUXC5Q3xkvGW/K8GcsaXSbkHnxp8F7bjrPGfE X-Received: by 2002:a05:6a21:6da4:b0:10f:d1d4:40d4 with SMTP id wl36-20020a056a216da400b0010fd1d440d4mr2619587pzb.14.1686123381933; Wed, 07 Jun 2023 00:36:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123381; cv=none; d=google.com; s=arc-20160816; b=s1i2wmz8K93mjqU73W97ky3rIjbneKikVwr70n31v/jYVP1kX0y8DJXdHr11vD8Ptr SP7fTjjcJ+vHpfK57lAMb1s/gxIQIjOe31G9/5JAt5F3Bnvxu18fi8HXJzYdFUdhJhJK BWbCl1vO6Cy6oAG1mntXltpgzuW3Z8VX/DgpkFaVsKrm8U11qvus0dKeV/A9wYcSBpPr v+JzX8n/JFJzAIc2tokTZeCMc8aHWNqGZhnIjOtVpEkJUhq3qSSNOoEYO25DQFLZcN4a sXwiVldClOQE1t+TNgskQCvNh+NnEgTurauHkjsjio2HhvV5h2ApP4yDY9Ge/34+YN4r 306Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=S3lEqGjsTGw1NWcFmbt4O+4A6ddMT7nl/cAxjp7Py8E=; b=pA4H/iA3sGhHmrO3cS1pnxH1MFsjv5Bj4JeY8wUIXVUHvEZbKzpgtPUECEHVegrqqz PxPa0Cf7f9j0jxy3/vbJOQtbrsiwwGCpO5KDdtiWy472Av++ecNAP8JHqBU+g69Y44oZ b+2XfglzodsYkqza/d7c+9Dr0lOkuAFPikqgbUICZq5t03dsiw1MTLvepIoytUcj/4/A KNVuU/wY8UBpDxTnPjCEq8u9+L+dUQwUDD/rHrOD/UXi6qNDS2iiAC7NZnLZ5hyDQWd9 6/wEPppF/h9FwqzHnM68waOEKXbJAb7UdEVRGHQ8FMJNLQKdJeMZYIfttiJx3B1jgGyo jnkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=S+XySU4G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y192-20020a638ac9000000b0052856ce370asi8658451pgd.426.2023.06.07.00.36.08; Wed, 07 Jun 2023 00:36:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=S+XySU4G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239213AbjFGHZe (ORCPT + 99 others); Wed, 7 Jun 2023 03:25:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235142AbjFGHY5 (ORCPT ); Wed, 7 Jun 2023 03:24:57 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1493B2684; Wed, 7 Jun 2023 00:24:18 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 2714C63B61; Wed, 7 Jun 2023 07:24:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BC4AAC4339E; Wed, 7 Jun 2023 07:24:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122657; bh=ygI81p6SqewQoIn6tCgoZLUH2Qo5tPVgo3BNvwGSS3U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=S+XySU4GwLNBlMd01swdW/4ofacMcN3PCM0wAMFxZJIQZtyqzm3dyafUMPMzwfPPR rXW4w0ECIdYGnEmh9EbLWCs5nyRrjKi3oBOUlg6nb3Z3//UDTpCFYbE3/Ukn6FS7l1 ROYcWld6wrG3koLGVRx0jdCFi7ozGFcsU8xxn+n2WD/hLk5VWJCvBCV4wILQhQp4Fw VT99ZoRnXtt7wQBxBCLRfx4DKeO1iunxTuOIHJ5FwDnYpsVH7d9ZVHsGNTqJhFnHER sf+bFE1zJjlZHkJFkiyynFh6hp7LyePiPNJq8R4tsYVU1VSMFL48nq17hcRXeIaS8+ 16919uac1tTFw== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 05/20] x86/decompressor: Use proper sequence to take the address of the GOT Date: Wed, 7 Jun 2023 09:23:27 +0200 Message-Id: <20230607072342.4054036-6-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1770; i=ardb@kernel.org; h=from:subject; bh=ygI81p6SqewQoIn6tCgoZLUH2Qo5tPVgo3BNvwGSS3U=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBIGtfjGz12WInw7ydqfPDJ7Ukb5i9b7FGVn2Z7oyqH cdfVGp0lLIwiHEwyIopsgjM/vtu5+mJUrXOs2Rh5rAygQxh4OIUgIksucPI8Ik7Pn35zuQ7Hp/C nC9+dF6mGnKjPzXqmjWPa6XbHPVffxgZ5mVe+BW5tCnk+YZFLF9Fe2IW7xSvsf+7r1S15+fH5q4 ZzAA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028511524373975?= X-GMAIL-MSGID: =?utf-8?q?1768028511524373975?= The 32-bit decompressor does not actually use a global offset table (GOT), but as is common for 32-bit position independent code, it uses the magic symbol _GLOBAL_OFFSET_TABLE_ as an anchor from which to derive the actual runtime addresses of other symbols, using special @GOTOFF symbol references that are resolved at link time, and populated with the distance between the address of the magic _GLOBAL_OFFSET_TABLE_ anchor and the address of the symbol in question. This means _GLOBAL_OFFSET_TABLE_ is the only symbol whose actual runtime address needs to be determined explicitly, which is one of the first things that happens in startup_32. However, it does so by taking the absolute address via the immediate field of an ADD instruction (plus a small offset), which seems to defeat the point. Fortunately, the assembler knows that _GLOBAL_OFFSET_TABLE_ is magic, and emits a special relative relocation instead, and so the resulting code works as expected. However, this is not obvious for someone reading the code, and the use of LEA with an explicit relative addend is more idiomatic so use that instead. Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/head_32.S | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/boot/compressed/head_32.S b/arch/x86/boot/compressed/head_32.S index 8876ffe30e9a4819..3530465b5b85ccf3 100644 --- a/arch/x86/boot/compressed/head_32.S +++ b/arch/x86/boot/compressed/head_32.S @@ -58,7 +58,7 @@ SYM_FUNC_START(startup_32) leal (BP_scratch+4)(%esi), %esp call 1f 1: popl %edx - addl $_GLOBAL_OFFSET_TABLE_+(.-1b), %edx + leal (_GLOBAL_OFFSET_TABLE_ - 1b)(%edx), %edx /* Load new GDT */ leal gdt@GOTOFF(%edx), %eax From patchwork Wed Jun 7 07:23:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104302 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp75410vqr; Wed, 7 Jun 2023 00:37:17 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7lbzFa1cR+hXkNvRucAfmmojl7z3qKciWe5VbKg8nG/hSPAiz9ChSX+T/f4n+9/1tBMiFZ X-Received: by 2002:a17:90b:438f:b0:255:3f6f:2c6 with SMTP id in15-20020a17090b438f00b002553f6f02c6mr3734866pjb.22.1686123436812; Wed, 07 Jun 2023 00:37:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123436; cv=none; d=google.com; s=arc-20160816; b=NM4u4fI1SB8mMihCZNCsQc0+IQe6J4F62zsMMNXsHzFb9Gc59zqNDW6i4S0hLlfYFN WztuPzj4ITUw5IKB6e499b9yk374Ao4jFual8SNYWV5Hz7ALKmiXTL4HfuRHBNwTU6Zs QVRZCxc3GI+0jl5Y4nB/PpbsmqCy4SD2bIl1eDL9i8xdsybY71fQYWiv0ak5xpv78X9g 9uK5tMJyjUfpcU1P48OhG/T/XXIALJfYZvA4zBpjQjUEB0sj8B6vI0DjOqyp6MU+ju9B J3F6vgcQhjOt3F3mdlNq1HXhsfbIgDChIKhwLwbsuZ4B8FyV7zoNH7e59aZj8dpDFs9d HdNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=9LNa+G2wrQIjRvt2UCstrzgBbRTUvf6M5+JO2w9PXAw=; b=A+svhEg//BLuvHF909uJtOgdwI5fAsrYcU7ZJxSOGJsvlGtbPgoZO3Tln+QJAQFq4M 3P9B5CTWJN75+roV1fKCcqjwfbuBslk2TWTRqETQDojAS+9nzHyumAtORLAhJLTr7U2t PVkG8OcWgX8X45PLhs9ChD93Z9QFcBnxuxObZR1+4Jkeb1uIldgflBqky2UjgKx5ORQ8 RRmUs1ZxOVumr2fxSovozgKjTcY9AJetRaC5Z8Lt+xP6S9s/WNLnONicGhGkA+BbkbC9 tW9JbaZpid4GNaWVWjg412hVQ4/CeUHX5kUBKET+OelfM6qIP1zUhsl1a+ondaSV3PaA ZKUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JJBBWOju; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u190-20020a6379c7000000b0054402b987ecsi1476597pgc.803.2023.06.07.00.37.04; Wed, 07 Jun 2023 00:37:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JJBBWOju; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239119AbjFGH0z (ORCPT + 99 others); Wed, 7 Jun 2023 03:26:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239074AbjFGH0K (ORCPT ); Wed, 7 Jun 2023 03:26:10 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0CEB1FD0; Wed, 7 Jun 2023 00:25:01 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5A6AA63B62; Wed, 7 Jun 2023 07:24:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 03AA2C4339C; Wed, 7 Jun 2023 07:24:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122661; bh=XH7auhXZovyO0ZjhIGuK6rXf321+XRWDmJGikLNf51o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JJBBWOju3jzimt9XHj0dQdaQl5fDorDEiv6i+7ANrm1By1xxmoV4UJzXYAM0GdbK0 e9BTIP38djJ/ctAjSchzSpuKhPMop1poy9RURzfNzPyw7OTZwL1s+0vmT6b+T6DbTy kY6cdkOvJAdIy2sw3Hh0BsMzxe2RSrw3w/0gB4uKvecoWn7rwOvOHfQ5DtL9h4Be/p pW5+jaVyqlASAVLHg4kK/e9hbC8+/YOjayy4VcrpJccL0GHm5yVGT2LzcKUlkqPOTH KP57M6fzTVlR1qGN9gV3AerC2QOHXMM4dw3lOzIQ4ZW1NW5tYVIdjRHvDFNd7BWtue eNW52uIxbLiew== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 06/20] x86/decompressor: Store boot_params pointer in callee save register Date: Wed, 7 Jun 2023 09:23:28 +0200 Message-Id: <20230607072342.4054036-7-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3619; i=ardb@kernel.org; h=from:subject; bh=XH7auhXZovyO0ZjhIGuK6rXf321+XRWDmJGikLNf51o=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBIHtRpJHd1RnWG0UnzPsfEPSq6/POZO+LSS9tdY8IO KjNVzfuKGVhEONgkBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABPpXs7IMMc4uPmBmrH7vWlz TW4tW8KvdmZ+XC/fq/upR4/m3b+nu5fhfxLHq93y6nxPrC36JOfN4L46fV1M/02e0jK7bfFbX/6 3ZgAA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028569123145022?= X-GMAIL-MSGID: =?utf-8?q?1768028569123145022?= Instead of pushing and popping %RSI several times to preserve the struct boot_params pointer across the execution of the startup code, move it into a callee save register before the first call into C, and copy it back when needed. Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/head_64.S | 34 +++++++------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index 9f90661744741210..2d1b0ee94929f7ec 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -405,10 +405,14 @@ SYM_CODE_START(startup_64) lretq .Lon_kernel_cs: + /* + * RSI holds a pointer to a boot_params structure provided by the + * loader, and this needs to be preserved across C function calls. So + * move it into a callee saved register. + */ + movq %rsi, %r15 - pushq %rsi call load_stage1_idt - popq %rsi #ifdef CONFIG_AMD_MEM_ENCRYPT /* @@ -421,10 +425,8 @@ SYM_CODE_START(startup_64) * detection/setup to ensure that has been done in advance of any dependent * code. */ - pushq %rsi - movq %rsi, %rdi /* real mode address */ + movq %r15, %rdi /* pass struct boot_params pointer */ call sev_enable - popq %rsi #endif /* @@ -437,13 +439,9 @@ SYM_CODE_START(startup_64) * - Non zero RDX means trampoline needs to enable 5-level * paging. * - * RSI holds real mode data and needs to be preserved across - * this function call. */ - pushq %rsi - movq %rsi, %rdi /* real mode address */ + movq %r15, %rdi /* pass struct boot_params pointer */ call paging_prepare - popq %rsi /* Save the trampoline address in RCX */ movq %rax, %rcx @@ -468,14 +466,9 @@ trampoline_return: * * RDI is address of the page table to use instead of page table * in trampoline memory (if required). - * - * RSI holds real mode data and needs to be preserved across - * this function call. */ - pushq %rsi leaq rva(top_pgtable)(%rbx), %rdi call cleanup_trampoline - popq %rsi /* Zero EFLAGS */ pushq $0 @@ -485,7 +478,6 @@ trampoline_return: * Copy the compressed kernel to the end of our buffer * where decompression in place becomes safe. */ - pushq %rsi leaq (_bss-8)(%rip), %rsi leaq rva(_bss-8)(%rbx), %rdi movl $(_bss - startup_32), %ecx @@ -493,7 +485,6 @@ trampoline_return: std rep movsq cld - popq %rsi /* * The GDT may get overwritten either during the copy we just did or @@ -525,30 +516,27 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) shrq $3, %rcx rep stosq - pushq %rsi call load_stage2_idt /* Pass boot_params to initialize_identity_maps() */ - movq (%rsp), %rdi + movq %r15, %rdi /* pass struct boot_params pointer */ call initialize_identity_maps - popq %rsi /* * Do the extraction, and jump to the new kernel.. */ - pushq %rsi /* Save the real mode argument */ - movq %rsi, %rdi /* real mode address */ + movq %r15, %rdi /* pass struct boot_params pointer */ leaq boot_heap(%rip), %rsi /* malloc area for uncompression */ leaq input_data(%rip), %rdx /* input_data */ movl input_len(%rip), %ecx /* input_len */ movq %rbp, %r8 /* output target address */ movl output_len(%rip), %r9d /* decompressed length, end of relocs */ call extract_kernel /* returns kernel entry point in %rax */ - popq %rsi /* * Jump to the decompressed kernel. */ + movq %r15, %rsi jmp *%rax SYM_FUNC_END(.Lrelocated) From patchwork Wed Jun 7 07:23:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104289 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp71770vqr; Wed, 7 Jun 2023 00:28:44 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6HmswNm97v1GOkcjKbAzcDB5ZoGoKTLhlf56g5LwhUU0c6aX76o92G7oJEG8I6BKStIy/a X-Received: by 2002:a05:6a00:1743:b0:656:c7b2:876f with SMTP id j3-20020a056a00174300b00656c7b2876fmr5626810pfc.14.1686122924604; Wed, 07 Jun 2023 00:28:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686122924; cv=none; d=google.com; s=arc-20160816; b=Ed7uD1hbUFTtuxuhN8wmFBIChac9LRJFKeslU5VWEWlpW9mUMkMv9m8ti+KLIBp2+o W0jSaf62kfBB+HFeTsySfHsG34lZuoh9GKUjg7b+Ftb+Xi0Ugnp3o14bFV/BxfNYE0W3 5P3eNbm8eCVT2nQN7BYyJAlxN808spSt7sVtHtcrWPflq7FTirPOSobnEf+P2m4pEZJo 2s16+iEs75YndvF6qM02L+J78ZuThWzi/oaLL60yqLT5/9lOOmU6Gzo5TiV/P782OdkH WNuVa5YwUJWwnugF47iLLud4EslEeIjifukap36z60iKOyEwBz5Uyu4hfmyxIt4FsUE+ GNQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=sQZhXDdW/Pt1TLOgC10hAFzrGwzWSSI2c8Hy68wku88=; b=R28f1CbQBd0N/jvKR0/cElPG4e+rLpu4prKj/BUnLCr6fln7h4nznoKavlUlBXw25w BnXcGns3obuzH42tBd36AbsR0vAWPyTL0oy3P1w9Jjbq4U08GRoh/yRXfWqot70Mm+Zm 9VVkmqvCZxDigl6iFFyYdvovbSctQLvt7byn2ffI7TjBBcM/f+JiCzmvzjlaS/yrYm8f XTuXm+oVGZuA4R5VjB9InQQnEvIf6cjSJpc4spz0BzXiNOPTjxHGxig2YVLqQdWmQ9ok ZzhgjDa2mI15z3B44gFYbrHD/j0R5C4PtlYb9Oww5d1viDG6Sj2miOL+qq8oGZEpYR6A o45A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rzOazdyg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z3-20020aa79903000000b006501b3c2010si7502421pff.55.2023.06.07.00.28.29; Wed, 07 Jun 2023 00:28:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rzOazdyg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233798AbjFGH1h (ORCPT + 99 others); Wed, 7 Jun 2023 03:27:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239160AbjFGH0f (ORCPT ); Wed, 7 Jun 2023 03:26:35 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85EA2199A; Wed, 7 Jun 2023 00:25:02 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 9647563550; Wed, 7 Jun 2023 07:24:26 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3E06FC4339B; Wed, 7 Jun 2023 07:24:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122666; bh=4Bh3UcI6mBb60v5tgLvoAeGffUld7BrhFCXiRmmP3sE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rzOazdygcvRuY93cXLsS8hgWuvB8GQgaTic9tG6nGelF4i5JdK64FZwgf+gJCb6i2 8XSlimfFCarpG3ArfY028b4gxt+pWdpuCLoXPsoQnSZ08GRG8f0ec2tYwq5FIGGetV l7tBa6SyfWteIEtLzC9gFJvuAyE2/VtTJL7Y0B3xmw5G2NgFKiKFMHZL/o1wNcVf2w ADIS/mZuSOf4m0+CynpMHNNjt3+/9ZmK2LCKQlrNCtqr1s6YokuK15SUJVIWUSFcOS kB7uE2qrG3V57Yo7ILhK7ut4G650PmOXPuE5QIrT+J1zB1WMwDQD+kqGjn90po6pb7 AjmubYQrfrgwQ== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 07/20] x86/decompressor: Call trampoline as a normal function Date: Wed, 7 Jun 2023 09:23:29 +0200 Message-Id: <20230607072342.4054036-8-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2554; i=ardb@kernel.org; h=from:subject; bh=4Bh3UcI6mBb60v5tgLvoAeGffUld7BrhFCXiRmmP3sE=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBIOfyXcGvbvdk7s59zjTFLSjIQEnfS8e2IGX29+utW 1ncSkI6SlkYxDgYZMUUWQRm/3238/REqVrnWbIwc1iZQIYwcHEKwETy+hn+xxVWFTTnnGldY9+m eUmtLzCG0anjqcuUBPPQ3c5LL3j4MPz3qrt0paj+qkaBsewU1mWqRUe32/abr9sTFn39wCNjvRW sAA== X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028031949196014?= X-GMAIL-MSGID: =?utf-8?q?1768028031949196014?= Move the long return to switch to 32-bit mode into the trampoline code so it can be called as an ordinary function. This will allow it to be called directly from C code in a subsequent patch. Acked-by: Kirill A. Shutemov Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/head_64.S | 25 +++++++++----------- arch/x86/boot/compressed/pgtable.h | 2 +- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index 2d1b0ee94929f7ec..af45ddd8297a4a07 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -446,18 +446,9 @@ SYM_CODE_START(startup_64) /* Save the trampoline address in RCX */ movq %rax, %rcx - /* - * Load the address of trampoline_return() into RDI. - * It will be used by the trampoline to return to the main code. - */ - leaq trampoline_return(%rip), %rdi - - /* Switch to compatibility mode (CS.L = 0 CS.D = 1) via far return */ - pushq $__KERNEL32_CS leaq TRAMPOLINE_32BIT_CODE_OFFSET(%rax), %rax - pushq %rax - lretq -trampoline_return: + call *%rax + /* Restore the stack, the 32-bit trampoline uses its own stack */ leaq rva(boot_stack_end)(%rbx), %rsp @@ -540,16 +531,22 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) jmp *%rax SYM_FUNC_END(.Lrelocated) - .code32 /* * This is the 32-bit trampoline that will be copied over to low memory. * - * RDI contains the return address (might be above 4G). * ECX contains the base address of the trampoline memory. * Non zero RDX means trampoline needs to enable 5-level paging. */ SYM_CODE_START(trampoline_32bit_src) - /* Set up data and stack segments */ + popq %rdi + /* Switch to compatibility mode (CS.L = 0 CS.D = 1) via far return */ + pushq $__KERNEL32_CS + leaq 0f(%rip), %rax + pushq %rax + lretq + + .code32 +0: /* Set up data and stack segments */ movl $__KERNEL_DS, %eax movl %eax, %ds movl %eax, %ss diff --git a/arch/x86/boot/compressed/pgtable.h b/arch/x86/boot/compressed/pgtable.h index cc9b2529a08634b4..91dbb99203fbce2d 100644 --- a/arch/x86/boot/compressed/pgtable.h +++ b/arch/x86/boot/compressed/pgtable.h @@ -6,7 +6,7 @@ #define TRAMPOLINE_32BIT_PGTABLE_OFFSET 0 #define TRAMPOLINE_32BIT_CODE_OFFSET PAGE_SIZE -#define TRAMPOLINE_32BIT_CODE_SIZE 0x80 +#define TRAMPOLINE_32BIT_CODE_SIZE 0xA0 #define TRAMPOLINE_32BIT_STACK_END TRAMPOLINE_32BIT_SIZE From patchwork Wed Jun 7 07:23:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104294 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp74950vqr; Wed, 7 Jun 2023 00:36:09 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6P/2EZxE2lc+sdqbQ74YS/FcUQZ4ZvnX4dXjkxrd7CBetDZuHOmhlblA6+0QXh3ofCyQAi X-Received: by 2002:a05:6a20:2591:b0:110:9210:f6af with SMTP id k17-20020a056a20259100b001109210f6afmr792162pzd.27.1686123369314; Wed, 07 Jun 2023 00:36:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123369; cv=none; d=google.com; s=arc-20160816; b=SY7F1faLs/gNIBpA774HR0DY9GbJ/t31coZ9rYxxyDSNfAuC0PucDhV9hLSqEqRi4M 8bNAF0EhpqUiKaAGUS09zTU65oy5n+7ucbQxpaMiKQ1t9JqV+j3S5yU7XX3tCXG5Rdhk BcfX2WSI1XNqeXR+ykfqv/xSLsAv3uR8ngeB0t/DMjGn5bFuGNC/WBKWYROOGTRfsQY2 sFIofcVf69S/5Gn2sasJvQSpcCSFka76obB+ehTWaQVSQ18JSGJN2aS4aaVkoJXgN399 PA5KBnyjzeLD4xMCaPe6qWFlB9N59FgwWt3cFVvSKo3Q63bZD8NbnD/hSoLZrFTJMqy+ Uq0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=owkHrfGk6rkCsLR4jaeO3WUYAvrwcw7T/GSFdPTyMwM=; b=fSGOUAtjUq8K5nvW+9afbEQQ2onTejjH1CiZILzAeNKPlmHqmsIU8ZkTnp6f3VqePz 8xMGcxdmjSNMbDcenjeHQtZRTqDKs35hA+NoWBIxXADbA5I59jwLHXViyCh3U07+XBto +/k23bp0419SPIvhSsR7isMHqB9sXGTVgFv9Y9C0FPfSdifITCMwHAdvtKGRmcuAO5g7 Y+rnSMKbx3xGEU5qnhv03IX4wIqZ19jdEGFnHmZQZrxJnvkmAlVS8v3nnfzeDhrXeHFE dnTr4WuGadYQSX7QucAeap71LWhgjWvtcSEebyUvS1altUK4G4fMipEyPOIYIbvWStyB EVKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=IgM11BuI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u8-20020a170902e5c800b001a59439dbfesi8903231plf.529.2023.06.07.00.35.55; Wed, 07 Jun 2023 00:36:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=IgM11BuI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234162AbjFGHZ5 (ORCPT + 99 others); Wed, 7 Jun 2023 03:25:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239202AbjFGHZ0 (ORCPT ); Wed, 7 Jun 2023 03:25:26 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 681BF1FD4; Wed, 7 Jun 2023 00:24:41 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id DA77463B70; Wed, 7 Jun 2023 07:24:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 79581C433EF; Wed, 7 Jun 2023 07:24:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122670; bh=6LI2p3tMGpqgjf5Vh98U5/dlrPhBIFsp0XryDg0+x/4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IgM11BuIp3TzyU26yqt96LabnmOCTvGHaPsyBKuLDOzlPjLzSSUgk6lhaBLEMlLgZ PYpYt3y1fOhyNa5YWFzrfNtRxOMMvWTRWWw2q2CvV+lWP91uU03/kr2OjAwltciJcw WFsq8tCURRaSCs6OLg9K25Sjc/JEw2f4zfNrG/ovNcFhOP1EDTOQrcn6DLyUwJxKS5 eph44AGkWFbmVmMi55+hPsfSq5TEK/E/e/NfRZLGRSMg4XYlZgbDDTgF9cXKvHJlam XQ5W2E4qBHKB2B41W4z12g3jdfBcjedfCqxKJhOJ9TtZ7302p4y9o7SzxID9qHlRCL 7tSsImNsDEdWg== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 08/20] x86/decompressor: Use standard calling convention for trampoline Date: Wed, 7 Jun 2023 09:23:30 +0200 Message-Id: <20230607072342.4054036-9-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4143; i=ardb@kernel.org; h=from:subject; bh=6LI2p3tMGpqgjf5Vh98U5/dlrPhBIFsp0XryDg0+x/4=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBIG9l6kWJ8zVOTq1sXadOmfbaxrX+bM1U2TXTp8ROI vgNn21HKQuDGAeDrJgii8Dsv+92np4oVes8SxZmDisTyBAGLk4BmEjRBYb/+a4JOuuYb/Ite7Lo 1+rYCnfuU29Oi3C/7JUPaU458kokjJHh2k9Za4Hmk1166rJ7PnHv6ytfyJMkfGjZs+V+6utfPLP iAQA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028497783641781?= X-GMAIL-MSGID: =?utf-8?q?1768028497783641781?= Update the trampoline code so its arguments are passed via RDI and RSI, which matches the ordinary SysV calling convention for x86_64. This will allow this code to be called directly from C. Acked-by: Kirill A. Shutemov Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/head_64.S | 30 +++++++++----------- arch/x86/boot/compressed/pgtable.h | 2 +- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index af45ddd8297a4a07..a387cd80964e1a1e 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -443,9 +443,9 @@ SYM_CODE_START(startup_64) movq %r15, %rdi /* pass struct boot_params pointer */ call paging_prepare - /* Save the trampoline address in RCX */ - movq %rax, %rcx - + /* Pass the trampoline address and boolean flag as args #1 and #2 */ + movq %rax, %rdi + movq %rdx, %rsi leaq TRAMPOLINE_32BIT_CODE_OFFSET(%rax), %rax call *%rax @@ -534,11 +534,11 @@ SYM_FUNC_END(.Lrelocated) /* * This is the 32-bit trampoline that will be copied over to low memory. * - * ECX contains the base address of the trampoline memory. - * Non zero RDX means trampoline needs to enable 5-level paging. + * EDI contains the base address of the trampoline memory. + * Non-zero ESI means trampoline needs to enable 5-level paging. */ SYM_CODE_START(trampoline_32bit_src) - popq %rdi + popq %r8 /* Switch to compatibility mode (CS.L = 0 CS.D = 1) via far return */ pushq $__KERNEL32_CS leaq 0f(%rip), %rax @@ -552,7 +552,7 @@ SYM_CODE_START(trampoline_32bit_src) movl %eax, %ss /* Set up new stack */ - leal TRAMPOLINE_32BIT_STACK_END(%ecx), %esp + leal TRAMPOLINE_32BIT_STACK_END(%edi), %esp /* Disable paging */ movl %cr0, %eax @@ -560,7 +560,7 @@ SYM_CODE_START(trampoline_32bit_src) movl %eax, %cr0 /* Check what paging mode we want to be in after the trampoline */ - testl %edx, %edx + testl %esi, %esi jz 1f /* We want 5-level paging: don't touch CR3 if it already points to 5-level page tables */ @@ -575,21 +575,17 @@ SYM_CODE_START(trampoline_32bit_src) jz 3f 2: /* Point CR3 to the trampoline's new top level page table */ - leal TRAMPOLINE_32BIT_PGTABLE_OFFSET(%ecx), %eax + leal TRAMPOLINE_32BIT_PGTABLE_OFFSET(%edi), %eax movl %eax, %cr3 3: /* Set EFER.LME=1 as a precaution in case hypervsior pulls the rug */ - pushl %ecx - pushl %edx movl $MSR_EFER, %ecx rdmsr btsl $_EFER_LME, %eax /* Avoid writing EFER if no change was made (for TDX guest) */ jc 1f wrmsr -1: popl %edx - popl %ecx - +1: #ifdef CONFIG_X86_MCE /* * Preserve CR4.MCE if the kernel will enable #MC support. @@ -606,14 +602,14 @@ SYM_CODE_START(trampoline_32bit_src) /* Enable PAE and LA57 (if required) paging modes */ orl $X86_CR4_PAE, %eax - testl %edx, %edx + testl %esi, %esi jz 1f orl $X86_CR4_LA57, %eax 1: movl %eax, %cr4 /* Calculate address of paging_enabled() once we are executing in the trampoline */ - leal .Lpaging_enabled - trampoline_32bit_src + TRAMPOLINE_32BIT_CODE_OFFSET(%ecx), %eax + leal .Lpaging_enabled - trampoline_32bit_src + TRAMPOLINE_32BIT_CODE_OFFSET(%edi), %eax /* Prepare the stack for far return to Long Mode */ pushl $__KERNEL_CS @@ -630,7 +626,7 @@ SYM_CODE_END(trampoline_32bit_src) .code64 SYM_FUNC_START_LOCAL_NOALIGN(.Lpaging_enabled) /* Return from the trampoline */ - jmp *%rdi + jmp *%r8 SYM_FUNC_END(.Lpaging_enabled) /* diff --git a/arch/x86/boot/compressed/pgtable.h b/arch/x86/boot/compressed/pgtable.h index 91dbb99203fbce2d..4e8cef135226bcbb 100644 --- a/arch/x86/boot/compressed/pgtable.h +++ b/arch/x86/boot/compressed/pgtable.h @@ -14,7 +14,7 @@ extern unsigned long *trampoline_32bit; -extern void trampoline_32bit_src(void *return_ptr); +extern void trampoline_32bit_src(void *trampoline, bool enable_5lvl); #endif /* __ASSEMBLER__ */ #endif /* BOOT_COMPRESSED_PAGETABLE_H */ From patchwork Wed Jun 7 07:23:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104310 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp75789vqr; Wed, 7 Jun 2023 00:38:07 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6tq6Aq/9RHenpaGFsqIO9iphlfW+sMvXSlUkfqVPJP/8VQdl9J6o2T8s3diAXa3Nkgheba X-Received: by 2002:a17:90a:354:b0:258:9174:20ca with SMTP id 20-20020a17090a035400b00258917420camr4144539pjf.15.1686123487020; Wed, 07 Jun 2023 00:38:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123487; cv=none; d=google.com; s=arc-20160816; b=SwkZts//EekUNm+bMqOw8SkhmOaw7abuXlluc9tyfT78hZ1W3qib2TJoVfFPaS0IYC up6v7zPADOXvPJACf23Ai1VYkPd8aUh5REU0KxTCBu2uCfD2wBPooXnLGWNDqa/uk941 ZeAck+VPraztQe0OR5IRg7saClur3mv9KCfoNdBHLUls5dTsdjw0MuaNHcjs4jUaZmxZ l2xJokdBqr0iP6Xvrlv+wwgbSfEmZVNil187UELwcdBVz3EbTVkWuLjAy7p1/54i6lLS PHgivC+pHv9bNcfsgoGblPUyh10ww5vza7fXsJKVNVPjNcUiODw9XXE3JnkL2PKKthsk yvmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=qVXMF75ZNrTk+WlKqokJbxwehDFqqlzQVlkaBIty1wg=; b=ZcUXMCmXbz0zNfv4z/RA9hLWgb5FVvGqna8nJ/ZHMPb+XGvn/pQ+s8wSHV59CT3bLr Tgs2ILRRz6ORKdfb8Ivfo4xch3WgDquj1KhP9DDk1SS8ThVudX/5A+Y66TvPSW6ke6Sp cm7r/3L1bVrZRFcPM0g8UC0vqFBADeDhQSvDpTqsRuJrNhJW/BmpEkcNHotnVIn/sW84 1JI8g1JW+sveqxCITxa+uAxUqeGNLjMyuZI9QBJFbY1288DRL19NTgQ79xLS+6Z9s6fs jtYVG2n7/95a7WuaVwy1L3LX3HL4RfUuZtIoUcpOkBI/eLcuVyCw4KKqs0VhaJ6QjN0/ 3DAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=l7HWBXaJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g14-20020a17090a300e00b0024b27e48a27si727760pjb.74.2023.06.07.00.37.54; Wed, 07 Jun 2023 00:38:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=l7HWBXaJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239143AbjFGH0t (ORCPT + 99 others); Wed, 7 Jun 2023 03:26:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239218AbjFGH0J (ORCPT ); Wed, 7 Jun 2023 03:26:09 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51AC92733; Wed, 7 Jun 2023 00:25:02 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 2418C633D6; Wed, 7 Jun 2023 07:24:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B6175C433A1; Wed, 7 Jun 2023 07:24:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122674; bh=gxLsEkoDGg4mIvvhxqLtxwNQiYAezxQxa0+9h4F2jjU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=l7HWBXaJpWPhzKi3lc1wVD2Z2EMcOliBv5MFGMJ1SyEh8z85J1xHLu4d7/vkZhw2a +cGmaFx3D3obhq/m9tl4U6Mmb3tBjCkEHmRf99fT8s2L14/vUpSFp2Nb3gB2GNIW0v uGvap7ISzB0uWWzfIR0aiUYR1ANFA573hXyqcp+iL8SHWQrkf5pvV1QBs7HxJ2PxXl YRwb1Tbtvz5QvHa4v+KuBxvYWjWwIhWRwUpKeA+22m84nn0pgrjPTBAWKhiwppgR1G lRqDY9QNhI4n6Z5zbRoF8UG2zA88nLaMC5QQnDMZENKs9UjI+ZDZIDA+QdWFtY7uqc zkADpbx14Y/AQ== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 09/20] x86/decompressor: Avoid the need for a stack in the 32-bit trampoline Date: Wed, 7 Jun 2023 09:23:31 +0200 Message-Id: <20230607072342.4054036-10-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=6253; i=ardb@kernel.org; h=from:subject; bh=gxLsEkoDGg4mIvvhxqLtxwNQiYAezxQxa0+9h4F2jjU=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBIP9m3YK9Qs/ke1M2r0g+f+7EluZr2zY33ZJ6reAWI CzsHHmqo5SFQYyDQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAEyERYPhnzKXcvrqqbxaba5L i4TKnJb3L/DbMYn5paNX7OLnR861zWNkaJsi/X6lLsP5c96GqrU+n2pvvfDQeMd5aqfxWomuW4c uMAEA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028621672577391?= X-GMAIL-MSGID: =?utf-8?q?1768028621672577391?= The 32-bit trampoline no longer uses the stack for anything except performing a far return back to long mode. Currently, this stack is placed in the same page that carries the trampoline code, which means this page must be mapped writable and executable, and the stack is therefore executable as well. Replace the far return with a far jump, so that the return address can be pre-calculated and patched into the code before it is called. This removes the need for a stack entirely, and in a later patch, this will be taken advantage of by removing writable permissions from (and adding executable permissions to) this code page explicitly when booting via the EFI stub. Not touching the stack pointer also makes it more straight-forward to call the trampoline code as an ordinary 64-bit function from C code. Acked-by: Kirill A. Shutemov Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/head_64.S | 48 +++++++++----------- arch/x86/boot/compressed/pgtable.h | 6 +-- arch/x86/boot/compressed/pgtable_64.c | 12 ++++- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index a387cd80964e1a1e..cdefafd456c70335 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -449,9 +449,6 @@ SYM_CODE_START(startup_64) leaq TRAMPOLINE_32BIT_CODE_OFFSET(%rax), %rax call *%rax - /* Restore the stack, the 32-bit trampoline uses its own stack */ - leaq rva(boot_stack_end)(%rbx), %rsp - /* * cleanup_trampoline() would restore trampoline memory. * @@ -537,24 +534,22 @@ SYM_FUNC_END(.Lrelocated) * EDI contains the base address of the trampoline memory. * Non-zero ESI means trampoline needs to enable 5-level paging. */ + .section ".rodata", "a", @progbits SYM_CODE_START(trampoline_32bit_src) - popq %r8 /* Switch to compatibility mode (CS.L = 0 CS.D = 1) via far return */ pushq $__KERNEL32_CS leaq 0f(%rip), %rax pushq %rax lretq + /* + * The 32-bit code below will do a far jump back to long mode and end + * up here after reconfiguring the number of paging levels. + */ +.Lret: retq + .code32 -0: /* Set up data and stack segments */ - movl $__KERNEL_DS, %eax - movl %eax, %ds - movl %eax, %ss - - /* Set up new stack */ - leal TRAMPOLINE_32BIT_STACK_END(%edi), %esp - - /* Disable paging */ +0: /* Disable paging */ movl %cr0, %eax btrl $X86_CR0_PG_BIT, %eax movl %eax, %cr0 @@ -608,26 +603,25 @@ SYM_CODE_START(trampoline_32bit_src) 1: movl %eax, %cr4 - /* Calculate address of paging_enabled() once we are executing in the trampoline */ - leal .Lpaging_enabled - trampoline_32bit_src + TRAMPOLINE_32BIT_CODE_OFFSET(%edi), %eax - - /* Prepare the stack for far return to Long Mode */ - pushl $__KERNEL_CS - pushl %eax - /* Enable paging again. */ movl %cr0, %eax btsl $X86_CR0_PG_BIT, %eax movl %eax, %cr0 - lret + /* + * Return to the 64-bit calling code using LJMP rather than LRET, to + * avoid the need for a 32-bit addressable stack. The destination + * address will be adjusted after the template code is copied into a + * 32-bit addressable buffer. + */ +.Ljmp: ljmpl $__KERNEL_CS, $(.Lret - trampoline_32bit_src) SYM_CODE_END(trampoline_32bit_src) - .code64 -SYM_FUNC_START_LOCAL_NOALIGN(.Lpaging_enabled) - /* Return from the trampoline */ - jmp *%r8 -SYM_FUNC_END(.Lpaging_enabled) +/* + * This symbol is placed right after trampoline_32bit_src() so its address can + * be used to infer the size of the trampoline code. + */ +SYM_DATA(trampoline_ljmp_imm_offset, .word .Ljmp + 1 - trampoline_32bit_src) /* * The trampoline code has a size limit. @@ -636,7 +630,7 @@ SYM_FUNC_END(.Lpaging_enabled) */ .org trampoline_32bit_src + TRAMPOLINE_32BIT_CODE_SIZE - .code32 + .text SYM_FUNC_START_LOCAL_NOALIGN(.Lno_longmode) /* This isn't an x86-64 CPU, so hang intentionally, we cannot continue */ 1: diff --git a/arch/x86/boot/compressed/pgtable.h b/arch/x86/boot/compressed/pgtable.h index 4e8cef135226bcbb..131488f50af55d0a 100644 --- a/arch/x86/boot/compressed/pgtable.h +++ b/arch/x86/boot/compressed/pgtable.h @@ -6,9 +6,7 @@ #define TRAMPOLINE_32BIT_PGTABLE_OFFSET 0 #define TRAMPOLINE_32BIT_CODE_OFFSET PAGE_SIZE -#define TRAMPOLINE_32BIT_CODE_SIZE 0xA0 - -#define TRAMPOLINE_32BIT_STACK_END TRAMPOLINE_32BIT_SIZE +#define TRAMPOLINE_32BIT_CODE_SIZE 0x80 #ifndef __ASSEMBLER__ @@ -16,5 +14,7 @@ extern unsigned long *trampoline_32bit; extern void trampoline_32bit_src(void *trampoline, bool enable_5lvl); +extern const u16 trampoline_ljmp_imm_offset; + #endif /* __ASSEMBLER__ */ #endif /* BOOT_COMPRESSED_PAGETABLE_H */ diff --git a/arch/x86/boot/compressed/pgtable_64.c b/arch/x86/boot/compressed/pgtable_64.c index 2ac12ff4111bf8c0..d66639c961b8eeda 100644 --- a/arch/x86/boot/compressed/pgtable_64.c +++ b/arch/x86/boot/compressed/pgtable_64.c @@ -109,6 +109,7 @@ static unsigned long find_trampoline_placement(void) struct paging_config paging_prepare(void *rmode) { struct paging_config paging_config = {}; + void *tramp_code; /* Initialize boot_params. Required for cmdline_find_option_bool(). */ boot_params = rmode; @@ -143,9 +144,18 @@ struct paging_config paging_prepare(void *rmode) memset(trampoline_32bit, 0, TRAMPOLINE_32BIT_SIZE); /* Copy trampoline code in place */ - memcpy(trampoline_32bit + TRAMPOLINE_32BIT_CODE_OFFSET / sizeof(unsigned long), + tramp_code = memcpy(trampoline_32bit + + TRAMPOLINE_32BIT_CODE_OFFSET / sizeof(unsigned long), &trampoline_32bit_src, TRAMPOLINE_32BIT_CODE_SIZE); + /* + * Avoid the need for a stack in the 32-bit trampoline code, by using + * LJMP rather than LRET to return back to long mode. LJMP takes an + * immediate absolute address, which needs to be adjusted based on the + * placement of the trampoline. + */ + *(u32 *)(tramp_code + trampoline_ljmp_imm_offset) += (unsigned long)tramp_code; + /* * The code below prepares page table in trampoline memory. * From patchwork Wed Jun 7 07:23:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104309 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp75679vqr; Wed, 7 Jun 2023 00:37:54 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5Cd/8yCfsp28wKgdyfRYYIwegF/iXXVqcuwSUgyiuxhVggOphQgqTY01SWr3/E3Z+LIA4/ X-Received: by 2002:a05:6870:e742:b0:19a:5f5a:a32f with SMTP id t2-20020a056870e74200b0019a5f5aa32fmr3714844oak.1.1686123473835; Wed, 07 Jun 2023 00:37:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123473; cv=none; d=google.com; s=arc-20160816; b=lkI7sSWeEE0LDNwOnj7xHufLyhvJW2f1zXJZ2nXiZMr0qf7P3ppzFBVGHghyY9TJv8 uSdstr35xvzrG1lC6JvxYXA6atHVm0Hxp9HoFfhEYDrOo/SyDhMrufh5fkPhbKb5aqE5 lBdPghVd8xiuH06yiOUkUJjWBcd6rXkvIzbxzcdOhU/ziauME2+7yRedqXgINydhwfkb GwGdOUy1qzU059R8iRGNq8udh2KIvTqfhPqKSCp6qIzwinOGpE0Zs8WWGsadbzngqQef aEJiHa9nkKzCEzI+qXtHzqGC3IvWCgMxreQRAiYv3/RMdCTtB/MhepWWtztzFJQs5nE/ 9yXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=7yfG1UIYjNX0n1C+Qc7b/72wdkGfnC/v9kXmtLkHgfw=; b=PBfVdBXm4s/WtMXCPu31ghhHqirm13drF0pZ05IxE4rvD6yreMy/nYeRArS65y7Sgc jP54i7lAuWWzLYuECN5n7gPhTvRmyU5VORYYB7/VPZK7kvcymqONBwI7bN9J1gfoWH3D MB2DjyjPbZNCeU90l8+O06ClLafYmINdhq/q9h9lzoYk5kfbuvaUodk0CluxOa0paYL9 m8xCtNLiEpOLYO53ZxSFEfwTfz87rkuYBdUu4SYFISnP3NWwAg8OCZLkFyIs7XqRixKb mVjLyZq+lHKjAfb88q2tuyGB6gJNlJPT4G8vEO5+d6uHyDLBHwjfwVFVsvIYVXyW+Ky2 mgNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RWG4jlIZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j12-20020a636e0c000000b0053ef158b0c3si8441422pgc.25.2023.06.07.00.37.40; Wed, 07 Jun 2023 00:37:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RWG4jlIZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233418AbjFGH1M (ORCPT + 99 others); Wed, 7 Jun 2023 03:27:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239094AbjFGH0Q (ORCPT ); Wed, 7 Jun 2023 03:26:16 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC637273E; Wed, 7 Jun 2023 00:25:02 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 56A2F633AD; Wed, 7 Jun 2023 07:24:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F2D84C4339C; Wed, 7 Jun 2023 07:24:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122678; bh=RE5W68XPDnGbVeBBhzYfWFU/rF3D5/t2hy2u0mxg2G8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RWG4jlIZQdL1k+w4KLt2n/kvjWrWaov1+Y68sakYEaari1qauw2LHLEkufbWqKJFI xXZecxc/yF9QgJ7x5xSRqTQTNrYgDMNBKWtUACMwHvz1xNfZ2uIIvIGZAKyJxdnD6f QzDd3hVsung79G2vuB+KEnQ8vdvVA4eeSp8MIjQiyqfn5IK3bYtn21ej3dD0pyKVRO jgu+TNabvE4AuMSrLEyYS429uKN1pFGX9hx+APOEoG7hy9kQV+cI4xsWhzf55zHAv7 x9J/kE+wJ+is8vhZdyvvCTpKK16YjzflfQKM3bb7lj4kYN+TuGxcVMn+VwvdOvEkbT iXB5AEzrp16Ow== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 10/20] x86/decompressor: Call trampoline directly from C code Date: Wed, 7 Jun 2023 09:23:32 +0200 Message-Id: <20230607072342.4054036-11-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4967; i=ardb@kernel.org; h=from:subject; bh=RE5W68XPDnGbVeBBhzYfWFU/rF3D5/t2hy2u0mxg2G8=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBoIDntVbaGYNZF05+sOFs4/9fbFG/J2ci3y7xstQHa wyWvNLqKGVhEONgkBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABPZac/IcHT6wfvuCzqYQtm8 5p/yNOfat+FKT3SVjenlky/bb5Y2WTIyTDWK3W/aMfkuT+YFS/N1CWLtN1b8ZQu9cnz5srAnFZ4 nWQA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028607399566687?= X-GMAIL-MSGID: =?utf-8?q?1768028607399566687?= Instead of returning to the asm calling code to invoke the trampoline, call it straight from the C code that sets the scene. That way, the struct return type is no longer needed for returning two values, and the call can be made conditional more cleanly in a subsequent patch. Acked-by: Kirill A. Shutemov Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/head_64.S | 20 +++----------- arch/x86/boot/compressed/pgtable_64.c | 28 ++++++++------------ 2 files changed, 15 insertions(+), 33 deletions(-) diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index cdefafd456c70335..3d4da7e5270c8d4d 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -430,24 +430,12 @@ SYM_CODE_START(startup_64) #endif /* - * paging_prepare() sets up the trampoline and checks if we need to - * enable 5-level paging. - * - * paging_prepare() returns a two-quadword structure which lands - * into RDX:RAX: - * - Address of the trampoline is returned in RAX. - * - Non zero RDX means trampoline needs to enable 5-level - * paging. - * + * set_paging_levels() updates the number of paging levels using a + * trampoline in 32-bit addressable memory if the current number does + * not match the desired number. */ movq %r15, %rdi /* pass struct boot_params pointer */ - call paging_prepare - - /* Pass the trampoline address and boolean flag as args #1 and #2 */ - movq %rax, %rdi - movq %rdx, %rsi - leaq TRAMPOLINE_32BIT_CODE_OFFSET(%rax), %rax - call *%rax + call set_paging_levels /* * cleanup_trampoline() would restore trampoline memory. diff --git a/arch/x86/boot/compressed/pgtable_64.c b/arch/x86/boot/compressed/pgtable_64.c index d66639c961b8eeda..1d28ad95ea839531 100644 --- a/arch/x86/boot/compressed/pgtable_64.c +++ b/arch/x86/boot/compressed/pgtable_64.c @@ -16,11 +16,6 @@ unsigned int __section(".data") pgdir_shift = 39; unsigned int __section(".data") ptrs_per_p4d = 1; #endif -struct paging_config { - unsigned long trampoline_start; - unsigned long l5_required; -}; - /* Buffer to preserve trampoline memory */ static char trampoline_save[TRAMPOLINE_32BIT_SIZE]; @@ -106,10 +101,10 @@ static unsigned long find_trampoline_placement(void) return bios_start - TRAMPOLINE_32BIT_SIZE; } -struct paging_config paging_prepare(void *rmode) +asmlinkage void set_paging_levels(void *rmode) { - struct paging_config paging_config = {}; - void *tramp_code; + void (*toggle_la57)(void *trampoline, bool enable_5lvl); + bool l5_required = false; /* Initialize boot_params. Required for cmdline_find_option_bool(). */ boot_params = rmode; @@ -130,12 +125,10 @@ struct paging_config paging_prepare(void *rmode) !cmdline_find_option_bool("no5lvl") && native_cpuid_eax(0) >= 7 && (native_cpuid_ecx(7) & (1 << (X86_FEATURE_LA57 & 31)))) { - paging_config.l5_required = 1; + l5_required = true; } - paging_config.trampoline_start = find_trampoline_placement(); - - trampoline_32bit = (unsigned long *)paging_config.trampoline_start; + trampoline_32bit = (unsigned long *)find_trampoline_placement(); /* Preserve trampoline memory */ memcpy(trampoline_save, trampoline_32bit, TRAMPOLINE_32BIT_SIZE); @@ -144,7 +137,7 @@ struct paging_config paging_prepare(void *rmode) memset(trampoline_32bit, 0, TRAMPOLINE_32BIT_SIZE); /* Copy trampoline code in place */ - tramp_code = memcpy(trampoline_32bit + + toggle_la57 = memcpy(trampoline_32bit + TRAMPOLINE_32BIT_CODE_OFFSET / sizeof(unsigned long), &trampoline_32bit_src, TRAMPOLINE_32BIT_CODE_SIZE); @@ -154,7 +147,8 @@ struct paging_config paging_prepare(void *rmode) * immediate absolute address, which needs to be adjusted based on the * placement of the trampoline. */ - *(u32 *)(tramp_code + trampoline_ljmp_imm_offset) += (unsigned long)tramp_code; + *(u32 *)((u8 *)toggle_la57 + trampoline_ljmp_imm_offset) += + (unsigned long)toggle_la57; /* * The code below prepares page table in trampoline memory. @@ -170,10 +164,10 @@ struct paging_config paging_prepare(void *rmode) * We are not going to use the page table in trampoline memory if we * are already in the desired paging mode. */ - if (paging_config.l5_required == !!(native_read_cr4() & X86_CR4_LA57)) + if (l5_required == !!(native_read_cr4() & X86_CR4_LA57)) goto out; - if (paging_config.l5_required) { + if (l5_required) { /* * For 4- to 5-level paging transition, set up current CR3 as * the first and the only entry in a new top-level page table. @@ -196,7 +190,7 @@ struct paging_config paging_prepare(void *rmode) } out: - return paging_config; + toggle_la57(trampoline_32bit, l5_required); } void cleanup_trampoline(void *pgtable) From patchwork Wed Jun 7 07:23:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104297 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp75099vqr; Wed, 7 Jun 2023 00:36:31 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7X3Y266iBrAaZlrovNwiKqp9hSPY9977DPXEjEgvbp8fKzt4OayV1Z9qXJnr6GZbLgw/7K X-Received: by 2002:a92:d9cf:0:b0:32b:1536:f3e9 with SMTP id n15-20020a92d9cf000000b0032b1536f3e9mr6099519ilq.18.1686123391559; Wed, 07 Jun 2023 00:36:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123391; cv=none; d=google.com; s=arc-20160816; b=EtindfOgxUWDJ7ckziJnMTAjRLwKpR5yLPgTbe/exI1obCZDLDsr7ZnIeoFf5K/qBC HiiAI883nGtbSMQjGC1FCJUqaS5xI8ON5OHT38kIV6vjOUxzHWRbRYWXYe8DjH5xf5vt FNgiyo5Hlr03+z8H03/RZwtTGXcRdd7HkgOC22f2DlC02HjmoqJOf7neDirapf/s3yyU aHbFKZ90J3k7Ln4oegj47N+3hR3Y0ekj0ynqNwoJKGWEdeahBWxV2Rl2AmcHFKulcRPK QqAW7KCW0ZDk4ZvYMIZ9yij36sv9D8g2U1w7mmUmJvsjmFBXPuRZWVodSAVLrwCESCcB IzSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=uMDWIDF77iuBD0RJXEdt/pezidON+Mzi6E+7aSfz3v0=; b=KA/IgY9a5iwRuK+xV0Bhoj1Ip5YUzeOlyCaz88p9G1POODVBuJRE7A5bpaN7v4TLzi vFI1NyWO1azE1G+npjJoYdlB/+ZklkVWe+f/pWCy2foGlFZlk3TqJk/0qeusx7Zy5fnm JNdR3pO0MRxngQuGXzQ14NCyGjZ//LVJej6bGJOCp0IzndewEdM3nFv6zaCVSlkzlUxp LeYM4Tu68Ltdpx+SRU09H8yYdiikRsgiRLH13Uo/FChFlG18RluiSQS9trD+5efp4hEC 4ps7JJaKFc+9W7jiBgkg4lL6azEKQxsxNsraRa0oF5zCzJD5sgFef+D2BDaNaa/q6aq/ k5BA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=sf3muz+U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t3-20020a639543000000b0053f955eda7csi3585288pgn.559.2023.06.07.00.36.17; Wed, 07 Jun 2023 00:36:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=sf3muz+U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238886AbjFGH1p (ORCPT + 99 others); Wed, 7 Jun 2023 03:27:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239234AbjFGH00 (ORCPT ); Wed, 7 Jun 2023 03:26:26 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA9EF2D48; Wed, 7 Jun 2023 00:25:03 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 9A81A61587; Wed, 7 Jun 2023 07:24:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 39C82C4339E; Wed, 7 Jun 2023 07:24:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122683; bh=PsQ8al6sNOe3j1ZfxUsRkdXdICx8owngaMHdAfuDB5Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sf3muz+U21ReBQT3a26E0Xc0gHR5+q0bZNlfAFZDNmrqxF45a2W+qlmcOKFt8YLx6 cIKC7efyPs4A+isbwJUTaaTwRZc40X3tSIkTR+7/w4ctUC1a6+b8x8EuELhEt4R7Aa X3wE3PIilL8K6RfvgtaupRHNRoQVCTIvZqPeGU33ZndhKo9UTGsUJgmOXxP0CStqTA 9vTbMOAEwdfS/nHYfQ5J7Zhf705WCAfJoi8SqB8eZRSmpJC/aJ6mPOQxkldq8hbF5n ftbVm+VTfJA1KNmcpRDn+Oo4BNkwpFjx3t2HROVveiTb+o+GNL/zGTQCRKwGD8RYLz Qvkpwqs7wt8Dg== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 11/20] x86/decompressor: Only call the trampoline when changing paging levels Date: Wed, 7 Jun 2023 09:23:33 +0200 Message-Id: <20230607072342.4054036-12-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3740; i=ardb@kernel.org; h=from:subject; bh=PsQ8al6sNOe3j1ZfxUsRkdXdICx8owngaMHdAfuDB5Q=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBoEim8mh/RsPHj1ZOoj/7a5ce0ry+ZF/YMYsna48+5 C1LsLftKGVhEONgkBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABN58ZSRYYc6b9+P+09Zbucd 4rq9UN96o5hmV/Klzyom9mXsoYvFJjMy/D0SkDThzY/6mS4mXqsnPlNc9u9dvKLC/VN//v578P/ DNxYA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028521697359214?= X-GMAIL-MSGID: =?utf-8?q?1768028521697359214?= Since the current and desired number of paging levels are known when the trampoline is being prepared, avoid calling the trampoline at all if it is clear that calling it is not going to result in a change to the number of paging levels. Given that the CPU is already running in long mode, the PAE and LA57 settings are necessarily consistent with the currently active page tables - the only difference is that CR4.MCE will always be preserved in this case, but it will be cleared by the real kernel startup code if CONFIG_X86_MCE is not enabled. Acked-by: Kirill A. Shutemov Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/head_64.S | 21 +------------------- arch/x86/boot/compressed/pgtable_64.c | 18 +++++++---------- 2 files changed, 8 insertions(+), 31 deletions(-) diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index 3d4da7e5270c8d4d..577173be8ec805cd 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -387,10 +387,6 @@ SYM_CODE_START(startup_64) * For the trampoline, we need the top page table to reside in lower * memory as we don't have a way to load 64-bit values into CR3 in * 32-bit mode. - * - * We go though the trampoline even if we don't have to: if we're - * already in a desired paging mode. This way the trampoline code gets - * tested on every boot. */ /* Make sure we have GDT with 32-bit code segment */ @@ -542,25 +538,10 @@ SYM_CODE_START(trampoline_32bit_src) btrl $X86_CR0_PG_BIT, %eax movl %eax, %cr0 - /* Check what paging mode we want to be in after the trampoline */ - testl %esi, %esi - jz 1f - - /* We want 5-level paging: don't touch CR3 if it already points to 5-level page tables */ - movl %cr4, %eax - testl $X86_CR4_LA57, %eax - jnz 3f - jmp 2f -1: - /* We want 4-level paging: don't touch CR3 if it already points to 4-level page tables */ - movl %cr4, %eax - testl $X86_CR4_LA57, %eax - jz 3f -2: /* Point CR3 to the trampoline's new top level page table */ leal TRAMPOLINE_32BIT_PGTABLE_OFFSET(%edi), %eax movl %eax, %cr3 -3: + /* Set EFER.LME=1 as a precaution in case hypervsior pulls the rug */ movl $MSR_EFER, %ecx rdmsr diff --git a/arch/x86/boot/compressed/pgtable_64.c b/arch/x86/boot/compressed/pgtable_64.c index 1d28ad95ea839531..5b15d823e7010650 100644 --- a/arch/x86/boot/compressed/pgtable_64.c +++ b/arch/x86/boot/compressed/pgtable_64.c @@ -128,6 +128,13 @@ asmlinkage void set_paging_levels(void *rmode) l5_required = true; } + /* + * The trampoline will not be used if the paging mode is already set to + * the desired one. + */ + if (l5_required == !!(native_read_cr4() & X86_CR4_LA57)) + return; + trampoline_32bit = (unsigned long *)find_trampoline_placement(); /* Preserve trampoline memory */ @@ -155,18 +162,8 @@ asmlinkage void set_paging_levels(void *rmode) * * The new page table will be used by trampoline code for switching * from 4- to 5-level paging or vice versa. - * - * If switching is not required, the page table is unused: trampoline - * code wouldn't touch CR3. */ - /* - * We are not going to use the page table in trampoline memory if we - * are already in the desired paging mode. - */ - if (l5_required == !!(native_read_cr4() & X86_CR4_LA57)) - goto out; - if (l5_required) { /* * For 4- to 5-level paging transition, set up current CR3 as @@ -189,7 +186,6 @@ asmlinkage void set_paging_levels(void *rmode) (void *)src, PAGE_SIZE); } -out: toggle_la57(trampoline_32bit, l5_required); } From patchwork Wed Jun 7 07:23:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104311 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp75848vqr; Wed, 7 Jun 2023 00:38:15 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4m9sPPEYXjPurS/9C5SYMRAHJ33NYIxR4CRZ96+0lZUah/9WstO2Lp567vxo5++PupHSXH X-Received: by 2002:a6b:f219:0:b0:76f:1664:672 with SMTP id q25-20020a6bf219000000b0076f16640672mr7004244ioh.13.1686123495453; Wed, 07 Jun 2023 00:38:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123495; cv=none; d=google.com; s=arc-20160816; b=CjBJc7OiIINs37l4gHAFaT3ppMEkI2VCGvDrIlwmlv2L8yglA3scys6dncPZhzoxG3 EwzwQ1Rqjq8Fyx9HnpViQtDdmkGitw0uCunzFaoB3fJ4Ez7S8Y/rGReexauMBIp7oo8w gvoduiZhJhUjrDNfdPEWVrVri+sc/D2TCFFenvJn9lw+1LnBkHs4WtrzOQWRI5HnQPCb xYr6LZYYbiLLmyFndPzqpF/wl3VX1ZPTLIY1AFmHkOu1Zvmg/z34oUn3aJKhLmMOrak5 /O6rPwIMRGBJqxI7nOysJvbu4U7C53XjCB4Txtqa8vrJxBB+iGqJLusD90l0Y/XJ+Zjc 62lQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=3sFTGEyidjqUSDQclhSdRmPJgW79qg+h5+aPdA18W0w=; b=WQ7iezUpp6nYElMwbKMtAlXgMegCl5xgbVdVTOgfkh2VV+51wq4pjbOvlwbBMS2Hq/ jov5/QxCupXqcTkWMY4bk3TdEDrwHLy5wWaLNCH0IYDFazZLP3szv8Tg6Nt9chI4I331 dl89er6p1I6kVxymBLpLyThoZ5quwh2hMLFK0kXyRVMZOM/7LfERA75wNqTm4E25mwcw t+fQK0XSxHFX/xRgpDa4m5/jaiqcb+LW0Qc9jeS/uwGosGcH0OlHKxr8wlMsMG3MIl1k tH5xt8E+J7xZdRdesvSftVjdKm135e0ZyxX5SgHxoO6SA8ybKCf8Yyd/jyNWB1KRtyyJ RbPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="j+a5A/m9"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k62-20020a638441000000b00530b7eca08csi8062301pgd.524.2023.06.07.00.38.02; Wed, 07 Jun 2023 00:38:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="j+a5A/m9"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239074AbjFGH1D (ORCPT + 99 others); Wed, 7 Jun 2023 03:27:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239079AbjFGH0P (ORCPT ); Wed, 7 Jun 2023 03:26:15 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97D66211C; Wed, 7 Jun 2023 00:25:02 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id D2D3A63B5A; Wed, 7 Jun 2023 07:24:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74667C433EF; Wed, 7 Jun 2023 07:24:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122687; bh=wnXABypBwYJfJbzxjxAgP1dA1CDsVnpCOGP4ksJdq04=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=j+a5A/m9mZptatmMzhz8kiPsT+ye0NHVPOuZ1H5eLzeK26KE+nmSKD4VLA4+Ennp7 ZIQYR2/pVjBFXPIC+7EZjrDxgM67Qwh+yorm4fTQq5zTR1/7pQ3VKuUVTkym+J5xUR hithePsc1BVzC50MnNPgdVm2o829/C22S4T7q0lw6+59NQC2PgngVxaoWhh3tuomLK ZqIatIas7wD9F5gFlly+KG3PD+EuaRpAeXQN1ndjqvOz2PRZl83VzNTKi0MjhIECEt kD9PaeO/F6jsTXgarZuE43Kzr6RqhyoLaL44gInTo0nQFMIQ0Sp2LlgDuKABVWn17W ICbu9mA5kV8uA== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 12/20] x86/decompressor: Merge trampoline cleanup with switching code Date: Wed, 7 Jun 2023 09:23:34 +0200 Message-Id: <20230607072342.4054036-13-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4143; i=ardb@kernel.org; h=from:subject; bh=wnXABypBwYJfJbzxjxAgP1dA1CDsVnpCOGP4ksJdq04=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBoNg9Tr8jkHXB0eUF3zJmXJJPa2O/5py2VuZZ9cRvT 4/W6Yh3lLIwiHEwyIopsgjM/vtu5+mJUrXOs2Rh5rAygQxh4OIUgIkcM2dk+HGqm1OAK5S97m7m wmvdU/Tjd27ZKFE67WlAwbuyqpWC2xkZLsQGCXzJ+uIevM7kWsrFb24PekT2OPJcfBWc4/Y1p8q IAwA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028629852329264?= X-GMAIL-MSGID: =?utf-8?q?1768028629852329264?= Now that the trampoline setup code and the actual invocation of it are all done from the C routine, the trampoline cleanup can be merged into it as well, instead of returning to asm just to call another C function. Acked-by: Kirill A. Shutemov Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/head_64.S | 13 +++------ arch/x86/boot/compressed/pgtable_64.c | 28 ++++++++------------ 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index 577173be8ec805cd..408c7824b647ff51 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -429,19 +429,14 @@ SYM_CODE_START(startup_64) * set_paging_levels() updates the number of paging levels using a * trampoline in 32-bit addressable memory if the current number does * not match the desired number. + * + * RSI is the relocated address of the page table to use instead of + * page table in trampoline memory (if required). */ movq %r15, %rdi /* pass struct boot_params pointer */ + leaq rva(top_pgtable)(%rbx), %rsi call set_paging_levels - /* - * cleanup_trampoline() would restore trampoline memory. - * - * RDI is address of the page table to use instead of page table - * in trampoline memory (if required). - */ - leaq rva(top_pgtable)(%rbx), %rdi - call cleanup_trampoline - /* Zero EFLAGS */ pushq $0 popfq diff --git a/arch/x86/boot/compressed/pgtable_64.c b/arch/x86/boot/compressed/pgtable_64.c index 5b15d823e7010650..b9a8aa1ff6d7515c 100644 --- a/arch/x86/boot/compressed/pgtable_64.c +++ b/arch/x86/boot/compressed/pgtable_64.c @@ -101,9 +101,10 @@ static unsigned long find_trampoline_placement(void) return bios_start - TRAMPOLINE_32BIT_SIZE; } -asmlinkage void set_paging_levels(void *rmode) +asmlinkage void set_paging_levels(void *rmode, void *pgtable) { void (*toggle_la57)(void *trampoline, bool enable_5lvl); + void *trampoline_pgtable; bool l5_required = false; /* Initialize boot_params. Required for cmdline_find_option_bool(). */ @@ -133,7 +134,7 @@ asmlinkage void set_paging_levels(void *rmode) * the desired one. */ if (l5_required == !!(native_read_cr4() & X86_CR4_LA57)) - return; + goto out; trampoline_32bit = (unsigned long *)find_trampoline_placement(); @@ -163,6 +164,8 @@ asmlinkage void set_paging_levels(void *rmode) * The new page table will be used by trampoline code for switching * from 4- to 5-level paging or vice versa. */ + trampoline_pgtable = trampoline_32bit + + TRAMPOLINE_32BIT_PGTABLE_OFFSET / sizeof(unsigned long); if (l5_required) { /* @@ -182,31 +185,21 @@ asmlinkage void set_paging_levels(void *rmode) * may be above 4G. */ src = *(unsigned long *)__native_read_cr3() & PAGE_MASK; - memcpy(trampoline_32bit + TRAMPOLINE_32BIT_PGTABLE_OFFSET / sizeof(unsigned long), - (void *)src, PAGE_SIZE); + memcpy(trampoline_pgtable, (void *)src, PAGE_SIZE); } toggle_la57(trampoline_32bit, l5_required); -} - -void cleanup_trampoline(void *pgtable) -{ - void *trampoline_pgtable; - - trampoline_pgtable = trampoline_32bit + TRAMPOLINE_32BIT_PGTABLE_OFFSET / sizeof(unsigned long); /* - * Move the top level page table out of trampoline memory, - * if it's there. + * Move the top level page table out of trampoline memory. */ - if ((void *)__native_read_cr3() == trampoline_pgtable) { - memcpy(pgtable, trampoline_pgtable, PAGE_SIZE); - native_write_cr3((unsigned long)pgtable); - } + memcpy(pgtable, trampoline_pgtable, PAGE_SIZE); + native_write_cr3((unsigned long)pgtable); /* Restore trampoline memory */ memcpy(trampoline_32bit, trampoline_save, TRAMPOLINE_32BIT_SIZE); +out: /* Initialize variables for 5-level paging */ #ifdef CONFIG_X86_5LEVEL if (__read_cr4() & X86_CR4_LA57) { @@ -215,4 +208,5 @@ void cleanup_trampoline(void *pgtable) ptrs_per_p4d = 512; } #endif + return; } From patchwork Wed Jun 7 07:23:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104313 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp76064vqr; Wed, 7 Jun 2023 00:38:48 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ75k8MWXe6ZXwy8kpbIAwca36gIbnk6LMpa3n98BZ1ED6N288AvWSgA+QSk0g1zsvbh11GW X-Received: by 2002:a81:a511:0:b0:565:9105:d043 with SMTP id u17-20020a81a511000000b005659105d043mr6253493ywg.9.1686123528447; Wed, 07 Jun 2023 00:38:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123528; cv=none; d=google.com; s=arc-20160816; b=iM9Y5kQtXcssgjdVXDQ4KSIcdtHl1OJ88IBTDdaXE8ilDzwTpSEaB8eiqAztj9w1MR YfX6Vn4nwNuiztLG4Olc5/Y94jAZ+FWUpJhFKtH4tIUVJWVifRfPbgZz+Enasjw0PV9d n89Lp9NTFKQKSpB1pw9J2JzPrw2i5Ub8er+8aFEe3Z42ZYmYJQgP6mYkkixGCTdWWxXu 36gSnQCchpkvySl3TAy4s3xK1s/Voo0rERbLznq9Av2cR/34HkSBGRRThGzAvXwoZzp8 +oKCeAWPV/fgJ716RnAoq50TH2ti0Lsh7sWo8TJxK8m0n+cR10o8QthhAgvE0NArXCqu KEBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=C7mrlOPiDtxYQ1qWZmuVTUwdDCSWhd5NFZmzKN9db4M=; b=QaA20y1hZt0D6RD+QO6hE3A6pWST12zTH89qaxiCtGYeXugkQYFB3pXwhve60+YWf4 Vd9mQ1O3uMnuFS10vbS1OPbqSoY9YBk24Xai649Mse3YUlnegXtKX8ukp7CJkYmA4PaW VS0ka4ybxvhx/zUPzmbYnLOsGgEBLHCeEJRzmdz9riX6pJAvboxdFb5LVno04j5nNeX2 hViTW1dTm/2eW2yFPKOk/ruiFcx7WgJM/HcUJeNHJOFRst/FdV9+XAwzc3zhXl8YJjSq sFDbNphp/07pVoMOYgmkPPneX7b7hFeuzzNZ1zNUfYoD9EReVPR2gMop04PMPAYWnI07 6HUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fKDpVR5j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f127-20020a625185000000b00625c6092a12si8339947pfb.215.2023.06.07.00.38.35; Wed, 07 Jun 2023 00:38:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fKDpVR5j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239055AbjFGH0J (ORCPT + 99 others); Wed, 7 Jun 2023 03:26:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238984AbjFGHZn (ORCPT ); Wed, 7 Jun 2023 03:25:43 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC82119BA; Wed, 7 Jun 2023 00:24:53 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 1979663B59; Wed, 7 Jun 2023 07:24:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AF8E5C4339E; Wed, 7 Jun 2023 07:24:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122691; bh=VwNUPlSJTEZ1Nu/b8lVG8H0h0GBUJ6Yb+ifMNnx28Zw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fKDpVR5jLbnX8a+137NAPmMCbjLYBt9XjKgffVppjqYgpoYfrmjr501HOvXaGFHc1 VSqH/bjCdPadhGNU6QKGo/V/89F5bddoNMySymTCEEpQDLdVy+K/5PbwwtaV+7MHON Yiufyx3NcBlj0ku0Ni5OTl5qNUGRiqzq6utRBR++h5QGK/8/h2xgijyoktT6lwLBXl yBRMYYSyEOqX6fq5DsihyrD2gHJ7iCtMQjZv6uwILEzhp0NKXo0h+DDvNnY4qVAMyt 1zjJjm/oXBp7kLC/T7pnwF1G0U0ntGNUl3jJP9lcpnzPxrfAGH5MacREs6p2tmq/aW mPk+yb+PBvqJw== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 13/20] x86/efistub: Perform 4/5 level paging switch from the stub Date: Wed, 7 Jun 2023 09:23:35 +0200 Message-Id: <20230607072342.4054036-14-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=10131; i=ardb@kernel.org; h=from:subject; bh=VwNUPlSJTEZ1Nu/b8lVG8H0h0GBUJ6Yb+ifMNnx28Zw=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBoOTteaUlqp+vs726cO3IwuMzTxwL2yBypuPzFqmQy 3u/fV+5saOUhUGMg0FWTJFFYPbfdztPT5SqdZ4lCzOHlQlkCAMXpwDcZDNGhil9z75HB2R0iSW0 y+R09/+NljUJign7cVg9yOLSfMO1yxkZtixSeJ/Es3cZh8Om/fn2V9+LTF5fGdY9XfzjlNp+1Y/ L2QA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028665101052741?= X-GMAIL-MSGID: =?utf-8?q?1768028665101052741?= In preparation for updating the EFI stub boot flow to avoid the bare metal decompressor code altogether, implement the support code for switching between 4 and 5 levels of paging before jumping to the kernel proper. This reuses the newly refactored trampoline that the bare metal decompressor uses, but relies on EFI APIs to allocate 32-bit addressable memory and remap it with the appropriate permissions. Given that the bare metal decompressor will no longer call into the trampoline if the number of paging levels is already set correctly, it is no longer needed to remove NX restrictions from the memory range where this trampoline may end up. Acked-by: Kirill A. Shutemov Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/Makefile | 1 + drivers/firmware/efi/libstub/efi-stub-helper.c | 2 + drivers/firmware/efi/libstub/efistub.h | 1 + drivers/firmware/efi/libstub/x86-5lvl.c | 95 ++++++++++++++++++++ drivers/firmware/efi/libstub/x86-stub.c | 40 +++------ drivers/firmware/efi/libstub/x86-stub.h | 17 ++++ 6 files changed, 130 insertions(+), 26 deletions(-) diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile index 16d64a34d1e19465..ae8874401a9f1490 100644 --- a/drivers/firmware/efi/libstub/Makefile +++ b/drivers/firmware/efi/libstub/Makefile @@ -88,6 +88,7 @@ lib-$(CONFIG_EFI_GENERIC_STUB) += efi-stub.o string.o intrinsics.o systable.o \ lib-$(CONFIG_ARM) += arm32-stub.o lib-$(CONFIG_ARM64) += arm64.o arm64-stub.o smbios.o lib-$(CONFIG_X86) += x86-stub.o +lib-$(CONFIG_X86_64) += x86-5lvl.o lib-$(CONFIG_RISCV) += riscv.o riscv-stub.o lib-$(CONFIG_LOONGARCH) += loongarch.o loongarch-stub.o diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c index 1e0203d74691ffcc..51779279fbff21b5 100644 --- a/drivers/firmware/efi/libstub/efi-stub-helper.c +++ b/drivers/firmware/efi/libstub/efi-stub-helper.c @@ -73,6 +73,8 @@ efi_status_t efi_parse_options(char const *cmdline) efi_loglevel = CONSOLE_LOGLEVEL_QUIET; } else if (!strcmp(param, "noinitrd")) { efi_noinitrd = true; + } else if (IS_ENABLED(CONFIG_X86_64) && !strcmp(param, "no5lvl")) { + efi_no5lvl = true; } else if (!strcmp(param, "efi") && val) { efi_nochunk = parse_option_str(val, "nochunk"); efi_novamap |= parse_option_str(val, "novamap"); diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h index 6aa38a1bf1265d83..06b7abc92ced9e18 100644 --- a/drivers/firmware/efi/libstub/efistub.h +++ b/drivers/firmware/efi/libstub/efistub.h @@ -33,6 +33,7 @@ #define EFI_ALLOC_LIMIT ULONG_MAX #endif +extern bool efi_no5lvl; extern bool efi_nochunk; extern bool efi_nokaslr; extern int efi_loglevel; diff --git a/drivers/firmware/efi/libstub/x86-5lvl.c b/drivers/firmware/efi/libstub/x86-5lvl.c new file mode 100644 index 0000000000000000..2428578a3ae08be7 --- /dev/null +++ b/drivers/firmware/efi/libstub/x86-5lvl.c @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include + +#include +#include +#include + +#include "efistub.h" +#include "x86-stub.h" + +bool efi_no5lvl; + +static void (*la57_toggle)(void *trampoline, bool enable_5lvl); + +static const struct desc_struct gdt[] = { + [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff), + [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff), +}; + +/* + * Enabling (or disabling) 5 level paging is tricky, because it can only be + * done from 32-bit mode with paging disabled. This means not only that the + * code itself must be running from 32-bit addressable physical memory, but + * also that the root page table must be 32-bit addressable, as programming + * a 64-bit value into CR3 when running in 32-bit mode is not supported. + */ +efi_status_t efi_setup_5level_paging(void) +{ + u8 tmpl_size = (u8 *)&trampoline_ljmp_imm_offset - (u8 *)&trampoline_32bit_src; + efi_status_t status; + u8 *la57_code; + + if (!efi_is_64bit()) + return EFI_SUCCESS; + + /* check for 5 level paging support */ + if (native_cpuid_eax(0) < 7 || + !(native_cpuid_ecx(7) & (1 << (X86_FEATURE_LA57 & 31)))) + return EFI_SUCCESS; + + /* allocate some 32-bit addressable memory for code and a page table */ + status = efi_allocate_pages(2 * PAGE_SIZE, (unsigned long *)&la57_code, + U32_MAX); + if (status != EFI_SUCCESS) + return status; + + la57_toggle = memcpy(la57_code, trampoline_32bit_src, tmpl_size); + memset(la57_code + tmpl_size, 0x90, PAGE_SIZE - tmpl_size); + + /* + * To avoid the need to allocate a 32-bit addressable stack, the + * trampoline uses a LJMP instruction to switch back to long mode. + * LJMP takes an absolute destination address, which needs to be + * fixed up at runtime. + */ + *(u32 *)&la57_code[trampoline_ljmp_imm_offset] += (unsigned long)la57_code; + + efi_adjust_memory_range_protection((unsigned long)la57_toggle, PAGE_SIZE); + + return EFI_SUCCESS; +} + +void efi_5level_switch(void) +{ + bool want_la57 = IS_ENABLED(CONFIG_X86_5LEVEL) && !efi_no5lvl; + bool have_la57 = native_read_cr4() & X86_CR4_LA57; + bool need_toggle = want_la57 ^ have_la57; + u64 *pgt = (void *)la57_toggle + PAGE_SIZE; + u64 *cr3 = (u64 *)__native_read_cr3(); + u64 *new_cr3; + + if (!la57_toggle || !need_toggle) + return; + + if (!have_la57) { + /* + * 5 level paging will be enabled, so a root level page needs + * to be allocated from the 32-bit addressable physical region, + * with its first entry referring to the existing hierarchy. + */ + new_cr3 = memset(pgt, 0, PAGE_SIZE); + new_cr3[0] = (u64)cr3 | _PAGE_TABLE_NOENC; + } else { + /* take the new root table pointer from the current entry #0 */ + new_cr3 = (u64 *)(cr3[0] & PAGE_MASK); + + /* copy the new root table if it is not 32-bit addressable */ + if ((u64)new_cr3 > U32_MAX) + new_cr3 = memcpy(pgt, new_cr3, PAGE_SIZE); + } + + native_load_gdt(&(struct desc_ptr){ sizeof(gdt) - 1, (u64)gdt }); + + la57_toggle(new_cr3, want_la57); +} diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c index 517cd68ea86cb7f4..f48a2e795d885af8 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -17,6 +17,7 @@ #include #include "efistub.h" +#include "x86-stub.h" /* Maximum physical address for 64-bit kernel with 4-level paging */ #define MAXMEM_X86_64_4LEVEL (1ull << 46) @@ -223,8 +224,8 @@ static void retrieve_apple_device_properties(struct boot_params *boot_params) } } -static void -adjust_memory_range_protection(unsigned long start, unsigned long size) +void efi_adjust_memory_range_protection(unsigned long start, + unsigned long size) { efi_status_t status; efi_gcd_memory_space_desc_t desc; @@ -278,35 +279,14 @@ adjust_memory_range_protection(unsigned long start, unsigned long size) } } -/* - * Trampoline takes 2 pages and can be loaded in first megabyte of memory - * with its end placed between 128k and 640k where BIOS might start. - * (see arch/x86/boot/compressed/pgtable_64.c) - * - * We cannot find exact trampoline placement since memory map - * can be modified by UEFI, and it can alter the computed address. - */ - -#define TRAMPOLINE_PLACEMENT_BASE ((128 - 8)*1024) -#define TRAMPOLINE_PLACEMENT_SIZE (640*1024 - (128 - 8)*1024) - void startup_32(struct boot_params *boot_params); static void setup_memory_protection(unsigned long image_base, unsigned long image_size) { - /* - * Allow execution of possible trampoline used - * for switching between 4- and 5-level page tables - * and relocated kernel image. - */ - - adjust_memory_range_protection(TRAMPOLINE_PLACEMENT_BASE, - TRAMPOLINE_PLACEMENT_SIZE); - #ifdef CONFIG_64BIT if (image_base != (unsigned long)startup_32) - adjust_memory_range_protection(image_base, image_size); + efi_adjust_memory_range_protection(image_base, image_size); #else /* * Clear protection flags on a whole range of possible @@ -316,8 +296,8 @@ setup_memory_protection(unsigned long image_base, unsigned long image_size) * need to remove possible protection on relocated image * itself disregarding further relocations. */ - adjust_memory_range_protection(LOAD_PHYSICAL_ADDR, - KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR); + efi_adjust_memory_range_protection(LOAD_PHYSICAL_ADDR, + KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR); #endif } @@ -839,6 +819,12 @@ void __noreturn efi_stub_entry(efi_handle_t handle, efi_dxe_table = NULL; } + status = efi_setup_5level_paging(); + if (status != EFI_SUCCESS) { + efi_err("efi_setup_5level_paging() failed!\n"); + goto fail; + } + /* * If the kernel isn't already loaded at a suitable address, * relocate it. @@ -959,6 +945,8 @@ void __noreturn efi_stub_entry(efi_handle_t handle, goto fail; } + efi_5level_switch(); + if (IS_ENABLED(CONFIG_X86_64)) /* add offset of startup_64() */ bzimage_addr += 0x200; diff --git a/drivers/firmware/efi/libstub/x86-stub.h b/drivers/firmware/efi/libstub/x86-stub.h new file mode 100644 index 0000000000000000..37c5a36b9d8cf9b2 --- /dev/null +++ b/drivers/firmware/efi/libstub/x86-stub.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include + +extern void trampoline_32bit_src(void *, bool); +extern const u16 trampoline_ljmp_imm_offset; + +void efi_adjust_memory_range_protection(unsigned long start, + unsigned long size); + +#ifdef CONFIG_X86_64 +efi_status_t efi_setup_5level_paging(void); +void efi_5level_switch(void); +#else +static inline efi_status_t efi_setup_5level_paging(void) { return EFI_SUCCESS; } +static inline void efi_5level_switch(void) {} +#endif From patchwork Wed Jun 7 07:23:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104292 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp73512vqr; Wed, 7 Jun 2023 00:32:37 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ558xVjxxrd8F6vbrtcjD9EkXGndoxAh3HpKcQDCmQo8BAiKig6nX7GLi3uI5grfD/BeV+I X-Received: by 2002:a05:6870:8227:b0:195:f440:40fa with SMTP id n39-20020a056870822700b00195f44040famr4146823oae.41.1686123157575; Wed, 07 Jun 2023 00:32:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123157; cv=none; d=google.com; s=arc-20160816; b=GIZVFBLWSzX6UQ7eO/Vv1xx9Fo4j2FX7muzHdvKrzRvXtNDFTu4M8DO91NP49LoLyS I+WlJFcGwyfA0GS6m2YNFhi0jMVSW5Xp3b0yocIsF7VG+9NA8my8/9mkX5zUiL9FG1wK +Re/vFUfAG18M/vNLxNen8HQe+/AmBL4dQp4QJC1K67g+xFC4H+lklEGtfRiSP5jYeEh EZa7PmOF+DAkrf2sBAiZN0nd+6w6E8mIzyr32EkRs7DhTeGvFN+c6a8H2EZxn5x3boQm ew8I8bAJomTd6UTCtgd9GzUSNnTW0/ODzb2Iq3hrvvF0ySgS/sr2R3Vi2+ZyuyCdTCUz Yp3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Nq0sabK8Oe4c1g62yHC/Z0qz2ZiDvtB18DCc4/BFfhg=; b=NFWMk1tXuvJh2RvA4+mZ6KQi7lzNhvfonuW3UtYaPmo++BV/O57XEe8VylKfxpJooA oe3bbLeOMYq95eeNITC0mmZLor8fDUddR1gEqvNkSDlBKWYSOKMRAL0sTYxO9NClzica 5WiKROeXQOZnxqdJqKVbg+MnPoVDUDVh9zrHKz8PProL8nZSGJ94qJ04BVIm/iYU9h8B TtyJEN6CBCqRAn8GNFgPQVNSILNJ+pOM/o982OdyrzYu1Rteh/ohyNaGPsqly3LPuoa3 L29XW5CFQG7gOB3SMWabnBaM7KL/Dv1CXASsyt46RxjK+vKRS15MpD0h16qlStgwKA7x QPoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="s4/EYgFd"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y9-20020a17090322c900b001b231cb6f51si2652618plg.150.2023.06.07.00.32.23; Wed, 07 Jun 2023 00:32:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="s4/EYgFd"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233896AbjFGH0Z (ORCPT + 99 others); Wed, 7 Jun 2023 03:26:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239159AbjFGHZx (ORCPT ); Wed, 7 Jun 2023 03:25:53 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB2022114; Wed, 7 Jun 2023 00:24:59 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4FF1363A8C; Wed, 7 Jun 2023 07:24:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E97D9C4339C; Wed, 7 Jun 2023 07:24:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122695; bh=Pxc47KrrrsEBit8kTu0GOtEy/tpcGOklzRJpkQn39Y8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=s4/EYgFdfjtIzQr8yP0jJgCBjOtdFWT/UO3ocuWZPAWZ/D3wWDLFy9FuM/L7bt+GC wBQ/dSV7INcCqA6rmO4Q43naX5ZtpU43WGVrF5Io8ITV4hHR6+gpVvG0wIZhZdXOI1 DD86kzwbVG573B9nBbEU6rZZJ/4rOF2UdEXYyu7mdU9NvFGoxNrKidYbVaGKxz+7H8 KUFrEUNd/As06wURitXLr7SoM8T7S/otVfnj/xu/Q4jDKPfT/M4D7SkJ6zAf5AmOqZ hGmb4I5j2TK5zq4lF4aWDfYgMLKXp0pDIjmK+2ZV+8OAns4A2fW4lngT8SSJyvFs5K bO6t2fkKk1Tng== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 14/20] x86/efistub: Prefer EFI memory attributes protocol over DXE services Date: Wed, 7 Jun 2023 09:23:36 +0200 Message-Id: <20230607072342.4054036-15-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3324; i=ardb@kernel.org; h=from:subject; bh=Pxc47KrrrsEBit8kTu0GOtEy/tpcGOklzRJpkQn39Y8=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBoOz034gVyw6flmTJ+t7ddUx6iu3Xx57bZnhesgk9l nz/7AGpjlIWBjEOBlkxRRaB2X/f7Tw9UarWeZYszBxWJpAhDFycAjCRxUIM/5TfFSw73Sx2+fj/ S3b82w2OdOp7JThnMwY9qmKbX19rl83IsNdg+uF/E+9WO0/2XSrQsa8w4o3sU9733/VVjghdfu/ LwwUA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028276038552110?= X-GMAIL-MSGID: =?utf-8?q?1768028276038552110?= Currently, the EFI stub relies on DXE services in some cases to clear non-execute restrictions from page allocations that need to be executable. This is dodgy, because DXE services are not specified by UEFI but by PI, and they are not intended for consumption by OS loaders. However, no alternative existed at the time. Now, there is a new UEFI protocol that should be used instead, so if it exists, prefer it over the DXE services calls. Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/x86-stub.c | 29 ++++++++++++++------ 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c index f48a2e795d885af8..abcd5703e9f3f980 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -26,6 +26,7 @@ const efi_system_table_t *efi_system_table; const efi_dxe_services_table_t *efi_dxe_table; u32 image_offset __section(".data"); static efi_loaded_image_t *image = NULL; +static efi_memory_attribute_protocol_t *memattr; typedef union sev_memory_acceptance_protocol sev_memory_acceptance_protocol_t; union sev_memory_acceptance_protocol { @@ -233,12 +234,18 @@ void efi_adjust_memory_range_protection(unsigned long start, unsigned long rounded_start, rounded_end; unsigned long unprotect_start, unprotect_size; - if (efi_dxe_table == NULL) - return; - rounded_start = rounddown(start, EFI_PAGE_SIZE); rounded_end = roundup(start + size, EFI_PAGE_SIZE); + if (memattr != NULL) { + efi_call_proto(memattr, clear_memory_attributes, rounded_start, + rounded_end - rounded_start, EFI_MEMORY_XP); + return; + } + + if (efi_dxe_table == NULL) + return; + /* * Don't modify memory region attributes, they are * already suitable, to lower the possibility to @@ -801,6 +808,7 @@ void __noreturn efi_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg, struct boot_params *boot_params) { + efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID; unsigned long bzimage_addr = (unsigned long)startup_32; unsigned long buffer_start, buffer_end; struct setup_header *hdr = &boot_params->hdr; @@ -812,13 +820,18 @@ void __noreturn efi_stub_entry(efi_handle_t handle, if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) efi_exit(handle, EFI_INVALID_PARAMETER); - efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID); - if (efi_dxe_table && - efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) { - efi_warn("Ignoring DXE services table: invalid signature\n"); - efi_dxe_table = NULL; + if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES)) { + efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID); + if (efi_dxe_table && + efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) { + efi_warn("Ignoring DXE services table: invalid signature\n"); + efi_dxe_table = NULL; + } } + /* grab the memory attributes protocol if it exists */ + efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr); + status = efi_setup_5level_paging(); if (status != EFI_SUCCESS) { efi_err("efi_setup_5level_paging() failed!\n"); From patchwork Wed Jun 7 07:23:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104288 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp71563vqr; Wed, 7 Jun 2023 00:28:09 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6b36FOSwc1ZqIh24cjSECznuCCSlyl+4w/S4Z1Bz8sm9UShmPvV7WKMROx7QAbXVXYsLJF X-Received: by 2002:a05:6e02:151:b0:33a:63b9:db83 with SMTP id j17-20020a056e02015100b0033a63b9db83mr3765435ilr.19.1686122888723; Wed, 07 Jun 2023 00:28:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686122888; cv=none; d=google.com; s=arc-20160816; b=OZMq9HXbPQcw/uYhL3prjihf0mrjEqOkJk3UcDt1FWkPAOfiyl7Hx7niSCFcdzJPXS WgvCeMVSa+PUwd2aH0FY7L2DZWoG5F9bFWCUsIJf+wjwrOAWTCI1e6nqnr0paNypVw0v tmTrS4X7KwSAEkzn8dO/NGFC+ohUB/KvATquL1AOP9nnQ0JUbC9sSdQ2VbMM3nMFZ+/4 md6NAS4dC2nE94fHF+nacb52OeRLdvChJsZtQ8nhsnF8JyPxOzKeOBSVRvUPBE5Sgru+ 3ZzF0sZzmBHm5jdJQCCsKT4TT6aqnTpwAToBGeqP+ayCIavn0tDIgD1kBaa0RBI09wOX vbjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=DB8oL3jXiN9e2dZTst6BBU9MW8a+CwDXQ55OFaZVfRU=; b=Q4gdf4iafCuDZjRhDtBjYiczjyQschc2IOXhgi6W6+5r1R4ryfGnKYroibFUfscwoM USmxBMA/xzOjC0IcfKYXXEx6m650PbV8bqDjECmOLp56aRNEMV5Sg/bxDcOpQFXW624O v+UcmLLL3ZyG2oVdsG2wNNKVxuPc8Gx157VcT2KEVkyXHx0eJAnXa/EJGhCtX39yqLPf gMFEFF50OGZaMVLtkhCDLRM5A15j1Ihrm/8jmKvjbY+56bn5hINARO92B2LOxbfJW4tc 02jdT+CqT7DVWhAIvQPcWWDcJndHWrHHTl025PzRPHHCBd8qdIONteUX73CW8IQsnskr WMZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lI7VBfh3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u10-20020a17090341ca00b001ac62935789si8602378ple.468.2023.06.07.00.27.52; Wed, 07 Jun 2023 00:28:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lI7VBfh3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239214AbjFGH0p (ORCPT + 99 others); Wed, 7 Jun 2023 03:26:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238923AbjFGH0H (ORCPT ); Wed, 7 Jun 2023 03:26:07 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C9482712; Wed, 7 Jun 2023 00:25:01 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 8FE3A63B5E; Wed, 7 Jun 2023 07:25:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2F7C3C433EF; Wed, 7 Jun 2023 07:24:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122700; bh=TDhagb7/1BjVNI9fsBOdvguiTH1+pJKnlE7Km5AKGt0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lI7VBfh3FTa+/2DGIQjc1gr3ILzC2t6Ms0aGwGTwrUtF4VPlbF1RALiGUY0dH0QRD A3ggROvDAfMP5yaR3Cf5kue7TsSvwVOR//bIpbHe4ZzAPU2gB3rt6RnuFeQAyzA5st O9PN3yhOwKJVjdCNZKObitd6C5vcBlDidZVw4xrll42+QvQHFHsNlANBGeQa4XFLDC GCz95IjoX0p2HVE/XT19Lt3pYrTsAgEbXVg+au5gnMLwe3VM4lPvqgNRRZNoULc20H xeGe82bRE7yfX9H/j9PMHg6L4Xjf06k8iPP1zi7gU7+cD/qn8ILEGjMTYQ8ru27Euv oeQ8cSu29UZOQ== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 15/20] decompress: Use 8 byte alignment Date: Wed, 7 Jun 2023 09:23:37 +0200 Message-Id: <20230607072342.4054036-16-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=737; i=ardb@kernel.org; h=from:subject; bh=TDhagb7/1BjVNI9fsBOdvguiTH1+pJKnlE7Km5AKGt0=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBoNwx+Byz1Ywd4vxs4ZLqct3rK24U3ZBefYk5ou4gx 1T+gDsdpSwMYhwMsmKKLAKz/77beXqiVK3zLFmYOaxMIEMYuDgFYCICJxj+CpbPu3D/CcsitZol Eht9De+3R/xcu7zg3NfQn78EuLIeHWP4p/9kduedmXcF1pc/XPt+lnXrSbsHZ6b/7W+++l7v19z Lp7gA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768027993796264573?= X-GMAIL-MSGID: =?utf-8?q?1768027993796264573?= The ZSTD decompressor requires malloc() allocations to be 8 byte aligned, so ensure that this the case. Signed-off-by: Ard Biesheuvel --- include/linux/decompress/mm.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/linux/decompress/mm.h b/include/linux/decompress/mm.h index 9192986b1a731323..ac862422df158bef 100644 --- a/include/linux/decompress/mm.h +++ b/include/linux/decompress/mm.h @@ -48,7 +48,7 @@ MALLOC_VISIBLE void *malloc(int size) if (!malloc_ptr) malloc_ptr = free_mem_ptr; - malloc_ptr = (malloc_ptr + 3) & ~3; /* Align */ + malloc_ptr = (malloc_ptr + 7) & ~7; /* Align */ p = (void *)malloc_ptr; malloc_ptr += size; From patchwork Wed Jun 7 07:23:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104298 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp75127vqr; Wed, 7 Jun 2023 00:36:35 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6c9ba6kNXG8/HlKLsIOoXApmG5vTBMeOb2g586QCS6Tg4TcjWjfkC6tx5LTLU87/r+8Ozq X-Received: by 2002:aca:1204:0:b0:397:fbe7:a0fb with SMTP id 4-20020aca1204000000b00397fbe7a0fbmr5097206ois.18.1686123395173; Wed, 07 Jun 2023 00:36:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123395; cv=none; d=google.com; s=arc-20160816; b=IRATdLpkdGlqBNO6lxpTNnrB+RVFvZ50yw+ErIobrKNCAGDHkBhytywgDOPk0HWybC 1JSzvXIERpbmUvFGnfBYd9jC7nu3NqDWBzoD5fihGulVXQxAxDY39LSv4ksHyD5/1p4l igOMhOl/GN5/W2i9P5aIiQHiIytpwaT3QRd3DnxH3ZGk2ZmipYfECtzB0YJUWVyXuT5V ISB73TB4QxrOOoK5/qmldaIKERk7uUc42GLWRL+PVMydNPGL+uP+HN2LQqGC0o7JZWT4 6yV5iYWZi/z8lZ9TlcjBDrxH2OWuz/kcgm2AgzNbrlUaVTWnr2NG3YIKGED4wlwFeWAx 93wg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=u2/SCo5m3z1e4xTg6tfOnhUyURbP1ZtoeaFw+Zn+qTw=; b=QbioA+7mjfupYyLBK083DVuSjT/4P/u8KrQxetKWF5RoNnP/syty7T5jlWgn/vC5WK LGUoD0QX25ao+1NIfVd7BdHZ8VuI0d901se/8HXW6VQ0kE7Zsxl8m1UEi+hm5578OgxR hucZU6Y7PjcohKOssUmfT0Z4OHTNZ5qagyTW/gOHeUYDRoxnmHQTyrElB+M1NAeAhD2V ajWTtKrI9E8KPGkpknVKGWlqixKIWa/XcDdL28ILF2ltQw2/IZ3LA9+3oOMvDuyOw5Xs bh/+0z6buZ+mVzyMhjkeZIzBmrCO/MeE03V08GWlTwoZh7nk/rlEqZ1sGU4CCSdrb9mF kQoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HbIRsV7a; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n127-20020a632785000000b005287f5fbf5esi8641065pgn.254.2023.06.07.00.36.20; Wed, 07 Jun 2023 00:36:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HbIRsV7a; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239048AbjFGH20 (ORCPT + 99 others); Wed, 7 Jun 2023 03:28:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239033AbjFGH0r (ORCPT ); Wed, 7 Jun 2023 03:26:47 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED5092D72; Wed, 7 Jun 2023 00:25:11 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C68F761158; Wed, 7 Jun 2023 07:25:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6A596C4339E; Wed, 7 Jun 2023 07:25:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122704; bh=DgIwHiqbmr593p9YlPsAqYmPhsSZnjp5/M5rhaPu7Lk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HbIRsV7ajtzb9V60YJCrMao2S2XndXaJgossj0OsUpBPBNvJoErUVKwGh20aRLgYc Su8hyyK+zKzuO+OemgRTc2naA64H/zDUxk/UoPeXOS/4Ncki/4x7AYPY16K0SWmkP9 muku/oWOA066kBNACTi3lTiw6DP7j4mI4SigYu2NzLUbYxm7jzH1uW/j71dygNxDCw vsIZ+M4gFxjI1Uj/AaPDBwpuHbc6V4bzUZgRsiC/JyuwzlQmz8T/T3hc69HkCCngW5 cFO5wR+TEmFDIjq090eHJxgL1jwpNPFSziEztnlRmXa/ySFU57nMlfwV0JFZp4qQtJ YoH+hpZ/cD+lw== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 16/20] x86/decompressor: Move global symbol references to C code Date: Wed, 7 Jun 2023 09:23:38 +0200 Message-Id: <20230607072342.4054036-17-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5078; i=ardb@kernel.org; h=from:subject; bh=DgIwHiqbmr593p9YlPsAqYmPhsSZnjp5/M5rhaPu7Lk=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBoCI4JFJK5cSdbQzaEa9WvHAvalOcb2s9b8Vmhcc8r BqL4po7SlkYxDgYZMUUWQRm/3238/REqVrnWbIwc1iZQIYwcHEKwEQsMhn+qZo4R87bcM/hT/HF njsHDvvb/V52aUm4lUq08LLKgyUfNBj+5xkZSV9r7TTybZKvlmIVn6zpEHF1vnTv7LSttU0ai2U 4AQ== X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028525619547989?= X-GMAIL-MSGID: =?utf-8?q?1768028525619547989?= It is no longer necessary to be cautious when referring to global variables in the position independent decompressor code, now that it is built using PIE codegen and makes an assertion in the linker script that no GOT entries exist (which would require adjustment for the actual runtime load address of the decompressor binary). This means global variables can be referenced directly from C code, instead of having to pass their runtime addresses into C routines from asm code, which needs to happen at each call site. Do so for the code that will be called directly from the EFI stub after a subsequent patch, and avoid the need to duplicate this logic a third time. Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/head_32.S | 8 -------- arch/x86/boot/compressed/head_64.S | 8 +------- arch/x86/boot/compressed/misc.c | 16 +++++++++------- 3 files changed, 10 insertions(+), 22 deletions(-) diff --git a/arch/x86/boot/compressed/head_32.S b/arch/x86/boot/compressed/head_32.S index 3530465b5b85ccf3..beee858058df4403 100644 --- a/arch/x86/boot/compressed/head_32.S +++ b/arch/x86/boot/compressed/head_32.S @@ -168,13 +168,7 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) */ /* push arguments for extract_kernel: */ - pushl output_len@GOTOFF(%ebx) /* decompressed length, end of relocs */ pushl %ebp /* output address */ - pushl input_len@GOTOFF(%ebx) /* input_len */ - leal input_data@GOTOFF(%ebx), %eax - pushl %eax /* input_data */ - leal boot_heap@GOTOFF(%ebx), %eax - pushl %eax /* heap area */ pushl %esi /* real mode pointer */ call extract_kernel /* returns kernel entry point in %eax */ addl $24, %esp @@ -202,8 +196,6 @@ SYM_DATA_END_LABEL(gdt, SYM_L_LOCAL, gdt_end) */ .bss .balign 4 -boot_heap: - .fill BOOT_HEAP_SIZE, 1, 0 boot_stack: .fill BOOT_STACK_SIZE, 1, 0 boot_stack_end: diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index 408c7824b647ff51..556ee504325ae50e 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -493,11 +493,7 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) * Do the extraction, and jump to the new kernel.. */ movq %r15, %rdi /* pass struct boot_params pointer */ - leaq boot_heap(%rip), %rsi /* malloc area for uncompression */ - leaq input_data(%rip), %rdx /* input_data */ - movl input_len(%rip), %ecx /* input_len */ - movq %rbp, %r8 /* output target address */ - movl output_len(%rip), %r9d /* decompressed length, end of relocs */ + movq %rbp, %rsi /* output target address */ call extract_kernel /* returns kernel entry point in %rax */ /* @@ -639,8 +635,6 @@ SYM_DATA_END_LABEL(boot_idt, SYM_L_GLOBAL, boot_idt_end) */ .bss .balign 4 -SYM_DATA_LOCAL(boot_heap, .fill BOOT_HEAP_SIZE, 1, 0) - SYM_DATA_START_LOCAL(boot_stack) .fill BOOT_STACK_SIZE, 1, 0 .balign 16 diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/misc.c index 94b7abcf624b3b55..0b29a3183df42246 100644 --- a/arch/x86/boot/compressed/misc.c +++ b/arch/x86/boot/compressed/misc.c @@ -330,6 +330,11 @@ static size_t parse_elf(void *output) return ehdr.e_entry - LOAD_PHYSICAL_ADDR; } +static u8 boot_heap[BOOT_HEAP_SIZE] __aligned(4); + +extern unsigned char input_data[]; +extern unsigned int input_len, output_len; + /* * The compressed kernel image (ZO), has been moved so that its position * is against the end of the buffer used to hold the uncompressed kernel @@ -347,14 +352,11 @@ static size_t parse_elf(void *output) * |-------uncompressed kernel image---------| * */ -asmlinkage __visible void *extract_kernel(void *rmode, memptr heap, - unsigned char *input_data, - unsigned long input_len, - unsigned char *output, - unsigned long output_len) +asmlinkage __visible void *extract_kernel(void *rmode, unsigned char *output) { const unsigned long kernel_total_size = VO__end - VO__text; unsigned long virt_addr = LOAD_PHYSICAL_ADDR; + memptr heap = (memptr)boot_heap; unsigned long needed_size; size_t entry_offset; @@ -412,7 +414,7 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap, * entries. This ensures the full mapped area is usable RAM * and doesn't include any reserved areas. */ - needed_size = max(output_len, kernel_total_size); + needed_size = max((unsigned long)output_len, kernel_total_size); #ifdef CONFIG_X86_64 needed_size = ALIGN(needed_size, MIN_KERNEL_ALIGN); #endif @@ -443,7 +445,7 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap, #ifdef CONFIG_X86_64 if (heap > 0x3fffffffffffUL) error("Destination address too large"); - if (virt_addr + max(output_len, kernel_total_size) > KERNEL_IMAGE_SIZE) + if (virt_addr + needed_size > KERNEL_IMAGE_SIZE) error("Destination virtual address is beyond the kernel mapping area"); #else if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff)) From patchwork Wed Jun 7 07:23:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104295 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp75037vqr; Wed, 7 Jun 2023 00:36:21 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6+gUOgsrkKv3/fg1CnOAEXRMFtEOKDFj1WHO38KGS0MpRYREcDAws4tR+mRzAFomfq6mPC X-Received: by 2002:a05:6a21:3714:b0:112:6217:7a62 with SMTP id yl20-20020a056a21371400b0011262177a62mr2023538pzb.45.1686123381550; Wed, 07 Jun 2023 00:36:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123381; cv=none; d=google.com; s=arc-20160816; b=HsGwEXt4v7DgzaqebSFUWzL6yd1CsHvWkSqdJ66PmrXRXX7siKanskaX+1OUaGwX5t sLMDTDsfJGgfhyyJgDJ7kFVBy6shQWYuWyAvlDVPqIou/zp4qalWsUdXff3r+KNzD/C1 8QKFZfDBboZEy03y/FFeilFbU+ynbspnFYEIFlXXv5FC0CR4tNQbZsPNvHNAex7XabLB 278G7IkOYBaXxXIm9MYpLEbseFLXFoFRD+UT/E0bhaUABBKCXAbZTNM1oQLVgnS5r1oD 368T4RyprFK0/Sb21I8TGwAObj5mLo2FDb+xZHZ4JelZ8zWNEuXzFGr6thN/7PkpUd0p 6HSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0EChhZLGsOkUurjosqlVA0InwtYI2wSmS1z5ylf1X6U=; b=ZJQJ7x+je64Znoc2tgCjZlazRVqQjvAnqC9zRLjuOnEERp0E/luPOf0sTd1e1QvbwP ehDlrMjowdEzcLPJsxThc1nC8BbBx+kqeMmzgpko3b5wHlBJNzLlZpYLcG3yNAX2BCJu t/nV6ZTzbaRijpL3uNr1B7Ln8Rn7zjCaHBJb7zGcu1lJp4sQAg+d7HyuRFmZwWl6wjH5 7OAXT0M7J5iD0Lfufi3wHSnH+l84JhMj/B8tF+GrEeN7Pl6WQcBm4slXjrU6cTSmgbVV ycJnPRlVmEL39TzkUzuEw/qkKNxR/go/CYVuFkBP6VS/cgBtMjNWxH46YGkCAUZcUcVa ClXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Eu7a3ltL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a187-20020a6390c4000000b00543eddc6206si2655194pge.431.2023.06.07.00.36.08; Wed, 07 Jun 2023 00:36:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Eu7a3ltL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239139AbjFGH2F (ORCPT + 99 others); Wed, 7 Jun 2023 03:28:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238984AbjFGH0p (ORCPT ); Wed, 7 Jun 2023 03:26:45 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CB8C2D6A; Wed, 7 Jun 2023 00:25:09 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 135FB634D4; Wed, 7 Jun 2023 07:25:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A5209C4339C; Wed, 7 Jun 2023 07:25:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122708; bh=OPBcikll8Xc3e/JAsC+t93XsxVWQdgLYFLK1YAtUnZI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Eu7a3ltLtybCWEGlYROAtRjRMrWDla6YAZMxImo/tLUJJn4ftORrrYTYEAlD+8Vd6 hrbPlcVXaAsR8ssRvUaL4DHuB9oY8L5vVHlMFzY6PK7gaS2mRwX4MTN/CMdhvR1z9F RZ7KsZenDwvfYUZYdZ3jXiY13+RRzG1B1uvkWjOMFBC/Xp41VDsWIBVnDn/E2jYsub NV4BAaJmRRnOWxGqouxCIIG8yA/QfmJ99F2LVvJopEwPXHa9+BNDYqy4wUqg+8qfCU OgoCo/+IIc6sjz2MkORNqanqSGsMF98fXzuEyq6YOcVEA0KpC+A/KSHQyOMndMOxDk wWBFe5vuw8NJA== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 17/20] x86/decompressor: Factor out kernel decompression and relocation Date: Wed, 7 Jun 2023 09:23:39 +0200 Message-Id: <20230607072342.4054036-18-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2968; i=ardb@kernel.org; h=from:subject; bh=OPBcikll8Xc3e/JAsC+t93XsxVWQdgLYFLK1YAtUnZI=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBoMom6LTuxX+/zu5fcIVV+winTcebO1dKNxlpyVys5 lxfXnOwo5SFQYyDQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAEwkRoOR4WBW/p3cR58f79sg dbFn0pxJ+b/2vP9XJa8cy6AQc2DpCROG/xEPPLZ82Z1vcTBsodetWQ9M7meXsE74t1p8H+slnev vmFkB X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028510820818948?= X-GMAIL-MSGID: =?utf-8?q?1768028510820818948?= Factor out the decompressor sequence that invokes the decompressor, parses the ELF and applies the relocations so that it can be called directly from the EFI stub. Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/misc.c | 28 ++++++++++++++++---- arch/x86/include/asm/boot.h | 8 ++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/misc.c index 0b29a3183df42246..93220a2a276b0ffb 100644 --- a/arch/x86/boot/compressed/misc.c +++ b/arch/x86/boot/compressed/misc.c @@ -330,11 +330,33 @@ static size_t parse_elf(void *output) return ehdr.e_entry - LOAD_PHYSICAL_ADDR; } +const unsigned long kernel_total_size = VO__end - VO__text; + static u8 boot_heap[BOOT_HEAP_SIZE] __aligned(4); extern unsigned char input_data[]; extern unsigned int input_len, output_len; +unsigned long decompress_kernel(unsigned char *outbuf, unsigned long virt_addr, + void (*error)(char *x)) +{ + unsigned long entry; + + if (!free_mem_ptr) { + free_mem_ptr = (unsigned long)boot_heap; + free_mem_end_ptr = (unsigned long)boot_heap + sizeof(boot_heap); + } + + if (__decompress(input_data, input_len, NULL, NULL, outbuf, output_len, + NULL, error) < 0) + return ULONG_MAX; + + entry = parse_elf(outbuf); + handle_relocations(outbuf, output_len, virt_addr); + + return entry; +} + /* * The compressed kernel image (ZO), has been moved so that its position * is against the end of the buffer used to hold the uncompressed kernel @@ -354,7 +376,6 @@ extern unsigned int input_len, output_len; */ asmlinkage __visible void *extract_kernel(void *rmode, unsigned char *output) { - const unsigned long kernel_total_size = VO__end - VO__text; unsigned long virt_addr = LOAD_PHYSICAL_ADDR; memptr heap = (memptr)boot_heap; unsigned long needed_size; @@ -463,10 +484,7 @@ asmlinkage __visible void *extract_kernel(void *rmode, unsigned char *output) accept_memory(__pa(output), __pa(output) + needed_size); } - __decompress(input_data, input_len, NULL, NULL, output, output_len, - NULL, error); - entry_offset = parse_elf(output); - handle_relocations(output, output_len, virt_addr); + entry_offset = decompress_kernel(output, virt_addr, error); debug_putstr("done.\nBooting the kernel (entry_offset: 0x"); debug_puthex(entry_offset); diff --git a/arch/x86/include/asm/boot.h b/arch/x86/include/asm/boot.h index 9191280d9ea3160d..4ae14339cb8cc72d 100644 --- a/arch/x86/include/asm/boot.h +++ b/arch/x86/include/asm/boot.h @@ -62,4 +62,12 @@ # define BOOT_STACK_SIZE 0x1000 #endif +#ifndef __ASSEMBLY__ +extern unsigned int output_len; +extern const unsigned long kernel_total_size; + +unsigned long decompress_kernel(unsigned char *outbuf, unsigned long virt_addr, + void (*error)(char *x)); +#endif + #endif /* _ASM_X86_BOOT_H */ From patchwork Wed Jun 7 07:23:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104304 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp75477vqr; Wed, 7 Jun 2023 00:37:26 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5yWCMJlt2mgK9qbehZjG+q8LStPD+kJ0yHFmkVsmpxi+8+0c5UAhYztn9zOcKjTNBXRqm8 X-Received: by 2002:a81:4e44:0:b0:561:d6dd:bc84 with SMTP id c65-20020a814e44000000b00561d6ddbc84mr5436881ywb.48.1686123446153; Wed, 07 Jun 2023 00:37:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123446; cv=none; d=google.com; s=arc-20160816; b=VLgZEaHQXyJFN3N3VydUAMWW+NAAaUWRJPomsB8bh9l/m/WAXNn8ShaeYNFyzQYfhi KRx+fFmuUMd3xRvxkXUwAVIBElPAIwbbR/I8HpRb2EirPQdVo1UIDi2SvTGk5I9ujE0k lLKuAid61Y8hmHQAXWa2g1PO0I0GdEh5d+6v6YRVwSiJhvREhZk5sam5LDuLHrHaDBGa hAz3XM4OMPnliaX7FzXFtnR8Ln97IiC4Xgf1FSYU6fwCQvOhYdGVZYsvs8OBwDcVBmKg r9KtJCRJh5Iq94+P6bWoloMf6oQMgPeLgk97SutE1ULT96t5XVr9Go6SBd+k53whEL+c z3JA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=IQL49+wkBQCrsk56hKhP7yDvFPKBFHJ/K6FeWsnjjXc=; b=TrNckW3E7RvLIgTlU0wQ2/iNWn4tHFnFBNhKgpwfNqEM2T+CDgGlaq3mzR2DmRgMIs p+5qtC9S8KEXkEDqtR7639Ygkg+vihe0IlIFJelPHZgG5/UxUNvygwiX+vruD5kVIVsT DDQcZAg0qKBspP0Do5DbaYwVO5afOone+fKkJkP2NiYCcyatxeqzYBlOilYi5N7x2YTf K5ZYbvKgPZYiOGNAkBePthHWBAD5E2fd4uHBwANNxvcQLMavMrJh2tiel77+Q/YEQ1VW s0eGM3tG0vXfL21QrC3sFPgEZG6RjMGlGigbY8kyrrTrNBTJruQHmsizGuFrMqNOGEMx p9DA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Wm1NqZL1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g19-20020a633753000000b00543a6b76430si5055050pgn.481.2023.06.07.00.37.13; Wed, 07 Jun 2023 00:37:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Wm1NqZL1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239218AbjFGH2l (ORCPT + 99 others); Wed, 7 Jun 2023 03:28:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239121AbjFGH05 (ORCPT ); Wed, 7 Jun 2023 03:26:57 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F74D271C; Wed, 7 Jun 2023 00:25:13 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4C771633AD; Wed, 7 Jun 2023 07:25:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E3386C4339E; Wed, 7 Jun 2023 07:25:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122712; bh=Os+OvcWXVTEgn4GvlOd4rs76sb2kuX0zlm0OhlZtKNw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Wm1NqZL1ZMBk78SzRLrKdyPbegO9VprXxcWayVhd2l9t+3p/xcHmYK6xy2phDWJxd M6yue+9OZnQgHFuPgHzHqkFD6/E7uhPuNLgRpj2iQV7vDpO4qyaAZJMRW9k2xpqBd4 boLjV0DQZ+L9P18rLqiVzelLs9a+tm+es3TcFatlp0KaEu6jQ42guZrLLP3rve7hK8 SWo/0ojIZYqbdE4jWuht5Kw7kUcRHIP0w6MrlfZNIPtDWeAOhy8ysTMATncYyr77zw 73S7xdY41F7Cs+Atw13l3CUnBgewgGFsKwKSwYKQS11KIalRqJ5+4M4hmv/lDGcHXP piM3bVFz/uZ9g== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 18/20] efi/libstub: Add limit argument to efi_random_alloc() Date: Wed, 7 Jun 2023 09:23:40 +0200 Message-Id: <20230607072342.4054036-19-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3943; i=ardb@kernel.org; h=from:subject; bh=Os+OvcWXVTEgn4GvlOd4rs76sb2kuX0zlm0OhlZtKNw=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBoHqJcu2dUB2rs1yvj98/8c2t+udkxtvMqoJCfGLbi nedlzbqKGVhEONgkBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABPJmcXI8NrO/v22yzKzdlke /pZ0/6vbrgkpM+9PWx94fWKdbX5jRhnD/0rtHOnbUw7b8Rz7/E7g7V/OWsZQwYXN/HOnfO6YfCt AjQEA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028578339599725?= X-GMAIL-MSGID: =?utf-8?q?1768028578339599725?= x86 will need to limit the kernel memory allocation to the lowest 512 MiB of memory, to match the behavior of the existing bare metal KASLR physical randomization logic. So in preparation for that, add a limit parameter to efi_random_alloc() and wire it up. Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/arm64-stub.c | 2 +- drivers/firmware/efi/libstub/efistub.h | 2 +- drivers/firmware/efi/libstub/randomalloc.c | 10 ++++++---- drivers/firmware/efi/libstub/zboot.c | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/drivers/firmware/efi/libstub/arm64-stub.c b/drivers/firmware/efi/libstub/arm64-stub.c index 770b8ecb73984c61..8c40fc89f5f99209 100644 --- a/drivers/firmware/efi/libstub/arm64-stub.c +++ b/drivers/firmware/efi/libstub/arm64-stub.c @@ -106,7 +106,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, */ status = efi_random_alloc(*reserve_size, min_kimg_align, reserve_addr, phys_seed, - EFI_LOADER_CODE); + EFI_LOADER_CODE, EFI_ALLOC_LIMIT); if (status != EFI_SUCCESS) efi_warn("efi_random_alloc() failed: 0x%lx\n", status); } else { diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h index 06b7abc92ced9e18..9823f6fb3e01f718 100644 --- a/drivers/firmware/efi/libstub/efistub.h +++ b/drivers/firmware/efi/libstub/efistub.h @@ -956,7 +956,7 @@ efi_status_t efi_get_random_bytes(unsigned long size, u8 *out); efi_status_t efi_random_alloc(unsigned long size, unsigned long align, unsigned long *addr, unsigned long random_seed, - int memory_type); + int memory_type, unsigned long alloc_limit); efi_status_t efi_random_get_seed(void); diff --git a/drivers/firmware/efi/libstub/randomalloc.c b/drivers/firmware/efi/libstub/randomalloc.c index 32c7a54923b4c127..674a064b8f7adc68 100644 --- a/drivers/firmware/efi/libstub/randomalloc.c +++ b/drivers/firmware/efi/libstub/randomalloc.c @@ -16,7 +16,8 @@ */ static unsigned long get_entry_num_slots(efi_memory_desc_t *md, unsigned long size, - unsigned long align_shift) + unsigned long align_shift, + u64 alloc_limit) { unsigned long align = 1UL << align_shift; u64 first_slot, last_slot, region_end; @@ -29,7 +30,7 @@ static unsigned long get_entry_num_slots(efi_memory_desc_t *md, return 0; region_end = min(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - 1, - (u64)EFI_ALLOC_LIMIT); + alloc_limit); if (region_end < size) return 0; @@ -54,7 +55,8 @@ efi_status_t efi_random_alloc(unsigned long size, unsigned long align, unsigned long *addr, unsigned long random_seed, - int memory_type) + int memory_type, + unsigned long alloc_limit) { unsigned long total_slots = 0, target_slot; unsigned long total_mirrored_slots = 0; @@ -76,7 +78,7 @@ efi_status_t efi_random_alloc(unsigned long size, efi_memory_desc_t *md = (void *)map->map + map_offset; unsigned long slots; - slots = get_entry_num_slots(md, size, ilog2(align)); + slots = get_entry_num_slots(md, size, ilog2(align), alloc_limit); MD_NUM_SLOTS(md) = slots; total_slots += slots; if (md->attribute & EFI_MEMORY_MORE_RELIABLE) diff --git a/drivers/firmware/efi/libstub/zboot.c b/drivers/firmware/efi/libstub/zboot.c index e5d7fa1f1d8fd160..bdb17eac0cb401be 100644 --- a/drivers/firmware/efi/libstub/zboot.c +++ b/drivers/firmware/efi/libstub/zboot.c @@ -119,7 +119,7 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab) } status = efi_random_alloc(alloc_size, min_kimg_align, &image_base, - seed, EFI_LOADER_CODE); + seed, EFI_LOADER_CODE, EFI_ALLOC_LIMIT); if (status != EFI_SUCCESS) { efi_err("Failed to allocate memory\n"); goto free_cmdline; From patchwork Wed Jun 7 07:23:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104301 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp75386vqr; Wed, 7 Jun 2023 00:37:13 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4S2Nhwj+03KRZT/y09i9rvahgPDw6hjwxhLgvBIRHGI2yT6hSHYBu3nzJqbhvZJuXRYQxW X-Received: by 2002:a54:4487:0:b0:398:26f0:67a9 with SMTP id v7-20020a544487000000b0039826f067a9mr4468207oiv.55.1686123433371; Wed, 07 Jun 2023 00:37:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123433; cv=none; d=google.com; s=arc-20160816; b=FFmSInptQpNm1vmcnElzciGjriNFhaJf8be6UhiA64IJKJunyPZmOr+ewTH9sgWFjX ZoFiGP2Oe3UOhcxvvY3pGa//Yg+7skG46RSaG2l17p7EEpZIMNEGc3Y143j1uRlnW/KT zJ+Zj2PQs0+nSCXHGXiFD7hewhUd76jHA/0cOTAkA6F30kHDQksJdIlGRlpNp/FOSZsL bIzqJUFEbHj4M/lFfp0DQ3PzgFyDtIQOE+M6hX6zceuZQ/1J5CxAvNFOrm/SN3L+prPC p6UOuplGCnD6/KW4jbgjS2DpUZj/ZeMz3fOeTolhA3ntDDbAvKiahFj6MGd52MnmhiQY TvYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=uAEifwGQDWIJsSJHjEVhUxiJRfkgjNiu12WoJbu55AI=; b=dm5BhoesOec9W4LhTyFfBFsnl7xF72b1eT3tOxlbv6H8lBRz2HoB0hQ9uIl5eQ8YYZ T8gVCV1PP2Owmsv61BIGJUtTuLk8u5ia6H22HOWrYTBWq/t7JIgRkpTAkKKVURaAxP+0 kGhhm3h9Ul2vu6M8SKAcPATghEyoZFuri8gmD91+yGujVp5XvI+PwxOOwTsuJX7tUd/L 1fsymPo4gQAWhB3ux0Ax56wCVe7z/Ol9gvy8LhdRy8Ywod0PUoAH5HOw0r/IpSZIT31A aWeAY8BE6ujtDwuyByPCyX1x3WPLjTQA/lGuHsmS7QW6Stf3wxTStQqqhmJxo54n/FDV 5zHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HLobFQI3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x6-20020a170902a38600b001ab20d029e0si8178745pla.582.2023.06.07.00.37.00; Wed, 07 Jun 2023 00:37:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HLobFQI3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238938AbjFGH3F (ORCPT + 99 others); Wed, 7 Jun 2023 03:29:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239273AbjFGH1I (ORCPT ); Wed, 7 Jun 2023 03:27:08 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B925D2D79; Wed, 7 Jun 2023 00:25:17 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 86BC6633D6; Wed, 7 Jun 2023 07:25:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2BBD6C433D2; Wed, 7 Jun 2023 07:25:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122717; bh=FifYFQbUgsc3xafluaKb/KA+ANrpnvv0InNGmp59Xxo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HLobFQI3nq61lNAKvAy0REEflUflfwMGrkYDtNrNYQR2B3eGyqA9Ieaxh95UNDqpE uFedpF9qe2WaEr+c3U9zmy8N6JI3IkQWa7IjP1xLF+/tZtDK5+isuFGO6FO1+FX6Uo WnxtZybrW6vIFDvmFHY3qH1rlav3huINWl+ndrCQFDJ4amPviqu0OnxIxZFzllilS6 9FjDm2R6lK3KpTWumt1jqP98UfNgKsvlzElLn/sOX2kUu7crVJAIS54vrsF2onqa+u DdDFP3+/Fv2JngSKHdNvY24HuW1eoDJ3ptYGOwsAFnrPvGUG5q+2EwoWJiL0cKhWRq y2XZiClpN6wWA== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 19/20] x86/efistub: Perform SNP feature test while running in the firmware Date: Wed, 7 Jun 2023 09:23:41 +0200 Message-Id: <20230607072342.4054036-20-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=6538; i=ardb@kernel.org; h=from:subject; bh=FifYFQbUgsc3xafluaKb/KA+ANrpnvv0InNGmp59Xxo=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBoOYn5601kzxYG7UMTh7aM2n1ZmUxsVVueuHZ/1Ofc 3Idu5PeUcrCIMbBICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACbysYvhD5fxtouPBN57/ps0 c5+VQuzRmWoTthY6+BqVuHzvPbPohBUjw7UXuhZLynKXmhnMkDl9vpg1Woo/f6HW8jLJoP7yAy2 fOAE= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028564897781255?= X-GMAIL-MSGID: =?utf-8?q?1768028564897781255?= Before refactoring the EFI stub boot flow to avoid the legacy bare metal decompressor, duplicate the SNP feature check in the EFI stub before handing over to the kernel proper. The SNP feature check can be performed while running under the EFI boot services, which means we can fail gracefully and return an error to the bootloader if the loaded kernel does not implement support for all the features that the hypervisor enabled. Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/sev.c | 71 +++++++++++--------- arch/x86/include/asm/sev.h | 4 ++ drivers/firmware/efi/libstub/x86-stub.c | 17 +++++ 3 files changed, 62 insertions(+), 30 deletions(-) diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index 09dc8c187b3cc752..9593bc80c9c6b89d 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -367,6 +367,11 @@ static void enforce_vmpl0(void) */ #define SNP_FEATURES_PRESENT (0) +u64 snp_get_unsupported_features(u64 status) +{ + return status & SNP_FEATURES_IMPL_REQ & ~SNP_FEATURES_PRESENT; +} + void snp_check_features(void) { u64 unsupported; @@ -380,7 +385,7 @@ void snp_check_features(void) * EXIT_INFO_2 of the GHCB protocol so that those features can be reported * as part of the guest boot failure. */ - unsupported = sev_status & SNP_FEATURES_IMPL_REQ & ~SNP_FEATURES_PRESENT; + unsupported = snp_get_unsupported_features(sev_status); if (unsupported) { if (ghcb_version < 2 || (!boot_ghcb && !early_setup_ghcb())) sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); @@ -390,10 +395,42 @@ void snp_check_features(void) } } -void sev_enable(struct boot_params *bp) +u64 sev_get_status(void) { unsigned int eax, ebx, ecx, edx; struct msr m; + + /* Check for the SME/SEV support leaf */ + eax = 0x80000000; + ecx = 0; + native_cpuid(&eax, &ebx, &ecx, &edx); + if (eax < 0x8000001f) + return 0; + + /* + * Check for the SME/SEV feature: + * CPUID Fn8000_001F[EAX] + * - Bit 0 - Secure Memory Encryption support + * - Bit 1 - Secure Encrypted Virtualization support + * CPUID Fn8000_001F[EBX] + * - Bits 5:0 - Pagetable bit position used to indicate encryption + */ + eax = 0x8000001f; + ecx = 0; + native_cpuid(&eax, &ebx, &ecx, &edx); + /* Check whether SEV is supported */ + if (!(eax & BIT(1))) + return 0; + + /* Set the SME mask if this is an SEV guest. */ + sme_me_mask = BIT_ULL(ebx & 0x3f); + + boot_rdmsr(MSR_AMD64_SEV, &m); + return m.q; +} + +void sev_enable(struct boot_params *bp) +{ bool snp; /* @@ -410,37 +447,13 @@ void sev_enable(struct boot_params *bp) */ snp = snp_init(bp); - /* Check for the SME/SEV support leaf */ - eax = 0x80000000; - ecx = 0; - native_cpuid(&eax, &ebx, &ecx, &edx); - if (eax < 0x8000001f) - return; - - /* - * Check for the SME/SEV feature: - * CPUID Fn8000_001F[EAX] - * - Bit 0 - Secure Memory Encryption support - * - Bit 1 - Secure Encrypted Virtualization support - * CPUID Fn8000_001F[EBX] - * - Bits 5:0 - Pagetable bit position used to indicate encryption - */ - eax = 0x8000001f; - ecx = 0; - native_cpuid(&eax, &ebx, &ecx, &edx); - /* Check whether SEV is supported */ - if (!(eax & BIT(1))) { + sev_status = sev_get_status(); + if (!(sev_status & MSR_AMD64_SEV_ENABLED)) { if (snp) error("SEV-SNP support indicated by CC blob, but not CPUID."); return; } - /* Set the SME mask if this is an SEV guest. */ - boot_rdmsr(MSR_AMD64_SEV, &m); - sev_status = m.q; - if (!(sev_status & MSR_AMD64_SEV_ENABLED)) - return; - /* Negotiate the GHCB protocol version. */ if (sev_status & MSR_AMD64_SEV_ES_ENABLED) { if (!sev_es_negotiate_protocol()) @@ -460,8 +473,6 @@ void sev_enable(struct boot_params *bp) if (snp && !(sev_status & MSR_AMD64_SEV_SNP_ENABLED)) error("SEV-SNP supported indicated by CC blob, but not SEV status MSR."); - - sme_me_mask = BIT_ULL(ebx & 0x3f); } /* Search for Confidential Computing blob in the EFI config table. */ diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 86e1296e87f513b7..081c39b0e8d0d208 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -207,6 +207,8 @@ bool snp_init(struct boot_params *bp); void __init __noreturn snp_abort(void); int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio); void snp_accept_memory(phys_addr_t start, phys_addr_t end); +u64 snp_get_unsupported_features(u64 status); +u64 sev_get_status(void); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -232,6 +234,8 @@ static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *in } static inline void snp_accept_memory(phys_addr_t start, phys_addr_t end) { } +static inline u64 snp_get_unsupported_features(u64 status) { return 0; } +static inline u64 sev_get_status(void) { return 0; } #endif #endif diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c index abcd5703e9f3f980..1015ef883f5850a4 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "efistub.h" #include "x86-stub.h" @@ -790,6 +791,19 @@ static efi_status_t exit_boot(struct boot_params *boot_params, void *handle) return EFI_SUCCESS; } +static bool have_unsupported_snp_features(void) +{ + u64 unsupported; + + unsupported = snp_get_unsupported_features(sev_get_status()); + if (unsupported) { + efi_err("Unsupported SEV-SNP features detected: 0x%llx\n", + unsupported); + return true; + } + return false; +} + static void __noreturn enter_kernel(unsigned long kernel_addr, struct boot_params *boot_params) { @@ -820,6 +834,9 @@ void __noreturn efi_stub_entry(efi_handle_t handle, if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) efi_exit(handle, EFI_INVALID_PARAMETER); + if (have_unsupported_snp_features()) + efi_exit(handle, EFI_UNSUPPORTED); + if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES)) { efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID); if (efi_dxe_table && From patchwork Wed Jun 7 07:23:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104314 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp76255vqr; Wed, 7 Jun 2023 00:39:21 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7n6BLa89FFPVbu9kNrKwMcdOgzlUxe4gXE6E5g/Q5NHfVMYb3KkirRf9YguUC0DhDYKct2 X-Received: by 2002:a05:6808:90e:b0:398:182f:2ba1 with SMTP id w14-20020a056808090e00b00398182f2ba1mr4983099oih.25.1686123561237; Wed, 07 Jun 2023 00:39:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123561; cv=none; d=google.com; s=arc-20160816; b=ZxAz5krdNG/dqZlSmO40+uAlfuf+TApVZrCe6DJUhjvyU/mWQz+DrFfBVZ5UjMVQWj lD3HQ3dAMHhYIhM3Sx9BP2zKZplrjrufSl25HjWbpdzxh4kHV8O0u/g5inBYjCS0EePh AkWs15BDiAIkC9SI3hgqZgPfYw9EDGSrbreARk/ZtLIAEMv2hiXVoDID55fy6ywm2gwb yEFYervgYxIc8NveTzdzDntdJmI1j+RXNj2vQEat9G6tah9Jvp2ukFM///aDjYuo0pYF 1Wfg7lq+yLeJjiEOzxoZW+pvctnnRfGcVZ7QDbZPOyeuBDzB7a0VWwZa2Fu9bOQ3JFEh CxsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=UuqcskEHQ3JN56/Y9rPXvh6/o5Cr2vjin1TefbxPvok=; b=yiVf+z4hkmrq7kFUd66p3pZkNGCjc4kZfFJ2x6ccI+Lpnr08hSM9Esfkt+97a9cJk1 lZ2etzyrCBzMrp1vE/Tf4bdqRxTKLyzIaMgzNvNyY+mCFIBYei4nD40w8+D1dzuy30eg KqP/rjoK1F1tS0ntGxsvI63SCjexc/SUxnSTOe8s3tmWan/QKgCoAzdnP31TN83qwOm/ G/1nhV09X74cDipVDR2HLOrcCh0Te2nppFtMUjs713R8mMTEtcbjV30AbehlctJYqfN9 OIKylQVt7bXjMwf1CPEpicaQRikmJxJ5IZa916lq8kiuM63snHnUxuKFIi5/VizfoeZz 9Y5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jRhL5iPm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b4-20020a633404000000b004faf33e2755si8672756pga.349.2023.06.07.00.39.06; Wed, 07 Jun 2023 00:39:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jRhL5iPm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239302AbjFGH3A (ORCPT + 99 others); Wed, 7 Jun 2023 03:29:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239203AbjFGH1d (ORCPT ); Wed, 7 Jun 2023 03:27:33 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6111C2682; Wed, 7 Jun 2023 00:25:22 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id BFDD0636A4; Wed, 7 Jun 2023 07:25:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 66350C4339E; Wed, 7 Jun 2023 07:25:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122721; bh=bDY2BJK5KLpNF4F9VGbdSrBwRx/fvDJnvG5G13zYe18=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jRhL5iPmu0lkVmVRlWhI01NHkfO7/YEeU3evpu66AuxODbwuH+sLbg8LlmSr53TNz 7qVb3ZJhFHUFzCSFZzAxvjDZLv/Wj6LEpDp/AHia6V7Ft6lw1/AntUl9Lf6LymGxn9 Ky333JYuM29mcm6XTmkRedBO52yK6Pjs8CDgzUCD3nQfvlJYwh+owKIKeNhQVQqxkr S3Va4IDWjhA9BFFzF3rEdqwc0WH5vo7VDhI4uXvWWpPexbMy/eouGqgPbCo0o3Kzrh pll7Mca0br46hLC53zuKu2GGligbIuGy8NaF2ek3rwq2FSE044uPDmjfuuot3VpQ9j G7BfP2PX7nxIA== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 20/20] x86/efistub: Avoid legacy decompressor when doing EFI boot Date: Wed, 7 Jun 2023 09:23:42 +0200 Message-Id: <20230607072342.4054036-21-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=18528; i=ardb@kernel.org; h=from:subject; bh=bDY2BJK5KLpNF4F9VGbdSrBwRx/fvDJnvG5G13zYe18=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBoLbl2avzbWvP8PZ94bww3X1D1Nx9kSe93gpILvpez vQx2curo5SFQYyDQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAExEv5KR4eCjZVeUHm9b0Mdf O0EnZr0YU7HVPr68O7+jprI+/KV1fDEjw8SNR83avZazPt16WzpX5uqRcyoFEZN+V/O6LdZn5Hn LzgcA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028699375180121?= X-GMAIL-MSGID: =?utf-8?q?1768028699375180121?= The bare metal decompressor code was never really intended to run in a hosted environment such as the EFI boot services, and does a few things that are problematic in the context of EFI boot now that the logo requirements are getting tighter. In particular, the decompressor moves its own executable image around in memory, and relies on demand paging to populate the identity mappings, and these things are difficult to support in a context where memory is not permitted to be mapped writable and executable at the same time or, at the very least, is mapped non-executable by default, and needs special treatment for this restriction to be lifted. Since EFI already maps all of memory 1:1, it is unnecessary to create new page tables or handle page faults when decompressing the kernel. That means there is also no need to replace the special exception handlers for SEV. Generally, there is little need to do anything that the decompressor does beyond - initialize SEV encryption, if needed, - perform the 4/5 level paging switch, if needed, - decompress the kernel - relocate the kernel So do all of this from the EFI stub code, and avoid the bare metal decompressor altogether. Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/Makefile | 5 + arch/x86/boot/compressed/efi_mixed.S | 55 ------- arch/x86/boot/compressed/head_32.S | 13 -- arch/x86/boot/compressed/head_64.S | 27 ---- arch/x86/include/asm/efi.h | 7 +- arch/x86/include/asm/sev.h | 2 + drivers/firmware/efi/libstub/x86-stub.c | 167 +++++++++----------- 7 files changed, 84 insertions(+), 192 deletions(-) diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile index b13a580210867ffb..535608fe72e11265 100644 --- a/arch/x86/boot/compressed/Makefile +++ b/arch/x86/boot/compressed/Makefile @@ -74,6 +74,11 @@ LDFLAGS_vmlinux += -z noexecstack ifeq ($(CONFIG_LD_IS_BFD),y) LDFLAGS_vmlinux += $(call ld-option,--no-warn-rwx-segments) endif +ifeq ($(CONFIG_EFI_STUB),y) +# ensure that the static EFI stub library will be pulled in, even if it is +# never referenced explicitly from the startup code +LDFLAGS_vmlinux += -u efi_pe_entry +endif LDFLAGS_vmlinux += -T hostprogs := mkpiggy diff --git a/arch/x86/boot/compressed/efi_mixed.S b/arch/x86/boot/compressed/efi_mixed.S index 8a02a151806df14c..f4e22ef774ab6b4a 100644 --- a/arch/x86/boot/compressed/efi_mixed.S +++ b/arch/x86/boot/compressed/efi_mixed.S @@ -269,10 +269,6 @@ SYM_FUNC_START_LOCAL(efi32_entry) jmp startup_32 SYM_FUNC_END(efi32_entry) -#define ST32_boottime 60 // offsetof(efi_system_table_32_t, boottime) -#define BS32_handle_protocol 88 // offsetof(efi_boot_services_32_t, handle_protocol) -#define LI32_image_base 32 // offsetof(efi_loaded_image_32_t, image_base) - /* * efi_status_t efi32_pe_entry(efi_handle_t image_handle, * efi_system_table_32_t *sys_table) @@ -280,8 +276,6 @@ SYM_FUNC_END(efi32_entry) SYM_FUNC_START(efi32_pe_entry) pushl %ebp movl %esp, %ebp - pushl %eax // dummy push to allocate loaded_image - pushl %ebx // save callee-save registers pushl %edi @@ -290,48 +284,8 @@ SYM_FUNC_START(efi32_pe_entry) movl $0x80000003, %eax // EFI_UNSUPPORTED jnz 2f - call 1f -1: pop %ebx - - /* Get the loaded image protocol pointer from the image handle */ - leal -4(%ebp), %eax - pushl %eax // &loaded_image - leal (loaded_image_proto - 1b)(%ebx), %eax - pushl %eax // pass the GUID address - pushl 8(%ebp) // pass the image handle - - /* - * Note the alignment of the stack frame. - * sys_table - * handle <-- 16-byte aligned on entry by ABI - * return address - * frame pointer - * loaded_image <-- local variable - * saved %ebx <-- 16-byte aligned here - * saved %edi - * &loaded_image - * &loaded_image_proto - * handle <-- 16-byte aligned for call to handle_protocol - */ - - movl 12(%ebp), %eax // sys_table - movl ST32_boottime(%eax), %eax // sys_table->boottime - call *BS32_handle_protocol(%eax) // sys_table->boottime->handle_protocol - addl $12, %esp // restore argument space - testl %eax, %eax - jnz 2f - movl 8(%ebp), %ecx // image_handle movl 12(%ebp), %edx // sys_table - movl -4(%ebp), %esi // loaded_image - movl LI32_image_base(%esi), %esi // loaded_image->image_base - leal (startup_32 - 1b)(%ebx), %ebp // runtime address of startup_32 - /* - * We need to set the image_offset variable here since startup_32() will - * use it before we get to the 64-bit efi_pe_entry() in C code. - */ - subl %esi, %ebp // calculate image_offset - movl %ebp, (image_offset - 1b)(%ebx) // save image_offset xorl %esi, %esi jmp efi32_entry // pass %ecx, %edx, %esi // no other registers remain live @@ -350,15 +304,6 @@ SYM_FUNC_START_NOALIGN(efi64_stub_entry) SYM_FUNC_END(efi64_stub_entry) #endif - .section ".rodata" - /* EFI loaded image protocol GUID */ - .balign 4 -SYM_DATA_START_LOCAL(loaded_image_proto) - .long 0x5b1b31a1 - .word 0x9562, 0x11d2 - .byte 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b -SYM_DATA_END(loaded_image_proto) - .data .balign 8 SYM_DATA_START_LOCAL(efi32_boot_gdt) diff --git a/arch/x86/boot/compressed/head_32.S b/arch/x86/boot/compressed/head_32.S index beee858058df4403..cd9587fcd5084f22 100644 --- a/arch/x86/boot/compressed/head_32.S +++ b/arch/x86/boot/compressed/head_32.S @@ -84,19 +84,6 @@ SYM_FUNC_START(startup_32) #ifdef CONFIG_RELOCATABLE leal startup_32@GOTOFF(%edx), %ebx - -#ifdef CONFIG_EFI_STUB -/* - * If we were loaded via the EFI LoadImage service, startup_32() will be at an - * offset to the start of the space allocated for the image. efi_pe_entry() will - * set up image_offset to tell us where the image actually starts, so that we - * can use the full available buffer. - * image_offset = startup_32 - image_base - * Otherwise image_offset will be zero and has no effect on the calculations. - */ - subl image_offset@GOTOFF(%edx), %ebx -#endif - movl BP_kernel_alignment(%esi), %eax decl %eax addl %eax, %ebx diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index 556ee504325ae50e..1027e28ced8f2d52 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -146,19 +146,6 @@ SYM_FUNC_START(startup_32) #ifdef CONFIG_RELOCATABLE movl %ebp, %ebx - -#ifdef CONFIG_EFI_STUB -/* - * If we were loaded via the EFI LoadImage service, startup_32 will be at an - * offset to the start of the space allocated for the image. efi_pe_entry will - * set up image_offset to tell us where the image actually starts, so that we - * can use the full available buffer. - * image_offset = startup_32 - image_base - * Otherwise image_offset will be zero and has no effect on the calculations. - */ - subl rva(image_offset)(%ebp), %ebx -#endif - movl BP_kernel_alignment(%esi), %eax decl %eax addl %eax, %ebx @@ -335,20 +322,6 @@ SYM_CODE_START(startup_64) /* Start with the delta to where the kernel will run at. */ #ifdef CONFIG_RELOCATABLE leaq startup_32(%rip) /* - $startup_32 */, %rbp - -#ifdef CONFIG_EFI_STUB -/* - * If we were loaded via the EFI LoadImage service, startup_32 will be at an - * offset to the start of the space allocated for the image. efi_pe_entry will - * set up image_offset to tell us where the image actually starts, so that we - * can use the full available buffer. - * image_offset = startup_32 - image_base - * Otherwise image_offset will be zero and has no effect on the calculations. - */ - movl image_offset(%rip), %eax - subq %rax, %rbp -#endif - movl BP_kernel_alignment(%rsi), %eax decl %eax addq %rax, %rbp diff --git a/arch/x86/include/asm/efi.h b/arch/x86/include/asm/efi.h index 8b4be7cecdb8eb73..b0994ae3bc23f84d 100644 --- a/arch/x86/include/asm/efi.h +++ b/arch/x86/include/asm/efi.h @@ -90,6 +90,8 @@ static inline void efi_fpu_end(void) } #ifdef CONFIG_X86_32 +#define EFI_X86_KERNEL_ALLOC_LIMIT (SZ_512M - 1) + #define arch_efi_call_virt_setup() \ ({ \ efi_fpu_begin(); \ @@ -103,8 +105,7 @@ static inline void efi_fpu_end(void) }) #else /* !CONFIG_X86_32 */ - -#define EFI_LOADER_SIGNATURE "EL64" +#define EFI_X86_KERNEL_ALLOC_LIMIT EFI_ALLOC_LIMIT extern asmlinkage u64 __efi_call(void *fp, ...); @@ -218,6 +219,8 @@ efi_status_t efi_set_virtual_address_map(unsigned long memory_map_size, #ifdef CONFIG_EFI_MIXED +#define EFI_ALLOC_LIMIT (efi_is_64bit() ? ULONG_MAX : U32_MAX) + #define ARCH_HAS_EFISTUB_WRAPPERS static inline bool efi_is_64bit(void) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 081c39b0e8d0d208..2cc2ef57e1241a87 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -161,6 +161,7 @@ static __always_inline void sev_es_nmi_complete(void) __sev_es_nmi_complete(); } extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd); +extern void sev_enable(struct boot_params *bp); static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs) { @@ -215,6 +216,7 @@ static inline void sev_es_ist_exit(void) { } static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; } static inline void sev_es_nmi_complete(void) { } static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; } +static inline void sev_enable(struct boot_params *bp) { } static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) { return 0; } static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs) { return 0; } static inline void setup_ghcb(void) { } diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c index 1015ef883f5850a4..ae1a9b4c1af6191f 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -15,17 +15,14 @@ #include #include #include +#include #include #include "efistub.h" #include "x86-stub.h" -/* Maximum physical address for 64-bit kernel with 4-level paging */ -#define MAXMEM_X86_64_4LEVEL (1ull << 46) - const efi_system_table_t *efi_system_table; const efi_dxe_services_table_t *efi_dxe_table; -u32 image_offset __section(".data"); static efi_loaded_image_t *image = NULL; static efi_memory_attribute_protocol_t *memattr; @@ -287,28 +284,6 @@ void efi_adjust_memory_range_protection(unsigned long start, } } -void startup_32(struct boot_params *boot_params); - -static void -setup_memory_protection(unsigned long image_base, unsigned long image_size) -{ -#ifdef CONFIG_64BIT - if (image_base != (unsigned long)startup_32) - efi_adjust_memory_range_protection(image_base, image_size); -#else - /* - * Clear protection flags on a whole range of possible - * addresses used for KASLR. We don't need to do that - * on x86_64, since KASLR/extraction is performed after - * dedicated identity page tables are built and we only - * need to remove possible protection on relocated image - * itself disregarding further relocations. - */ - efi_adjust_memory_range_protection(LOAD_PHYSICAL_ADDR, - KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR); -#endif -} - static void setup_unaccepted_memory(void) { efi_guid_t mem_acceptance_proto = OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID; @@ -334,9 +309,7 @@ static void setup_unaccepted_memory(void) static const efi_char16_t apple[] = L"Apple"; -static void setup_quirks(struct boot_params *boot_params, - unsigned long image_base, - unsigned long image_size) +static void setup_quirks(struct boot_params *boot_params) { efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long) efi_table_attr(efi_system_table, fw_vendor); @@ -345,9 +318,6 @@ static void setup_quirks(struct boot_params *boot_params, if (IS_ENABLED(CONFIG_APPLE_PROPERTIES)) retrieve_apple_device_properties(boot_params); } - - if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES)) - setup_memory_protection(image_base, image_size); } /* @@ -500,7 +470,6 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, } image_base = efi_table_attr(image, image_base); - image_offset = (void *)startup_32 - image_base; status = efi_allocate_pages(sizeof(struct boot_params), (unsigned long *)&boot_params, ULONG_MAX); @@ -804,6 +773,61 @@ static bool have_unsupported_snp_features(void) return false; } +static void efi_get_seed(void *seed, int size) +{ + efi_get_random_bytes(size, seed); + + /* + * This only updates seed[0] when running on 32-bit, but in that case, + * seed[1] is not used anyway, as there is no virtual KASLR on 32-bit. + */ + *(unsigned long *)seed ^= kaslr_get_random_long("EFI"); +} + +static void error(char *str) +{ + efi_warn("Decompression failed: %s\n", str); +} + +static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry) +{ + unsigned long virt_addr = LOAD_PHYSICAL_ADDR; + unsigned long addr, alloc_size, entry; + efi_status_t status; + u32 seed[2] = {}; + + /* determine the required size of the allocation */ + alloc_size = ALIGN(max((unsigned long)output_len, kernel_total_size), + MIN_KERNEL_ALIGN); + + if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && !efi_nokaslr) { + u64 range = KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR - kernel_total_size; + + efi_get_seed(seed, sizeof(seed)); + + virt_addr += (range * seed[1]) >> 32; + virt_addr &= ~(CONFIG_PHYSICAL_ALIGN - 1); + } + + status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr, + seed[0], EFI_LOADER_CODE, + EFI_X86_KERNEL_ALLOC_LIMIT); + if (status != EFI_SUCCESS) + return status; + + entry = decompress_kernel((void *)addr, virt_addr, error); + if (entry == ULONG_MAX) { + efi_free(alloc_size, addr); + return EFI_LOAD_ERROR; + } + + *kernel_entry = addr + entry; + + efi_adjust_memory_range_protection(addr, kernel_total_size); + + return EFI_SUCCESS; +} + static void __noreturn enter_kernel(unsigned long kernel_addr, struct boot_params *boot_params) { @@ -823,10 +847,9 @@ void __noreturn efi_stub_entry(efi_handle_t handle, struct boot_params *boot_params) { efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID; - unsigned long bzimage_addr = (unsigned long)startup_32; - unsigned long buffer_start, buffer_end; struct setup_header *hdr = &boot_params->hdr; const struct linux_efi_initrd *initrd = NULL; + unsigned long kernel_entry; efi_status_t status; efi_system_table = sys_table_arg; @@ -855,60 +878,6 @@ void __noreturn efi_stub_entry(efi_handle_t handle, goto fail; } - /* - * If the kernel isn't already loaded at a suitable address, - * relocate it. - * - * It must be loaded above LOAD_PHYSICAL_ADDR. - * - * The maximum address for 64-bit is 1 << 46 for 4-level paging. This - * is defined as the macro MAXMEM, but unfortunately that is not a - * compile-time constant if 5-level paging is configured, so we instead - * define our own macro for use here. - * - * For 32-bit, the maximum address is complicated to figure out, for - * now use KERNEL_IMAGE_SIZE, which will be 512MiB, the same as what - * KASLR uses. - * - * Also relocate it if image_offset is zero, i.e. the kernel wasn't - * loaded by LoadImage, but rather by a bootloader that called the - * handover entry. The reason we must always relocate in this case is - * to handle the case of systemd-boot booting a unified kernel image, - * which is a PE executable that contains the bzImage and an initrd as - * COFF sections. The initrd section is placed after the bzImage - * without ensuring that there are at least init_size bytes available - * for the bzImage, and thus the compressed kernel's startup code may - * overwrite the initrd unless it is moved out of the way. - */ - - buffer_start = ALIGN(bzimage_addr - image_offset, - hdr->kernel_alignment); - buffer_end = buffer_start + hdr->init_size; - - if ((buffer_start < LOAD_PHYSICAL_ADDR) || - (IS_ENABLED(CONFIG_X86_32) && buffer_end > KERNEL_IMAGE_SIZE) || - (IS_ENABLED(CONFIG_X86_64) && buffer_end > MAXMEM_X86_64_4LEVEL) || - (image_offset == 0)) { - extern char _bss[]; - - status = efi_relocate_kernel(&bzimage_addr, - (unsigned long)_bss - bzimage_addr, - hdr->init_size, - hdr->pref_address, - hdr->kernel_alignment, - LOAD_PHYSICAL_ADDR); - if (status != EFI_SUCCESS) { - efi_err("efi_relocate_kernel() failed!\n"); - goto fail; - } - /* - * Now that we've copied the kernel elsewhere, we no longer - * have a set up block before startup_32(), so reset image_offset - * to zero in case it was set earlier. - */ - image_offset = 0; - } - #ifdef CONFIG_CMDLINE_BOOL status = efi_parse_options(CONFIG_CMDLINE); if (status != EFI_SUCCESS) { @@ -926,6 +895,12 @@ void __noreturn efi_stub_entry(efi_handle_t handle, } } + status = efi_decompress_kernel(&kernel_entry); + if (status != EFI_SUCCESS) { + efi_err("Failed to decompress kernel\n"); + goto fail; + } + /* * At this point, an initrd may already have been loaded by the * bootloader and passed via bootparams. We permit an initrd loaded @@ -965,7 +940,7 @@ void __noreturn efi_stub_entry(efi_handle_t handle, setup_efi_pci(boot_params); - setup_quirks(boot_params, bzimage_addr, buffer_end - buffer_start); + setup_quirks(boot_params); setup_unaccepted_memory(); @@ -975,13 +950,15 @@ void __noreturn efi_stub_entry(efi_handle_t handle, goto fail; } + /* + * Call the SEV init code while still running with the firmware's + * GDT/IDT, so #VC exceptions will be handled by EFI. + */ + sev_enable(boot_params); + efi_5level_switch(); - if (IS_ENABLED(CONFIG_X86_64)) - /* add offset of startup_64() */ - bzimage_addr += 0x200; - - enter_kernel(bzimage_addr, boot_params); + enter_kernel(kernel_entry, boot_params); fail: efi_err("efi_stub_entry() failed!\n");