debuggers.hg

view xen/arch/x86/domain.c @ 3668:d55d523078f7

bitkeeper revision 1.1159.212.77 (4202221693AFbvFZWeMHHIjQfbzTIQ)

More x86_64 prgress. Many more gaps filled in. Next step is DOM0
construction.
Signed-off-by: keir.fraser@cl.cam.ac.uk
author kaf24@scramble.cl.cam.ac.uk
date Thu Feb 03 13:07:34 2005 +0000 (2005-02-03)
parents 060c1ea52343
children 677cb76cff18
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;
265 SET_DEFAULT_FAST_TRAP(&ed->thread);
267 if ( d->id == IDLE_DOMAIN_ID )
268 {
269 ed->thread.schedule_tail = continue_idle_task;
270 }
271 else
272 {
273 ed->thread.schedule_tail = continue_nonidle_task;
275 d->shared_info = (void *)alloc_xenheap_page();
276 memset(d->shared_info, 0, PAGE_SIZE);
277 ed->vcpu_info = &d->shared_info->vcpu_data[ed->eid];
278 SHARE_PFN_WITH_DOMAIN(virt_to_page(d->shared_info), d);
279 machine_to_phys_mapping[virt_to_phys(d->shared_info) >>
280 PAGE_SHIFT] = INVALID_P2M_ENTRY;
282 d->mm_perdomain_pt = (l1_pgentry_t *)alloc_xenheap_page();
283 memset(d->mm_perdomain_pt, 0, PAGE_SIZE);
284 machine_to_phys_mapping[virt_to_phys(d->mm_perdomain_pt) >>
285 PAGE_SHIFT] = INVALID_P2M_ENTRY;
286 ed->mm.perdomain_ptes = d->mm_perdomain_pt;
287 }
288 }
290 #ifdef CONFIG_VMX
291 void arch_vmx_do_resume(struct exec_domain *ed)
292 {
293 u64 vmcs_phys_ptr = (u64) virt_to_phys(ed->thread.arch_vmx.vmcs);
295 load_vmcs(&ed->thread.arch_vmx, vmcs_phys_ptr);
296 vmx_do_resume(ed);
297 reset_stack_and_jump(vmx_asm_do_resume);
298 }
300 void arch_vmx_do_launch(struct exec_domain *ed)
301 {
302 u64 vmcs_phys_ptr = (u64) virt_to_phys(ed->thread.arch_vmx.vmcs);
304 load_vmcs(&ed->thread.arch_vmx, vmcs_phys_ptr);
305 vmx_do_launch(ed);
306 reset_stack_and_jump(vmx_asm_do_launch);
307 }
309 static void monitor_mk_pagetable(struct exec_domain *ed)
310 {
311 unsigned long mpfn;
312 l2_pgentry_t *mpl2e;
313 struct pfn_info *mpfn_info;
314 struct mm_struct *m = &ed->mm;
315 struct domain *d = ed->domain;
317 mpfn_info = alloc_domheap_page(NULL);
318 ASSERT( mpfn_info );
320 mpfn = (unsigned long) (mpfn_info - frame_table);
321 mpl2e = (l2_pgentry_t *) map_domain_mem(mpfn << L1_PAGETABLE_SHIFT);
322 memset(mpl2e, 0, PAGE_SIZE);
324 memcpy(&mpl2e[DOMAIN_ENTRIES_PER_L2_PAGETABLE],
325 &idle_pg_table[DOMAIN_ENTRIES_PER_L2_PAGETABLE],
326 HYPERVISOR_ENTRIES_PER_L2_PAGETABLE * sizeof(l2_pgentry_t));
328 m->monitor_table = mk_pagetable(mpfn << L1_PAGETABLE_SHIFT);
329 m->shadow_mode = SHM_full_32;
331 mpl2e[PERDOMAIN_VIRT_START >> L2_PAGETABLE_SHIFT] =
332 mk_l2_pgentry((__pa(d->mm_perdomain_pt) & PAGE_MASK)
333 | __PAGE_HYPERVISOR);
335 unmap_domain_mem(mpl2e);
336 }
338 /*
339 * Free the pages for monitor_table and guest_pl2e_cache
340 */
341 static void monitor_rm_pagetable(struct exec_domain *ed)
342 {
343 struct mm_struct *m = &ed->mm;
344 l2_pgentry_t *mpl2e;
345 unsigned long mpfn;
347 mpl2e = (l2_pgentry_t *) map_domain_mem(pagetable_val(m->monitor_table));
348 /*
349 * First get the pfn for guest_pl2e_cache by looking at monitor_table
350 */
351 mpfn = l2_pgentry_val(mpl2e[LINEAR_PT_VIRT_START >> L2_PAGETABLE_SHIFT])
352 >> PAGE_SHIFT;
354 free_domheap_page(&frame_table[mpfn]);
355 unmap_domain_mem(mpl2e);
357 /*
358 * Then free monitor_table.
359 */
360 mpfn = (pagetable_val(m->monitor_table)) >> PAGE_SHIFT;
361 free_domheap_page(&frame_table[mpfn]);
363 m->monitor_table = mk_pagetable(0);
364 }
366 static int vmx_final_setup_guestos(struct exec_domain *ed,
367 full_execution_context_t *full_context)
368 {
369 int error;
370 execution_context_t *context;
371 struct vmcs_struct *vmcs;
373 context = &full_context->cpu_ctxt;
375 /*
376 * Create a new VMCS
377 */
378 if (!(vmcs = alloc_vmcs())) {
379 printk("Failed to create a new VMCS\n");
380 return -ENOMEM;
381 }
383 memset(&ed->thread.arch_vmx, 0, sizeof (struct arch_vmx_struct));
385 ed->thread.arch_vmx.vmcs = vmcs;
386 error = construct_vmcs(&ed->thread.arch_vmx, context, full_context, VMCS_USE_HOST_ENV);
387 if (error < 0) {
388 printk("Failed to construct a new VMCS\n");
389 goto out;
390 }
392 monitor_mk_pagetable(ed);
393 ed->thread.schedule_tail = arch_vmx_do_launch;
394 clear_bit(VMX_CPU_STATE_PG_ENABLED, &ed->thread.arch_vmx.cpu_state);
396 #if defined (__i386)
397 ed->thread.arch_vmx.vmx_platform.real_mode_data =
398 (unsigned long *) context->esi;
399 #endif
401 if (ed == ed->domain->exec_domain[0]) {
402 /*
403 * Required to do this once per domain
404 */
405 memset(&ed->domain->shared_info->evtchn_mask[0], 0xff,
406 sizeof(ed->domain->shared_info->evtchn_mask));
407 clear_bit(IOPACKET_PORT, &ed->domain->shared_info->evtchn_mask[0]);
408 }
410 return 0;
412 out:
413 free_vmcs(vmcs);
414 ed->thread.arch_vmx.vmcs = 0;
415 return error;
416 }
417 #endif
419 int arch_final_setup_guestos(struct exec_domain *d, full_execution_context_t *c)
420 {
421 unsigned long phys_basetab;
422 int i, rc;
424 clear_bit(EDF_DONEFPUINIT, &d->ed_flags);
425 if ( c->flags & ECF_I387_VALID )
426 set_bit(EDF_DONEFPUINIT, &d->ed_flags);
428 memcpy(&d->thread.user_ctxt,
429 &c->cpu_ctxt,
430 sizeof(d->thread.user_ctxt));
432 /* Clear IOPL for unprivileged domains. */
433 if (!IS_PRIV(d->domain))
434 d->thread.user_ctxt.eflags &= 0xffffcfff;
436 /*
437 * This is sufficient! If the descriptor DPL differs from CS RPL then we'll
438 * #GP. If DS, ES, FS, GS are DPL 0 then they'll be cleared automatically.
439 * If SS RPL or DPL differs from CS RPL then we'll #GP.
440 */
441 if (!(c->flags & ECF_VMX_GUEST))
442 if ( ((d->thread.user_ctxt.cs & 3) == 0) ||
443 ((d->thread.user_ctxt.ss & 3) == 0) )
444 return -EINVAL;
446 memcpy(&d->thread.i387,
447 &c->fpu_ctxt,
448 sizeof(d->thread.i387));
450 memcpy(d->thread.traps,
451 &c->trap_ctxt,
452 sizeof(d->thread.traps));
454 if ( (rc = (int)set_fast_trap(d, c->fast_trap_idx)) != 0 )
455 return rc;
457 d->mm.ldt_base = c->ldt_base;
458 d->mm.ldt_ents = c->ldt_ents;
460 d->thread.guestos_ss = c->guestos_ss;
461 d->thread.guestos_sp = c->guestos_esp;
463 for ( i = 0; i < 8; i++ )
464 (void)set_debugreg(d, i, c->debugreg[i]);
466 d->thread.event_selector = c->event_callback_cs;
467 d->thread.event_address = c->event_callback_eip;
468 d->thread.failsafe_selector = c->failsafe_callback_cs;
469 d->thread.failsafe_address = c->failsafe_callback_eip;
471 phys_basetab = c->pt_base;
472 d->mm.pagetable = mk_pagetable(phys_basetab);
473 if ( !get_page_and_type(&frame_table[phys_basetab>>PAGE_SHIFT], d->domain,
474 PGT_base_page_table) )
475 return -EINVAL;
477 /* Failure to set GDT is harmless. */
478 SET_GDT_ENTRIES(d, DEFAULT_GDT_ENTRIES);
479 SET_GDT_ADDRESS(d, DEFAULT_GDT_ADDRESS);
480 if ( c->gdt_ents != 0 )
481 {
482 if ( (rc = (int)set_gdt(d, c->gdt_frames, c->gdt_ents)) != 0 )
483 {
484 put_page_and_type(&frame_table[phys_basetab>>PAGE_SHIFT]);
485 return rc;
486 }
487 }
489 #ifdef CONFIG_VMX
490 if (c->flags & ECF_VMX_GUEST)
491 return vmx_final_setup_guestos(d, c);
492 #endif
494 return 0;
495 }
497 void new_thread(struct exec_domain *d,
498 unsigned long start_pc,
499 unsigned long start_stack,
500 unsigned long start_info)
501 {
502 execution_context_t *ec = &d->thread.user_ctxt;
504 /*
505 * Initial register values:
506 * DS,ES,FS,GS = FLAT_RING1_DS
507 * CS:EIP = FLAT_RING1_CS:start_pc
508 * SS:ESP = FLAT_RING1_DS:start_stack
509 * ESI = start_info
510 * [EAX,EBX,ECX,EDX,EDI,EBP are zero]
511 */
512 ec->ds = ec->es = ec->fs = ec->gs = ec->ss = FLAT_GUESTOS_DS;
513 ec->cs = FLAT_GUESTOS_CS;
514 ec->eip = start_pc;
515 ec->esp = start_stack;
516 ec->esi = start_info;
518 __save_flags(ec->eflags);
519 ec->eflags |= X86_EFLAGS_IF;
520 }
523 /*
524 * This special macro can be used to load a debugging register
525 */
526 #define loaddebug(thread,register) \
527 __asm__("mov %0,%%db" #register \
528 : /* no output */ \
529 :"r" (thread->debugreg[register]))
531 void switch_to(struct exec_domain *prev_p, struct exec_domain *next_p)
532 {
533 struct thread_struct *next = &next_p->thread;
534 struct tss_struct *tss = init_tss + smp_processor_id();
535 execution_context_t *stack_ec = get_execution_context();
536 int i;
537 #ifdef CONFIG_VMX
538 unsigned long vmx_domain = next_p->thread.arch_vmx.flags;
539 #endif
541 __cli();
543 /* Switch guest general-register state. */
544 if ( !is_idle_task(prev_p->domain) )
545 {
546 memcpy(&prev_p->thread.user_ctxt,
547 stack_ec,
548 sizeof(*stack_ec));
549 unlazy_fpu(prev_p);
550 CLEAR_FAST_TRAP(&prev_p->thread);
551 }
553 if ( !is_idle_task(next_p->domain) )
554 {
555 memcpy(stack_ec,
556 &next_p->thread.user_ctxt,
557 sizeof(*stack_ec));
559 /* Maybe switch the debug registers. */
560 if ( unlikely(next->debugreg[7]) )
561 {
562 loaddebug(next, 0);
563 loaddebug(next, 1);
564 loaddebug(next, 2);
565 loaddebug(next, 3);
566 /* no 4 and 5 */
567 loaddebug(next, 6);
568 loaddebug(next, 7);
569 }
571 #ifdef CONFIG_VMX
572 if ( vmx_domain )
573 {
574 /* Switch page tables. */
575 write_ptbase(&next_p->mm);
577 set_current(next_p);
578 /* Switch GDT and LDT. */
579 __asm__ __volatile__ ("lgdt %0" : "=m" (*next_p->mm.gdt));
581 __sti();
582 return;
583 }
584 #endif
586 SET_FAST_TRAP(&next_p->thread);
588 #ifdef __i386__
589 /* Switch the guest OS ring-1 stack. */
590 tss->esp1 = next->guestos_sp;
591 tss->ss1 = next->guestos_ss;
592 #endif
594 /* Switch page tables. */
595 write_ptbase(&next_p->mm);
596 }
598 if ( unlikely(prev_p->thread.io_bitmap != NULL) )
599 {
600 for ( i = 0; i < sizeof(prev_p->thread.io_bitmap_sel) * 8; i++ )
601 if ( !test_bit(i, &prev_p->thread.io_bitmap_sel) )
602 memset(&tss->io_bitmap[i * IOBMP_BYTES_PER_SELBIT],
603 ~0U, IOBMP_BYTES_PER_SELBIT);
604 tss->bitmap = IOBMP_INVALID_OFFSET;
605 }
607 if ( unlikely(next_p->thread.io_bitmap != NULL) )
608 {
609 for ( i = 0; i < sizeof(next_p->thread.io_bitmap_sel) * 8; i++ )
610 if ( !test_bit(i, &next_p->thread.io_bitmap_sel) )
611 memcpy(&tss->io_bitmap[i * IOBMP_BYTES_PER_SELBIT],
612 &next_p->thread.io_bitmap[i * IOBMP_BYTES_PER_SELBIT],
613 IOBMP_BYTES_PER_SELBIT);
614 tss->bitmap = IOBMP_OFFSET;
615 }
617 set_current(next_p);
619 /* Switch GDT and LDT. */
620 __asm__ __volatile__ ("lgdt %0" : "=m" (*next_p->mm.gdt));
621 load_LDT(next_p);
623 __sti();
624 }
627 /* XXX Currently the 'domain' field is ignored! XXX */
628 long do_iopl(domid_t domain, unsigned int new_io_pl)
629 {
630 execution_context_t *ec = get_execution_context();
631 ec->eflags = (ec->eflags & 0xffffcfff) | ((new_io_pl&3) << 12);
632 return 0;
633 }
635 unsigned long hypercall_create_continuation(
636 unsigned int op, unsigned int nr_args, ...)
637 {
638 struct mc_state *mcs = &mc_state[smp_processor_id()];
639 execution_context_t *ec;
640 unsigned long *preg;
641 unsigned int i;
642 va_list args;
644 va_start(args, nr_args);
646 if ( test_bit(_MCSF_in_multicall, &mcs->flags) )
647 {
648 __set_bit(_MCSF_call_preempted, &mcs->flags);
650 for ( i = 0; i < nr_args; i++ )
651 mcs->call.args[i] = va_arg(args, unsigned long);
652 }
653 else
654 {
655 ec = get_execution_context();
656 #if defined(__i386__)
657 ec->eax = op;
658 ec->eip -= 2; /* re-execute 'int 0x82' */
660 for ( i = 0, preg = &ec->ebx; i < nr_args; i++, preg++ )
661 *preg = va_arg(args, unsigned long);
662 #else
663 preg = NULL; /* XXX x86/64 */
664 #endif
665 }
667 va_end(args);
669 return op;
670 }
672 static void relinquish_list(struct domain *d, struct list_head *list)
673 {
674 struct list_head *ent;
675 struct pfn_info *page;
676 unsigned long x, y;
678 /* Use a recursive lock, as we may enter 'free_domheap_page'. */
679 spin_lock_recursive(&d->page_alloc_lock);
681 ent = list->next;
682 while ( ent != list )
683 {
684 page = list_entry(ent, struct pfn_info, list);
686 /* Grab a reference to the page so it won't disappear from under us. */
687 if ( unlikely(!get_page(page, d)) )
688 {
689 /* Couldn't get a reference -- someone is freeing this page. */
690 ent = ent->next;
691 continue;
692 }
694 if ( test_and_clear_bit(_PGT_pinned, &page->u.inuse.type_info) )
695 put_page_and_type(page);
697 if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
698 put_page(page);
700 /*
701 * Forcibly invalidate base page tables at this point to break circular
702 * 'linear page table' references. This is okay because MMU structures
703 * are not shared across domains and this domain is now dead. Thus base
704 * tables are not in use so a non-zero count means circular reference.
705 */
706 y = page->u.inuse.type_info;
707 for ( ; ; )
708 {
709 x = y;
710 if ( likely((x & (PGT_type_mask|PGT_validated)) !=
711 (PGT_base_page_table|PGT_validated)) )
712 break;
714 y = cmpxchg(&page->u.inuse.type_info, x, x & ~PGT_validated);
715 if ( likely(y == x) )
716 {
717 free_page_type(page, PGT_base_page_table);
718 break;
719 }
720 }
722 /* Follow the list chain and /then/ potentially free the page. */
723 ent = ent->next;
724 put_page(page);
725 }
727 spin_unlock_recursive(&d->page_alloc_lock);
728 }
730 #ifdef CONFIG_VMX
731 static void vmx_domain_relinquish_memory(struct exec_domain *ed)
732 {
733 struct domain *d = ed->domain;
735 /*
736 * Free VMCS
737 */
738 ASSERT(ed->thread.arch_vmx.vmcs);
739 free_vmcs(ed->thread.arch_vmx.vmcs);
740 ed->thread.arch_vmx.vmcs = 0;
742 monitor_rm_pagetable(ed);
744 if (ed == d->exec_domain[0]) {
745 int i;
746 unsigned long pfn;
748 for (i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++) {
749 unsigned long l1e;
751 l1e = l1_pgentry_val(d->mm_perdomain_pt[i]);
752 if (l1e & _PAGE_PRESENT) {
753 pfn = l1e >> PAGE_SHIFT;
754 free_domheap_page(&frame_table[pfn]);
755 }
756 }
757 }
759 }
760 #endif
762 void domain_relinquish_memory(struct domain *d)
763 {
764 struct exec_domain *ed;
766 /* Ensure that noone is running over the dead domain's page tables. */
767 synchronise_pagetables(~0UL);
769 /* Exit shadow mode before deconstructing final guest page table. */
770 shadow_mode_disable(d);
772 /* Drop the in-use reference to the page-table base. */
773 for_each_exec_domain ( d, ed )
774 {
775 if ( pagetable_val(ed->mm.pagetable) != 0 )
776 put_page_and_type(&frame_table[pagetable_val(ed->mm.pagetable) >>
777 PAGE_SHIFT]);
778 }
780 #ifdef CONFIG_VMX
781 if ( VMX_DOMAIN(d->exec_domain[0]) )
782 for_each_exec_domain ( d, ed )
783 vmx_domain_relinquish_memory(ed);
784 #endif
786 /*
787 * Relinquish GDT mappings. No need for explicit unmapping of the LDT as
788 * it automatically gets squashed when the guest's mappings go away.
789 */
790 for_each_exec_domain(d, ed)
791 destroy_gdt(ed);
793 /* Relinquish every page of memory. */
794 relinquish_list(d, &d->xenpage_list);
795 relinquish_list(d, &d->page_list);
796 }
799 int construct_dom0(struct domain *p,
800 unsigned long alloc_start,
801 unsigned long alloc_end,
802 char *image_start, unsigned long image_len,
803 char *initrd_start, unsigned long initrd_len,
804 char *cmdline)
805 {
806 char *dst;
807 int i, rc;
808 unsigned long pfn, mfn;
809 unsigned long nr_pages = (alloc_end - alloc_start) >> PAGE_SHIFT;
810 unsigned long nr_pt_pages;
811 unsigned long count;
812 l2_pgentry_t *l2tab, *l2start;
813 l1_pgentry_t *l1tab = NULL, *l1start = NULL;
814 struct pfn_info *page = NULL;
815 start_info_t *si;
816 struct exec_domain *ed = p->exec_domain[0];
818 /*
819 * This fully describes the memory layout of the initial domain. All
820 * *_start address are page-aligned, except v_start (and v_end) which are
821 * superpage-aligned.
822 */
823 struct domain_setup_info dsi;
824 unsigned long vinitrd_start;
825 unsigned long vinitrd_end;
826 unsigned long vphysmap_start;
827 unsigned long vphysmap_end;
828 unsigned long vstartinfo_start;
829 unsigned long vstartinfo_end;
830 unsigned long vstack_start;
831 unsigned long vstack_end;
832 unsigned long vpt_start;
833 unsigned long vpt_end;
834 unsigned long v_end;
836 /* Machine address of next candidate page-table page. */
837 unsigned long mpt_alloc;
839 extern void physdev_init_dom0(struct domain *);
841 /* Sanity! */
842 if ( p->id != 0 )
843 BUG();
844 if ( test_bit(DF_CONSTRUCTED, &p->d_flags) )
845 BUG();
847 memset(&dsi, 0, sizeof(struct domain_setup_info));
849 printk("*** LOADING DOMAIN 0 ***\n");
851 /*
852 * This is all a bit grim. We've moved the modules to the "safe" physical
853 * memory region above MAP_DIRECTMAP_ADDRESS (48MB). Later in this
854 * routine we're going to copy it down into the region that's actually
855 * been allocated to domain 0. This is highly likely to be overlapping, so
856 * we use a forward copy.
857 *
858 * MAP_DIRECTMAP_ADDRESS should be safe. The worst case is a machine with
859 * 4GB and lots of network/disk cards that allocate loads of buffers.
860 * We'll have to revisit this if we ever support PAE (64GB).
861 */
863 rc = parseelfimage(image_start, image_len, &dsi);
864 if ( rc != 0 )
865 return rc;
867 /* Set up domain options */
868 if ( dsi.use_writable_pagetables )
869 vm_assist(p, VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
871 if ( (dsi.v_start & (PAGE_SIZE-1)) != 0 )
872 {
873 printk("Initial guest OS must load to a page boundary.\n");
874 return -EINVAL;
875 }
877 /*
878 * Why do we need this? The number of page-table frames depends on the
879 * size of the bootstrap address space. But the size of the address space
880 * depends on the number of page-table frames (since each one is mapped
881 * read-only). We have a pair of simultaneous equations in two unknowns,
882 * which we solve by exhaustive search.
883 */
884 vinitrd_start = round_pgup(dsi.v_kernend);
885 vinitrd_end = vinitrd_start + initrd_len;
886 vphysmap_start = round_pgup(vinitrd_end);
887 vphysmap_end = vphysmap_start + (nr_pages * sizeof(unsigned long));
888 vpt_start = round_pgup(vphysmap_end);
889 for ( nr_pt_pages = 2; ; nr_pt_pages++ )
890 {
891 vpt_end = vpt_start + (nr_pt_pages * PAGE_SIZE);
892 vstartinfo_start = vpt_end;
893 vstartinfo_end = vstartinfo_start + PAGE_SIZE;
894 vstack_start = vstartinfo_end;
895 vstack_end = vstack_start + PAGE_SIZE;
896 v_end = (vstack_end + (1<<22)-1) & ~((1<<22)-1);
897 if ( (v_end - vstack_end) < (512 << 10) )
898 v_end += 1 << 22; /* Add extra 4MB to get >= 512kB padding. */
899 if ( (((v_end - dsi.v_start + ((1<<L2_PAGETABLE_SHIFT)-1)) >>
900 L2_PAGETABLE_SHIFT) + 1) <= nr_pt_pages )
901 break;
902 }
904 printk("PHYSICAL MEMORY ARRANGEMENT:\n"
905 " Kernel image: %p->%p\n"
906 " Initrd image: %p->%p\n"
907 " Dom0 alloc.: %08lx->%08lx\n",
908 image_start, image_start + image_len,
909 initrd_start, initrd_start + initrd_len,
910 alloc_start, alloc_end);
911 printk("VIRTUAL MEMORY ARRANGEMENT:\n"
912 " Loaded kernel: %08lx->%08lx\n"
913 " Init. ramdisk: %08lx->%08lx\n"
914 " Phys-Mach map: %08lx->%08lx\n"
915 " Page tables: %08lx->%08lx\n"
916 " Start info: %08lx->%08lx\n"
917 " Boot stack: %08lx->%08lx\n"
918 " TOTAL: %08lx->%08lx\n",
919 dsi.v_kernstart, dsi.v_kernend,
920 vinitrd_start, vinitrd_end,
921 vphysmap_start, vphysmap_end,
922 vpt_start, vpt_end,
923 vstartinfo_start, vstartinfo_end,
924 vstack_start, vstack_end,
925 dsi.v_start, v_end);
926 printk(" ENTRY ADDRESS: %08lx\n", dsi.v_kernentry);
928 if ( (v_end - dsi.v_start) > (nr_pages * PAGE_SIZE) )
929 {
930 printk("Initial guest OS requires too much space\n"
931 "(%luMB is greater than %luMB limit)\n",
932 (v_end-dsi.v_start)>>20, (nr_pages<<PAGE_SHIFT)>>20);
933 return -ENOMEM;
934 }
936 /*
937 * Protect the lowest 1GB of memory. We use a temporary mapping there
938 * from which we copy the kernel and ramdisk images.
939 */
940 if ( dsi.v_start < (1<<30) )
941 {
942 printk("Initial loading isn't allowed to lowest 1GB of memory.\n");
943 return -EINVAL;
944 }
946 /* Paranoia: scrub DOM0's memory allocation. */
947 printk("Scrubbing DOM0 RAM: ");
948 dst = (char *)alloc_start;
949 while ( dst < (char *)alloc_end )
950 {
951 #define SCRUB_BYTES (100 * 1024 * 1024) /* 100MB */
952 printk(".");
953 touch_nmi_watchdog();
954 if ( ((char *)alloc_end - dst) > SCRUB_BYTES )
955 {
956 memset(dst, 0, SCRUB_BYTES);
957 dst += SCRUB_BYTES;
958 }
959 else
960 {
961 memset(dst, 0, (char *)alloc_end - dst);
962 break;
963 }
964 }
965 printk("done.\n");
967 /* Construct a frame-allocation list for the initial domain. */
968 for ( mfn = (alloc_start>>PAGE_SHIFT);
969 mfn < (alloc_end>>PAGE_SHIFT);
970 mfn++ )
971 {
972 page = &frame_table[mfn];
973 page->u.inuse.domain = p;
974 page->u.inuse.type_info = 0;
975 page->count_info = PGC_allocated | 1;
976 list_add_tail(&page->list, &p->page_list);
977 p->tot_pages++; p->max_pages++;
978 }
980 mpt_alloc = (vpt_start - dsi.v_start) + alloc_start;
982 SET_GDT_ENTRIES(ed, DEFAULT_GDT_ENTRIES);
983 SET_GDT_ADDRESS(ed, DEFAULT_GDT_ADDRESS);
985 /*
986 * We're basically forcing default RPLs to 1, so that our "what privilege
987 * level are we returning to?" logic works.
988 */
989 ed->thread.failsafe_selector = FLAT_GUESTOS_CS;
990 ed->thread.event_selector = FLAT_GUESTOS_CS;
991 ed->thread.guestos_ss = FLAT_GUESTOS_DS;
992 for ( i = 0; i < 256; i++ )
993 ed->thread.traps[i].cs = FLAT_GUESTOS_CS;
995 /* WARNING: The new domain must have its 'processor' field filled in! */
996 l2start = l2tab = (l2_pgentry_t *)mpt_alloc; mpt_alloc += PAGE_SIZE;
997 memcpy(l2tab, &idle_pg_table[0], PAGE_SIZE);
998 l2tab[LINEAR_PT_VIRT_START >> L2_PAGETABLE_SHIFT] =
999 mk_l2_pgentry((unsigned long)l2start | __PAGE_HYPERVISOR);
1000 l2tab[PERDOMAIN_VIRT_START >> L2_PAGETABLE_SHIFT] =
1001 mk_l2_pgentry(__pa(p->mm_perdomain_pt) | __PAGE_HYPERVISOR);
1002 ed->mm.pagetable = mk_pagetable((unsigned long)l2start);
1004 l2tab += l2_table_offset(dsi.v_start);
1005 mfn = alloc_start >> PAGE_SHIFT;
1006 for ( count = 0; count < ((v_end-dsi.v_start)>>PAGE_SHIFT); count++ )
1008 if ( !((unsigned long)l1tab & (PAGE_SIZE-1)) )
1010 l1start = l1tab = (l1_pgentry_t *)mpt_alloc;
1011 mpt_alloc += PAGE_SIZE;
1012 *l2tab++ = mk_l2_pgentry((unsigned long)l1start | L2_PROT);
1013 clear_page(l1tab);
1014 if ( count == 0 )
1015 l1tab += l1_table_offset(dsi.v_start);
1017 *l1tab++ = mk_l1_pgentry((mfn << PAGE_SHIFT) | L1_PROT);
1019 page = &frame_table[mfn];
1020 if ( !get_page_and_type(page, p, PGT_writable_page) )
1021 BUG();
1023 mfn++;
1026 /* Pages that are part of page tables must be read only. */
1027 l2tab = l2start + l2_table_offset(vpt_start);
1028 l1start = l1tab = (l1_pgentry_t *)l2_pgentry_to_phys(*l2tab);
1029 l1tab += l1_table_offset(vpt_start);
1030 l2tab++;
1031 for ( count = 0; count < nr_pt_pages; count++ )
1033 *l1tab = mk_l1_pgentry(l1_pgentry_val(*l1tab) & ~_PAGE_RW);
1034 page = &frame_table[l1_pgentry_to_pagenr(*l1tab)];
1035 if ( count == 0 )
1037 page->u.inuse.type_info &= ~PGT_type_mask;
1038 page->u.inuse.type_info |= PGT_l2_page_table;
1040 /*
1041 * No longer writable: decrement the type_count.
1042 * Installed as CR3: increment both the ref_count and type_count.
1043 * Net: just increment the ref_count.
1044 */
1045 get_page(page, p); /* an extra ref because of readable mapping */
1047 /* Get another ref to L2 page so that it can be pinned. */
1048 if ( !get_page_and_type(page, p, PGT_l2_page_table) )
1049 BUG();
1050 set_bit(_PGT_pinned, &page->u.inuse.type_info);
1052 else
1054 page->u.inuse.type_info &= ~PGT_type_mask;
1055 page->u.inuse.type_info |= PGT_l1_page_table;
1056 page->u.inuse.type_info |=
1057 ((dsi.v_start>>L2_PAGETABLE_SHIFT)+(count-1))<<PGT_va_shift;
1059 /*
1060 * No longer writable: decrement the type_count.
1061 * This is an L1 page, installed in a validated L2 page:
1062 * increment both the ref_count and type_count.
1063 * Net: just increment the ref_count.
1064 */
1065 get_page(page, p); /* an extra ref because of readable mapping */
1067 l1tab++;
1068 if( !((unsigned long)l1tab & (PAGE_SIZE - 1)) )
1069 l1start = l1tab = (l1_pgentry_t *)l2_pgentry_to_phys(*l2tab);
1072 /* Set up shared-info area. */
1073 update_dom_time(p);
1074 p->shared_info->domain_time = 0;
1075 /* Mask all upcalls... */
1076 for ( i = 0; i < MAX_VIRT_CPUS; i++ )
1077 p->shared_info->vcpu_data[i].evtchn_upcall_mask = 1;
1078 p->shared_info->n_vcpu = smp_num_cpus;
1080 /* Install the new page tables. */
1081 __cli();
1082 write_ptbase(&ed->mm);
1084 /* Copy the OS image. */
1085 (void)loadelfimage(image_start);
1087 /* Copy the initial ramdisk. */
1088 if ( initrd_len != 0 )
1089 memcpy((void *)vinitrd_start, initrd_start, initrd_len);
1091 /* Set up start info area. */
1092 si = (start_info_t *)vstartinfo_start;
1093 memset(si, 0, PAGE_SIZE);
1094 si->nr_pages = p->tot_pages;
1095 si->shared_info = virt_to_phys(p->shared_info);
1096 si->flags = SIF_PRIVILEGED | SIF_INITDOMAIN;
1097 si->pt_base = vpt_start;
1098 si->nr_pt_frames = nr_pt_pages;
1099 si->mfn_list = vphysmap_start;
1101 /* Write the phys->machine and machine->phys table entries. */
1102 for ( pfn = 0; pfn < p->tot_pages; pfn++ )
1104 mfn = pfn + (alloc_start>>PAGE_SHIFT);
1105 #ifndef NDEBUG
1106 #define REVERSE_START ((v_end - dsi.v_start) >> PAGE_SHIFT)
1107 if ( pfn > REVERSE_START )
1108 mfn = (alloc_end>>PAGE_SHIFT) - (pfn - REVERSE_START);
1109 #endif
1110 ((unsigned long *)vphysmap_start)[pfn] = mfn;
1111 machine_to_phys_mapping[mfn] = pfn;
1114 if ( initrd_len != 0 )
1116 si->mod_start = vinitrd_start;
1117 si->mod_len = initrd_len;
1118 printk("Initrd len 0x%lx, start at 0x%08lx\n",
1119 si->mod_len, si->mod_start);
1122 dst = si->cmd_line;
1123 if ( cmdline != NULL )
1125 for ( i = 0; i < 255; i++ )
1127 if ( cmdline[i] == '\0' )
1128 break;
1129 *dst++ = cmdline[i];
1132 *dst = '\0';
1134 /* Reinstate the caller's page tables. */
1135 write_ptbase(&current->mm);
1136 __sti();
1138 /* Destroy low mappings - they were only for our convenience. */
1139 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
1140 if ( l2_pgentry_val(l2start[i]) & _PAGE_PSE )
1141 l2start[i] = mk_l2_pgentry(0);
1142 zap_low_mappings(); /* Do the same for the idle page tables. */
1144 /* DOM0 gets access to everything. */
1145 physdev_init_dom0(p);
1147 set_bit(DF_CONSTRUCTED, &p->d_flags);
1149 new_thread(ed, dsi.v_kernentry, vstack_end, vstartinfo_start);
1151 #if 0 /* XXXXX DO NOT CHECK IN ENABLED !!! (but useful for testing so leave) */
1152 shadow_lock(&p->mm);
1153 shadow_mode_enable(p, SHM_test);
1154 shadow_unlock(&p->mm);
1155 #endif
1157 return 0;