Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp1127550rwo; Wed, 2 Aug 2023 09:05:29 -0700 (PDT) X-Google-Smtp-Source: APBJJlHj8sXkET48X6KGDs2o1vstTyKXmAZ2vElkQTyz4uVa6pmb0rFFsm9+VAKO19ySFX2wbngo X-Received: by 2002:a05:6512:3b97:b0:4f8:651f:9bbe with SMTP id g23-20020a0565123b9700b004f8651f9bbemr6610465lfv.54.1690992328719; Wed, 02 Aug 2023 09:05:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690992328; cv=none; d=google.com; s=arc-20160816; b=WCqjECV5Um2F/ItejgiqksfHMxksDXXvXLlxXYAQo/FIA/Fs4poxMmYEH9AIi2Sy8j AJXNGMRly71QKtl1JjGS9mHs4JzB5V3LOjnywAnprqgng5nFZU5TpIU3pQYys6DAZXWk yQ8jzKicdDMPPfcam7oS95zCXGZwYYHlEWqmnneT8boYAb7iSWiADITn3AxPEh/NNjGl 8mgb/YNzZCOpESzTEhBwM1oSv8o5q6l6U21V1vn50geymV2Bwhr5rhFcdLN6KDXZEkQm /vR2rsXBW2w77vHPB5TC+EJFdTAnAMin4d1SQ59kxcu2DlOH1oSLnu2Rq5jpX97irwQG u8fg== 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=C69ABrgXNKFWMd1YawkeMyj2Gl7APwoI4DAz37maNOg=; fh=UoPnrMfWVKGOqsML3Cwqzr4dYAyNRBpPmY/ITnBmPi0=; b=mJSrOxqT0iR6xHhMoCFNX7TKhzzIRdRMMpmXK3PgMnUN1hs5erdVW4GPQtcp1bWoqz ERFReHt2XmglpnSBdB+j1l6tP98o1cHniHSy15qvfCbZ6uZcI7ew1i0vAH/TTlDmpTQ4 85Dl4qV5tVhjdsYwY2oBDWRxTw84rT0InYjalXn29/1VXRa/CsJAp/RfyNL6deW++JK2 ZS+NiciCv9aooFiVSAV8C4B1bilBSvfpso63rLj7LL+pvh4nE+ipqH8fIhAfdg4aeEdc NTBLK5FJwDWu4CIFwaX3k6KkgdNM1cZ0c+hN3Yoq2AZmWEHCcSX7GInAIdJlb+n8gpsB 48Xg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=TBGV7Kle; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a17-20020aa7d911000000b005221f6cd2cfsi9667381edr.294.2023.08.02.09.05.01; Wed, 02 Aug 2023 09:05:28 -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=TBGV7Kle; 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 S235414AbjHBPvh (ORCPT + 99 others); Wed, 2 Aug 2023 11:51:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235418AbjHBPvA (ORCPT ); Wed, 2 Aug 2023 11:51:00 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0AD343C2B; Wed, 2 Aug 2023 08:50:19 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C592A619D9; Wed, 2 Aug 2023 15:50:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 50933C433CB; Wed, 2 Aug 2023 15:50:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690991419; bh=35ydgYIIKoLxZL2vz2F8XVJhO0j9D3mjWHEuMrtQ7Ck=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TBGV7KlefXGHmsD+HOTcRqgYHCSof4BZcFbVEH5gszSi4zld41ZqILqjGWia2vxOh w4gvqeYcttH5A6M3OyV4HRNbCMzNlKIe2wEiy0LXbquTUk+Be9RgImv7nl2Pl9nL5w BPemBTmbR6g3Ndy1Kmc0/sYO5ltuR0I3Z/ChM1SVNbFzv8FQt581nXFpaEbsN9mlEU 2nS0RJu6AZG0p8WvndJIsgNt0vbY65NPwDLunld+2PcWbQ5yzG+LR4sSHrXuhU8ieJ oYQaD76Aj6AIFlijAP3KKZRcwCoTCQ74vvq4L9zv/N+XX6L/zw6mIra99lZN7Bghhy NFhNT6y+sJeDw== 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 v8 20/23] x86/decompressor: Factor out kernel decompression and relocation Date: Wed, 2 Aug 2023 17:48:28 +0200 Message-Id: <20230802154831.2147855-21-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230802154831.2147855-1-ardb@kernel.org> References: <20230802154831.2147855-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=35ydgYIIKoLxZL2vz2F8XVJhO0j9D3mjWHEuMrtQ7Ck=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIeVU1ekFc9wUapLUD1QecUzquslXIul15lj76WX1V3vWf b4Y4fmpo5SFQYyDQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAExk3hWG/zHrFpx4tv7Vtf6H oiLNjIePBMXc/ZpTu8yx7qWSavLsJRoM/0MW+0/1k7DhY558Xqhs++GYSSUfJqh+XiN73SZK/Z2 vCi8A X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 2d91d56b59e1af93..f711f2a85862e9ef 100644 --- a/arch/x86/boot/compressed/misc.c +++ b/arch/x86/boot/compressed/misc.c @@ -330,11 +330,33 @@ static size_t parse_elf(void *output) return ehdr.e_entry - LOAD_PHYSICAL_ADDR; } +const unsigned long kernel_total_size = VO__end - VO__text; + static u8 boot_heap[BOOT_HEAP_SIZE] __aligned(4); extern unsigned char input_data[]; extern unsigned int input_len, output_len; +unsigned long decompress_kernel(unsigned char *outbuf, unsigned long virt_addr, + void (*error)(char *x)) +{ + unsigned long entry; + + if (!free_mem_ptr) { + free_mem_ptr = (unsigned long)boot_heap; + free_mem_end_ptr = (unsigned long)boot_heap + sizeof(boot_heap); + } + + if (__decompress(input_data, input_len, NULL, NULL, outbuf, output_len, + NULL, error) < 0) + return ULONG_MAX; + + entry = parse_elf(outbuf); + handle_relocations(outbuf, output_len, virt_addr); + + return entry; +} + /* * The compressed kernel image (ZO), has been moved so that its position * is against the end of the buffer used to hold the uncompressed kernel @@ -354,7 +376,6 @@ extern unsigned int input_len, output_len; */ asmlinkage __visible void *extract_kernel(void *rmode, unsigned char *output) { - const unsigned long kernel_total_size = VO__end - VO__text; unsigned long virt_addr = LOAD_PHYSICAL_ADDR; memptr heap = (memptr)boot_heap; unsigned long needed_size; @@ -463,10 +484,7 @@ asmlinkage __visible void *extract_kernel(void *rmode, unsigned char *output) accept_memory(__pa(output), __pa(output) + needed_size); } - __decompress(input_data, input_len, NULL, NULL, output, output_len, - NULL, error); - entry_offset = parse_elf(output); - handle_relocations(output, output_len, virt_addr); + entry_offset = decompress_kernel(output, virt_addr, error); debug_putstr("done.\nBooting the kernel (entry_offset: 0x"); debug_puthex(entry_offset); diff --git a/arch/x86/include/asm/boot.h b/arch/x86/include/asm/boot.h index 9191280d9ea3160d..4ae14339cb8cc72d 100644 --- a/arch/x86/include/asm/boot.h +++ b/arch/x86/include/asm/boot.h @@ -62,4 +62,12 @@ # define BOOT_STACK_SIZE 0x1000 #endif +#ifndef __ASSEMBLY__ +extern unsigned int output_len; +extern const unsigned long kernel_total_size; + +unsigned long decompress_kernel(unsigned char *outbuf, unsigned long virt_addr, + void (*error)(char *x)); +#endif + #endif /* _ASM_X86_BOOT_H */ -- 2.39.2