ia64/xen-unstable

view xen/arch/x86/setup.c @ 19061:681af1946724

x86_32: Get rid of xenheap_phys_start as it is zero after start of day.

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