1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2014 Ian Lepore <ian@freebsd.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_acpi.h" 33 #include "opt_ddb.h" 34 35 /* 36 * Routines for describing and initializing anything related to physical memory. 37 */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/physmem.h> 42 #include <vm/vm.h> 43 #include <vm/vm_param.h> 44 #include <vm/vm_page.h> 45 #include <vm/vm_phys.h> 46 #include <vm/vm_dumpset.h> 47 #include <machine/md_var.h> 48 49 /* 50 * These structures are used internally to keep track of regions of physical 51 * ram, and regions within the physical ram that need to be excluded. An 52 * exclusion region can be excluded from crash dumps, from the vm pool of pages 53 * that can be allocated, or both, depending on the exclusion flags associated 54 * with the region. 55 */ 56 #ifdef DEV_ACPI 57 #define MAX_HWCNT 32 /* ACPI needs more regions */ 58 #define MAX_EXCNT 32 59 #else 60 #define MAX_HWCNT 16 61 #define MAX_EXCNT 16 62 #endif 63 64 #if defined(__arm__) 65 #define MAX_PHYS_ADDR 0xFFFFFFFFull 66 #elif defined(__aarch64__) || defined(__riscv) 67 #define MAX_PHYS_ADDR 0xFFFFFFFFFFFFFFFFull 68 #endif 69 70 struct region { 71 vm_paddr_t addr; 72 vm_size_t size; 73 uint32_t flags; 74 }; 75 76 static struct region hwregions[MAX_HWCNT]; 77 static struct region exregions[MAX_EXCNT]; 78 79 static size_t hwcnt; 80 static size_t excnt; 81 82 /* 83 * realmem is the total number of hardware pages, excluded or not. 84 * Maxmem is one greater than the last physical page number. 85 */ 86 long realmem; 87 long Maxmem; 88 89 /* 90 * Print the contents of the physical and excluded region tables using the 91 * provided printf-like output function (which will be either printf or 92 * db_printf). 93 */ 94 static void 95 physmem_dump_tables(int (*prfunc)(const char *, ...)) 96 { 97 int flags, i; 98 uintmax_t addr, size; 99 const unsigned int mbyte = 1024 * 1024; 100 101 prfunc("Physical memory chunk(s):\n"); 102 for (i = 0; i < hwcnt; ++i) { 103 addr = hwregions[i].addr; 104 size = hwregions[i].size; 105 prfunc(" 0x%08jx - 0x%08jx, %5ju MB (%7ju pages)\n", addr, 106 addr + size - 1, size / mbyte, size / PAGE_SIZE); 107 } 108 109 prfunc("Excluded memory regions:\n"); 110 for (i = 0; i < excnt; ++i) { 111 addr = exregions[i].addr; 112 size = exregions[i].size; 113 flags = exregions[i].flags; 114 prfunc(" 0x%08jx - 0x%08jx, %5ju MB (%7ju pages) %s %s\n", 115 addr, addr + size - 1, size / mbyte, size / PAGE_SIZE, 116 (flags & EXFLAG_NOALLOC) ? "NoAlloc" : "", 117 (flags & EXFLAG_NODUMP) ? "NoDump" : ""); 118 } 119 120 #ifdef DEBUG 121 prfunc("Avail lists:\n"); 122 for (i = 0; phys_avail[i] != 0; ++i) { 123 prfunc(" phys_avail[%d] 0x%08x\n", i, phys_avail[i]); 124 } 125 for (i = 0; dump_avail[i] != 0; ++i) { 126 prfunc(" dump_avail[%d] 0x%08x\n", i, dump_avail[i]); 127 } 128 #endif 129 } 130 131 /* 132 * Print the contents of the static mapping table. Used for bootverbose. 133 */ 134 void 135 physmem_print_tables(void) 136 { 137 138 physmem_dump_tables(printf); 139 } 140 141 /* 142 * Walk the list of hardware regions, processing it against the list of 143 * exclusions that contain the given exflags, and generating an "avail list". 144 * 145 * Updates the value at *pavail with the sum of all pages in all hw regions. 146 * 147 * Returns the number of pages of non-excluded memory added to the avail list. 148 */ 149 static size_t 150 regions_to_avail(vm_paddr_t *avail, uint32_t exflags, size_t maxavail, 151 long *pavail, long *prealmem) 152 { 153 size_t acnt, exi, hwi; 154 uint64_t end, start, xend, xstart; 155 long availmem, totalmem; 156 const struct region *exp, *hwp; 157 158 totalmem = 0; 159 availmem = 0; 160 acnt = 0; 161 for (hwi = 0, hwp = hwregions; hwi < hwcnt; ++hwi, ++hwp) { 162 start = hwp->addr; 163 end = hwp->size + start; 164 totalmem += atop((vm_offset_t)(end - start)); 165 for (exi = 0, exp = exregions; exi < excnt; ++exi, ++exp) { 166 /* 167 * If the excluded region does not match given flags, 168 * continue checking with the next excluded region. 169 */ 170 if ((exp->flags & exflags) == 0) 171 continue; 172 xstart = exp->addr; 173 xend = exp->size + xstart; 174 /* 175 * If the excluded region ends before this hw region, 176 * continue checking with the next excluded region. 177 */ 178 if (xend <= start) 179 continue; 180 /* 181 * If the excluded region begins after this hw region 182 * we're done because both lists are sorted. 183 */ 184 if (xstart >= end) 185 break; 186 /* 187 * If the excluded region completely covers this hw 188 * region, shrink this hw region to zero size. 189 */ 190 if ((start >= xstart) && (end <= xend)) { 191 start = xend; 192 end = xend; 193 break; 194 } 195 /* 196 * If the excluded region falls wholly within this hw 197 * region without abutting or overlapping the beginning 198 * or end, create an available entry from the leading 199 * fragment, then adjust the start of this hw region to 200 * the end of the excluded region, and continue checking 201 * the next excluded region because another exclusion 202 * could affect the remainder of this hw region. 203 */ 204 if ((xstart > start) && (xend < end)) { 205 if (acnt > 0 && 206 avail[acnt - 1] == (vm_paddr_t)start) { 207 avail[acnt - 1] = (vm_paddr_t)xstart; 208 } else { 209 avail[acnt++] = (vm_paddr_t)start; 210 avail[acnt++] = (vm_paddr_t)xstart; 211 } 212 availmem += atop((vm_offset_t)(xstart - start)); 213 start = xend; 214 continue; 215 } 216 /* 217 * We know the excluded region overlaps either the start 218 * or end of this hardware region (but not both), trim 219 * the excluded portion off the appropriate end. 220 */ 221 if (xstart <= start) 222 start = xend; 223 else 224 end = xstart; 225 } 226 /* 227 * If the trimming actions above left a non-zero size, create an 228 * available entry for it. 229 */ 230 if (end > start) { 231 if (acnt > 0 && avail[acnt - 1] == (vm_paddr_t)start) { 232 avail[acnt - 1] = (vm_paddr_t)end; 233 } else { 234 avail[acnt++] = (vm_paddr_t)start; 235 avail[acnt++] = (vm_paddr_t)end; 236 } 237 availmem += atop((vm_offset_t)(end - start)); 238 } 239 if (acnt >= maxavail) 240 panic("Not enough space in the dump/phys_avail arrays"); 241 } 242 243 if (pavail != NULL) 244 *pavail = availmem; 245 if (prealmem != NULL) 246 *prealmem = totalmem; 247 return (acnt); 248 } 249 250 /* 251 * Insertion-sort a new entry into a regions list; sorted by start address. 252 */ 253 static size_t 254 insert_region(struct region *regions, size_t rcnt, vm_paddr_t addr, 255 vm_size_t size, uint32_t flags) 256 { 257 size_t i; 258 struct region *ep, *rp; 259 260 ep = regions + rcnt; 261 for (i = 0, rp = regions; i < rcnt; ++i, ++rp) { 262 if (rp->addr == addr && rp->size == size) /* Pure dup. */ 263 return (rcnt); 264 if (flags == rp->flags) { 265 if (addr + size == rp->addr) { 266 rp->addr = addr; 267 rp->size += size; 268 return (rcnt); 269 } else if (rp->addr + rp->size == addr) { 270 rp->size += size; 271 return (rcnt); 272 } 273 } 274 if (addr < rp->addr) { 275 bcopy(rp, rp + 1, (ep - rp) * sizeof(*rp)); 276 break; 277 } 278 } 279 rp->addr = addr; 280 rp->size = size; 281 rp->flags = flags; 282 rcnt++; 283 284 return (rcnt); 285 } 286 287 /* 288 * Add a hardware memory region. 289 */ 290 void 291 physmem_hardware_region(uint64_t pa, uint64_t sz) 292 { 293 vm_offset_t adj; 294 295 /* 296 * Filter out the page at PA 0x00000000. The VM can't handle it, as 297 * pmap_extract() == 0 means failure. 298 */ 299 if (pa == 0) { 300 if (sz <= PAGE_SIZE) 301 return; 302 pa = PAGE_SIZE; 303 sz -= PAGE_SIZE; 304 } else if (pa > MAX_PHYS_ADDR) { 305 /* This range is past usable memory, ignore it */ 306 return; 307 } 308 309 /* 310 * Also filter out the page at the end of the physical address space -- 311 * if addr is non-zero and addr+size is zero we wrapped to the next byte 312 * beyond what vm_paddr_t can express. That leads to a NULL pointer 313 * deref early in startup; work around it by leaving the last page out. 314 * 315 * XXX This just in: subtract out a whole megabyte, not just 1 page. 316 * Reducing the size by anything less than 1MB results in the NULL 317 * pointer deref in _vm_map_lock_read(). Better to give up a megabyte 318 * than leave some folks with an unusable system while we investigate. 319 */ 320 if ((pa + sz) > (MAX_PHYS_ADDR - 1024 * 1024)) { 321 sz = MAX_PHYS_ADDR - pa + 1; 322 if (sz <= 1024 * 1024) 323 return; 324 sz -= 1024 * 1024; 325 } 326 327 /* 328 * Round the starting address up to a page boundary, and truncate the 329 * ending page down to a page boundary. 330 */ 331 adj = round_page(pa) - pa; 332 pa = round_page(pa); 333 sz = trunc_page(sz - adj); 334 335 if (sz > 0 && hwcnt < nitems(hwregions)) 336 hwcnt = insert_region(hwregions, hwcnt, pa, sz, 0); 337 } 338 339 /* 340 * Add an exclusion region. 341 */ 342 void 343 physmem_exclude_region(vm_paddr_t pa, vm_size_t sz, uint32_t exflags) 344 { 345 vm_offset_t adj; 346 347 /* 348 * Truncate the starting address down to a page boundary, and round the 349 * ending page up to a page boundary. 350 */ 351 adj = pa - trunc_page(pa); 352 pa = trunc_page(pa); 353 sz = round_page(sz + adj); 354 355 if (excnt >= nitems(exregions)) 356 panic("failed to exclude region %#jx-%#jx", (uintmax_t)pa, 357 (uintmax_t)(pa + sz)); 358 excnt = insert_region(exregions, excnt, pa, sz, exflags); 359 } 360 361 size_t 362 physmem_avail(vm_paddr_t *avail, size_t maxavail) 363 { 364 365 return (regions_to_avail(avail, EXFLAG_NOALLOC, maxavail, NULL, NULL)); 366 } 367 368 /* 369 * Process all the regions added earlier into the global avail lists. 370 * 371 * Updates the kernel global 'physmem' with the number of physical pages 372 * available for use (all pages not in any exclusion region). 373 * 374 * Updates the kernel global 'Maxmem' with the page number one greater then the 375 * last page of physical memory in the system. 376 */ 377 void 378 physmem_init_kernel_globals(void) 379 { 380 size_t nextidx; 381 382 regions_to_avail(dump_avail, EXFLAG_NODUMP, PHYS_AVAIL_ENTRIES, NULL, 383 NULL); 384 nextidx = regions_to_avail(phys_avail, EXFLAG_NOALLOC, 385 PHYS_AVAIL_ENTRIES, &physmem, &realmem); 386 if (nextidx == 0) 387 panic("No memory entries in phys_avail"); 388 Maxmem = atop(phys_avail[nextidx - 1]); 389 } 390 391 #ifdef DDB 392 #include <ddb/ddb.h> 393 394 DB_SHOW_COMMAND(physmem, db_show_physmem) 395 { 396 397 physmem_dump_tables(db_printf); 398 } 399 400 #endif /* DDB */ 401