| 1 |
/* |
| 2 |
* Copyright (c) 2010-2014 Richard Braun. |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include <string.h> |
| 19 |
#include <inttypes.h> |
| 20 |
#include <i386/model_dep.h> |
| 21 |
#include <i386at/biosmem.h> |
| 22 |
#include <kern/assert.h> |
| 23 |
#include <kern/debug.h> |
| 24 |
#include <kern/macros.h> |
| 25 |
#include <kern/printf.h> |
| 26 |
#include <mach/vm_param.h> |
| 27 |
#include <mach/xen.h> |
| 28 |
#include <mach/machine/multiboot.h> |
| 29 |
#include <sys/types.h> |
| 30 |
#include <vm/vm_page.h> |
| 31 |
|
| 32 |
#define DEBUG 0 |
| 33 |
|
| 34 |
#define __boot |
| 35 |
#define __bootdata |
| 36 |
#define __init |
| 37 |
|
| 38 |
#define boot_memmove memmove |
| 39 |
#define boot_panic(s) panic("%s", s) |
| 40 |
#define boot_strlen strlen |
| 41 |
|
| 42 |
#define BOOT_CGAMEM phystokv(0xb8000) |
| 43 |
#define BOOT_CGACHARS (80 * 25) |
| 44 |
#define BOOT_CGACOLOR 0x7 |
| 45 |
|
| 46 |
#define BIOSMEM_MAX_BOOT_DATA 64 |
| 47 |
|
| 48 |
/* |
| 49 |
* Boot data descriptor. |
| 50 |
* |
| 51 |
* The start and end addresses must not be page-aligned, since there |
| 52 |
* could be more than one range inside a single page. |
| 53 |
*/ |
| 54 |
struct biosmem_boot_data { |
| 55 |
phys_addr_t start; |
| 56 |
phys_addr_t end; |
| 57 |
boolean_t temporary; |
| 58 |
}; |
| 59 |
|
| 60 |
/* |
| 61 |
* Sorted array of boot data descriptors. |
| 62 |
*/ |
| 63 |
static struct biosmem_boot_data biosmem_boot_data_array[BIOSMEM_MAX_BOOT_DATA] |
| 64 |
__bootdata; |
| 65 |
static unsigned int biosmem_nr_boot_data __bootdata; |
| 66 |
|
| 67 |
/* |
| 68 |
* Maximum number of entries in the BIOS memory map. |
| 69 |
* |
| 70 |
* Because of adjustments of overlapping ranges, the memory map can grow |
| 71 |
* to twice this size. |
| 72 |
*/ |
| 73 |
#define BIOSMEM_MAX_MAP_SIZE 128 |
| 74 |
|
| 75 |
/* |
| 76 |
* Memory range types. |
| 77 |
*/ |
| 78 |
#define BIOSMEM_TYPE_AVAILABLE 1 |
| 79 |
#define BIOSMEM_TYPE_RESERVED 2 |
| 80 |
#define BIOSMEM_TYPE_ACPI 3 |
| 81 |
#define BIOSMEM_TYPE_NVS 4 |
| 82 |
#define BIOSMEM_TYPE_UNUSABLE 5 |
| 83 |
#define BIOSMEM_TYPE_DISABLED 6 |
| 84 |
|
| 85 |
/* |
| 86 |
* Bitmask corresponding to memory ranges that require narrowing |
| 87 |
* to page boundaries. |
| 88 |
*/ |
| 89 |
#define BIOSMEM_MASK_NARROW (((1u << BIOSMEM_TYPE_AVAILABLE) | \ |
| 90 |
(1u << BIOSMEM_TYPE_NVS) | \ |
| 91 |
(1u << BIOSMEM_TYPE_DISABLED))) |
| 92 |
|
| 93 |
/* |
| 94 |
* Helper macro to test if range type needs narrowing. |
| 95 |
*/ |
| 96 |
#define BIOSMEM_NEEDS_NARROW(t) ((1u << t) & BIOSMEM_MASK_NARROW) |
| 97 |
|
| 98 |
/* |
| 99 |
* Memory map entry. |
| 100 |
*/ |
| 101 |
struct biosmem_map_entry { |
| 102 |
uint64_t base_addr; |
| 103 |
uint64_t length; |
| 104 |
unsigned int type; |
| 105 |
}; |
| 106 |
|
| 107 |
/* |
| 108 |
* Memory map built from the information passed by the boot loader. |
| 109 |
* |
| 110 |
* If the boot loader didn't pass a valid memory map, a simple map is built |
| 111 |
* based on the mem_lower and mem_upper multiboot fields. |
| 112 |
*/ |
| 113 |
static struct biosmem_map_entry biosmem_map[BIOSMEM_MAX_MAP_SIZE * 2] |
| 114 |
__bootdata; |
| 115 |
static unsigned int biosmem_map_size __bootdata; |
| 116 |
|
| 117 |
/* |
| 118 |
* Contiguous block of physical memory. |
| 119 |
*/ |
| 120 |
struct biosmem_segment { |
| 121 |
phys_addr_t start; |
| 122 |
phys_addr_t end; |
| 123 |
}; |
| 124 |
|
| 125 |
/* |
| 126 |
* Physical segment boundaries. |
| 127 |
*/ |
| 128 |
static struct biosmem_segment biosmem_segments[VM_PAGE_MAX_SEGS] __bootdata; |
| 129 |
|
| 130 |
/* |
| 131 |
* Boundaries of the simple bootstrap heap. |
| 132 |
* |
| 133 |
* This heap is located above BIOS memory. |
| 134 |
*/ |
| 135 |
static phys_addr_t biosmem_heap_start __bootdata; |
| 136 |
static phys_addr_t biosmem_heap_bottom __bootdata; |
| 137 |
static phys_addr_t biosmem_heap_top __bootdata; |
| 138 |
static phys_addr_t biosmem_heap_end __bootdata; |
| 139 |
|
| 140 |
/* |
| 141 |
* Boot allocation policy. |
| 142 |
* |
| 143 |
* Top-down allocations are normally preferred to avoid unnecessarily |
| 144 |
* filling the DMA segment. |
| 145 |
*/ |
| 146 |
static boolean_t biosmem_heap_topdown __bootdata; |
| 147 |
|
| 148 |
static char biosmem_panic_inval_boot_data[] __bootdata |
| 149 |
= "biosmem: invalid boot data"; |
| 150 |
static char biosmem_panic_too_many_boot_data[] __bootdata |
| 151 |
= "biosmem: too many boot data ranges"; |
| 152 |
static char biosmem_panic_too_big_msg[] __bootdata |
| 153 |
= "biosmem: too many memory map entries"; |
| 154 |
#ifndef MACH_HYP |
| 155 |
static char biosmem_panic_setup_msg[] __bootdata |
| 156 |
= "biosmem: unable to set up the early memory allocator"; |
| 157 |
#endif /* MACH_HYP */ |
| 158 |
static char biosmem_panic_noseg_msg[] __bootdata |
| 159 |
= "biosmem: unable to find any memory segment"; |
| 160 |
static char biosmem_panic_inval_msg[] __bootdata |
| 161 |
= "biosmem: attempt to allocate 0 page"; |
| 162 |
static char biosmem_panic_nomem_msg[] __bootdata |
| 163 |
= "biosmem: unable to allocate memory"; |
| 164 |
|
| 165 |
void __boot |
| 166 |
biosmem_register_boot_data(phys_addr_t start, phys_addr_t end, |
| 167 |
boolean_t temporary) |
| 168 |
{ |
| 169 |
unsigned int i; |
| 170 |
|
| 171 |
if (start >= end) { |
| 172 |
boot_panic(biosmem_panic_inval_boot_data); |
| 173 |
} |
| 174 |
|
| 175 |
if (biosmem_nr_boot_data == ARRAY_SIZE(biosmem_boot_data_array)) { |
| 176 |
boot_panic(biosmem_panic_too_many_boot_data); |
| 177 |
} |
| 178 |
|
| 179 |
for (i = 0; i < biosmem_nr_boot_data; i++) { |
| 180 |
/* Check if the new range overlaps */ |
| 181 |
if ((end > biosmem_boot_data_array[i].start) |
| 182 |
&& (start < biosmem_boot_data_array[i].end)) { |
| 183 |
|
| 184 |
/* |
| 185 |
* If it does, check whether it's part of another range. |
| 186 |
* For example, this applies to debugging symbols directly |
| 187 |
* taken from the kernel image. |
| 188 |
*/ |
| 189 |
if ((start >= biosmem_boot_data_array[i].start) |
| 190 |
&& (end <= biosmem_boot_data_array[i].end)) { |
| 191 |
|
| 192 |
/* |
| 193 |
* If it's completely included, make sure that a permanent |
| 194 |
* range remains permanent. |
| 195 |
* |
| 196 |
* XXX This means that if one big range is first registered |
| 197 |
* as temporary, and a smaller range inside of it is |
| 198 |
* registered as permanent, the bigger range becomes |
| 199 |
* permanent. It's not easy nor useful in practice to do |
| 200 |
* better than that. |
| 201 |
*/ |
| 202 |
if (biosmem_boot_data_array[i].temporary != temporary) { |
| 203 |
biosmem_boot_data_array[i].temporary = FALSE; |
| 204 |
} |
| 205 |
|
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
boot_panic(biosmem_panic_inval_boot_data); |
| 210 |
} |
| 211 |
|
| 212 |
if (end <= biosmem_boot_data_array[i].start) { |
| 213 |
break; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
boot_memmove(&biosmem_boot_data_array[i + 1], |
| 218 |
&biosmem_boot_data_array[i], |
| 219 |
(biosmem_nr_boot_data - i) * sizeof(*biosmem_boot_data_array)); |
| 220 |
|
| 221 |
biosmem_boot_data_array[i].start = start; |
| 222 |
biosmem_boot_data_array[i].end = end; |
| 223 |
biosmem_boot_data_array[i].temporary = temporary; |
| 224 |
biosmem_nr_boot_data++; |
| 225 |
} |
| 226 |
|
| 227 |
static void __init |
| 228 |
biosmem_unregister_boot_data(phys_addr_t start, phys_addr_t end) |
| 229 |
{ |
| 230 |
unsigned int i; |
| 231 |
|
| 232 |
if (start >= end) { |
| 233 |
panic("%s", biosmem_panic_inval_boot_data); |
| 234 |
} |
| 235 |
|
| 236 |
assert(biosmem_nr_boot_data != 0); |
| 237 |
|
| 238 |
for (i = 0; biosmem_nr_boot_data; i++) { |
| 239 |
if ((start == biosmem_boot_data_array[i].start) |
| 240 |
&& (end == biosmem_boot_data_array[i].end)) { |
| 241 |
break; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
if (i == biosmem_nr_boot_data) { |
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
#if DEBUG |
| 250 |
printf("biosmem: unregister boot data: %llx:%llx\n", |
| 251 |
(unsigned long long)biosmem_boot_data_array[i].start, |
| 252 |
(unsigned long long)biosmem_boot_data_array[i].end); |
| 253 |
#endif /* DEBUG */ |
| 254 |
|
| 255 |
biosmem_nr_boot_data--; |
| 256 |
|
| 257 |
boot_memmove(&biosmem_boot_data_array[i], |
| 258 |
&biosmem_boot_data_array[i + 1], |
| 259 |
(biosmem_nr_boot_data - i) * sizeof(*biosmem_boot_data_array)); |
| 260 |
} |
| 261 |
|
| 262 |
#ifndef MACH_HYP |
| 263 |
|
| 264 |
static void __boot |
| 265 |
biosmem_map_adjust_alignment(struct biosmem_map_entry *e) |
| 266 |
{ |
| 267 |
uint64_t end = e->base_addr + e->length; |
| 268 |
|
| 269 |
if (BIOSMEM_NEEDS_NARROW(e->type)) { |
| 270 |
e->base_addr = vm_page_round (e->base_addr); |
| 271 |
e->length = vm_page_trunc (end) - e->base_addr; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
static void __boot |
| 276 |
biosmem_map_build(const struct multiboot_raw_info *mbi) |
| 277 |
{ |
| 278 |
struct multiboot_raw_mmap_entry *mb_entry, *mb_end; |
| 279 |
struct biosmem_map_entry *start, *entry, *end; |
| 280 |
unsigned long addr; |
| 281 |
|
| 282 |
addr = phystokv(mbi->mmap_addr); |
| 283 |
mb_entry = (struct multiboot_raw_mmap_entry *)addr; |
| 284 |
mb_end = (struct multiboot_raw_mmap_entry *)(addr + mbi->mmap_length); |
| 285 |
start = biosmem_map; |
| 286 |
entry = start; |
| 287 |
end = entry + BIOSMEM_MAX_MAP_SIZE; |
| 288 |
|
| 289 |
while ((mb_entry < mb_end) && (entry < end)) { |
| 290 |
entry->base_addr = mb_entry->base_addr; |
| 291 |
entry->length = mb_entry->length; |
| 292 |
entry->type = mb_entry->type; |
| 293 |
|
| 294 |
mb_entry = (void *)mb_entry + sizeof(mb_entry->size) + mb_entry->size; |
| 295 |
|
| 296 |
biosmem_map_adjust_alignment(entry); |
| 297 |
entry++; |
| 298 |
} |
| 299 |
|
| 300 |
biosmem_map_size = entry - start; |
| 301 |
} |
| 302 |
|
| 303 |
static void __boot |
| 304 |
biosmem_map_build_simple(const struct multiboot_raw_info *mbi) |
| 305 |
{ |
| 306 |
struct biosmem_map_entry *entry; |
| 307 |
|
| 308 |
entry = biosmem_map; |
| 309 |
entry->base_addr = 0; |
| 310 |
entry->length = mbi->mem_lower << 10; |
| 311 |
entry->type = BIOSMEM_TYPE_AVAILABLE; |
| 312 |
biosmem_map_adjust_alignment(entry); |
| 313 |
|
| 314 |
entry++; |
| 315 |
entry->base_addr = BIOSMEM_END; |
| 316 |
entry->length = mbi->mem_upper << 10; |
| 317 |
entry->type = BIOSMEM_TYPE_AVAILABLE; |
| 318 |
biosmem_map_adjust_alignment(entry); |
| 319 |
|
| 320 |
biosmem_map_size = 2; |
| 321 |
} |
| 322 |
|
| 323 |
#endif /* MACH_HYP */ |
| 324 |
|
| 325 |
static int __boot |
| 326 |
biosmem_map_entry_is_invalid(const struct biosmem_map_entry *entry) |
| 327 |
{ |
| 328 |
return (entry->base_addr + entry->length) <= entry->base_addr; |
| 329 |
} |
| 330 |
|
| 331 |
static void __boot |
| 332 |
biosmem_map_filter(void) |
| 333 |
{ |
| 334 |
struct biosmem_map_entry *entry; |
| 335 |
unsigned int i; |
| 336 |
|
| 337 |
i = 0; |
| 338 |
|
| 339 |
while (i < biosmem_map_size) { |
| 340 |
entry = &biosmem_map[i]; |
| 341 |
|
| 342 |
if (biosmem_map_entry_is_invalid(entry)) { |
| 343 |
biosmem_map_size--; |
| 344 |
boot_memmove(entry, entry + 1, |
| 345 |
(biosmem_map_size - i) * sizeof(*entry)); |
| 346 |
continue; |
| 347 |
} |
| 348 |
|
| 349 |
i++; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
static void __boot |
| 354 |
biosmem_map_sort(void) |
| 355 |
{ |
| 356 |
struct biosmem_map_entry tmp; |
| 357 |
unsigned int i, j; |
| 358 |
|
| 359 |
/* |
| 360 |
* Simple insertion sort. |
| 361 |
*/ |
| 362 |
for (i = 1; i < biosmem_map_size; i++) { |
| 363 |
tmp = biosmem_map[i]; |
| 364 |
|
| 365 |
for (j = i - 1; j < i; j--) { |
| 366 |
if (biosmem_map[j].base_addr < tmp.base_addr) |
| 367 |
break; |
| 368 |
|
| 369 |
biosmem_map[j + 1] = biosmem_map[j]; |
| 370 |
} |
| 371 |
|
| 372 |
biosmem_map[j + 1] = tmp; |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
static void __boot |
| 377 |
biosmem_map_adjust(void) |
| 378 |
{ |
| 379 |
struct biosmem_map_entry tmp, *a, *b, *first, *second; |
| 380 |
uint64_t a_end, b_end, last_end; |
| 381 |
unsigned int i, j, last_type; |
| 382 |
|
| 383 |
biosmem_map_filter(); |
| 384 |
|
| 385 |
/* |
| 386 |
* Resolve overlapping areas, giving priority to most restrictive |
| 387 |
* (i.e. numerically higher) types. |
| 388 |
*/ |
| 389 |
for (i = 0; i < biosmem_map_size; i++) { |
| 390 |
a = &biosmem_map[i]; |
| 391 |
a_end = a->base_addr + a->length; |
| 392 |
|
| 393 |
j = i + 1; |
| 394 |
|
| 395 |
while (j < biosmem_map_size) { |
| 396 |
b = &biosmem_map[j]; |
| 397 |
b_end = b->base_addr + b->length; |
| 398 |
|
| 399 |
if ((a->base_addr >= b_end) || (a_end <= b->base_addr)) { |
| 400 |
j++; |
| 401 |
continue; |
| 402 |
} |
| 403 |
|
| 404 |
if (a->base_addr < b->base_addr) { |
| 405 |
first = a; |
| 406 |
second = b; |
| 407 |
} else { |
| 408 |
first = b; |
| 409 |
second = a; |
| 410 |
} |
| 411 |
|
| 412 |
if (a_end > b_end) { |
| 413 |
last_end = a_end; |
| 414 |
last_type = a->type; |
| 415 |
} else { |
| 416 |
last_end = b_end; |
| 417 |
last_type = b->type; |
| 418 |
} |
| 419 |
|
| 420 |
tmp.base_addr = second->base_addr; |
| 421 |
tmp.length = MIN(a_end, b_end) - tmp.base_addr; |
| 422 |
tmp.type = MAX(a->type, b->type); |
| 423 |
first->length = tmp.base_addr - first->base_addr; |
| 424 |
second->base_addr += tmp.length; |
| 425 |
second->length = last_end - second->base_addr; |
| 426 |
second->type = last_type; |
| 427 |
|
| 428 |
/* |
| 429 |
* Filter out invalid entries. |
| 430 |
*/ |
| 431 |
if (biosmem_map_entry_is_invalid(a) |
| 432 |
&& biosmem_map_entry_is_invalid(b)) { |
| 433 |
*a = tmp; |
| 434 |
biosmem_map_size--; |
| 435 |
memmove(b, b + 1, (biosmem_map_size - j) * sizeof(*b)); |
| 436 |
continue; |
| 437 |
} else if (biosmem_map_entry_is_invalid(a)) { |
| 438 |
*a = tmp; |
| 439 |
j++; |
| 440 |
continue; |
| 441 |
} else if (biosmem_map_entry_is_invalid(b)) { |
| 442 |
*b = tmp; |
| 443 |
j++; |
| 444 |
continue; |
| 445 |
} |
| 446 |
|
| 447 |
if (tmp.type == a->type) |
| 448 |
first = a; |
| 449 |
else if (tmp.type == b->type) |
| 450 |
first = b; |
| 451 |
else { |
| 452 |
|
| 453 |
/* |
| 454 |
* If the overlapping area can't be merged with one of its |
| 455 |
* neighbors, it must be added as a new entry. |
| 456 |
*/ |
| 457 |
|
| 458 |
if (biosmem_map_size >= ARRAY_SIZE(biosmem_map)) |
| 459 |
boot_panic(biosmem_panic_too_big_msg); |
| 460 |
|
| 461 |
biosmem_map[biosmem_map_size] = tmp; |
| 462 |
biosmem_map_size++; |
| 463 |
j++; |
| 464 |
continue; |
| 465 |
} |
| 466 |
|
| 467 |
if (first->base_addr > tmp.base_addr) |
| 468 |
first->base_addr = tmp.base_addr; |
| 469 |
|
| 470 |
first->length += tmp.length; |
| 471 |
j++; |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
biosmem_map_sort(); |
| 476 |
} |
| 477 |
|
| 478 |
/* |
| 479 |
* Find addresses of physical memory within a given range. |
| 480 |
* |
| 481 |
* This function considers the memory map with the [*phys_start, *phys_end] |
| 482 |
* range on entry, and returns the lowest address of physical memory |
| 483 |
* in *phys_start, and the highest address of unusable memory immediately |
| 484 |
* following physical memory in *phys_end. |
| 485 |
* |
| 486 |
* These addresses are normally used to establish the range of a segment. |
| 487 |
*/ |
| 488 |
static int __boot |
| 489 |
biosmem_map_find_avail(phys_addr_t *phys_start, phys_addr_t *phys_end) |
| 490 |
{ |
| 491 |
const struct biosmem_map_entry *entry, *map_end; |
| 492 |
phys_addr_t seg_start, seg_end; |
| 493 |
uint64_t start, end; |
| 494 |
|
| 495 |
seg_start = (phys_addr_t)-1; |
| 496 |
seg_end = (phys_addr_t)-1; |
| 497 |
map_end = biosmem_map + biosmem_map_size; |
| 498 |
|
| 499 |
for (entry = biosmem_map; entry < map_end; entry++) { |
| 500 |
if (entry->type != BIOSMEM_TYPE_AVAILABLE) |
| 501 |
continue; |
| 502 |
|
| 503 |
start = vm_page_round(entry->base_addr); |
| 504 |
|
| 505 |
if (start >= *phys_end) |
| 506 |
break; |
| 507 |
|
| 508 |
end = vm_page_trunc(entry->base_addr + entry->length); |
| 509 |
|
| 510 |
if ((start < end) && (start < *phys_end) && (end > *phys_start)) { |
| 511 |
if (seg_start == (phys_addr_t)-1) |
| 512 |
seg_start = start; |
| 513 |
|
| 514 |
seg_end = end; |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
if ((seg_start == (phys_addr_t)-1) || (seg_end == (phys_addr_t)-1)) |
| 519 |
return -1; |
| 520 |
|
| 521 |
if (seg_start > *phys_start) |
| 522 |
*phys_start = seg_start; |
| 523 |
|
| 524 |
if (seg_end < *phys_end) |
| 525 |
*phys_end = seg_end; |
| 526 |
|
| 527 |
return 0; |
| 528 |
} |
| 529 |
|
| 530 |
static void __boot |
| 531 |
biosmem_set_segment(unsigned int seg_index, phys_addr_t start, phys_addr_t end) |
| 532 |
{ |
| 533 |
biosmem_segments[seg_index].start = start; |
| 534 |
biosmem_segments[seg_index].end = end; |
| 535 |
} |
| 536 |
|
| 537 |
static phys_addr_t __boot |
| 538 |
biosmem_segment_end(unsigned int seg_index) |
| 539 |
{ |
| 540 |
return biosmem_segments[seg_index].end; |
| 541 |
} |
| 542 |
|
| 543 |
static phys_addr_t __boot |
| 544 |
biosmem_segment_size(unsigned int seg_index) |
| 545 |
{ |
| 546 |
return biosmem_segments[seg_index].end - biosmem_segments[seg_index].start; |
| 547 |
} |
| 548 |
|
| 549 |
static int __boot |
| 550 |
biosmem_find_avail_clip(phys_addr_t *avail_start, phys_addr_t *avail_end, |
| 551 |
phys_addr_t data_start, phys_addr_t data_end) |
| 552 |
{ |
| 553 |
phys_addr_t orig_end; |
| 554 |
|
| 555 |
assert(data_start < data_end); |
| 556 |
|
| 557 |
orig_end = data_end; |
| 558 |
data_start = vm_page_trunc(data_start); |
| 559 |
data_end = vm_page_round(data_end); |
| 560 |
|
| 561 |
if (data_end < orig_end) { |
| 562 |
boot_panic(biosmem_panic_inval_boot_data); |
| 563 |
} |
| 564 |
|
| 565 |
if ((data_end <= *avail_start) || (data_start >= *avail_end)) { |
| 566 |
return 0; |
| 567 |
} |
| 568 |
|
| 569 |
if (data_start > *avail_start) { |
| 570 |
*avail_end = data_start; |
| 571 |
} else { |
| 572 |
if (data_end >= *avail_end) { |
| 573 |
return -1; |
| 574 |
} |
| 575 |
|
| 576 |
*avail_start = data_end; |
| 577 |
} |
| 578 |
|
| 579 |
return 0; |
| 580 |
} |
| 581 |
|
| 582 |
/* |
| 583 |
* Find available memory in the given range. |
| 584 |
* |
| 585 |
* The search starts at the given start address, up to the given end address. |
| 586 |
* If a range is found, it is stored through the avail_startp and avail_endp |
| 587 |
* pointers. |
| 588 |
* |
| 589 |
* The range boundaries are page-aligned on return. |
| 590 |
*/ |
| 591 |
static int __boot |
| 592 |
biosmem_find_avail(phys_addr_t start, phys_addr_t end, |
| 593 |
phys_addr_t *avail_start, phys_addr_t *avail_end) |
| 594 |
{ |
| 595 |
phys_addr_t orig_start; |
| 596 |
unsigned int i; |
| 597 |
int error; |
| 598 |
|
| 599 |
assert(start <= end); |
| 600 |
|
| 601 |
orig_start = start; |
| 602 |
start = vm_page_round(start); |
| 603 |
end = vm_page_trunc(end); |
| 604 |
|
| 605 |
if ((start < orig_start) || (start >= end)) { |
| 606 |
return -1; |
| 607 |
} |
| 608 |
|
| 609 |
*avail_start = start; |
| 610 |
*avail_end = end; |
| 611 |
|
| 612 |
for (i = 0; i < biosmem_nr_boot_data; i++) { |
| 613 |
error = biosmem_find_avail_clip(avail_start, avail_end, |
| 614 |
biosmem_boot_data_array[i].start, |
| 615 |
biosmem_boot_data_array[i].end); |
| 616 |
|
| 617 |
if (error) { |
| 618 |
return -1; |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
return 0; |
| 623 |
} |
| 624 |
|
| 625 |
#ifndef MACH_HYP |
| 626 |
|
| 627 |
static void __boot |
| 628 |
biosmem_setup_allocator(const struct multiboot_raw_info *mbi) |
| 629 |
{ |
| 630 |
phys_addr_t heap_start, heap_end, max_heap_start, max_heap_end; |
| 631 |
phys_addr_t start, end; |
| 632 |
int error; |
| 633 |
|
| 634 |
/* |
| 635 |
* Find some memory for the heap. Look for the largest unused area in |
| 636 |
* upper memory, carefully avoiding all boot data. |
| 637 |
*/ |
| 638 |
end = vm_page_trunc((mbi->mem_upper + 1024) << 10); |
| 639 |
|
| 640 |
if (end > VM_PAGE_DIRECTMAP_LIMIT) |
| 641 |
end = VM_PAGE_DIRECTMAP_LIMIT; |
| 642 |
|
| 643 |
max_heap_start = 0; |
| 644 |
max_heap_end = 0; |
| 645 |
start = BIOSMEM_END; |
| 646 |
|
| 647 |
for (;;) { |
| 648 |
error = biosmem_find_avail(start, end, &heap_start, &heap_end); |
| 649 |
|
| 650 |
if (error) { |
| 651 |
break; |
| 652 |
} |
| 653 |
|
| 654 |
if ((heap_end - heap_start) > (max_heap_end - max_heap_start)) { |
| 655 |
max_heap_start = heap_start; |
| 656 |
max_heap_end = heap_end; |
| 657 |
} |
| 658 |
|
| 659 |
start = heap_end; |
| 660 |
} |
| 661 |
|
| 662 |
if (max_heap_start >= max_heap_end) |
| 663 |
boot_panic(biosmem_panic_setup_msg); |
| 664 |
|
| 665 |
biosmem_heap_start = max_heap_start; |
| 666 |
biosmem_heap_end = max_heap_end; |
| 667 |
biosmem_heap_bottom = biosmem_heap_start; |
| 668 |
biosmem_heap_top = biosmem_heap_end; |
| 669 |
biosmem_heap_topdown = TRUE; |
| 670 |
|
| 671 |
/* Prevent biosmem_free_usable() from releasing the heap */ |
| 672 |
biosmem_register_boot_data(biosmem_heap_start, biosmem_heap_end, FALSE); |
| 673 |
} |
| 674 |
|
| 675 |
#endif /* MACH_HYP */ |
| 676 |
|
| 677 |
static void __boot |
| 678 |
biosmem_bootstrap_common(void) |
| 679 |
{ |
| 680 |
phys_addr_t phys_start, phys_end; |
| 681 |
int error; |
| 682 |
|
| 683 |
biosmem_map_adjust(); |
| 684 |
|
| 685 |
phys_start = BIOSMEM_BASE; |
| 686 |
phys_end = VM_PAGE_DMA_LIMIT; |
| 687 |
error = biosmem_map_find_avail(&phys_start, &phys_end); |
| 688 |
|
| 689 |
if (error) |
| 690 |
boot_panic(biosmem_panic_noseg_msg); |
| 691 |
|
| 692 |
#if !defined(MACH_HYP) && NCPUS > 1 |
| 693 |
/* |
| 694 |
* Grab an early page for AP boot code which needs to be below 1MB. |
| 695 |
*/ |
| 696 |
assert (phys_start < 0x100000); |
| 697 |
apboot_addr = phys_start; |
| 698 |
phys_start += PAGE_SIZE; |
| 699 |
#endif |
| 700 |
|
| 701 |
biosmem_set_segment(VM_PAGE_SEG_DMA, phys_start, phys_end); |
| 702 |
|
| 703 |
phys_start = VM_PAGE_DMA_LIMIT; |
| 704 |
|
| 705 |
#ifdef VM_PAGE_DMA32_LIMIT |
| 706 |
#if VM_PAGE_DMA32_LIMIT < VM_PAGE_DIRECTMAP_LIMIT |
| 707 |
phys_end = VM_PAGE_DMA32_LIMIT; |
| 708 |
error = biosmem_map_find_avail(&phys_start, &phys_end); |
| 709 |
|
| 710 |
if (error) |
| 711 |
return; |
| 712 |
|
| 713 |
biosmem_set_segment(VM_PAGE_SEG_DMA32, phys_start, phys_end); |
| 714 |
|
| 715 |
phys_start = VM_PAGE_DMA32_LIMIT; |
| 716 |
#endif |
| 717 |
#endif /* VM_PAGE_DMA32_LIMIT */ |
| 718 |
|
| 719 |
phys_end = VM_PAGE_DIRECTMAP_LIMIT; |
| 720 |
error = biosmem_map_find_avail(&phys_start, &phys_end); |
| 721 |
|
| 722 |
if (error) |
| 723 |
return; |
| 724 |
|
| 725 |
biosmem_set_segment(VM_PAGE_SEG_DIRECTMAP, phys_start, phys_end); |
| 726 |
|
| 727 |
phys_start = VM_PAGE_DIRECTMAP_LIMIT; |
| 728 |
|
| 729 |
#ifdef VM_PAGE_DMA32_LIMIT |
| 730 |
#if VM_PAGE_DMA32_LIMIT > VM_PAGE_DIRECTMAP_LIMIT |
| 731 |
phys_end = VM_PAGE_DMA32_LIMIT; |
| 732 |
error = biosmem_map_find_avail(&phys_start, &phys_end); |
| 733 |
|
| 734 |
if (error) |
| 735 |
return; |
| 736 |
|
| 737 |
biosmem_set_segment(VM_PAGE_SEG_DMA32, phys_start, phys_end); |
| 738 |
|
| 739 |
phys_start = VM_PAGE_DMA32_LIMIT; |
| 740 |
#endif |
| 741 |
#endif /* VM_PAGE_DMA32_LIMIT */ |
| 742 |
|
| 743 |
phys_end = VM_PAGE_HIGHMEM_LIMIT; |
| 744 |
error = biosmem_map_find_avail(&phys_start, &phys_end); |
| 745 |
|
| 746 |
if (error) |
| 747 |
return; |
| 748 |
|
| 749 |
biosmem_set_segment(VM_PAGE_SEG_HIGHMEM, phys_start, phys_end); |
| 750 |
} |
| 751 |
|
| 752 |
#ifdef MACH_HYP |
| 753 |
|
| 754 |
void |
| 755 |
biosmem_xen_bootstrap(void) |
| 756 |
{ |
| 757 |
struct biosmem_map_entry *entry; |
| 758 |
|
| 759 |
entry = biosmem_map; |
| 760 |
entry->base_addr = 0; |
| 761 |
entry->length = boot_info.nr_pages << PAGE_SHIFT; |
| 762 |
entry->type = BIOSMEM_TYPE_AVAILABLE; |
| 763 |
|
| 764 |
biosmem_map_size = 1; |
| 765 |
|
| 766 |
biosmem_bootstrap_common(); |
| 767 |
|
| 768 |
biosmem_heap_start = _kvtophys(boot_info.pt_base) |
| 769 |
+ (boot_info.nr_pt_frames + 3) * 0x1000; |
| 770 |
biosmem_heap_end = boot_info.nr_pages << PAGE_SHIFT; |
| 771 |
|
| 772 |
#ifndef __LP64__ |
| 773 |
if (biosmem_heap_end > VM_PAGE_DIRECTMAP_LIMIT) |
| 774 |
biosmem_heap_end = VM_PAGE_DIRECTMAP_LIMIT; |
| 775 |
#endif /* __LP64__ */ |
| 776 |
|
| 777 |
biosmem_heap_bottom = biosmem_heap_start; |
| 778 |
biosmem_heap_top = biosmem_heap_end; |
| 779 |
|
| 780 |
/* |
| 781 |
* XXX Allocation on Xen are initially bottom-up : |
| 782 |
* At the "start of day", only 512k are available after the boot |
| 783 |
* data. The pmap module then creates a 4g mapping so all physical |
| 784 |
* memory is available, but it uses this allocator to do so. |
| 785 |
* Therefore, it must return pages from this small 512k regions |
| 786 |
* first. |
| 787 |
*/ |
| 788 |
biosmem_heap_topdown = FALSE; |
| 789 |
|
| 790 |
/* |
| 791 |
* Prevent biosmem_free_usable() from releasing the Xen boot information |
| 792 |
* and the heap. |
| 793 |
*/ |
| 794 |
biosmem_register_boot_data(0, biosmem_heap_end, FALSE); |
| 795 |
} |
| 796 |
|
| 797 |
#else /* MACH_HYP */ |
| 798 |
|
| 799 |
void __boot |
| 800 |
biosmem_bootstrap(const struct multiboot_raw_info *mbi) |
| 801 |
{ |
| 802 |
if (mbi->flags & MULTIBOOT_LOADER_MMAP) |
| 803 |
biosmem_map_build(mbi); |
| 804 |
else |
| 805 |
biosmem_map_build_simple(mbi); |
| 806 |
|
| 807 |
biosmem_bootstrap_common(); |
| 808 |
biosmem_setup_allocator(mbi); |
| 809 |
} |
| 810 |
|
| 811 |
#endif /* MACH_HYP */ |
| 812 |
|
| 813 |
unsigned long __boot |
| 814 |
biosmem_bootalloc(unsigned int nr_pages) |
| 815 |
{ |
| 816 |
unsigned long addr, size; |
| 817 |
|
| 818 |
size = vm_page_ptoa(nr_pages); |
| 819 |
|
| 820 |
if (size == 0) |
| 821 |
boot_panic(biosmem_panic_inval_msg); |
| 822 |
|
| 823 |
if (biosmem_heap_topdown) { |
| 824 |
addr = biosmem_heap_top - size; |
| 825 |
|
| 826 |
if ((addr < biosmem_heap_start) || (addr > biosmem_heap_top)) { |
| 827 |
boot_panic(biosmem_panic_nomem_msg); |
| 828 |
} |
| 829 |
|
| 830 |
biosmem_heap_top = addr; |
| 831 |
} else { |
| 832 |
unsigned long end; |
| 833 |
|
| 834 |
addr = biosmem_heap_bottom; |
| 835 |
end = addr + size; |
| 836 |
|
| 837 |
if ((end > biosmem_heap_end) || (end < biosmem_heap_bottom)) { |
| 838 |
boot_panic(biosmem_panic_nomem_msg); |
| 839 |
} |
| 840 |
|
| 841 |
biosmem_heap_bottom = end; |
| 842 |
} |
| 843 |
|
| 844 |
return addr; |
| 845 |
} |
| 846 |
|
| 847 |
phys_addr_t __boot |
| 848 |
biosmem_directmap_end(void) |
| 849 |
{ |
| 850 |
if (biosmem_segment_size(VM_PAGE_SEG_DIRECTMAP) != 0) |
| 851 |
return biosmem_segment_end(VM_PAGE_SEG_DIRECTMAP); |
| 852 |
#if defined(VM_PAGE_DMA32_LIMIT) && (VM_PAGE_DMA32_LIMIT < VM_PAGE_DIRECTMAP_LIMIT) |
| 853 |
if (biosmem_segment_size(VM_PAGE_SEG_DMA32) != 0) |
| 854 |
return biosmem_segment_end(VM_PAGE_SEG_DMA32); |
| 855 |
#endif |
| 856 |
return biosmem_segment_end(VM_PAGE_SEG_DMA); |
| 857 |
} |
| 858 |
|
| 859 |
static const char * __init |
| 860 |
biosmem_type_desc(unsigned int type) |
| 861 |
{ |
| 862 |
switch (type) { |
| 863 |
case BIOSMEM_TYPE_AVAILABLE: |
| 864 |
return "available"; |
| 865 |
case BIOSMEM_TYPE_RESERVED: |
| 866 |
return "reserved"; |
| 867 |
case BIOSMEM_TYPE_ACPI: |
| 868 |
return "ACPI"; |
| 869 |
case BIOSMEM_TYPE_NVS: |
| 870 |
return "ACPI NVS"; |
| 871 |
case BIOSMEM_TYPE_UNUSABLE: |
| 872 |
return "unusable"; |
| 873 |
default: |
| 874 |
return "unknown (reserved)"; |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
static void __init |
| 879 |
biosmem_map_show(void) |
| 880 |
{ |
| 881 |
const struct biosmem_map_entry *entry, *end; |
| 882 |
|
| 883 |
printf("biosmem: physical memory map:\n"); |
| 884 |
|
| 885 |
for (entry = biosmem_map, end = entry + biosmem_map_size; |
| 886 |
entry < end; |
| 887 |
entry++) |
| 888 |
printf("biosmem: %018"PRIx64":%018"PRIx64", %s\n", entry->base_addr, |
| 889 |
entry->base_addr + entry->length, |
| 890 |
biosmem_type_desc(entry->type)); |
| 891 |
|
| 892 |
#if DEBUG |
| 893 |
printf("biosmem: heap: %llx:%llx\n", |
| 894 |
(unsigned long long)biosmem_heap_start, |
| 895 |
(unsigned long long)biosmem_heap_end); |
| 896 |
#endif |
| 897 |
} |
| 898 |
|
| 899 |
static void __init |
| 900 |
biosmem_load_segment(struct biosmem_segment *seg, uint64_t max_phys_end) |
| 901 |
{ |
| 902 |
phys_addr_t phys_start, phys_end, avail_start, avail_end; |
| 903 |
unsigned int seg_index; |
| 904 |
|
| 905 |
phys_start = seg->start; |
| 906 |
phys_end = seg->end; |
| 907 |
seg_index = seg - biosmem_segments; |
| 908 |
|
| 909 |
if (phys_end > max_phys_end) { |
| 910 |
if (max_phys_end <= phys_start) { |
| 911 |
printf("biosmem: warning: segment %s physically unreachable, " |
| 912 |
"not loaded\n", vm_page_seg_name(seg_index)); |
| 913 |
return; |
| 914 |
} |
| 915 |
|
| 916 |
printf("biosmem: warning: segment %s truncated to %#"PRIx64"\n", |
| 917 |
vm_page_seg_name(seg_index), max_phys_end); |
| 918 |
phys_end = max_phys_end; |
| 919 |
} |
| 920 |
|
| 921 |
if (phys_end > MAX_PHYS_END) { |
| 922 |
if (MAX_PHYS_END <= phys_start) { |
| 923 |
printf("biosmem: warning: segment %s beyond tested memory size, " |
| 924 |
"not loaded\n", vm_page_seg_name(seg_index)); |
| 925 |
return; |
| 926 |
} |
| 927 |
|
| 928 |
printf("biosmem: warning: segment %s truncated to tested %#"PRIx64"\n", |
| 929 |
vm_page_seg_name(seg_index), MAX_PHYS_END); |
| 930 |
phys_end = MAX_PHYS_END; |
| 931 |
} |
| 932 |
|
| 933 |
vm_page_load(seg_index, phys_start, phys_end); |
| 934 |
|
| 935 |
/* |
| 936 |
* Clip the remaining available heap to fit it into the loaded |
| 937 |
* segment if possible. |
| 938 |
*/ |
| 939 |
|
| 940 |
if ((biosmem_heap_top > phys_start) && (biosmem_heap_bottom < phys_end)) { |
| 941 |
if (biosmem_heap_bottom >= phys_start) { |
| 942 |
avail_start = biosmem_heap_bottom; |
| 943 |
} else { |
| 944 |
avail_start = phys_start; |
| 945 |
} |
| 946 |
|
| 947 |
if (biosmem_heap_top <= phys_end) { |
| 948 |
avail_end = biosmem_heap_top; |
| 949 |
} else { |
| 950 |
avail_end = phys_end; |
| 951 |
} |
| 952 |
|
| 953 |
vm_page_load_heap(seg_index, avail_start, avail_end); |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
void __init |
| 958 |
biosmem_setup(void) |
| 959 |
{ |
| 960 |
struct biosmem_segment *seg; |
| 961 |
unsigned int i; |
| 962 |
|
| 963 |
biosmem_map_show(); |
| 964 |
|
| 965 |
for (i = 0; i < ARRAY_SIZE(biosmem_segments); i++) { |
| 966 |
if (biosmem_segment_size(i) == 0) |
| 967 |
break; |
| 968 |
|
| 969 |
seg = &biosmem_segments[i]; |
| 970 |
biosmem_load_segment(seg, VM_PAGE_HIGHMEM_LIMIT); |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
static void __init |
| 975 |
biosmem_unregister_temporary_boot_data(void) |
| 976 |
{ |
| 977 |
struct biosmem_boot_data *data; |
| 978 |
int i; |
| 979 |
|
| 980 |
for (i = 0; i < (int) biosmem_nr_boot_data; i++) { |
| 981 |
data = &biosmem_boot_data_array[i]; |
| 982 |
|
| 983 |
if (!data->temporary) { |
| 984 |
continue; |
| 985 |
} |
| 986 |
|
| 987 |
biosmem_unregister_boot_data(data->start, data->end); |
| 988 |
i = -1; |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
static void __init |
| 993 |
biosmem_free_usable_range(phys_addr_t start, phys_addr_t end) |
| 994 |
{ |
| 995 |
struct vm_page *page; |
| 996 |
|
| 997 |
#if DEBUG |
| 998 |
printf("biosmem: release to vm_page: %llx:%llx (%lluk)\n", |
| 999 |
(unsigned long long)start, (unsigned long long)end, |
| 1000 |
(unsigned long long)((end - start) >> 10)); |
| 1001 |
#endif |
| 1002 |
|
| 1003 |
while (start < end) { |
| 1004 |
page = vm_page_lookup_pa(start); |
| 1005 |
assert(page != NULL); |
| 1006 |
vm_page_manage(page); |
| 1007 |
start += PAGE_SIZE; |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
static void __init |
| 1012 |
biosmem_free_usable_entry(phys_addr_t start, phys_addr_t end) |
| 1013 |
{ |
| 1014 |
phys_addr_t avail_start, avail_end; |
| 1015 |
int error; |
| 1016 |
|
| 1017 |
for (;;) { |
| 1018 |
error = biosmem_find_avail(start, end, &avail_start, &avail_end); |
| 1019 |
|
| 1020 |
if (error) { |
| 1021 |
break; |
| 1022 |
} |
| 1023 |
|
| 1024 |
biosmem_free_usable_range(avail_start, avail_end); |
| 1025 |
start = avail_end; |
| 1026 |
} |
| 1027 |
} |
| 1028 |
|
| 1029 |
void __init |
| 1030 |
biosmem_free_usable(void) |
| 1031 |
{ |
| 1032 |
struct biosmem_map_entry *entry; |
| 1033 |
uint64_t start, end; |
| 1034 |
unsigned int i; |
| 1035 |
|
| 1036 |
biosmem_unregister_temporary_boot_data(); |
| 1037 |
|
| 1038 |
for (i = 0; i < biosmem_map_size; i++) { |
| 1039 |
entry = &biosmem_map[i]; |
| 1040 |
|
| 1041 |
if (entry->type != BIOSMEM_TYPE_AVAILABLE) |
| 1042 |
continue; |
| 1043 |
|
| 1044 |
start = vm_page_round(entry->base_addr); |
| 1045 |
|
| 1046 |
if (start >= VM_PAGE_HIGHMEM_LIMIT) |
| 1047 |
break; |
| 1048 |
|
| 1049 |
if (start >= MAX_PHYS_END) |
| 1050 |
break; |
| 1051 |
|
| 1052 |
end = vm_page_trunc(entry->base_addr + entry->length); |
| 1053 |
|
| 1054 |
if (end > VM_PAGE_HIGHMEM_LIMIT) { |
| 1055 |
end = VM_PAGE_HIGHMEM_LIMIT; |
| 1056 |
} |
| 1057 |
|
| 1058 |
if (end > MAX_PHYS_END) { |
| 1059 |
end = MAX_PHYS_END; |
| 1060 |
} |
| 1061 |
|
| 1062 |
if (start < BIOSMEM_BASE) |
| 1063 |
start = BIOSMEM_BASE; |
| 1064 |
|
| 1065 |
if (start >= end) { |
| 1066 |
continue; |
| 1067 |
} |
| 1068 |
|
| 1069 |
biosmem_free_usable_entry(start, end); |
| 1070 |
} |
| 1071 |
} |
| 1072 |
|
| 1073 |
boolean_t |
| 1074 |
biosmem_addr_available(phys_addr_t addr) |
| 1075 |
{ |
| 1076 |
struct biosmem_map_entry *entry; |
| 1077 |
unsigned i; |
| 1078 |
|
| 1079 |
if (addr < BIOSMEM_BASE) |
| 1080 |
return FALSE; |
| 1081 |
|
| 1082 |
for (i = 0; i < biosmem_map_size; i++) { |
| 1083 |
entry = &biosmem_map[i]; |
| 1084 |
|
| 1085 |
if (addr >= entry->base_addr && addr < entry->base_addr + entry->length) |
| 1086 |
return entry->type == BIOSMEM_TYPE_AVAILABLE; |
| 1087 |
} |
| 1088 |
return FALSE; |
| 1089 |
} |