ARM: mm: Extend the runtime patch stub for PAE systems
cover ARM: mm: Extend the runtime patch stub for PAE systems
Based on discussion/debate on Cyril's generic code patching framework, we cooked up this series which basically trying to extend the existing v2p runtime patching for LPAE machines which can have physical memory beyond 4 GB. Keystone is one such ARM machine.
We think the 64 bit patching can be still made better than the proposed patch in the series and hence seeking expert comments from RMK, Nico and others. Last patch in the series added to just give perspective on how machine code will make use of the available bits from the series.
Santosh Shilimkar (6):
ARM: mm: LPAE: use phys_addr_t appropriately in p2v and v2p
conversions
ARM: mm: Introduce virt_to_idmap() with an arch hook
ARM: mm: Move the idmap print to appropriate place in the code
ARM: mm: Pass the constant as an argument to fixup_pv_table()
ARM: mm: Recreate kernel mappings in early_paging_init()
ARM: keystone: Switch over to high physical address range
Sricharan R (2):
ARM: mm: Add __pv_stub_mov to patch MOV instruction
ARM: mm: LPAE: Correct virt_to_phys patching for 64 bit physical
addresses
arch/arm/include/asm/mach/arch.h | 1 + arch/arm/include/asm/memory.h | 72 ++++++++++++++++++++++++--- arch/arm/kernel/armksyms.c | 2 + arch/arm/kernel/head.S | 39 +++++++++++++-- arch/arm/kernel/module.c | 11 ++++- arch/arm/kernel/setup.c | 3 ++ arch/arm/kernel/smp.c | 2 +- arch/arm/kernel/vmlinux.lds.S | 5 ++ arch/arm/mach-keystone/keystone.c | 49 ++++++++++++++++++ arch/arm/mach-keystone/memory.h | 24 +++++++++ arch/arm/mach-keystone/platsmp.c | 16 +++++- arch/arm/mm/idmap.c | 8 +-- arch/arm/mm/mmu.c | 99 +++++++++++++++++++++++++++++++++++++ 13 files changed, 311 insertions(+), 20 deletions(-) create mode 100644 arch/arm/mach-keystone/memory.h
Signature · 1 line
1.7.9.5
PATCH 1/8 ARM: mm: LPAE: use phys_addr_t appropriately in p2v and v2p conversions
Fix remainder types used when converting back and forth between physical and virtual addresses.
--- arch/arm/include/asm/memory.h | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
arch/arm/include/asm/memory.h +16 -6
@@ -185,22 +185,32 @@ extern unsigned long __pv_phys_offset; : "=r" (to) \ : "r" (from), "I" (type)) -static inline unsigned long __virt_to_phys (unsigned long x) +static inline phys_addr_t __virt_to_phys (unsigned long x) {unsigned long t;__pv_stub (x, t,"add" , __PV_BITS_31_24);return t; } -static inline unsigned long __phys_to_virt (unsigned long x) +static inline unsigned long __phys_to_virt (phys_addr_t x) {unsigned long t;__pv_stub (x, t,"sub" , __PV_BITS_31_24);return t; } +#else -#define __virt_to_phys (x) ((x) - PAGE_OFFSET + PHYS_OFFSET) -#define __phys_to_virt (x) ((x) - PHYS_OFFSET + PAGE_OFFSET) + +static inline phys_addr_t __virt_to_phys (unsigned long x) +{ +return (phys_addr_t )x -PAGE_OFFSET +PHYS_OFFSET ; +} + +static inline unsigned long __phys_to_virt (phys_addr_t x) +{ +return x -PHYS_OFFSET +PAGE_OFFSET ; +} +#endif #endif #endif /* __ASSEMBLY__ */
@@ -238,14 +248,14 @@ static inline phys_addr_t virt_to_phys(const volatile void *x)static inline void *phys_to_virt (phys_addr_t x) { -return (void *)(__phys_to_virt ((unsigned long )(x))); +return (void *)__phys_to_virt (x); }/* * Drivers should NOT use these either. */ #define __pa (x) __virt_to_phys((unsigned long)(x)) -#define __va (x) ((void *)__phys_to_virt((unsigned long)(x))) +#define __va (x) ((void *)__phys_to_virt((phys_addr_t)(x)))#define pfn_to_kaddr (pfn) __va((pfn) << PAGE_SHIFT)/*
Signature · 1 line
1.7.9.5
PATCH 1/8 ARM: mm: LPAE: use phys_addr_t appropriately in p2v and v2p conversions
Quoted 7 lines · Santosh Shilimkar
Fix remainder types used when converting back and forth between physical and virtual addresses.
Cc Nicolas Pitre <[email protected]>Cc Will Deacon <[email protected]>Signed-off-by Santosh Shilimkar <[email protected]>
Quoted 65 lines
--- arch/arm/include/asm/memory.h | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)arch/arm/include/asm/memory.h +16
-6@@ -185,22 +185,32 @@ extern unsigned long __pv_phys_offset; : "=r" (to) \ : "r" (from), "I" (type)) -static inline unsigned long __virt_to_phys (unsigned long x) +static inline phys_addr_t __virt_to_phys (unsigned long x) {unsigned long t;__pv_stub (x, t,"add" , __PV_BITS_31_24);return t; } -static inline unsigned long __phys_to_virt (unsigned long x) +static inline unsigned long __phys_to_virt (phys_addr_t x) {unsigned long t;__pv_stub (x, t,"sub" , __PV_BITS_31_24);return t; } +#else -#define __virt_to_phys (x) ((x) - PAGE_OFFSET + PHYS_OFFSET) -#define __phys_to_virt (x) ((x) - PHYS_OFFSET + PAGE_OFFSET) + +static inline phys_addr_t __virt_to_phys (unsigned long x) +{ +return (phys_addr_t )x -PAGE_OFFSET +PHYS_OFFSET ; +} + +static inline unsigned long __phys_to_virt (phys_addr_t x) +{ +return x -PHYS_OFFSET +PAGE_OFFSET ; +} +#endif #endif #endif /* __ASSEMBLY__ */ @@ -238,14 +248,14 @@ static inline phys_addr_t virt_to_phys(const volatile void *x)static inline void *phys_to_virt (phys_addr_t x) { -return (void *)(__phys_to_virt ((unsigned long )(x))); +return (void *)__phys_to_virt (x); }/* * Drivers should NOT use these either. */ #define __pa (x) __virt_to_phys((unsigned long)(x)) -#define __va (x) ((void *)__phys_to_virt((unsigned long)(x))) +#define __va (x) ((void *)__phys_to_virt((phys_addr_t)(x)))#define pfn_to_kaddr (pfn) __va((pfn) << PAGE_SHIFT)/* Signature · 1 line
1.7.9.5
PATCH 2/8 ARM: mm: Introduce virt_to_idmap() with an arch hook
In reply to Santosh ShilimkarOn some PAE systems (e.g. TI Keystone), memory is above the 32-bit addressable limit, and the interconnect provides an aliased view of parts of physical memory in the 32-bit addressable space. This alias is strictly for boot time usage, and is not otherwise usable because of coherency limitations. On such systems, the idmap mechanism needs to take this aliased mapping into account.
This patch introduces virt_to_idmap() and a arch function pointer which can be populated by platform which needs it. Also populate necessary idmap spots with now available virt_to_idmap(). Avoided #ifdef approach to be compatible with multi-platform builds.
Most architecture won't touch it and in that case virt_to_idmap() fall-back to existing virt_to_phys() macro.
--- arch/arm/include/asm/memory.h | 16 ++++++++++++++++ arch/arm/kernel/smp.c | 2 +- arch/arm/mm/idmap.c | 5 +++-- 3 files changed, 20 insertions(+), 3 deletions(-)
arch/arm/include/asm/memory.h +16 -0
@@ -173,6 +173,7 @@*/ #define __PV_BITS_31_24 0x81000000 +extern phys_addr_t (*arch_virt_to_idmap) (unsigned long x);extern unsigned long __pv_phys_offset;#define PHYS_OFFSET __pv_phys_offset
@@ -259,6 +260,21 @@ static inline void *phys_to_virt(phys_addr_t x)#define pfn_to_kaddr (pfn) __va((pfn) << PAGE_SHIFT)/* +* These are for systems that have a hardware interconnect supported alias of +* physical memory for idmap purposes. Most cases should leave these +* untouched. +*/ +static inline phys_addr_t __virt_to_idmap (unsigned long x) +{ +if (arch_virt_to_idmap) +return arch_virt_to_idmap (x); +else +return __virt_to_phys (x); +} + +#define virt_to_idmap (x) __virt_to_idmap((unsigned long)(x)) + +/* * Virtual <-> DMA view memory address translations * Again, these are *only* valid on the kernel direct mapped RAM * memory. Use of these is *deprecated* (and that doesn't mean
arch/arm/kernel/smp.c +1 -1
@@ -80,7 +80,7 @@ void __init smp_set_ops(struct smp_operations *ops)static unsigned long get_arch_pgd (pgd_t *pgd) { -phys_addr_t pgdir =virt_to_phys (pgd); +phys_addr_t pgdir =virt_to_idmap (pgd);BUG_ON (pgdir &ARCH_PGD_MASK );return pgdir >>ARCH_PGD_SHIFT ; }
arch/arm/mm/idmap.c +3 -2
@@ -10,6 +10,7 @@#include <asm/system_info.h> pgd_t *idmap_pgd; +phys_addr_t (*arch_virt_to_idmap) (unsigned long x);#ifdef CONFIG_ARM_LPAE static void idmap_add_pmd(pud_t *pud, unsigned long addr, unsigned long end,
@@ -67,8 +68,8 @@ static void identity_mapping_add(pgd_t *pgd, const char *text_start,unsigned long addr, end;unsigned long next; - addr =virt_to_phys (text_start); - end =virt_to_phys (text_end); + addr =virt_to_idmap (text_start); + end =virt_to_idmap (text_end); prot |=PMD_TYPE_SECT |PMD_SECT_AP_WRITE |PMD_SECT_AF ;
Signature · 1 line
1.7.9.5
PATCH 3/8 ARM: mm: Move the idmap print to appropriate place in the code
In reply to Santosh ShilimkarCommit 9e9a367c29cebd2 {ARM: Section based HYP idmap} moved the address conversion inside identity_mapping_add() without respective print which carries useful idmap information.
Move the print as well inside identity_mapping_add() to fix the same.
--- arch/arm/mm/idmap.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/arm/mm/idmap.c +1 -2
@@ -70,6 +70,7 @@ static void identity_mapping_add(pgd_t *pgd, const char *text_start, addr =virt_to_idmap (text_start); end =virt_to_idmap (text_end); +pr_info ("Setting up static identity map for 0x%lx - 0x%lx\n" , addr, end); prot |=PMD_TYPE_SECT |PMD_SECT_AP_WRITE |PMD_SECT_AF ;
@@ -91,8 +92,6 @@ static int __init init_static_idmap(void)if (!idmap_pgd)return -ENOMEM ; -pr_info ("Setting up static identity map for 0x%p - 0x%p\n" , - __idmap_text_start, __idmap_text_end);identity_mapping_add (idmap_pgd, __idmap_text_start, __idmap_text_end,0 );
Signature · 1 line
1.7.9.5
PATCH 4/8 ARM: mm: Pass the constant as an argument to fixup_pv_table()
In reply to Santosh ShilimkarCurrent fixup_pv_table() API hardcoded the constant to be patched. Make that as an argument to fixup_pv_table() so that different type of instructions can be patched.
This is preparatory patch to add the subsequent 64 bit patching.
--- arch/arm/include/asm/memory.h | 2 ++ arch/arm/kernel/head.S | 3 +-- arch/arm/kernel/module.c | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-)
arch/arm/include/asm/memory.h +2 -0
@@ -175,6 +175,8 @@extern phys_addr_t (*arch_virt_to_idmap) (unsigned long x);extern unsigned long __pv_phys_offset; +extern unsigned long __pv_offset; +#define PHYS_OFFSET __pv_phys_offset#define __pv_stub (from,to,instr,type) \
arch/arm/kernel/head.S +1 -2
@@ -604,11 +604,10 @@ ENDPROC(__fixup_a_pv_table) ENTRY(fixup_pv_table) stmfd sp!, {r4 - r7, lr} - ldr r2, 2f @ get address of __pv_phys_offset mov r3, #0 @ no offset mov r4, r0 @ r0 = table start add r5, r0, r1 @ r1 = table size - ldr r6, [r2, #4] @ get __pv_offset + mov r6, r2 @ get constant to be patched bl __fixup_a_pv_table ldmfd sp!, {r4 - r7, pc} ENDPROC(fixup_pv_table)
arch/arm/kernel/module.c +2 -2
@@ -265,7 +265,7 @@ static const Elf_Shdr *find_mod_section(const Elf32_Ehdr *hdr,return NULL ; } -extern void fixup_pv_table (const void *,unsigned long ); +extern void fixup_pv_table (const void *,unsigned long ,unsigned long );extern void fixup_smp (const void *,unsigned long ); int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
@@ -319,7 +319,7 @@ int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,#ifdef CONFIG_ARM_PATCH_PHYS_VIRT s =find_mod_section (hdr, sechdrs,".pv_table" );if (s) -fixup_pv_table ((void *)s->sh_addr, s->sh_size); +fixup_pv_table ((void *)s->sh_addr, s->sh_size, __pv_offset);#endif s =find_mod_section (hdr, sechdrs,".alt.smp.init" );if (s && !is_smp ())
Signature · 1 line
1.7.9.5
PATCH 5/8 ARM: mm: Add __pv_stub_mov to patch MOV instruction
In reply to Santosh ShilimkarFrom: Sricharan R <[email protected]>
Patch adds stub for MOV instruction. This is preparatory patch to add the subsequent 64 bit patching which will use MOV stub.
For review simplicity the patch is kept as a separate change
--- arch/arm/include/asm/memory.h | 10 ++++++++++ arch/arm/kernel/head.S | 2 ++ arch/arm/kernel/vmlinux.lds.S | 5 +++++ 3 files changed, 17 insertions(+)
arch/arm/include/asm/memory.h +10 -0
@@ -172,6 +172,7 @@* so that all we need to do is modify the 8-bit constant field. */ #define __PV_BITS_31_24 0x81000000 +#define __PV_BITS_7_0 0x81extern phys_addr_t (*arch_virt_to_idmap) (unsigned long x);extern unsigned long __pv_phys_offset;
@@ -188,6 +189,15 @@ extern unsigned long __pv_offset; : "=r" (to) \ : "r" (from), "I" (type)) +#define __pv_stub_mov (to, instr, type) \ + __asm__ volatile("@ __pv_stub_mov\n" \ + "1: " instr " %R0, %1\n" \ + " .pushsection .pv_high_table,\"a\"\n" \ + " .long 1b\n" \ + " .popsection\n" \ + : "=r" (to) \ + : "I" (type)) +static inline phys_addr_t __virt_to_phys (unsigned long x) {unsigned long t;
arch/arm/kernel/head.S +2 -0
@@ -565,6 +565,8 @@ ENDPROC(__fixup_pv_table) .long __pv_table_begin .long __pv_table_end 2: .long __pv_phys_offset +3: .long __pv_high_table_begin + .long __pv_high_table_end .text __fixup_a_pv_table:
arch/arm/kernel/vmlinux.lds.S +5 -0
@@ -182,6 +182,11 @@ SECTIONS *(.pv_table) __pv_table_end = .; } + .init.pv_high_table : { + __pv_high_table_begin = .; + *(.pv_high_table) + __pv_high_table_end = .; + } .init.data : { #ifndef CONFIG_XIP_KERNEL INIT_DATA
Signature · 1 line
1.7.9.5
PATCH 6/8 ARM: mm: LPAE: Correct virt_to_phys patching for 64 bit physical addresses
In reply to Santosh ShilimkarFrom: Sricharan R <[email protected]>
The current phys_to_virt patching mechanism does not work for 64 bit physical addressesp. Note that constant used in add/sub instructions is encoded in to the last 8 bits of the opcode. So shift the _pv_offset constant by 24 to get it in to the correct place.
The v2p patching mechanism patches the higher 32bits of physical address with a constant. While this is correct, in those platforms where the lowmem addressable physical memory spawns across 4GB boundary, a carry bit can be produced as a result of addition of lower 32bits. This has to be taken in to account and added in to the upper. The patched __pv_offset and va are added in lower 32bits, where __pv_offset can be in two's complement form when PA_START < VA_START and that can result in a false carry bit.
e.g PA = 0x80000000 VA = 0xC0000000 __pv_offset = PA - VA = 0xC0000000 (2's complement)
So adding __pv_offset + VA should never result in a true overflow. So in order to differentiate between a true carry, a extra flag __pv_sign_flag is introduced.
There is no corresponding change on the phys_to_virt() side, because computations on the upper 32-bits would be discarded anyway.
We think, the patch can be further optimised and made bit better with expert review from RMK, Nico and others.
--- arch/arm/include/asm/memory.h | 22 ++++++++++++++++++++-- arch/arm/kernel/armksyms.c | 2 ++ arch/arm/kernel/head.S | 34 ++++++++++++++++++++++++++++++++-- arch/arm/kernel/module.c | 7 +++++++ 4 files changed, 61 insertions(+), 4 deletions(-)
arch/arm/include/asm/memory.h +20 -2
@@ -174,9 +174,17 @@#define __PV_BITS_31_24 0x81000000#define __PV_BITS_7_0 0x81 +/* +* PV patch constants. +* Lower 32bits are 16MB aligned. +*/ +#define PV_LOW_SHIFT 24 +#define PV_HIGH_SHIFT 32 +extern phys_addr_t (*arch_virt_to_idmap) (unsigned long x); -extern unsigned long __pv_phys_offset; +extern phys_addr_t __pv_phys_offset;extern unsigned long __pv_offset; +extern unsigned long __pv_sign_flag;#define PHYS_OFFSET __pv_phys_offset
@@ -187,7 +195,8 @@ extern unsigned long __pv_offset; " .long 1b\n" \ " .popsection\n" \ : "=r" (to) \ - : "r" (from), "I" (type)) + : "r" (from), "I" (type) \ + : "cc")#define __pv_stub_mov (to, instr, type) \ __asm__ volatile("@ __pv_stub_mov\n" \
@@ -200,8 +209,17 @@ extern unsigned long __pv_offset;static inline phys_addr_t __virt_to_phys (unsigned long x) { +#ifdef CONFIG_ARM_LPAE + register phys_addr_t t asm("r4") = 0; + +__pv_stub_mov (t,"mov" , __PV_BITS_7_0); +__pv_stub (x, t,"adds" , __PV_BITS_31_24); + __asm__volatile ("adc %R0, %R0, %1" :"+r" (t) :"I" (0x0 )); + __asm__volatile ("sub %R0, %R0, %1" :"+r" (t) :"r" (__pv_sign_flag)); +#else unsigned long t;__pv_stub (x, t,"add" , __PV_BITS_31_24); +#endif return t; }
arch/arm/kernel/armksyms.c +2 -0
@@ -155,4 +155,6 @@ EXPORT_SYMBOL(__gnu_mcount_nc);#ifdef CONFIG_ARM_PATCH_PHYS_VIRT EXPORT_SYMBOL (__pv_phys_offset); +EXPORT_SYMBOL (__pv_offset); +EXPORT_SYMBOL (__pv_sign_flag);#endif
arch/arm/kernel/head.S +32 -2
@@ -546,24 +546,42 @@ ENDPROC(fixup_smp) __HEAD __fixup_pv_table: adr r0, 1f - ldmia r0, {r3-r5, r7} + ldmia r0, {r3-r7} + cmp r0, r3 + mov ip, #1 sub r3, r0, r3 @ PHYS_OFFSET - PAGE_OFFSET add r4, r4, r3 @ adjust table start address add r5, r5, r3 @ adjust table end address add r7, r7, r3 @ adjust __pv_phys_offset address + add r6, r6, r3 @ adjust __pv_sign_flag + strcc ip, [r6] @ save __pv_sign_flag str r8, [r7] @ save computed PHYS_OFFSET to __pv_phys_offset mov r6, r3, lsr #24 @ constant for add/sub instructions teq r3, r6, lsl #24 @ must be 16MiB aligned THUMB( it ne @ cross section branch ) bne __error +#ifndef CONFIG_ARM_LPAE str r6, [r7, #4] @ save to __pv_offset b __fixup_a_pv_table +#else + str r6, [r7, #8] @ save to __pv_offset + mov r0, r14 @ save lr + bl __fixup_a_pv_table + adr r6, 3f + ldmia r6, {r4-r5} + add r4, r4, r3 @ adjust __pv_high_table start address + add r5, r5, r3 @ adjust __pv_high_table end address + mov r6, #0 @ higher 32 bits of PHYS_OFFSET to start with + bl __fixup_a_pv_table + mov pc, r0 +#endif ENDPROC(__fixup_pv_table) .align 1: .long . .long __pv_table_begin .long __pv_table_end + .long __pv_sign_flag 2: .long __pv_phys_offset 3: .long __pv_high_table_begin .long __pv_high_table_end
@@ -621,10 +639,22 @@ ENDPROC(fixup_pv_table) .globl __pv_phys_offset .type __pv_phys_offset, %object __pv_phys_offset: +#ifdef CONFIG_ARM_LPAE + .quad 0 +#else .long 0 - .size __pv_phys_offset, . - __pv_phys_offset +#endif + .data + .globl __pv_offset + .type __pv_offset, %object __pv_offset: .long 0 + + .data + .globl __pv_sign_flag + .type __pv_sign_flag, %object +__pv_sign_flag: + .long 0 #endif #include "head-common.S"
arch/arm/kernel/module.c +7 -0
@@ -320,6 +320,13 @@ int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs, s =find_mod_section (hdr, sechdrs,".pv_table" );if (s)fixup_pv_table ((void *)s->sh_addr, s->sh_size, __pv_offset); + +#ifdef CONFIG_ARM_LPAE + s =find_mod_section (hdr, sechdrs,".pv_high_table" ); +if (s) +fixup_pv_table ((void *)s->sh_addr, s->sh_size, + __pv_phys_offset >>PV_HIGH_SHIFT ); +#endif #endif s =find_mod_section (hdr, sechdrs,".alt.smp.init" );if (s && !is_smp ())
Signature · 1 line
1.7.9.5
PATCH 6/8 ARM: mm: LPAE: Correct virt_to_phys patching for 64 bit physical addresses
Quoted 16 lines · Santosh Shilimkar
From: Sricharan R <[email protected]>
The current
phys_to_virtpatching mechanism does not work for 64 bit physical addressesp. Note that constant used in add/sub instructions is encoded in to the last 8 bits of the opcode. So shift the_pv_offsetconstant by 24 to get it in to the correct place.The v2p patching mechanism patches the higher 32bits of physical address with a constant. While this is correct, in those platforms where the lowmem addressable physical memory spawns across 4GB boundary, a carry bit can be produced as a result of addition of lower 32bits. This has to be taken in to account and added in to the upper. The patched
__pv_offsetand va are added in lower 32bits, where__pv_offsetcan be in two's complement form whenPA_START<VA_STARTand that can result in a false carry bit.
Quoted 6 lines
e.g PA =
0x80000000VA =0xC0000000__pv_offset= PA - VA =0xC0000000(2's complement)So adding
__pv_offset+ VA should never result in a true overflow. So in order to differentiate between a true carry, a extra flag__pv_sign_flagis introduced.
I'm still wondering if this is worth bothering about.
If PA = 0x80000000 and VA = 0xC0000000 there will never be a real carry to propagate to the high word of the physical address as the VA space cannot be larger than 0x40000000.
So is there really a case where:
1) physical memory is crossing the 4GB mark, and ...
2) physical memory start address is higher than virtual memory start address needing a carry due to the 32-bit add overflow?
It is easy to create (2) just by having a different user:kernel address space split. However I wonder if (1) is likely. Sure you need a memory alias in physical space to be able to boot, however you shouldn't need to address that memory alias via virtual addresses for any significant amount of time. In fact, as soon as the MMU is turned on, there shouldn't be any issue simply using the final physical memory addresses right away.
What am I missing?
Nicolas
PATCH 6/8 ARM: mm: LPAE: Correct virt_to_phys patching for 64 bit physical addresses
Quoted 18 lines · Nicolas Pitre
Quoted 16 lines · Santosh Shilimkar
From: Sricharan R <[email protected]>
The current
phys_to_virtpatching mechanism does not work for 64 bit physical addressesp. Note that constant used in add/sub instructions is encoded in to the last 8 bits of the opcode. So shift the_pv_offsetconstant by 24 to get it in to the correct place.The v2p patching mechanism patches the higher 32bits of physical address with a constant. While this is correct, in those platforms where the lowmem addressable physical memory spawns across 4GB boundary, a carry bit can be produced as a result of addition of lower 32bits. This has to be taken in to account and added in to the upper. The patched
__pv_offsetand va are added in lower 32bits, where__pv_offsetcan be in two's complement form whenPA_START<VA_STARTand that can result in a false carry bit.
Quoted 7 lines
Quoted 6 lines
e.g PA =
0x80000000VA =0xC0000000__pv_offset= PA - VA =0xC0000000(2's complement)So adding
__pv_offset+ VA should never result in a true overflow. So in order to differentiate between a true carry, a extra flag__pv_sign_flagis introduced.
First of all thanks for the review.
Quoted 6 lines
I'm still wondering if this is worth bothering about.
If PA =
0x80000000and VA =0xC0000000there will never be a real carry to propagate to the high word of the physical address as the VA space cannot be larger than0x40000000.
Agreed.
Quoted 7 lines
So is there really a case where:
1) physical memory is crossing the 4GB mark, and ...
2) physical memory start address is higher than virtual memory start address needing a carry due to the 32-bit add overflow?
Consider below two cases of memory layout apart from one mentioned above where the carry is bit irrelevant as you rightly said.
1) PA = 0x8_0000_0000, VA= 0xC000_0000, absolute pv_offset = 0x7_4000_0000
2) PA = 0x2_8000_0000, VA= 0xC000_000, absolute pv_offset = 0x1_C000_0000
In both of these cases there a true carry which needs to be considered.
Quoted 10 lines
It is easy to create (2) just by having a different user:kernel address space split. However I wonder if (1) is likely. Sure you need a memory alias in physical space to be able to boot, however you shouldn't need to address that memory alias via virtual addresses for any significant amount of time. In fact, as soon as the MMU is turned on, there shouldn't be any issue simply using the final physical memory addresses right away.
What am I missing?
I thought about switching to the final address space along with MMU enable at startup but then based on the discussion earlier (RMK suggested), to have such a patching support in least disruptive manner, we could patch once at boot, and then re-patch at switch over. This also gives flexibility to be able to patch code post machine init. Hopefully I haven't missed your point here.
regards,
Santosh
PATCH 6/8 ARM: mm: LPAE: Correct virt_to_phys patching for 64 bit physical addresses
Quoted 40 lines · Santosh Shilimkar
Quoted 18 lines · Nicolas Pitre
Quoted 16 lines · Santosh Shilimkar
From: Sricharan R <[email protected]>
The current
phys_to_virtpatching mechanism does not work for 64 bit physical addressesp. Note that constant used in add/sub instructions is encoded in to the last 8 bits of the opcode. So shift the_pv_offsetconstant by 24 to get it in to the correct place.The v2p patching mechanism patches the higher 32bits of physical address with a constant. While this is correct, in those platforms where the lowmem addressable physical memory spawns across 4GB boundary, a carry bit can be produced as a result of addition of lower 32bits. This has to be taken in to account and added in to the upper. The patched
__pv_offsetand va are added in lower 32bits, where__pv_offsetcan be in two's complement form whenPA_START<VA_STARTand that can result in a false carry bit.Quoted 7 lines
Quoted 6 lines
e.g PA =
0x80000000VA =0xC0000000__pv_offset= PA - VA =0xC0000000(2's complement)So adding
__pv_offset+ VA should never result in a true overflow. So in order to differentiate between a true carry, a extra flag__pv_sign_flagis introduced.First of all thanks for the review.
Quoted 6 lines
I'm still wondering if this is worth bothering about.
If PA =
0x80000000and VA =0xC0000000there will never be a real carry to propagate to the high word of the physical address as the VA space cannot be larger than0x40000000.Agreed.
Quoted 4 lines
So is there really a case where:
1) physical memory is crossing the 4GB mark, and ...
Quoted 7 lines
Quoted 3 lines
2) physical memory start address is higher than virtual memory start address needing a carry due to the 32-bit add overflow?
Consider below two cases of memory layout apart from one mentioned above where the carry is bit irrelevant as you rightly said.
1) PA = 0x8_0000_0000, VA= 0xC000_0000, absolute
pv_offset= 0x7_4000_0000
This can be patched as:
mov phys_hi, #0x8 add phys_lo, virt, #0x40000000 @ carry ignored
Quoted 1 line
2) PA = 0x2_8000_0000, VA= 0xC000_000, absolute
pv_offset= 0x1_C000_0000
mov phys_hi, #0x2 add phys_lo, virt, #0xc0000000 @ carry ignored
Quoted 2 lines
In both of these cases there a true carry which needs to be considered.
Well, not really. However, if you have:
3) PA = 0x2_8000_0000, VA = 0x4000-0000, pv_offset = 0x2-4000-0000
... then you need:
mov phys_hi, #0x2 adds phys_lo, virt, #0x40000000 adc phys_hi, phys_hi, #0
My question is: how likely is this?
What is your actual physical memory start address?
If we really need to cope with the carry, then the __pv_sign_flag should instead be represented in pv_offset directly:
Taking example #2 above, that would be:
mov phys_hi, #0x1 adds phys_lo, virt, #0xc0000000 adc phys_hi, phys_hi, #0
If PA = 0x8000-0000 and VA = 0xc000-0000 then pv_offset is 0xffff-ffff-c000-0000, meaning:
mvn phys_hi, #0 add phys_lo, virt, #0xc0000000 adc phys_hi, phys_hi, #0
So that would require a special case in the patching code where a mvn with 0 is used if the high part of pv_offset is 0xffffffff.
Nicolas
PATCH 6/8 ARM: mm: LPAE: Correct virt_to_phys patching for 64 bit physical addresses
Quoted 65 lines · Nicolas Pitre
Quoted 39 lines · Santosh Shilimkar
Quoted 18 lines · Nicolas Pitre
Quoted 16 lines · Santosh Shilimkar
From: Sricharan R <[email protected]>
The current
phys_to_virtpatching mechanism does not work for 64 bit physical addressesp. Note that constant used in add/sub instructions is encoded in to the last 8 bits of the opcode. So shift the_pv_offsetconstant by 24 to get it in to the correct place.The v2p patching mechanism patches the higher 32bits of physical address with a constant. While this is correct, in those platforms where the lowmem addressable physical memory spawns across 4GB boundary, a carry bit can be produced as a result of addition of lower 32bits. This has to be taken in to account and added in to the upper. The patched
__pv_offsetand va are added in lower 32bits, where__pv_offsetcan be in two's complement form whenPA_START<VA_STARTand that can result in a false carry bit.Quoted 6 lines
Quoted 6 lines
e.g PA =
0x80000000VA =0xC0000000__pv_offset= PA - VA =0xC0000000(2's complement)So adding
__pv_offset+ VA should never result in a true overflow. So in order to differentiate between a true carry, a extra flag__pv_sign_flagis introduced.First of all thanks for the review.
Quoted 6 lines
I'm still wondering if this is worth bothering about.
If PA =
0x80000000and VA =0xC0000000there will never be a real carry to propagate to the high word of the physical address as the VA space cannot be larger than0x40000000.Agreed.
Quoted 4 lines
So is there really a case where:
1) physical memory is crossing the 4GB mark, and ...
Quoted 7 lines
Quoted 3 lines
2) physical memory start address is higher than virtual memory start address needing a carry due to the 32-bit add overflow?
Consider below two cases of memory layout apart from one mentioned above where the carry is bit irrelevant as you rightly said.
1) PA = 0x8_0000_0000, VA= 0xC000_0000, absolute
pv_offset= 0x7_4000_0000This can be patched as:
mov phys_hi, #0x8 add phys_lo, virt, #0x40000000 @ carry ignoredQuoted 1 line
2) PA = 0x2_8000_0000, VA= 0xC000_000, absolute
pv_offset= 0x1_C000_0000mov phys_hi, #0x2 add phys_lo, virt, #0xc0000000 @ carry ignoredQuoted 2 lines
In both of these cases there a true carry which needs to be considered.
Well, not really. However, if you have:
3) PA = 0x2_8000_0000, VA = 0x4000-0000,
pv_offset= 0x2-4000-0000... then you need:
Quoted 7 lines
mov phys_hi, #0x2 adds phys_lo, virt, #0x40000000 adc phys_hi, phys_hi, #0My question is: how likely is this?
What is your actual physical memory start address?
Agreed. In our case we do not have the Physical address crossing across
4GB. So ignoring the carry would have be been OK. But we are
also addressing the other case where it would really crossover.
Quoted 16 lines
If we really need to cope with the carry, then the
__pv_sign_flagshould instead be represented inpv_offsetdirectly:Taking example #2 above, that would be:
mov phys_hi, #0x1 adds phys_lo, virt, #0xc0000000 adc phys_hi, phys_hi, #0If PA = 0x8000-0000 and VA = 0xc000-0000 then
pv_offsetis 0xffff-ffff-c000-0000, meaning:mvn phys_hi, #0 add phys_lo, virt, #0xc0000000 adc phys_hi, phys_hi, #0
Quoted 5 lines
So that would require a special case in the patching code where a mvn with 0 is used if the high part of
pv_offsetis0xffffffff.Nicolas
Extending pv_offset to 64bit is really neat way. When PA > VA, then pv_offset is going to be actual value and not 2's complement. Fine here. When running from higher physical address space, we will always fall here.
So for the second case where pv_offset is 0xffffffff .., (PA < VA) is a problem only when we run from lower physical address. So we can safely assume that the higher 32bits of PA are '0' and stub it initially. In this way we can avoid the special case.
Regards,
Sricharan
PATCH 6/8 ARM: mm: LPAE: Correct virt_to_phys patching for 64 bit physical addresses
Quoted 98 lines · Sricharan R
Quoted 65 lines · Nicolas Pitre
Quoted 39 lines · Santosh Shilimkar
Quoted 18 lines · Nicolas Pitre
Quoted 16 lines · Santosh Shilimkar
From: Sricharan R <[email protected]>
The current
phys_to_virtpatching mechanism does not work for 64 bit physical addressesp. Note that constant used in add/sub instructions is encoded in to the last 8 bits of the opcode. So shift the_pv_offsetconstant by 24 to get it in to the correct place.The v2p patching mechanism patches the higher 32bits of physical address with a constant. While this is correct, in those platforms where the lowmem addressable physical memory spawns across 4GB boundary, a carry bit can be produced as a result of addition of lower 32bits. This has to be taken in to account and added in to the upper. The patched
__pv_offsetand va are added in lower 32bits, where__pv_offsetcan be in two's complement form whenPA_START<VA_STARTand that can result in a false carry bit.Quoted 6 lines
Quoted 6 lines
e.g PA =
0x80000000VA =0xC0000000__pv_offset= PA - VA =0xC0000000(2's complement)So adding
__pv_offset+ VA should never result in a true overflow. So in order to differentiate between a true carry, a extra flag__pv_sign_flagis introduced.First of all thanks for the review.
Quoted 6 lines
I'm still wondering if this is worth bothering about.
If PA =
0x80000000and VA =0xC0000000there will never be a real carry to propagate to the high word of the physical address as the VA space cannot be larger than0x40000000.Agreed.
Quoted 4 lines
So is there really a case where:
1) physical memory is crossing the 4GB mark, and ...
Quoted 7 lines
Quoted 3 lines
2) physical memory start address is higher than virtual memory start address needing a carry due to the 32-bit add overflow?
Consider below two cases of memory layout apart from one mentioned above where the carry is bit irrelevant as you rightly said.
1) PA = 0x8_0000_0000, VA= 0xC000_0000, absolute
pv_offset= 0x7_4000_0000This can be patched as:
mov phys_hi, #0x8 add phys_lo, virt, #0x40000000 @ carry ignoredQuoted 1 line
2) PA = 0x2_8000_0000, VA= 0xC000_000, absolute
pv_offset= 0x1_C000_0000mov phys_hi, #0x2 add phys_lo, virt, #0xc0000000 @ carry ignoredQuoted 2 lines
In both of these cases there a true carry which needs to be considered.
Well, not really. However, if you have:
3) PA = 0x2_8000_0000, VA = 0x4000-0000,
pv_offset= 0x2-4000-0000... then you need:
Quoted 7 lines
mov phys_hi, #0x2 adds phys_lo, virt, #0x40000000 adc phys_hi, phys_hi, #0My question is: how likely is this?
What is your actual physical memory start address?
Agreed. In our case we do not have the Physical address crossing across
4GB. So ignoring the carry would have be been OK. But we are
also addressing the other case where it would really crossover.Quoted 16 lines
If we really need to cope with the carry, then the
__pv_sign_flagshould instead be represented inpv_offsetdirectly:Taking example #2 above, that would be:
mov phys_hi, #0x1 adds phys_lo, virt, #0xc0000000 adc phys_hi, phys_hi, #0If PA = 0x8000-0000 and VA = 0xc000-0000 then
pv_offsetis 0xffff-ffff-c000-0000, meaning:mvn phys_hi, #0 add phys_lo, virt, #0xc0000000 adc phys_hi, phys_hi, #0Quoted 5 lines
So that would require a special case in the patching code where a mvn with 0 is used if the high part of
pv_offsetis0xffffffff.Nicolas
Quoted 8 lines
Extending
pv_offsetto 64bit is really neat way. When PA > VA, thenpv_offsetis going to be actual value and not 2's complement. Fine here. When running from higher physical address space, we will always fall here.So for the second case where
pv_offsetis0xffffffff.., (PA < VA) is a problem only when we run from lower physical address. So we can safely assume that the higher 32bits of PA are '0' and stub it initially. In this way we can avoid the special case.
Sorry, I missed one more point here. In the second case,we should patch it with 0x0 when (PA > VA) and with 0xffffffff when (PA < VA).
Regards,
Sricharan
PATCH 6/8 ARM: mm: LPAE: Correct virt_to_phys patching for 64 bit physical addresses
Quoted 71 lines · Sricharan R
Quoted 70 lines · Sricharan R
Quoted 69 lines · Nicolas Pitre
Quoted 39 lines · Santosh Shilimkar
Quoted 18 lines · Nicolas Pitre
Quoted 16 lines · Santosh Shilimkar
From: Sricharan R <[email protected]>
The current
phys_to_virtpatching mechanism does not work for 64 bit physical addressesp. Note that constant used in add/sub instructions is encoded in to the last 8 bits of the opcode. So shift the_pv_offsetconstant by 24 to get it in to the correct place.The v2p patching mechanism patches the higher 32bits of physical address with a constant. While this is correct, in those platforms where the lowmem addressable physical memory spawns across 4GB boundary, a carry bit can be produced as a result of addition of lower 32bits. This has to be taken in to account and added in to the upper. The patched
__pv_offsetand va are added in lower 32bits, where__pv_offsetcan be in two's complement form whenPA_START<VA_STARTand that can result in a false carry bit.Quoted 6 lines
Quoted 6 lines
e.g PA =
0x80000000VA =0xC0000000__pv_offset= PA - VA =0xC0000000(2's complement)So adding
__pv_offset+ VA should never result in a true overflow. So in order to differentiate between a true carry, a extra flag__pv_sign_flagis introduced.First of all thanks for the review.
Quoted 6 lines
I'm still wondering if this is worth bothering about.
If PA =
0x80000000and VA =0xC0000000there will never be a real carry to propagate to the high word of the physical address as the VA space cannot be larger than0x40000000.Agreed.
Quoted 4 lines
So is there really a case where:
1) physical memory is crossing the 4GB mark, and ...
Quoted 7 lines
Quoted 3 lines
2) physical memory start address is higher than virtual memory start address needing a carry due to the 32-bit add overflow?
Consider below two cases of memory layout apart from one mentioned above where the carry is bit irrelevant as you rightly said.
1) PA = 0x8_0000_0000, VA= 0xC000_0000, absolute
pv_offset= 0x7_4000_0000This can be patched as:
mov phys_hi, #0x8 add phys_lo, virt, #0x40000000 @ carry ignoredQuoted 1 line
2) PA = 0x2_8000_0000, VA= 0xC000_000, absolute
pv_offset= 0x1_C000_0000mov phys_hi, #0x2 add phys_lo, virt, #0xc0000000 @ carry ignoredQuoted 2 lines
In both of these cases there a true carry which needs to be considered.
Well, not really. However, if you have:
3) PA = 0x2_8000_0000, VA = 0x4000-0000,
pv_offset= 0x2-4000-0000... then you need:
mov phys_hi, #0x2 adds phys_lo, virt, #0x40000000 adc phys_hi, phys_hi, #0
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
My question is: how likely is this?
What is your actual physical memory start address?
Agreed. In our case we do not have the Physical address crossing across
4GB. So ignoring the carry would have be been OK. But we are
also addressing the other case where it would really crossover.
Yes. We don't need to worry this case. We can get to this with kernel:user split but nobody uses that case so we can safely ignore this case.
Quoted 25 lines
Quoted 25 lines
Quoted 16 lines
If we really need to cope with the carry, then the
__pv_sign_flagshould instead be represented inpv_offsetdirectly:Taking example #2 above, that would be:
mov phys_hi, #0x1 adds phys_lo, virt, #0xc0000000 adc phys_hi, phys_hi, #0If PA = 0x8000-0000 and VA = 0xc000-0000 then
pv_offsetis 0xffff-ffff-c000-0000, meaning:mvn phys_hi, #0 add phys_lo, virt, #0xc0000000 adc phys_hi, phys_hi, #0Quoted 5 lines
So that would require a special case in the patching code where a mvn with 0 is used if the high part of
pv_offsetis0xffffffff.Nicolas
Extending
pv_offsetto 64bit is really neat way. When PA > VA, thenpv_offsetis going to be actual value and not 2's complement. Fine here. When running from higher physical address space, we will always fall here.
Quoted 7 lines
Quoted 4 lines
So for the second case where
pv_offsetis0xffffffff.., (PA < VA) is a problem only when we run from lower physical address. So we can safely assume that the higher 32bits of PA are '0' and stub it initially. In this way we can avoid the special case.Sorry, I missed one more point here. In the second case,we should patch it with
0x0when (PA > VA) and with0xffffffffwhen (PA < VA).
As Sricharan said, we agree with your suggestion for the special case patching. It will be either 0x0 or 0xffffffff so easy to take care. We will try out the suggested changes.
Thanks a lot again.
Regards,
Santosh
PATCH 6/8 ARM: mm: LPAE: Correct virt_to_phys patching for 64 bit physical addresses
In reply to Sricharan RQuoted 103 lines · Sricharan R
Quoted 102 lines · Sricharan R
Quoted 65 lines · Nicolas Pitre
Quoted 39 lines · Santosh Shilimkar
Quoted 18 lines · Nicolas Pitre
Quoted 16 lines · Santosh Shilimkar
From: Sricharan R <[email protected]>
The current
phys_to_virtpatching mechanism does not work for 64 bit physical addressesp. Note that constant used in add/sub instructions is encoded in to the last 8 bits of the opcode. So shift the_pv_offsetconstant by 24 to get it in to the correct place.The v2p patching mechanism patches the higher 32bits of physical address with a constant. While this is correct, in those platforms where the lowmem addressable physical memory spawns across 4GB boundary, a carry bit can be produced as a result of addition of lower 32bits. This has to be taken in to account and added in to the upper. The patched
__pv_offsetand va are added in lower 32bits, where__pv_offsetcan be in two's complement form whenPA_START<VA_STARTand that can result in a false carry bit.Quoted 6 lines
Quoted 6 lines
e.g PA =
0x80000000VA =0xC0000000__pv_offset= PA - VA =0xC0000000(2's complement)So adding
__pv_offset+ VA should never result in a true overflow. So in order to differentiate between a true carry, a extra flag__pv_sign_flagis introduced.First of all thanks for the review.
Quoted 6 lines
I'm still wondering if this is worth bothering about.
If PA =
0x80000000and VA =0xC0000000there will never be a real carry to propagate to the high word of the physical address as the VA space cannot be larger than0x40000000.Agreed.
Quoted 4 lines
So is there really a case where:
1) physical memory is crossing the 4GB mark, and ...
Quoted 7 lines
Quoted 3 lines
2) physical memory start address is higher than virtual memory start address needing a carry due to the 32-bit add overflow?
Consider below two cases of memory layout apart from one mentioned above where the carry is bit irrelevant as you rightly said.
1) PA = 0x8_0000_0000, VA= 0xC000_0000, absolute
pv_offset= 0x7_4000_0000This can be patched as:
mov phys_hi, #0x8 add phys_lo, virt, #0x40000000 @ carry ignoredQuoted 1 line
2) PA = 0x2_8000_0000, VA= 0xC000_000, absolute
pv_offset= 0x1_C000_0000mov phys_hi, #0x2 add phys_lo, virt, #0xc0000000 @ carry ignoredQuoted 2 lines
In both of these cases there a true carry which needs to be considered.
Well, not really. However, if you have:
3) PA = 0x2_8000_0000, VA = 0x4000-0000,
pv_offset= 0x2-4000-0000... then you need:
Quoted 7 lines
mov phys_hi, #0x2 adds phys_lo, virt, #0x40000000 adc phys_hi, phys_hi, #0My question is: how likely is this?
What is your actual physical memory start address?
Agreed. In our case we do not have the Physical address crossing across
4GB. So ignoring the carry would have be been OK. But we are
also addressing the other case where it would really crossover.Quoted 16 lines
If we really need to cope with the carry, then the
__pv_sign_flagshould instead be represented inpv_offsetdirectly:Taking example #2 above, that would be:
mov phys_hi, #0x1 adds phys_lo, virt, #0xc0000000 adc phys_hi, phys_hi, #0If PA = 0x8000-0000 and VA = 0xc000-0000 then
pv_offsetis 0xffff-ffff-c000-0000, meaning:mvn phys_hi, #0 add phys_lo, virt, #0xc0000000 adc phys_hi, phys_hi, #0Quoted 5 lines
So that would require a special case in the patching code where a mvn with 0 is used if the high part of
pv_offsetis0xffffffff.Nicolas
Extending
pv_offsetto 64bit is really neat way. When PA > VA, thenpv_offsetis going to be actual value and not 2's complement. Fine here. When running from higher physical address space, we will always fall here.
Quoted 6 lines
Quoted 4 lines
So for the second case where
pv_offsetis0xffffffff.., (PA < VA) is a problem only when we run from lower physical address. So we can safely assume that the higher 32bits of PA are '0' and stub it initially. In this way we can avoid the special case.Sorry, I missed one more point here. In the second case,we should patch it with
0x0when (PA > VA) and with0xffffffffwhen (PA < VA).
I don't think I follow you here.
Let's assume:
phys_addr_t __pv_offset = PHYS_START - VIRT_START;
If PA = 0x0-8000-0000 and VA = 0xc000-0000 then__pv_offset = 0xffff-ffff-c000-0000.
If PA = 0x2-8000-0000 and VA = 0xc000-0000 then__pv_offset = 0x1-c000-0000.
So the __virt_to_phys() assembly stub could look like:
static inline phys_addr_t __virt_to_phys(unsigned long x)
{
phys_addr_t t;
if if (sizeof(phys_addr_t) == 4) {
__pv_stub(x, t, "add", __PV_BITS_31_24);
} else {
__pv_movhi_stub(t);
__pv_add_carry_stub(x, t);
}
return t;
}And...
#define __pv_movhi_stub(y) \
__asm__("@ __pv_movhi_stub\n" \
"1: mov %R0, %1\n" \
" .pushsection .pv_table,\"a\"\n" \
" .long 1b\n" \
" .popsection\n" \
: "=r" (y) \
: "I" (__PV_BITS_8_0))#define __pv_add_carry_stub(x, y) \
__asm__("@ __pv_add_carry_stub\n" \
"1: adds %Q0, %1, %2\n" \
" adc %R0, %R0, #0\n" \
" .pushsection .pv_table,\"a\"\n" \
" .long 1b\n" \
" .popsection\n" \
: "+r" (y) \
: "r" (x), "I" (__PV_BITS_31_24) \
: "cc")The stub bits such as __PV_BITS_8_0 can be augmented with more bits in the middle to determine the type of fixup needed. The fixup code would determine the shift needed on the value, and whether or not the low or high word of __pv_offset should be used according to those bits.
Then, in the case where a mov is patched, you need to check if the high word of __pv_offset is 0xffffffff and if so the mov should be turned into a "mvn rn, #0".
And there you are with all possible cases handled.
Nicolas
PATCH 6/8 ARM: mm: LPAE: Correct virt_to_phys patching for 64 bit physical addresses
Hi Nicolas,
Quoted 166 lines · Nicolas Pitre
Quoted 103 lines · Sricharan R
Quoted 102 lines · Sricharan R
Quoted 65 lines · Nicolas Pitre
Quoted 39 lines · Santosh Shilimkar
Quoted 18 lines · Nicolas Pitre
Quoted 16 lines · Santosh Shilimkar
From: Sricharan R <[email protected]>
The current
phys_to_virtpatching mechanism does not work for 64 bit physical addressesp. Note that constant used in add/sub instructions is encoded in to the last 8 bits of the opcode. So shift the_pv_offsetconstant by 24 to get it in to the correct place.The v2p patching mechanism patches the higher 32bits of physical address with a constant. While this is correct, in those platforms where the lowmem addressable physical memory spawns across 4GB boundary, a carry bit can be produced as a result of addition of lower 32bits. This has to be taken in to account and added in to the upper. The patched
__pv_offsetand va are added in lower 32bits, where__pv_offsetcan be in two's complement form whenPA_START<VA_STARTand that can result in a false carry bit.Quoted 6 lines
Quoted 6 lines
e.g PA =
0x80000000VA =0xC0000000__pv_offset= PA - VA =0xC0000000(2's complement)So adding
__pv_offset+ VA should never result in a true overflow. So in order to differentiate between a true carry, a extra flag__pv_sign_flagis introduced.First of all thanks for the review.
Quoted 6 lines
I'm still wondering if this is worth bothering about.
If PA =
0x80000000and VA =0xC0000000there will never be a real carry to propagate to the high word of the physical address as the VA space cannot be larger than0x40000000.Agreed.
Quoted 4 lines
So is there really a case where:
1) physical memory is crossing the 4GB mark, and ...
Quoted 7 lines
Quoted 3 lines
2) physical memory start address is higher than virtual memory start address needing a carry due to the 32-bit add overflow?
Consider below two cases of memory layout apart from one mentioned above where the carry is bit irrelevant as you rightly said.
1) PA = 0x8_0000_0000, VA= 0xC000_0000, absolute
pv_offset= 0x7_4000_0000This can be patched as:
mov phys_hi, #0x8 add phys_lo, virt, #0x40000000 @ carry ignoredQuoted 1 line
2) PA = 0x2_8000_0000, VA= 0xC000_000, absolute
pv_offset= 0x1_C000_0000mov phys_hi, #0x2 add phys_lo, virt, #0xc0000000 @ carry ignoredQuoted 2 lines
In both of these cases there a true carry which needs to be considered.
Well, not really. However, if you have:
3) PA = 0x2_8000_0000, VA = 0x4000-0000,
pv_offset= 0x2-4000-0000... then you need:
Quoted 7 lines
mov phys_hi, #0x2 adds phys_lo, virt, #0x40000000 adc phys_hi, phys_hi, #0My question is: how likely is this?
What is your actual physical memory start address?
Agreed. In our case we do not have the Physical address crossing across
4GB. So ignoring the carry would have be been OK. But we are
also addressing the other case where it would really crossover.Quoted 16 lines
If we really need to cope with the carry, then the
__pv_sign_flagshould instead be represented inpv_offsetdirectly:Taking example #2 above, that would be:
mov phys_hi, #0x1 adds phys_lo, virt, #0xc0000000 adc phys_hi, phys_hi, #0If PA = 0x8000-0000 and VA = 0xc000-0000 then
pv_offsetis 0xffff-ffff-c000-0000, meaning:mvn phys_hi, #0 add phys_lo, virt, #0xc0000000 adc phys_hi, phys_hi, #0Quoted 5 lines
So that would require a special case in the patching code where a mvn with 0 is used if the high part of
pv_offsetis0xffffffff.Nicolas
Extending
pv_offsetto 64bit is really neat way. When PA > VA, thenpv_offsetis going to be actual value and not 2's complement. Fine here. When running from higher physical address space, we will always fall here.Quoted 6 lines
Quoted 4 lines
So for the second case where
pv_offsetis0xffffffff.., (PA < VA) is a problem only when we run from lower physical address. So we can safely assume that the higher 32bits of PA are '0' and stub it initially. In this way we can avoid the special case.Sorry, I missed one more point here. In the second case,we should patch it with
0x0when (PA > VA) and with0xffffffffwhen (PA < VA).I don't think I follow you here.
Let's assume:
phys_addr_t__pv_offset=PHYS_START-VIRT_START;If PA = 0x0-8000-0000 and VA = 0xc000-0000 then
__pv_offset= 0xffff-ffff-c000-0000.If PA = 0x2-8000-0000 and VA = 0xc000-0000 then
__pv_offset= 0x1-c000-0000.So the
__virt_to_phys()assembly stub could look like:static inline
phys_addr_t__virt_to_phys(unsigned long x){ phys_addr_t t; if if (sizeof(phys_addr_t) == 4) { __pv_stub(x, t, "add", __PV_BITS_31_24); } else { __pv_movhi_stub(t); __pv_add_carry_stub(x, t); } return t; }And...
#define __pv_movhi_stub(y) \ __asm__("@ __pv_movhi_stub\n" \ "1: mov %R0, %1\n" \ " .pushsection .pv_table,\"a\"\n" \ " .long 1b\n" \ " .popsection\n" \ : "=r" (y) \ : "I" (__PV_BITS_8_0))#define __pv_add_carry_stub(x, y) \ __asm__("@ __pv_add_carry_stub\n" \ "1: adds %Q0, %1, %2\n" \ " adc %R0, %R0, #0\n" \ " .pushsection .pv_table,\"a\"\n" \ " .long 1b\n" \ " .popsection\n" \ : "+r" (y) \ : "r" (x), "I" (__PV_BITS_31_24) \ : "cc")The stub bits such as
__PV_BITS_8_0can be augmented with more bits in the middle to determine the type of fixup needed. The fixup code would determine the shift needed on the value, and whether or not the low or high word of__pv_offsetshould be used according to those bits.
Quoted 8 lines
Then, in the case where a mov is patched, you need to check if the high word of
__pv_offsetis0xffffffffand if so the mov should be turned into a "mvn rn, #0".And there you are with all possible cases handled.
Nicolas
Thanks and you have given the full details here.
Sorry if i was not clear on my previous response.
1) When i said special case can be avoided, i meant that we need not differentiate the 0xfffffff case inside the __virt_to_phy macro, but can handle it at the time of patching. Your above code makes that clear.
2) I would have ended creating separate tables for 'mov' and 'add' case. But again thanks to your above idea of augumenting the __PV_BITS, with which we can find out run time. And 'mvn' would be needed for moving '0xffffffff' . Now I can get rid of the separate section that i created for 'mov' in my previous version.
I will make the above suggestions and come back.
Regards,
Sricharan
PATCH 6/8 ARM: mm: LPAE: Correct virt_to_phys patching for 64 bit physical addresses
Quoted 3 lines · Sricharan R
Hi Nicolas,
On Thursday 25 July 2013 01:51 AM, Nicolas Pitre wrote:
[..]
Quoted 55 lines
Quoted 55 lines
I don't think I follow you here.
Let's assume:
phys_addr_t__pv_offset=PHYS_START-VIRT_START;If PA = 0x0-8000-0000 and VA = 0xc000-0000 then
__pv_offset= 0xffff-ffff-c000-0000.If PA = 0x2-8000-0000 and VA = 0xc000-0000 then
__pv_offset= 0x1-c000-0000.So the
__virt_to_phys()assembly stub could look like:static inline
phys_addr_t__virt_to_phys(unsigned long x){ phys_addr_t t; if if (sizeof(phys_addr_t) == 4) { __pv_stub(x, t, "add", __PV_BITS_31_24); } else { __pv_movhi_stub(t); __pv_add_carry_stub(x, t); } return t; }And...
#define __pv_movhi_stub(y) \ __asm__("@ __pv_movhi_stub\n" \ "1: mov %R0, %1\n" \ " .pushsection .pv_table,\"a\"\n" \ " .long 1b\n" \ " .popsection\n" \ : "=r" (y) \ : "I" (__PV_BITS_8_0))#define __pv_add_carry_stub(x, y) \ __asm__("@ __pv_add_carry_stub\n" \ "1: adds %Q0, %1, %2\n" \ " adc %R0, %R0, #0\n" \ " .pushsection .pv_table,\"a\"\n" \ " .long 1b\n" \ " .popsection\n" \ : "+r" (y) \ : "r" (x), "I" (__PV_BITS_31_24) \ : "cc")The stub bits such as
__PV_BITS_8_0can be augmented with more bits in the middle to determine the type of fixup needed. The fixup code would determine the shift needed on the value, and whether or not the low or high word of__pv_offsetshould be used according to those bits.
Quoted 6 lines
Quoted 6 lines
Then, in the case where a mov is patched, you need to check if the high word of
__pv_offsetis0xffffffffand if so the mov should be turned into a "mvn rn, #0".And there you are with all possible cases handled.
Brilliant !!
We knew you will have some tricks and better way. We were not convinced with the extra stub for 'mov' but also didn't have idea to avoid it.
Quoted 9 lines
Thanks and you have given the full details here.
Sorry if i was not clear on my previous response.
1) When i said special case can be avoided, i meant that we need not differentiate the
0xfffffffcase inside the__virt_to_phymacro, but can handle it at the time of patching. Your above code makes that clear.
Quoted 7 lines
2) I would have ended creating separate tables for 'mov' and 'add' case. But again thanks to your above idea of augumenting the
__PV_BITS, with which we can find out run time. And 'mvn' would be needed for moving '0xffffffff' . Now I can get rid of the separate section that i created for 'mov' in my previous version.
We also get rid of calling separate patching for modules as well as late patching. Overall the patch-set becomes smaller and simpler. Thanks for help.
Regards,
Santosh
PATCH 7/8 ARM: mm: Recreate kernel mappings in early_paging_init()
In reply to Santosh ShilimkarThis patch adds a step in the init sequence, in order to recreate the kernel code/data page table mappings prior to full paging initialization. This is necessary on LPAE systems that run out of a physical address space outside the 4G limit. On these systems, this implementation provides a machine descriptor hook that allows the PHYS_OFFSET to be overridden in a machine specific fashion.
Based on Cyril's initial patch. The pv_table needs to be patched again after switching to higher address space.
--- arch/arm/include/asm/mach/arch.h | 1 + arch/arm/kernel/setup.c | 3 ++ arch/arm/mm/mmu.c | 99 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+)
arch/arm/include/asm/mach/arch.h +1 -0
@@ -43,6 +43,7 @@ struct machine_desc {struct smp_operations *smp;/* SMP operations */ void (*fixup)(struct tag *,char **,struct meminfo *); +void (*init_meminfo)(void );void (*reserve)(void );/* reserve mem blocks */ void (*map_io)(void );/* IO mapping function */ void (*init_early)(void );
arch/arm/kernel/setup.c +3 -0
@@ -71,6 +71,7 @@ static int __init fpe_setup(char *line)__setup ("fpe=" , fpe_setup);#endif +extern void early_paging_init (struct machine_desc *,struct proc_info_list *);extern void paging_init (struct machine_desc *desc);extern void sanity_check_meminfo (void );extern void reboot_setup (char *str);
@@ -789,6 +790,8 @@ void __init setup_arch(char **cmdline_p)parse_early_param ();sort (&meminfo.bank, meminfo.nr_banks,sizeof (meminfo.bank[0 ]), meminfo_cmp,NULL ); + +early_paging_init (mdesc,lookup_processor_type (read_cpuid_id ()));sanity_check_meminfo ();arm_memblock_init (&meminfo, mdesc);
arch/arm/mm/mmu.c +99 -0
111 lines not on this page · raw message
Signature · 1 line
1.7.9.5
PATCH 8/8 ARM: keystone: Switch over to high physical address range
In reply to Santosh ShilimkarKeystone platforms have their physical memory mapped at an address outside the 32-bit physical range. A Keystone machine with 16G of RAM would find its memory at 0x0800000000 - 0x0bffffffff.
For boot purposes, the interconnect supports a limited alias of some of this memory within the 32-bit addressable space (0x80000000 - 0xffffffff). This aliasing is implemented in hardware, and is not intended to be used much beyond boot. For instance, DMA coherence does not work when running out of this aliased address space.
Therefore, a two pahsed boot approach is implemented.
1) Boot out of the low physical address range
2) Switch over to the high range once we're safely inside machine initialization.
This patch implements this switch over mechanism, which involves rewiring the TTBRs and page tables to point to the new physical address space. Based on earlier patch version from Cyril.
--- arch/arm/mach-keystone/keystone.c | 49 +++++++++++++++++++++++++++++++++++++ arch/arm/mach-keystone/memory.h | 24 ++++++++++++++++++ arch/arm/mach-keystone/platsmp.c | 16 +++++++++++- 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 arch/arm/mach-keystone/memory.h
arch/arm/mach-keystone/keystone.c +49 -0
@@ -20,6 +20,9 @@#include <asm/mach/arch.h> #include <asm/mach/time.h> #include <asm/smp_plat.h> +#include <asm/memory.h> + +#include "memory.h" #include "keystone.h"
@@ -44,6 +47,51 @@ static void __init keystone_init(void)of_platform_populate (NULL , of_default_bus_match_table,NULL ,NULL ); } +static phys_addr_t keystone_virt_to_idmap (unsigned long x) +{ +return (phys_addr_t)(x) -CONFIG_PAGE_OFFSET +KEYSTONE_LOW_PHYS_START ; +} + +static void __init keystone_init_meminfo (void ) +{ +bool lpae =IS_ENABLED (CONFIG_ARM_LPAE ); +bool pvpatch =IS_ENABLED (CONFIG_ARM_PATCH_PHYS_VIRT ); +phys_addr_t offset =PHYS_OFFSET -KEYSTONE_LOW_PHYS_START ; +phys_addr_t mem_start, mem_end; + +BUG_ON (meminfo.nr_banks <1 ); + mem_start = meminfo.bank[0 ].start; + mem_end = mem_start + meminfo.bank[0 ].size -1 ; + +/* nothing to do if we are running out of the <32-bit space */ +if (mem_start >=KEYSTONE_LOW_PHYS_START && + mem_end <=KEYSTONE_LOW_PHYS_END ) +return ; + +if (!lpae || !pvpatch) { +pr_crit ("Enable %s%s%s to run outside 32-bit space\n" , + !lpae ?__stringify (CONFIG_ARM_LPAE ) :"" , + (!lpae && !pvpatch) ?" and " :"" , + !pvpatch ?__stringify (CONFIG_ARM_PATCH_PHYS_VIRT ) :"" ); + } + +if (mem_start <KEYSTONE_HIGH_PHYS_START || + mem_end >KEYSTONE_HIGH_PHYS_END ) { +pr_crit ("Invalid address space for memory (%08llx-%08llx)\n" , + (u64 )mem_start, (u64 )mem_end); + } + + offset +=KEYSTONE_HIGH_PHYS_START ; + __pv_phys_offset = offset; + __pv_offset = (offset -PAGE_OFFSET ); + __pv_offset >>=PV_LOW_SHIFT ; + +/* Populate the arch idmap hook */ + arch_virt_to_idmap = keystone_virt_to_idmap; + +pr_info ("Switching to high address space at 0x%llx\n" , (u64 )offset); +} +static const char *keystone_match[]__initconst = {"ti,keystone-evm" ,NULL ,
@@ -72,4 +120,5 @@ DT_MACHINE_START(KEYSTONE, "Keystone") .init_machine = keystone_init, .dt_compat = keystone_match, .restart = keystone_restart, + .init_meminfo = keystone_init_meminfo,MACHINE_END
arch/arm/mach-keystone/memory.h new file +24 -0
@@ -0,0 +1,24 @@ +/* +* Copyright 2013 Texas Instruments, Inc. +* Santosh Shilimkar <[email protected]> +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +*/ +#ifndef __MACH_MEMORY_H +#define __MACH_MEMORY_H + +#define MAX_PHYSMEM_BITS 36 +#define SECTION_SIZE_BITS 34 + +#define KEYSTONE_LOW_PHYS_START 0x80000000ULL +#define KEYSTONE_LOW_PHYS_SIZE 0x80000000ULL/* 2G */ +#define KEYSTONE_LOW_PHYS_END (KEYSTONE_LOW_PHYS_START + \ + KEYSTONE_LOW_PHYS_SIZE - 1) + +#define KEYSTONE_HIGH_PHYS_START 0x800000000ULL +#define KEYSTONE_HIGH_PHYS_SIZE 0x400000000ULL/* 16G */ +#define KEYSTONE_HIGH_PHYS_END (KEYSTONE_HIGH_PHYS_START + \ + KEYSTONE_HIGH_PHYS_SIZE - 1) +#endif /* _MACH_MEMORY_H */
arch/arm/mach-keystone/platsmp.c +15 -1
@@ -18,13 +18,15 @@#include <asm/smp_plat.h> #include <asm/prom.h> +#include <asm/tlbflush.h> +#include <asm/pgtable.h> #include "keystone.h" static int __cpuinit keystone_smp_boot_secondary (unsigned int cpu,struct task_struct *idle) { -unsigned long start =virt_to_phys (&secondary_startup); +unsigned long start =virt_to_idmap (&secondary_startup);int error; pr_debug("keystone-smp: booting cpu %d, vector %08lx\n",
@@ -37,7 +39,19 @@ static int __cpuinit keystone_smp_boot_secondary(unsigned int cpu,return error; } +static void __cpuinit keystone_smp_secondary_initmem (unsigned int cpu) +{ +bool lpae =IS_ENABLED (CONFIG_ARM_LPAE ); + +if (lpae) { +pgd_t *pgd0 =pgd_offset_k (0 ); +cpu_set_ttbr (1 ,__pa (pgd0) +TTBR1_OFFSET ); +local_flush_tlb_all (); + } +} +struct smp_operations keystone_smp_ops__initdata = { .smp_init_cpus = arm_dt_init_cpu_maps, .smp_boot_secondary = keystone_smp_boot_secondary, + .smp_secondary_init = keystone_smp_secondary_initmem, };
Signature · 1 line
1.7.9.5
cover ARM: mm: Extend the runtime patch stub for PAE systems
In reply to Santosh ShilimkarQuoted 9 lines · Santosh Shilimkar
Based on discussion/debate on Cyril's generic code patching framework, we cooked up this series which basically trying to extend the existing v2p runtime patching for LPAE machines which can have physical memory beyond 4 GB. Keystone is one such ARM machine.
We think the 64 bit patching can be still made better than the proposed patch in the series and hence seeking expert comments from RMK, Nico and others. Last patch in the series added to just give perspective on how machine code will make use of the available bits from the series.
FYI I'm currently on vacation and only handling quick emails.
I'll review your series in a week or so.
Nicolas
cover ARM: mm: Extend the runtime patch stub for PAE systems
Quoted 7 lines · Nicolas Pitre
Quoted 5 lines · Santosh Shilimkar
Based on discussion/debate on Cyril's generic code patching framework, we cooked up this series which basically trying to extend the existing v2p runtime patching for LPAE machines which can have physical memory beyond 4 GB. Keystone is one such ARM machine.
Quoted 9 lines
Quoted 4 lines
We think the 64 bit patching can be still made better than the proposed patch in the series and hence seeking expert comments from RMK, Nico and others. Last patch in the series added to just give perspective on how machine code will make use of the available bits from the series.
FYI I'm currently on vacation and only handling quick emails.
I'll review your series in a week or so.
NP. Thanks for the note.
Regards,
Santosh
cover ARM: mm: Extend the runtime patch stub for PAE systems
In reply to Nicolas PitreRussell, Nico,
Ping.
Quoted 16 lines · Nicolas Pitre
Quoted 9 lines · Santosh Shilimkar
Based on discussion/debate on Cyril's generic code patching framework, we cooked up this series which basically trying to extend the existing v2p runtime patching for LPAE machines which can have physical memory beyond 4 GB. Keystone is one such ARM machine.
We think the 64 bit patching can be still made better than the proposed patch in the series and hence seeking expert comments from RMK, Nico and others. Last patch in the series added to just give perspective on how machine code will make use of the available bits from the series.
FYI I'm currently on vacation and only handling quick emails.
I'll review your series in a week or so.