ia64/xen-unstable

view xen/arch/x86/setup.c @ 19190:8d7a6bc2575d

x86_64: Ensure enough space for allocator bitmap after relocated Xen image.
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Feb 11 10:41:45 2009 +0000 (2009-02-11)
parents deab3a069185
children 507b264f0a21
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;
621 /* ensure mod[0] is mapped before parsing */
622 bootstrap_map(mod[0].mod_start, mod[0].mod_end);
623 modules_headroom = bzimage_headroom(
624 (char *)(unsigned long)mod[0].mod_start,
625 (unsigned long)(mod[0].mod_end - mod[0].mod_start));
627 for ( i = boot_e820.nr_map-1; i >= 0; i-- )
628 {
629 uint64_t s, e, mask = (1UL << L2_PAGETABLE_SHIFT) - 1;
631 /* Superpage-aligned chunks from 16MB to BOOTSTRAP_DIRECTMAP_END. */
632 s = (boot_e820.map[i].addr + mask) & ~mask;
633 e = (boot_e820.map[i].addr + boot_e820.map[i].size) & ~mask;
634 s = max_t(uint64_t, s, 16 << 20);
635 e = min_t(uint64_t, e, BOOTSTRAP_DIRECTMAP_END);
636 if ( (boot_e820.map[i].type != E820_RAM) || (s >= e) )
637 continue;
639 /* Map the chunk. No memory will need to be allocated to do this. */
640 map_pages_to_xen(
641 (unsigned long)maddr_to_bootstrap_virt(s),
642 s >> PAGE_SHIFT, (e-s) >> PAGE_SHIFT, PAGE_HYPERVISOR);
644 #if defined(CONFIG_X86_64)
645 /* Relocate Xen image, allocation bitmap, and one page of padding. */
646 #define reloc_size ((__pa(&_end) + max_page/8 + PAGE_SIZE + mask) & ~mask)
647 /* Is the region suitable for relocating Xen? */
648 if ( !xen_phys_start && ((e-s) >= reloc_size) )
649 {
650 extern l2_pgentry_t l2_xenmap[];
651 l4_pgentry_t *pl4e;
652 l3_pgentry_t *pl3e;
653 l2_pgentry_t *pl2e;
654 int i, j, k;
656 /* Select relocation address. */
657 e -= reloc_size;
658 xen_phys_start = e;
659 bootsym(trampoline_xen_phys_start) = e;
661 /*
662 * Perform relocation to new physical address.
663 * Before doing so we must sync static/global data with main memory
664 * with a barrier(). After this we must *not* modify static/global
665 * data until after we have switched to the relocated pagetables!
666 */
667 barrier();
668 move_memory(e, 0, __pa(&_end) - xen_phys_start);
670 /* Poison low 1MB to detect stray pointers to physical 0-1MB. */
671 memset(maddr_to_bootstrap_virt(e), 0x55, 1U<<20);
673 /* Walk initial pagetables, relocating page directory entries. */
674 pl4e = __va(__pa(idle_pg_table));
675 for ( i = 0 ; i < L4_PAGETABLE_ENTRIES; i++, pl4e++ )
676 {
677 if ( !(l4e_get_flags(*pl4e) & _PAGE_PRESENT) )
678 continue;
679 *pl4e = l4e_from_intpte(l4e_get_intpte(*pl4e) +
680 xen_phys_start);
681 pl3e = l4e_to_l3e(*pl4e);
682 for ( j = 0; j < L3_PAGETABLE_ENTRIES; j++, pl3e++ )
683 {
684 /* Not present, 1GB mapping, or already relocated? */
685 if ( !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) ||
686 (l3e_get_flags(*pl3e) & _PAGE_PSE) ||
687 (l3e_get_pfn(*pl3e) > 0x1000) )
688 continue;
689 *pl3e = l3e_from_intpte(l3e_get_intpte(*pl3e) +
690 xen_phys_start);
691 pl2e = l3e_to_l2e(*pl3e);
692 for ( k = 0; k < L2_PAGETABLE_ENTRIES; k++, pl2e++ )
693 {
694 /* Not present, PSE, or already relocated? */
695 if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ||
696 (l2e_get_flags(*pl2e) & _PAGE_PSE) ||
697 (l2e_get_pfn(*pl2e) > 0x1000) )
698 continue;
699 *pl2e = l2e_from_intpte(l2e_get_intpte(*pl2e) +
700 xen_phys_start);
701 }
702 }
703 }
705 /* The only data mappings to be relocated are in the Xen area. */
706 pl2e = __va(__pa(l2_xenmap));
707 *pl2e++ = l2e_from_pfn(xen_phys_start >> PAGE_SHIFT,
708 PAGE_HYPERVISOR | _PAGE_PSE);
709 for ( i = 1; i < L2_PAGETABLE_ENTRIES; i++, pl2e++ )
710 {
711 if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) )
712 continue;
713 *pl2e = l2e_from_intpte(l2e_get_intpte(*pl2e) +
714 xen_phys_start);
715 }
717 /* Re-sync the stack and then switch to relocated pagetables. */
718 asm volatile (
719 "rep movsb ; " /* re-sync the stack */
720 "movq %%cr4,%%rsi ; "
721 "andb $0x7f,%%sil ; "
722 "movq %%rsi,%%cr4 ; " /* CR4.PGE == 0 */
723 "movq %0,%%cr3 ; " /* CR3 == new pagetables */
724 "orb $0x80,%%sil ; "
725 "movq %%rsi,%%cr4 " /* CR4.PGE == 1 */
726 : : "r" (__pa(idle_pg_table)), "S" (cpu0_stack),
727 "D" (__va(__pa(cpu0_stack))), "c" (STACK_SIZE) : "memory" );
728 }
729 #endif
731 /* Is the region suitable for relocating the multiboot modules? */
732 if ( !initial_images_start && (s < e) &&
733 ((e-s) >= (modules_length+modules_headroom)) )
734 {
735 initial_images_end = e;
736 e = (e - modules_length) & PAGE_MASK;
737 initial_images_start = e;
738 e -= modules_headroom;
739 initial_images_base = e;
740 move_memory(initial_images_start,
741 mod[0].mod_start, mod[mbi->mods_count-1].mod_end);
742 }
744 if ( !kexec_crash_area.start && (s < e) &&
745 ((e-s) >= kexec_crash_area.size) )
746 {
747 e = (e - kexec_crash_area.size) & PAGE_MASK;
748 kexec_crash_area.start = e;
749 }
750 }
752 if ( !initial_images_start )
753 EARLY_FAIL("Not enough memory to relocate the dom0 kernel image.\n");
754 reserve_e820_ram(&boot_e820, initial_images_base, initial_images_end);
756 /* Initialise boot heap. */
757 allocator_bitmap_end = init_boot_allocator(__pa(&_end));
758 #if defined(CONFIG_X86_32)
759 xenheap_initial_phys_start = allocator_bitmap_end;
760 xenheap_phys_end = DIRECTMAP_MBYTES << 20;
761 #else
762 if ( !xen_phys_start )
763 EARLY_FAIL("Not enough memory to relocate Xen.\n");
764 reserve_e820_ram(&boot_e820, __pa(&_start), allocator_bitmap_end);
765 #endif
767 /* Late kexec reservation (dynamic start address). */
768 kexec_reserve_area(&boot_e820);
770 /*
771 * With the boot allocator now initialised, we can walk every RAM region
772 * and map it in its entirety (on x86/64, at least) and notify it to the
773 * boot allocator.
774 */
775 for ( i = 0; i < boot_e820.nr_map; i++ )
776 {
777 uint64_t s, e, map_s, map_e, mask = PAGE_SIZE - 1;
779 /* Only page alignment required now. */
780 s = (boot_e820.map[i].addr + mask) & ~mask;
781 e = (boot_e820.map[i].addr + boot_e820.map[i].size) & ~mask;
782 #if defined(CONFIG_X86_32)
783 s = max_t(uint64_t, s, xenheap_phys_end);
784 #else
785 s = max_t(uint64_t, s, 1<<20);
786 #endif
787 if ( (boot_e820.map[i].type != E820_RAM) || (s >= e) )
788 continue;
790 /* Need to create mappings above 16MB. */
791 map_s = max_t(uint64_t, s, 16<<20);
792 map_e = e;
793 #if defined(CONFIG_X86_32) /* mappings are truncated on x86_32 */
794 map_e = min_t(uint64_t, map_e, BOOTSTRAP_DIRECTMAP_END);
795 #endif
797 /* Pass mapped memory to allocator /before/ creating new mappings. */
798 init_boot_pages(s, min_t(uint64_t, map_s, e));
800 /* Create new mappings /before/ passing memory to the allocator. */
801 if ( map_s < map_e )
802 map_pages_to_xen(
803 (unsigned long)maddr_to_bootstrap_virt(map_s),
804 map_s >> PAGE_SHIFT, (map_e-map_s) >> PAGE_SHIFT,
805 PAGE_HYPERVISOR);
807 /* Pass remainder of this memory chunk to the allocator. */
808 init_boot_pages(map_s, e);
809 }
811 memguard_init();
813 nr_pages = 0;
814 for ( i = 0; i < e820.nr_map; i++ )
815 if ( e820.map[i].type == E820_RAM )
816 nr_pages += e820.map[i].size >> PAGE_SHIFT;
817 printk("System RAM: %luMB (%lukB)\n",
818 nr_pages >> (20 - PAGE_SHIFT),
819 nr_pages << (PAGE_SHIFT - 10));
820 total_pages = nr_pages;
822 /* Sanity check for unwanted bloat of certain hypercall structures. */
823 BUILD_BUG_ON(sizeof(((struct xen_platform_op *)0)->u) !=
824 sizeof(((struct xen_platform_op *)0)->u.pad));
825 BUILD_BUG_ON(sizeof(((struct xen_domctl *)0)->u) !=
826 sizeof(((struct xen_domctl *)0)->u.pad));
827 BUILD_BUG_ON(sizeof(((struct xen_sysctl *)0)->u) !=
828 sizeof(((struct xen_sysctl *)0)->u.pad));
830 BUILD_BUG_ON(sizeof(start_info_t) > PAGE_SIZE);
831 BUILD_BUG_ON(sizeof(shared_info_t) > PAGE_SIZE);
832 BUILD_BUG_ON(sizeof(struct vcpu_info) != 64);
834 #ifdef CONFIG_COMPAT
835 BUILD_BUG_ON(sizeof(((struct compat_platform_op *)0)->u) !=
836 sizeof(((struct compat_platform_op *)0)->u.pad));
837 BUILD_BUG_ON(sizeof(start_info_compat_t) > PAGE_SIZE);
838 BUILD_BUG_ON(sizeof(struct compat_vcpu_info) != 64);
839 #endif
841 /* Check definitions in public headers match internal defs. */
842 BUILD_BUG_ON(__HYPERVISOR_VIRT_START != HYPERVISOR_VIRT_START);
843 #ifdef HYPERVISOR_VIRT_END
844 BUILD_BUG_ON(__HYPERVISOR_VIRT_END != HYPERVISOR_VIRT_END);
845 #endif
846 BUILD_BUG_ON(MACH2PHYS_VIRT_START != RO_MPT_VIRT_START);
847 BUILD_BUG_ON(MACH2PHYS_VIRT_END != RO_MPT_VIRT_END);
849 init_frametable();
851 acpi_boot_table_init();
853 acpi_numa_init();
855 numa_initmem_init(0, max_page);
857 #if defined(CONFIG_X86_32)
858 /* Initialise the Xen heap. */
859 init_xenheap_pages(xenheap_initial_phys_start, xenheap_phys_end);
860 nr_pages = (xenheap_phys_end - xenheap_initial_phys_start) >> PAGE_SHIFT;
861 printk("Xen heap: %luMB (%lukB)\n",
862 nr_pages >> (20 - PAGE_SHIFT),
863 nr_pages << (PAGE_SHIFT - 10));
864 #endif
866 end_boot_allocator();
867 early_boot = 0;
869 #if defined(CONFIG_X86_64)
870 vesa_init();
871 #endif
873 softirq_init();
875 early_cpu_init();
877 paging_init();
879 tboot_probe();
881 /* Unmap the first page of CPU0's stack. */
882 memguard_guard_stack(cpu0_stack);
884 open_softirq(NEW_TLBFLUSH_CLOCK_PERIOD_SOFTIRQ, new_tlbflush_clock_period);
886 if ( opt_watchdog )
887 nmi_watchdog = NMI_LOCAL_APIC;
889 sort_exception_tables();
891 find_smp_config();
893 dmi_scan_machine();
895 generic_apic_probe();
897 if ( x2apic_is_available() )
898 enable_x2apic();
900 acpi_boot_init();
902 init_cpu_to_node();
904 if ( smp_found_config )
905 get_smp_config();
907 #ifdef CONFIG_X86_64
908 /* Low mappings were only needed for some BIOS table parsing. */
909 zap_low_mappings();
910 #endif
912 init_apic_mappings();
914 init_IRQ();
916 percpu_init_areas();
918 xsm_init(&initrdidx, mbi, initial_images_start);
920 init_idle_domain();
922 trap_init();
924 rcu_init();
926 timer_init();
928 early_time_init();
930 arch_init_memory();
932 identify_cpu(&boot_cpu_data);
933 if ( cpu_has_fxsr )
934 set_in_cr4(X86_CR4_OSFXSR);
935 if ( cpu_has_xmm )
936 set_in_cr4(X86_CR4_OSXMMEXCPT);
938 local_irq_enable();
940 #ifdef CONFIG_X86_64
941 vesa_mtrr_init();
942 #endif
944 if ( opt_nosmp )
945 max_cpus = 0;
947 smp_prepare_cpus(max_cpus);
949 spin_debug_enable();
951 /*
952 * Initialise higher-level timer functions. We do this fairly late
953 * (post-SMP) because the time bases and scale factors need to be updated
954 * regularly, and SMP initialisation can cause a long delay with
955 * interrupts not yet enabled.
956 */
957 init_xen_time();
959 initialize_keytable();
961 serial_init_postirq();
963 for_each_present_cpu ( i )
964 {
965 if ( num_online_cpus() >= max_cpus )
966 break;
967 if ( !cpu_online(i) )
968 {
969 rcu_online_cpu(i);
970 __cpu_up(i);
971 }
973 /* Set up cpu_to_node[]. */
974 srat_detect_node(i);
975 /* Set up node_to_cpumask based on cpu_to_node[]. */
976 numa_add_cpu(i);
977 }
979 printk("Brought up %ld CPUs\n", (long)num_online_cpus());
980 smp_cpus_done(max_cpus);
982 initialise_gdb(); /* could be moved earlier */
984 do_initcalls();
986 if ( opt_watchdog )
987 watchdog_enable();
989 /* Create initial domain 0. */
990 dom0 = domain_create(0, 0, DOM0_SSIDREF);
991 if ( (dom0 == NULL) || (alloc_vcpu(dom0, 0, 0) == NULL) )
992 panic("Error creating domain 0\n");
994 dom0->is_privileged = 1;
995 dom0->target = NULL;
997 /* Grab the DOM0 command line. */
998 cmdline = (char *)(mod[0].string ? __va(mod[0].string) : NULL);
999 if ( (cmdline != NULL) || (kextra != NULL) )
1001 static char dom0_cmdline[MAX_GUEST_CMDLINE];
1003 cmdline = cmdline_cook(cmdline);
1004 safe_strcpy(dom0_cmdline, cmdline);
1006 if ( kextra != NULL )
1007 /* kextra always includes exactly one leading space. */
1008 safe_strcat(dom0_cmdline, kextra);
1010 /* Append any extra parameters. */
1011 if ( skip_ioapic_setup && !strstr(dom0_cmdline, "noapic") )
1012 safe_strcat(dom0_cmdline, " noapic");
1013 if ( acpi_skip_timer_override &&
1014 !strstr(dom0_cmdline, "acpi_skip_timer_override") )
1015 safe_strcat(dom0_cmdline, " acpi_skip_timer_override");
1016 if ( (strlen(acpi_param) == 0) && acpi_disabled )
1018 printk("ACPI is disabled, notifying Domain 0 (acpi=off)\n");
1019 safe_strcpy(acpi_param, "off");
1021 if ( (strlen(acpi_param) != 0) && !strstr(dom0_cmdline, "acpi=") )
1023 safe_strcat(dom0_cmdline, " acpi=");
1024 safe_strcat(dom0_cmdline, acpi_param);
1027 cmdline = dom0_cmdline;
1030 if ( (initrdidx > 0) && (initrdidx < mbi->mods_count) )
1032 _initrd_start = initial_images_start +
1033 (mod[initrdidx].mod_start - mod[0].mod_start);
1034 _initrd_len = mod[initrdidx].mod_end - mod[initrdidx].mod_start;
1037 if ( xen_cpuidle )
1038 xen_processor_pmbits |= XEN_PROCESSOR_PM_CX;
1040 if ( !tboot_protect_mem_regions() )
1041 panic("Could not protect TXT memory regions\n");
1043 /*
1044 * We're going to setup domain0 using the module(s) that we stashed safely
1045 * above our heap. The second module, if present, is an initrd ramdisk.
1046 */
1047 if ( construct_dom0(dom0,
1048 initial_images_base,
1049 initial_images_start,
1050 mod[0].mod_end-mod[0].mod_start,
1051 _initrd_start,
1052 _initrd_len,
1053 cmdline) != 0)
1054 panic("Could not set up DOM0 guest OS\n");
1056 /* Scrub RAM that is still free and so may go to an unprivileged domain. */
1057 scrub_heap_pages();
1059 init_trace_bufs();
1061 console_endboot();
1063 /* Hide UART from DOM0 if we're using it */
1064 serial_endboot();
1066 domain_unpause_by_systemcontroller(dom0);
1068 reset_stack_and_jump(init_done);
1071 void arch_get_xen_caps(xen_capabilities_info_t *info)
1073 /* Interface name is always xen-3.0-* for Xen-3.x. */
1074 int major = 3, minor = 0;
1075 char s[32];
1077 (*info)[0] = '\0';
1079 #if defined(CONFIG_X86_32)
1081 snprintf(s, sizeof(s), "xen-%d.%d-x86_32p ", major, minor);
1082 safe_strcat(*info, s);
1083 if ( hvm_enabled )
1085 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32 ", major, minor);
1086 safe_strcat(*info, s);
1087 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32p ", major, minor);
1088 safe_strcat(*info, s);
1091 #elif defined(CONFIG_X86_64)
1093 snprintf(s, sizeof(s), "xen-%d.%d-x86_64 ", major, minor);
1094 safe_strcat(*info, s);
1095 #ifdef CONFIG_COMPAT
1096 snprintf(s, sizeof(s), "xen-%d.%d-x86_32p ", major, minor);
1097 safe_strcat(*info, s);
1098 #endif
1099 if ( hvm_enabled )
1101 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32 ", major, minor);
1102 safe_strcat(*info, s);
1103 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32p ", major, minor);
1104 safe_strcat(*info, s);
1105 snprintf(s, sizeof(s), "hvm-%d.%d-x86_64 ", major, minor);
1106 safe_strcat(*info, s);
1109 #endif
1112 int xen_in_range(paddr_t start, paddr_t end)
1114 #if defined(CONFIG_X86_32)
1115 paddr_t xs = 0;
1116 paddr_t xe = xenheap_phys_end;
1117 #else
1118 paddr_t xs = __pa(&_stext);
1119 paddr_t xe = __pa(&_etext);
1120 #endif
1122 return (start < xe) && (end > xs);
1125 /*
1126 * Local variables:
1127 * mode: C
1128 * c-set-style: "BSD"
1129 * c-basic-offset: 4
1130 * tab-width: 4
1131 * indent-tabs-mode: nil
1132 * End:
1133 */