Debian Codegraph @

i386/i386at/model_dep.c

raw
1 /*
2 * Mach Operating System
3 * Copyright (c) 1991,1990,1989, 1988 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or [email protected]
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie Mellon
24 * the rights to redistribute these changes.
25 */
26 /*
27 * File: model_dep.c
28 * Author: Avadis Tevanian, Jr., Michael Wayne Young
29 *
30 * Copyright (C) 1986, Avadis Tevanian, Jr., Michael Wayne Young
31 *
32 * Basic initialization for I386 - ISA bus machines.
33 */
34
35 #include <inttypes.h>
36 #include <string.h>
37
38 #include <device/cons.h>
39
40 #include <mach/vm_param.h>
41 #include <mach/vm_prot.h>
42 #include <mach/machine.h>
43 #include <mach/machine/multiboot.h>
44 #include <mach/xen.h>
45
46 #include <kern/assert.h>
47 #include <kern/cpu_number.h>
48 #include <kern/debug.h>
49 #include <kern/mach_clock.h>
50 #include <kern/macros.h>
51 #include <kern/printf.h>
52 #include <kern/startup.h>
53 #include <kern/smp.h>
54 #include <sys/types.h>
55 #include <vm/vm_page.h>
56 #include <i386/fpu.h>
57 #include <i386/gdt.h>
58 #include <i386/ktss.h>
59 #include <i386/ldt.h>
60 #include <i386/spl.h>
61 #include <i386/mp_desc.h>
62 #include <i386/pit.h>
63 #include <i386/pmap.h>
64 #include <i386/proc_reg.h>
65 #include <i386/vm_param.h>
66 #include <i386/locore.h>
67 #include <i386/model_dep.h>
68 #include <i386/smp.h>
69 #include <i386/seg.h>
70 #include <i386at/acpi_parse_apic.h>
71 #include <i386at/autoconf.h>
72 #include <i386at/biosmem.h>
73 #include <i386at/elf.h>
74 #include <i386at/idt.h>
75 #include <i386at/int_init.h>
76 #include <i386at/kd.h>
77 #include <i386at/rtc.h>
78 #include <i386at/mbinfo.h>
79 #include <i386at/model_dep.h>
80 #include <machine/irq.h>
81
82 #ifdef MACH_XEN
83 #include <xen/console.h>
84 #include <xen/store.h>
85 #include <xen/evt.h>
86 #include <xen/xen.h>
87 #endif /* MACH_XEN */
88
89 #if ENABLE_IMMEDIATE_CONSOLE
90 #include "immc.h"
91 #endif /* ENABLE_IMMEDIATE_CONSOLE */
92
93 /* Location of the kernel's symbol table.
94 Both of these are 0 if none is available. */
95 #if MACH_KDB
96 #include <ddb/db_sym.h>
97 #include <i386/db_interface.h>
98
99 /* ELF section header */
100 static unsigned elf_shdr_num;
101 static vm_size_t elf_shdr_size;
102 static vm_offset_t elf_shdr_addr;
103 static unsigned elf_shdr_shndx;
104
105 #endif /* MACH_KDB */
106
107 #define RESERVED_BIOS 0x10000
108
109 /* A copy of the multiboot info structure passed by the boot loader. */
110 #ifdef MACH_XEN
111 struct start_info boot_info;
112 #ifdef MACH_PSEUDO_PHYS
113 unsigned long *mfn_list;
114 #if VM_MIN_KERNEL_ADDRESS != LINEAR_MIN_KERNEL_ADDRESS
115 unsigned long *pfn_list = (void*) PFN_LIST;
116 #endif
117 #endif /* MACH_PSEUDO_PHYS */
118 #if VM_MIN_KERNEL_ADDRESS != LINEAR_MIN_KERNEL_ADDRESS
119 unsigned long la_shift = VM_MIN_KERNEL_ADDRESS;
120 #endif
121 #else /* MACH_XEN */
122 struct multiboot_raw_info boot_info;
123 #endif /* MACH_XEN */
124
125 /* Command line supplied to kernel. */
126 char *kernel_cmdline = "";
127
128 extern char version[];
129
130 /* Realmode temporary GDT */
131 extern struct pseudo_descriptor gdt_descr_tmp;
132
133 /* Realmode relocated jmp */
134 extern uint32_t apboot_jmp_offset;
135
136 /* If set, reboot the system on ctrl-alt-delete. */
137 boolean_t rebootflag = FALSE; /* exported to kdintr */
138
139 #ifdef LINUX_DEV
140 extern void linux_init(void);
141 #endif
142
143 /*
144 * Find devices. The system is alive.
145 */
146 void machine_init(void)
147 {
148 /*
149 * Make more free memory.
150 *
151 * This is particularly important for the Linux drivers which
152 * require available DMA memory.
153 */
154 biosmem_free_usable();
155
156 /*
157 * Set up to use floating point.
158 */
159 init_fpu();
160
161 #ifdef MACH_HYP
162 hyp_init();
163 #else /* MACH_HYP */
164 #if defined(APIC)
165 int err;
166
167 err = acpi_apic_init();
168 if (err) {
169 printf("acpi_apic_init failed with %d\n", err);
170 for (;;);
171 }
172 #endif
173 #if (NCPUS > 1)
174 smp_init();
175 #endif
176 init_irqs();
177 #if defined(APIC)
178 ioapic_configure();
179 #endif
180 clkstart();
181
182 /*
183 * Initialize the console.
184 */
185 cninit();
186
187 #ifdef LINUX_DEV
188 /*
189 * Initialize Linux drivers.
190 */
191 linux_init();
192 #endif
193 /*
194 * Find the devices
195 */
196 probeio();
197 #endif /* MACH_HYP */
198
199 /*
200 * Get the time
201 */
202 inittodr();
203
204 #ifndef MACH_HYP
205 /*
206 * Tell the BIOS not to clear and test memory.
207 */
208 *(unsigned short *)phystokv(0x472) = 0x1234;
209 #endif /* MACH_HYP */
210
211 #if VM_MIN_KERNEL_ADDRESS == 0
212 /*
213 * Unmap page 0 to trap NULL references.
214 *
215 * Note that this breaks accessing some BIOS areas stored there.
216 */
217 pmap_unmap_page_zero();
218 #endif
219
220 #ifndef MACH_XEN
221 #if NCPUS > 1
222 /*
223 * Patch the realmode gdt with the correct offset and the first jmp to
224 * protected mode with the correct target.
225 */
226 #ifdef __i386__
227 gdt_descr_tmp.linear_base += apboot_addr;
228 apboot_jmp_offset += apboot_addr;
229 #endif
230 #ifdef __x86_64__
231 /* Section .boot.text is located at a 32 bit offset.
232 * To access it here, we need to add KERNEL_MAP_BASE to pointers. */
233 *(uint32_t *)phystokv(&gdt_descr_tmp.linear_base) += apboot_addr;
234 *(uint32_t *)phystokv(&apboot_jmp_offset) += apboot_addr;
235 #endif
236 #endif
237 #endif
238
239 #ifdef APIC
240 /*
241 * Initialize the HPET
242 */
243 hpet_init();
244 #endif
245 }
246
247 /* Conserve power on processor CPU. */
248 void machine_idle (int cpu)
249 {
250 #ifdef MACH_HYP
251 hyp_idle();
252 #else /* MACH_HYP */
253 assert (cpu == cpu_number ());
254 asm volatile ("hlt" : : : "memory");
255 #endif /* MACH_HYP */
256 }
257
258 void machine_relax (void)
259 {
260 asm volatile ("rep; nop" : : : "memory");
261 }
262
263 /*
264 * Halt a cpu.
265 */
266 void halt_cpu(void)
267 {
268 #ifdef MACH_HYP
269 hyp_halt();
270 #else /* MACH_HYP */
271 asm volatile("cli");
272 while (TRUE)
273 machine_idle (cpu_number ());
274 #endif /* MACH_HYP */
275 }
276
277 /*
278 * Halt the system or reboot.
279 */
280 void halt_all_cpus(boolean_t reboot)
281 {
282 if (reboot) {
283 #ifdef MACH_HYP
284 hyp_reboot();
285 #endif /* MACH_HYP */
286 kdreboot();
287 }
288 else {
289 rebootflag = TRUE;
290 #ifdef MACH_HYP
291 hyp_halt();
292 #endif /* MACH_HYP */
293 printf("Shutdown completed successfully, now in tight loop.\n");
294 printf("You can safely power off the system or hit ctl-alt-del to reboot\n");
295 (void) spl0();
296 }
297 while (TRUE)
298 machine_idle (cpu_number ());
299 }
300
301 void db_halt_cpu(void)
302 {
303 halt_all_cpus(0);
304 }
305
306 void db_reset_cpu(void)
307 {
308 halt_all_cpus(1);
309 }
310
311 #ifndef MACH_HYP
312
313 static void
314 register_boot_data(const struct multiboot_raw_info *mbi)
315 {
316 struct multiboot_raw_module *mod;
317 struct elf_shdr *shdr;
318 unsigned long tmp;
319 unsigned int i;
320
321 extern char _start[], _end[];
322
323 biosmem_register_boot_data(_kvtophys(&_start), _kvtophys(&_end), FALSE);
324
325 /* cmdline and modules are moved to a safe place by i386at_init. */
326
327 if ((mbi->flags & MULTIBOOT_LOADER_CMDLINE) && (mbi->cmdline != 0)) {
328 biosmem_register_boot_data(mbi->cmdline,
329 mbi->cmdline
330 + strlen((void *)phystokv(mbi->cmdline)) + 1, TRUE);
331 }
332
333 if (mbi->flags & MULTIBOOT_LOADER_MODULES && mbi->mods_count) {
334 i = mbi->mods_count * sizeof(struct multiboot_raw_module);
335 biosmem_register_boot_data(mbi->mods_addr, mbi->mods_addr + i, TRUE);
336
337 tmp = phystokv(mbi->mods_addr);
338
339 for (i = 0; i < mbi->mods_count; i++) {
340 mod = (struct multiboot_raw_module *)tmp + i;
341 if (mod->mod_end != mod->mod_start)
342 biosmem_register_boot_data(mod->mod_start, mod->mod_end, TRUE);
343
344 if (mod->string != 0) {
345 biosmem_register_boot_data(mod->string,
346 mod->string
347 + strlen((void *)phystokv(mod->string)) + 1,
348 TRUE);
349 }
350 }
351 }
352
353 if (mbi->flags & MULTIBOOT_LOADER_SHDR) {
354 tmp = mbi->shdr_num * mbi->shdr_size;
355 if (tmp != 0)
356 biosmem_register_boot_data(mbi->shdr_addr, mbi->shdr_addr + tmp, FALSE);
357
358 tmp = phystokv(mbi->shdr_addr);
359
360 for (i = 0; i < mbi->shdr_num; i++) {
361 shdr = (struct elf_shdr *)(tmp + (i * mbi->shdr_size));
362
363 if ((shdr->type != ELF_SHT_SYMTAB)
364 && (shdr->type != ELF_SHT_STRTAB))
365 continue;
366
367 if (shdr->size != 0)
368 biosmem_register_boot_data(shdr->addr, shdr->addr + shdr->size, FALSE);
369 }
370 }
371
372 mbinfo_register_boot_data(mbi);
373 }
374
375 #endif /* MACH_HYP */
376
377 /*
378 * Basic PC VM initialization.
379 * Turns on paging and changes the kernel segments to use high linear addresses.
380 */
381 static void
382 i386at_init(void)
383 {
384 /*
385 * Initialize the PIC prior to any possible call to an spl.
386 */
387 #ifndef MACH_HYP
388 # ifdef APIC
389 picdisable();
390 # else
391 picinit();
392 # endif
393 #else /* MACH_HYP */
394 hyp_intrinit();
395 #endif /* MACH_HYP */
396
397 /*
398 * Read memory map and load it into the physical page allocator.
399 */
400 #ifdef MACH_HYP
401 biosmem_xen_bootstrap();
402 #else /* MACH_HYP */
403 register_boot_data((struct multiboot_raw_info *) &boot_info);
404 biosmem_bootstrap((struct multiboot_raw_info *) &boot_info);
405 #endif /* MACH_HYP */
406
407 #ifdef MACH_XEN
408 kernel_cmdline = (char*) boot_info.cmd_line;
409 #else /* MACH_XEN */
410 vm_offset_t addr;
411
412 /* Copy content pointed by boot_info before losing access to it when it
413 * is too far in physical memory.
414 * Also avoids leaving them in precious areas such as DMA memory. */
415 if (boot_info.flags & MULTIBOOT_CMDLINE) {
416 int len = strlen ((char*)phystokv(boot_info.cmdline)) + 1;
417 if (! init_alloc_aligned(round_page(len), &addr))
418 panic("could not allocate memory for multiboot command line");
419 kernel_cmdline = (char*) phystokv(addr);
420 memcpy(kernel_cmdline, (void *)phystokv(boot_info.cmdline), len);
421 boot_info.cmdline = addr;
422 }
423
424 if (boot_info.flags & MULTIBOOT_MODS && boot_info.mods_count) {
425 struct multiboot_raw_module *m;
426 int i;
427
428 if (! init_alloc_aligned(
429 round_page(boot_info.mods_count * sizeof(*m)), &addr))
430 panic("could not allocate memory for multiboot modules");
431 m = (void*) phystokv(addr);
432 memcpy(m, (void*) phystokv(boot_info.mods_addr), boot_info.mods_count * sizeof(*m));
433 boot_info.mods_addr = addr;
434
435 for (i = 0; i < boot_info.mods_count; i++) {
436 vm_size_t size = m[i].mod_end - m[i].mod_start;
437 if (! init_alloc_aligned(round_page(size), &addr))
438 panic("could not allocate memory for multiboot "
439 "module %d", i);
440 memcpy((void*) phystokv(addr), (void*) phystokv(m[i].mod_start), size);
441 m[i].mod_start = addr;
442 m[i].mod_end = addr + size;
443
444 size = strlen((char*) phystokv(m[i].string)) + 1;
445 if (! init_alloc_aligned(round_page(size), &addr))
446 panic("could not allocate memory for multiboot "
447 "module command line %d", i);
448 memcpy((void*) phystokv(addr), (void*) phystokv(m[i].string), size);
449 m[i].string = addr;
450 }
451 }
452 #endif /* MACH_XEN */
453
454 /*
455 * Initialize kernel physical map, mapping the
456 * region from loadpt to avail_start.
457 * Kernel virtual address starts at VM_KERNEL_MIN_ADDRESS.
458 * XXX make the BIOS page (page 0) read-only.
459 */
460 pmap_bootstrap();
461
462 /*
463 * Load physical segments into the VM system.
464 * The early allocation functions become unusable after
465 * this point.
466 */
467 biosmem_setup();
468
469 pmap_make_temporary_mapping();
470 pmap_set_page_dir();
471
472 #ifndef MACH_HYP
473 /* Turn paging on.
474 * Also set the WP bit so that on 486 or better processors
475 * page-level write protection works in kernel mode.
476 */
477 set_cr0(get_cr0() | CR0_PG | CR0_WP);
478 set_cr0(get_cr0() & ~(CR0_CD | CR0_NW));
479 if (CPU_HAS_FEATURE(CPU_FEATURE_PGE))
480 set_cr4(get_cr4() | CR4_PGE);
481 #endif /* MACH_HYP */
482 flush_instr_queue();
483 #ifdef MACH_PV_PAGETABLES
484 pmap_clear_bootstrap_pagetable((void *)boot_info.pt_base);
485 #endif /* MACH_PV_PAGETABLES */
486
487 /*
488 * Initialize and activate the real i386 protected-mode structures.
489 */
490 gdt_init();
491 idt_init();
492 #ifndef MACH_HYP
493 int_init();
494 #endif /* MACH_HYP */
495 ldt_init();
496 ktss_init();
497
498 #ifndef MACH_XEN
499 init_percpu(0);
500 #endif
501 #if NCPUS > 1
502 /* Initialize SMP structures in the master processor */
503 mp_desc_init(0);
504 #endif // NCPUS
505
506 pmap_remove_temporary_mapping();
507
508 #ifdef MACH_XEN
509 hyp_p2m_init();
510 #endif /* MACH_XEN */
511
512 interrupt_stack_alloc();
513 spl_init = 1;
514 }
515
516 /*
517 * C boot entrypoint - called by boot_entry in boothdr.S.
518 * Running in flat mode, but without paging yet.
519 */
520 void c_boot_entry(vm_offset_t bi)
521 {
522 #if ENABLE_IMMEDIATE_CONSOLE
523 romputc = immc_romputc;
524 #endif /* ENABLE_IMMEDIATE_CONSOLE */
525
526 /* Stash the boot_image_info pointer. */
527 boot_info = *(typeof(boot_info)*)phystokv(bi);
528 int cpu_type;
529
530 /* Before we do _anything_ else, print the hello message.
531 If there are no initialized console devices yet,
532 it will be stored and printed at the first opportunity. */
533 printf("%s", version);
534 printf("\n");
535
536 #ifdef MACH_XEN
537 printf("Running on %s.\n", boot_info.magic);
538 if (boot_info.flags & SIF_PRIVILEGED)
539 printf("WARNING: Mach running as dom0, this is not really supported yet.\n");
540 #ifdef MACH_PSEUDO_PHYS
541 mfn_list = (void*)boot_info.mfn_list;
542 #endif
543 #else /* MACH_XEN */
544
545 #if MACH_KDB
546 /*
547 * Locate the kernel's symbol table, if the boot loader provided it.
548 * We need to do this before i386at_init()
549 * so that the symbol table's memory won't be stomped on.
550 */
551 if ((boot_info.flags & MULTIBOOT_ELF_SHDR)
552 && boot_info.shdr_num)
553 {
554 elf_shdr_num = boot_info.shdr_num;
555 elf_shdr_size = boot_info.shdr_size;
556 elf_shdr_addr = (vm_offset_t)phystokv(boot_info.shdr_addr);
557 elf_shdr_shndx = boot_info.shdr_strndx;
558
559 printf("ELF section header table at %08" PRIxPTR "\n", elf_shdr_addr);
560 }
561 #endif /* MACH_KDB */
562 #endif /* MACH_XEN */
563
564 cpu_type = discover_x86_cpu_type ();
565
566 /*
567 * Do basic VM initialization
568 */
569 i386at_init();
570
571 #if MACH_KDB
572 /*
573 * Initialize the kernel debugger's kernel symbol table.
574 */
575 if (elf_shdr_num)
576 {
577 elf_db_sym_init(elf_shdr_num,elf_shdr_size,
578 elf_shdr_addr, elf_shdr_shndx,
579 "mach", NULL);
580 }
581 #endif /* MACH_KDB */
582
583 machine_slot[0].is_cpu = TRUE;
584 machine_slot[0].cpu_subtype = CPU_SUBTYPE_AT386;
585
586 #if defined(__x86_64__) && !defined(USER32)
587 machine_slot[0].cpu_type = CPU_TYPE_X86_64;
588 #else
589 switch (cpu_type)
590 {
591 default:
592 printf("warning: unknown cpu type %d, assuming i386\n", cpu_type);
593 case 3:
594 machine_slot[0].cpu_type = CPU_TYPE_I386;
595 break;
596 case 4:
597 machine_slot[0].cpu_type = CPU_TYPE_I486;
598 break;
599 case 5:
600 machine_slot[0].cpu_type = CPU_TYPE_PENTIUM;
601 break;
602 case 6:
603 case 15:
604 machine_slot[0].cpu_type = CPU_TYPE_PENTIUMPRO;
605 break;
606 }
607 #endif
608
609 /*
610 * Start the system.
611 */
612 setup_main();
613
614 }
615
616 #include <mach/vm_prot.h>
617 #include <vm/pmap.h>
618 #include <mach/time_value.h>
619
620 vm_offset_t
621 timemmap(dev_t dev, vm_offset_t off, vm_prot_t prot)
622 {
623 extern time_value_t *mtime;
624
625 if (prot & VM_PROT_WRITE) return (-1);
626
627 return (i386_btop(pmap_extract(pmap_kernel(), (vm_offset_t) mtime)));
628 }
629
630 void
631 startrtclock(void)
632 {
633 #ifdef APIC
634 unmask_irq(timer_pin);
635 calibrate_lapic_timer();
636 if (cpu_number() != 0) {
637 lapic_enable_timer();
638 }
639 #else
640 clkstart();
641 #ifndef MACH_HYP
642 unmask_irq(0);
643 #endif
644 #endif
645 }
646
647 void
648 inittodr(void)
649 {
650 time_value64_t new_time;
651 uint64_t newsecs;
652
653 (void) readtodc(&newsecs);
654 new_time.seconds = newsecs;
655 new_time.nanoseconds = 0;
656
657 {
658 spl_t s = splhigh();
659 time = new_time;
660 splx(s);
661 }
662 }
663
664 void
665 resettodr(void)
666 {
667 writetodc();
668 }
669
670 boolean_t
671 init_alloc_aligned(vm_size_t size, vm_offset_t *addrp)
672 {
673 *addrp = biosmem_bootalloc(vm_page_atop(vm_page_round(size)));
674
675 if (*addrp == 0)
676 return FALSE;
677
678 return TRUE;
679 }
680
681 /* Grab a physical page:
682 the standard memory allocation mechanism
683 during system initialization. */
684 vm_offset_t
685 pmap_grab_page(void)
686 {
687 vm_offset_t addr;
688 if (!init_alloc_aligned(PAGE_SIZE, &addr))
689 panic("Not enough memory to initialize Mach");
690 return addr;
691 }