debuggers.hg

view xen/arch/x86/domain.c @ 3129:e0351a3744a5

bitkeeper revision 1.1159.187.4 (41a471c8NjyQJy-vepqpb8H7LdzHzA)

Allow preemption of long-running hypercalls for softirq processing.
author kaf24@scramble.cl.cam.ac.uk
date Wed Nov 24 11:34:32 2004 +0000 (2004-11-24)
parents e16455258e29
children 2754a2ed61c3 2fae9947de6f b013a6b30d9e
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/regs.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->id == 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->thread.user_ctxt,
244 &c->cpu_ctxt,
245 sizeof(d->thread.user_ctxt));
247 /*
248 * This is sufficient! If the descriptor DPL differs from CS RPL then we'll
249 * #GP. If DS, ES, FS, GS are DPL 0 then they'll be cleared automatically.
250 * If SS RPL or DPL differs from CS RPL then we'll #GP.
251 */
252 if ( ((d->thread.user_ctxt.cs & 3) == 0) ||
253 ((d->thread.user_ctxt.ss & 3) == 0) )
254 return -EINVAL;
256 memcpy(&d->thread.i387,
257 &c->fpu_ctxt,
258 sizeof(d->thread.i387));
260 memcpy(d->thread.traps,
261 &c->trap_ctxt,
262 sizeof(d->thread.traps));
264 #ifdef ARCH_HAS_FAST_TRAP
265 SET_DEFAULT_FAST_TRAP(&d->thread);
266 if ( (rc = (int)set_fast_trap(d, c->fast_trap_idx)) != 0 )
267 return rc;
268 #endif
270 d->mm.ldt_base = c->ldt_base;
271 d->mm.ldt_ents = c->ldt_ents;
273 d->thread.guestos_ss = c->guestos_ss;
274 d->thread.guestos_sp = c->guestos_esp;
276 for ( i = 0; i < 8; i++ )
277 (void)set_debugreg(d, i, c->debugreg[i]);
279 d->thread.event_selector = c->event_callback_cs;
280 d->thread.event_address = c->event_callback_eip;
281 d->thread.failsafe_selector = c->failsafe_callback_cs;
282 d->thread.failsafe_address = c->failsafe_callback_eip;
284 phys_basetab = c->pt_base;
285 d->mm.pagetable = mk_pagetable(phys_basetab);
286 if ( !get_page_and_type(&frame_table[phys_basetab>>PAGE_SHIFT], d,
287 PGT_base_page_table) )
288 return -EINVAL;
290 /* Failure to set GDT is harmless. */
291 SET_GDT_ENTRIES(d, DEFAULT_GDT_ENTRIES);
292 SET_GDT_ADDRESS(d, DEFAULT_GDT_ADDRESS);
293 if ( c->gdt_ents != 0 )
294 {
295 if ( (rc = (int)set_gdt(d, c->gdt_frames, c->gdt_ents)) != 0 )
296 {
297 put_page_and_type(&frame_table[phys_basetab>>PAGE_SHIFT]);
298 return rc;
299 }
300 }
302 return 0;
303 }
305 #if defined(__i386__)
307 void new_thread(struct domain *d,
308 unsigned long start_pc,
309 unsigned long start_stack,
310 unsigned long start_info)
311 {
312 execution_context_t *ec = &d->thread.user_ctxt;
314 /*
315 * Initial register values:
316 * DS,ES,FS,GS = FLAT_RING1_DS
317 * CS:EIP = FLAT_RING1_CS:start_pc
318 * SS:ESP = FLAT_RING1_DS:start_stack
319 * ESI = start_info
320 * [EAX,EBX,ECX,EDX,EDI,EBP are zero]
321 */
322 ec->ds = ec->es = ec->fs = ec->gs = ec->ss = FLAT_RING1_DS;
323 ec->cs = FLAT_RING1_CS;
324 ec->eip = start_pc;
325 ec->esp = start_stack;
326 ec->esi = start_info;
328 __save_flags(ec->eflags);
329 ec->eflags |= X86_EFLAGS_IF;
331 /* No fast trap at start of day. */
332 SET_DEFAULT_FAST_TRAP(&d->thread);
333 }
336 /*
337 * This special macro can be used to load a debugging register
338 */
339 #define loaddebug(thread,register) \
340 __asm__("movl %0,%%db" #register \
341 : /* no output */ \
342 :"r" (thread->debugreg[register]))
345 void switch_to(struct domain *prev_p, struct domain *next_p)
346 {
347 struct thread_struct *next = &next_p->thread;
348 struct tss_struct *tss = init_tss + smp_processor_id();
349 execution_context_t *stack_ec = get_execution_context();
350 int i;
352 __cli();
354 /* Switch guest general-register state. */
355 if ( !is_idle_task(prev_p) )
356 {
357 memcpy(&prev_p->thread.user_ctxt,
358 stack_ec,
359 sizeof(*stack_ec));
360 unlazy_fpu(prev_p);
361 CLEAR_FAST_TRAP(&prev_p->thread);
362 }
364 if ( !is_idle_task(next_p) )
365 {
366 memcpy(stack_ec,
367 &next_p->thread.user_ctxt,
368 sizeof(*stack_ec));
370 SET_FAST_TRAP(&next_p->thread);
372 /* Switch the guest OS ring-1 stack. */
373 tss->esp1 = next->guestos_sp;
374 tss->ss1 = next->guestos_ss;
376 /* Maybe switch the debug registers. */
377 if ( unlikely(next->debugreg[7]) )
378 {
379 loaddebug(next, 0);
380 loaddebug(next, 1);
381 loaddebug(next, 2);
382 loaddebug(next, 3);
383 /* no 4 and 5 */
384 loaddebug(next, 6);
385 loaddebug(next, 7);
386 }
388 /* Switch page tables. */
389 write_ptbase(&next_p->mm);
390 }
392 if ( unlikely(prev_p->thread.io_bitmap != NULL) )
393 {
394 for ( i = 0; i < sizeof(prev_p->thread.io_bitmap_sel) * 8; i++ )
395 if ( !test_bit(i, &prev_p->thread.io_bitmap_sel) )
396 memset(&tss->io_bitmap[i * IOBMP_BYTES_PER_SELBIT],
397 ~0U, IOBMP_BYTES_PER_SELBIT);
398 tss->bitmap = IOBMP_INVALID_OFFSET;
399 }
401 if ( unlikely(next_p->thread.io_bitmap != NULL) )
402 {
403 for ( i = 0; i < sizeof(next_p->thread.io_bitmap_sel) * 8; i++ )
404 if ( !test_bit(i, &next_p->thread.io_bitmap_sel) )
405 memcpy(&tss->io_bitmap[i * IOBMP_BYTES_PER_SELBIT],
406 &next_p->thread.io_bitmap[i * IOBMP_BYTES_PER_SELBIT],
407 IOBMP_BYTES_PER_SELBIT);
408 tss->bitmap = IOBMP_OFFSET;
409 }
411 set_current(next_p);
413 /* Switch GDT and LDT. */
414 __asm__ __volatile__ ("lgdt %0" : "=m" (*next_p->mm.gdt));
415 load_LDT(next_p);
417 __sti();
418 }
421 /* XXX Currently the 'domain' field is ignored! XXX */
422 long do_iopl(domid_t domain, unsigned int new_io_pl)
423 {
424 execution_context_t *ec = get_execution_context();
425 ec->eflags = (ec->eflags & 0xffffcfff) | ((new_io_pl&3) << 12);
426 return 0;
427 }
429 void hypercall_create_continuation(unsigned int op, unsigned int nr_args, ...)
430 {
431 execution_context_t *ec = get_execution_context();
432 unsigned long *preg = &ec->ebx;
433 unsigned int i;
434 va_list args;
436 ec->eax = op;
437 ec->eip -= 2; /* re-execute 'int 0x82' */
439 va_start(args, nr_args);
440 for ( i = 0; i < nr_args; i++ )
441 *preg++ = va_arg(args, unsigned long);
442 va_end(args);
443 }
445 #endif
448 static void relinquish_list(struct domain *d, struct list_head *list)
449 {
450 struct list_head *ent;
451 struct pfn_info *page;
452 unsigned long x, y;
454 /* Use a recursive lock, as we may enter 'free_domheap_page'. */
455 spin_lock_recursive(&d->page_alloc_lock);
457 ent = list->next;
458 while ( ent != list )
459 {
460 page = list_entry(ent, struct pfn_info, list);
462 /* Grab a reference to the page so it won't disappear from under us. */
463 if ( unlikely(!get_page(page, d)) )
464 {
465 /* Couldn't get a reference -- someone is freeing this page. */
466 ent = ent->next;
467 continue;
468 }
470 if ( test_and_clear_bit(_PGT_pinned, &page->u.inuse.type_info) )
471 put_page_and_type(page);
473 if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
474 put_page(page);
476 /*
477 * Forcibly invalidate base page tables at this point to break circular
478 * 'linear page table' references. This is okay because MMU structures
479 * are not shared across domains and this domain is now dead. Thus base
480 * tables are not in use so a non-zero count means circular reference.
481 */
482 y = page->u.inuse.type_info;
483 for ( ; ; )
484 {
485 x = y;
486 if ( likely((x & (PGT_type_mask|PGT_validated)) !=
487 (PGT_base_page_table|PGT_validated)) )
488 break;
490 y = cmpxchg(&page->u.inuse.type_info, x, x & ~PGT_validated);
491 if ( likely(y == x) )
492 {
493 free_page_type(page, PGT_base_page_table);
494 break;
495 }
496 }
498 /* Follow the list chain and /then/ potentially free the page. */
499 ent = ent->next;
500 put_page(page);
501 }
503 spin_unlock_recursive(&d->page_alloc_lock);
504 }
507 void domain_relinquish_memory(struct domain *d)
508 {
509 audit_domain(d);
511 /* Ensure that noone is running over the dead domain's page tables. */
512 synchronise_pagetables(~0UL);
514 /* Exit shadow mode before deconstructing final guest page table. */
515 shadow_mode_disable(d);
517 /* Drop the in-use reference to the page-table base. */
518 if ( pagetable_val(d->mm.pagetable) != 0 )
519 put_page_and_type(&frame_table[pagetable_val(d->mm.pagetable) >>
520 PAGE_SHIFT]);
522 /*
523 * Relinquish GDT mappings. No need for explicit unmapping of the LDT as
524 * it automatically gets squashed when the guest's mappings go away.
525 */
526 destroy_gdt(d);
528 /* Relinquish every page of memory. */
529 relinquish_list(d, &d->xenpage_list);
530 relinquish_list(d, &d->page_list);
531 }
534 int construct_dom0(struct domain *p,
535 unsigned long alloc_start,
536 unsigned long alloc_end,
537 char *image_start, unsigned long image_len,
538 char *initrd_start, unsigned long initrd_len,
539 char *cmdline)
540 {
541 char *dst;
542 int i, rc;
543 unsigned long pfn, mfn;
544 unsigned long nr_pages = (alloc_end - alloc_start) >> PAGE_SHIFT;
545 unsigned long nr_pt_pages;
546 unsigned long count;
547 l2_pgentry_t *l2tab, *l2start;
548 l1_pgentry_t *l1tab = NULL, *l1start = NULL;
549 struct pfn_info *page = NULL;
550 start_info_t *si;
552 /*
553 * This fully describes the memory layout of the initial domain. All
554 * *_start address are page-aligned, except v_start (and v_end) which are
555 * superpage-aligned.
556 */
557 struct domain_setup_info dsi;
558 unsigned long vinitrd_start;
559 unsigned long vinitrd_end;
560 unsigned long vphysmap_start;
561 unsigned long vphysmap_end;
562 unsigned long vstartinfo_start;
563 unsigned long vstartinfo_end;
564 unsigned long vstack_start;
565 unsigned long vstack_end;
566 unsigned long vpt_start;
567 unsigned long vpt_end;
568 unsigned long v_end;
570 /* Machine address of next candidate page-table page. */
571 unsigned long mpt_alloc;
573 extern void physdev_init_dom0(struct domain *);
575 /* Sanity! */
576 if ( p->id != 0 )
577 BUG();
578 if ( test_bit(DF_CONSTRUCTED, &p->flags) )
579 BUG();
581 memset(&dsi, 0, sizeof(struct domain_setup_info));
583 printk("*** LOADING DOMAIN 0 ***\n");
585 /*
586 * This is all a bit grim. We've moved the modules to the "safe" physical
587 * memory region above MAP_DIRECTMAP_ADDRESS (48MB). Later in this
588 * routine we're going to copy it down into the region that's actually
589 * been allocated to domain 0. This is highly likely to be overlapping, so
590 * we use a forward copy.
591 *
592 * MAP_DIRECTMAP_ADDRESS should be safe. The worst case is a machine with
593 * 4GB and lots of network/disk cards that allocate loads of buffers.
594 * We'll have to revisit this if we ever support PAE (64GB).
595 */
597 rc = parseelfimage(image_start, image_len, &dsi);
598 if ( rc != 0 )
599 return rc;
601 /* Set up domain options */
602 if ( dsi.use_writable_pagetables )
603 vm_assist(p, VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
605 if ( (dsi.v_start & (PAGE_SIZE-1)) != 0 )
606 {
607 printk("Initial guest OS must load to a page boundary.\n");
608 return -EINVAL;
609 }
611 /*
612 * Why do we need this? The number of page-table frames depends on the
613 * size of the bootstrap address space. But the size of the address space
614 * depends on the number of page-table frames (since each one is mapped
615 * read-only). We have a pair of simultaneous equations in two unknowns,
616 * which we solve by exhaustive search.
617 */
618 vinitrd_start = round_pgup(dsi.v_kernend);
619 vinitrd_end = vinitrd_start + initrd_len;
620 vphysmap_start = round_pgup(vinitrd_end);
621 vphysmap_end = vphysmap_start + (nr_pages * sizeof(unsigned long));
622 vpt_start = round_pgup(vphysmap_end);
623 for ( nr_pt_pages = 2; ; nr_pt_pages++ )
624 {
625 vpt_end = vpt_start + (nr_pt_pages * PAGE_SIZE);
626 vstartinfo_start = vpt_end;
627 vstartinfo_end = vstartinfo_start + PAGE_SIZE;
628 vstack_start = vstartinfo_end;
629 vstack_end = vstack_start + PAGE_SIZE;
630 v_end = (vstack_end + (1<<22)-1) & ~((1<<22)-1);
631 if ( (v_end - vstack_end) < (512 << 10) )
632 v_end += 1 << 22; /* Add extra 4MB to get >= 512kB padding. */
633 if ( (((v_end - dsi.v_start + ((1<<L2_PAGETABLE_SHIFT)-1)) >>
634 L2_PAGETABLE_SHIFT) + 1) <= nr_pt_pages )
635 break;
636 }
638 printk("PHYSICAL MEMORY ARRANGEMENT:\n"
639 " Kernel image: %p->%p\n"
640 " Initrd image: %p->%p\n"
641 " Dom0 alloc.: %08lx->%08lx\n",
642 image_start, image_start + image_len,
643 initrd_start, initrd_start + initrd_len,
644 alloc_start, alloc_end);
645 printk("VIRTUAL MEMORY ARRANGEMENT:\n"
646 " Loaded kernel: %08lx->%08lx\n"
647 " Init. ramdisk: %08lx->%08lx\n"
648 " Phys-Mach map: %08lx->%08lx\n"
649 " Page tables: %08lx->%08lx\n"
650 " Start info: %08lx->%08lx\n"
651 " Boot stack: %08lx->%08lx\n"
652 " TOTAL: %08lx->%08lx\n",
653 dsi.v_kernstart, dsi.v_kernend,
654 vinitrd_start, vinitrd_end,
655 vphysmap_start, vphysmap_end,
656 vpt_start, vpt_end,
657 vstartinfo_start, vstartinfo_end,
658 vstack_start, vstack_end,
659 dsi.v_start, v_end);
660 printk(" ENTRY ADDRESS: %08lx\n", dsi.v_kernentry);
662 if ( (v_end - dsi.v_start) > (nr_pages * PAGE_SIZE) )
663 {
664 printk("Initial guest OS requires too much space\n"
665 "(%luMB is greater than %luMB limit)\n",
666 (v_end-dsi.v_start)>>20, (nr_pages<<PAGE_SHIFT)>>20);
667 return -ENOMEM;
668 }
670 /*
671 * Protect the lowest 1GB of memory. We use a temporary mapping there
672 * from which we copy the kernel and ramdisk images.
673 */
674 if ( dsi.v_start < (1<<30) )
675 {
676 printk("Initial loading isn't allowed to lowest 1GB of memory.\n");
677 return -EINVAL;
678 }
680 /* Paranoia: scrub DOM0's memory allocation. */
681 printk("Scrubbing DOM0 RAM: ");
682 dst = (char *)alloc_start;
683 while ( dst < (char *)alloc_end )
684 {
685 #define SCRUB_BYTES (100 * 1024 * 1024) /* 100MB */
686 printk(".");
687 touch_nmi_watchdog();
688 if ( ((char *)alloc_end - dst) > SCRUB_BYTES )
689 {
690 memset(dst, 0, SCRUB_BYTES);
691 dst += SCRUB_BYTES;
692 }
693 else
694 {
695 memset(dst, 0, (char *)alloc_end - dst);
696 break;
697 }
698 }
699 printk("done.\n");
701 /* Construct a frame-allocation list for the initial domain. */
702 for ( mfn = (alloc_start>>PAGE_SHIFT);
703 mfn < (alloc_end>>PAGE_SHIFT);
704 mfn++ )
705 {
706 page = &frame_table[mfn];
707 page->u.inuse.domain = p;
708 page->u.inuse.type_info = 0;
709 page->count_info = PGC_allocated | 1;
710 list_add_tail(&page->list, &p->page_list);
711 p->tot_pages++; p->max_pages++;
712 }
714 mpt_alloc = (vpt_start - dsi.v_start) + alloc_start;
716 SET_GDT_ENTRIES(p, DEFAULT_GDT_ENTRIES);
717 SET_GDT_ADDRESS(p, DEFAULT_GDT_ADDRESS);
719 /*
720 * We're basically forcing default RPLs to 1, so that our "what privilege
721 * level are we returning to?" logic works.
722 */
723 p->thread.failsafe_selector = FLAT_GUESTOS_CS;
724 p->thread.event_selector = FLAT_GUESTOS_CS;
725 p->thread.guestos_ss = FLAT_GUESTOS_DS;
726 for ( i = 0; i < 256; i++ )
727 p->thread.traps[i].cs = FLAT_GUESTOS_CS;
729 /* WARNING: The new domain must have its 'processor' field filled in! */
730 l2start = l2tab = (l2_pgentry_t *)mpt_alloc; mpt_alloc += PAGE_SIZE;
731 memcpy(l2tab, &idle_pg_table[0], PAGE_SIZE);
732 l2tab[LINEAR_PT_VIRT_START >> L2_PAGETABLE_SHIFT] =
733 mk_l2_pgentry((unsigned long)l2start | __PAGE_HYPERVISOR);
734 l2tab[PERDOMAIN_VIRT_START >> L2_PAGETABLE_SHIFT] =
735 mk_l2_pgentry(__pa(p->mm.perdomain_pt) | __PAGE_HYPERVISOR);
736 p->mm.pagetable = mk_pagetable((unsigned long)l2start);
738 l2tab += l2_table_offset(dsi.v_start);
739 mfn = alloc_start >> PAGE_SHIFT;
740 for ( count = 0; count < ((v_end-dsi.v_start)>>PAGE_SHIFT); count++ )
741 {
742 if ( !((unsigned long)l1tab & (PAGE_SIZE-1)) )
743 {
744 l1start = l1tab = (l1_pgentry_t *)mpt_alloc;
745 mpt_alloc += PAGE_SIZE;
746 *l2tab++ = mk_l2_pgentry((unsigned long)l1start | L2_PROT);
747 clear_page(l1tab);
748 if ( count == 0 )
749 l1tab += l1_table_offset(dsi.v_start);
750 }
751 *l1tab++ = mk_l1_pgentry((mfn << PAGE_SHIFT) | L1_PROT);
753 page = &frame_table[mfn];
754 if ( !get_page_and_type(page, p, PGT_writable_page) )
755 BUG();
757 mfn++;
758 }
760 /* Pages that are part of page tables must be read only. */
761 l2tab = l2start + l2_table_offset(vpt_start);
762 l1start = l1tab = (l1_pgentry_t *)l2_pgentry_to_phys(*l2tab);
763 l1tab += l1_table_offset(vpt_start);
764 l2tab++;
765 for ( count = 0; count < nr_pt_pages; count++ )
766 {
767 *l1tab = mk_l1_pgentry(l1_pgentry_val(*l1tab) & ~_PAGE_RW);
768 page = &frame_table[l1_pgentry_to_pagenr(*l1tab)];
769 if ( count == 0 )
770 {
771 page->u.inuse.type_info &= ~PGT_type_mask;
772 page->u.inuse.type_info |= PGT_l2_page_table;
774 /*
775 * No longer writable: decrement the type_count.
776 * Installed as CR3: increment both the ref_count and type_count.
777 * Net: just increment the ref_count.
778 */
779 get_page(page, p); /* an extra ref because of readable mapping */
781 /* Get another ref to L2 page so that it can be pinned. */
782 if ( !get_page_and_type(page, p, PGT_l2_page_table) )
783 BUG();
784 set_bit(_PGT_pinned, &page->u.inuse.type_info);
785 }
786 else
787 {
788 page->u.inuse.type_info &= ~PGT_type_mask;
789 page->u.inuse.type_info |= PGT_l1_page_table;
790 page->u.inuse.type_info |=
791 ((dsi.v_start>>L2_PAGETABLE_SHIFT)+(count-1))<<PGT_va_shift;
793 /*
794 * No longer writable: decrement the type_count.
795 * This is an L1 page, installed in a validated L2 page:
796 * increment both the ref_count and type_count.
797 * Net: just increment the ref_count.
798 */
799 get_page(page, p); /* an extra ref because of readable mapping */
800 }
801 l1tab++;
802 if( !((unsigned long)l1tab & (PAGE_SIZE - 1)) )
803 l1start = l1tab = (l1_pgentry_t *)l2_pgentry_to_phys(*l2tab);
804 }
806 /* Set up shared-info area. */
807 update_dom_time(p->shared_info);
808 p->shared_info->domain_time = 0;
809 /* Mask all upcalls... */
810 for ( i = 0; i < MAX_VIRT_CPUS; i++ )
811 p->shared_info->vcpu_data[i].evtchn_upcall_mask = 1;
813 /* Install the new page tables. */
814 __cli();
815 write_ptbase(&p->mm);
817 /* Copy the OS image. */
818 (void)loadelfimage(image_start);
820 /* Copy the initial ramdisk. */
821 if ( initrd_len != 0 )
822 memcpy((void *)vinitrd_start, initrd_start, initrd_len);
824 /* Set up start info area. */
825 si = (start_info_t *)vstartinfo_start;
826 memset(si, 0, PAGE_SIZE);
827 si->nr_pages = p->tot_pages;
828 si->shared_info = virt_to_phys(p->shared_info);
829 si->flags = SIF_PRIVILEGED | SIF_INITDOMAIN;
830 si->pt_base = vpt_start;
831 si->nr_pt_frames = nr_pt_pages;
832 si->mfn_list = vphysmap_start;
834 /* Write the phys->machine and machine->phys table entries. */
835 for ( pfn = 0; pfn < p->tot_pages; pfn++ )
836 {
837 mfn = pfn + (alloc_start>>PAGE_SHIFT);
838 #ifndef NDEBUG
839 #define REVERSE_START ((v_end - dsi.v_start) >> PAGE_SHIFT)
840 if ( pfn > REVERSE_START )
841 mfn = (alloc_end>>PAGE_SHIFT) - (pfn - REVERSE_START);
842 #endif
843 ((unsigned long *)vphysmap_start)[pfn] = mfn;
844 machine_to_phys_mapping[mfn] = pfn;
845 }
847 if ( initrd_len != 0 )
848 {
849 si->mod_start = vinitrd_start;
850 si->mod_len = initrd_len;
851 printk("Initrd len 0x%lx, start at 0x%08lx\n",
852 si->mod_len, si->mod_start);
853 }
855 dst = si->cmd_line;
856 if ( cmdline != NULL )
857 {
858 for ( i = 0; i < 255; i++ )
859 {
860 if ( cmdline[i] == '\0' )
861 break;
862 *dst++ = cmdline[i];
863 }
864 }
865 *dst = '\0';
867 /* Reinstate the caller's page tables. */
868 write_ptbase(&current->mm);
869 __sti();
871 /* Destroy low mappings - they were only for our convenience. */
872 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
873 if ( l2_pgentry_val(l2start[i]) & _PAGE_PSE )
874 l2start[i] = mk_l2_pgentry(0);
875 zap_low_mappings(); /* Do the same for the idle page tables. */
877 /* DOM0 gets access to everything. */
878 physdev_init_dom0(p);
880 set_bit(DF_CONSTRUCTED, &p->flags);
882 new_thread(p, dsi.v_kernentry, vstack_end, vstartinfo_start);
884 #if 0 /* XXXXX DO NOT CHECK IN ENABLED !!! (but useful for testing so leave) */
885 shadow_lock(&p->mm);
886 shadow_mode_enable(p, SHM_test);
887 shadow_unlock(&p->mm);
888 #endif
890 return 0;
891 }