2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989, 1988 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
52 #include <platforms.h>
55 #include <fast_idle.h>
57 #include <mach/i386/vm_param.h>
60 #include <mach/vm_param.h>
61 #include <mach/vm_prot.h>
62 #include <mach/machine.h>
63 #include <mach/time_value.h>
64 #include <kern/etap_macros.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>
73 #include <vm/vm_kern.h>
74 #include <i386/pmap.h>
77 #include <i386/misc_protos.h>
78 #include <i386/mp_slave_boot.h>
80 #include <mach/boot_info.h>
81 #include <mach/thread_status.h>
84 vm_size_t mem_size
= 0;
85 vm_offset_t first_addr
= 0; /* set by start.s - keep out of bss */
86 vm_offset_t first_avail
= 0;/* first after page tables */
87 vm_offset_t last_addr
;
92 vm_offset_t avail_start
, avail_end
;
93 vm_offset_t virtual_avail
, virtual_end
;
94 vm_offset_t hole_start
, hole_end
;
95 vm_offset_t avail_next
;
96 unsigned int avail_remaining
;
98 /* parameters passed from bootstrap loader */
99 int cnvmem
= 0; /* must be in .data section */
103 extern char edata
, end
;
107 #include <mach-o/loader.h>
108 vm_offset_t edata
, etext
, end
;
110 extern struct mach_header _mh_execute_header
;
111 void *sectTEXTB
; int sectSizeTEXT
;
112 void *sectDATAB
; int sectSizeDATA
;
113 void *sectOBJCB
; int sectSizeOBJC
;
114 void *sectLINKB
; int sectSizeLINK
;
115 void *sectPRELINKB
; int sectSizePRELINK
;
120 * Basic VM initialization.
123 i386_vm_init(unsigned int maxmem
, KernelBootArgs_t
*args
)
125 int i
,j
; /* Standard index vars. */
126 vm_size_t bios_hole_size
;
129 /* Now retrieve addresses for end, edata, and etext
130 * from MACH-O headers.
133 sectTEXTB
= (void *) getsegdatafromheader(
134 &_mh_execute_header
, "__TEXT", §SizeTEXT
);
135 sectDATAB
= (void *) getsegdatafromheader(
136 &_mh_execute_header
, "__DATA", §SizeDATA
);
137 sectOBJCB
= (void *) getsegdatafromheader(
138 &_mh_execute_header
, "__OBJC", §SizeOBJC
);
139 sectLINKB
= (void *) getsegdatafromheader(
140 &_mh_execute_header
, "__LINKEDIT", §SizeLINK
);
141 sectPRELINKB
= (void *) getsegdatafromheader(
142 &_mh_execute_header
, "__PRELINK", §SizePRELINK
);
144 etext
= (vm_offset_t
) sectTEXTB
+ sectSizeTEXT
;
145 edata
= (vm_offset_t
) sectDATAB
+ sectSizeDATA
;
152 bzero((char *)&edata
,(unsigned)(&end
- &edata
));
155 /* Now copy over various boot args bits.. */
156 cnvmem
= args
->convmem
;
157 extmem
= args
->extmem
;
160 * Initialize the pic prior to any possible call to an spl.
167 * Initialize the Event Trace Analysis Package
168 * Static Phase: 1 of 2
173 * Compute the memory size.
177 /* First two pages are used to boot the other cpus. */
178 /* TODO - reclaim pages after all cpus have booted */
180 first_addr
= MP_FIRST_ADDR
;
185 /* BIOS leaves data in low memory */
186 last_addr
= 1024*1024 + extmem
*1024;
188 /* extended memory starts at 1MB */
190 bios_hole_size
= 1024*1024 - trunc_page((vm_offset_t
)(1024 * cnvmem
));
193 * Initialize for pmap_free_pages and pmap_next_page.
194 * These guys should be page-aligned.
197 hole_start
= trunc_page((vm_offset_t
)(1024 * cnvmem
));
198 hole_end
= round_page((vm_offset_t
)first_avail
);
205 * We're currently limited to 512 MB max physical memory.
207 #define M (1024*1024)
208 #define MAXMEM (512*M)
209 if ((maxmem
== 0) && (last_addr
- bios_hole_size
> MAXMEM
)) {
210 printf("Physical memory %d MB, "\
211 "maximum usable memory limited to %d MB\n",
212 (last_addr
- bios_hole_size
)/M
, MAXMEM
/M
);
217 if (maxmem
< (last_addr
) - bios_hole_size
)
218 last_addr
= maxmem
+ bios_hole_size
;
221 first_addr
= round_page(first_addr
);
222 last_addr
= trunc_page(last_addr
);
223 mem_size
= last_addr
- bios_hole_size
;
225 max_mem
= (uint64_t)mem_size
;
228 avail_start
= first_addr
;
229 avail_end
= last_addr
;
230 avail_next
= avail_start
;
233 interrupt_stack_alloc();
234 #endif /* NCPUS > 1 */
237 * Initialize kernel physical map.
238 * Kernel virtual address starts at VM_KERNEL_MIN_ADDRESS.
242 avail_remaining
= atop((avail_end
- avail_start
) -
243 (hole_end
- hole_start
));
247 pmap_free_pages(void)
249 return avail_remaining
;
256 if (avail_next
== avail_end
)
261 if (avail_next
== hole_start
)
262 avail_next
= hole_end
;
264 *pn
= (ppnum_t
)i386_btop(avail_next
);
265 avail_next
+= PAGE_SIZE
;
275 return ((avail_start
<= x
) && (x
< avail_end
));