arm/arm64: mmu fixes and feature
Subsystems (2)
v2 cover arm/arm64: mmu fixes and feature
The fixes are getting tests to run on 32-bit ARM again and also fixing virt_to_phys(). The feature is to allow a unit test to start with the MMU disabled. Couple minor cleanups thrown in too.
I tested x86 and ppc64 over TCG, but not s390 (please-test: David).
v2:
- fix compiling on s390x by adding bitops/fls [reported by David]
- fix page_memalign order calculation for non-power-of-2 number of pages (need to round up) [drew]
Andrew Jones (12):
arm/arm64: cleanup alloc.h includes
arm/arm64: add pgtable to thread_info
arm/arm64: fix virt_to_phys
arm/arm64: flush page table cache when installing entries arm/arm64: setup: don't allow gaps in phys range
phys_alloc: ensure we account all allocations
page_alloc: allow initialization before setup_vm call
bitops: add fls
page_alloc: add yet another memalign lib/auxinfo: add flags field
arm/arm64: allow setup_vm to be skipped arm/arm64: sieve should start with the mmu off
arm/Makefile.common | 5 ++++- arm/cstart.S | 19 ++++++++++++++++++- arm/cstart64.S | 17 ++++++++++++++++- lib/alloc_page.c | 36 ++++++++++++++++++++++++++++++++++++ lib/alloc_page.h | 2 ++ lib/alloc_phys.c | 10 ++++++++++ lib/alloc_phys.h | 6 ++++-- lib/arm/asm/bitops.h | 2 ++ lib/arm/asm/mmu.h | 5 +++++ lib/arm/asm/page.h | 8 +++----- lib/arm/asm/pgtable.h | 16 ++++++++++++---- lib/arm/asm/setup.h | 1 - lib/arm/asm/thread_info.h | 1 + lib/arm/mmu.c | 43 ++++++++++++++++++++++++++++++++++++++----- lib/arm/processor.c | 1 + lib/arm/setup.c | 22 ++++++++++++++-------- lib/arm/smp.c | 7 ++++++- lib/arm64/asm/bitops.h | 2 ++ lib/arm64/asm/mmu.h | 5 +++++ lib/arm64/asm/page.h | 10 +++------- lib/arm64/asm/pgtable.h | 12 ++++++++++-- lib/arm64/processor.c | 1 + lib/arm64/spinlock.c | 1 + lib/auxinfo.c | 13 ++++++++++++- lib/auxinfo.h | 6 ++++++ lib/bitops.h | 38 ++++++++++++++++++++++++++++++++++++++ lib/ppc64/asm/bitops.h | 2 ++ lib/s390x/asm/bitops.h | 10 ++++++++++ lib/vmalloc.c | 9 ++++++--- lib/x86/asm/bitops.h | 2 ++ 30 files changed, 270 insertions(+), 42 deletions(-) create mode 100644 lib/s390x/asm/bitops.h
Signature · 1 line
2.13.6
PATCH v2 1/12 arm/arm64: cleanup alloc.h includes
It used to be necessary for the phys_addr_t typedef, but now it doesn't make sense in a few places.
--- lib/arm/asm/setup.h | 1 - lib/arm/mmu.c | 1 - lib/arm64/asm/page.h | 2 -- lib/arm64/spinlock.c | 1 + 4 files changed, 1 insertion(+), 4 deletions(-)
lib/arm/asm/setup.h +0 -1
@@ -6,7 +6,6 @@* This work is licensed under the terms of the GNU LGPL, version 2. */ #include <libcflat.h> -#include <alloc.h> /* phys_addr_t */ #include <asm/page.h> #include <asm/pgtable-hwdef.h>
lib/arm/mmu.c +0 -1
@@ -12,7 +12,6 @@#include <asm/setup.h> #include <asm/page.h> -#include "alloc.h" #include "alloc_page.h" #include "vmalloc.h" #include <asm/pgtable-hwdef.h>
lib/arm64/asm/page.h +0 -2
@@ -23,8 +23,6 @@#define PAGE_ALIGN (addr) ALIGN(addr, PAGE_SIZE) -#include <alloc.h> -typedef u64 pteval_t ;typedef u64 pmdval_t ;typedef u64 pgdval_t ;
lib/arm64/spinlock.c +1 -0
@@ -5,6 +5,7 @@* * This work is licensed under the terms of the GNU LGPL, version 2. */ +#include <libcflat.h> #include <asm/spinlock.h> #include <asm/barrier.h> #include <asm/mmu.h>
Signature · 1 line
2.13.6
PATCH v2 2/12 arm/arm64: add pgtable to thread_info
In reply to Andrew Jones--- lib/arm/asm/thread_info.h | 1 + lib/arm/mmu.c | 5 +++-- lib/arm/processor.c | 1 + lib/arm/smp.c | 1 + lib/arm64/processor.c | 1 + 5 files changed, 7 insertions(+), 2 deletions(-)
lib/arm/asm/thread_info.h +1 -0
@@ -47,6 +47,7 @@ static inline void *thread_stack_alloc(void)struct thread_info {int cpu;unsigned int flags; +void *pgtable;#ifdef __arm__exception_fn exception_handlers[EXCPTN_MAX ];#else
lib/arm/mmu.c +3 -2
@@ -54,12 +54,13 @@ void mmu_mark_disabled(int cpu)extern void asm_mmu_enable (phys_addr_t pgtable);void mmu_enable (pgd_t *pgtable) { -int cpu =current_thread_info ()->cpu; +struct thread_info *info =current_thread_info ();asm_mmu_enable (__pa (pgtable));flush_tlb_all (); -mmu_mark_enabled (cpu); + info->pgtable = pgtable; +mmu_mark_enabled (info->cpu); }extern void asm_mmu_disable (void );
lib/arm/processor.c +1 -0
@@ -124,6 +124,7 @@ void start_usr(void (*func)(void *arg), void *arg, unsigned long sp_usr) sp_usr &= (~7UL );/* stack ptr needs 8-byte alignment */ thread_info_init (thread_info_sp (sp_usr),TIF_USER_MODE ); +thread_info_sp (sp_usr)->pgtable =current_thread_info ()->pgtable; asmvolatile ( "mrs r0, cpsr\n"
lib/arm/smp.c +1 -0
@@ -33,6 +33,7 @@ secondary_entry_fn secondary_cinit(void)secondary_entry_fn entry;thread_info_init (ti,0 ); + ti->pgtable = mmu_idmap;mmu_mark_enabled (ti->cpu);/*
lib/arm64/processor.c +1 -0
@@ -238,6 +238,7 @@ void start_usr(void (*func)(void *arg), void *arg, unsigned long sp_usr) sp_usr &= (~15UL );/* stack ptr needs 16-byte alignment */ __thread_info_init (thread_info_sp (sp_usr),TIF_USER_MODE ); +thread_info_sp (sp_usr)->pgtable =current_thread_info ()->pgtable; asmvolatile ( "mov x0, %0\n"
Signature · 1 line
2.13.6
PATCH v2 3/12 arm/arm64: fix virt_to_phys
In reply to Andrew JonesSince switching to the vm_memalign() allocator virt_to_phys() hasn't been returning the correct address, as it was assuming an identity map.
--- lib/arm/asm/page.h | 8 +++----- lib/arm/asm/pgtable.h | 16 ++++++++++++---- lib/arm/mmu.c | 20 ++++++++++++++++++++ lib/arm64/asm/page.h | 8 +++----- lib/arm64/asm/pgtable.h | 12 ++++++++++-- 5 files changed, 48 insertions(+), 16 deletions(-)
lib/arm/asm/page.h +3 -5
@@ -34,16 +34,14 @@ typedef struct { pteval_t pgprot; } pgprot_t;#define __pgd (x) ((pgd_t) { (x) } )#define __pgprot (x) ((pgprot_t) { (x) } ) -#ifndef __virt_to_phys -#define __phys_to_virt (x) ((unsigned long) (x)) -#define __virt_to_phys (x) (x) -#endif -#define __va (x) ((void *)__phys_to_virt((phys_addr_t)(x)))#define __pa (x) __virt_to_phys((unsigned long)(x))#define virt_to_pfn (kaddr) (__pa(kaddr) >> PAGE_SHIFT)#define pfn_to_virt (pfn) __va((pfn) << PAGE_SHIFT) +extern phys_addr_t __virt_to_phys (unsigned long addr); +extern unsigned long __phys_to_virt (phys_addr_t addr); +#endif /* !__ASSEMBLY__ */ #endif /* _ASMARM_PAGE_H_ */
lib/arm/asm/pgtable.h +12 -4
@@ -14,6 +14,14 @@* This work is licensed under the terms of the GNU GPL, version 2. */ +/* +* We can convert va <=> pa page table addresses with simple casts +* because we always allocate their pages with alloc_page(), and +* alloc_page() always returns identity mapped pages. +*/ +#define pgtable_va (x) ((void *)(unsigned long)(x)) +#define pgtable_pa (x) ((unsigned long)(x)) +#define pgd_none (pgd) (!pgd_val(pgd))#define pmd_none (pmd) (!pmd_val(pmd))#define pte_none (pte) (!pte_val(pte))
@@ -32,7 +40,7 @@ static inline pgd_t *pgd_alloc(void)static inline pmd_t *pgd_page_vaddr (pgd_t pgd) { -return __va (pgd_val (pgd) &PHYS_MASK & (s32 )PAGE_MASK ); +return pgtable_va (pgd_val (pgd) &PHYS_MASK & (s32 )PAGE_MASK ); }#define pmd_index (addr) \
@@ -52,14 +60,14 @@ static inline pmd_t *pmd_alloc(pgd_t *pgd, unsigned long addr) {if (pgd_none (*pgd)) {pmd_t *pmd =pmd_alloc_one (); -pgd_val (*pgd) =__pa (pmd) |PMD_TYPE_TABLE ; +pgd_val (*pgd) =pgtable_pa (pmd) |PMD_TYPE_TABLE ; }return pmd_offset (pgd, addr); }static inline pte_t *pmd_page_vaddr (pmd_t pmd) { -return __va (pmd_val (pmd) &PHYS_MASK & (s32 )PAGE_MASK ); +return pgtable_va (pmd_val (pmd) &PHYS_MASK & (s32 )PAGE_MASK ); }#define pte_index (addr) \
@@ -79,7 +87,7 @@ static inline pte_t *pte_alloc(pmd_t *pmd, unsigned long addr) {if (pmd_none (*pmd)) {pte_t *pte =pte_alloc_one (); -pmd_val (*pmd) =__pa (pte) |PMD_TYPE_TABLE ; +pmd_val (*pmd) =pgtable_pa (pte) |PMD_TYPE_TABLE ; }return pte_offset (pmd, addr); }
lib/arm/mmu.c +20 -0
@@ -171,3 +171,23 @@ void *setup_mmu(phys_addr_t phys_end)mmu_enable (mmu_idmap);return mmu_idmap; } + +phys_addr_t __virt_to_phys (unsigned long addr) +{ +if (mmu_enabled ()) { +pgd_t *pgtable =current_thread_info ()->pgtable; +return virt_to_pte_phys (pgtable, (void *)addr); + } +return addr; +} + +unsigned long __phys_to_virt (phys_addr_t addr) +{ +/* +* We don't guarantee that phys_to_virt(virt_to_phys(vaddr)) == vaddr, but +* the default page tables do identity map all physical addresses, which +* means phys_to_virt(virt_to_phys((void *)paddr)) == paddr. +*/ +assert (!mmu_enabled () ||__virt_to_phys (addr) == addr); +return addr; +}
lib/arm64/asm/page.h +3 -5
@@ -42,16 +42,14 @@ typedef struct { pgd_t pgd; } pmd_t;#define pmd_val (x) (pgd_val((x).pgd))#define __pmd (x) ((pmd_t) { __pgd(x) } ) -#ifndef __virt_to_phys -#define __phys_to_virt (x) ((unsigned long) (x)) -#define __virt_to_phys (x) (x) -#endif -#define __va (x) ((void *)__phys_to_virt((phys_addr_t)(x)))#define __pa (x) __virt_to_phys((unsigned long)(x))#define virt_to_pfn (kaddr) (__pa(kaddr) >> PAGE_SHIFT)#define pfn_to_virt (pfn) __va((pfn) << PAGE_SHIFT) +extern phys_addr_t __virt_to_phys (unsigned long addr); +extern unsigned long __phys_to_virt (phys_addr_t addr); +#endif /* !__ASSEMBLY__ */ #endif /* _ASMARM64_PAGE_H_ */
lib/arm64/asm/pgtable.h +10 -2
@@ -18,6 +18,14 @@#include <asm/page.h> #include <asm/pgtable-hwdef.h> +/* +* We can convert va <=> pa page table addresses with simple casts +* because we always allocate their pages with alloc_page(), and +* alloc_page() always returns identity mapped pages. +*/ +#define pgtable_va (x) ((void *)(unsigned long)(x)) +#define pgtable_pa (x) ((unsigned long)(x)) +#define pgd_none (pgd) (!pgd_val(pgd))#define pmd_none (pmd) (!pmd_val(pmd))#define pte_none (pte) (!pte_val(pte))
@@ -40,7 +48,7 @@ static inline pgd_t *pgd_alloc(void)static inline pte_t *pmd_page_vaddr (pmd_t pmd) { -return __va (pmd_val (pmd) &PHYS_MASK & (s32 )PAGE_MASK ); +return pgtable_va (pmd_val (pmd) &PHYS_MASK & (s32 )PAGE_MASK ); }#define pte_index (addr) \
@@ -60,7 +68,7 @@ static inline pte_t *pte_alloc(pmd_t *pmd, unsigned long addr) {if (pmd_none (*pmd)) {pte_t *pte =pte_alloc_one (); -pmd_val (*pmd) =__pa (pte) |PMD_TYPE_TABLE ; +pmd_val (*pmd) =pgtable_pa (pte) |PMD_TYPE_TABLE ; }return pte_offset (pmd, addr); }
Signature · 1 line
2.13.6
PATCH v2 4/12 arm/arm64: flush page table cache when installing entries
In reply to Andrew JonesThis fixes the use of non-identity mapped page table entries for arm32 and AArch32 unit tests.
--- lib/arm/asm/mmu.h | 5 +++++ lib/arm/mmu.c | 17 +++++++++++++++-- lib/arm64/asm/mmu.h | 5 +++++ 3 files changed, 25 insertions(+), 2 deletions(-)
lib/arm/asm/mmu.h +5 -0
@@ -37,6 +37,11 @@ static inline void flush_tlb_page(unsigned long vaddr) isb(); } +static inline void flush_dcache_addr (unsigned long vaddr) +{ + asmvolatile ("mcr p15, 0, %0, c7, c14, 1" ::"r" (vaddr)); +} +#include <asm/mmu-api.h> #endif /* __ASMARM_MMU_H_ */
lib/arm/mmu.c +15 -2
@@ -73,6 +73,17 @@ void mmu_disable(void)asm_mmu_disable (); } +static void flush_entry (pgd_t *pgtable,uintptr_t vaddr) +{ +pgd_t *pgd =pgd_offset (pgtable, vaddr); +pmd_t *pmd =pmd_offset (pgd, vaddr); + +flush_dcache_addr ((ulong )pgd); +flush_dcache_addr ((ulong )pmd); +flush_dcache_addr ((ulong )pte_offset (pmd, vaddr)); +flush_tlb_page (vaddr); +} +static pteval_t *get_pte (pgd_t *pgtable,uintptr_t vaddr) {pgd_t *pgd =pgd_offset (pgtable, vaddr);
@@ -85,8 +96,9 @@ static pteval_t *get_pte(pgd_t *pgtable, uintptr_t vaddr)static pteval_t *install_pte (pgd_t *pgtable,uintptr_t vaddr,pteval_t pte) {pteval_t *p_pte =get_pte (pgtable, vaddr); + *p_pte = pte; -flush_tlb_page (vaddr); +flush_entry (pgtable, vaddr);return p_pte; }
@@ -136,8 +148,9 @@ void mmu_set_range_sect(pgd_t *pgtable, uintptr_t virt_offset,pgd_val (*pgd) = paddr;pgd_val (*pgd) |=PMD_TYPE_SECT |PMD_SECT_AF |PMD_SECT_S ;pgd_val (*pgd) |=pgprot_val (prot); +flush_dcache_addr ((ulong )pgd); +flush_tlb_page (vaddr); } -flush_tlb_all (); }void *setup_mmu (phys_addr_t phys_end)
lib/arm64/asm/mmu.h +5 -0
@@ -26,6 +26,11 @@ static inline void flush_tlb_page(unsigned long vaddr) dsb(ish); } +static inline void flush_dcache_addr (unsigned long vaddr) +{ + asmvolatile ("dc civac, %0" ::"r" (vaddr)); +} +#include <asm/mmu-api.h> #endif /* __ASMARM64_MMU_H_ */
Signature · 1 line
2.13.6
PATCH v2 5/12 arm/arm64: setup: don't allow gaps in phys range
In reply to Andrew JonesSet the basic range parameters PHYS_OFFSET and PHYS_END to the primary memory region range parameters to ensure there are no gaps, as the regions in the device tree may not be contiguous. If a unit test wants to use memory in another region, then it can get it from mem_regions.
--- lib/arm/setup.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
lib/arm/setup.c +3 -3
@@ -75,7 +75,7 @@ static void mem_init(phys_addr_t freemem_start) nr_regs =dt_get_memory_params (regs,NR_MEM_REGIONS );assert (nr_regs >0 ); - primary.end =0 ; + primary = (struct mem_region ){0 };for (i =0 ; i < nr_regs; ++i) { mem_regions[i].start = regs[i].addr;
@@ -102,8 +102,8 @@ static void mem_init(phys_addr_t freemem_start)assert (primary.end !=0 );assert (!(mem.start & ~PHYS_MASK ) && !((mem.end -1 ) & ~PHYS_MASK )); - __phys_offset = mem.start;/* PHYS_OFFSET */ - __phys_end = mem.end;/* PHYS_END */ + __phys_offset = primary.start;/* PHYS_OFFSET */ + __phys_end = primary.end;/* PHYS_END */ phys_alloc_init (freemem_start, primary.end - freemem_start);phys_alloc_set_minimum_alignment (SMP_CACHE_BYTES );
Signature · 1 line
2.13.6
PATCH v2 6/12 phys_alloc: ensure we account all allocations
In reply to Andrew JonesAlso throw another assert into phys_alloc_aligned_safe.
--- lib/alloc_phys.c | 10 ++++++++++ lib/alloc_phys.h | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-)
lib/alloc_phys.c +10 -0
@@ -81,6 +81,8 @@ static phys_addr_t phys_alloc_aligned_safe(phys_addr_t size,if (safe &&sizeof (long ) ==4 ) top_safe =MIN (top_safe,1ULL <<32 ); +assert (base < top_safe); + addr =ALIGN (base, align); size += addr - base;
@@ -116,6 +118,14 @@ void phys_alloc_get_unused(phys_addr_t *p_base, phys_addr_t *p_top) { *p_base = base; *p_top = top; +if (base == top) +return ; +spin_lock (&lock); + regions[nr_regions].base = base; + regions[nr_regions].size = top - base; + ++nr_regions; + base = top; +spin_unlock (&lock); }static void *early_memalign (size_t alignment,size_t size)
lib/alloc_phys.h +4 -2
@@ -37,8 +37,10 @@ extern void phys_alloc_set_minimum_alignment(phys_addr_t align);extern void phys_alloc_show (void );/* -* phys_alloc_get_unused returns the addresses for the still-unused part -* of the initial free memory region passed to phys_alloc_init. +* phys_alloc_get_unused allocates all remaining memory from the region +* passed to phys_alloc_init, returning the newly allocated memory's base +* and top addresses. phys_allo_get_unused will still return base and top +* when no free memory is remaining, but base will equal top. */ extern void phys_alloc_get_unused (phys_addr_t *p_base,phys_addr_t *p_top);
Signature · 1 line
2.13.6
PATCH v2 7/12 page_alloc: allow initialization before setup_vm call
In reply to Andrew Jones--- lib/alloc_page.c | 5 +++++ lib/alloc_page.h | 1 + lib/vmalloc.c | 9 ++++++--- 3 files changed, 12 insertions(+), 3 deletions(-)
lib/alloc_page.c +5 -0
@@ -13,6 +13,11 @@static struct spinlock lock;static void *freelist =0 ; +bool page_alloc_initialized (void ) +{ +return freelist !=0 ; +} +void free_pages (void *mem,unsigned long size) {void *old_freelist;
lib/alloc_page.h +1 -0
@@ -8,6 +8,7 @@#ifndef ALLOC_PAGE_H #define ALLOC_PAGE_H 1 +bool page_alloc_initialized (void );void *alloc_page ();void *alloc_pages (unsigned long order);void free_page (void *page);
lib/vmalloc.c +6 -3
@@ -94,9 +94,12 @@ void setup_vm()return ;phys_alloc_get_unused (&base, &top); - base = (base +PAGE_SIZE -1 ) & -PAGE_SIZE ; - top = top & -PAGE_SIZE ; -free_pages (phys_to_virt (base), top - base); +assert (base != top ||page_alloc_initialized ()); +if (!page_alloc_initialized ()) { + base = (base +PAGE_SIZE -1 ) & -PAGE_SIZE ; + top = top & -PAGE_SIZE ; +free_pages (phys_to_virt (base), top - base); + } page_root =setup_mmu (top); alloc_ops = &vmalloc_ops; }
Signature · 1 line
2.13.6
PATCH v2 8/12 bitops: add fls
In reply to Andrew Jones--- lib/arm/asm/bitops.h | 2 ++ lib/arm64/asm/bitops.h | 2 ++ lib/bitops.h | 38 ++++++++++++++++++++++++++++++++++++++ lib/ppc64/asm/bitops.h | 2 ++ lib/s390x/asm/bitops.h | 10 ++++++++++ lib/x86/asm/bitops.h | 2 ++ 6 files changed, 56 insertions(+) create mode 100644 lib/s390x/asm/bitops.h
lib/arm/asm/bitops.h +2 -0
@@ -15,6 +15,8 @@#define BITS_PER_LONG 32 +#define HAVE_BUILTIN_FLS 1 +#define ATOMIC_BITOP (insn, mask, word) \ ({ \ unsigned long tmp1, tmp2; \
lib/arm64/asm/bitops.h +2 -0
@@ -15,6 +15,8 @@#define BITS_PER_LONG 64 +#define HAVE_BUILTIN_FLS 1 +#define ATOMIC_BITOP (insn, mask, word) \ ({ \ unsigned long tmp1, tmp2; \
lib/bitops.h +38 -0
@@ -33,4 +33,42 @@#define GENMASK_ULL (h, l) \ (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h)))) +#ifndef HAVE_BUILTIN_FLS +static inline unsigned long fls (unsigned long word) +{ +int num =BITS_PER_LONG -1 ; + +#if BITS_PER_LONG ==64 +if (!(word & (~0ul <<32 ))) { + num -=32 ; + word <<=32 ; + } +#endif +if (!(word & (~0ul << (BITS_PER_LONG -16 )))) { + num -=16 ; + word <<=16 ; + } +if (!(word & (~0ul << (BITS_PER_LONG -8 )))) { + num -=8 ; + word <<=8 ; + } +if (!(word & (~0ul << (BITS_PER_LONG -4 )))) { + num -=4 ; + word <<=4 ; + } +if (!(word & (~0ul << (BITS_PER_LONG -2 )))) { + num -=2 ; + word <<=2 ; + } +if (!(word & (~0ul << (BITS_PER_LONG -1 )))) + num -=1 ; +return num; +} +#else +static inline unsigned long fls (unsigned long word) +{ +return BITS_PER_LONG -__builtin_clzl (word) -1 ; +} +#endif +#endif
lib/ppc64/asm/bitops.h +2 -0
@@ -7,4 +7,6 @@#define BITS_PER_LONG 64 +#define HAVE_BUILTIN_FLS 1 +#endif
lib/s390x/asm/bitops.h new file +10 -0
@@ -0,0 +1,10 @@ +#ifndef _ASMS390X_BITOPS_H_ +#define _ASMS390X_BITOPS_H_ + +#ifndef _BITOPS_H_ +#error only <bitops.h> can be included directly +#endif + +#define BITS_PER_LONG 64 + +#endif
lib/x86/asm/bitops.h +2 -0
@@ -11,4 +11,6 @@#define BITS_PER_LONG 32#endif +#define HAVE_BUILTIN_FLS 1 +#endif
Signature · 1 line
2.13.6
PATCH v2 9/12 page_alloc: add yet another memalign
In reply to Andrew JonesIf we want both early alloc ops and alloc_page(), then it's best to just give all the memory to page_alloc and then base the early alloc ops on that.
--- lib/alloc_page.c | 31 +++++++++++++++++++++++++++++++ lib/alloc_page.h | 1 + 2 files changed, 32 insertions(+)
lib/alloc_page.c +31 -0
@@ -5,7 +5,9 @@* with page granularity. */ #include "libcflat.h" +#include "alloc.h" #include "alloc_phys.h" +#include "bitops.h" #include <asm/page.h> #include <asm/io.h> #include <asm/spinlock.h>
@@ -134,3 +136,32 @@ void free_page(void *page) freelist = page;spin_unlock (&lock); } + +static void *page_memalign (size_t alignment,size_t size) +{ +unsigned long n =ALIGN (size,PAGE_SIZE ) >>PAGE_SHIFT ; +unsigned long order; + +if (!size) +return NULL ; + + order =is_power_of_2 (n) ?fls (n) :fls (n) +1 ; + +return alloc_pages (order); +} + +static void page_free (void *mem,size_t size) +{ +free_pages (mem, size); +} + +static struct alloc_ops page_alloc_ops = { + .memalign = page_memalign, + .free = page_free, + .align_min =PAGE_SIZE , +}; + +void page_alloc_ops_enable (void ) +{ + alloc_ops = &page_alloc_ops; +}
lib/alloc_page.h +1 -0
@@ -9,6 +9,7 @@#define ALLOC_PAGE_H 1bool page_alloc_initialized (void ); +void page_alloc_ops_enable (void );void *alloc_page ();void *alloc_pages (unsigned long order);void free_page (void *page);
Signature · 1 line
2.13.6
PATCH v2 10/12 lib/auxinfo: add flags field
In reply to Andrew Jones--- arm/Makefile.common | 3 ++- lib/auxinfo.c | 13 ++++++++++++- lib/auxinfo.h | 1 + 3 files changed, 15 insertions(+), 2 deletions(-)
arm/Makefile.common +2 -1
@@ -22,6 +22,7 @@ all: directories $(tests-all) ################################################################## phys_base = $(LOADADDR) +AUXFLAGS ?= 0x0 CFLAGS += -std=gnu99 CFLAGS += -ffreestanding
@@ -69,7 +70,7 @@ FLATLIBS = $(libcflat) $(LIBFDT_archive) $(libgcc) $(libeabi) $(CC) $(LDFLAGS) -o $@ \ -Wl,-T,$(SRCDIR)/arm/flat.lds,--build-id=none,-Ttext=$(start_addr) \ $(filter %.o, $^) $(FLATLIBS) \ - $(SRCDIR)/lib/auxinfo.c -DPROGNAME=\"$(@:.elf=.flat)\" + $(SRCDIR)/lib/auxinfo.c -DPROGNAME=\"$(@:.elf=.flat)\" -DAUXFLAGS=$(AUXFLAGS) %.flat: %.elf $(OBJCOPY) -O binary $^ $@
lib/auxinfo.c +12 -1
@@ -1,2 +1,13 @@#include "auxinfo.h" -struct auxinfo auxinfo = {PROGNAME }; + +#ifndef PROGNAME +#define PROGNAME ((void *)0) +#endif +#ifndef AUXFLAGS +#define AUXFLAGS 0 +#endif + +struct auxinfo auxinfo = { + .progname =PROGNAME , + .flags =AUXFLAGS , +};
lib/auxinfo.h +1 -0
@@ -6,6 +6,7 @@#define _AUXINFO_H_struct auxinfo {const char *progname; +unsigned long flags; };/* No extern! Define a common symbol. */
Signature · 1 line
2.13.6
PATCH v2 11/12 arm/arm64: allow setup_vm to be skipped
In reply to Andrew JonesDetermine whether or not to enable the MMU during setup with an auxinfo flag. This gives unit tests that need to start cpu0 at main() with the MMU off, and no page tables constructed, the option to do so. The physical page allocator is now used as the basis for alloc_ops, allowing both malloc() and page_alloc() to work without a setup_vm() call. The unit test can still call setup_vm() itself later. Secondaries will also start in their entry points with the MMU off. If page tables have already been constructed by another CPU, and are pointed to by e.g. 'pgtable', then the secondary can easily enable the MMU with mmu_enable(pgtable).
Naturally unit tests that start multiple CPUs with the MMU off need to keep track of each CPU's MMU enable status and which set of ops are pointed to by alloc_ops. Also note, spinlocks may not work as expected with the MMU off. IOW, this option gives a unit test plenty of rope to shoot itself with.
--- arm/cstart.S | 19 ++++++++++++++++++- arm/cstart64.S | 17 ++++++++++++++++- lib/arm/setup.c | 16 +++++++++++----- lib/arm/smp.c | 8 ++++++-- lib/auxinfo.h | 5 +++++ 5 files changed, 56 insertions(+), 9 deletions(-)
arm/cstart.S +18 -1
@@ -6,6 +6,7 @@ * This work is licensed under the terms of the GNU LGPL, version 2. */ #define __ASSEMBLY__ +#include <auxinfo.h> #include <asm/thread_info.h> #include <asm/asm-offsets.h> #include <asm/ptrace.h>
@@ -57,7 +58,12 @@ start: /* complete setup */ pop {r0-r1} bl setup + bl get_mmu_off + cmp r0, #0 + bne 1f + bl setup_vm +1: /* run the test */ ldr r0, =__argc ldr r0, [r0]
@@ -96,14 +102,25 @@ exceptions_init: .text +.global get_mmu_off +get_mmu_off: + ldr r0, =auxinfo + ldr r0, [r0, #4] + and r0, #AUXINFO_MMU_OFF + mov pc, lr + .global secondary_entry secondary_entry: - /* enable the MMU */ + /* enable the MMU unless requested off */ + bl get_mmu_off + cmp r0, #0 + bne 1f mov r1, #0 ldr r0, =mmu_idmap ldr r0, [r0] bl asm_mmu_enable +1: /* * Set the stack, and set up vector table * and exception stacks. Exception stacks
arm/cstart64.S +16 -1
@@ -6,6 +6,7 @@ * This work is licensed under the terms of the GNU GPL, version 2. */ #define __ASSEMBLY__ +#include <auxinfo.h> #include <asm/asm-offsets.h> #include <asm/ptrace.h> #include <asm/processor.h>
@@ -38,7 +39,11 @@ start: /* complete setup */ ldp x0, x1, [sp], #16 bl setup + bl get_mmu_off + cbnz x0, 1f + bl setup_vm +1: /* run the test */ adrp x0, __argc ldr x0, [x0, :lo12:__argc]
@@ -59,6 +64,13 @@ exceptions_init: .text +.globl get_mmu_off +get_mmu_off: + adrp x0, auxinfo + ldr x0, [x0, :lo12:auxinfo + 8] + and x0, x0, #AUXINFO_MMU_OFF + ret + .globl secondary_entry secondary_entry: /* Enable FP/ASIMD */
@@ -68,11 +80,14 @@ secondary_entry: /* set up exception handling */ bl exceptions_init - /* enable the MMU */ + /* enable the MMU unless requested off */ + bl get_mmu_off + cbnz x0, 1f adrp x0, mmu_idmap ldr x0, [x0, :lo12:mmu_idmap] bl asm_mmu_enable +1: /* set the stack */ adrp x0, secondary_data ldr x0, [x0, :lo12:secondary_data]
lib/arm/setup.c +11 -5
@@ -15,11 +15,11 @@#include <devicetree.h> #include <alloc.h> #include <alloc_phys.h> +#include <alloc_page.h> #include <argv.h> #include <asm/thread_info.h> #include <asm/setup.h> #include <asm/page.h> -#include <asm/mmu.h> #include <asm/smp.h> extern unsigned long stacktop;
@@ -70,6 +70,7 @@ static void mem_init(phys_addr_t freemem_start)struct mem_region primary, mem = { .start = (phys_addr_t)-1 , }; +phys_addr_t base, top;int nr_regs, i; nr_regs =dt_get_memory_params (regs,NR_MEM_REGIONS );
@@ -108,7 +109,14 @@ static void mem_init(phys_addr_t freemem_start)phys_alloc_init (freemem_start, primary.end - freemem_start);phys_alloc_set_minimum_alignment (SMP_CACHE_BYTES ); -setup_vm (); +phys_alloc_get_unused (&base, &top); + base =PAGE_ALIGN (base); + top = top &PAGE_MASK ; +assert (sizeof (long ) ==8 || !(base >>32 )); +if (sizeof (long ) !=8 && (top >>32 ) !=0 ) + top = ((uint64_t )1 <<32 ); +free_pages ((void *)(unsigned long )base, top - base); +page_alloc_ops_enable (); }void setup (const void *fdt)
@@ -156,14 +164,12 @@ void setup(const void *fdt) }/* call init functions */ +mem_init (PAGE_ALIGN ((unsigned long )freemem));cpu_init ();/* cpu_init must be called before thread_info_init */ thread_info_init (current_thread_info (),0 ); -/* thread_info_init must be called before mem_init */ -mem_init (PAGE_ALIGN ((unsigned long )freemem)); -/* mem_init must be called before io_init */ io_init ();
lib/arm/smp.c +6 -2
@@ -6,6 +6,7 @@* This work is licensed under the terms of the GNU LGPL, version 2. */ #include <libcflat.h> +#include <auxinfo.h> #include <asm/thread_info.h> #include <asm/spinlock.h> #include <asm/cpumask.h>
@@ -33,8 +34,11 @@ secondary_entry_fn secondary_cinit(void)secondary_entry_fn entry;thread_info_init (ti,0 ); - ti->pgtable = mmu_idmap; -mmu_mark_enabled (ti->cpu); + +if (!(auxinfo.flags &AUXINFO_MMU_OFF )) { + ti->pgtable = mmu_idmap; +mmu_mark_enabled (ti->cpu); + }/* * Save secondary_data.entry locally to avoid opening a race
lib/auxinfo.h +5 -0
@@ -4,6 +4,10 @@*/ #ifndef _AUXINFO_H_ #define _AUXINFO_H_ + +#define AUXINFO_MMU_OFF (1 << 0) + +#ifndef __ASSEMBLY__ struct auxinfo { const char *progname; unsigned long flags;
@@ -12,3 +16,4 @@ struct auxinfo {/* No extern! Define a common symbol. */ struct auxinfo auxinfo;#endif +#endif
Signature · 1 line
2.13.6
PATCH v2 12/12 arm/arm64: sieve should start with the mmu off
In reply to Andrew Jones--- arm/Makefile.common | 2 ++ 1 file changed, 2 insertions(+)
arm/Makefile.common +2 -0
@@ -20,6 +20,8 @@ tests-common += $(TEST_DIR)/sieve.flat tests-all = $(tests-common) $(tests) all: directories $(tests-all) +$(TEST_DIR)/sieve.elf: AUXFLAGS = 0x1 + ################################################################## phys_base = $(LOADADDR) AUXFLAGS ?= 0x0
Signature · 1 line
2.13.6
Quoted 10 lines · Andrew Jones
The fixes are getting tests to run on 32-bit ARM again and also fixing
virt_to_phys(). The feature is to allow a unit test to start with the MMU disabled. Couple minor cleanups thrown in too.I tested x86 and ppc64 over TCG, but not s390 (please-test: David).
v2:
- fix compiling on s390x by adding bitops/fls [reported by David]
- fixpage_memalignorder calculation for non-power-of-2 number of pages (need to round up) [drew]
PASS selftest-setup (14 tests)
FAIL intercept (20 tests, 3 unexpected failures)
PASS emulator (89 tests, 1 skipped)
PASS sieve
Which is expected on my version of QEMU s390x tcg. So virtual memory access still works fine.
Quoted 48 lines
Andrew Jones (12):
arm/arm64: cleanupalloc.hincludes
arm/arm64: add pgtable tothread_info
arm/arm64: fixvirt_to_phys
arm/arm64: flush page table cache when installing entries arm/arm64: setup: don't allow gaps in phys range
phys_alloc: ensure we account all allocations
page_alloc: allow initialization beforesetup_vmcall
bitops: add fls
page_alloc: add yet another memalign lib/auxinfo: add flags field
arm/arm64: allowsetup_vmto be skipped arm/arm64: sieve should start with the mmu offarm/Makefile.common | 5 ++++- arm/cstart.S | 19 ++++++++++++++++++- arm/cstart64.S | 17 ++++++++++++++++- lib/alloc_page.c | 36 ++++++++++++++++++++++++++++++++++++ lib/alloc_page.h | 2 ++ lib/alloc_phys.c | 10 ++++++++++ lib/alloc_phys.h | 6 ++++-- lib/arm/asm/bitops.h | 2 ++ lib/arm/asm/mmu.h | 5 +++++ lib/arm/asm/page.h | 8 +++----- lib/arm/asm/pgtable.h | 16 ++++++++++++---- lib/arm/asm/setup.h | 1 - lib/arm/asm/thread_info.h | 1 + lib/arm/mmu.c | 43 ++++++++++++++++++++++++++++++++++++++----- lib/arm/processor.c | 1 + lib/arm/setup.c | 22 ++++++++++++++-------- lib/arm/smp.c | 7 ++++++- lib/arm64/asm/bitops.h | 2 ++ lib/arm64/asm/mmu.h | 5 +++++ lib/arm64/asm/page.h | 10 +++------- lib/arm64/asm/pgtable.h | 12 ++++++++++-- lib/arm64/processor.c | 1 + lib/arm64/spinlock.c | 1 + lib/auxinfo.c | 13 ++++++++++++- lib/auxinfo.h | 6 ++++++ lib/bitops.h | 38 ++++++++++++++++++++++++++++++++++++++ lib/ppc64/asm/bitops.h | 2 ++ lib/s390x/asm/bitops.h | 10 ++++++++++ lib/vmalloc.c | 9 ++++++--- lib/x86/asm/bitops.h | 2 ++ 30 files changed, 270 insertions(+), 42 deletions(-) create mode 100644 lib/s390x/asm/bitops.h
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH kvm-unit-tests v2 06/12] phys_alloc: ensure we account all allocations
In reply to Andrew JonesQuoted 41 lines · Andrew Jones
Also throw another assert into
phys_alloc_aligned_safe.Signed-off-by Andrew Jones <[email protected]>--- lib/alloc_phys.c | 10 ++++++++++ lib/alloc_phys.h | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-)lib/alloc_phys.c +10
-0@@ -81,6 +81,8 @@ static phys_addr_t phys_alloc_aligned_safe(phys_addr_t size,if (safe &&sizeof (long ) ==4 ) top_safe =MIN (top_safe,1ULL <<32 ); +assert (base < top_safe); + addr =ALIGN (base, align); size += addr - base;@@ -116,6 +118,14 @@ void phys_alloc_get_unused(phys_addr_t *p_base, phys_addr_t *p_top) { *p_base = base; *p_top = top; +if (base == top) +return ; +spin_lock (&lock); + regions[nr_regions].base = base; + regions[nr_regions].size = top - base; + ++nr_regions; + base = top; +spin_unlock (&lock); }static void *early_memalign (size_t alignment,size_t size)lib/alloc_phys.h +0
-0
lib/alloc_phys.h extern void phys_alloc_set_minimum_alignment(phys_addr_t align); · line 42
Quoted 8 lines
lib/alloc_phys.h +3
-2@@ -37,8 +37,10 @@ extern void phys_alloc_set_minimum_alignment(phys_addr_t align);extern void phys_alloc_show (void );/* -* phys_alloc_get_unused returns the addresses for the still-unused part -* of the initial free memory region passed to phys_alloc_init. +* phys_alloc_get_unused allocates all remaining memory from the region +* passed to phys_alloc_init, returning the newly allocated memory's base +* and top addresses. phys_allo_get_unused will still return base and top
s/phys_allo_get_unused/phys_alloc_get_unused/
Looks good to me
Signature · 3 lines
Thanks, David / dhildenb