From patchwork Fri Apr 28 09:51:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hou Wenlong X-Patchwork-Id: 88578 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp824178vqo; Fri, 28 Apr 2023 03:17:23 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5HzkGmiVS/SetraPwcoW/b0Xw3T5rbDxo4yoAkpZNol4bLFM1BpFb7bpNM5SRcKpxTo+5O X-Received: by 2002:a05:6a21:398e:b0:f0:6aaf:1abf with SMTP id ad14-20020a056a21398e00b000f06aaf1abfmr5901916pzc.4.1682677043491; Fri, 28 Apr 2023 03:17:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682677043; cv=none; d=google.com; s=arc-20160816; b=nf4Pm9HK3tQjDbddv+8x83eN0FN4Z6W9EuYu68JbzDVn5rJSaGJt6gGG6/6K3Lg/pF RJu6slXCLRX1hO+NrbjFomgr7ZyzJatKEIxLdiqWXsyJIXprR8w+Fj7Y9WZchziIggvo FxnRS1WuAX4hRCGdvWzxUZ0IIFOZsKvBzPQEZfsE3q1JJp6gqKkJ/ieb+WmlJ0SOYtpO rhvRURxP5MLu+mzxQk9+sckqVwnk641Ml/OLPlUPZPHfSp/L/CFxEeehWQm9+jctb5zS SXNrNx0TNhQ5OiBWJ2epcvTwLg6jW4PNG+wj9KH+1P2ezFxHVt2WKy5ft6zpPg2x+XHX jrFQ== 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; bh=BpdtSE05DbOZZ0btNTmC6i8q/9pqNl0eZ1fSGDvAIQA=; b=dR1sdJ36jRMzfAZJ4QiokJYj3vj7EGQmZ+HXRlnxu2SFQJ2sH3g4t/C2zIkHyJwnMS Aq0jYEJi1fjCOPffOU10olbw05nwxUpzqWpAsFSSx/tSxRHXWdZ2bduH6JlFyjs5Id0A RYbjwrHtYlXE62PlQGTLyW5RYAD3/4xipzLbXdGuFXT1k+w3rf3sm8Q7zrm+HaD62VP4 5FVsOaxmt2dFpRkXWqIpZfyjGdw15MQ5uj6XgSkJrMRd36r1cumXNts+jYVXyR8jHpNX JU1+TOW8n5G8nkQpUbsm8RxQ/Jc5m+RZ/Xk3gEuOS/Y5SUQDh8KUIgTaabia/lku5C+e 1s8w== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=antgroup.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c25-20020a631c19000000b00503355255cdsi16278307pgc.488.2023.04.28.03.17.08; Fri, 28 Apr 2023 03:17:23 -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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=antgroup.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345863AbjD1KB5 (ORCPT + 99 others); Fri, 28 Apr 2023 06:01:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345847AbjD1KBc (ORCPT ); Fri, 28 Apr 2023 06:01:32 -0400 Received: from out187-3.us.a.mail.aliyun.com (out187-3.us.a.mail.aliyun.com [47.90.187.3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9875944AB for ; Fri, 28 Apr 2023 03:01:06 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R201e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018047198;MF=houwenlong.hwl@antgroup.com;NM=1;PH=DS;RN=16;SR=0;TI=SMTPD_---.STDfsHv_1682675644; Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.STDfsHv_1682675644) by smtp.aliyun-inc.com; Fri, 28 Apr 2023 17:54:05 +0800 From: "Hou Wenlong" To: linux-kernel@vger.kernel.org Cc: "Thomas Garnier" , "Lai Jiangshan" , "Kees Cook" , "Hou Wenlong" , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "Dave Hansen" , , "H. Peter Anvin" , "Juergen Gross" , "Anshuman Khandual" , "Mike Rapoport" , "Josh Poimboeuf" , "Pasha Tatashin" Subject: [PATCH RFC 40/43] x86/boot: Fill kernel image puds dynamically Date: Fri, 28 Apr 2023 17:51:20 +0800 Message-Id: X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY 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?1764414763318524115?= X-GMAIL-MSGID: =?utf-8?q?1764414763318524115?= For PIE kernel, it could be randomized in any address. Later, kernel image would be moved down the top 2G, so fille kernel image puds dynamically. Signed-off-by: Hou Wenlong Cc: Thomas Garnier a Cc: Lai Jiangshan Cc: Kees Cook --- arch/x86/kernel/head64.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c index 8295b547b64f..c5cd61aab8ae 100644 --- a/arch/x86/kernel/head64.c +++ b/arch/x86/kernel/head64.c @@ -239,8 +239,18 @@ unsigned long __head __startup_64(unsigned long physaddr, } pud = fixup_pointer(&level3_kernel_pgt, physaddr); - pud[510] += load_delta; - pud[511] += load_delta; + if (IS_ENABLED(CONFIG_X86_PIE)) { + pud[510] = 0; + pud[511] = 0; + + i = pud_index(text_base); + pgtable_flags = _KERNPG_TABLE_NOENC - __START_KERNEL_map + load_delta; + pud[i] = pgtable_flags + SYM_ABS_VAL(level2_kernel_pgt); + pud[i + 1] = pgtable_flags + SYM_ABS_VAL(level2_fixmap_pgt); + } else { + pud[510] += load_delta; + pud[511] += load_delta; + } pmd = fixup_pointer(level2_fixmap_pgt, physaddr); for (i = FIXMAP_PMD_TOP; i > FIXMAP_PMD_TOP - FIXMAP_PMD_NUM; i--)