debuggers.hg

view xen/arch/x86/domain.c @ 3678:44232b00c34a

bitkeeper revision 1.1159.241.2 (4202bc56K28qaGGkiiKbJBLxsgnXSA)

Enable monitor assert. Earlier assert was broken and was deleted.

Signed-off-by: Joe Cihula <joseph.cihula@intel.com>
Signed-off-by: ian.pratt@cl.cam.ac.uk
author iap10@labyrinth.cl.cam.ac.uk
date Fri Feb 04 00:05:42 2005 +0000 (2005-02-04)
parents 0ef6e8e6e85d
children 26a00896163a
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/init.h>
16 #include <xen/lib.h>
17 #include <xen/errno.h>
18 #include <xen/sched.h>
19 #include <xen/smp.h>
20 #include <xen/delay.h>
21 #include <xen/softirq.h>
22 #include <asm/regs.h>
23 #include <asm/mc146818rtc.h>
24 #include <asm/system.h>
25 #include <asm/io.h>
26 #include <asm/processor.h>
27 #include <asm/desc.h>
28 #include <asm/i387.h>
29 #include <asm/mpspec.h>
30 #include <asm/ldt.h>
31 #include <xen/irq.h>
32 #include <xen/event.h>
33 #include <asm/shadow.h>
34 #include <xen/console.h>
35 #include <xen/elf.h>
36 #include <asm/vmx.h>
37 #include <asm/vmx_vmcs.h>
38 #include <xen/kernel.h>
39 #include <public/io/ioreq.h>
40 #include <xen/multicall.h>
42 /* opt_noreboot: If true, machine will need manual reset on error. */
43 static int opt_noreboot = 0;
44 boolean_param("noreboot", opt_noreboot);
46 #if !defined(CONFIG_X86_64BITMODE)
47 /* No ring-3 access in initial page tables. */
48 #define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED)
49 #else
50 /* Allow ring-3 access in long mode as guest cannot use ring 1. */
51 #define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_USER)
52 #endif
53 #define L2_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
54 #define L3_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
55 #define L4_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
57 #define round_pgup(_p) (((_p)+(PAGE_SIZE-1))&PAGE_MASK)
58 #define round_pgdown(_p) ((_p)&PAGE_MASK)
60 static void default_idle(void)
61 {
62 __cli();
63 if ( !softirq_pending(smp_processor_id()) )
64 safe_halt();
65 else
66 __sti();
67 }
69 static __attribute_used__ void idle_loop(void)
70 {
71 int cpu = smp_processor_id();
72 for ( ; ; )
73 {
74 irq_stat[cpu].idle_timestamp = jiffies;
75 while ( !softirq_pending(cpu) )
76 default_idle();
77 do_softirq();
78 }
79 }
81 void startup_cpu_idle_loop(void)
82 {
83 /* Just some sanity to ensure that the scheduler is set up okay. */
84 ASSERT(current->domain->id == IDLE_DOMAIN_ID);
85 domain_unpause_by_systemcontroller(current->domain);
86 __enter_scheduler();
88 /*
89 * Declares CPU setup done to the boot processor.
90 * Therefore memory barrier to ensure state is visible.
91 */
92 smp_mb();
93 init_idle();
95 idle_loop();
96 }
98 static long no_idt[2];
99 static int reboot_mode;
100 int reboot_thru_bios = 0;
102 #ifdef CONFIG_SMP
103 int reboot_smp = 0;
104 static int reboot_cpu = -1;
105 /* shamelessly grabbed from lib/vsprintf.c for readability */
106 #define is_digit(c) ((c) >= '0' && (c) <= '9')
107 #endif
110 static inline void kb_wait(void)
111 {
112 int i;
114 for (i=0; i<0x10000; i++)
115 if ((inb_p(0x64) & 0x02) == 0)
116 break;
117 }
120 void machine_restart(char * __unused)
121 {
122 #ifdef CONFIG_SMP
123 int cpuid;
124 #endif
126 if ( opt_noreboot )
127 {
128 printk("Reboot disabled on cmdline: require manual reset\n");
129 for ( ; ; ) __asm__ __volatile__ ("hlt");
130 }
132 #ifdef CONFIG_SMP
133 cpuid = GET_APIC_ID(apic_read(APIC_ID));
135 /* KAF: Need interrupts enabled for safe IPI. */
136 __sti();
138 if (reboot_smp) {
140 /* check to see if reboot_cpu is valid
141 if its not, default to the BSP */
142 if ((reboot_cpu == -1) ||
143 (reboot_cpu > (NR_CPUS -1)) ||
144 !(phys_cpu_present_map & (1<<cpuid)))
145 reboot_cpu = boot_cpu_physical_apicid;
147 reboot_smp = 0; /* use this as a flag to only go through this once*/
148 /* re-run this function on the other CPUs
149 it will fall though this section since we have
150 cleared reboot_smp, and do the reboot if it is the
151 correct CPU, otherwise it halts. */
152 if (reboot_cpu != cpuid)
153 smp_call_function((void *)machine_restart , NULL, 1, 0);
154 }
156 /* if reboot_cpu is still -1, then we want a tradional reboot,
157 and if we are not running on the reboot_cpu,, halt */
158 if ((reboot_cpu != -1) && (cpuid != reboot_cpu)) {
159 for (;;)
160 __asm__ __volatile__ ("hlt");
161 }
162 /*
163 * Stop all CPUs and turn off local APICs and the IO-APIC, so
164 * other OSs see a clean IRQ state.
165 */
166 smp_send_stop();
167 disable_IO_APIC();
168 #endif
169 #ifdef CONFIG_VMX
170 stop_vmx();
171 #endif
173 if(!reboot_thru_bios) {
174 /* rebooting needs to touch the page at absolute addr 0 */
175 *((unsigned short *)__va(0x472)) = reboot_mode;
176 for (;;) {
177 int i;
178 for (i=0; i<100; i++) {
179 kb_wait();
180 udelay(50);
181 outb(0xfe,0x64); /* pulse reset low */
182 udelay(50);
183 }
184 /* That didn't work - force a triple fault.. */
185 __asm__ __volatile__("lidt %0": "=m" (no_idt));
186 __asm__ __volatile__("int3");
187 }
188 }
190 panic("Need to reinclude BIOS reboot code\n");
191 }
194 void __attribute__((noreturn)) __machine_halt(void *unused)
195 {
196 for ( ; ; )
197 __asm__ __volatile__ ( "cli; hlt" );
198 }
200 void machine_halt(void)
201 {
202 smp_call_function(__machine_halt, NULL, 1, 1);
203 __machine_halt(NULL);
204 }
206 void dump_pageframe_info(struct domain *d)
207 {
208 struct pfn_info *page;
210 if ( d->tot_pages < 10 )
211 {
212 list_for_each_entry ( page, &d->page_list, list )
213 {
214 printk("Page %08x: caf=%08x, taf=%08x\n",
215 page_to_phys(page), page->count_info,
216 page->u.inuse.type_info);
217 }
218 }
220 page = virt_to_page(d->shared_info);
221 printk("Shared_info@%08x: caf=%08x, taf=%08x\n",
222 page_to_phys(page), page->count_info,
223 page->u.inuse.type_info);
224 }
226 struct domain *arch_alloc_domain_struct(void)
227 {
228 return xmalloc(struct domain);
229 }
231 void arch_free_domain_struct(struct domain *d)
232 {
233 xfree(d);
234 }
236 struct exec_domain *arch_alloc_exec_domain_struct(void)
237 {
238 return xmalloc(struct exec_domain);
239 }
241 void arch_free_exec_domain_struct(struct exec_domain *ed)
242 {
243 xfree(ed);
244 }
246 void free_perdomain_pt(struct domain *d)
247 {
248 free_xenheap_page((unsigned long)d->mm_perdomain_pt);
249 }
251 static void continue_idle_task(struct exec_domain *ed)
252 {
253 reset_stack_and_jump(idle_loop);
254 }
256 static void continue_nonidle_task(struct exec_domain *ed)
257 {
258 reset_stack_and_jump(ret_from_intr);
259 }
261 void arch_do_createdomain(struct exec_domain *ed)
262 {
263 struct domain *d = ed->domain;
264 #ifdef ARCH_HAS_FAST_TRAP
265 SET_DEFAULT_FAST_TRAP(&ed->thread);
266 #endif
268 if ( d->id == IDLE_DOMAIN_ID )
269 {
270 ed->thread.schedule_tail = continue_idle_task;
271 }
272 else
273 {
274 ed->thread.schedule_tail = continue_nonidle_task;
276 d->shared_info = (void *)alloc_xenheap_page();
277 memset(d->shared_info, 0, PAGE_SIZE);
278 ed->vcpu_info = &d->shared_info->vcpu_data[ed->eid];
279 d->shared_info->arch.mfn_to_pfn_start = m2p_start_mfn;
280 SHARE_PFN_WITH_DOMAIN(virt_to_page(d->shared_info), d);
281 machine_to_phys_mapping[virt_to_phys(d->shared_info) >>
282 PAGE_SHIFT] = INVALID_P2M_ENTRY;
284 d->mm_perdomain_pt = (l1_pgentry_t *)alloc_xenheap_page();
285 memset(d->mm_perdomain_pt, 0, PAGE_SIZE);
286 machine_to_phys_mapping[virt_to_phys(d->mm_perdomain_pt) >>
287 PAGE_SHIFT] = INVALID_P2M_ENTRY;
288 ed->mm.perdomain_ptes = d->mm_perdomain_pt;
289 }
290 }
292 #ifdef CONFIG_VMX
293 void arch_vmx_do_resume(struct exec_domain *ed)
294 {
295 u64 vmcs_phys_ptr = (u64) virt_to_phys(ed->thread.arch_vmx.vmcs);
297 load_vmcs(&ed->thread.arch_vmx, vmcs_phys_ptr);
298 vmx_do_resume(ed);
299 reset_stack_and_jump(vmx_asm_do_resume);
300 }
302 void arch_vmx_do_launch(struct exec_domain *ed)
303 {
304 u64 vmcs_phys_ptr = (u64) virt_to_phys(ed->thread.arch_vmx.vmcs);
306 load_vmcs(&ed->thread.arch_vmx, vmcs_phys_ptr);
307 vmx_do_launch(ed);
308 reset_stack_and_jump(vmx_asm_do_launch);
309 }
311 static void monitor_mk_pagetable(struct exec_domain *ed)
312 {
313 unsigned long mpfn;
314 l2_pgentry_t *mpl2e;
315 struct pfn_info *mpfn_info;
316 struct mm_struct *m = &ed->mm;
317 struct domain *d = ed->domain;
319 mpfn_info = alloc_domheap_page(NULL);
320 ASSERT( mpfn_info );
322 mpfn = (unsigned long) (mpfn_info - frame_table);
323 mpl2e = (l2_pgentry_t *) map_domain_mem(mpfn << L1_PAGETABLE_SHIFT);
324 memset(mpl2e, 0, PAGE_SIZE);
326 memcpy(&mpl2e[DOMAIN_ENTRIES_PER_L2_PAGETABLE],
327 &idle_pg_table[DOMAIN_ENTRIES_PER_L2_PAGETABLE],
328 HYPERVISOR_ENTRIES_PER_L2_PAGETABLE * sizeof(l2_pgentry_t));
330 m->monitor_table = mk_pagetable(mpfn << L1_PAGETABLE_SHIFT);
331 m->shadow_mode = SHM_full_32;
333 mpl2e[PERDOMAIN_VIRT_START >> L2_PAGETABLE_SHIFT] =
334 mk_l2_pgentry((__pa(d->mm_perdomain_pt) & PAGE_MASK)
335 | __PAGE_HYPERVISOR);
337 unmap_domain_mem(mpl2e);
338 }
340 /*
341 * Free the pages for monitor_table and guest_pl2e_cache
342 */
343 static void monitor_rm_pagetable(struct exec_domain *ed)
344 {
345 struct mm_struct *m = &ed->mm;
346 l2_pgentry_t *mpl2e;
347 unsigned long mpfn;
349 ASSERT( pagetable_val(m->monitor_table) );
351 mpl2e = (l2_pgentry_t *) map_domain_mem(pagetable_val(m->monitor_table));
352 /*
353 * First get the pfn for guest_pl2e_cache by looking at monitor_table
354 */
355 mpfn = l2_pgentry_val(mpl2e[LINEAR_PT_VIRT_START >> L2_PAGETABLE_SHIFT])
356 >> PAGE_SHIFT;
358 free_domheap_page(&frame_table[mpfn]);
359 unmap_domain_mem(mpl2e);
361 /*
362 * Then free monitor_table.
363 */
364 mpfn = (pagetable_val(m->monitor_table)) >> PAGE_SHIFT;
365 free_domheap_page(&frame_table[mpfn]);
367 m->monitor_table = mk_pagetable(0);
368 }
370 static int vmx_final_setup_guestos(struct exec_domain *ed,
371 full_execution_context_t *full_context)
372 {
373 int error;
374 execution_context_t *context;
375 struct vmcs_struct *vmcs;
377 context = &full_context->cpu_ctxt;
379 /*
380 * Create a new VMCS
381 */
382 if (!(vmcs = alloc_vmcs())) {
383 printk("Failed to create a new VMCS\n");
384 return -ENOMEM;
385 }
387 memset(&ed->thread.arch_vmx, 0, sizeof (struct arch_vmx_struct));
389 ed->thread.arch_vmx.vmcs = vmcs;
390 error = construct_vmcs(&ed->thread.arch_vmx, context, full_context, VMCS_USE_HOST_ENV);
391 if (error < 0) {
392 printk("Failed to construct a new VMCS\n");
393 goto out;
394 }
396 monitor_mk_pagetable(ed);
397 ed->thread.schedule_tail = arch_vmx_do_launch;
398 clear_bit(VMX_CPU_STATE_PG_ENABLED, &ed->thread.arch_vmx.cpu_state);
400 #if defined (__i386)
401 ed->thread.arch_vmx.vmx_platform.real_mode_data =
402 (unsigned long *) context->esi;
403 #endif
405 if (ed == ed->domain->exec_domain[0]) {
406 /*
407 * Required to do this once per domain
408 */
409 memset(&ed->domain->shared_info->evtchn_mask[0], 0xff,
410 sizeof(ed->domain->shared_info->evtchn_mask));
411 clear_bit(IOPACKET_PORT, &ed->domain->shared_info->evtchn_mask[0]);
412 }
414 return 0;
416 out:
417 free_vmcs(vmcs);
418 ed->thread.arch_vmx.vmcs = 0;
419 return error;
420 }
421 #endif
423 int arch_final_setup_guestos(struct exec_domain *d, full_execution_context_t *c)
424 {
425 unsigned long phys_basetab;
426 int i, rc;
428 clear_bit(EDF_DONEFPUINIT, &d->ed_flags);
429 if ( c->flags & ECF_I387_VALID )
430 set_bit(EDF_DONEFPUINIT, &d->ed_flags);
432 memcpy(&d->thread.user_ctxt,
433 &c->cpu_ctxt,
434 sizeof(d->thread.user_ctxt));
436 /* Clear IOPL for unprivileged domains. */
437 if (!IS_PRIV(d->domain))
438 d->thread.user_ctxt.eflags &= 0xffffcfff;
440 /*
441 * This is sufficient! If the descriptor DPL differs from CS RPL then we'll
442 * #GP. If DS, ES, FS, GS are DPL 0 then they'll be cleared automatically.
443 * If SS RPL or DPL differs from CS RPL then we'll #GP.
444 */
445 if (!(c->flags & ECF_VMX_GUEST))
446 if ( ((d->thread.user_ctxt.cs & 3) == 0) ||
447 ((d->thread.user_ctxt.ss & 3) == 0) )
448 return -EINVAL;
450 memcpy(&d->thread.i387,
451 &c->fpu_ctxt,
452 sizeof(d->thread.i387));
454 memcpy(d->thread.traps,
455 &c->trap_ctxt,
456 sizeof(d->thread.traps));
458 #ifdef ARCH_HAS_FAST_TRAP
459 if ( (rc = (int)set_fast_trap(d, c->fast_trap_idx)) != 0 )
460 return rc;
461 #endif
463 d->mm.ldt_base = c->ldt_base;
464 d->mm.ldt_ents = c->ldt_ents;
466 d->thread.guestos_ss = c->guestos_ss;
467 d->thread.guestos_sp = c->guestos_esp;
469 for ( i = 0; i < 8; i++ )
470 (void)set_debugreg(d, i, c->debugreg[i]);
472 d->thread.event_selector = c->event_callback_cs;
473 d->thread.event_address = c->event_callback_eip;
474 d->thread.failsafe_selector = c->failsafe_callback_cs;
475 d->thread.failsafe_address = c->failsafe_callback_eip;
477 phys_basetab = c->pt_base;
478 d->mm.pagetable = mk_pagetable(phys_basetab);
479 if ( !get_page_and_type(&frame_table[phys_basetab>>PAGE_SHIFT], d->domain,
480 PGT_base_page_table) )
481 return -EINVAL;
483 /* Failure to set GDT is harmless. */
484 SET_GDT_ENTRIES(d, DEFAULT_GDT_ENTRIES);
485 SET_GDT_ADDRESS(d, DEFAULT_GDT_ADDRESS);
486 if ( c->gdt_ents != 0 )
487 {
488 if ( (rc = (int)set_gdt(d, c->gdt_frames, c->gdt_ents)) != 0 )
489 {
490 put_page_and_type(&frame_table[phys_basetab>>PAGE_SHIFT]);
491 return rc;
492 }
493 }
495 #ifdef CONFIG_VMX
496 if (c->flags & ECF_VMX_GUEST)
497 return vmx_final_setup_guestos(d, c);
498 #endif
500 return 0;
501 }
503 #if defined(__i386__)
505 void new_thread(struct exec_domain *d,
506 unsigned long start_pc,
507 unsigned long start_stack,
508 unsigned long start_info)
509 {
510 execution_context_t *ec = &d->thread.user_ctxt;
512 /*
513 * Initial register values:
514 * DS,ES,FS,GS = FLAT_RING1_DS
515 * CS:EIP = FLAT_RING1_CS:start_pc
516 * SS:ESP = FLAT_RING1_DS:start_stack
517 * ESI = start_info
518 * [EAX,EBX,ECX,EDX,EDI,EBP are zero]
519 */
520 ec->ds = ec->es = ec->fs = ec->gs = ec->ss = FLAT_RING1_DS;
521 ec->cs = FLAT_RING1_CS;
522 ec->eip = start_pc;
523 ec->esp = start_stack;
524 ec->esi = start_info;
526 __save_flags(ec->eflags);
527 ec->eflags |= X86_EFLAGS_IF;
528 }
531 /*
532 * This special macro can be used to load a debugging register
533 */
534 #define loaddebug(thread,register) \
535 __asm__("movl %0,%%db" #register \
536 : /* no output */ \
537 :"r" (thread->debugreg[register]))
540 void switch_to(struct exec_domain *prev_p, struct exec_domain *next_p)
541 {
542 struct thread_struct *next = &next_p->thread;
543 struct tss_struct *tss = init_tss + smp_processor_id();
544 execution_context_t *stack_ec = get_execution_context();
545 int i;
546 unsigned long vmx_domain = next_p->thread.arch_vmx.flags;
548 __cli();
550 /* Switch guest general-register state. */
551 if ( !is_idle_task(prev_p->domain) )
552 {
553 memcpy(&prev_p->thread.user_ctxt,
554 stack_ec,
555 sizeof(*stack_ec));
556 unlazy_fpu(prev_p);
557 CLEAR_FAST_TRAP(&prev_p->thread);
558 }
560 if ( !is_idle_task(next_p->domain) )
561 {
562 memcpy(stack_ec,
563 &next_p->thread.user_ctxt,
564 sizeof(*stack_ec));
566 /* Maybe switch the debug registers. */
567 if ( unlikely(next->debugreg[7]) )
568 {
569 loaddebug(next, 0);
570 loaddebug(next, 1);
571 loaddebug(next, 2);
572 loaddebug(next, 3);
573 /* no 4 and 5 */
574 loaddebug(next, 6);
575 loaddebug(next, 7);
576 }
578 if (vmx_domain) {
579 /* Switch page tables. */
580 write_ptbase(&next_p->mm);
582 set_current(next_p);
583 /* Switch GDT and LDT. */
584 __asm__ __volatile__ ("lgdt %0" : "=m" (*next_p->mm.gdt));
586 __sti();
587 return;
588 }
590 SET_FAST_TRAP(&next_p->thread);
592 /* Switch the guest OS ring-1 stack. */
593 tss->esp1 = next->guestos_sp;
594 tss->ss1 = next->guestos_ss;
596 /* Switch page tables. */
597 write_ptbase(&next_p->mm);
598 }
600 if ( unlikely(prev_p->thread.io_bitmap != NULL) )
601 {
602 for ( i = 0; i < sizeof(prev_p->thread.io_bitmap_sel) * 8; i++ )
603 if ( !test_bit(i, &prev_p->thread.io_bitmap_sel) )
604 memset(&tss->io_bitmap[i * IOBMP_BYTES_PER_SELBIT],
605 ~0U, IOBMP_BYTES_PER_SELBIT);
606 tss->bitmap = IOBMP_INVALID_OFFSET;
607 }
609 if ( unlikely(next_p->thread.io_bitmap != NULL) )
610 {
611 for ( i = 0; i < sizeof(next_p->thread.io_bitmap_sel) * 8; i++ )
612 if ( !test_bit(i, &next_p->thread.io_bitmap_sel) )
613 memcpy(&tss->io_bitmap[i * IOBMP_BYTES_PER_SELBIT],
614 &next_p->thread.io_bitmap[i * IOBMP_BYTES_PER_SELBIT],
615 IOBMP_BYTES_PER_SELBIT);
616 tss->bitmap = IOBMP_OFFSET;
617 }
619 set_current(next_p);
621 /* Switch GDT and LDT. */
622 __asm__ __volatile__ ("lgdt %0" : "=m" (*next_p->mm.gdt));
623 load_LDT(next_p);
625 __sti();
626 }
629 /* XXX Currently the 'domain' field is ignored! XXX */
630 long do_iopl(domid_t domain, unsigned int new_io_pl)
631 {
632 execution_context_t *ec = get_execution_context();
633 ec->eflags = (ec->eflags & 0xffffcfff) | ((new_io_pl&3) << 12);
634 return 0;
635 }
637 #endif
639 unsigned long hypercall_create_continuation(
640 unsigned int op, unsigned int nr_args, ...)
641 {
642 struct mc_state *mcs = &mc_state[smp_processor_id()];
643 execution_context_t *ec;
644 unsigned long *preg;
645 unsigned int i;
646 va_list args;
648 va_start(args, nr_args);
650 if ( test_bit(_MCSF_in_multicall, &mcs->flags) )
651 {
652 __set_bit(_MCSF_call_preempted, &mcs->flags);
654 for ( i = 0; i < nr_args; i++ )
655 mcs->call.args[i] = va_arg(args, unsigned long);
656 }
657 else
658 {
659 ec = get_execution_context();
660 #if defined(__i386__)
661 ec->eax = op;
662 ec->eip -= 2; /* re-execute 'int 0x82' */
664 for ( i = 0, preg = &ec->ebx; i < nr_args; i++, preg++ )
665 *preg = va_arg(args, unsigned long);
666 #else
667 preg = NULL; /* XXX x86/64 */
668 #endif
669 }
671 va_end(args);
673 return op;
674 }
676 static void relinquish_list(struct domain *d, struct list_head *list)
677 {
678 struct list_head *ent;
679 struct pfn_info *page;
680 unsigned long x, y;
682 /* Use a recursive lock, as we may enter 'free_domheap_page'. */
683 spin_lock_recursive(&d->page_alloc_lock);
685 ent = list->next;
686 while ( ent != list )
687 {
688 page = list_entry(ent, struct pfn_info, list);
690 /* Grab a reference to the page so it won't disappear from under us. */
691 if ( unlikely(!get_page(page, d)) )
692 {
693 /* Couldn't get a reference -- someone is freeing this page. */
694 ent = ent->next;
695 continue;
696 }
698 if ( test_and_clear_bit(_PGT_pinned, &page->u.inuse.type_info) )
699 put_page_and_type(page);
701 if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
702 put_page(page);
704 /*
705 * Forcibly invalidate base page tables at this point to break circular
706 * 'linear page table' references. This is okay because MMU structures
707 * are not shared across domains and this domain is now dead. Thus base
708 * tables are not in use so a non-zero count means circular reference.
709 */
710 y = page->u.inuse.type_info;
711 for ( ; ; )
712 {
713 x = y;
714 if ( likely((x & (PGT_type_mask|PGT_validated)) !=
715 (PGT_base_page_table|PGT_validated)) )
716 break;
718 y = cmpxchg(&page->u.inuse.type_info, x, x & ~PGT_validated);
719 if ( likely(y == x) )
720 {
721 free_page_type(page, PGT_base_page_table);
722 break;
723 }
724 }
726 /* Follow the list chain and /then/ potentially free the page. */
727 ent = ent->next;
728 put_page(page);
729 }
731 spin_unlock_recursive(&d->page_alloc_lock);
732 }
734 #ifdef CONFIG_VMX
735 static void vmx_domain_relinquish_memory(struct exec_domain *ed)
736 {
737 struct domain *d = ed->domain;
739 /*
740 * Free VMCS
741 */
742 ASSERT(ed->thread.arch_vmx.vmcs);
743 free_vmcs(ed->thread.arch_vmx.vmcs);
744 ed->thread.arch_vmx.vmcs = 0;
746 monitor_rm_pagetable(ed);
748 if (ed == d->exec_domain[0]) {
749 int i;
750 unsigned long pfn;
752 for (i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++) {
753 unsigned long l1e;
755 l1e = l1_pgentry_val(d->mm_perdomain_pt[i]);
756 if (l1e & _PAGE_PRESENT) {
757 pfn = l1e >> PAGE_SHIFT;
758 free_domheap_page(&frame_table[pfn]);
759 }
760 }
761 }
763 }
764 #endif
766 void domain_relinquish_memory(struct domain *d)
767 {
768 struct exec_domain *ed;
770 /* Ensure that noone is running over the dead domain's page tables. */
771 synchronise_pagetables(~0UL);
773 /* Exit shadow mode before deconstructing final guest page table. */
774 shadow_mode_disable(d);
776 /* Drop the in-use reference to the page-table base. */
777 for_each_exec_domain ( d, ed )
778 {
779 if ( pagetable_val(ed->mm.pagetable) != 0 )
780 put_page_and_type(&frame_table[pagetable_val(ed->mm.pagetable) >>
781 PAGE_SHIFT]);
782 }
784 #ifdef CONFIG_VMX
785 if ( VMX_DOMAIN(d->exec_domain[0]) )
786 for_each_exec_domain ( d, ed )
787 vmx_domain_relinquish_memory(ed);
788 #endif
790 /*
791 * Relinquish GDT mappings. No need for explicit unmapping of the LDT as
792 * it automatically gets squashed when the guest's mappings go away.
793 */
794 for_each_exec_domain(d, ed)
795 destroy_gdt(ed);
797 /* Relinquish every page of memory. */
798 relinquish_list(d, &d->xenpage_list);
799 relinquish_list(d, &d->page_list);
800 }
803 int construct_dom0(struct domain *p,
804 unsigned long alloc_start,
805 unsigned long alloc_end,
806 char *image_start, unsigned long image_len,
807 char *initrd_start, unsigned long initrd_len,
808 char *cmdline)
809 {
810 char *dst;
811 int i, rc;
812 unsigned long pfn, mfn;
813 unsigned long nr_pages = (alloc_end - alloc_start) >> PAGE_SHIFT;
814 unsigned long nr_pt_pages;
815 unsigned long count;
816 l2_pgentry_t *l2tab, *l2start;
817 l1_pgentry_t *l1tab = NULL, *l1start = NULL;
818 struct pfn_info *page = NULL;
819 start_info_t *si;
820 struct exec_domain *ed = p->exec_domain[0];
822 /*
823 * This fully describes the memory layout of the initial domain. All
824 * *_start address are page-aligned, except v_start (and v_end) which are
825 * superpage-aligned.
826 */
827 struct domain_setup_info dsi;
828 unsigned long vinitrd_start;
829 unsigned long vinitrd_end;
830 unsigned long vphysmap_start;
831 unsigned long vphysmap_end;
832 unsigned long vstartinfo_start;
833 unsigned long vstartinfo_end;
834 unsigned long vstack_start;
835 unsigned long vstack_end;
836 unsigned long vpt_start;
837 unsigned long vpt_end;
838 unsigned long v_end;
840 /* Machine address of next candidate page-table page. */
841 unsigned long mpt_alloc;
843 extern void physdev_init_dom0(struct domain *);
845 /* Sanity! */
846 if ( p->id != 0 )
847 BUG();
848 if ( test_bit(DF_CONSTRUCTED, &p->d_flags) )
849 BUG();
851 memset(&dsi, 0, sizeof(struct domain_setup_info));
853 printk("*** LOADING DOMAIN 0 ***\n");
855 /*
856 * This is all a bit grim. We've moved the modules to the "safe" physical
857 * memory region above MAP_DIRECTMAP_ADDRESS (48MB). Later in this
858 * routine we're going to copy it down into the region that's actually
859 * been allocated to domain 0. This is highly likely to be overlapping, so
860 * we use a forward copy.
861 *
862 * MAP_DIRECTMAP_ADDRESS should be safe. The worst case is a machine with
863 * 4GB and lots of network/disk cards that allocate loads of buffers.
864 * We'll have to revisit this if we ever support PAE (64GB).
865 */
867 rc = parseelfimage(image_start, image_len, &dsi);
868 if ( rc != 0 )
869 return rc;
871 /* Set up domain options */
872 if ( dsi.use_writable_pagetables )
873 vm_assist(p, VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
875 if ( (dsi.v_start & (PAGE_SIZE-1)) != 0 )
876 {
877 printk("Initial guest OS must load to a page boundary.\n");
878 return -EINVAL;
879 }
881 /*
882 * Why do we need this? The number of page-table frames depends on the
883 * size of the bootstrap address space. But the size of the address space
884 * depends on the number of page-table frames (since each one is mapped
885 * read-only). We have a pair of simultaneous equations in two unknowns,
886 * which we solve by exhaustive search.
887 */
888 vinitrd_start = round_pgup(dsi.v_kernend);
889 vinitrd_end = vinitrd_start + initrd_len;
890 vphysmap_start = round_pgup(vinitrd_end);
891 vphysmap_end = vphysmap_start + (nr_pages * sizeof(unsigned long));
892 vpt_start = round_pgup(vphysmap_end);
893 for ( nr_pt_pages = 2; ; nr_pt_pages++ )
894 {
895 vpt_end = vpt_start + (nr_pt_pages * PAGE_SIZE);
896 vstartinfo_start = vpt_end;
897 vstartinfo_end = vstartinfo_start + PAGE_SIZE;
898 vstack_start = vstartinfo_end;
899 vstack_end = vstack_start + PAGE_SIZE;
900 v_end = (vstack_end + (1<<22)-1) & ~((1<<22)-1);
901 if ( (v_end - vstack_end) < (512 << 10) )
902 v_end += 1 << 22; /* Add extra 4MB to get >= 512kB padding. */
903 if ( (((v_end - dsi.v_start + ((1<<L2_PAGETABLE_SHIFT)-1)) >>
904 L2_PAGETABLE_SHIFT) + 1) <= nr_pt_pages )
905 break;
906 }
908 printk("PHYSICAL MEMORY ARRANGEMENT:\n"
909 " Kernel image: %p->%p\n"
910 " Initrd image: %p->%p\n"
911 " Dom0 alloc.: %08lx->%08lx\n",
912 image_start, image_start + image_len,
913 initrd_start, initrd_start + initrd_len,
914 alloc_start, alloc_end);
915 printk("VIRTUAL MEMORY ARRANGEMENT:\n"
916 " Loaded kernel: %08lx->%08lx\n"
917 " Init. ramdisk: %08lx->%08lx\n"
918 " Phys-Mach map: %08lx->%08lx\n"
919 " Page tables: %08lx->%08lx\n"
920 " Start info: %08lx->%08lx\n"
921 " Boot stack: %08lx->%08lx\n"
922 " TOTAL: %08lx->%08lx\n",
923 dsi.v_kernstart, dsi.v_kernend,
924 vinitrd_start, vinitrd_end,
925 vphysmap_start, vphysmap_end,
926 vpt_start, vpt_end,
927 vstartinfo_start, vstartinfo_end,
928 vstack_start, vstack_end,
929 dsi.v_start, v_end);
930 printk(" ENTRY ADDRESS: %08lx\n", dsi.v_kernentry);
932 if ( (v_end - dsi.v_start) > (nr_pages * PAGE_SIZE) )
933 {
934 printk("Initial guest OS requires too much space\n"
935 "(%luMB is greater than %luMB limit)\n",
936 (v_end-dsi.v_start)>>20, (nr_pages<<PAGE_SHIFT)>>20);
937 return -ENOMEM;
938 }
940 /*
941 * Protect the lowest 1GB of memory. We use a temporary mapping there
942 * from which we copy the kernel and ramdisk images.
943 */
944 if ( dsi.v_start < (1<<30) )
945 {
946 printk("Initial loading isn't allowed to lowest 1GB of memory.\n");
947 return -EINVAL;
948 }
950 /* Paranoia: scrub DOM0's memory allocation. */
951 printk("Scrubbing DOM0 RAM: ");
952 dst = (char *)alloc_start;
953 while ( dst < (char *)alloc_end )
954 {
955 #define SCRUB_BYTES (100 * 1024 * 1024) /* 100MB */
956 printk(".");
957 touch_nmi_watchdog();
958 if ( ((char *)alloc_end - dst) > SCRUB_BYTES )
959 {
960 memset(dst, 0, SCRUB_BYTES);
961 dst += SCRUB_BYTES;
962 }
963 else
964 {
965 memset(dst, 0, (char *)alloc_end - dst);
966 break;
967 }
968 }
969 printk("done.\n");
971 /* Construct a frame-allocation list for the initial domain. */
972 for ( mfn = (alloc_start>>PAGE_SHIFT);
973 mfn < (alloc_end>>PAGE_SHIFT);
974 mfn++ )
975 {
976 page = &frame_table[mfn];
977 page->u.inuse.domain = p;
978 page->u.inuse.type_info = 0;
979 page->count_info = PGC_allocated | 1;
980 list_add_tail(&page->list, &p->page_list);
981 p->tot_pages++; p->max_pages++;
982 }
984 mpt_alloc = (vpt_start - dsi.v_start) + alloc_start;
986 SET_GDT_ENTRIES(ed, DEFAULT_GDT_ENTRIES);
987 SET_GDT_ADDRESS(ed, DEFAULT_GDT_ADDRESS);
989 /*
990 * We're basically forcing default RPLs to 1, so that our "what privilege
991 * level are we returning to?" logic works.
992 */
993 ed->thread.failsafe_selector = FLAT_GUESTOS_CS;
994 ed->thread.event_selector = FLAT_GUESTOS_CS;
995 ed->thread.guestos_ss = FLAT_GUESTOS_DS;
996 for ( i = 0; i < 256; i++ )
997 ed->thread.traps[i].cs = FLAT_GUESTOS_CS;
999 /* WARNING: The new domain must have its 'processor' field filled in! */
1000 l2start = l2tab = (l2_pgentry_t *)mpt_alloc; mpt_alloc += PAGE_SIZE;
1001 memcpy(l2tab, &idle_pg_table[0], PAGE_SIZE);
1002 l2tab[LINEAR_PT_VIRT_START >> L2_PAGETABLE_SHIFT] =
1003 mk_l2_pgentry((unsigned long)l2start | __PAGE_HYPERVISOR);
1004 l2tab[PERDOMAIN_VIRT_START >> L2_PAGETABLE_SHIFT] =
1005 mk_l2_pgentry(__pa(p->mm_perdomain_pt) | __PAGE_HYPERVISOR);
1006 ed->mm.pagetable = mk_pagetable((unsigned long)l2start);
1008 l2tab += l2_table_offset(dsi.v_start);
1009 mfn = alloc_start >> PAGE_SHIFT;
1010 for ( count = 0; count < ((v_end-dsi.v_start)>>PAGE_SHIFT); count++ )
1012 if ( !((unsigned long)l1tab & (PAGE_SIZE-1)) )
1014 l1start = l1tab = (l1_pgentry_t *)mpt_alloc;
1015 mpt_alloc += PAGE_SIZE;
1016 *l2tab++ = mk_l2_pgentry((unsigned long)l1start | L2_PROT);
1017 clear_page(l1tab);
1018 if ( count == 0 )
1019 l1tab += l1_table_offset(dsi.v_start);
1021 *l1tab++ = mk_l1_pgentry((mfn << PAGE_SHIFT) | L1_PROT);
1023 page = &frame_table[mfn];
1024 if ( !get_page_and_type(page, p, PGT_writable_page) )
1025 BUG();
1027 mfn++;
1030 /* Pages that are part of page tables must be read only. */
1031 l2tab = l2start + l2_table_offset(vpt_start);
1032 l1start = l1tab = (l1_pgentry_t *)l2_pgentry_to_phys(*l2tab);
1033 l1tab += l1_table_offset(vpt_start);
1034 l2tab++;
1035 for ( count = 0; count < nr_pt_pages; count++ )
1037 *l1tab = mk_l1_pgentry(l1_pgentry_val(*l1tab) & ~_PAGE_RW);
1038 page = &frame_table[l1_pgentry_to_pagenr(*l1tab)];
1039 if ( count == 0 )
1041 page->u.inuse.type_info &= ~PGT_type_mask;
1042 page->u.inuse.type_info |= PGT_l2_page_table;
1044 /*
1045 * No longer writable: decrement the type_count.
1046 * Installed as CR3: increment both the ref_count and type_count.
1047 * Net: just increment the ref_count.
1048 */
1049 get_page(page, p); /* an extra ref because of readable mapping */
1051 /* Get another ref to L2 page so that it can be pinned. */
1052 if ( !get_page_and_type(page, p, PGT_l2_page_table) )
1053 BUG();
1054 set_bit(_PGT_pinned, &page->u.inuse.type_info);
1056 else
1058 page->u.inuse.type_info &= ~PGT_type_mask;
1059 page->u.inuse.type_info |= PGT_l1_page_table;
1060 page->u.inuse.type_info |=
1061 ((dsi.v_start>>L2_PAGETABLE_SHIFT)+(count-1))<<PGT_va_shift;
1063 /*
1064 * No longer writable: decrement the type_count.
1065 * This is an L1 page, installed in a validated L2 page:
1066 * increment both the ref_count and type_count.
1067 * Net: just increment the ref_count.
1068 */
1069 get_page(page, p); /* an extra ref because of readable mapping */
1071 l1tab++;
1072 if( !((unsigned long)l1tab & (PAGE_SIZE - 1)) )
1073 l1start = l1tab = (l1_pgentry_t *)l2_pgentry_to_phys(*l2tab);
1076 /* Set up shared-info area. */
1077 update_dom_time(p);
1078 p->shared_info->domain_time = 0;
1079 /* Mask all upcalls... */
1080 for ( i = 0; i < MAX_VIRT_CPUS; i++ )
1081 p->shared_info->vcpu_data[i].evtchn_upcall_mask = 1;
1082 p->shared_info->n_vcpu = smp_num_cpus;
1084 /* Install the new page tables. */
1085 __cli();
1086 write_ptbase(&ed->mm);
1088 /* Copy the OS image. */
1089 (void)loadelfimage(image_start);
1091 /* Copy the initial ramdisk. */
1092 if ( initrd_len != 0 )
1093 memcpy((void *)vinitrd_start, initrd_start, initrd_len);
1095 /* Set up start info area. */
1096 si = (start_info_t *)vstartinfo_start;
1097 memset(si, 0, PAGE_SIZE);
1098 si->nr_pages = p->tot_pages;
1099 si->shared_info = virt_to_phys(p->shared_info);
1100 si->flags = SIF_PRIVILEGED | SIF_INITDOMAIN;
1101 si->pt_base = vpt_start;
1102 si->nr_pt_frames = nr_pt_pages;
1103 si->mfn_list = vphysmap_start;
1105 /* Write the phys->machine and machine->phys table entries. */
1106 for ( pfn = 0; pfn < p->tot_pages; pfn++ )
1108 mfn = pfn + (alloc_start>>PAGE_SHIFT);
1109 #ifndef NDEBUG
1110 #define REVERSE_START ((v_end - dsi.v_start) >> PAGE_SHIFT)
1111 if ( pfn > REVERSE_START )
1112 mfn = (alloc_end>>PAGE_SHIFT) - (pfn - REVERSE_START);
1113 #endif
1114 ((unsigned long *)vphysmap_start)[pfn] = mfn;
1115 machine_to_phys_mapping[mfn] = pfn;
1118 if ( initrd_len != 0 )
1120 si->mod_start = vinitrd_start;
1121 si->mod_len = initrd_len;
1122 printk("Initrd len 0x%lx, start at 0x%08lx\n",
1123 si->mod_len, si->mod_start);
1126 dst = si->cmd_line;
1127 if ( cmdline != NULL )
1129 for ( i = 0; i < 255; i++ )
1131 if ( cmdline[i] == '\0' )
1132 break;
1133 *dst++ = cmdline[i];
1136 *dst = '\0';
1138 /* Reinstate the caller's page tables. */
1139 write_ptbase(&current->mm);
1140 __sti();
1142 /* Destroy low mappings - they were only for our convenience. */
1143 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
1144 if ( l2_pgentry_val(l2start[i]) & _PAGE_PSE )
1145 l2start[i] = mk_l2_pgentry(0);
1146 zap_low_mappings(); /* Do the same for the idle page tables. */
1148 /* DOM0 gets access to everything. */
1149 physdev_init_dom0(p);
1151 set_bit(DF_CONSTRUCTED, &p->d_flags);
1153 new_thread(ed, dsi.v_kernentry, vstack_end, vstartinfo_start);
1155 #if 0 /* XXXXX DO NOT CHECK IN ENABLED !!! (but useful for testing so leave) */
1156 shadow_lock(&p->mm);
1157 shadow_mode_enable(p, SHM_test);
1158 shadow_unlock(&p->mm);
1159 #endif
1161 return 0;