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