From patchwork Tue Aug 8 08:43:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 132619 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2289811vqr; Tue, 8 Aug 2023 10:47:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEr7baL8QqtctF/EJk+xgCEirtHr1GkUWdgQXta4J4xGuHzsx22zsc3yhaSJ7WzUy6vpALB X-Received: by 2002:a05:6a00:198c:b0:682:5634:3df1 with SMTP id d12-20020a056a00198c00b0068256343df1mr213235pfl.10.1691516827216; Tue, 08 Aug 2023 10:47:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691516827; cv=none; d=google.com; s=arc-20160816; b=lix5lHX6fJQqvjfJwMn2dBQ7vJByOi+SEqsy/5VwgbcfwVzCrD+XZ6tHGRH4qAjfG9 LmMcgL47WnsXxMDURtIWvD3rpZgsFIu3T3SCwDZb3Blz1zNlA1tZfImAbypDHtXfrMkw LkTqkLAVaQfkwJ5Q7YAbZHYmLZ2HpRAyBVPwf+guUjDVe9PXN1pMaMPafmmrH328+VVK tmUHjUMarJiitekOvNDgcFdW6lrpJzA6t7TkuYmZj55XhHD/lDblJv04Sh+hA6P0hoM/ WP9hJouy849k+SQDBKmNCauB3mWDZscGhp5h2vIYYBz2mIknMGKifbrhuJiwG4GZjS9J d0xA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=r8MSe0CeLnSuXDYNXmQMGffVQ+qqWANcy97jyL1wqRA=; fh=upm4t26Ojn/J6N+Q9k8Qqzx52VymNDAlYlARzLeNh5s=; b=uKExOGgZnp0579nhgYsvEtDJTNMUxV9BpTzYbraul4vdsMBU7ZCUfhEcbmpjch355j HTssK/w78nYL/S7KxHuKlqz1ixlN8RBog00PK+qYsqv5yYtvMbWaYHvWcVdlVRtLn2zX Joju1wn73i0SluX6VMODaDLAic9af6+v8e3ZuJflm1pa4+sEZe8FdxtFmTymklohQUUI iY3lM9MO8Ddjbh1U73mJF7hrEa25YRGGIYGu5GuNvHhNL5Ixcvzr1JfpiFoxjNqN6g7X V15/Il1AoN0IIpyZKaSbEGb1xWEVyrBsQwLPaCD5MX+TodSJtn9GBC5RHLBzjZGHLQO5 4PQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="ZRTnQ2X/"; dkim=neutral (no key) header.i=@linutronix.de; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ca20-20020a056a02069400b00565131aae88si1818464pgb.154.2023.08.08.10.46.54; Tue, 08 Aug 2023 10:47:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="ZRTnQ2X/"; dkim=neutral (no key) header.i=@linutronix.de; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232842AbjHHQ3i (ORCPT + 99 others); Tue, 8 Aug 2023 12:29:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232731AbjHHQ20 (ORCPT ); Tue, 8 Aug 2023 12:28:26 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8297A1251D; Tue, 8 Aug 2023 08:51:02 -0700 (PDT) Date: Tue, 08 Aug 2023 08:43:59 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1691484239; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=r8MSe0CeLnSuXDYNXmQMGffVQ+qqWANcy97jyL1wqRA=; b=ZRTnQ2X/RZ70sXWwIrfUBcu8cWR3hvaqzrzYrHIoRrBbpdx3WfABs0BZG7eiq4I4dT+Ui6 WJSDqY0fVR5WXXBtdAKmznTxX/wwCT4G3qXt9btoyZiAPdLfG00uDxnqYiUkQOqjzG9n0p sF7PVCDV9/WbO33LnSUgc9fW2QSDx4cB87vF80CRrC5ImyOlZCejWfPAW8SXd+kQntOtTq bUj3RoEBIFcg6cEPb/rEHAfD4fPnx0+YhIAYdAVE6k4SX0tqFECEDnxYbWP5wPWYeaMtJN 3ZgyaOSffTVsyJPHv2IVpM03K5L7xLyGadRF6Nd7nmNsyu4V0PtT/v1dVGbbbw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1691484239; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=r8MSe0CeLnSuXDYNXmQMGffVQ+qqWANcy97jyL1wqRA=; b=j2aHBmoGe5zObUg1ajj5udUKdf8gpjp6/755aVGDJJ+G31NJtq+86PyRcz5H4AjKcXhbTi lg1QM2wczZtv7CDQ== From: "tip-bot2 for Ard Biesheuvel" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/boot] x86/decompressor: Factor out kernel decompression and relocation Cc: Ard Biesheuvel , "Borislav Petkov (AMD)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230807162720.545787-21-ardb@kernel.org> References: <20230807162720.545787-21-ardb@kernel.org> MIME-Version: 1.0 Message-ID: <169148423908.27769.3075033623173952060.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-0.6 required=5.0 tests=BAYES_00,DATE_IN_PAST_06_12, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no 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: INBOX X-GMAIL-THRID: 1773589807427537004 X-GMAIL-MSGID: 1773683948993730565 The following commit has been merged into the x86/boot branch of tip: Commit-ID: 83381519352d6b5b3e429bf72aaab907480cb6b6 Gitweb: https://git.kernel.org/tip/83381519352d6b5b3e429bf72aaab907480cb6b6 Author: Ard Biesheuvel AuthorDate: Mon, 07 Aug 2023 18:27:17 +02:00 Committer: Borislav Petkov (AMD) CommitterDate: Mon, 07 Aug 2023 20:59:13 +02:00 x86/decompressor: Factor out kernel decompression and relocation 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 Signed-off-by: Borislav Petkov (AMD) Link: https://lore.kernel.org/r/20230807162720.545787-21-ardb@kernel.org --- 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 2d91d56..f711f2a 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 9191280..4ae1433 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 */