debuggers.hg

view xen/arch/x86/domain.c @ 3674:fb875591fd72

bitkeeper revision 1.1159.223.63 (42028527-fv-d9BM0_LRp8UKGP19gQ)

Fix NMI deferral.
Signed-off-by: keir.fraser@cl.cam.ac.uk
author kaf24@scramble.cl.cam.ac.uk
date Thu Feb 03 20:10:15 2005 +0000 (2005-02-03)
parents dee91b44a753
children 51052c8b6456 6062bb54a227
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 <xen/multicall.h>
38 /* opt_noreboot: If true, machine will need manual reset on error. */
39 static int opt_noreboot = 0;
40 boolean_param("noreboot", opt_noreboot);
42 #if !defined(CONFIG_X86_64BITMODE)
43 /* No ring-3 access in initial page tables. */
44 #define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED)
45 #else
46 /* Allow ring-3 access in long mode as guest cannot use ring 1. */
47 #define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_USER)
48 #endif
49 #define L2_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
50 #define L3_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
51 #define L4_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
53 #define round_pgup(_p) (((_p)+(PAGE_SIZE-1))&PAGE_MASK)
54 #define round_pgdown(_p) ((_p)&PAGE_MASK)
56 static void default_idle(void)
57 {
58 __cli();
59 if ( !softirq_pending(smp_processor_id()) )
60 safe_halt();
61 else
62 __sti();
63 }
65 static __attribute_used__ void idle_loop(void)
66 {
67 int cpu = smp_processor_id();
68 for ( ; ; )
69 {
70 irq_stat[cpu].idle_timestamp = jiffies;
71 while ( !softirq_pending(cpu) )
72 default_idle();
73 do_softirq();
74 }
75 }
77 void startup_cpu_idle_loop(void)
78 {
79 /* Just some sanity to ensure that the scheduler is set up okay. */
80 ASSERT(current->id == IDLE_DOMAIN_ID);
81 domain_unpause_by_systemcontroller(current);
82 __enter_scheduler();
84 /*
85 * Declares CPU setup done to the boot processor.
86 * Therefore memory barrier to ensure state is visible.
87 */
88 smp_mb();
89 init_idle();
91 idle_loop();
92 }
94 static long no_idt[2];
95 static int reboot_mode;
96 int reboot_thru_bios = 0;
98 #ifdef CONFIG_SMP
99 int reboot_smp = 0;
100 static int reboot_cpu = -1;
101 /* shamelessly grabbed from lib/vsprintf.c for readability */
102 #define is_digit(c) ((c) >= '0' && (c) <= '9')
103 #endif
106 static inline void kb_wait(void)
107 {
108 int i;
110 for (i=0; i<0x10000; i++)
111 if ((inb_p(0x64) & 0x02) == 0)
112 break;
113 }
116 void machine_restart(char * __unused)
117 {
118 #ifdef CONFIG_SMP
119 int cpuid;
120 #endif
122 if ( opt_noreboot )
123 {
124 printk("Reboot disabled on cmdline: require manual reset\n");
125 for ( ; ; ) __asm__ __volatile__ ("hlt");
126 }
128 #ifdef CONFIG_SMP
129 cpuid = GET_APIC_ID(apic_read(APIC_ID));
131 /* KAF: Need interrupts enabled for safe IPI. */
132 __sti();
134 if (reboot_smp) {
136 /* check to see if reboot_cpu is valid
137 if its not, default to the BSP */
138 if ((reboot_cpu == -1) ||
139 (reboot_cpu > (NR_CPUS -1)) ||
140 !(phys_cpu_present_map & (1<<cpuid)))
141 reboot_cpu = boot_cpu_physical_apicid;
143 reboot_smp = 0; /* use this as a flag to only go through this once*/
144 /* re-run this function on the other CPUs
145 it will fall though this section since we have
146 cleared reboot_smp, and do the reboot if it is the
147 correct CPU, otherwise it halts. */
148 if (reboot_cpu != cpuid)
149 smp_call_function((void *)machine_restart , NULL, 1, 0);
150 }
152 /* if reboot_cpu is still -1, then we want a tradional reboot,
153 and if we are not running on the reboot_cpu,, halt */
154 if ((reboot_cpu != -1) && (cpuid != reboot_cpu)) {
155 for (;;)
156 __asm__ __volatile__ ("hlt");
157 }
158 /*
159 * Stop all CPUs and turn off local APICs and the IO-APIC, so
160 * other OSs see a clean IRQ state.
161 */
162 smp_send_stop();
163 disable_IO_APIC();
164 #endif
166 if(!reboot_thru_bios) {
167 /* rebooting needs to touch the page at absolute addr 0 */
168 *((unsigned short *)__va(0x472)) = reboot_mode;
169 for (;;) {
170 int i;
171 for (i=0; i<100; i++) {
172 kb_wait();
173 udelay(50);
174 outb(0xfe,0x64); /* pulse reset low */
175 udelay(50);
176 }
177 /* That didn't work - force a triple fault.. */
178 __asm__ __volatile__("lidt %0": "=m" (no_idt));
179 __asm__ __volatile__("int3");
180 }
181 }
183 panic("Need to reinclude BIOS reboot code\n");
184 }
187 void __attribute__((noreturn)) __machine_halt(void *unused)
188 {
189 for ( ; ; )
190 __asm__ __volatile__ ( "cli; hlt" );
191 }
193 void machine_halt(void)
194 {
195 smp_call_function(__machine_halt, NULL, 1, 1);
196 __machine_halt(NULL);
197 }
199 void dump_pageframe_info(struct domain *d)
200 {
201 struct pfn_info *page;
203 if ( d->tot_pages < 10 )
204 {
205 list_for_each_entry ( page, &d->page_list, list )
206 {
207 printk("Page %08x: caf=%08x, taf=%08x\n",
208 page_to_phys(page), page->count_info,
209 page->u.inuse.type_info);
210 }
211 }
213 page = virt_to_page(d->shared_info);
214 printk("Shared_info@%08x: caf=%08x, taf=%08x\n",
215 page_to_phys(page), page->count_info,
216 page->u.inuse.type_info);
217 }
219 xmem_cache_t *domain_struct_cachep;
220 void __init domain_startofday(void)
221 {
222 domain_struct_cachep = xmem_cache_create(
223 "domain_cache", sizeof(struct domain),
224 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
225 if ( domain_struct_cachep == NULL )
226 panic("No slab cache for domain structs.");
227 }
229 struct domain *arch_alloc_domain_struct(void)
230 {
231 return xmem_cache_alloc(domain_struct_cachep);
232 }
234 void arch_free_domain_struct(struct domain *d)
235 {
236 xmem_cache_free(domain_struct_cachep, d);
237 }
239 void free_perdomain_pt(struct domain *d)
240 {
241 free_xenheap_page((unsigned long)d->mm.perdomain_pt);
242 }
244 static void continue_idle_task(struct domain *d)
245 {
246 reset_stack_and_jump(idle_loop);
247 }
249 static void continue_nonidle_task(struct domain *d)
250 {
251 reset_stack_and_jump(ret_from_intr);
252 }
254 void arch_do_createdomain(struct domain *d)
255 {
256 #ifdef ARCH_HAS_FAST_TRAP
257 SET_DEFAULT_FAST_TRAP(&d->thread);
258 #endif
260 if ( d->id == IDLE_DOMAIN_ID )
261 {
262 d->thread.schedule_tail = continue_idle_task;
263 }
264 else
265 {
266 d->thread.schedule_tail = continue_nonidle_task;
268 d->shared_info = (void *)alloc_xenheap_page();
269 memset(d->shared_info, 0, PAGE_SIZE);
270 d->shared_info->arch.mfn_to_pfn_start = m2p_start_mfn;
271 SHARE_PFN_WITH_DOMAIN(virt_to_page(d->shared_info), d);
272 machine_to_phys_mapping[virt_to_phys(d->shared_info) >>
273 PAGE_SHIFT] = INVALID_P2M_ENTRY;
275 d->mm.perdomain_pt = (l1_pgentry_t *)alloc_xenheap_page();
276 memset(d->mm.perdomain_pt, 0, PAGE_SIZE);
277 machine_to_phys_mapping[virt_to_phys(d->mm.perdomain_pt) >>
278 PAGE_SHIFT] = INVALID_P2M_ENTRY;
279 }
280 }
282 int arch_final_setup_guestos(struct domain *d, full_execution_context_t *c)
283 {
284 unsigned long phys_basetab;
285 int i, rc;
287 clear_bit(DF_DONEFPUINIT, &d->flags);
288 if ( c->flags & ECF_I387_VALID )
289 set_bit(DF_DONEFPUINIT, &d->flags);
291 memcpy(&d->thread.user_ctxt,
292 &c->cpu_ctxt,
293 sizeof(d->thread.user_ctxt));
295 /*
296 * This is sufficient! If the descriptor DPL differs from CS RPL then we'll
297 * #GP. If DS, ES, FS, GS are DPL 0 then they'll be cleared automatically.
298 * If SS RPL or DPL differs from CS RPL then we'll #GP.
299 */
300 if ( ((d->thread.user_ctxt.cs & 3) == 0) ||
301 ((d->thread.user_ctxt.ss & 3) == 0) )
302 return -EINVAL;
304 memcpy(&d->thread.i387,
305 &c->fpu_ctxt,
306 sizeof(d->thread.i387));
308 memcpy(d->thread.traps,
309 &c->trap_ctxt,
310 sizeof(d->thread.traps));
312 #ifdef ARCH_HAS_FAST_TRAP
313 if ( (rc = (int)set_fast_trap(d, c->fast_trap_idx)) != 0 )
314 return rc;
315 #endif
317 d->mm.ldt_base = c->ldt_base;
318 d->mm.ldt_ents = c->ldt_ents;
320 d->thread.guestos_ss = c->guestos_ss;
321 d->thread.guestos_sp = c->guestos_esp;
323 for ( i = 0; i < 8; i++ )
324 (void)set_debugreg(d, i, c->debugreg[i]);
326 d->thread.event_selector = c->event_callback_cs;
327 d->thread.event_address = c->event_callback_eip;
328 d->thread.failsafe_selector = c->failsafe_callback_cs;
329 d->thread.failsafe_address = c->failsafe_callback_eip;
331 phys_basetab = c->pt_base;
332 d->mm.pagetable = mk_pagetable(phys_basetab);
333 if ( !get_page_and_type(&frame_table[phys_basetab>>PAGE_SHIFT], d,
334 PGT_base_page_table) )
335 return -EINVAL;
337 /* Failure to set GDT is harmless. */
338 SET_GDT_ENTRIES(d, DEFAULT_GDT_ENTRIES);
339 SET_GDT_ADDRESS(d, DEFAULT_GDT_ADDRESS);
340 if ( c->gdt_ents != 0 )
341 {
342 if ( (rc = (int)set_gdt(d, c->gdt_frames, c->gdt_ents)) != 0 )
343 {
344 put_page_and_type(&frame_table[phys_basetab>>PAGE_SHIFT]);
345 return rc;
346 }
347 }
349 return 0;
350 }
352 #if defined(__i386__)
354 void new_thread(struct domain *d,
355 unsigned long start_pc,
356 unsigned long start_stack,
357 unsigned long start_info)
358 {
359 execution_context_t *ec = &d->thread.user_ctxt;
361 /*
362 * Initial register values:
363 * DS,ES,FS,GS = FLAT_RING1_DS
364 * CS:EIP = FLAT_RING1_CS:start_pc
365 * SS:ESP = FLAT_RING1_DS:start_stack
366 * ESI = start_info
367 * [EAX,EBX,ECX,EDX,EDI,EBP are zero]
368 */
369 ec->ds = ec->es = ec->fs = ec->gs = ec->ss = FLAT_RING1_DS;
370 ec->cs = FLAT_RING1_CS;
371 ec->eip = start_pc;
372 ec->esp = start_stack;
373 ec->esi = start_info;
375 __save_flags(ec->eflags);
376 ec->eflags |= X86_EFLAGS_IF;
377 }
380 /*
381 * This special macro can be used to load a debugging register
382 */
383 #define loaddebug(thread,register) \
384 __asm__("movl %0,%%db" #register \
385 : /* no output */ \
386 :"r" (thread->debugreg[register]))
389 void switch_to(struct domain *prev_p, struct domain *next_p)
390 {
391 struct thread_struct *next = &next_p->thread;
392 struct tss_struct *tss = init_tss + smp_processor_id();
393 execution_context_t *stack_ec = get_execution_context();
394 int i;
396 __cli();
398 /* Switch guest general-register state. */
399 if ( !is_idle_task(prev_p) )
400 {
401 memcpy(&prev_p->thread.user_ctxt,
402 stack_ec,
403 sizeof(*stack_ec));
404 unlazy_fpu(prev_p);
405 CLEAR_FAST_TRAP(&prev_p->thread);
406 }
408 if ( !is_idle_task(next_p) )
409 {
410 memcpy(stack_ec,
411 &next_p->thread.user_ctxt,
412 sizeof(*stack_ec));
414 SET_FAST_TRAP(&next_p->thread);
416 /* Switch the guest OS ring-1 stack. */
417 tss->esp1 = next->guestos_sp;
418 tss->ss1 = next->guestos_ss;
420 /* Maybe switch the debug registers. */
421 if ( unlikely(next->debugreg[7]) )
422 {
423 loaddebug(next, 0);
424 loaddebug(next, 1);
425 loaddebug(next, 2);
426 loaddebug(next, 3);
427 /* no 4 and 5 */
428 loaddebug(next, 6);
429 loaddebug(next, 7);
430 }
432 /* Switch page tables. */
433 write_ptbase(&next_p->mm);
434 }
436 if ( unlikely(prev_p->thread.io_bitmap != NULL) )
437 {
438 for ( i = 0; i < sizeof(prev_p->thread.io_bitmap_sel) * 8; i++ )
439 if ( !test_bit(i, &prev_p->thread.io_bitmap_sel) )
440 memset(&tss->io_bitmap[i * IOBMP_BYTES_PER_SELBIT],
441 ~0U, IOBMP_BYTES_PER_SELBIT);
442 tss->bitmap = IOBMP_INVALID_OFFSET;
443 }
445 if ( unlikely(next_p->thread.io_bitmap != NULL) )
446 {
447 for ( i = 0; i < sizeof(next_p->thread.io_bitmap_sel) * 8; i++ )
448 if ( !test_bit(i, &next_p->thread.io_bitmap_sel) )
449 memcpy(&tss->io_bitmap[i * IOBMP_BYTES_PER_SELBIT],
450 &next_p->thread.io_bitmap[i * IOBMP_BYTES_PER_SELBIT],
451 IOBMP_BYTES_PER_SELBIT);
452 tss->bitmap = IOBMP_OFFSET;
453 }
455 set_current(next_p);
457 /* Switch GDT and LDT. */
458 __asm__ __volatile__ ("lgdt %0" : "=m" (*next_p->mm.gdt));
459 load_LDT(next_p);
461 __sti();
462 }
465 /* XXX Currently the 'domain' field is ignored! XXX */
466 long do_iopl(domid_t domain, unsigned int new_io_pl)
467 {
468 execution_context_t *ec = get_execution_context();
469 ec->eflags = (ec->eflags & 0xffffcfff) | ((new_io_pl&3) << 12);
470 return 0;
471 }
473 #endif
475 unsigned long hypercall_create_continuation(
476 unsigned int op, unsigned int nr_args, ...)
477 {
478 struct mc_state *mcs = &mc_state[smp_processor_id()];
479 execution_context_t *ec;
480 unsigned long *preg;
481 unsigned int i;
482 va_list args;
484 va_start(args, nr_args);
486 if ( test_bit(_MCSF_in_multicall, &mcs->flags) )
487 {
488 __set_bit(_MCSF_call_preempted, &mcs->flags);
490 for ( i = 0; i < nr_args; i++ )
491 mcs->call.args[i] = va_arg(args, unsigned long);
492 }
493 else
494 {
495 ec = get_execution_context();
496 #if defined(__i386__)
497 ec->eax = op;
498 ec->eip -= 2; /* re-execute 'int 0x82' */
500 for ( i = 0, preg = &ec->ebx; i < nr_args; i++, preg++ )
501 *preg = va_arg(args, unsigned long);
502 #else
503 preg = NULL; /* XXX x86/64 */
504 #endif
505 }
507 va_end(args);
509 return op;
510 }
512 static void relinquish_list(struct domain *d, struct list_head *list)
513 {
514 struct list_head *ent;
515 struct pfn_info *page;
516 unsigned long x, y;
518 /* Use a recursive lock, as we may enter 'free_domheap_page'. */
519 spin_lock_recursive(&d->page_alloc_lock);
521 ent = list->next;
522 while ( ent != list )
523 {
524 page = list_entry(ent, struct pfn_info, list);
526 /* Grab a reference to the page so it won't disappear from under us. */
527 if ( unlikely(!get_page(page, d)) )
528 {
529 /* Couldn't get a reference -- someone is freeing this page. */
530 ent = ent->next;
531 continue;
532 }
534 if ( test_and_clear_bit(_PGT_pinned, &page->u.inuse.type_info) )
535 put_page_and_type(page);
537 if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
538 put_page(page);
540 /*
541 * Forcibly invalidate base page tables at this point to break circular
542 * 'linear page table' references. This is okay because MMU structures
543 * are not shared across domains and this domain is now dead. Thus base
544 * tables are not in use so a non-zero count means circular reference.
545 */
546 y = page->u.inuse.type_info;
547 for ( ; ; )
548 {
549 x = y;
550 if ( likely((x & (PGT_type_mask|PGT_validated)) !=
551 (PGT_base_page_table|PGT_validated)) )
552 break;
554 y = cmpxchg(&page->u.inuse.type_info, x, x & ~PGT_validated);
555 if ( likely(y == x) )
556 {
557 free_page_type(page, PGT_base_page_table);
558 break;
559 }
560 }
562 /* Follow the list chain and /then/ potentially free the page. */
563 ent = ent->next;
564 put_page(page);
565 }
567 spin_unlock_recursive(&d->page_alloc_lock);
568 }
571 void domain_relinquish_memory(struct domain *d)
572 {
573 /* Ensure that noone is running over the dead domain's page tables. */
574 synchronise_pagetables(~0UL);
576 /* Exit shadow mode before deconstructing final guest page table. */
577 shadow_mode_disable(d);
579 /* Drop the in-use reference to the page-table base. */
580 if ( pagetable_val(d->mm.pagetable) != 0 )
581 put_page_and_type(&frame_table[pagetable_val(d->mm.pagetable) >>
582 PAGE_SHIFT]);
584 /*
585 * Relinquish GDT mappings. No need for explicit unmapping of the LDT as
586 * it automatically gets squashed when the guest's mappings go away.
587 */
588 destroy_gdt(d);
590 /* Relinquish every page of memory. */
591 relinquish_list(d, &d->xenpage_list);
592 relinquish_list(d, &d->page_list);
593 }
596 int construct_dom0(struct domain *p,
597 unsigned long alloc_start,
598 unsigned long alloc_end,
599 char *image_start, unsigned long image_len,
600 char *initrd_start, unsigned long initrd_len,
601 char *cmdline)
602 {
603 char *dst;
604 int i, rc;
605 unsigned long pfn, mfn;
606 unsigned long nr_pages = (alloc_end - alloc_start) >> PAGE_SHIFT;
607 unsigned long nr_pt_pages;
608 unsigned long count;
609 l2_pgentry_t *l2tab, *l2start;
610 l1_pgentry_t *l1tab = NULL, *l1start = NULL;
611 struct pfn_info *page = NULL;
612 start_info_t *si;
614 /*
615 * This fully describes the memory layout of the initial domain. All
616 * *_start address are page-aligned, except v_start (and v_end) which are
617 * superpage-aligned.
618 */
619 struct domain_setup_info dsi;
620 unsigned long vinitrd_start;
621 unsigned long vinitrd_end;
622 unsigned long vphysmap_start;
623 unsigned long vphysmap_end;
624 unsigned long vstartinfo_start;
625 unsigned long vstartinfo_end;
626 unsigned long vstack_start;
627 unsigned long vstack_end;
628 unsigned long vpt_start;
629 unsigned long vpt_end;
630 unsigned long v_end;
632 /* Machine address of next candidate page-table page. */
633 unsigned long mpt_alloc;
635 extern void physdev_init_dom0(struct domain *);
637 /* Sanity! */
638 if ( p->id != 0 )
639 BUG();
640 if ( test_bit(DF_CONSTRUCTED, &p->flags) )
641 BUG();
643 memset(&dsi, 0, sizeof(struct domain_setup_info));
645 printk("*** LOADING DOMAIN 0 ***\n");
647 /*
648 * This is all a bit grim. We've moved the modules to the "safe" physical
649 * memory region above MAP_DIRECTMAP_ADDRESS (48MB). Later in this
650 * routine we're going to copy it down into the region that's actually
651 * been allocated to domain 0. This is highly likely to be overlapping, so
652 * we use a forward copy.
653 *
654 * MAP_DIRECTMAP_ADDRESS should be safe. The worst case is a machine with
655 * 4GB and lots of network/disk cards that allocate loads of buffers.
656 * We'll have to revisit this if we ever support PAE (64GB).
657 */
659 rc = parseelfimage(image_start, image_len, &dsi);
660 if ( rc != 0 )
661 return rc;
663 /* Set up domain options */
664 if ( dsi.use_writable_pagetables )
665 vm_assist(p, VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
667 if ( (dsi.v_start & (PAGE_SIZE-1)) != 0 )
668 {
669 printk("Initial guest OS must load to a page boundary.\n");
670 return -EINVAL;
671 }
673 /*
674 * Why do we need this? The number of page-table frames depends on the
675 * size of the bootstrap address space. But the size of the address space
676 * depends on the number of page-table frames (since each one is mapped
677 * read-only). We have a pair of simultaneous equations in two unknowns,
678 * which we solve by exhaustive search.
679 */
680 vinitrd_start = round_pgup(dsi.v_kernend);
681 vinitrd_end = vinitrd_start + initrd_len;
682 vphysmap_start = round_pgup(vinitrd_end);
683 vphysmap_end = vphysmap_start + (nr_pages * sizeof(unsigned long));
684 vpt_start = round_pgup(vphysmap_end);
685 for ( nr_pt_pages = 2; ; nr_pt_pages++ )
686 {
687 vpt_end = vpt_start + (nr_pt_pages * PAGE_SIZE);
688 vstartinfo_start = vpt_end;
689 vstartinfo_end = vstartinfo_start + PAGE_SIZE;
690 vstack_start = vstartinfo_end;
691 vstack_end = vstack_start + PAGE_SIZE;
692 v_end = (vstack_end + (1<<22)-1) & ~((1<<22)-1);
693 if ( (v_end - vstack_end) < (512 << 10) )
694 v_end += 1 << 22; /* Add extra 4MB to get >= 512kB padding. */
695 if ( (((v_end - dsi.v_start + ((1<<L2_PAGETABLE_SHIFT)-1)) >>
696 L2_PAGETABLE_SHIFT) + 1) <= nr_pt_pages )
697 break;
698 }
700 printk("PHYSICAL MEMORY ARRANGEMENT:\n"
701 " Kernel image: %p->%p\n"
702 " Initrd image: %p->%p\n"
703 " Dom0 alloc.: %08lx->%08lx\n",
704 image_start, image_start + image_len,
705 initrd_start, initrd_start + initrd_len,
706 alloc_start, alloc_end);
707 printk("VIRTUAL MEMORY ARRANGEMENT:\n"
708 " Loaded kernel: %08lx->%08lx\n"
709 " Init. ramdisk: %08lx->%08lx\n"
710 " Phys-Mach map: %08lx->%08lx\n"
711 " Page tables: %08lx->%08lx\n"
712 " Start info: %08lx->%08lx\n"
713 " Boot stack: %08lx->%08lx\n"
714 " TOTAL: %08lx->%08lx\n",
715 dsi.v_kernstart, dsi.v_kernend,
716 vinitrd_start, vinitrd_end,
717 vphysmap_start, vphysmap_end,
718 vpt_start, vpt_end,
719 vstartinfo_start, vstartinfo_end,
720 vstack_start, vstack_end,
721 dsi.v_start, v_end);
722 printk(" ENTRY ADDRESS: %08lx\n", dsi.v_kernentry);
724 if ( (v_end - dsi.v_start) > (nr_pages * PAGE_SIZE) )
725 {
726 printk("Initial guest OS requires too much space\n"
727 "(%luMB is greater than %luMB limit)\n",
728 (v_end-dsi.v_start)>>20, (nr_pages<<PAGE_SHIFT)>>20);
729 return -ENOMEM;
730 }
732 /*
733 * Protect the lowest 1GB of memory. We use a temporary mapping there
734 * from which we copy the kernel and ramdisk images.
735 */
736 if ( dsi.v_start < (1<<30) )
737 {
738 printk("Initial loading isn't allowed to lowest 1GB of memory.\n");
739 return -EINVAL;
740 }
742 /* Paranoia: scrub DOM0's memory allocation. */
743 printk("Scrubbing DOM0 RAM: ");
744 dst = (char *)alloc_start;
745 while ( dst < (char *)alloc_end )
746 {
747 #define SCRUB_BYTES (100 * 1024 * 1024) /* 100MB */
748 printk(".");
749 touch_nmi_watchdog();
750 if ( ((char *)alloc_end - dst) > SCRUB_BYTES )
751 {
752 memset(dst, 0, SCRUB_BYTES);
753 dst += SCRUB_BYTES;
754 }
755 else
756 {
757 memset(dst, 0, (char *)alloc_end - dst);
758 break;
759 }
760 }
761 printk("done.\n");
763 /* Construct a frame-allocation list for the initial domain. */
764 for ( mfn = (alloc_start>>PAGE_SHIFT);
765 mfn < (alloc_end>>PAGE_SHIFT);
766 mfn++ )
767 {
768 page = &frame_table[mfn];
769 page->u.inuse.domain = p;
770 page->u.inuse.type_info = 0;
771 page->count_info = PGC_allocated | 1;
772 list_add_tail(&page->list, &p->page_list);
773 p->tot_pages++; p->max_pages++;
774 }
776 mpt_alloc = (vpt_start - dsi.v_start) + alloc_start;
778 SET_GDT_ENTRIES(p, DEFAULT_GDT_ENTRIES);
779 SET_GDT_ADDRESS(p, DEFAULT_GDT_ADDRESS);
781 /*
782 * We're basically forcing default RPLs to 1, so that our "what privilege
783 * level are we returning to?" logic works.
784 */
785 p->thread.failsafe_selector = FLAT_GUESTOS_CS;
786 p->thread.event_selector = FLAT_GUESTOS_CS;
787 p->thread.guestos_ss = FLAT_GUESTOS_DS;
788 for ( i = 0; i < 256; i++ )
789 p->thread.traps[i].cs = FLAT_GUESTOS_CS;
791 /* WARNING: The new domain must have its 'processor' field filled in! */
792 l2start = l2tab = (l2_pgentry_t *)mpt_alloc; mpt_alloc += PAGE_SIZE;
793 memcpy(l2tab, &idle_pg_table[0], PAGE_SIZE);
794 l2tab[LINEAR_PT_VIRT_START >> L2_PAGETABLE_SHIFT] =
795 mk_l2_pgentry((unsigned long)l2start | __PAGE_HYPERVISOR);
796 l2tab[PERDOMAIN_VIRT_START >> L2_PAGETABLE_SHIFT] =
797 mk_l2_pgentry(__pa(p->mm.perdomain_pt) | __PAGE_HYPERVISOR);
798 p->mm.pagetable = mk_pagetable((unsigned long)l2start);
800 l2tab += l2_table_offset(dsi.v_start);
801 mfn = alloc_start >> PAGE_SHIFT;
802 for ( count = 0; count < ((v_end-dsi.v_start)>>PAGE_SHIFT); count++ )
803 {
804 if ( !((unsigned long)l1tab & (PAGE_SIZE-1)) )
805 {
806 l1start = l1tab = (l1_pgentry_t *)mpt_alloc;
807 mpt_alloc += PAGE_SIZE;
808 *l2tab++ = mk_l2_pgentry((unsigned long)l1start | L2_PROT);
809 clear_page(l1tab);
810 if ( count == 0 )
811 l1tab += l1_table_offset(dsi.v_start);
812 }
813 *l1tab++ = mk_l1_pgentry((mfn << PAGE_SHIFT) | L1_PROT);
815 page = &frame_table[mfn];
816 if ( !get_page_and_type(page, p, PGT_writable_page) )
817 BUG();
819 mfn++;
820 }
822 /* Pages that are part of page tables must be read only. */
823 l2tab = l2start + l2_table_offset(vpt_start);
824 l1start = l1tab = (l1_pgentry_t *)l2_pgentry_to_phys(*l2tab);
825 l1tab += l1_table_offset(vpt_start);
826 l2tab++;
827 for ( count = 0; count < nr_pt_pages; count++ )
828 {
829 *l1tab = mk_l1_pgentry(l1_pgentry_val(*l1tab) & ~_PAGE_RW);
830 page = &frame_table[l1_pgentry_to_pagenr(*l1tab)];
831 if ( count == 0 )
832 {
833 page->u.inuse.type_info &= ~PGT_type_mask;
834 page->u.inuse.type_info |= PGT_l2_page_table;
836 /*
837 * No longer writable: decrement the type_count.
838 * Installed as CR3: increment both the ref_count and type_count.
839 * Net: just increment the ref_count.
840 */
841 get_page(page, p); /* an extra ref because of readable mapping */
843 /* Get another ref to L2 page so that it can be pinned. */
844 if ( !get_page_and_type(page, p, PGT_l2_page_table) )
845 BUG();
846 set_bit(_PGT_pinned, &page->u.inuse.type_info);
847 }
848 else
849 {
850 page->u.inuse.type_info &= ~PGT_type_mask;
851 page->u.inuse.type_info |= PGT_l1_page_table;
852 page->u.inuse.type_info |=
853 ((dsi.v_start>>L2_PAGETABLE_SHIFT)+(count-1))<<PGT_va_shift;
855 /*
856 * No longer writable: decrement the type_count.
857 * This is an L1 page, installed in a validated L2 page:
858 * increment both the ref_count and type_count.
859 * Net: just increment the ref_count.
860 */
861 get_page(page, p); /* an extra ref because of readable mapping */
862 }
863 l1tab++;
864 if( !((unsigned long)l1tab & (PAGE_SIZE - 1)) )
865 l1start = l1tab = (l1_pgentry_t *)l2_pgentry_to_phys(*l2tab);
866 }
868 /* Set up shared-info area. */
869 update_dom_time(p->shared_info);
870 p->shared_info->domain_time = 0;
871 /* Mask all upcalls... */
872 for ( i = 0; i < MAX_VIRT_CPUS; i++ )
873 p->shared_info->vcpu_data[i].evtchn_upcall_mask = 1;
875 /* Install the new page tables. */
876 __cli();
877 write_ptbase(&p->mm);
879 /* Copy the OS image. */
880 (void)loadelfimage(image_start);
882 /* Copy the initial ramdisk. */
883 if ( initrd_len != 0 )
884 memcpy((void *)vinitrd_start, initrd_start, initrd_len);
886 /* Set up start info area. */
887 si = (start_info_t *)vstartinfo_start;
888 memset(si, 0, PAGE_SIZE);
889 si->nr_pages = p->tot_pages;
890 si->shared_info = virt_to_phys(p->shared_info);
891 si->flags = SIF_PRIVILEGED | SIF_INITDOMAIN;
892 si->pt_base = vpt_start;
893 si->nr_pt_frames = nr_pt_pages;
894 si->mfn_list = vphysmap_start;
896 /* Write the phys->machine and machine->phys table entries. */
897 for ( pfn = 0; pfn < p->tot_pages; pfn++ )
898 {
899 mfn = pfn + (alloc_start>>PAGE_SHIFT);
900 #ifndef NDEBUG
901 #define REVERSE_START ((v_end - dsi.v_start) >> PAGE_SHIFT)
902 if ( pfn > REVERSE_START )
903 mfn = (alloc_end>>PAGE_SHIFT) - (pfn - REVERSE_START);
904 #endif
905 ((unsigned long *)vphysmap_start)[pfn] = mfn;
906 machine_to_phys_mapping[mfn] = pfn;
907 }
909 if ( initrd_len != 0 )
910 {
911 si->mod_start = vinitrd_start;
912 si->mod_len = initrd_len;
913 printk("Initrd len 0x%lx, start at 0x%08lx\n",
914 si->mod_len, si->mod_start);
915 }
917 dst = si->cmd_line;
918 if ( cmdline != NULL )
919 {
920 for ( i = 0; i < 255; i++ )
921 {
922 if ( cmdline[i] == '\0' )
923 break;
924 *dst++ = cmdline[i];
925 }
926 }
927 *dst = '\0';
929 /* Reinstate the caller's page tables. */
930 write_ptbase(&current->mm);
931 __sti();
933 /* Destroy low mappings - they were only for our convenience. */
934 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
935 if ( l2_pgentry_val(l2start[i]) & _PAGE_PSE )
936 l2start[i] = mk_l2_pgentry(0);
937 zap_low_mappings(); /* Do the same for the idle page tables. */
939 /* DOM0 gets access to everything. */
940 physdev_init_dom0(p);
942 set_bit(DF_CONSTRUCTED, &p->flags);
944 new_thread(p, dsi.v_kernentry, vstack_end, vstartinfo_start);
946 #if 0 /* XXXXX DO NOT CHECK IN ENABLED !!! (but useful for testing so leave) */
947 shadow_lock(&p->mm);
948 shadow_mode_enable(p, SHM_test);
949 shadow_unlock(&p->mm);
950 #endif
952 return 0;
953 }