From patchwork Wed Jun 7 07:23:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 104295 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp75037vqr; Wed, 7 Jun 2023 00:36:21 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6+gUOgsrkKv3/fg1CnOAEXRMFtEOKDFj1WHO38KGS0MpRYREcDAws4tR+mRzAFomfq6mPC X-Received: by 2002:a05:6a21:3714:b0:112:6217:7a62 with SMTP id yl20-20020a056a21371400b0011262177a62mr2023538pzb.45.1686123381550; Wed, 07 Jun 2023 00:36:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686123381; cv=none; d=google.com; s=arc-20160816; b=HsGwEXt4v7DgzaqebSFUWzL6yd1CsHvWkSqdJ66PmrXRXX7siKanskaX+1OUaGwX5t sLMDTDsfJGgfhyyJgDJ7kFVBy6shQWYuWyAvlDVPqIou/zp4qalWsUdXff3r+KNzD/C1 8QKFZfDBboZEy03y/FFeilFbU+ynbspnFYEIFlXXv5FC0CR4tNQbZsPNvHNAex7XabLB 278G7IkOYBaXxXIm9MYpLEbseFLXFoFRD+UT/E0bhaUABBKCXAbZTNM1oQLVgnS5r1oD 368T4RyprFK0/Sb21I8TGwAObj5mLo2FDb+xZHZ4JelZ8zWNEuXzFGr6thN/7PkpUd0p 6HSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0EChhZLGsOkUurjosqlVA0InwtYI2wSmS1z5ylf1X6U=; b=ZJQJ7x+je64Znoc2tgCjZlazRVqQjvAnqC9zRLjuOnEERp0E/luPOf0sTd1e1QvbwP ehDlrMjowdEzcLPJsxThc1nC8BbBx+kqeMmzgpko3b5wHlBJNzLlZpYLcG3yNAX2BCJu t/nV6ZTzbaRijpL3uNr1B7Ln8Rn7zjCaHBJb7zGcu1lJp4sQAg+d7HyuRFmZwWl6wjH5 7OAXT0M7J5iD0Lfufi3wHSnH+l84JhMj/B8tF+GrEeN7Pl6WQcBm4slXjrU6cTSmgbVV ycJnPRlVmEL39TzkUzuEw/qkKNxR/go/CYVuFkBP6VS/cgBtMjNWxH46YGkCAUZcUcVa ClXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Eu7a3ltL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a187-20020a6390c4000000b00543eddc6206si2655194pge.431.2023.06.07.00.36.08; Wed, 07 Jun 2023 00:36:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Eu7a3ltL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239139AbjFGH2F (ORCPT + 99 others); Wed, 7 Jun 2023 03:28:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238984AbjFGH0p (ORCPT ); Wed, 7 Jun 2023 03:26:45 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CB8C2D6A; Wed, 7 Jun 2023 00:25:09 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 135FB634D4; Wed, 7 Jun 2023 07:25:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A5209C4339C; Wed, 7 Jun 2023 07:25:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686122708; bh=OPBcikll8Xc3e/JAsC+t93XsxVWQdgLYFLK1YAtUnZI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Eu7a3ltLtybCWEGlYROAtRjRMrWDla6YAZMxImo/tLUJJn4ftORrrYTYEAlD+8Vd6 hrbPlcVXaAsR8ssRvUaL4DHuB9oY8L5vVHlMFzY6PK7gaS2mRwX4MTN/CMdhvR1z9F RZ7KsZenDwvfYUZYdZ3jXiY13+RRzG1B1uvkWjOMFBC/Xp41VDsWIBVnDn/E2jYsub NV4BAaJmRRnOWxGqouxCIIG8yA/QfmJ99F2LVvJopEwPXHa9+BNDYqy4wUqg+8qfCU OgoCo/+IIc6sjz2MkORNqanqSGsMF98fXzuEyq6YOcVEA0KpC+A/KSHQyOMndMOxDk wWBFe5vuw8NJA== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v5 17/20] x86/decompressor: Factor out kernel decompression and relocation Date: Wed, 7 Jun 2023 09:23:39 +0200 Message-Id: <20230607072342.4054036-18-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607072342.4054036-1-ardb@kernel.org> References: <20230607072342.4054036-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2968; i=ardb@kernel.org; h=from:subject; bh=OPBcikll8Xc3e/JAsC+t93XsxVWQdgLYFLK1YAtUnZI=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIaXBoMom6LTuxX+/zu5fcIVV+winTcebO1dKNxlpyVys5 lxfXnOwo5SFQYyDQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAEwkRoOR4WBW/p3cR58f79sg dbFn0pxJ+b/2vP9XJa8cy6AQc2DpCROG/xEPPLZ82Z1vcTBsodetWQ9M7meXsE74t1p8H+slnev vmFkB X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768028510820818948?= X-GMAIL-MSGID: =?utf-8?q?1768028510820818948?= Factor out the decompressor sequence that invokes the decompressor, parses the ELF and applies the relocations so that it can be called directly from the EFI stub. Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/misc.c | 28 ++++++++++++++++---- arch/x86/include/asm/boot.h | 8 ++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/misc.c index 0b29a3183df42246..93220a2a276b0ffb 100644 --- a/arch/x86/boot/compressed/misc.c +++ b/arch/x86/boot/compressed/misc.c @@ -330,11 +330,33 @@ static size_t parse_elf(void *output) return ehdr.e_entry - LOAD_PHYSICAL_ADDR; } +const unsigned long kernel_total_size = VO__end - VO__text; + static u8 boot_heap[BOOT_HEAP_SIZE] __aligned(4); extern unsigned char input_data[]; extern unsigned int input_len, output_len; +unsigned long decompress_kernel(unsigned char *outbuf, unsigned long virt_addr, + void (*error)(char *x)) +{ + unsigned long entry; + + if (!free_mem_ptr) { + free_mem_ptr = (unsigned long)boot_heap; + free_mem_end_ptr = (unsigned long)boot_heap + sizeof(boot_heap); + } + + if (__decompress(input_data, input_len, NULL, NULL, outbuf, output_len, + NULL, error) < 0) + return ULONG_MAX; + + entry = parse_elf(outbuf); + handle_relocations(outbuf, output_len, virt_addr); + + return entry; +} + /* * The compressed kernel image (ZO), has been moved so that its position * is against the end of the buffer used to hold the uncompressed kernel @@ -354,7 +376,6 @@ extern unsigned int input_len, output_len; */ asmlinkage __visible void *extract_kernel(void *rmode, unsigned char *output) { - const unsigned long kernel_total_size = VO__end - VO__text; unsigned long virt_addr = LOAD_PHYSICAL_ADDR; memptr heap = (memptr)boot_heap; unsigned long needed_size; @@ -463,10 +484,7 @@ asmlinkage __visible void *extract_kernel(void *rmode, unsigned char *output) accept_memory(__pa(output), __pa(output) + needed_size); } - __decompress(input_data, input_len, NULL, NULL, output, output_len, - NULL, error); - entry_offset = parse_elf(output); - handle_relocations(output, output_len, virt_addr); + entry_offset = decompress_kernel(output, virt_addr, error); debug_putstr("done.\nBooting the kernel (entry_offset: 0x"); debug_puthex(entry_offset); diff --git a/arch/x86/include/asm/boot.h b/arch/x86/include/asm/boot.h index 9191280d9ea3160d..4ae14339cb8cc72d 100644 --- a/arch/x86/include/asm/boot.h +++ b/arch/x86/include/asm/boot.h @@ -62,4 +62,12 @@ # define BOOT_STACK_SIZE 0x1000 #endif +#ifndef __ASSEMBLY__ +extern unsigned int output_len; +extern const unsigned long kernel_total_size; + +unsigned long decompress_kernel(unsigned char *outbuf, unsigned long virt_addr, + void (*error)(char *x)); +#endif + #endif /* _ASM_X86_BOOT_H */