]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ppc/ppc_vm_init.c
xnu-792.6.56.tar.gz
[apple/xnu.git] / osfmk / ppc / ppc_vm_init.c
CommitLineData
1c79356b 1/*
3a60a9f5 2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
ff6e181a
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
1c79356b 12 *
ff6e181a
A
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
ff6e181a
A
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
1c79356b
A
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 * @OSF_COPYRIGHT@
25 */
26/*
27 * @APPLE_FREE_COPYRIGHT@
28 */
29
30#include <mach_debug.h>
31#include <mach_kdb.h>
32#include <mach_kdp.h>
33#include <debug.h>
1c79356b
A
34
35#include <mach/vm_types.h>
36#include <mach/vm_param.h>
37#include <mach/thread_status.h>
38#include <kern/misc_protos.h>
39#include <kern/assert.h>
40#include <kern/cpu_number.h>
91447636 41#include <kern/thread.h>
1c79356b
A
42
43#include <ppc/proc_reg.h>
44#include <ppc/Firmware.h>
45#include <ppc/boot.h>
46#include <ppc/misc_protos.h>
47#include <ppc/pmap.h>
1c79356b
A
48#include <ppc/mem.h>
49#include <ppc/mappings.h>
50#include <ppc/exception.h>
55e303ae 51#include <ppc/lowglobals.h>
1c79356b 52
1c79356b 53#include <mach-o/mach_header.h>
1c79356b 54
55e303ae
A
55extern const char version[];
56extern const char version_variant[];
1c79356b 57
55e303ae
A
58addr64_t hash_table_base; /* Hash table base */
59unsigned int hash_table_size; /* Hash table size */
91447636 60int hash_table_shift; /* "ht_shift" boot arg, used to scale hash_table_size */
55e303ae
A
61vm_offset_t taproot_addr; /* (BRINGUP) */
62unsigned int taproot_size; /* (BRINGUP) */
63unsigned int serialmode; /* Serial mode keyboard and console control */
64extern int disableConsoleOutput;
1c79356b 65
55e303ae 66struct shadowBAT shadow_BAT;
d7e50217 67
3a60a9f5
A
68
69
55e303ae
A
70/*
71 * NOTE: mem_size is bogus on large memory machines. We will pin it to 0x80000000 if there is more than 2 GB
72 * This is left only for compatibility and max_mem should be used.
73 */
74vm_offset_t mem_size; /* Size of actual physical memory present
75 minus any performance buffer and possibly limited
76 by mem_limit in bytes */
77uint64_t mem_actual; /* The "One True" physical memory size
78 actually, it's the highest physical address + 1 */
79uint64_t max_mem; /* Size of physical memory (bytes), adjusted by maxmem */
80uint64_t sane_size; /* Memory size to use for defaults calculations */
81
82
83mem_region_t pmap_mem_regions[PMAP_MEM_REGION_MAX + 1];
84int pmap_mem_regions_count = 0; /* Assume no non-contiguous memory regions */
1c79356b
A
85
86unsigned int avail_remaining = 0;
87vm_offset_t first_avail;
88vm_offset_t static_memory_end;
55e303ae 89addr64_t vm_last_addr = VM_MAX_KERNEL_ADDRESS; /* Highest kernel virtual address known to the VM system */
1c79356b 90
1c79356b
A
91extern struct mach_header _mh_execute_header;
92vm_offset_t sectTEXTB;
93int sectSizeTEXT;
94vm_offset_t sectDATAB;
95int sectSizeDATA;
1c79356b
A
96vm_offset_t sectLINKB;
97int sectSizeLINK;
98vm_offset_t sectKLDB;
99int sectSizeKLD;
55e303ae
A
100vm_offset_t sectPRELINKB;
101int sectSizePRELINK;
91447636
A
102vm_offset_t sectHIBB;
103int sectSizeHIB;
1c79356b
A
104
105vm_offset_t end, etext, edata;
1c79356b
A
106
107extern unsigned long exception_entry;
108extern unsigned long exception_end;
109
110
55e303ae 111void ppc_vm_init(uint64_t mem_limit, boot_args *args)
1c79356b 112{
91447636
A
113 unsigned int i, kmapsize, pvr;
114 vm_offset_t addr;
55e303ae
A
115 unsigned int *xtaproot, bank_shift;
116 uint64_t cbsize, xhid0;
1c79356b 117
de355530 118
55e303ae
A
119/*
120 * Invalidate all shadow BATs
1c79356b 121 */
1c79356b 122
55e303ae
A
123 /* Initialize shadow IBATs */
124 shadow_BAT.IBATs[0].upper=BAT_INVALID;
125 shadow_BAT.IBATs[0].lower=BAT_INVALID;
126 shadow_BAT.IBATs[1].upper=BAT_INVALID;
127 shadow_BAT.IBATs[1].lower=BAT_INVALID;
128 shadow_BAT.IBATs[2].upper=BAT_INVALID;
129 shadow_BAT.IBATs[2].lower=BAT_INVALID;
130 shadow_BAT.IBATs[3].upper=BAT_INVALID;
131 shadow_BAT.IBATs[3].lower=BAT_INVALID;
132
133 /* Initialize shadow DBATs */
134 shadow_BAT.DBATs[0].upper=BAT_INVALID;
135 shadow_BAT.DBATs[0].lower=BAT_INVALID;
136 shadow_BAT.DBATs[1].upper=BAT_INVALID;
137 shadow_BAT.DBATs[1].lower=BAT_INVALID;
138 shadow_BAT.DBATs[2].upper=BAT_INVALID;
139 shadow_BAT.DBATs[2].lower=BAT_INVALID;
140 shadow_BAT.DBATs[3].upper=BAT_INVALID;
141 shadow_BAT.DBATs[3].lower=BAT_INVALID;
142
143
144 /*
145 * Go through the list of memory regions passed in via the boot_args
1c79356b
A
146 * and copy valid entries into the pmap_mem_regions table, adding
147 * further calculated entries.
55e303ae
A
148 *
149 * boot_args version 1 has address instead of page numbers
150 * in the PhysicalDRAM banks, set bank_shift accordingly.
1c79356b
A
151 */
152
55e303ae
A
153 bank_shift = 0;
154 if (args->Version == kBootArgsVersion1) bank_shift = 12;
155
1c79356b 156 pmap_mem_regions_count = 0;
55e303ae
A
157 max_mem = 0; /* Will use to total memory found so far */
158 mem_actual = 0; /* Actual size of memory */
159
160 if (mem_limit == 0) mem_limit = 0xFFFFFFFFFFFFFFFFULL; /* If there is no set limit, use all */
161
162 for (i = 0; i < kMaxDRAMBanks; i++) { /* Look at all of the banks */
163
164 cbsize = (uint64_t)args->PhysicalDRAM[i].size << (12 - bank_shift); /* Remember current size */
165
166 if (!cbsize) continue; /* Skip if the bank is empty */
167
168 mem_actual = mem_actual + cbsize; /* Get true memory size */
1c79356b 169
55e303ae 170 if(mem_limit == 0) continue; /* If we hit restriction, just keep counting */
1c79356b 171
55e303ae
A
172 if (cbsize > mem_limit) cbsize = mem_limit; /* Trim to max allowed */
173 max_mem += cbsize; /* Total up what we have so far */
174 mem_limit = mem_limit - cbsize; /* Calculate amount left to do */
175
176 pmap_mem_regions[pmap_mem_regions_count].mrStart = args->PhysicalDRAM[i].base >> bank_shift; /* Set the start of the bank */
177 pmap_mem_regions[pmap_mem_regions_count].mrAStart = pmap_mem_regions[pmap_mem_regions_count].mrStart; /* Set the start of allocatable area */
178 pmap_mem_regions[pmap_mem_regions_count].mrEnd = ((uint64_t)args->PhysicalDRAM[i].base >> bank_shift) + (cbsize >> 12) - 1; /* Set the end address of bank */
179 pmap_mem_regions[pmap_mem_regions_count].mrAEnd = pmap_mem_regions[pmap_mem_regions_count].mrEnd; /* Set the end address of allocatable area */
1c79356b 180
de355530
A
181 /* Regions must be provided in ascending order */
182 assert ((pmap_mem_regions_count == 0) ||
55e303ae
A
183 pmap_mem_regions[pmap_mem_regions_count].mrStart >
184 pmap_mem_regions[pmap_mem_regions_count-1].mrStart);
1c79356b 185
55e303ae 186 pmap_mem_regions_count++; /* Count this region */
de355530 187 }
91447636 188
55e303ae
A
189 mem_size = (unsigned int)max_mem; /* Get size of memory */
190 if(max_mem > 0x0000000080000000ULL) mem_size = 0x80000000; /* Pin at 2 GB */
d7e50217 191
55e303ae
A
192 sane_size = max_mem; /* Calculate a sane value to use for init */
193 if(sane_size > (addr64_t)(VM_MAX_KERNEL_ADDRESS + 1))
194 sane_size = (addr64_t)(VM_MAX_KERNEL_ADDRESS + 1); /* If flush with ram, use addressible portion */
43866e37 195
d7e50217 196
55e303ae
A
197/*
198 * Initialize the pmap system, using space above `first_avail'
199 * for the necessary data structures.
200 * NOTE : assume that we'll have enough space mapped in already
201 */
202
203 first_avail = static_memory_end;
d7e50217 204
91447636
A
205/*
206 * Now retrieve addresses for end, edata, and etext
207 * from MACH-O headers for the currently running 32 bit kernel.
55e303ae
A
208 */
209 sectTEXTB = (vm_offset_t)getsegdatafromheader(
210 &_mh_execute_header, "__TEXT", &sectSizeTEXT);
211 sectDATAB = (vm_offset_t)getsegdatafromheader(
212 &_mh_execute_header, "__DATA", &sectSizeDATA);
213 sectLINKB = (vm_offset_t)getsegdatafromheader(
214 &_mh_execute_header, "__LINKEDIT", &sectSizeLINK);
215 sectKLDB = (vm_offset_t)getsegdatafromheader(
216 &_mh_execute_header, "__KLD", &sectSizeKLD);
91447636
A
217 sectHIBB = (vm_offset_t)getsegdatafromheader(
218 &_mh_execute_header, "__HIB", &sectSizeHIB);
55e303ae
A
219 sectPRELINKB = (vm_offset_t)getsegdatafromheader(
220 &_mh_execute_header, "__PRELINK", &sectSizePRELINK);
221
222 etext = (vm_offset_t) sectTEXTB + sectSizeTEXT;
223 edata = (vm_offset_t) sectDATAB + sectSizeDATA;
91447636 224 end = round_page(getlastaddr()); /* Force end to next page */
1c79356b 225
91447636
A
226 kmapsize = (round_page(exception_end) - trunc_page(exception_entry)) + /* Get size we will map later */
227 (round_page(sectTEXTB+sectSizeTEXT) - trunc_page(sectTEXTB)) +
228 (round_page(sectDATAB+sectSizeDATA) - trunc_page(sectDATAB)) +
229 (round_page(sectLINKB+sectSizeLINK) - trunc_page(sectLINKB)) +
230 (round_page(sectKLDB+sectSizeKLD) - trunc_page(sectKLDB)) +
231 (round_page_32(sectKLDB+sectSizeHIB) - trunc_page_32(sectHIBB)) +
232 (round_page(sectPRELINKB+sectSizePRELINK) - trunc_page(sectPRELINKB)) +
233 (round_page(static_memory_end) - trunc_page(end));
d7e50217 234
55e303ae 235 pmap_bootstrap(max_mem, &first_avail, kmapsize);
d7e50217 236
91447636
A
237 pmap_map(trunc_page(exception_entry), trunc_page(exception_entry),
238 round_page(exception_end), VM_PROT_READ|VM_PROT_EXECUTE);
55e303ae 239
91447636
A
240 pmap_map(trunc_page(sectTEXTB), trunc_page(sectTEXTB),
241 round_page(sectTEXTB+sectSizeTEXT), VM_PROT_READ|VM_PROT_EXECUTE);
55e303ae 242
91447636
A
243 pmap_map(trunc_page(sectDATAB), trunc_page(sectDATAB),
244 round_page(sectDATAB+sectSizeDATA), VM_PROT_READ|VM_PROT_WRITE);
55e303ae
A
245
246/* The KLD and LINKEDIT segments are unloaded in toto after boot completes,
247* but via ml_static_mfree(), through IODTFreeLoaderInfo(). Hence, we have
248* to map both segments page-by-page.
249*/
250
91447636
A
251 for (addr = trunc_page(sectPRELINKB);
252 addr < round_page(sectPRELINKB+sectSizePRELINK);
253 addr += PAGE_SIZE) {
254
255 pmap_enter(kernel_pmap, (vm_map_offset_t)addr, (ppnum_t)(addr>>12),
256 VM_PROT_READ|VM_PROT_WRITE,
257 VM_WIMG_USE_DEFAULT, TRUE);
258
259 }
260
261 for (addr = trunc_page(sectKLDB);
262 addr < round_page(sectKLDB+sectSizeKLD);
1c79356b
A
263 addr += PAGE_SIZE) {
264
91447636 265 pmap_enter(kernel_pmap, (vm_map_offset_t)addr, (ppnum_t)(addr>>12),
9bccf70c
A
266 VM_PROT_READ|VM_PROT_WRITE,
267 VM_WIMG_USE_DEFAULT, TRUE);
55e303ae 268
1c79356b
A
269 }
270
91447636
A
271 for (addr = trunc_page(sectLINKB);
272 addr < round_page(sectLINKB+sectSizeLINK);
1c79356b
A
273 addr += PAGE_SIZE) {
274
91447636
A
275 pmap_enter(kernel_pmap, (vm_map_offset_t)addr,
276 (ppnum_t)(addr>>12),
9bccf70c
A
277 VM_PROT_READ|VM_PROT_WRITE,
278 VM_WIMG_USE_DEFAULT, TRUE);
55e303ae 279
1c79356b
A
280 }
281
91447636
A
282 for (addr = trunc_page_32(sectHIBB);
283 addr < round_page_32(sectHIBB+sectSizeHIB);
55e303ae
A
284 addr += PAGE_SIZE) {
285
91447636 286 pmap_enter(kernel_pmap, (vm_map_offset_t)addr, (ppnum_t)(addr>>12),
55e303ae
A
287 VM_PROT_READ|VM_PROT_WRITE,
288 VM_WIMG_USE_DEFAULT, TRUE);
289
290 }
291
91447636
A
292 pmap_enter(kernel_pmap, (vm_map_offset_t)&sharedPage,
293 (ppnum_t)&sharedPage >> 12, /* Make sure the sharedPage is mapped */
55e303ae
A
294 VM_PROT_READ|VM_PROT_WRITE,
295 VM_WIMG_USE_DEFAULT, TRUE);
296
91447636
A
297 pmap_enter(kernel_pmap, (vm_map_offset_t)&lowGlo.lgVerCode,
298 (ppnum_t)&lowGlo.lgVerCode >> 12, /* Make sure the low memory globals are mapped */
55e303ae
A
299 VM_PROT_READ|VM_PROT_WRITE,
300 VM_WIMG_USE_DEFAULT, TRUE);
301
1c79356b
A
302/*
303 * We need to map the remainder page-by-page because some of this will
304 * be released later, but not all. Ergo, no block mapping here
305 */
55e303ae 306
91447636 307 for(addr = trunc_page(end); addr < round_page(static_memory_end); addr += PAGE_SIZE) {
55e303ae 308
91447636 309 pmap_enter(kernel_pmap, (vm_map_address_t)addr, (ppnum_t)addr>>12,
9bccf70c
A
310 VM_PROT_READ|VM_PROT_WRITE,
311 VM_WIMG_USE_DEFAULT, TRUE);
d7e50217 312
de355530 313 }
91447636
A
314
315/*
316 * Here we map a window into the kernel address space that will be used to
317 * access a slice of a user address space. Clients for this service include
318 * copyin/out and copypv.
319 */
55e303ae 320
91447636
A
321 lowGlo.lgUMWvaddr = USER_MEM_WINDOW_VADDR;
322 /* Initialize user memory window base address */
323 MapUserMemoryWindowInit(); /* Go initialize user memory window */
1c79356b 324
d52fe63f 325/*
55e303ae
A
326 * At this point, there is enough mapped memory and all hw mapping structures are
327 * allocated and initialized. Here is where we turn on translation for the
328 * VERY first time....
329 *
330 * NOTE: Here is where our very first interruption will happen.
331 *
d52fe63f 332 */
1c79356b 333
55e303ae 334 hw_start_trans(); /* Start translating */
3a60a9f5
A
335 PE_init_platform(TRUE, args); /* Initialize this right off the bat */
336
55e303ae
A
337
338#if 0
339 GratefulDebInit((bootBumbleC *)&(args->Video)); /* Initialize the GratefulDeb debugger */
340#endif
341
342
343 printf_init(); /* Init this in case we need debugger */
344 panic_init(); /* Init this in case we need debugger */
345 PE_init_kprintf(TRUE); /* Note on PPC we only call this after VM is set up */
346
347 kprintf("kprintf initialized\n");
348
349 serialmode = 0; /* Assume normal keyboard and console */
350 if(PE_parse_boot_arg("serial", &serialmode)) { /* Do we want a serial keyboard and/or console? */
351 kprintf("Serial mode specified: %08X\n", serialmode);
352 }
353 if(serialmode & 1) { /* Start serial if requested */
354 (void)switch_to_serial_console(); /* Switch into serial mode */
355 disableConsoleOutput = FALSE; /* Allow printfs to happen */
356 }
357
358 kprintf("max_mem: %ld M\n", (unsigned long)(max_mem >> 20));
359 kprintf("version_variant = %s\n", version_variant);
360 kprintf("version = %s\n\n", version);
361 __asm__ ("mfpvr %0" : "=r" (pvr));
362 kprintf("proc version = %08x\n", pvr);
91447636 363 if(getPerProc()->pf.Available & pf64Bit) { /* 64-bit processor? */
55e303ae
A
364 xhid0 = hid0get64(); /* Get the hid0 */
365 if(xhid0 & (1ULL << (63 - 19))) kprintf("Time base is externally clocked\n");
366 else kprintf("Time base is internally clocked\n");
367 }
368
369
370 taproot_size = PE_init_taproot(&taproot_addr); /* (BRINGUP) See if there is a taproot */
371 if(taproot_size) { /* (BRINGUP) */
372 kprintf("TapRoot card configured to use vaddr = %08X, size = %08X\n", taproot_addr, taproot_size);
373 bcopy_nc((void *)version, (void *)(taproot_addr + 16), strlen(version)); /* (BRINGUP) Pass it our kernel version */
374 __asm__ volatile("eieio"); /* (BRINGUP) */
375 xtaproot = (unsigned int *)taproot_addr; /* (BRINGUP) */
376 xtaproot[0] = 1; /* (BRINGUP) */
377 __asm__ volatile("eieio"); /* (BRINGUP) */
378 }
379
380 PE_create_console(); /* create the console for verbose or pretty mode */
1c79356b 381
55e303ae
A
382 /* setup console output */
383 PE_init_printf(FALSE);
1c79356b 384
1c79356b 385#if DEBUG
55e303ae
A
386 printf("\n\n\nThis program was compiled using gcc %d.%d for powerpc\n",
387 __GNUC__,__GNUC_MINOR__);
388
389
390 /* Processor version information */
391 {
392 unsigned int pvr;
393 __asm__ ("mfpvr %0" : "=r" (pvr));
394 printf("processor version register : %08X\n", pvr);
395 }
396
397 kprintf("Args at %08X\n", args);
398 for (i = 0; i < pmap_mem_regions_count; i++) {
399 printf("DRAM at %08X size %08X\n",
400 args->PhysicalDRAM[i].base,
401 args->PhysicalDRAM[i].size);
402 }
403#endif /* DEBUG */
404
405#if DEBUG
406 kprintf("Mapped memory:\n");
91447636
A
407 kprintf(" exception vector: %08X, %08X - %08X\n", trunc_page(exception_entry),
408 trunc_page(exception_entry), round_page(exception_end));
409 kprintf(" sectTEXTB: %08X, %08X - %08X\n", trunc_page(sectTEXTB),
410 trunc_page(sectTEXTB), round_page(sectTEXTB+sectSizeTEXT));
411 kprintf(" sectDATAB: %08X, %08X - %08X\n", trunc_page(sectDATAB),
412 trunc_page(sectDATAB), round_page(sectDATAB+sectSizeDATA));
413 kprintf(" sectLINKB: %08X, %08X - %08X\n", trunc_page(sectLINKB),
414 trunc_page(sectLINKB), round_page(sectLINKB+sectSizeLINK));
415 kprintf(" sectKLDB: %08X, %08X - %08X\n", trunc_page(sectKLDB),
416 trunc_page(sectKLDB), round_page(sectKLDB+sectSizeKLD));
417 kprintf(" end: %08X, %08X - %08X\n", trunc_page(end),
418 trunc_page(end), static_memory_end);
55e303ae 419
1c79356b 420#endif
55e303ae
A
421
422 return;
1c79356b
A
423}
424