From patchwork Fri Apr 28 09:51:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hou Wenlong X-Patchwork-Id: 88555 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp816942vqo; Fri, 28 Apr 2023 03:03:18 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ77tOmF1K70HULHSxn8XHw7Y4CZFnsipUnGuWMUQkPKN0P8voJEUDKiYWCo59AzLU81RpAF X-Received: by 2002:a17:90a:eace:b0:23f:e4b7:afb3 with SMTP id ev14-20020a17090aeace00b0023fe4b7afb3mr4752251pjb.9.1682676198348; Fri, 28 Apr 2023 03:03:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682676198; cv=none; d=google.com; s=arc-20160816; b=DS2VGFP1TnpUpDerpoXdr+36rOPEL1F2Ssqd7FYjT5OrXTlRpukpXQFMgYDokYGKZX hW7qTuDEE1NVn4r5khLmJls+N3i9yKdsJJwgoWMUnogfQllnsRMomk0y8BQK+AuEpL4F f1CnblKH81XXMcgVy8AMPfnVymxIwDpQhCyTxZ3PR67vzD3iNXuuu62toE934OX3uyvr GJKzRqEdacYW5RTpFst0Hv72lYHFzHEDDpZyyLco13pC0ChMM2w7ci8D3nKM6uJhUny6 +tSZVsZtbjj1ZOzle0GvPr3HErhfUmFDOVDS9Kn506GT0g3xJrlLnZ2bdl8d93kTwYVt EKmw== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e125-20020a636983000000b00524f00fee68si16861896pgc.372.2023.04.28.03.03.04; Fri, 28 Apr 2023 03:03:18 -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 S1345977AbjD1J4Y (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 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?1764413876977116015?= X-GMAIL-MSGID: =?utf-8?q?1764413876977116015?= 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;