]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/AT386/model_dep.c
a1afdfa9fe584bebb0d24e6df20e499f9560b652
[apple/xnu.git] / osfmk / i386 / AT386 / model_dep.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28 /*
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989, 1988 Carnegie Mellon University
31 * All Rights Reserved.
32 *
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
38 *
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
42 *
43 * Carnegie Mellon requests users of this software to return to
44 *
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
49 *
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
52 */
53
54 /*
55 */
56
57 /*
58 * File: model_dep.c
59 * Author: Avadis Tevanian, Jr., Michael Wayne Young
60 *
61 * Copyright (C) 1986, Avadis Tevanian, Jr., Michael Wayne Young
62 *
63 * Basic initialization for I386 - ISA bus machines.
64 */
65
66 #include <cpus.h>
67 #include <platforms.h>
68 #include <mp_v1_1.h>
69 #include <mach_kdb.h>
70 #include <himem.h>
71 #include <fast_idle.h>
72
73 #include <mach/i386/vm_param.h>
74
75 #include <string.h>
76 #include <mach/vm_param.h>
77 #include <mach/vm_prot.h>
78 #include <mach/machine.h>
79 #include <mach/time_value.h>
80 #include <kern/etap_macros.h>
81 #include <kern/spl.h>
82 #include <kern/assert.h>
83 #include <kern/debug.h>
84 #include <kern/misc_protos.h>
85 #include <kern/startup.h>
86 #include <kern/clock.h>
87 #include <kern/time_out.h>
88 #include <kern/xpr.h>
89 #include <kern/cpu_data.h>
90 #include <vm/vm_page.h>
91 #include <vm/pmap.h>
92 #include <vm/vm_kern.h>
93 #include <i386/fpu.h>
94 #include <i386/pmap.h>
95 #include <i386/ipl.h>
96 #include <i386/pio.h>
97 #include <i386/misc_protos.h>
98 #include <i386/cpuid.h>
99 #include <i386/rtclock_entries.h>
100 #include <i386/AT386/mp/mp.h>
101 #if MACH_KDB
102 #include <ddb/db_aout.h>
103 #endif /* MACH_KDB */
104 #include <ddb/tr.h>
105 #ifdef __MACHO__
106 #include <i386/AT386/kernBootStruct.h>
107 #include <mach/boot_info.h>
108 #include <mach/thread_status.h>
109 #endif
110
111 #if NCPUS > 1
112 #include <i386/mp_desc.h>
113 #endif /* NCPUS */
114
115 #if MP_V1_1
116 #include <i386/AT386/mp/mp_v1_1.h>
117 #endif /* MP_V1_1 */
118
119 #include <IOKit/IOPlatformExpert.h>
120
121 vm_size_t mem_size = 0;
122 uint64_t max_mem;
123 vm_offset_t first_addr = 0; /* set by start.s - keep out of bss */
124 vm_offset_t first_avail = 0;/* first after page tables */
125 vm_offset_t last_addr;
126
127 vm_offset_t avail_start, avail_end;
128 vm_offset_t virtual_avail, virtual_end;
129 vm_offset_t hole_start, hole_end;
130 vm_offset_t avail_next;
131 unsigned int avail_remaining;
132
133 /* parameters passed from bootstrap loader */
134 int cnvmem = 0; /* must be in .data section */
135 int extmem = 0;
136
137 /* FIXME!! REMOVE WHEN OSFMK DEVICES ARE COMPLETELY PULLED OUT */
138 int dev_name_count = 0;
139 int dev_name_list = 0;
140
141 #ifndef __MACHO__
142 extern char edata, end;
143 #endif
144
145 extern char version[];
146
147 void parse_arguments(void);
148 const char *getenv(const char *);
149
150 #define BOOT_LINE_LENGTH 160
151 char boot_string_store[BOOT_LINE_LENGTH] = {0};
152 char *boot_string = (char *)0;
153 int boot_string_sz = BOOT_LINE_LENGTH;
154 int boottype = 0;
155
156 #if __MACHO__
157 #include <mach-o/loader.h>
158 vm_offset_t edata, etext, end;
159
160 extern struct mach_header _mh_execute_header;
161 void *sectTEXTB; int sectSizeTEXT;
162 void *sectDATAB; int sectSizeDATA;
163 void *sectOBJCB; int sectSizeOBJC;
164 void *sectLINKB; int sectSizeLINK;
165
166 /* Kernel boot information */
167 KERNBOOTSTRUCT kernBootStructData;
168 KERNBOOTSTRUCT *kernBootStruct;
169 #endif
170
171 vm_offset_t kern_args_start = 0; /* kernel arguments */
172 vm_size_t kern_args_size = 0; /* size of kernel arguments */
173
174 #ifdef __MACHO__
175
176 unsigned long
177 i386_preinit()
178 {
179 int i;
180 struct segment_command *sgp;
181 struct section *sp;
182
183 sgp = (struct segment_command *) getsegbyname("__DATA");
184 if (sgp) {
185 sp = (struct section *) firstsect(sgp);
186 if (sp) {
187 do {
188 if (sp->flags & S_ZEROFILL)
189 bzero((char *) sp->addr, sp->size);
190 } while (sp = (struct section *)nextsect(sgp, sp));
191 }
192 }
193
194 bcopy((char *) KERNSTRUCT_ADDR, (char *) &kernBootStructData,
195 sizeof(kernBootStructData));
196
197 kernBootStruct = &kernBootStructData;
198
199 end = round_page( kernBootStruct->kaddr + kernBootStruct->ksize );
200
201 return end;
202 }
203 #endif
204
205 /*
206 * Cpu initialization. Running virtual, but without MACH VM
207 * set up. First C routine called.
208 */
209 void
210 machine_startup(void)
211 {
212
213 #ifdef __MACHO__
214 /* Now copy over various bits.. */
215 cnvmem = kernBootStruct->convmem;
216 extmem = kernBootStruct->extmem;
217 kern_args_start = (vm_offset_t) kernBootStruct->bootString;
218 kern_args_size = strlen(kernBootStruct->bootString);
219 boottype = kernBootStruct->rootdev;
220
221 /* Now retrieve addresses for end, edata, and etext
222 * from MACH-O headers.
223 */
224
225 sectTEXTB = (void *) getsegdatafromheader(
226 &_mh_execute_header, "__TEXT", &sectSizeTEXT);
227 sectDATAB = (void *) getsegdatafromheader(
228 &_mh_execute_header, "__DATA", &sectSizeDATA);
229 sectOBJCB = (void *) getsegdatafromheader(
230 &_mh_execute_header, "__OBJC", &sectSizeOBJC);
231 sectLINKB = (void *) getsegdatafromheader(
232 &_mh_execute_header, "__LINKEDIT", &sectSizeLINK);
233
234 etext = (vm_offset_t) sectTEXTB + sectSizeTEXT;
235 edata = (vm_offset_t) sectDATAB + sectSizeDATA;
236 #endif
237
238 printf_init(); /* Init this in case we need debugger */
239 panic_init(); /* Init this in case we need debugger */
240
241 PE_init_platform(FALSE, kernBootStruct);
242 PE_init_kprintf(FALSE);
243 PE_init_printf(FALSE);
244
245 /*
246 * Parse startup arguments
247 */
248 parse_arguments();
249
250 /*
251 * Set up initial thread so current_thread() works early on
252 */
253 pageout_thread.top_act = &pageout_act;
254 pageout_act.thread = &pageout_thread;
255 thread_machine_set_current(&pageout_thread);
256
257 /*
258 * Do basic VM initialization
259 */
260 i386_init();
261
262 PE_init_platform(TRUE, kernBootStruct);
263 PE_init_kprintf(TRUE);
264 PE_init_printf(TRUE);
265
266 #if MACH_KDB
267 /*
268 * Initialize the kernel debugger.
269 */
270 ddb_init();
271
272 /*
273 * Cause a breakpoint trap to the debugger before proceeding
274 * any further if the proper option bit was specified in
275 * the boot flags.
276 */
277
278 if (halt_in_debugger) {
279 printf("inline call to debugger(machine_startup)\n");
280 Debugger("inline call");
281 }
282 #endif /* MACH_KDB */
283
284 TR_INIT();
285
286 printf(version);
287
288 machine_slot[0].is_cpu = TRUE;
289 machine_slot[0].running = TRUE;
290 #ifdef MACH_BSD
291 /* FIXME */
292 machine_slot[0].cpu_type = CPU_TYPE_I386;
293 machine_slot[0].cpu_subtype = CPU_SUBTYPE_PENTPRO;
294 #else
295 machine_slot[0].cpu_type = cpuid_cputype(0);
296 machine_slot[0].cpu_subtype = CPU_SUBTYPE_AT386;
297 #endif
298
299 /*
300 * Start the system.
301 */
302 #if NCPUS > 1
303 mp_desc_init(0);
304 #endif /* NCPUS */
305
306 setup_main();
307 }
308
309
310 vm_offset_t env_start = 0; /* environment */
311 vm_size_t env_size = 0; /* size of environment */
312
313 /*
314 * Parse command line arguments.
315 */
316 void
317 parse_arguments(void)
318 {
319 unsigned int boot_arg;
320
321 if (PE_parse_boot_arg("maxmem", &boot_arg))
322 {
323 mem_size = boot_arg * (1024 * 1024);
324 }
325
326 if (PE_parse_boot_arg("debug", &boot_arg))
327 {
328 if (boot_arg & DB_HALT) halt_in_debugger = 1;
329 if (boot_arg & DB_PRT) disableDebugOuput = FALSE;
330 }
331 }
332
333 const char *
334 getenv(const char *name)
335 {
336 int len = strlen(name);
337 const char *p = (const char *)env_start;
338 const char *endp = p + env_size;
339
340 while (p < endp) {
341 if (len >= endp - p)
342 break;
343 if (strncmp(name, p, len) == 0 && *(p + len) == '=')
344 return p + len + 1;
345 while (*p++)
346 ;
347 }
348 return NULL;
349 }
350
351 extern void
352 calibrate_delay(void);
353
354 /*
355 * Find devices. The system is alive.
356 */
357 void
358 machine_init(void)
359 {
360 int unit;
361 const char *p;
362 int n;
363
364 /*
365 * Adjust delay count before entering drivers
366 */
367
368 calibrate_delay();
369
370 /*
371 * Display CPU identification
372 */
373 cpuid_cpu_display("CPU identification", 0);
374 cpuid_cache_display("CPU configuration", 0);
375
376 #if MP_V1_1
377 mp_v1_1_init();
378 #endif /* MP_V1_1 */
379
380 /*
381 * Set up to use floating point.
382 */
383 init_fpu();
384
385 #if 0
386 #if NPCI > 0
387 dma_zones_init();
388 #endif /* NPCI > 0 */
389 #endif
390
391 /*
392 * Configure clock devices.
393 */
394 clock_config();
395 }
396
397 /*
398 * Halt a cpu.
399 */
400 void
401 halt_cpu(void)
402 {
403 halt_all_cpus(FALSE);
404 }
405
406 int reset_mem_on_reboot = 1;
407
408 /*
409 * Halt the system or reboot.
410 */
411 void
412 halt_all_cpus(boolean_t reboot)
413 {
414 if (reboot)
415 {
416 /*
417 * Tell the BIOS not to clear and test memory.
418 */
419 if (!reset_mem_on_reboot)
420 *(unsigned short *)phystokv(0x472) = 0x1234;
421
422 printf("MACH Reboot\n");
423 PEHaltRestart( kPERestartCPU );
424 }
425 else
426 {
427 printf("CPU halted\n");
428 PEHaltRestart( kPEHaltCPU );
429 }
430 while(1);
431 }
432
433 /*
434 * Basic VM initialization.
435 */
436
437 void
438 i386_init(void)
439 {
440 int i,j; /* Standard index vars. */
441 vm_size_t bios_hole_size;
442
443 #ifndef __MACHO__
444 /*
445 * Zero the BSS.
446 */
447
448 bzero((char *)&edata,(unsigned)(&end - &edata));
449 #endif
450
451 boot_string = &boot_string_store[0];
452
453 /*
454 * Initialize the pic prior to any possible call to an spl.
455 */
456
457 set_cpu_model();
458 vm_set_page_size();
459
460 /*
461 * Initialize the Event Trace Analysis Package
462 * Static Phase: 1 of 2
463 */
464 etap_init_phase1();
465
466 /*
467 * Compute the memory size.
468 */
469
470 #if 1
471 /* FIXME
472 * fdisk needs to change to use a sysctl instead of
473 * opening /dev/kmem and reading out the kernboot structure
474 */
475
476 first_addr = (char *)(KERNSTRUCT_ADDR) + sizeof(KERNBOOTSTRUCT);
477 #else
478 #if NCPUS > 1
479 first_addr = 0x1000;
480 #else
481 /* First two pages are used to boot the other cpus. */
482 /* TODO - reclaim pages after all cpus have booted */
483
484 first_addr = 0x3000;
485 #endif
486 #endif
487
488 /* BIOS leaves data in low memory */
489 last_addr = 1024*1024 + extmem*1024;
490
491 /* extended memory starts at 1MB */
492
493 bios_hole_size = 1024*1024 - trunc_page((vm_offset_t)(1024 * cnvmem));
494
495 /*
496 * Initialize for pmap_free_pages and pmap_next_page.
497 * These guys should be page-aligned.
498 */
499
500 hole_start = trunc_page((vm_offset_t)(1024 * cnvmem));
501 hole_end = round_page((vm_offset_t)first_avail);
502
503 /*
504 * compute mem_size
505 */
506
507 if (mem_size != 0) {
508 if (mem_size < (last_addr) - bios_hole_size)
509 last_addr = mem_size + bios_hole_size;
510 }
511
512 first_addr = round_page(first_addr);
513 last_addr = trunc_page(last_addr);
514 mem_size = last_addr - bios_hole_size;
515 max_mem = mem_size;
516
517 avail_start = first_addr;
518 avail_end = last_addr;
519 avail_next = avail_start;
520
521 /*
522 * Initialize kernel physical map, mapping the
523 * region from loadpt to avail_start.
524 * Kernel virtual address starts at VM_KERNEL_MIN_ADDRESS.
525 */
526
527
528 #if NCPUS > 1 && AT386
529 /*
530 * Must Allocate interrupt stacks before kdb is called and also
531 * before vm is initialized. Must find out number of cpus first.
532 */
533 /*
534 * Get number of cpus to boot, passed as an optional argument
535 * boot: mach [-sah#] # from 0 to 9 is the number of cpus to boot
536 */
537 if (wncpu == -1) {
538 /*
539 * "-1" check above is to allow for old boot loader to pass
540 * wncpu through boothowto. New boot loader uses environment.
541 */
542 const char *cpus;
543 if ((cpus = getenv("cpus")) != NULL) {
544 /* only a single digit for now */
545 if ((*cpus > '0') && (*cpus <= '9'))
546 wncpu = *cpus - '0';
547 } else
548 wncpu = NCPUS;
549 }
550 mp_probe_cpus();
551 interrupt_stack_alloc();
552
553 #endif /* NCPUS > 1 && AT386 */
554
555 pmap_bootstrap(0);
556
557 avail_remaining = atop((avail_end - avail_start) -
558 (hole_end - hole_start));
559 }
560
561 unsigned int
562 pmap_free_pages(void)
563 {
564 return avail_remaining;
565 }
566
567 boolean_t
568 pmap_next_page(
569 vm_offset_t *addrp)
570 {
571 if (avail_next == avail_end)
572 return FALSE;
573
574 /* skip the hole */
575
576 if (avail_next == hole_start)
577 avail_next = hole_end;
578
579 *addrp = avail_next;
580 avail_next += PAGE_SIZE;
581 avail_remaining--;
582
583 return TRUE;
584 }
585
586 boolean_t
587 pmap_valid_page(
588 vm_offset_t x)
589 {
590 return ((avail_start <= x) && (x < avail_end));
591 }
592
593 /*XXX*/
594 void fc_get(mach_timespec_t *ts);
595 #include <kern/clock.h>
596 #include <i386/rtclock_entries.h>
597 extern kern_return_t sysclk_gettime(
598 mach_timespec_t *cur_time);
599 void fc_get(mach_timespec_t *ts) {
600 (void )sysclk_gettime(ts);
601 }
602
603 void
604 Debugger(
605 const char *message)
606 {
607 printf("Debugger called: <%s>\n", message);
608
609 __asm__("int3");
610 }
611
612 void
613 display_syscall(int syscall)
614 {
615 printf("System call happened %d\n", syscall);
616 }
617
618 #if XPR_DEBUG && (NCPUS == 1 || MP_V1_1)
619
620 extern kern_return_t sysclk_gettime_interrupts_disabled(
621 mach_timespec_t *cur_time);
622
623 int xpr_time(void)
624 {
625 mach_timespec_t time;
626
627 sysclk_gettime_interrupts_disabled(&time);
628 return(time.tv_sec*1000000 + time.tv_nsec/1000);
629 }
630 #endif /* XPR_DEBUG && (NCPUS == 1 || MP_V1_1) */
631
632 enable_bluebox()
633 {
634 }
635 disable_bluebox()
636 {
637 }
638
639 char *
640 machine_boot_info(char *buf, vm_size_t size)
641 {
642 *buf ='\0';
643 return buf;
644 }
645