From patchwork Tue Oct 25 14:12:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evgeniy Baskov X-Patchwork-Id: 10823 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1033155wru; Tue, 25 Oct 2022 07:15:32 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5kWDHY7wcHFsODHJU/8ruwVAZ/42yJIU4uhY2ifj1fsEh56Cp+xaF3uhhM3EbZrufMowuh X-Received: by 2002:a17:90b:691:b0:20d:6171:c94 with SMTP id m17-20020a17090b069100b0020d61710c94mr78135313pjz.25.1666707331863; Tue, 25 Oct 2022 07:15:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666707331; cv=none; d=google.com; s=arc-20160816; b=zEiZohw8S7xi06hDtTM4IIcj6gFM7v51jg6EdTlG3vg8h77htBdb8SVh0tRDnINYNZ QzuoJ0AaO3xekDJ3O18fxhQLKzUhd8hmjfuSopSbWUboIoU6jstttU+dhrnASy9RxEvq 9xVfGiJAmUpk1i9Ghd2GMyToJtkwxNmfe0HGUzCRW+0tVWKu6JHS5BUSIU6bCP6s1vju I3BGUKKZQ9IAxU1yEo2BGuFQ4tGwh2x4GehzQP8YCR6f7xZpSdeL/HgMRh6lgPwFUYTm rcs5ubXoeJPG0DsCi7Tl4qgtDCzgvY3e8OSHPJ55Fe6EQfLnQ10R9QcQXjvODqHj+J8q w0Cg== 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:dkim-filter; bh=DZTrbaBaHe77iGD0+HZCDY+dD1tpBzVWypKX0yhMApE=; b=omkQMf35dZJEvXAsRnvG8knTQ3XR5Z0+8vjva0q/M/gOhRmPZqtistN9Skk2udCK36 bgPIA7fIPUnsvpm5wowdYaD3yXY376K9Q4831/spYNkqEcxbO7suV9HtBzIdKT0/n965 LxODKo1Rpl6TgY1/lfU6LBO/OYRiZEu0mQplqavGlYpt1M2SPaRtmOp3tJbG8XnsGCjh zStxg9CCRmo+UVOq5Q896dLJKwG+4yaeI1GEoeTRD3K7OFUus/Zj2XfDim1l0BDTG2zR eyrARBSjgsx3nviEE1bjp/ID+rY8x9Fy2O/UPbgvF+NMUj/s6XoV7nxR+M34iismhElT OHyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ispras.ru header.s=default header.b=hSA4oTac; 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=ispras.ru Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k6-20020a056a00168600b005676e2c36b4si3787188pfc.47.2022.10.25.07.15.18; Tue, 25 Oct 2022 07:15:31 -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=@ispras.ru header.s=default header.b=hSA4oTac; 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=ispras.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233227AbiJYOOa (ORCPT + 99 others); Tue, 25 Oct 2022 10:14:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233052AbiJYONv (ORCPT ); Tue, 25 Oct 2022 10:13:51 -0400 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2D51A4853; Tue, 25 Oct 2022 07:13:49 -0700 (PDT) Received: from localhost.localdomain (unknown [83.149.199.65]) by mail.ispras.ru (Postfix) with ESMTPSA id 4832640737AC; Tue, 25 Oct 2022 14:13:48 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.ispras.ru 4832640737AC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ispras.ru; s=default; t=1666707228; bh=DZTrbaBaHe77iGD0+HZCDY+dD1tpBzVWypKX0yhMApE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hSA4oTacdmboTGxjLXOr5oGkOsiMFnT3xJPfu/VNlPtW7JIOXZMeMgWWHF0EgbLVW D5UuKr7CK2Q4bYnegBpFEjz0ocLVEntGoQSj33IHkzhtpWZxhwb7DWXd69Xz/sPf7t 6n8NSPXbDeCVRIXbPDY2qQ2kgLdqTk2er3PdiL3E= From: Evgeniy Baskov To: Ard Biesheuvel Cc: Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , lvc-project@linuxtesting.org, x86@kernel.org, linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v2 12/23] x86/boot: Make kernel_add_identity_map() a pointer Date: Tue, 25 Oct 2022 17:12:50 +0300 Message-Id: X-Mailer: git-send-email 2.37.4 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED 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?1747669307386471774?= X-GMAIL-MSGID: =?utf-8?q?1747669307386471774?= Convert kernel_add_identity_map() into a function pointer to be able to provide alternative implementations of this function. Required to enable calling the code using this function from EFI environment. Signed-off-by: Evgeniy Baskov --- arch/x86/boot/compressed/ident_map_64.c | 7 ++++--- arch/x86/boot/compressed/misc.c | 24 ++++++++++++++++++++++++ arch/x86/boot/compressed/misc.h | 15 +++------------ 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/arch/x86/boot/compressed/ident_map_64.c b/arch/x86/boot/compressed/ident_map_64.c index ba5108c58a4e..1aee524d3c2b 100644 --- a/arch/x86/boot/compressed/ident_map_64.c +++ b/arch/x86/boot/compressed/ident_map_64.c @@ -92,9 +92,9 @@ bool has_nx; /* set in head_64.S */ /* * Adds the specified range to the identity mappings. */ -unsigned long kernel_add_identity_map(unsigned long start, - unsigned long end, - unsigned int flags) +unsigned long kernel_add_identity_map_(unsigned long start, + unsigned long end, + unsigned int flags) { int ret; @@ -142,6 +142,7 @@ void initialize_identity_maps(void *rmode) struct setup_data *sd; boot_params = rmode; + kernel_add_identity_map = kernel_add_identity_map_; /* Exclude the encryption mask from __PHYSICAL_MASK */ physical_mask &= ~sme_me_mask; diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/misc.c index aa4a22bc9cf9..c9c235d65d16 100644 --- a/arch/x86/boot/compressed/misc.c +++ b/arch/x86/boot/compressed/misc.c @@ -275,6 +275,22 @@ static void parse_elf(void *output, unsigned long output_len, free(phdrs); } +/* + * This points to actual implementation of mapping function + * for current environment: either EFI API wrapper, + * own implementation or dummy implementation below. + */ +unsigned long (*kernel_add_identity_map)(unsigned long start, + unsigned long end, + unsigned int flags); + +static inline unsigned long kernel_add_identity_map_dummy(unsigned long start, + unsigned long end, + unsigned int flags) +{ + return start; +} + /* * 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 @@ -312,6 +328,14 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap, init_default_io_ops(); + /* + * On 64-bit this pointer is set during page table uninitialization, + * but on 32-bit it remains uninitialized, since paging is disabled. + */ + if (IS_ENABLED(CONFIG_X86_32)) + kernel_add_identity_map = kernel_add_identity_map_dummy; + + /* * Detect TDX guest environment. * diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h index 38d31bec062d..0076b2845b4b 100644 --- a/arch/x86/boot/compressed/misc.h +++ b/arch/x86/boot/compressed/misc.h @@ -180,18 +180,9 @@ static inline int count_immovable_mem_regions(void) { return 0; } #ifdef CONFIG_X86_5LEVEL extern unsigned int __pgtable_l5_enabled, pgdir_shift, ptrs_per_p4d; #endif -#ifdef CONFIG_X86_64 -extern unsigned long kernel_add_identity_map(unsigned long start, - unsigned long end, - unsigned int flags); -#else -static inline unsigned long kernel_add_identity_map(unsigned long start, - unsigned long end, - unsigned int flags) -{ - return start; -} -#endif +extern unsigned long (*kernel_add_identity_map)(unsigned long start, + unsigned long end, + unsigned int flags); /* Used by PAGE_KERN* macros: */ extern pteval_t __default_kernel_pte_mask;