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