direct-io.hg

view xen/arch/x86/setup.c @ 15454:83cbda5c1e1b

x86-64: bump STACK_SIZE to 32 so that trampoline and IST stacks fit
without undue squeezing.
Signed-off-by: Jan Beulich <jbeulich@novell.com>
author kfraser@localhost.localdomain
date Tue Jul 03 11:41:25 2007 +0100 (2007-07-03)
parents 0900fb1a3693
children d7e3224b661a
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 <public/version.h>
23 #ifdef CONFIG_COMPAT
24 #include <compat/platform.h>
25 #include <compat/xen.h>
26 #endif
27 #include <asm/bitops.h>
28 #include <asm/smp.h>
29 #include <asm/processor.h>
30 #include <asm/mpspec.h>
31 #include <asm/apic.h>
32 #include <asm/desc.h>
33 #include <asm/paging.h>
34 #include <asm/e820.h>
35 #include <acm/acm_hooks.h>
36 #include <xen/kexec.h>
37 #include <asm/edd.h>
39 #if defined(CONFIG_X86_64)
40 #define BOOTSTRAP_DIRECTMAP_END (1UL << 32)
41 #define maddr_to_bootstrap_virt(m) maddr_to_virt(m)
42 #else
43 #define BOOTSTRAP_DIRECTMAP_END HYPERVISOR_VIRT_START
44 #define maddr_to_bootstrap_virt(m) ((void *)(long)(m))
45 #endif
47 extern void dmi_scan_machine(void);
48 extern void generic_apic_probe(void);
49 extern void numa_initmem_init(unsigned long start_pfn, unsigned long end_pfn);
51 extern u16 boot_edid_caps;
52 extern u8 boot_edid_info[128];
53 extern struct boot_video_info boot_vid_info;
55 /*
56 * opt_xenheap_megabytes: Size of Xen heap in megabytes, excluding the
57 * page_info table and allocation bitmap.
58 */
59 static unsigned int opt_xenheap_megabytes = XENHEAP_DEFAULT_MB;
60 #if defined(CONFIG_X86_64)
61 integer_param("xenheap_megabytes", opt_xenheap_megabytes);
62 #endif
64 /* opt_nosmp: If true, secondary processors are ignored. */
65 static int opt_nosmp = 0;
66 boolean_param("nosmp", opt_nosmp);
68 /* maxcpus: maximum number of CPUs to activate. */
69 static unsigned int max_cpus = NR_CPUS;
70 integer_param("maxcpus", max_cpus);
72 /* opt_watchdog: If true, run a watchdog NMI on each processor. */
73 static int opt_watchdog = 0;
74 boolean_param("watchdog", opt_watchdog);
76 /* **** Linux config option: propagated to domain0. */
77 /* "acpi=off": Sisables both ACPI table parsing and interpreter. */
78 /* "acpi=force": Override the disable blacklist. */
79 /* "acpi=strict": Disables out-of-spec workarounds. */
80 /* "acpi=ht": Limit ACPI just to boot-time to enable HT. */
81 /* "acpi=noirq": Disables ACPI interrupt routing. */
82 static void parse_acpi_param(char *s);
83 custom_param("acpi", parse_acpi_param);
85 /* **** Linux config option: propagated to domain0. */
86 /* acpi_skip_timer_override: Skip IRQ0 overrides. */
87 extern int acpi_skip_timer_override;
88 boolean_param("acpi_skip_timer_override", acpi_skip_timer_override);
90 /* **** Linux config option: propagated to domain0. */
91 /* noapic: Disable IOAPIC setup. */
92 extern int skip_ioapic_setup;
93 boolean_param("noapic", skip_ioapic_setup);
95 int early_boot = 1;
97 cpumask_t cpu_present_map;
99 unsigned long xen_phys_start;
101 /* Limits of Xen heap, used to initialise the allocator. */
102 unsigned long xenheap_phys_start, xenheap_phys_end;
104 extern void arch_init_memory(void);
105 extern void init_IRQ(void);
106 extern void trap_init(void);
107 extern void early_time_init(void);
108 extern void early_cpu_init(void);
110 struct tss_struct init_tss[NR_CPUS];
112 char __attribute__ ((__section__(".bss.stack_aligned"))) cpu0_stack[STACK_SIZE];
114 struct cpuinfo_x86 boot_cpu_data = { 0, 0, 0, 0, -1, 1, 0, 0, -1 };
116 #if CONFIG_PAGING_LEVELS > 2
117 unsigned long mmu_cr4_features = X86_CR4_PSE | X86_CR4_PGE | X86_CR4_PAE;
118 #else
119 unsigned long mmu_cr4_features = X86_CR4_PSE;
120 #endif
121 EXPORT_SYMBOL(mmu_cr4_features);
123 int acpi_disabled;
125 int acpi_force;
126 char acpi_param[10] = "";
127 static void __init parse_acpi_param(char *s)
128 {
129 /* Save the parameter so it can be propagated to domain0. */
130 safe_strcpy(acpi_param, s);
132 /* Interpret the parameter for use within Xen. */
133 if ( !strcmp(s, "off") )
134 {
135 disable_acpi();
136 }
137 else if ( !strcmp(s, "force") )
138 {
139 acpi_force = 1;
140 acpi_ht = 1;
141 acpi_disabled = 0;
142 }
143 else if ( !strcmp(s, "strict") )
144 {
145 acpi_strict = 1;
146 }
147 else if ( !strcmp(s, "ht") )
148 {
149 if ( !acpi_force )
150 disable_acpi();
151 acpi_ht = 1;
152 }
153 else if ( !strcmp(s, "noirq") )
154 {
155 acpi_noirq_set();
156 }
157 }
159 static void __init do_initcalls(void)
160 {
161 initcall_t *call;
162 for ( call = &__initcall_start; call < &__initcall_end; call++ )
163 (*call)();
164 }
166 #define EARLY_FAIL(f, a...) do { \
167 printk( f , ## a ); \
168 for ( ; ; ) __asm__ __volatile__ ( "hlt" ); \
169 } while (0)
171 static unsigned long __initdata initial_images_start, initial_images_end;
173 unsigned long __init initial_images_nrpages(void)
174 {
175 ASSERT(!(initial_images_start & ~PAGE_MASK));
176 ASSERT(!(initial_images_end & ~PAGE_MASK));
177 return ((initial_images_end >> PAGE_SHIFT) -
178 (initial_images_start >> PAGE_SHIFT));
179 }
181 void __init discard_initial_images(void)
182 {
183 init_domheap_pages(initial_images_start, initial_images_end);
184 }
186 extern char __per_cpu_start[], __per_cpu_data_end[], __per_cpu_end[];
188 static void __init percpu_init_areas(void)
189 {
190 unsigned int i, data_size = __per_cpu_data_end - __per_cpu_start;
191 unsigned int first_unused;
193 BUG_ON(data_size > PERCPU_SIZE);
195 /* Initialise per-cpu data area for all possible secondary CPUs. */
196 for ( i = 1; (i < NR_CPUS) && cpu_possible(i); i++ )
197 memcpy(__per_cpu_start + (i << PERCPU_SHIFT),
198 __per_cpu_start,
199 data_size);
200 first_unused = i;
202 /* Check that there are no holes in cpu_possible_map. */
203 for ( ; i < NR_CPUS; i++ )
204 BUG_ON(cpu_possible(i));
206 #ifndef MEMORY_GUARD
207 init_xenheap_pages(__pa(__per_cpu_start) + (first_unused << PERCPU_SHIFT),
208 __pa(__per_cpu_end));
209 #endif
210 memguard_guard_range(&__per_cpu_start[first_unused << PERCPU_SHIFT],
211 (NR_CPUS - first_unused) << PERCPU_SHIFT);
212 #if defined(CONFIG_X86_64)
213 /* Also zap the mapping in the 1:1 area. */
214 memguard_guard_range(__va(__pa(__per_cpu_start)) +
215 (first_unused << PERCPU_SHIFT),
216 (NR_CPUS - first_unused) << PERCPU_SHIFT);
217 #endif
218 }
220 /* Fetch acm policy module from multiboot modules. */
221 static void __init extract_acm_policy(
222 multiboot_info_t *mbi,
223 unsigned int *initrdidx,
224 char **_policy_start,
225 unsigned long *_policy_len)
226 {
227 int i;
228 module_t *mod = (module_t *)__va(mbi->mods_addr);
229 unsigned long start, policy_len;
230 char *policy_start;
232 /*
233 * Try all modules and see whichever could be the binary policy.
234 * Adjust the initrdidx if module[1] is the binary policy.
235 */
236 for ( i = mbi->mods_count-1; i >= 1; i-- )
237 {
238 start = initial_images_start + (mod[i].mod_start-mod[0].mod_start);
239 policy_start = maddr_to_bootstrap_virt(start);
240 policy_len = mod[i].mod_end - mod[i].mod_start;
241 if ( acm_is_policy(policy_start, policy_len) )
242 {
243 printk("Policy len 0x%lx, start at %p - module %d.\n",
244 policy_len, policy_start, i);
245 *_policy_start = policy_start;
246 *_policy_len = policy_len;
247 if ( i == 1 )
248 *initrdidx = (mbi->mods_count > 2) ? 2 : 0;
249 break;
250 }
251 }
252 }
254 static void __init init_idle_domain(void)
255 {
256 struct domain *idle_domain;
258 /* Domain creation requires that scheduler structures are initialised. */
259 scheduler_init();
261 idle_domain = domain_create(IDLE_DOMAIN_ID, 0, 0);
262 if ( (idle_domain == NULL) || (alloc_vcpu(idle_domain, 0, 0) == NULL) )
263 BUG();
265 set_current(idle_domain->vcpu[0]);
266 idle_vcpu[0] = this_cpu(curr_vcpu) = current;
268 setup_idle_pagetable();
269 }
271 static void __init srat_detect_node(int cpu)
272 {
273 unsigned node;
274 u8 apicid = x86_cpu_to_apicid[cpu];
276 node = apicid_to_node[apicid];
277 if ( node == NUMA_NO_NODE )
278 node = 0;
279 numa_set_node(cpu, node);
281 if ( acpi_numa > 0 )
282 printk(KERN_INFO "CPU %d APIC %d -> Node %d\n", cpu, apicid, node);
283 }
285 static void __init move_memory(
286 unsigned long dst, unsigned long src_start, unsigned long src_end)
287 {
288 memmove(maddr_to_bootstrap_virt(dst),
289 maddr_to_bootstrap_virt(src_start),
290 src_end - src_start);
291 }
293 /* A temporary copy of the e820 map that we can mess with during bootstrap. */
294 static struct e820map __initdata boot_e820;
296 /* Reserve area (@s,@e) in the temporary bootstrap e820 map. */
297 static int __init reserve_in_boot_e820(unsigned long s, unsigned long e)
298 {
299 uint64_t rs, re;
300 int i;
302 for ( i = 0; i < boot_e820.nr_map; i++ )
303 {
304 /* Have we found the e820 region that includes the specified range? */
305 rs = boot_e820.map[i].addr;
306 re = rs + boot_e820.map[i].size;
307 if ( (s >= rs) && (e <= re) )
308 goto found;
309 }
311 return 0;
313 found:
314 /* Start fragment. */
315 boot_e820.map[i].size = s - rs;
317 /* End fragment. */
318 if ( e < re )
319 {
320 memmove(&boot_e820.map[i+1], &boot_e820.map[i],
321 (boot_e820.nr_map-i) * sizeof(boot_e820.map[0]));
322 boot_e820.nr_map++;
323 i++;
324 boot_e820.map[i].addr = e;
325 boot_e820.map[i].size = re - e;
326 }
328 return 1;
329 }
331 struct boot_video_info {
332 u8 orig_x; /* 0x00 */
333 u8 orig_y; /* 0x01 */
334 u8 orig_video_mode; /* 0x02 */
335 u8 orig_video_cols; /* 0x03 */
336 u8 orig_video_lines; /* 0x04 */
337 u8 orig_video_isVGA; /* 0x05 */
338 u16 orig_video_points; /* 0x06 */
340 /* VESA graphic mode -- linear frame buffer */
341 u32 capabilities; /* 0x08 */
342 u16 lfb_linelength; /* 0x0c */
343 u16 lfb_width; /* 0x0e */
344 u16 lfb_height; /* 0x10 */
345 u16 lfb_depth; /* 0x12 */
346 u32 lfb_base; /* 0x14 */
347 u32 lfb_size; /* 0x18 */
348 u8 red_size; /* 0x1c */
349 u8 red_pos; /* 0x1d */
350 u8 green_size; /* 0x1e */
351 u8 green_pos; /* 0x1f */
352 u8 blue_size; /* 0x20 */
353 u8 blue_pos; /* 0x21 */
354 u8 rsvd_size; /* 0x22 */
355 u8 rsvd_pos; /* 0x23 */
356 u16 vesapm_seg; /* 0x24 */
357 u16 vesapm_off; /* 0x26 */
358 };
360 static void __init parse_video_info(void)
361 {
362 struct boot_video_info *bvi = &bootsym(boot_vid_info);
364 if ( (bvi->orig_video_isVGA == 1) && (bvi->orig_video_mode == 3) )
365 {
366 vga_console_info.video_type = XEN_VGATYPE_TEXT_MODE_3;
367 vga_console_info.u.text_mode_3.font_height = bvi->orig_video_points;
368 vga_console_info.u.text_mode_3.cursor_x = bvi->orig_x;
369 vga_console_info.u.text_mode_3.cursor_y = bvi->orig_y;
370 vga_console_info.u.text_mode_3.rows = bvi->orig_video_lines;
371 vga_console_info.u.text_mode_3.columns = bvi->orig_video_cols;
372 }
373 else if ( bvi->orig_video_isVGA == 0x23 )
374 {
375 vga_console_info.video_type = XEN_VGATYPE_VESA_LFB;
376 vga_console_info.u.vesa_lfb.width = bvi->lfb_width;
377 vga_console_info.u.vesa_lfb.height = bvi->lfb_height;
378 vga_console_info.u.vesa_lfb.bytes_per_line = bvi->lfb_linelength;
379 vga_console_info.u.vesa_lfb.bits_per_pixel = bvi->lfb_depth;
380 vga_console_info.u.vesa_lfb.lfb_base = bvi->lfb_base;
381 vga_console_info.u.vesa_lfb.lfb_size = bvi->lfb_size;
382 vga_console_info.u.vesa_lfb.red_pos = bvi->red_pos;
383 vga_console_info.u.vesa_lfb.red_size = bvi->red_size;
384 vga_console_info.u.vesa_lfb.green_pos = bvi->green_pos;
385 vga_console_info.u.vesa_lfb.green_size = bvi->green_size;
386 vga_console_info.u.vesa_lfb.blue_pos = bvi->blue_pos;
387 vga_console_info.u.vesa_lfb.blue_size = bvi->blue_size;
388 vga_console_info.u.vesa_lfb.rsvd_pos = bvi->rsvd_pos;
389 vga_console_info.u.vesa_lfb.rsvd_size = bvi->rsvd_size;
390 }
391 }
393 void init_done(void)
394 {
395 extern char __init_begin[], __init_end[];
397 /* Free (or page-protect) the init areas. */
398 #ifndef MEMORY_GUARD
399 init_xenheap_pages(__pa(__init_begin), __pa(__init_end));
400 #endif
401 memguard_guard_range(__init_begin, __init_end - __init_begin);
402 #if defined(CONFIG_X86_64)
403 /* Also zap the mapping in the 1:1 area. */
404 memguard_guard_range(__va(__pa(__init_begin)), __init_end - __init_begin);
405 #endif
406 printk("Freed %ldkB init memory.\n", (long)(__init_end-__init_begin)>>10);
408 startup_cpu_idle_loop();
409 }
411 void __init __start_xen(unsigned long mbi_p)
412 {
413 char *memmap_type = NULL;
414 char __cmdline[] = "", *cmdline = __cmdline, *kextra;
415 unsigned long _initrd_start = 0, _initrd_len = 0;
416 unsigned int initrdidx = 1;
417 char *_policy_start = NULL;
418 unsigned long _policy_len = 0;
419 multiboot_info_t *mbi = __va(mbi_p);
420 module_t *mod = (module_t *)__va(mbi->mods_addr);
421 unsigned long nr_pages, modules_length;
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 if ( (mbi->flags & MBI_CMDLINE) && (mbi->cmdline != 0) )
434 cmdline = __va(mbi->cmdline);
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() */
454 smp_prepare_boot_cpu();
456 /* We initialise the serial devices very early so we can get debugging. */
457 ns16550.io_base = 0x3f8;
458 ns16550.irq = 4;
459 ns16550_init(0, &ns16550);
460 ns16550.io_base = 0x2f8;
461 ns16550.irq = 3;
462 ns16550_init(1, &ns16550);
463 serial_init_preirq();
465 init_console();
467 printk("Command line: %s\n", cmdline);
469 printk("Video information:\n");
471 /* Print VGA display mode information. */
472 switch ( vga_console_info.video_type )
473 {
474 case XEN_VGATYPE_TEXT_MODE_3:
475 printk(" VGA is text mode %dx%d, font 8x%d\n",
476 vga_console_info.u.text_mode_3.columns,
477 vga_console_info.u.text_mode_3.rows,
478 vga_console_info.u.text_mode_3.font_height);
479 break;
480 case XEN_VGATYPE_VESA_LFB:
481 printk(" VGA is graphics mode %dx%d, %d bpp\n",
482 vga_console_info.u.vesa_lfb.width,
483 vga_console_info.u.vesa_lfb.height,
484 vga_console_info.u.vesa_lfb.bits_per_pixel);
485 break;
486 default:
487 printk(" No VGA detected\n");
488 break;
489 }
491 /* Print VBE/DDC EDID information. */
492 if ( bootsym(boot_edid_caps) != 0x1313 )
493 {
494 u16 caps = bootsym(boot_edid_caps);
495 printk(" VBE/DDC methods:%s%s%s; ",
496 (caps & 1) ? " V1" : "",
497 (caps & 2) ? " V2" : "",
498 !(caps & 3) ? " none" : "");
499 printk("EDID transfer time: %d seconds\n", caps >> 8);
500 if ( *(u32 *)bootsym(boot_edid_info) == 0x13131313 )
501 {
502 printk(" EDID info not retrieved because ");
503 if ( !(caps & 3) )
504 printk("no DDC retrieval method detected\n");
505 else if ( (caps >> 8) > 5 )
506 printk("takes longer than 5 seconds\n");
507 else
508 printk("of reasons unknown\n");
509 }
510 }
512 printk("Disc information:\n");
513 printk(" Found %d MBR signatures\n",
514 bootsym(boot_mbr_signature_nr));
515 printk(" Found %d EDD information structures\n",
516 bootsym(boot_edd_info_nr));
518 /* Check that we have at least one Multiboot module. */
519 if ( !(mbi->flags & MBI_MODULES) || (mbi->mods_count == 0) )
520 EARLY_FAIL("dom0 kernel not specified. "
521 "Check bootloader configuration.\n");
523 if ( ((unsigned long)cpu0_stack & (STACK_SIZE-1)) != 0 )
524 EARLY_FAIL("Misaligned CPU0 stack.\n");
526 /*
527 * Since there are some stubs getting built on the stacks which use
528 * direct calls/jumps, the heap must be confined to the lower 2G so
529 * that those branches can reach their targets.
530 */
531 if ( opt_xenheap_megabytes > 2048 )
532 opt_xenheap_megabytes = 2048;
534 if ( e820_raw_nr != 0 )
535 {
536 memmap_type = "Xen-e820";
537 }
538 else if ( bootsym(lowmem_kb) )
539 {
540 memmap_type = "Xen-e801";
541 e820_raw[0].addr = 0;
542 e820_raw[0].size = bootsym(lowmem_kb) << 10;
543 e820_raw[0].type = E820_RAM;
544 e820_raw[1].addr = 0x100000;
545 e820_raw[1].size = bootsym(highmem_kb) << 10;
546 e820_raw[1].type = E820_RAM;
547 e820_raw_nr = 2;
548 }
549 else if ( mbi->flags & MBI_MEMMAP )
550 {
551 memmap_type = "Multiboot-e820";
552 while ( bytes < mbi->mmap_length )
553 {
554 memory_map_t *map = __va(mbi->mmap_addr + bytes);
556 /*
557 * This is a gross workaround for a BIOS bug. Some bootloaders do
558 * not write e820 map entries into pre-zeroed memory. This is
559 * okay if the BIOS fills in all fields of the map entry, but
560 * some broken BIOSes do not bother to write the high word of
561 * the length field if the length is smaller than 4GB. We
562 * detect and fix this by flagging sections below 4GB that
563 * appear to be larger than 4GB in size.
564 */
565 if ( (map->base_addr_high == 0) && (map->length_high != 0) )
566 {
567 if ( !e820_warn )
568 {
569 printk("WARNING: Buggy e820 map detected and fixed "
570 "(truncated length fields).\n");
571 e820_warn = 1;
572 }
573 map->length_high = 0;
574 }
576 e820_raw[e820_raw_nr].addr =
577 ((u64)map->base_addr_high << 32) | (u64)map->base_addr_low;
578 e820_raw[e820_raw_nr].size =
579 ((u64)map->length_high << 32) | (u64)map->length_low;
580 e820_raw[e820_raw_nr].type =
581 (map->type > E820_NVS) ? E820_RESERVED : map->type;
582 e820_raw_nr++;
584 bytes += map->size + 4;
585 }
586 }
587 else if ( mbi->flags & MBI_MEMLIMITS )
588 {
589 memmap_type = "Multiboot-e801";
590 e820_raw[0].addr = 0;
591 e820_raw[0].size = mbi->mem_lower << 10;
592 e820_raw[0].type = E820_RAM;
593 e820_raw[1].addr = 0x100000;
594 e820_raw[1].size = mbi->mem_upper << 10;
595 e820_raw[1].type = E820_RAM;
596 e820_raw_nr = 2;
597 }
598 else
599 {
600 EARLY_FAIL("Bootloader provided no memory information.\n");
601 }
603 /* Ensure that all E820 RAM regions are page-aligned and -sized. */
604 for ( i = 0; i < e820_raw_nr; i++ )
605 {
606 uint64_t s, e;
608 if ( e820_raw[i].type != E820_RAM )
609 continue;
610 s = PFN_UP(e820_raw[i].addr);
611 e = PFN_DOWN(e820_raw[i].addr + e820_raw[i].size);
612 e820_raw[i].size = 0; /* discarded later */
613 if ( s < e )
614 {
615 e820_raw[i].addr = s << PAGE_SHIFT;
616 e820_raw[i].size = (e - s) << PAGE_SHIFT;
617 }
618 }
620 /* Sanitise the raw E820 map to produce a final clean version. */
621 max_page = init_e820(memmap_type, e820_raw, &e820_raw_nr);
623 /*
624 * Create a temporary copy of the E820 map. Truncate it to above 16MB
625 * as anything below that is already mapped and has a statically-allocated
626 * purpose.
627 */
628 memcpy(&boot_e820, &e820, sizeof(e820));
629 for ( i = 0; i < boot_e820.nr_map; i++ )
630 {
631 uint64_t s, e, min = 16 << 20; /* 16MB */
632 s = boot_e820.map[i].addr;
633 e = boot_e820.map[i].addr + boot_e820.map[i].size;
634 if ( s >= min )
635 continue;
636 if ( e > min )
637 {
638 boot_e820.map[i].addr = min;
639 boot_e820.map[i].size = e - min;
640 }
641 else
642 boot_e820.map[i].type = E820_RESERVED;
643 }
645 /*
646 * Iterate backwards over all superpage-aligned RAM regions.
647 *
648 * We require superpage alignment because the boot allocator is not yet
649 * initialised. Hence we can only map superpages in the address range
650 * 0 to BOOTSTRAP_DIRECTMAP_END, as this is guaranteed not to require
651 * dynamic allocation of pagetables.
652 *
653 * As well as mapping superpages in that range, in preparation for
654 * initialising the boot allocator, we also look for a region to which
655 * we can relocate the dom0 kernel and other multiboot modules. Also, on
656 * x86/64, we relocate Xen to higher memory.
657 */
658 modules_length = mod[mbi->mods_count-1].mod_end - mod[0].mod_start;
659 for ( i = boot_e820.nr_map-1; i >= 0; i-- )
660 {
661 uint64_t s, e, mask = (1UL << L2_PAGETABLE_SHIFT) - 1;
663 /* Superpage-aligned chunks up to BOOTSTRAP_DIRECTMAP_END, please. */
664 s = (boot_e820.map[i].addr + mask) & ~mask;
665 e = (boot_e820.map[i].addr + boot_e820.map[i].size) & ~mask;
666 e = min_t(uint64_t, e, BOOTSTRAP_DIRECTMAP_END);
667 if ( (boot_e820.map[i].type != E820_RAM) || (s >= e) )
668 continue;
670 /* Map the chunk. No memory will need to be allocated to do this. */
671 map_pages_to_xen(
672 (unsigned long)maddr_to_bootstrap_virt(s),
673 s >> PAGE_SHIFT, (e-s) >> PAGE_SHIFT, PAGE_HYPERVISOR);
675 #if defined(CONFIG_X86_64)
676 /* Is the region suitable for relocating Xen? */
677 if ( !xen_phys_start && (((e-s) >> 20) >= opt_xenheap_megabytes) )
678 {
679 extern l2_pgentry_t l2_xenmap[];
680 l4_pgentry_t *pl4e;
681 l3_pgentry_t *pl3e;
682 l2_pgentry_t *pl2e;
683 int i, j;
685 /* Select relocation address. */
686 e = (e - (opt_xenheap_megabytes << 20)) & ~mask;
687 xen_phys_start = e;
688 bootsym(trampoline_xen_phys_start) = e;
690 /*
691 * Perform relocation to new physical address.
692 * Before doing so we must sync static/global data with main memory
693 * with a barrier(). After this we must *not* modify static/global
694 * data until after we have switched to the relocated pagetables!
695 */
696 barrier();
697 move_memory(e, 0, __pa(&_end) - xen_phys_start);
699 /* Poison low 1MB to detect stray pointers to physical 0-1MB. */
700 memset(maddr_to_bootstrap_virt(e), 0x55, 1U<<20);
702 /* Walk initial pagetables, relocating page directory entries. */
703 pl4e = __va(__pa(idle_pg_table));
704 for ( i = 0 ; i < L4_PAGETABLE_ENTRIES; i++, pl4e++ )
705 {
706 if ( !(l4e_get_flags(*pl4e) & _PAGE_PRESENT) )
707 continue;
708 *pl4e = l4e_from_intpte(l4e_get_intpte(*pl4e) +
709 xen_phys_start);
710 pl3e = l4e_to_l3e(*pl4e);
711 for ( j = 0; j < L3_PAGETABLE_ENTRIES; j++, pl3e++ )
712 {
713 /* Not present or already relocated? */
714 if ( !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) ||
715 (l3e_get_pfn(*pl3e) > 0x1000) )
716 continue;
717 *pl3e = l3e_from_intpte(l3e_get_intpte(*pl3e) +
718 xen_phys_start);
719 }
720 }
722 /* The only data mappings to be relocated are in the Xen area. */
723 pl2e = __va(__pa(l2_xenmap));
724 for ( i = 0; i < L2_PAGETABLE_ENTRIES; i++, pl2e++ )
725 {
726 if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) )
727 continue;
728 *pl2e = l2e_from_intpte(l2e_get_intpte(*pl2e) +
729 xen_phys_start);
730 }
732 /* Re-sync the stack and then switch to relocated pagetables. */
733 asm volatile (
734 "rep movsb ; " /* re-sync the stack */
735 "movq %%cr4,%%rsi ; "
736 "andb $0x7f,%%sil ; "
737 "movq %%rsi,%%cr4 ; " /* CR4.PGE == 0 */
738 "movq %0,%%cr3 ; " /* CR3 == new pagetables */
739 "orb $0x80,%%sil ; "
740 "movq %%rsi,%%cr4 " /* CR4.PGE == 1 */
741 : : "r" (__pa(idle_pg_table)), "S" (cpu0_stack),
742 "D" (__va(__pa(cpu0_stack))), "c" (STACK_SIZE) : "memory" );
743 }
744 #endif
746 /* Is the region suitable for relocating the multiboot modules? */
747 if ( !initial_images_start && (s < e) && ((e-s) >= modules_length) )
748 {
749 initial_images_end = e;
750 e = (e - modules_length) & PAGE_MASK;
751 initial_images_start = e;
752 move_memory(initial_images_start,
753 mod[0].mod_start, mod[mbi->mods_count-1].mod_end);
754 }
756 if ( !kexec_crash_area.start && (s < e) &&
757 ((e-s) >= kexec_crash_area.size) )
758 {
759 e = (e - kexec_crash_area.size) & PAGE_MASK;
760 kexec_crash_area.start = e;
761 }
762 }
764 if ( !initial_images_start )
765 EARLY_FAIL("Not enough memory to relocate the dom0 kernel image.\n");
766 reserve_in_boot_e820(initial_images_start, initial_images_end);
768 /*
769 * With modules (and Xen itself, on x86/64) relocated out of the way, we
770 * can now initialise the boot allocator with some memory.
771 */
772 xenheap_phys_start = init_boot_allocator(__pa(&_end));
773 xenheap_phys_end = opt_xenheap_megabytes << 20;
774 #if defined(CONFIG_X86_64)
775 if ( !xen_phys_start )
776 EARLY_FAIL("Not enough memory to relocate Xen.\n");
777 xenheap_phys_end += xen_phys_start;
778 reserve_in_boot_e820(xen_phys_start,
779 xen_phys_start + (opt_xenheap_megabytes<<20));
780 init_boot_pages(1<<20, 16<<20); /* Initial seed: 15MB */
781 #else
782 init_boot_pages(xenheap_phys_end, 16<<20); /* Initial seed: 4MB */
783 #endif
785 if ( kexec_crash_area.size != 0 )
786 {
787 unsigned long kdump_start = kexec_crash_area.start;
788 unsigned long kdump_size = kexec_crash_area.size;
790 kdump_size = (kdump_size + PAGE_SIZE - 1) & PAGE_MASK;
792 if ( !reserve_in_boot_e820(kdump_start, kdump_size) )
793 {
794 printk("Kdump: DISABLED (failed to reserve %luMB (%lukB) at 0x%lx)"
795 "\n", kdump_size >> 20, kdump_size >> 10, kdump_start);
796 kexec_crash_area.start = kexec_crash_area.size = 0;
797 }
798 else
799 {
800 printk("Kdump: %luMB (%lukB) at 0x%lx\n",
801 kdump_size >> 20, kdump_size >> 10, kdump_start);
802 }
803 }
805 /*
806 * With the boot allocator now seeded, we can walk every RAM region and
807 * map it in its entirety (on x86/64, at least) and notify it to the
808 * boot allocator.
809 */
810 for ( i = 0; i < boot_e820.nr_map; i++ )
811 {
812 uint64_t s, e, map_e, mask = PAGE_SIZE - 1;
814 /* Only page alignment required now. */
815 s = (boot_e820.map[i].addr + mask) & ~mask;
816 e = (boot_e820.map[i].addr + boot_e820.map[i].size) & ~mask;
817 if ( (boot_e820.map[i].type != E820_RAM) || (s >= e) )
818 continue;
820 /* Perform the mapping (truncated in 32-bit mode). */
821 map_e = e;
822 #if defined(CONFIG_X86_32)
823 map_e = min_t(uint64_t, map_e, BOOTSTRAP_DIRECTMAP_END);
824 #endif
825 if ( s < map_e )
826 map_pages_to_xen(
827 (unsigned long)maddr_to_bootstrap_virt(s),
828 s >> PAGE_SHIFT, (map_e-s) >> PAGE_SHIFT, PAGE_HYPERVISOR);
830 init_boot_pages(s, e);
831 }
833 memguard_init();
835 nr_pages = 0;
836 for ( i = 0; i < e820.nr_map; i++ )
837 if ( e820.map[i].type == E820_RAM )
838 nr_pages += e820.map[i].size >> PAGE_SHIFT;
839 printk("System RAM: %luMB (%lukB)\n",
840 nr_pages >> (20 - PAGE_SHIFT),
841 nr_pages << (PAGE_SHIFT - 10));
842 total_pages = nr_pages;
844 /* Sanity check for unwanted bloat of certain hypercall structures. */
845 BUILD_BUG_ON(sizeof(((struct xen_platform_op *)0)->u) !=
846 sizeof(((struct xen_platform_op *)0)->u.pad));
847 BUILD_BUG_ON(sizeof(((struct xen_domctl *)0)->u) !=
848 sizeof(((struct xen_domctl *)0)->u.pad));
849 BUILD_BUG_ON(sizeof(((struct xen_sysctl *)0)->u) !=
850 sizeof(((struct xen_sysctl *)0)->u.pad));
852 BUILD_BUG_ON(sizeof(start_info_t) > PAGE_SIZE);
853 BUILD_BUG_ON(sizeof(shared_info_t) > PAGE_SIZE);
854 BUILD_BUG_ON(sizeof(struct vcpu_info) != 64);
856 #ifdef CONFIG_COMPAT
857 BUILD_BUG_ON(sizeof(((struct compat_platform_op *)0)->u) !=
858 sizeof(((struct compat_platform_op *)0)->u.pad));
859 BUILD_BUG_ON(sizeof(start_info_compat_t) > PAGE_SIZE);
860 BUILD_BUG_ON(sizeof(struct compat_vcpu_info) != 64);
861 #endif
863 /* Check definitions in public headers match internal defs. */
864 BUILD_BUG_ON(__HYPERVISOR_VIRT_START != HYPERVISOR_VIRT_START);
865 #ifdef HYPERVISOR_VIRT_END
866 BUILD_BUG_ON(__HYPERVISOR_VIRT_END != HYPERVISOR_VIRT_END);
867 #endif
868 BUILD_BUG_ON(MACH2PHYS_VIRT_START != RO_MPT_VIRT_START);
869 BUILD_BUG_ON(MACH2PHYS_VIRT_END != RO_MPT_VIRT_END);
871 init_frametable();
873 acpi_boot_table_init();
875 acpi_numa_init();
877 numa_initmem_init(0, max_page);
879 /* Initialise the Xen heap, skipping RAM holes. */
880 init_xenheap_pages(xenheap_phys_start, xenheap_phys_end);
881 nr_pages = (xenheap_phys_end - xenheap_phys_start) >> PAGE_SHIFT;
882 #ifdef __x86_64__
883 init_xenheap_pages(xen_phys_start, __pa(&_start));
884 nr_pages += (__pa(&_start) - xen_phys_start) >> PAGE_SHIFT;
885 #endif
886 xenheap_phys_start = xen_phys_start;
887 printk("Xen heap: %luMB (%lukB)\n",
888 nr_pages >> (20 - PAGE_SHIFT),
889 nr_pages << (PAGE_SHIFT - 10));
891 end_boot_allocator();
893 early_boot = 0;
895 early_cpu_init();
897 paging_init();
899 /* Unmap the first page of CPU0's stack. */
900 memguard_guard_stack(cpu0_stack);
902 open_softirq(NEW_TLBFLUSH_CLOCK_PERIOD_SOFTIRQ, new_tlbflush_clock_period);
904 if ( opt_watchdog )
905 nmi_watchdog = NMI_LOCAL_APIC;
907 sort_exception_tables();
909 find_smp_config();
911 dmi_scan_machine();
913 generic_apic_probe();
915 acpi_boot_init();
917 init_cpu_to_node();
919 if ( smp_found_config )
920 get_smp_config();
922 #ifdef CONFIG_X86_64
923 /* Low mappings were only needed for some BIOS table parsing. */
924 zap_low_mappings();
925 #endif
927 init_apic_mappings();
929 init_IRQ();
931 percpu_init_areas();
933 init_idle_domain();
935 trap_init();
937 rcu_init();
939 timer_init();
941 early_time_init();
943 arch_init_memory();
945 identify_cpu(&boot_cpu_data);
946 if ( cpu_has_fxsr )
947 set_in_cr4(X86_CR4_OSFXSR);
948 if ( cpu_has_xmm )
949 set_in_cr4(X86_CR4_OSXMMEXCPT);
951 if ( opt_nosmp )
952 max_cpus = 0;
954 smp_prepare_cpus(max_cpus);
956 /*
957 * Initialise higher-level timer functions. We do this fairly late
958 * (post-SMP) because the time bases and scale factors need to be updated
959 * regularly, and SMP initialisation can cause a long delay with
960 * interrupts not yet enabled.
961 */
962 init_xen_time();
964 initialize_keytable();
966 serial_init_postirq();
968 BUG_ON(!local_irq_is_enabled());
970 for_each_present_cpu ( i )
971 {
972 if ( num_online_cpus() >= max_cpus )
973 break;
974 if ( !cpu_online(i) )
975 {
976 rcu_online_cpu(i);
977 __cpu_up(i);
978 }
980 /* Set up cpu_to_node[]. */
981 srat_detect_node(i);
982 /* Set up node_to_cpumask based on cpu_to_node[]. */
983 numa_add_cpu(i);
984 }
986 printk("Brought up %ld CPUs\n", (long)num_online_cpus());
987 smp_cpus_done(max_cpus);
989 initialise_gdb(); /* could be moved earlier */
991 do_initcalls();
993 if ( opt_watchdog )
994 watchdog_enable();
996 /* Extract policy from multiboot. */
997 extract_acm_policy(mbi, &initrdidx, &_policy_start, &_policy_len);
999 /* initialize access control security module */
1000 acm_init(_policy_start, _policy_len);
1002 /* Create initial domain 0. */
1003 dom0 = domain_create(0, 0, DOM0_SSIDREF);
1004 if ( (dom0 == NULL) || (alloc_vcpu(dom0, 0, 0) == NULL) )
1005 panic("Error creating domain 0\n");
1007 dom0->is_privileged = 1;
1009 /* Grab the DOM0 command line. */
1010 cmdline = (char *)(mod[0].string ? __va(mod[0].string) : NULL);
1011 if ( (cmdline != NULL) || (kextra != NULL) )
1013 static char dom0_cmdline[MAX_GUEST_CMDLINE];
1015 dom0_cmdline[0] = '\0';
1017 if ( cmdline != NULL )
1019 /* Skip past the image name and copy to a local buffer. */
1020 while ( *cmdline == ' ' ) cmdline++;
1021 if ( (cmdline = strchr(cmdline, ' ')) != NULL )
1023 while ( *cmdline == ' ' ) cmdline++;
1024 safe_strcpy(dom0_cmdline, cmdline);
1028 if ( kextra != NULL )
1029 /* kextra always includes exactly one leading space. */
1030 safe_strcat(dom0_cmdline, kextra);
1032 /* Append any extra parameters. */
1033 if ( skip_ioapic_setup && !strstr(dom0_cmdline, "noapic") )
1034 safe_strcat(dom0_cmdline, " noapic");
1035 if ( acpi_skip_timer_override &&
1036 !strstr(dom0_cmdline, "acpi_skip_timer_override") )
1037 safe_strcat(dom0_cmdline, " acpi_skip_timer_override");
1038 if ( (strlen(acpi_param) != 0) && !strstr(dom0_cmdline, "acpi=") )
1040 safe_strcat(dom0_cmdline, " acpi=");
1041 safe_strcat(dom0_cmdline, acpi_param);
1044 cmdline = dom0_cmdline;
1047 if ( (initrdidx > 0) && (initrdidx < mbi->mods_count) )
1049 _initrd_start = initial_images_start +
1050 (mod[initrdidx].mod_start - mod[0].mod_start);
1051 _initrd_len = mod[initrdidx].mod_end - mod[initrdidx].mod_start;
1054 /*
1055 * We're going to setup domain0 using the module(s) that we stashed safely
1056 * above our heap. The second module, if present, is an initrd ramdisk.
1057 */
1058 if ( construct_dom0(dom0,
1059 initial_images_start,
1060 mod[0].mod_end-mod[0].mod_start,
1061 _initrd_start,
1062 _initrd_len,
1063 cmdline) != 0)
1064 panic("Could not set up DOM0 guest OS\n");
1066 /* Scrub RAM that is still free and so may go to an unprivileged domain. */
1067 scrub_heap_pages();
1069 init_trace_bufs();
1071 console_endboot();
1073 /* Hide UART from DOM0 if we're using it */
1074 serial_endboot();
1076 domain_unpause_by_systemcontroller(dom0);
1078 reset_stack_and_jump(init_done);
1081 void arch_get_xen_caps(xen_capabilities_info_t *info)
1083 /* Interface name is always xen-3.0-* for Xen-3.x. */
1084 int major = 3, minor = 0;
1085 char s[32];
1087 (*info)[0] = '\0';
1089 #if defined(CONFIG_X86_32) && !defined(CONFIG_X86_PAE)
1091 snprintf(s, sizeof(s), "xen-%d.%d-x86_32 ", major, minor);
1092 safe_strcat(*info, s);
1093 if ( hvm_enabled )
1095 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32 ", major, minor);
1096 safe_strcat(*info, s);
1099 #elif defined(CONFIG_X86_32) && defined(CONFIG_X86_PAE)
1101 snprintf(s, sizeof(s), "xen-%d.%d-x86_32p ", major, minor);
1102 safe_strcat(*info, s);
1103 if ( hvm_enabled )
1105 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32 ", major, minor);
1106 safe_strcat(*info, s);
1107 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32p ", major, minor);
1108 safe_strcat(*info, s);
1111 #elif defined(CONFIG_X86_64)
1113 snprintf(s, sizeof(s), "xen-%d.%d-x86_64 ", major, minor);
1114 safe_strcat(*info, s);
1115 #ifdef CONFIG_COMPAT
1116 snprintf(s, sizeof(s), "xen-%d.%d-x86_32p ", major, minor);
1117 safe_strcat(*info, s);
1118 #endif
1119 if ( hvm_enabled )
1121 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32 ", major, minor);
1122 safe_strcat(*info, s);
1123 snprintf(s, sizeof(s), "hvm-%d.%d-x86_32p ", major, minor);
1124 safe_strcat(*info, s);
1125 snprintf(s, sizeof(s), "hvm-%d.%d-x86_64 ", major, minor);
1126 safe_strcat(*info, s);
1129 #endif
1132 /*
1133 * Local variables:
1134 * mode: C
1135 * c-set-style: "BSD"
1136 * c-basic-offset: 4
1137 * tab-width: 4
1138 * indent-tabs-mode: nil
1139 * End:
1140 */