debuggers.hg

view xen/arch/x86/domain.c @ 2637:0dfd459518e4

bitkeeper revision 1.1159.1.205 (4162aff3DKXHUIthGYqb0hkSmWnxQw)

Clean up memory auditing, and always an audit a domain before
destroying it. (debug builds only)
author kaf24@freefall.cl.cam.ac.uk
date Tue Oct 05 14:30:11 2004 +0000 (2004-10-05)
parents a8fef40fad11
children 215824d97bfc
line source
1 /******************************************************************************
2 * arch/x86/domain.c
3 *
4 * x86-specific domain handling (e.g., register setup and context switching).
5 */
7 /*
8 * Copyright (C) 1995 Linus Torvalds
9 *
10 * Pentium III FXSR, SSE support
11 * Gareth Hughes <gareth@valinux.com>, May 2000
12 */
14 #include <xen/config.h>
15 #include <xen/lib.h>
16 #include <xen/errno.h>
17 #include <xen/sched.h>
18 #include <xen/smp.h>
19 #include <xen/delay.h>
20 #include <xen/softirq.h>
21 #include <asm/ptrace.h>
22 #include <asm/mc146818rtc.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/mpspec.h>
29 #include <asm/ldt.h>
30 #include <xen/irq.h>
31 #include <xen/event.h>
32 #include <asm/shadow.h>
33 #include <xen/console.h>
34 #include <xen/elf.h>
36 #if !defined(CONFIG_X86_64BITMODE)
37 /* No ring-3 access in initial page tables. */
38 #define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED)
39 #else
40 /* Allow ring-3 access in long mode as guest cannot use ring 1. */
41 #define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_USER)
42 #endif
43 #define L2_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
44 #define L3_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
45 #define L4_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
47 #define round_pgup(_p) (((_p)+(PAGE_SIZE-1))&PAGE_MASK)
48 #define round_pgdown(_p) ((_p)&PAGE_MASK)
50 int hlt_counter;
52 void disable_hlt(void)
53 {
54 hlt_counter++;
55 }
57 void enable_hlt(void)
58 {
59 hlt_counter--;
60 }
62 /*
63 * We use this if we don't have any better
64 * idle routine..
65 */
66 static void default_idle(void)
67 {
68 if ( hlt_counter == 0 )
69 {
70 __cli();
71 if ( !softirq_pending(smp_processor_id()) )
72 safe_halt();
73 else
74 __sti();
75 }
76 }
78 void continue_cpu_idle_loop(void)
79 {
80 int cpu = smp_processor_id();
81 for ( ; ; )
82 {
83 irq_stat[cpu].idle_timestamp = jiffies;
84 while ( !softirq_pending(cpu) )
85 default_idle();
86 do_softirq();
87 }
88 }
90 void startup_cpu_idle_loop(void)
91 {
92 /* Just some sanity to ensure that the scheduler is set up okay. */
93 ASSERT(current->domain == IDLE_DOMAIN_ID);
94 domain_unpause_by_systemcontroller(current);
95 __enter_scheduler();
97 /*
98 * Declares CPU setup done to the boot processor.
99 * Therefore memory barrier to ensure state is visible.
100 */
101 smp_mb();
102 init_idle();
104 continue_cpu_idle_loop();
105 }
107 static long no_idt[2];
108 static int reboot_mode;
109 int reboot_thru_bios = 0;
111 #ifdef CONFIG_SMP
112 int reboot_smp = 0;
113 static int reboot_cpu = -1;
114 /* shamelessly grabbed from lib/vsprintf.c for readability */
115 #define is_digit(c) ((c) >= '0' && (c) <= '9')
116 #endif
119 static inline void kb_wait(void)
120 {
121 int i;
123 for (i=0; i<0x10000; i++)
124 if ((inb_p(0x64) & 0x02) == 0)
125 break;
126 }
129 void machine_restart(char * __unused)
130 {
131 extern int opt_noreboot;
132 #ifdef CONFIG_SMP
133 int cpuid;
134 #endif
136 if ( opt_noreboot )
137 {
138 printk("Reboot disabled on cmdline: require manual reset\n");
139 for ( ; ; ) __asm__ __volatile__ ("hlt");
140 }
142 #ifdef CONFIG_SMP
143 cpuid = GET_APIC_ID(apic_read(APIC_ID));
145 /* KAF: Need interrupts enabled for safe IPI. */
146 __sti();
148 if (reboot_smp) {
150 /* check to see if reboot_cpu is valid
151 if its not, default to the BSP */
152 if ((reboot_cpu == -1) ||
153 (reboot_cpu > (NR_CPUS -1)) ||
154 !(phys_cpu_present_map & (1<<cpuid)))
155 reboot_cpu = boot_cpu_physical_apicid;
157 reboot_smp = 0; /* use this as a flag to only go through this once*/
158 /* re-run this function on the other CPUs
159 it will fall though this section since we have
160 cleared reboot_smp, and do the reboot if it is the
161 correct CPU, otherwise it halts. */
162 if (reboot_cpu != cpuid)
163 smp_call_function((void *)machine_restart , NULL, 1, 0);
164 }
166 /* if reboot_cpu is still -1, then we want a tradional reboot,
167 and if we are not running on the reboot_cpu,, halt */
168 if ((reboot_cpu != -1) && (cpuid != reboot_cpu)) {
169 for (;;)
170 __asm__ __volatile__ ("hlt");
171 }
172 /*
173 * Stop all CPUs and turn off local APICs and the IO-APIC, so
174 * other OSs see a clean IRQ state.
175 */
176 smp_send_stop();
177 disable_IO_APIC();
178 #endif
180 if(!reboot_thru_bios) {
181 /* rebooting needs to touch the page at absolute addr 0 */
182 *((unsigned short *)__va(0x472)) = reboot_mode;
183 for (;;) {
184 int i;
185 for (i=0; i<100; i++) {
186 kb_wait();
187 udelay(50);
188 outb(0xfe,0x64); /* pulse reset low */
189 udelay(50);
190 }
191 /* That didn't work - force a triple fault.. */
192 __asm__ __volatile__("lidt %0": "=m" (no_idt));
193 __asm__ __volatile__("int3");
194 }
195 }
197 panic("Need to reinclude BIOS reboot code\n");
198 }
201 void __attribute__((noreturn)) __machine_halt(void *unused)
202 {
203 for ( ; ; )
204 __asm__ __volatile__ ( "cli; hlt" );
205 }
207 void machine_halt(void)
208 {
209 smp_call_function(__machine_halt, NULL, 1, 1);
210 __machine_halt(NULL);
211 }
213 void free_perdomain_pt(struct domain *d)
214 {
215 free_xenheap_page((unsigned long)d->mm.perdomain_pt);
216 }
218 void arch_do_createdomain(struct domain *d)
219 {
220 d->shared_info = (void *)alloc_xenheap_page();
221 memset(d->shared_info, 0, PAGE_SIZE);
222 d->shared_info->arch.mfn_to_pfn_start =
223 virt_to_phys(&machine_to_phys_mapping[0])>>PAGE_SHIFT;
224 SHARE_PFN_WITH_DOMAIN(virt_to_page(d->shared_info), d);
225 machine_to_phys_mapping[virt_to_phys(d->shared_info) >>
226 PAGE_SHIFT] = INVALID_P2M_ENTRY;
228 d->mm.perdomain_pt = (l1_pgentry_t *)alloc_xenheap_page();
229 memset(d->mm.perdomain_pt, 0, PAGE_SIZE);
230 machine_to_phys_mapping[virt_to_phys(d->mm.perdomain_pt) >>
231 PAGE_SHIFT] = INVALID_P2M_ENTRY;
232 }
234 int arch_final_setup_guestos(struct domain *d, full_execution_context_t *c)
235 {
236 unsigned long phys_basetab;
237 int i, rc;
239 clear_bit(DF_DONEFPUINIT, &d->flags);
240 if ( c->flags & ECF_I387_VALID )
241 set_bit(DF_DONEFPUINIT, &d->flags);
243 memcpy(&d->shared_info->execution_context,
244 &c->cpu_ctxt,
245 sizeof(d->shared_info->execution_context));
247 memcpy(&d->thread.i387,
248 &c->fpu_ctxt,
249 sizeof(d->thread.i387));
251 memcpy(d->thread.traps,
252 &c->trap_ctxt,
253 sizeof(d->thread.traps));
255 #ifdef ARCH_HAS_FAST_TRAP
256 SET_DEFAULT_FAST_TRAP(&d->thread);
257 if ( (rc = (int)set_fast_trap(d, c->fast_trap_idx)) != 0 )
258 return rc;
259 #endif
261 d->mm.ldt_base = c->ldt_base;
262 d->mm.ldt_ents = c->ldt_ents;
264 d->thread.guestos_ss = c->guestos_ss;
265 d->thread.guestos_sp = c->guestos_esp;
267 for ( i = 0; i < 8; i++ )
268 (void)set_debugreg(d, i, c->debugreg[i]);
270 d->event_selector = c->event_callback_cs;
271 d->event_address = c->event_callback_eip;
272 d->failsafe_selector = c->failsafe_callback_cs;
273 d->failsafe_address = c->failsafe_callback_eip;
275 phys_basetab = c->pt_base;
276 d->mm.pagetable = mk_pagetable(phys_basetab);
277 if ( !get_page_and_type(&frame_table[phys_basetab>>PAGE_SHIFT], d,
278 PGT_base_page_table) )
279 return -EINVAL;
281 /* Failure to set GDT is harmless. */
282 SET_GDT_ENTRIES(d, DEFAULT_GDT_ENTRIES);
283 SET_GDT_ADDRESS(d, DEFAULT_GDT_ADDRESS);
284 if ( c->gdt_ents != 0 )
285 {
286 if ( (rc = (int)set_gdt(d, c->gdt_frames, c->gdt_ents)) != 0 )
287 {
288 put_page_and_type(&frame_table[phys_basetab>>PAGE_SHIFT]);
289 return rc;
290 }
291 }
293 return 0;
294 }
296 #if defined(__i386__)
298 void new_thread(struct domain *p,
299 unsigned long start_pc,
300 unsigned long start_stack,
301 unsigned long start_info)
302 {
303 execution_context_t *ec = &p->shared_info->execution_context;
305 /*
306 * Initial register values:
307 * DS,ES,FS,GS = FLAT_RING1_DS
308 * CS:EIP = FLAT_RING1_CS:start_pc
309 * SS:ESP = FLAT_RING1_DS:start_stack
310 * ESI = start_info
311 * [EAX,EBX,ECX,EDX,EDI,EBP are zero]
312 */
313 ec->ds = ec->es = ec->fs = ec->gs = ec->ss = FLAT_RING1_DS;
314 ec->cs = FLAT_RING1_CS;
315 ec->eip = start_pc;
316 ec->esp = start_stack;
317 ec->esi = start_info;
319 __save_flags(ec->eflags);
320 ec->eflags |= X86_EFLAGS_IF;
322 /* No fast trap at start of day. */
323 SET_DEFAULT_FAST_TRAP(&p->thread);
324 }
327 /*
328 * This special macro can be used to load a debugging register
329 */
330 #define loaddebug(thread,register) \
331 __asm__("movl %0,%%db" #register \
332 : /* no output */ \
333 :"r" (thread->debugreg[register]))
336 void switch_to(struct domain *prev_p, struct domain *next_p)
337 {
338 struct thread_struct *next = &next_p->thread;
339 struct tss_struct *tss = init_tss + smp_processor_id();
340 execution_context_t *stack_ec = get_execution_context();
341 int i;
343 __cli();
345 /* Switch guest general-register state. */
346 if ( !is_idle_task(prev_p) )
347 {
348 memcpy(&prev_p->shared_info->execution_context,
349 stack_ec,
350 sizeof(*stack_ec));
351 unlazy_fpu(prev_p);
352 CLEAR_FAST_TRAP(&prev_p->thread);
353 }
355 if ( !is_idle_task(next_p) )
356 {
357 memcpy(stack_ec,
358 &next_p->shared_info->execution_context,
359 sizeof(*stack_ec));
361 /*
362 * This is sufficient! If the descriptor DPL differs from CS RPL then
363 * we'll #GP. If DS, ES, FS, GS are DPL 0 then they'll be cleared
364 * automatically. If SS RPL or DPL differs from CS RPL then we'll #GP.
365 */
366 if ( (stack_ec->cs & 3) == 0 )
367 stack_ec->cs = FLAT_RING1_CS;
368 if ( (stack_ec->ss & 3) == 0 )
369 stack_ec->ss = FLAT_RING1_DS;
371 SET_FAST_TRAP(&next_p->thread);
373 /* Switch the guest OS ring-1 stack. */
374 tss->esp1 = next->guestos_sp;
375 tss->ss1 = next->guestos_ss;
377 /* Maybe switch the debug registers. */
378 if ( unlikely(next->debugreg[7]) )
379 {
380 loaddebug(next, 0);
381 loaddebug(next, 1);
382 loaddebug(next, 2);
383 loaddebug(next, 3);
384 /* no 4 and 5 */
385 loaddebug(next, 6);
386 loaddebug(next, 7);
387 }
389 /* Switch page tables. */
390 write_ptbase(&next_p->mm);
391 tlb_clocktick();
392 }
394 if ( unlikely(prev_p->io_bitmap != NULL) ||
395 unlikely(next_p->io_bitmap != NULL) )
396 {
397 if ( next_p->io_bitmap != NULL )
398 {
399 /* Copy in the appropriate parts of the IO bitmap. We use the
400 * selector to copy only the interesting parts of the bitmap. */
402 u64 old_sel = ~0ULL; /* IO bitmap selector for previous task. */
404 if ( prev_p->io_bitmap != NULL)
405 {
406 old_sel = prev_p->io_bitmap_sel;
408 /* Replace any areas of the IO bitmap that had bits cleared. */
409 for ( i = 0; i < sizeof(prev_p->io_bitmap_sel) * 8; i++ )
410 if ( !test_bit(i, &prev_p->io_bitmap_sel) )
411 memcpy(&tss->io_bitmap[i * IOBMP_SELBIT_LWORDS],
412 &next_p->io_bitmap[i * IOBMP_SELBIT_LWORDS],
413 IOBMP_SELBIT_LWORDS * sizeof(unsigned long));
414 }
416 /* Copy in any regions of the new task's bitmap that have bits
417 * clear and we haven't already dealt with. */
418 for ( i = 0; i < sizeof(prev_p->io_bitmap_sel) * 8; i++ )
419 {
420 if ( test_bit(i, &old_sel)
421 && !test_bit(i, &next_p->io_bitmap_sel) )
422 memcpy(&tss->io_bitmap[i * IOBMP_SELBIT_LWORDS],
423 &next_p->io_bitmap[i * IOBMP_SELBIT_LWORDS],
424 IOBMP_SELBIT_LWORDS * sizeof(unsigned long));
425 }
427 tss->bitmap = IO_BITMAP_OFFSET;
429 }
430 else
431 {
432 /* In this case, we're switching FROM a task with IO port access,
433 * to a task that doesn't use the IO bitmap. We set any TSS bits
434 * that might have been cleared, ready for future use. */
435 for ( i = 0; i < sizeof(prev_p->io_bitmap_sel) * 8; i++ )
436 if ( !test_bit(i, &prev_p->io_bitmap_sel) )
437 memset(&tss->io_bitmap[i * IOBMP_SELBIT_LWORDS],
438 0xFF, IOBMP_SELBIT_LWORDS * sizeof(unsigned long));
440 /*
441 * a bitmap offset pointing outside of the TSS limit
442 * causes a nicely controllable SIGSEGV if a process
443 * tries to use a port IO instruction. The first
444 * sys_ioperm() call sets up the bitmap properly.
445 */
446 tss->bitmap = INVALID_IO_BITMAP_OFFSET;
447 }
448 }
450 set_current(next_p);
452 /* Switch GDT and LDT. */
453 __asm__ __volatile__ ("lgdt %0" : "=m" (*next_p->mm.gdt));
454 load_LDT(next_p);
456 __sti();
457 }
460 /* XXX Currently the 'domain' field is ignored! XXX */
461 long do_iopl(domid_t domain, unsigned int new_io_pl)
462 {
463 execution_context_t *ec = get_execution_context();
464 ec->eflags = (ec->eflags & 0xffffcfff) | ((new_io_pl&3) << 12);
465 return 0;
466 }
468 #endif
471 static void relinquish_list(struct domain *d, struct list_head *list)
472 {
473 struct list_head *ent;
474 struct pfn_info *page;
475 unsigned long x, y;
477 /* Use a recursive lock, as we may enter 'free_domheap_page'. */
478 spin_lock_recursive(&d->page_alloc_lock);
480 ent = list->next;
481 while ( ent != list )
482 {
483 page = list_entry(ent, struct pfn_info, list);
485 /* Grab a reference to the page so it won't disappear from under us. */
486 if ( unlikely(!get_page(page, d)) )
487 {
488 /* Couldn't get a reference -- someone is freeing this page. */
489 ent = ent->next;
490 continue;
491 }
493 if ( test_and_clear_bit(_PGT_pinned, &page->u.inuse.type_info) )
494 put_page_and_type(page);
496 if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
497 put_page(page);
499 /*
500 * Forcibly invalidate base page tables at this point to break circular
501 * 'linear page table' references. This is okay because MMU structures
502 * are not shared across domains and this domain is now dead. Thus base
503 * tables are not in use so a non-zero count means circular reference.
504 */
505 y = page->u.inuse.type_info;
506 for ( ; ; )
507 {
508 x = y;
509 if ( likely((x & (PGT_type_mask|PGT_validated)) !=
510 (PGT_base_page_table|PGT_validated)) )
511 break;
513 y = cmpxchg(&page->u.inuse.type_info, x, x & ~PGT_validated);
514 if ( likely(y == x) )
515 {
516 free_page_type(page, PGT_base_page_table);
517 break;
518 }
519 }
521 /* Follow the list chain and /then/ potentially free the page. */
522 ent = ent->next;
523 put_page(page);
524 }
526 spin_unlock_recursive(&d->page_alloc_lock);
527 }
530 void domain_relinquish_memory(struct domain *d)
531 {
532 audit_domain(d);
534 /* Ensure that noone is running over the dead domain's page tables. */
535 synchronise_pagetables(~0UL);
537 /* Exit shadow mode before deconstructing final guest page table. */
538 shadow_mode_disable(d);
540 /* Drop the in-use reference to the page-table base. */
541 if ( pagetable_val(d->mm.pagetable) != 0 )
542 put_page_and_type(&frame_table[pagetable_val(d->mm.pagetable) >>
543 PAGE_SHIFT]);
545 /*
546 * Relinquish GDT mappings. No need for explicit unmapping of the LDT as
547 * it automatically gets squashed when the guest's mappings go away.
548 */
549 destroy_gdt(d);
551 /* Relinquish every page of memory. */
552 relinquish_list(d, &d->xenpage_list);
553 relinquish_list(d, &d->page_list);
554 }
557 int construct_dom0(struct domain *p,
558 unsigned long alloc_start,
559 unsigned long alloc_end,
560 char *image_start, unsigned long image_len,
561 char *initrd_start, unsigned long initrd_len,
562 char *cmdline)
563 {
564 char *dst;
565 int i, rc;
566 unsigned long pfn, mfn;
567 unsigned long nr_pages = (alloc_end - alloc_start) >> PAGE_SHIFT;
568 unsigned long nr_pt_pages;
569 unsigned long count;
570 l2_pgentry_t *l2tab, *l2start;
571 l1_pgentry_t *l1tab = NULL, *l1start = NULL;
572 struct pfn_info *page = NULL;
573 start_info_t *si;
575 /*
576 * This fully describes the memory layout of the initial domain. All
577 * *_start address are page-aligned, except v_start (and v_end) which are
578 * superpage-aligned.
579 */
580 struct domain_setup_info dsi;
581 unsigned long vinitrd_start;
582 unsigned long vinitrd_end;
583 unsigned long vphysmap_start;
584 unsigned long vphysmap_end;
585 unsigned long vstartinfo_start;
586 unsigned long vstartinfo_end;
587 unsigned long vstack_start;
588 unsigned long vstack_end;
589 unsigned long vpt_start;
590 unsigned long vpt_end;
591 unsigned long v_end;
593 /* Machine address of next candidate page-table page. */
594 unsigned long mpt_alloc;
596 extern void physdev_init_dom0(struct domain *);
598 /* Sanity! */
599 if ( p->domain != 0 )
600 BUG();
601 if ( test_bit(DF_CONSTRUCTED, &p->flags) )
602 BUG();
604 memset(&dsi, 0, sizeof(struct domain_setup_info));
606 printk("*** LOADING DOMAIN 0 ***\n");
608 /*
609 * This is all a bit grim. We've moved the modules to the "safe" physical
610 * memory region above MAP_DIRECTMAP_ADDRESS (48MB). Later in this
611 * routine we're going to copy it down into the region that's actually
612 * been allocated to domain 0. This is highly likely to be overlapping, so
613 * we use a forward copy.
614 *
615 * MAP_DIRECTMAP_ADDRESS should be safe. The worst case is a machine with
616 * 4GB and lots of network/disk cards that allocate loads of buffers.
617 * We'll have to revisit this if we ever support PAE (64GB).
618 */
620 rc = parseelfimage(image_start, image_len, &dsi);
621 if ( rc != 0 )
622 return rc;
624 /* Set up domain options */
625 if ( dsi.use_writable_pagetables )
626 vm_assist(p, VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
628 if ( (dsi.v_start & (PAGE_SIZE-1)) != 0 )
629 {
630 printk("Initial guest OS must load to a page boundary.\n");
631 return -EINVAL;
632 }
634 /*
635 * Why do we need this? The number of page-table frames depends on the
636 * size of the bootstrap address space. But the size of the address space
637 * depends on the number of page-table frames (since each one is mapped
638 * read-only). We have a pair of simultaneous equations in two unknowns,
639 * which we solve by exhaustive search.
640 */
641 vinitrd_start = round_pgup(dsi.v_kernend);
642 vinitrd_end = vinitrd_start + initrd_len;
643 vphysmap_start = round_pgup(vinitrd_end);
644 vphysmap_end = vphysmap_start + (nr_pages * sizeof(unsigned long));
645 vpt_start = round_pgup(vphysmap_end);
646 for ( nr_pt_pages = 2; ; nr_pt_pages++ )
647 {
648 vpt_end = vpt_start + (nr_pt_pages * PAGE_SIZE);
649 vstartinfo_start = vpt_end;
650 vstartinfo_end = vstartinfo_start + PAGE_SIZE;
651 vstack_start = vstartinfo_end;
652 vstack_end = vstack_start + PAGE_SIZE;
653 v_end = (vstack_end + (1<<22)-1) & ~((1<<22)-1);
654 if ( (v_end - vstack_end) < (512 << 10) )
655 v_end += 1 << 22; /* Add extra 4MB to get >= 512kB padding. */
656 if ( (((v_end - dsi.v_start + ((1<<L2_PAGETABLE_SHIFT)-1)) >>
657 L2_PAGETABLE_SHIFT) + 1) <= nr_pt_pages )
658 break;
659 }
661 printk("PHYSICAL MEMORY ARRANGEMENT:\n"
662 " Kernel image: %p->%p\n"
663 " Initrd image: %p->%p\n"
664 " Dom0 alloc.: %08lx->%08lx\n",
665 image_start, image_start + image_len,
666 initrd_start, initrd_start + initrd_len,
667 alloc_start, alloc_end);
668 printk("VIRTUAL MEMORY ARRANGEMENT:\n"
669 " Loaded kernel: %08lx->%08lx\n"
670 " Init. ramdisk: %08lx->%08lx\n"
671 " Phys-Mach map: %08lx->%08lx\n"
672 " Page tables: %08lx->%08lx\n"
673 " Start info: %08lx->%08lx\n"
674 " Boot stack: %08lx->%08lx\n"
675 " TOTAL: %08lx->%08lx\n",
676 dsi.v_kernstart, dsi.v_kernend,
677 vinitrd_start, vinitrd_end,
678 vphysmap_start, vphysmap_end,
679 vpt_start, vpt_end,
680 vstartinfo_start, vstartinfo_end,
681 vstack_start, vstack_end,
682 dsi.v_start, v_end);
683 printk(" ENTRY ADDRESS: %08lx\n", dsi.v_kernentry);
685 if ( (v_end - dsi.v_start) > (nr_pages * PAGE_SIZE) )
686 {
687 printk("Initial guest OS requires too much space\n"
688 "(%luMB is greater than %luMB limit)\n",
689 (v_end-dsi.v_start)>>20, (nr_pages<<PAGE_SHIFT)>>20);
690 return -ENOMEM;
691 }
693 /*
694 * Protect the lowest 1GB of memory. We use a temporary mapping there
695 * from which we copy the kernel and ramdisk images.
696 */
697 if ( dsi.v_start < (1<<30) )
698 {
699 printk("Initial loading isn't allowed to lowest 1GB of memory.\n");
700 return -EINVAL;
701 }
703 /* Construct a frame-allocation list for the initial domain. */
704 for ( mfn = (alloc_start>>PAGE_SHIFT);
705 mfn < (alloc_end>>PAGE_SHIFT);
706 mfn++ )
707 {
708 page = &frame_table[mfn];
709 page->u.inuse.domain = p;
710 page->u.inuse.type_info = 0;
711 page->count_info = PGC_always_set | PGC_allocated | 1;
712 list_add_tail(&page->list, &p->page_list);
713 p->tot_pages++; p->max_pages++;
714 }
716 mpt_alloc = (vpt_start - dsi.v_start) + alloc_start;
718 SET_GDT_ENTRIES(p, DEFAULT_GDT_ENTRIES);
719 SET_GDT_ADDRESS(p, DEFAULT_GDT_ADDRESS);
721 /*
722 * We're basically forcing default RPLs to 1, so that our "what privilege
723 * level are we returning to?" logic works.
724 */
725 p->failsafe_selector = FLAT_GUESTOS_CS;
726 p->event_selector = FLAT_GUESTOS_CS;
727 p->thread.guestos_ss = FLAT_GUESTOS_DS;
728 for ( i = 0; i < 256; i++ )
729 p->thread.traps[i].cs = FLAT_GUESTOS_CS;
731 /* WARNING: The new domain must have its 'processor' field filled in! */
732 l2start = l2tab = (l2_pgentry_t *)mpt_alloc; mpt_alloc += PAGE_SIZE;
733 memcpy(l2tab, &idle_pg_table[0], PAGE_SIZE);
734 l2tab[LINEAR_PT_VIRT_START >> L2_PAGETABLE_SHIFT] =
735 mk_l2_pgentry((unsigned long)l2start | __PAGE_HYPERVISOR);
736 l2tab[PERDOMAIN_VIRT_START >> L2_PAGETABLE_SHIFT] =
737 mk_l2_pgentry(__pa(p->mm.perdomain_pt) | __PAGE_HYPERVISOR);
738 p->mm.pagetable = mk_pagetable((unsigned long)l2start);
740 l2tab += l2_table_offset(dsi.v_start);
741 mfn = alloc_start >> PAGE_SHIFT;
742 for ( count = 0; count < ((v_end-dsi.v_start)>>PAGE_SHIFT); count++ )
743 {
744 if ( !((unsigned long)l1tab & (PAGE_SIZE-1)) )
745 {
746 l1start = l1tab = (l1_pgentry_t *)mpt_alloc;
747 mpt_alloc += PAGE_SIZE;
748 *l2tab++ = mk_l2_pgentry((unsigned long)l1start | L2_PROT);
749 clear_page(l1tab);
750 if ( count == 0 )
751 l1tab += l1_table_offset(dsi.v_start);
752 }
753 *l1tab++ = mk_l1_pgentry((mfn << PAGE_SHIFT) | L1_PROT);
755 page = &frame_table[mfn];
756 if ( !get_page_and_type(page, p, PGT_writable_page) )
757 BUG();
759 mfn++;
760 }
762 /* Pages that are part of page tables must be read only. */
763 l2tab = l2start + l2_table_offset(vpt_start);
764 l1start = l1tab = (l1_pgentry_t *)l2_pgentry_to_phys(*l2tab);
765 l1tab += l1_table_offset(vpt_start);
766 l2tab++;
767 for ( count = 0; count < nr_pt_pages; count++ )
768 {
769 *l1tab = mk_l1_pgentry(l1_pgentry_val(*l1tab) & ~_PAGE_RW);
770 page = &frame_table[l1_pgentry_to_pagenr(*l1tab)];
771 if ( count == 0 )
772 {
773 page->u.inuse.type_info &= ~PGT_type_mask;
774 page->u.inuse.type_info |= PGT_l2_page_table;
775 get_page(page, p); /* an extra ref because of readable mapping */
776 /* Get another ref to L2 page so that it can be pinned. */
777 if ( !get_page_and_type(page, p, PGT_l2_page_table) )
778 BUG();
779 set_bit(_PGT_pinned, &page->u.inuse.type_info);
780 }
781 else
782 {
783 page->u.inuse.type_info &= ~PGT_type_mask;
784 page->u.inuse.type_info |= PGT_l1_page_table;
785 page->u.inuse.type_info |=
786 ((dsi.v_start>>L2_PAGETABLE_SHIFT)+(count-1))<<PGT_va_shift;
788 get_page(page, p); /* an extra ref because of readable mapping */
789 }
790 l1tab++;
791 if( !((unsigned long)l1tab & (PAGE_SIZE - 1)) )
792 l1start = l1tab = (l1_pgentry_t *)l2_pgentry_to_phys(*l2tab);
793 }
795 /* Set up shared-info area. */
796 update_dom_time(p->shared_info);
797 p->shared_info->domain_time = 0;
798 /* Mask all upcalls... */
799 for ( i = 0; i < MAX_VIRT_CPUS; i++ )
800 p->shared_info->vcpu_data[i].evtchn_upcall_mask = 1;
802 /* Install the new page tables. */
803 __cli();
804 write_ptbase(&p->mm);
806 /* Copy the OS image. */
807 (void)loadelfimage(image_start);
809 /* Copy the initial ramdisk. */
810 if ( initrd_len != 0 )
811 memcpy((void *)vinitrd_start, initrd_start, initrd_len);
813 /* Set up start info area. */
814 si = (start_info_t *)vstartinfo_start;
815 memset(si, 0, PAGE_SIZE);
816 si->nr_pages = p->tot_pages;
817 si->shared_info = virt_to_phys(p->shared_info);
818 si->flags = SIF_PRIVILEGED | SIF_INITDOMAIN;
819 si->pt_base = vpt_start;
820 si->nr_pt_frames = nr_pt_pages;
821 si->mfn_list = vphysmap_start;
823 /* Write the phys->machine and machine->phys table entries. */
824 for ( pfn = 0; pfn < p->tot_pages; pfn++ )
825 {
826 mfn = pfn + (alloc_start>>PAGE_SHIFT);
827 #ifndef NDEBUG
828 #define REVERSE_START ((v_end - dsi.v_start) >> PAGE_SHIFT)
829 if ( pfn > REVERSE_START )
830 mfn = (alloc_end>>PAGE_SHIFT) - (pfn - REVERSE_START);
831 #endif
832 ((unsigned long *)vphysmap_start)[pfn] = mfn;
833 machine_to_phys_mapping[mfn] = pfn;
834 }
836 if ( initrd_len != 0 )
837 {
838 si->mod_start = vinitrd_start;
839 si->mod_len = initrd_len;
840 printk("Initrd len 0x%lx, start at 0x%08lx\n",
841 si->mod_len, si->mod_start);
842 }
844 dst = si->cmd_line;
845 if ( cmdline != NULL )
846 {
847 for ( i = 0; i < 255; i++ )
848 {
849 if ( cmdline[i] == '\0' )
850 break;
851 *dst++ = cmdline[i];
852 }
853 }
854 *dst = '\0';
856 /* Reinstate the caller's page tables. */
857 write_ptbase(&current->mm);
858 __sti();
860 /* Destroy low mappings - they were only for our convenience. */
861 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
862 if ( l2_pgentry_val(l2start[i]) & _PAGE_PSE )
863 l2start[i] = mk_l2_pgentry(0);
864 zap_low_mappings(); /* Do the same for the idle page tables. */
866 /* Give up the VGA console if DOM0 is configured to grab it. */
867 console_endboot(cmdline && strstr(cmdline, "tty0"));
869 /* DOM0 gets access to everything. */
870 physdev_init_dom0(p);
872 set_bit(DF_CONSTRUCTED, &p->flags);
874 new_thread(p, dsi.v_kernentry, vstack_end, vstartinfo_start);
876 #if 0 /* XXXXX DO NOT CHECK IN ENABLED !!! (but useful for testing so leave) */
877 shadow_lock(&p->mm);
878 shadow_mode_enable(p, SHM_test);
879 shadow_unlock(&p->mm);
880 #endif
882 return 0;
883 }