From patchwork Mon Apr 24 16:57:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 87113 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2882251vqo; Mon, 24 Apr 2023 10:00:06 -0700 (PDT) X-Google-Smtp-Source: AKy350YNiU5eqlEJDRW0VUg9qctvweEFCE08SvLGbW61yUhoI6FUdpmjXXzykEUXXYmVKatB6Bqc X-Received: by 2002:a17:90b:1494:b0:23c:fa83:2a7d with SMTP id js20-20020a17090b149400b0023cfa832a7dmr14893698pjb.12.1682355606585; Mon, 24 Apr 2023 10:00:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682355606; cv=none; d=google.com; s=arc-20160816; b=oLqlIgc4EKMOjAHuU76dElRrxsd9MfNCfJXkNe9os3ja0BgoYUK4s8moJ2XDNg9raW /w9i4+g+J62j/VP/Jm5bhIRGU1/XqAMUHwkLl9nCKqPd2NPlh0cp0IPtWReRt6gF+GlN 9dcOnE68TH5VBjXkWNI+tZCZlrvqc11vF6EUXPRK9NzmdfPAMY/R/ZM7Oq1lNjrZTJ8i p9opq9l04YBvHvqF1utZQ3bBzBVVEz2yQT/uITUxY430u6abrSrqmruJytQAd9e/KuQs 4qZ4YMGEjSnGQ4GBZcENeLpkufQwzZNoQvalPt170rc0OeDZ7ZkE2M/+vzC8ZK3ZmdMK NeRw== 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=8VEMeEsA7W90C2uTS/bSR/reNQk2pcEF8kw8oYHBfpw=; b=aR+44H5Ksw2eMspv16vmAH+HUrlnl+ygBdYrlm8+whqgZmlcXz0wJcCumjfXjC+Dzs kfVTiqVLKI6Sqdlqr+o2h2kK1eDZPCpYik0vPHMEYD1kSpQo2/VzZkX3RVutsB3lj2MC 3kPlAgVCVoAEdqiZhAtaC+0F/Umu3l0xwJ7w/x361i/fggrZpBb0Mm4xBGZ/TgLl15Rt lbs0cfxiqIu1nllbAz5GrmikI1d2dLKVYmuZnM1n0qJguHGqprkNmutHeHVqoLhL0uub fcX5PBrI05BglKhWcEQ+C4v0NJHPWBlaAHlw72J0ROmo9/Hte7pQGg1tV4eb1jTfXzBO bmWg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uNZinKz9; 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 jx8-20020a17090b46c800b002474c31b885si7402867pjb.78.2023.04.24.09.59.51; Mon, 24 Apr 2023 10:00:06 -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=uNZinKz9; 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 S232136AbjDXQ5v (ORCPT + 99 others); Mon, 24 Apr 2023 12:57:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231207AbjDXQ5t (ORCPT ); Mon, 24 Apr 2023 12:57:49 -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 4D9078683; Mon, 24 Apr 2023 09:57:42 -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 B358B6270A; Mon, 24 Apr 2023 16:57:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AFE33C433EF; Mon, 24 Apr 2023 16:57:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1682355461; bh=+4Gsdymv19vmn8hoQcaZQ5FlCwbe5D3nkuUdwGr3sXE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uNZinKz91CayYTEyw9M05IndlzScBNe9IHdWaBuTUc3XrNIwdt4ScWJPzPk0I0D1Z CbbNntlY3otWTJcGdkwxYDbWXCkJkViclstHuX7M0RvW48HxH5Il/SJv2tFsSqQ/VG d75UMGdOnvbwUs2bcWS/iP3ZhYNrIgden+SezaVvNJ6IbD1nyC7l+8kfng2eKKqKmk kKTUeSuJLXCyV77gfrWjZGS3Dhxz4kxPElOzITAyURC78JHOpOVBWnLgjl/JTOPbn+ rYxSyYmqbggVJNk7DkYHtv4ZzdjwgP0uw6c0+1MyCGGNiWsrAfXpjeQ197NmYT9qKj 5TzOUTWFD1BLw== 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 1/6] x86: decompressor: Move global symbol references to C code Date: Mon, 24 Apr 2023 18:57:21 +0200 Message-Id: <20230424165726.2245548-2-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230424165726.2245548-1-ardb@kernel.org> References: <20230424165726.2245548-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4975; i=ardb@kernel.org; h=from:subject; bh=+4Gsdymv19vmn8hoQcaZQ5FlCwbe5D3nkuUdwGr3sXE=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIcVty7uCizdqbotOvJN6kVXm2OHVuxKfH7FkND+V7al2/ tVR1jPFHaUsDGIcDLJiiiwCs/++23l6olSt8yxZmDmsTCBDGLg4BWAit9gZ/mcdc19hYbddda3N +wqJmjCWmXnx/L2sJaWpGYq3fDyvLwOqsLwuJRWX3cm4UC1YqnlWtK6E8Zne+fMVLNMSVt+0DeU GAA== X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Spam-Status: No, score=-4.6 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?1764077712610585589?= X-GMAIL-MSGID: =?utf-8?q?1764077712610585589?= We no longer have to be cautious when referring to global variables in the position independent decompressor code, now that we use PIE codegen and assert 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 we can simply refer to global variables directly, instead of passing them into C routines from asm code. Let's do so for the code that will be called directly from the EFI stub after a subsequent patch, and avoid the need to pass these quantities directly. Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/head_32.S | 8 -------- arch/x86/boot/compressed/head_64.S | 8 +------- arch/x86/boot/compressed/misc.c | 17 ++++++++++------- 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/arch/x86/boot/compressed/head_32.S b/arch/x86/boot/compressed/head_32.S index 987ae727cf9f0d04..3ecc1bbe971e1d43 100644 --- a/arch/x86/boot/compressed/head_32.S +++ b/arch/x86/boot/compressed/head_32.S @@ -179,13 +179,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 @@ -213,8 +207,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 03c4328a88cbd5d0..5ab014be179c1103 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -564,11 +564,7 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) */ pushq %rsi /* Save the real mode argument */ movq %rsi, %rdi /* real mode address */ - 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 /* output target address */ call extract_kernel /* returns kernel entry point in %rax */ popq %rsi @@ -726,8 +722,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 014ff222bf4b35c2..3b79667412ceb388 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,12 @@ 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 +415,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((unsigned long)output_len, kernel_total_size); #ifdef CONFIG_X86_64 needed_size = ALIGN(needed_size, MIN_KERNEL_ALIGN); #endif @@ -443,7 +446,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))