direct-io.hg

view xen/arch/x86/domain_build.c @ 15458:8adfd96f62ae

xen: Remove useless check from dom0 builder.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Tue Jul 03 12:04:03 2007 +0100 (2007-07-03)
parents 005dd6b1cf8e
children
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>
30 #include <public/version.h>
31 #include <public/libelf.h>
33 extern unsigned long initial_images_nrpages(void);
34 extern void discard_initial_images(void);
36 static long __initdata dom0_nrpages;
37 static long __initdata dom0_min_nrpages;
38 static long __initdata dom0_max_nrpages = LONG_MAX;
40 /*
41 * dom0_mem=[min:<min_amt>,][max:<max_amt>,][<amt>]
42 *
43 * <min_amt>: The minimum amount of memory which should be allocated for dom0.
44 * <max_amt>: The maximum amount of memory which should be allocated for dom0.
45 * <amt>: The precise amount of memory to allocate for dom0.
46 *
47 * Notes:
48 * 1. <amt> is clamped from below by <min_amt> and from above by available
49 * memory and <max_amt>
50 * 2. <min_amt> is clamped from above by available memory and <max_amt>
51 * 3. <min_amt> is ignored if it is greater than <max_amt>
52 * 4. If <amt> is not specified, it is calculated as follows:
53 * "All of memory is allocated to domain 0, minus 1/16th which is reserved
54 * for uses such as DMA buffers (the reservation is clamped to 128MB)."
55 *
56 * Each value can be specified as positive or negative:
57 * If +ve: The specified amount is an absolute value.
58 * If -ve: The specified amount is subtracted from total available memory.
59 */
60 static long __init parse_amt(const char *s, const char **ps)
61 {
62 long pages = parse_size_and_unit((*s == '-') ? s+1 : s, ps) >> PAGE_SHIFT;
63 return (*s == '-') ? -pages : pages;
64 }
65 static void __init parse_dom0_mem(const char *s)
66 {
67 do {
68 if ( !strncmp(s, "min:", 4) )
69 dom0_min_nrpages = parse_amt(s+4, &s);
70 else if ( !strncmp(s, "max:", 4) )
71 dom0_max_nrpages = parse_amt(s+4, &s);
72 else
73 dom0_nrpages = parse_amt(s, &s);
74 if ( *s != ',' )
75 break;
76 } while ( *s++ == ',' );
77 }
78 custom_param("dom0_mem", parse_dom0_mem);
80 static unsigned int opt_dom0_max_vcpus;
81 integer_param("dom0_max_vcpus", opt_dom0_max_vcpus);
83 static unsigned int opt_dom0_shadow;
84 boolean_param("dom0_shadow", opt_dom0_shadow);
86 static char opt_dom0_ioports_disable[200] = "";
87 string_param("dom0_ioports_disable", opt_dom0_ioports_disable);
89 #if defined(__i386__)
90 /* No ring-3 access in initial leaf page tables. */
91 #define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED)
92 #define L2_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
93 #define L3_PROT (_PAGE_PRESENT)
94 #elif defined(__x86_64__)
95 /* Allow ring-3 access in long mode as guest cannot use ring 1 ... */
96 #define BASE_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_USER)
97 #define L1_PROT (BASE_PROT|_PAGE_GUEST_KERNEL)
98 /* ... except for compatibility mode guests. */
99 #define COMPAT_L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED)
100 #define L2_PROT (BASE_PROT|_PAGE_DIRTY)
101 #define L3_PROT (BASE_PROT|_PAGE_DIRTY)
102 #define L4_PROT (BASE_PROT|_PAGE_DIRTY)
103 #endif
105 #define round_pgup(_p) (((_p)+(PAGE_SIZE-1))&PAGE_MASK)
106 #define round_pgdown(_p) ((_p)&PAGE_MASK)
108 static struct page_info * __init alloc_chunk(
109 struct domain *d, unsigned long max_pages)
110 {
111 struct page_info *page;
112 unsigned int order;
113 /*
114 * Allocate up to 2MB at a time: It prevents allocating very large chunks
115 * from DMA pools before the >4GB pool is fully depleted.
116 */
117 if ( max_pages > (2UL << (20 - PAGE_SHIFT)) )
118 max_pages = 2UL << (20 - PAGE_SHIFT);
119 order = get_order_from_pages(max_pages);
120 if ( (max_pages & (max_pages-1)) != 0 )
121 order--;
122 while ( (page = alloc_domheap_pages(d, order, 0)) == NULL )
123 if ( order-- == 0 )
124 break;
125 return page;
126 }
128 static unsigned long __init compute_dom0_nr_pages(void)
129 {
130 unsigned long avail = avail_domheap_pages() + initial_images_nrpages();
132 /*
133 * If domain 0 allocation isn't specified, reserve 1/16th of available
134 * memory for things like DMA buffers. This reservation is clamped to
135 * a maximum of 128MB.
136 */
137 if ( dom0_nrpages == 0 )
138 {
139 dom0_nrpages = avail;
140 dom0_nrpages = min(dom0_nrpages / 16, 128L << (20 - PAGE_SHIFT));
141 dom0_nrpages = -dom0_nrpages;
142 }
144 /* Negative memory specification means "all memory - specified amount". */
145 if ( dom0_nrpages < 0 ) dom0_nrpages += avail;
146 if ( dom0_min_nrpages < 0 ) dom0_min_nrpages += avail;
147 if ( dom0_max_nrpages < 0 ) dom0_max_nrpages += avail;
149 /* Clamp dom0 memory according to min/max limits and available memory. */
150 dom0_nrpages = max(dom0_nrpages, dom0_min_nrpages);
151 dom0_nrpages = min(dom0_nrpages, dom0_max_nrpages);
152 dom0_nrpages = min(dom0_nrpages, (long)avail);
154 return dom0_nrpages;
155 }
157 static void __init process_dom0_ioports_disable(void)
158 {
159 unsigned long io_from, io_to;
160 char *t, *s = opt_dom0_ioports_disable;
161 const char *u;
163 if ( *s == '\0' )
164 return;
166 while ( (t = strsep(&s, ",")) != NULL )
167 {
168 io_from = simple_strtoul(t, &u, 16);
169 if ( u == t )
170 {
171 parse_error:
172 printk("Invalid ioport range <%s> "
173 "in dom0_ioports_disable, skipping\n", t);
174 continue;
175 }
177 if ( *u == '\0' )
178 io_to = io_from;
179 else if ( *u == '-' )
180 io_to = simple_strtoul(u + 1, &u, 16);
181 else
182 goto parse_error;
184 if ( (*u != '\0') || (io_to < io_from) || (io_to >= 65536) )
185 goto parse_error;
187 printk("Disabling dom0 access to ioport range %04lx-%04lx\n",
188 io_from, io_to);
190 if ( ioports_deny_access(dom0, io_from, io_to) != 0 )
191 BUG();
192 }
193 }
195 int __init construct_dom0(
196 struct domain *d,
197 unsigned long _image_start, unsigned long image_len,
198 unsigned long _initrd_start, unsigned long initrd_len,
199 char *cmdline)
200 {
201 int i, rc, compatible, compat32, order, machine;
202 struct cpu_user_regs *regs;
203 unsigned long pfn, mfn;
204 unsigned long nr_pages;
205 unsigned long nr_pt_pages;
206 unsigned long alloc_spfn;
207 unsigned long alloc_epfn;
208 unsigned long count;
209 struct page_info *page = NULL;
210 start_info_t *si;
211 struct vcpu *v = d->vcpu[0];
212 unsigned long long value;
213 #if defined(__i386__)
214 char *image_start = (char *)_image_start; /* use lowmem mappings */
215 char *initrd_start = (char *)_initrd_start; /* use lowmem mappings */
216 #elif defined(__x86_64__)
217 char *image_start = __va(_image_start);
218 char *initrd_start = __va(_initrd_start);
219 #endif
220 #if CONFIG_PAGING_LEVELS >= 4
221 l4_pgentry_t *l4tab = NULL, *l4start = NULL;
222 #endif
223 #if CONFIG_PAGING_LEVELS >= 3
224 l3_pgentry_t *l3tab = NULL, *l3start = NULL;
225 #endif
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 /* Features supported. */
255 uint32_t dom0_features_supported[XENFEAT_NR_SUBMAPS] = { 0 };
256 uint32_t dom0_features_required[XENFEAT_NR_SUBMAPS] = { 0 };
258 /* Sanity! */
259 BUG_ON(d->domain_id != 0);
260 BUG_ON(d->vcpu[0] == NULL);
261 BUG_ON(v->is_initialised);
263 printk("*** LOADING DOMAIN 0 ***\n");
265 d->max_pages = ~0U;
267 nr_pages = compute_dom0_nr_pages();
269 if ( (rc = elf_init(&elf, image_start, image_len)) != 0 )
270 return rc;
271 #ifdef VERBOSE
272 elf_set_verbose(&elf);
273 #endif
274 elf_parse_binary(&elf);
275 if ( (rc = elf_xen_parse(&elf, &parms)) != 0 )
276 return rc;
278 /* compatibility check */
279 compatible = 0;
280 compat32 = 0;
281 machine = elf_uval(&elf, elf.ehdr, e_machine);
282 switch (CONFIG_PAGING_LEVELS) {
283 case 2: /* x86_32 */
284 if (parms.pae == PAEKERN_bimodal)
285 parms.pae = PAEKERN_no;
286 printk(" Xen kernel: 32-bit, lsb\n");
287 if (elf_32bit(&elf) && !parms.pae && machine == EM_386)
288 compatible = 1;
289 break;
290 case 3: /* x86_32p */
291 if (parms.pae == PAEKERN_bimodal)
292 parms.pae = PAEKERN_extended_cr3;
293 printk(" Xen kernel: 32-bit, PAE, lsb\n");
294 if (elf_32bit(&elf) && parms.pae && machine == EM_386)
295 compatible = 1;
296 break;
297 case 4: /* x86_64 */
298 #ifndef CONFIG_COMPAT
299 printk(" Xen kernel: 64-bit, lsb\n");
300 #else
301 printk(" Xen kernel: 64-bit, lsb, compat32\n");
302 if (elf_32bit(&elf) && parms.pae == PAEKERN_bimodal)
303 parms.pae = PAEKERN_extended_cr3;
304 if (elf_32bit(&elf) && parms.pae && machine == EM_386)
305 {
306 compat32 = 1;
307 compatible = 1;
308 }
309 #endif
310 if (elf_64bit(&elf) && machine == EM_X86_64)
311 compatible = 1;
312 break;
313 }
314 printk(" Dom0 kernel: %s%s, %s, paddr 0x%" PRIx64 " -> 0x%" PRIx64 "\n",
315 elf_64bit(&elf) ? "64-bit" : "32-bit",
316 parms.pae ? ", PAE" : "",
317 elf_msb(&elf) ? "msb" : "lsb",
318 elf.pstart, elf.pend);
320 if ( !compatible )
321 {
322 printk("Mismatch between Xen and DOM0 kernel\n");
323 return -EINVAL;
324 }
326 #ifdef CONFIG_COMPAT
327 if ( compat32 )
328 {
329 l1_pgentry_t gdt_l1e;
331 d->arch.is_32bit_pv = d->arch.has_32bit_shinfo = 1;
332 v->vcpu_info = (void *)&d->shared_info->compat.vcpu_info[0];
334 if ( nr_pages != (unsigned int)nr_pages )
335 nr_pages = UINT_MAX;
337 /*
338 * Map compatibility Xen segments into every VCPU's GDT. See
339 * arch_domain_create() for further comments.
340 */
341 gdt_l1e = l1e_from_page(virt_to_page(compat_gdt_table),
342 PAGE_HYPERVISOR);
343 for ( i = 0; i < MAX_VIRT_CPUS; i++ )
344 d->arch.mm_perdomain_pt[((i << GDT_LDT_VCPU_SHIFT) +
345 FIRST_RESERVED_GDT_PAGE)] = gdt_l1e;
346 local_flush_tlb_one(GDT_LDT_VIRT_START + FIRST_RESERVED_GDT_BYTE);
347 }
348 #endif
349 if ( parms.pae == PAEKERN_extended_cr3 )
350 set_bit(VMASST_TYPE_pae_extended_cr3, &d->vm_assist);
352 if ( UNSET_ADDR != parms.virt_hv_start_low && elf_32bit(&elf) )
353 {
354 #if CONFIG_PAGING_LEVELS < 4
355 unsigned long mask = (1UL << L2_PAGETABLE_SHIFT) - 1;
356 #else
357 unsigned long mask = is_pv_32bit_domain(d)
358 ? (1UL << L2_PAGETABLE_SHIFT) - 1
359 : (1UL << L4_PAGETABLE_SHIFT) - 1;
360 #endif
362 value = (parms.virt_hv_start_low + mask) & ~mask;
363 #ifdef CONFIG_COMPAT
364 HYPERVISOR_COMPAT_VIRT_START(d) =
365 max_t(unsigned int, m2p_compat_vstart, value);
366 d->arch.physaddr_bitsize = !is_pv_32on64_domain(d) ? 64 :
367 fls((1UL << 32) - HYPERVISOR_COMPAT_VIRT_START(d)) - 1
368 + (PAGE_SIZE - 2);
369 if ( value > (!is_pv_32on64_domain(d) ?
370 HYPERVISOR_VIRT_START :
371 __HYPERVISOR_COMPAT_VIRT_START) )
372 #else
373 if ( value > HYPERVISOR_VIRT_START )
374 #endif
375 panic("Domain 0 expects too high a hypervisor start address.\n");
376 }
378 /*
379 * Why do we need this? The number of page-table frames depends on the
380 * size of the bootstrap address space. But the size of the address space
381 * depends on the number of page-table frames (since each one is mapped
382 * read-only). We have a pair of simultaneous equations in two unknowns,
383 * which we solve by exhaustive search.
384 */
385 v_start = parms.virt_base;
386 vkern_start = parms.virt_kstart;
387 vkern_end = parms.virt_kend;
388 vinitrd_start = round_pgup(vkern_end);
389 vinitrd_end = vinitrd_start + initrd_len;
390 vphysmap_start = round_pgup(vinitrd_end);
391 vphysmap_end = vphysmap_start + (nr_pages * (!is_pv_32on64_domain(d) ?
392 sizeof(unsigned long) :
393 sizeof(unsigned int)));
394 vstartinfo_start = round_pgup(vphysmap_end);
395 vstartinfo_end = (vstartinfo_start +
396 sizeof(struct start_info) +
397 sizeof(struct dom0_vga_console_info));
398 vpt_start = round_pgup(vstartinfo_end);
399 for ( nr_pt_pages = 2; ; nr_pt_pages++ )
400 {
401 vpt_end = vpt_start + (nr_pt_pages * PAGE_SIZE);
402 vstack_start = vpt_end;
403 vstack_end = vstack_start + PAGE_SIZE;
404 v_end = (vstack_end + (1UL<<22)-1) & ~((1UL<<22)-1);
405 if ( (v_end - vstack_end) < (512UL << 10) )
406 v_end += 1UL << 22; /* Add extra 4MB to get >= 512kB padding. */
407 #if defined(__i386__) && !defined(CONFIG_X86_PAE)
408 if ( (((v_end - v_start + ((1UL<<L2_PAGETABLE_SHIFT)-1)) >>
409 L2_PAGETABLE_SHIFT) + 1) <= nr_pt_pages )
410 break;
411 #elif defined(__i386__) && defined(CONFIG_X86_PAE)
412 /* 5 pages: 1x 3rd + 4x 2nd level */
413 if ( (((v_end - v_start + ((1UL<<L2_PAGETABLE_SHIFT)-1)) >>
414 L2_PAGETABLE_SHIFT) + 5) <= nr_pt_pages )
415 break;
416 #elif defined(__x86_64__)
417 #define NR(_l,_h,_s) \
418 (((((_h) + ((1UL<<(_s))-1)) & ~((1UL<<(_s))-1)) - \
419 ((_l) & ~((1UL<<(_s))-1))) >> (_s))
420 if ( (1 + /* # L4 */
421 NR(v_start, v_end, L4_PAGETABLE_SHIFT) + /* # L3 */
422 (!is_pv_32on64_domain(d) ?
423 NR(v_start, v_end, L3_PAGETABLE_SHIFT) : /* # L2 */
424 4) + /* # compat L2 */
425 NR(v_start, v_end, L2_PAGETABLE_SHIFT)) /* # L1 */
426 <= nr_pt_pages )
427 break;
428 #endif
429 }
431 order = get_order_from_bytes(v_end - v_start);
432 if ( (1UL << order) > nr_pages )
433 panic("Domain 0 allocation is too small for kernel image.\n");
435 #ifdef __i386__
436 /* Ensure that our low-memory 1:1 mapping covers the allocation. */
437 page = alloc_domheap_pages(d, order,
438 MEMF_bits(30 + (v_start >> 31)));
439 #else
440 page = alloc_domheap_pages(d, order, 0);
441 #endif
442 if ( page == NULL )
443 panic("Not enough RAM for domain 0 allocation.\n");
444 alloc_spfn = page_to_mfn(page);
445 alloc_epfn = alloc_spfn + d->tot_pages;
447 printk("PHYSICAL MEMORY ARRANGEMENT:\n"
448 " Dom0 alloc.: %"PRIpaddr"->%"PRIpaddr,
449 pfn_to_paddr(alloc_spfn), pfn_to_paddr(alloc_epfn));
450 if ( d->tot_pages < nr_pages )
451 printk(" (%lu pages to be allocated)",
452 nr_pages - d->tot_pages);
453 printk("\nVIRTUAL MEMORY ARRANGEMENT:\n"
454 " Loaded kernel: %p->%p\n"
455 " Init. ramdisk: %p->%p\n"
456 " Phys-Mach map: %p->%p\n"
457 " Start info: %p->%p\n"
458 " Page tables: %p->%p\n"
459 " Boot stack: %p->%p\n"
460 " TOTAL: %p->%p\n",
461 _p(vkern_start), _p(vkern_end),
462 _p(vinitrd_start), _p(vinitrd_end),
463 _p(vphysmap_start), _p(vphysmap_end),
464 _p(vstartinfo_start), _p(vstartinfo_end),
465 _p(vpt_start), _p(vpt_end),
466 _p(vstack_start), _p(vstack_end),
467 _p(v_start), _p(v_end));
468 printk(" ENTRY ADDRESS: %p\n", _p(parms.virt_entry));
470 if ( ((v_end - v_start)>>PAGE_SHIFT) > nr_pages )
471 {
472 printk("Initial guest OS requires too much space\n"
473 "(%luMB is greater than %luMB limit)\n",
474 (v_end-v_start)>>20, nr_pages>>(20-PAGE_SHIFT));
475 return -ENOMEM;
476 }
478 mpt_alloc = (vpt_start - v_start) +
479 (unsigned long)pfn_to_paddr(alloc_spfn);
481 #if defined(__i386__)
482 /*
483 * Protect the lowest 1GB of memory. We use a temporary mapping there
484 * from which we copy the kernel and ramdisk images.
485 */
486 if ( v_start < (1UL<<30) )
487 {
488 printk("Initial loading isn't allowed to lowest 1GB of memory.\n");
489 return -EINVAL;
490 }
492 /* WARNING: The new domain must have its 'processor' field filled in! */
493 #if CONFIG_PAGING_LEVELS == 3
494 l3start = l3tab = (l3_pgentry_t *)mpt_alloc; mpt_alloc += PAGE_SIZE;
495 l2start = l2tab = (l2_pgentry_t *)mpt_alloc; mpt_alloc += 4*PAGE_SIZE;
496 memcpy(l2tab, idle_pg_table_l2, 4*PAGE_SIZE);
497 for (i = 0; i < 4; i++) {
498 l3tab[i] = l3e_from_paddr((u32)l2tab + i*PAGE_SIZE, L3_PROT);
499 l2tab[(LINEAR_PT_VIRT_START >> L2_PAGETABLE_SHIFT)+i] =
500 l2e_from_paddr((u32)l2tab + i*PAGE_SIZE, __PAGE_HYPERVISOR);
501 }
502 v->arch.guest_table = pagetable_from_paddr((unsigned long)l3start);
503 #else
504 l2start = l2tab = (l2_pgentry_t *)mpt_alloc; mpt_alloc += PAGE_SIZE;
505 copy_page(l2tab, idle_pg_table);
506 l2tab[LINEAR_PT_VIRT_START >> L2_PAGETABLE_SHIFT] =
507 l2e_from_paddr((unsigned long)l2start, __PAGE_HYPERVISOR);
508 v->arch.guest_table = pagetable_from_paddr((unsigned long)l2start);
509 #endif
511 for ( i = 0; i < PDPT_L2_ENTRIES; i++ )
512 l2tab[l2_linear_offset(PERDOMAIN_VIRT_START) + i] =
513 l2e_from_page(virt_to_page(d->arch.mm_perdomain_pt) + i,
514 __PAGE_HYPERVISOR);
516 l2tab += l2_linear_offset(v_start);
517 mfn = alloc_spfn;
518 for ( count = 0; count < ((v_end-v_start)>>PAGE_SHIFT); count++ )
519 {
520 if ( !((unsigned long)l1tab & (PAGE_SIZE-1)) )
521 {
522 l1start = l1tab = (l1_pgentry_t *)mpt_alloc;
523 mpt_alloc += PAGE_SIZE;
524 *l2tab = l2e_from_paddr((unsigned long)l1start, L2_PROT);
525 l2tab++;
526 clear_page(l1tab);
527 if ( count == 0 )
528 l1tab += l1_table_offset(v_start);
529 }
530 *l1tab = l1e_from_pfn(mfn, L1_PROT);
531 l1tab++;
533 page = mfn_to_page(mfn);
534 if ( !get_page_and_type(page, d, PGT_writable_page) )
535 BUG();
537 mfn++;
538 }
540 /* Pages that are part of page tables must be read only. */
541 l2tab = l2start + l2_linear_offset(vpt_start);
542 l1start = l1tab = (l1_pgentry_t *)(u32)l2e_get_paddr(*l2tab);
543 l1tab += l1_table_offset(vpt_start);
544 for ( count = 0; count < nr_pt_pages; count++ )
545 {
546 page = mfn_to_page(l1e_get_pfn(*l1tab));
547 if ( !opt_dom0_shadow )
548 l1e_remove_flags(*l1tab, _PAGE_RW);
549 else
550 if ( !get_page_type(page, PGT_writable_page) )
551 BUG();
553 #if CONFIG_PAGING_LEVELS == 3
554 switch (count) {
555 case 0:
556 page->u.inuse.type_info &= ~PGT_type_mask;
557 page->u.inuse.type_info |= PGT_l3_page_table;
558 get_page(page, d); /* an extra ref because of readable mapping */
560 /* Get another ref to L3 page so that it can be pinned. */
561 if ( !get_page_and_type(page, d, PGT_l3_page_table) )
562 BUG();
563 set_bit(_PGT_pinned, &page->u.inuse.type_info);
564 break;
565 case 1 ... 4:
566 page->u.inuse.type_info &= ~PGT_type_mask;
567 page->u.inuse.type_info |= PGT_l2_page_table;
568 if ( count == 4 )
569 page->u.inuse.type_info |= PGT_pae_xen_l2;
570 get_page(page, d); /* an extra ref because of readable mapping */
571 break;
572 default:
573 page->u.inuse.type_info &= ~PGT_type_mask;
574 page->u.inuse.type_info |= PGT_l1_page_table;
575 get_page(page, d); /* an extra ref because of readable mapping */
576 break;
577 }
578 #else
579 if ( count == 0 )
580 {
581 page->u.inuse.type_info &= ~PGT_type_mask;
582 page->u.inuse.type_info |= PGT_l2_page_table;
584 /*
585 * No longer writable: decrement the type_count.
586 * Installed as CR3: increment both the ref_count and type_count.
587 * Net: just increment the ref_count.
588 */
589 get_page(page, d); /* an extra ref because of readable mapping */
591 /* Get another ref to L2 page so that it can be pinned. */
592 if ( !get_page_and_type(page, d, PGT_l2_page_table) )
593 BUG();
594 set_bit(_PGT_pinned, &page->u.inuse.type_info);
595 }
596 else
597 {
598 page->u.inuse.type_info &= ~PGT_type_mask;
599 page->u.inuse.type_info |= PGT_l1_page_table;
601 /*
602 * No longer writable: decrement the type_count.
603 * This is an L1 page, installed in a validated L2 page:
604 * increment both the ref_count and type_count.
605 * Net: just increment the ref_count.
606 */
607 get_page(page, d); /* an extra ref because of readable mapping */
608 }
609 #endif
610 if ( !((unsigned long)++l1tab & (PAGE_SIZE - 1)) )
611 l1start = l1tab = (l1_pgentry_t *)(u32)l2e_get_paddr(*++l2tab);
612 }
614 #elif defined(__x86_64__)
616 /* Overlap with Xen protected area? */
617 if ( !is_pv_32on64_domain(d) ?
618 ((v_start < HYPERVISOR_VIRT_END) &&
619 (v_end > HYPERVISOR_VIRT_START)) :
620 (v_end > HYPERVISOR_COMPAT_VIRT_START(d)) )
621 {
622 printk("DOM0 image overlaps with Xen private area.\n");
623 return -EINVAL;
624 }
626 if ( is_pv_32on64_domain(d) )
627 {
628 v->arch.guest_context.failsafe_callback_cs = FLAT_COMPAT_KERNEL_CS;
629 v->arch.guest_context.event_callback_cs = FLAT_COMPAT_KERNEL_CS;
630 }
632 /* WARNING: The new domain must have its 'processor' field filled in! */
633 if ( !is_pv_32on64_domain(d) )
634 {
635 maddr_to_page(mpt_alloc)->u.inuse.type_info = PGT_l4_page_table;
636 l4start = l4tab = __va(mpt_alloc); mpt_alloc += PAGE_SIZE;
637 }
638 else
639 {
640 page = alloc_domheap_page(NULL);
641 if ( !page )
642 panic("Not enough RAM for domain 0 PML4.\n");
643 l4start = l4tab = page_to_virt(page);
644 }
645 copy_page(l4tab, idle_pg_table);
646 l4tab[l4_table_offset(LINEAR_PT_VIRT_START)] =
647 l4e_from_paddr(__pa(l4start), __PAGE_HYPERVISOR);
648 l4tab[l4_table_offset(PERDOMAIN_VIRT_START)] =
649 l4e_from_paddr(__pa(d->arch.mm_perdomain_l3), __PAGE_HYPERVISOR);
650 v->arch.guest_table = pagetable_from_paddr(__pa(l4start));
651 if ( is_pv_32on64_domain(d) )
652 {
653 v->arch.guest_table_user = v->arch.guest_table;
654 if ( setup_arg_xlat_area(v, l4start) < 0 )
655 panic("Not enough RAM for domain 0 hypercall argument translation.\n");
656 }
658 l4tab += l4_table_offset(v_start);
659 mfn = alloc_spfn;
660 for ( count = 0; count < ((v_end-v_start)>>PAGE_SHIFT); count++ )
661 {
662 if ( !((unsigned long)l1tab & (PAGE_SIZE-1)) )
663 {
664 maddr_to_page(mpt_alloc)->u.inuse.type_info = PGT_l1_page_table;
665 l1start = l1tab = __va(mpt_alloc); mpt_alloc += PAGE_SIZE;
666 clear_page(l1tab);
667 if ( count == 0 )
668 l1tab += l1_table_offset(v_start);
669 if ( !((unsigned long)l2tab & (PAGE_SIZE-1)) )
670 {
671 maddr_to_page(mpt_alloc)->u.inuse.type_info = PGT_l2_page_table;
672 l2start = l2tab = __va(mpt_alloc); mpt_alloc += PAGE_SIZE;
673 clear_page(l2tab);
674 if ( count == 0 )
675 l2tab += l2_table_offset(v_start);
676 if ( !((unsigned long)l3tab & (PAGE_SIZE-1)) )
677 {
678 maddr_to_page(mpt_alloc)->u.inuse.type_info =
679 PGT_l3_page_table;
680 l3start = l3tab = __va(mpt_alloc); mpt_alloc += PAGE_SIZE;
681 clear_page(l3tab);
682 if ( count == 0 )
683 l3tab += l3_table_offset(v_start);
684 *l4tab = l4e_from_paddr(__pa(l3start), L4_PROT);
685 l4tab++;
686 }
687 *l3tab = l3e_from_paddr(__pa(l2start), L3_PROT);
688 l3tab++;
689 }
690 *l2tab = l2e_from_paddr(__pa(l1start), L2_PROT);
691 l2tab++;
692 }
693 *l1tab = l1e_from_pfn(mfn, (!is_pv_32on64_domain(d) ?
694 L1_PROT : COMPAT_L1_PROT));
695 l1tab++;
697 page = mfn_to_page(mfn);
698 if ( (page->u.inuse.type_info == 0) &&
699 !get_page_and_type(page, d, PGT_writable_page) )
700 BUG();
702 mfn++;
703 }
705 #ifdef CONFIG_COMPAT
706 if ( is_pv_32on64_domain(d) )
707 {
708 /* Ensure the first four L3 entries are all populated. */
709 for ( i = 0, l3tab = l3start; i < 4; ++i, ++l3tab )
710 {
711 if ( !l3e_get_intpte(*l3tab) )
712 {
713 maddr_to_page(mpt_alloc)->u.inuse.type_info = PGT_l2_page_table;
714 l2tab = __va(mpt_alloc); mpt_alloc += PAGE_SIZE;
715 clear_page(l2tab);
716 *l3tab = l3e_from_paddr(__pa(l2tab), L3_PROT);
717 }
718 if ( i == 3 )
719 l3e_get_page(*l3tab)->u.inuse.type_info |= PGT_pae_xen_l2;
720 }
721 /* Install read-only guest visible MPT mapping. */
722 l2tab = l3e_to_l2e(l3start[3]);
723 memcpy(&l2tab[COMPAT_L2_PAGETABLE_FIRST_XEN_SLOT(d)],
724 &compat_idle_pg_table_l2[l2_table_offset(HIRO_COMPAT_MPT_VIRT_START)],
725 COMPAT_L2_PAGETABLE_XEN_SLOTS(d) * sizeof(*l2tab));
726 }
727 #endif
729 /* Pages that are part of page tables must be read only. */
730 l4tab = l4start + l4_table_offset(vpt_start);
731 l3start = l3tab = l4e_to_l3e(*l4tab);
732 l3tab += l3_table_offset(vpt_start);
733 l2start = l2tab = l3e_to_l2e(*l3tab);
734 l2tab += l2_table_offset(vpt_start);
735 l1start = l1tab = l2e_to_l1e(*l2tab);
736 l1tab += l1_table_offset(vpt_start);
737 for ( count = 0; count < nr_pt_pages; count++ )
738 {
739 l1e_remove_flags(*l1tab, _PAGE_RW);
740 page = mfn_to_page(l1e_get_pfn(*l1tab));
742 /* Read-only mapping + PGC_allocated + page-table page. */
743 page->count_info = PGC_allocated | 3;
744 page->u.inuse.type_info |= PGT_validated | 1;
746 /* Top-level p.t. is pinned. */
747 if ( (page->u.inuse.type_info & PGT_type_mask) ==
748 (!is_pv_32on64_domain(d) ?
749 PGT_l4_page_table : PGT_l3_page_table) )
750 {
751 page->count_info += 1;
752 page->u.inuse.type_info += 1 | PGT_pinned;
753 }
755 /* Iterate. */
756 if ( !((unsigned long)++l1tab & (PAGE_SIZE - 1)) )
757 {
758 if ( !((unsigned long)++l2tab & (PAGE_SIZE - 1)) )
759 {
760 if ( !((unsigned long)++l3tab & (PAGE_SIZE - 1)) )
761 l3start = l3tab = l4e_to_l3e(*++l4tab);
762 l2start = l2tab = l3e_to_l2e(*l3tab);
763 }
764 l1start = l1tab = l2e_to_l1e(*l2tab);
765 }
766 }
768 #endif /* __x86_64__ */
770 /* Mask all upcalls... */
771 for ( i = 0; i < MAX_VIRT_CPUS; i++ )
772 shared_info(d, vcpu_info[i].evtchn_upcall_mask) = 1;
774 if ( opt_dom0_max_vcpus == 0 )
775 opt_dom0_max_vcpus = num_online_cpus();
776 if ( opt_dom0_max_vcpus > num_online_cpus() )
777 opt_dom0_max_vcpus = num_online_cpus();
778 if ( opt_dom0_max_vcpus > MAX_VIRT_CPUS )
779 opt_dom0_max_vcpus = MAX_VIRT_CPUS;
780 if ( opt_dom0_max_vcpus > BITS_PER_GUEST_LONG(d) )
781 opt_dom0_max_vcpus = BITS_PER_GUEST_LONG(d);
782 printk("Dom0 has maximum %u VCPUs\n", opt_dom0_max_vcpus);
784 for ( i = 1; i < opt_dom0_max_vcpus; i++ )
785 (void)alloc_vcpu(d, i, i);
787 /* Set up CR3 value for write_ptbase */
788 if ( paging_mode_enabled(v->domain) )
789 paging_update_paging_modes(v);
790 else
791 update_cr3(v);
793 /* Install the new page tables. */
794 local_irq_disable();
795 write_ptbase(v);
797 /* Copy the OS image and free temporary buffer. */
798 elf.dest = (void*)vkern_start;
799 elf_load_binary(&elf);
801 if ( UNSET_ADDR != parms.virt_hypercall )
802 {
803 if ( (parms.virt_hypercall < v_start) ||
804 (parms.virt_hypercall >= v_end) )
805 {
806 write_ptbase(current);
807 local_irq_enable();
808 printk("Invalid HYPERCALL_PAGE field in ELF notes.\n");
809 return -1;
810 }
811 hypercall_page_initialise(d, (void *)(unsigned long)parms.virt_hypercall);
812 }
814 /* Copy the initial ramdisk. */
815 if ( initrd_len != 0 )
816 memcpy((void *)vinitrd_start, initrd_start, initrd_len);
818 /* Free temporary buffers. */
819 discard_initial_images();
821 /* Set up start info area. */
822 si = (start_info_t *)vstartinfo_start;
823 clear_page(si);
824 si->nr_pages = nr_pages;
826 si->shared_info = virt_to_maddr(d->shared_info);
828 si->flags = SIF_PRIVILEGED | SIF_INITDOMAIN;
829 si->pt_base = vpt_start + 2 * PAGE_SIZE * !!is_pv_32on64_domain(d);
830 si->nr_pt_frames = nr_pt_pages;
831 si->mfn_list = vphysmap_start;
832 snprintf(si->magic, sizeof(si->magic), "xen-%i.%i-x86_%d%s",
833 xen_major_version(), xen_minor_version(),
834 elf_64bit(&elf) ? 64 : 32,
835 parms.pae ? "p" : "");
837 /* Write the phys->machine and machine->phys table entries. */
838 for ( pfn = 0; pfn < d->tot_pages; pfn++ )
839 {
840 mfn = pfn + alloc_spfn;
841 #ifndef NDEBUG
842 #define REVERSE_START ((v_end - v_start) >> PAGE_SHIFT)
843 if ( pfn > REVERSE_START )
844 mfn = alloc_epfn - (pfn - REVERSE_START);
845 #endif
846 if ( !is_pv_32on64_domain(d) )
847 ((unsigned long *)vphysmap_start)[pfn] = mfn;
848 else
849 ((unsigned int *)vphysmap_start)[pfn] = mfn;
850 set_gpfn_from_mfn(mfn, pfn);
851 }
852 while ( pfn < nr_pages )
853 {
854 if ( (page = alloc_chunk(d, nr_pages - d->tot_pages)) == NULL )
855 panic("Not enough RAM for DOM0 reservation.\n");
856 while ( pfn < d->tot_pages )
857 {
858 mfn = page_to_mfn(page);
859 #ifndef NDEBUG
860 #define pfn (nr_pages - 1 - (pfn - (alloc_epfn - alloc_spfn)))
861 #endif
862 if ( !is_pv_32on64_domain(d) )
863 ((unsigned long *)vphysmap_start)[pfn] = mfn;
864 else
865 ((unsigned int *)vphysmap_start)[pfn] = mfn;
866 set_gpfn_from_mfn(mfn, pfn);
867 #undef pfn
868 page++; pfn++;
869 }
870 }
872 if ( initrd_len != 0 )
873 {
874 si->mod_start = vinitrd_start;
875 si->mod_len = initrd_len;
876 printk("Initrd len 0x%lx, start at 0x%lx\n",
877 si->mod_len, si->mod_start);
878 }
880 memset(si->cmd_line, 0, sizeof(si->cmd_line));
881 if ( cmdline != NULL )
882 strlcpy((char *)si->cmd_line, cmdline, sizeof(si->cmd_line));
884 if ( fill_console_start_info((void *)(si + 1)) )
885 {
886 si->console.dom0.info_off = sizeof(struct start_info);
887 si->console.dom0.info_size = sizeof(struct dom0_vga_console_info);
888 }
890 #ifdef CONFIG_COMPAT
891 if ( is_pv_32on64_domain(d) )
892 xlat_start_info(si, XLAT_start_info_console_dom0);
893 #endif
895 /* Reinstate the caller's page tables. */
896 write_ptbase(current);
897 local_irq_enable();
899 #if defined(__i386__)
900 /* Destroy low mappings - they were only for our convenience. */
901 zap_low_mappings(l2start);
902 #endif
904 update_domain_wallclock_time(d);
906 v->is_initialised = 1;
907 clear_bit(_VPF_down, &v->pause_flags);
909 /*
910 * Initial register values:
911 * DS,ES,FS,GS = FLAT_KERNEL_DS
912 * CS:EIP = FLAT_KERNEL_CS:start_pc
913 * SS:ESP = FLAT_KERNEL_SS:start_stack
914 * ESI = start_info
915 * [EAX,EBX,ECX,EDX,EDI,EBP are zero]
916 */
917 regs = &v->arch.guest_context.user_regs;
918 regs->ds = regs->es = regs->fs = regs->gs =
919 !is_pv_32on64_domain(d) ? FLAT_KERNEL_DS : FLAT_COMPAT_KERNEL_DS;
920 regs->ss = (!is_pv_32on64_domain(d) ?
921 FLAT_KERNEL_SS : FLAT_COMPAT_KERNEL_SS);
922 regs->cs = (!is_pv_32on64_domain(d) ?
923 FLAT_KERNEL_CS : FLAT_COMPAT_KERNEL_CS);
924 regs->eip = parms.virt_entry;
925 regs->esp = vstack_end;
926 regs->esi = vstartinfo_start;
927 regs->eflags = X86_EFLAGS_IF;
929 if ( opt_dom0_shadow )
930 if ( paging_enable(d, PG_SH_enable) == 0 )
931 paging_update_paging_modes(v);
933 if ( supervisor_mode_kernel )
934 {
935 v->arch.guest_context.kernel_ss &= ~3;
936 v->arch.guest_context.user_regs.ss &= ~3;
937 v->arch.guest_context.user_regs.es &= ~3;
938 v->arch.guest_context.user_regs.ds &= ~3;
939 v->arch.guest_context.user_regs.fs &= ~3;
940 v->arch.guest_context.user_regs.gs &= ~3;
941 printk("Dom0 runs in ring 0 (supervisor mode)\n");
942 if ( !test_bit(XENFEAT_supervisor_mode_kernel,
943 dom0_features_supported) )
944 panic("Dom0 does not support supervisor-mode execution\n");
945 }
946 else
947 {
948 if ( test_bit(XENFEAT_supervisor_mode_kernel, dom0_features_required) )
949 panic("Dom0 requires supervisor-mode execution\n");
950 }
952 rc = 0;
954 /* DOM0 is permitted full I/O capabilities. */
955 rc |= ioports_permit_access(dom0, 0, 0xFFFF);
956 rc |= iomem_permit_access(dom0, 0UL, ~0UL);
957 rc |= irqs_permit_access(dom0, 0, NR_IRQS-1);
959 /*
960 * Modify I/O port access permissions.
961 */
962 /* Master Interrupt Controller (PIC). */
963 rc |= ioports_deny_access(dom0, 0x20, 0x21);
964 /* Slave Interrupt Controller (PIC). */
965 rc |= ioports_deny_access(dom0, 0xA0, 0xA1);
966 /* Interval Timer (PIT). */
967 rc |= ioports_deny_access(dom0, 0x40, 0x43);
968 /* PIT Channel 2 / PC Speaker Control. */
969 rc |= ioports_deny_access(dom0, 0x61, 0x61);
970 /* Command-line I/O ranges. */
971 process_dom0_ioports_disable();
973 /*
974 * Modify I/O memory access permissions.
975 */
976 /* Local APIC. */
977 if ( mp_lapic_addr != 0 )
978 {
979 mfn = paddr_to_pfn(mp_lapic_addr);
980 rc |= iomem_deny_access(dom0, mfn, mfn);
981 }
982 /* I/O APICs. */
983 for ( i = 0; i < nr_ioapics; i++ )
984 {
985 mfn = paddr_to_pfn(mp_ioapics[i].mpc_apicaddr);
986 if ( smp_found_config )
987 rc |= iomem_deny_access(dom0, mfn, mfn);
988 }
990 BUG_ON(rc != 0);
992 return 0;
993 }
995 /*
996 * Local variables:
997 * mode: C
998 * c-set-style: "BSD"
999 * c-basic-offset: 4
1000 * tab-width: 4
1001 * indent-tabs-mode: nil
1002 * End:
1003 */