arm64: kexec: translate relocate_kernel.S to C languange
cover arm64: kexec: translate relocate_kernel.S to C languange
Translate relocate_kernel.S to C languange to make it more readable.
The relocate_kernel.c is PIE, but I am not sure whether it should locate in kernel/pi, which runs check on the PIE code. (I can correct it in next version)
Cc: Catalin Marinas <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: Ard Biesheuvel <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: Mark Rutland <[email protected]>
Cc: Pasha Tatashin <[email protected]>
To: [email protected]
---
Pingfan Liu (4):
arm64: relocate: Let __relocate_new_kernel_start align on SZ_4K
arm64: mm: Provide prot param in trans_pgd_idmap_page()'s prototype
arm64: kexec: Introduce d_size to carry cacheline size information
arm64: kexec: Change relocate_kernel to C code
arch/arm64/include/asm/kexec.h | 1 + arch/arm64/include/asm/trans_pgd.h | 2 +- arch/arm64/kernel/Makefile | 1 + arch/arm64/kernel/asm-offsets.c | 10 -- arch/arm64/kernel/hibernate.c | 3 +- arch/arm64/kernel/machine_kexec.c | 16 ++- arch/arm64/kernel/relocate_kernel.S | 100 -------------- arch/arm64/kernel/relocate_kernel.c | 197 ++++++++++++++++++++++++++++ arch/arm64/kernel/vmlinux.lds.S | 3 +- arch/arm64/mm/trans_pgd.c | 4 +- 10 files changed, 218 insertions(+), 119 deletions(-) delete mode 100644 arch/arm64/kernel/relocate_kernel.S create mode 100644 arch/arm64/kernel/relocate_kernel.c
Signature · 7 lines
2.41.0 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH 1/4 arm64: relocate: Let __relocate_new_kernel_start align on SZ_4K
For upcoming C implement, let this section align on SZ_4K, so that it makes potential instruction pairs 'adrp, add' work.
--- arch/arm64/kernel/vmlinux.lds.S | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kernel/vmlinux.lds.S +1 -1
@@ -103,7 +103,7 @@ jiffies = jiffies_64; #ifdef CONFIG_KEXEC_CORE #define KEXEC_TEXT \ - ALIGN_FUNCTION(); \ + . = ALIGN(SZ_4K); \ __relocate_new_kernel_start = .; \ *(.kexec_relocate.text) \ __relocate_new_kernel_end = .;
Signature · 7 lines
2.41.0 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH 2/4 arm64: mm: Provide prot param in trans_pgd_idmap_page()'s prototype
In reply to Pingfan LiuSince relocate_kernel code will build stack at the rear of the page, it requires 'wx' on the page. Adapting the prototype of trans_pgd_idmap_page() to make it doable.
The trans_pgd_idmap_page() can be enhanced further to support multiple pages. But since the change has met the requirement, it is not necessary to enhance for the time being.
--- arch/arm64/include/asm/trans_pgd.h | 2 +- arch/arm64/kernel/hibernate.c | 3 ++- arch/arm64/kernel/machine_kexec.c | 4 ++-- arch/arm64/mm/trans_pgd.c | 4 ++-- 4 files changed, 7 insertions(+), 6 deletions(-)
arch/arm64/include/asm/trans_pgd.h +1 -1
@@ -31,7 +31,7 @@ int trans_pgd_create_copy(struct trans_pgd_info *info, pgd_t **trans_pgd, unsigned long start, unsigned long end);int trans_pgd_idmap_page (struct trans_pgd_info *info,phys_addr_t *trans_ttbr0, -unsigned long *t0sz,void *page); +unsigned long *t0sz,void *page,pgprot_t prot);int trans_pgd_copy_el2_vectors (struct trans_pgd_info *info,phys_addr_t *el2_vectors);
arch/arm64/kernel/hibernate.c +2 -1
@@ -203,7 +203,8 @@ static int create_safe_exec_page(void *src_start, size_t length,memcpy (page, src_start, length);caches_clean_inval_pou ((unsigned long )page, (unsigned long )page + length); - rc =trans_pgd_idmap_page (&trans_info, &trans_ttbr0, &t0sz, page); + rc =trans_pgd_idmap_page (&trans_info, &trans_ttbr0, &t0sz, page, +PAGE_KERNEL_ROX );if (rc)return rc;
arch/arm64/kernel/machine_kexec.c +2 -2
@@ -141,8 +141,8 @@ int machine_kexec_post_load(struct kimage *kimage) reloc_size = __relocate_new_kernel_end - __relocate_new_kernel_start;memcpy (reloc_code, __relocate_new_kernel_start, reloc_size); kimage->arch.kern_reloc =__pa (reloc_code); - rc =trans_pgd_idmap_page (&info, &kimage->arch.ttbr0, - &kimage->arch.t0sz, reloc_code); + rc =trans_pgd_idmap_page (&info, &kimage->arch.ttbr0, &kimage->arch.t0sz, + reloc_code,PAGE_KERNEL_EXEC );if (rc)return rc; kimage->arch.phys_offset =virt_to_phys (kimage) - (long )kimage;
arch/arm64/mm/trans_pgd.c +2 -2
@@ -230,7 +230,7 @@ int trans_pgd_create_copy(struct trans_pgd_info *info, pgd_t **dst_pgdp,* maximum T0SZ for this page. */ int trans_pgd_idmap_page (struct trans_pgd_info *info,phys_addr_t *trans_ttbr0, -unsigned long *t0sz,void *page) +unsigned long *t0sz,void *page,pgprot_t prot) {phys_addr_t dst_addr =virt_to_phys (page);unsigned long pfn =__phys_to_pfn (dst_addr);
@@ -240,7 +240,7 @@ int trans_pgd_idmap_page(struct trans_pgd_info *info, phys_addr_t *trans_ttbr0,int this_level, index, level_lsb, level_msb; dst_addr &=PAGE_MASK ; - prev_level_entry =pte_val (pfn_pte (pfn,PAGE_KERNEL_ROX )); + prev_level_entry =pte_val (pfn_pte (pfn, prot));for (this_level =3 ; this_level >=0 ; this_level--) { levels[this_level] =trans_alloc (info);
Signature · 7 lines
2.41.0 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH 3/4 arm64: kexec: Introduce d_size to carry cacheline size information
In reply to Pingfan LiuIntroducing kimage_arch.d_size to carry the data cacheline size information, so that the relocate_kernel C routine can be implemented simpler. The cache size info will be used in the next patch
--- arch/arm64/include/asm/kexec.h | 1 + arch/arm64/kernel/machine_kexec.c | 3 +++ 2 files changed, 4 insertions(+)
arch/arm64/include/asm/kexec.h +1 -0
@@ -116,6 +116,7 @@ struct kimage_arch {phys_addr_t zero_page;unsigned long phys_offset;unsigned long t0sz; +unsigned long d_size; };#ifdef CONFIG_KEXEC_FILE
arch/arm64/kernel/machine_kexec.c +3 -0
@@ -146,6 +146,9 @@ int machine_kexec_post_load(struct kimage *kimage)if (rc)return rc; kimage->arch.phys_offset =virt_to_phys (kimage) - (long )kimage; + kimage->arch.d_size =4 <<cpuid_feature_extract_unsigned_field ( + arm64_ftr_reg_ctrel0.sys_val, + CTR_EL0_DminLine_SHIFT);/* Flush the reloc_code in preparation for its execution. */ dcache_clean_inval_poc((unsigned long)reloc_code,
Signature · 7 lines
2.41.0 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH 4/4 arm64: kexec: Change relocate_kernel to C code
In reply to Pingfan LiuThe kexec_relocate.o is a self-contained section, and it should be PIE.
Beside that, C function call requires stack, which is built on the idmap of the rear of kimage->control_code_page.
--- arch/arm64/kernel/Makefile | 1 + arch/arm64/kernel/asm-offsets.c | 10 -- arch/arm64/kernel/machine_kexec.c | 9 +- arch/arm64/kernel/relocate_kernel.S | 100 -------------- arch/arm64/kernel/relocate_kernel.c | 197 ++++++++++++++++++++++++++++ arch/arm64/kernel/vmlinux.lds.S | 1 + 6 files changed, 206 insertions(+), 112 deletions(-) delete mode 100644 arch/arm64/kernel/relocate_kernel.S create mode 100644 arch/arm64/kernel/relocate_kernel.c
arch/arm64/kernel/Makefile +1 -0
@@ -13,6 +13,7 @@ CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) # checks due to randomize_kstack_offset. CFLAGS_REMOVE_syscall.o = -fstack-protector -fstack-protector-strong CFLAGS_syscall.o += -fno-stack-protector +CFLAGS_relocate_kernel.o += -fPIE # When KASAN is enabled, a stack trace is recorded for every alloc/free, which # can significantly impact performance. Avoid instrumenting the stack trace
arch/arm64/kernel/asm-offsets.c +0 -10
@@ -186,16 +186,6 @@ int main(void)#endif BLANK ();#endif -#ifdef CONFIG_KEXEC_CORE - DEFINE(KIMAGE_ARCH_DTB_MEM, offsetof(struct kimage, arch.dtb_mem)); - DEFINE(KIMAGE_ARCH_EL2_VECTORS, offsetof(struct kimage, arch.el2_vectors)); - DEFINE(KIMAGE_ARCH_ZERO_PAGE, offsetof(struct kimage, arch.zero_page)); - DEFINE(KIMAGE_ARCH_PHYS_OFFSET, offsetof(struct kimage, arch.phys_offset)); - DEFINE(KIMAGE_ARCH_TTBR1, offsetof(struct kimage, arch.ttbr1)); -DEFINE (KIMAGE_HEAD , offsetof(struct kimage , head)); -DEFINE (KIMAGE_START , offsetof(struct kimage , start)); -BLANK (); -#endif #ifdef CONFIG_FUNCTION_TRACER DEFINE (FTRACE_OPS_FUNC , offsetof(struct ftrace_ops , func));#endif
arch/arm64/kernel/machine_kexec.c +7 -2
@@ -198,13 +198,18 @@ void machine_kexec(struct kimage *kimage)restart (is_hyp_nvhe (), kimage->start, kimage->arch.dtb_mem,0 ,0 ); }else { -void (*kernel_reloc)(struct kimage *kimage); +void (*kernel_reloc)(struct kimage *kimage,unsigned long sp); +u64 new_sp = (u64)(page_to_pfn (kimage->control_code_page) <<PAGE_SHIFT ) + +KEXEC_CONTROL_PAGE_SIZE ;if (is_hyp_nvhe ())__hyp_set_vectors (kimage->arch.el2_vectors);cpu_install_ttbr0 (kimage->arch.ttbr0, kimage->arch.t0sz); kernel_reloc = (void *)kimage->arch.kern_reloc; -kernel_reloc (kimage); +pr_info ("jump to relocation at: 0x%llx, with sp:0x%llx\n" , + (u64 )kernel_reloc, new_sp); +/* new_sp is accessible through idmap */ +kernel_reloc (kimage, new_sp); }BUG ();/* Should never get here. */
arch/arm64/kernel/relocate_kernel.S deleted +0 -100
100 lines not on this page · raw message
arch/arm64/kernel/relocate_kernel.c new file +197 -0
197 lines not on this page · raw message
arch/arm64/kernel/vmlinux.lds.S +1 -0
@@ -105,6 +105,7 @@ jiffies = jiffies_64; #define KEXEC_TEXT \ . = ALIGN(SZ_4K); \ __relocate_new_kernel_start = .; \ + *(.kexec_relocate.entry.text) \ *(.kexec_relocate.text) \ __relocate_new_kernel_end = .; #else
Signature · 7 lines
2.41.0 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Mark Rutland
reviewerRe: [PATCH 4/4] arm64: kexec: Change relocate_kernel to C code
Quoted 25 lines · Pingfan Liu
The kexec_relocate.o is a self-contained section, and it should be PIE.
Beside that, C function call requires stack, which is built on the idmap of the rear of
kimage->control_code_page.Signed-off-by Pingfan Liu <[email protected]>Cc Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Ard Biesheuvel <[email protected]>Cc Kees Cook <[email protected]>Cc Mark Rutland <[email protected]>Cc Pasha Tatashin <[email protected]>--- arch/arm64/kernel/Makefile | 1 + arch/arm64/kernel/asm-offsets.c | 10 -- arch/arm64/kernel/machine_kexec.c | 9 +- arch/arm64/kernel/relocate_kernel.S | 100 -------------- arch/arm64/kernel/relocate_kernel.c | 197 ++++++++++++++++++++++++++++ arch/arm64/kernel/vmlinux.lds.S | 1 + 6 files changed, 206 insertions(+), 112 deletions(-) delete mode 100644 arch/arm64/kernel/relocate_kernel.S create mode 100644 arch/arm64/kernel/relocate_kernel.carch/arm64/kernel/relocate_kernel.c new file +1
-0+static void __kexec_section turn_mmu_off(void)
arch/arm64/kernel/relocate_kernel.c line 76
Quoted 10 lines
arch/arm64/kernel/relocate_kernel.c new file +10
-0+{ +u64 tmp =INIT_SCTLR_EL1_MMU_OFF ; + +/* pre_disable_mmu_workaround */ +#ifdef CONFIG_QCOM_FALKOR_ERRATUM_E1041 +isb (); +#endif +write_sysreg (tmp, sctlr_el1); +isb (); +}
Disabling the MMU cannot be done from C; as soon as we write to SCTLR_EL1 (even before the ISB) we cannot safely access the stack until that has been explcitly cleaned+invalidated to the PoC (and that has to be done by VA).
I don't think we should bother trying to move this to C; the MMU-off portions should remain as asssembly.
If you want to move the MMU-on portions to C, then *maybe* that's worthwhile, but given the diffstat I reckon it's better to leave this all as asm for now. We can make this more legibile without converting it to C.
Mark.
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel