ia64/xen-unstable

view xen/arch/x86/setup.c @ 18988:d77f66f89c04

x86: Poison initmem at end of Xen bootstrap

Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Jan 05 11:55:24 2009 +0000 (2009-01-05)
parents df32584505c2
children 845aa241e163
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 memset(__init_begin, 0xcc, __init_end - __init_begin); /* int3 poison */
390 #ifndef MEMORY_GUARD
391 init_xenheap_pages(__pa(__init_begin), __pa(__init_end));
392 #endif
393 memguard_guard_range(__init_begin, __init_end - __init_begin);
394 #if defined(CONFIG_X86_64)
395 /* Also zap the mapping in the 1:1 area. */
396 memguard_guard_range(__va(__pa(__init_begin)), __init_end - __init_begin);
397 #endif
398 printk("Freed %ldkB init memory.\n", (long)(__init_end-__init_begin)>>10);
400 startup_cpu_idle_loop();
401 }
403 static char * __init cmdline_cook(char *p)
404 {
405 p = p ? : "";
406 while ( *p == ' ' )
407 p++;
408 while ( (*p != ' ') && (*p != '\0') )
409 p++;
410 while ( *p == ' ' )
411 p++;
412 return p;
413 }
415 void __init __start_xen(unsigned long mbi_p)
416 {
417 char *memmap_type = NULL;
418 char *cmdline, *kextra;
419 unsigned long _initrd_start = 0, _initrd_len = 0;
420 unsigned int initrdidx = 1;
421 multiboot_info_t *mbi = __va(mbi_p);
422 module_t *mod = (module_t *)__va(mbi->mods_addr);
423 unsigned long nr_pages, modules_length;
424 int i, e820_warn = 0, bytes = 0;
425 struct ns16550_defaults ns16550 = {
426 .data_bits = 8,
427 .parity = 'n',
428 .stop_bits = 1
429 };
431 extern void early_page_fault(void);
432 set_intr_gate(TRAP_page_fault, &early_page_fault);
434 /* Parse the command-line options. */
435 cmdline = cmdline_cook((mbi->flags & MBI_CMDLINE) ?
436 __va(mbi->cmdline) : NULL);
437 if ( (kextra = strstr(cmdline, " -- ")) != NULL )
438 {
439 /*
440 * Options after ' -- ' separator belong to dom0.
441 * 1. Orphan dom0's options from Xen's command line.
442 * 2. Skip all but final leading space from dom0's options.
443 */
444 *kextra = '\0';
445 kextra += 3;
446 while ( kextra[1] == ' ' ) kextra++;
447 }
448 cmdline_parse(cmdline);
450 parse_video_info();
452 set_current((struct vcpu *)0xfffff000); /* debug sanity */
453 idle_vcpu[0] = current;
454 set_processor_id(0); /* needed early, for smp_processor_id() */
455 if ( cpu_has_efer )
456 rdmsrl(MSR_EFER, this_cpu(efer));
457 asm volatile ( "mov %%cr4,%0" : "=r" (this_cpu(cr4)) );
459 smp_prepare_boot_cpu();
461 /* We initialise the serial devices very early so we can get debugging. */
462 ns16550.io_base = 0x3f8;
463 ns16550.irq = 4;
464 ns16550_init(0, &ns16550);
465 ns16550.io_base = 0x2f8;
466 ns16550.irq = 3;
467 ns16550_init(1, &ns16550);
468 serial_init_preirq();
470 init_console();
472 printk("Command line: %s\n", cmdline);
474 printk("Video information:\n");
476 /* Print VGA display mode information. */
477 switch ( vga_console_info.video_type )
478 {
479 case XEN_VGATYPE_TEXT_MODE_3:
480 printk(" VGA is text mode %dx%d, font 8x%d\n",
481 vga_console_info.u.text_mode_3.columns,
482 vga_console_info.u.text_mode_3.rows,
483 vga_console_info.u.text_mode_3.font_height);
484 break;
485 case XEN_VGATYPE_VESA_LFB:
486 printk(" VGA is graphics mode %dx%d, %d bpp\n",
487 vga_console_info.u.vesa_lfb.width,
488 vga_console_info.u.vesa_lfb.height,
489 vga_console_info.u.vesa_lfb.bits_per_pixel);
490 break;
491 default:
492 printk(" No VGA detected\n");
493 break;
494 }
496 /* Print VBE/DDC EDID information. */
497 if ( bootsym(boot_edid_caps) != 0x1313 )
498 {
499 u16 caps = bootsym(boot_edid_caps);
500 printk(" VBE/DDC methods:%s%s%s; ",
501 (caps & 1) ? " V1" : "",
502 (caps & 2) ? " V2" : "",
503 !(caps & 3) ? " none" : "");
504 printk("EDID transfer time: %d seconds\n", caps >> 8);
505 if ( *(u32 *)bootsym(boot_edid_info) == 0x13131313 )
506 {
507 printk(" EDID info not retrieved because ");
508 if ( !(caps & 3) )
509 printk("no DDC retrieval method detected\n");
510 else if ( (caps >> 8) > 5 )
511 printk("takes longer than 5 seconds\n");
512 else
513 printk("of reasons unknown\n");
514 }
515 }
517 printk("Disc information:\n");
518 printk(" Found %d MBR signatures\n",
519 bootsym(boot_mbr_signature_nr));
520 printk(" Found %d EDD information structures\n",
521 bootsym(boot_edd_info_nr));
523 /* Check that we have at least one Multiboot module. */
524 if ( !(mbi->flags & MBI_MODULES) || (mbi->mods_count == 0) )
525 EARLY_FAIL("dom0 kernel not specified. "
526 "Check bootloader configuration.\n");
528 if ( ((unsigned long)cpu0_stack & (STACK_SIZE-1)) != 0 )
529 EARLY_FAIL("Misaligned CPU0 stack.\n");
531 if ( e820_raw_nr != 0 )
532 {
533 memmap_type = "Xen-e820";
534 }
535 else if ( bootsym(lowmem_kb) )
536 {
537 memmap_type = "Xen-e801";
538 e820_raw[0].addr = 0;
539 e820_raw[0].size = bootsym(lowmem_kb) << 10;
540 e820_raw[0].type = E820_RAM;
541 e820_raw[1].addr = 0x100000;
542 e820_raw[1].size = bootsym(highmem_kb) << 10;
543 e820_raw[1].type = E820_RAM;
544 e820_raw_nr = 2;
545 }
546 else if ( mbi->flags & MBI_MEMMAP )
547 {
548 memmap_type = "Multiboot-e820";
549 while ( (bytes < mbi->mmap_length) && (e820_raw_nr < E820MAX) )
550 {
551 memory_map_t *map = __va(mbi->mmap_addr + bytes);
553 /*
554 * This is a gross workaround for a BIOS bug. Some bootloaders do
555 * not write e820 map entries into pre-zeroed memory. This is
556 * okay if the BIOS fills in all fields of the map entry, but
557 * some broken BIOSes do not bother to write the high word of
558 * the length field if the length is smaller than 4GB. We
559 * detect and fix this by flagging sections below 4GB that
560 * appear to be larger than 4GB in size.
561 */
562 if ( (map->base_addr_high == 0) && (map->length_high != 0) )
563 {
564 if ( !e820_warn )
565 {
566 printk("WARNING: Buggy e820 map detected and fixed "
567 "(truncated length fields).\n");
568 e820_warn = 1;
569 }
570 map->length_high = 0;
571 }
573 e820_raw[e820_raw_nr].addr =
574 ((u64)map->base_addr_high << 32) | (u64)map->base_addr_low;
575 e820_raw[e820_raw_nr].size =
576 ((u64)map->length_high << 32) | (u64)map->length_low;
577 e820_raw[e820_raw_nr].type = map->type;
578 e820_raw_nr++;
580 bytes += map->size + 4;
581 }
582 }
583 else if ( mbi->flags & MBI_MEMLIMITS )
584 {
585 memmap_type = "Multiboot-e801";
586 e820_raw[0].addr = 0;
587 e820_raw[0].size = mbi->mem_lower << 10;
588 e820_raw[0].type = E820_RAM;
589 e820_raw[1].addr = 0x100000;
590 e820_raw[1].size = mbi->mem_upper << 10;
591 e820_raw[1].type = E820_RAM;
592 e820_raw_nr = 2;
593 }
594 else
595 {
596 EARLY_FAIL("Bootloader provided no memory information.\n");
597 }
599 /* Sanitise the raw E820 map to produce a final clean version. */
600 max_page = init_e820(memmap_type, e820_raw, &e820_raw_nr);
602 #ifdef CONFIG_X86_64
603 /*
604 * On x86/64 we are able to account for the allocation bitmap
605 * (allocated in common/page_alloc.c:init_boot_allocator()) stealing
606 * from the Xen heap. Here we make the Xen heap appropriately larger.
607 */
608 opt_xenheap_megabytes += (max_page / 8) >> 20;
609 #endif
611 /*
612 * Since there are some stubs getting built on the stacks which use
613 * direct calls/jumps, the heap must be confined to the lower 2G so
614 * that those branches can reach their targets.
615 */
616 if ( opt_xenheap_megabytes > 2048 )
617 opt_xenheap_megabytes = 2048;
619 /* Create a temporary copy of the E820 map. */
620 memcpy(&boot_e820, &e820, sizeof(e820));
622 /* Early kexec reservation (explicit static start address). */
623 kexec_reserve_area(&boot_e820);
625 /*
626 * Iterate backwards over all superpage-aligned RAM regions.
627 *
628 * We require superpage alignment because the boot allocator is not yet
629 * initialised. Hence we can only map superpages in the address range
630 * 0 to BOOTSTRAP_DIRECTMAP_END, as this is guaranteed not to require
631 * dynamic allocation of pagetables.
632 *
633 * As well as mapping superpages in that range, in preparation for
634 * initialising the boot allocator, we also look for a region to which
635 * we can relocate the dom0 kernel and other multiboot modules. Also, on
636 * x86/64, we relocate Xen to higher memory.
637 */
638 modules_length = mod[mbi->mods_count-1].mod_end - mod[0].mod_start;
639 for ( i = boot_e820.nr_map-1; i >= 0; i-- )
640 {
641 uint64_t s, e, mask = (1UL << L2_PAGETABLE_SHIFT) - 1;
643 /* Superpage-aligned chunks from 16MB to BOOTSTRAP_DIRECTMAP_END. */
644 s = (boot_e820.map[i].addr + mask) & ~mask;
645 e = (boot_e820.map[i].addr + boot_e820.map[i].size) & ~mask;
646 s = max_t(uint64_t, s, 16 << 20);
647 e = min_t(uint64_t, e, BOOTSTRAP_DIRECTMAP_END);
648 if ( (boot_e820.map[i].type != E820_RAM) || (s >= e) )
649 continue;
651 /* Map the chunk. No memory will need to be allocated to do this. */
652 map_pages_to_xen(
653 (unsigned long)maddr_to_bootstrap_virt(s),
654 s >> PAGE_SHIFT, (e-s) >> PAGE_SHIFT, PAGE_HYPERVISOR);
656 #if defined(CONFIG_X86_64)
657 /* Is the region suitable for relocating Xen? */
658 if ( !xen_phys_start && (((e-s) >> 20) >= opt_xenheap_megabytes) )
659 {
660 extern l2_pgentry_t l2_xenmap[];
661 l4_pgentry_t *pl4e;
662 l3_pgentry_t *pl3e;
663 l2_pgentry_t *pl2e;
664 int i, j, k;
666 /* Select relocation address. */
667 e = (e - (opt_xenheap_megabytes << 20)) & ~mask;
668 xen_phys_start = e;
669 bootsym(trampoline_xen_phys_start) = e;
671 /*
672 * Perform relocation to new physical address.
673 * Before doing so we must sync static/global data with main memory
674 * with a barrier(). After this we must *not* modify static/global
675 * data until after we have switched to the relocated pagetables!
676 */
677 barrier();
678 move_memory(e, 0, __pa(&_end) - xen_phys_start);
680 /* Poison low 1MB to detect stray pointers to physical 0-1MB. */
681 memset(maddr_to_bootstrap_virt(e), 0x55, 1U<<20);
683 /* Walk initial pagetables, relocating page directory entries. */
684 pl4e = __va(__pa(idle_pg_table));
685 for ( i = 0 ; i < L4_PAGETABLE_ENTRIES; i++, pl4e++ )
686 {
687 if ( !(l4e_get_flags(*pl4e) & _PAGE_PRESENT) )
688 continue;
689 *pl4e = l4e_from_intpte(l4e_get_intpte(*pl4e) +
690 xen_phys_start);
691 pl3e = l4e_to_l3e(*pl4e);
692 for ( j = 0; j < L3_PAGETABLE_ENTRIES; j++, pl3e++ )
693 {
694 /* Not present, 1GB mapping, or already relocated? */
695 if ( !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) ||
696 (l3e_get_flags(*pl3e) & _PAGE_PSE) ||
697 (l3e_get_pfn(*pl3e) > 0x1000) )
698 continue;
699 *pl3e = l3e_from_intpte(l3e_get_intpte(*pl3e) +
700 xen_phys_start);
701 pl2e = l3e_to_l2e(*pl3e);
702 for ( k = 0; k < L2_PAGETABLE_ENTRIES; k++, pl2e++ )
703 {
704 /* Not present, PSE, or already relocated? */
705 if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ||
706 (l2e_get_flags(*pl2e) & _PAGE_PSE) ||
707 (l2e_get_pfn(*pl2e) > 0x1000) )
708 continue;
709 *pl2e = l2e_from_intpte(l2e_get_intpte(*pl2e) +
710 xen_phys_start);
711 }
712 }
713 }
715 /* The only data mappings to be relocated are in the Xen area. */
716 pl2e = __va(__pa(l2_xenmap));
717 *pl2e++ = l2e_from_pfn(xen_phys_start >> PAGE_SHIFT,
718 PAGE_HYPERVISOR | _PAGE_PSE);
719 for ( i = 1; i < L2_PAGETABLE_ENTRIES; i++, pl2e++ )
720 {
721 if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) )
722 continue;
723 *pl2e = l2e_from_intpte(l2e_get_intpte(*pl2e) +
724 xen_phys_start);
725 }
727 /* Re-sync the stack and then switch to relocated pagetables. */
728 asm volatile (
729 "rep movsb ; " /* re-sync the stack */
730 "movq %%cr4,%%rsi ; "
731 "andb $0x7f,%%sil ; "
732 "movq %%rsi,%%cr4 ; " /* CR4.PGE == 0 */
733 "movq %0,%%cr3 ; " /* CR3 == new pagetables */
734 "orb $0x80,%%sil ; "
735 "movq %%rsi,%%cr4 " /* CR4.PGE == 1 */
736 : : "r" (__pa(idle_pg_table)), "S" (cpu0_stack),
737 "D" (__va(__pa(cpu0_stack))), "c" (STACK_SIZE) : "memory" );
738 }
739 #endif
741 /* Is the region suitable for relocating the multiboot modules? */
742 if ( !initial_images_start && (s < e) && ((e-s) >= modules_length) )
743 {
744 initial_images_end = e;
745 e = (e - modules_length) & PAGE_MASK;
746 initial_images_start = e;
747 move_memory(initial_images_start,
748 mod[0].mod_start, mod[mbi->mods_count-1].mod_end);
749 }
751 if ( !kexec_crash_area.start && (s < e) &&
752 ((e-s) >= kexec_crash_area.size) )
753 {
754 e = (e - kexec_crash_area.size) & PAGE_MASK;
755 kexec_crash_area.start = e;
756 }
757 }
759 if ( !initial_images_start )
760 EARLY_FAIL("Not enough memory to relocate the dom0 kernel image.\n");
761 reserve_e820_ram(&boot_e820, initial_images_start, initial_images_end);
763 /* Initialise Xen heap and boot heap. */
764 xenheap_phys_start = init_boot_allocator(__pa(&_end));
765 xenheap_phys_end = opt_xenheap_megabytes << 20;
766 #if defined(CONFIG_X86_64)
767 if ( !xen_phys_start )
768 EARLY_FAIL("Not enough memory to relocate Xen.\n");
769 xenheap_phys_end += xen_phys_start;
770 reserve_e820_ram(&boot_e820, xen_phys_start,
771 xen_phys_start + (opt_xenheap_megabytes<<20));
772 #endif
774 /* Late kexec reservation (dynamic start address). */
775 kexec_reserve_area(&boot_e820);
777 /*
778 * With the boot allocator now initialised, we can walk every RAM region
779 * and map it in its entirety (on x86/64, at least) and notify it to the
780 * boot allocator.
781 */
782 for ( i = 0; i < boot_e820.nr_map; i++ )
783 {
784 uint64_t s, e, map_s, map_e, mask = PAGE_SIZE - 1;
786 /* Only page alignment required now. */
787 s = (boot_e820.map[i].addr + mask) & ~mask;
788 e = (boot_e820.map[i].addr + boot_e820.map[i].size) & ~mask;
789 #if defined(CONFIG_X86_32)
790 s = max_t(uint64_t, s, xenheap_phys_end);
791 #else
792 s = max_t(uint64_t, s, 1<<20);
793 #endif
794 if ( (boot_e820.map[i].type != E820_RAM) || (s >= e) )
795 continue;
797 /* Need to create mappings above 16MB. */
798 map_s = max_t(uint64_t, s, 16<<20);
799 map_e = e;
800 #if defined(CONFIG_X86_32) /* mappings are truncated on x86_32 */
801 map_e = min_t(uint64_t, map_e, BOOTSTRAP_DIRECTMAP_END);
802 #endif
804 /* Pass mapped memory to allocator /before/ creating new mappings. */
805 init_boot_pages(s, min_t(uint64_t, map_s, e));
807 /* Create new mappings /before/ passing memory to the allocator. */
808 if ( map_s < map_e )
809 map_pages_to_xen(
810 (unsigned long)maddr_to_bootstrap_virt(map_s),
811 map_s >> PAGE_SHIFT, (map_e-map_s) >> PAGE_SHIFT,
812 PAGE_HYPERVISOR);
814 /* Pass remainder of this memory chunk to the allocator. */
815 init_boot_pages(map_s, e);
816 }
818 memguard_init();
820 nr_pages = 0;
821 for ( i = 0; i < e820.nr_map; i++ )
822 if ( e820.map[i].type == E820_RAM )
823 nr_pages += e820.map[i].size >> PAGE_SHIFT;
824 printk("System RAM: %luMB (%lukB)\n",
825 nr_pages >> (20 - PAGE_SHIFT),
826 nr_pages << (PAGE_SHIFT - 10));
827 total_pages = nr_pages;
829 /* Sanity check for unwanted bloat of certain hypercall structures. */
830 BUILD_BUG_ON(sizeof(((struct xen_platform_op *)0)->u) !=
831 sizeof(((struct xen_platform_op *)0)->u.pad));
832 BUILD_BUG_ON(sizeof(((struct xen_domctl *)0)->u) !=
833 sizeof(((struct xen_domctl *)0)->u.pad));
834 BUILD_BUG_ON(sizeof(((struct xen_sysctl *)0)->u) !=
835 sizeof(((struct xen_sysctl *)0)->u.pad));
837 BUILD_BUG_ON(sizeof(start_info_t) > PAGE_SIZE);
838 BUILD_BUG_ON(sizeof(shared_info_t) > PAGE_SIZE);
839 BUILD_BUG_ON(sizeof(struct vcpu_info) != 64);
841 #ifdef CONFIG_COMPAT
842 BUILD_BUG_ON(sizeof(((struct compat_platform_op *)0)->u) !=
843 sizeof(((struct compat_platform_op *)0)->u.pad));
844 BUILD_BUG_ON(sizeof(start_info_compat_t) > PAGE_SIZE);
845 BUILD_BUG_ON(sizeof(struct compat_vcpu_info) != 64);
846 #endif
848 /* Check definitions in public headers match internal defs. */
849 BUILD_BUG_ON(__HYPERVISOR_VIRT_START != HYPERVISOR_VIRT_START);
850 #ifdef HYPERVISOR_VIRT_END
851 BUILD_BUG_ON(__HYPERVISOR_VIRT_END != HYPERVISOR_VIRT_END);
852 #endif
853 BUILD_BUG_ON(MACH2PHYS_VIRT_START != RO_MPT_VIRT_START);
854 BUILD_BUG_ON(MACH2PHYS_VIRT_END != RO_MPT_VIRT_END);
856 init_frametable();
858 acpi_boot_table_init();
860 acpi_numa_init();
862 numa_initmem_init(0, max_page);
864 /* Initialise the Xen heap, skipping RAM holes. */
865 init_xenheap_pages(xenheap_phys_start, xenheap_phys_end);
866 nr_pages = (xenheap_phys_end - xenheap_phys_start) >> PAGE_SHIFT;
867 #ifdef __x86_64__
868 init_xenheap_pages(xen_phys_start, __pa(&_start));
869 nr_pages += (__pa(&_start) - xen_phys_start) >> PAGE_SHIFT;
870 vesa_init();
871 #endif
872 xenheap_phys_start = xen_phys_start;
873 printk("Xen heap: %luMB (%lukB)\n",
874 nr_pages >> (20 - PAGE_SHIFT),
875 nr_pages << (PAGE_SHIFT - 10));
877 end_boot_allocator();
879 early_boot = 0;
881 softirq_init();
883 early_cpu_init();
885 paging_init();
887 tboot_probe();
889 /* Unmap the first page of CPU0's stack. */
890 memguard_guard_stack(cpu0_stack);
892 open_softirq(NEW_TLBFLUSH_CLOCK_PERIOD_SOFTIRQ, new_tlbflush_clock_period);
894 if ( opt_watchdog )
895 nmi_watchdog = NMI_LOCAL_APIC;
897 sort_exception_tables();
899 find_smp_config();
901 dmi_scan_machine();
903 generic_apic_probe();
905 if ( x2apic_is_available() )
906 enable_x2apic();
908 acpi_boot_init();
910 init_cpu_to_node();
912 if ( smp_found_config )
913 get_smp_config();
915 #ifdef CONFIG_X86_64
916 /* Low mappings were only needed for some BIOS table parsing. */
917 zap_low_mappings();
918 #endif
920 init_apic_mappings();
922 init_IRQ();
924 percpu_init_areas();
926 xsm_init(&initrdidx, mbi, initial_images_start);
928 init_idle_domain();
930 trap_init();
932 rcu_init();
934 timer_init();
936 early_time_init();
938 arch_init_memory();
940 identify_cpu(&boot_cpu_data);
941 if ( cpu_has_fxsr )
942 set_in_cr4(X86_CR4_OSFXSR);
943 if ( cpu_has_xmm )
944 set_in_cr4(X86_CR4_OSXMMEXCPT);
946 local_irq_enable();
948 #ifdef CONFIG_X86_64
949 vesa_mtrr_init();
950 #endif
952 if ( opt_nosmp )
953 max_cpus = 0;
955 smp_prepare_cpus(max_cpus);
957 spin_debug_enable();
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 for_each_present_cpu ( i )
972 {
973 if ( num_online_cpus() >= max_cpus )
974 break;
975 if ( !cpu_online(i) )
976 {
977 rcu_online_cpu(i);
978 __cpu_up(i);
979 }
981 /* Set up cpu_to_node[]. */
982 srat_detect_node(i);
983 /* Set up node_to_cpumask based on cpu_to_node[]. */
984 numa_add_cpu(i);
985 }
987 printk("Brought up %ld CPUs\n", (long)num_online_cpus());
988 smp_cpus_done(max_cpus);
990 initialise_gdb(); /* could be moved earlier */
992 do_initcalls();
994 if ( opt_watchdog )
995 watchdog_enable();
997 /* Create initial domain 0. */
998 dom0 = domain_create(0, 0, DOM0_SSIDREF);
999 if ( (dom0 == NULL) || (alloc_vcpu(dom0, 0, 0) == NULL) )
1000 panic("Error creating domain 0\n");
1002 dom0->is_privileged = 1;
1003 dom0->target = NULL;
1005 /* Grab the DOM0 command line. */
1006 cmdline = (char *)(mod[0].string ? __va(mod[0].string) : NULL);
1007 if ( (cmdline != NULL) || (kextra != NULL) )
1009 static char dom0_cmdline[MAX_GUEST_CMDLINE];
1011 cmdline = cmdline_cook(cmdline);
1012 safe_strcpy(dom0_cmdline, cmdline);
1014 if ( kextra != NULL )
1015 /* kextra always includes exactly one leading space. */
1016 safe_strcat(dom0_cmdline, kextra);
1018 /* Append any extra parameters. */
1019 if ( skip_ioapic_setup && !strstr(dom0_cmdline, "noapic") )
1020 safe_strcat(dom0_cmdline, " noapic");
1021 if ( acpi_skip_timer_override &&
1022 !strstr(dom0_cmdline, "acpi_skip_timer_override") )
1023 safe_strcat(dom0_cmdline, " acpi_skip_timer_override");
1024 if ( (strlen(acpi_param) == 0) && acpi_disabled )
1026 printk("ACPI is disabled, notifying Domain 0 (acpi=off)\n");
1027 safe_strcpy(acpi_param, "off");
1029 if ( (strlen(acpi_param) != 0) && !strstr(dom0_cmdline, "acpi=") )
1031 safe_strcat(dom0_cmdline, " acpi=");
1032 safe_strcat(dom0_cmdline, acpi_param);
1035 cmdline = dom0_cmdline;
1038 if ( (initrdidx > 0) && (initrdidx < mbi->mods_count) )
1040 _initrd_start = initial_images_start +
1041 (mod[initrdidx].mod_start - mod[0].mod_start);
1042 _initrd_len = mod[initrdidx].mod_end - mod[initrdidx].mod_start;
1045 if ( xen_cpuidle )
1046 xen_processor_pmbits |= XEN_PROCESSOR_PM_CX;
1048 /*
1049 * We're going to setup domain0 using the module(s) that we stashed safely
1050 * above our heap. The second module, if present, is an initrd ramdisk.
1051 */
1052 if ( construct_dom0(dom0,
1053 initial_images_start,
1054 mod[0].mod_end-mod[0].mod_start,
1055 _initrd_start,
1056 _initrd_len,
1057 cmdline) != 0)
1058 panic("Could not set up DOM0 guest OS\n");
1060 /* Scrub RAM that is still free and so may go to an unprivileged domain. */
1061 scrub_heap_pages();
1063 init_trace_bufs();
1065 console_endboot();
1067 /* Hide UART from DOM0 if we're using it */
1068 serial_endboot();
1070 domain_unpause_by_systemcontroller(dom0);
1072 reset_stack_and_jump(init_done);
1075 void arch_get_xen_caps(xen_capabilities_info_t *info)
1077 /* Interface name is always xen-3.0-* for Xen-3.x. */
1078 int major = 3, minor = 0;
1079 char s[32];
1081 (*info)[0] = '\0';
1083 #if defined(CONFIG_X86_32)
1085 snprintf(s, sizeof(s), "xen-%d.%d-x86_32p ", major, minor);
1086 safe_strcat(*info, s);
1087 if ( hvm_enabled )
1089 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32 ", major, minor);
1090 safe_strcat(*info, s);
1091 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32p ", major, minor);
1092 safe_strcat(*info, s);
1095 #elif defined(CONFIG_X86_64)
1097 snprintf(s, sizeof(s), "xen-%d.%d-x86_64 ", major, minor);
1098 safe_strcat(*info, s);
1099 #ifdef CONFIG_COMPAT
1100 snprintf(s, sizeof(s), "xen-%d.%d-x86_32p ", major, minor);
1101 safe_strcat(*info, s);
1102 #endif
1103 if ( hvm_enabled )
1105 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32 ", major, minor);
1106 safe_strcat(*info, s);
1107 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32p ", major, minor);
1108 safe_strcat(*info, s);
1109 snprintf(s, sizeof(s), "hvm-%d.%d-x86_64 ", major, minor);
1110 safe_strcat(*info, s);
1113 #endif
1116 int xen_in_range(paddr_t start, paddr_t end)
1118 start = max_t(paddr_t, start, xenheap_phys_start);
1119 end = min_t(paddr_t, end, xenheap_phys_end);
1121 return start < end;
1124 /*
1125 * Local variables:
1126 * mode: C
1127 * c-set-style: "BSD"
1128 * c-basic-offset: 4
1129 * tab-width: 4
1130 * indent-tabs-mode: nil
1131 * End:
1132 */