direct-io.hg

view xen/arch/x86/domain.c @ 3280:dda5ab69e74a

bitkeeper revision 1.1159.1.477 (41bf20d2wgoxIqhcE0nzBC8W-yFPhg)

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