ia64/xen-unstable

view xen/arch/x86/domain_build.c @ 19003:95d77829216f

x86: remove pointless dom0 check

This check is redundant with a check a few dozen lines up resulting in
a panic().

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jan 08 11:19:38 2009 +0000 (2009-01-08)
parents 8de4b4e9a435
children 292919f61238
line source
1 /******************************************************************************
2 * domain_build.c
3 *
4 * Copyright (c) 2002-2005, K A Fraser
5 */
7 #include <xen/config.h>
8 #include <xen/init.h>
9 #include <xen/lib.h>
10 #include <xen/ctype.h>
11 #include <xen/sched.h>
12 #include <xen/smp.h>
13 #include <xen/delay.h>
14 #include <xen/event.h>
15 #include <xen/console.h>
16 #include <xen/kernel.h>
17 #include <xen/domain.h>
18 #include <xen/version.h>
19 #include <xen/iocap.h>
20 #include <xen/bitops.h>
21 #include <xen/compat.h>
22 #include <asm/regs.h>
23 #include <asm/system.h>
24 #include <asm/io.h>
25 #include <asm/processor.h>
26 #include <asm/desc.h>
27 #include <asm/i387.h>
28 #include <asm/paging.h>
29 #include <asm/p2m.h>
30 #include <asm/e820.h>
32 #include <public/version.h>
33 #include <public/libelf.h>
35 extern unsigned long initial_images_nrpages(void);
36 extern void discard_initial_images(void);
38 static long __initdata dom0_nrpages;
39 static long __initdata dom0_min_nrpages;
40 static long __initdata dom0_max_nrpages = LONG_MAX;
42 /*
43 * dom0_mem=[min:<min_amt>,][max:<max_amt>,][<amt>]
44 *
45 * <min_amt>: The minimum amount of memory which should be allocated for dom0.
46 * <max_amt>: The maximum amount of memory which should be allocated for dom0.
47 * <amt>: The precise amount of memory to allocate for dom0.
48 *
49 * Notes:
50 * 1. <amt> is clamped from below by <min_amt> and from above by available
51 * memory and <max_amt>
52 * 2. <min_amt> is clamped from above by available memory and <max_amt>
53 * 3. <min_amt> is ignored if it is greater than <max_amt>
54 * 4. If <amt> is not specified, it is calculated as follows:
55 * "All of memory is allocated to domain 0, minus 1/16th which is reserved
56 * for uses such as DMA buffers (the reservation is clamped to 128MB)."
57 *
58 * Each value can be specified as positive or negative:
59 * If +ve: The specified amount is an absolute value.
60 * If -ve: The specified amount is subtracted from total available memory.
61 */
62 static long __init parse_amt(const char *s, const char **ps)
63 {
64 long pages = parse_size_and_unit((*s == '-') ? s+1 : s, ps) >> PAGE_SHIFT;
65 return (*s == '-') ? -pages : pages;
66 }
67 static void __init parse_dom0_mem(const char *s)
68 {
69 do {
70 if ( !strncmp(s, "min:", 4) )
71 dom0_min_nrpages = parse_amt(s+4, &s);
72 else if ( !strncmp(s, "max:", 4) )
73 dom0_max_nrpages = parse_amt(s+4, &s);
74 else
75 dom0_nrpages = parse_amt(s, &s);
76 if ( *s != ',' )
77 break;
78 } while ( *s++ == ',' );
79 }
80 custom_param("dom0_mem", parse_dom0_mem);
82 static unsigned int opt_dom0_max_vcpus;
83 integer_param("dom0_max_vcpus", opt_dom0_max_vcpus);
85 static unsigned int opt_dom0_shadow;
86 boolean_param("dom0_shadow", opt_dom0_shadow);
88 static char opt_dom0_ioports_disable[200] = "";
89 string_param("dom0_ioports_disable", opt_dom0_ioports_disable);
91 #if defined(__i386__)
92 /* No ring-3 access in initial leaf page tables. */
93 #define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED)
94 #define L2_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
95 #define L3_PROT (_PAGE_PRESENT)
96 #elif defined(__x86_64__)
97 /* Allow ring-3 access in long mode as guest cannot use ring 1 ... */
98 #define BASE_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_USER)
99 #define L1_PROT (BASE_PROT|_PAGE_GUEST_KERNEL)
100 /* ... except for compatibility mode guests. */
101 #define COMPAT_L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED)
102 #define L2_PROT (BASE_PROT|_PAGE_DIRTY)
103 #define L3_PROT (BASE_PROT|_PAGE_DIRTY)
104 #define L4_PROT (BASE_PROT|_PAGE_DIRTY)
105 #endif
107 #define round_pgup(_p) (((_p)+(PAGE_SIZE-1))&PAGE_MASK)
108 #define round_pgdown(_p) ((_p)&PAGE_MASK)
110 static struct page_info * __init alloc_chunk(
111 struct domain *d, unsigned long max_pages)
112 {
113 struct page_info *page;
114 unsigned int order;
115 /*
116 * Allocate up to 2MB at a time: It prevents allocating very large chunks
117 * from DMA pools before the >4GB pool is fully depleted.
118 */
119 if ( max_pages > (2UL << (20 - PAGE_SHIFT)) )
120 max_pages = 2UL << (20 - PAGE_SHIFT);
121 order = get_order_from_pages(max_pages);
122 if ( (max_pages & (max_pages-1)) != 0 )
123 order--;
124 while ( (page = alloc_domheap_pages(d, order, 0)) == NULL )
125 if ( order-- == 0 )
126 break;
127 return page;
128 }
130 static unsigned long __init compute_dom0_nr_pages(void)
131 {
132 unsigned long avail = avail_domheap_pages() + initial_images_nrpages();
134 /*
135 * If domain 0 allocation isn't specified, reserve 1/16th of available
136 * memory for things like DMA buffers. This reservation is clamped to
137 * a maximum of 128MB.
138 */
139 if ( dom0_nrpages == 0 )
140 {
141 dom0_nrpages = avail;
142 dom0_nrpages = min(dom0_nrpages / 16, 128L << (20 - PAGE_SHIFT));
143 dom0_nrpages = -dom0_nrpages;
144 }
146 /* Negative memory specification means "all memory - specified amount". */
147 if ( dom0_nrpages < 0 ) dom0_nrpages += avail;
148 if ( dom0_min_nrpages < 0 ) dom0_min_nrpages += avail;
149 if ( dom0_max_nrpages < 0 ) dom0_max_nrpages += avail;
151 /* Clamp dom0 memory according to min/max limits and available memory. */
152 dom0_nrpages = max(dom0_nrpages, dom0_min_nrpages);
153 dom0_nrpages = min(dom0_nrpages, dom0_max_nrpages);
154 dom0_nrpages = min(dom0_nrpages, (long)avail);
156 return dom0_nrpages;
157 }
159 static void __init process_dom0_ioports_disable(void)
160 {
161 unsigned long io_from, io_to;
162 char *t, *s = opt_dom0_ioports_disable;
163 const char *u;
165 if ( *s == '\0' )
166 return;
168 while ( (t = strsep(&s, ",")) != NULL )
169 {
170 io_from = simple_strtoul(t, &u, 16);
171 if ( u == t )
172 {
173 parse_error:
174 printk("Invalid ioport range <%s> "
175 "in dom0_ioports_disable, skipping\n", t);
176 continue;
177 }
179 if ( *u == '\0' )
180 io_to = io_from;
181 else if ( *u == '-' )
182 io_to = simple_strtoul(u + 1, &u, 16);
183 else
184 goto parse_error;
186 if ( (*u != '\0') || (io_to < io_from) || (io_to >= 65536) )
187 goto parse_error;
189 printk("Disabling dom0 access to ioport range %04lx-%04lx\n",
190 io_from, io_to);
192 if ( ioports_deny_access(dom0, io_from, io_to) != 0 )
193 BUG();
194 }
195 }
197 int __init construct_dom0(
198 struct domain *d,
199 unsigned long _image_start, unsigned long image_len,
200 unsigned long _initrd_start, unsigned long initrd_len,
201 char *cmdline)
202 {
203 int i, rc, compatible, compat32, order, machine;
204 struct cpu_user_regs *regs;
205 unsigned long pfn, mfn;
206 unsigned long nr_pages;
207 unsigned long nr_pt_pages;
208 unsigned long alloc_spfn;
209 unsigned long alloc_epfn;
210 unsigned long count;
211 struct page_info *page = NULL;
212 start_info_t *si;
213 struct vcpu *v = d->vcpu[0];
214 unsigned long long value;
215 #if defined(__i386__)
216 char *image_start = (char *)_image_start; /* use lowmem mappings */
217 char *initrd_start = (char *)_initrd_start; /* use lowmem mappings */
218 #elif defined(__x86_64__)
219 char *image_start = __va(_image_start);
220 char *initrd_start = __va(_initrd_start);
221 #endif
222 #if CONFIG_PAGING_LEVELS >= 4
223 l4_pgentry_t *l4tab = NULL, *l4start = NULL;
224 #endif
225 l3_pgentry_t *l3tab = NULL, *l3start = NULL;
226 l2_pgentry_t *l2tab = NULL, *l2start = NULL;
227 l1_pgentry_t *l1tab = NULL, *l1start = NULL;
229 /*
230 * This fully describes the memory layout of the initial domain. All
231 * *_start address are page-aligned, except v_start (and v_end) which are
232 * superpage-aligned.
233 */
234 struct elf_binary elf;
235 struct elf_dom_parms parms;
236 unsigned long vkern_start;
237 unsigned long vkern_end;
238 unsigned long vinitrd_start;
239 unsigned long vinitrd_end;
240 unsigned long vphysmap_start;
241 unsigned long vphysmap_end;
242 unsigned long vstartinfo_start;
243 unsigned long vstartinfo_end;
244 unsigned long vstack_start;
245 unsigned long vstack_end;
246 unsigned long vpt_start;
247 unsigned long vpt_end;
248 unsigned long v_start;
249 unsigned long v_end;
251 /* Machine address of next candidate page-table page. */
252 unsigned long mpt_alloc;
254 /* Sanity! */
255 BUG_ON(d->domain_id != 0);
256 BUG_ON(d->vcpu[0] == NULL);
257 BUG_ON(v->is_initialised);
259 printk("*** LOADING DOMAIN 0 ***\n");
261 d->max_pages = ~0U;
263 nr_pages = compute_dom0_nr_pages();
265 if ( (rc = elf_init(&elf, image_start, image_len)) != 0 )
266 return rc;
267 #ifdef VERBOSE
268 elf_set_verbose(&elf);
269 #endif
270 elf_parse_binary(&elf);
271 if ( (rc = elf_xen_parse(&elf, &parms)) != 0 )
272 return rc;
274 /* compatibility check */
275 compatible = 0;
276 compat32 = 0;
277 machine = elf_uval(&elf, elf.ehdr, e_machine);
278 switch (CONFIG_PAGING_LEVELS) {
279 case 3: /* x86_32p */
280 if (parms.pae == PAEKERN_bimodal)
281 parms.pae = PAEKERN_extended_cr3;
282 printk(" Xen kernel: 32-bit, PAE, lsb\n");
283 if (elf_32bit(&elf) && parms.pae && machine == EM_386)
284 compatible = 1;
285 break;
286 case 4: /* x86_64 */
287 printk(" Xen kernel: 64-bit, lsb, compat32\n");
288 if (elf_32bit(&elf) && parms.pae == PAEKERN_bimodal)
289 parms.pae = PAEKERN_extended_cr3;
290 if (elf_32bit(&elf) && parms.pae && machine == EM_386)
291 {
292 compat32 = 1;
293 compatible = 1;
294 }
295 if (elf_64bit(&elf) && machine == EM_X86_64)
296 compatible = 1;
297 break;
298 }
299 printk(" Dom0 kernel: %s%s, %s, paddr 0x%" PRIx64 " -> 0x%" PRIx64 "\n",
300 elf_64bit(&elf) ? "64-bit" : "32-bit",
301 parms.pae ? ", PAE" : "",
302 elf_msb(&elf) ? "msb" : "lsb",
303 elf.pstart, elf.pend);
304 if ( elf.bsd_symtab_pstart )
305 printk(" Dom0 symbol map 0x%" PRIx64 " -> 0x%" PRIx64 "\n",
306 elf.bsd_symtab_pstart, elf.bsd_symtab_pend);
308 if ( !compatible )
309 {
310 printk("Mismatch between Xen and DOM0 kernel\n");
311 return -EINVAL;
312 }
314 #if defined(__x86_64__)
315 if ( compat32 )
316 {
317 d->arch.is_32bit_pv = d->arch.has_32bit_shinfo = 1;
318 v->vcpu_info = (void *)&d->shared_info->compat.vcpu_info[0];
320 if ( nr_pages != (unsigned int)nr_pages )
321 nr_pages = UINT_MAX;
322 }
323 #endif
325 if ( parms.pae == PAEKERN_extended_cr3 )
326 set_bit(VMASST_TYPE_pae_extended_cr3, &d->vm_assist);
328 if ( (parms.virt_hv_start_low != UNSET_ADDR) && elf_32bit(&elf) )
329 {
330 unsigned long mask = (1UL << L2_PAGETABLE_SHIFT) - 1;
331 value = (parms.virt_hv_start_low + mask) & ~mask;
332 BUG_ON(!is_pv_32bit_domain(d));
333 #if defined(__i386__)
334 if ( value > HYPERVISOR_VIRT_START )
335 panic("Domain 0 expects too high a hypervisor start address.\n");
336 #else
337 if ( value > __HYPERVISOR_COMPAT_VIRT_START )
338 panic("Domain 0 expects too high a hypervisor start address.\n");
339 HYPERVISOR_COMPAT_VIRT_START(d) =
340 max_t(unsigned int, m2p_compat_vstart, value);
341 #endif
342 }
344 domain_set_alloc_bitsize(d);
346 /*
347 * Why do we need this? The number of page-table frames depends on the
348 * size of the bootstrap address space. But the size of the address space
349 * depends on the number of page-table frames (since each one is mapped
350 * read-only). We have a pair of simultaneous equations in two unknowns,
351 * which we solve by exhaustive search.
352 */
353 v_start = parms.virt_base;
354 vkern_start = parms.virt_kstart;
355 vkern_end = parms.virt_kend;
356 vinitrd_start = round_pgup(vkern_end);
357 vinitrd_end = vinitrd_start + initrd_len;
358 vphysmap_start = round_pgup(vinitrd_end);
359 vphysmap_end = vphysmap_start + (nr_pages * (!is_pv_32on64_domain(d) ?
360 sizeof(unsigned long) :
361 sizeof(unsigned int)));
362 vstartinfo_start = round_pgup(vphysmap_end);
363 vstartinfo_end = (vstartinfo_start +
364 sizeof(struct start_info) +
365 sizeof(struct dom0_vga_console_info));
366 vpt_start = round_pgup(vstartinfo_end);
367 for ( nr_pt_pages = 2; ; nr_pt_pages++ )
368 {
369 vpt_end = vpt_start + (nr_pt_pages * PAGE_SIZE);
370 vstack_start = vpt_end;
371 vstack_end = vstack_start + PAGE_SIZE;
372 v_end = (vstack_end + (1UL<<22)-1) & ~((1UL<<22)-1);
373 if ( (v_end - vstack_end) < (512UL << 10) )
374 v_end += 1UL << 22; /* Add extra 4MB to get >= 512kB padding. */
375 #if defined(__i386__)
376 /* 5 pages: 1x 3rd + 4x 2nd level */
377 if ( (((v_end - v_start + ((1UL<<L2_PAGETABLE_SHIFT)-1)) >>
378 L2_PAGETABLE_SHIFT) + 5) <= nr_pt_pages )
379 break;
380 #elif defined(__x86_64__)
381 #define NR(_l,_h,_s) \
382 (((((_h) + ((1UL<<(_s))-1)) & ~((1UL<<(_s))-1)) - \
383 ((_l) & ~((1UL<<(_s))-1))) >> (_s))
384 if ( (1 + /* # L4 */
385 NR(v_start, v_end, L4_PAGETABLE_SHIFT) + /* # L3 */
386 (!is_pv_32on64_domain(d) ?
387 NR(v_start, v_end, L3_PAGETABLE_SHIFT) : /* # L2 */
388 4) + /* # compat L2 */
389 NR(v_start, v_end, L2_PAGETABLE_SHIFT)) /* # L1 */
390 <= nr_pt_pages )
391 break;
392 #endif
393 }
395 order = get_order_from_bytes(v_end - v_start);
396 if ( (1UL << order) > nr_pages )
397 panic("Domain 0 allocation is too small for kernel image.\n");
399 #ifdef __i386__
400 /* Ensure that our low-memory 1:1 mapping covers the allocation. */
401 page = alloc_domheap_pages(d, order, MEMF_bits(30));
402 #else
403 page = alloc_domheap_pages(d, order, 0);
404 #endif
405 if ( page == NULL )
406 panic("Not enough RAM for domain 0 allocation.\n");
407 alloc_spfn = page_to_mfn(page);
408 alloc_epfn = alloc_spfn + d->tot_pages;
410 printk("PHYSICAL MEMORY ARRANGEMENT:\n"
411 " Dom0 alloc.: %"PRIpaddr"->%"PRIpaddr,
412 pfn_to_paddr(alloc_spfn), pfn_to_paddr(alloc_epfn));
413 if ( d->tot_pages < nr_pages )
414 printk(" (%lu pages to be allocated)",
415 nr_pages - d->tot_pages);
416 printk("\nVIRTUAL MEMORY ARRANGEMENT:\n"
417 " Loaded kernel: %p->%p\n"
418 " Init. ramdisk: %p->%p\n"
419 " Phys-Mach map: %p->%p\n"
420 " Start info: %p->%p\n"
421 " Page tables: %p->%p\n"
422 " Boot stack: %p->%p\n"
423 " TOTAL: %p->%p\n",
424 _p(vkern_start), _p(vkern_end),
425 _p(vinitrd_start), _p(vinitrd_end),
426 _p(vphysmap_start), _p(vphysmap_end),
427 _p(vstartinfo_start), _p(vstartinfo_end),
428 _p(vpt_start), _p(vpt_end),
429 _p(vstack_start), _p(vstack_end),
430 _p(v_start), _p(v_end));
431 printk(" ENTRY ADDRESS: %p\n", _p(parms.virt_entry));
433 mpt_alloc = (vpt_start - v_start) +
434 (unsigned long)pfn_to_paddr(alloc_spfn);
436 #if defined(__i386__)
437 /*
438 * Protect the lowest 1GB of memory. We use a temporary mapping there
439 * from which we copy the kernel and ramdisk images.
440 */
441 if ( v_start < (1UL<<30) )
442 {
443 printk("Initial loading isn't allowed to lowest 1GB of memory.\n");
444 return -EINVAL;
445 }
447 /* WARNING: The new domain must have its 'processor' field filled in! */
448 l3start = l3tab = (l3_pgentry_t *)mpt_alloc; mpt_alloc += PAGE_SIZE;
449 l2start = l2tab = (l2_pgentry_t *)mpt_alloc; mpt_alloc += 4*PAGE_SIZE;
450 for (i = 0; i < L3_PAGETABLE_ENTRIES; i++) {
451 copy_page(l2tab + i * L2_PAGETABLE_ENTRIES,
452 idle_pg_table_l2 + i * L2_PAGETABLE_ENTRIES);
453 l3tab[i] = l3e_from_paddr((u32)l2tab + i*PAGE_SIZE, L3_PROT);
454 l2tab[(LINEAR_PT_VIRT_START >> L2_PAGETABLE_SHIFT)+i] =
455 l2e_from_paddr((u32)l2tab + i*PAGE_SIZE, __PAGE_HYPERVISOR);
456 }
457 v->arch.guest_table = pagetable_from_paddr((unsigned long)l3start);
459 for ( i = 0; i < PDPT_L2_ENTRIES; i++ )
460 l2tab[l2_linear_offset(PERDOMAIN_VIRT_START) + i] =
461 l2e_from_page(virt_to_page(d->arch.mm_perdomain_pt) + i,
462 __PAGE_HYPERVISOR);
464 l2tab += l2_linear_offset(v_start);
465 mfn = alloc_spfn;
466 for ( count = 0; count < ((v_end-v_start)>>PAGE_SHIFT); count++ )
467 {
468 if ( !((unsigned long)l1tab & (PAGE_SIZE-1)) )
469 {
470 l1start = l1tab = (l1_pgentry_t *)mpt_alloc;
471 mpt_alloc += PAGE_SIZE;
472 *l2tab = l2e_from_paddr((unsigned long)l1start, L2_PROT);
473 l2tab++;
474 clear_page(l1tab);
475 if ( count == 0 )
476 l1tab += l1_table_offset(v_start);
477 }
478 *l1tab = l1e_from_pfn(mfn, L1_PROT);
479 l1tab++;
481 page = mfn_to_page(mfn);
482 if ( !get_page_and_type(page, d, PGT_writable_page) )
483 BUG();
485 mfn++;
486 }
488 /* Pages that are part of page tables must be read only. */
489 l2tab = l2start + l2_linear_offset(vpt_start);
490 l1start = l1tab = (l1_pgentry_t *)(u32)l2e_get_paddr(*l2tab);
491 l1tab += l1_table_offset(vpt_start);
492 for ( count = 0; count < nr_pt_pages; count++ )
493 {
494 page = mfn_to_page(l1e_get_pfn(*l1tab));
495 if ( !opt_dom0_shadow )
496 l1e_remove_flags(*l1tab, _PAGE_RW);
497 else
498 if ( !get_page_type(page, PGT_writable_page) )
499 BUG();
501 switch ( count )
502 {
503 case 0:
504 page->u.inuse.type_info &= ~PGT_type_mask;
505 page->u.inuse.type_info |= PGT_l3_page_table;
506 get_page(page, d); /* an extra ref because of readable mapping */
508 /* Get another ref to L3 page so that it can be pinned. */
509 page->u.inuse.type_info++;
510 page->count_info++;
511 set_bit(_PGT_pinned, &page->u.inuse.type_info);
512 break;
513 case 1 ... 4:
514 page->u.inuse.type_info &= ~PGT_type_mask;
515 page->u.inuse.type_info |= PGT_l2_page_table;
516 if ( count == 4 )
517 page->u.inuse.type_info |= PGT_pae_xen_l2;
518 get_page(page, d); /* an extra ref because of readable mapping */
519 break;
520 default:
521 page->u.inuse.type_info &= ~PGT_type_mask;
522 page->u.inuse.type_info |= PGT_l1_page_table;
523 get_page(page, d); /* an extra ref because of readable mapping */
524 break;
525 }
526 if ( !((unsigned long)++l1tab & (PAGE_SIZE - 1)) )
527 l1start = l1tab = (l1_pgentry_t *)(u32)l2e_get_paddr(*++l2tab);
528 }
530 #elif defined(__x86_64__)
532 /* Overlap with Xen protected area? */
533 if ( !is_pv_32on64_domain(d) ?
534 ((v_start < HYPERVISOR_VIRT_END) &&
535 (v_end > HYPERVISOR_VIRT_START)) :
536 (v_end > HYPERVISOR_COMPAT_VIRT_START(d)) )
537 {
538 printk("DOM0 image overlaps with Xen private area.\n");
539 return -EINVAL;
540 }
542 if ( is_pv_32on64_domain(d) )
543 {
544 v->arch.guest_context.failsafe_callback_cs = FLAT_COMPAT_KERNEL_CS;
545 v->arch.guest_context.event_callback_cs = FLAT_COMPAT_KERNEL_CS;
546 }
548 /* WARNING: The new domain must have its 'processor' field filled in! */
549 if ( !is_pv_32on64_domain(d) )
550 {
551 maddr_to_page(mpt_alloc)->u.inuse.type_info = PGT_l4_page_table;
552 l4start = l4tab = __va(mpt_alloc); mpt_alloc += PAGE_SIZE;
553 }
554 else
555 {
556 page = alloc_domheap_page(NULL, 0);
557 if ( !page )
558 panic("Not enough RAM for domain 0 PML4.\n");
559 page->u.inuse.type_info = PGT_l4_page_table|PGT_validated|1;
560 l4start = l4tab = page_to_virt(page);
561 }
562 copy_page(l4tab, idle_pg_table);
563 l4tab[0] = l4e_empty(); /* zap trampoline mapping */
564 l4tab[l4_table_offset(LINEAR_PT_VIRT_START)] =
565 l4e_from_paddr(__pa(l4start), __PAGE_HYPERVISOR);
566 l4tab[l4_table_offset(PERDOMAIN_VIRT_START)] =
567 l4e_from_paddr(__pa(d->arch.mm_perdomain_l3), __PAGE_HYPERVISOR);
568 v->arch.guest_table = pagetable_from_paddr(__pa(l4start));
569 if ( is_pv_32on64_domain(d) )
570 v->arch.guest_table_user = v->arch.guest_table;
572 l4tab += l4_table_offset(v_start);
573 mfn = alloc_spfn;
574 for ( count = 0; count < ((v_end-v_start)>>PAGE_SHIFT); count++ )
575 {
576 if ( !((unsigned long)l1tab & (PAGE_SIZE-1)) )
577 {
578 maddr_to_page(mpt_alloc)->u.inuse.type_info = PGT_l1_page_table;
579 l1start = l1tab = __va(mpt_alloc); mpt_alloc += PAGE_SIZE;
580 clear_page(l1tab);
581 if ( count == 0 )
582 l1tab += l1_table_offset(v_start);
583 if ( !((unsigned long)l2tab & (PAGE_SIZE-1)) )
584 {
585 maddr_to_page(mpt_alloc)->u.inuse.type_info = PGT_l2_page_table;
586 l2start = l2tab = __va(mpt_alloc); mpt_alloc += PAGE_SIZE;
587 clear_page(l2tab);
588 if ( count == 0 )
589 l2tab += l2_table_offset(v_start);
590 if ( !((unsigned long)l3tab & (PAGE_SIZE-1)) )
591 {
592 maddr_to_page(mpt_alloc)->u.inuse.type_info =
593 PGT_l3_page_table;
594 l3start = l3tab = __va(mpt_alloc); mpt_alloc += PAGE_SIZE;
595 clear_page(l3tab);
596 if ( count == 0 )
597 l3tab += l3_table_offset(v_start);
598 *l4tab = l4e_from_paddr(__pa(l3start), L4_PROT);
599 l4tab++;
600 }
601 *l3tab = l3e_from_paddr(__pa(l2start), L3_PROT);
602 l3tab++;
603 }
604 *l2tab = l2e_from_paddr(__pa(l1start), L2_PROT);
605 l2tab++;
606 }
607 *l1tab = l1e_from_pfn(mfn, (!is_pv_32on64_domain(d) ?
608 L1_PROT : COMPAT_L1_PROT));
609 l1tab++;
611 page = mfn_to_page(mfn);
612 if ( (page->u.inuse.type_info == 0) &&
613 !get_page_and_type(page, d, PGT_writable_page) )
614 BUG();
616 mfn++;
617 }
619 if ( is_pv_32on64_domain(d) )
620 {
621 /* Ensure the first four L3 entries are all populated. */
622 for ( i = 0, l3tab = l3start; i < 4; ++i, ++l3tab )
623 {
624 if ( !l3e_get_intpte(*l3tab) )
625 {
626 maddr_to_page(mpt_alloc)->u.inuse.type_info = PGT_l2_page_table;
627 l2tab = __va(mpt_alloc); mpt_alloc += PAGE_SIZE;
628 clear_page(l2tab);
629 *l3tab = l3e_from_paddr(__pa(l2tab), L3_PROT);
630 }
631 if ( i == 3 )
632 l3e_get_page(*l3tab)->u.inuse.type_info |= PGT_pae_xen_l2;
633 }
634 /* Install read-only guest visible MPT mapping. */
635 l2tab = l3e_to_l2e(l3start[3]);
636 memcpy(&l2tab[COMPAT_L2_PAGETABLE_FIRST_XEN_SLOT(d)],
637 &compat_idle_pg_table_l2[l2_table_offset(HIRO_COMPAT_MPT_VIRT_START)],
638 COMPAT_L2_PAGETABLE_XEN_SLOTS(d) * sizeof(*l2tab));
639 }
641 /* Pages that are part of page tables must be read only. */
642 l4tab = l4start + l4_table_offset(vpt_start);
643 l3start = l3tab = l4e_to_l3e(*l4tab);
644 l3tab += l3_table_offset(vpt_start);
645 l2start = l2tab = l3e_to_l2e(*l3tab);
646 l2tab += l2_table_offset(vpt_start);
647 l1start = l1tab = l2e_to_l1e(*l2tab);
648 l1tab += l1_table_offset(vpt_start);
649 for ( count = 0; count < nr_pt_pages; count++ )
650 {
651 l1e_remove_flags(*l1tab, _PAGE_RW);
652 page = mfn_to_page(l1e_get_pfn(*l1tab));
654 /* Read-only mapping + PGC_allocated + page-table page. */
655 page->count_info = PGC_allocated | 3;
656 page->u.inuse.type_info |= PGT_validated | 1;
658 /* Top-level p.t. is pinned. */
659 if ( (page->u.inuse.type_info & PGT_type_mask) ==
660 (!is_pv_32on64_domain(d) ?
661 PGT_l4_page_table : PGT_l3_page_table) )
662 {
663 page->count_info += 1;
664 page->u.inuse.type_info += 1 | PGT_pinned;
665 }
667 /* Iterate. */
668 if ( !((unsigned long)++l1tab & (PAGE_SIZE - 1)) )
669 {
670 if ( !((unsigned long)++l2tab & (PAGE_SIZE - 1)) )
671 {
672 if ( !((unsigned long)++l3tab & (PAGE_SIZE - 1)) )
673 l3start = l3tab = l4e_to_l3e(*++l4tab);
674 l2start = l2tab = l3e_to_l2e(*l3tab);
675 }
676 l1start = l1tab = l2e_to_l1e(*l2tab);
677 }
678 }
680 #endif /* __x86_64__ */
682 /* Mask all upcalls... */
683 for ( i = 0; i < MAX_VIRT_CPUS; i++ )
684 shared_info(d, vcpu_info[i].evtchn_upcall_mask) = 1;
686 if ( opt_dom0_max_vcpus == 0 )
687 opt_dom0_max_vcpus = num_online_cpus();
688 if ( opt_dom0_max_vcpus > MAX_VIRT_CPUS )
689 opt_dom0_max_vcpus = MAX_VIRT_CPUS;
690 printk("Dom0 has maximum %u VCPUs\n", opt_dom0_max_vcpus);
692 for ( i = 1; i < opt_dom0_max_vcpus; i++ )
693 (void)alloc_vcpu(d, i, i % num_online_cpus());
695 /* Set up CR3 value for write_ptbase */
696 if ( paging_mode_enabled(d) )
697 paging_update_paging_modes(v);
698 else
699 update_cr3(v);
701 /* We run on dom0's page tables for the final part of the build process. */
702 write_ptbase(v);
704 /* Copy the OS image and free temporary buffer. */
705 elf.dest = (void*)vkern_start;
706 elf_load_binary(&elf);
708 if ( UNSET_ADDR != parms.virt_hypercall )
709 {
710 if ( (parms.virt_hypercall < v_start) ||
711 (parms.virt_hypercall >= v_end) )
712 {
713 write_ptbase(current);
714 printk("Invalid HYPERCALL_PAGE field in ELF notes.\n");
715 return -1;
716 }
717 hypercall_page_initialise(
718 d, (void *)(unsigned long)parms.virt_hypercall);
719 }
721 /* Copy the initial ramdisk. */
722 if ( initrd_len != 0 )
723 memcpy((void *)vinitrd_start, initrd_start, initrd_len);
725 /* Free temporary buffers. */
726 discard_initial_images();
728 /* Set up start info area. */
729 si = (start_info_t *)vstartinfo_start;
730 clear_page(si);
731 si->nr_pages = nr_pages;
733 si->shared_info = virt_to_maddr(d->shared_info);
735 si->flags = SIF_PRIVILEGED | SIF_INITDOMAIN;
736 si->flags |= (xen_processor_pmbits << 8) & SIF_PM_MASK;
737 si->pt_base = vpt_start + 2 * PAGE_SIZE * !!is_pv_32on64_domain(d);
738 si->nr_pt_frames = nr_pt_pages;
739 si->mfn_list = vphysmap_start;
740 snprintf(si->magic, sizeof(si->magic), "xen-3.0-x86_%d%s",
741 elf_64bit(&elf) ? 64 : 32, parms.pae ? "p" : "");
743 /* Write the phys->machine and machine->phys table entries. */
744 for ( pfn = 0; pfn < d->tot_pages; pfn++ )
745 {
746 mfn = pfn + alloc_spfn;
747 #ifndef NDEBUG
748 #define REVERSE_START ((v_end - v_start) >> PAGE_SHIFT)
749 if ( pfn > REVERSE_START )
750 mfn = alloc_epfn - (pfn - REVERSE_START);
751 #endif
752 if ( !is_pv_32on64_domain(d) )
753 ((unsigned long *)vphysmap_start)[pfn] = mfn;
754 else
755 ((unsigned int *)vphysmap_start)[pfn] = mfn;
756 set_gpfn_from_mfn(mfn, pfn);
757 }
758 while ( pfn < nr_pages )
759 {
760 if ( (page = alloc_chunk(d, nr_pages - d->tot_pages)) == NULL )
761 panic("Not enough RAM for DOM0 reservation.\n");
762 while ( pfn < d->tot_pages )
763 {
764 mfn = page_to_mfn(page);
765 #ifndef NDEBUG
766 #define pfn (nr_pages - 1 - (pfn - (alloc_epfn - alloc_spfn)))
767 #endif
768 if ( !is_pv_32on64_domain(d) )
769 ((unsigned long *)vphysmap_start)[pfn] = mfn;
770 else
771 ((unsigned int *)vphysmap_start)[pfn] = mfn;
772 set_gpfn_from_mfn(mfn, pfn);
773 #undef pfn
774 page++; pfn++;
775 }
776 }
778 if ( initrd_len != 0 )
779 {
780 si->mod_start = vinitrd_start;
781 si->mod_len = initrd_len;
782 }
784 memset(si->cmd_line, 0, sizeof(si->cmd_line));
785 if ( cmdline != NULL )
786 strlcpy((char *)si->cmd_line, cmdline, sizeof(si->cmd_line));
788 if ( fill_console_start_info((void *)(si + 1)) )
789 {
790 si->console.dom0.info_off = sizeof(struct start_info);
791 si->console.dom0.info_size = sizeof(struct dom0_vga_console_info);
792 }
794 #if defined(__x86_64__)
795 if ( is_pv_32on64_domain(d) )
796 xlat_start_info(si, XLAT_start_info_console_dom0);
797 #endif
799 /* Return to idle domain's page tables. */
800 write_ptbase(current);
802 #if defined(__i386__)
803 /* Destroy low mappings - they were only for our convenience. */
804 zap_low_mappings(l2start);
805 #endif
807 update_domain_wallclock_time(d);
809 v->is_initialised = 1;
810 clear_bit(_VPF_down, &v->pause_flags);
812 /*
813 * Initial register values:
814 * DS,ES,FS,GS = FLAT_KERNEL_DS
815 * CS:EIP = FLAT_KERNEL_CS:start_pc
816 * SS:ESP = FLAT_KERNEL_SS:start_stack
817 * ESI = start_info
818 * [EAX,EBX,ECX,EDX,EDI,EBP are zero]
819 */
820 regs = &v->arch.guest_context.user_regs;
821 regs->ds = regs->es = regs->fs = regs->gs =
822 !is_pv_32on64_domain(d) ? FLAT_KERNEL_DS : FLAT_COMPAT_KERNEL_DS;
823 regs->ss = (!is_pv_32on64_domain(d) ?
824 FLAT_KERNEL_SS : FLAT_COMPAT_KERNEL_SS);
825 regs->cs = (!is_pv_32on64_domain(d) ?
826 FLAT_KERNEL_CS : FLAT_COMPAT_KERNEL_CS);
827 regs->eip = parms.virt_entry;
828 regs->esp = vstack_end;
829 regs->esi = vstartinfo_start;
830 regs->eflags = X86_EFLAGS_IF;
832 if ( opt_dom0_shadow )
833 if ( paging_enable(d, PG_SH_enable) == 0 )
834 paging_update_paging_modes(v);
836 if ( supervisor_mode_kernel )
837 {
838 v->arch.guest_context.kernel_ss &= ~3;
839 v->arch.guest_context.user_regs.ss &= ~3;
840 v->arch.guest_context.user_regs.es &= ~3;
841 v->arch.guest_context.user_regs.ds &= ~3;
842 v->arch.guest_context.user_regs.fs &= ~3;
843 v->arch.guest_context.user_regs.gs &= ~3;
844 printk("Dom0 runs in ring 0 (supervisor mode)\n");
845 if ( !test_bit(XENFEAT_supervisor_mode_kernel,
846 parms.f_supported) )
847 panic("Dom0 does not support supervisor-mode execution\n");
848 }
849 else
850 {
851 if ( test_bit(XENFEAT_supervisor_mode_kernel, parms.f_required) )
852 panic("Dom0 requires supervisor-mode execution\n");
853 }
855 rc = 0;
857 /* DOM0 is permitted full I/O capabilities. */
858 rc |= ioports_permit_access(dom0, 0, 0xFFFF);
859 rc |= iomem_permit_access(dom0, 0UL, ~0UL);
860 rc |= irqs_permit_access(dom0, 0, NR_IRQS-1);
862 /*
863 * Modify I/O port access permissions.
864 */
865 /* Master Interrupt Controller (PIC). */
866 rc |= ioports_deny_access(dom0, 0x20, 0x21);
867 /* Slave Interrupt Controller (PIC). */
868 rc |= ioports_deny_access(dom0, 0xA0, 0xA1);
869 /* Interval Timer (PIT). */
870 rc |= ioports_deny_access(dom0, 0x40, 0x43);
871 /* PIT Channel 2 / PC Speaker Control. */
872 rc |= ioports_deny_access(dom0, 0x61, 0x61);
873 /* PCI configuration space (NB. 0xcf8 has special treatment). */
874 rc |= ioports_deny_access(dom0, 0xcfc, 0xcff);
875 /* Command-line I/O ranges. */
876 process_dom0_ioports_disable();
878 /*
879 * Modify I/O memory access permissions.
880 */
881 /* Local APIC. */
882 if ( mp_lapic_addr != 0 )
883 {
884 mfn = paddr_to_pfn(mp_lapic_addr);
885 rc |= iomem_deny_access(dom0, mfn, mfn);
886 }
887 /* I/O APICs. */
888 for ( i = 0; i < nr_ioapics; i++ )
889 {
890 mfn = paddr_to_pfn(mp_ioapics[i].mpc_apicaddr);
891 if ( smp_found_config )
892 rc |= iomem_deny_access(dom0, mfn, mfn);
893 }
895 /* Remove access to E820_UNUSABLE I/O regions above 1MB. */
896 for ( i = 0; i < e820.nr_map; i++ )
897 {
898 unsigned long sfn, efn;
899 sfn = max_t(unsigned long, paddr_to_pfn(e820.map[i].addr), 0x100ul);
900 efn = paddr_to_pfn(e820.map[i].addr + e820.map[i].size - 1);
901 if ( (e820.map[i].type == E820_UNUSABLE) &&
902 (e820.map[i].size != 0) &&
903 (sfn <= efn) )
904 rc |= iomem_deny_access(dom0, sfn, efn);
905 }
907 BUG_ON(rc != 0);
909 return 0;
910 }
912 /*
913 * Local variables:
914 * mode: C
915 * c-set-style: "BSD"
916 * c-basic-offset: 4
917 * tab-width: 4
918 * indent-tabs-mode: nil
919 * End:
920 */