2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989, 1988 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
57 #include <platforms.h>
60 #include <mach/i386/vm_param.h>
63 #include <mach/vm_param.h>
64 #include <mach/vm_prot.h>
65 #include <mach/machine.h>
66 #include <mach/time_value.h>
68 #include <kern/assert.h>
69 #include <kern/debug.h>
70 #include <kern/misc_protos.h>
71 #include <kern/cpu_data.h>
72 #include <kern/processor.h>
73 #include <vm/vm_page.h>
75 #include <vm/vm_kern.h>
76 #include <i386/pmap.h>
78 #include <i386/misc_protos.h>
79 #include <i386/mp_slave_boot.h>
80 #include <i386/cpuid.h>
81 #include <mach/thread_status.h>
82 #include <pexpert/i386/efi.h>
83 #include "i386_lowmem.h"
85 vm_size_t mem_size
= 0;
86 vm_offset_t first_avail
= 0;/* first after page tables */
87 vm_offset_t last_addr
;
89 uint64_t max_mem
; /* Size of physical memory (bytes), adjusted by maxmem */
91 uint64_t sane_size
= 0; /* Memory size to use for defaults calculations */
93 #define MAXBOUNCEPOOL (128 * 1024 * 1024)
94 #define MAXLORESERVE ( 32 * 1024 * 1024)
96 extern int bsd_mbuf_cluster_reserve(void);
99 uint32_t bounce_pool_base
= 0;
100 uint32_t bounce_pool_size
= 0;
102 static void reserve_bouncepool(uint32_t);
105 pmap_paddr_t avail_start
, avail_end
;
106 vm_offset_t virtual_avail
, virtual_end
;
107 static pmap_paddr_t avail_remaining
;
108 vm_offset_t static_memory_end
= 0;
110 #include <mach-o/loader.h>
111 vm_offset_t edata
, etext
, end
;
114 * _mh_execute_header is the mach_header for the currently executing
117 extern struct mach_header _mh_execute_header
;
118 void *sectTEXTB
; int sectSizeTEXT
;
119 void *sectDATAB
; int sectSizeDATA
;
120 void *sectOBJCB
; int sectSizeOBJC
;
121 void *sectLINKB
; int sectSizeLINK
;
122 void *sectPRELINKB
; int sectSizePRELINK
;
123 void *sectHIBB
; int sectSizeHIB
;
125 extern void *getsegdatafromheader(struct mach_header
*, const char *, int *);
126 extern struct segment_command
*getsegbyname(const char *);
127 extern struct section
*firstsect(struct segment_command
*);
128 extern struct section
*nextsect(struct segment_command
*, struct section
*);
132 i386_macho_zerofill(void)
134 struct segment_command
*sgp
;
137 sgp
= getsegbyname("__DATA");
142 if ((sp
->flags
& S_ZEROFILL
))
143 bzero((char *) sp
->addr
, sp
->size
);
144 } while ((sp
= nextsect(sgp
, sp
)));
152 * Basic VM initialization.
155 i386_vm_init(uint64_t maxmem
,
159 pmap_memory_region_t
*pmptr
;
160 pmap_memory_region_t
*prev_pmptr
;
161 EfiMemoryRange
*mptr
;
166 unsigned int safeboot
;
169 uint32_t maxbouncepoolsize
;
170 uint32_t maxloreserve
;
174 * Now retrieve addresses for end, edata, and etext
175 * from MACH-O headers.
178 sectTEXTB
= (void *) getsegdatafromheader(
179 &_mh_execute_header
, "__TEXT", §SizeTEXT
);
180 sectDATAB
= (void *) getsegdatafromheader(
181 &_mh_execute_header
, "__DATA", §SizeDATA
);
182 sectOBJCB
= (void *) getsegdatafromheader(
183 &_mh_execute_header
, "__OBJC", §SizeOBJC
);
184 sectLINKB
= (void *) getsegdatafromheader(
185 &_mh_execute_header
, "__LINKEDIT", §SizeLINK
);
186 sectHIBB
= (void *)getsegdatafromheader(
187 &_mh_execute_header
, "__HIB", §SizeHIB
);
188 sectPRELINKB
= (void *) getsegdatafromheader(
189 &_mh_execute_header
, "__PRELINK", §SizePRELINK
);
191 etext
= (vm_offset_t
) sectTEXTB
+ sectSizeTEXT
;
192 edata
= (vm_offset_t
) sectDATAB
+ sectSizeDATA
;
197 * Compute the memory size.
200 if ((1 == vm_himemory_mode
) || PE_parse_boot_arg("-x", &safeboot
)) {
201 maxpg
= 1 << (32 - I386_PGSHIFT
);
205 pmptr
= pmap_memory_regions
;
207 pmap_memory_region_count
= pmap_memory_region_current
= 0;
208 fap
= (ppnum_t
) i386_btop(first_avail
);
210 mptr
= (EfiMemoryRange
*)args
->MemoryMap
;
211 if (args
->MemoryMapDescriptorSize
== 0)
212 panic("Invalid memory map descriptor size");
213 msize
= args
->MemoryMapDescriptorSize
;
214 mcount
= args
->MemoryMapSize
/ msize
;
216 #define FOURGIG 0x0000000100000000ULL
218 for (i
= 0; i
< mcount
; i
++, mptr
= (EfiMemoryRange
*)(((vm_offset_t
)mptr
) + msize
)) {
221 if (pmap_memory_region_count
>= PMAP_MEMORY_REGIONS_SIZE
) {
222 kprintf("WARNING: truncating memory region count at %d\n", pmap_memory_region_count
);
225 base
= (ppnum_t
) (mptr
->PhysicalStart
>> I386_PGSHIFT
);
226 top
= (ppnum_t
) ((mptr
->PhysicalStart
) >> I386_PGSHIFT
) + mptr
->NumberOfPages
- 1;
228 switch (mptr
->Type
) {
231 case kEfiBootServicesCode
:
232 case kEfiBootServicesData
:
233 case kEfiConventionalMemory
:
235 * Consolidate usable memory types into one.
237 pmap_type
= kEfiConventionalMemory
;
238 sane_size
+= (uint64_t)(mptr
->NumberOfPages
<< I386_PGSHIFT
);
241 case kEfiRuntimeServicesCode
:
242 case kEfiRuntimeServicesData
:
243 case kEfiACPIReclaimMemory
:
244 case kEfiACPIMemoryNVS
:
247 * sane_size should reflect the total amount of physical ram
248 * in the system, not just the amount that is available for
251 sane_size
+= (uint64_t)(mptr
->NumberOfPages
<< I386_PGSHIFT
);
254 case kEfiUnusableMemory
:
255 case kEfiMemoryMappedIO
:
256 case kEfiMemoryMappedIOPortSpace
:
257 case kEfiReservedMemoryType
:
259 pmap_type
= mptr
->Type
;
262 kprintf("EFI region: type = %d/%d, base = 0x%x, top = 0x%x\n", mptr
->Type
, pmap_type
, base
, top
);
267 top
= (top
> maxpg
) ? maxpg
: top
;
273 if (kEfiACPIMemoryNVS
== pmap_type
) {
276 } else if ((mptr
->Attribute
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
||
277 pmap_type
!= kEfiConventionalMemory
) {
282 * Usable memory region
284 if (top
< I386_LOWMEM_RESERVED
) {
290 * entire range below first_avail
291 * salvage some low memory pages
292 * we use some very low memory at startup
293 * mark as already allocated here
295 if (base
>= I386_LOWMEM_RESERVED
)
298 pmptr
->base
= I386_LOWMEM_RESERVED
;
300 * mark as already mapped
302 pmptr
->alloc
= pmptr
->end
= top
;
303 pmptr
->type
= pmap_type
;
305 else if ( (base
< fap
) && (top
> fap
) ) {
308 * put mem below first avail in table but
309 * mark already allocated
312 pmptr
->alloc
= pmptr
->end
= (fap
- 1);
313 pmptr
->type
= pmap_type
;
315 * we bump these here inline so the accounting
316 * below works correctly
319 pmap_memory_region_count
++;
320 pmptr
->alloc
= pmptr
->base
= fap
;
321 pmptr
->type
= pmap_type
;
326 * entire range useable
328 pmptr
->alloc
= pmptr
->base
= base
;
329 pmptr
->type
= pmap_type
;
333 if (i386_ptob(pmptr
->end
) > avail_end
)
334 avail_end
= i386_ptob(pmptr
->end
);
336 avail_remaining
+= (pmptr
->end
- pmptr
->base
);
339 * Consolidate contiguous memory regions, if possible
342 pmptr
->type
== prev_pmptr
->type
&&
343 pmptr
->base
== pmptr
->alloc
&&
344 pmptr
->base
== (prev_pmptr
->end
+ 1)) {
345 prev_pmptr
->end
= pmptr
->end
;
347 pmap_memory_region_count
++;
355 #ifdef PRINT_PMAP_MEMORY_TABLE
358 pmap_memory_region_t
*p
= pmap_memory_regions
;
359 vm_offset_t region_start
, region_end
;
360 vm_offset_t efi_start
, efi_end
;
361 for (j
=0;j
<pmap_memory_region_count
;j
++, p
++) {
362 kprintf("type %d base 0x%x alloc 0x%x top 0x%x\n", p
->type
,
363 p
->base
<< I386_PGSHIFT
, p
->alloc
<< I386_PGSHIFT
, p
->end
<< I386_PGSHIFT
);
364 region_start
= p
->base
<< I386_PGSHIFT
;
365 region_end
= (p
->end
<< I386_PGSHIFT
) - 1;
366 mptr
= args
->MemoryMap
;
367 for (i
=0; i
<mcount
; i
++, mptr
= (EfiMemoryRange
*)(((vm_offset_t
)mptr
) + msize
)) {
368 if (mptr
->Type
!= kEfiLoaderCode
&&
369 mptr
->Type
!= kEfiLoaderData
&&
370 mptr
->Type
!= kEfiBootServicesCode
&&
371 mptr
->Type
!= kEfiBootServicesData
&&
372 mptr
->Type
!= kEfiConventionalMemory
) {
373 efi_start
= (vm_offset_t
)mptr
->PhysicalStart
;
374 efi_end
= efi_start
+ ((vm_offset_t
)mptr
->NumberOfPages
<< I386_PGSHIFT
) - 1;
375 if ((efi_start
>= region_start
&& efi_start
<= region_end
) ||
376 (efi_end
>= region_start
&& efi_end
<= region_end
)) {
377 kprintf(" *** Overlapping region with EFI runtime region %d\n", i
);
386 avail_start
= first_avail
;
387 mem_actual
= sane_size
;
389 #define MEG (1024*1024)
392 * For user visible memory size, round up to 128 Mb - accounting for the various stolen memory
393 * not reported by EFI.
396 sane_size
= (sane_size
+ 128 * MEG
- 1) & ~((uint64_t)(128 * MEG
- 1));
399 * if user set maxmem, reduce memory sizes
401 if ( (maxmem
> (uint64_t)first_avail
) && (maxmem
< sane_size
)) {
402 ppnum_t discarded_pages
= (sane_size
- maxmem
) >> I386_PGSHIFT
;
404 if (avail_remaining
> discarded_pages
)
405 avail_remaining
-= discarded_pages
;
411 * mem_size is only a 32 bit container... follow the PPC route
412 * and pin it to a 2 Gbyte maximum
414 if (sane_size
> (FOURGIG
>> 1))
415 mem_size
= (vm_size_t
)(FOURGIG
>> 1);
417 mem_size
= (vm_size_t
)sane_size
;
420 kprintf("Physical memory %d MB\n", sane_size
/MEG
);
422 if (!PE_parse_boot_arg("max_valid_dma_addr", &maxdmaaddr
))
423 max_valid_dma_address
= 1024ULL * 1024ULL * 4096ULL;
425 max_valid_dma_address
= ((uint64_t) maxdmaaddr
) * 1024ULL * 1024ULL;
427 if (!PE_parse_boot_arg("maxbouncepool", &maxbouncepoolsize
))
428 maxbouncepoolsize
= MAXBOUNCEPOOL
;
430 maxbouncepoolsize
= maxbouncepoolsize
* (1024 * 1024);
433 * bsd_mbuf_cluster_reserve depends on sane_size being set
434 * in order to correctly determine the size of the mbuf pool
435 * that will be reserved
437 if (!PE_parse_boot_arg("maxloreserve", &maxloreserve
))
438 maxloreserve
= MAXLORESERVE
+ bsd_mbuf_cluster_reserve();
440 maxloreserve
= maxloreserve
* (1024 * 1024);
443 if (avail_end
>= max_valid_dma_address
) {
444 if (maxbouncepoolsize
)
445 reserve_bouncepool(maxbouncepoolsize
);
448 vm_lopage_poolsize
= maxloreserve
/ PAGE_SIZE
;
452 * Initialize kernel physical map.
453 * Kernel virtual address starts at VM_KERNEL_MIN_ADDRESS.
455 pmap_bootstrap(0, IA32e
);
460 pmap_free_pages(void)
462 return avail_remaining
;
471 if (avail_remaining
) while (pmap_memory_region_current
< pmap_memory_region_count
) {
472 if (pmap_memory_regions
[pmap_memory_region_current
].alloc
==
473 pmap_memory_regions
[pmap_memory_region_current
].end
) {
474 pmap_memory_region_current
++;
477 *pn
= pmap_memory_regions
[pmap_memory_region_current
].alloc
++;
491 pmap_memory_region_t
*pmptr
= pmap_memory_regions
;
494 for (i
= 0; i
< pmap_memory_region_count
; i
++, pmptr
++) {
495 if ( (pn
>= pmptr
->base
) && (pn
<= pmptr
->end
) && pmptr
->type
== kEfiConventionalMemory
)
503 reserve_bouncepool(uint32_t bounce_pool_wanted
)
505 pmap_memory_region_t
*pmptr
= pmap_memory_regions
;
506 pmap_memory_region_t
*lowest
= NULL
;
508 unsigned int pages_needed
;
510 pages_needed
= bounce_pool_wanted
/ PAGE_SIZE
;
512 for (i
= 0; i
< pmap_memory_region_count
; i
++, pmptr
++) {
513 if ( (pmptr
->type
== kEfiConventionalMemory
) && ((pmptr
->end
- pmptr
->alloc
) >= pages_needed
) ) {
514 if ( (lowest
== NULL
) || (pmptr
->alloc
< lowest
->alloc
) )
518 if ( (lowest
!= NULL
) ) {
519 bounce_pool_base
= lowest
->alloc
* PAGE_SIZE
;
520 bounce_pool_size
= bounce_pool_wanted
;
522 lowest
->alloc
+= pages_needed
;
523 avail_remaining
-= pages_needed
;