Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1862250rwr; Fri, 28 Apr 2023 02:56:55 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4UM2TxNcqp+/4iSP+PhG7Xn/eXjxv+hrBxYjD1rkhzpPdmr4+MKPnv3NVKuvndX72/CnSC X-Received: by 2002:a05:6a20:4426:b0:f3:2d33:de79 with SMTP id ce38-20020a056a20442600b000f32d33de79mr5599007pzb.43.1682675815266; Fri, 28 Apr 2023 02:56:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682675815; cv=none; d=google.com; s=arc-20160816; b=cu08O31453PlpCxZCxnzGf0++VAk1/RFnPGfcRUPMJ75XyKAgkhMisbxgPs8+Zsnny dej8ovUDWjseK2xOr/oNrrZo4Mvjlk141v6lmXJYWMu0mMSrfprwBQ+Z3VGawkiE8Eo0 i0324xk6gcB53nCOiz8dAnxlkkfK64p4mo1oxD+jPajg20D+1nQUy73EPVAETbEWCZmB AS3hCE7OR67VflXVnJMQpl29Qhv7EEfpLmf+C3z/A3mA4fcFtJ6yQePUGmDdX1dGay3r ymt9nGUZHoq6Y5NolKGniNAQ3tiLHTnHX9NoN+qeRlqkrbKnSDxMhrI5ida0RkEUkoKt s4Lw== 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=t19rlhQukHy3BmESCo4nd5Es4b9/Y53s54s79z1+Xic=; b=rsCPmOL++tfk8LodXaF6TmUVljawqs3YoHGQQcTqqYf3ffuG0mLDNovJ83guAs9oLI W6shMc5p6NrU0n+oEQD6VHKQfwji1vMGVATyzvlPYhqKNBp0k9uQYTh7ab9POdtAgARE 2HGdieT9o7oSua/ad40dTKQZbdjLK3ZFtjR8hElh/7BkG6yi3kRel1/jdJaXg5B7hK65 gq5IN3/z8DvIKpT+KWYqK5CgyVRZbHYl3JmI0SGoyK8p00TzfdoZwYpq2LsjzjciJbFV hFOwqN+QJ1jKdaVkOgvz4PeXl7Rvs1v3YoPgykEEWGev/9pF3A6h0cDxPsFGzVfg0cr8 bWug== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s19-20020a632c13000000b00528c16e9283si458494pgs.92.2023.04.28.02.56.44; Fri, 28 Apr 2023 02:56:55 -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 S1345976AbjD1J4Y (ORCPT + 99 others); Fri, 28 Apr 2023 05:56:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345838AbjD1Jzo (ORCPT ); Fri, 28 Apr 2023 05:55:44 -0400 Received: from out187-18.us.a.mail.aliyun.com (out187-18.us.a.mail.aliyun.com [47.90.187.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED3095BBC for ; Fri, 28 Apr 2023 02:55:12 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R271e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018047213;MF=houwenlong.hwl@antgroup.com;NM=1;PH=DS;RN=16;SR=0;TI=SMTPD_---.STFoGcy_1682675612; Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.STFoGcy_1682675612) by smtp.aliyun-inc.com; Fri, 28 Apr 2023 17:53:33 +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 32/43] x86/boot/64: Use data relocation to get absloute address when PIE is enabled Date: Fri, 28 Apr 2023 17:51:12 +0800 Message-Id: X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 When PIE is enabled, all symbol references are RIP-relative, so there is no need to fixup global symbol references when in low address. However, in order to acquire absloute virtual address of symbol, introduce a macro to use data relocation to get it. Suggested-by: Lai Jiangshan Signed-off-by: Hou Wenlong Cc: Thomas Garnier Cc: Kees Cook --- arch/x86/kernel/head64.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c index 49f7629b17f7..ef7ad96f2154 100644 --- a/arch/x86/kernel/head64.c +++ b/arch/x86/kernel/head64.c @@ -86,10 +86,22 @@ static struct desc_ptr startup_gdt_descr = { #define __head __section(".head.text") +#ifdef CONFIG_X86_PIE +#define SYM_ABS_VAL(sym) \ + ({ static unsigned long __initdata __##sym = (unsigned long)sym; __##sym; }) + +static void __head *fixup_pointer(void *ptr, unsigned long physaddr) +{ + return ptr; +} +#else +#define SYM_ABS_VAL(sym) ((unsigned long)sym) + static void __head *fixup_pointer(void *ptr, unsigned long physaddr) { return ptr - (void *)_text + (void *)physaddr; } +#endif /* CONFIG_X86_PIE */ static unsigned long __head *fixup_long(void *ptr, unsigned long physaddr) { @@ -142,8 +154,8 @@ static unsigned long __head sme_postprocess_startup(struct boot_params *bp, pmdv * attribute. */ if (sme_get_me_mask()) { - vaddr = (unsigned long)__start_bss_decrypted; - vaddr_end = (unsigned long)__end_bss_decrypted; + vaddr = SYM_ABS_VAL(__start_bss_decrypted); + vaddr_end = SYM_ABS_VAL(__end_bss_decrypted); for (; vaddr < vaddr_end; vaddr += PMD_SIZE) { /* @@ -189,6 +201,8 @@ unsigned long __head __startup_64(unsigned long physaddr, bool la57; int i; unsigned int *next_pgt_ptr; + unsigned long text_base = SYM_ABS_VAL(_text); + unsigned long end_base = SYM_ABS_VAL(_end); la57 = check_la57_support(physaddr); @@ -200,7 +214,7 @@ unsigned long __head __startup_64(unsigned long physaddr, * Compute the delta between the address I am compiled to run at * and the address I am actually running at. */ - load_delta = physaddr - (unsigned long)(_text - __START_KERNEL_map); + load_delta = physaddr - (text_base - __START_KERNEL_map); /* Is the address not 2M aligned? */ if (load_delta & ~PMD_MASK) @@ -214,9 +228,9 @@ unsigned long __head __startup_64(unsigned long physaddr, pgd = fixup_pointer(&early_top_pgt, physaddr); p = pgd + pgd_index(__START_KERNEL_map); if (la57) - *p = (unsigned long)level4_kernel_pgt; + *p = SYM_ABS_VAL(level4_kernel_pgt); else - *p = (unsigned long)level3_kernel_pgt; + *p = SYM_ABS_VAL(level3_kernel_pgt); *p += _PAGE_TABLE_NOENC - __START_KERNEL_map + load_delta; if (la57) { @@ -273,7 +287,7 @@ unsigned long __head __startup_64(unsigned long physaddr, pmd_entry += sme_get_me_mask(); pmd_entry += physaddr; - for (i = 0; i < DIV_ROUND_UP(_end - _text, PMD_SIZE); i++) { + for (i = 0; i < DIV_ROUND_UP(end_base - text_base, PMD_SIZE); i++) { int idx = i + (physaddr >> PMD_SHIFT); pmd[idx % PTRS_PER_PMD] = pmd_entry + i * PMD_SIZE; @@ -298,11 +312,11 @@ unsigned long __head __startup_64(unsigned long physaddr, pmd = fixup_pointer(level2_kernel_pgt, physaddr); /* invalidate pages before the kernel image */ - for (i = 0; i < pmd_index((unsigned long)_text); i++) + for (i = 0; i < pmd_index(text_base); i++) pmd[i] &= ~_PAGE_PRESENT; /* fixup pages that are part of the kernel image */ - for (; i <= pmd_index((unsigned long)_end); i++) + for (; i <= pmd_index(end_base); i++) if (pmd[i] & _PAGE_PRESENT) pmd[i] += load_delta; -- 2.31.1