debuggers.hg

view xen/arch/x86/domain.c @ 3700:61899d6ae2eb

bitkeeper revision 1.1159.244.1 (4204932bmZgxudypIonkb5Mm9GmAtA)

Create arch_do_boot_vcpu().
Signed-off-by: keir.fraser@cl.cam.ac.uk
author kaf24@scramble.cl.cam.ac.uk
date Sat Feb 05 09:34:35 2005 +0000 (2005-02-05)
parents 5c112b235281
children 523e995bcc57
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 static void default_idle(void)
47 {
48 __cli();
49 if ( !softirq_pending(smp_processor_id()) )
50 safe_halt();
51 else
52 __sti();
53 }
55 static __attribute_used__ void idle_loop(void)
56 {
57 int cpu = smp_processor_id();
58 for ( ; ; )
59 {
60 irq_stat[cpu].idle_timestamp = jiffies;
61 while ( !softirq_pending(cpu) )
62 default_idle();
63 do_softirq();
64 }
65 }
67 void startup_cpu_idle_loop(void)
68 {
69 /* Just some sanity to ensure that the scheduler is set up okay. */
70 ASSERT(current->domain->id == IDLE_DOMAIN_ID);
71 domain_unpause_by_systemcontroller(current->domain);
72 __enter_scheduler();
74 /*
75 * Declares CPU setup done to the boot processor.
76 * Therefore memory barrier to ensure state is visible.
77 */
78 smp_mb();
79 init_idle();
81 idle_loop();
82 }
84 static long no_idt[2];
85 static int reboot_mode;
86 int reboot_thru_bios = 0;
88 #ifdef CONFIG_SMP
89 int reboot_smp = 0;
90 static int reboot_cpu = -1;
91 /* shamelessly grabbed from lib/vsprintf.c for readability */
92 #define is_digit(c) ((c) >= '0' && (c) <= '9')
93 #endif
96 static inline void kb_wait(void)
97 {
98 int i;
100 for (i=0; i<0x10000; i++)
101 if ((inb_p(0x64) & 0x02) == 0)
102 break;
103 }
106 void machine_restart(char * __unused)
107 {
108 #ifdef CONFIG_SMP
109 int cpuid;
110 #endif
112 if ( opt_noreboot )
113 {
114 printk("Reboot disabled on cmdline: require manual reset\n");
115 for ( ; ; ) __asm__ __volatile__ ("hlt");
116 }
118 #ifdef CONFIG_SMP
119 cpuid = GET_APIC_ID(apic_read(APIC_ID));
121 /* KAF: Need interrupts enabled for safe IPI. */
122 __sti();
124 if (reboot_smp) {
126 /* check to see if reboot_cpu is valid
127 if its not, default to the BSP */
128 if ((reboot_cpu == -1) ||
129 (reboot_cpu > (NR_CPUS -1)) ||
130 !(phys_cpu_present_map & (1<<cpuid)))
131 reboot_cpu = boot_cpu_physical_apicid;
133 reboot_smp = 0; /* use this as a flag to only go through this once*/
134 /* re-run this function on the other CPUs
135 it will fall though this section since we have
136 cleared reboot_smp, and do the reboot if it is the
137 correct CPU, otherwise it halts. */
138 if (reboot_cpu != cpuid)
139 smp_call_function((void *)machine_restart , NULL, 1, 0);
140 }
142 /* if reboot_cpu is still -1, then we want a tradional reboot,
143 and if we are not running on the reboot_cpu,, halt */
144 if ((reboot_cpu != -1) && (cpuid != reboot_cpu)) {
145 for (;;)
146 __asm__ __volatile__ ("hlt");
147 }
148 /*
149 * Stop all CPUs and turn off local APICs and the IO-APIC, so
150 * other OSs see a clean IRQ state.
151 */
152 smp_send_stop();
153 disable_IO_APIC();
154 #endif
155 #ifdef CONFIG_VMX
156 stop_vmx();
157 #endif
159 if(!reboot_thru_bios) {
160 /* rebooting needs to touch the page at absolute addr 0 */
161 *((unsigned short *)__va(0x472)) = reboot_mode;
162 for (;;) {
163 int i;
164 for (i=0; i<100; i++) {
165 kb_wait();
166 udelay(50);
167 outb(0xfe,0x64); /* pulse reset low */
168 udelay(50);
169 }
170 /* That didn't work - force a triple fault.. */
171 __asm__ __volatile__("lidt %0": "=m" (no_idt));
172 __asm__ __volatile__("int3");
173 }
174 }
176 panic("Need to reinclude BIOS reboot code\n");
177 }
180 void __attribute__((noreturn)) __machine_halt(void *unused)
181 {
182 for ( ; ; )
183 __asm__ __volatile__ ( "cli; hlt" );
184 }
186 void machine_halt(void)
187 {
188 smp_call_function(__machine_halt, NULL, 1, 1);
189 __machine_halt(NULL);
190 }
192 void dump_pageframe_info(struct domain *d)
193 {
194 struct pfn_info *page;
196 if ( d->tot_pages < 10 )
197 {
198 list_for_each_entry ( page, &d->page_list, list )
199 {
200 printk("Page %08x: caf=%08x, taf=%08x\n",
201 page_to_phys(page), page->count_info,
202 page->u.inuse.type_info);
203 }
204 }
206 page = virt_to_page(d->shared_info);
207 printk("Shared_info@%08x: caf=%08x, taf=%08x\n",
208 page_to_phys(page), page->count_info,
209 page->u.inuse.type_info);
210 }
212 struct domain *arch_alloc_domain_struct(void)
213 {
214 return xmalloc(struct domain);
215 }
217 void arch_free_domain_struct(struct domain *d)
218 {
219 xfree(d);
220 }
222 struct exec_domain *arch_alloc_exec_domain_struct(void)
223 {
224 return xmalloc(struct exec_domain);
225 }
227 void arch_free_exec_domain_struct(struct exec_domain *ed)
228 {
229 xfree(ed);
230 }
232 void free_perdomain_pt(struct domain *d)
233 {
234 free_xenheap_page((unsigned long)d->mm_perdomain_pt);
235 }
237 static void continue_idle_task(struct exec_domain *ed)
238 {
239 reset_stack_and_jump(idle_loop);
240 }
242 static void continue_nonidle_task(struct exec_domain *ed)
243 {
244 reset_stack_and_jump(ret_from_intr);
245 }
247 void arch_do_createdomain(struct exec_domain *ed)
248 {
249 struct domain *d = ed->domain;
251 SET_DEFAULT_FAST_TRAP(&ed->thread);
253 if ( d->id == IDLE_DOMAIN_ID )
254 {
255 ed->thread.schedule_tail = continue_idle_task;
256 }
257 else
258 {
259 ed->thread.schedule_tail = continue_nonidle_task;
261 d->shared_info = (void *)alloc_xenheap_page();
262 memset(d->shared_info, 0, PAGE_SIZE);
263 ed->vcpu_info = &d->shared_info->vcpu_data[ed->eid];
264 SHARE_PFN_WITH_DOMAIN(virt_to_page(d->shared_info), d);
265 machine_to_phys_mapping[virt_to_phys(d->shared_info) >>
266 PAGE_SHIFT] = INVALID_P2M_ENTRY;
268 d->mm_perdomain_pt = (l1_pgentry_t *)alloc_xenheap_page();
269 memset(d->mm_perdomain_pt, 0, PAGE_SIZE);
270 machine_to_phys_mapping[virt_to_phys(d->mm_perdomain_pt) >>
271 PAGE_SHIFT] = INVALID_P2M_ENTRY;
272 ed->mm.perdomain_ptes = d->mm_perdomain_pt;
273 }
274 }
276 void arch_do_boot_vcpu(struct exec_domain *ed)
277 {
278 struct domain *d = ed->domain;
279 ed->thread.schedule_tail = d->exec_domain[0]->thread.schedule_tail;
280 ed->mm.perdomain_ptes = d->mm_perdomain_pt + (ed->eid << PDPT_VCPU_SHIFT);
281 }
283 #ifdef CONFIG_VMX
284 void arch_vmx_do_resume(struct exec_domain *ed)
285 {
286 u64 vmcs_phys_ptr = (u64) virt_to_phys(ed->thread.arch_vmx.vmcs);
288 load_vmcs(&ed->thread.arch_vmx, vmcs_phys_ptr);
289 vmx_do_resume(ed);
290 reset_stack_and_jump(vmx_asm_do_resume);
291 }
293 void arch_vmx_do_launch(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_launch(ed);
299 reset_stack_and_jump(vmx_asm_do_launch);
300 }
302 static void monitor_mk_pagetable(struct exec_domain *ed)
303 {
304 unsigned long mpfn;
305 l2_pgentry_t *mpl2e;
306 struct pfn_info *mpfn_info;
307 struct mm_struct *m = &ed->mm;
308 struct domain *d = ed->domain;
310 mpfn_info = alloc_domheap_page(NULL);
311 ASSERT( mpfn_info );
313 mpfn = (unsigned long) (mpfn_info - frame_table);
314 mpl2e = (l2_pgentry_t *) map_domain_mem(mpfn << L1_PAGETABLE_SHIFT);
315 memset(mpl2e, 0, PAGE_SIZE);
317 memcpy(&mpl2e[DOMAIN_ENTRIES_PER_L2_PAGETABLE],
318 &idle_pg_table[DOMAIN_ENTRIES_PER_L2_PAGETABLE],
319 HYPERVISOR_ENTRIES_PER_L2_PAGETABLE * sizeof(l2_pgentry_t));
321 m->monitor_table = mk_pagetable(mpfn << L1_PAGETABLE_SHIFT);
322 m->shadow_mode = SHM_full_32;
324 mpl2e[PERDOMAIN_VIRT_START >> L2_PAGETABLE_SHIFT] =
325 mk_l2_pgentry((__pa(d->mm_perdomain_pt) & PAGE_MASK)
326 | __PAGE_HYPERVISOR);
328 unmap_domain_mem(mpl2e);
329 }
331 /*
332 * Free the pages for monitor_table and guest_pl2e_cache
333 */
334 static void monitor_rm_pagetable(struct exec_domain *ed)
335 {
336 struct mm_struct *m = &ed->mm;
337 l2_pgentry_t *mpl2e;
338 unsigned long mpfn;
340 ASSERT( pagetable_val(m->monitor_table) );
342 mpl2e = (l2_pgentry_t *) map_domain_mem(pagetable_val(m->monitor_table));
343 /*
344 * First get the pfn for guest_pl2e_cache by looking at monitor_table
345 */
346 mpfn = l2_pgentry_val(mpl2e[LINEAR_PT_VIRT_START >> L2_PAGETABLE_SHIFT])
347 >> PAGE_SHIFT;
349 free_domheap_page(&frame_table[mpfn]);
350 unmap_domain_mem(mpl2e);
352 /*
353 * Then free monitor_table.
354 */
355 mpfn = (pagetable_val(m->monitor_table)) >> PAGE_SHIFT;
356 free_domheap_page(&frame_table[mpfn]);
358 m->monitor_table = mk_pagetable(0);
359 }
361 static int vmx_final_setup_guestos(struct exec_domain *ed,
362 full_execution_context_t *full_context)
363 {
364 int error;
365 execution_context_t *context;
366 struct vmcs_struct *vmcs;
368 context = &full_context->cpu_ctxt;
370 /*
371 * Create a new VMCS
372 */
373 if (!(vmcs = alloc_vmcs())) {
374 printk("Failed to create a new VMCS\n");
375 return -ENOMEM;
376 }
378 memset(&ed->thread.arch_vmx, 0, sizeof (struct arch_vmx_struct));
380 ed->thread.arch_vmx.vmcs = vmcs;
381 error = construct_vmcs(&ed->thread.arch_vmx, context, full_context, VMCS_USE_HOST_ENV);
382 if (error < 0) {
383 printk("Failed to construct a new VMCS\n");
384 goto out;
385 }
387 monitor_mk_pagetable(ed);
388 ed->thread.schedule_tail = arch_vmx_do_launch;
389 clear_bit(VMX_CPU_STATE_PG_ENABLED, &ed->thread.arch_vmx.cpu_state);
391 #if defined (__i386)
392 ed->thread.arch_vmx.vmx_platform.real_mode_data =
393 (unsigned long *) context->esi;
394 #endif
396 if (ed == ed->domain->exec_domain[0]) {
397 /*
398 * Required to do this once per domain
399 */
400 memset(&ed->domain->shared_info->evtchn_mask[0], 0xff,
401 sizeof(ed->domain->shared_info->evtchn_mask));
402 clear_bit(IOPACKET_PORT, &ed->domain->shared_info->evtchn_mask[0]);
403 }
405 return 0;
407 out:
408 free_vmcs(vmcs);
409 ed->thread.arch_vmx.vmcs = 0;
410 return error;
411 }
412 #endif
414 int arch_final_setup_guestos(struct exec_domain *d, full_execution_context_t *c)
415 {
416 unsigned long phys_basetab;
417 int i, rc;
419 clear_bit(EDF_DONEFPUINIT, &d->ed_flags);
420 if ( c->flags & ECF_I387_VALID )
421 set_bit(EDF_DONEFPUINIT, &d->ed_flags);
423 memcpy(&d->thread.user_ctxt,
424 &c->cpu_ctxt,
425 sizeof(d->thread.user_ctxt));
427 /* Clear IOPL for unprivileged domains. */
428 if (!IS_PRIV(d->domain))
429 d->thread.user_ctxt.eflags &= 0xffffcfff;
431 /*
432 * This is sufficient! If the descriptor DPL differs from CS RPL then we'll
433 * #GP. If DS, ES, FS, GS are DPL 0 then they'll be cleared automatically.
434 * If SS RPL or DPL differs from CS RPL then we'll #GP.
435 */
436 if (!(c->flags & ECF_VMX_GUEST))
437 if ( ((d->thread.user_ctxt.cs & 3) == 0) ||
438 ((d->thread.user_ctxt.ss & 3) == 0) )
439 return -EINVAL;
441 memcpy(&d->thread.i387,
442 &c->fpu_ctxt,
443 sizeof(d->thread.i387));
445 memcpy(d->thread.traps,
446 &c->trap_ctxt,
447 sizeof(d->thread.traps));
449 if ( (rc = (int)set_fast_trap(d, c->fast_trap_idx)) != 0 )
450 return rc;
452 d->mm.ldt_base = c->ldt_base;
453 d->mm.ldt_ents = c->ldt_ents;
455 d->thread.guestos_ss = c->guestos_ss;
456 d->thread.guestos_sp = c->guestos_esp;
458 for ( i = 0; i < 8; i++ )
459 (void)set_debugreg(d, i, c->debugreg[i]);
461 d->thread.event_selector = c->event_callback_cs;
462 d->thread.event_address = c->event_callback_eip;
463 d->thread.failsafe_selector = c->failsafe_callback_cs;
464 d->thread.failsafe_address = c->failsafe_callback_eip;
466 phys_basetab = c->pt_base;
467 d->mm.pagetable = mk_pagetable(phys_basetab);
468 if ( !get_page_and_type(&frame_table[phys_basetab>>PAGE_SHIFT], d->domain,
469 PGT_base_page_table) )
470 return -EINVAL;
472 /* Failure to set GDT is harmless. */
473 SET_GDT_ENTRIES(d, DEFAULT_GDT_ENTRIES);
474 SET_GDT_ADDRESS(d, DEFAULT_GDT_ADDRESS);
475 if ( c->gdt_ents != 0 )
476 {
477 if ( (rc = (int)set_gdt(d, c->gdt_frames, c->gdt_ents)) != 0 )
478 {
479 put_page_and_type(&frame_table[phys_basetab>>PAGE_SHIFT]);
480 return rc;
481 }
482 }
484 #ifdef CONFIG_VMX
485 if (c->flags & ECF_VMX_GUEST)
486 return vmx_final_setup_guestos(d, c);
487 #endif
489 return 0;
490 }
492 void new_thread(struct exec_domain *d,
493 unsigned long start_pc,
494 unsigned long start_stack,
495 unsigned long start_info)
496 {
497 execution_context_t *ec = &d->thread.user_ctxt;
499 /*
500 * Initial register values:
501 * DS,ES,FS,GS = FLAT_GUESTOS_DS
502 * CS:EIP = FLAT_GUESTOS_CS:start_pc
503 * SS:ESP = FLAT_GUESTOS_SS:start_stack
504 * ESI = start_info
505 * [EAX,EBX,ECX,EDX,EDI,EBP are zero]
506 */
507 ec->ds = ec->es = ec->fs = ec->gs = FLAT_GUESTOS_DS;
508 ec->ss = FLAT_GUESTOS_SS;
509 ec->cs = FLAT_GUESTOS_CS;
510 ec->eip = start_pc;
511 ec->esp = start_stack;
512 ec->esi = start_info;
514 __save_flags(ec->eflags);
515 ec->eflags |= X86_EFLAGS_IF;
516 }
519 /*
520 * This special macro can be used to load a debugging register
521 */
522 #define loaddebug(thread,register) \
523 __asm__("mov %0,%%db" #register \
524 : /* no output */ \
525 :"r" (thread->debugreg[register]))
527 void switch_to(struct exec_domain *prev_p, struct exec_domain *next_p)
528 {
529 struct thread_struct *next = &next_p->thread;
530 struct tss_struct *tss = init_tss + smp_processor_id();
531 execution_context_t *stack_ec = get_execution_context();
532 int i;
533 #ifdef CONFIG_VMX
534 unsigned long vmx_domain = next_p->thread.arch_vmx.flags;
535 #endif
537 __cli();
539 /* Switch guest general-register state. */
540 if ( !is_idle_task(prev_p->domain) )
541 {
542 memcpy(&prev_p->thread.user_ctxt,
543 stack_ec,
544 sizeof(*stack_ec));
545 unlazy_fpu(prev_p);
546 CLEAR_FAST_TRAP(&prev_p->thread);
547 }
549 if ( !is_idle_task(next_p->domain) )
550 {
551 memcpy(stack_ec,
552 &next_p->thread.user_ctxt,
553 sizeof(*stack_ec));
555 /* Maybe switch the debug registers. */
556 if ( unlikely(next->debugreg[7]) )
557 {
558 loaddebug(next, 0);
559 loaddebug(next, 1);
560 loaddebug(next, 2);
561 loaddebug(next, 3);
562 /* no 4 and 5 */
563 loaddebug(next, 6);
564 loaddebug(next, 7);
565 }
567 #ifdef CONFIG_VMX
568 if ( vmx_domain )
569 {
570 /* Switch page tables. */
571 write_ptbase(&next_p->mm);
573 set_current(next_p);
574 /* Switch GDT and LDT. */
575 __asm__ __volatile__ ("lgdt %0" : "=m" (*next_p->mm.gdt));
577 __sti();
578 return;
579 }
580 #endif
582 SET_FAST_TRAP(&next_p->thread);
584 #ifdef __i386__
585 /* Switch the guest OS ring-1 stack. */
586 tss->esp1 = next->guestos_sp;
587 tss->ss1 = next->guestos_ss;
588 #endif
590 /* Switch page tables. */
591 write_ptbase(&next_p->mm);
592 }
594 if ( unlikely(prev_p->thread.io_bitmap != NULL) )
595 {
596 for ( i = 0; i < sizeof(prev_p->thread.io_bitmap_sel) * 8; i++ )
597 if ( !test_bit(i, &prev_p->thread.io_bitmap_sel) )
598 memset(&tss->io_bitmap[i * IOBMP_BYTES_PER_SELBIT],
599 ~0U, IOBMP_BYTES_PER_SELBIT);
600 tss->bitmap = IOBMP_INVALID_OFFSET;
601 }
603 if ( unlikely(next_p->thread.io_bitmap != NULL) )
604 {
605 for ( i = 0; i < sizeof(next_p->thread.io_bitmap_sel) * 8; i++ )
606 if ( !test_bit(i, &next_p->thread.io_bitmap_sel) )
607 memcpy(&tss->io_bitmap[i * IOBMP_BYTES_PER_SELBIT],
608 &next_p->thread.io_bitmap[i * IOBMP_BYTES_PER_SELBIT],
609 IOBMP_BYTES_PER_SELBIT);
610 tss->bitmap = IOBMP_OFFSET;
611 }
613 set_current(next_p);
615 /* Switch GDT and LDT. */
616 __asm__ __volatile__ ("lgdt %0" : "=m" (*next_p->mm.gdt));
617 load_LDT(next_p);
619 __sti();
620 }
623 /* XXX Currently the 'domain' field is ignored! XXX */
624 long do_iopl(domid_t domain, unsigned int new_io_pl)
625 {
626 execution_context_t *ec = get_execution_context();
627 ec->eflags = (ec->eflags & 0xffffcfff) | ((new_io_pl&3) << 12);
628 return 0;
629 }
631 unsigned long hypercall_create_continuation(
632 unsigned int op, unsigned int nr_args, ...)
633 {
634 struct mc_state *mcs = &mc_state[smp_processor_id()];
635 execution_context_t *ec;
636 unsigned long *preg;
637 unsigned int i;
638 va_list args;
640 va_start(args, nr_args);
642 if ( test_bit(_MCSF_in_multicall, &mcs->flags) )
643 {
644 __set_bit(_MCSF_call_preempted, &mcs->flags);
646 for ( i = 0; i < nr_args; i++ )
647 mcs->call.args[i] = va_arg(args, unsigned long);
648 }
649 else
650 {
651 ec = get_execution_context();
652 #if defined(__i386__)
653 ec->eax = op;
654 ec->eip -= 2; /* re-execute 'int 0x82' */
656 for ( i = 0, preg = &ec->ebx; i < nr_args; i++, preg++ )
657 *preg = va_arg(args, unsigned long);
658 #else
659 preg = NULL; /* XXX x86/64 */
660 #endif
661 }
663 va_end(args);
665 return op;
666 }
668 static void relinquish_list(struct domain *d, struct list_head *list)
669 {
670 struct list_head *ent;
671 struct pfn_info *page;
672 unsigned long x, y;
674 /* Use a recursive lock, as we may enter 'free_domheap_page'. */
675 spin_lock_recursive(&d->page_alloc_lock);
677 ent = list->next;
678 while ( ent != list )
679 {
680 page = list_entry(ent, struct pfn_info, list);
682 /* Grab a reference to the page so it won't disappear from under us. */
683 if ( unlikely(!get_page(page, d)) )
684 {
685 /* Couldn't get a reference -- someone is freeing this page. */
686 ent = ent->next;
687 continue;
688 }
690 if ( test_and_clear_bit(_PGT_pinned, &page->u.inuse.type_info) )
691 put_page_and_type(page);
693 if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
694 put_page(page);
696 /*
697 * Forcibly invalidate base page tables at this point to break circular
698 * 'linear page table' references. This is okay because MMU structures
699 * are not shared across domains and this domain is now dead. Thus base
700 * tables are not in use so a non-zero count means circular reference.
701 */
702 y = page->u.inuse.type_info;
703 for ( ; ; )
704 {
705 x = y;
706 if ( likely((x & (PGT_type_mask|PGT_validated)) !=
707 (PGT_base_page_table|PGT_validated)) )
708 break;
710 y = cmpxchg(&page->u.inuse.type_info, x, x & ~PGT_validated);
711 if ( likely(y == x) )
712 {
713 free_page_type(page, PGT_base_page_table);
714 break;
715 }
716 }
718 /* Follow the list chain and /then/ potentially free the page. */
719 ent = ent->next;
720 put_page(page);
721 }
723 spin_unlock_recursive(&d->page_alloc_lock);
724 }
726 #ifdef CONFIG_VMX
727 static void vmx_domain_relinquish_memory(struct exec_domain *ed)
728 {
729 struct domain *d = ed->domain;
731 /*
732 * Free VMCS
733 */
734 ASSERT(ed->thread.arch_vmx.vmcs);
735 free_vmcs(ed->thread.arch_vmx.vmcs);
736 ed->thread.arch_vmx.vmcs = 0;
738 monitor_rm_pagetable(ed);
740 if (ed == d->exec_domain[0]) {
741 int i;
742 unsigned long pfn;
744 for (i = 0; i < ENTRIES_PER_L1_PAGETABLE; i++) {
745 unsigned long l1e;
747 l1e = l1_pgentry_val(d->mm_perdomain_pt[i]);
748 if (l1e & _PAGE_PRESENT) {
749 pfn = l1e >> PAGE_SHIFT;
750 free_domheap_page(&frame_table[pfn]);
751 }
752 }
753 }
755 }
756 #endif
758 void domain_relinquish_memory(struct domain *d)
759 {
760 struct exec_domain *ed;
762 /* Ensure that noone is running over the dead domain's page tables. */
763 synchronise_pagetables(~0UL);
765 /* Exit shadow mode before deconstructing final guest page table. */
766 shadow_mode_disable(d);
768 /* Drop the in-use reference to the page-table base. */
769 for_each_exec_domain ( d, ed )
770 {
771 if ( pagetable_val(ed->mm.pagetable) != 0 )
772 put_page_and_type(&frame_table[pagetable_val(ed->mm.pagetable) >>
773 PAGE_SHIFT]);
774 }
776 #ifdef CONFIG_VMX
777 if ( VMX_DOMAIN(d->exec_domain[0]) )
778 for_each_exec_domain ( d, ed )
779 vmx_domain_relinquish_memory(ed);
780 #endif
782 /*
783 * Relinquish GDT mappings. No need for explicit unmapping of the LDT as
784 * it automatically gets squashed when the guest's mappings go away.
785 */
786 for_each_exec_domain(d, ed)
787 destroy_gdt(ed);
789 /* Relinquish every page of memory. */
790 relinquish_list(d, &d->xenpage_list);
791 relinquish_list(d, &d->page_list);
792 }