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