3 * Copyright (c) 2004 Christian Limpach.
4 * Copyright (c) 2004-2006,2008 Kip Macy
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Christian Limpach.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/mount.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/kernel.h>
44 #include <sys/reboot.h>
45 #include <sys/sysproto.h>
47 #include <machine/xen/xen-os.h>
51 #include <machine/segments.h>
52 #include <machine/pcb.h>
53 #include <machine/stdarg.h>
54 #include <machine/vmparam.h>
55 #include <machine/cpu.h>
56 #include <machine/intr_machdep.h>
57 #include <machine/md_var.h>
58 #include <machine/asmacros.h>
62 #include <xen/hypervisor.h>
63 #include <machine/xen/xenvar.h>
64 #include <machine/xen/xenfunc.h>
65 #include <machine/xen/xenpmap.h>
66 #include <machine/xen/xenfunc.h>
67 #include <xen/interface/memory.h>
68 #include <machine/xen/features.h>
70 #include <machine/privatespace.h>
74 #include <vm/vm_page.h>
77 #define IDTVEC(name) __CONCAT(X,name)
80 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
81 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
82 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
83 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
84 IDTVEC(xmm), IDTVEC(lcall_syscall), IDTVEC(int0x80_syscall);
88 start_info_t *xen_start_info;
89 shared_info_t *HYPERVISOR_shared_info;
90 xen_pfn_t *xen_machine_phys = machine_to_phys_mapping;
91 xen_pfn_t *xen_phys_machine;
92 xen_pfn_t *xen_pfn_to_mfn_frame_list[16];
93 xen_pfn_t *xen_pfn_to_mfn_frame_list_list;
94 int preemptable, init_first;
95 extern unsigned int avail_space;
104 __asm__("pushl %edx;"
117 __asm__("pushl %edx;"
129 * Modify the cmd_line by converting ',' to NULLs so that it is in a format
130 * suitable for the static env vars.
133 xen_setbootenv(char *cmd_line)
137 /* Skip leading spaces */
138 for (; *cmd_line == ' '; cmd_line++);
140 printk("xen_setbootenv(): cmd_line='%s'\n", cmd_line);
142 for (cmd_line_next = cmd_line; strsep(&cmd_line_next, ",") != NULL;);
151 {"boot_askname", RB_ASKNAME},
152 {"boot_single", RB_SINGLE},
153 {"boot_nosync", RB_NOSYNC},
154 {"boot_halt", RB_ASKNAME},
155 {"boot_serial", RB_SERIAL},
156 {"boot_cdrom", RB_CDROM},
157 {"boot_gdb", RB_GDB},
158 {"boot_gdb_pause", RB_RESERVED1},
159 {"boot_verbose", RB_VERBOSE},
160 {"boot_multicons", RB_MULTIPLE},
165 xen_boothowto(char *envp)
169 /* get equivalents from the environment */
170 for (i = 0; howto_names[i].ev != NULL; i++)
171 if (getenv(howto_names[i].ev) != NULL)
172 howto |= howto_names[i].mask;
176 #define PRINTK_BUFSIZE 1024
178 printk(const char *fmt, ...)
182 static char buf[PRINTK_BUFSIZE];
185 retval = vsnprintf(buf, PRINTK_BUFSIZE - 1, fmt, ap);
188 (void)HYPERVISOR_console_write(buf, retval);
192 #define XPQUEUE_SIZE 128
200 /* per-cpu queues and indices */
202 static struct mmu_log xpq_queue_log[MAX_VIRT_CPUS][XPQUEUE_SIZE];
205 static int xpq_idx[MAX_VIRT_CPUS];
206 static mmu_update_t xpq_queue[MAX_VIRT_CPUS][XPQUEUE_SIZE];
208 #define XPQ_QUEUE_LOG xpq_queue_log[vcpu]
209 #define XPQ_QUEUE xpq_queue[vcpu]
210 #define XPQ_IDX xpq_idx[vcpu]
211 #define SET_VCPU() int vcpu = smp_processor_id()
214 static mmu_update_t xpq_queue[XPQUEUE_SIZE];
215 static struct mmu_log xpq_queue_log[XPQUEUE_SIZE];
216 static int xpq_idx = 0;
218 #define XPQ_QUEUE_LOG xpq_queue_log
219 #define XPQ_QUEUE xpq_queue
220 #define XPQ_IDX xpq_idx
224 #define XPQ_IDX_INC atomic_add_int(&XPQ_IDX, 1);
230 int _xpq_idx = XPQ_IDX;
236 printk("xen_dump_queue(): %u entries\n", _xpq_idx);
237 for (i = 0; i < _xpq_idx; i++) {
238 printk(" val: %llx ptr: %llx\n", XPQ_QUEUE[i].val, XPQ_QUEUE[i].ptr);
245 _xen_flush_queue(void)
248 int _xpq_idx = XPQ_IDX;
250 /* window of vulnerability here? */
252 if (__predict_true(gdtset))
255 /* Make sure index is cleared first to avoid double updates. */
256 error = HYPERVISOR_mmu_update((mmu_update_t *)&XPQ_QUEUE,
257 _xpq_idx, NULL, DOMID_SELF);
260 if (__predict_true(gdtset))
261 for (i = _xpq_idx; i > 0;) {
263 CTR6(KTR_PMAP, "mmu:val: %lx ptr: %lx val: %lx "
264 "ptr: %lx val: %lx ptr: %lx",
265 (XPQ_QUEUE[i-1].val & 0xffffffff),
266 (XPQ_QUEUE[i-1].ptr & 0xffffffff),
267 (XPQ_QUEUE[i-2].val & 0xffffffff),
268 (XPQ_QUEUE[i-2].ptr & 0xffffffff),
269 (XPQ_QUEUE[i-3].val & 0xffffffff),
270 (XPQ_QUEUE[i-3].ptr & 0xffffffff));
273 CTR4(KTR_PMAP, "mmu: val: %lx ptr: %lx val: %lx ptr: %lx",
274 (XPQ_QUEUE[i-1].val & 0xffffffff),
275 (XPQ_QUEUE[i-1].ptr & 0xffffffff),
276 (XPQ_QUEUE[i-2].val & 0xffffffff),
277 (XPQ_QUEUE[i-2].ptr & 0xffffffff));
280 CTR2(KTR_PMAP, "mmu: val: %lx ptr: %lx",
281 (XPQ_QUEUE[i-1].val & 0xffffffff),
282 (XPQ_QUEUE[i-1].ptr & 0xffffffff));
287 if (__predict_true(gdtset))
289 if (__predict_false(error < 0)) {
290 for (i = 0; i < _xpq_idx; i++)
291 printf("val: %llx ptr: %llx\n",
292 XPQ_QUEUE[i].val, XPQ_QUEUE[i].ptr);
293 panic("Failed to execute MMU updates: %d", error);
299 xen_flush_queue(void)
302 if (XPQ_IDX != 0) _xen_flush_queue();
306 xen_increment_idx(void)
311 if (__predict_false(XPQ_IDX == XPQUEUE_SIZE))
316 xen_check_queue(void)
321 KASSERT(XPQ_IDX == 0, ("pending operations XPQ_IDX=%d", XPQ_IDX));
326 xen_invlpg(vm_offset_t va)
329 op.cmd = MMUEXT_INVLPG_ALL;
330 op.arg1.linear_addr = va & ~PAGE_MASK;
331 PANIC_IF(HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0);
335 xen_load_cr3(u_int val)
341 KASSERT(XPQ_IDX == 0, ("pending operations XPQ_IDX=%d", XPQ_IDX));
343 op.cmd = MMUEXT_NEW_BASEPTR;
344 op.arg1.mfn = xpmap_ptom(val) >> PAGE_SHIFT;
345 PANIC_IF(HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0);
349 xen_restore_flags(u_int eflags)
352 eflags = ((eflags & PSL_I) == 0);
354 __restore_flags(eflags);
358 xen_save_and_cli(void)
362 __save_and_cli(eflags);
382 return (HYPERVISOR_shared_info->vcpu_info[curcpu].arch.cr2);
386 _xen_machphys_update(vm_paddr_t mfn, vm_paddr_t pfn, char *file, int line)
390 if (__predict_true(gdtset))
392 XPQ_QUEUE[XPQ_IDX].ptr = (mfn << PAGE_SHIFT) | MMU_MACHPHYS_UPDATE;
393 XPQ_QUEUE[XPQ_IDX].val = pfn;
395 XPQ_QUEUE_LOG[XPQ_IDX].file = file;
396 XPQ_QUEUE_LOG[XPQ_IDX].line = line;
399 if (__predict_true(gdtset))
404 _xen_queue_pt_update(vm_paddr_t ptr, vm_paddr_t val, char *file, int line)
408 if (__predict_true(gdtset))
409 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
411 KASSERT((ptr & 7) == 0, ("misaligned update"));
413 if (__predict_true(gdtset))
416 XPQ_QUEUE[XPQ_IDX].ptr = ((uint64_t)ptr) | MMU_NORMAL_PT_UPDATE;
417 XPQ_QUEUE[XPQ_IDX].val = (uint64_t)val;
419 XPQ_QUEUE_LOG[XPQ_IDX].file = file;
420 XPQ_QUEUE_LOG[XPQ_IDX].line = line;
423 if (__predict_true(gdtset))
428 xen_pgdpt_pin(vm_paddr_t ma)
431 op.cmd = MMUEXT_PIN_L3_TABLE;
432 op.arg1.mfn = ma >> PAGE_SHIFT;
434 PANIC_IF(HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0);
438 xen_pgd_pin(vm_paddr_t ma)
441 op.cmd = MMUEXT_PIN_L2_TABLE;
442 op.arg1.mfn = ma >> PAGE_SHIFT;
444 PANIC_IF(HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0);
448 xen_pgd_unpin(vm_paddr_t ma)
451 op.cmd = MMUEXT_UNPIN_TABLE;
452 op.arg1.mfn = ma >> PAGE_SHIFT;
454 PANIC_IF(HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0);
458 xen_pt_pin(vm_paddr_t ma)
461 op.cmd = MMUEXT_PIN_L1_TABLE;
462 op.arg1.mfn = ma >> PAGE_SHIFT;
463 printk("xen_pt_pin(): mfn=%x\n", op.arg1.mfn);
465 PANIC_IF(HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0);
469 xen_pt_unpin(vm_paddr_t ma)
472 op.cmd = MMUEXT_UNPIN_TABLE;
473 op.arg1.mfn = ma >> PAGE_SHIFT;
475 PANIC_IF(HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0);
479 xen_set_ldt(vm_paddr_t ptr, unsigned long len)
482 op.cmd = MMUEXT_SET_LDT;
483 op.arg1.linear_addr = ptr;
484 op.arg2.nr_ents = len;
486 PANIC_IF(HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0);
489 void xen_tlb_flush(void)
492 op.cmd = MMUEXT_TLB_FLUSH_LOCAL;
494 PANIC_IF(HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0);
498 xen_update_descriptor(union descriptor *table, union descriptor *entry)
503 ptp = vtopte((vm_offset_t)table);
504 pa = (*ptp & PG_FRAME) | ((vm_offset_t)table & PAGE_MASK);
505 if (HYPERVISOR_update_descriptor(pa, *(uint64_t *)entry))
506 panic("HYPERVISOR_update_descriptor failed\n");
512 * Bitmap is indexed by page number. If bit is set, the page is part of a
513 * xen_create_contiguous_region() area of memory.
515 unsigned long *contiguous_bitmap;
518 contiguous_bitmap_set(unsigned long first_page, unsigned long nr_pages)
520 unsigned long start_off, end_off, curr_idx, end_idx;
522 curr_idx = first_page / BITS_PER_LONG;
523 start_off = first_page & (BITS_PER_LONG-1);
524 end_idx = (first_page + nr_pages) / BITS_PER_LONG;
525 end_off = (first_page + nr_pages) & (BITS_PER_LONG-1);
527 if (curr_idx == end_idx) {
528 contiguous_bitmap[curr_idx] |=
529 ((1UL<<end_off)-1) & -(1UL<<start_off);
531 contiguous_bitmap[curr_idx] |= -(1UL<<start_off);
532 while ( ++curr_idx < end_idx )
533 contiguous_bitmap[curr_idx] = ~0UL;
534 contiguous_bitmap[curr_idx] |= (1UL<<end_off)-1;
539 contiguous_bitmap_clear(unsigned long first_page, unsigned long nr_pages)
541 unsigned long start_off, end_off, curr_idx, end_idx;
543 curr_idx = first_page / BITS_PER_LONG;
544 start_off = first_page & (BITS_PER_LONG-1);
545 end_idx = (first_page + nr_pages) / BITS_PER_LONG;
546 end_off = (first_page + nr_pages) & (BITS_PER_LONG-1);
548 if (curr_idx == end_idx) {
549 contiguous_bitmap[curr_idx] &=
550 -(1UL<<end_off) | ((1UL<<start_off)-1);
552 contiguous_bitmap[curr_idx] &= (1UL<<start_off)-1;
553 while ( ++curr_idx != end_idx )
554 contiguous_bitmap[curr_idx] = 0;
555 contiguous_bitmap[curr_idx] &= -(1UL<<end_off);
560 /* Ensure multi-page extents are contiguous in machine memory. */
562 xen_create_contiguous_region(vm_page_t pages, int npages)
564 unsigned long mfn, i, flags;
566 struct xen_memory_reservation reservation = {
571 set_xen_guest_handle(reservation.extent_start, &mfn);
575 /* can currently only handle power of two allocation */
576 PANIC_IF(ffs(npages) != fls(npages));
578 /* 0. determine order */
579 order = (ffs(npages) == fls(npages)) ? fls(npages) - 1 : fls(npages);
581 /* 1. give away machine pages. */
582 for (i = 0; i < (1 << order); i++) {
584 pfn = VM_PAGE_TO_PHYS(&pages[i]) >> PAGE_SHIFT;
586 PFNTOMFN(pfn) = INVALID_P2M_ENTRY;
587 PANIC_IF(HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation) != 1);
591 /* 2. Get a new contiguous memory extent. */
592 reservation.extent_order = order;
593 /* xenlinux hardcodes this because of aacraid - maybe set to 0 if we're not
594 * running with a broxen driver XXXEN
596 reservation.address_bits = 31;
597 if (HYPERVISOR_memory_op(XENMEM_increase_reservation, &reservation) != 1)
600 /* 3. Map the new extent in place of old pages. */
601 for (i = 0; i < (1 << order); i++) {
603 pfn = VM_PAGE_TO_PHYS(&pages[i]) >> PAGE_SHIFT;
604 xen_machphys_update(mfn+i, pfn);
605 PFNTOMFN(pfn) = mfn+i;
611 contiguous_bitmap_set(VM_PAGE_TO_PHYS(&pages[0]) >> PAGE_SHIFT, 1UL << order);
614 balloon_unlock(flags);
619 reservation.extent_order = 0;
620 reservation.address_bits = 0;
622 for (i = 0; i < (1 << order); i++) {
624 pfn = VM_PAGE_TO_PHYS(&pages[i]) >> PAGE_SHIFT;
625 PANIC_IF(HYPERVISOR_memory_op(
626 XENMEM_increase_reservation, &reservation) != 1);
627 xen_machphys_update(mfn, pfn);
633 balloon_unlock(flags);
639 xen_destroy_contiguous_region(void *addr, int npages)
641 unsigned long mfn, i, flags, order, pfn0;
642 struct xen_memory_reservation reservation = {
647 set_xen_guest_handle(reservation.extent_start, &mfn);
649 pfn0 = vtophys(addr) >> PAGE_SHIFT;
651 scrub_pages(vstart, 1 << order);
653 /* can currently only handle power of two allocation */
654 PANIC_IF(ffs(npages) != fls(npages));
656 /* 0. determine order */
657 order = (ffs(npages) == fls(npages)) ? fls(npages) - 1 : fls(npages);
662 contiguous_bitmap_clear(vtophys(addr) >> PAGE_SHIFT, 1UL << order);
665 /* 1. Zap current PTEs, giving away the underlying pages. */
666 for (i = 0; i < (1 << order); i++) {
668 uint64_t new_val = 0;
669 pfn = vtomach((char *)addr + i*PAGE_SIZE) >> PAGE_SHIFT;
671 PANIC_IF(HYPERVISOR_update_va_mapping((vm_offset_t)((char *)addr + (i * PAGE_SIZE)), new_val, 0));
672 PFNTOMFN(pfn) = INVALID_P2M_ENTRY;
673 PANIC_IF(HYPERVISOR_memory_op(
674 XENMEM_decrease_reservation, &reservation) != 1);
677 /* 2. Map new pages in place of old pages. */
678 for (i = 0; i < (1 << order); i++) {
682 PANIC_IF(HYPERVISOR_memory_op(XENMEM_increase_reservation, &reservation) != 1);
684 new_val = mfn << PAGE_SHIFT;
685 PANIC_IF(HYPERVISOR_update_va_mapping((vm_offset_t)addr + (i * PAGE_SIZE),
686 new_val, PG_KERNEL));
687 xen_machphys_update(mfn, pfn);
693 balloon_unlock(flags);
696 extern unsigned long cpu0prvpage;
697 extern unsigned long *SMPpt;
698 extern struct user *proc0uarea;
699 extern vm_offset_t proc0kstack;
700 extern int vm86paddr, vm86phystk;
701 char *bootmem_start, *bootmem_current, *bootmem_end;
703 pteinfo_t *pteinfo_list;
704 void initvalues(start_info_t *startinfo);
706 struct ringbuf_head *xen_store; /* XXX move me */
710 bootmem_alloc(unsigned int size)
714 retptr = bootmem_current;
715 PANIC_IF(retptr + size > bootmem_end);
716 bootmem_current += size;
722 bootmem_free(void *ptr, unsigned int size)
727 PANIC_IF(tptr != bootmem_current - size ||
728 bootmem_current - size < bootmem_start);
730 bootmem_current -= size;
735 xpmap_mtop2(vm_paddr_t mpa)
737 return ((machine_to_phys_mapping[mpa >> PAGE_SHIFT] << PAGE_SHIFT)
738 ) | (mpa & ~PG_FRAME);
742 xpmap_get_bootpde(vm_paddr_t va)
745 return ((pd_entry_t *)xen_start_info->pt_base)[va >> 22];
749 xpmap_get_vbootpde(vm_paddr_t va)
753 pde = xpmap_get_bootpde(va);
754 if ((pde & PG_V) == 0)
755 return (pde & ~PG_FRAME);
756 return (pde & ~PG_FRAME) |
757 (xpmap_mtop2(pde & PG_FRAME) + KERNBASE);
761 xpmap_get_bootptep(vm_paddr_t va)
765 pde = xpmap_get_vbootpde(va);
766 if ((pde & PG_V) == 0)
768 #define PT_MASK 0x003ff000 /* page table address bits */
769 return &(((pt_entry_t *)(pde & PG_FRAME))[(va & PT_MASK) >> PAGE_SHIFT]);
773 xpmap_get_bootpte(vm_paddr_t va)
776 return xpmap_get_bootptep(va)[0];
783 shift_phys_machine(unsigned long *phys_machine, int nr_pages)
786 unsigned long *tmp_page, *current_page, *next_page;
789 tmp_page = bootmem_alloc(PAGE_SIZE);
790 current_page = phys_machine + nr_pages - (PAGE_SIZE/sizeof(unsigned long));
791 next_page = current_page - (PAGE_SIZE/sizeof(unsigned long));
792 bcopy(phys_machine, tmp_page, PAGE_SIZE);
794 while (current_page > phys_machine) {
796 bcopy(next_page, tmp_page, PAGE_SIZE);
797 /* shift down page */
798 bcopy(current_page, next_page, PAGE_SIZE);
800 bcopy(tmp_page, current_page, PAGE_SIZE);
802 current_page -= (PAGE_SIZE/sizeof(unsigned long));
803 next_page -= (PAGE_SIZE/sizeof(unsigned long));
805 bootmem_free(tmp_page, PAGE_SIZE);
807 for (i = 0; i < nr_pages; i++) {
808 xen_machphys_update(phys_machine[i], i);
810 memset(phys_machine, INVALID_P2M_ENTRY, PAGE_SIZE);
813 #endif /* ADD_ISA_HOLE */
816 * Build a directory of the pages that make up our Physical to Machine
817 * mapping table. The Xen suspend/restore code uses this to find our
821 init_frame_list_list(void *arg)
823 unsigned long nr_pages = xen_start_info->nr_pages;
824 #define FPP (PAGE_SIZE/sizeof(xen_pfn_t))
827 xen_pfn_to_mfn_frame_list_list = malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK);
828 for (i = 0, j = 0, k = -1; i < nr_pages;
830 if ((j & (FPP - 1)) == 0) {
832 xen_pfn_to_mfn_frame_list[k] =
833 malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK);
834 xen_pfn_to_mfn_frame_list_list[k] =
835 VTOMFN(xen_pfn_to_mfn_frame_list[k]);
838 xen_pfn_to_mfn_frame_list[k][j] =
839 VTOMFN(&xen_phys_machine[i]);
842 HYPERVISOR_shared_info->arch.max_pfn = nr_pages;
843 HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list
844 = VTOMFN(xen_pfn_to_mfn_frame_list_list);
846 SYSINIT(init_fll, SI_SUB_DEVFS, SI_ORDER_ANY, init_frame_list_list, NULL);
848 extern unsigned long physfree;
854 initvalues(start_info_t *startinfo)
856 int l3_pages, l2_pages, l1_pages, offset;
857 vm_offset_t cur_space, cur_space_pt;
858 struct physdev_set_iopl set_iopl;
860 vm_paddr_t KPTphys, IdlePTDma;
861 vm_paddr_t console_page_ma, xen_store_ma;
862 vm_offset_t KPTphysoff, tmpva;
865 vm_paddr_t IdlePDPTma, IdlePDPTnewma;
866 vm_paddr_t IdlePTDnewma[4];
867 pd_entry_t *IdlePDPTnew, *IdlePTDnew;
869 vm_paddr_t pdir_shadow_ma;
876 max((startinfo->nr_pages >> NPGPTD_SHIFT), nkpt),
877 NPGPTD*NPDEPG - KPTDI),
878 (HYPERVISOR_VIRT_START - KERNBASE) >> PDRSHIFT);
880 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
883 * need to install handler
885 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments_notify);
887 xen_start_info = startinfo;
888 xen_phys_machine = (xen_pfn_t *)startinfo->mfn_list;
890 IdlePTD = (pd_entry_t *)((uint8_t *)startinfo->pt_base + PAGE_SIZE);
896 IdlePDPT = (pd_entry_t *)startinfo->pt_base;
897 IdlePDPTma = xpmap_ptom(VTOP(startinfo->pt_base));
898 for (i = (KERNBASE >> 30);
899 (i < 4) && (IdlePDPT[i] != 0); i++)
902 * Note that only one page directory has been allocated at this point.
906 for (i = 0; i < l2_pages; i++)
907 IdlePTDma[i] = xpmap_ptom(VTOP(IdlePTD + i*PAGE_SIZE));
910 l2_pages = (l2_pages == 0) ? 1 : l2_pages;
915 for (i = (((KERNBASE>>18) & PAGE_MASK)>>PAGE_SHIFT);
916 (i<l2_pages*NPDEPG) && (i<(VM_MAX_KERNEL_ADDRESS>>PDRSHIFT)); i++) {
923 /* number of pages allocated after the pts + 1*/;
924 cur_space = xen_start_info->pt_base +
925 ((xen_start_info->nr_pt_frames) + 3 )*PAGE_SIZE;
926 printk("initvalues(): wooh - availmem=%x,%x\n", avail_space, cur_space);
928 printk("KERNBASE=%x,pt_base=%x, VTOPFN(base)=%x, nr_pt_frames=%x\n",
929 KERNBASE,xen_start_info->pt_base, VTOPFN(xen_start_info->pt_base),
930 xen_start_info->nr_pt_frames);
931 xendebug_flags = 0; /* 0xffffffff; */
933 /* allocate 4 pages for bootmem allocator */
934 bootmem_start = bootmem_current = (char *)cur_space;
935 cur_space += (4 * PAGE_SIZE);
936 bootmem_end = (char *)cur_space;
938 /* allocate page for gdt */
939 gdt = (union descriptor *)cur_space;
940 cur_space += PAGE_SIZE*ncpus;
942 /* allocate page for ldt */
943 ldt = (union descriptor *)cur_space; cur_space += PAGE_SIZE;
944 cur_space += PAGE_SIZE;
946 HYPERVISOR_shared_info = (shared_info_t *)cur_space;
947 cur_space += PAGE_SIZE;
949 xen_store = (struct ringbuf_head *)cur_space;
950 cur_space += PAGE_SIZE;
952 console_page = (char *)cur_space;
953 cur_space += PAGE_SIZE;
956 shift_phys_machine(xen_phys_machine, xen_start_info->nr_pages);
959 * pre-zero unused mapped pages - mapped on 4MB boundary
962 IdlePDPT = (pd_entry_t *)startinfo->pt_base;
963 IdlePDPTma = xpmap_ptom(VTOP(startinfo->pt_base));
965 * Note that only one page directory has been allocated at this point.
968 IdlePTD = (pd_entry_t *)((uint8_t *)startinfo->pt_base + PAGE_SIZE);
969 IdlePTDma = xpmap_ptom(VTOP(IdlePTD));
972 IdlePTD = (pd_entry_t *)startinfo->pt_base;
973 IdlePTDma = xpmap_ptom(VTOP(startinfo->pt_base));
977 l1_pages = xen_start_info->nr_pt_frames - l2_pages - l3_pages;
979 KPTphysoff = (l2_pages + l3_pages)*PAGE_SIZE;
981 KPTphys = xpmap_ptom(VTOP(startinfo->pt_base + KPTphysoff));
982 XENPRINTF("IdlePTD %p\n", IdlePTD);
983 XENPRINTF("nr_pages: %ld shared_info: 0x%lx flags: 0x%lx pt_base: 0x%lx "
984 "mod_start: 0x%lx mod_len: 0x%lx\n",
985 xen_start_info->nr_pages, xen_start_info->shared_info,
986 xen_start_info->flags, xen_start_info->pt_base,
987 xen_start_info->mod_start, xen_start_info->mod_len);
988 /* Map proc0's KSTACK */
990 proc0kstack = cur_space; cur_space += (KSTACK_PAGES * PAGE_SIZE);
991 printk("proc0kstack=%u\n", proc0kstack);
993 /* vm86/bios stack */
994 cur_space += PAGE_SIZE;
996 /* Map space for the vm86 region */
997 vm86paddr = (vm_offset_t)cur_space;
998 cur_space += (PAGE_SIZE * 3);
1001 IdlePDPTnew = (pd_entry_t *)cur_space; cur_space += PAGE_SIZE;
1002 bzero(IdlePDPTnew, PAGE_SIZE);
1004 IdlePDPTnewma = xpmap_ptom(VTOP(IdlePDPTnew));
1005 IdlePTDnew = (pd_entry_t *)cur_space; cur_space += 4*PAGE_SIZE;
1006 bzero(IdlePTDnew, 4*PAGE_SIZE);
1008 for (i = 0; i < 4; i++)
1010 xpmap_ptom(VTOP((uint8_t *)IdlePTDnew + i*PAGE_SIZE));
1014 * Copy the 4 machine addresses of the new PTDs in to the PDPT
1017 for (i = 0; i < 4; i++)
1018 IdlePDPTnew[i] = IdlePTDnewma[i] | PG_V;
1023 * re-map the new PDPT read-only
1025 PT_SET_MA(IdlePDPTnew, IdlePDPTnewma | PG_V);
1028 * Unpin the current PDPT
1030 xen_pt_unpin(IdlePDPTma);
1032 for (i = 0; i < 20; i++) {
1033 int startidx = ((KERNBASE >> 18) & PAGE_MASK) >> 3;
1035 if (IdlePTD[startidx + i] == 0) {
1043 /* unmap remaining pages from initial 4MB chunk
1046 for (tmpva = cur_space; (tmpva & ((1<<22)-1)) != 0; tmpva += PAGE_SIZE) {
1047 bzero((char *)tmpva, PAGE_SIZE);
1048 PT_SET_MA(tmpva, (vm_paddr_t)0);
1053 memcpy(((uint8_t *)IdlePTDnew) + ((unsigned int)(KERNBASE >> 18)),
1054 ((uint8_t *)IdlePTD) + ((KERNBASE >> 18) & PAGE_MASK),
1055 l1_pages*sizeof(pt_entry_t));
1057 for (i = 0; i < 4; i++) {
1058 PT_SET_MA((uint8_t *)IdlePTDnew + i*PAGE_SIZE,
1059 IdlePTDnewma[i] | PG_V);
1061 xen_load_cr3(VTOP(IdlePDPTnew));
1062 xen_pgdpt_pin(xpmap_ptom(VTOP(IdlePDPTnew)));
1064 /* allocate remainder of nkpt pages */
1065 cur_space_pt = cur_space;
1066 for (offset = (KERNBASE >> PDRSHIFT), i = l1_pages; i < nkpt;
1067 i++, cur_space += PAGE_SIZE) {
1068 pdir = (offset + i) / NPDEPG;
1069 curoffset = ((offset + i) % NPDEPG);
1070 if (((offset + i) << PDRSHIFT) == VM_MAX_KERNEL_ADDRESS)
1074 * make sure that all the initial page table pages
1077 PT_SET_MA(cur_space_pt,
1078 xpmap_ptom(VTOP(cur_space)) | PG_V | PG_RW);
1079 bzero((char *)cur_space_pt, PAGE_SIZE);
1080 PT_SET_MA(cur_space_pt, (vm_paddr_t)0);
1081 xen_pt_pin(xpmap_ptom(VTOP(cur_space)));
1082 xen_queue_pt_update((vm_paddr_t)(IdlePTDnewma[pdir] +
1083 curoffset*sizeof(vm_paddr_t)),
1084 xpmap_ptom(VTOP(cur_space)) | PG_KERNEL);
1088 for (i = 0; i < 4; i++) {
1089 pdir = (PTDPTDI + i) / NPDEPG;
1090 curoffset = (PTDPTDI + i) % NPDEPG;
1092 xen_queue_pt_update((vm_paddr_t)(IdlePTDnewma[pdir] +
1093 curoffset*sizeof(vm_paddr_t)),
1094 IdlePTDnewma[i] | PG_V);
1099 IdlePTD = IdlePTDnew;
1100 IdlePDPT = IdlePDPTnew;
1101 IdlePDPTma = IdlePDPTnewma;
1104 * shared_info is an unsigned long so this will randomly break if
1105 * it is allocated above 4GB - I guess people are used to that
1106 * sort of thing with Xen ... sigh
1108 shinfo = xen_start_info->shared_info;
1109 PT_SET_MA(HYPERVISOR_shared_info, shinfo | PG_KERNEL);
1113 xen_store_ma = (((vm_paddr_t)xen_start_info->store_mfn) << PAGE_SHIFT);
1114 PT_SET_MA(xen_store, xen_store_ma | PG_KERNEL);
1115 console_page_ma = (((vm_paddr_t)xen_start_info->console.domU.mfn) << PAGE_SHIFT);
1116 PT_SET_MA(console_page, console_page_ma | PG_KERNEL);
1121 PANIC_IF(HYPERVISOR_physdev_op(PHYSDEVOP_SET_IOPL, &set_iopl));
1124 /* add page table for KERNBASE */
1125 xen_queue_pt_update(IdlePTDma + KPTDI*sizeof(vm_paddr_t),
1126 xpmap_ptom(VTOP(cur_space) | PG_KERNEL));
1129 xen_queue_pt_update(pdir_shadow_ma[3] + KPTDI*sizeof(vm_paddr_t),
1130 xpmap_ptom(VTOP(cur_space) | PG_V | PG_A));
1132 xen_queue_pt_update(pdir_shadow_ma + KPTDI*sizeof(vm_paddr_t),
1133 xpmap_ptom(VTOP(cur_space) | PG_V | PG_A));
1136 cur_space += PAGE_SIZE;
1140 if (xen_start_info->flags & SIF_INITDOMAIN) {
1141 /* Map first megabyte */
1142 for (i = 0; i < (256 << PAGE_SHIFT); i += PAGE_SIZE)
1143 PT_SET_MA(KERNBASE + i, i | PG_KERNEL | PG_NC_PCD);
1148 * re-map kernel text read-only
1151 for (i = (((vm_offset_t)&btext) & ~PAGE_MASK);
1152 i < (((vm_offset_t)&etext) & ~PAGE_MASK); i += PAGE_SIZE)
1153 PT_SET_MA(i, xpmap_ptom(VTOP(i)) | PG_V | PG_A);
1156 physfree = VTOP(cur_space);
1157 init_first = physfree >> PAGE_SHIFT;
1158 IdlePTD = (pd_entry_t *)VTOP(IdlePTD);
1159 IdlePDPT = (pd_entry_t *)VTOP(IdlePDPT);
1160 setup_xen_features();
1161 printk("#8, proc0kstack=%u\n", proc0kstack);
1165 trap_info_t trap_table[] = {
1166 { 0, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(div)},
1167 { 1, 0|4, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(dbg)},
1168 { 3, 3|4, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(bpt)},
1169 { 4, 3, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(ofl)},
1170 /* This is UPL on Linux and KPL on BSD */
1171 { 5, 3, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(bnd)},
1172 { 6, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(ill)},
1173 { 7, 0|4, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(dna)},
1175 * { 8, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(XXX)},
1176 * no handler for double fault
1178 { 9, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(fpusegm)},
1179 {10, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(tss)},
1180 {11, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(missing)},
1181 {12, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(stk)},
1182 {13, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(prot)},
1183 {14, 0|4, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(page)},
1184 {15, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(rsvd)},
1185 {16, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(fpu)},
1186 {17, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(align)},
1187 {18, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(mchk)},
1188 {19, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(xmm)},
1189 {0x80, 3, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(int0x80_syscall)},
1194 /********** CODE WORTH KEEPING ABOVE HERE *****************/
1196 void xen_failsafe_handler(void);
1199 xen_failsafe_handler(void)
1202 panic("xen_failsafe_handler called!\n");
1205 void xen_handle_thread_switch(struct pcb *pcb);
1207 /* This is called by cpu_switch() when switching threads. */
1208 /* The pcb arg refers to the process control block of the */
1209 /* next thread which is to run */
1211 xen_handle_thread_switch(struct pcb *pcb)
1213 uint32_t *a = (uint32_t *)&PCPU_GET(fsgs_gdt)[0];
1214 uint32_t *b = (uint32_t *)&pcb->pcb_fsd;
1215 multicall_entry_t mcl[3];
1218 /* Notify Xen of task switch */
1219 mcl[i].op = __HYPERVISOR_stack_switch;
1220 mcl[i].args[0] = GSEL(GDATA_SEL, SEL_KPL);
1221 mcl[i++].args[1] = (unsigned long)pcb;
1223 /* Check for update of fsd */
1224 if (*a != *b || *(a+1) != *(b+1)) {
1225 mcl[i].op = __HYPERVISOR_update_descriptor;
1226 *(uint64_t *)&mcl[i].args[0] = vtomach((vm_offset_t)a);
1227 *(uint64_t *)&mcl[i++].args[2] = *(uint64_t *)b;
1233 /* Check for update of gsd */
1234 if (*a != *b || *(a+1) != *(b+1)) {
1235 mcl[i].op = __HYPERVISOR_update_descriptor;
1236 *(uint64_t *)&mcl[i].args[0] = vtomach((vm_offset_t)a);
1237 *(uint64_t *)&mcl[i++].args[2] = *(uint64_t *)b;
1240 (void)HYPERVISOR_multicall(mcl, i);