ia64/xen-unstable

view xen/arch/x86/setup.c @ 18719:874d0d673ecb

x86: Enable spinlock debugging earlier during boot.
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Oct 23 15:38:52 2008 +0100 (2008-10-23)
parents 0358305c6883
children 8e18dd41c6c7
line source
1 #include <xen/config.h>
2 #include <xen/init.h>
3 #include <xen/lib.h>
4 #include <xen/sched.h>
5 #include <xen/domain.h>
6 #include <xen/serial.h>
7 #include <xen/softirq.h>
8 #include <xen/acpi.h>
9 #include <xen/console.h>
10 #include <xen/serial.h>
11 #include <xen/trace.h>
12 #include <xen/multiboot.h>
13 #include <xen/domain_page.h>
14 #include <xen/version.h>
15 #include <xen/gdbstub.h>
16 #include <xen/percpu.h>
17 #include <xen/hypercall.h>
18 #include <xen/keyhandler.h>
19 #include <xen/numa.h>
20 #include <xen/rcupdate.h>
21 #include <xen/vga.h>
22 #include <xen/dmi.h>
23 #include <public/version.h>
24 #ifdef CONFIG_COMPAT
25 #include <compat/platform.h>
26 #include <compat/xen.h>
27 #endif
28 #include <asm/bitops.h>
29 #include <asm/smp.h>
30 #include <asm/processor.h>
31 #include <asm/mpspec.h>
32 #include <asm/apic.h>
33 #include <asm/desc.h>
34 #include <asm/paging.h>
35 #include <asm/e820.h>
36 #include <xsm/acm/acm_hooks.h>
37 #include <xen/kexec.h>
38 #include <asm/edd.h>
39 #include <xsm/xsm.h>
40 #include <asm/tboot.h>
42 #if defined(CONFIG_X86_64)
43 #define BOOTSTRAP_DIRECTMAP_END (1UL << 32) /* 4GB */
44 #define maddr_to_bootstrap_virt(m) maddr_to_virt(m)
45 #else
46 #define BOOTSTRAP_DIRECTMAP_END (1UL << 30) /* 1GB */
47 #define maddr_to_bootstrap_virt(m) ((void *)(long)(m))
48 #endif
50 extern void generic_apic_probe(void);
51 extern void numa_initmem_init(unsigned long start_pfn, unsigned long end_pfn);
53 extern u16 boot_edid_caps;
54 extern u8 boot_edid_info[128];
55 extern struct boot_video_info boot_vid_info;
57 /*
58 * opt_xenheap_megabytes: Size of Xen heap in megabytes, excluding the
59 * page_info table and allocation bitmap.
60 */
61 static unsigned int opt_xenheap_megabytes = XENHEAP_DEFAULT_MB;
62 #if defined(CONFIG_X86_64)
63 integer_param("xenheap_megabytes", opt_xenheap_megabytes);
64 #endif
66 /* opt_nosmp: If true, secondary processors are ignored. */
67 static int opt_nosmp = 0;
68 boolean_param("nosmp", opt_nosmp);
70 /* maxcpus: maximum number of CPUs to activate. */
71 static unsigned int max_cpus = NR_CPUS;
72 integer_param("maxcpus", max_cpus);
74 /* opt_watchdog: If true, run a watchdog NMI on each processor. */
75 static int opt_watchdog = 0;
76 boolean_param("watchdog", opt_watchdog);
78 /* **** Linux config option: propagated to domain0. */
79 /* "acpi=off": Sisables both ACPI table parsing and interpreter. */
80 /* "acpi=force": Override the disable blacklist. */
81 /* "acpi=strict": Disables out-of-spec workarounds. */
82 /* "acpi=ht": Limit ACPI just to boot-time to enable HT. */
83 /* "acpi=noirq": Disables ACPI interrupt routing. */
84 static void parse_acpi_param(char *s);
85 custom_param("acpi", parse_acpi_param);
87 /* **** Linux config option: propagated to domain0. */
88 /* acpi_skip_timer_override: Skip IRQ0 overrides. */
89 extern int acpi_skip_timer_override;
90 boolean_param("acpi_skip_timer_override", acpi_skip_timer_override);
92 /* **** Linux config option: propagated to domain0. */
93 /* noapic: Disable IOAPIC setup. */
94 extern int skip_ioapic_setup;
95 boolean_param("noapic", skip_ioapic_setup);
97 /* **** Linux config option: propagated to domain0. */
98 /* xen_cpuidle: xen control cstate. */
99 /*static*/ int xen_cpuidle;
100 boolean_param("cpuidle", xen_cpuidle);
102 int early_boot = 1;
104 cpumask_t cpu_present_map;
106 unsigned long xen_phys_start;
108 /* Limits of Xen heap, used to initialise the allocator. */
109 unsigned long xenheap_phys_start, xenheap_phys_end;
111 extern void arch_init_memory(void);
112 extern void init_IRQ(void);
113 extern void early_time_init(void);
114 extern void early_cpu_init(void);
115 extern void vesa_init(void);
116 extern void vesa_mtrr_init(void);
118 DEFINE_PER_CPU(struct desc_struct *, gdt_table) = boot_cpu_gdt_table;
119 #ifdef CONFIG_COMPAT
120 DEFINE_PER_CPU(struct desc_struct *, compat_gdt_table)
121 = boot_cpu_compat_gdt_table;
122 #endif
124 struct tss_struct init_tss[NR_CPUS];
126 char __attribute__ ((__section__(".bss.stack_aligned"))) cpu0_stack[STACK_SIZE];
128 struct cpuinfo_x86 boot_cpu_data = { 0, 0, 0, 0, -1 };
130 unsigned long mmu_cr4_features = X86_CR4_PSE | X86_CR4_PGE | X86_CR4_PAE;
131 EXPORT_SYMBOL(mmu_cr4_features);
133 int acpi_disabled;
135 int acpi_force;
136 char acpi_param[10] = "";
137 static void __init parse_acpi_param(char *s)
138 {
139 /* Save the parameter so it can be propagated to domain0. */
140 safe_strcpy(acpi_param, s);
142 /* Interpret the parameter for use within Xen. */
143 if ( !strcmp(s, "off") )
144 {
145 disable_acpi();
146 }
147 else if ( !strcmp(s, "force") )
148 {
149 acpi_force = 1;
150 acpi_ht = 1;
151 acpi_disabled = 0;
152 }
153 else if ( !strcmp(s, "strict") )
154 {
155 acpi_strict = 1;
156 }
157 else if ( !strcmp(s, "ht") )
158 {
159 if ( !acpi_force )
160 disable_acpi();
161 acpi_ht = 1;
162 }
163 else if ( !strcmp(s, "noirq") )
164 {
165 acpi_noirq_set();
166 }
167 }
169 static void __init do_initcalls(void)
170 {
171 initcall_t *call;
172 for ( call = &__initcall_start; call < &__initcall_end; call++ )
173 (*call)();
174 }
176 #define EARLY_FAIL(f, a...) do { \
177 printk( f , ## a ); \
178 for ( ; ; ) halt(); \
179 } while (0)
181 static unsigned long __initdata initial_images_start, initial_images_end;
183 unsigned long __init initial_images_nrpages(void)
184 {
185 ASSERT(!(initial_images_start & ~PAGE_MASK));
186 ASSERT(!(initial_images_end & ~PAGE_MASK));
187 return ((initial_images_end >> PAGE_SHIFT) -
188 (initial_images_start >> PAGE_SHIFT));
189 }
191 void __init discard_initial_images(void)
192 {
193 init_domheap_pages(initial_images_start, initial_images_end);
194 }
196 extern char __per_cpu_start[], __per_cpu_data_end[], __per_cpu_end[];
198 static void __init percpu_init_areas(void)
199 {
200 unsigned int i, data_size = __per_cpu_data_end - __per_cpu_start;
201 unsigned int first_unused;
203 BUG_ON(data_size > PERCPU_SIZE);
205 /* Initialise per-cpu data area for all possible secondary CPUs. */
206 for ( i = 1; (i < NR_CPUS) && cpu_possible(i); i++ )
207 memcpy(__per_cpu_start + (i << PERCPU_SHIFT),
208 __per_cpu_start,
209 data_size);
210 first_unused = i;
212 /* Check that there are no holes in cpu_possible_map. */
213 for ( ; i < NR_CPUS; i++ )
214 BUG_ON(cpu_possible(i));
216 #ifndef MEMORY_GUARD
217 init_xenheap_pages(__pa(__per_cpu_start) + (first_unused << PERCPU_SHIFT),
218 __pa(__per_cpu_end));
219 #endif
220 memguard_guard_range(&__per_cpu_start[first_unused << PERCPU_SHIFT],
221 (NR_CPUS - first_unused) << PERCPU_SHIFT);
222 #if defined(CONFIG_X86_64)
223 /* Also zap the mapping in the 1:1 area. */
224 memguard_guard_range(__va(__pa(__per_cpu_start)) +
225 (first_unused << PERCPU_SHIFT),
226 (NR_CPUS - first_unused) << PERCPU_SHIFT);
227 #endif
228 }
230 static void __init init_idle_domain(void)
231 {
232 struct domain *idle_domain;
233 unsigned int i;
235 /* Domain creation requires that scheduler structures are initialised. */
236 scheduler_init();
238 idle_domain = domain_create(IDLE_DOMAIN_ID, 0, 0);
239 if ( (idle_domain == NULL) || (alloc_vcpu(idle_domain, 0, 0) == NULL) )
240 BUG();
242 set_current(idle_domain->vcpu[0]);
243 idle_vcpu[0] = this_cpu(curr_vcpu) = current;
245 setup_idle_pagetable();
247 for (i = 0; i < NR_RESERVED_GDT_PAGES; ++i)
248 idle_domain->arch.mm_perdomain_pt[FIRST_RESERVED_GDT_PAGE + i] =
249 l1e_from_page(virt_to_page(boot_cpu_gdt_table) + i,
250 __PAGE_HYPERVISOR);
252 }
254 static void __init srat_detect_node(int cpu)
255 {
256 unsigned node;
257 u32 apicid = x86_cpu_to_apicid[cpu];
259 node = apicid_to_node[apicid];
260 if ( node == NUMA_NO_NODE )
261 node = 0;
262 numa_set_node(cpu, node);
264 if ( acpi_numa > 0 )
265 printk(KERN_INFO "CPU %d APIC %d -> Node %d\n", cpu, apicid, node);
266 }
268 /*
269 * Ensure a given physical memory range is present in the bootstrap mappings.
270 * Use superpage mappings to ensure that pagetable memory needn't be allocated.
271 */
272 static void __init bootstrap_map(unsigned long start, unsigned long end)
273 {
274 unsigned long mask = (1UL << L2_PAGETABLE_SHIFT) - 1;
275 start = max_t(unsigned long, start & ~mask, 16UL << 20);
276 end = (end + mask) & ~mask;
277 if ( start >= end )
278 return;
279 if ( end > BOOTSTRAP_DIRECTMAP_END )
280 panic("Cannot access memory beyond end of "
281 "bootstrap direct-map area\n");
282 map_pages_to_xen(
283 (unsigned long)maddr_to_bootstrap_virt(start),
284 start >> PAGE_SHIFT, (end-start) >> PAGE_SHIFT, PAGE_HYPERVISOR);
285 }
287 static void __init move_memory(
288 unsigned long dst, unsigned long src_start, unsigned long src_end)
289 {
290 bootstrap_map(src_start, src_end);
291 bootstrap_map(dst, dst + src_end - src_start);
292 memmove(maddr_to_bootstrap_virt(dst),
293 maddr_to_bootstrap_virt(src_start),
294 src_end - src_start);
295 }
297 /* A temporary copy of the e820 map that we can mess with during bootstrap. */
298 static struct e820map __initdata boot_e820;
300 struct boot_video_info {
301 u8 orig_x; /* 0x00 */
302 u8 orig_y; /* 0x01 */
303 u8 orig_video_mode; /* 0x02 */
304 u8 orig_video_cols; /* 0x03 */
305 u8 orig_video_lines; /* 0x04 */
306 u8 orig_video_isVGA; /* 0x05 */
307 u16 orig_video_points; /* 0x06 */
309 /* VESA graphic mode -- linear frame buffer */
310 u32 capabilities; /* 0x08 */
311 u16 lfb_linelength; /* 0x0c */
312 u16 lfb_width; /* 0x0e */
313 u16 lfb_height; /* 0x10 */
314 u16 lfb_depth; /* 0x12 */
315 u32 lfb_base; /* 0x14 */
316 u32 lfb_size; /* 0x18 */
317 u8 red_size; /* 0x1c */
318 u8 red_pos; /* 0x1d */
319 u8 green_size; /* 0x1e */
320 u8 green_pos; /* 0x1f */
321 u8 blue_size; /* 0x20 */
322 u8 blue_pos; /* 0x21 */
323 u8 rsvd_size; /* 0x22 */
324 u8 rsvd_pos; /* 0x23 */
325 u16 vesapm_seg; /* 0x24 */
326 u16 vesapm_off; /* 0x26 */
327 u16 vesa_attrib; /* 0x28 */
328 };
330 static void __init parse_video_info(void)
331 {
332 struct boot_video_info *bvi = &bootsym(boot_vid_info);
334 if ( (bvi->orig_video_isVGA == 1) && (bvi->orig_video_mode == 3) )
335 {
336 vga_console_info.video_type = XEN_VGATYPE_TEXT_MODE_3;
337 vga_console_info.u.text_mode_3.font_height = bvi->orig_video_points;
338 vga_console_info.u.text_mode_3.cursor_x = bvi->orig_x;
339 vga_console_info.u.text_mode_3.cursor_y = bvi->orig_y;
340 vga_console_info.u.text_mode_3.rows = bvi->orig_video_lines;
341 vga_console_info.u.text_mode_3.columns = bvi->orig_video_cols;
342 }
343 else if ( bvi->orig_video_isVGA == 0x23 )
344 {
345 vga_console_info.video_type = XEN_VGATYPE_VESA_LFB;
346 vga_console_info.u.vesa_lfb.width = bvi->lfb_width;
347 vga_console_info.u.vesa_lfb.height = bvi->lfb_height;
348 vga_console_info.u.vesa_lfb.bytes_per_line = bvi->lfb_linelength;
349 vga_console_info.u.vesa_lfb.bits_per_pixel = bvi->lfb_depth;
350 vga_console_info.u.vesa_lfb.lfb_base = bvi->lfb_base;
351 vga_console_info.u.vesa_lfb.lfb_size = bvi->lfb_size;
352 vga_console_info.u.vesa_lfb.red_pos = bvi->red_pos;
353 vga_console_info.u.vesa_lfb.red_size = bvi->red_size;
354 vga_console_info.u.vesa_lfb.green_pos = bvi->green_pos;
355 vga_console_info.u.vesa_lfb.green_size = bvi->green_size;
356 vga_console_info.u.vesa_lfb.blue_pos = bvi->blue_pos;
357 vga_console_info.u.vesa_lfb.blue_size = bvi->blue_size;
358 vga_console_info.u.vesa_lfb.rsvd_pos = bvi->rsvd_pos;
359 vga_console_info.u.vesa_lfb.rsvd_size = bvi->rsvd_size;
360 vga_console_info.u.vesa_lfb.gbl_caps = bvi->capabilities;
361 vga_console_info.u.vesa_lfb.mode_attrs = bvi->vesa_attrib;
362 }
363 }
365 void __init kexec_reserve_area(struct e820map *e820)
366 {
367 unsigned long kdump_start = kexec_crash_area.start;
368 unsigned long kdump_size = kexec_crash_area.size;
369 static int is_reserved = 0;
371 kdump_size = (kdump_size + PAGE_SIZE - 1) & PAGE_MASK;
373 if ( (kdump_start == 0) || (kdump_size == 0) || is_reserved )
374 return;
376 is_reserved = 1;
378 if ( !reserve_e820_ram(e820, kdump_start, kdump_start + kdump_size) )
379 {
380 printk("Kdump: DISABLED (failed to reserve %luMB (%lukB) at 0x%lx)"
381 "\n", kdump_size >> 20, kdump_size >> 10, kdump_start);
382 kexec_crash_area.start = kexec_crash_area.size = 0;
383 }
384 else
385 {
386 printk("Kdump: %luMB (%lukB) at 0x%lx\n",
387 kdump_size >> 20, kdump_size >> 10, kdump_start);
388 }
389 }
391 void init_done(void)
392 {
393 extern char __init_begin[], __init_end[];
395 /* Free (or page-protect) the init areas. */
396 #ifndef MEMORY_GUARD
397 init_xenheap_pages(__pa(__init_begin), __pa(__init_end));
398 #endif
399 memguard_guard_range(__init_begin, __init_end - __init_begin);
400 #if defined(CONFIG_X86_64)
401 /* Also zap the mapping in the 1:1 area. */
402 memguard_guard_range(__va(__pa(__init_begin)), __init_end - __init_begin);
403 #endif
404 printk("Freed %ldkB init memory.\n", (long)(__init_end-__init_begin)>>10);
406 startup_cpu_idle_loop();
407 }
409 static char * __init cmdline_cook(char *p)
410 {
411 p = p ? : "";
412 while ( *p == ' ' )
413 p++;
414 while ( (*p != ' ') && (*p != '\0') )
415 p++;
416 while ( *p == ' ' )
417 p++;
418 return p;
419 }
421 void __init __start_xen(unsigned long mbi_p)
422 {
423 char *memmap_type = NULL;
424 char *cmdline, *kextra;
425 unsigned long _initrd_start = 0, _initrd_len = 0;
426 unsigned int initrdidx = 1;
427 multiboot_info_t *mbi = __va(mbi_p);
428 module_t *mod = (module_t *)__va(mbi->mods_addr);
429 unsigned long nr_pages, modules_length;
430 int i, e820_warn = 0, bytes = 0;
431 struct ns16550_defaults ns16550 = {
432 .data_bits = 8,
433 .parity = 'n',
434 .stop_bits = 1
435 };
437 extern void early_page_fault(void);
438 set_intr_gate(TRAP_page_fault, &early_page_fault);
440 /* Parse the command-line options. */
441 cmdline = cmdline_cook((mbi->flags & MBI_CMDLINE) ?
442 __va(mbi->cmdline) : NULL);
443 if ( (kextra = strstr(cmdline, " -- ")) != NULL )
444 {
445 /*
446 * Options after ' -- ' separator belong to dom0.
447 * 1. Orphan dom0's options from Xen's command line.
448 * 2. Skip all but final leading space from dom0's options.
449 */
450 *kextra = '\0';
451 kextra += 3;
452 while ( kextra[1] == ' ' ) kextra++;
453 }
454 cmdline_parse(cmdline);
456 parse_video_info();
458 set_current((struct vcpu *)0xfffff000); /* debug sanity */
459 set_processor_id(0); /* needed early, for smp_processor_id() */
460 if ( cpu_has_efer )
461 rdmsrl(MSR_EFER, this_cpu(efer));
462 asm volatile ( "mov %%cr4,%0" : "=r" (this_cpu(cr4)) );
464 smp_prepare_boot_cpu();
466 /* We initialise the serial devices very early so we can get debugging. */
467 ns16550.io_base = 0x3f8;
468 ns16550.irq = 4;
469 ns16550_init(0, &ns16550);
470 ns16550.io_base = 0x2f8;
471 ns16550.irq = 3;
472 ns16550_init(1, &ns16550);
473 serial_init_preirq();
475 init_console();
477 printk("Command line: %s\n", cmdline);
479 printk("Video information:\n");
481 /* Print VGA display mode information. */
482 switch ( vga_console_info.video_type )
483 {
484 case XEN_VGATYPE_TEXT_MODE_3:
485 printk(" VGA is text mode %dx%d, font 8x%d\n",
486 vga_console_info.u.text_mode_3.columns,
487 vga_console_info.u.text_mode_3.rows,
488 vga_console_info.u.text_mode_3.font_height);
489 break;
490 case XEN_VGATYPE_VESA_LFB:
491 printk(" VGA is graphics mode %dx%d, %d bpp\n",
492 vga_console_info.u.vesa_lfb.width,
493 vga_console_info.u.vesa_lfb.height,
494 vga_console_info.u.vesa_lfb.bits_per_pixel);
495 break;
496 default:
497 printk(" No VGA detected\n");
498 break;
499 }
501 /* Print VBE/DDC EDID information. */
502 if ( bootsym(boot_edid_caps) != 0x1313 )
503 {
504 u16 caps = bootsym(boot_edid_caps);
505 printk(" VBE/DDC methods:%s%s%s; ",
506 (caps & 1) ? " V1" : "",
507 (caps & 2) ? " V2" : "",
508 !(caps & 3) ? " none" : "");
509 printk("EDID transfer time: %d seconds\n", caps >> 8);
510 if ( *(u32 *)bootsym(boot_edid_info) == 0x13131313 )
511 {
512 printk(" EDID info not retrieved because ");
513 if ( !(caps & 3) )
514 printk("no DDC retrieval method detected\n");
515 else if ( (caps >> 8) > 5 )
516 printk("takes longer than 5 seconds\n");
517 else
518 printk("of reasons unknown\n");
519 }
520 }
522 printk("Disc information:\n");
523 printk(" Found %d MBR signatures\n",
524 bootsym(boot_mbr_signature_nr));
525 printk(" Found %d EDD information structures\n",
526 bootsym(boot_edd_info_nr));
528 /* Check that we have at least one Multiboot module. */
529 if ( !(mbi->flags & MBI_MODULES) || (mbi->mods_count == 0) )
530 EARLY_FAIL("dom0 kernel not specified. "
531 "Check bootloader configuration.\n");
533 if ( ((unsigned long)cpu0_stack & (STACK_SIZE-1)) != 0 )
534 EARLY_FAIL("Misaligned CPU0 stack.\n");
536 if ( e820_raw_nr != 0 )
537 {
538 memmap_type = "Xen-e820";
539 }
540 else if ( bootsym(lowmem_kb) )
541 {
542 memmap_type = "Xen-e801";
543 e820_raw[0].addr = 0;
544 e820_raw[0].size = bootsym(lowmem_kb) << 10;
545 e820_raw[0].type = E820_RAM;
546 e820_raw[1].addr = 0x100000;
547 e820_raw[1].size = bootsym(highmem_kb) << 10;
548 e820_raw[1].type = E820_RAM;
549 e820_raw_nr = 2;
550 }
551 else if ( mbi->flags & MBI_MEMMAP )
552 {
553 memmap_type = "Multiboot-e820";
554 while ( (bytes < mbi->mmap_length) && (e820_raw_nr < E820MAX) )
555 {
556 memory_map_t *map = __va(mbi->mmap_addr + bytes);
558 /*
559 * This is a gross workaround for a BIOS bug. Some bootloaders do
560 * not write e820 map entries into pre-zeroed memory. This is
561 * okay if the BIOS fills in all fields of the map entry, but
562 * some broken BIOSes do not bother to write the high word of
563 * the length field if the length is smaller than 4GB. We
564 * detect and fix this by flagging sections below 4GB that
565 * appear to be larger than 4GB in size.
566 */
567 if ( (map->base_addr_high == 0) && (map->length_high != 0) )
568 {
569 if ( !e820_warn )
570 {
571 printk("WARNING: Buggy e820 map detected and fixed "
572 "(truncated length fields).\n");
573 e820_warn = 1;
574 }
575 map->length_high = 0;
576 }
578 e820_raw[e820_raw_nr].addr =
579 ((u64)map->base_addr_high << 32) | (u64)map->base_addr_low;
580 e820_raw[e820_raw_nr].size =
581 ((u64)map->length_high << 32) | (u64)map->length_low;
582 e820_raw[e820_raw_nr].type = map->type;
583 e820_raw_nr++;
585 bytes += map->size + 4;
586 }
587 }
588 else if ( mbi->flags & MBI_MEMLIMITS )
589 {
590 memmap_type = "Multiboot-e801";
591 e820_raw[0].addr = 0;
592 e820_raw[0].size = mbi->mem_lower << 10;
593 e820_raw[0].type = E820_RAM;
594 e820_raw[1].addr = 0x100000;
595 e820_raw[1].size = mbi->mem_upper << 10;
596 e820_raw[1].type = E820_RAM;
597 e820_raw_nr = 2;
598 }
599 else
600 {
601 EARLY_FAIL("Bootloader provided no memory information.\n");
602 }
604 /* Sanitise the raw E820 map to produce a final clean version. */
605 max_page = init_e820(memmap_type, e820_raw, &e820_raw_nr);
607 #ifdef CONFIG_X86_64
608 /*
609 * On x86/64 we are able to account for the allocation bitmap
610 * (allocated in common/page_alloc.c:init_boot_allocator()) stealing
611 * from the Xen heap. Here we make the Xen heap appropriately larger.
612 */
613 opt_xenheap_megabytes += (max_page / 8) >> 20;
614 #endif
616 /*
617 * Since there are some stubs getting built on the stacks which use
618 * direct calls/jumps, the heap must be confined to the lower 2G so
619 * that those branches can reach their targets.
620 */
621 if ( opt_xenheap_megabytes > 2048 )
622 opt_xenheap_megabytes = 2048;
624 /* Create a temporary copy of the E820 map. */
625 memcpy(&boot_e820, &e820, sizeof(e820));
627 /* Early kexec reservation (explicit static start address). */
628 kexec_reserve_area(&boot_e820);
630 /*
631 * Iterate backwards over all superpage-aligned RAM regions.
632 *
633 * We require superpage alignment because the boot allocator is not yet
634 * initialised. Hence we can only map superpages in the address range
635 * 0 to BOOTSTRAP_DIRECTMAP_END, as this is guaranteed not to require
636 * dynamic allocation of pagetables.
637 *
638 * As well as mapping superpages in that range, in preparation for
639 * initialising the boot allocator, we also look for a region to which
640 * we can relocate the dom0 kernel and other multiboot modules. Also, on
641 * x86/64, we relocate Xen to higher memory.
642 */
643 modules_length = mod[mbi->mods_count-1].mod_end - mod[0].mod_start;
644 for ( i = boot_e820.nr_map-1; i >= 0; i-- )
645 {
646 uint64_t s, e, mask = (1UL << L2_PAGETABLE_SHIFT) - 1;
648 /* Superpage-aligned chunks from 16MB to BOOTSTRAP_DIRECTMAP_END. */
649 s = (boot_e820.map[i].addr + mask) & ~mask;
650 e = (boot_e820.map[i].addr + boot_e820.map[i].size) & ~mask;
651 s = max_t(uint64_t, s, 16 << 20);
652 e = min_t(uint64_t, e, BOOTSTRAP_DIRECTMAP_END);
653 if ( (boot_e820.map[i].type != E820_RAM) || (s >= e) )
654 continue;
656 /* Map the chunk. No memory will need to be allocated to do this. */
657 map_pages_to_xen(
658 (unsigned long)maddr_to_bootstrap_virt(s),
659 s >> PAGE_SHIFT, (e-s) >> PAGE_SHIFT, PAGE_HYPERVISOR);
661 #if defined(CONFIG_X86_64)
662 /* Is the region suitable for relocating Xen? */
663 if ( !xen_phys_start && (((e-s) >> 20) >= opt_xenheap_megabytes) )
664 {
665 extern l2_pgentry_t l2_xenmap[];
666 l4_pgentry_t *pl4e;
667 l3_pgentry_t *pl3e;
668 l2_pgentry_t *pl2e;
669 int i, j, k;
671 /* Select relocation address. */
672 e = (e - (opt_xenheap_megabytes << 20)) & ~mask;
673 xen_phys_start = e;
674 bootsym(trampoline_xen_phys_start) = e;
676 /*
677 * Perform relocation to new physical address.
678 * Before doing so we must sync static/global data with main memory
679 * with a barrier(). After this we must *not* modify static/global
680 * data until after we have switched to the relocated pagetables!
681 */
682 barrier();
683 move_memory(e, 0, __pa(&_end) - xen_phys_start);
685 /* Poison low 1MB to detect stray pointers to physical 0-1MB. */
686 memset(maddr_to_bootstrap_virt(e), 0x55, 1U<<20);
688 /* Walk initial pagetables, relocating page directory entries. */
689 pl4e = __va(__pa(idle_pg_table));
690 for ( i = 0 ; i < L4_PAGETABLE_ENTRIES; i++, pl4e++ )
691 {
692 if ( !(l4e_get_flags(*pl4e) & _PAGE_PRESENT) )
693 continue;
694 *pl4e = l4e_from_intpte(l4e_get_intpte(*pl4e) +
695 xen_phys_start);
696 pl3e = l4e_to_l3e(*pl4e);
697 for ( j = 0; j < L3_PAGETABLE_ENTRIES; j++, pl3e++ )
698 {
699 /* Not present, 1GB mapping, or already relocated? */
700 if ( !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) ||
701 (l3e_get_flags(*pl3e) & _PAGE_PSE) ||
702 (l3e_get_pfn(*pl3e) > 0x1000) )
703 continue;
704 *pl3e = l3e_from_intpte(l3e_get_intpte(*pl3e) +
705 xen_phys_start);
706 pl2e = l3e_to_l2e(*pl3e);
707 for ( k = 0; k < L2_PAGETABLE_ENTRIES; k++, pl2e++ )
708 {
709 /* Not present, PSE, or already relocated? */
710 if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ||
711 (l2e_get_flags(*pl2e) & _PAGE_PSE) ||
712 (l2e_get_pfn(*pl2e) > 0x1000) )
713 continue;
714 *pl2e = l2e_from_intpte(l2e_get_intpte(*pl2e) +
715 xen_phys_start);
716 }
717 }
718 }
720 /* The only data mappings to be relocated are in the Xen area. */
721 pl2e = __va(__pa(l2_xenmap));
722 *pl2e++ = l2e_from_pfn(xen_phys_start >> PAGE_SHIFT,
723 PAGE_HYPERVISOR | _PAGE_PSE);
724 for ( i = 1; i < L2_PAGETABLE_ENTRIES; i++, pl2e++ )
725 {
726 if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) )
727 continue;
728 *pl2e = l2e_from_intpte(l2e_get_intpte(*pl2e) +
729 xen_phys_start);
730 }
732 /* Re-sync the stack and then switch to relocated pagetables. */
733 asm volatile (
734 "rep movsb ; " /* re-sync the stack */
735 "movq %%cr4,%%rsi ; "
736 "andb $0x7f,%%sil ; "
737 "movq %%rsi,%%cr4 ; " /* CR4.PGE == 0 */
738 "movq %0,%%cr3 ; " /* CR3 == new pagetables */
739 "orb $0x80,%%sil ; "
740 "movq %%rsi,%%cr4 " /* CR4.PGE == 1 */
741 : : "r" (__pa(idle_pg_table)), "S" (cpu0_stack),
742 "D" (__va(__pa(cpu0_stack))), "c" (STACK_SIZE) : "memory" );
743 }
744 #endif
746 /* Is the region suitable for relocating the multiboot modules? */
747 if ( !initial_images_start && (s < e) && ((e-s) >= modules_length) )
748 {
749 initial_images_end = e;
750 e = (e - modules_length) & PAGE_MASK;
751 initial_images_start = e;
752 move_memory(initial_images_start,
753 mod[0].mod_start, mod[mbi->mods_count-1].mod_end);
754 }
756 if ( !kexec_crash_area.start && (s < e) &&
757 ((e-s) >= kexec_crash_area.size) )
758 {
759 e = (e - kexec_crash_area.size) & PAGE_MASK;
760 kexec_crash_area.start = e;
761 }
762 }
764 if ( !initial_images_start )
765 EARLY_FAIL("Not enough memory to relocate the dom0 kernel image.\n");
766 reserve_e820_ram(&boot_e820, initial_images_start, initial_images_end);
768 /* Initialise Xen heap and boot heap. */
769 xenheap_phys_start = init_boot_allocator(__pa(&_end));
770 xenheap_phys_end = opt_xenheap_megabytes << 20;
771 #if defined(CONFIG_X86_64)
772 if ( !xen_phys_start )
773 EARLY_FAIL("Not enough memory to relocate Xen.\n");
774 xenheap_phys_end += xen_phys_start;
775 reserve_e820_ram(&boot_e820, xen_phys_start,
776 xen_phys_start + (opt_xenheap_megabytes<<20));
777 #endif
779 /* Late kexec reservation (dynamic start address). */
780 kexec_reserve_area(&boot_e820);
782 /*
783 * With the boot allocator now initialised, we can walk every RAM region
784 * and map it in its entirety (on x86/64, at least) and notify it to the
785 * boot allocator.
786 */
787 for ( i = 0; i < boot_e820.nr_map; i++ )
788 {
789 uint64_t s, e, map_s, map_e, mask = PAGE_SIZE - 1;
791 /* Only page alignment required now. */
792 s = (boot_e820.map[i].addr + mask) & ~mask;
793 e = (boot_e820.map[i].addr + boot_e820.map[i].size) & ~mask;
794 #if defined(CONFIG_X86_32)
795 s = max_t(uint64_t, s, xenheap_phys_end);
796 #else
797 s = max_t(uint64_t, s, 1<<20);
798 #endif
799 if ( (boot_e820.map[i].type != E820_RAM) || (s >= e) )
800 continue;
802 /* Need to create mappings above 16MB. */
803 map_s = max_t(uint64_t, s, 16<<20);
804 map_e = e;
805 #if defined(CONFIG_X86_32) /* mappings are truncated on x86_32 */
806 map_e = min_t(uint64_t, map_e, BOOTSTRAP_DIRECTMAP_END);
807 #endif
809 /* Pass mapped memory to allocator /before/ creating new mappings. */
810 init_boot_pages(s, min_t(uint64_t, map_s, e));
812 /* Create new mappings /before/ passing memory to the allocator. */
813 if ( map_s < map_e )
814 map_pages_to_xen(
815 (unsigned long)maddr_to_bootstrap_virt(map_s),
816 map_s >> PAGE_SHIFT, (map_e-map_s) >> PAGE_SHIFT,
817 PAGE_HYPERVISOR);
819 /* Pass remainder of this memory chunk to the allocator. */
820 init_boot_pages(map_s, e);
821 }
823 memguard_init();
825 nr_pages = 0;
826 for ( i = 0; i < e820.nr_map; i++ )
827 if ( e820.map[i].type == E820_RAM )
828 nr_pages += e820.map[i].size >> PAGE_SHIFT;
829 printk("System RAM: %luMB (%lukB)\n",
830 nr_pages >> (20 - PAGE_SHIFT),
831 nr_pages << (PAGE_SHIFT - 10));
832 total_pages = nr_pages;
834 /* Sanity check for unwanted bloat of certain hypercall structures. */
835 BUILD_BUG_ON(sizeof(((struct xen_platform_op *)0)->u) !=
836 sizeof(((struct xen_platform_op *)0)->u.pad));
837 BUILD_BUG_ON(sizeof(((struct xen_domctl *)0)->u) !=
838 sizeof(((struct xen_domctl *)0)->u.pad));
839 BUILD_BUG_ON(sizeof(((struct xen_sysctl *)0)->u) !=
840 sizeof(((struct xen_sysctl *)0)->u.pad));
842 BUILD_BUG_ON(sizeof(start_info_t) > PAGE_SIZE);
843 BUILD_BUG_ON(sizeof(shared_info_t) > PAGE_SIZE);
844 BUILD_BUG_ON(sizeof(struct vcpu_info) != 64);
846 #ifdef CONFIG_COMPAT
847 BUILD_BUG_ON(sizeof(((struct compat_platform_op *)0)->u) !=
848 sizeof(((struct compat_platform_op *)0)->u.pad));
849 BUILD_BUG_ON(sizeof(start_info_compat_t) > PAGE_SIZE);
850 BUILD_BUG_ON(sizeof(struct compat_vcpu_info) != 64);
851 #endif
853 /* Check definitions in public headers match internal defs. */
854 BUILD_BUG_ON(__HYPERVISOR_VIRT_START != HYPERVISOR_VIRT_START);
855 #ifdef HYPERVISOR_VIRT_END
856 BUILD_BUG_ON(__HYPERVISOR_VIRT_END != HYPERVISOR_VIRT_END);
857 #endif
858 BUILD_BUG_ON(MACH2PHYS_VIRT_START != RO_MPT_VIRT_START);
859 BUILD_BUG_ON(MACH2PHYS_VIRT_END != RO_MPT_VIRT_END);
861 init_frametable();
863 acpi_boot_table_init();
865 acpi_numa_init();
867 numa_initmem_init(0, max_page);
869 /* Initialise the Xen heap, skipping RAM holes. */
870 init_xenheap_pages(xenheap_phys_start, xenheap_phys_end);
871 nr_pages = (xenheap_phys_end - xenheap_phys_start) >> PAGE_SHIFT;
872 #ifdef __x86_64__
873 init_xenheap_pages(xen_phys_start, __pa(&_start));
874 nr_pages += (__pa(&_start) - xen_phys_start) >> PAGE_SHIFT;
875 vesa_init();
876 #endif
877 xenheap_phys_start = xen_phys_start;
878 printk("Xen heap: %luMB (%lukB)\n",
879 nr_pages >> (20 - PAGE_SHIFT),
880 nr_pages << (PAGE_SHIFT - 10));
882 end_boot_allocator();
884 early_boot = 0;
886 softirq_init();
888 early_cpu_init();
890 paging_init();
892 tboot_probe();
894 /* Unmap the first page of CPU0's stack. */
895 memguard_guard_stack(cpu0_stack);
897 open_softirq(NEW_TLBFLUSH_CLOCK_PERIOD_SOFTIRQ, new_tlbflush_clock_period);
899 if ( opt_watchdog )
900 nmi_watchdog = NMI_LOCAL_APIC;
902 sort_exception_tables();
904 find_smp_config();
906 dmi_scan_machine();
908 generic_apic_probe();
910 if ( x2apic_is_available() )
911 enable_x2apic();
913 acpi_boot_init();
915 init_cpu_to_node();
917 if ( smp_found_config )
918 get_smp_config();
920 #ifdef CONFIG_X86_64
921 /* Low mappings were only needed for some BIOS table parsing. */
922 zap_low_mappings();
923 #endif
925 init_apic_mappings();
927 init_IRQ();
929 percpu_init_areas();
931 xsm_init(&initrdidx, mbi, initial_images_start);
933 init_idle_domain();
935 trap_init();
937 rcu_init();
939 timer_init();
941 early_time_init();
943 arch_init_memory();
945 identify_cpu(&boot_cpu_data);
946 if ( cpu_has_fxsr )
947 set_in_cr4(X86_CR4_OSFXSR);
948 if ( cpu_has_xmm )
949 set_in_cr4(X86_CR4_OSXMMEXCPT);
950 #ifdef CONFIG_X86_64
951 vesa_mtrr_init();
952 #endif
954 if ( opt_nosmp )
955 max_cpus = 0;
957 smp_prepare_cpus(max_cpus);
959 /*
960 * Initialise higher-level timer functions. We do this fairly late
961 * (post-SMP) because the time bases and scale factors need to be updated
962 * regularly, and SMP initialisation can cause a long delay with
963 * interrupts not yet enabled.
964 */
965 init_xen_time();
967 initialize_keytable();
969 serial_init_postirq();
971 BUG_ON(!local_irq_is_enabled());
972 spin_debug_enable();
974 for_each_present_cpu ( i )
975 {
976 if ( num_online_cpus() >= max_cpus )
977 break;
978 if ( !cpu_online(i) )
979 {
980 rcu_online_cpu(i);
981 __cpu_up(i);
982 }
984 /* Set up cpu_to_node[]. */
985 srat_detect_node(i);
986 /* Set up node_to_cpumask based on cpu_to_node[]. */
987 numa_add_cpu(i);
988 }
990 printk("Brought up %ld CPUs\n", (long)num_online_cpus());
991 smp_cpus_done(max_cpus);
993 initialise_gdb(); /* could be moved earlier */
995 do_initcalls();
997 if ( opt_watchdog )
998 watchdog_enable();
1000 /* Create initial domain 0. */
1001 dom0 = domain_create(0, 0, DOM0_SSIDREF);
1002 if ( (dom0 == NULL) || (alloc_vcpu(dom0, 0, 0) == NULL) )
1003 panic("Error creating domain 0\n");
1005 dom0->is_privileged = 1;
1006 dom0->target = NULL;
1008 /* Grab the DOM0 command line. */
1009 cmdline = (char *)(mod[0].string ? __va(mod[0].string) : NULL);
1010 if ( (cmdline != NULL) || (kextra != NULL) )
1012 static char dom0_cmdline[MAX_GUEST_CMDLINE];
1014 cmdline = cmdline_cook(cmdline);
1015 safe_strcpy(dom0_cmdline, cmdline);
1017 if ( kextra != NULL )
1018 /* kextra always includes exactly one leading space. */
1019 safe_strcat(dom0_cmdline, kextra);
1021 /* Append any extra parameters. */
1022 if ( skip_ioapic_setup && !strstr(dom0_cmdline, "noapic") )
1023 safe_strcat(dom0_cmdline, " noapic");
1024 if ( acpi_skip_timer_override &&
1025 !strstr(dom0_cmdline, "acpi_skip_timer_override") )
1026 safe_strcat(dom0_cmdline, " acpi_skip_timer_override");
1027 if ( (strlen(acpi_param) == 0) && acpi_disabled )
1029 printk("ACPI is disabled, notifying Domain 0 (acpi=off)\n");
1030 safe_strcpy(acpi_param, "off");
1032 if ( (strlen(acpi_param) != 0) && !strstr(dom0_cmdline, "acpi=") )
1034 safe_strcat(dom0_cmdline, " acpi=");
1035 safe_strcat(dom0_cmdline, acpi_param);
1038 cmdline = dom0_cmdline;
1041 if ( (initrdidx > 0) && (initrdidx < mbi->mods_count) )
1043 _initrd_start = initial_images_start +
1044 (mod[initrdidx].mod_start - mod[0].mod_start);
1045 _initrd_len = mod[initrdidx].mod_end - mod[initrdidx].mod_start;
1048 if ( xen_cpuidle )
1049 xen_processor_pmbits |= XEN_PROCESSOR_PM_CX;
1051 /*
1052 * We're going to setup domain0 using the module(s) that we stashed safely
1053 * above our heap. The second module, if present, is an initrd ramdisk.
1054 */
1055 if ( construct_dom0(dom0,
1056 initial_images_start,
1057 mod[0].mod_end-mod[0].mod_start,
1058 _initrd_start,
1059 _initrd_len,
1060 cmdline) != 0)
1061 panic("Could not set up DOM0 guest OS\n");
1063 /* Scrub RAM that is still free and so may go to an unprivileged domain. */
1064 scrub_heap_pages();
1066 init_trace_bufs();
1068 console_endboot();
1070 /* Hide UART from DOM0 if we're using it */
1071 serial_endboot();
1073 domain_unpause_by_systemcontroller(dom0);
1075 reset_stack_and_jump(init_done);
1078 void arch_get_xen_caps(xen_capabilities_info_t *info)
1080 /* Interface name is always xen-3.0-* for Xen-3.x. */
1081 int major = 3, minor = 0;
1082 char s[32];
1084 (*info)[0] = '\0';
1086 #if defined(CONFIG_X86_32)
1088 snprintf(s, sizeof(s), "xen-%d.%d-x86_32p ", major, minor);
1089 safe_strcat(*info, s);
1090 if ( hvm_enabled )
1092 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32 ", major, minor);
1093 safe_strcat(*info, s);
1094 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32p ", major, minor);
1095 safe_strcat(*info, s);
1098 #elif defined(CONFIG_X86_64)
1100 snprintf(s, sizeof(s), "xen-%d.%d-x86_64 ", major, minor);
1101 safe_strcat(*info, s);
1102 #ifdef CONFIG_COMPAT
1103 snprintf(s, sizeof(s), "xen-%d.%d-x86_32p ", major, minor);
1104 safe_strcat(*info, s);
1105 #endif
1106 if ( hvm_enabled )
1108 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32 ", major, minor);
1109 safe_strcat(*info, s);
1110 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32p ", major, minor);
1111 safe_strcat(*info, s);
1112 snprintf(s, sizeof(s), "hvm-%d.%d-x86_64 ", major, minor);
1113 safe_strcat(*info, s);
1116 #endif
1119 int xen_in_range(paddr_t start, paddr_t end)
1121 start = max_t(paddr_t, start, xenheap_phys_start);
1122 end = min_t(paddr_t, end, xenheap_phys_end);
1124 return start < end;
1127 /*
1128 * Local variables:
1129 * mode: C
1130 * c-set-style: "BSD"
1131 * c-basic-offset: 4
1132 * tab-width: 4
1133 * indent-tabs-mode: nil
1134 * End:
1135 */