ia64/xen-unstable

view xen/arch/x86/setup.c @ 19082:f0b46b3a9efd

x86_64, vt-d: Do not include pages 'in xen range' which may be freed
to domain heap.

Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Sat Jan 24 09:24:46 2009 +0000 (2009-01-24)
parents 9b0289a165eb
children 3449d8e50fb9
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 int __init bzimage_headroom(char *image_start, unsigned long image_length);
44 #if defined(CONFIG_X86_64)
45 #define BOOTSTRAP_DIRECTMAP_END (1UL << 32) /* 4GB */
46 #define maddr_to_bootstrap_virt(m) maddr_to_virt(m)
47 #else
48 #define BOOTSTRAP_DIRECTMAP_END (1UL << 30) /* 1GB */
49 #define maddr_to_bootstrap_virt(m) ((void *)(long)(m))
50 #endif
52 extern void generic_apic_probe(void);
53 extern void numa_initmem_init(unsigned long start_pfn, unsigned long end_pfn);
55 extern u16 boot_edid_caps;
56 extern u8 boot_edid_info[128];
57 extern struct boot_video_info boot_vid_info;
59 /* opt_nosmp: If true, secondary processors are ignored. */
60 static int opt_nosmp = 0;
61 boolean_param("nosmp", opt_nosmp);
63 /* maxcpus: maximum number of CPUs to activate. */
64 static unsigned int max_cpus = NR_CPUS;
65 integer_param("maxcpus", max_cpus);
67 /* opt_watchdog: If true, run a watchdog NMI on each processor. */
68 static int opt_watchdog = 0;
69 boolean_param("watchdog", opt_watchdog);
71 /* **** Linux config option: propagated to domain0. */
72 /* "acpi=off": Sisables both ACPI table parsing and interpreter. */
73 /* "acpi=force": Override the disable blacklist. */
74 /* "acpi=strict": Disables out-of-spec workarounds. */
75 /* "acpi=ht": Limit ACPI just to boot-time to enable HT. */
76 /* "acpi=noirq": Disables ACPI interrupt routing. */
77 static void parse_acpi_param(char *s);
78 custom_param("acpi", parse_acpi_param);
80 /* **** Linux config option: propagated to domain0. */
81 /* acpi_skip_timer_override: Skip IRQ0 overrides. */
82 extern int acpi_skip_timer_override;
83 boolean_param("acpi_skip_timer_override", acpi_skip_timer_override);
85 /* **** Linux config option: propagated to domain0. */
86 /* noapic: Disable IOAPIC setup. */
87 extern int skip_ioapic_setup;
88 boolean_param("noapic", skip_ioapic_setup);
90 /* **** Linux config option: propagated to domain0. */
91 /* xen_cpuidle: xen control cstate. */
92 /*static*/ int xen_cpuidle;
93 boolean_param("cpuidle", xen_cpuidle);
95 int early_boot = 1;
97 cpumask_t cpu_present_map;
99 unsigned long xen_phys_start;
101 #ifdef CONFIG_X86_32
102 /* Limits of Xen heap, used to initialise the allocator. */
103 unsigned long xenheap_initial_phys_start, xenheap_phys_end;
104 #endif
106 extern void arch_init_memory(void);
107 extern void init_IRQ(void);
108 extern void early_time_init(void);
109 extern void early_cpu_init(void);
110 extern void vesa_init(void);
111 extern void vesa_mtrr_init(void);
113 DEFINE_PER_CPU(struct desc_struct *, gdt_table) = boot_cpu_gdt_table;
114 #ifdef CONFIG_COMPAT
115 DEFINE_PER_CPU(struct desc_struct *, compat_gdt_table)
116 = boot_cpu_compat_gdt_table;
117 #endif
119 struct tss_struct init_tss[NR_CPUS];
121 char __attribute__ ((__section__(".bss.stack_aligned"))) cpu0_stack[STACK_SIZE];
123 struct cpuinfo_x86 boot_cpu_data = { 0, 0, 0, 0, -1 };
125 unsigned long mmu_cr4_features = X86_CR4_PSE | X86_CR4_PGE | X86_CR4_PAE;
126 EXPORT_SYMBOL(mmu_cr4_features);
128 int acpi_disabled;
130 int acpi_force;
131 char acpi_param[10] = "";
132 static void __init parse_acpi_param(char *s)
133 {
134 /* Save the parameter so it can be propagated to domain0. */
135 safe_strcpy(acpi_param, s);
137 /* Interpret the parameter for use within Xen. */
138 if ( !strcmp(s, "off") )
139 {
140 disable_acpi();
141 }
142 else if ( !strcmp(s, "force") )
143 {
144 acpi_force = 1;
145 acpi_ht = 1;
146 acpi_disabled = 0;
147 }
148 else if ( !strcmp(s, "strict") )
149 {
150 acpi_strict = 1;
151 }
152 else if ( !strcmp(s, "ht") )
153 {
154 if ( !acpi_force )
155 disable_acpi();
156 acpi_ht = 1;
157 }
158 else if ( !strcmp(s, "noirq") )
159 {
160 acpi_noirq_set();
161 }
162 }
164 static void __init do_initcalls(void)
165 {
166 initcall_t *call;
167 for ( call = &__initcall_start; call < &__initcall_end; call++ )
168 (*call)();
169 }
171 #define EARLY_FAIL(f, a...) do { \
172 printk( f , ## a ); \
173 for ( ; ; ) halt(); \
174 } while (0)
176 static unsigned long __initdata initial_images_base;
177 static unsigned long __initdata initial_images_start;
178 static unsigned long __initdata initial_images_end;
180 unsigned long __init initial_images_nrpages(void)
181 {
182 ASSERT(!(initial_images_base & ~PAGE_MASK));
183 ASSERT(!(initial_images_end & ~PAGE_MASK));
184 return ((initial_images_end >> PAGE_SHIFT) -
185 (initial_images_base >> PAGE_SHIFT));
186 }
188 void __init discard_initial_images(void)
189 {
190 init_domheap_pages(initial_images_base, initial_images_end);
191 }
193 extern char __per_cpu_start[], __per_cpu_data_end[], __per_cpu_end[];
195 static void __init percpu_init_areas(void)
196 {
197 unsigned int i, data_size = __per_cpu_data_end - __per_cpu_start;
198 unsigned int first_unused;
200 BUG_ON(data_size > PERCPU_SIZE);
202 /* Initialise per-cpu data area for all possible secondary CPUs. */
203 for ( i = 1; (i < NR_CPUS) && cpu_possible(i); i++ )
204 memcpy(__per_cpu_start + (i << PERCPU_SHIFT),
205 __per_cpu_start,
206 data_size);
207 first_unused = i;
209 /* Check that there are no holes in cpu_possible_map. */
210 for ( ; i < NR_CPUS; i++ )
211 BUG_ON(cpu_possible(i));
213 #ifndef MEMORY_GUARD
214 init_xenheap_pages(__pa(__per_cpu_start) + (first_unused << PERCPU_SHIFT),
215 __pa(__per_cpu_end));
216 #endif
217 memguard_guard_range(&__per_cpu_start[first_unused << PERCPU_SHIFT],
218 (NR_CPUS - first_unused) << PERCPU_SHIFT);
219 #if defined(CONFIG_X86_64)
220 /* Also zap the mapping in the 1:1 area. */
221 memguard_guard_range(__va(__pa(__per_cpu_start)) +
222 (first_unused << PERCPU_SHIFT),
223 (NR_CPUS - first_unused) << PERCPU_SHIFT);
224 #endif
225 }
227 static void __init init_idle_domain(void)
228 {
229 struct domain *idle_domain;
231 /* Domain creation requires that scheduler structures are initialised. */
232 scheduler_init();
234 idle_domain = domain_create(IDLE_DOMAIN_ID, 0, 0);
235 if ( (idle_domain == NULL) || (alloc_vcpu(idle_domain, 0, 0) == NULL) )
236 BUG();
238 set_current(idle_domain->vcpu[0]);
239 idle_vcpu[0] = this_cpu(curr_vcpu) = current;
241 setup_idle_pagetable();
242 }
244 static void __init srat_detect_node(int cpu)
245 {
246 unsigned node;
247 u32 apicid = x86_cpu_to_apicid[cpu];
249 node = apicid_to_node[apicid];
250 if ( node == NUMA_NO_NODE )
251 node = 0;
252 numa_set_node(cpu, node);
254 if ( acpi_numa > 0 )
255 printk(KERN_INFO "CPU %d APIC %d -> Node %d\n", cpu, apicid, node);
256 }
258 /*
259 * Ensure a given physical memory range is present in the bootstrap mappings.
260 * Use superpage mappings to ensure that pagetable memory needn't be allocated.
261 */
262 static void __init bootstrap_map(unsigned long start, unsigned long end)
263 {
264 unsigned long mask = (1UL << L2_PAGETABLE_SHIFT) - 1;
265 start = max_t(unsigned long, start & ~mask, 16UL << 20);
266 end = (end + mask) & ~mask;
267 if ( start >= end )
268 return;
269 if ( end > BOOTSTRAP_DIRECTMAP_END )
270 panic("Cannot access memory beyond end of "
271 "bootstrap direct-map area\n");
272 map_pages_to_xen(
273 (unsigned long)maddr_to_bootstrap_virt(start),
274 start >> PAGE_SHIFT, (end-start) >> PAGE_SHIFT, PAGE_HYPERVISOR);
275 }
277 static void __init move_memory(
278 unsigned long dst, unsigned long src_start, unsigned long src_end)
279 {
280 bootstrap_map(src_start, src_end);
281 bootstrap_map(dst, dst + src_end - src_start);
282 memmove(maddr_to_bootstrap_virt(dst),
283 maddr_to_bootstrap_virt(src_start),
284 src_end - src_start);
285 }
287 /* A temporary copy of the e820 map that we can mess with during bootstrap. */
288 static struct e820map __initdata boot_e820;
290 struct boot_video_info {
291 u8 orig_x; /* 0x00 */
292 u8 orig_y; /* 0x01 */
293 u8 orig_video_mode; /* 0x02 */
294 u8 orig_video_cols; /* 0x03 */
295 u8 orig_video_lines; /* 0x04 */
296 u8 orig_video_isVGA; /* 0x05 */
297 u16 orig_video_points; /* 0x06 */
299 /* VESA graphic mode -- linear frame buffer */
300 u32 capabilities; /* 0x08 */
301 u16 lfb_linelength; /* 0x0c */
302 u16 lfb_width; /* 0x0e */
303 u16 lfb_height; /* 0x10 */
304 u16 lfb_depth; /* 0x12 */
305 u32 lfb_base; /* 0x14 */
306 u32 lfb_size; /* 0x18 */
307 u8 red_size; /* 0x1c */
308 u8 red_pos; /* 0x1d */
309 u8 green_size; /* 0x1e */
310 u8 green_pos; /* 0x1f */
311 u8 blue_size; /* 0x20 */
312 u8 blue_pos; /* 0x21 */
313 u8 rsvd_size; /* 0x22 */
314 u8 rsvd_pos; /* 0x23 */
315 u16 vesapm_seg; /* 0x24 */
316 u16 vesapm_off; /* 0x26 */
317 u16 vesa_attrib; /* 0x28 */
318 };
320 static void __init parse_video_info(void)
321 {
322 struct boot_video_info *bvi = &bootsym(boot_vid_info);
324 if ( (bvi->orig_video_isVGA == 1) && (bvi->orig_video_mode == 3) )
325 {
326 vga_console_info.video_type = XEN_VGATYPE_TEXT_MODE_3;
327 vga_console_info.u.text_mode_3.font_height = bvi->orig_video_points;
328 vga_console_info.u.text_mode_3.cursor_x = bvi->orig_x;
329 vga_console_info.u.text_mode_3.cursor_y = bvi->orig_y;
330 vga_console_info.u.text_mode_3.rows = bvi->orig_video_lines;
331 vga_console_info.u.text_mode_3.columns = bvi->orig_video_cols;
332 }
333 else if ( bvi->orig_video_isVGA == 0x23 )
334 {
335 vga_console_info.video_type = XEN_VGATYPE_VESA_LFB;
336 vga_console_info.u.vesa_lfb.width = bvi->lfb_width;
337 vga_console_info.u.vesa_lfb.height = bvi->lfb_height;
338 vga_console_info.u.vesa_lfb.bytes_per_line = bvi->lfb_linelength;
339 vga_console_info.u.vesa_lfb.bits_per_pixel = bvi->lfb_depth;
340 vga_console_info.u.vesa_lfb.lfb_base = bvi->lfb_base;
341 vga_console_info.u.vesa_lfb.lfb_size = bvi->lfb_size;
342 vga_console_info.u.vesa_lfb.red_pos = bvi->red_pos;
343 vga_console_info.u.vesa_lfb.red_size = bvi->red_size;
344 vga_console_info.u.vesa_lfb.green_pos = bvi->green_pos;
345 vga_console_info.u.vesa_lfb.green_size = bvi->green_size;
346 vga_console_info.u.vesa_lfb.blue_pos = bvi->blue_pos;
347 vga_console_info.u.vesa_lfb.blue_size = bvi->blue_size;
348 vga_console_info.u.vesa_lfb.rsvd_pos = bvi->rsvd_pos;
349 vga_console_info.u.vesa_lfb.rsvd_size = bvi->rsvd_size;
350 vga_console_info.u.vesa_lfb.gbl_caps = bvi->capabilities;
351 vga_console_info.u.vesa_lfb.mode_attrs = bvi->vesa_attrib;
352 }
353 }
355 void __init kexec_reserve_area(struct e820map *e820)
356 {
357 unsigned long kdump_start = kexec_crash_area.start;
358 unsigned long kdump_size = kexec_crash_area.size;
359 static int is_reserved = 0;
361 kdump_size = (kdump_size + PAGE_SIZE - 1) & PAGE_MASK;
363 if ( (kdump_start == 0) || (kdump_size == 0) || is_reserved )
364 return;
366 is_reserved = 1;
368 if ( !reserve_e820_ram(e820, kdump_start, kdump_start + kdump_size) )
369 {
370 printk("Kdump: DISABLED (failed to reserve %luMB (%lukB) at 0x%lx)"
371 "\n", kdump_size >> 20, kdump_size >> 10, kdump_start);
372 kexec_crash_area.start = kexec_crash_area.size = 0;
373 }
374 else
375 {
376 printk("Kdump: %luMB (%lukB) at 0x%lx\n",
377 kdump_size >> 20, kdump_size >> 10, kdump_start);
378 }
379 }
381 void init_done(void)
382 {
383 extern char __init_begin[], __init_end[];
385 /* Free (or page-protect) the init areas. */
386 memset(__init_begin, 0xcc, __init_end - __init_begin); /* int3 poison */
387 #ifndef MEMORY_GUARD
388 init_xenheap_pages(__pa(__init_begin), __pa(__init_end));
389 #endif
390 memguard_guard_range(__init_begin, __init_end - __init_begin);
391 #if defined(CONFIG_X86_64)
392 /* Also zap the mapping in the 1:1 area. */
393 memguard_guard_range(__va(__pa(__init_begin)), __init_end - __init_begin);
394 #endif
395 printk("Freed %ldkB init memory.\n", (long)(__init_end-__init_begin)>>10);
397 startup_cpu_idle_loop();
398 }
400 static char * __init cmdline_cook(char *p)
401 {
402 p = p ? : "";
403 while ( *p == ' ' )
404 p++;
405 while ( (*p != ' ') && (*p != '\0') )
406 p++;
407 while ( *p == ' ' )
408 p++;
409 return p;
410 }
412 void __init __start_xen(unsigned long mbi_p)
413 {
414 char *memmap_type = NULL;
415 char *cmdline, *kextra;
416 unsigned long _initrd_start = 0, _initrd_len = 0;
417 unsigned int initrdidx = 1;
418 multiboot_info_t *mbi = __va(mbi_p);
419 module_t *mod = (module_t *)__va(mbi->mods_addr);
420 unsigned long nr_pages, modules_length, modules_headroom;
421 unsigned long allocator_bitmap_end;
422 int i, e820_warn = 0, bytes = 0;
423 struct ns16550_defaults ns16550 = {
424 .data_bits = 8,
425 .parity = 'n',
426 .stop_bits = 1
427 };
429 extern void early_page_fault(void);
430 set_intr_gate(TRAP_page_fault, &early_page_fault);
432 /* Parse the command-line options. */
433 cmdline = cmdline_cook((mbi->flags & MBI_CMDLINE) ?
434 __va(mbi->cmdline) : NULL);
435 if ( (kextra = strstr(cmdline, " -- ")) != NULL )
436 {
437 /*
438 * Options after ' -- ' separator belong to dom0.
439 * 1. Orphan dom0's options from Xen's command line.
440 * 2. Skip all but final leading space from dom0's options.
441 */
442 *kextra = '\0';
443 kextra += 3;
444 while ( kextra[1] == ' ' ) kextra++;
445 }
446 cmdline_parse(cmdline);
448 parse_video_info();
450 set_current((struct vcpu *)0xfffff000); /* debug sanity */
451 idle_vcpu[0] = current;
452 set_processor_id(0); /* needed early, for smp_processor_id() */
453 if ( cpu_has_efer )
454 rdmsrl(MSR_EFER, this_cpu(efer));
455 asm volatile ( "mov %%cr4,%0" : "=r" (this_cpu(cr4)) );
457 smp_prepare_boot_cpu();
459 /* We initialise the serial devices very early so we can get debugging. */
460 ns16550.io_base = 0x3f8;
461 ns16550.irq = 4;
462 ns16550_init(0, &ns16550);
463 ns16550.io_base = 0x2f8;
464 ns16550.irq = 3;
465 ns16550_init(1, &ns16550);
466 serial_init_preirq();
468 init_console();
470 printk("Command line: %s\n", cmdline);
472 printk("Video information:\n");
474 /* Print VGA display mode information. */
475 switch ( vga_console_info.video_type )
476 {
477 case XEN_VGATYPE_TEXT_MODE_3:
478 printk(" VGA is text mode %dx%d, font 8x%d\n",
479 vga_console_info.u.text_mode_3.columns,
480 vga_console_info.u.text_mode_3.rows,
481 vga_console_info.u.text_mode_3.font_height);
482 break;
483 case XEN_VGATYPE_VESA_LFB:
484 printk(" VGA is graphics mode %dx%d, %d bpp\n",
485 vga_console_info.u.vesa_lfb.width,
486 vga_console_info.u.vesa_lfb.height,
487 vga_console_info.u.vesa_lfb.bits_per_pixel);
488 break;
489 default:
490 printk(" No VGA detected\n");
491 break;
492 }
494 /* Print VBE/DDC EDID information. */
495 if ( bootsym(boot_edid_caps) != 0x1313 )
496 {
497 u16 caps = bootsym(boot_edid_caps);
498 printk(" VBE/DDC methods:%s%s%s; ",
499 (caps & 1) ? " V1" : "",
500 (caps & 2) ? " V2" : "",
501 !(caps & 3) ? " none" : "");
502 printk("EDID transfer time: %d seconds\n", caps >> 8);
503 if ( *(u32 *)bootsym(boot_edid_info) == 0x13131313 )
504 {
505 printk(" EDID info not retrieved because ");
506 if ( !(caps & 3) )
507 printk("no DDC retrieval method detected\n");
508 else if ( (caps >> 8) > 5 )
509 printk("takes longer than 5 seconds\n");
510 else
511 printk("of reasons unknown\n");
512 }
513 }
515 printk("Disc information:\n");
516 printk(" Found %d MBR signatures\n",
517 bootsym(boot_mbr_signature_nr));
518 printk(" Found %d EDD information structures\n",
519 bootsym(boot_edd_info_nr));
521 /* Check that we have at least one Multiboot module. */
522 if ( !(mbi->flags & MBI_MODULES) || (mbi->mods_count == 0) )
523 EARLY_FAIL("dom0 kernel not specified. "
524 "Check bootloader configuration.\n");
526 if ( ((unsigned long)cpu0_stack & (STACK_SIZE-1)) != 0 )
527 EARLY_FAIL("Misaligned CPU0 stack.\n");
529 if ( e820_raw_nr != 0 )
530 {
531 memmap_type = "Xen-e820";
532 }
533 else if ( bootsym(lowmem_kb) )
534 {
535 memmap_type = "Xen-e801";
536 e820_raw[0].addr = 0;
537 e820_raw[0].size = bootsym(lowmem_kb) << 10;
538 e820_raw[0].type = E820_RAM;
539 e820_raw[1].addr = 0x100000;
540 e820_raw[1].size = bootsym(highmem_kb) << 10;
541 e820_raw[1].type = E820_RAM;
542 e820_raw_nr = 2;
543 }
544 else if ( mbi->flags & MBI_MEMMAP )
545 {
546 memmap_type = "Multiboot-e820";
547 while ( (bytes < mbi->mmap_length) && (e820_raw_nr < E820MAX) )
548 {
549 memory_map_t *map = __va(mbi->mmap_addr + bytes);
551 /*
552 * This is a gross workaround for a BIOS bug. Some bootloaders do
553 * not write e820 map entries into pre-zeroed memory. This is
554 * okay if the BIOS fills in all fields of the map entry, but
555 * some broken BIOSes do not bother to write the high word of
556 * the length field if the length is smaller than 4GB. We
557 * detect and fix this by flagging sections below 4GB that
558 * appear to be larger than 4GB in size.
559 */
560 if ( (map->base_addr_high == 0) && (map->length_high != 0) )
561 {
562 if ( !e820_warn )
563 {
564 printk("WARNING: Buggy e820 map detected and fixed "
565 "(truncated length fields).\n");
566 e820_warn = 1;
567 }
568 map->length_high = 0;
569 }
571 e820_raw[e820_raw_nr].addr =
572 ((u64)map->base_addr_high << 32) | (u64)map->base_addr_low;
573 e820_raw[e820_raw_nr].size =
574 ((u64)map->length_high << 32) | (u64)map->length_low;
575 e820_raw[e820_raw_nr].type = map->type;
576 e820_raw_nr++;
578 bytes += map->size + 4;
579 }
580 }
581 else if ( mbi->flags & MBI_MEMLIMITS )
582 {
583 memmap_type = "Multiboot-e801";
584 e820_raw[0].addr = 0;
585 e820_raw[0].size = mbi->mem_lower << 10;
586 e820_raw[0].type = E820_RAM;
587 e820_raw[1].addr = 0x100000;
588 e820_raw[1].size = mbi->mem_upper << 10;
589 e820_raw[1].type = E820_RAM;
590 e820_raw_nr = 2;
591 }
592 else
593 {
594 EARLY_FAIL("Bootloader provided no memory information.\n");
595 }
597 /* Sanitise the raw E820 map to produce a final clean version. */
598 max_page = init_e820(memmap_type, e820_raw, &e820_raw_nr);
600 /* Create a temporary copy of the E820 map. */
601 memcpy(&boot_e820, &e820, sizeof(e820));
603 /* Early kexec reservation (explicit static start address). */
604 kexec_reserve_area(&boot_e820);
606 /*
607 * Iterate backwards over all superpage-aligned RAM regions.
608 *
609 * We require superpage alignment because the boot allocator is not yet
610 * initialised. Hence we can only map superpages in the address range
611 * 0 to BOOTSTRAP_DIRECTMAP_END, as this is guaranteed not to require
612 * dynamic allocation of pagetables.
613 *
614 * As well as mapping superpages in that range, in preparation for
615 * initialising the boot allocator, we also look for a region to which
616 * we can relocate the dom0 kernel and other multiboot modules. Also, on
617 * x86/64, we relocate Xen to higher memory.
618 */
619 modules_length = mod[mbi->mods_count-1].mod_end - mod[0].mod_start;
620 modules_headroom = bzimage_headroom(
621 (char *)(unsigned long)mod[0].mod_start,
622 (unsigned long)(mod[0].mod_end - mod[0].mod_start));
624 for ( i = boot_e820.nr_map-1; i >= 0; i-- )
625 {
626 uint64_t s, e, mask = (1UL << L2_PAGETABLE_SHIFT) - 1;
628 /* Superpage-aligned chunks from 16MB to BOOTSTRAP_DIRECTMAP_END. */
629 s = (boot_e820.map[i].addr + mask) & ~mask;
630 e = (boot_e820.map[i].addr + boot_e820.map[i].size) & ~mask;
631 s = max_t(uint64_t, s, 16 << 20);
632 e = min_t(uint64_t, e, BOOTSTRAP_DIRECTMAP_END);
633 if ( (boot_e820.map[i].type != E820_RAM) || (s >= e) )
634 continue;
636 /* Map the chunk. No memory will need to be allocated to do this. */
637 map_pages_to_xen(
638 (unsigned long)maddr_to_bootstrap_virt(s),
639 s >> PAGE_SHIFT, (e-s) >> PAGE_SHIFT, PAGE_HYPERVISOR);
641 #if defined(CONFIG_X86_64)
642 #define reloc_size ((__pa(&_end) + mask) & ~mask)
643 /* Is the region suitable for relocating Xen? */
644 if ( !xen_phys_start && ((e-s) >= reloc_size) )
645 {
646 extern l2_pgentry_t l2_xenmap[];
647 l4_pgentry_t *pl4e;
648 l3_pgentry_t *pl3e;
649 l2_pgentry_t *pl2e;
650 int i, j, k;
652 /* Select relocation address. */
653 e -= reloc_size;
654 xen_phys_start = e;
655 bootsym(trampoline_xen_phys_start) = e;
657 /*
658 * Perform relocation to new physical address.
659 * Before doing so we must sync static/global data with main memory
660 * with a barrier(). After this we must *not* modify static/global
661 * data until after we have switched to the relocated pagetables!
662 */
663 barrier();
664 move_memory(e, 0, __pa(&_end) - xen_phys_start);
666 /* Poison low 1MB to detect stray pointers to physical 0-1MB. */
667 memset(maddr_to_bootstrap_virt(e), 0x55, 1U<<20);
669 /* Walk initial pagetables, relocating page directory entries. */
670 pl4e = __va(__pa(idle_pg_table));
671 for ( i = 0 ; i < L4_PAGETABLE_ENTRIES; i++, pl4e++ )
672 {
673 if ( !(l4e_get_flags(*pl4e) & _PAGE_PRESENT) )
674 continue;
675 *pl4e = l4e_from_intpte(l4e_get_intpte(*pl4e) +
676 xen_phys_start);
677 pl3e = l4e_to_l3e(*pl4e);
678 for ( j = 0; j < L3_PAGETABLE_ENTRIES; j++, pl3e++ )
679 {
680 /* Not present, 1GB mapping, or already relocated? */
681 if ( !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) ||
682 (l3e_get_flags(*pl3e) & _PAGE_PSE) ||
683 (l3e_get_pfn(*pl3e) > 0x1000) )
684 continue;
685 *pl3e = l3e_from_intpte(l3e_get_intpte(*pl3e) +
686 xen_phys_start);
687 pl2e = l3e_to_l2e(*pl3e);
688 for ( k = 0; k < L2_PAGETABLE_ENTRIES; k++, pl2e++ )
689 {
690 /* Not present, PSE, or already relocated? */
691 if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ||
692 (l2e_get_flags(*pl2e) & _PAGE_PSE) ||
693 (l2e_get_pfn(*pl2e) > 0x1000) )
694 continue;
695 *pl2e = l2e_from_intpte(l2e_get_intpte(*pl2e) +
696 xen_phys_start);
697 }
698 }
699 }
701 /* The only data mappings to be relocated are in the Xen area. */
702 pl2e = __va(__pa(l2_xenmap));
703 *pl2e++ = l2e_from_pfn(xen_phys_start >> PAGE_SHIFT,
704 PAGE_HYPERVISOR | _PAGE_PSE);
705 for ( i = 1; i < L2_PAGETABLE_ENTRIES; i++, pl2e++ )
706 {
707 if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) )
708 continue;
709 *pl2e = l2e_from_intpte(l2e_get_intpte(*pl2e) +
710 xen_phys_start);
711 }
713 /* Re-sync the stack and then switch to relocated pagetables. */
714 asm volatile (
715 "rep movsb ; " /* re-sync the stack */
716 "movq %%cr4,%%rsi ; "
717 "andb $0x7f,%%sil ; "
718 "movq %%rsi,%%cr4 ; " /* CR4.PGE == 0 */
719 "movq %0,%%cr3 ; " /* CR3 == new pagetables */
720 "orb $0x80,%%sil ; "
721 "movq %%rsi,%%cr4 " /* CR4.PGE == 1 */
722 : : "r" (__pa(idle_pg_table)), "S" (cpu0_stack),
723 "D" (__va(__pa(cpu0_stack))), "c" (STACK_SIZE) : "memory" );
724 }
725 #endif
727 /* Is the region suitable for relocating the multiboot modules? */
728 if ( !initial_images_start && (s < e) &&
729 ((e-s) >= (modules_length+modules_headroom)) )
730 {
731 initial_images_end = e;
732 e = (e - modules_length) & PAGE_MASK;
733 initial_images_start = e;
734 e -= modules_headroom;
735 initial_images_base = e;
736 move_memory(initial_images_start,
737 mod[0].mod_start, mod[mbi->mods_count-1].mod_end);
738 }
740 if ( !kexec_crash_area.start && (s < e) &&
741 ((e-s) >= kexec_crash_area.size) )
742 {
743 e = (e - kexec_crash_area.size) & PAGE_MASK;
744 kexec_crash_area.start = e;
745 }
746 }
748 if ( !initial_images_start )
749 EARLY_FAIL("Not enough memory to relocate the dom0 kernel image.\n");
750 reserve_e820_ram(&boot_e820, initial_images_base, initial_images_end);
752 /* Initialise boot heap. */
753 allocator_bitmap_end = init_boot_allocator(__pa(&_end));
754 #if defined(CONFIG_X86_32)
755 xenheap_initial_phys_start = allocator_bitmap_end;
756 xenheap_phys_end = DIRECTMAP_MBYTES << 20;
757 #else
758 if ( !xen_phys_start )
759 EARLY_FAIL("Not enough memory to relocate Xen.\n");
760 reserve_e820_ram(&boot_e820, __pa(&_start), allocator_bitmap_end);
761 #endif
763 /* Late kexec reservation (dynamic start address). */
764 kexec_reserve_area(&boot_e820);
766 /*
767 * With the boot allocator now initialised, we can walk every RAM region
768 * and map it in its entirety (on x86/64, at least) and notify it to the
769 * boot allocator.
770 */
771 for ( i = 0; i < boot_e820.nr_map; i++ )
772 {
773 uint64_t s, e, map_s, map_e, mask = PAGE_SIZE - 1;
775 /* Only page alignment required now. */
776 s = (boot_e820.map[i].addr + mask) & ~mask;
777 e = (boot_e820.map[i].addr + boot_e820.map[i].size) & ~mask;
778 #if defined(CONFIG_X86_32)
779 s = max_t(uint64_t, s, xenheap_phys_end);
780 #else
781 s = max_t(uint64_t, s, 1<<20);
782 #endif
783 if ( (boot_e820.map[i].type != E820_RAM) || (s >= e) )
784 continue;
786 /* Need to create mappings above 16MB. */
787 map_s = max_t(uint64_t, s, 16<<20);
788 map_e = e;
789 #if defined(CONFIG_X86_32) /* mappings are truncated on x86_32 */
790 map_e = min_t(uint64_t, map_e, BOOTSTRAP_DIRECTMAP_END);
791 #endif
793 /* Pass mapped memory to allocator /before/ creating new mappings. */
794 init_boot_pages(s, min_t(uint64_t, map_s, e));
796 /* Create new mappings /before/ passing memory to the allocator. */
797 if ( map_s < map_e )
798 map_pages_to_xen(
799 (unsigned long)maddr_to_bootstrap_virt(map_s),
800 map_s >> PAGE_SHIFT, (map_e-map_s) >> PAGE_SHIFT,
801 PAGE_HYPERVISOR);
803 /* Pass remainder of this memory chunk to the allocator. */
804 init_boot_pages(map_s, e);
805 }
807 memguard_init();
809 nr_pages = 0;
810 for ( i = 0; i < e820.nr_map; i++ )
811 if ( e820.map[i].type == E820_RAM )
812 nr_pages += e820.map[i].size >> PAGE_SHIFT;
813 printk("System RAM: %luMB (%lukB)\n",
814 nr_pages >> (20 - PAGE_SHIFT),
815 nr_pages << (PAGE_SHIFT - 10));
816 total_pages = nr_pages;
818 /* Sanity check for unwanted bloat of certain hypercall structures. */
819 BUILD_BUG_ON(sizeof(((struct xen_platform_op *)0)->u) !=
820 sizeof(((struct xen_platform_op *)0)->u.pad));
821 BUILD_BUG_ON(sizeof(((struct xen_domctl *)0)->u) !=
822 sizeof(((struct xen_domctl *)0)->u.pad));
823 BUILD_BUG_ON(sizeof(((struct xen_sysctl *)0)->u) !=
824 sizeof(((struct xen_sysctl *)0)->u.pad));
826 BUILD_BUG_ON(sizeof(start_info_t) > PAGE_SIZE);
827 BUILD_BUG_ON(sizeof(shared_info_t) > PAGE_SIZE);
828 BUILD_BUG_ON(sizeof(struct vcpu_info) != 64);
830 #ifdef CONFIG_COMPAT
831 BUILD_BUG_ON(sizeof(((struct compat_platform_op *)0)->u) !=
832 sizeof(((struct compat_platform_op *)0)->u.pad));
833 BUILD_BUG_ON(sizeof(start_info_compat_t) > PAGE_SIZE);
834 BUILD_BUG_ON(sizeof(struct compat_vcpu_info) != 64);
835 #endif
837 /* Check definitions in public headers match internal defs. */
838 BUILD_BUG_ON(__HYPERVISOR_VIRT_START != HYPERVISOR_VIRT_START);
839 #ifdef HYPERVISOR_VIRT_END
840 BUILD_BUG_ON(__HYPERVISOR_VIRT_END != HYPERVISOR_VIRT_END);
841 #endif
842 BUILD_BUG_ON(MACH2PHYS_VIRT_START != RO_MPT_VIRT_START);
843 BUILD_BUG_ON(MACH2PHYS_VIRT_END != RO_MPT_VIRT_END);
845 init_frametable();
847 acpi_boot_table_init();
849 acpi_numa_init();
851 numa_initmem_init(0, max_page);
853 #if defined(CONFIG_X86_32)
854 /* Initialise the Xen heap. */
855 init_xenheap_pages(xenheap_initial_phys_start, xenheap_phys_end);
856 nr_pages = (xenheap_phys_end - xenheap_initial_phys_start) >> PAGE_SHIFT;
857 printk("Xen heap: %luMB (%lukB)\n",
858 nr_pages >> (20 - PAGE_SHIFT),
859 nr_pages << (PAGE_SHIFT - 10));
860 #endif
862 end_boot_allocator();
863 early_boot = 0;
865 #if defined(CONFIG_X86_64)
866 vesa_init();
867 #endif
869 softirq_init();
871 early_cpu_init();
873 paging_init();
875 tboot_probe();
877 /* Unmap the first page of CPU0's stack. */
878 memguard_guard_stack(cpu0_stack);
880 open_softirq(NEW_TLBFLUSH_CLOCK_PERIOD_SOFTIRQ, new_tlbflush_clock_period);
882 if ( opt_watchdog )
883 nmi_watchdog = NMI_LOCAL_APIC;
885 sort_exception_tables();
887 find_smp_config();
889 dmi_scan_machine();
891 generic_apic_probe();
893 if ( x2apic_is_available() )
894 enable_x2apic();
896 acpi_boot_init();
898 init_cpu_to_node();
900 if ( smp_found_config )
901 get_smp_config();
903 #ifdef CONFIG_X86_64
904 /* Low mappings were only needed for some BIOS table parsing. */
905 zap_low_mappings();
906 #endif
908 init_apic_mappings();
910 init_IRQ();
912 percpu_init_areas();
914 xsm_init(&initrdidx, mbi, initial_images_start);
916 init_idle_domain();
918 trap_init();
920 rcu_init();
922 timer_init();
924 early_time_init();
926 arch_init_memory();
928 identify_cpu(&boot_cpu_data);
929 if ( cpu_has_fxsr )
930 set_in_cr4(X86_CR4_OSFXSR);
931 if ( cpu_has_xmm )
932 set_in_cr4(X86_CR4_OSXMMEXCPT);
934 local_irq_enable();
936 #ifdef CONFIG_X86_64
937 vesa_mtrr_init();
938 #endif
940 if ( opt_nosmp )
941 max_cpus = 0;
943 smp_prepare_cpus(max_cpus);
945 spin_debug_enable();
947 /*
948 * Initialise higher-level timer functions. We do this fairly late
949 * (post-SMP) because the time bases and scale factors need to be updated
950 * regularly, and SMP initialisation can cause a long delay with
951 * interrupts not yet enabled.
952 */
953 init_xen_time();
955 initialize_keytable();
957 serial_init_postirq();
959 for_each_present_cpu ( i )
960 {
961 if ( num_online_cpus() >= max_cpus )
962 break;
963 if ( !cpu_online(i) )
964 {
965 rcu_online_cpu(i);
966 __cpu_up(i);
967 }
969 /* Set up cpu_to_node[]. */
970 srat_detect_node(i);
971 /* Set up node_to_cpumask based on cpu_to_node[]. */
972 numa_add_cpu(i);
973 }
975 printk("Brought up %ld CPUs\n", (long)num_online_cpus());
976 smp_cpus_done(max_cpus);
978 initialise_gdb(); /* could be moved earlier */
980 do_initcalls();
982 if ( opt_watchdog )
983 watchdog_enable();
985 /* Create initial domain 0. */
986 dom0 = domain_create(0, 0, DOM0_SSIDREF);
987 if ( (dom0 == NULL) || (alloc_vcpu(dom0, 0, 0) == NULL) )
988 panic("Error creating domain 0\n");
990 dom0->is_privileged = 1;
991 dom0->target = NULL;
993 /* Grab the DOM0 command line. */
994 cmdline = (char *)(mod[0].string ? __va(mod[0].string) : NULL);
995 if ( (cmdline != NULL) || (kextra != NULL) )
996 {
997 static char dom0_cmdline[MAX_GUEST_CMDLINE];
999 cmdline = cmdline_cook(cmdline);
1000 safe_strcpy(dom0_cmdline, cmdline);
1002 if ( kextra != NULL )
1003 /* kextra always includes exactly one leading space. */
1004 safe_strcat(dom0_cmdline, kextra);
1006 /* Append any extra parameters. */
1007 if ( skip_ioapic_setup && !strstr(dom0_cmdline, "noapic") )
1008 safe_strcat(dom0_cmdline, " noapic");
1009 if ( acpi_skip_timer_override &&
1010 !strstr(dom0_cmdline, "acpi_skip_timer_override") )
1011 safe_strcat(dom0_cmdline, " acpi_skip_timer_override");
1012 if ( (strlen(acpi_param) == 0) && acpi_disabled )
1014 printk("ACPI is disabled, notifying Domain 0 (acpi=off)\n");
1015 safe_strcpy(acpi_param, "off");
1017 if ( (strlen(acpi_param) != 0) && !strstr(dom0_cmdline, "acpi=") )
1019 safe_strcat(dom0_cmdline, " acpi=");
1020 safe_strcat(dom0_cmdline, acpi_param);
1023 cmdline = dom0_cmdline;
1026 if ( (initrdidx > 0) && (initrdidx < mbi->mods_count) )
1028 _initrd_start = initial_images_start +
1029 (mod[initrdidx].mod_start - mod[0].mod_start);
1030 _initrd_len = mod[initrdidx].mod_end - mod[initrdidx].mod_start;
1033 if ( xen_cpuidle )
1034 xen_processor_pmbits |= XEN_PROCESSOR_PM_CX;
1036 /*
1037 * We're going to setup domain0 using the module(s) that we stashed safely
1038 * above our heap. The second module, if present, is an initrd ramdisk.
1039 */
1040 if ( construct_dom0(dom0,
1041 initial_images_base,
1042 initial_images_start,
1043 mod[0].mod_end-mod[0].mod_start,
1044 _initrd_start,
1045 _initrd_len,
1046 cmdline) != 0)
1047 panic("Could not set up DOM0 guest OS\n");
1049 /* Scrub RAM that is still free and so may go to an unprivileged domain. */
1050 scrub_heap_pages();
1052 init_trace_bufs();
1054 console_endboot();
1056 /* Hide UART from DOM0 if we're using it */
1057 serial_endboot();
1059 domain_unpause_by_systemcontroller(dom0);
1061 reset_stack_and_jump(init_done);
1064 void arch_get_xen_caps(xen_capabilities_info_t *info)
1066 /* Interface name is always xen-3.0-* for Xen-3.x. */
1067 int major = 3, minor = 0;
1068 char s[32];
1070 (*info)[0] = '\0';
1072 #if defined(CONFIG_X86_32)
1074 snprintf(s, sizeof(s), "xen-%d.%d-x86_32p ", major, minor);
1075 safe_strcat(*info, s);
1076 if ( hvm_enabled )
1078 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32 ", major, minor);
1079 safe_strcat(*info, s);
1080 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32p ", major, minor);
1081 safe_strcat(*info, s);
1084 #elif defined(CONFIG_X86_64)
1086 snprintf(s, sizeof(s), "xen-%d.%d-x86_64 ", major, minor);
1087 safe_strcat(*info, s);
1088 #ifdef CONFIG_COMPAT
1089 snprintf(s, sizeof(s), "xen-%d.%d-x86_32p ", major, minor);
1090 safe_strcat(*info, s);
1091 #endif
1092 if ( hvm_enabled )
1094 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32 ", major, minor);
1095 safe_strcat(*info, s);
1096 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32p ", major, minor);
1097 safe_strcat(*info, s);
1098 snprintf(s, sizeof(s), "hvm-%d.%d-x86_64 ", major, minor);
1099 safe_strcat(*info, s);
1102 #endif
1105 int xen_in_range(paddr_t start, paddr_t end)
1107 #if defined(CONFIG_X86_32)
1108 paddr_t xs = 0;
1109 paddr_t xe = xenheap_phys_end;
1110 #else
1111 paddr_t xs = __pa(&_stext);
1112 paddr_t xe = __pa(&_etext);
1113 #endif
1115 return (start < xe) && (end > xs);
1118 /*
1119 * Local variables:
1120 * mode: C
1121 * c-set-style: "BSD"
1122 * c-basic-offset: 4
1123 * tab-width: 4
1124 * indent-tabs-mode: nil
1125 * End:
1126 */