ia64/xen-unstable

view xen/arch/x86/setup.c @ 18806:ed8524f4a044

x86: Re-initialise HPET on resume from S3

Signed-off-by: Guanqun Lu <guanqun.lu@intel.com>
Signed-off-by: Kevin Tian <kevin.tian@intel.com>
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Nov 18 15:55:14 2008 +0000 (2008-11-18)
parents 8e18dd41c6c7
children df32584505c2
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;
234 /* Domain creation requires that scheduler structures are initialised. */
235 scheduler_init();
237 idle_domain = domain_create(IDLE_DOMAIN_ID, 0, 0);
238 if ( (idle_domain == NULL) || (alloc_vcpu(idle_domain, 0, 0) == NULL) )
239 BUG();
241 set_current(idle_domain->vcpu[0]);
242 idle_vcpu[0] = this_cpu(curr_vcpu) = current;
244 setup_idle_pagetable();
245 }
247 static void __init srat_detect_node(int cpu)
248 {
249 unsigned node;
250 u32 apicid = x86_cpu_to_apicid[cpu];
252 node = apicid_to_node[apicid];
253 if ( node == NUMA_NO_NODE )
254 node = 0;
255 numa_set_node(cpu, node);
257 if ( acpi_numa > 0 )
258 printk(KERN_INFO "CPU %d APIC %d -> Node %d\n", cpu, apicid, node);
259 }
261 /*
262 * Ensure a given physical memory range is present in the bootstrap mappings.
263 * Use superpage mappings to ensure that pagetable memory needn't be allocated.
264 */
265 static void __init bootstrap_map(unsigned long start, unsigned long end)
266 {
267 unsigned long mask = (1UL << L2_PAGETABLE_SHIFT) - 1;
268 start = max_t(unsigned long, start & ~mask, 16UL << 20);
269 end = (end + mask) & ~mask;
270 if ( start >= end )
271 return;
272 if ( end > BOOTSTRAP_DIRECTMAP_END )
273 panic("Cannot access memory beyond end of "
274 "bootstrap direct-map area\n");
275 map_pages_to_xen(
276 (unsigned long)maddr_to_bootstrap_virt(start),
277 start >> PAGE_SHIFT, (end-start) >> PAGE_SHIFT, PAGE_HYPERVISOR);
278 }
280 static void __init move_memory(
281 unsigned long dst, unsigned long src_start, unsigned long src_end)
282 {
283 bootstrap_map(src_start, src_end);
284 bootstrap_map(dst, dst + src_end - src_start);
285 memmove(maddr_to_bootstrap_virt(dst),
286 maddr_to_bootstrap_virt(src_start),
287 src_end - src_start);
288 }
290 /* A temporary copy of the e820 map that we can mess with during bootstrap. */
291 static struct e820map __initdata boot_e820;
293 struct boot_video_info {
294 u8 orig_x; /* 0x00 */
295 u8 orig_y; /* 0x01 */
296 u8 orig_video_mode; /* 0x02 */
297 u8 orig_video_cols; /* 0x03 */
298 u8 orig_video_lines; /* 0x04 */
299 u8 orig_video_isVGA; /* 0x05 */
300 u16 orig_video_points; /* 0x06 */
302 /* VESA graphic mode -- linear frame buffer */
303 u32 capabilities; /* 0x08 */
304 u16 lfb_linelength; /* 0x0c */
305 u16 lfb_width; /* 0x0e */
306 u16 lfb_height; /* 0x10 */
307 u16 lfb_depth; /* 0x12 */
308 u32 lfb_base; /* 0x14 */
309 u32 lfb_size; /* 0x18 */
310 u8 red_size; /* 0x1c */
311 u8 red_pos; /* 0x1d */
312 u8 green_size; /* 0x1e */
313 u8 green_pos; /* 0x1f */
314 u8 blue_size; /* 0x20 */
315 u8 blue_pos; /* 0x21 */
316 u8 rsvd_size; /* 0x22 */
317 u8 rsvd_pos; /* 0x23 */
318 u16 vesapm_seg; /* 0x24 */
319 u16 vesapm_off; /* 0x26 */
320 u16 vesa_attrib; /* 0x28 */
321 };
323 static void __init parse_video_info(void)
324 {
325 struct boot_video_info *bvi = &bootsym(boot_vid_info);
327 if ( (bvi->orig_video_isVGA == 1) && (bvi->orig_video_mode == 3) )
328 {
329 vga_console_info.video_type = XEN_VGATYPE_TEXT_MODE_3;
330 vga_console_info.u.text_mode_3.font_height = bvi->orig_video_points;
331 vga_console_info.u.text_mode_3.cursor_x = bvi->orig_x;
332 vga_console_info.u.text_mode_3.cursor_y = bvi->orig_y;
333 vga_console_info.u.text_mode_3.rows = bvi->orig_video_lines;
334 vga_console_info.u.text_mode_3.columns = bvi->orig_video_cols;
335 }
336 else if ( bvi->orig_video_isVGA == 0x23 )
337 {
338 vga_console_info.video_type = XEN_VGATYPE_VESA_LFB;
339 vga_console_info.u.vesa_lfb.width = bvi->lfb_width;
340 vga_console_info.u.vesa_lfb.height = bvi->lfb_height;
341 vga_console_info.u.vesa_lfb.bytes_per_line = bvi->lfb_linelength;
342 vga_console_info.u.vesa_lfb.bits_per_pixel = bvi->lfb_depth;
343 vga_console_info.u.vesa_lfb.lfb_base = bvi->lfb_base;
344 vga_console_info.u.vesa_lfb.lfb_size = bvi->lfb_size;
345 vga_console_info.u.vesa_lfb.red_pos = bvi->red_pos;
346 vga_console_info.u.vesa_lfb.red_size = bvi->red_size;
347 vga_console_info.u.vesa_lfb.green_pos = bvi->green_pos;
348 vga_console_info.u.vesa_lfb.green_size = bvi->green_size;
349 vga_console_info.u.vesa_lfb.blue_pos = bvi->blue_pos;
350 vga_console_info.u.vesa_lfb.blue_size = bvi->blue_size;
351 vga_console_info.u.vesa_lfb.rsvd_pos = bvi->rsvd_pos;
352 vga_console_info.u.vesa_lfb.rsvd_size = bvi->rsvd_size;
353 vga_console_info.u.vesa_lfb.gbl_caps = bvi->capabilities;
354 vga_console_info.u.vesa_lfb.mode_attrs = bvi->vesa_attrib;
355 }
356 }
358 void __init kexec_reserve_area(struct e820map *e820)
359 {
360 unsigned long kdump_start = kexec_crash_area.start;
361 unsigned long kdump_size = kexec_crash_area.size;
362 static int is_reserved = 0;
364 kdump_size = (kdump_size + PAGE_SIZE - 1) & PAGE_MASK;
366 if ( (kdump_start == 0) || (kdump_size == 0) || is_reserved )
367 return;
369 is_reserved = 1;
371 if ( !reserve_e820_ram(e820, kdump_start, kdump_start + kdump_size) )
372 {
373 printk("Kdump: DISABLED (failed to reserve %luMB (%lukB) at 0x%lx)"
374 "\n", kdump_size >> 20, kdump_size >> 10, kdump_start);
375 kexec_crash_area.start = kexec_crash_area.size = 0;
376 }
377 else
378 {
379 printk("Kdump: %luMB (%lukB) at 0x%lx\n",
380 kdump_size >> 20, kdump_size >> 10, kdump_start);
381 }
382 }
384 void init_done(void)
385 {
386 extern char __init_begin[], __init_end[];
388 /* Free (or page-protect) the init areas. */
389 #ifndef MEMORY_GUARD
390 init_xenheap_pages(__pa(__init_begin), __pa(__init_end));
391 #endif
392 memguard_guard_range(__init_begin, __init_end - __init_begin);
393 #if defined(CONFIG_X86_64)
394 /* Also zap the mapping in the 1:1 area. */
395 memguard_guard_range(__va(__pa(__init_begin)), __init_end - __init_begin);
396 #endif
397 printk("Freed %ldkB init memory.\n", (long)(__init_end-__init_begin)>>10);
399 startup_cpu_idle_loop();
400 }
402 static char * __init cmdline_cook(char *p)
403 {
404 p = p ? : "";
405 while ( *p == ' ' )
406 p++;
407 while ( (*p != ' ') && (*p != '\0') )
408 p++;
409 while ( *p == ' ' )
410 p++;
411 return p;
412 }
414 void __init __start_xen(unsigned long mbi_p)
415 {
416 char *memmap_type = NULL;
417 char *cmdline, *kextra;
418 unsigned long _initrd_start = 0, _initrd_len = 0;
419 unsigned int initrdidx = 1;
420 multiboot_info_t *mbi = __va(mbi_p);
421 module_t *mod = (module_t *)__va(mbi->mods_addr);
422 unsigned long nr_pages, modules_length;
423 int i, e820_warn = 0, bytes = 0;
424 struct ns16550_defaults ns16550 = {
425 .data_bits = 8,
426 .parity = 'n',
427 .stop_bits = 1
428 };
430 extern void early_page_fault(void);
431 set_intr_gate(TRAP_page_fault, &early_page_fault);
433 /* Parse the command-line options. */
434 cmdline = cmdline_cook((mbi->flags & MBI_CMDLINE) ?
435 __va(mbi->cmdline) : NULL);
436 if ( (kextra = strstr(cmdline, " -- ")) != NULL )
437 {
438 /*
439 * Options after ' -- ' separator belong to dom0.
440 * 1. Orphan dom0's options from Xen's command line.
441 * 2. Skip all but final leading space from dom0's options.
442 */
443 *kextra = '\0';
444 kextra += 3;
445 while ( kextra[1] == ' ' ) kextra++;
446 }
447 cmdline_parse(cmdline);
449 parse_video_info();
451 set_current((struct vcpu *)0xfffff000); /* debug sanity */
452 idle_vcpu[0] = current;
453 set_processor_id(0); /* needed early, for smp_processor_id() */
454 if ( cpu_has_efer )
455 rdmsrl(MSR_EFER, this_cpu(efer));
456 asm volatile ( "mov %%cr4,%0" : "=r" (this_cpu(cr4)) );
458 smp_prepare_boot_cpu();
460 /* We initialise the serial devices very early so we can get debugging. */
461 ns16550.io_base = 0x3f8;
462 ns16550.irq = 4;
463 ns16550_init(0, &ns16550);
464 ns16550.io_base = 0x2f8;
465 ns16550.irq = 3;
466 ns16550_init(1, &ns16550);
467 serial_init_preirq();
469 init_console();
471 printk("Command line: %s\n", cmdline);
473 printk("Video information:\n");
475 /* Print VGA display mode information. */
476 switch ( vga_console_info.video_type )
477 {
478 case XEN_VGATYPE_TEXT_MODE_3:
479 printk(" VGA is text mode %dx%d, font 8x%d\n",
480 vga_console_info.u.text_mode_3.columns,
481 vga_console_info.u.text_mode_3.rows,
482 vga_console_info.u.text_mode_3.font_height);
483 break;
484 case XEN_VGATYPE_VESA_LFB:
485 printk(" VGA is graphics mode %dx%d, %d bpp\n",
486 vga_console_info.u.vesa_lfb.width,
487 vga_console_info.u.vesa_lfb.height,
488 vga_console_info.u.vesa_lfb.bits_per_pixel);
489 break;
490 default:
491 printk(" No VGA detected\n");
492 break;
493 }
495 /* Print VBE/DDC EDID information. */
496 if ( bootsym(boot_edid_caps) != 0x1313 )
497 {
498 u16 caps = bootsym(boot_edid_caps);
499 printk(" VBE/DDC methods:%s%s%s; ",
500 (caps & 1) ? " V1" : "",
501 (caps & 2) ? " V2" : "",
502 !(caps & 3) ? " none" : "");
503 printk("EDID transfer time: %d seconds\n", caps >> 8);
504 if ( *(u32 *)bootsym(boot_edid_info) == 0x13131313 )
505 {
506 printk(" EDID info not retrieved because ");
507 if ( !(caps & 3) )
508 printk("no DDC retrieval method detected\n");
509 else if ( (caps >> 8) > 5 )
510 printk("takes longer than 5 seconds\n");
511 else
512 printk("of reasons unknown\n");
513 }
514 }
516 printk("Disc information:\n");
517 printk(" Found %d MBR signatures\n",
518 bootsym(boot_mbr_signature_nr));
519 printk(" Found %d EDD information structures\n",
520 bootsym(boot_edd_info_nr));
522 /* Check that we have at least one Multiboot module. */
523 if ( !(mbi->flags & MBI_MODULES) || (mbi->mods_count == 0) )
524 EARLY_FAIL("dom0 kernel not specified. "
525 "Check bootloader configuration.\n");
527 if ( ((unsigned long)cpu0_stack & (STACK_SIZE-1)) != 0 )
528 EARLY_FAIL("Misaligned CPU0 stack.\n");
530 if ( e820_raw_nr != 0 )
531 {
532 memmap_type = "Xen-e820";
533 }
534 else if ( bootsym(lowmem_kb) )
535 {
536 memmap_type = "Xen-e801";
537 e820_raw[0].addr = 0;
538 e820_raw[0].size = bootsym(lowmem_kb) << 10;
539 e820_raw[0].type = E820_RAM;
540 e820_raw[1].addr = 0x100000;
541 e820_raw[1].size = bootsym(highmem_kb) << 10;
542 e820_raw[1].type = E820_RAM;
543 e820_raw_nr = 2;
544 }
545 else if ( mbi->flags & MBI_MEMMAP )
546 {
547 memmap_type = "Multiboot-e820";
548 while ( (bytes < mbi->mmap_length) && (e820_raw_nr < E820MAX) )
549 {
550 memory_map_t *map = __va(mbi->mmap_addr + bytes);
552 /*
553 * This is a gross workaround for a BIOS bug. Some bootloaders do
554 * not write e820 map entries into pre-zeroed memory. This is
555 * okay if the BIOS fills in all fields of the map entry, but
556 * some broken BIOSes do not bother to write the high word of
557 * the length field if the length is smaller than 4GB. We
558 * detect and fix this by flagging sections below 4GB that
559 * appear to be larger than 4GB in size.
560 */
561 if ( (map->base_addr_high == 0) && (map->length_high != 0) )
562 {
563 if ( !e820_warn )
564 {
565 printk("WARNING: Buggy e820 map detected and fixed "
566 "(truncated length fields).\n");
567 e820_warn = 1;
568 }
569 map->length_high = 0;
570 }
572 e820_raw[e820_raw_nr].addr =
573 ((u64)map->base_addr_high << 32) | (u64)map->base_addr_low;
574 e820_raw[e820_raw_nr].size =
575 ((u64)map->length_high << 32) | (u64)map->length_low;
576 e820_raw[e820_raw_nr].type = map->type;
577 e820_raw_nr++;
579 bytes += map->size + 4;
580 }
581 }
582 else if ( mbi->flags & MBI_MEMLIMITS )
583 {
584 memmap_type = "Multiboot-e801";
585 e820_raw[0].addr = 0;
586 e820_raw[0].size = mbi->mem_lower << 10;
587 e820_raw[0].type = E820_RAM;
588 e820_raw[1].addr = 0x100000;
589 e820_raw[1].size = mbi->mem_upper << 10;
590 e820_raw[1].type = E820_RAM;
591 e820_raw_nr = 2;
592 }
593 else
594 {
595 EARLY_FAIL("Bootloader provided no memory information.\n");
596 }
598 /* Sanitise the raw E820 map to produce a final clean version. */
599 max_page = init_e820(memmap_type, e820_raw, &e820_raw_nr);
601 #ifdef CONFIG_X86_64
602 /*
603 * On x86/64 we are able to account for the allocation bitmap
604 * (allocated in common/page_alloc.c:init_boot_allocator()) stealing
605 * from the Xen heap. Here we make the Xen heap appropriately larger.
606 */
607 opt_xenheap_megabytes += (max_page / 8) >> 20;
608 #endif
610 /*
611 * Since there are some stubs getting built on the stacks which use
612 * direct calls/jumps, the heap must be confined to the lower 2G so
613 * that those branches can reach their targets.
614 */
615 if ( opt_xenheap_megabytes > 2048 )
616 opt_xenheap_megabytes = 2048;
618 /* Create a temporary copy of the E820 map. */
619 memcpy(&boot_e820, &e820, sizeof(e820));
621 /* Early kexec reservation (explicit static start address). */
622 kexec_reserve_area(&boot_e820);
624 /*
625 * Iterate backwards over all superpage-aligned RAM regions.
626 *
627 * We require superpage alignment because the boot allocator is not yet
628 * initialised. Hence we can only map superpages in the address range
629 * 0 to BOOTSTRAP_DIRECTMAP_END, as this is guaranteed not to require
630 * dynamic allocation of pagetables.
631 *
632 * As well as mapping superpages in that range, in preparation for
633 * initialising the boot allocator, we also look for a region to which
634 * we can relocate the dom0 kernel and other multiboot modules. Also, on
635 * x86/64, we relocate Xen to higher memory.
636 */
637 modules_length = mod[mbi->mods_count-1].mod_end - mod[0].mod_start;
638 for ( i = boot_e820.nr_map-1; i >= 0; i-- )
639 {
640 uint64_t s, e, mask = (1UL << L2_PAGETABLE_SHIFT) - 1;
642 /* Superpage-aligned chunks from 16MB to BOOTSTRAP_DIRECTMAP_END. */
643 s = (boot_e820.map[i].addr + mask) & ~mask;
644 e = (boot_e820.map[i].addr + boot_e820.map[i].size) & ~mask;
645 s = max_t(uint64_t, s, 16 << 20);
646 e = min_t(uint64_t, e, BOOTSTRAP_DIRECTMAP_END);
647 if ( (boot_e820.map[i].type != E820_RAM) || (s >= e) )
648 continue;
650 /* Map the chunk. No memory will need to be allocated to do this. */
651 map_pages_to_xen(
652 (unsigned long)maddr_to_bootstrap_virt(s),
653 s >> PAGE_SHIFT, (e-s) >> PAGE_SHIFT, PAGE_HYPERVISOR);
655 #if defined(CONFIG_X86_64)
656 /* Is the region suitable for relocating Xen? */
657 if ( !xen_phys_start && (((e-s) >> 20) >= opt_xenheap_megabytes) )
658 {
659 extern l2_pgentry_t l2_xenmap[];
660 l4_pgentry_t *pl4e;
661 l3_pgentry_t *pl3e;
662 l2_pgentry_t *pl2e;
663 int i, j, k;
665 /* Select relocation address. */
666 e = (e - (opt_xenheap_megabytes << 20)) & ~mask;
667 xen_phys_start = e;
668 bootsym(trampoline_xen_phys_start) = e;
670 /*
671 * Perform relocation to new physical address.
672 * Before doing so we must sync static/global data with main memory
673 * with a barrier(). After this we must *not* modify static/global
674 * data until after we have switched to the relocated pagetables!
675 */
676 barrier();
677 move_memory(e, 0, __pa(&_end) - xen_phys_start);
679 /* Poison low 1MB to detect stray pointers to physical 0-1MB. */
680 memset(maddr_to_bootstrap_virt(e), 0x55, 1U<<20);
682 /* Walk initial pagetables, relocating page directory entries. */
683 pl4e = __va(__pa(idle_pg_table));
684 for ( i = 0 ; i < L4_PAGETABLE_ENTRIES; i++, pl4e++ )
685 {
686 if ( !(l4e_get_flags(*pl4e) & _PAGE_PRESENT) )
687 continue;
688 *pl4e = l4e_from_intpte(l4e_get_intpte(*pl4e) +
689 xen_phys_start);
690 pl3e = l4e_to_l3e(*pl4e);
691 for ( j = 0; j < L3_PAGETABLE_ENTRIES; j++, pl3e++ )
692 {
693 /* Not present, 1GB mapping, or already relocated? */
694 if ( !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) ||
695 (l3e_get_flags(*pl3e) & _PAGE_PSE) ||
696 (l3e_get_pfn(*pl3e) > 0x1000) )
697 continue;
698 *pl3e = l3e_from_intpte(l3e_get_intpte(*pl3e) +
699 xen_phys_start);
700 pl2e = l3e_to_l2e(*pl3e);
701 for ( k = 0; k < L2_PAGETABLE_ENTRIES; k++, pl2e++ )
702 {
703 /* Not present, PSE, or already relocated? */
704 if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ||
705 (l2e_get_flags(*pl2e) & _PAGE_PSE) ||
706 (l2e_get_pfn(*pl2e) > 0x1000) )
707 continue;
708 *pl2e = l2e_from_intpte(l2e_get_intpte(*pl2e) +
709 xen_phys_start);
710 }
711 }
712 }
714 /* The only data mappings to be relocated are in the Xen area. */
715 pl2e = __va(__pa(l2_xenmap));
716 *pl2e++ = l2e_from_pfn(xen_phys_start >> PAGE_SHIFT,
717 PAGE_HYPERVISOR | _PAGE_PSE);
718 for ( i = 1; i < L2_PAGETABLE_ENTRIES; i++, pl2e++ )
719 {
720 if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) )
721 continue;
722 *pl2e = l2e_from_intpte(l2e_get_intpte(*pl2e) +
723 xen_phys_start);
724 }
726 /* Re-sync the stack and then switch to relocated pagetables. */
727 asm volatile (
728 "rep movsb ; " /* re-sync the stack */
729 "movq %%cr4,%%rsi ; "
730 "andb $0x7f,%%sil ; "
731 "movq %%rsi,%%cr4 ; " /* CR4.PGE == 0 */
732 "movq %0,%%cr3 ; " /* CR3 == new pagetables */
733 "orb $0x80,%%sil ; "
734 "movq %%rsi,%%cr4 " /* CR4.PGE == 1 */
735 : : "r" (__pa(idle_pg_table)), "S" (cpu0_stack),
736 "D" (__va(__pa(cpu0_stack))), "c" (STACK_SIZE) : "memory" );
737 }
738 #endif
740 /* Is the region suitable for relocating the multiboot modules? */
741 if ( !initial_images_start && (s < e) && ((e-s) >= modules_length) )
742 {
743 initial_images_end = e;
744 e = (e - modules_length) & PAGE_MASK;
745 initial_images_start = e;
746 move_memory(initial_images_start,
747 mod[0].mod_start, mod[mbi->mods_count-1].mod_end);
748 }
750 if ( !kexec_crash_area.start && (s < e) &&
751 ((e-s) >= kexec_crash_area.size) )
752 {
753 e = (e - kexec_crash_area.size) & PAGE_MASK;
754 kexec_crash_area.start = e;
755 }
756 }
758 if ( !initial_images_start )
759 EARLY_FAIL("Not enough memory to relocate the dom0 kernel image.\n");
760 reserve_e820_ram(&boot_e820, initial_images_start, initial_images_end);
762 /* Initialise Xen heap and boot heap. */
763 xenheap_phys_start = init_boot_allocator(__pa(&_end));
764 xenheap_phys_end = opt_xenheap_megabytes << 20;
765 #if defined(CONFIG_X86_64)
766 if ( !xen_phys_start )
767 EARLY_FAIL("Not enough memory to relocate Xen.\n");
768 xenheap_phys_end += xen_phys_start;
769 reserve_e820_ram(&boot_e820, xen_phys_start,
770 xen_phys_start + (opt_xenheap_megabytes<<20));
771 #endif
773 /* Late kexec reservation (dynamic start address). */
774 kexec_reserve_area(&boot_e820);
776 /*
777 * With the boot allocator now initialised, we can walk every RAM region
778 * and map it in its entirety (on x86/64, at least) and notify it to the
779 * boot allocator.
780 */
781 for ( i = 0; i < boot_e820.nr_map; i++ )
782 {
783 uint64_t s, e, map_s, map_e, mask = PAGE_SIZE - 1;
785 /* Only page alignment required now. */
786 s = (boot_e820.map[i].addr + mask) & ~mask;
787 e = (boot_e820.map[i].addr + boot_e820.map[i].size) & ~mask;
788 #if defined(CONFIG_X86_32)
789 s = max_t(uint64_t, s, xenheap_phys_end);
790 #else
791 s = max_t(uint64_t, s, 1<<20);
792 #endif
793 if ( (boot_e820.map[i].type != E820_RAM) || (s >= e) )
794 continue;
796 /* Need to create mappings above 16MB. */
797 map_s = max_t(uint64_t, s, 16<<20);
798 map_e = e;
799 #if defined(CONFIG_X86_32) /* mappings are truncated on x86_32 */
800 map_e = min_t(uint64_t, map_e, BOOTSTRAP_DIRECTMAP_END);
801 #endif
803 /* Pass mapped memory to allocator /before/ creating new mappings. */
804 init_boot_pages(s, min_t(uint64_t, map_s, e));
806 /* Create new mappings /before/ passing memory to the allocator. */
807 if ( map_s < map_e )
808 map_pages_to_xen(
809 (unsigned long)maddr_to_bootstrap_virt(map_s),
810 map_s >> PAGE_SHIFT, (map_e-map_s) >> PAGE_SHIFT,
811 PAGE_HYPERVISOR);
813 /* Pass remainder of this memory chunk to the allocator. */
814 init_boot_pages(map_s, e);
815 }
817 memguard_init();
819 nr_pages = 0;
820 for ( i = 0; i < e820.nr_map; i++ )
821 if ( e820.map[i].type == E820_RAM )
822 nr_pages += e820.map[i].size >> PAGE_SHIFT;
823 printk("System RAM: %luMB (%lukB)\n",
824 nr_pages >> (20 - PAGE_SHIFT),
825 nr_pages << (PAGE_SHIFT - 10));
826 total_pages = nr_pages;
828 /* Sanity check for unwanted bloat of certain hypercall structures. */
829 BUILD_BUG_ON(sizeof(((struct xen_platform_op *)0)->u) !=
830 sizeof(((struct xen_platform_op *)0)->u.pad));
831 BUILD_BUG_ON(sizeof(((struct xen_domctl *)0)->u) !=
832 sizeof(((struct xen_domctl *)0)->u.pad));
833 BUILD_BUG_ON(sizeof(((struct xen_sysctl *)0)->u) !=
834 sizeof(((struct xen_sysctl *)0)->u.pad));
836 BUILD_BUG_ON(sizeof(start_info_t) > PAGE_SIZE);
837 BUILD_BUG_ON(sizeof(shared_info_t) > PAGE_SIZE);
838 BUILD_BUG_ON(sizeof(struct vcpu_info) != 64);
840 #ifdef CONFIG_COMPAT
841 BUILD_BUG_ON(sizeof(((struct compat_platform_op *)0)->u) !=
842 sizeof(((struct compat_platform_op *)0)->u.pad));
843 BUILD_BUG_ON(sizeof(start_info_compat_t) > PAGE_SIZE);
844 BUILD_BUG_ON(sizeof(struct compat_vcpu_info) != 64);
845 #endif
847 /* Check definitions in public headers match internal defs. */
848 BUILD_BUG_ON(__HYPERVISOR_VIRT_START != HYPERVISOR_VIRT_START);
849 #ifdef HYPERVISOR_VIRT_END
850 BUILD_BUG_ON(__HYPERVISOR_VIRT_END != HYPERVISOR_VIRT_END);
851 #endif
852 BUILD_BUG_ON(MACH2PHYS_VIRT_START != RO_MPT_VIRT_START);
853 BUILD_BUG_ON(MACH2PHYS_VIRT_END != RO_MPT_VIRT_END);
855 init_frametable();
857 acpi_boot_table_init();
859 acpi_numa_init();
861 numa_initmem_init(0, max_page);
863 /* Initialise the Xen heap, skipping RAM holes. */
864 init_xenheap_pages(xenheap_phys_start, xenheap_phys_end);
865 nr_pages = (xenheap_phys_end - xenheap_phys_start) >> PAGE_SHIFT;
866 #ifdef __x86_64__
867 init_xenheap_pages(xen_phys_start, __pa(&_start));
868 nr_pages += (__pa(&_start) - xen_phys_start) >> PAGE_SHIFT;
869 vesa_init();
870 #endif
871 xenheap_phys_start = xen_phys_start;
872 printk("Xen heap: %luMB (%lukB)\n",
873 nr_pages >> (20 - PAGE_SHIFT),
874 nr_pages << (PAGE_SHIFT - 10));
876 end_boot_allocator();
878 early_boot = 0;
880 softirq_init();
882 early_cpu_init();
884 paging_init();
886 tboot_probe();
888 /* Unmap the first page of CPU0's stack. */
889 memguard_guard_stack(cpu0_stack);
891 open_softirq(NEW_TLBFLUSH_CLOCK_PERIOD_SOFTIRQ, new_tlbflush_clock_period);
893 if ( opt_watchdog )
894 nmi_watchdog = NMI_LOCAL_APIC;
896 sort_exception_tables();
898 find_smp_config();
900 dmi_scan_machine();
902 generic_apic_probe();
904 if ( x2apic_is_available() )
905 enable_x2apic();
907 acpi_boot_init();
909 init_cpu_to_node();
911 if ( smp_found_config )
912 get_smp_config();
914 #ifdef CONFIG_X86_64
915 /* Low mappings were only needed for some BIOS table parsing. */
916 zap_low_mappings();
917 #endif
919 init_apic_mappings();
921 init_IRQ();
923 percpu_init_areas();
925 xsm_init(&initrdidx, mbi, initial_images_start);
927 init_idle_domain();
929 trap_init();
931 rcu_init();
933 timer_init();
935 early_time_init();
937 arch_init_memory();
939 identify_cpu(&boot_cpu_data);
940 if ( cpu_has_fxsr )
941 set_in_cr4(X86_CR4_OSFXSR);
942 if ( cpu_has_xmm )
943 set_in_cr4(X86_CR4_OSXMMEXCPT);
944 #ifdef CONFIG_X86_64
945 vesa_mtrr_init();
946 #endif
948 if ( opt_nosmp )
949 max_cpus = 0;
951 smp_prepare_cpus(max_cpus);
953 /*
954 * Initialise higher-level timer functions. We do this fairly late
955 * (post-SMP) because the time bases and scale factors need to be updated
956 * regularly, and SMP initialisation can cause a long delay with
957 * interrupts not yet enabled.
958 */
959 init_xen_time();
961 initialize_keytable();
963 serial_init_postirq();
965 BUG_ON(!local_irq_is_enabled());
966 spin_debug_enable();
968 for_each_present_cpu ( i )
969 {
970 if ( num_online_cpus() >= max_cpus )
971 break;
972 if ( !cpu_online(i) )
973 {
974 rcu_online_cpu(i);
975 __cpu_up(i);
976 }
978 /* Set up cpu_to_node[]. */
979 srat_detect_node(i);
980 /* Set up node_to_cpumask based on cpu_to_node[]. */
981 numa_add_cpu(i);
982 }
984 printk("Brought up %ld CPUs\n", (long)num_online_cpus());
985 smp_cpus_done(max_cpus);
987 initialise_gdb(); /* could be moved earlier */
989 do_initcalls();
991 if ( opt_watchdog )
992 watchdog_enable();
994 /* Create initial domain 0. */
995 dom0 = domain_create(0, 0, DOM0_SSIDREF);
996 if ( (dom0 == NULL) || (alloc_vcpu(dom0, 0, 0) == NULL) )
997 panic("Error creating domain 0\n");
999 dom0->is_privileged = 1;
1000 dom0->target = NULL;
1002 /* Grab the DOM0 command line. */
1003 cmdline = (char *)(mod[0].string ? __va(mod[0].string) : NULL);
1004 if ( (cmdline != NULL) || (kextra != NULL) )
1006 static char dom0_cmdline[MAX_GUEST_CMDLINE];
1008 cmdline = cmdline_cook(cmdline);
1009 safe_strcpy(dom0_cmdline, cmdline);
1011 if ( kextra != NULL )
1012 /* kextra always includes exactly one leading space. */
1013 safe_strcat(dom0_cmdline, kextra);
1015 /* Append any extra parameters. */
1016 if ( skip_ioapic_setup && !strstr(dom0_cmdline, "noapic") )
1017 safe_strcat(dom0_cmdline, " noapic");
1018 if ( acpi_skip_timer_override &&
1019 !strstr(dom0_cmdline, "acpi_skip_timer_override") )
1020 safe_strcat(dom0_cmdline, " acpi_skip_timer_override");
1021 if ( (strlen(acpi_param) == 0) && acpi_disabled )
1023 printk("ACPI is disabled, notifying Domain 0 (acpi=off)\n");
1024 safe_strcpy(acpi_param, "off");
1026 if ( (strlen(acpi_param) != 0) && !strstr(dom0_cmdline, "acpi=") )
1028 safe_strcat(dom0_cmdline, " acpi=");
1029 safe_strcat(dom0_cmdline, acpi_param);
1032 cmdline = dom0_cmdline;
1035 if ( (initrdidx > 0) && (initrdidx < mbi->mods_count) )
1037 _initrd_start = initial_images_start +
1038 (mod[initrdidx].mod_start - mod[0].mod_start);
1039 _initrd_len = mod[initrdidx].mod_end - mod[initrdidx].mod_start;
1042 if ( xen_cpuidle )
1043 xen_processor_pmbits |= XEN_PROCESSOR_PM_CX;
1045 /*
1046 * We're going to setup domain0 using the module(s) that we stashed safely
1047 * above our heap. The second module, if present, is an initrd ramdisk.
1048 */
1049 if ( construct_dom0(dom0,
1050 initial_images_start,
1051 mod[0].mod_end-mod[0].mod_start,
1052 _initrd_start,
1053 _initrd_len,
1054 cmdline) != 0)
1055 panic("Could not set up DOM0 guest OS\n");
1057 /* Scrub RAM that is still free and so may go to an unprivileged domain. */
1058 scrub_heap_pages();
1060 init_trace_bufs();
1062 console_endboot();
1064 /* Hide UART from DOM0 if we're using it */
1065 serial_endboot();
1067 domain_unpause_by_systemcontroller(dom0);
1069 reset_stack_and_jump(init_done);
1072 void arch_get_xen_caps(xen_capabilities_info_t *info)
1074 /* Interface name is always xen-3.0-* for Xen-3.x. */
1075 int major = 3, minor = 0;
1076 char s[32];
1078 (*info)[0] = '\0';
1080 #if defined(CONFIG_X86_32)
1082 snprintf(s, sizeof(s), "xen-%d.%d-x86_32p ", major, minor);
1083 safe_strcat(*info, s);
1084 if ( hvm_enabled )
1086 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32 ", major, minor);
1087 safe_strcat(*info, s);
1088 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32p ", major, minor);
1089 safe_strcat(*info, s);
1092 #elif defined(CONFIG_X86_64)
1094 snprintf(s, sizeof(s), "xen-%d.%d-x86_64 ", major, minor);
1095 safe_strcat(*info, s);
1096 #ifdef CONFIG_COMPAT
1097 snprintf(s, sizeof(s), "xen-%d.%d-x86_32p ", major, minor);
1098 safe_strcat(*info, s);
1099 #endif
1100 if ( hvm_enabled )
1102 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32 ", major, minor);
1103 safe_strcat(*info, s);
1104 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32p ", major, minor);
1105 safe_strcat(*info, s);
1106 snprintf(s, sizeof(s), "hvm-%d.%d-x86_64 ", major, minor);
1107 safe_strcat(*info, s);
1110 #endif
1113 int xen_in_range(paddr_t start, paddr_t end)
1115 start = max_t(paddr_t, start, xenheap_phys_start);
1116 end = min_t(paddr_t, end, xenheap_phys_end);
1118 return start < end;
1121 /*
1122 * Local variables:
1123 * mode: C
1124 * c-set-style: "BSD"
1125 * c-basic-offset: 4
1126 * tab-width: 4
1127 * indent-tabs-mode: nil
1128 * End:
1129 */