Home | History | Annotate | Line # | Download | only in arm32
arm32_kvminit.c revision 1.67
      1 /*	$NetBSD: arm32_kvminit.c,v 1.67 2020/12/12 09:27:31 skrll Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2002, 2003, 2005  Genetec Corporation.  All rights reserved.
      5  * Written by Hiroyuki Bessho for Genetec Corporation.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of Genetec Corporation may not be used to endorse or
     16  *    promote products derived from this software without specific prior
     17  *    written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  *
     31  * Copyright (c) 2001 Wasabi Systems, Inc.
     32  * All rights reserved.
     33  *
     34  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed for the NetBSD Project by
     47  *	Wasabi Systems, Inc.
     48  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     49  *    or promote products derived from this software without specific prior
     50  *    written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     54  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     55  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     56  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     57  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     58  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     59  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     60  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     61  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     62  * POSSIBILITY OF SUCH DAMAGE.
     63  *
     64  * Copyright (c) 1997,1998 Mark Brinicombe.
     65  * Copyright (c) 1997,1998 Causality Limited.
     66  * All rights reserved.
     67  *
     68  * Redistribution and use in source and binary forms, with or without
     69  * modification, are permitted provided that the following conditions
     70  * are met:
     71  * 1. Redistributions of source code must retain the above copyright
     72  *    notice, this list of conditions and the following disclaimer.
     73  * 2. Redistributions in binary form must reproduce the above copyright
     74  *    notice, this list of conditions and the following disclaimer in the
     75  *    documentation and/or other materials provided with the distribution.
     76  * 3. All advertising materials mentioning features or use of this software
     77  *    must display the following acknowledgement:
     78  *	This product includes software developed by Mark Brinicombe
     79  *	for the NetBSD Project.
     80  * 4. The name of the company nor the name of the author may be used to
     81  *    endorse or promote products derived from this software without specific
     82  *    prior written permission.
     83  *
     84  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     85  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     86  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     87  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     88  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     89  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     90  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     91  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     92  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     93  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     94  * SUCH DAMAGE.
     95  *
     96  * Copyright (c) 2007 Microsoft
     97  * All rights reserved.
     98  *
     99  * Redistribution and use in source and binary forms, with or without
    100  * modification, are permitted provided that the following conditions
    101  * are met:
    102  * 1. Redistributions of source code must retain the above copyright
    103  *    notice, this list of conditions and the following disclaimer.
    104  * 2. Redistributions in binary form must reproduce the above copyright
    105  *    notice, this list of conditions and the following disclaimer in the
    106  *    documentation and/or other materials provided with the distribution.
    107  * 3. All advertising materials mentioning features or use of this software
    108  *    must display the following acknowledgement:
    109  *	This product includes software developed by Microsoft
    110  *
    111  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
    112  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    113  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    114  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTERS BE LIABLE FOR ANY DIRECT,
    115  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    116  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    117  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    118  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    119  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    120  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    121  * SUCH DAMAGE.
    122  */
    123 
    124 #include "opt_arm_debug.h"
    125 #include "opt_arm_start.h"
    126 #include "opt_fdt.h"
    127 #include "opt_multiprocessor.h"
    128 
    129 #include <sys/cdefs.h>
    130 __KERNEL_RCSID(0, "$NetBSD: arm32_kvminit.c,v 1.67 2020/12/12 09:27:31 skrll Exp $");
    131 
    132 #include <sys/param.h>
    133 
    134 #include <sys/asan.h>
    135 #include <sys/bus.h>
    136 #include <sys/device.h>
    137 #include <sys/kernel.h>
    138 #include <sys/reboot.h>
    139 
    140 #include <dev/cons.h>
    141 
    142 #include <uvm/uvm_extern.h>
    143 
    144 #include <arm/arm32/machdep.h>
    145 #include <arm/bootconfig.h>
    146 #include <arm/db_machdep.h>
    147 #include <arm/locore.h>
    148 #include <arm/undefined.h>
    149 
    150 #if defined(FDT)
    151 #include <arch/evbarm/fdt/platform.h>
    152 #include <arm/fdt/arm_fdtvar.h>
    153 #include <dev/fdt/fdt_memory.h>
    154 #endif
    155 
    156 #ifdef MULTIPROCESSOR
    157 #ifndef __HAVE_CPU_UAREA_ALLOC_IDLELWP
    158 #error __HAVE_CPU_UAREA_ALLOC_IDLELWP required to not waste pages for idlestack
    159 #endif
    160 #endif
    161 
    162 #ifdef VERBOSE_INIT_ARM
    163 #define VPRINTF(...)	printf(__VA_ARGS__)
    164 #else
    165 #define VPRINTF(...)	__nothing
    166 #endif
    167 
    168 struct bootmem_info bootmem_info;
    169 
    170 extern void *msgbufaddr;
    171 paddr_t msgbufphys;
    172 paddr_t physical_start;
    173 paddr_t physical_end;
    174 
    175 extern char etext[];
    176 extern char __data_start[], _edata[];
    177 extern char __bss_start[], __bss_end__[];
    178 extern char _end[];
    179 
    180 /* Page tables for mapping kernel VM */
    181 #define KERNEL_L2PT_VMDATA_NUM	8	/* start with 32MB of KVM */
    182 
    183 #ifdef KASAN
    184 vaddr_t kasan_kernelstart;
    185 vaddr_t kasan_kernelsize;
    186 
    187 #define	KERNEL_L2PT_KASAN_NUM	howmany(VM_KERNEL_KASAN_SIZE, L2_S_SEGSIZE)
    188 bool kasan_l2pts_created  __attribute__((__section__(".data"))) = false;
    189 pv_addr_t kasan_l2pt[KERNEL_L2PT_KASAN_NUM];
    190 #else
    191 #define KERNEL_L2PT_KASAN_NUM	0
    192 #endif
    193 
    194 u_long kern_vtopdiff __attribute__((__section__(".data")));
    195 
    196 void
    197 arm32_bootmem_init(paddr_t memstart, psize_t memsize, vsize_t kernelstart)
    198 {
    199 	struct bootmem_info * const bmi = &bootmem_info;
    200 	pv_addr_t *pv = bmi->bmi_freeblocks;
    201 
    202 	/*
    203 	 * FDT/generic start fills in kern_vtopdiff early
    204 	 */
    205 #if defined(__HAVE_GENERIC_START)
    206 	extern char KERNEL_BASE_virt[];
    207 	extern char const __stop__init_memory[];
    208 
    209 	VPRINTF("%s: kern_vtopdiff=%#lx\n", __func__, kern_vtopdiff);
    210 
    211 	vaddr_t kstartva = trunc_page((vaddr_t)KERNEL_BASE_virt);
    212 	vaddr_t kendva = round_page((vaddr_t)__stop__init_memory);
    213 
    214 	kernelstart = KERN_VTOPHYS(kstartva);
    215 
    216 	VPRINTF("%s: kstartva=%#lx, kernelstart=%#lx\n", __func__, kstartva, kernelstart);
    217 #else
    218 	vaddr_t kendva = round_page((vaddr_t)_end);
    219 
    220 #if defined(KERNEL_BASE_VOFFSET)
    221 	kern_vtopdiff = KERNEL_BASE_VOFFSET;
    222 #else
    223 	KASSERT(memstart == kernelstart);
    224 	kern_vtopdiff = KERNEL_BASE + memstart;
    225 #endif
    226 #endif
    227 	paddr_t kernelend = KERN_VTOPHYS(kendva);
    228 
    229 	VPRINTF("%s: memstart=%#lx, memsize=%#lx\n", __func__,
    230 	    memstart, memsize);
    231 	VPRINTF("%s: kernelstart=%#lx, kernelend=%#lx\n", __func__,
    232 	    kernelstart, kernelend);
    233 
    234 	physical_start = bmi->bmi_start = memstart;
    235 	physical_end = bmi->bmi_end = memstart + memsize;
    236 #ifndef ARM_HAS_LPAE
    237 	if (physical_end == 0) {
    238 		physical_end = -PAGE_SIZE;
    239 		memsize -= PAGE_SIZE;
    240 		bmi->bmi_end -= PAGE_SIZE;
    241 		VPRINTF("%s: memsize shrunk by a page to avoid ending at 4GB\n",
    242 		    __func__);
    243 	}
    244 #endif
    245 	physmem = memsize / PAGE_SIZE;
    246 
    247 	/*
    248 	 * Let's record where the kernel lives.
    249 	 */
    250 
    251 	bmi->bmi_kernelstart = kernelstart;
    252 	bmi->bmi_kernelend = kernelend;
    253 
    254 #if defined(FDT)
    255 	fdt_memory_remove_range(bmi->bmi_kernelstart,
    256 	    bmi->bmi_kernelend - bmi->bmi_kernelstart);
    257 #endif
    258 
    259 	VPRINTF("%s: kernel phys start %#lx end %#lx\n", __func__, kernelstart,
    260 	    kernelend);
    261 
    262 #if 0
    263 	// XXX Makes RPI abort
    264 	KASSERT((kernelstart & (L2_S_SEGSIZE - 1)) == 0);
    265 #endif
    266 	/*
    267 	 * Now the rest of the free memory must be after the kernel.
    268 	 */
    269 	pv->pv_pa = bmi->bmi_kernelend;
    270 	pv->pv_va = KERN_PHYSTOV(pv->pv_pa);
    271 	pv->pv_size = bmi->bmi_end - bmi->bmi_kernelend;
    272 	bmi->bmi_freepages += pv->pv_size / PAGE_SIZE;
    273 	VPRINTF("%s: adding %lu free pages: [%#lx..%#lx] (VA %#lx)\n",
    274 	    __func__, pv->pv_size / PAGE_SIZE, pv->pv_pa,
    275 	    pv->pv_pa + pv->pv_size - 1, pv->pv_va);
    276 	pv++;
    277 
    278 	/*
    279 	 * Add a free block for any memory before the kernel.
    280 	 */
    281 	if (bmi->bmi_start < bmi->bmi_kernelstart) {
    282 		pv->pv_pa = bmi->bmi_start;
    283 		pv->pv_va = KERN_PHYSTOV(pv->pv_pa);
    284 		pv->pv_size = bmi->bmi_kernelstart - pv->pv_pa;
    285 		bmi->bmi_freepages += pv->pv_size / PAGE_SIZE;
    286 		VPRINTF("%s: adding %lu free pages: [%#lx..%#lx] (VA %#lx)\n",
    287 		    __func__, pv->pv_size / PAGE_SIZE, pv->pv_pa,
    288 		    pv->pv_pa + pv->pv_size - 1, pv->pv_va);
    289 		pv++;
    290 	}
    291 
    292 	bmi->bmi_nfreeblocks = pv - bmi->bmi_freeblocks;
    293 
    294 	SLIST_INIT(&bmi->bmi_freechunks);
    295 	SLIST_INIT(&bmi->bmi_chunks);
    296 }
    297 
    298 static bool
    299 concat_pvaddr(pv_addr_t *acc_pv, pv_addr_t *pv)
    300 {
    301 	if (acc_pv->pv_pa + acc_pv->pv_size == pv->pv_pa
    302 	    && acc_pv->pv_va + acc_pv->pv_size == pv->pv_va
    303 	    && acc_pv->pv_prot == pv->pv_prot
    304 	    && acc_pv->pv_cache == pv->pv_cache) {
    305 #if 0
    306 		VPRINTF("%s: appending pv %p (%#lx..%#lx) to %#lx..%#lx\n",
    307 		    __func__, pv, pv->pv_pa, pv->pv_pa + pv->pv_size,
    308 		    acc_pv->pv_pa, acc_pv->pv_pa + acc_pv->pv_size);
    309 #endif
    310 		acc_pv->pv_size += pv->pv_size;
    311 		return true;
    312 	}
    313 
    314 	return false;
    315 }
    316 
    317 static void
    318 add_pages(struct bootmem_info *bmi, pv_addr_t *pv)
    319 {
    320 	pv_addr_t **pvp = &SLIST_FIRST(&bmi->bmi_chunks);
    321 	while ((*pvp) != NULL && (*pvp)->pv_va <= pv->pv_va) {
    322 		pv_addr_t * const pv0 = (*pvp);
    323 		KASSERT(SLIST_NEXT(pv0, pv_list) == NULL || pv0->pv_pa < SLIST_NEXT(pv0, pv_list)->pv_pa);
    324 		if (concat_pvaddr(pv0, pv)) {
    325 			VPRINTF("%s: %s pv %p (%#lx..%#lx) to %#lx..%#lx\n",
    326 			    __func__, "appending", pv,
    327 			    pv->pv_pa, pv->pv_pa + pv->pv_size - 1,
    328 			    pv0->pv_pa, pv0->pv_pa + pv0->pv_size - pv->pv_size - 1);
    329 			pv = SLIST_NEXT(pv0, pv_list);
    330 			if (pv != NULL && concat_pvaddr(pv0, pv)) {
    331 				VPRINTF("%s: %s pv %p (%#lx..%#lx) to %#lx..%#lx\n",
    332 				    __func__, "merging", pv,
    333 				    pv->pv_pa, pv->pv_pa + pv->pv_size - 1,
    334 				    pv0->pv_pa,
    335 				    pv0->pv_pa + pv0->pv_size - pv->pv_size - 1);
    336 				SLIST_REMOVE_AFTER(pv0, pv_list);
    337 				SLIST_INSERT_HEAD(&bmi->bmi_freechunks, pv, pv_list);
    338 			}
    339 			return;
    340 		}
    341 		KASSERT(pv->pv_va != (*pvp)->pv_va);
    342 		pvp = &SLIST_NEXT(*pvp, pv_list);
    343 	}
    344 	KASSERT((*pvp) == NULL || pv->pv_va < (*pvp)->pv_va);
    345 	pv_addr_t * const new_pv = SLIST_FIRST(&bmi->bmi_freechunks);
    346 	KASSERT(new_pv != NULL);
    347 	SLIST_REMOVE_HEAD(&bmi->bmi_freechunks, pv_list);
    348 	*new_pv = *pv;
    349 	SLIST_NEXT(new_pv, pv_list) = *pvp;
    350 	(*pvp) = new_pv;
    351 
    352 	VPRINTF("%s: adding pv %p (pa %#lx, va %#lx, %lu pages) ",
    353 	    __func__, new_pv, new_pv->pv_pa, new_pv->pv_va,
    354 	    new_pv->pv_size / PAGE_SIZE);
    355 	if (SLIST_NEXT(new_pv, pv_list)) {
    356 		VPRINTF("before pa %#lx\n", SLIST_NEXT(new_pv, pv_list)->pv_pa);
    357 	} else {
    358 		VPRINTF("at tail\n");
    359 	}
    360 }
    361 
    362 static void
    363 valloc_pages(struct bootmem_info *bmi, pv_addr_t *pv, size_t npages,
    364     int prot, int cache, bool zero_p)
    365 {
    366 	size_t nbytes = npages * PAGE_SIZE;
    367 	pv_addr_t *free_pv = bmi->bmi_freeblocks;
    368 	size_t free_idx = 0;
    369 	static bool l1pt_found;
    370 
    371 	KASSERT(npages > 0);
    372 
    373 	/*
    374 	 * If we haven't allocated the kernel L1 page table and we are aligned
    375 	 * at a L1 table boundary, alloc the memory for it.
    376 	 */
    377 	if (!l1pt_found
    378 	    && (free_pv->pv_pa & (L1_TABLE_SIZE - 1)) == 0
    379 	    && free_pv->pv_size >= L1_TABLE_SIZE) {
    380 		l1pt_found = true;
    381 		VPRINTF(" l1pt");
    382 
    383 		valloc_pages(bmi, &kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE,
    384 		    VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE, true);
    385 		add_pages(bmi, &kernel_l1pt);
    386 	}
    387 
    388 	while (nbytes > free_pv->pv_size) {
    389 		free_pv++;
    390 		free_idx++;
    391 		if (free_idx == bmi->bmi_nfreeblocks) {
    392 			panic("%s: could not allocate %zu bytes",
    393 			    __func__, nbytes);
    394 		}
    395 	}
    396 
    397 	/*
    398 	 * As we allocate the memory, make sure that we don't walk over
    399 	 * our current first level translation table.
    400 	 */
    401 	KASSERT((armreg_ttbr_read() & ~(L1_TABLE_SIZE - 1)) != free_pv->pv_pa);
    402 
    403 #if defined(FDT)
    404 	fdt_memory_remove_range(free_pv->pv_pa, nbytes);
    405 #endif
    406 	pv->pv_pa = free_pv->pv_pa;
    407 	pv->pv_va = free_pv->pv_va;
    408 	pv->pv_size = nbytes;
    409 	pv->pv_prot = prot;
    410 	pv->pv_cache = cache;
    411 
    412 	/*
    413 	 * If PTE_PAGETABLE uses the same cache modes as PTE_CACHE
    414 	 * just use PTE_CACHE.
    415 	 */
    416 	if (cache == PTE_PAGETABLE
    417 	    && pte_l1_s_cache_mode == pte_l1_s_cache_mode_pt
    418 	    && pte_l2_l_cache_mode == pte_l2_l_cache_mode_pt
    419 	    && pte_l2_s_cache_mode == pte_l2_s_cache_mode_pt)
    420 		pv->pv_cache = PTE_CACHE;
    421 
    422 	free_pv->pv_pa += nbytes;
    423 	free_pv->pv_va += nbytes;
    424 	free_pv->pv_size -= nbytes;
    425 	if (free_pv->pv_size == 0) {
    426 		--bmi->bmi_nfreeblocks;
    427 		for (; free_idx < bmi->bmi_nfreeblocks; free_idx++) {
    428 			free_pv[0] = free_pv[1];
    429 		}
    430 	}
    431 
    432 	bmi->bmi_freepages -= npages;
    433 
    434 	if (zero_p)
    435 		memset((void *)pv->pv_va, 0, nbytes);
    436 }
    437 
    438 void
    439 arm32_kernel_vm_init(vaddr_t kernel_vm_base, vaddr_t vectors, vaddr_t iovbase,
    440     const struct pmap_devmap *devmap, bool mapallmem_p)
    441 {
    442 	struct bootmem_info * const bmi = &bootmem_info;
    443 #ifdef MULTIPROCESSOR
    444 	const size_t cpu_num = arm_cpu_max;
    445 #else
    446 	const size_t cpu_num = 1;
    447 #endif
    448 
    449 #ifdef ARM_HAS_VBAR
    450 	const bool map_vectors_p = false;
    451 #elif defined(CPU_ARMV7) || defined(CPU_ARM11)
    452 	const bool map_vectors_p = vectors == ARM_VECTORS_HIGH
    453 	    || (armreg_pfr1_read() & ARM_PFR1_SEC_MASK) == 0;
    454 #else
    455 	const bool map_vectors_p = true;
    456 #endif
    457 
    458 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
    459 	KASSERT(mapallmem_p);
    460 #ifdef ARM_MMU_EXTENDED
    461 	/*
    462 	 * The direct map VA space ends at the start of the kernel VM space.
    463 	 */
    464 	pmap_directlimit = kernel_vm_base;
    465 #else
    466 	KASSERT(kernel_vm_base - KERNEL_BASE >= physical_end - physical_start);
    467 #endif /* ARM_MMU_EXTENDED */
    468 #endif /* __HAVE_MM_MD_DIRECT_MAPPED_PHYS */
    469 
    470 	/*
    471 	 * Calculate the number of L2 pages needed for mapping the
    472 	 * kernel + data + stuff.  Assume 2 L2 pages for kernel, 1 for vectors,
    473 	 * and 1 for IO
    474 	 */
    475 	size_t kernel_size = bmi->bmi_kernelend;
    476 	kernel_size -= (bmi->bmi_kernelstart & -L2_S_SEGSIZE);
    477 	kernel_size += L1_TABLE_SIZE;
    478 	kernel_size += PAGE_SIZE * KERNEL_L2PT_VMDATA_NUM;
    479 	kernel_size += PAGE_SIZE * KERNEL_L2PT_KASAN_NUM;
    480 	if (map_vectors_p) {
    481 		kernel_size += PAGE_SIZE;	/* L2PT for VECTORS */
    482 	}
    483 	if (iovbase) {
    484 		kernel_size += PAGE_SIZE;	/* L2PT for IO */
    485 	}
    486 	kernel_size +=
    487 	    cpu_num * (ABT_STACK_SIZE + FIQ_STACK_SIZE + IRQ_STACK_SIZE
    488 	    + UND_STACK_SIZE + UPAGES) * PAGE_SIZE;
    489 	kernel_size += round_page(MSGBUFSIZE);
    490 	kernel_size += 0x10000;	/* slop */
    491 	if (!mapallmem_p) {
    492 		kernel_size += PAGE_SIZE
    493 		    * howmany(kernel_size, L2_S_SEGSIZE);
    494 	}
    495 	kernel_size = round_page(kernel_size);
    496 
    497 	/*
    498 	 * Now we know how many L2 pages it will take.
    499 	 */
    500 	const size_t KERNEL_L2PT_KERNEL_NUM =
    501 	    howmany(kernel_size, L2_S_SEGSIZE);
    502 
    503 	VPRINTF("%s: %zu L2 pages are needed to map %#zx kernel bytes\n",
    504 	    __func__, KERNEL_L2PT_KERNEL_NUM, kernel_size);
    505 
    506 	KASSERT(KERNEL_L2PT_KERNEL_NUM + KERNEL_L2PT_VMDATA_NUM < __arraycount(bmi->bmi_l2pts));
    507 	pv_addr_t * const kernel_l2pt = bmi->bmi_l2pts;
    508 	pv_addr_t * const vmdata_l2pt = kernel_l2pt + KERNEL_L2PT_KERNEL_NUM;
    509 	pv_addr_t msgbuf;
    510 	pv_addr_t text;
    511 	pv_addr_t data;
    512 	pv_addr_t chunks[__arraycount(bmi->bmi_l2pts) + 11];
    513 #if ARM_MMU_XSCALE == 1
    514 	pv_addr_t minidataclean;
    515 #endif
    516 
    517 	/*
    518 	 * We need to allocate some fixed page tables to get the kernel going.
    519 	 *
    520 	 * We are going to allocate our bootstrap pages from the beginning of
    521 	 * the free space that we just calculated.  We allocate one page
    522 	 * directory and a number of page tables and store the physical
    523 	 * addresses in the bmi_l2pts array in bootmem_info.
    524 	 *
    525 	 * The kernel page directory must be on a 16K boundary.  The page
    526 	 * tables must be on 4K boundaries.  What we do is allocate the
    527 	 * page directory on the first 16K boundary that we encounter, and
    528 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    529 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    530 	 * least one 16K aligned region.
    531 	 */
    532 
    533 	VPRINTF("%s: allocating page tables for", __func__);
    534 	for (size_t i = 0; i < __arraycount(chunks); i++) {
    535 		SLIST_INSERT_HEAD(&bmi->bmi_freechunks, &chunks[i], pv_list);
    536 	}
    537 
    538 	kernel_l1pt.pv_pa = 0;
    539 	kernel_l1pt.pv_va = 0;
    540 
    541 	/*
    542 	 * Allocate the L2 pages, but if we get to a page that is aligned for
    543 	 * an L1 page table, we will allocate the pages for it first and then
    544 	 * allocate the L2 page.
    545 	 */
    546 
    547 	if (map_vectors_p) {
    548 		/*
    549 		 * First allocate L2 page for the vectors.
    550 		 */
    551 		VPRINTF(" vector");
    552 		valloc_pages(bmi, &bmi->bmi_vector_l2pt, 1,
    553 		    VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE, true);
    554 		add_pages(bmi, &bmi->bmi_vector_l2pt);
    555 	}
    556 
    557 	/*
    558 	 * Now allocate L2 pages for the kernel
    559 	 */
    560 	VPRINTF(" kernel");
    561 	for (size_t idx = 0; idx < KERNEL_L2PT_KERNEL_NUM; ++idx) {
    562 		valloc_pages(bmi, &kernel_l2pt[idx], 1,
    563 		    VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE, true);
    564 		add_pages(bmi, &kernel_l2pt[idx]);
    565 	}
    566 
    567 	/*
    568 	 * Now allocate L2 pages for the initial kernel VA space.
    569 	 */
    570 	VPRINTF(" vm");
    571 	for (size_t idx = 0; idx < KERNEL_L2PT_VMDATA_NUM; ++idx) {
    572 		valloc_pages(bmi, &vmdata_l2pt[idx], 1,
    573 		    VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE, true);
    574 		add_pages(bmi, &vmdata_l2pt[idx]);
    575 	}
    576 
    577 #ifdef KASAN
    578 	/*
    579 	 * Now allocate L2 pages for the KASAN shadow map l2pt VA space.
    580 	 */
    581 	VPRINTF(" kasan");
    582 	for (size_t idx = 0; idx < KERNEL_L2PT_KASAN_NUM; ++idx) {
    583 		valloc_pages(bmi, &kasan_l2pt[idx], 1,
    584 		    VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE, true);
    585 		add_pages(bmi, &kasan_l2pt[idx]);
    586 	}
    587 
    588 #endif
    589 	/*
    590 	 * If someone wanted a L2 page for I/O, allocate it now.
    591 	 */
    592 	if (iovbase) {
    593 		VPRINTF(" io");
    594 		valloc_pages(bmi, &bmi->bmi_io_l2pt, 1,
    595 		    VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE, true);
    596 		add_pages(bmi, &bmi->bmi_io_l2pt);
    597 	}
    598 
    599 	VPRINTF("%s: allocating stacks\n", __func__);
    600 
    601 	/* Allocate stacks for all modes and CPUs */
    602 	valloc_pages(bmi, &abtstack, ABT_STACK_SIZE * cpu_num,
    603 	    VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE, true);
    604 	add_pages(bmi, &abtstack);
    605 	valloc_pages(bmi, &fiqstack, FIQ_STACK_SIZE * cpu_num,
    606 	    VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE, true);
    607 	add_pages(bmi, &fiqstack);
    608 	valloc_pages(bmi, &irqstack, IRQ_STACK_SIZE * cpu_num,
    609 	    VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE, true);
    610 	add_pages(bmi, &irqstack);
    611 	valloc_pages(bmi, &undstack, UND_STACK_SIZE * cpu_num,
    612 	    VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE, true);
    613 	add_pages(bmi, &undstack);
    614 	valloc_pages(bmi, &idlestack, UPAGES * cpu_num,		/* SVC32 */
    615 	    VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE, true);
    616 	add_pages(bmi, &idlestack);
    617 	valloc_pages(bmi, &kernelstack, UPAGES,			/* SVC32 */
    618 	    VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE, true);
    619 	add_pages(bmi, &kernelstack);
    620 
    621 	/* Allocate the message buffer from the end of memory. */
    622 	const size_t msgbuf_pgs = round_page(MSGBUFSIZE) / PAGE_SIZE;
    623 	valloc_pages(bmi, &msgbuf, msgbuf_pgs,
    624 	    VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE, false);
    625 	add_pages(bmi, &msgbuf);
    626 	msgbufphys = msgbuf.pv_pa;
    627 	msgbufaddr = (void *)msgbuf.pv_va;
    628 
    629 #ifdef KASAN
    630 	kasan_kernelstart = KERNEL_BASE;
    631 	kasan_kernelsize = (msgbuf.pv_va + round_page(MSGBUFSIZE)) - KERNEL_BASE;
    632 #endif
    633 
    634 	if (map_vectors_p) {
    635 		/*
    636 		 * Allocate a page for the system vector page.
    637 		 * This page will just contain the system vectors and can be
    638 		 * shared by all processes.
    639 		 */
    640 		VPRINTF(" vector");
    641 
    642 		valloc_pages(bmi, &systempage, 1,
    643 		    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE,
    644 		    PTE_CACHE, true);
    645 	}
    646 	systempage.pv_va = vectors;
    647 
    648 	/*
    649 	 * If the caller needed a few extra pages for some reason, allocate
    650 	 * them now.
    651 	 */
    652 #if ARM_MMU_XSCALE == 1
    653 #if (ARM_NMMUS > 1)
    654 	if (xscale_use_minidata)
    655 #endif
    656 		valloc_pages(bmi, &minidataclean, 1,
    657 		    VM_PROT_READ | VM_PROT_WRITE, 0, true);
    658 #endif
    659 
    660 	/*
    661 	 * Ok we have allocated physical pages for the primary kernel
    662 	 * page tables and stacks.  Let's just confirm that.
    663 	 */
    664 	if (kernel_l1pt.pv_va == 0
    665 	    && (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE - 1)) != 0))
    666 		panic("%s: Failed to allocate or align the kernel "
    667 		    "page directory", __func__);
    668 
    669 	VPRINTF("Creating L1 page table at 0x%08lx/0x%08lx\n",
    670 	    kernel_l1pt.pv_va, kernel_l1pt.pv_pa);
    671 
    672 	/*
    673 	 * Now we start construction of the L1 page table
    674 	 * We start by mapping the L2 page tables into the L1.
    675 	 * This means that we can replace L1 mappings later on if necessary
    676 	 */
    677 	vaddr_t l1pt_va = kernel_l1pt.pv_va;
    678 	paddr_t l1pt_pa = kernel_l1pt.pv_pa;
    679 
    680 	if (map_vectors_p) {
    681 		/* Map the L2 pages tables in the L1 page table */
    682 		const vaddr_t va = systempage.pv_va & -L2_S_SEGSIZE;
    683 
    684 		pmap_link_l2pt(l1pt_va, va,  &bmi->bmi_vector_l2pt);
    685 
    686 		VPRINTF("%s: adding L2 pt (VA %#lx, PA %#lx) for VA %#lx %s\n",
    687 		    __func__, bmi->bmi_vector_l2pt.pv_va,
    688 		    bmi->bmi_vector_l2pt.pv_pa, systempage.pv_va, "(vectors)");
    689 	}
    690 
    691 	/*
    692 	 * This enforces an alignment requirement of L2_S_SEGSIZE for kernel
    693 	 * start PA
    694 	 */
    695 	const vaddr_t kernel_base =
    696 	    KERN_PHYSTOV(bmi->bmi_kernelstart & -L2_S_SEGSIZE);
    697 
    698 	VPRINTF("%s: kernel_base %lx KERNEL_L2PT_KERNEL_NUM %zu\n", __func__,
    699 	    kernel_base, KERNEL_L2PT_KERNEL_NUM);
    700 
    701 	for (size_t idx = 0; idx < KERNEL_L2PT_KERNEL_NUM; idx++) {
    702 		const vaddr_t va = kernel_base + idx * L2_S_SEGSIZE;
    703 
    704 		pmap_link_l2pt(l1pt_va, va, &kernel_l2pt[idx]);
    705 
    706 		VPRINTF("%s: adding L2 pt (VA %#lx, PA %#lx) for VA %#lx %s\n",
    707 		    __func__, kernel_l2pt[idx].pv_va, kernel_l2pt[idx].pv_pa,
    708 		    va, "(kernel)");
    709 	}
    710 
    711 	VPRINTF("%s: kernel_vm_base %lx KERNEL_L2PT_VMDATA_NUM %d\n", __func__,
    712 	    kernel_vm_base, KERNEL_L2PT_VMDATA_NUM);
    713 
    714 	for (size_t idx = 0; idx < KERNEL_L2PT_VMDATA_NUM; idx++) {
    715 		const vaddr_t va = kernel_vm_base + idx * L2_S_SEGSIZE;
    716 
    717 		pmap_link_l2pt(l1pt_va, va, &vmdata_l2pt[idx]);
    718 
    719 		VPRINTF("%s: adding L2 pt (VA %#lx, PA %#lx) for VA %#lx %s\n",
    720 		    __func__, vmdata_l2pt[idx].pv_va, vmdata_l2pt[idx].pv_pa,
    721 		    va, "(vm)");
    722 	}
    723 	if (iovbase) {
    724 		const vaddr_t va = iovbase & -L2_S_SEGSIZE;
    725 
    726 		pmap_link_l2pt(l1pt_va, va, &bmi->bmi_io_l2pt);
    727 
    728 		VPRINTF("%s: adding L2 pt (VA %#lx, PA %#lx) for VA %#lx %s\n",
    729 		    __func__, bmi->bmi_io_l2pt.pv_va, bmi->bmi_io_l2pt.pv_pa,
    730 		    va, "(io)");
    731 	}
    732 
    733 #ifdef KASAN
    734 	VPRINTF("%s: kasan_shadow_base %x KERNEL_L2PT_KASAN_NUM %d\n", __func__,
    735 	    VM_KERNEL_KASAN_BASE, KERNEL_L2PT_KASAN_NUM);
    736 
    737 	for (size_t idx = 0; idx < KERNEL_L2PT_KASAN_NUM; idx++) {
    738 		const vaddr_t va = VM_KERNEL_KASAN_BASE  + idx * L2_S_SEGSIZE;
    739 
    740 		pmap_link_l2pt(l1pt_va, va, &kasan_l2pt[idx]);
    741 
    742 		VPRINTF("%s: adding L2 pt (VA %#lx, PA %#lx) for VA %#lx %s\n",
    743 		    __func__, kasan_l2pt[idx].pv_va, kasan_l2pt[idx].pv_pa,
    744 		    va, "(kasan)");
    745 	}
    746 	kasan_l2pts_created = true;
    747 #endif
    748 
    749 	/* update the top of the kernel VM */
    750 	pmap_curmaxkvaddr =
    751 	    kernel_vm_base + (KERNEL_L2PT_VMDATA_NUM * L2_S_SEGSIZE);
    752 
    753 	// This could be done earlier and then the kernel data and pages
    754 	// allocated above would get merged (concatentated)
    755 
    756 	VPRINTF("Mapping kernel\n");
    757 
    758 	extern char etext[];
    759 	size_t totalsize = bmi->bmi_kernelend - bmi->bmi_kernelstart;
    760 	size_t textsize = KERN_VTOPHYS((uintptr_t)etext) - bmi->bmi_kernelstart;
    761 
    762 	textsize = (textsize + PGOFSET) & ~PGOFSET;
    763 
    764 	/* start at offset of kernel in RAM */
    765 
    766 	text.pv_pa = bmi->bmi_kernelstart;
    767 	text.pv_va = KERN_PHYSTOV(bmi->bmi_kernelstart);
    768 	text.pv_size = textsize;
    769 	text.pv_prot = VM_PROT_READ | VM_PROT_EXECUTE;
    770 	text.pv_cache = PTE_CACHE;
    771 
    772 	VPRINTF("%s: adding chunk for kernel text %#lx..%#lx (VA %#lx)\n",
    773 	    __func__, text.pv_pa, text.pv_pa + text.pv_size - 1, text.pv_va);
    774 
    775 	add_pages(bmi, &text);
    776 
    777 	data.pv_pa = text.pv_pa + textsize;
    778 	data.pv_va = text.pv_va + textsize;
    779 	data.pv_size = totalsize - textsize;
    780 	data.pv_prot = VM_PROT_READ | VM_PROT_WRITE;
    781 	data.pv_cache = PTE_CACHE;
    782 
    783 	VPRINTF("%s: adding chunk for kernel data/bss %#lx..%#lx (VA %#lx)\n",
    784 	    __func__, data.pv_pa, data.pv_pa + data.pv_size - 1, data.pv_va);
    785 
    786 	add_pages(bmi, &data);
    787 
    788 	VPRINTF("Listing Chunks\n");
    789 
    790 	pv_addr_t *lpv;
    791 	SLIST_FOREACH(lpv, &bmi->bmi_chunks, pv_list) {
    792 		VPRINTF("%s: pv %p: chunk VA %#lx..%#lx "
    793 		    "(PA %#lx, prot %d, cache %d)\n",
    794 		    __func__, lpv, lpv->pv_va, lpv->pv_va + lpv->pv_size - 1,
    795 		    lpv->pv_pa, lpv->pv_prot, lpv->pv_cache);
    796 	}
    797 	VPRINTF("\nMapping Chunks\n");
    798 
    799 	pv_addr_t cur_pv;
    800 	pv_addr_t *pv = SLIST_FIRST(&bmi->bmi_chunks);
    801 	if (!mapallmem_p || pv->pv_pa == bmi->bmi_start) {
    802 		cur_pv = *pv;
    803 		KASSERTMSG(cur_pv.pv_va >= KERNEL_BASE, "%#lx", cur_pv.pv_va);
    804 		pv = SLIST_NEXT(pv, pv_list);
    805 	} else {
    806 		cur_pv.pv_va = KERNEL_BASE;
    807 		cur_pv.pv_pa = KERN_VTOPHYS(cur_pv.pv_va);
    808 		cur_pv.pv_size = pv->pv_pa - cur_pv.pv_pa;
    809 		cur_pv.pv_prot = VM_PROT_READ | VM_PROT_WRITE;
    810 		cur_pv.pv_cache = PTE_CACHE;
    811 	}
    812 	while (pv != NULL) {
    813 		if (mapallmem_p) {
    814 			if (concat_pvaddr(&cur_pv, pv)) {
    815 				pv = SLIST_NEXT(pv, pv_list);
    816 				continue;
    817 			}
    818 			if (cur_pv.pv_pa + cur_pv.pv_size < pv->pv_pa) {
    819 				/*
    820 				 * See if we can extend the current pv to emcompass the
    821 				 * hole, and if so do it and retry the concatenation.
    822 				 */
    823 				if (cur_pv.pv_prot == (VM_PROT_READ | VM_PROT_WRITE)
    824 				    && cur_pv.pv_cache == PTE_CACHE) {
    825 					cur_pv.pv_size = pv->pv_pa - cur_pv.pv_va;
    826 					continue;
    827 				}
    828 
    829 				/*
    830 				 * We couldn't so emit the current chunk and then
    831 				 */
    832 				VPRINTF("%s: mapping chunk VA %#lx..%#lx "
    833 				    "(PA %#lx, prot %d, cache %d)\n",
    834 				    __func__,
    835 				    cur_pv.pv_va, cur_pv.pv_va + cur_pv.pv_size - 1,
    836 				    cur_pv.pv_pa, cur_pv.pv_prot, cur_pv.pv_cache);
    837 				pmap_map_chunk(l1pt_va, cur_pv.pv_va, cur_pv.pv_pa,
    838 				    cur_pv.pv_size, cur_pv.pv_prot, cur_pv.pv_cache);
    839 
    840 				/*
    841 				 * set the current chunk to the hole and try again.
    842 				 */
    843 				cur_pv.pv_pa += cur_pv.pv_size;
    844 				cur_pv.pv_va += cur_pv.pv_size;
    845 				cur_pv.pv_size = pv->pv_pa - cur_pv.pv_va;
    846 				cur_pv.pv_prot = VM_PROT_READ | VM_PROT_WRITE;
    847 				cur_pv.pv_cache = PTE_CACHE;
    848 				continue;
    849 			}
    850 		}
    851 
    852 		/*
    853 		 * The new pv didn't concatenate so emit the current one
    854 		 * and use the new pv as the current pv.
    855 		 */
    856 		VPRINTF("%s: mapping chunk VA %#lx..%#lx "
    857 		    "(PA %#lx, prot %d, cache %d)\n",
    858 		    __func__, cur_pv.pv_va, cur_pv.pv_va + cur_pv.pv_size - 1,
    859 		    cur_pv.pv_pa, cur_pv.pv_prot, cur_pv.pv_cache);
    860 		pmap_map_chunk(l1pt_va, cur_pv.pv_va, cur_pv.pv_pa,
    861 		    cur_pv.pv_size, cur_pv.pv_prot, cur_pv.pv_cache);
    862 		cur_pv = *pv;
    863 		pv = SLIST_NEXT(pv, pv_list);
    864 	}
    865 
    866 	/*
    867 	 * If we are mapping all of memory, let's map the rest of memory.
    868 	 */
    869 	if (mapallmem_p && cur_pv.pv_pa + cur_pv.pv_size < bmi->bmi_end) {
    870 		if (cur_pv.pv_prot == (VM_PROT_READ | VM_PROT_WRITE)
    871 		    && cur_pv.pv_cache == PTE_CACHE) {
    872 			cur_pv.pv_size = bmi->bmi_end - cur_pv.pv_pa;
    873 		} else {
    874 			KASSERTMSG(cur_pv.pv_va + cur_pv.pv_size <= kernel_vm_base,
    875 			    "%#lx >= %#lx", cur_pv.pv_va + cur_pv.pv_size,
    876 			    kernel_vm_base);
    877 			VPRINTF("%s: mapping chunk VA %#lx..%#lx "
    878 			    "(PA %#lx, prot %d, cache %d)\n",
    879 			    __func__, cur_pv.pv_va, cur_pv.pv_va + cur_pv.pv_size - 1,
    880 			    cur_pv.pv_pa, cur_pv.pv_prot, cur_pv.pv_cache);
    881 			pmap_map_chunk(l1pt_va, cur_pv.pv_va, cur_pv.pv_pa,
    882 			    cur_pv.pv_size, cur_pv.pv_prot, cur_pv.pv_cache);
    883 			cur_pv.pv_pa += cur_pv.pv_size;
    884 			cur_pv.pv_va += cur_pv.pv_size;
    885 			cur_pv.pv_size = bmi->bmi_end - cur_pv.pv_pa;
    886 			cur_pv.pv_prot = VM_PROT_READ | VM_PROT_WRITE;
    887 			cur_pv.pv_cache = PTE_CACHE;
    888 		}
    889 	}
    890 
    891 	/*
    892 	 * The amount we can direct map is limited by the start of the
    893 	 * virtual part of the kernel address space.  Don't overrun
    894 	 * into it.
    895 	 */
    896 	if (mapallmem_p && cur_pv.pv_va + cur_pv.pv_size > kernel_vm_base) {
    897 		cur_pv.pv_size = kernel_vm_base - cur_pv.pv_va;
    898 	}
    899 
    900 	/*
    901 	 * Now we map the final chunk.
    902 	 */
    903 	VPRINTF("%s: mapping last chunk VA %#lx..%#lx (PA %#lx, prot %d, cache %d)\n",
    904 	    __func__, cur_pv.pv_va, cur_pv.pv_va + cur_pv.pv_size - 1,
    905 	    cur_pv.pv_pa, cur_pv.pv_prot, cur_pv.pv_cache);
    906 	pmap_map_chunk(l1pt_va, cur_pv.pv_va, cur_pv.pv_pa,
    907 	    cur_pv.pv_size, cur_pv.pv_prot, cur_pv.pv_cache);
    908 
    909 	/*
    910 	 * Now we map the stuff that isn't directly after the kernel
    911 	 */
    912 	if (map_vectors_p) {
    913 		/* Map the vector page. */
    914 		pmap_map_entry(l1pt_va, systempage.pv_va, systempage.pv_pa,
    915 		    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, PTE_CACHE);
    916 	}
    917 
    918 	/* Map the Mini-Data cache clean area. */
    919 #if ARM_MMU_XSCALE == 1
    920 #if (ARM_NMMUS > 1)
    921 	if (xscale_use_minidata)
    922 #endif
    923 		xscale_setup_minidata(l1pt_va, minidataclean.pv_va,
    924 		    minidataclean.pv_pa);
    925 #endif
    926 
    927 	/*
    928 	 * Map integrated peripherals at same address in first level page
    929 	 * table so that we can continue to use console.
    930 	 */
    931 	if (devmap)
    932 		pmap_devmap_bootstrap(l1pt_va, devmap);
    933 
    934 	/* Tell the user about where all the bits and pieces live. */
    935 	VPRINTF("%22s       Physical              Virtual        Num\n", " ");
    936 	VPRINTF("%22s Starting    Ending    Starting    Ending   Pages\n", " ");
    937 
    938 #ifdef VERBOSE_INIT_ARM
    939 	static const char mem_fmt[] =
    940 	    "%20s: 0x%08lx 0x%08lx 0x%08lx 0x%08lx %u\n";
    941 	static const char mem_fmt_nov[] =
    942 	    "%20s: 0x%08lx 0x%08lx                       %zu\n";
    943 #endif
    944 
    945 #if 0
    946 	// XXX Doesn't make sense if kernel not at bottom of RAM
    947 	VPRINTF(mem_fmt, "SDRAM", bmi->bmi_start, bmi->bmi_end - 1,
    948 	    KERN_PHYSTOV(bmi->bmi_start), KERN_PHYSTOV(bmi->bmi_end - 1),
    949 	    (int)physmem);
    950 #endif
    951 	VPRINTF(mem_fmt, "text section",
    952 	       text.pv_pa, text.pv_pa + text.pv_size - 1,
    953 	       text.pv_va, text.pv_va + text.pv_size - 1,
    954 	       (int)(text.pv_size / PAGE_SIZE));
    955 	VPRINTF(mem_fmt, "data section",
    956 	       KERN_VTOPHYS((vaddr_t)__data_start), KERN_VTOPHYS((vaddr_t)_edata),
    957 	       (vaddr_t)__data_start, (vaddr_t)_edata,
    958 	       (int)((round_page((vaddr_t)_edata)
    959 		      - trunc_page((vaddr_t)__data_start)) / PAGE_SIZE));
    960 	VPRINTF(mem_fmt, "bss section",
    961 	       KERN_VTOPHYS((vaddr_t)__bss_start), KERN_VTOPHYS((vaddr_t)__bss_end__),
    962 	       (vaddr_t)__bss_start, (vaddr_t)__bss_end__,
    963 	       (int)((round_page((vaddr_t)__bss_end__)
    964 		      - trunc_page((vaddr_t)__bss_start)) / PAGE_SIZE));
    965 	VPRINTF(mem_fmt, "L1 page directory",
    966 	    kernel_l1pt.pv_pa, kernel_l1pt.pv_pa + L1_TABLE_SIZE - 1,
    967 	    kernel_l1pt.pv_va, kernel_l1pt.pv_va + L1_TABLE_SIZE - 1,
    968 	    L1_TABLE_SIZE / PAGE_SIZE);
    969 	VPRINTF(mem_fmt, "ABT stack (CPU 0)",
    970 	    abtstack.pv_pa, abtstack.pv_pa + (ABT_STACK_SIZE * PAGE_SIZE) - 1,
    971 	    abtstack.pv_va, abtstack.pv_va + (ABT_STACK_SIZE * PAGE_SIZE) - 1,
    972 	    ABT_STACK_SIZE);
    973 	VPRINTF(mem_fmt, "FIQ stack (CPU 0)",
    974 	    fiqstack.pv_pa, fiqstack.pv_pa + (FIQ_STACK_SIZE * PAGE_SIZE) - 1,
    975 	    fiqstack.pv_va, fiqstack.pv_va + (FIQ_STACK_SIZE * PAGE_SIZE) - 1,
    976 	    FIQ_STACK_SIZE);
    977 	VPRINTF(mem_fmt, "IRQ stack (CPU 0)",
    978 	    irqstack.pv_pa, irqstack.pv_pa + (IRQ_STACK_SIZE * PAGE_SIZE) - 1,
    979 	    irqstack.pv_va, irqstack.pv_va + (IRQ_STACK_SIZE * PAGE_SIZE) - 1,
    980 	    IRQ_STACK_SIZE);
    981 	VPRINTF(mem_fmt, "UND stack (CPU 0)",
    982 	    undstack.pv_pa, undstack.pv_pa + (UND_STACK_SIZE * PAGE_SIZE) - 1,
    983 	    undstack.pv_va, undstack.pv_va + (UND_STACK_SIZE * PAGE_SIZE) - 1,
    984 	    UND_STACK_SIZE);
    985 	VPRINTF(mem_fmt, "IDLE stack (CPU 0)",
    986 	    idlestack.pv_pa, idlestack.pv_pa + (UPAGES * PAGE_SIZE) - 1,
    987 	    idlestack.pv_va, idlestack.pv_va + (UPAGES * PAGE_SIZE) - 1,
    988 	    UPAGES);
    989 	VPRINTF(mem_fmt, "SVC stack",
    990 	    kernelstack.pv_pa, kernelstack.pv_pa + (UPAGES * PAGE_SIZE) - 1,
    991 	    kernelstack.pv_va, kernelstack.pv_va + (UPAGES * PAGE_SIZE) - 1,
    992 	    UPAGES);
    993 	VPRINTF(mem_fmt, "Message Buffer",
    994 	    msgbuf.pv_pa, msgbuf.pv_pa + (msgbuf_pgs * PAGE_SIZE) - 1,
    995 	    msgbuf.pv_va, msgbuf.pv_va + (msgbuf_pgs * PAGE_SIZE) - 1,
    996 	    (int)msgbuf_pgs);
    997 	if (map_vectors_p) {
    998 		VPRINTF(mem_fmt, "Exception Vectors",
    999 		    systempage.pv_pa, systempage.pv_pa + PAGE_SIZE - 1,
   1000 		    systempage.pv_va, systempage.pv_va + PAGE_SIZE - 1,
   1001 		    1);
   1002 	}
   1003 	for (size_t i = 0; i < bmi->bmi_nfreeblocks; i++) {
   1004 		pv = &bmi->bmi_freeblocks[i];
   1005 
   1006 		VPRINTF(mem_fmt_nov, "Free Memory",
   1007 		    pv->pv_pa, pv->pv_pa + pv->pv_size - 1,
   1008 		    pv->pv_size / PAGE_SIZE);
   1009 	}
   1010 	/*
   1011 	 * Now we have the real page tables in place so we can switch to them.
   1012 	 * Once this is done we will be running with the REAL kernel page
   1013 	 * tables.
   1014 	 */
   1015 
   1016 	VPRINTF("TTBR0=%#x", armreg_ttbr_read());
   1017 #ifdef _ARM_ARCH_6
   1018 	VPRINTF(" TTBR1=%#x TTBCR=%#x CONTEXTIDR=%#x",
   1019 	    armreg_ttbr1_read(), armreg_ttbcr_read(),
   1020 	    armreg_contextidr_read());
   1021 #endif
   1022 	VPRINTF("\n");
   1023 
   1024 	/* Switch tables */
   1025 	VPRINTF("switching to new L1 page table @%#lx...\n", l1pt_pa);
   1026 
   1027 	cpu_ttb = l1pt_pa;
   1028 
   1029 	cpu_domains(DOMAIN_DEFAULT);
   1030 
   1031 	cpu_idcache_wbinv_all();
   1032 
   1033 #ifdef __HAVE_GENERIC_START
   1034 
   1035 	/*
   1036 	 * Turn on caches and set SCTLR/ACTLR
   1037 	 */
   1038 	cpu_setup(boot_args);
   1039 #endif
   1040 
   1041 	VPRINTF(" ttb");
   1042 
   1043 #ifdef ARM_MMU_EXTENDED
   1044 	/*
   1045 	 * TTBCR should have been initialized by the MD start code.
   1046 	 */
   1047 	KASSERT((armreg_contextidr_read() & 0xff) == 0);
   1048 	KASSERT(armreg_ttbcr_read() == __SHIFTIN(1, TTBCR_S_N));
   1049 	/*
   1050 	 * Disable lookups via TTBR0 until there is an activated pmap.
   1051 	 */
   1052 	armreg_ttbcr_write(armreg_ttbcr_read() | TTBCR_S_PD0);
   1053 	cpu_setttb(l1pt_pa, KERNEL_PID);
   1054 	isb();
   1055 #else
   1056 	cpu_setttb(l1pt_pa, true);
   1057 #endif
   1058 
   1059 	cpu_tlb_flushID();
   1060 
   1061 #ifdef KASAN
   1062 	extern uint8_t start_stacks_bottom[];
   1063 	kasan_early_init((void *)start_stacks_bottom);
   1064 #endif
   1065 
   1066 #ifdef ARM_MMU_EXTENDED
   1067 	VPRINTF("\nsctlr=%#x actlr=%#x\n",
   1068 	    armreg_sctlr_read(), armreg_auxctl_read());
   1069 #else
   1070 	VPRINTF(" (TTBR0=%#x)", armreg_ttbr_read());
   1071 #endif
   1072 
   1073 #ifdef MULTIPROCESSOR
   1074 #ifndef __HAVE_GENERIC_START
   1075 	/*
   1076 	 * Kick the secondaries to load the TTB.  After which they'll go
   1077 	 * back to sleep to wait for the final kick so they will hatch.
   1078 	 */
   1079 	VPRINTF(" hatchlings");
   1080 	cpu_boot_secondary_processors();
   1081 #endif
   1082 #endif
   1083 
   1084 	VPRINTF(" OK\n");
   1085 }
   1086