ia64/xen-unstable

view xen/arch/x86/domain.c @ 2912:1c0b0d49f291

bitkeeper revision 1.1159.169.1 (418e9fbcYPjvgCp5hHW29dMCpCsuDA)

Disable Xen VGA output later in the boot, so we get more start-of-day
messages. Most importantly, users get a visual cue on VGA console when
whole of main memory is scrubbed immediately before starting dom0.
author kaf24@freefall.cl.cam.ac.uk
date Sun Nov 07 22:20:44 2004 +0000 (2004-11-07)
parents 0956f3af9fd3
children 79d8674c230f
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->event_selector = c->event_callback_cs;
280 d->event_address = c->event_callback_eip;
281 d->failsafe_selector = c->failsafe_callback_cs;
282 d->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->io_bitmap != NULL) ||
393 unlikely(next_p->io_bitmap != NULL) )
394 {
395 if ( next_p->io_bitmap != NULL )
396 {
397 /* Copy in the appropriate parts of the IO bitmap. We use the
398 * selector to copy only the interesting parts of the bitmap. */
400 u64 old_sel = ~0ULL; /* IO bitmap selector for previous task. */
402 if ( prev_p->io_bitmap != NULL)
403 {
404 old_sel = prev_p->io_bitmap_sel;
406 /* Replace any areas of the IO bitmap that had bits cleared. */
407 for ( i = 0; i < sizeof(prev_p->io_bitmap_sel) * 8; i++ )
408 if ( !test_bit(i, &prev_p->io_bitmap_sel) )
409 memcpy(&tss->io_bitmap[i * IOBMP_SELBIT_LWORDS],
410 &next_p->io_bitmap[i * IOBMP_SELBIT_LWORDS],
411 IOBMP_SELBIT_LWORDS * sizeof(unsigned long));
412 }
414 /* Copy in any regions of the new task's bitmap that have bits
415 * clear and we haven't already dealt with. */
416 for ( i = 0; i < sizeof(prev_p->io_bitmap_sel) * 8; i++ )
417 {
418 if ( test_bit(i, &old_sel)
419 && !test_bit(i, &next_p->io_bitmap_sel) )
420 memcpy(&tss->io_bitmap[i * IOBMP_SELBIT_LWORDS],
421 &next_p->io_bitmap[i * IOBMP_SELBIT_LWORDS],
422 IOBMP_SELBIT_LWORDS * sizeof(unsigned long));
423 }
425 tss->bitmap = IO_BITMAP_OFFSET;
427 }
428 else
429 {
430 /* In this case, we're switching FROM a task with IO port access,
431 * to a task that doesn't use the IO bitmap. We set any TSS bits
432 * that might have been cleared, ready for future use. */
433 for ( i = 0; i < sizeof(prev_p->io_bitmap_sel) * 8; i++ )
434 if ( !test_bit(i, &prev_p->io_bitmap_sel) )
435 memset(&tss->io_bitmap[i * IOBMP_SELBIT_LWORDS],
436 0xFF, IOBMP_SELBIT_LWORDS * sizeof(unsigned long));
438 /*
439 * a bitmap offset pointing outside of the TSS limit
440 * causes a nicely controllable SIGSEGV if a process
441 * tries to use a port IO instruction. The first
442 * sys_ioperm() call sets up the bitmap properly.
443 */
444 tss->bitmap = INVALID_IO_BITMAP_OFFSET;
445 }
446 }
448 set_current(next_p);
450 /* Switch GDT and LDT. */
451 __asm__ __volatile__ ("lgdt %0" : "=m" (*next_p->mm.gdt));
452 load_LDT(next_p);
454 __sti();
455 }
458 /* XXX Currently the 'domain' field is ignored! XXX */
459 long do_iopl(domid_t domain, unsigned int new_io_pl)
460 {
461 execution_context_t *ec = get_execution_context();
462 ec->eflags = (ec->eflags & 0xffffcfff) | ((new_io_pl&3) << 12);
463 return 0;
464 }
466 #endif
469 static void relinquish_list(struct domain *d, struct list_head *list)
470 {
471 struct list_head *ent;
472 struct pfn_info *page;
473 unsigned long x, y;
475 /* Use a recursive lock, as we may enter 'free_domheap_page'. */
476 spin_lock_recursive(&d->page_alloc_lock);
478 ent = list->next;
479 while ( ent != list )
480 {
481 page = list_entry(ent, struct pfn_info, list);
483 /* Grab a reference to the page so it won't disappear from under us. */
484 if ( unlikely(!get_page(page, d)) )
485 {
486 /* Couldn't get a reference -- someone is freeing this page. */
487 ent = ent->next;
488 continue;
489 }
491 if ( test_and_clear_bit(_PGT_pinned, &page->u.inuse.type_info) )
492 put_page_and_type(page);
494 if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
495 put_page(page);
497 /*
498 * Forcibly invalidate base page tables at this point to break circular
499 * 'linear page table' references. This is okay because MMU structures
500 * are not shared across domains and this domain is now dead. Thus base
501 * tables are not in use so a non-zero count means circular reference.
502 */
503 y = page->u.inuse.type_info;
504 for ( ; ; )
505 {
506 x = y;
507 if ( likely((x & (PGT_type_mask|PGT_validated)) !=
508 (PGT_base_page_table|PGT_validated)) )
509 break;
511 y = cmpxchg(&page->u.inuse.type_info, x, x & ~PGT_validated);
512 if ( likely(y == x) )
513 {
514 free_page_type(page, PGT_base_page_table);
515 break;
516 }
517 }
519 /* Follow the list chain and /then/ potentially free the page. */
520 ent = ent->next;
521 put_page(page);
522 }
524 spin_unlock_recursive(&d->page_alloc_lock);
525 }
528 void domain_relinquish_memory(struct domain *d)
529 {
530 audit_domain(d);
532 /* Ensure that noone is running over the dead domain's page tables. */
533 synchronise_pagetables(~0UL);
535 /* Exit shadow mode before deconstructing final guest page table. */
536 shadow_mode_disable(d);
538 /* Drop the in-use reference to the page-table base. */
539 if ( pagetable_val(d->mm.pagetable) != 0 )
540 put_page_and_type(&frame_table[pagetable_val(d->mm.pagetable) >>
541 PAGE_SHIFT]);
543 /*
544 * Relinquish GDT mappings. No need for explicit unmapping of the LDT as
545 * it automatically gets squashed when the guest's mappings go away.
546 */
547 destroy_gdt(d);
549 /* Relinquish every page of memory. */
550 relinquish_list(d, &d->xenpage_list);
551 relinquish_list(d, &d->page_list);
552 }
555 int construct_dom0(struct domain *p,
556 unsigned long alloc_start,
557 unsigned long alloc_end,
558 char *image_start, unsigned long image_len,
559 char *initrd_start, unsigned long initrd_len,
560 char *cmdline)
561 {
562 char *dst;
563 int i, rc;
564 unsigned long pfn, mfn;
565 unsigned long nr_pages = (alloc_end - alloc_start) >> PAGE_SHIFT;
566 unsigned long nr_pt_pages;
567 unsigned long count;
568 l2_pgentry_t *l2tab, *l2start;
569 l1_pgentry_t *l1tab = NULL, *l1start = NULL;
570 struct pfn_info *page = NULL;
571 start_info_t *si;
573 /*
574 * This fully describes the memory layout of the initial domain. All
575 * *_start address are page-aligned, except v_start (and v_end) which are
576 * superpage-aligned.
577 */
578 struct domain_setup_info dsi;
579 unsigned long vinitrd_start;
580 unsigned long vinitrd_end;
581 unsigned long vphysmap_start;
582 unsigned long vphysmap_end;
583 unsigned long vstartinfo_start;
584 unsigned long vstartinfo_end;
585 unsigned long vstack_start;
586 unsigned long vstack_end;
587 unsigned long vpt_start;
588 unsigned long vpt_end;
589 unsigned long v_end;
591 /* Machine address of next candidate page-table page. */
592 unsigned long mpt_alloc;
594 extern void physdev_init_dom0(struct domain *);
596 /* Sanity! */
597 if ( p->id != 0 )
598 BUG();
599 if ( test_bit(DF_CONSTRUCTED, &p->flags) )
600 BUG();
602 memset(&dsi, 0, sizeof(struct domain_setup_info));
604 printk("*** LOADING DOMAIN 0 ***\n");
606 /*
607 * This is all a bit grim. We've moved the modules to the "safe" physical
608 * memory region above MAP_DIRECTMAP_ADDRESS (48MB). Later in this
609 * routine we're going to copy it down into the region that's actually
610 * been allocated to domain 0. This is highly likely to be overlapping, so
611 * we use a forward copy.
612 *
613 * MAP_DIRECTMAP_ADDRESS should be safe. The worst case is a machine with
614 * 4GB and lots of network/disk cards that allocate loads of buffers.
615 * We'll have to revisit this if we ever support PAE (64GB).
616 */
618 rc = parseelfimage(image_start, image_len, &dsi);
619 if ( rc != 0 )
620 return rc;
622 /* Set up domain options */
623 if ( dsi.use_writable_pagetables )
624 vm_assist(p, VMASST_CMD_enable, VMASST_TYPE_writable_pagetables);
626 if ( (dsi.v_start & (PAGE_SIZE-1)) != 0 )
627 {
628 printk("Initial guest OS must load to a page boundary.\n");
629 return -EINVAL;
630 }
632 /*
633 * Why do we need this? The number of page-table frames depends on the
634 * size of the bootstrap address space. But the size of the address space
635 * depends on the number of page-table frames (since each one is mapped
636 * read-only). We have a pair of simultaneous equations in two unknowns,
637 * which we solve by exhaustive search.
638 */
639 vinitrd_start = round_pgup(dsi.v_kernend);
640 vinitrd_end = vinitrd_start + initrd_len;
641 vphysmap_start = round_pgup(vinitrd_end);
642 vphysmap_end = vphysmap_start + (nr_pages * sizeof(unsigned long));
643 vpt_start = round_pgup(vphysmap_end);
644 for ( nr_pt_pages = 2; ; nr_pt_pages++ )
645 {
646 vpt_end = vpt_start + (nr_pt_pages * PAGE_SIZE);
647 vstartinfo_start = vpt_end;
648 vstartinfo_end = vstartinfo_start + PAGE_SIZE;
649 vstack_start = vstartinfo_end;
650 vstack_end = vstack_start + PAGE_SIZE;
651 v_end = (vstack_end + (1<<22)-1) & ~((1<<22)-1);
652 if ( (v_end - vstack_end) < (512 << 10) )
653 v_end += 1 << 22; /* Add extra 4MB to get >= 512kB padding. */
654 if ( (((v_end - dsi.v_start + ((1<<L2_PAGETABLE_SHIFT)-1)) >>
655 L2_PAGETABLE_SHIFT) + 1) <= nr_pt_pages )
656 break;
657 }
659 printk("PHYSICAL MEMORY ARRANGEMENT:\n"
660 " Kernel image: %p->%p\n"
661 " Initrd image: %p->%p\n"
662 " Dom0 alloc.: %08lx->%08lx\n",
663 image_start, image_start + image_len,
664 initrd_start, initrd_start + initrd_len,
665 alloc_start, alloc_end);
666 printk("VIRTUAL MEMORY ARRANGEMENT:\n"
667 " Loaded kernel: %08lx->%08lx\n"
668 " Init. ramdisk: %08lx->%08lx\n"
669 " Phys-Mach map: %08lx->%08lx\n"
670 " Page tables: %08lx->%08lx\n"
671 " Start info: %08lx->%08lx\n"
672 " Boot stack: %08lx->%08lx\n"
673 " TOTAL: %08lx->%08lx\n",
674 dsi.v_kernstart, dsi.v_kernend,
675 vinitrd_start, vinitrd_end,
676 vphysmap_start, vphysmap_end,
677 vpt_start, vpt_end,
678 vstartinfo_start, vstartinfo_end,
679 vstack_start, vstack_end,
680 dsi.v_start, v_end);
681 printk(" ENTRY ADDRESS: %08lx\n", dsi.v_kernentry);
683 if ( (v_end - dsi.v_start) > (nr_pages * PAGE_SIZE) )
684 {
685 printk("Initial guest OS requires too much space\n"
686 "(%luMB is greater than %luMB limit)\n",
687 (v_end-dsi.v_start)>>20, (nr_pages<<PAGE_SHIFT)>>20);
688 return -ENOMEM;
689 }
691 /*
692 * Protect the lowest 1GB of memory. We use a temporary mapping there
693 * from which we copy the kernel and ramdisk images.
694 */
695 if ( dsi.v_start < (1<<30) )
696 {
697 printk("Initial loading isn't allowed to lowest 1GB of memory.\n");
698 return -EINVAL;
699 }
701 /* Paranoia: scrub DOM0's memory allocation. */
702 printk("Scrubbing DOM0 RAM: ");
703 dst = (char *)alloc_start;
704 while ( dst < (char *)alloc_end )
705 {
706 #define SCRUB_BYTES (100 * 1024 * 1024) /* 100MB */
707 printk(".");
708 touch_nmi_watchdog();
709 if ( ((char *)alloc_end - dst) > SCRUB_BYTES )
710 {
711 memset(dst, 0, SCRUB_BYTES);
712 dst += SCRUB_BYTES;
713 }
714 else
715 {
716 memset(dst, 0, (char *)alloc_end - dst);
717 break;
718 }
719 }
720 printk("done.\n");
722 /* Construct a frame-allocation list for the initial domain. */
723 for ( mfn = (alloc_start>>PAGE_SHIFT);
724 mfn < (alloc_end>>PAGE_SHIFT);
725 mfn++ )
726 {
727 page = &frame_table[mfn];
728 page->u.inuse.domain = p;
729 page->u.inuse.type_info = 0;
730 page->count_info = PGC_allocated | 1;
731 list_add_tail(&page->list, &p->page_list);
732 p->tot_pages++; p->max_pages++;
733 }
735 mpt_alloc = (vpt_start - dsi.v_start) + alloc_start;
737 SET_GDT_ENTRIES(p, DEFAULT_GDT_ENTRIES);
738 SET_GDT_ADDRESS(p, DEFAULT_GDT_ADDRESS);
740 /*
741 * We're basically forcing default RPLs to 1, so that our "what privilege
742 * level are we returning to?" logic works.
743 */
744 p->failsafe_selector = FLAT_GUESTOS_CS;
745 p->event_selector = FLAT_GUESTOS_CS;
746 p->thread.guestos_ss = FLAT_GUESTOS_DS;
747 for ( i = 0; i < 256; i++ )
748 p->thread.traps[i].cs = FLAT_GUESTOS_CS;
750 /* WARNING: The new domain must have its 'processor' field filled in! */
751 l2start = l2tab = (l2_pgentry_t *)mpt_alloc; mpt_alloc += PAGE_SIZE;
752 memcpy(l2tab, &idle_pg_table[0], PAGE_SIZE);
753 l2tab[LINEAR_PT_VIRT_START >> L2_PAGETABLE_SHIFT] =
754 mk_l2_pgentry((unsigned long)l2start | __PAGE_HYPERVISOR);
755 l2tab[PERDOMAIN_VIRT_START >> L2_PAGETABLE_SHIFT] =
756 mk_l2_pgentry(__pa(p->mm.perdomain_pt) | __PAGE_HYPERVISOR);
757 p->mm.pagetable = mk_pagetable((unsigned long)l2start);
759 l2tab += l2_table_offset(dsi.v_start);
760 mfn = alloc_start >> PAGE_SHIFT;
761 for ( count = 0; count < ((v_end-dsi.v_start)>>PAGE_SHIFT); count++ )
762 {
763 if ( !((unsigned long)l1tab & (PAGE_SIZE-1)) )
764 {
765 l1start = l1tab = (l1_pgentry_t *)mpt_alloc;
766 mpt_alloc += PAGE_SIZE;
767 *l2tab++ = mk_l2_pgentry((unsigned long)l1start | L2_PROT);
768 clear_page(l1tab);
769 if ( count == 0 )
770 l1tab += l1_table_offset(dsi.v_start);
771 }
772 *l1tab++ = mk_l1_pgentry((mfn << PAGE_SHIFT) | L1_PROT);
774 page = &frame_table[mfn];
775 if ( !get_page_and_type(page, p, PGT_writable_page) )
776 BUG();
778 mfn++;
779 }
781 /* Pages that are part of page tables must be read only. */
782 l2tab = l2start + l2_table_offset(vpt_start);
783 l1start = l1tab = (l1_pgentry_t *)l2_pgentry_to_phys(*l2tab);
784 l1tab += l1_table_offset(vpt_start);
785 l2tab++;
786 for ( count = 0; count < nr_pt_pages; count++ )
787 {
788 *l1tab = mk_l1_pgentry(l1_pgentry_val(*l1tab) & ~_PAGE_RW);
789 page = &frame_table[l1_pgentry_to_pagenr(*l1tab)];
790 if ( count == 0 )
791 {
792 page->u.inuse.type_info &= ~PGT_type_mask;
793 page->u.inuse.type_info |= PGT_l2_page_table;
795 /*
796 * No longer writable: decrement the type_count.
797 * Installed as CR3: increment both the ref_count and type_count.
798 * Net: just increment the ref_count.
799 */
800 get_page(page, p); /* an extra ref because of readable mapping */
802 /* Get another ref to L2 page so that it can be pinned. */
803 if ( !get_page_and_type(page, p, PGT_l2_page_table) )
804 BUG();
805 set_bit(_PGT_pinned, &page->u.inuse.type_info);
806 }
807 else
808 {
809 page->u.inuse.type_info &= ~PGT_type_mask;
810 page->u.inuse.type_info |= PGT_l1_page_table;
811 page->u.inuse.type_info |=
812 ((dsi.v_start>>L2_PAGETABLE_SHIFT)+(count-1))<<PGT_va_shift;
814 /*
815 * No longer writable: decrement the type_count.
816 * This is an L1 page, installed in a validated L2 page:
817 * increment both the ref_count and type_count.
818 * Net: just increment the ref_count.
819 */
820 get_page(page, p); /* an extra ref because of readable mapping */
821 }
822 l1tab++;
823 if( !((unsigned long)l1tab & (PAGE_SIZE - 1)) )
824 l1start = l1tab = (l1_pgentry_t *)l2_pgentry_to_phys(*l2tab);
825 }
827 /* Set up shared-info area. */
828 update_dom_time(p->shared_info);
829 p->shared_info->domain_time = 0;
830 /* Mask all upcalls... */
831 for ( i = 0; i < MAX_VIRT_CPUS; i++ )
832 p->shared_info->vcpu_data[i].evtchn_upcall_mask = 1;
834 /* Install the new page tables. */
835 __cli();
836 write_ptbase(&p->mm);
838 /* Copy the OS image. */
839 (void)loadelfimage(image_start);
841 /* Copy the initial ramdisk. */
842 if ( initrd_len != 0 )
843 memcpy((void *)vinitrd_start, initrd_start, initrd_len);
845 /* Set up start info area. */
846 si = (start_info_t *)vstartinfo_start;
847 memset(si, 0, PAGE_SIZE);
848 si->nr_pages = p->tot_pages;
849 si->shared_info = virt_to_phys(p->shared_info);
850 si->flags = SIF_PRIVILEGED | SIF_INITDOMAIN;
851 si->pt_base = vpt_start;
852 si->nr_pt_frames = nr_pt_pages;
853 si->mfn_list = vphysmap_start;
855 /* Write the phys->machine and machine->phys table entries. */
856 for ( pfn = 0; pfn < p->tot_pages; pfn++ )
857 {
858 mfn = pfn + (alloc_start>>PAGE_SHIFT);
859 #ifndef NDEBUG
860 #define REVERSE_START ((v_end - dsi.v_start) >> PAGE_SHIFT)
861 if ( pfn > REVERSE_START )
862 mfn = (alloc_end>>PAGE_SHIFT) - (pfn - REVERSE_START);
863 #endif
864 ((unsigned long *)vphysmap_start)[pfn] = mfn;
865 machine_to_phys_mapping[mfn] = pfn;
866 }
868 if ( initrd_len != 0 )
869 {
870 si->mod_start = vinitrd_start;
871 si->mod_len = initrd_len;
872 printk("Initrd len 0x%lx, start at 0x%08lx\n",
873 si->mod_len, si->mod_start);
874 }
876 dst = si->cmd_line;
877 if ( cmdline != NULL )
878 {
879 for ( i = 0; i < 255; i++ )
880 {
881 if ( cmdline[i] == '\0' )
882 break;
883 *dst++ = cmdline[i];
884 }
885 }
886 *dst = '\0';
888 /* Reinstate the caller's page tables. */
889 write_ptbase(&current->mm);
890 __sti();
892 /* Destroy low mappings - they were only for our convenience. */
893 for ( i = 0; i < DOMAIN_ENTRIES_PER_L2_PAGETABLE; i++ )
894 if ( l2_pgentry_val(l2start[i]) & _PAGE_PSE )
895 l2start[i] = mk_l2_pgentry(0);
896 zap_low_mappings(); /* Do the same for the idle page tables. */
898 /* DOM0 gets access to everything. */
899 physdev_init_dom0(p);
901 set_bit(DF_CONSTRUCTED, &p->flags);
903 new_thread(p, dsi.v_kernentry, vstack_end, vstartinfo_start);
905 #if 0 /* XXXXX DO NOT CHECK IN ENABLED !!! (but useful for testing so leave) */
906 shadow_lock(&p->mm);
907 shadow_mode_enable(p, SHM_test);
908 shadow_unlock(&p->mm);
909 #endif
911 return 0;
912 }