2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
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
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
21 * @APPLE_LICENSE_HEADER_END@
27 * @APPLE_FREE_COPYRIGHT@
30 #include <mach_debug.h>
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>
41 #include <kern/thread.h>
43 #include <ppc/proc_reg.h>
44 #include <ppc/Firmware.h>
46 #include <ppc/misc_protos.h>
49 #include <ppc/mappings.h>
50 #include <ppc/exception.h>
51 #include <ppc/lowglobals.h>
53 #include <mach-o/mach_header.h>
55 extern const char version
[];
56 extern const char version_variant
[];
58 addr64_t hash_table_base
; /* Hash table base */
59 unsigned int hash_table_size
; /* Hash table size */
60 int hash_table_shift
; /* "ht_shift" boot arg, used to scale hash_table_size */
61 vm_offset_t taproot_addr
; /* (BRINGUP) */
62 unsigned int taproot_size
; /* (BRINGUP) */
63 unsigned int serialmode
; /* Serial mode keyboard and console control */
64 extern int disableConsoleOutput
;
66 struct shadowBAT shadow_BAT
;
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.
74 vm_offset_t mem_size
; /* Size of actual physical memory present
75 minus any performance buffer and possibly limited
76 by mem_limit in bytes */
77 uint64_t mem_actual
; /* The "One True" physical memory size
78 actually, it's the highest physical address + 1 */
79 uint64_t max_mem
; /* Size of physical memory (bytes), adjusted by maxmem */
80 uint64_t sane_size
; /* Memory size to use for defaults calculations */
83 mem_region_t pmap_mem_regions
[PMAP_MEM_REGION_MAX
+ 1];
84 int pmap_mem_regions_count
= 0; /* Assume no non-contiguous memory regions */
86 unsigned int avail_remaining
= 0;
87 vm_offset_t first_avail
;
88 vm_offset_t static_memory_end
;
89 addr64_t vm_last_addr
= VM_MAX_KERNEL_ADDRESS
; /* Highest kernel virtual address known to the VM system */
91 extern struct mach_header _mh_execute_header
;
92 vm_offset_t sectTEXTB
;
94 vm_offset_t sectDATAB
;
96 vm_offset_t sectLINKB
;
100 vm_offset_t sectPRELINKB
;
102 vm_offset_t sectHIBB
;
105 vm_offset_t end
, etext
, edata
;
107 extern unsigned long exception_entry
;
108 extern unsigned long exception_end
;
111 void ppc_vm_init(uint64_t mem_limit
, boot_args
*args
)
113 unsigned int i
, kmapsize
, pvr
;
115 unsigned int *xtaproot
, bank_shift
;
116 uint64_t cbsize
, xhid0
;
120 * Invalidate all shadow BATs
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
;
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
;
145 * Go through the list of memory regions passed in via the boot_args
146 * and copy valid entries into the pmap_mem_regions table, adding
147 * further calculated entries.
149 * boot_args version 1 has address instead of page numbers
150 * in the PhysicalDRAM banks, set bank_shift accordingly.
154 if (args
->Version
== kBootArgsVersion1
) bank_shift
= 12;
156 pmap_mem_regions_count
= 0;
157 max_mem
= 0; /* Will use to total memory found so far */
158 mem_actual
= 0; /* Actual size of memory */
160 if (mem_limit
== 0) mem_limit
= 0xFFFFFFFFFFFFFFFFULL
; /* If there is no set limit, use all */
162 for (i
= 0; i
< kMaxDRAMBanks
; i
++) { /* Look at all of the banks */
164 cbsize
= (uint64_t)args
->PhysicalDRAM
[i
].size
<< (12 - bank_shift
); /* Remember current size */
166 if (!cbsize
) continue; /* Skip if the bank is empty */
168 mem_actual
= mem_actual
+ cbsize
; /* Get true memory size */
170 if(mem_limit
== 0) continue; /* If we hit restriction, just keep counting */
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 */
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 */
181 /* Regions must be provided in ascending order */
182 assert ((pmap_mem_regions_count
== 0) ||
183 pmap_mem_regions
[pmap_mem_regions_count
].mrStart
>
184 pmap_mem_regions
[pmap_mem_regions_count
-1].mrStart
);
186 pmap_mem_regions_count
++; /* Count this region */
189 mem_size
= (unsigned int)max_mem
; /* Get size of memory */
190 if(max_mem
> 0x0000000080000000ULL
) mem_size
= 0x80000000; /* Pin at 2 GB */
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 */
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
203 first_avail
= static_memory_end
;
206 * Now retrieve addresses for end, edata, and etext
207 * from MACH-O headers for the currently running 32 bit kernel.
209 sectTEXTB
= (vm_offset_t
)getsegdatafromheader(
210 &_mh_execute_header
, "__TEXT", §SizeTEXT
);
211 sectDATAB
= (vm_offset_t
)getsegdatafromheader(
212 &_mh_execute_header
, "__DATA", §SizeDATA
);
213 sectLINKB
= (vm_offset_t
)getsegdatafromheader(
214 &_mh_execute_header
, "__LINKEDIT", §SizeLINK
);
215 sectKLDB
= (vm_offset_t
)getsegdatafromheader(
216 &_mh_execute_header
, "__KLD", §SizeKLD
);
217 sectHIBB
= (vm_offset_t
)getsegdatafromheader(
218 &_mh_execute_header
, "__HIB", §SizeHIB
);
219 sectPRELINKB
= (vm_offset_t
)getsegdatafromheader(
220 &_mh_execute_header
, "__PRELINK", §SizePRELINK
);
222 etext
= (vm_offset_t
) sectTEXTB
+ sectSizeTEXT
;
223 edata
= (vm_offset_t
) sectDATAB
+ sectSizeDATA
;
224 end
= round_page(getlastaddr()); /* Force end to next page */
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
));
235 pmap_bootstrap(max_mem
, &first_avail
, kmapsize
);
237 pmap_map(trunc_page(exception_entry
), trunc_page(exception_entry
),
238 round_page(exception_end
), VM_PROT_READ
|VM_PROT_EXECUTE
);
240 pmap_map(trunc_page(sectTEXTB
), trunc_page(sectTEXTB
),
241 round_page(sectTEXTB
+sectSizeTEXT
), VM_PROT_READ
|VM_PROT_EXECUTE
);
243 pmap_map(trunc_page(sectDATAB
), trunc_page(sectDATAB
),
244 round_page(sectDATAB
+sectSizeDATA
), VM_PROT_READ
|VM_PROT_WRITE
);
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.
251 for (addr
= trunc_page(sectPRELINKB
);
252 addr
< round_page(sectPRELINKB
+sectSizePRELINK
);
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
);
261 for (addr
= trunc_page(sectKLDB
);
262 addr
< round_page(sectKLDB
+sectSizeKLD
);
265 pmap_enter(kernel_pmap
, (vm_map_offset_t
)addr
, (ppnum_t
)(addr
>>12),
266 VM_PROT_READ
|VM_PROT_WRITE
,
267 VM_WIMG_USE_DEFAULT
, TRUE
);
271 for (addr
= trunc_page(sectLINKB
);
272 addr
< round_page(sectLINKB
+sectSizeLINK
);
275 pmap_enter(kernel_pmap
, (vm_map_offset_t
)addr
,
277 VM_PROT_READ
|VM_PROT_WRITE
,
278 VM_WIMG_USE_DEFAULT
, TRUE
);
282 for (addr
= trunc_page_32(sectHIBB
);
283 addr
< round_page_32(sectHIBB
+sectSizeHIB
);
286 pmap_enter(kernel_pmap
, (vm_map_offset_t
)addr
, (ppnum_t
)(addr
>>12),
287 VM_PROT_READ
|VM_PROT_WRITE
,
288 VM_WIMG_USE_DEFAULT
, TRUE
);
292 pmap_enter(kernel_pmap
, (vm_map_offset_t
)&sharedPage
,
293 (ppnum_t
)&sharedPage
>> 12, /* Make sure the sharedPage is mapped */
294 VM_PROT_READ
|VM_PROT_WRITE
,
295 VM_WIMG_USE_DEFAULT
, TRUE
);
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 */
299 VM_PROT_READ
|VM_PROT_WRITE
,
300 VM_WIMG_USE_DEFAULT
, TRUE
);
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
307 for(addr
= trunc_page(end
); addr
< round_page(static_memory_end
); addr
+= PAGE_SIZE
) {
309 pmap_enter(kernel_pmap
, (vm_map_address_t
)addr
, (ppnum_t
)addr
>>12,
310 VM_PROT_READ
|VM_PROT_WRITE
,
311 VM_WIMG_USE_DEFAULT
, TRUE
);
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.
321 lowGlo
.lgUMWvaddr
= USER_MEM_WINDOW_VADDR
;
322 /* Initialize user memory window base address */
323 MapUserMemoryWindowInit(); /* Go initialize user memory window */
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....
330 * NOTE: Here is where our very first interruption will happen.
334 hw_start_trans(); /* Start translating */
335 PE_init_platform(TRUE
, args
); /* Initialize this right off the bat */
339 GratefulDebInit((bootBumbleC
*)&(args
->Video
)); /* Initialize the GratefulDeb debugger */
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 */
347 kprintf("kprintf initialized\n");
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
);
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 */
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
);
363 if(getPerProc()->pf
.Available
& pf64Bit
) { /* 64-bit processor? */
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");
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) */
380 PE_create_console(); /* create the console for verbose or pretty mode */
382 /* setup console output */
383 PE_init_printf(FALSE
);
386 printf("\n\n\nThis program was compiled using gcc %d.%d for powerpc\n",
387 __GNUC__
,__GNUC_MINOR__
);
390 /* Processor version information */
393 __asm__ ("mfpvr %0" : "=r" (pvr
));
394 printf("processor version register : %08X\n", pvr
);
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
);
406 kprintf("Mapped memory:\n");
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
);