ia64/xen-unstable

view xen/arch/x86/setup.c @ 8534:da7873110bbb

Tiny bootstrap cleanup.

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Mon Jan 09 19:46:46 2006 +0100 (2006-01-09)
parents 1572681e4e5a
children c055d76ec559
line source
2 #include <xen/config.h>
3 #include <xen/init.h>
4 #include <xen/lib.h>
5 #include <xen/sched.h>
6 #include <xen/domain.h>
7 #include <xen/serial.h>
8 #include <xen/softirq.h>
9 #include <xen/acpi.h>
10 #include <xen/console.h>
11 #include <xen/serial.h>
12 #include <xen/trace.h>
13 #include <xen/multiboot.h>
14 #include <xen/domain_page.h>
15 #include <xen/compile.h>
16 #include <public/version.h>
17 #include <asm/bitops.h>
18 #include <asm/smp.h>
19 #include <asm/processor.h>
20 #include <asm/mpspec.h>
21 #include <asm/apic.h>
22 #include <asm/desc.h>
23 #include <asm/shadow.h>
24 #include <asm/e820.h>
25 #include <acm/acm_hooks.h>
27 extern void dmi_scan_machine(void);
28 extern void generic_apic_probe(void);
30 /*
31 * opt_xenheap_megabytes: Size of Xen heap in megabytes, excluding the
32 * pfn_info table and allocation bitmap.
33 */
34 static unsigned int opt_xenheap_megabytes = XENHEAP_DEFAULT_MB;
35 #if defined(CONFIG_X86_64)
36 integer_param("xenheap_megabytes", opt_xenheap_megabytes);
37 #endif
39 /* opt_nosmp: If true, secondary processors are ignored. */
40 static int opt_nosmp = 0;
41 boolean_param("nosmp", opt_nosmp);
43 /* maxcpus: maximum number of CPUs to activate. */
44 static unsigned int max_cpus = NR_CPUS;
45 integer_param("maxcpus", max_cpus);
47 /* opt_watchdog: If true, run a watchdog NMI on each processor. */
48 static int opt_watchdog = 0;
49 boolean_param("watchdog", opt_watchdog);
51 /* **** Linux config option: propagated to domain0. */
52 /* "acpi=off": Sisables both ACPI table parsing and interpreter. */
53 /* "acpi=force": Override the disable blacklist. */
54 /* "acpi=strict": Disables out-of-spec workarounds. */
55 /* "acpi=ht": Limit ACPI just to boot-time to enable HT. */
56 /* "acpi=noirq": Disables ACPI interrupt routing. */
57 static void parse_acpi_param(char *s);
58 custom_param("acpi", parse_acpi_param);
60 /* **** Linux config option: propagated to domain0. */
61 /* acpi_skip_timer_override: Skip IRQ0 overrides. */
62 extern int acpi_skip_timer_override;
63 boolean_param("acpi_skip_timer_override", acpi_skip_timer_override);
65 /* **** Linux config option: propagated to domain0. */
66 /* noapic: Disable IOAPIC setup. */
67 extern int skip_ioapic_setup;
68 boolean_param("noapic", skip_ioapic_setup);
70 int early_boot = 1;
72 cpumask_t cpu_present_map;
74 /* Limits of Xen heap, used to initialise the allocator. */
75 unsigned long xenheap_phys_start, xenheap_phys_end;
77 extern void arch_init_memory(void);
78 extern void init_IRQ(void);
79 extern void trap_init(void);
80 extern void early_time_init(void);
81 extern void initialize_keytable(void);
82 extern void early_cpu_init(void);
84 struct tss_struct init_tss[NR_CPUS];
86 struct vcpu *idle_vcpu[NR_CPUS];
88 extern unsigned long cpu0_stack[];
90 struct cpuinfo_x86 boot_cpu_data = { 0, 0, 0, 0, -1, 1, 0, 0, -1 };
92 #if CONFIG_PAGING_LEVELS > 2
93 unsigned long mmu_cr4_features = X86_CR4_PSE | X86_CR4_PGE | X86_CR4_PAE;
94 #else
95 unsigned long mmu_cr4_features = X86_CR4_PSE;
96 #endif
97 EXPORT_SYMBOL(mmu_cr4_features);
99 int acpi_disabled;
101 int acpi_force;
102 char acpi_param[10] = "";
103 static void parse_acpi_param(char *s)
104 {
105 /* Save the parameter so it can be propagated to domain0. */
106 strncpy(acpi_param, s, sizeof(acpi_param));
107 acpi_param[sizeof(acpi_param)-1] = '\0';
109 /* Interpret the parameter for use within Xen. */
110 if ( !strcmp(s, "off") )
111 {
112 disable_acpi();
113 }
114 else if ( !strcmp(s, "force") )
115 {
116 acpi_force = 1;
117 acpi_ht = 1;
118 acpi_disabled = 0;
119 }
120 else if ( !strcmp(s, "strict") )
121 {
122 acpi_strict = 1;
123 }
124 else if ( !strcmp(s, "ht") )
125 {
126 if ( !acpi_force )
127 disable_acpi();
128 acpi_ht = 1;
129 }
130 else if ( !strcmp(s, "noirq") )
131 {
132 acpi_noirq_set();
133 }
134 }
136 static void __init do_initcalls(void)
137 {
138 initcall_t *call;
139 for ( call = &__initcall_start; call < &__initcall_end; call++ )
140 (*call)();
141 }
143 #define EARLY_FAIL() for ( ; ; ) __asm__ __volatile__ ( "hlt" )
145 static struct e820entry e820_raw[E820MAX];
147 void __init __start_xen(multiboot_info_t *mbi)
148 {
149 char *cmdline;
150 struct domain *idle_domain;
151 unsigned long _initrd_start = 0, _initrd_len = 0;
152 unsigned int initrdidx = 1;
153 module_t *mod = (module_t *)__va(mbi->mods_addr);
154 unsigned long nr_pages, modules_length;
155 unsigned long initial_images_start, initial_images_end;
156 physaddr_t s, e;
157 int i, e820_warn = 0, e820_raw_nr = 0, bytes = 0;
158 struct ns16550_defaults ns16550 = {
159 .data_bits = 8,
160 .parity = 'n',
161 .stop_bits = 1
162 };
164 /* Parse the command-line options. */
165 if ( (mbi->flags & MBI_CMDLINE) && (mbi->cmdline != 0) )
166 cmdline_parse(__va(mbi->cmdline));
168 set_current((struct vcpu *)0xfffff000); /* debug sanity */
169 set_processor_id(0); /* needed early, for smp_processor_id() */
171 smp_prepare_boot_cpu();
173 /* We initialise the serial devices very early so we can get debugging. */
174 ns16550.io_base = 0x3f8;
175 ns16550.irq = 4;
176 ns16550_init(0, &ns16550);
177 ns16550.io_base = 0x2f8;
178 ns16550.irq = 3;
179 ns16550_init(1, &ns16550);
180 serial_init_preirq();
182 init_console();
184 /* Check that we have at least one Multiboot module. */
185 if ( !(mbi->flags & MBI_MODULES) || (mbi->mods_count == 0) )
186 {
187 printk("FATAL ERROR: dom0 kernel not specified."
188 " Check bootloader configuration.\n");
189 EARLY_FAIL();
190 }
192 if ( ((unsigned long)cpu0_stack & (STACK_SIZE-1)) != 0 )
193 {
194 printk("FATAL ERROR: Misaligned CPU0 stack.\n");
195 EARLY_FAIL();
196 }
198 xenheap_phys_end = opt_xenheap_megabytes << 20;
200 if ( mbi->flags & MBI_MEMMAP )
201 {
202 while ( bytes < mbi->mmap_length )
203 {
204 memory_map_t *map = __va(mbi->mmap_addr + bytes);
206 /*
207 * This is a gross workaround for a BIOS bug. Some bootloaders do
208 * not write e820 map entries into pre-zeroed memory. This is
209 * okay if the BIOS fills in all fields of the map entry, but
210 * some broken BIOSes do not bother to write the high word of
211 * the length field if the length is smaller than 4GB. We
212 * detect and fix this by flagging sections below 4GB that
213 * appear to be larger than 4GB in size.
214 */
215 if ( (map->base_addr_high == 0) && (map->length_high != 0) )
216 {
217 e820_warn = 1;
218 map->length_high = 0;
219 }
221 e820_raw[e820_raw_nr].addr =
222 ((u64)map->base_addr_high << 32) | (u64)map->base_addr_low;
223 e820_raw[e820_raw_nr].size =
224 ((u64)map->length_high << 32) | (u64)map->length_low;
225 e820_raw[e820_raw_nr].type =
226 (map->type > E820_SHARED_PAGE) ? E820_RESERVED : map->type;
227 e820_raw_nr++;
229 bytes += map->size + 4;
230 }
231 }
232 else if ( mbi->flags & MBI_MEMLIMITS )
233 {
234 e820_raw[0].addr = 0;
235 e820_raw[0].size = mbi->mem_lower << 10;
236 e820_raw[0].type = E820_RAM;
237 e820_raw[1].addr = 0x100000;
238 e820_raw[1].size = mbi->mem_upper << 10;
239 e820_raw[1].type = E820_RAM;
240 e820_raw_nr = 2;
241 }
242 else
243 {
244 printk("FATAL ERROR: Bootloader provided no memory information.\n");
245 for ( ; ; ) ;
246 }
248 if ( e820_warn )
249 printk("WARNING: Buggy e820 map detected and fixed "
250 "(truncated length fields).\n");
252 max_page = init_e820(e820_raw, &e820_raw_nr);
254 modules_length = mod[mbi->mods_count-1].mod_end - mod[0].mod_start;
256 /* Find a large enough RAM extent to stash the DOM0 modules. */
257 for ( i = 0; ; i++ )
258 {
259 if ( i == e820.nr_map )
260 {
261 printk("Not enough memory to stash the DOM0 kernel image.\n");
262 for ( ; ; ) ;
263 }
265 if ( (e820.map[i].type == E820_RAM) &&
266 (e820.map[i].size >= modules_length) &&
267 ((e820.map[i].addr + e820.map[i].size) >=
268 (xenheap_phys_end + modules_length)) )
269 break;
270 }
272 /* Stash as near as possible to the beginning of the RAM extent. */
273 initial_images_start = e820.map[i].addr;
274 if ( initial_images_start < xenheap_phys_end )
275 initial_images_start = xenheap_phys_end;
276 initial_images_end = initial_images_start + modules_length;
278 #if defined(CONFIG_X86_32)
279 memmove((void *)initial_images_start, /* use low mapping */
280 (void *)mod[0].mod_start, /* use low mapping */
281 mod[mbi->mods_count-1].mod_end - mod[0].mod_start);
282 #elif defined(CONFIG_X86_64)
283 memmove(__va(initial_images_start),
284 __va(mod[0].mod_start),
285 mod[mbi->mods_count-1].mod_end - mod[0].mod_start);
286 #endif
288 /* Initialise boot-time allocator with all RAM situated after modules. */
289 xenheap_phys_start = init_boot_allocator(__pa(&_end));
290 nr_pages = 0;
291 for ( i = 0; i < e820.nr_map; i++ )
292 {
293 if ( e820.map[i].type != E820_RAM )
294 continue;
296 nr_pages += e820.map[i].size >> PAGE_SHIFT;
298 /* Initialise boot heap, skipping Xen heap and dom0 modules. */
299 s = e820.map[i].addr;
300 e = s + e820.map[i].size;
301 if ( s < xenheap_phys_end )
302 s = xenheap_phys_end;
303 if ( (s < initial_images_end) && (e > initial_images_start) )
304 s = initial_images_end;
305 init_boot_pages(s, e);
307 #if defined (CONFIG_X86_64)
308 /*
309 * x86/64 maps all registered RAM. Points to note:
310 * 1. The initial pagetable already maps low 64MB, so skip that.
311 * 2. We must map *only* RAM areas, taking care to avoid I/O holes.
312 * Failure to do this can cause coherency problems and deadlocks
313 * due to cache-attribute mismatches (e.g., AMD/AGP Linux bug).
314 */
315 {
316 /* Calculate page-frame range, discarding partial frames. */
317 unsigned long start, end;
318 start = PFN_UP(e820.map[i].addr);
319 end = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
320 /* Clip the range to above 64MB. */
321 if ( end < (64UL << (20-PAGE_SHIFT)) )
322 continue;
323 if ( start < (64UL << (20-PAGE_SHIFT)) )
324 start = 64UL << (20-PAGE_SHIFT);
325 /* Request the mapping. */
326 map_pages_to_xen(
327 PAGE_OFFSET + (start << PAGE_SHIFT),
328 start, end-start, PAGE_HYPERVISOR);
329 }
330 #endif
331 }
333 memguard_init();
335 printk("System RAM: %luMB (%lukB)\n",
336 nr_pages >> (20 - PAGE_SHIFT),
337 nr_pages << (PAGE_SHIFT - 10));
338 total_pages = nr_pages;
340 /* Sanity check for unwanted bloat of dom0_op_t structure. */
341 BUG_ON(sizeof(((dom0_op_t *)0)->u) != sizeof(((dom0_op_t *)0)->u.pad));
343 BUG_ON(sizeof(start_info_t) > PAGE_SIZE);
344 BUG_ON(sizeof(shared_info_t) > PAGE_SIZE);
345 BUG_ON(sizeof(vcpu_info_t) != 64);
347 /* __foo are defined in public headers. Check they match internal defs. */
348 BUG_ON(__HYPERVISOR_VIRT_START != HYPERVISOR_VIRT_START);
349 #ifdef HYPERVISOR_VIRT_END
350 BUG_ON(__HYPERVISOR_VIRT_END != HYPERVISOR_VIRT_END);
351 #endif
353 init_frametable();
355 end_boot_allocator();
357 /* Initialise the Xen heap, skipping RAM holes. */
358 nr_pages = 0;
359 for ( i = 0; i < e820.nr_map; i++ )
360 {
361 if ( e820.map[i].type != E820_RAM )
362 continue;
364 s = e820.map[i].addr;
365 e = s + e820.map[i].size;
366 if ( s < xenheap_phys_start )
367 s = xenheap_phys_start;
368 if ( e > xenheap_phys_end )
369 e = xenheap_phys_end;
371 if ( s < e )
372 {
373 nr_pages += (e - s) >> PAGE_SHIFT;
374 init_xenheap_pages(s, e);
375 }
376 }
378 printk("Xen heap: %luMB (%lukB)\n",
379 nr_pages >> (20 - PAGE_SHIFT),
380 nr_pages << (PAGE_SHIFT - 10));
382 early_boot = 0;
384 early_cpu_init();
386 scheduler_init();
388 idle_domain = do_createdomain(IDLE_DOMAIN_ID, 0);
389 BUG_ON(idle_domain == NULL);
391 set_current(idle_domain->vcpu[0]);
392 idle_vcpu[0] = current;
394 paging_init();
396 /* Unmap the first page of CPU0's stack. */
397 memguard_guard_stack(cpu0_stack);
399 open_softirq(NEW_TLBFLUSH_CLOCK_PERIOD_SOFTIRQ, new_tlbflush_clock_period);
401 if ( opt_watchdog )
402 nmi_watchdog = NMI_LOCAL_APIC;
404 sort_exception_tables();
406 find_smp_config();
408 smp_alloc_memory();
410 dmi_scan_machine();
412 generic_apic_probe();
414 acpi_boot_table_init();
415 acpi_boot_init();
417 if ( smp_found_config )
418 get_smp_config();
420 init_apic_mappings();
422 init_IRQ();
424 trap_init();
426 ac_timer_init();
428 early_time_init();
430 arch_init_memory();
432 identify_cpu(&boot_cpu_data);
433 if ( cpu_has_fxsr )
434 set_in_cr4(X86_CR4_OSFXSR);
435 if ( cpu_has_xmm )
436 set_in_cr4(X86_CR4_OSXMMEXCPT);
438 if ( opt_nosmp )
439 {
440 max_cpus = 0;
441 smp_num_siblings = 1;
442 boot_cpu_data.x86_num_cores = 1;
443 }
445 smp_prepare_cpus(max_cpus);
447 /* We aren't hotplug-capable yet. */
448 BUG_ON(!cpus_empty(cpu_present_map));
449 for_each_cpu ( i )
450 cpu_set(i, cpu_present_map);
452 /*
453 * Initialise higher-level timer functions. We do this fairly late
454 * (post-SMP) because the time bases and scale factors need to be updated
455 * regularly, and SMP initialisation can cause a long delay with
456 * interrupts not yet enabled.
457 */
458 init_xen_time();
460 initialize_keytable();
462 serial_init_postirq();
464 BUG_ON(!local_irq_is_enabled());
466 for_each_present_cpu ( i )
467 {
468 if ( num_online_cpus() >= max_cpus )
469 break;
470 if ( !cpu_online(i) )
471 __cpu_up(i);
472 }
474 printk("Brought up %ld CPUs\n", (long)num_online_cpus());
475 smp_cpus_done(max_cpus);
477 do_initcalls();
479 schedulers_start();
481 watchdog_enable();
483 shadow_mode_init();
485 /* initialize access control security module */
486 acm_init(&initrdidx, mbi, initial_images_start);
488 /* Create initial domain 0. */
489 dom0 = do_createdomain(0, 0);
490 if ( dom0 == NULL )
491 panic("Error creating domain 0\n");
493 set_bit(_DOMF_privileged, &dom0->domain_flags);
494 /* post-create hooks sets security label */
495 acm_post_domain0_create(dom0->domain_id);
497 /* Grab the DOM0 command line. */
498 cmdline = (char *)(mod[0].string ? __va(mod[0].string) : NULL);
499 if ( cmdline != NULL )
500 {
501 static char dom0_cmdline[MAX_GUEST_CMDLINE];
503 /* Skip past the image name and copy to a local buffer. */
504 while ( *cmdline == ' ' ) cmdline++;
505 if ( (cmdline = strchr(cmdline, ' ')) != NULL )
506 {
507 while ( *cmdline == ' ' ) cmdline++;
508 strcpy(dom0_cmdline, cmdline);
509 }
511 cmdline = dom0_cmdline;
513 /* Append any extra parameters. */
514 if ( skip_ioapic_setup && !strstr(cmdline, "noapic") )
515 strcat(cmdline, " noapic");
516 if ( acpi_skip_timer_override &&
517 !strstr(cmdline, "acpi_skip_timer_override") )
518 strcat(cmdline, " acpi_skip_timer_override");
519 if ( (strlen(acpi_param) != 0) && !strstr(cmdline, "acpi=") )
520 {
521 strcat(cmdline, " acpi=");
522 strcat(cmdline, acpi_param);
523 }
524 }
526 if ( (initrdidx > 0) && (initrdidx < mbi->mods_count) )
527 {
528 _initrd_start = initial_images_start +
529 (mod[initrdidx].mod_start - mod[0].mod_start);
530 _initrd_len = mod[initrdidx].mod_end - mod[initrdidx].mod_start;
531 }
533 /*
534 * We're going to setup domain0 using the module(s) that we stashed safely
535 * above our heap. The second module, if present, is an initrd ramdisk.
536 */
537 if ( construct_dom0(dom0,
538 initial_images_start,
539 mod[0].mod_end-mod[0].mod_start,
540 _initrd_start,
541 _initrd_len,
542 cmdline) != 0)
543 panic("Could not set up DOM0 guest OS\n");
545 /* Scrub RAM that is still free and so may go to an unprivileged domain. */
546 scrub_heap_pages();
548 init_trace_bufs();
550 /* Give up the VGA console if DOM0 is configured to grab it. */
551 console_endboot(cmdline && strstr(cmdline, "tty0"));
553 /* Hide UART from DOM0 if we're using it */
554 serial_endboot();
556 domain_unpause_by_systemcontroller(dom0);
558 startup_cpu_idle_loop();
559 }
561 void arch_get_xen_caps(xen_capabilities_info_t info)
562 {
563 char *p = info;
565 #if defined(CONFIG_X86_32) && !defined(CONFIG_X86_PAE)
567 p += sprintf(p, "xen-%d.%d-x86_32 ", XEN_VERSION, XEN_SUBVERSION);
568 if ( hvm_enabled )
569 p += sprintf(p, "hvm-%d.%d-x86_32 ", XEN_VERSION, XEN_SUBVERSION);
571 #elif defined(CONFIG_X86_32) && defined(CONFIG_X86_PAE)
573 p += sprintf(p, "xen-%d.%d-x86_32p ", XEN_VERSION, XEN_SUBVERSION);
574 if ( hvm_enabled )
575 {
576 //p += sprintf(p, "hvm-%d.%d-x86_32 ", XEN_VERSION, XEN_SUBVERSION);
577 //p += sprintf(p, "hvm-%d.%d-x86_32p ", XEN_VERSION, XEN_SUBVERSION);
578 }
580 #elif defined(CONFIG_X86_64)
582 p += sprintf(p, "xen-%d.%d-x86_64 ", XEN_VERSION, XEN_SUBVERSION);
583 if ( hvm_enabled )
584 {
585 p += sprintf(p, "hvm-%d.%d-x86_32 ", XEN_VERSION, XEN_SUBVERSION);
586 //p += sprintf(p, "hvm-%d.%d-x86_32p ", XEN_VERSION, XEN_SUBVERSION);
587 p += sprintf(p, "hvm-%d.%d-x86_64 ", XEN_VERSION, XEN_SUBVERSION);
588 }
590 #else
592 p++;
594 #endif
596 *(p-1) = 0;
598 BUG_ON((p - info) > sizeof(xen_capabilities_info_t));
599 }
601 /*
602 * Local variables:
603 * mode: C
604 * c-set-style: "BSD"
605 * c-basic-offset: 4
606 * tab-width: 4
607 * indent-tabs-mode: nil
608 * End:
609 */