Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2568461rwd; Mon, 22 May 2023 00:24:50 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6mbdgBI0qoGolYZCsfZH0b5j6hfQRYxqeGyGPc/OyHdEewE43V+JcAmoiulMmwgD93Px67 X-Received: by 2002:a17:902:c94a:b0:1a5:22a:165c with SMTP id i10-20020a170902c94a00b001a5022a165cmr13033136pla.0.1684740289849; Mon, 22 May 2023 00:24:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684740289; cv=none; d=google.com; s=arc-20160816; b=VZ7lT8feinut2D3MjN1clm6N7h9WpqMGY4jVUv6z3q7Yam0ySNWvuf1DjhmlyJAImN xYfoETFBI2Pw04Mi2AFu08xeUkJzxAJBgabTVBeBIzca4/ZTvwCrixgSts7kN1Sr1cMy IZaKtiy3TenYgGo41xuflGLN5oMiE21lNaPchLbcBTt/MY2q7v+izm95f20kWYrkmNS5 pF+CzYlygd7bdl9kbdbASSPKmwxLJI+MCkUL36kxAvvHzKOqwTmkAL2WwAh6pfDrY1Ji VsJJs58Ge/r5IzWtrBn7jHf2mrSAxq0AVpwV1YubLio5PO4o25j6Zec9fmWjng5hZz7U E5Uw== 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=D43WjcGOL9KxhFwmQP1Fz2Efyn68t+ClAfe9PyW3qY/vW46jKyf7w7cZsH6glk7RnV q8+t0wvRgHXoeeTXXNXIEkH36sFnZLCg67LE21Awff0TKCvD5tRZ9+yO0jtcUY3yJ87x hSlt8tSar2Q37IdllzmsVOwC9ttzzQ3Cp/HvdyarCsvKNW2FwnAGEQvHwqmzlyPVOiDz vNMaTNYvaj5mCjq3mXIPAqrdoQ6fdb53DvKG+yDSyH1JE/Ag2x1ljnrJ8TpV/SOxGrwC s32yi+LKw6uRWRb6N/G95xjowwPyz+YfBTnzLZTOfQ/xfz+ekt++J8TTQ1mUF4a3TYrd 7U6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rr136Js0; 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 jf10-20020a170903268a00b001a92a507187si4061877plb.80.2023.05.22.00.24.35; Mon, 22 May 2023 00:24:49 -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=rr136Js0; 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 S232569AbjEVHRY (ORCPT + 99 others); Mon, 22 May 2023 03:17:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232500AbjEVHQy (ORCPT ); Mon, 22 May 2023 03:16: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 84C62DB; Mon, 22 May 2023 00:15: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 4293261DD9; Mon, 22 May 2023 07:15:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F0AA3C433D2; Mon, 22 May 2023 07:15:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1684739744; bh=U1WWTB8Ed6jF5PagzQzhXVR5oyHC5bjwVmmRHfA2IP0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rr136Js0c7uMMk3uXQJZmoR+32dVkdLQdERBEOeclNrZle0SdfRIkLTAsRQdO77OR Ssee8IShaZWdYn3ltkiVok+AZkNQOFffZDPdO/WoMgLPJd5X1jmTygJgjNnIoSMDlw a938/yTL505LpkDrgCrhmTZvwidrQ27HKpIcXyWJ/mPtvQWfa8Tv/nfk9+ohhlwbCh Ox0Q2wYUfBL9gmwcNmAeeAbXfAW83O9H8eah3uMh9ywfT3FtxRL+dzCiJCfBXEaJrr flnvFdWOkh9mDJ5L5+m51UyipTj/owTVQQw/OURG9K5yog28dyhUW1cnLwDwh+S29T W8bd/ajAXFM7Q== 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 Subject: [PATCH v3 17/21] x86/decompressor: Factor out kernel decompression and relocation Date: Mon, 22 May 2023 09:14:11 +0200 Message-Id: <20230522071415.501717-18-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230522071415.501717-1-ardb@kernel.org> References: <20230522071415.501717-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=owGbwMvMwCFmkMcZplerG8N4Wi2JISVbzInp2qXX4X8a2I+/eyMtFH7p1BEGxr2LvH6UaC27O y/lfuHXjlIWBjEOBlkxRRaB2X/f7Tw9UarWeZYszBxWJpAhDFycAjCRtOkM/+s0DRi736y1lWna uO3hvhy7XN5lpWtPqb3582tOtcwBoxcM/2v/zm2ZMdt7s6f9XN93Af9dVy39UvvofmyPo2eSzcL wFHYA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 */ -- 2.39.2