From patchwork Thu Mar 14 12:53:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pingfan Liu X-Patchwork-Id: 13592415 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 07429C54E68 for ; Thu, 14 Mar 2024 12:54:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=cvxd7N8FLEZMXVqF7xjTH6/Gdjgren7Ntmp58TDoTWo=; b=f8AH/b9E7v4LVt jxGspxMMKBftjZQFwAUMR+SbqCc16HQsTA8zCkPKh8R5Vel+QR9lTJD+J59+fjwObQshj1rsgarCP AlncJ+XjBuRPQnfnuHJgRbSxezbNtu1cx2fItgey6y4OCTqDV1Ixd9xzurVDk/zUbtKsx6/b9kTAk FOCdLosv8zkrXx3pwCn9y3kXXZyJ75uaMN5J74jVCOYJFwpjWhWp07DJGJfRIDx+ixTCL5a69nCCh qOmCg2niaUxJFlgFxHHnwv0+VRJUVjbx+f/nm3ldMhE9dMb3i/WPde8IW8zoYqBMPLuAxATYObyFO CeOTCRn/ajIjbK0xuIqw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rkkak-0000000EJJf-3zxG; Thu, 14 Mar 2024 12:53:50 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rkkai-0000000EJJD-31Yo for linux-arm-kernel@lists.infradead.org; Thu, 14 Mar 2024 12:53:49 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710420827; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=kKrV8uzPkOlFKp2VOfzqHtBUMwlPoag98YPSWNg6rps=; b=YRPa9B74k9eWVYWBARPqyLvY9o67fR967/huO4qhsWDv2GjW5S4KvXa1F3JFbXn7kjufEs 5hKWLT6z55vW1BqFMBNM4Ohosa9ulA+o1MarbNmWwcnk8G5zcKzs+PsMU9ACtYUo9RZDuV XhqLXuIWuxTWIH6XY1q7t1mk0nD6eOw= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-100-hjrIphnTNbqKLhAYPVKX3g-1; Thu, 14 Mar 2024 08:53:44 -0400 X-MC-Unique: hjrIphnTNbqKLhAYPVKX3g-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BDCFA101A526; Thu, 14 Mar 2024 12:53:43 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.72.112.98]) by smtp.corp.redhat.com (Postfix) with ESMTP id 301602024517; Thu, 14 Mar 2024 12:53:40 +0000 (UTC) From: Pingfan Liu To: linux-arm-kernel@lists.infradead.org Cc: Pingfan Liu , Catalin Marinas , Will Deacon , Ard Biesheuvel , Kees Cook Subject: [PATCH] arm64: mm: Pass physical address explicitly in map_range Date: Thu, 14 Mar 2024 20:53:29 +0800 Message-ID: <20240314125331.24082-1-piliu@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240314_055348_859826_56633548 X-CRM114-Status: GOOD ( 12.64 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This patch is not a fix, just a improvement in code reading. At the present, the deduction of a symbol's physical address hides in the compiler's trick. Introduce a function paddr(), which make the process explicitly at the sacrifice of one sub and one add instruction. Signed-off-by: Pingfan Liu Cc: Catalin Marinas Cc: Will Deacon Cc: Ard Biesheuvel Cc: Kees Cook To: linux-arm-kernel@lists.infradead.org --- arch/arm64/kernel/pi/map_kernel.c | 30 +++++++++++++++--------------- arch/arm64/kernel/pi/map_range.c | 21 +++++++++++++++++++-- arch/arm64/kernel/pi/pi.h | 1 + 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/arch/arm64/kernel/pi/map_kernel.c b/arch/arm64/kernel/pi/map_kernel.c index cac1e1f63c44..f0bad9ff3cce 100644 --- a/arch/arm64/kernel/pi/map_kernel.c +++ b/arch/arm64/kernel/pi/map_kernel.c @@ -78,16 +78,16 @@ static void __init map_kernel(u64 kaslr_offset, u64 va_offset, int root_level) twopass |= enable_scs; prot = twopass ? data_prot : text_prot; - map_segment(init_pg_dir, &pgdp, va_offset, _stext, _etext, prot, + map_segment(init_pg_dir, &pgdp, va_offset, paddr(_stext), paddr(_etext), prot, !twopass, root_level); - map_segment(init_pg_dir, &pgdp, va_offset, __start_rodata, - __inittext_begin, data_prot, false, root_level); - map_segment(init_pg_dir, &pgdp, va_offset, __inittext_begin, - __inittext_end, prot, false, root_level); - map_segment(init_pg_dir, &pgdp, va_offset, __initdata_begin, - __initdata_end, data_prot, false, root_level); - map_segment(init_pg_dir, &pgdp, va_offset, _data, _end, data_prot, - true, root_level); + map_segment(init_pg_dir, &pgdp, va_offset, paddr(__start_rodata), + paddr(__inittext_begin), data_prot, false, root_level); + map_segment(init_pg_dir, &pgdp, va_offset, paddr(__inittext_begin), + paddr(__inittext_end), prot, false, root_level); + map_segment(init_pg_dir, &pgdp, va_offset, paddr(__initdata_begin), + paddr(__initdata_end), data_prot, false, root_level); + map_segment(init_pg_dir, &pgdp, va_offset, paddr(_data), paddr(_end), + data_prot, true, root_level); dsb(ishst); idmap_cpu_replace_ttbr1(init_pg_dir); @@ -109,7 +109,7 @@ static void __init map_kernel(u64 kaslr_offset, u64 va_offset, int root_level) * potential TLB conflicts when creating the contiguous * descriptors. */ - unmap_segment(init_pg_dir, va_offset, _stext, _etext, + unmap_segment(init_pg_dir, va_offset, paddr(_stext), paddr(_etext), root_level); dsb(ishst); isb(); @@ -120,10 +120,10 @@ static void __init map_kernel(u64 kaslr_offset, u64 va_offset, int root_level) * Remap these segments with different permissions * No new page table allocations should be needed */ - map_segment(init_pg_dir, NULL, va_offset, _stext, _etext, - text_prot, true, root_level); - map_segment(init_pg_dir, NULL, va_offset, __inittext_begin, - __inittext_end, text_prot, false, root_level); + map_segment(init_pg_dir, NULL, va_offset, paddr(_stext), + paddr(_etext), text_prot, true, root_level); + map_segment(init_pg_dir, NULL, va_offset, paddr(__inittext_begin), + paddr(__inittext_end), text_prot, false, root_level); } /* Copy the root page table to its final location */ @@ -223,7 +223,7 @@ static void __init map_fdt(u64 fdt) asmlinkage void __init early_map_kernel(u64 boot_status, void *fdt) { static char const chosen_str[] __initconst = "/chosen"; - u64 va_base, pa_base = (u64)&_text; + u64 va_base, pa_base = paddr(_text); u64 kaslr_offset = pa_base % MIN_KIMG_ALIGN; int root_level = 4 - CONFIG_PGTABLE_LEVELS; int va_bits = VA_BITS; diff --git a/arch/arm64/kernel/pi/map_range.c b/arch/arm64/kernel/pi/map_range.c index 5410b2cac590..b5a68dcf3cf5 100644 --- a/arch/arm64/kernel/pi/map_range.c +++ b/arch/arm64/kernel/pi/map_range.c @@ -87,6 +87,23 @@ void __init map_range(u64 *pte, u64 start, u64 end, u64 pa, pgprot_t prot, } } +u64 __init paddr(char *symbol) +{ + u64 _text_paddr; + u64 delta; + + asm volatile( + "adrp %0, _text;" + "add %0, %0, #:lo12:_text;" + : "=r" (_text_paddr) + : + : + ); + + delta = symbol - _text; + return _text_paddr + delta; +} + asmlinkage u64 __init create_init_idmap(pgd_t *pg_dir, pteval_t clrmask) { u64 ptep = (u64)pg_dir + PAGE_SIZE; @@ -96,9 +113,9 @@ asmlinkage u64 __init create_init_idmap(pgd_t *pg_dir, pteval_t clrmask) pgprot_val(text_prot) &= ~clrmask; pgprot_val(data_prot) &= ~clrmask; - map_range(&ptep, (u64)_stext, (u64)__initdata_begin, (u64)_stext, + map_range(&ptep, paddr(_stext), paddr(__initdata_begin), paddr(_stext), text_prot, IDMAP_ROOT_LEVEL, (pte_t *)pg_dir, false, 0); - map_range(&ptep, (u64)__initdata_begin, (u64)_end, (u64)__initdata_begin, + map_range(&ptep, paddr(__initdata_begin), paddr(_end), paddr(__initdata_begin), data_prot, IDMAP_ROOT_LEVEL, (pte_t *)pg_dir, false, 0); return ptep; diff --git a/arch/arm64/kernel/pi/pi.h b/arch/arm64/kernel/pi/pi.h index c91e5e965cd3..7ffba712da06 100644 --- a/arch/arm64/kernel/pi/pi.h +++ b/arch/arm64/kernel/pi/pi.h @@ -28,6 +28,7 @@ u64 kaslr_early_init(void *fdt, int chosen); void relocate_kernel(u64 offset); int scs_patch(const u8 eh_frame[], int size); +u64 paddr(char *symbol); void map_range(u64 *pgd, u64 start, u64 end, u64 pa, pgprot_t prot, int level, pte_t *tbl, bool may_use_cont, u64 va_offset);