From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 03AF0606A31A for ; Tue, 1 Dec 2026 01:54:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=eVLblwLJ; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Ehq8nHEg DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=ayz47azVGWN4EaaBB79gwMVNfchWUuXBS2BBWG9eDOU=; b=eVLblwLJ+odvK8 DXITgPZupTjrPISnRV1zqM2ewk8ApZ+/b6DxCwRyHfBkC/tzpjMd5mg8hJ7GHwxWsrnmP55qOyp48 CVBBtefSNb6l+1DlovvLYOUk7IFd43ByQdV00e0D3wUlTdSObdL0wNSzGSIKwxrr0Sxq2BqyyI6Dw uNTx/8OiGnnHVh4VhbE0cjy8h/ByBnWnTBPgu+XlkYNCky74o9v1ro6dtWYuw/WokbtdXKSbsnQ7g ggdMWOtlyeoxrzVIzK5RGjXCGcqEXAVrjpecakx5MemI3KXqWXrUPP3E4EIVVzsj8erDI5t2oubRu O9vR5qRt2k40hZSV2mrA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYD4M-00000006EVj-28nL; Tue, 01 Dec 2026 01:54:10 +0000 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYD3o-00000006DtT-3kkW for linux-arm-kernel@lists.infradead.org; Tue, 01 Dec 2026 01:53:41 +0000 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-4bb64ef5a00so91208025e9.3 for ; Mon, 30 Nov 2026 17:53:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796090014; x=1796694814; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=ayz47azVGWN4EaaBB79gwMVNfchWUuXBS2BBWG9eDOU=; b=Ehq8nHEg48NnF8UoSk/coAWeiXaBxpWdroqfH9d0vjkz/sdO+QyZw4RIrVWPd7mPWc WC+EAn8JKhE5n5GHRZjMlr15hIAQ9owRRMz7yoEBmNg563VCGDLUXbsU32B+493Y/49J o9goJRX0bHdugnShaSGjRUUnD4lXvMTITaP1qq47BVKRtjLv7/tKUb9yKL/uccc532OE 907fqTa1rtwbeaRArU2gaPqX0KmUMkxOoIvN1dnSRCZ7LfxEahODb+C+/l/2uNSWDbEN UcTcfExdTe4zyc+Bc815Irt5Z2cM2pYPE4a4hL2cv0AANBe2mvw2AFbGpB4pkCrd1+ni wZMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796090014; x=1796694814; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=ayz47azVGWN4EaaBB79gwMVNfchWUuXBS2BBWG9eDOU=; b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nk3HkpZWc7VI7bB6r /qDYKn1wK56/09YWzGi1JqsHnm2751+pSuQ5Fj7iUjQ50M0Nry0+ArjCGsQ= X-Received: by 2002:a05:600c:5249:b0:4bb:665c:a078 with SMTP id 5b1f17b1804b1-4bb8893a89bmr713252255e9.28.1796090014029; Mon, 30 Nov 2026 17:53:34 -0800 (PST) Received: from NULL.Home.net ([2001:8a0:7280:4000:12d:c3b9:cecc:8966]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb884911e3sm1199037055e9.6.2026.11.30.17.53.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 17:53:33 -0800 (PST) From: Eric Curtin To: linux-arm-kernel@lists.infradead.org Cc: Ryan Roberts , Catalin Marinas , Will Deacon , Mark Rutland , Marc Zyngier , Oliver Upton , Fuad Tabba , Joey Gouly , Steffen Eiden , Suzuki K Poulose , Zenghui Yu , kvmarm@lists.linux.dev Subject: [RFC PATCH v2 29/43] arm64: Look up the page size at runtime in assembly code Date: Tue, 1 Dec 2026 01:52:01 +0000 Message-ID: <20261201015240.1017259-30-ericcurtin17@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261201015240.1017259-1-ericcurtin17@gmail.com> References: <20261201015240.1017259-1-ericcurtin17@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261130_175337_054668_DEBCC9E6 X-CRM114-Status: GOOD ( 21.60 ) X-Spam-Score: -1.8 (-) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Ryan Roberts PAGE_SHIFT, PAGE_SIZE and PAGE_MASK are not compile-time constants with boot-time page size selection, so they can no longer be used in assembly. Add get_page_size and get_page_mask macros which derive the value from TCR_EL1.TG0, and use them in copy_page, the MTE page tag helpers in mte.S, swsusp_arch_suspend_exit and arm64_relocate_new_kernel. The lookup is done outside the loops, so the cost is a few extra instructions per call. The copy_page macro takes an additional temporary register to hold the page mask. Two cases need different handling: - __pi_copy_page has no spare register for the page size and should not reload it on every iteration. Its loop body is now a macro that is instantiated once for each page size, and the function branches to the right copy at entry based on TG0. - invalid_host_el2_vect has no spare register and needs the page shift as an immediate, so with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE it selects the shift with alternatives based on ARM64_USE_PAGE_SIZE_*K. That code cannot run before alternatives have been applied. Signed-off-by: Ryan Roberts Signed-off-by: Eric Curtin (cherry picked from commit 6e16e3bbf859c1da94ba9a3247514132994a5195) --- arch/arm64/include/asm/assembler.h | 18 +++++++-- arch/arm64/kernel/hibernate-asm.S | 6 ++- arch/arm64/kernel/relocate_kernel.S | 10 +++-- arch/arm64/kvm/hyp/nvhe/host.S | 12 ++++++ arch/arm64/lib/copy_page.S | 59 ++++++++++++++++++----------- arch/arm64/lib/mte.S | 27 ++++++++++--- 6 files changed, 96 insertions(+), 36 deletions(-) diff --git a/arch/arm64/include/asm/assembler.h b/arch/arm64/include/asm/assembler.h index fc9659c64848..9ee11672d192 100644 --- a/arch/arm64/include/asm/assembler.h +++ b/arch/arm64/include/asm/assembler.h @@ -491,9 +491,11 @@ alternative_else_nop_endif .Lskip_\@: .endm /* - * copy_page - copy src to dest using temp registers t1-t8 + * copy_page - copy src to dest using temp registers t1-t9 */ - .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req + .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req t9:req + get_page_size \t9 + sub \t9, \t9, #1 // (PAGE_SIZE - 1) in \t9 9998: ldp \t1, \t2, [\src] ldp \t3, \t4, [\src, #16] ldp \t5, \t6, [\src, #32] @@ -504,7 +506,7 @@ alternative_else_nop_endif stnp \t5, \t6, [\dest, #32] stnp \t7, \t8, [\dest, #48] add \dest, \dest, #64 - tst \src, #(PAGE_SIZE - 1) + tst \src, \t9 b.ne 9998b .endm @@ -885,4 +887,14 @@ alternative_cb_end .macro tgran_lpa2, val, tg0 value_for_page_size \val, \tg0, ID_AA64MMFR0_EL1_TGRAN4_52_BIT, ID_AA64MMFR0_EL1_TGRAN16_52_BIT, -1 .endm + + .macro get_page_size, val + get_tg0 \val + value_for_page_size \val, \val, SZ_4K, SZ_16K, SZ_64K + .endm + + .macro get_page_mask, val + get_tg0 \val + value_for_page_size \val, \val, (~(SZ_4K-1)), (~(SZ_16K-1)), (~(SZ_64K-1)) + .endm #endif /* __ASM_ASSEMBLER_H */ diff --git a/arch/arm64/kernel/hibernate-asm.S b/arch/arm64/kernel/hibernate-asm.S index 2baefe7a82d3..556a7ecd3575 100644 --- a/arch/arm64/kernel/hibernate-asm.S +++ b/arch/arm64/kernel/hibernate-asm.S @@ -57,6 +57,8 @@ SYM_CODE_START(swsusp_arch_suspend_exit) mov x24, x4 mov x25, x5 + get_page_size x12 + /* walk the restore_pblist and use copy_page() to over-write memory */ mov x19, x3 @@ -64,9 +66,9 @@ SYM_CODE_START(swsusp_arch_suspend_exit) mov x0, x10 ldr x1, [x19, #HIBERN_PBE_ADDR] - copy_page x0, x1, x2, x3, x4, x5, x6, x7, x8, x9 + copy_page x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x11 - add x1, x10, #PAGE_SIZE + add x1, x10, x12 /* Clean the copied page to PoU - based on caches_clean_inval_pou() */ raw_dcache_line_size x2, x3 sub x3, x2, #1 diff --git a/arch/arm64/kernel/relocate_kernel.S b/arch/arm64/kernel/relocate_kernel.S index 064db5856bba..3ffb5682ee83 100644 --- a/arch/arm64/kernel/relocate_kernel.S +++ b/arch/arm64/kernel/relocate_kernel.S @@ -49,6 +49,10 @@ SYM_TYPED_FUNC_START(arm64_relocate_new_kernel) ldr x27, [x0, #KIMAGE_ARCH_EL2_VECTORS] ldr x26, [x0, #KIMAGE_ARCH_DTB_MEM] + /* Grab page size values. */ + get_page_size x10 /* x10 = PAGE_SIZE */ + get_page_mask x11 /* x11 = PAGE_MASK */ + /* Setup the list loop variables. */ ldr x18, [x0, #KIMAGE_ARCH_ZERO_PAGE] /* x18 = zero page for BBM */ ldr x17, [x0, #KIMAGE_ARCH_TTBR1] /* x17 = linear map copy */ @@ -57,7 +61,7 @@ SYM_TYPED_FUNC_START(arm64_relocate_new_kernel) raw_dcache_line_size x15, x1 /* x15 = dcache line size */ break_before_make_ttbr_switch x18, x17, x1, x2 /* set linear map */ .Lloop: - and x12, x16, PAGE_MASK /* x12 = addr */ + and x12, x16, x11 /* x12 = addr */ sub x12, x12, x22 /* Convert x12 to virt */ /* Test the entry flags. */ .Ltest_source: @@ -65,8 +69,8 @@ SYM_TYPED_FUNC_START(arm64_relocate_new_kernel) /* Invalidate dest page to PoC. */ mov x19, x13 - copy_page x13, x12, x1, x2, x3, x4, x5, x6, x7, x8 - add x1, x19, #PAGE_SIZE + copy_page x13, x12, x1, x2, x3, x4, x5, x6, x7, x8, x9 + add x1, x19, x10 dcache_by_myline_op_nosync civac, x19, x1, x15, x20 dsb sy b .Lnext diff --git a/arch/arm64/kvm/hyp/nvhe/host.S b/arch/arm64/kvm/hyp/nvhe/host.S index 50581822ec74..6c12a9a90e86 100644 --- a/arch/arm64/kvm/hyp/nvhe/host.S +++ b/arch/arm64/kvm/hyp/nvhe/host.S @@ -192,7 +192,19 @@ SYM_FUNC_END(__host_hvc) */ add sp, sp, x0 // sp' = sp + x0 sub x0, sp, x0 // x0' = sp' - x0 = (sp + x0) - x0 = sp +#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE +alternative_if ARM64_USE_PAGE_SIZE_4K + tbz x0, #ARM64_PAGE_SHIFT_4K, .L__hyp_sp_overflow\@ +alternative_else_nop_endif +alternative_if ARM64_USE_PAGE_SIZE_16K + tbz x0, #ARM64_PAGE_SHIFT_16K, .L__hyp_sp_overflow\@ +alternative_else_nop_endif +alternative_if ARM64_USE_PAGE_SIZE_64K + tbz x0, #ARM64_PAGE_SHIFT_64K, .L__hyp_sp_overflow\@ +alternative_else_nop_endif +#else tbz x0, #NVHE_STACK_SHIFT, .L__hyp_sp_overflow\@ +#endif sub x0, sp, x0 // x0'' = sp' - x0' = (sp + x0) - sp = x0 sub sp, sp, x0 // sp'' = sp' - x0 = (sp + x0) - x0 = sp diff --git a/arch/arm64/lib/copy_page.S b/arch/arm64/lib/copy_page.S index e6374e7e5511..5b4fc2bb983e 100644 --- a/arch/arm64/lib/copy_page.S +++ b/arch/arm64/lib/copy_page.S @@ -10,27 +10,7 @@ #include #include -/* - * Copy a page from src to dest (both are page aligned) - * - * Parameters: - * x0 - dest - * x1 - src - */ -SYM_FUNC_START(__pi_copy_page) -#ifdef CONFIG_AS_HAS_MOPS - .arch_extension mops -alternative_if_not ARM64_HAS_MOPS - b .Lno_mops -alternative_else_nop_endif - - mov x2, #PAGE_SIZE - cpypwn [x0]!, [x1]!, x2! - cpymwn [x0]!, [x1]!, x2! - cpyewn [x0]!, [x1]!, x2! - ret -.Lno_mops: -#endif + .macro copy_page_body, page_size ldp x2, x3, [x1] ldp x4, x5, [x1, #16] ldp x6, x7, [x1, #32] @@ -43,7 +23,7 @@ alternative_else_nop_endif add x0, x0, #256 add x1, x1, #128 1: - tst x0, #(PAGE_SIZE - 1) + tst x0, #(\page_size - 1) stnp x2, x3, [x0, #-256] ldp x2, x3, [x1] @@ -75,7 +55,42 @@ alternative_else_nop_endif stnp x12, x13, [x0, #80 - 256] stnp x14, x15, [x0, #96 - 256] stnp x16, x17, [x0, #112 - 256] + .endm + +/* + * Copy a page from src to dest (both are page aligned) + * + * Parameters: + * x0 - dest + * x1 - src + */ +SYM_FUNC_START(__pi_copy_page) +#ifdef CONFIG_AS_HAS_MOPS + .arch_extension mops +alternative_if_not ARM64_HAS_MOPS + b .Lno_mops +alternative_else_nop_endif + get_page_size x2 + cpypwn [x0]!, [x1]!, x2! + cpymwn [x0]!, [x1]!, x2! + cpyewn [x0]!, [x1]!, x2! + ret +.Lno_mops: +#endif + get_tg0 x2 +.Lsz_64k: + cmp x2, #TCR_TG0_64K + b.ne .Lsz_16k + copy_page_body SZ_64K + ret +.Lsz_16k: + cmp x2, #TCR_TG0_16K + b.ne .Lsz_4k + copy_page_body SZ_16K + ret +.Lsz_4k: + copy_page_body SZ_4K ret SYM_FUNC_END(__pi_copy_page) SYM_FUNC_ALIAS(copy_page, __pi_copy_page) diff --git a/arch/arm64/lib/mte.S b/arch/arm64/lib/mte.S index 5018ac03b6bf..b4f6f5be0ec7 100644 --- a/arch/arm64/lib/mte.S +++ b/arch/arm64/lib/mte.S @@ -28,10 +28,13 @@ * x0 - address of the page to be cleared */ SYM_FUNC_START(mte_clear_page_tags) + get_page_size x3 + sub x3, x3, #1 // (PAGE_SIZE - 1) in x3 + multitag_transfer_size x1, x2 1: stgm xzr, [x0] add x0, x0, x1 - tst x0, #(PAGE_SIZE - 1) + tst x0, x3 b.ne 1b ret SYM_FUNC_END(mte_clear_page_tags) @@ -43,6 +46,9 @@ SYM_FUNC_END(mte_clear_page_tags) * x0 - address to the beginning of the page */ SYM_FUNC_START(mte_zero_clear_page_tags) + get_page_size x3 + sub x3, x3, #1 // (PAGE_SIZE - 1) in x3 + and x0, x0, #(1 << MTE_TAG_SHIFT) - 1 // clear the tag mrs x1, dczid_el0 tbnz x1, #4, 2f // Branch if DC GZVA is prohibited @@ -52,12 +58,12 @@ SYM_FUNC_START(mte_zero_clear_page_tags) 1: dc gzva, x0 add x0, x0, x1 - tst x0, #(PAGE_SIZE - 1) + tst x0, x3 b.ne 1b ret 2: stz2g x0, [x0], #(MTE_GRANULE_SIZE * 2) - tst x0, #(PAGE_SIZE - 1) + tst x0, x3 b.ne 2b ret SYM_FUNC_END(mte_zero_clear_page_tags) @@ -68,6 +74,9 @@ SYM_FUNC_END(mte_zero_clear_page_tags) * x1 - address of the source page */ SYM_FUNC_START(mte_copy_page_tags) + get_page_size x7 + sub x7, x7, #1 // (PAGE_SIZE - 1) in x7 + mov x2, x0 mov x3, x1 multitag_transfer_size x5, x6 @@ -75,7 +84,7 @@ SYM_FUNC_START(mte_copy_page_tags) stgm x4, [x2] add x2, x2, x5 add x3, x3, x5 - tst x2, #(PAGE_SIZE - 1) + tst x2, x7 b.ne 1b ret SYM_FUNC_END(mte_copy_page_tags) @@ -137,6 +146,9 @@ SYM_FUNC_END(mte_copy_tags_to_user) * x1 - tag storage, MTE_PAGE_TAG_STORAGE bytes */ SYM_FUNC_START(mte_save_page_tags) + get_page_size x3 + sub x3, x3, #1 // (PAGE_SIZE - 1) in x3 + multitag_transfer_size x7, x5 1: mov x2, #0 @@ -149,7 +161,7 @@ SYM_FUNC_START(mte_save_page_tags) str x2, [x1], #8 - tst x0, #(PAGE_SIZE - 1) + tst x0, x3 b.ne 1b ret @@ -161,6 +173,9 @@ SYM_FUNC_END(mte_save_page_tags) * x1 - tag storage, MTE_PAGE_TAG_STORAGE bytes */ SYM_FUNC_START(mte_restore_page_tags) + get_page_size x3 + sub x3, x3, #1 // (PAGE_SIZE - 1) in x3 + multitag_transfer_size x7, x5 1: ldr x2, [x1], #8 @@ -170,7 +185,7 @@ SYM_FUNC_START(mte_restore_page_tags) tst x0, #0xFF b.ne 2b - tst x0, #(PAGE_SIZE - 1) + tst x0, x3 b.ne 1b ret