From patchwork Fri Jun 2 10:12:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102453 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp926371vqr; Fri, 2 Jun 2023 03:32:20 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6/fM69xMu+adnIv9DnMryvvyXak8d0jxtlMkM0fIJc+vOSuo9EQr08VfgBEXeiNvYDdrXY X-Received: by 2002:a05:622a:10a:b0:3f6:aff0:6dfe with SMTP id u10-20020a05622a010a00b003f6aff06dfemr13357507qtw.37.1685701939920; Fri, 02 Jun 2023 03:32:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685701939; cv=none; d=google.com; s=arc-20160816; b=ue5uaxGxE9cutxxCKntm1I9+cj8Cf/rc1jb/Q3iMYFycRXMAd8rNVxPb7G78fT6dag XGvZEkUgwT+TTo5PkImVJcSP2KLqPjq46BXv4/vBz8jIIvz/Fl1VYzieCD2cqLBtIrxI atRvzm+d0oV1+sJb7KILrNQITXrOVlXwfrXI2r02hxDSkQlc3GlKzrK4UaM5KhjHCZPj SUhxuKyJ//d9lWNRHFyACRcAwUXqe3xWmusdBv8KyLPk6lOj8VdbjaaFGBhmyV1TNLVN awz1z9evQnRAso2CBHINBWGhDEzQ/vNMiFwVteyaJB6vo4sRnOzP1UujyybezMwKKeLR dtCw== 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=KeTPHj+I1fYwbE5NTuxo0fAziTIJ76oBXolfyY/XHLA=; b=LByMT2Ga14pQcWsADFCNUtp+CNHrRJUszQgPgjixMV5sIRKBNbaeIVRrqtRh68RFjr LXIy/YPUjC7Qb9XmRKX5FSmvV+1ld5LNMqxm5d1An27nsENeNgWZqO1fWzUUvBTXwKgy snt9czOinBrB2V8cyQPg+a1MnK4xbnhlt2Elbyq1BdN/It3qMsnOfFAlH2dJYO5ziB3u 6omNk64kIzMKy/0KZVaPNLcxUS+fVKkfOXrSFtUzRi2nBJAqvlnwc6jNowunlmcXRimF xYGjDlGx75ug/BxD7L27Ucmi8UJXJPdHkV9wKoEZ1CRvS5em2+NasJuB2FIjQZe7UpiQ p+bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=K2u71tj2; 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 l62-20020a638841000000b005362a6e5395si836507pgd.229.2023.06.02.03.32.05; Fri, 02 Jun 2023 03:32: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=K2u71tj2; 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 S235444AbjFBKXJ (ORCPT + 99 others); Fri, 2 Jun 2023 06:23:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235377AbjFBKWO (ORCPT ); Fri, 2 Jun 2023 06:22:14 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E5E51A7; Fri, 2 Jun 2023 03:22:12 -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 3BA4364E77; Fri, 2 Jun 2023 10:22:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AC33DC433D2; Fri, 2 Jun 2023 10:22:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701331; bh=2ugw2aI3WS8XFLpq392iX/qheNdS02iXQMzmuFjsdqI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K2u71tj2CtRCMgj8JKI0rybfZ5Qn7hS9qJVCdQZcd2kt0ypXAbHBekGJyMIRwRSr1 1iQqYieUF7zYnbAlhIz+2cDP6gMjjdZRTiKVvCMH+FLd+FL/kU6avIoWPCKKEDpEXC /lXwDojb2VykTcf2Vu6lQpUVQ4A7uOIqAmO0kuhmnReuuN8rrDfKw46IcYHHRjCVyu 1C1pQZrCCOPQNwiMTbxRr+kCaxCr+HRf18Slv934O4rcNhhIIZMe/XTwBU8DuHCOTx bd/nZ0BIHtmiptb3okFsRCbflPMOLYD0oo5qEnY/rZjIhjtDXbz+PiLFuVSnvn0OMx nLx+Cq4Fiv3jA== 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 v4 01/21] x86/efistub: Branch straight to kernel entry point from C code Date: Fri, 2 Jun 2023 12:12:53 +0200 Message-Id: <20230602101313.3557775-2-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1778; i=ardb@kernel.org; h=from:subject; bh=2ugw2aI3WS8XFLpq392iX/qheNdS02iXQMzmuFjsdqI=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywIKrhi7XVpdfTgu8p1a5ddOvKrn5p+fWbTFQUbgaP PEPz4M/HaUsDGIcDLJiiiwCs/++23l6olSt8yxZmDmsTCBDGLg4BWAi+XcZGdZHROwtufPF4oSU 4hadjQKn7+TV9U/9oNL/7sHMEyWBBpEMf+XsN5qEaiwT5NSzecemE+4q/p7pTxxjUG32wqctL19 P4gQA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-7.3 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?1767586597544281750?= X-GMAIL-MSGID: =?utf-8?q?1767586597544281750?= 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 | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c index 8d17cee8b98e1a63..095aaa8b0ee30fb9 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -769,9 +769,17 @@ 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 success, we jump to the relocated kernel directly and never return. * On failure, we exit to the firmware via efi_exit instead of returning. */ asmlinkage unsigned long efi_main(efi_handle_t handle, @@ -914,7 +922,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 Fri Jun 2 10:12:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102464 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp928264vqr; Fri, 2 Jun 2023 03:36:23 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6XVuv1mVZ8H9dcoM89kNgrh/ucg+rdeaC+AH4ATDleaF9RkHPwzY1zeHOaVAAVWU1xuvXu X-Received: by 2002:a17:902:7209:b0:1ac:753a:7933 with SMTP id ba9-20020a170902720900b001ac753a7933mr1039192plb.39.1685702183187; Fri, 02 Jun 2023 03:36:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685702183; cv=none; d=google.com; s=arc-20160816; b=Tdk5ZgJVf9z9EKdxCVg/cnlfCgkSVGIYlKAbuoYHYDkw+IoviEckssFZKGVVyua9hs FGBY4FHMzr17doWymluwLf/99bWw1TofAaRhqQmgfBDW3rghokLPKAxbl7eLBaU48Oqh vaKlc0rSJSBkrmf+gXAI/1khAufZyBdxRS4uEsTG+ERt4r+PQG4a+ruKRiDP2e8e7GyW vedVE/CpVl2BWN72+qA2P2WFNwzS0W0ylWr7CtvmF+AsEIIH2309g25ij/2NLELuxWJn ggvvKjUY2pQFiwHyz6c4JsQr0MOvtd/x+hKTDOQFpC3FRgk8WURngzfqJWyNl9/UhB0k jpYA== 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=HHl6DaPAZuzuVBaUYnhJ30eQSA1WIWAumx+zdtOJIoE=; b=cp1LRoHpEg6iJIi3TwGkEvyg/+uO9b1ertNStyboc7SAvs2MVVJ+kLqIXygHGQUobI gaRHleGYBUfx5mjKWOpdPnSjNuO8InDPnxbEd9dBIOSlPLZjWZA2e8ePi7sbD8Dgv/qZ gK2lnIggGthsLmTa8I1eNhDaEjtZ8GeN0RQekRCpzFJsP+mGj994Bo4t7oGXxBAlY9l5 XthhwmSDhqfdePguW9NaSN5SvuVo19OL6fLhs9r8BW3W8ouCFqHyja6eND7gippDgFMv GDuymqTbCutpfoMORGIP2/dcnfsLgOo3Gq59/u3HvWIlzEKvQA83kaJsZnYAClzWR7/v KoXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FwZc6n7D; 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 je17-20020a170903265100b001ac82e60516si683109plb.323.2023.06.02.03.36.09; Fri, 02 Jun 2023 03:36: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=FwZc6n7D; 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 S235438AbjFBKXE (ORCPT + 99 others); Fri, 2 Jun 2023 06:23:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235151AbjFBKWS (ORCPT ); Fri, 2 Jun 2023 06:22:18 -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 11A971A2; Fri, 2 Jun 2023 03:22: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 9732364E71; Fri, 2 Jun 2023 10:22:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1DCE8C433A1; Fri, 2 Jun 2023 10:22:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701336; bh=6vIoJTudACrMHaUZpEmA9kKzuuAA0lBBUtfzLDgP/Vg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FwZc6n7DPdapDp2Ze4CXOM31IKbh0eg+q4IJAck/FYqJ6Pz4jl6R/9drIZeK4FZ/7 PqCGsVkq+GgAU+9RX53A/RYcc7yJCpN9flRRXsfEICAkAHJCCNAyf+Q4r/b7Gi42mB gKkLWw3hcIu/Ocu6mMro2rN4WnuJoBNPa3HHU/a/f+n2q8Qrcr3bMhvXg9tZaVPQJk pLQl1nE5+isCzjxCq0327s91qm2sNU5Awqvug+S8dUaMj8il0sgoiP4hCjP3GbwOaB SrnKqvGsDaSe9y5zxOQ0ER/nYjhVfc3O0zW1c+P1DesA/U2Uv0N1vGHQ7eoGh1Qd7j noy2vRtlcTjfw== 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 v4 02/21] x86/efistub: Simplify and clean up handover entry code Date: Fri, 2 Jun 2023 12:12:54 +0200 Message-Id: <20230602101313.3557775-3-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=7508; i=ardb@kernel.org; h=from:subject; bh=6vIoJTudACrMHaUZpEmA9kKzuuAA0lBBUtfzLDgP/Vg=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywMI/xWdftd5W1Ti0skFl44qdD6xDGlvOaew6xSowt /1M7bKVHaUsDGIcDLJiiiwCs/++23l6olSt8yxZmDmsTCBDGLg4BWAiMucYGW7nBm2VDizK9tJo 0rxft+L47YZdLmv54j4nHXx7IovdQoGRYU5ikSNT4RX+S3fOf7Ooda2+btKRHtksrPvvpP3JT6q XeAA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.6 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?1767586852600741892?= X-GMAIL-MSGID: =?utf-8?q?1767586852600741892?= 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 095aaa8b0ee30fb9..d6a376e52cbe1399 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -782,9 +782,9 @@ static void __noreturn enter_kernel(unsigned long kernel_addr, * On success, we jump to the relocated kernel directly and never return. * On failure, we 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; @@ -928,7 +928,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 Fri Jun 2 10:12:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102443 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp924129vqr; Fri, 2 Jun 2023 03:27:35 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7jeMDTQ+Qjl5lJLyKu8RHO1sSAo22gXvU8m0lC+9tL3ejmjh9YDBLT7WwAm7X4EVki+inh X-Received: by 2002:a81:d54e:0:b0:54d:ea34:c31 with SMTP id l14-20020a81d54e000000b0054dea340c31mr13007413ywj.29.1685701655340; Fri, 02 Jun 2023 03:27:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685701655; cv=none; d=google.com; s=arc-20160816; b=0GtOYLiUXXDtLvbp0iZ7Td6UWbhRhtiMqwFYXDPRJUNv/0JXBD/j+ez4ShzP5dbdhz qUhFEWdOzMSSowG8Ok0/1j9bBsAVbUbbzRcm28KcI/rCa9QKzFFmiPeU27aQ/iyOmrdU 2lOAIG/W80sEollVOLYdGrln8iYyMIe6MWdLETgyRRAqk8/mNO6zRHddQlhZDHjgSDry QDUSn9IYNS4hJ3ugYmqfj98VMn1WzzqbasZ5/LtHyXS8XrwcQdbgrlv2SsOMS9SUBnsW +eASHTRoJvj8WCSw5BribaMlQgpIMZ8aUu0YAv1uqw+MsKPZOJTZ66Zez70hh9p3kUH7 gIDw== 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=IFmGxTNTlUQSxzDxLfj1MOCIbgN66pJj5zEHmmDfi/jtMFQ/JzpVWUYk/OPH2/RU+v YTPZaUylkn7eOAmNMoxizoSiXech9hp1RQUDcOMWdM2ZopgLusl2hixdH8iTQP5nKoPD jkqrUKl59bCYtJt2bKIsAnPtOcG0oP+fFWwDu78Jxe0jS0xAm9pHUVqIXYsNb5NubwiR o+CH4Q6HvPErKGXIQXskfXOmYqQsg1a4mGWMTk/M8oWjrrzKisy4ivfIc1hdMahAEw+n boIJJiDP8Y70+N+V+Kd+VGf01TRA84qG9zUcssfe/47TRT+tYffHiU+7lOXEVmVXs6Bo //sw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZHQQ8iia; 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 r16-20020a638f50000000b00530b135eae9si806643pgn.119.2023.06.02.03.27.19; Fri, 02 Jun 2023 03:27: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=ZHQQ8iia; 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 S235042AbjFBKXO (ORCPT + 99 others); Fri, 2 Jun 2023 06:23:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235342AbjFBKWX (ORCPT ); Fri, 2 Jun 2023 06:22:23 -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 83AD11AE; Fri, 2 Jun 2023 03:22:21 -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 05C0464E71; Fri, 2 Jun 2023 10:22:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7A312C4339C; Fri, 2 Jun 2023 10:22:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701340; bh=+pOeFG4I2M0NxWTqt2DlFeFWBXmc5I0TflwHlC5RHXA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZHQQ8iiabB8LyufShp63/BFbUFbzOsYVWTKUi65Muc89+1LlzSmC4seQ4t5NnekYW q3amSyw7qDJpJgAyzuClLUQ7PNqWnsuOwFCAS6/xoEyBBmv9gb+Q5OKkmYLrUAft1m +RSn0pbgLQ82JlXs70oJnJXuOpa8EyTaBm5N45QdyDML95rmNZa/Rchlvv36h1n3bX UYMM8V/chWu+W8URIXqRvjD1ZXRVbayKKYZxOklXL/wBgc+iVr6Ayn/NIXny2SCBi3 kQ5ypVcJWllJpClE0XKIB57ucg9ULU++xnhhQ3pRG8UnBLugQiySiwAraJCOAm78s/ 97tKMrlBS55lg== 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 v4 03/21] x86/decompressor: Avoid magic offsets for EFI handover entrypoint Date: Fri, 2 Jun 2023 12:12:55 +0200 Message-Id: <20230602101313.3557775-4-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-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=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywOI3HT6SmyscomyMjU58fXF7TUzazdYEE8dZQYfXb 9MJ36HZUcrCIMbBICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACbyjJvhD//d1w33X2q9MVm8 xXJS1KQJbA3zJ/9Ss5v14eNruejeKZMY/tfet08tOHyzIObI+l2dwpuNLC4pM+6Z6me5JWVaT8W RN0wA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.6 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?1767586298911442201?= X-GMAIL-MSGID: =?utf-8?q?1767586298911442201?= 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 Fri Jun 2 10:12:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102447 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp925056vqr; Fri, 2 Jun 2023 03:29:43 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5YcZA8PDFrouwFu4kD9wd9sXCA64e3502RRR7F0z4Np4Jd2WXkrNdgw3gEC0CEk3LcNmKB X-Received: by 2002:a05:6870:73c4:b0:187:9783:a20e with SMTP id a4-20020a05687073c400b001879783a20emr1697378oan.37.1685701783216; Fri, 02 Jun 2023 03:29:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685701783; cv=none; d=google.com; s=arc-20160816; b=C0XPqD85je4SKr0drVNp53loExY8xmiDkr/ezoAiuFgQ386oCc68fd1tAU4+n7Lon/ /8p/kGvvnFUeM4+GkLkcVXNhtJQ79mkLaqxb06Rbh0nqyEcFh/2cBCzWQEU4SuHS9oRa ILy9h/RCNWEgOxAGWPtJirkqCns39PxvCy1DzK0b6C2njii4geLkBpOp2/sBBuqziusL TZlsq40WQoGmWD6fek0zNThrIpROzXEkSMujQpF+qzRR5QGV3KUtebqRgw5m9BPWle6o /e8yulBQeaeSdll77QL+ajLYKv/SWfxIhdwbJ0OmifxekrV5GwH0RV5iwIuUb9EXV7Dn 5Dhg== 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=KnbIczG5VJn+Jh7FPVtSUai82qqjNe0euxAX6s+PaBo=; b=C8DwcP5HtGzaUx5DS8IrJH3BLG8hV1NdzwL8hKZguJltRVuzder4Qksa5BRrOVYniM EzEDp9VyMAXfMp7Ml1YeyVq9/YjfnXFvpXczezvlbggJoq7Dy3oH99zDzNwNTOd6+Cjy yGg6xqnGaLQMDqCwpvjynhpoSPRY3E5j8YlsfzBf+6bLpDwVForxYwwkuGfM2TR9fZWa y7ZNNdsXJHtfaLq4pmPZI81i3ObWbMqcbaJUeeje+0kYPLI1phxMxg8kIopgtdDEZYoR 2qaNHFpRHiW5Z76DHDtUAy2V5sYR9BUcmANg5zyKoctPh6QQ7lQdkjf9ViI+XPERYKcq N5LA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=J8XIym0L; 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 q29-20020a638c5d000000b0053b9702001csi802792pgn.314.2023.06.02.03.29.28; Fri, 02 Jun 2023 03:29: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=J8XIym0L; 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 S234410AbjFBKXT (ORCPT + 99 others); Fri, 2 Jun 2023 06:23:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235346AbjFBKW0 (ORCPT ); Fri, 2 Jun 2023 06:22:26 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5EB61AD; Fri, 2 Jun 2023 03:22:25 -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 6245364D72; Fri, 2 Jun 2023 10:22:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D7F09C433EF; Fri, 2 Jun 2023 10:22:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701344; bh=GI53KZcq3ZI3RxFd9aG0XytLXRdQ1EhHXwVomNHohgI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J8XIym0Lyhfz4AvAyKuXkqdvzHiAvwTYsNh8b2C+V5KUAfkWwbPccxBAepB/jmwGx CKwDMLbaZBnojettYP09S/iRurmuFOUzshOLeTs4x8Mgmk6i3Tr8sLLfLkunMbYw6q 4Y5XFHNAOhfRyh4jTLSsKw6B8T006NY4dPwAq1ePcMYOD0hnAVad4YdbWTRG9StB9q gr1DJffn+Cah5RqWIKrFIwvQSSwEGEkACput+uX05JOhaWcU9Uh+KkDMy/3/5vukFw 29KkC8Uv0hj/6LqsQ252KOQQ3RheQKfktbz9hhdDvb5szqrnnnWEYQoaCXPER3VxOq iRy23vVmIkhVA== 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 v4 04/21] x86/efistub: Clear BSS in EFI handover protocol entrypoint Date: Fri, 2 Jun 2023 12:12:56 +0200 Message-Id: <20230602101313.3557775-5-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-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=GI53KZcq3ZI3RxFd9aG0XytLXRdQ1EhHXwVomNHohgI=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywJL4TY7ZNU/ZV6cpv2uJT3g7L0reK014z1yz8u1LG rd8NkzsKGVhEONgkBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABPJXsnI8OpnWZB8rkj81fyI O0z8Dw/M4nR6NtFywRGBtW9N572qs2FkeLT00Yncj/E/xAPyZcQPL/New+NT3Ct1cEn2SsmChL/ J3AA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-7.3 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?1767586433027399904?= X-GMAIL-MSGID: =?utf-8?q?1767586433027399904?= 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 d6a376e52cbe1399..d010448dffb12cb8 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -934,12 +934,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 Fri Jun 2 10:12:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102461 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp927964vqr; Fri, 2 Jun 2023 03:35:41 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4zSS3eK+zFlUPmGA0vyCFNkkoGWgRb8UabtpXlYWuX7/lYBGeKmRBO54gly81w3RI3zqDZ X-Received: by 2002:a17:903:22c7:b0:1aa:86a4:37ed with SMTP id y7-20020a17090322c700b001aa86a437edmr1085163plg.55.1685702141073; Fri, 02 Jun 2023 03:35:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685702141; cv=none; d=google.com; s=arc-20160816; b=Pe+tXSCMzfnmWHxMrKTCBuBvQaUWmZpaGu7+HUrPZ+UbEcs5O7V0sk80WG/E3XU2Pg O+g0ORMBsYD4N9i6VeG+aS7P3e/JhSxulLHKM6rzmlhVMYEAmR4y5mxb0XyOnnuI1pkI uCkv8rcdowjWQlPCE6yXND0YmuggYNiMcpcKFelAaAsn8BJt5hpQWEEUn6IjDk1fX7GD fRz24V5JNQJ/i408gp2G89RDKj23u8kPUEWeNM2mwWx4mo5JoS+HjAHER0VNuhtTubk8 qakWOV9LpGKOC2K/Ji08xovd3cGOEQREptiA1R24Oft+L/QGdBveJITZfhB2Fld6wJWu YBbg== 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=FjT5Yai+SQZTHu/rdI8VDU1/HKrlP4T+d64UJHOBVE/3Yn9ycHwj9+O7zrG7WAbtdF +qDcvd55CrSCIiOKNTQFSlftgGMwgZr+Lqk4RooQjmhTEAY/OHPYQnA/L+6/Gb3J1xE+ SYtj6W+G9edtOuLnJog5ZGNbzwc/GiwGpUYFb5Zm4lXVAjSz8yC3Fm2d6+MQOGHd+ajp MZHSfUT2+jZP9nxuOvFChXvYzuw0ISYRCcFu8GyEKMswSIkkKEDHEsUn951yz6y/tD3f ZCASW2VPdWC3DbTkMWnT6/jszcjF8XlDnDLccGB3ujnDP6curoKg3wqSgcvlqHa/ReD0 5rqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GZ1i4lSN; 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 u1-20020a170902e80100b001b1be1317a2si753161plg.103.2023.06.02.03.35.26; Fri, 02 Jun 2023 03:35:41 -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=GZ1i4lSN; 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 S235473AbjFBKXZ (ORCPT + 99 others); Fri, 2 Jun 2023 06:23:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235110AbjFBKWj (ORCPT ); Fri, 2 Jun 2023 06:22:39 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43F8AE4A; Fri, 2 Jun 2023 03:22:30 -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 BC4DE6150D; Fri, 2 Jun 2023 10:22:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 40178C4339E; Fri, 2 Jun 2023 10:22:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701349; bh=ygI81p6SqewQoIn6tCgoZLUH2Qo5tPVgo3BNvwGSS3U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GZ1i4lSNKD6ujUUsHx+hPnCpXcwIdXt7P5tqVeJtNUBNzyDpooOlOTw+T21o+TsZv naJVHYDHvDFpzoY0b86GkXMYO5xTC/ZyTyTEqTpYFvgVj0WUt6vCGCLzH9QWjSzgJ+ /zlHY32uUjWUK6/K83Npl3OFBknz761RAdthFdtMKVkrDh8ngOd7itHF8mjqPA3pic oqJZSI30+M/doTqE8bM3PhF/Z88E+6P0U47FmAiCuWuIfAj25VzsJnJQSUJFw3uJmq IKHAMrwJWUWcpgcNxCn2MWywcOTMvNtaTOdSUWlYuL7/29Z3XWZDXQkyH3O+yc6HYn K+ej+axOgFS6A== 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 v4 05/21] x86/decompressor: Use proper sequence to take the address of the GOT Date: Fri, 2 Jun 2023 12:12:57 +0200 Message-Id: <20230602101313.3557775-6-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-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=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywNJ9MbLVZ4udDPN2ps4Pn9SSvGH2vsUaWfVlujOqd hx/UanRUcrCIMbBICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACbydRbD/9Tq5ZsP/5x+825W lvFh/z0C51+52kyxidQ9pZbMz1SrmcnwPyQ3hI/BZYrdas6alLLsTyGux3ZJ6PLsFVASevzi650 2LgA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-7.3 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?1767586808273131704?= X-GMAIL-MSGID: =?utf-8?q?1767586808273131704?= 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 Fri Jun 2 10:12:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102444 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp924558vqr; Fri, 2 Jun 2023 03:28:36 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6a6lxHXepVv3lhBPiVfuSAMWfvuq24wniGXdJxjyrrIvA9EvbH1GWJ5jlJRXpTKpvfpySP X-Received: by 2002:a05:6358:714:b0:123:3203:9287 with SMTP id e20-20020a056358071400b0012332039287mr11197087rwj.4.1685701715767; Fri, 02 Jun 2023 03:28:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685701715; cv=none; d=google.com; s=arc-20160816; b=C+nB7tLKnKic1HnqvYkx7iJxVOR8jZRZFTnzF1zaeJxmmId0jJ0Up/sdjiGaIJ9U3F joYqkr1M4BgsMDx2ewhVK+YCM6uC7DzTeWtLXjSCT7ML/ybA3lLO6oMhWR1w6THyng7d Zgv0wBjrBjVKMGiDwFfFQvmWs01DG/JpeuatFuHVgv2GxS/yTCqEzkWfK2j8NXIKyCql O/bi5VXPAKMzjGD0GzKo717507a44gTsyNqXcvb5AgGEsPPPNlk+qDK/IPG88zneOqK7 uiioyIYRN9Jf0oSH/vuXHecr+jC7XtIpOrROIQ9spX8LjeCtjKDOQdiNbxG9eG787EAS +CDg== 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=poHomjragXGOq3rvQ/By9rHKWSkPvCZKzPxc4WzumXBtPIVUhZoCgAS3Y5vbOhRkBr dkx+By3Omw2newiT9F6kcNSOmVXjHliEfexgbnHOCxNka8QnRJ5NDSvWd0QtlZJpWlPH 9ybEdzFqjYN4O/pGSHxhQHiIG/mm1xefriYAHQimQtQODZsYG+fl4YU9XlZZ/sKYedrx 7uSok+75jxMCP5pD8dPXQTw8IW435i9erneNa3129i4+WDT/DFq3Y/ZCaoTunvj6oUWM 6MFwkjjEM3g5U1Js5sG54Qv2685ClzoBuhB+vhOJdtAXKLc7vhcB4gTg/aJ2OkPo43Xq I8wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=srYbptzQ; 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 lr8-20020a17090b4b8800b00250d2e5df8esi2693761pjb.45.2023.06.02.03.28.20; Fri, 02 Jun 2023 03:28: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=srYbptzQ; 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 S235402AbjFBKX3 (ORCPT + 99 others); Fri, 2 Jun 2023 06:23:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235397AbjFBKWl (ORCPT ); Fri, 2 Jun 2023 06:22:41 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99E53E6B; Fri, 2 Jun 2023 03:22:34 -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 2065064E80; Fri, 2 Jun 2023 10:22:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9E721C433D2; Fri, 2 Jun 2023 10:22:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701353; bh=XH7auhXZovyO0ZjhIGuK6rXf321+XRWDmJGikLNf51o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=srYbptzQBzYV3/OxXwnNT/jdduhoVxDUDCfoZipJ4I3dR5FsBOg+xMN5NP/esZqNe KgaYwWj2BdXEqRO2k7MhTX23ywnGyatZaIhhh2oIQ8CzhqC/rCpsmuLfjTDmfQ8epM rjFH6gimB/KKWYakSB5KMA4UUKL7RcvRtEBeU2qszAfUx5v608d54yF4jnXkneqfMV zvux2ocAAf95TYFHt7mchz8Sx/waq5mFSe8BHpVaZTRMk3mESEy8Zmr+eCsA1Pg+ao whiAk6oQh7twHuuFCdtjE/Ve9ErQbXaZrRHJK87Df67IOl0K+jcsi64mGu56MU298h zCy6IifwYUujA== 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 v4 06/21] x86/decompressor: Store boot_params pointer in callee save register Date: Fri, 2 Jun 2023 12:12:58 +0200 Message-Id: <20230602101313.3557775-7-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-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=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywLJFkUZ2V2dYbxSdMO9/QNCrrs87k70vJr201T0i4 KA2X924o5SFQYyDQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAEykPIOR4VTCcq7gj64idQ5T tkel7vc7PJVHsvVheWn50e0zJpS+k2NkuFW8tWtleNjlzMez98+OXsar+8nuwt/n9rU8/1NtFLk imAE= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-7.3 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?1767586362212356592?= X-GMAIL-MSGID: =?utf-8?q?1767586362212356592?= 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 Fri Jun 2 10:12:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102455 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp926477vqr; Fri, 2 Jun 2023 03:32:32 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5mSGcnEGf3RwbDXWnWwfwXRw4f9ZC8f7o6p6p1OC7StLXpSBjiGVGI+a5MbgzTaNZeE+Hn X-Received: by 2002:a05:6a20:4283:b0:10c:90ac:442d with SMTP id o3-20020a056a20428300b0010c90ac442dmr9651776pzj.13.1685701952390; Fri, 02 Jun 2023 03:32:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685701952; cv=none; d=google.com; s=arc-20160816; b=L8zq2DpBUaRt2NR7hSvqAKDfiYpOjltVa1/60f7SxsI3XjD3SvgkylunjLRhUiaEir JKmClT5yYb7UjOkRoLxXtxFy58KBerTncrYxzvtWCWt95QTtjyNji+qEnMhR1NQFayir sxZlLSwKRJRJ4UqQf7c6VlgtdUEaox5d65zQIXAe+/gdQkuwMv0muhPDIYYHAu5+f0G2 47bCLc/ZF2WrFvnE9rYJM5lBTtGC0gxws590/Gaktw9isG0k64fs1UyLPCH0xik9OvZs iwSirFIvAuVmngeexbpaNuJ8wCXaDUErb4MPHApKzeuLo6oN5xzWjNJkSKW9Fv3+Tc3O 48Tw== 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=dfIyqhOb7xf8N5XiHsGyoHbjNZEjXtjY43Vc5GxaUxuEQamaGxBCYutnqGKVNL34k8 ffz9Wnnjc649iSL5DPTv6mQIL316IkMtSQHn75uiKtMSmLz6jnRKLfebXPHYrSdmRFhM SBLRZCVLfORFG/psETx5mrLqJy8IorZfGdjPURlh3lIEWON0+Rnf1XRa5BRJpPuqYkz+ 6fEkXnmTGbgB700P8Nt8sUq4w6pt3J38YYt6H3FIfYOdmRoid8m7hiAaWA/mQeko04od hNUrWmoCsUvEJTemxptZX3qtZTFG7uGDDCtz1zsIXcek72HIPGFzFf54i9BrcjYcaZjW BqnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="geZ/QPOO"; 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 w4-20020a170902904400b001b18ec6ddfcsi678816plz.294.2023.06.02.03.32.10; Fri, 02 Jun 2023 03:32:32 -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="geZ/QPOO"; 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 S235414AbjFBKXr (ORCPT + 99 others); Fri, 2 Jun 2023 06:23:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235275AbjFBKWt (ORCPT ); Fri, 2 Jun 2023 06:22:49 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F215210C6; Fri, 2 Jun 2023 03:22:38 -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 7FC7D64E4E; Fri, 2 Jun 2023 10:22:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 06598C4339E; Fri, 2 Jun 2023 10:22:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701357; bh=4Bh3UcI6mBb60v5tgLvoAeGffUld7BrhFCXiRmmP3sE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=geZ/QPOOKIpEeGzmFOkHVZ1D8zL1xKTkxEVDKssO4q1c7HqrdjTON3rt+Sy2PqtCf stx9KDftl3r7NLYiVvEpL09K/njN+Qzhbm2tULVn0cXX1TuVTDfKbJ9zPDUwfA8juj ruitBx7gywBEfdCqPDTFyRGMeyYNR46Hfra1dOBGlQKdPaTso0QAcaXZaB92/mPq5K CtdM5lhmpSUQ1tkgeYaGjmIaXhTPPZX28lS2hJWAPXrKAome8t/GVxwW5u4qWCfEPB j2oFsEB08YZWTlbx52KXqF5ZLw70By4GIYoqQN3XpqiTB3/tTdAlMSQXr2+Gnb15ll fILd++h7RhN2A== 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 v4 07/21] x86/decompressor: Call trampoline as a normal function Date: Fri, 2 Jun 2023 12:12:59 +0200 Message-Id: <20230602101313.3557775-8-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-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=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywIrLdwW/ut2TuTv3OdMUt6AgAyV9Lx3bgpTZ36+3b mVxKwnpKGVhEONgkBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABO5YM3wV/yCv0jOD32zyzGW NT//+SmuNHt0vb6u+Iz0LY8dEzL3xzMyLJxyS/unhU/uq1VyDMvSQ10nTAj75LBjX1Ifk/uLmqL NXAA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-7.3 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?1767586610357133081?= X-GMAIL-MSGID: =?utf-8?q?1767586610357133081?= 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 Fri Jun 2 10:13:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102460 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp927570vqr; Fri, 2 Jun 2023 03:34:53 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5XpywfpjZl2UQWVvTKorufYolSZbSZXV3qq2vg5KYUOrHwUpZ0loaY8Bbm93PvPE9XmRKm X-Received: by 2002:a92:ce84:0:b0:33d:5314:d74b with SMTP id r4-20020a92ce84000000b0033d5314d74bmr848139ilo.17.1685702092986; Fri, 02 Jun 2023 03:34:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685702092; cv=none; d=google.com; s=arc-20160816; b=0Go5ZBL2Ou2JKgWxtVA0bWO0D6xKKYLWybQfquGOFP2XUYV2IBO6bTpW72xlycl4lV nhM1aDCJOZ0F3MTG9ZRDcfrxktW3j0ZDB9MW0hZX9UNUxxvyxWuU/+2RjoyYRmRxxwnJ NV62HF384YYMNBez3dTYHoBNZi5klAY1hLyPzmzd96WZHEzBMuovIvOslQgyQfwOrU4R ilDnJDvRfSiMF7/JOU/Kb4SRk6YnrxkN8fkQi/ksEX4JLKA388sd/wA7e8xgdrc4u+ml RoSHMdMJnH2IpaFoSsy5nZvxZXpu8DDdAvNRnNeI1Re3851/AGhGoZsetQb1cgREXgrS tIuA== 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=gRg+cdjChP2jA7waIoxB4rpwVa4Ftjy3yFuF4b4eZakMjsQgRcoM1C7xICFwRYVTZS HxRh65muDADmf8FEZDfqOH6BpTGCuR23ZDOzK89HpsE5WdlSTBGDW9ax1tXZnks38Ch3 Eabk0k1CrpDV889PsPrli7059rYXoRxUpwmYNbzNhkce2PvV/1/FyYzBEyXvyNRIaKA2 QKAz9RF5Bbg7n9naG3F7G/SSnwdiKSv/BG5rlqgKbqWWttwaYH7CnBr0aY5aiiXkY086 K3kHvrSxmiBGDxeeH1ArZ+90Dp7rzlj2x4c+Uj/hG/ZW8C2ccAR8C+eUf0cRhr2L/eXq s/Kg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=OcyfFAQ6; 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 t12-20020aa7946c000000b00652908aedd3si522143pfq.249.2023.06.02.03.34.39; Fri, 02 Jun 2023 03:34:52 -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=OcyfFAQ6; 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 S235433AbjFBKXu (ORCPT + 99 others); Fri, 2 Jun 2023 06:23:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235424AbjFBKWy (ORCPT ); Fri, 2 Jun 2023 06:22:54 -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 52C1B194; Fri, 2 Jun 2023 03:22: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 DB2FA64D72; Fri, 2 Jun 2023 10:22:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 62616C433D2; Fri, 2 Jun 2023 10:22:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701362; bh=6LI2p3tMGpqgjf5Vh98U5/dlrPhBIFsp0XryDg0+x/4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OcyfFAQ6RVV+8Lu4USq6OoJtHmakXPe0UpZ5D5MSPgjEPNyDgGPzD7FDo/+AnqzRv 71onc3n/g25MjzXII0oQMdweJta0Q5vHBnNIcJPnc3udd0D/5f3bu3zsBQIXPWaWa1 mOufZDRojnZ/XXzxjT53CrfmzYFOIczhBNFi4kmXHBTrSo3YpmgRBs9IwWk9qqTLlQ kB8NZDU705pyAiNh9Ps73zgdZ4cdIIThOcZYP8B6nupWPdDgwWMTIKqLiu2CkfhXOg zStzpM/NfVUPbY7u+l5jkbnUgWN3nyB6XaWvMRywkNeK0ZmlT0hSkuFYTWwqsNAOPL IeGuyDXIzRpkg== 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 v4 08/21] x86/decompressor: Use standard calling convention for trampoline Date: Fri, 2 Jun 2023 12:13:00 +0200 Message-Id: <20230602101313.3557775-9-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-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=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywMqVqRclztc4ObWydZ06ZdprG9f6szVTZddMnxI7i eA3fLYdpSwMYhwMsmKKLAKz/77beXqiVK3zLFmYOaxMIEMYuDgFYCI35jD84T2fOUOZ7Umtt5Di 2vUfHLYrVuxKsyk8afpbUN/cKrRzDSPDxd1CX1//vuPOuf6jtUFfn0xvrfdHk4Ib1goLtl3mTWh mAQA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.6 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?1767586757868161115?= X-GMAIL-MSGID: =?utf-8?q?1767586757868161115?= 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 Fri Jun 2 10:13:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102454 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp926472vqr; Fri, 2 Jun 2023 03:32:32 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4NPPXJy9rfK4EAIjdffLSAdMzNBbknAh28TZffZE1wCxaatkxgjaNwEskoARe0FdKnb0Sd X-Received: by 2002:a05:6a20:54a3:b0:10d:951f:58bd with SMTP id i35-20020a056a2054a300b0010d951f58bdmr5026880pzk.19.1685701952051; Fri, 02 Jun 2023 03:32:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685701952; cv=none; d=google.com; s=arc-20160816; b=P7IM5c18+1FqE3pw4rEItFxjxx2wl9OKIFkWWWTfDTc1706zRsndcYtBrtkW1Z3sYE rc1m4Z5n93CHY5XymoKVydenBLQlf+8mf6yxqE61jyWRVQoMd6rM5jnysuMeGiWd8ZzP RiM+3/FWblnFHqJaG9C4a13IUdn4nqIbmjaWSq26F/4FWCQ1Ka3cHQgN4ogirKnL8efS MdVaCCZwew/2vmR2Ovo2NMV70Z2hnpgCzxxPdXOhA/D1mpzNjAv+k4b0LMlo7VZ6lotP JRteWo+K7YCo5EOVnnuHut4+0z7AIdAShDNoCCCtj1jM+tQcsHnSxEx8+OdBNEeG3cAm j9Fg== 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=2nct0eXrdF4JNt6BmazV/eXEUxn2frpqXHOq85y9xRY=; b=ZgnT5YYh7tEiwp1J8YMXACr30+1n6Tqbauaw4qBdgdoz+v5jZy/g1JigupTo0WYTTI O0maA3ESm3X+HaOuBbTyi4sdEG4ykzYTIUWWO6CrmK3ua/h/6ao9ONyJT/xESqSkGhlJ sTMQF+A+cZxI8d80Z6usP2Bl9tKp1kNem/YQlJSDAY1qiUftxDIA507hktGCntZZ/hwu V8qI3KeJeHDuHhZci2d89S7sklMcHu1iX8btD59yXuzpAkWX4XWgAC76RK8lYWtFirt2 TVoe1hmn+YJtG60nxLAMv8NtBu9XuJ+fdx7nSpmzg49FTHH0AGcKaCJvy4pI1xxBkzEE xKOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HMziCQIV; 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 g10-20020a63f40a000000b0054290efc1d8si789658pgi.504.2023.06.02.03.32.15; Fri, 02 Jun 2023 03:32:32 -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=HMziCQIV; 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 S235516AbjFBKXx (ORCPT + 99 others); Fri, 2 Jun 2023 06:23:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235388AbjFBKWz (ORCPT ); Fri, 2 Jun 2023 06:22:55 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C02A71A2; Fri, 2 Jun 2023 03:22:47 -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 479C764E6C; Fri, 2 Jun 2023 10:22:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C0A38C4339E; Fri, 2 Jun 2023 10:22:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701366; bh=opd5H6IVQawxAyaERFSxkNMBcud1PLTZeWa/VKUckUQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HMziCQIVqQGPlxVhREtKOQUKNdUzCNaj8norH3m8d9ok64s2AgSIVPGbFwGC8pTNZ 8fBlaSBOkbDaNKchiVuUcX2LI96YhWaKKvNkP+59H1H+RskBg0f7qqZ7lDrefC7VVF mHhH+6of6LC/Nrcoofc3CB7IFfsztqW1VUnc1iBNsQHguqko7vpVJFVad6h/ghnWc4 bAaNQqvhisciRrf0UBwxtXappXKZJqaHyQLE4D+QYwRlmE2n6084qz0ER8eLxmOYqF rPKZNXpQYn0aiIQdNj0JdD1cahzAACVGXITO+7+1/PFvxxGSN4drTlzYx4vJE1mLZB kLq95up9Nk59g== 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 v4 09/21] x86/decompressor: Avoid the need for a stack in the 32-bit trampoline Date: Fri, 2 Jun 2023 12:13:01 +0200 Message-Id: <20230602101313.3557775-10-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=6184; i=ardb@kernel.org; h=from:subject; bh=opd5H6IVQawxAyaERFSxkNMBcud1PLTZeWa/VKUckUQ=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywKrD16I62NpYrdcd4c0/OFVmC+uFDfX//6yR3fBXM Kcl3reuo5SFQYyDQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAEzEMomRYYHD/GJBbqcbry4d D+vusGev3NSrOXWX8qPTTV8tFp3bpsfwT1tiIa9iwh7tqdET1x2dzqHVVd2+/7bt/QUP4x7NWsP 6gh8A X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-7.3 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?1767586610211362651?= X-GMAIL-MSGID: =?utf-8?q?1767586610211362651?= 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 | 45 ++++++++------------ arch/x86/boot/compressed/pgtable.h | 6 +-- arch/x86/boot/compressed/pgtable_64.c | 12 +++++- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index a387cd80964e1a1e..741b4e8fefc915ea 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,22 @@ 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) +/* keep this right after trampoline_32bit_src() so we can infer its size */ +SYM_DATA(trampoline_ljmp_imm_offset, .word .Ljmp + 1 - trampoline_32bit_src) /* * The trampoline code has a size limit. @@ -636,7 +627,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..09fc18180929fab3 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, so we have to adjust that 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 Fri Jun 2 10:13:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102465 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp928401vqr; Fri, 2 Jun 2023 03:36:36 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4XX6ZEF1hH0KYp0MEw34o1YvvDQFRBs1VWWd8By6JdJQF+17Z79xRMuA21zb5pnqJgaInv X-Received: by 2002:a05:6a20:7484:b0:10a:f5ca:153d with SMTP id p4-20020a056a20748400b0010af5ca153dmr9628821pzd.41.1685702196372; Fri, 02 Jun 2023 03:36:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685702196; cv=none; d=google.com; s=arc-20160816; b=iIcLlhGNHM4sk6R2Jp/KONnCOsZWtskPAxljW0KP6luLL87ou3mvcwq1ucwR1CrwMr jOenAGDb6nOdyPIPdNAhJS3+5sI6IWxyOz35fEPrT6fZBEVAXTQmtCfzg+v4Tudhf6S2 0JfaW4MEICOSzUmirPJsUGH5xR5ZDNXZXn76kZIfxJWjWyw937pAZRyHkIz3kQQHe/7J DDSF3//0ouPSW6XhkDefV2hSWDzPd6BjDju5Wk1uLyDqbh3NsJUDlCq/OYFn0glOibzj a6KhHuO40n90qcVDNImS3mQd+TQxhhf16eZFDu9pJ4rDrAqu6sQi9jGbxfhZ17MG6NE4 E4Ug== 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=oRBXgbs8R67IxSY7rXKZgl8HECs+aqz1XKXNDvM1rAU=; b=cRSqM/4WquwdK6q/NRH1HlAYRAEN1H3FIDl8nPDejf8zv4/ViQHgHihurHSZR9K8Sh q3At4tf58Q60BFxINHu+q79hVeXRd0RCUAEIT5/Avlb8znBoNfux3tQNfaLvFbD3ffWu CFQidCVyVqxq3S4L2x6pn1UzIQhgFOBLoTerUlzRUd1a33Vl1PJENilJeS/nawT1nQoi DFK8UuVu+FxpoetkZMZSJznFYEEKvlwolUdvMJyv2x3/V8Psub/Gn+4Y92lJh9QusGWl eqzb4P1JQVhkzqE3VTn/mfdeBNAhRRL1oxpQQKUX6ivIWUqp5IbEPcycwqWsHA+FZJyb Ybfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Nfff8boM; 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 q2-20020a170902edc200b001ab1fb8cc83si687831plk.161.2023.06.02.03.36.22; Fri, 02 Jun 2023 03:36:36 -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=Nfff8boM; 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 S235492AbjFBKYI (ORCPT + 99 others); Fri, 2 Jun 2023 06:24:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235470AbjFBKXU (ORCPT ); Fri, 2 Jun 2023 06:23:20 -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 55397E62; Fri, 2 Jun 2023 03:22:52 -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 A6F5164E4E; Fri, 2 Jun 2023 10:22:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 29891C433A1; Fri, 2 Jun 2023 10:22:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701371; bh=hFZ/SCviIEc1YZbiLBLlEDyweQ6PGSxYwFHksamIzHs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Nfff8boM+8RvgzsdhvHcWIJlC+g8g4Wq7plDTW+pSwMYIQ+FA/pQdAwR9wmQEH9Hu tozvIv+dHkPk86T9EBsrWvOOuGTPKW7gZ1ZHFUVu2k6Ovt3PHh+gxoR8+qQPsVhlDW I6Y/rXYNtS0G3JWI7pYAWi7AWH91sSo+67RvGTNLsw5fRCo9oURHj2UeXSnUhWALgD JPnuP1Y3H72MpzNEBpqLfzxVt5VauFyizJ1+959HL6IdT4uP5VmAQ8c1oz24VJVXBI nCzjaghvF4ZqJNjwJRi87HnpiVPxe0xMZ/kWrnWUBOv80HiGGxF9Se1bYlP7zMhhrY WEN/khJAwidrA== 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 v4 10/21] x86/decompressor: Call trampoline directly from C code Date: Fri, 2 Jun 2023 12:13:02 +0200 Message-Id: <20230602101313.3557775-11-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4966; i=ardb@kernel.org; h=from:subject; bh=hFZ/SCviIEc1YZbiLBLlEDyweQ6PGSxYwFHksamIzHs=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywJrzy7b+fukf7pKxUXarwbGeLatCFi5a9qu7hWG6Y BHjqmPzO0pZGMQ4GGTFFFkEZv99t/P0RKla51myMHNYmUCGMHBxCsBE2NYwMvQlzjl3wOv7IbPb G1zn37z3xyPONlq48lbd/YbFa+5dmvWK4X+69v0z8yrf/xZ/78K/z/ENe0idEl+Imabpz9+VvoJ OAewA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.6 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?1767586866446675355?= X-GMAIL-MSGID: =?utf-8?q?1767586866446675355?= 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 741b4e8fefc915ea..a60ec9283bd760e3 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 09fc18180929fab3..b62b6819dcdd01be 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, so we have to adjust that 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 Fri Jun 2 10:13:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102448 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp926100vqr; Fri, 2 Jun 2023 03:31:48 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5Kd4+Kt5yiZrLyLLIWkCAqiPBV2uY5qWiaur7+dv9VeG82u3YWMx00P7rZO6Q06rk17VXV X-Received: by 2002:a05:6870:8444:b0:19f:6711:8e0a with SMTP id n4-20020a056870844400b0019f67118e0amr1498080oak.32.1685701908140; Fri, 02 Jun 2023 03:31:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685701908; cv=none; d=google.com; s=arc-20160816; b=lh/hc+worRWLKzxxEgHvvvud4NVA5KmhFCmz8Wt3iDQuNMTEZ4UVhLEHS5dG5lIHt6 bUFhSxTIBT2an4LaUT+hWnT87EBWdr8AjAwBBKJAjCJPWenlKu9GW1LlVdo+FD5/2SWl f286gsr5WvZ0bRpQlxgfa0VBujYMfw2NbSkQOnt2PyeWH0YSDRSYMknP4xUGALHrZdbr wal2Gb15p45fg3KztPL5avK00dSfQW8OSv4Su9jl4mBkvVAkNBlakRaPmBk3PHUIP6jb 0JE8lm2qLygEGXK1I6c5xe9lOFaLwv279O2uj/nKCv1MxQaTq2Z6Dh7TtMF9FueYaFoK NOrw== 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=x6HDeOfTmKCWyOfu+z54dADCNk6vl1IOzYSyYB4KuDs=; b=ndooyfkZkkpag7KxJSFk2317rs7zxYTV9brv342K45dpyu1DoWzkr2DKNjHzcHh1P/ FUmky4t07CY97LF0rd8BnYYZUwbLhc4t9ImEytR4FuuII05EtG7cA/5AOhaUJk7HQaLn xmNzmYI+gtE3SfGqCgUHY0PM4bQ6b+noiZ0D2CRr4FEJmAg8EWMUe51ZXvOvxOtMzqae WQs4ls8OwOX0HhXCA6V3Rn0ySmiwbSlyiNsA3uzL0EkWoKpefJcDuPJ5UT/+mkrGro9q z4QazxQdmcoG4D9F2QewA9LsvjsTHdyL/f/TyZA4F9KzYs2j3BhY44mwsuqFKiZxxM57 6olQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=SEw5WzIv; 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 h191-20020a6383c8000000b0053488ce246dsi797514pge.463.2023.06.02.03.31.33; Fri, 02 Jun 2023 03:31: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=SEw5WzIv; 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 S234984AbjFBKYN (ORCPT + 99 others); Fri, 2 Jun 2023 06:24:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235404AbjFBKXd (ORCPT ); Fri, 2 Jun 2023 06:23:33 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F94AE49; Fri, 2 Jun 2023 03:22:56 -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 0C21461626; Fri, 2 Jun 2023 10:22:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 888DCC4339C; Fri, 2 Jun 2023 10:22:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701375; bh=u+mBV9LASMmynY9CQFGbcEYb3DED+j3/4fMymyiLQXA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SEw5WzIvMZEi1C6SDbCrEwfXPL4dMy5g6DuXLSNjQTOMLr0LYqwl/gLdfVE9oiTHy 7ltF6fpA+ViZ/h0E8bHq15dKnJ18g3g/rHoUg7S1SbJnvickMwjo817z920zium54A kD/JWQlvWMUaF7WZgle0hq4hOiP2sG4IIfykhRW0pjDZfte5cvwyxDgVX6pGDlnGmy J4fxmv1zG7qKIjIHsaOnj52gk6ooneg0CKvDs3lfMhz3bqRor8LkSPejWrs1Sgl/7h d5o9rJ3Ccogv/VuqybG+UHOzdwgp9mWJ71aBhNRHdTBnZ1OWbajAr3NDBYbXgOY6fe FBGb0E60gLGEA== 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 v4 11/21] x86/decompressor: Only call the trampoline when changing paging levels Date: Fri, 2 Jun 2023 12:13:03 +0200 Message-Id: <20230602101313.3557775-12-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3739; i=ardb@kernel.org; h=from:subject; bh=u+mBV9LASMmynY9CQFGbcEYb3DED+j3/4fMymyiLQXA=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywFoL2/M9ifYK4fo86pwGr3K0tW+xTTjFrfHV0uPNx kvdj107SlkYxDgYZMUUWQRm/3238/REqVrnWbIwc1iZQIYwcHEKwEQijjD8d9htIMtz8Mu1JUxd 29Qqlx6LNWJvMz0mffD/nMA53B95ZzIytFsYxCXG37x4Zrqd8waB/1r+f1q5VC/cDpGv2fbaW2A 1JwA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-7.3 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?1767586564147357060?= X-GMAIL-MSGID: =?utf-8?q?1767586564147357060?= 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 a60ec9283bd760e3..403c96dae34d9c6d 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 b62b6819dcdd01be..b92cf1d6e156d5f6 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; } + /* + * We are not going to use the trampoline if we + * are already in the desired paging mode. + */ + 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 Fri Jun 2 10:13:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102470 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp929045vqr; Fri, 2 Jun 2023 03:38:00 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6CaVuiNYrb9cUKMDkZVfiZXR8z+LnpSOKYUj4glDItwSYcCOisydlLzxBjB/JnDWMxhuS8 X-Received: by 2002:a17:903:182:b0:1ab:1b8:8a5f with SMTP id z2-20020a170903018200b001ab01b88a5fmr2518001plg.33.1685702280137; Fri, 02 Jun 2023 03:38:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685702280; cv=none; d=google.com; s=arc-20160816; b=UzAAphZjKGIcTRUmem2vB4Bu24kCDSJ9b2n1AmbTlsLpwF9dUFvcFKTta03HpOmuwf Y0J2HQoRcx05xTxOV0UsheDbo6cfJfYsbcbp1gutnZk+G/bfzPtPZmhrF0rAKA+QPpiB rKao/Xy9zRJ2V/Esly+69C6BmPk44FRSsnflPIvvVKCxD5T3OUWxs5oHnTmCZv3TM/TU TZ4SVZTb48RZSb5LBzpzWPL1K9ow4UZLqvAcLvNilFYRRNAoOgQwl0bfTReAS2ZtLin6 fHdK4j77uTdD8RM//d/XFj/XMxs1m1sYWjQh04Pbz5fGprkhSGNrrOoHylp2bQNHNWNJ K9Og== 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=2GvniZfDsJu6SuNQirP/r0oCYG6eEQ0It0hxOuT6TVk=; b=XrP++NVL7DM0QzxYo0leoOMBOK4d9sEeDWQUZF52PYXzZgjO246k8T4GpwA4SQdcpl NJSkPry4Yf1Tiw8pqNsrtN553EGFUYNJwHdR8+7Syf8i4V3k1efLEIpKsNsQvJEP5yJT pjPsUF9176hAMhcPphrSecYMGFBtwZdxPIxueC061f9tPMWyn21Pu9MwCimKdMok3AIc eNPAIek7vGB+GAGsAvjST6GZerGb+/rRlTHALGPDFvLdiczFml1UIGGT0noztNHMhDZ3 tHhduoi35qhgDuxy2ioXuQ5kE57CLDAQKl2fnQ4r5/UX28AooSxpb5+zq4bAgscr5iar CInw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iUQjx7k1; 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 iy9-20020a170903130900b001a19bd8aa63si730790plb.36.2023.06.02.03.37.45; Fri, 02 Jun 2023 03:38:00 -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=iUQjx7k1; 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 S235545AbjFBKYT (ORCPT + 99 others); Fri, 2 Jun 2023 06:24:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235493AbjFBKXo (ORCPT ); Fri, 2 Jun 2023 06:23:44 -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 EC9BF10D2; Fri, 2 Jun 2023 03:23:00 -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 6BF0E64D72; Fri, 2 Jun 2023 10:23:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E63D4C433EF; Fri, 2 Jun 2023 10:22:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701379; bh=CnFv45/Vh35jv2ecaVpF4w7431pbCKxy7DMiPDdcENI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iUQjx7k13wrKSTU1kAQSwHaPTJ3vJ4TbY6F0wPnBR9oKHGQa9a4vablgt2la4spVl Gn8hOZQstppOvz+pGJRjqID6yolYtv/9GGOl6lv04CvOI+/0hZKi924ZLPhcHnjGaz HgvP1reOu2hl29sNFXl3oVz9alo/BgaLzJc3ksXstftmI2gDkX5p9B2RlYEa3YAP9Z 9wjXXWnSA7nFhFo1MHAHfjl/HaYRh+vX7T0nvMTJTNr2DWwZam/hx0QlwF6ysc27bq RyzHGM4YO4ybNwGOtUP/g4hE39gro4Z2SDqGvHxeJlYy6yJ6if5icImBB5cw/zcTSv yVQ5i6HQP8N7A== 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 v4 12/21] x86/decompressor: Merge trampoline cleanup with switching code Date: Fri, 2 Jun 2023 12:13:04 +0200 Message-Id: <20230602101313.3557775-13-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4166; i=ardb@kernel.org; h=from:subject; bh=CnFv45/Vh35jv2ecaVpF4w7431pbCKxy7DMiPDdcENI=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywLpmfZ+JtbF2N7ZGfVrba5ei/557++WT3HeNa+4/O FKycu3djlIWBjEOBlkxRRaB2X/f7Tw9UarWeZYszBxWJpAhDFycAjCRuuMM/xP16rc73b79aO/O gsmcTYs35zOa78u4cPeO7qKvk/Xqrs1k+M2WO7M5R/7Qs9Q5O/XOlfxXV1NP6Ei/vnzB5n41mbs f3VgA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.6 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?1767586953787822053?= X-GMAIL-MSGID: =?utf-8?q?1767586953787822053?= 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 403c96dae34d9c6d..b5bd6be035a7b7ec 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 b92cf1d6e156d5f6..eeddad8c8335655e 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) * are already in the desired paging mode. */ 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 Fri Jun 2 10:13:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102451 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp926316vqr; Fri, 2 Jun 2023 03:32:12 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7SDwJC3wvcNaBpcw9SqldK+6ANj5/X8p085De0w8mfwV3S7dZbdT5d2I1ejqdqE2qLgr3q X-Received: by 2002:aca:1e03:0:b0:398:8ad8:2b6 with SMTP id m3-20020aca1e03000000b003988ad802b6mr1930735oic.31.1685701932699; Fri, 02 Jun 2023 03:32:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685701932; cv=none; d=google.com; s=arc-20160816; b=yVPX3l83mY2FT8unjDx7/WY36DXyxwZhP8Wn5FFYolQ1lCwHESpqd6T2Js7BmwqYYv q3Mw9g45hhyzsc4Iy5FPsXl3c1ifYr8+RLiZlQagwyYCNO/TDv/lMXrpEgrsNgfrkl8h 8sMfz6loqkPLENOjFU4mlEwscdYnf7WVSNiiuQwRVxvPAb+KUSm32+ZrRxLjVIK8QnaS THH+LN0WwtrHQ6iMvpW7ZJW8hH5tTHCyGvfmM0hA87iTA0UR7PxAIn9V6MlyLjXvWOtc wj5T0B939ByJ2Hid9KKR/R9pFfZt+uxXocKtfTQe7Q5xZdQaZ83MmlWSBRg19yF3/TJz uOfQ== 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=wRhFLJMesW3l4P2dxSMaLJ+dnvFBn+OMkiVt/dl9v2E=; b=Sg+Q/BrsBgvHSfMUlNDpkzCspxM8iXQ6w+PLwzZQn6Pba89+xquF2Hph8gNQcIICQb N4jFKSC16Oy20G3yyC40Ap+Utw7oTAq//vq0ts4B6icbbiClREusvjI7qyi8BTn0aHMB pIAUWMMFrSO+U73eyCANpppsd3YPCx1fvVr2oYR8/hUgvBfQ6COpqvGpIdppYUTop67y BbQo/GpLv05OZOjQMU3bmm0jYCVkeBWkTf8x2HYxax3WPrKle3Y8zyVhBS+3aBYn/Nmu ZUrwKZ3B1yXiehdalTD4l9kcqqC2zRpPHaTaxz98JN6+eaU0WLzKBTlGR8KN262RxJdh ReTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FQVut3s5; 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 l17-20020a170903121100b001aafe3f409asi735259plh.205.2023.06.02.03.31.58; Fri, 02 Jun 2023 03:32:12 -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=FQVut3s5; 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 S235553AbjFBKYZ (ORCPT + 99 others); Fri, 2 Jun 2023 06:24:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235170AbjFBKXp (ORCPT ); Fri, 2 Jun 2023 06:23: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 4530E10D7; Fri, 2 Jun 2023 03:23:05 -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 CF40764E6C; Fri, 2 Jun 2023 10:23:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 512DBC433A0; Fri, 2 Jun 2023 10:23:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701384; bh=qSgmzewWVKnnUee0WJqteVdeET11EUDulO4v6yJCdIc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FQVut3s5U9T+3LYhk66bKvpmn6FqcxlkNLEn/8Nh6YzIjqtDebcucSUh++WaKzoDe /dcyEM3ksvmUCUDWdDC9hAgHhfA66pPh17miBS7H9aoNjAobsa6nOfhUCtlAcTLjsA vjndQRELfccd7QcI3emzayAoF8aZY7fuvV73omVWXzV0aXCZJtbEYy3kucH8ClkMCu +ftHmqNUhAkZtGH159xqpt7mGANfGkOqeuEqTAMs3Orkk54A18DyCSHQKi+y+vu8R4 NgdcUgUWAoBle4h+HcqEKH7cIWQlcnjSvkcHTdhGgwp6NT/bVN1Jg9w9oZQzKodWVC Mdv93Ri5t8FEQ== 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 v4 13/21] x86/efistub: Perform 4/5 level paging switch from the stub Date: Fri, 2 Jun 2023 12:13:05 +0200 Message-Id: <20230602101313.3557775-14-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=10070; i=ardb@kernel.org; h=from:subject; bh=qSgmzewWVKnnUee0WJqteVdeET11EUDulO4v6yJCdIc=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywPr78zd3BNqtP3xL96b0OtaeuL++Nt1HWk7enfiPp V0qbBd/RykLgxgHg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZhI0F+G/4EZS9Lfn681lQnb tvCu2Z0bwnvfRf/gLZ/mnay58dLpk1GMDH+4ZdjqEroFVmebT8g5bNW271F38ubAJjdjbZlJy5f MYgUA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.6 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?1767586589669504538?= X-GMAIL-MSGID: =?utf-8?q?1767586589669504538?= 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 | 94 ++++++++++++++++++++ drivers/firmware/efi/libstub/x86-stub.c | 40 +++------ drivers/firmware/efi/libstub/x86-stub.h | 17 ++++ 6 files changed, 129 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 8659a01664b85d95..191698e8489d82e7 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..f7284f6270abcc18 --- /dev/null +++ b/drivers/firmware/efi/libstub/x86-5lvl.c @@ -0,0 +1,94 @@ +// 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 we cannot + * program a 64-bit value into CR3 when running in 32-bit mode. + */ +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 having to allocate a 32-bit addressable stack, we use a + * ljmp to switch back to long mode. However, this takes an absolute + * address, so we have to poke it in 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) { + /* + * We are going to enable 5 level paging, so we need to + * allocate a root level page from the 32-bit addressable + * physical region, and plug the existing hierarchy into it. + */ + 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 level 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 d010448dffb12cb8..c55c028cf911bce0 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) @@ -212,8 +213,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; @@ -267,35 +268,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 @@ -305,8 +285,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 } @@ -804,6 +784,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. @@ -922,6 +908,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 Fri Jun 2 10:13:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102445 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp924774vqr; Fri, 2 Jun 2023 03:29:08 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4gsi5y0AFH9F+5Wagp6BYsyEHr01pyggj8cdRqIJVL73toU6ueZDQtYnpF7Dm2PLiBqfVH X-Received: by 2002:a25:ab28:0:b0:ba8:29f6:5899 with SMTP id u37-20020a25ab28000000b00ba829f65899mr3074547ybi.16.1685701748650; Fri, 02 Jun 2023 03:29:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685701748; cv=none; d=google.com; s=arc-20160816; b=ZiJnLs09wwjR+kAMN66OVd3hPPSH6yPfn+uMyUKDY4FJ7+n74Nzv86de3dxh10LsQ0 pL40Vh0WrmE87V/cYF5023HV35Mbb6NySju7ZFpakH8wfwVjGekW2mAJBONdgYXf0C1B cpykt2G1wNxcumpXV++ndtAYtqkEIZGEFANVQ87C9w0xol0uzLd2nh5bzDMarc0WHyoB IJgUVjZtAvEoheDdDOG5tivd4QsLLKoeHTUi3VNvRpuYdSk146e9TzAr25XQIsrAmgGy xUvMaQLsGci9POpnc6MMBLNVW+hFo5Yel9UVRMVYgfGE8ScEcBPo6B34kuGtAYsBcTz8 05HA== 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=fS8HBZQZyn7l0eLJNE+h5YRvuXpBYFpZHU223rS3V8Q=; b=C+ysGvavGh4Hf5VqThYT4rSeIA7RvuC3FybMuebisR1hjrNLQk8Q6BW3akNiMc1cdu CSoXSANwDmbezxQr/7J7SIblj9VjvZ6i4aubrjtV2SglVgXcJc/PtSMEYwVHY2oFs1nN VpdpYfX169vTNMRFi26TDwzSs0PMWNIvxTqAwvApo/OywTPShUhRTIoN1pnC0LXo5igh iV4IWdiFwbNJVQU56kHoDWKmjFCtOkxDOiPZbOimV/MDAt0RCOUFqOnbNQhQzyBMfEBu vDPPXU96GRNzVn4C5bZufLDMShzDNenPaYFlF7Lh5VfNcoi4sXUrhGrGRoaJXG3ZydEm yMQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=W02pzc04; 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 v11-20020a170902b7cb00b0019e2bd0fea8si708894plz.143.2023.06.02.03.28.54; Fri, 02 Jun 2023 03:29: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=W02pzc04; 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 S235380AbjFBKY3 (ORCPT + 99 others); Fri, 2 Jun 2023 06:24:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235353AbjFBKXq (ORCPT ); Fri, 2 Jun 2023 06:23:46 -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 6BFF710E0; Fri, 2 Jun 2023 03:23: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 3AB7564D72; Fri, 2 Jun 2023 10:23:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AF6D6C4339C; Fri, 2 Jun 2023 10:23:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701388; bh=ILOYseVDB35z5GQ5Qwen/Xris3QVBcVN/ymf61ysRYw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=W02pzc04ErlYysGiY4iN2FSYtKQ1szh/aHdjkX82grtpZn+Jg6s+ctlE72YdTMaeX 2yi/5H4XPP+qlAZe2v0I94rT5sCOXQpz65tAXOkA5AWBT20lCOVviL2g/HGj3FDhPl MqKzO9ldIkIRUtBtvx7U8WxWiHfFCP7Sh02wpaAJ84J6DMa0LpFW+fLUkGZInh5M5I pt5J/2YKHN7n08ZQe2X3zeapqlHDITDXsWWc5RYt1UJvYd0lklToxtAUpxdGKW6Ew1 FcUAjrak44D1VAl/KJgoccKRnYeZTJvByOv2gPbifU/ZwrDOCS2cTofOxeuNC2izfq 6RLK8qMx237Gg== 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 v4 14/21] x86/efistub: Prefer EFI memory attributes protocol over DXE services Date: Fri, 2 Jun 2023 12:13:06 +0200 Message-Id: <20230602101313.3557775-15-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3307; i=ardb@kernel.org; h=from:subject; bh=ILOYseVDB35z5GQ5Qwen/Xris3QVBcVN/ymf61ysRYw=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywIaABb6yz/Z+ZPMSMXkxSV5dfOv+sJwzdbtvLp3Ef m6eOHNDRykLgxgHg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZiI4wpGhpkWfmmvej89nlIp s7J9caeN7I2V5eFtD++6tXsfevOdo5WR4eSOc5ctPJ1aKqRaOrLNNzRu2eX9zCFivdSWK3Lzjr9 3ZAIA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.6 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?1767586396383266515?= X-GMAIL-MSGID: =?utf-8?q?1767586396383266515?= 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 c55c028cf911bce0..2d3282d2ed6eb756 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; static efi_status_t preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom) @@ -222,12 +223,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 @@ -766,6 +773,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; @@ -777,13 +785,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 Fri Jun 2 10:13:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102457 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp926557vqr; Fri, 2 Jun 2023 03:32:42 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5+nayJQEw2S8oRAgV87sh9WtdYDkbn4wjfLQZi4osiyC0QVPSS66S2o36O6H+ZiM4/T51Q X-Received: by 2002:a05:6a00:1a88:b0:643:8496:e41c with SMTP id e8-20020a056a001a8800b006438496e41cmr14423103pfv.20.1685701961830; Fri, 02 Jun 2023 03:32:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685701961; cv=none; d=google.com; s=arc-20160816; b=VLXTDTx1fb2hLkqbgqOxADRmwS6U0sfUptZ0ZD9tyj1trYVpdXI1TZpB7dIIATPAgB JKDhPS1c/p53k9q3DyJv3zqZOsW53dHPYDCM7LB5AfPO/PyM8+G6LEGdbSGybwfJ8PgU /8q2xIKZd35U9ZYcwtBFlKeEPUSqpR085XJzv5wRvp9W4wpwqFXMJ9mQ/pwt9WeNLvYx n15uBJVc+3rPjntISBnfQ7worc3yRcAnsb/mk9RtThfe0EVrtg0z2NMDDmK8cb9AX8E8 TVUIjMuczKBnR2/EO/8Oh6Cip2HpaHEKjf/tahd6s1x7p7U77BRP2FjSiG4o54JrigAT SpFA== 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=dskHVcfZbpqp4nZyo5aXoYY4fe/csqAb1QJLFK8TEgyg8jpgdVcUO6qvmSn5/iHq+V G+2k56qe/6sVAJcSH1PtXXhiwHvlXmtCcjfXx6Xiqt8OyCi14yL7Xj1iBklBCAvHXQTQ VfWcgxzgUmc/Esswuxs6XwFGlKNHEuyN+P0KvlFwjYIIHRs/0LFyYcshQRMZ+VkabhYs UiqdLO8kjgHFrVggCH3o7K/Keubvn1SjtgEddS7Yl3zDbjNSvuzzhqvjRdEOcYL/mD+R L0h8jJguEnVP6F3zGrfsCgz5Enwbn3DzPGLTfeHByeyJLGxsM3a6GDXprVFhHq/8ge/v AShA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JDWx2x8N; 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 76-20020a63004f000000b0053ef51d3fa8si788130pga.401.2023.06.02.03.32.27; Fri, 02 Jun 2023 03:32:41 -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=JDWx2x8N; 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 S235564AbjFBKYd (ORCPT + 99 others); Fri, 2 Jun 2023 06:24:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235182AbjFBKXq (ORCPT ); Fri, 2 Jun 2023 06:23:46 -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 1F11E10E4; Fri, 2 Jun 2023 03:23: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 A89A664E63; Fri, 2 Jun 2023 10:23:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1853FC4339E; Fri, 2 Jun 2023 10:23:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701393; bh=TDhagb7/1BjVNI9fsBOdvguiTH1+pJKnlE7Km5AKGt0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JDWx2x8NS3EXWONbS4ao2cLywwjBkOxSVub0aB4TdyV8RL9twr4aztr/IY/zCAljA K2quBW9clDp8TUhlmV9LD7TzM4L//8fZFYHZxvLElvz+tgnqe5I6lJvMhD7jblttSS 1N1vZZRsCxgUl/E46/JyddBynvcCdonJFVvlc+uuAJM52e6vBet/2QZaRFHfx+Cut9 kDEFCXx3l8l31o6QLZXmYpe6EdwwX2LiIBnkKJ+Ez4/OIGufEmZEr10tw3rXyWY14F UCzGLja/1kx6eg7oMFgPqx5tOR1ZsTRI6M8pmsxCLN/hL0tsJdjZm0+enwFpL9tMHp MLatLcXPzWj8Q== 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 v4 15/21] decompress: Use 8 byte alignment Date: Fri, 2 Jun 2023 12:13:07 +0200 Message-Id: <20230602101313.3557775-16-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-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=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywCbH4HPMVjN2iPOzhUuqy3Wvr7hRdEN69SXmiLqDH FP5A+50lLIwiHEwyIopsgjM/vtu5+mJUrXOs2Rh5rAygQxh4OIUgIlE32b4pxNVkM2cIBzz1En+ xx4jBomCA/ICNzRFHvntseObOCV2CyPD4fyFE/jqUv6/lGutyexOcKy+msrx/7ZPzWwdR2abMxq sAA== X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.6 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?1767586620589005410?= X-GMAIL-MSGID: =?utf-8?q?1767586620589005410?= 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 Fri Jun 2 10:13:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102466 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp928405vqr; Fri, 2 Jun 2023 03:36:36 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6JSRyObcQScFlfkrhL2q56Al8kfJbvCLKKQ5CcfH4H8hPm9fcDLgqBDoWMss2JamT1/5AM X-Received: by 2002:a05:6a00:23d6:b0:63d:3a18:4a08 with SMTP id g22-20020a056a0023d600b0063d3a184a08mr10101218pfc.24.1685702196546; Fri, 02 Jun 2023 03:36:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685702196; cv=none; d=google.com; s=arc-20160816; b=l3EvylYUBx9t8SkGxnAbQ6x4hHGF7vtGnNLuWBOtyG+1AIxfhDbfJ5vpWPH+l1SzaE 4dVIzVfCODbHlyIF1J6nH9uWhV6h48YSjtSCzYDCytmLak3Vz37dGtWwAOW6239+jfU0 nAY30KiVf+dmARw8aGNdIjVsKcFRUuqYsfxVQaIn7aYZrqM3ZViiaPKnsXdmCSNweGk8 T0FF7kJPOw3l+X73UH8qGse8jv/knuxW/Kv1EP3KvMUBAjdfLkOeaW9E21IYUaymsv/9 lNWoA+Er7jn4UnBhpOtMwm+qroCGwZvN0PQWudvFvHPeACJ0Cki2u2GyFY7icScF0KM5 W8KA== 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=nEzGCrqtPMvKAlAsmeFYNAjbdU1NAif1Mw7xsg8ylaY=; b=bfHgK2ik4utNqrsE8ezyJv1K0AiPNMHxWqPMa+ifM0pkfQ13jE6WHDGy7TxfS1aXEi OcWP0nQtpvzJpnjpUzB1GD3xUTbrsTHdt3Vkwlz3Kp1TdsdyvjUB1NjN6IAAdnRYxXNw pwS5trjn0ZxBmlkfNtauYKCTbcOy59EPrGNRU4TeXp+jjSfU+8K8RqqBA16UBI9g0T9H 5MHwiC0S1BuK/OE0ZANmA9U/VMl/GIfFH+Fq30CZjiOIxNWvC0O4gafcr7xsZnMbLf8K GKXVNc0HOCXdtcQ7+6TZLakP4LCoap9TJ6iLxskA3y41ch+bHYmtsWk5eWESp5Z5WZD9 P/NQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=NBKBOfWv; 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 t12-20020aa7946c000000b00652908aedd3si522143pfq.249.2023.06.02.03.36.20; Fri, 02 Jun 2023 03:36:36 -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=NBKBOfWv; 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 S235508AbjFBKYq (ORCPT + 99 others); Fri, 2 Jun 2023 06:24:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235420AbjFBKXs (ORCPT ); Fri, 2 Jun 2023 06:23:48 -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 8F32010EB; Fri, 2 Jun 2023 03:23: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 01D2364E63; Fri, 2 Jun 2023 10:23:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76E3FC433D2; Fri, 2 Jun 2023 10:23:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701397; bh=g3nSTumb/KYsp4GqRL0zJ1a7x2cI2HwxP02u+R5/mbI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NBKBOfWv0jG5QaD+uhn1eNfnePlAIv9FspYCYiwSE/yvaw5zGyKDvWW8yYW2na42/ bTVAMTliii6hsUXd6P4tiPR69wJqTkvBYhvAzkJpRf2j2ARIxRg86WzubzBviwg8ws 2JBxVKNT/IfMkDqvbdb04mJVKiem5nKawkp8TXkl+uTnN4++DJbWG1uZqEHm4O5wpR gOKplx6OOltL7RPaTRZmXoO5Z6IU9VI1rwn7cEQrRzuMNR/FUuIOIodG/Aq53nrErF VqrdpHKz0Eh8lOolqeK7CSND6MOvNmhmsMBGGc5DmvSS6RtQEMEbwNY16RKuswjeze ZkBUiKkNQdckw== 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 v4 16/21] x86/decompressor: Move global symbol references to C code Date: Fri, 2 Jun 2023 12:13:08 +0200 Message-Id: <20230602101313.3557775-17-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-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=g3nSTumb/KYsp4GqRL0zJ1a7x2cI2HwxP02u+R5/mbI=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywOaiaX/FNbXq3vm+2fMiODti3b04m93//ps0VaS6T jil1snTUcrCIMbBICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACYiGcXI8EyOrTFdqrL5h9PG r9lzXaqOerSuXXDv+rETL4v933KIf2b4HyGVMkdz2YpnYne3/T4vGio+S+hyevjxquY3rRMyElT qOAA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.6 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?1767586866429542635?= X-GMAIL-MSGID: =?utf-8?q?1767586866429542635?= 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 b5bd6be035a7b7ec..3074d278c7e665d8 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 */ /* @@ -636,8 +632,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 36535a3753f5d5fa..ad7a2297c9e186df 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 Fri Jun 2 10:13:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102446 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp924888vqr; Fri, 2 Jun 2023 03:29:24 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6L2CjAkZAX4IGRaefIcFw3UfDJAxZkHBqBUW2wnxPuTdDgM1QTsdFnWKbzdT0SSPjwcPzp X-Received: by 2002:a05:6358:2812:b0:123:5664:e493 with SMTP id k18-20020a056358281200b001235664e493mr10197116rwb.27.1685701764222; Fri, 02 Jun 2023 03:29:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685701764; cv=none; d=google.com; s=arc-20160816; b=JaPzOmoCjmHhUuZl28te9xaPgmbFtytV9T5g9IPLgEYGpyP9rQzbzOuIzoCb558zVo O7gendTN6YO+m4WbHT87KRsd2+utT+VDmJhUNIHk4eCfi76AV/jwbOuEfT7xLLP84IKe iouV4hdtxYjMIRJf7nMvu8+afuyxrOgztxIBwXFQw5Sg+IstoPNSNHqTSYBHAdogcfZp x+yObrgAjnlwVdIa1HXc3jqsxSsYjDtzNs8nlOEuUWb8kivI5v4xvKrXH76iE+Aqncoj VAv4I4tpEdzTzKHSK5PMbABaLQ/SFbQu0s4fWMhsZHRnRHFNNtHgcnY2/qAn9AHJfvSy qUwA== 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=Mn7RXE41n21od5stVTbucM8IPehZAKJb7S+xRWs313U=; b=a48dGBRwlg+MKJhVDGCdBCkqGOp5hnjACbqWWW++4wb7m+kAeZoSW6uyU8zI35GXBO AqAOLliQpXWhNEJXyFUgZXYJxDDI1lK1IbknwX3zxQbo78T9znjE0XzaI7tS9o1MvKp8 XGl5zIRarZMGGUjoBb5TNG3N6GWxYLYkNLs4lKSL3Kac3pomMjNvni7yCKoLMrec/0C7 kejsf1OwtoGYq9MKtBhnDDxHURuBbkOp6j/sSc7LlG8zAB0ao8fLMxpuONEY9fBTkqAs CeCixEXumYItsDN9QT5dScpkv0ZAmSifxoh7gsK8b5dheON6ydLOYFAmDNtN6ym4uIjc NWpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HJXId3a8; 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 my14-20020a17090b4c8e00b0025082583ae6si857169pjb.187.2023.06.02.03.29.09; Fri, 02 Jun 2023 03:29:24 -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=HJXId3a8; 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 S235451AbjFBKYu (ORCPT + 99 others); Fri, 2 Jun 2023 06:24:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235389AbjFBKXu (ORCPT ); Fri, 2 Jun 2023 06:23:50 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE4FA10F1; Fri, 2 Jun 2023 03:23: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 5B5A664E63; Fri, 2 Jun 2023 10:23:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D3C10C4339C; Fri, 2 Jun 2023 10:23:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701401; bh=U1WWTB8Ed6jF5PagzQzhXVR5oyHC5bjwVmmRHfA2IP0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HJXId3a8i18aoMDC6f7F7nH7CSVy8FgoZIaU6lLBaNsLeOpllBOWqUOmwRwz0Ok+q vg+ynEoLQlKVXG5DTE4vJYpBgYoVBbr+ZG8NyT+9NOV7FwJFbdqKVBWF+XEoTYADwJ HIq4C3KDpQV+jSOv61iGLifJ+FvXTCs2KPY8vNyus4h0ZHXkX3dvdW9J7crnUScUw6 QyKYB507rOC65FsX4ZIWPpGUbFRGQSc/VkdOhTd/91IDGrflqp5oJZDxANpJigC4jB PKwGjNSfKZBU3qw7XsEunHVzQ5PPgX44aBoaqFAhFyZpAPsBcCigyU0mFXQNcUbBxe +bl5DpMPH/A3w== 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 v4 17/21] x86/decompressor: Factor out kernel decompression and relocation Date: Fri, 2 Jun 2023 12:13:09 +0200 Message-Id: <20230602101313.3557775-18-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-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=U1WWTB8Ed6jF5PagzQzhXVR5oyHC5bjwVmmRHfA2IP0=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywBama5deh/9pYD/+7o20UPilU0cYGPcu8vpRorXs7 ryU+4VfO0pZGMQ4GGTFFFkEZv99t/P0RKla51myMHNYmUCGMHBxCsBEVH8yMuz8u3q9ROlEpuip d573LL8bY3g3rqxZZPlM9U5VMfZrskmMDG8Cci5/Z7aIk2c/mS94a/8Gk/623aE7LVQ3ffhxXWK RNi8A X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-7.3 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?1767586413490898434?= X-GMAIL-MSGID: =?utf-8?q?1767586413490898434?= 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 ad7a2297c9e186df..831485a66300319e 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; @@ -464,10 +485,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 Fri Jun 2 10:13:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102471 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp929052vqr; Fri, 2 Jun 2023 03:38:01 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5kBbrT7Q/pc0jCV208mjwZoYLU/0VwCsFAdmXLrtB/WHrCTcGOiDEfiOxn/27QM6AjZMNt X-Received: by 2002:a54:4190:0:b0:39a:748f:4bbd with SMTP id 16-20020a544190000000b0039a748f4bbdmr2188222oiy.15.1685702280944; Fri, 02 Jun 2023 03:38:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685702280; cv=none; d=google.com; s=arc-20160816; b=ATzZ2DrcP+gwjGrqeypH/jAvmdIFkhHCcwFenG4ob65hR1//Sdy9J4x35wLMMZ1EYz /BPJbxDp3KcS3in8T6EwDxtpF25jTxlc8hURalLXWZIbSOfVhQ/HoT8/6H8lnnW41Jsa GUUC3wtEFFX4BkxILNbnvSXCevkbJD9RqxNpoTPjgEqGSkT5Dt46A7swBTfMNL9H6pj5 qIJA/ONbIA6m3rLuYgBhd8ajZH4+SXGBOoeFPkyR8UNZwXcsGnNGjB+EjMk//tI7nhPa jdQmiA+O8sthQW5FfGsG7YuKqtiTRxeRahZFdH/JtstCfRpZDVw5HMAQ5eiEwxzPaK8Z gtBA== 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=6rKAANIqTwIC8Ceh4KPhaYfQQa+5WnOGnakTa9Yj4l4=; b=XL9nDyfVSeIVf3tjQmROiSCW31Att2Cfg7hiREhcDDqPIuQXt9PkzJdFn+1+h3qiIA Zfof8JMG0hqA0prgTG9ddhnHpyjxb8ZAfIU9EIlUmEdPXbnAd5B48x32ncj6megTBeIn Wds3NJVS+t45/8me/KvDoPyG+NJupOJ/438nUTNJ7R7JBGwrwTOSUa4W6dRHAZykga3T 1MutjIJX3Cu/3I2aZkQWBor6jeJIRKtglKYIkvkCopWy1aHQvRH59hYhDBjW6+4M+q31 fwgAtwtp2sYlXx/UGx6ESZ7RvNC9LByJMbiFA5GucrZp7tCseP5dZYGgulHioY/ImVte 2WYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nb8cdG+r; 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 u14-20020a170902a60e00b001b04a6330a1si711744plq.170.2023.06.02.03.37.46; Fri, 02 Jun 2023 03:38:00 -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=nb8cdG+r; 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 S235365AbjFBKZS (ORCPT + 99 others); Fri, 2 Jun 2023 06:25:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235527AbjFBKYD (ORCPT ); Fri, 2 Jun 2023 06:24:03 -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 624CC1702; Fri, 2 Jun 2023 03:23:27 -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 B4E3D64E70; Fri, 2 Jun 2023 10:23:26 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3D0E9C4339E; Fri, 2 Jun 2023 10:23:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701406; bh=XRnDiVEqXvkUypxOdLrzhfuiI1tjnMjnlxv76SViP7E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nb8cdG+r1sdlV0apRdvce02oohM8CR0EvHFvqpfm3DH1KVSVehy/Y61E+mDv5bwSk 1QJLdJLsskRV37lIubKeA+HS+dVI1mbbqM8u0VI4gBsDOBBUkjVZPIEk8KR7F37QJy URkBtMeAdd2UWM3tRVGrrOvgNZSnqK4dDNQwwJYoiDdKr1qRl1a6hGlIfT/h/CZGnm W+qVrZ5ShzYgkKdWTG+okyUaeWW1hqKngGSVk+AcRiVuOYly6dOzXTQsMygjnF2JnS RyZgCnMP01HhEshdc0KlTW+ZqtC1Pm8qEZ5mNn8WeodO78hLyudU5Z9LUk0bl9J5LM 47cC7cpCVk/vA== 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 v4 18/21] x86/head_64: Store boot_params pointer in callee-preserved register Date: Fri, 2 Jun 2023 12:13:10 +0200 Message-Id: <20230602101313.3557775-19-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2440; i=ardb@kernel.org; h=from:subject; bh=XRnDiVEqXvkUypxOdLrzhfuiI1tjnMjnlxv76SViP7E=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywNYmuWOdi4WEwk+wdhR3/nt6SW2CpJWp+IZwqfNP7 yhsP3+ho5SFQYyDQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAEyk+hQjw9vOUIOu2tWv0vp/ r5AxM1CI1Tl36LvWq2BROx0XY/6ynwz/bHs3vOC5Pbt34irHu60P2mcfEueL2Xs65sCDF2Wn79S tYQQA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.6 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?1767586955150844438?= X-GMAIL-MSGID: =?utf-8?q?1767586955150844438?= Instead of pushing/popping %RSI to/from the stack every time a function is called from startup_64(), store it in a callee preserved register and grab it from there when its value is actualled needed. Signed-off-by: Ard Biesheuvel --- arch/x86/kernel/head_64.S | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index a5df3e994f04f10f..95b12fdae10e1dc9 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -60,6 +60,7 @@ SYM_CODE_START_NOALIGN(startup_64) * compiled to run at we first fixup the physical addresses in our page * tables and then reload them. */ + mov %rsi, %r15 /* Preserve boot_params pointer */ /* Set up the stack for verify_cpu() */ leaq (__end_init_task - PTREGS_SIZE)(%rip), %rsp @@ -73,9 +74,7 @@ SYM_CODE_START_NOALIGN(startup_64) shrq $32, %rdx wrmsr - pushq %rsi call startup_64_setup_env - popq %rsi #ifdef CONFIG_AMD_MEM_ENCRYPT /* @@ -84,10 +83,8 @@ SYM_CODE_START_NOALIGN(startup_64) * which needs to be done before any CPUID instructions are executed in * subsequent code. */ - movq %rsi, %rdi - pushq %rsi + movq %r15, %rdi call sme_enable - popq %rsi #endif /* Now switch to __KERNEL_CS so IRET works reliably */ @@ -109,9 +106,7 @@ SYM_CODE_START_NOALIGN(startup_64) * programmed into CR3. */ leaq _text(%rip), %rdi - pushq %rsi call __startup_64 - popq %rsi /* Form the CR3 value being sure to include the CR3 modifier */ addq $(early_top_pgt - __START_KERNEL_map), %rax @@ -200,10 +195,8 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) * %rsi carries pointer to realmode data and is callee-clobbered. Save * and restore it. */ - pushq %rsi movq %rax, %rdi call sev_verify_cbit - popq %rsi /* * Switch to new page-table @@ -294,9 +287,7 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) wrmsr /* Setup and Load IDT */ - pushq %rsi call early_setup_idt - popq %rsi /* Check if nx is implemented */ movl $0x80000001, %eax @@ -334,7 +325,7 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) /* rsi is pointer to real mode structure with interesting info. pass it to C */ - movq %rsi, %rdi + movq %r15, %rdi .Ljump_to_C_code: /* From patchwork Fri Jun 2 10:13:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102458 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp927229vqr; Fri, 2 Jun 2023 03:34:10 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5YkV36YtwDeN8XQZk/utCwMgJUf89PrnRrZ2s4uRXux9IoE4T+xFEE2AxteswP8d6NXE3x X-Received: by 2002:a25:6d85:0:b0:bab:8ecf:3377 with SMTP id i127-20020a256d85000000b00bab8ecf3377mr2363428ybc.14.1685702050396; Fri, 02 Jun 2023 03:34:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685702050; cv=none; d=google.com; s=arc-20160816; b=S24BYt9n/TGqT+HZAGSgzgJJ0qsW+sQzNTMKX6VZ8lLZ2oQKrjAEWhzlHD5sDtMkL4 w4O2XzrB7IbJRQ5JPbFwx466FfVfKWZnK4B7ue9kVOpYKWGoi4A490WwCTxmBFhx24g2 BKh8kaYqFwb4H2TexelJ3mO6EXeCGZytro0/BVXvLIqS1/Apu7sof4QbZtnYMtF3KYyw IGVgfxw3H1wDpXHF9lu3pfsz0nWhQZI5z2aD68PXgLSYjUiuWgAnt5J8hvku3TnpfCfT Aacr29VX2aUu1X16Ug0XYxd3KJf+7wgDCa3OyK6kclgcchaHyNksMDBraKCjWnNPKEMN gsVg== 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=U22TslAsOouX8iihIejwgc+OY0cfIU4IwzZif0GRiQ4=; b=Y5Sduqjs80c660ycbUhANiveBNsOW/jl/p4IxmUQtj+H/PLFdevYNknONJYHyOSA3t mP6jvDMBNZCEqAJRqDsEkQOlB5jcgPgKkV9NJqVhgY/Os+11d0k7SxoKXMNqpBW6Fk4H +q0/RhmUkUFG73HUjtwNHY56DsT9NCIcVVZRFzTn3ANy4HHOdwkScAUonmUZ51jOYJ2x KNUt4t2xJBaL4AnacIDrwRKzFZEpgerMiI/WPexwLJzGsecxau357tMtU5B3lMTislLs 5FXc4Mxh7GtiEgBEBidTN6WtFiBJHw63rS93ltH4NNlWu7E5E/C2ekQmigKuEEPycpi4 HpWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="P/gwLBIz"; 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 k26-20020a637b5a000000b005301151503esi806717pgn.186.2023.06.02.03.33.55; Fri, 02 Jun 2023 03:34:10 -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="P/gwLBIz"; 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 S235466AbjFBKZY (ORCPT + 99 others); Fri, 2 Jun 2023 06:25:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235530AbjFBKYH (ORCPT ); Fri, 2 Jun 2023 06:24: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 902951709; Fri, 2 Jun 2023 03:23:31 -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 2433A64E6C; Fri, 2 Jun 2023 10:23:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9A6B6C433D2; Fri, 2 Jun 2023 10:23:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701410; bh=H6tv4XyD73jIrY44u3Vc1zagcnctlaNlh/vBGAcqcQA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P/gwLBIzNH+NElYX2XCXlc0mfqavS4Z9lEpjyRWc4yupGqJalLdp/xVB2IIta23p7 iCXQZa1XpC2u93fYEToTQBUhyYpw6uY0QK8mXpv/sXc0wW4Lpo7kM3ilZATRgSxxNK oJ/vP8O5iG+LNoBBD9BiP3zezS5nKcFR/xcZ5lL5v6Uz1JUCg1VWpSZr15qSrme4+i ZoSHamEkcbGlYsigt+cHNCtb4WHkcwYsUaHUHA2dEKgMHD/zDqZZS9ayeAxC1aMIJ0 +CE3OaNoxNbgsFsF5sDryALj4lGaC1pUhfX2D2NkDF9PBUwgsLHpcjuCAezof+S0P8 A/0bXSf6bmf6w== 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 v4 19/21] efi/libstub: Add limit argument to efi_random_alloc() Date: Fri, 2 Jun 2023 12:13:11 +0200 Message-Id: <20230602101313.3557775-20-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-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=H6tv4XyD73jIrY44u3Vc1zagcnctlaNlh/vBGAcqcQA=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywLbJCyR6Dz2PmK6q6BH/Yd9x/4dF2rM9P1xfHvTCf I+SuO6+jlIWBjEOBlkxRRaB2X/f7Tw9UarWeZYszBxWJpAhDFycAjAR79sMf0VZTnhx6DbIbfMU 847K2bdpk6G63bwpixfOFvH0EfXl/cDIsLJOmMmluPuNttwtxf6nz7bd5hM6oHRSTnyZuNWHCd6 iPAA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.6 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?1767586713331111802?= X-GMAIL-MSGID: =?utf-8?q?1767586713331111802?= 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 191698e8489d82e7..e90b8d1d5c7e8fd4 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 Fri Jun 2 10:13:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102456 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp926522vqr; Fri, 2 Jun 2023 03:32:38 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7ub0XeTGpwhK4AQoYLLbUUlQVDU8BR4Nnes65rwHd415ON+XEfIFrYHsuX5wPyprRjwkrQ X-Received: by 2002:a05:6a20:160a:b0:10a:ef03:3346 with SMTP id l10-20020a056a20160a00b0010aef033346mr14037676pzj.9.1685701958108; Fri, 02 Jun 2023 03:32:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685701958; cv=none; d=google.com; s=arc-20160816; b=VgmNcgnwXjBx74Qodo0eYDItBiETUtDiKGN00NJU53UYJSvRVuDLOEwpSmKvJwwX5e MQ3Xemh7djXNVxnJXkNVtq3C/63KAJhM984ob5D3uGBuvIiccDbF02dpWtmlAD6sY7Z4 mkGpXqDhqRLQ/NC+8xndi8RHnMhhTU+kiq595itUMoe9h3V4if9uY8h8pG5AouS8rQ9R 7lcHv+QMGsFs2zQ+k7rGOHrF/hQzJzzZIobr28yjmFwXkRMCbod95RuwAeVjO23sBQrc wdxdVgRbhu1CR+etNata7tLCLaqqb0iCkebq6sLMHMdgQlQAKKUpG4owWJSJgtamUqrz 6sng== 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=zMCRK05bXSWzy7kxAgWhd9ZyBOQnWhVfSuvi7H2l874=; b=bWagLyQQUrLn69nXa7E3VFudoLeWIEPgogdyOQRteTiaWCea+PE1wObXInPPCfb9Cy tdvxky92njTOWDwBCBhJTQeXeA8MXDFJmJhPiEtYplcMtQ0YveS9j3C65T78opoh57LR km2BLIO9gYcFdKpAB8g9BZF2wj3wqM9vhbFT0uLj5zyK6rZJx7oSH0tsi8e1nm37aHL3 Cc2KFZMfVSXR6a/jqcWZ89QEtMlzdLV18eoB74GfSawSj5XqkDaK71rwMZ2kKwpZIFvK SZessW/K4so6PGR9+REwtKQUuRYw+4KgGlfkPiDS5ecpnf4asDHDTFKHgrPmwXlqZDff JDOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uYk1Zs4t; 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 d10-20020a63360a000000b0053f2fc4379csi767983pga.391.2023.06.02.03.32.24; Fri, 02 Jun 2023 03:32:38 -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=uYk1Zs4t; 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 S235555AbjFBKZl (ORCPT + 99 others); Fri, 2 Jun 2023 06:25:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235485AbjFBKYJ (ORCPT ); Fri, 2 Jun 2023 06:24:09 -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 017E01710; Fri, 2 Jun 2023 03:23:35 -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 7E7DD64E63; Fri, 2 Jun 2023 10:23:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 02497C4339C; Fri, 2 Jun 2023 10:23:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701414; bh=Uzc6TTIkVLCBtkTdTqlEAm179xBBwMbrhTFacsgdIgc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uYk1Zs4tYhSRbRmPt6QrbkWNXwtU/5wEasqMtMC8VqgbAXOCdhpbpGfJDr7UIH/UL DAG4Qr2okHRoXHaBooODGt+jY8vdrofyLbC8MAA7e+JqMQoJqNE6tn6b1molzC4kKf p4GqArNxm3NOKBiQLhq4S2692DhKyyPTd30gNrzGMAcnnU2fJmjlfoRC2eovkjt+1T UUTJgcj0m+P5T7gf5dewMHv2EJ4l0A/6MOeF/g+BHgSTXnjVlpZZnvEV1H8nHRlcxV agDzt81CGGipQSzrDszyvIcyNqClz8vqn3qkJWnn2NU6TxkI00f7CtDUYjiNU/JJIi a4qBd7O6uhyKw== 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 v4 20/21] x86/efistub: Perform SNP feature test while running in the firmware Date: Fri, 2 Jun 2023 12:13:12 +0200 Message-Id: <20230602101313.3557775-21-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=6505; i=ardb@kernel.org; h=from:subject; bh=Uzc6TTIkVLCBtkTdTqlEAm179xBBwMbrhTFacsgdIgc=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywI6G13uSLty0ypBXNL8/nX/CnRtbrQvfTFeMO2w5j ylcp0ezo5SFQYyDQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAEwk/DjD/7xn6vPNjk145idQ LZXgkXdllcjlTatvv5LcnDt1asiWTamMDA2889Td5+4M+CHXtrlBLVv4kpKnZt7sI855My4wx3z J4AQA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.6 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?1767586616432754563?= X-GMAIL-MSGID: =?utf-8?q?1767586616432754563?= 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 | 74 ++++++++++++-------- arch/x86/include/asm/sev.h | 4 ++ drivers/firmware/efi/libstub/x86-stub.c | 17 +++++ 3 files changed, 67 insertions(+), 28 deletions(-) diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index 014b89c890887b9a..be021e24f1ece421 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -315,6 +315,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; @@ -328,7 +333,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); @@ -338,10 +343,38 @@ 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 + */ + eax = 0x8000001f; + ecx = 0; + native_cpuid(&eax, &ebx, &ecx, &edx); + /* Check whether SEV is supported */ + if (!(eax & BIT(1))) + return 0; + + boot_rdmsr(MSR_AMD64_SEV, &m); + return m.q; +} + +void sev_enable(struct boot_params *bp) +{ + unsigned int eax, ebx, ecx, edx; bool snp; /* @@ -358,37 +391,14 @@ 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))) { + /* Set the SME mask if this is an SEV guest. */ + 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()) @@ -409,6 +419,14 @@ 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."); + /* + * Check for the SME/SEV feature: + * CPUID Fn8000_001F[EBX] + * - Bits 5:0 - Pagetable bit position used to indicate encryption + */ + eax = 0x8000001f; + ecx = 0; + native_cpuid(&eax, &ebx, &ecx, &edx); sme_me_mask = BIT_ULL(ebx & 0x3f); } diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 13dc2a9d23c1eb25..e5aad673194698b8 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -202,6 +202,8 @@ void snp_set_wakeup_secondary_cpu(void); 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); +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) { } @@ -225,6 +227,8 @@ static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *in { return -ENOTTY; } +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 2d3282d2ed6eb756..f9d203b5ee6236e8 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" @@ -756,6 +757,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) { @@ -785,6 +799,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 Fri Jun 2 10:13:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 102463 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp928239vqr; Fri, 2 Jun 2023 03:36:19 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7QtD7LycCVI6tNhPbLif0rJ6SZ6ea1gVRbXuvY8UWHoEOp0Ou2gihVCEwWOvON+dZC8i1d X-Received: by 2002:a05:6808:b21:b0:398:5df8:5d63 with SMTP id t1-20020a0568080b2100b003985df85d63mr1480369oij.16.1685702178784; Fri, 02 Jun 2023 03:36:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685702178; cv=none; d=google.com; s=arc-20160816; b=Ce7gZ43EwLAlzEFzpRePTsiY/no4gVYnJ5sS0p8pCBxd1h2FcCYBBvg3+1cT/hs1h2 wwjw05sMz7vIdKzWCSiW/1hukKmGSd3mHlOEG42jOeNsxXgunhdj5e/zcd/2R4LO80mY u+cTamCgBjDc2tDFug+EATuKBEg+7Hs/DWIP74HPDfxAeElSofllGFhNIhA0mTXHmTcR Fdgzpc2Fm+jVqJBEeqeeu/Nkub/SoiPT8255KR89+xEPut/+c2tJufRChy2rFMhOhyk7 RNcvSV2aQXAzkJ7yM5MJMkfqSNUXmksVp5PZhFYCxz5eoTLI/GHA/wj6tul/BpWwWfPQ hkOw== 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=yNHQ+nl3/blCatkPv6TQWRU7CT4eGBDLfelGUE9cU50=; b=B/+OWWeIg6pDD5LqB6LzHpq1Z2S3tuJtVYGE+a/ysHuXABbgpK4PvG+q9XUbEEE/D/ XLKxTh+FTKsUK8aaP9jOAT5mxeN4WhZXAKQ6FHIpPpm02ejnuM3LEUuUy2GX7WT6dQyv qNC+T0ypFsFzCAcNi18yHy8HPLze085xx6END5mdda4jxUHCzbh1+HF+odhAGO6ttyle c3eGyOxUwNq4BoB3FRGBTzfIaibqgThbXkWx4mXXWiRqsUrdpAWAusOTGTZSYsIy/6Gm thokclbCSBPoV1fZ4HkgSa93FZSGyhgKHgxjxUsbQKrUWZ8Nh+jHwhdKr/HoehJ7U4jO vauA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JJwTqDrQ; 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 c6-20020a6566c6000000b0053fa62fc004si809089pgw.33.2023.06.02.03.36.04; Fri, 02 Jun 2023 03:36:18 -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=JJwTqDrQ; 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 S235590AbjFBKZr (ORCPT + 99 others); Fri, 2 Jun 2023 06:25:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235541AbjFBKYR (ORCPT ); Fri, 2 Jun 2023 06:24:17 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FE1F171D; Fri, 2 Jun 2023 03:23:40 -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 DB83A64E80; Fri, 2 Jun 2023 10:23:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 60956C433EF; Fri, 2 Jun 2023 10:23:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685701419; bh=x7IKVLjcFP3CYkwluyIAzVjkKiuNw7y/y12cM78AR+s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JJwTqDrQGN6dJh9XDufzT2ymF63m/yrtttTQ5jQEnHkfZcTYh/6FdOt2ZBizC4/EY 3D1f3FobDS6xpip2z2/3iARDpiD2WRQN59Bs6MUMoWlroV7c5FdWWnGFtYYb9YZ619 Vapsx3IUKxNMrE4fFf+fMIe5wmnzejBuMMoqpSQ8/G2v8mfNo+NPlwgwT5+A6XmLCD ziAbvoMRWCa9RgIiOAe7/bbHEs1hR/vp7JWCoPSmpgWAtdDIJ0+kMPFu0hr/7dtcI2 LhJM1kqGc82S4Ap70aAjZnol4IOZj4Yi651wT1S/K4Np/GwNs6wBbQiovSo2bl/wtE 2AytDbT7k65KA== 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 v4 21/21] x86/efistub: Avoid legacy decompressor when doing EFI boot Date: Fri, 2 Jun 2023 12:13:13 +0200 Message-Id: <20230602101313.3557775-22-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230602101313.3557775-1-ardb@kernel.org> References: <20230602101313.3557775-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=18379; i=ardb@kernel.org; h=from:subject; bh=x7IKVLjcFP3CYkwluyIAzVjkKiuNw7y/y12cM78AR+s=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXywE7jEv2vBoEvpSQVEpxndXBY/dtuvti63umWY/K3S zfDLhR1lLIwiHEwyIopsgjM/vtu5+mJUrXOs2Rh5rAygQxh4OIUgIm03Gb4X90jqmU+5WDEilVn dGv177u2deyc8qlCYbv3E8f3OZ4vjzH8U/X++V7m30wvZ9/zc58k9qQ+vzRh2lIFtqKpv3yusVk eYAcA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-7.3 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?1767586847374577724?= X-GMAIL-MSGID: =?utf-8?q?1767586847374577724?= 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 3074d278c7e665d8..aee518e71cdb7e75 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 e5aad673194698b8..bb1ed2a8b8fb122d 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -157,6 +157,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) { @@ -210,6 +211,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 f9d203b5ee6236e8..ed91a2a4984d4bfb 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; @@ -276,33 +273,9 @@ 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 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); @@ -311,9 +284,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); } /* @@ -466,7 +436,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); @@ -770,6 +739,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, + * we don't use seed[1] 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) { @@ -788,10 +812,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; @@ -820,60 +843,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) { @@ -891,6 +860,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 @@ -930,7 +905,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); status = exit_boot(boot_params, handle); if (status != EFI_SUCCESS) { @@ -938,13 +913,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");