d66ffd4efe98f7bf59ce703a85ca6c03abab98d4
[apple/xnu.git] / osfmk / i386 / i386_vm_init.c
1 /*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989, 1988 Carnegie Mellon University
28 * All Rights Reserved.
29 *
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.
35 *
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.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50
51 #include <platforms.h>
52 #include <mach_kdb.h>
53 #include <himem.h>
54
55 #include <mach/i386/vm_param.h>
56
57 #include <string.h>
58 #include <mach/vm_param.h>
59 #include <mach/vm_prot.h>
60 #include <mach/machine.h>
61 #include <mach/time_value.h>
62 #include <kern/spl.h>
63 #include <kern/assert.h>
64 #include <kern/debug.h>
65 #include <kern/misc_protos.h>
66 #include <kern/cpu_data.h>
67 #include <kern/processor.h>
68 #include <vm/vm_page.h>
69 #include <vm/pmap.h>
70 #include <vm/vm_kern.h>
71 #include <i386/pmap.h>
72 #include <i386/ipl.h>
73 #include <i386/pio.h>
74 #include <i386/misc_protos.h>
75 #include <i386/mp_slave_boot.h>
76 #include <i386/cpuid.h>
77 #ifdef __MACHO__
78 #include <mach/thread_status.h>
79 #endif
80
81 vm_size_t mem_size = 0;
82 vm_offset_t first_avail = 0;/* first after page tables */
83 vm_offset_t last_addr;
84
85 uint64_t max_mem;
86 uint64_t sane_size = 0; /* we are going to use the booter memory
87 table info to construct this */
88
89 pmap_paddr_t avail_start, avail_end;
90 vm_offset_t virtual_avail, virtual_end;
91 pmap_paddr_t avail_remaining;
92 vm_offset_t static_memory_end = 0;
93
94 #ifndef __MACHO__
95 extern char edata, end;
96 #endif
97
98 #ifdef __MACHO__
99 #include <mach-o/loader.h>
100 vm_offset_t edata, etext, end;
101
102 /*
103 * _mh_execute_header is the mach_header for the currently executing
104 * 32 bit kernel
105 */
106 extern struct mach_header _mh_execute_header;
107 void *sectTEXTB; int sectSizeTEXT;
108 void *sectDATAB; int sectSizeDATA;
109 void *sectOBJCB; int sectSizeOBJC;
110 void *sectLINKB; int sectSizeLINK;
111 void *sectPRELINKB; int sectSizePRELINK;
112 void *sectHIBB; int sectSizeHIB;
113
114 extern void *getsegdatafromheader(struct mach_header *, const char *, int *);
115 #endif
116
117 /*
118 * Basic VM initialization.
119 */
120 void
121 i386_vm_init(unsigned int maxmem, KernelBootArgs_t *args)
122 {
123 pmap_memory_region_t *pmptr;
124 MemoryRange *mptr;
125 ppnum_t fap;
126 unsigned int i;
127 ppnum_t maxpg = (maxmem >> I386_PGSHIFT);
128
129 #ifdef __MACHO__
130 /* Now retrieve addresses for end, edata, and etext
131 * from MACH-O headers.
132 */
133
134 sectTEXTB = (void *) getsegdatafromheader(
135 &_mh_execute_header, "__TEXT", &sectSizeTEXT);
136 sectDATAB = (void *) getsegdatafromheader(
137 &_mh_execute_header, "__DATA", &sectSizeDATA);
138 sectOBJCB = (void *) getsegdatafromheader(
139 &_mh_execute_header, "__OBJC", &sectSizeOBJC);
140 sectLINKB = (void *) getsegdatafromheader(
141 &_mh_execute_header, "__LINKEDIT", &sectSizeLINK);
142 sectHIBB = (void *)getsegdatafromheader(
143 &_mh_execute_header, "__HIB", &sectSizeHIB);
144 sectPRELINKB = (void *) getsegdatafromheader(
145 &_mh_execute_header, "__PRELINK", &sectSizePRELINK);
146
147 etext = (vm_offset_t) sectTEXTB + sectSizeTEXT;
148 edata = (vm_offset_t) sectDATAB + sectSizeDATA;
149 #endif
150 #ifndef __MACHO__
151 /*
152 * Zero the BSS.
153 */
154
155 bzero((char *)&edata,(unsigned)(&end - &edata));
156 #endif
157
158 /*
159 * Initialize the pic prior to any possible call to an spl.
160 */
161
162 set_cpu_model();
163 vm_set_page_size();
164
165 /*
166 * Compute the memory size.
167 */
168
169 avail_remaining = 0;
170 avail_end = 0;
171 pmptr = pmap_memory_regions;
172 pmap_memory_region_count = pmap_memory_region_current = 0;
173 fap = (ppnum_t) i386_btop(first_avail);
174 mptr = args->memoryMap;
175
176 #ifdef PAE
177 #define FOURGIG 0x0000000100000000ULL
178 for (i=0; i < args->memoryMapCount; i++,mptr++) {
179 ppnum_t base, top;
180
181 base = (ppnum_t) (mptr->base >> I386_PGSHIFT);
182 top = (ppnum_t) ((mptr->base + mptr->length) >> I386_PGSHIFT) - 1;
183
184 if (maxmem) {
185 if (base >= maxpg) break;
186 top = (top > maxpg)? maxpg : top;
187 }
188
189 if (kMemoryRangeUsable != mptr->type) continue;
190 sane_size += (uint64_t)(mptr->length);
191 #ifdef DEVICES_HANDLE_64BIT_IO /* XXX enable else clause when I/O to high memory works */
192 if (top < fap) {
193 /* entire range below first_avail */
194 continue;
195 } else if (mptr->base >= FOURGIG) {
196 /* entire range above 4GB (pre PAE) */
197 continue;
198 } else if ( (base < fap) &&
199 (top > fap)) {
200 /* spans first_avail */
201 /* put mem below first avail in table but
202 mark already allocated */
203 pmptr->base = base;
204 pmptr->alloc = pmptr->end = (fap - 1);
205 pmptr->type = mptr->type;
206 /* we bump these here inline so the accounting below works
207 correctly */
208 pmptr++;
209 pmap_memory_region_count++;
210 pmptr->alloc = pmptr->base = fap;
211 pmptr->type = mptr->type;
212 pmptr->end = top;
213 } else if ( (mptr->base < FOURGIG) &&
214 ((mptr->base+mptr->length) > FOURGIG) ) {
215 /* spans across 4GB (pre PAE) */
216 pmptr->alloc = pmptr->base = base;
217 pmptr->type = mptr->type;
218 pmptr->end = (FOURGIG >> I386_PGSHIFT) - 1;
219 } else {
220 /* entire range useable */
221 pmptr->alloc = pmptr->base = base;
222 pmptr->type = mptr->type;
223 pmptr->end = top;
224 }
225 #else
226 if (top < fap) {
227 /* entire range below first_avail */
228 continue;
229 } else if ( (base < fap) &&
230 (top > fap)) {
231 /* spans first_avail */
232 pmptr->alloc = pmptr->base = fap;
233 pmptr->type = mptr->type;
234 pmptr->end = top;
235 } else {
236 /* entire range useable */
237 pmptr->alloc = pmptr->base = base;
238 pmptr->type = mptr->type;
239 pmptr->end = top;
240 }
241 #endif
242 if (i386_ptob(pmptr->end) > avail_end ) {
243 avail_end = i386_ptob(pmptr->end);
244 }
245 avail_remaining += (pmptr->end - pmptr->base);
246 pmap_memory_region_count++;
247 pmptr++;
248 }
249 #else /* non PAE follows */
250 #define FOURGIG 0x0000000100000000ULL
251 for (i=0; i < args->memoryMapCount; i++,mptr++) {
252 ppnum_t base, top;
253
254 base = (ppnum_t) (mptr->base >> I386_PGSHIFT);
255 top = (ppnum_t) ((mptr->base + mptr->length) >> I386_PGSHIFT) - 1;
256
257 if (maxmem) {
258 if (base >= maxpg) break;
259 top = (top > maxpg)? maxpg : top;
260 }
261
262 if (kMemoryRangeUsable != mptr->type) continue;
263
264 // save other regions
265 if (kMemoryRangeNVS == mptr->type) {
266 pmptr->base = base;
267 pmptr->end = ((mptr->base + mptr->length + I386_PGBYTES - 1) >> I386_PGSHIFT) - 1;
268 pmptr->alloc = pmptr->end;
269 pmptr->type = mptr->type;
270 kprintf("NVS region: 0x%x ->0x%x\n", pmptr->base, pmptr->end);
271 } else if (kMemoryRangeUsable != mptr->type) {
272 continue;
273 } else {
274 // Usable memory region
275 sane_size += (uint64_t)(mptr->length);
276 if (top < fap) {
277 /* entire range below first_avail */
278 /* salvage some low memory pages */
279 /* we use some very low memory at startup */
280 /* mark as already allocated here */
281 pmptr->base = 0x18; /* PAE and HIB use below this */
282 pmptr->alloc = pmptr->end = top; /* mark as already mapped */
283 pmptr->type = mptr->type;
284 } else if (mptr->base >= FOURGIG) {
285 /* entire range above 4GB (pre PAE) */
286 continue;
287 } else if ( (base < fap) &&
288 (top > fap)) {
289 /* spans first_avail */
290 /* put mem below first avail in table but
291 mark already allocated */
292 pmptr->base = base;
293 pmptr->alloc = pmptr->end = (fap - 1);
294 pmptr->type = mptr->type;
295 /* we bump these here inline so the accounting below works
296 correctly */
297 pmptr++;
298 pmap_memory_region_count++;
299 pmptr->alloc = pmptr->base = fap;
300 pmptr->type = mptr->type;
301 pmptr->end = top;
302 } else if ( (mptr->base < FOURGIG) &&
303 ((mptr->base+mptr->length) > FOURGIG) ) {
304 /* spans across 4GB (pre PAE) */
305 pmptr->alloc = pmptr->base = base;
306 pmptr->type = mptr->type;
307 pmptr->end = (FOURGIG >> I386_PGSHIFT) - 1;
308 } else {
309 /* entire range useable */
310 pmptr->alloc = pmptr->base = base;
311 pmptr->type = mptr->type;
312 pmptr->end = top;
313 }
314
315 if (i386_ptob(pmptr->end) > avail_end ) {
316 avail_end = i386_ptob(pmptr->end);
317 }
318
319 avail_remaining += (pmptr->end - pmptr->base);
320 pmap_memory_region_count++;
321 pmptr++;
322 }
323 }
324 #endif
325
326 #ifdef PRINT_PMAP_MEMORY_TABLE
327 {
328 unsigned int j;
329 pmap_memory_region_t *p = pmap_memory_regions;
330 for (j=0;j<pmap_memory_region_count;j++, p++) {
331 kprintf("%d base 0x%x alloc 0x%x top 0x%x\n",j,
332 p->base, p->alloc, p->end);
333 }
334 }
335 #endif
336
337 avail_start = first_avail;
338
339 if (maxmem) { /* if user set maxmem try to use it */
340 uint64_t tmp = (uint64_t)maxmem;
341 /* can't set below first_avail or above actual memory */
342 if ( (maxmem > first_avail) && (tmp < sane_size) ) {
343 sane_size = tmp;
344 avail_end = maxmem;
345 }
346 }
347 // round up to a megabyte - mostly accounting for the
348 // low mem madness
349 sane_size += ( 0x100000ULL - 1);
350 sane_size &= ~0xFFFFFULL;
351
352 #ifndef PAE
353 if (sane_size < FOURGIG)
354 mem_size = (unsigned long) sane_size;
355 else
356 mem_size = (unsigned long) (FOURGIG >> 1);
357 #else
358 mem_size = (unsigned long) sane_size;
359 #endif
360
361 max_mem = sane_size;
362
363 /* now make sane size sane */
364 #define MIN(a,b) (((a)<(b))?(a):(b))
365 #define MEG (1024*1024)
366 sane_size = MIN(sane_size, 256*MEG);
367
368 kprintf("Physical memory %d MB\n",
369 mem_size/MEG);
370
371 /*
372 * Initialize kernel physical map.
373 * Kernel virtual address starts at VM_KERNEL_MIN_ADDRESS.
374 */
375 pmap_bootstrap(0);
376
377
378 }
379
380 unsigned int
381 pmap_free_pages(void)
382 {
383 return avail_remaining;
384 }
385
386 boolean_t
387 pmap_next_page(
388 ppnum_t *pn)
389 {
390
391 while (pmap_memory_region_current < pmap_memory_region_count) {
392 if (pmap_memory_regions[pmap_memory_region_current].alloc ==
393 pmap_memory_regions[pmap_memory_region_current].end) {
394 pmap_memory_region_current++;
395 continue;
396 }
397 *pn = pmap_memory_regions[pmap_memory_region_current].alloc++;
398 avail_remaining--;
399
400 return TRUE;
401 }
402 return FALSE;
403 }
404
405 boolean_t
406 pmap_valid_page(
407 ppnum_t pn)
408 {
409 unsigned int i;
410 pmap_memory_region_t *pmptr = pmap_memory_regions;
411
412 assert(pn);
413 for (i=0; i<pmap_memory_region_count; i++, pmptr++) {
414 if ( (pn >= pmptr->base) && (pn <= pmptr->end) ) {
415 if (pmptr->type == kMemoryRangeUsable)
416 return TRUE;
417 else
418 return FALSE;
419 }
420 }
421 return FALSE;
422 }