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