ia64/xen-unstable

view xen/arch/x86/setup.c @ 18051:79517ed2a108

x86: PIT broadcast to fix local APIC timer stop issue for Deep C state

Local APIC timer may stop at deep C state (C3/C4...) entry. Initial
HPET broadcast working in legacy replacing mode, broke RTC intr, so
was bypassed. This patch add the logic that use platform timer (PIT)
to reenable local APIC timer at C state entry/exit.

Currently, only keep PIT enabled with 100Hz freq. The next step is
trying to dynamically enable/disable PIT while needed, and give it
lower freq.

Signed-off-by: Yu Ke <ke.yu@intel.com>
Signed-off-by: Tian Kevin <kevin.tian@intel.com>
Signed-off-by: Wei Gang <gang.wei@intel.com>
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Jul 14 10:43:32 2008 +0100 (2008-07-14)
parents e265878e8f65
children a637c023e066
line source
1 #include <xen/config.h>
2 #include <xen/init.h>
3 #include <xen/lib.h>
4 #include <xen/sched.h>
5 #include <xen/domain.h>
6 #include <xen/serial.h>
7 #include <xen/softirq.h>
8 #include <xen/acpi.h>
9 #include <xen/console.h>
10 #include <xen/serial.h>
11 #include <xen/trace.h>
12 #include <xen/multiboot.h>
13 #include <xen/domain_page.h>
14 #include <xen/version.h>
15 #include <xen/gdbstub.h>
16 #include <xen/percpu.h>
17 #include <xen/hypercall.h>
18 #include <xen/keyhandler.h>
19 #include <xen/numa.h>
20 #include <xen/rcupdate.h>
21 #include <xen/vga.h>
22 #include <xen/dmi.h>
23 #include <public/version.h>
24 #ifdef CONFIG_COMPAT
25 #include <compat/platform.h>
26 #include <compat/xen.h>
27 #endif
28 #include <asm/bitops.h>
29 #include <asm/smp.h>
30 #include <asm/processor.h>
31 #include <asm/mpspec.h>
32 #include <asm/apic.h>
33 #include <asm/desc.h>
34 #include <asm/paging.h>
35 #include <asm/e820.h>
36 #include <xsm/acm/acm_hooks.h>
37 #include <xen/kexec.h>
38 #include <asm/edd.h>
39 #include <xsm/xsm.h>
40 #include <asm/tboot.h>
42 #if defined(CONFIG_X86_64)
43 #define BOOTSTRAP_DIRECTMAP_END (1UL << 32) /* 4GB */
44 #define maddr_to_bootstrap_virt(m) maddr_to_virt(m)
45 #else
46 #define BOOTSTRAP_DIRECTMAP_END (1UL << 30) /* 1GB */
47 #define maddr_to_bootstrap_virt(m) ((void *)(long)(m))
48 #endif
50 extern void generic_apic_probe(void);
51 extern void numa_initmem_init(unsigned long start_pfn, unsigned long end_pfn);
53 extern u16 boot_edid_caps;
54 extern u8 boot_edid_info[128];
55 extern struct boot_video_info boot_vid_info;
57 /*
58 * opt_xenheap_megabytes: Size of Xen heap in megabytes, excluding the
59 * page_info table and allocation bitmap.
60 */
61 static unsigned int opt_xenheap_megabytes = XENHEAP_DEFAULT_MB;
62 #if defined(CONFIG_X86_64)
63 integer_param("xenheap_megabytes", opt_xenheap_megabytes);
64 #endif
66 /* opt_nosmp: If true, secondary processors are ignored. */
67 static int opt_nosmp = 0;
68 boolean_param("nosmp", opt_nosmp);
70 /* maxcpus: maximum number of CPUs to activate. */
71 static unsigned int max_cpus = NR_CPUS;
72 integer_param("maxcpus", max_cpus);
74 /* opt_watchdog: If true, run a watchdog NMI on each processor. */
75 static int opt_watchdog = 0;
76 boolean_param("watchdog", opt_watchdog);
78 /* **** Linux config option: propagated to domain0. */
79 /* "acpi=off": Sisables both ACPI table parsing and interpreter. */
80 /* "acpi=force": Override the disable blacklist. */
81 /* "acpi=strict": Disables out-of-spec workarounds. */
82 /* "acpi=ht": Limit ACPI just to boot-time to enable HT. */
83 /* "acpi=noirq": Disables ACPI interrupt routing. */
84 static void parse_acpi_param(char *s);
85 custom_param("acpi", parse_acpi_param);
87 /* **** Linux config option: propagated to domain0. */
88 /* acpi_skip_timer_override: Skip IRQ0 overrides. */
89 extern int acpi_skip_timer_override;
90 boolean_param("acpi_skip_timer_override", acpi_skip_timer_override);
92 /* **** Linux config option: propagated to domain0. */
93 /* noapic: Disable IOAPIC setup. */
94 extern int skip_ioapic_setup;
95 boolean_param("noapic", skip_ioapic_setup);
97 /* **** Linux config option: propagated to domain0. */
98 /* xen_cpuidle: xen control cstate. */
99 /*static*/ int xen_cpuidle;
100 boolean_param("cpuidle", xen_cpuidle);
102 int early_boot = 1;
104 cpumask_t cpu_present_map;
106 unsigned long xen_phys_start;
108 /* Limits of Xen heap, used to initialise the allocator. */
109 unsigned long xenheap_phys_start, xenheap_phys_end;
111 extern void arch_init_memory(void);
112 extern void init_IRQ(void);
113 extern void early_time_init(void);
114 extern void early_cpu_init(void);
115 extern void vesa_init(void);
116 extern void vesa_mtrr_init(void);
118 struct tss_struct init_tss[NR_CPUS];
120 char __attribute__ ((__section__(".bss.stack_aligned"))) cpu0_stack[STACK_SIZE];
122 struct cpuinfo_x86 boot_cpu_data = { 0, 0, 0, 0, -1 };
124 unsigned long mmu_cr4_features = X86_CR4_PSE | X86_CR4_PGE | X86_CR4_PAE;
125 EXPORT_SYMBOL(mmu_cr4_features);
127 int acpi_disabled;
129 int acpi_force;
130 char acpi_param[10] = "";
131 static void __init parse_acpi_param(char *s)
132 {
133 /* Save the parameter so it can be propagated to domain0. */
134 safe_strcpy(acpi_param, s);
136 /* Interpret the parameter for use within Xen. */
137 if ( !strcmp(s, "off") )
138 {
139 disable_acpi();
140 }
141 else if ( !strcmp(s, "force") )
142 {
143 acpi_force = 1;
144 acpi_ht = 1;
145 acpi_disabled = 0;
146 }
147 else if ( !strcmp(s, "strict") )
148 {
149 acpi_strict = 1;
150 }
151 else if ( !strcmp(s, "ht") )
152 {
153 if ( !acpi_force )
154 disable_acpi();
155 acpi_ht = 1;
156 }
157 else if ( !strcmp(s, "noirq") )
158 {
159 acpi_noirq_set();
160 }
161 }
163 static void __init do_initcalls(void)
164 {
165 initcall_t *call;
166 for ( call = &__initcall_start; call < &__initcall_end; call++ )
167 (*call)();
168 }
170 #define EARLY_FAIL(f, a...) do { \
171 printk( f , ## a ); \
172 for ( ; ; ) halt(); \
173 } while (0)
175 static unsigned long __initdata initial_images_start, initial_images_end;
177 unsigned long __init initial_images_nrpages(void)
178 {
179 ASSERT(!(initial_images_start & ~PAGE_MASK));
180 ASSERT(!(initial_images_end & ~PAGE_MASK));
181 return ((initial_images_end >> PAGE_SHIFT) -
182 (initial_images_start >> PAGE_SHIFT));
183 }
185 void __init discard_initial_images(void)
186 {
187 init_domheap_pages(initial_images_start, initial_images_end);
188 }
190 extern char __per_cpu_start[], __per_cpu_data_end[], __per_cpu_end[];
192 static void __init percpu_init_areas(void)
193 {
194 unsigned int i, data_size = __per_cpu_data_end - __per_cpu_start;
195 unsigned int first_unused;
197 BUG_ON(data_size > PERCPU_SIZE);
199 /* Initialise per-cpu data area for all possible secondary CPUs. */
200 for ( i = 1; (i < NR_CPUS) && cpu_possible(i); i++ )
201 memcpy(__per_cpu_start + (i << PERCPU_SHIFT),
202 __per_cpu_start,
203 data_size);
204 first_unused = i;
206 /* Check that there are no holes in cpu_possible_map. */
207 for ( ; i < NR_CPUS; i++ )
208 BUG_ON(cpu_possible(i));
210 #ifndef MEMORY_GUARD
211 init_xenheap_pages(__pa(__per_cpu_start) + (first_unused << PERCPU_SHIFT),
212 __pa(__per_cpu_end));
213 #endif
214 memguard_guard_range(&__per_cpu_start[first_unused << PERCPU_SHIFT],
215 (NR_CPUS - first_unused) << PERCPU_SHIFT);
216 #if defined(CONFIG_X86_64)
217 /* Also zap the mapping in the 1:1 area. */
218 memguard_guard_range(__va(__pa(__per_cpu_start)) +
219 (first_unused << PERCPU_SHIFT),
220 (NR_CPUS - first_unused) << PERCPU_SHIFT);
221 #endif
222 }
224 static void __init init_idle_domain(void)
225 {
226 struct domain *idle_domain;
228 /* Domain creation requires that scheduler structures are initialised. */
229 scheduler_init();
231 idle_domain = domain_create(IDLE_DOMAIN_ID, 0, 0);
232 if ( (idle_domain == NULL) || (alloc_vcpu(idle_domain, 0, 0) == NULL) )
233 BUG();
235 set_current(idle_domain->vcpu[0]);
236 idle_vcpu[0] = this_cpu(curr_vcpu) = current;
238 setup_idle_pagetable();
239 }
241 static void __init srat_detect_node(int cpu)
242 {
243 unsigned node;
244 u32 apicid = x86_cpu_to_apicid[cpu];
246 node = apicid_to_node[apicid];
247 if ( node == NUMA_NO_NODE )
248 node = 0;
249 numa_set_node(cpu, node);
251 if ( acpi_numa > 0 )
252 printk(KERN_INFO "CPU %d APIC %d -> Node %d\n", cpu, apicid, node);
253 }
255 /*
256 * Ensure a given physical memory range is present in the bootstrap mappings.
257 * Use superpage mappings to ensure that pagetable memory needn't be allocated.
258 */
259 static void __init bootstrap_map(unsigned long start, unsigned long end)
260 {
261 unsigned long mask = (1UL << L2_PAGETABLE_SHIFT) - 1;
262 start = max_t(unsigned long, start & ~mask, 16UL << 20);
263 end = (end + mask) & ~mask;
264 if ( start >= end )
265 return;
266 if ( end > BOOTSTRAP_DIRECTMAP_END )
267 panic("Cannot access memory beyond end of "
268 "bootstrap direct-map area\n");
269 map_pages_to_xen(
270 (unsigned long)maddr_to_bootstrap_virt(start),
271 start >> PAGE_SHIFT, (end-start) >> PAGE_SHIFT, PAGE_HYPERVISOR);
272 }
274 static void __init move_memory(
275 unsigned long dst, unsigned long src_start, unsigned long src_end)
276 {
277 bootstrap_map(src_start, src_end);
278 bootstrap_map(dst, dst + src_end - src_start);
279 memmove(maddr_to_bootstrap_virt(dst),
280 maddr_to_bootstrap_virt(src_start),
281 src_end - src_start);
282 }
284 /* A temporary copy of the e820 map that we can mess with during bootstrap. */
285 static struct e820map __initdata boot_e820;
287 struct boot_video_info {
288 u8 orig_x; /* 0x00 */
289 u8 orig_y; /* 0x01 */
290 u8 orig_video_mode; /* 0x02 */
291 u8 orig_video_cols; /* 0x03 */
292 u8 orig_video_lines; /* 0x04 */
293 u8 orig_video_isVGA; /* 0x05 */
294 u16 orig_video_points; /* 0x06 */
296 /* VESA graphic mode -- linear frame buffer */
297 u32 capabilities; /* 0x08 */
298 u16 lfb_linelength; /* 0x0c */
299 u16 lfb_width; /* 0x0e */
300 u16 lfb_height; /* 0x10 */
301 u16 lfb_depth; /* 0x12 */
302 u32 lfb_base; /* 0x14 */
303 u32 lfb_size; /* 0x18 */
304 u8 red_size; /* 0x1c */
305 u8 red_pos; /* 0x1d */
306 u8 green_size; /* 0x1e */
307 u8 green_pos; /* 0x1f */
308 u8 blue_size; /* 0x20 */
309 u8 blue_pos; /* 0x21 */
310 u8 rsvd_size; /* 0x22 */
311 u8 rsvd_pos; /* 0x23 */
312 u16 vesapm_seg; /* 0x24 */
313 u16 vesapm_off; /* 0x26 */
314 u16 vesa_attrib; /* 0x28 */
315 };
317 static void __init parse_video_info(void)
318 {
319 struct boot_video_info *bvi = &bootsym(boot_vid_info);
321 if ( (bvi->orig_video_isVGA == 1) && (bvi->orig_video_mode == 3) )
322 {
323 vga_console_info.video_type = XEN_VGATYPE_TEXT_MODE_3;
324 vga_console_info.u.text_mode_3.font_height = bvi->orig_video_points;
325 vga_console_info.u.text_mode_3.cursor_x = bvi->orig_x;
326 vga_console_info.u.text_mode_3.cursor_y = bvi->orig_y;
327 vga_console_info.u.text_mode_3.rows = bvi->orig_video_lines;
328 vga_console_info.u.text_mode_3.columns = bvi->orig_video_cols;
329 }
330 else if ( bvi->orig_video_isVGA == 0x23 )
331 {
332 vga_console_info.video_type = XEN_VGATYPE_VESA_LFB;
333 vga_console_info.u.vesa_lfb.width = bvi->lfb_width;
334 vga_console_info.u.vesa_lfb.height = bvi->lfb_height;
335 vga_console_info.u.vesa_lfb.bytes_per_line = bvi->lfb_linelength;
336 vga_console_info.u.vesa_lfb.bits_per_pixel = bvi->lfb_depth;
337 vga_console_info.u.vesa_lfb.lfb_base = bvi->lfb_base;
338 vga_console_info.u.vesa_lfb.lfb_size = bvi->lfb_size;
339 vga_console_info.u.vesa_lfb.red_pos = bvi->red_pos;
340 vga_console_info.u.vesa_lfb.red_size = bvi->red_size;
341 vga_console_info.u.vesa_lfb.green_pos = bvi->green_pos;
342 vga_console_info.u.vesa_lfb.green_size = bvi->green_size;
343 vga_console_info.u.vesa_lfb.blue_pos = bvi->blue_pos;
344 vga_console_info.u.vesa_lfb.blue_size = bvi->blue_size;
345 vga_console_info.u.vesa_lfb.rsvd_pos = bvi->rsvd_pos;
346 vga_console_info.u.vesa_lfb.rsvd_size = bvi->rsvd_size;
347 vga_console_info.u.vesa_lfb.gbl_caps = bvi->capabilities;
348 vga_console_info.u.vesa_lfb.mode_attrs = bvi->vesa_attrib;
349 }
350 }
352 void __init kexec_reserve_area(struct e820map *e820)
353 {
354 unsigned long kdump_start = kexec_crash_area.start;
355 unsigned long kdump_size = kexec_crash_area.size;
356 static int is_reserved = 0;
358 kdump_size = (kdump_size + PAGE_SIZE - 1) & PAGE_MASK;
360 if ( (kdump_start == 0) || (kdump_size == 0) || is_reserved )
361 return;
363 is_reserved = 1;
365 if ( !reserve_e820_ram(e820, kdump_start, kdump_start + kdump_size) )
366 {
367 printk("Kdump: DISABLED (failed to reserve %luMB (%lukB) at 0x%lx)"
368 "\n", kdump_size >> 20, kdump_size >> 10, kdump_start);
369 kexec_crash_area.start = kexec_crash_area.size = 0;
370 }
371 else
372 {
373 printk("Kdump: %luMB (%lukB) at 0x%lx\n",
374 kdump_size >> 20, kdump_size >> 10, kdump_start);
375 }
376 }
378 void init_done(void)
379 {
380 extern char __init_begin[], __init_end[];
382 /* Free (or page-protect) the init areas. */
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 int i, e820_warn = 0, bytes = 0;
418 struct ns16550_defaults ns16550 = {
419 .data_bits = 8,
420 .parity = 'n',
421 .stop_bits = 1
422 };
424 extern void early_page_fault(void);
425 set_intr_gate(TRAP_page_fault, &early_page_fault);
427 /* Parse the command-line options. */
428 cmdline = cmdline_cook((mbi->flags & MBI_CMDLINE) ?
429 __va(mbi->cmdline) : NULL);
430 if ( (kextra = strstr(cmdline, " -- ")) != NULL )
431 {
432 /*
433 * Options after ' -- ' separator belong to dom0.
434 * 1. Orphan dom0's options from Xen's command line.
435 * 2. Skip all but final leading space from dom0's options.
436 */
437 *kextra = '\0';
438 kextra += 3;
439 while ( kextra[1] == ' ' ) kextra++;
440 }
441 cmdline_parse(cmdline);
443 parse_video_info();
445 set_current((struct vcpu *)0xfffff000); /* debug sanity */
446 idle_vcpu[0] = current;
447 set_processor_id(0); /* needed early, for smp_processor_id() */
448 if ( cpu_has_efer )
449 rdmsrl(MSR_EFER, this_cpu(efer));
450 asm volatile ( "mov %%cr4,%0" : "=r" (this_cpu(cr4)) );
452 smp_prepare_boot_cpu();
454 /* We initialise the serial devices very early so we can get debugging. */
455 ns16550.io_base = 0x3f8;
456 ns16550.irq = 4;
457 ns16550_init(0, &ns16550);
458 ns16550.io_base = 0x2f8;
459 ns16550.irq = 3;
460 ns16550_init(1, &ns16550);
461 serial_init_preirq();
463 init_console();
465 printk("Command line: %s\n", cmdline);
467 printk("Video information:\n");
469 /* Print VGA display mode information. */
470 switch ( vga_console_info.video_type )
471 {
472 case XEN_VGATYPE_TEXT_MODE_3:
473 printk(" VGA is text mode %dx%d, font 8x%d\n",
474 vga_console_info.u.text_mode_3.columns,
475 vga_console_info.u.text_mode_3.rows,
476 vga_console_info.u.text_mode_3.font_height);
477 break;
478 case XEN_VGATYPE_VESA_LFB:
479 printk(" VGA is graphics mode %dx%d, %d bpp\n",
480 vga_console_info.u.vesa_lfb.width,
481 vga_console_info.u.vesa_lfb.height,
482 vga_console_info.u.vesa_lfb.bits_per_pixel);
483 break;
484 default:
485 printk(" No VGA detected\n");
486 break;
487 }
489 /* Print VBE/DDC EDID information. */
490 if ( bootsym(boot_edid_caps) != 0x1313 )
491 {
492 u16 caps = bootsym(boot_edid_caps);
493 printk(" VBE/DDC methods:%s%s%s; ",
494 (caps & 1) ? " V1" : "",
495 (caps & 2) ? " V2" : "",
496 !(caps & 3) ? " none" : "");
497 printk("EDID transfer time: %d seconds\n", caps >> 8);
498 if ( *(u32 *)bootsym(boot_edid_info) == 0x13131313 )
499 {
500 printk(" EDID info not retrieved because ");
501 if ( !(caps & 3) )
502 printk("no DDC retrieval method detected\n");
503 else if ( (caps >> 8) > 5 )
504 printk("takes longer than 5 seconds\n");
505 else
506 printk("of reasons unknown\n");
507 }
508 }
510 printk("Disc information:\n");
511 printk(" Found %d MBR signatures\n",
512 bootsym(boot_mbr_signature_nr));
513 printk(" Found %d EDD information structures\n",
514 bootsym(boot_edd_info_nr));
516 /* Check that we have at least one Multiboot module. */
517 if ( !(mbi->flags & MBI_MODULES) || (mbi->mods_count == 0) )
518 EARLY_FAIL("dom0 kernel not specified. "
519 "Check bootloader configuration.\n");
521 if ( ((unsigned long)cpu0_stack & (STACK_SIZE-1)) != 0 )
522 EARLY_FAIL("Misaligned CPU0 stack.\n");
524 /*
525 * Since there are some stubs getting built on the stacks which use
526 * direct calls/jumps, the heap must be confined to the lower 2G so
527 * that those branches can reach their targets.
528 */
529 if ( opt_xenheap_megabytes > 2048 )
530 opt_xenheap_megabytes = 2048;
532 if ( e820_raw_nr != 0 )
533 {
534 memmap_type = "Xen-e820";
535 }
536 else if ( bootsym(lowmem_kb) )
537 {
538 memmap_type = "Xen-e801";
539 e820_raw[0].addr = 0;
540 e820_raw[0].size = bootsym(lowmem_kb) << 10;
541 e820_raw[0].type = E820_RAM;
542 e820_raw[1].addr = 0x100000;
543 e820_raw[1].size = bootsym(highmem_kb) << 10;
544 e820_raw[1].type = E820_RAM;
545 e820_raw_nr = 2;
546 }
547 else if ( mbi->flags & MBI_MEMMAP )
548 {
549 memmap_type = "Multiboot-e820";
550 while ( (bytes < mbi->mmap_length) && (e820_raw_nr < E820MAX) )
551 {
552 memory_map_t *map = __va(mbi->mmap_addr + bytes);
554 /*
555 * This is a gross workaround for a BIOS bug. Some bootloaders do
556 * not write e820 map entries into pre-zeroed memory. This is
557 * okay if the BIOS fills in all fields of the map entry, but
558 * some broken BIOSes do not bother to write the high word of
559 * the length field if the length is smaller than 4GB. We
560 * detect and fix this by flagging sections below 4GB that
561 * appear to be larger than 4GB in size.
562 */
563 if ( (map->base_addr_high == 0) && (map->length_high != 0) )
564 {
565 if ( !e820_warn )
566 {
567 printk("WARNING: Buggy e820 map detected and fixed "
568 "(truncated length fields).\n");
569 e820_warn = 1;
570 }
571 map->length_high = 0;
572 }
574 e820_raw[e820_raw_nr].addr =
575 ((u64)map->base_addr_high << 32) | (u64)map->base_addr_low;
576 e820_raw[e820_raw_nr].size =
577 ((u64)map->length_high << 32) | (u64)map->length_low;
578 e820_raw[e820_raw_nr].type = map->type;
579 e820_raw_nr++;
581 bytes += map->size + 4;
582 }
583 }
584 else if ( mbi->flags & MBI_MEMLIMITS )
585 {
586 memmap_type = "Multiboot-e801";
587 e820_raw[0].addr = 0;
588 e820_raw[0].size = mbi->mem_lower << 10;
589 e820_raw[0].type = E820_RAM;
590 e820_raw[1].addr = 0x100000;
591 e820_raw[1].size = mbi->mem_upper << 10;
592 e820_raw[1].type = E820_RAM;
593 e820_raw_nr = 2;
594 }
595 else
596 {
597 EARLY_FAIL("Bootloader provided no memory information.\n");
598 }
600 /* Sanitise the raw E820 map to produce a final clean version. */
601 max_page = init_e820(memmap_type, e820_raw, &e820_raw_nr);
603 /* Create a temporary copy of the E820 map. */
604 memcpy(&boot_e820, &e820, sizeof(e820));
606 /* Early kexec reservation (explicit static start address). */
607 kexec_reserve_area(&boot_e820);
609 /*
610 * Iterate backwards over all superpage-aligned RAM regions.
611 *
612 * We require superpage alignment because the boot allocator is not yet
613 * initialised. Hence we can only map superpages in the address range
614 * 0 to BOOTSTRAP_DIRECTMAP_END, as this is guaranteed not to require
615 * dynamic allocation of pagetables.
616 *
617 * As well as mapping superpages in that range, in preparation for
618 * initialising the boot allocator, we also look for a region to which
619 * we can relocate the dom0 kernel and other multiboot modules. Also, on
620 * x86/64, we relocate Xen to higher memory.
621 */
622 modules_length = mod[mbi->mods_count-1].mod_end - mod[0].mod_start;
623 for ( i = boot_e820.nr_map-1; i >= 0; i-- )
624 {
625 uint64_t s, e, mask = (1UL << L2_PAGETABLE_SHIFT) - 1;
627 /* Superpage-aligned chunks from 16MB to BOOTSTRAP_DIRECTMAP_END. */
628 s = (boot_e820.map[i].addr + mask) & ~mask;
629 e = (boot_e820.map[i].addr + boot_e820.map[i].size) & ~mask;
630 s = max_t(uint64_t, s, 16 << 20);
631 e = min_t(uint64_t, e, BOOTSTRAP_DIRECTMAP_END);
632 if ( (boot_e820.map[i].type != E820_RAM) || (s >= e) )
633 continue;
635 /* Map the chunk. No memory will need to be allocated to do this. */
636 map_pages_to_xen(
637 (unsigned long)maddr_to_bootstrap_virt(s),
638 s >> PAGE_SHIFT, (e-s) >> PAGE_SHIFT, PAGE_HYPERVISOR);
640 #if defined(CONFIG_X86_64)
641 /* Is the region suitable for relocating Xen? */
642 if ( !xen_phys_start && (((e-s) >> 20) >= opt_xenheap_megabytes) )
643 {
644 extern l2_pgentry_t l2_xenmap[];
645 l4_pgentry_t *pl4e;
646 l3_pgentry_t *pl3e;
647 l2_pgentry_t *pl2e;
648 int i, j, k;
650 /* Select relocation address. */
651 e = (e - (opt_xenheap_megabytes << 20)) & ~mask;
652 xen_phys_start = e;
653 bootsym(trampoline_xen_phys_start) = e;
655 /*
656 * Perform relocation to new physical address.
657 * Before doing so we must sync static/global data with main memory
658 * with a barrier(). After this we must *not* modify static/global
659 * data until after we have switched to the relocated pagetables!
660 */
661 barrier();
662 move_memory(e, 0, __pa(&_end) - xen_phys_start);
664 /* Poison low 1MB to detect stray pointers to physical 0-1MB. */
665 memset(maddr_to_bootstrap_virt(e), 0x55, 1U<<20);
667 /* Walk initial pagetables, relocating page directory entries. */
668 pl4e = __va(__pa(idle_pg_table));
669 for ( i = 0 ; i < L4_PAGETABLE_ENTRIES; i++, pl4e++ )
670 {
671 if ( !(l4e_get_flags(*pl4e) & _PAGE_PRESENT) )
672 continue;
673 *pl4e = l4e_from_intpte(l4e_get_intpte(*pl4e) +
674 xen_phys_start);
675 pl3e = l4e_to_l3e(*pl4e);
676 for ( j = 0; j < L3_PAGETABLE_ENTRIES; j++, pl3e++ )
677 {
678 /* Not present, 1GB mapping, or already relocated? */
679 if ( !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) ||
680 (l3e_get_flags(*pl3e) & _PAGE_PSE) ||
681 (l3e_get_pfn(*pl3e) > 0x1000) )
682 continue;
683 *pl3e = l3e_from_intpte(l3e_get_intpte(*pl3e) +
684 xen_phys_start);
685 pl2e = l3e_to_l2e(*pl3e);
686 for ( k = 0; k < L2_PAGETABLE_ENTRIES; k++, pl2e++ )
687 {
688 /* Not present, PSE, or already relocated? */
689 if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ||
690 (l2e_get_flags(*pl2e) & _PAGE_PSE) ||
691 (l2e_get_pfn(*pl2e) > 0x1000) )
692 continue;
693 *pl2e = l2e_from_intpte(l2e_get_intpte(*pl2e) +
694 xen_phys_start);
695 }
696 }
697 }
699 /* The only data mappings to be relocated are in the Xen area. */
700 pl2e = __va(__pa(l2_xenmap));
701 *pl2e++ = l2e_from_pfn(xen_phys_start >> PAGE_SHIFT,
702 PAGE_HYPERVISOR | _PAGE_PSE);
703 for ( i = 1; i < L2_PAGETABLE_ENTRIES; i++, pl2e++ )
704 {
705 if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) )
706 continue;
707 *pl2e = l2e_from_intpte(l2e_get_intpte(*pl2e) +
708 xen_phys_start);
709 }
711 /* Re-sync the stack and then switch to relocated pagetables. */
712 asm volatile (
713 "rep movsb ; " /* re-sync the stack */
714 "movq %%cr4,%%rsi ; "
715 "andb $0x7f,%%sil ; "
716 "movq %%rsi,%%cr4 ; " /* CR4.PGE == 0 */
717 "movq %0,%%cr3 ; " /* CR3 == new pagetables */
718 "orb $0x80,%%sil ; "
719 "movq %%rsi,%%cr4 " /* CR4.PGE == 1 */
720 : : "r" (__pa(idle_pg_table)), "S" (cpu0_stack),
721 "D" (__va(__pa(cpu0_stack))), "c" (STACK_SIZE) : "memory" );
722 }
723 #endif
725 /* Is the region suitable for relocating the multiboot modules? */
726 if ( !initial_images_start && (s < e) && ((e-s) >= modules_length) )
727 {
728 initial_images_end = e;
729 e = (e - modules_length) & PAGE_MASK;
730 initial_images_start = e;
731 move_memory(initial_images_start,
732 mod[0].mod_start, mod[mbi->mods_count-1].mod_end);
733 }
735 if ( !kexec_crash_area.start && (s < e) &&
736 ((e-s) >= kexec_crash_area.size) )
737 {
738 e = (e - kexec_crash_area.size) & PAGE_MASK;
739 kexec_crash_area.start = e;
740 }
741 }
743 if ( !initial_images_start )
744 EARLY_FAIL("Not enough memory to relocate the dom0 kernel image.\n");
745 reserve_e820_ram(&boot_e820, initial_images_start, initial_images_end);
747 /* Initialise Xen heap and boot heap. */
748 xenheap_phys_start = init_boot_allocator(__pa(&_end));
749 xenheap_phys_end = opt_xenheap_megabytes << 20;
750 #if defined(CONFIG_X86_64)
751 if ( !xen_phys_start )
752 EARLY_FAIL("Not enough memory to relocate Xen.\n");
753 xenheap_phys_end += xen_phys_start;
754 reserve_e820_ram(&boot_e820, xen_phys_start,
755 xen_phys_start + (opt_xenheap_megabytes<<20));
756 #endif
758 /* Late kexec reservation (dynamic start address). */
759 kexec_reserve_area(&boot_e820);
761 /*
762 * With the boot allocator now initialised, we can walk every RAM region
763 * and map it in its entirety (on x86/64, at least) and notify it to the
764 * boot allocator.
765 */
766 for ( i = 0; i < boot_e820.nr_map; i++ )
767 {
768 uint64_t s, e, map_s, map_e, mask = PAGE_SIZE - 1;
770 /* Only page alignment required now. */
771 s = (boot_e820.map[i].addr + mask) & ~mask;
772 e = (boot_e820.map[i].addr + boot_e820.map[i].size) & ~mask;
773 #if defined(CONFIG_X86_32)
774 s = max_t(uint64_t, s, xenheap_phys_end);
775 #else
776 s = max_t(uint64_t, s, 1<<20);
777 #endif
778 if ( (boot_e820.map[i].type != E820_RAM) || (s >= e) )
779 continue;
781 /* Need to create mappings above 16MB. */
782 map_s = max_t(uint64_t, s, 16<<20);
783 map_e = e;
784 #if defined(CONFIG_X86_32) /* mappings are truncated on x86_32 */
785 map_e = min_t(uint64_t, map_e, BOOTSTRAP_DIRECTMAP_END);
786 #endif
788 /* Pass mapped memory to allocator /before/ creating new mappings. */
789 init_boot_pages(s, min_t(uint64_t, map_s, e));
791 /* Create new mappings /before/ passing memory to the allocator. */
792 if ( map_s < map_e )
793 map_pages_to_xen(
794 (unsigned long)maddr_to_bootstrap_virt(map_s),
795 map_s >> PAGE_SHIFT, (map_e-map_s) >> PAGE_SHIFT,
796 PAGE_HYPERVISOR);
798 /* Pass remainder of this memory chunk to the allocator. */
799 init_boot_pages(map_s, e);
800 }
802 memguard_init();
804 nr_pages = 0;
805 for ( i = 0; i < e820.nr_map; i++ )
806 if ( e820.map[i].type == E820_RAM )
807 nr_pages += e820.map[i].size >> PAGE_SHIFT;
808 printk("System RAM: %luMB (%lukB)\n",
809 nr_pages >> (20 - PAGE_SHIFT),
810 nr_pages << (PAGE_SHIFT - 10));
811 total_pages = nr_pages;
813 /* Sanity check for unwanted bloat of certain hypercall structures. */
814 BUILD_BUG_ON(sizeof(((struct xen_platform_op *)0)->u) !=
815 sizeof(((struct xen_platform_op *)0)->u.pad));
816 BUILD_BUG_ON(sizeof(((struct xen_domctl *)0)->u) !=
817 sizeof(((struct xen_domctl *)0)->u.pad));
818 BUILD_BUG_ON(sizeof(((struct xen_sysctl *)0)->u) !=
819 sizeof(((struct xen_sysctl *)0)->u.pad));
821 BUILD_BUG_ON(sizeof(start_info_t) > PAGE_SIZE);
822 BUILD_BUG_ON(sizeof(shared_info_t) > PAGE_SIZE);
823 BUILD_BUG_ON(sizeof(struct vcpu_info) != 64);
825 #ifdef CONFIG_COMPAT
826 BUILD_BUG_ON(sizeof(((struct compat_platform_op *)0)->u) !=
827 sizeof(((struct compat_platform_op *)0)->u.pad));
828 BUILD_BUG_ON(sizeof(start_info_compat_t) > PAGE_SIZE);
829 BUILD_BUG_ON(sizeof(struct compat_vcpu_info) != 64);
830 #endif
832 /* Check definitions in public headers match internal defs. */
833 BUILD_BUG_ON(__HYPERVISOR_VIRT_START != HYPERVISOR_VIRT_START);
834 #ifdef HYPERVISOR_VIRT_END
835 BUILD_BUG_ON(__HYPERVISOR_VIRT_END != HYPERVISOR_VIRT_END);
836 #endif
837 BUILD_BUG_ON(MACH2PHYS_VIRT_START != RO_MPT_VIRT_START);
838 BUILD_BUG_ON(MACH2PHYS_VIRT_END != RO_MPT_VIRT_END);
840 init_frametable();
842 acpi_boot_table_init();
844 acpi_numa_init();
846 numa_initmem_init(0, max_page);
848 /* Initialise the Xen heap, skipping RAM holes. */
849 init_xenheap_pages(xenheap_phys_start, xenheap_phys_end);
850 nr_pages = (xenheap_phys_end - xenheap_phys_start) >> PAGE_SHIFT;
851 #ifdef __x86_64__
852 init_xenheap_pages(xen_phys_start, __pa(&_start));
853 nr_pages += (__pa(&_start) - xen_phys_start) >> PAGE_SHIFT;
854 vesa_init();
855 #endif
856 xenheap_phys_start = xen_phys_start;
857 printk("Xen heap: %luMB (%lukB)\n",
858 nr_pages >> (20 - PAGE_SHIFT),
859 nr_pages << (PAGE_SHIFT - 10));
861 end_boot_allocator();
863 early_boot = 0;
865 softirq_init();
867 early_cpu_init();
869 paging_init();
871 tboot_probe();
873 /* Unmap the first page of CPU0's stack. */
874 memguard_guard_stack(cpu0_stack);
876 open_softirq(NEW_TLBFLUSH_CLOCK_PERIOD_SOFTIRQ, new_tlbflush_clock_period);
878 if ( opt_watchdog )
879 nmi_watchdog = NMI_LOCAL_APIC;
881 sort_exception_tables();
883 find_smp_config();
885 dmi_scan_machine();
887 generic_apic_probe();
889 if ( x2apic_is_available() )
890 enable_x2apic();
892 acpi_boot_init();
894 init_cpu_to_node();
896 if ( smp_found_config )
897 get_smp_config();
899 #ifdef CONFIG_X86_64
900 /* Low mappings were only needed for some BIOS table parsing. */
901 zap_low_mappings();
902 #endif
904 init_apic_mappings();
906 init_IRQ();
908 percpu_init_areas();
910 xsm_init(&initrdidx, mbi, initial_images_start);
912 init_idle_domain();
914 trap_init();
916 rcu_init();
918 timer_init();
920 early_time_init();
922 arch_init_memory();
924 identify_cpu(&boot_cpu_data);
925 if ( cpu_has_fxsr )
926 set_in_cr4(X86_CR4_OSFXSR);
927 if ( cpu_has_xmm )
928 set_in_cr4(X86_CR4_OSXMMEXCPT);
929 #ifdef CONFIG_X86_64
930 vesa_mtrr_init();
931 #endif
933 if ( opt_nosmp )
934 max_cpus = 0;
936 smp_prepare_cpus(max_cpus);
938 /*
939 * Initialise higher-level timer functions. We do this fairly late
940 * (post-SMP) because the time bases and scale factors need to be updated
941 * regularly, and SMP initialisation can cause a long delay with
942 * interrupts not yet enabled.
943 */
944 init_xen_time();
946 initialize_keytable();
948 serial_init_postirq();
950 BUG_ON(!local_irq_is_enabled());
952 for_each_present_cpu ( i )
953 {
954 if ( num_online_cpus() >= max_cpus )
955 break;
956 if ( !cpu_online(i) )
957 {
958 rcu_online_cpu(i);
959 __cpu_up(i);
960 }
962 /* Set up cpu_to_node[]. */
963 srat_detect_node(i);
964 /* Set up node_to_cpumask based on cpu_to_node[]. */
965 numa_add_cpu(i);
966 }
968 printk("Brought up %ld CPUs\n", (long)num_online_cpus());
969 smp_cpus_done(max_cpus);
971 initialise_gdb(); /* could be moved earlier */
973 do_initcalls();
975 if ( opt_watchdog )
976 watchdog_enable();
978 /* Create initial domain 0. */
979 dom0 = domain_create(0, 0, DOM0_SSIDREF);
980 if ( (dom0 == NULL) || (alloc_vcpu(dom0, 0, 0) == NULL) )
981 panic("Error creating domain 0\n");
983 dom0->is_privileged = 1;
984 dom0->target = NULL;
986 /* Grab the DOM0 command line. */
987 cmdline = (char *)(mod[0].string ? __va(mod[0].string) : NULL);
988 if ( (cmdline != NULL) || (kextra != NULL) )
989 {
990 static char dom0_cmdline[MAX_GUEST_CMDLINE];
991 char xen_pm_param[32];
993 cmdline = cmdline_cook(cmdline);
994 safe_strcpy(dom0_cmdline, cmdline);
996 if ( kextra != NULL )
997 /* kextra always includes exactly one leading space. */
998 safe_strcat(dom0_cmdline, kextra);
1000 /* Append any extra parameters. */
1001 if ( skip_ioapic_setup && !strstr(dom0_cmdline, "noapic") )
1002 safe_strcat(dom0_cmdline, " noapic");
1003 if ( acpi_skip_timer_override &&
1004 !strstr(dom0_cmdline, "acpi_skip_timer_override") )
1005 safe_strcat(dom0_cmdline, " acpi_skip_timer_override");
1006 if ( (strlen(acpi_param) == 0) && acpi_disabled )
1008 printk("ACPI is disabled, notifying Domain 0 (acpi=off)\n");
1009 safe_strcpy(acpi_param, "off");
1011 if ( (strlen(acpi_param) != 0) && !strstr(dom0_cmdline, "acpi=") )
1013 safe_strcat(dom0_cmdline, " acpi=");
1014 safe_strcat(dom0_cmdline, acpi_param);
1016 if ( xen_cpuidle )
1017 xen_processor_pmbits |= XEN_PROCESSOR_PM_CX;
1019 snprintf(xen_pm_param, sizeof(xen_pm_param),
1020 " xen_processor_pmbits=%d", xen_processor_pmbits);
1022 if ( !strstr(dom0_cmdline, "xen_processor_pmbits=") )
1023 safe_strcat(dom0_cmdline, xen_pm_param);
1025 cmdline = dom0_cmdline;
1028 if ( (initrdidx > 0) && (initrdidx < mbi->mods_count) )
1030 _initrd_start = initial_images_start +
1031 (mod[initrdidx].mod_start - mod[0].mod_start);
1032 _initrd_len = mod[initrdidx].mod_end - mod[initrdidx].mod_start;
1035 /*
1036 * We're going to setup domain0 using the module(s) that we stashed safely
1037 * above our heap. The second module, if present, is an initrd ramdisk.
1038 */
1039 if ( construct_dom0(dom0,
1040 initial_images_start,
1041 mod[0].mod_end-mod[0].mod_start,
1042 _initrd_start,
1043 _initrd_len,
1044 cmdline) != 0)
1045 panic("Could not set up DOM0 guest OS\n");
1047 /* Scrub RAM that is still free and so may go to an unprivileged domain. */
1048 scrub_heap_pages();
1050 init_trace_bufs();
1052 console_endboot();
1054 /* Hide UART from DOM0 if we're using it */
1055 serial_endboot();
1057 domain_unpause_by_systemcontroller(dom0);
1059 reset_stack_and_jump(init_done);
1062 void arch_get_xen_caps(xen_capabilities_info_t *info)
1064 /* Interface name is always xen-3.0-* for Xen-3.x. */
1065 int major = 3, minor = 0;
1066 char s[32];
1068 (*info)[0] = '\0';
1070 #if defined(CONFIG_X86_32)
1072 snprintf(s, sizeof(s), "xen-%d.%d-x86_32p ", major, minor);
1073 safe_strcat(*info, s);
1074 if ( hvm_enabled )
1076 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32 ", major, minor);
1077 safe_strcat(*info, s);
1078 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32p ", major, minor);
1079 safe_strcat(*info, s);
1082 #elif defined(CONFIG_X86_64)
1084 snprintf(s, sizeof(s), "xen-%d.%d-x86_64 ", major, minor);
1085 safe_strcat(*info, s);
1086 #ifdef CONFIG_COMPAT
1087 snprintf(s, sizeof(s), "xen-%d.%d-x86_32p ", major, minor);
1088 safe_strcat(*info, s);
1089 #endif
1090 if ( hvm_enabled )
1092 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32 ", major, minor);
1093 safe_strcat(*info, s);
1094 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32p ", major, minor);
1095 safe_strcat(*info, s);
1096 snprintf(s, sizeof(s), "hvm-%d.%d-x86_64 ", major, minor);
1097 safe_strcat(*info, s);
1100 #endif
1103 int xen_in_range(paddr_t start, paddr_t end)
1105 start = max_t(paddr_t, start, xenheap_phys_start);
1106 end = min_t(paddr_t, end, xenheap_phys_end);
1108 return start < end;
1111 /*
1112 * Local variables:
1113 * mode: C
1114 * c-set-style: "BSD"
1115 * c-basic-offset: 4
1116 * tab-width: 4
1117 * indent-tabs-mode: nil
1118 * End:
1119 */