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