]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/i386_vm_init.c
xnu-2782.10.72.tar.gz
[apple/xnu.git] / osfmk / i386 / i386_vm_init.c
1 /*
2 * Copyright (c) 2003-2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989, 1988 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56
57
58 #include <mach/i386/vm_param.h>
59
60 #include <string.h>
61 #include <mach/vm_param.h>
62 #include <mach/vm_prot.h>
63 #include <mach/machine.h>
64 #include <mach/time_value.h>
65 #include <kern/spl.h>
66 #include <kern/assert.h>
67 #include <kern/debug.h>
68 #include <kern/misc_protos.h>
69 #include <kern/cpu_data.h>
70 #include <kern/processor.h>
71 #include <vm/vm_page.h>
72 #include <vm/pmap.h>
73 #include <vm/vm_kern.h>
74 #include <i386/pmap.h>
75 #include <i386/misc_protos.h>
76 #include <i386/cpuid.h>
77 #include <mach/thread_status.h>
78 #include <pexpert/i386/efi.h>
79 #include <i386/i386_lowmem.h>
80 #include <x86_64/lowglobals.h>
81 #include <i386/pal_routines.h>
82
83 #include <mach-o/loader.h>
84 #include <libkern/kernel_mach_header.h>
85
86
87 vm_size_t mem_size = 0;
88 pmap_paddr_t first_avail = 0;/* first after page tables */
89
90 uint64_t max_mem; /* Size of physical memory (bytes), adjusted by maxmem */
91 uint64_t mem_actual;
92 uint64_t sane_size = 0; /* Memory size for defaults calculations */
93
94 /*
95 * KASLR parameters
96 */
97 ppnum_t vm_kernel_base_page;
98 vm_offset_t vm_kernel_base;
99 vm_offset_t vm_kernel_top;
100 vm_offset_t vm_kernel_stext;
101 vm_offset_t vm_kernel_etext;
102 vm_offset_t vm_kernel_slide;
103 vm_offset_t vm_hib_base;
104 vm_offset_t vm_kext_base = VM_MIN_KERNEL_AND_KEXT_ADDRESS;
105 vm_offset_t vm_kext_top = VM_MIN_KERNEL_ADDRESS;
106
107 vm_offset_t vm_prelink_stext;
108 vm_offset_t vm_prelink_etext;
109 vm_offset_t vm_prelink_sinfo;
110 vm_offset_t vm_prelink_einfo;
111 vm_offset_t vm_slinkedit;
112 vm_offset_t vm_elinkedit;
113
114 #define MAXLORESERVE (32 * 1024 * 1024)
115
116 ppnum_t max_ppnum = 0;
117 ppnum_t lowest_lo = 0;
118 ppnum_t lowest_hi = 0;
119 ppnum_t highest_hi = 0;
120
121 enum {PMAP_MAX_RESERVED_RANGES = 32};
122 uint32_t pmap_reserved_pages_allocated = 0;
123 uint32_t pmap_reserved_range_indices[PMAP_MAX_RESERVED_RANGES];
124 uint32_t pmap_last_reserved_range_index = 0;
125 uint32_t pmap_reserved_ranges = 0;
126
127 extern unsigned int bsd_mbuf_cluster_reserve(boolean_t *);
128
129 pmap_paddr_t avail_start, avail_end;
130 vm_offset_t virtual_avail, virtual_end;
131 static pmap_paddr_t avail_remaining;
132 vm_offset_t static_memory_end = 0;
133
134 vm_offset_t sHIB, eHIB, stext, etext, sdata, edata, sconstdata, econstdata, end;
135
136 /*
137 * _mh_execute_header is the mach_header for the currently executing kernel
138 */
139 vm_offset_t segTEXTB; unsigned long segSizeTEXT;
140 vm_offset_t segDATAB; unsigned long segSizeDATA;
141 vm_offset_t segLINKB; unsigned long segSizeLINK;
142 vm_offset_t segPRELINKB; unsigned long segSizePRELINK;
143 vm_offset_t segPRELINKINFOB; unsigned long segSizePRELINKINFO;
144 vm_offset_t segHIBB; unsigned long segSizeHIB;
145 vm_offset_t sectCONSTB; unsigned long sectSizeConst;
146
147 boolean_t doconstro_override = FALSE;
148
149 static kernel_segment_command_t *segTEXT, *segDATA;
150 static kernel_section_t *cursectTEXT, *lastsectTEXT;
151 static kernel_section_t *sectDCONST;
152
153 extern uint64_t firmware_Conventional_bytes;
154 extern uint64_t firmware_RuntimeServices_bytes;
155 extern uint64_t firmware_ACPIReclaim_bytes;
156 extern uint64_t firmware_ACPINVS_bytes;
157 extern uint64_t firmware_PalCode_bytes;
158 extern uint64_t firmware_Reserved_bytes;
159 extern uint64_t firmware_Unusable_bytes;
160 extern uint64_t firmware_other_bytes;
161 uint64_t firmware_MMIO_bytes;
162
163 /*
164 * Linker magic to establish the highest address in the kernel.
165 */
166 extern void *last_kernel_symbol;
167
168 #if DEBUG
169 #define PRINT_PMAP_MEMORY_TABLE
170 #define DBG(x...) kprintf(x)
171 #else
172 #define DBG(x...)
173 #endif /* DEBUG */
174 /*
175 * Basic VM initialization.
176 */
177 void
178 i386_vm_init(uint64_t maxmem,
179 boolean_t IA32e,
180 boot_args *args)
181 {
182 pmap_memory_region_t *pmptr;
183 pmap_memory_region_t *prev_pmptr;
184 EfiMemoryRange *mptr;
185 unsigned int mcount;
186 unsigned int msize;
187 ppnum_t fap;
188 unsigned int i;
189 ppnum_t maxpg = 0;
190 uint32_t pmap_type;
191 uint32_t maxloreserve;
192 uint32_t maxdmaaddr;
193 uint32_t mbuf_reserve = 0;
194 boolean_t mbuf_override = FALSE;
195 boolean_t coalescing_permitted;
196 vm_kernel_base_page = i386_btop(args->kaddr);
197 vm_offset_t base_address;
198 vm_offset_t static_base_address;
199
200 /*
201 * Establish the KASLR parameters.
202 */
203 static_base_address = ml_static_ptovirt(KERNEL_BASE_OFFSET);
204 base_address = ml_static_ptovirt(args->kaddr);
205 vm_kernel_slide = base_address - static_base_address;
206 if (args->kslide) {
207 kprintf("KASLR slide: 0x%016lx dynamic\n", vm_kernel_slide);
208 if (vm_kernel_slide != ((vm_offset_t)args->kslide))
209 panic("Kernel base inconsistent with slide - rebased?");
210 } else {
211 /* No slide relative to on-disk symbols */
212 kprintf("KASLR slide: 0x%016lx static and ignored\n",
213 vm_kernel_slide);
214 vm_kernel_slide = 0;
215 }
216
217 /*
218 * Zero out local relocations to avoid confusing kxld.
219 * TODO: might be better to move this code to OSKext::initialize
220 */
221 if (_mh_execute_header.flags & MH_PIE) {
222 struct load_command *loadcmd;
223 uint32_t cmd;
224
225 loadcmd = (struct load_command *)((uintptr_t)&_mh_execute_header +
226 sizeof (_mh_execute_header));
227
228 for (cmd = 0; cmd < _mh_execute_header.ncmds; cmd++) {
229 if (loadcmd->cmd == LC_DYSYMTAB) {
230 struct dysymtab_command *dysymtab;
231
232 dysymtab = (struct dysymtab_command *)loadcmd;
233 dysymtab->nlocrel = 0;
234 dysymtab->locreloff = 0;
235 kprintf("Hiding local relocations\n");
236 break;
237 }
238 loadcmd = (struct load_command *)((uintptr_t)loadcmd + loadcmd->cmdsize);
239 }
240 }
241
242 /*
243 * Now retrieve addresses for end, edata, and etext
244 * from MACH-O headers.
245 */
246 segTEXTB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header,
247 "__TEXT", &segSizeTEXT);
248 segDATAB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header,
249 "__DATA", &segSizeDATA);
250 segLINKB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header,
251 "__LINKEDIT", &segSizeLINK);
252 segHIBB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header,
253 "__HIB", &segSizeHIB);
254 segPRELINKB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header,
255 "__PRELINK_TEXT", &segSizePRELINK);
256 segPRELINKINFOB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header,
257 "__PRELINK_INFO", &segSizePRELINKINFO);
258 segTEXT = getsegbynamefromheader(&_mh_execute_header,
259 "__TEXT");
260 segDATA = getsegbynamefromheader(&_mh_execute_header,
261 "__DATA");
262 sectDCONST = getsectbynamefromheader(&_mh_execute_header,
263 "__DATA", "__const");
264 cursectTEXT = lastsectTEXT = firstsect(segTEXT);
265 /* Discover the last TEXT section within the TEXT segment */
266 while ((cursectTEXT = nextsect(segTEXT, cursectTEXT)) != NULL) {
267 lastsectTEXT = cursectTEXT;
268 }
269
270 sHIB = segHIBB;
271 eHIB = segHIBB + segSizeHIB;
272 vm_hib_base = sHIB;
273 /* Zero-padded from ehib to stext if text is 2M-aligned */
274 stext = segTEXTB;
275 lowGlo.lgStext = stext;
276 etext = (vm_offset_t) round_page_64(lastsectTEXT->addr + lastsectTEXT->size);
277 /* Zero-padded from etext to sdata if text is 2M-aligned */
278 sdata = segDATAB;
279 edata = segDATAB + segSizeDATA;
280
281 sectCONSTB = (vm_offset_t) sectDCONST->addr;
282 sectSizeConst = sectDCONST->size;
283 sconstdata = sectCONSTB;
284 econstdata = sectCONSTB + sectSizeConst;
285
286 if (sectSizeConst & PAGE_MASK) {
287 kernel_section_t *ns = nextsect(segDATA, sectDCONST);
288 if (ns && !(ns->addr & PAGE_MASK))
289 doconstro_override = TRUE;
290 } else
291 doconstro_override = TRUE;
292
293 DBG("segTEXTB = %p\n", (void *) segTEXTB);
294 DBG("segDATAB = %p\n", (void *) segDATAB);
295 DBG("segLINKB = %p\n", (void *) segLINKB);
296 DBG("segHIBB = %p\n", (void *) segHIBB);
297 DBG("segPRELINKB = %p\n", (void *) segPRELINKB);
298 DBG("segPRELINKINFOB = %p\n", (void *) segPRELINKINFOB);
299 DBG("sHIB = %p\n", (void *) sHIB);
300 DBG("eHIB = %p\n", (void *) eHIB);
301 DBG("stext = %p\n", (void *) stext);
302 DBG("etext = %p\n", (void *) etext);
303 DBG("sdata = %p\n", (void *) sdata);
304 DBG("edata = %p\n", (void *) edata);
305 DBG("sconstdata = %p\n", (void *) sconstdata);
306 DBG("econstdata = %p\n", (void *) econstdata);
307 DBG("kernel_top = %p\n", (void *) &last_kernel_symbol);
308
309 vm_kernel_base = sHIB;
310 vm_kernel_top = (vm_offset_t) &last_kernel_symbol;
311 vm_kernel_stext = stext;
312 vm_kernel_etext = etext;
313 vm_prelink_stext = segPRELINKB;
314 vm_prelink_etext = segPRELINKB + segSizePRELINK;
315 vm_prelink_sinfo = segPRELINKINFOB;
316 vm_prelink_einfo = segPRELINKINFOB + segSizePRELINKINFO;
317 vm_slinkedit = segLINKB;
318 vm_elinkedit = segLINKB + segSizePRELINK;
319
320 vm_set_page_size();
321
322 /*
323 * Compute the memory size.
324 */
325
326 avail_remaining = 0;
327 avail_end = 0;
328 pmptr = pmap_memory_regions;
329 prev_pmptr = 0;
330 pmap_memory_region_count = pmap_memory_region_current = 0;
331 fap = (ppnum_t) i386_btop(first_avail);
332
333 mptr = (EfiMemoryRange *)ml_static_ptovirt((vm_offset_t)args->MemoryMap);
334 if (args->MemoryMapDescriptorSize == 0)
335 panic("Invalid memory map descriptor size");
336 msize = args->MemoryMapDescriptorSize;
337 mcount = args->MemoryMapSize / msize;
338
339 #define FOURGIG 0x0000000100000000ULL
340 #define ONEGIG 0x0000000040000000ULL
341
342 for (i = 0; i < mcount; i++, mptr = (EfiMemoryRange *)(((vm_offset_t)mptr) + msize)) {
343 ppnum_t base, top;
344 uint64_t region_bytes = 0;
345
346 if (pmap_memory_region_count >= PMAP_MEMORY_REGIONS_SIZE) {
347 kprintf("WARNING: truncating memory region count at %d\n", pmap_memory_region_count);
348 break;
349 }
350 base = (ppnum_t) (mptr->PhysicalStart >> I386_PGSHIFT);
351 top = (ppnum_t) (((mptr->PhysicalStart) >> I386_PGSHIFT) + mptr->NumberOfPages - 1);
352
353 if (base == 0) {
354 /*
355 * Avoid having to deal with the edge case of the
356 * very first possible physical page and the roll-over
357 * to -1; just ignore that page.
358 */
359 kprintf("WARNING: ignoring first page in [0x%llx:0x%llx]\n", (uint64_t) base, (uint64_t) top);
360 base++;
361 }
362 if (top + 1 == 0) {
363 /*
364 * Avoid having to deal with the edge case of the
365 * very last possible physical page and the roll-over
366 * to 0; just ignore that page.
367 */
368 kprintf("WARNING: ignoring last page in [0x%llx:0x%llx]\n", (uint64_t) base, (uint64_t) top);
369 top--;
370 }
371 if (top < base) {
372 /*
373 * That was the only page in that region, so
374 * ignore the whole region.
375 */
376 continue;
377 }
378
379 #if MR_RSV_TEST
380 static uint32_t nmr = 0;
381 if ((base > 0x20000) && (nmr++ < 4))
382 mptr->Attribute |= EFI_MEMORY_KERN_RESERVED;
383 #endif
384 region_bytes = (uint64_t)(mptr->NumberOfPages << I386_PGSHIFT);
385 pmap_type = mptr->Type;
386
387 switch (mptr->Type) {
388 case kEfiLoaderCode:
389 case kEfiLoaderData:
390 case kEfiBootServicesCode:
391 case kEfiBootServicesData:
392 case kEfiConventionalMemory:
393 /*
394 * Consolidate usable memory types into one.
395 */
396 pmap_type = kEfiConventionalMemory;
397 sane_size += region_bytes;
398 firmware_Conventional_bytes += region_bytes;
399 break;
400 /*
401 * sane_size should reflect the total amount of physical
402 * RAM in the system, not just the amount that is
403 * available for the OS to use.
404 * FIXME:Consider deriving this value from SMBIOS tables
405 * rather than reverse engineering the memory map.
406 * Alternatively, see
407 * <rdar://problem/4642773> Memory map should
408 * describe all memory
409 * Firmware on some systems guarantees that the memory
410 * map is complete via the "RomReservedMemoryTracked"
411 * feature field--consult that where possible to
412 * avoid the "round up to 128M" workaround below.
413 */
414
415 case kEfiRuntimeServicesCode:
416 case kEfiRuntimeServicesData:
417 firmware_RuntimeServices_bytes += region_bytes;
418 sane_size += region_bytes;
419 break;
420 case kEfiACPIReclaimMemory:
421 firmware_ACPIReclaim_bytes += region_bytes;
422 sane_size += region_bytes;
423 break;
424 case kEfiACPIMemoryNVS:
425 firmware_ACPINVS_bytes += region_bytes;
426 sane_size += region_bytes;
427 break;
428 case kEfiPalCode:
429 firmware_PalCode_bytes += region_bytes;
430 sane_size += region_bytes;
431 break;
432
433 case kEfiReservedMemoryType:
434 firmware_Reserved_bytes += region_bytes;
435 break;
436 case kEfiUnusableMemory:
437 firmware_Unusable_bytes += region_bytes;
438 break;
439 case kEfiMemoryMappedIO:
440 case kEfiMemoryMappedIOPortSpace:
441 firmware_MMIO_bytes += region_bytes;
442 break;
443 default:
444 firmware_other_bytes += region_bytes;
445 break;
446 }
447
448 DBG("EFI region %d: type %u/%d, base 0x%x, top 0x%x %s\n",
449 i, mptr->Type, pmap_type, base, top,
450 (mptr->Attribute&EFI_MEMORY_KERN_RESERVED)? "RESERVED" :
451 (mptr->Attribute&EFI_MEMORY_RUNTIME)? "RUNTIME" : "");
452
453 if (maxpg) {
454 if (base >= maxpg)
455 break;
456 top = (top > maxpg) ? maxpg : top;
457 }
458
459 /*
460 * handle each region
461 */
462 if ((mptr->Attribute & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME ||
463 pmap_type != kEfiConventionalMemory) {
464 prev_pmptr = 0;
465 continue;
466 } else {
467 /*
468 * Usable memory region
469 */
470 if (top < I386_LOWMEM_RESERVED ||
471 !pal_is_usable_memory(base, top)) {
472 prev_pmptr = 0;
473 continue;
474 }
475 /*
476 * A range may be marked with with the
477 * EFI_MEMORY_KERN_RESERVED attribute
478 * on some systems, to indicate that the range
479 * must not be made available to devices.
480 */
481
482 if (mptr->Attribute & EFI_MEMORY_KERN_RESERVED) {
483 if (++pmap_reserved_ranges > PMAP_MAX_RESERVED_RANGES) {
484 panic("Too many reserved ranges %u\n", pmap_reserved_ranges);
485 }
486 }
487
488 if (top < fap) {
489 /*
490 * entire range below first_avail
491 * salvage some low memory pages
492 * we use some very low memory at startup
493 * mark as already allocated here
494 */
495 if (base >= I386_LOWMEM_RESERVED)
496 pmptr->base = base;
497 else
498 pmptr->base = I386_LOWMEM_RESERVED;
499
500 pmptr->end = top;
501
502
503 if ((mptr->Attribute & EFI_MEMORY_KERN_RESERVED) &&
504 (top < vm_kernel_base_page)) {
505 pmptr->alloc_up = pmptr->base;
506 pmptr->alloc_down = pmptr->end;
507 pmap_reserved_range_indices[pmap_last_reserved_range_index++] = pmap_memory_region_count;
508 }
509 else {
510 /*
511 * mark as already mapped
512 */
513 pmptr->alloc_up = top + 1;
514 pmptr->alloc_down = top;
515 }
516 pmptr->type = pmap_type;
517 pmptr->attribute = mptr->Attribute;
518 }
519 else if ( (base < fap) && (top > fap) ) {
520 /*
521 * spans first_avail
522 * put mem below first avail in table but
523 * mark already allocated
524 */
525 pmptr->base = base;
526 pmptr->end = (fap - 1);
527 pmptr->alloc_up = pmptr->end + 1;
528 pmptr->alloc_down = pmptr->end;
529 pmptr->type = pmap_type;
530 pmptr->attribute = mptr->Attribute;
531 /*
532 * we bump these here inline so the accounting
533 * below works correctly
534 */
535 pmptr++;
536 pmap_memory_region_count++;
537
538 pmptr->alloc_up = pmptr->base = fap;
539 pmptr->type = pmap_type;
540 pmptr->attribute = mptr->Attribute;
541 pmptr->alloc_down = pmptr->end = top;
542
543 if (mptr->Attribute & EFI_MEMORY_KERN_RESERVED)
544 pmap_reserved_range_indices[pmap_last_reserved_range_index++] = pmap_memory_region_count;
545 } else {
546 /*
547 * entire range useable
548 */
549 pmptr->alloc_up = pmptr->base = base;
550 pmptr->type = pmap_type;
551 pmptr->attribute = mptr->Attribute;
552 pmptr->alloc_down = pmptr->end = top;
553 if (mptr->Attribute & EFI_MEMORY_KERN_RESERVED)
554 pmap_reserved_range_indices[pmap_last_reserved_range_index++] = pmap_memory_region_count;
555 }
556
557 if (i386_ptob(pmptr->end) > avail_end )
558 avail_end = i386_ptob(pmptr->end);
559
560 avail_remaining += (pmptr->end - pmptr->base);
561 coalescing_permitted = (prev_pmptr && (pmptr->attribute == prev_pmptr->attribute) && ((pmptr->attribute & EFI_MEMORY_KERN_RESERVED) == 0));
562 /*
563 * Consolidate contiguous memory regions, if possible
564 */
565 if (prev_pmptr &&
566 (pmptr->type == prev_pmptr->type) &&
567 (coalescing_permitted) &&
568 (pmptr->base == pmptr->alloc_up) &&
569 (prev_pmptr->end == prev_pmptr->alloc_down) &&
570 (pmptr->base == (prev_pmptr->end + 1)))
571 {
572 prev_pmptr->end = pmptr->end;
573 prev_pmptr->alloc_down = pmptr->alloc_down;
574 } else {
575 pmap_memory_region_count++;
576 prev_pmptr = pmptr;
577 pmptr++;
578 }
579 }
580 }
581
582 #ifdef PRINT_PMAP_MEMORY_TABLE
583 {
584 unsigned int j;
585 pmap_memory_region_t *p = pmap_memory_regions;
586 addr64_t region_start, region_end;
587 addr64_t efi_start, efi_end;
588 for (j=0;j<pmap_memory_region_count;j++, p++) {
589 kprintf("pmap region %d type %d base 0x%llx alloc_up 0x%llx alloc_down 0x%llx top 0x%llx\n",
590 j, p->type,
591 (addr64_t) p->base << I386_PGSHIFT,
592 (addr64_t) p->alloc_up << I386_PGSHIFT,
593 (addr64_t) p->alloc_down << I386_PGSHIFT,
594 (addr64_t) p->end << I386_PGSHIFT);
595 region_start = (addr64_t) p->base << I386_PGSHIFT;
596 region_end = ((addr64_t) p->end << I386_PGSHIFT) - 1;
597 mptr = (EfiMemoryRange *) ml_static_ptovirt((vm_offset_t)args->MemoryMap);
598 for (i=0; i<mcount; i++, mptr = (EfiMemoryRange *)(((vm_offset_t)mptr) + msize)) {
599 if (mptr->Type != kEfiLoaderCode &&
600 mptr->Type != kEfiLoaderData &&
601 mptr->Type != kEfiBootServicesCode &&
602 mptr->Type != kEfiBootServicesData &&
603 mptr->Type != kEfiConventionalMemory) {
604 efi_start = (addr64_t)mptr->PhysicalStart;
605 efi_end = efi_start + ((vm_offset_t)mptr->NumberOfPages << I386_PGSHIFT) - 1;
606 if ((efi_start >= region_start && efi_start <= region_end) ||
607 (efi_end >= region_start && efi_end <= region_end)) {
608 kprintf(" *** Overlapping region with EFI runtime region %d\n", i);
609 }
610 }
611 }
612 }
613 }
614 #endif
615
616 avail_start = first_avail;
617 mem_actual = sane_size;
618
619 /*
620 * For user visible memory size, round up to 128 Mb - accounting for the various stolen memory
621 * not reported by EFI.
622 */
623
624 sane_size = (sane_size + 128 * MB - 1) & ~((uint64_t)(128 * MB - 1));
625
626 /*
627 * We cap at KERNEL_MAXMEM bytes (currently 32GB for K32, 96GB for K64).
628 * Unless overriden by the maxmem= boot-arg
629 * -- which is a non-zero maxmem argument to this function.
630 */
631 if (maxmem == 0 && sane_size > KERNEL_MAXMEM) {
632 maxmem = KERNEL_MAXMEM;
633 printf("Physical memory %lld bytes capped at %dGB\n",
634 sane_size, (uint32_t) (KERNEL_MAXMEM/GB));
635 }
636
637 /*
638 * if user set maxmem, reduce memory sizes
639 */
640 if ( (maxmem > (uint64_t)first_avail) && (maxmem < sane_size)) {
641 ppnum_t discarded_pages = (ppnum_t)((sane_size - maxmem) >> I386_PGSHIFT);
642 ppnum_t highest_pn = 0;
643 ppnum_t cur_end = 0;
644 uint64_t pages_to_use;
645 unsigned cur_region = 0;
646
647 sane_size = maxmem;
648
649 if (avail_remaining > discarded_pages)
650 avail_remaining -= discarded_pages;
651 else
652 avail_remaining = 0;
653
654 pages_to_use = avail_remaining;
655
656 while (cur_region < pmap_memory_region_count && pages_to_use) {
657 for (cur_end = pmap_memory_regions[cur_region].base;
658 cur_end < pmap_memory_regions[cur_region].end && pages_to_use;
659 cur_end++) {
660 if (cur_end > highest_pn)
661 highest_pn = cur_end;
662 pages_to_use--;
663 }
664 if (pages_to_use == 0) {
665 pmap_memory_regions[cur_region].end = cur_end;
666 pmap_memory_regions[cur_region].alloc_down = cur_end;
667 }
668
669 cur_region++;
670 }
671 pmap_memory_region_count = cur_region;
672
673 avail_end = i386_ptob(highest_pn + 1);
674 }
675
676 /*
677 * mem_size is only a 32 bit container... follow the PPC route
678 * and pin it to a 2 Gbyte maximum
679 */
680 if (sane_size > (FOURGIG >> 1))
681 mem_size = (vm_size_t)(FOURGIG >> 1);
682 else
683 mem_size = (vm_size_t)sane_size;
684 max_mem = sane_size;
685
686 kprintf("Physical memory %llu MB\n", sane_size/MB);
687
688 max_valid_low_ppnum = (2 * GB) / PAGE_SIZE;
689
690 if (!PE_parse_boot_argn("max_valid_dma_addr", &maxdmaaddr, sizeof (maxdmaaddr))) {
691 max_valid_dma_address = (uint64_t)4 * (uint64_t)GB;
692 } else {
693 max_valid_dma_address = ((uint64_t) maxdmaaddr) * MB;
694
695 if ((max_valid_dma_address / PAGE_SIZE) < max_valid_low_ppnum)
696 max_valid_low_ppnum = (ppnum_t)(max_valid_dma_address / PAGE_SIZE);
697 }
698 if (avail_end >= max_valid_dma_address) {
699
700 if (!PE_parse_boot_argn("maxloreserve", &maxloreserve, sizeof (maxloreserve))) {
701
702 if (sane_size >= (ONEGIG * 15))
703 maxloreserve = (MAXLORESERVE / PAGE_SIZE) * 4;
704 else if (sane_size >= (ONEGIG * 7))
705 maxloreserve = (MAXLORESERVE / PAGE_SIZE) * 2;
706 else
707 maxloreserve = MAXLORESERVE / PAGE_SIZE;
708
709 #if SOCKETS
710 mbuf_reserve = bsd_mbuf_cluster_reserve(&mbuf_override) / PAGE_SIZE;
711 #endif
712 } else
713 maxloreserve = (maxloreserve * (1024 * 1024)) / PAGE_SIZE;
714
715 if (maxloreserve) {
716 vm_lopage_free_limit = maxloreserve;
717
718 if (mbuf_override == TRUE) {
719 vm_lopage_free_limit += mbuf_reserve;
720 vm_lopage_lowater = 0;
721 } else
722 vm_lopage_lowater = vm_lopage_free_limit / 16;
723
724 vm_lopage_refill = TRUE;
725 vm_lopage_needed = TRUE;
726 }
727 }
728
729 /*
730 * Initialize kernel physical map.
731 * Kernel virtual address starts at VM_KERNEL_MIN_ADDRESS.
732 */
733 kprintf("avail_remaining = 0x%lx\n", (unsigned long)avail_remaining);
734 pmap_bootstrap(0, IA32e);
735 }
736
737
738 unsigned int
739 pmap_free_pages(void)
740 {
741 return (unsigned int)avail_remaining;
742 }
743
744
745 boolean_t pmap_next_page_reserved(ppnum_t *);
746
747 /*
748 * Pick a page from a "kernel private" reserved range; works around
749 * errata on some hardware.
750 */
751 boolean_t
752 pmap_next_page_reserved(ppnum_t *pn) {
753 if (pmap_reserved_ranges) {
754 uint32_t n;
755 pmap_memory_region_t *region;
756 for (n = 0; n < pmap_last_reserved_range_index; n++) {
757 uint32_t reserved_index = pmap_reserved_range_indices[n];
758 region = &pmap_memory_regions[reserved_index];
759 if (region->alloc_up <= region->alloc_down) {
760 *pn = region->alloc_up++;
761 avail_remaining--;
762
763 if (*pn > max_ppnum)
764 max_ppnum = *pn;
765
766 if (lowest_lo == 0 || *pn < lowest_lo)
767 lowest_lo = *pn;
768
769 pmap_reserved_pages_allocated++;
770 #if DEBUG
771 if (region->alloc_up > region->alloc_down) {
772 kprintf("Exhausted reserved range index: %u, base: 0x%x end: 0x%x, type: 0x%x, attribute: 0x%llx\n", reserved_index, region->base, region->end, region->type, region->attribute);
773 }
774 #endif
775 return TRUE;
776 }
777 }
778 }
779 return FALSE;
780 }
781
782
783 boolean_t
784 pmap_next_page_hi(
785 ppnum_t *pn)
786 {
787 pmap_memory_region_t *region;
788 int n;
789
790 if (pmap_next_page_reserved(pn))
791 return TRUE;
792
793 if (avail_remaining) {
794 for (n = pmap_memory_region_count - 1; n >= 0; n--) {
795 region = &pmap_memory_regions[n];
796
797 if (region->alloc_down >= region->alloc_up) {
798 *pn = region->alloc_down--;
799 avail_remaining--;
800
801 if (*pn > max_ppnum)
802 max_ppnum = *pn;
803
804 if (lowest_lo == 0 || *pn < lowest_lo)
805 lowest_lo = *pn;
806
807 if (lowest_hi == 0 || *pn < lowest_hi)
808 lowest_hi = *pn;
809
810 if (*pn > highest_hi)
811 highest_hi = *pn;
812
813 return TRUE;
814 }
815 }
816 }
817 return FALSE;
818 }
819
820
821 boolean_t
822 pmap_next_page(
823 ppnum_t *pn)
824 {
825 if (avail_remaining) while (pmap_memory_region_current < pmap_memory_region_count) {
826 if (pmap_memory_regions[pmap_memory_region_current].alloc_up >
827 pmap_memory_regions[pmap_memory_region_current].alloc_down) {
828 pmap_memory_region_current++;
829 continue;
830 }
831 *pn = pmap_memory_regions[pmap_memory_region_current].alloc_up++;
832 avail_remaining--;
833
834 if (*pn > max_ppnum)
835 max_ppnum = *pn;
836
837 if (lowest_lo == 0 || *pn < lowest_lo)
838 lowest_lo = *pn;
839
840 return TRUE;
841 }
842 return FALSE;
843 }
844
845
846 boolean_t
847 pmap_valid_page(
848 ppnum_t pn)
849 {
850 unsigned int i;
851 pmap_memory_region_t *pmptr = pmap_memory_regions;
852
853 for (i = 0; i < pmap_memory_region_count; i++, pmptr++) {
854 if ( (pn >= pmptr->base) && (pn <= pmptr->end) )
855 return TRUE;
856 }
857 return FALSE;
858 }
859