Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp1163348rwo; Wed, 2 Aug 2023 09:35:07 -0700 (PDT) X-Google-Smtp-Source: APBJJlGC8fDzQZ4Dy+wClBSmd0QP4wjxr9fKOP3FfkOqBflUVcwLjQeWPeEXftVRmCJpo2tKWF1v X-Received: by 2002:a05:6a21:780a:b0:13b:a129:a6cd with SMTP id be10-20020a056a21780a00b0013ba129a6cdmr15625590pzc.58.1690994107361; Wed, 02 Aug 2023 09:35:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690994107; cv=none; d=google.com; s=arc-20160816; b=PcA1D16bmoxZ58XoYqhnR6c6JbY5mnbU4NQPoCmv/FWGpSONGlkK3iMfjNn1fN1jtU fcM8m8WjVbDrDoLi6NDMiONuC1/3sKT8cYUCZ4h6307CSztW3LHGhk6hQ7QnhQBJHWZ5 jiIBf3aq7ahgcMXQP62TB5nm6M+4bbxXmwAnXMSAoIoUm6SZSYHcG0m9TglmpW81/+gR mmmj8YGt9ll7cIJKjcfoJg9C+Y0pj054ZooExSDcIDChJ9A1ms4IA8GZeE9bVIZ0lBpN 5sh9Vc8qJyvNDd6xfVQ1ZeA2qylklrFPIHkTNjQheKjWCrm+eGMsjY53u+dcebshKuov FSMg== 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=zW8jY2pNK1XqhCVf9zwn8MoXVkfdk0NGUJ3de32botk=; fh=UoPnrMfWVKGOqsML3Cwqzr4dYAyNRBpPmY/ITnBmPi0=; b=FJJ/TYTv1lUsDPSrw0Ye6hNvm8f488NJkOofmfBJhcryg9CHMLP0CYuRlQrL540Pe/ msg0I2401rGUHYt0/7JA/SUSTAC0ztJf1xpdDB8vz66hDztIbfksWW83X3kgqzPgPL09 XOmk//TQjTmftAnjlhiUbjSPy0KHcXJBXAVrx+GQu+SacnDE1f/0Efiln6Ds0VSkKOjt 558MWMRprE57zkjIXeWY94GeIU/oHGXq0BDDZ6sX+sf1PmMESy6Gb9bTdSmFm5kE51Iw x4TjuShHqtBRKyfDo7dVptyWSFj6oto4fC4PuR8zs6LnQpV7lQQ6mZIFhb+hzlbK8phe clBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=mMijuPhO; 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 h21-20020a056a00219500b00687275f80e8si6669224pfi.235.2023.08.02.09.34.55; Wed, 02 Aug 2023 09:35: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=@kernel.org header.s=k20201202 header.b=mMijuPhO; 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 S235459AbjHBPwC (ORCPT + 99 others); Wed, 2 Aug 2023 11:52:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234316AbjHBPve (ORCPT ); Wed, 2 Aug 2023 11:51:34 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F63E1BF0; Wed, 2 Aug 2023 08:50:37 -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 739BB61A39; Wed, 2 Aug 2023 15:50:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F2541C433C8; Wed, 2 Aug 2023 15:50:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690991414; bh=6lDMuI/WecUFs3bb1WoCYz9wip7qMV2yUSkgF3fUv7Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mMijuPhOqIZ6nsBdxqlXLDDPc9OtnzBhH+efMVOS3wsNrah7qHfaZmDgNoQBl2J86 SKl3Syr+YeEW1q8gnAway2wZHB5AC3zlGBLiscxPNctafRy/b228HzkQybTp0XEjJd BfSbn74drV9fbUKB3/QvsSxd9lSetuufNcQv6kPqe9UBL+BXQo31mFtopI4R1kUX/b 7Koh+bTMkty1f09/VNt+KxUNsuRQNgVJ8drQKsBJ8p63SrcbPSnpKXKbhS/ac6cndS lEsbdJQaAo+tdet6IMVW1XdNe9NSH0FMagcxuHku5J8rztDwjrLMqS15RnMJs7tGe+ cVDXsRC1kkzIQ== 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 19/23] x86/decompressor: Move global symbol references to C code Date: Wed, 2 Aug 2023 17:48:27 +0200 Message-Id: <20230802154831.2147855-20-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=5127; i=ardb@kernel.org; h=from:subject; bh=6lDMuI/WecUFs3bb1WoCYz9wip7qMV2yUSkgF3fUv7Q=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIeVU1cnH8+au8V1Ypy66YnfgYsfn6kKz6rar7xBnnpsQo 7JmaWdcRykLgxgHg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZjI22sM/4ss2I1f3BZw8OGt jCm/svHgpCbOzx0B3jE//j6dc0cncDcjw4Kbi2/c+nPzfFZi+3OXGdcKL6zbdHDCpbW1clNLZs4 pZ+cHAA== 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 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 | 10 ++-------- arch/x86/boot/compressed/misc.c | 16 +++++++++------- 3 files changed, 11 insertions(+), 23 deletions(-) diff --git a/arch/x86/boot/compressed/head_32.S b/arch/x86/boot/compressed/head_32.S index 8876ffe30e9a4819..3af4a383615b3e1f 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 fb0e562c26f64c9c..28f46051c706724e 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -493,13 +493,9 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) /* * Do the extraction, and jump to the new kernel.. */ - /* pass struct boot_params pointer */ + /* pass struct boot_params pointer and output target address */ movq %r15, %rdi - 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 call extract_kernel /* returns kernel entry point in %rax */ /* @@ -657,8 +653,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 94b7abcf624b3b55..2d91d56b59e1af93 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_t(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)) -- 2.39.2