]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/ppc/ppc_init.c
416b095f35cc86007017b6e63f608db65dab319b
[apple/xnu.git] / bsd / dev / ppc / ppc_init.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30
31
32 #include <mach/vm_types.h>
33 #include <mach/vm_param.h>
34 #include <mach/thread_status.h>
35 #include <kern/misc_protos.h>
36 #include <kern/assert.h>
37 #include <kern/cpu_number.h>
38
39 #include <ppc/proc_reg.h>
40 #include <ppc/boot.h>
41 #include <ppc/misc_protos.h>
42 #include <ppc/pmap.h>
43 #include <ppc/pmap_internals.h>
44 #include <ppc/mem.h>
45 #include <ppc/exception.h>
46 #include <ppc/gdb_defs.h>
47 #include <ppc/POWERMAC/video_board.h>
48 #include <ppc/POWERMAC/video_pdm.h>
49
50 #ifdef __MACHO__
51 #include <mach-o/mach_header.h>
52 #endif
53
54 /* External references */
55
56 extern unsigned int intstack[]; /* declared in start.s */
57 extern unsigned int intstack_top_ss; /* declared in start.s */
58 #if MACH_KGDB
59 extern unsigned int gdbstackptr; /* declared in start.s */
60 extern unsigned int gdbstack_top_ss; /* declared in start.s */
61 #endif /* MACH_KGDB */
62
63 /* Stuff declared in kern/bootstrap.c which we may need to initialise */
64
65 extern vm_offset_t boot_start;
66 extern vm_size_t boot_size;
67 extern vm_offset_t boot_region_desc;
68 extern vm_size_t boot_region_count;
69 extern int boot_thread_state_flavor;
70 extern thread_state_t boot_thread_state;
71 extern unsigned int boot_thread_state_count;
72
73 /* Trap handling function prototypes */
74
75 extern void thandler(void); /* trap handler */
76 extern void ihandler(void); /* interrupt handler */
77 extern void shandler(void); /* syscall handler */
78 extern void gdbhandler(void); /* debugger handler */
79 extern void fpu_switch(void); /* fp handler */
80 extern void atomic_switch_trap(void); /* fast path atomic thread switch */
81
82 /* definitions */
83
84 struct ppc_thread_state boot_task_thread_state;
85
86
87
88
89
90 #if 1 /* TODO NMGS - vm_map_steal_memory shouldn't use these - remove */
91 vm_offset_t avail_start;
92 vm_offset_t avail_end;
93 #endif
94 unsigned int avail_remaining = 0;
95 vm_offset_t first_avail;
96
97 /*
98 * Mach-O Support
99 */
100
101
102 #ifdef __MACHO__
103 extern struct mach_header _mh_execute_header;
104 void *sectTEXTB;
105 int sectSizeTEXT;
106 void *sectDATAB;
107 int sectSizeDATA;
108 void *sectOBJCB;
109 int sectSizeOBJC;
110 void *sectLINKB;
111 int sectSizeLINK;
112
113 vm_offset_t end, etext, edata;
114 #define ETEXT etext
115 #endif
116
117
118
119 void ppc_vm_init(unsigned int memory_size, boot_args *args)
120 {
121 unsigned int htabmask;
122 unsigned int i;
123 vm_offset_t addr;
124 int boot_task_end_offset;
125 #if NCPUS > 1
126 const char *cpus;
127 #endif /* NCPUS > 1 */
128
129 printf("mem_size = %d M\n",memory_size / (1024 * 1024));
130
131 #ifdef __MACHO__
132 /* Now retrieve addresses for end, edata, and etext
133 * from MACH-O headers.
134 */
135
136
137 etext = (vm_offset_t) sectTEXTB + sectSizeTEXT;
138 edata = (vm_offset_t) sectDATAB + sectSizeDATA;
139 end = getlastaddr();
140 #endif
141
142 /* Stitch valid memory regions together - they may be contiguous
143 * even though they're not already glued together
144 */
145
146 /* Go through the list of memory regions passed in via the args
147 * and copy valid entries into the pmap_mem_regions table, adding
148 * further calculated entries.
149 */
150
151
152 /* Initialise the pmap system, using space above `first_avail'*/
153
154 #ifndef __MACHO__
155 free_regions[free_regions_count].start =
156 round_page((unsigned int)&_ExceptionVectorsEnd -
157 (unsigned int)&_ExceptionVectorsStart);
158 #else
159 /* On MACH-O generated kernels, the Exception Vectors
160 * are already mapped and loaded at 0 -- no relocation
161 * or freeing of memory is needed
162 */
163
164 free_regions[free_regions_count].start = round_page((unsigned int)&_ExceptionVectorsEnd) + 4096;
165 #endif
166
167 /* If we are on a PDM machine memory at 1M might be used
168 * for video. TODO NMGS call video driver to do this
169 * somehow
170 */
171
172
173 /* For PowerMac, first_avail is set to above the bootstrap task.
174 * TODO NMGS - different screen modes - might free mem?
175 */
176
177 first_avail = round_page(args->first_avail);
178
179
180 /* map in the exception vectors */
181 /*
182 * map the kernel text, data and bss. Don't forget other regions too
183 */
184 for (i = 0; i < args->kern_info.region_count; i++) {
185 #if MACH_KDB
186 if (args->kern_info.regions[i].prot == VM_PROT_NONE &&
187 i == args->kern_info.region_count - 1) {
188 /* assume that's the kernel symbol table */
189 kern_sym_start = args->kern_info.regions[i].addr;
190 kern_sym_size = args->kern_info.regions[i].size;
191 printf("kernel symbol table at 0x%x size 0x%x\n",
192 kern_sym_start, kern_sym_size);
193 args->kern_info.regions[i].prot |=
194 (VM_PROT_WRITE|VM_PROT_READ);
195 }
196 #endif /* MACH_KDB */
197
198 #ifdef __MACHO__
199 /* Skip the VECTORS segment */
200 if (args->kern_info.regions[i].addr == 0)
201 continue;
202 #endif
203
204 boot_region_count = args->task_info.region_count;
205 boot_size = 0;
206 boot_task_end_offset = 0;
207 /* Map bootstrap task pages 1-1 so that user_bootstrap can find it */
208 for (i = 0; i < boot_region_count; i++) {
209 if (args->task_info.regions[i].mapped) {
210 /* kernel requires everything page aligned */
211 #if DEBUG
212 printf("mapping virt 0x%08x to phys 0x%08x end 0x%x, prot=0x%b\n",
213 ppc_trunc_page(args->task_info.base_addr +
214 args->task_info.regions[i].offset),
215 ppc_trunc_page(args->task_info.base_addr +
216 args->task_info.regions[i].offset),
217 ppc_round_page(args->task_info.base_addr +
218 args->task_info.regions[i].offset +
219 args->task_info.regions[i].size),
220 args->task_info.regions[i].prot,
221 "\x10\1READ\2WRITE\3EXEC");
222 #endif /* DEBUG */
223
224 (void)pmap_map(
225 ppc_trunc_page(args->task_info.base_addr +
226 args->task_info.regions[i].offset),
227 ppc_trunc_page(args->task_info.base_addr +
228 args->task_info.regions[i].offset),
229 ppc_round_page(args->task_info.base_addr +
230 args->task_info.regions[i].offset +
231 args->task_info.regions[i].size),
232 args->task_info.regions[i].prot);
233
234 /* Count the size of mapped space */
235 boot_size += args->task_info.regions[i].size;
236
237 /* There may be an overlapping physical page
238 * mapped to two different virtual addresses
239 */
240 if (boot_task_end_offset >
241 args->task_info.regions[i].offset) {
242 boot_size -= boot_task_end_offset -
243 args->task_info.regions[i].offset;
244 #if DEBUG
245 printf("WARNING - bootstrap overlaps regions\n");
246 #endif /* DEBUG */
247 }
248
249 boot_task_end_offset =
250 args->task_info.regions[i].offset +
251 args->task_info.regions[i].size;
252 }
253 }
254
255 if (boot_region_count) {
256
257 /* Add a new region to the bootstrap task for it's stack */
258 args->task_info.regions[boot_region_count].addr =
259 BOOT_STACK_BASE;
260 args->task_info.regions[boot_region_count].size =
261 BOOT_STACK_SIZE;
262 args->task_info.regions[boot_region_count].mapped = FALSE;
263 boot_region_count++;
264
265 boot_start = args->task_info.base_addr;
266 boot_region_desc = (vm_offset_t) args->task_info.regions;
267 /* TODO NMGS need to put param info onto top of boot stack */
268 boot_task_thread_state.r1 = BOOT_STACK_PTR-0x100;
269 boot_task_thread_state.srr0 = args->task_info.entry;
270 boot_task_thread_state.srr1 =
271 MSR_MARK_SYSCALL(MSR_EXPORT_MASK_SET);
272
273 boot_thread_state_flavor = PPC_THREAD_STATE;
274 boot_thread_state_count = PPC_THREAD_STATE_COUNT;
275 boot_thread_state =
276 (thread_state_t)&boot_task_thread_state;
277 }
278
279
280
281 }
282