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>
41 #include <sys/mount.h>
42 #include <sys/malloc.h>
43 #include <sys/mutex.h>
44 #include <sys/kernel.h>
45 #include <sys/reboot.h>
46 #include <sys/sysproto.h>
48 #include <machine/xen/xen-os.h>
52 #include <machine/segments.h>
53 #include <machine/pcb.h>
54 #include <machine/stdarg.h>
55 #include <machine/vmparam.h>
56 #include <machine/cpu.h>
57 #include <machine/intr_machdep.h>
58 #include <machine/md_var.h>
59 #include <machine/asmacros.h>
63 #include <xen/hypervisor.h>
64 #include <machine/xen/xenvar.h>
65 #include <machine/xen/xenfunc.h>
66 #include <machine/xen/xenpmap.h>
67 #include <machine/xen/xenfunc.h>
68 #include <xen/interface/memory.h>
69 #include <machine/xen/features.h>
71 #include <machine/privatespace.h>
75 #include <vm/vm_page.h>
78 #define IDTVEC(name) __CONCAT(X,name)
81 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
82 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
83 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
84 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
85 IDTVEC(xmm), IDTVEC(lcall_syscall), IDTVEC(int0x80_syscall);
89 start_info_t *xen_start_info;
90 shared_info_t *HYPERVISOR_shared_info;
91 xen_pfn_t *xen_machine_phys = machine_to_phys_mapping;
92 xen_pfn_t *xen_phys_machine;
93 xen_pfn_t *xen_pfn_to_mfn_frame_list[16];
94 xen_pfn_t *xen_pfn_to_mfn_frame_list_list;
95 int preemptable, init_first;
96 extern unsigned int avail_space;
105 CTR0(KTR_SPARE2, "ni_cli disabling interrupts");
106 __asm__("pushl %edx;"
119 __asm__("pushl %edx;"
131 * Modify the cmd_line by converting ',' to NULLs so that it is in a format
132 * suitable for the static env vars.
135 xen_setbootenv(char *cmd_line)
139 /* Skip leading spaces */
140 for (; *cmd_line == ' '; cmd_line++);
142 printk("xen_setbootenv(): cmd_line='%s'\n", cmd_line);
144 for (cmd_line_next = cmd_line; strsep(&cmd_line_next, ",") != NULL;);
153 {"boot_askname", RB_ASKNAME},
154 {"boot_single", RB_SINGLE},
155 {"boot_nosync", RB_NOSYNC},
156 {"boot_halt", RB_ASKNAME},
157 {"boot_serial", RB_SERIAL},
158 {"boot_cdrom", RB_CDROM},
159 {"boot_gdb", RB_GDB},
160 {"boot_gdb_pause", RB_RESERVED1},
161 {"boot_verbose", RB_VERBOSE},
162 {"boot_multicons", RB_MULTIPLE},
167 xen_boothowto(char *envp)
171 /* get equivalents from the environment */
172 for (i = 0; howto_names[i].ev != NULL; i++)
173 if (getenv(howto_names[i].ev) != NULL)
174 howto |= howto_names[i].mask;
178 #define PRINTK_BUFSIZE 1024
180 printk(const char *fmt, ...)
184 static char buf[PRINTK_BUFSIZE];
187 retval = vsnprintf(buf, PRINTK_BUFSIZE - 1, fmt, ap);
190 (void)HYPERVISOR_console_write(buf, retval);
194 #define XPQUEUE_SIZE 128
202 /* per-cpu queues and indices */
204 static struct mmu_log xpq_queue_log[MAX_VIRT_CPUS][XPQUEUE_SIZE];
207 static int xpq_idx[MAX_VIRT_CPUS];
208 static mmu_update_t xpq_queue[MAX_VIRT_CPUS][XPQUEUE_SIZE];
210 #define XPQ_QUEUE_LOG xpq_queue_log[vcpu]
211 #define XPQ_QUEUE xpq_queue[vcpu]
212 #define XPQ_IDX xpq_idx[vcpu]
213 #define SET_VCPU() int vcpu = smp_processor_id()
216 static mmu_update_t xpq_queue[XPQUEUE_SIZE];
217 static struct mmu_log xpq_queue_log[XPQUEUE_SIZE];
218 static int xpq_idx = 0;
220 #define XPQ_QUEUE_LOG xpq_queue_log
221 #define XPQ_QUEUE xpq_queue
222 #define XPQ_IDX xpq_idx
226 #define XPQ_IDX_INC atomic_add_int(&XPQ_IDX, 1);
232 int _xpq_idx = XPQ_IDX;
238 printk("xen_dump_queue(): %u entries\n", _xpq_idx);
239 for (i = 0; i < _xpq_idx; i++) {
240 printk(" val: %llx ptr: %llx\n", XPQ_QUEUE[i].val, XPQ_QUEUE[i].ptr);
247 _xen_flush_queue(void)
250 int _xpq_idx = XPQ_IDX;
252 /* window of vulnerability here? */
254 if (__predict_true(gdtset))
257 /* Make sure index is cleared first to avoid double updates. */
258 error = HYPERVISOR_mmu_update((mmu_update_t *)&XPQ_QUEUE,
259 _xpq_idx, NULL, DOMID_SELF);
262 if (__predict_true(gdtset))
263 for (i = _xpq_idx; i > 0;) {
265 CTR6(KTR_PMAP, "mmu:val: %lx ptr: %lx val: %lx "
266 "ptr: %lx val: %lx ptr: %lx",
267 (XPQ_QUEUE[i-1].val & 0xffffffff),
268 (XPQ_QUEUE[i-1].ptr & 0xffffffff),
269 (XPQ_QUEUE[i-2].val & 0xffffffff),
270 (XPQ_QUEUE[i-2].ptr & 0xffffffff),
271 (XPQ_QUEUE[i-3].val & 0xffffffff),
272 (XPQ_QUEUE[i-3].ptr & 0xffffffff));
275 CTR4(KTR_PMAP, "mmu: val: %lx ptr: %lx val: %lx ptr: %lx",
276 (XPQ_QUEUE[i-1].val & 0xffffffff),
277 (XPQ_QUEUE[i-1].ptr & 0xffffffff),
278 (XPQ_QUEUE[i-2].val & 0xffffffff),
279 (XPQ_QUEUE[i-2].ptr & 0xffffffff));
282 CTR2(KTR_PMAP, "mmu: val: %lx ptr: %lx",
283 (XPQ_QUEUE[i-1].val & 0xffffffff),
284 (XPQ_QUEUE[i-1].ptr & 0xffffffff));
289 if (__predict_true(gdtset))
291 if (__predict_false(error < 0)) {
292 for (i = 0; i < _xpq_idx; i++)
293 printf("val: %llx ptr: %llx\n",
294 XPQ_QUEUE[i].val, XPQ_QUEUE[i].ptr);
295 panic("Failed to execute MMU updates: %d", error);
301 xen_flush_queue(void)
304 if (XPQ_IDX != 0) _xen_flush_queue();
308 xen_increment_idx(void)
313 if (__predict_false(XPQ_IDX == XPQUEUE_SIZE))
318 xen_check_queue(void)
323 KASSERT(XPQ_IDX == 0, ("pending operations XPQ_IDX=%d", XPQ_IDX));
328 xen_invlpg(vm_offset_t va)
331 op.cmd = MMUEXT_INVLPG_ALL;
332 op.arg1.linear_addr = va & ~PAGE_MASK;
333 PANIC_IF(HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0);
337 xen_load_cr3(u_int val)
343 KASSERT(XPQ_IDX == 0, ("pending operations XPQ_IDX=%d", XPQ_IDX));
345 op.cmd = MMUEXT_NEW_BASEPTR;
346 op.arg1.mfn = xpmap_ptom(val) >> PAGE_SHIFT;
347 PANIC_IF(HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0);
351 static __inline u_int
356 __asm __volatile("movl 4(%%ebp),%0" : "=r" (data));
367 eflags = _read_eflags();
368 _vcpu = &HYPERVISOR_shared_info->vcpu_info[smp_processor_id()];
369 if (_vcpu->evtchn_upcall_mask)
376 write_eflags(u_int eflags)
380 CTR2(KTR_SPARE2, "%x xen_restore_flags eflags %x", rebp(), eflags);
381 intr = ((eflags & PSL_I) == 0);
382 __restore_flags(intr);
383 _write_eflags(eflags);
389 CTR1(KTR_SPARE2, "%x xen_cli disabling interrupts", rebp());
396 CTR1(KTR_SPARE2, "%x xen_sti enabling interrupts", rebp());
404 return (HYPERVISOR_shared_info->vcpu_info[curcpu].arch.cr2);
408 _xen_machphys_update(vm_paddr_t mfn, vm_paddr_t pfn, char *file, int line)
412 if (__predict_true(gdtset))
414 XPQ_QUEUE[XPQ_IDX].ptr = (mfn << PAGE_SHIFT) | MMU_MACHPHYS_UPDATE;
415 XPQ_QUEUE[XPQ_IDX].val = pfn;
417 XPQ_QUEUE_LOG[XPQ_IDX].file = file;
418 XPQ_QUEUE_LOG[XPQ_IDX].line = line;
421 if (__predict_true(gdtset))
426 _xen_queue_pt_update(vm_paddr_t ptr, vm_paddr_t val, char *file, int line)
430 if (__predict_true(gdtset))
431 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
433 KASSERT((ptr & 7) == 0, ("misaligned update"));
435 if (__predict_true(gdtset))
438 XPQ_QUEUE[XPQ_IDX].ptr = ((uint64_t)ptr) | MMU_NORMAL_PT_UPDATE;
439 XPQ_QUEUE[XPQ_IDX].val = (uint64_t)val;
441 XPQ_QUEUE_LOG[XPQ_IDX].file = file;
442 XPQ_QUEUE_LOG[XPQ_IDX].line = line;
445 if (__predict_true(gdtset))
450 xen_pgdpt_pin(vm_paddr_t ma)
453 op.cmd = MMUEXT_PIN_L3_TABLE;
454 op.arg1.mfn = ma >> PAGE_SHIFT;
456 PANIC_IF(HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0);
460 xen_pgd_pin(vm_paddr_t ma)
463 op.cmd = MMUEXT_PIN_L2_TABLE;
464 op.arg1.mfn = ma >> PAGE_SHIFT;
466 PANIC_IF(HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0);
470 xen_pgd_unpin(vm_paddr_t ma)
473 op.cmd = MMUEXT_UNPIN_TABLE;
474 op.arg1.mfn = ma >> PAGE_SHIFT;
476 PANIC_IF(HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0);
480 xen_pt_pin(vm_paddr_t ma)
483 op.cmd = MMUEXT_PIN_L1_TABLE;
484 op.arg1.mfn = ma >> PAGE_SHIFT;
485 printk("xen_pt_pin(): mfn=%x\n", op.arg1.mfn);
487 PANIC_IF(HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0);
491 xen_pt_unpin(vm_paddr_t ma)
494 op.cmd = MMUEXT_UNPIN_TABLE;
495 op.arg1.mfn = ma >> PAGE_SHIFT;
497 PANIC_IF(HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0);
501 xen_set_ldt(vm_paddr_t ptr, unsigned long len)
504 op.cmd = MMUEXT_SET_LDT;
505 op.arg1.linear_addr = ptr;
506 op.arg2.nr_ents = len;
508 PANIC_IF(HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0);
511 void xen_tlb_flush(void)
514 op.cmd = MMUEXT_TLB_FLUSH_LOCAL;
516 PANIC_IF(HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0);
520 xen_update_descriptor(union descriptor *table, union descriptor *entry)
525 ptp = vtopte((vm_offset_t)table);
526 pa = (*ptp & PG_FRAME) | ((vm_offset_t)table & PAGE_MASK);
527 if (HYPERVISOR_update_descriptor(pa, *(uint64_t *)entry))
528 panic("HYPERVISOR_update_descriptor failed\n");
534 * Bitmap is indexed by page number. If bit is set, the page is part of a
535 * xen_create_contiguous_region() area of memory.
537 unsigned long *contiguous_bitmap;
540 contiguous_bitmap_set(unsigned long first_page, unsigned long nr_pages)
542 unsigned long start_off, end_off, curr_idx, end_idx;
544 curr_idx = first_page / BITS_PER_LONG;
545 start_off = first_page & (BITS_PER_LONG-1);
546 end_idx = (first_page + nr_pages) / BITS_PER_LONG;
547 end_off = (first_page + nr_pages) & (BITS_PER_LONG-1);
549 if (curr_idx == end_idx) {
550 contiguous_bitmap[curr_idx] |=
551 ((1UL<<end_off)-1) & -(1UL<<start_off);
553 contiguous_bitmap[curr_idx] |= -(1UL<<start_off);
554 while ( ++curr_idx < end_idx )
555 contiguous_bitmap[curr_idx] = ~0UL;
556 contiguous_bitmap[curr_idx] |= (1UL<<end_off)-1;
561 contiguous_bitmap_clear(unsigned long first_page, unsigned long nr_pages)
563 unsigned long start_off, end_off, curr_idx, end_idx;
565 curr_idx = first_page / BITS_PER_LONG;
566 start_off = first_page & (BITS_PER_LONG-1);
567 end_idx = (first_page + nr_pages) / BITS_PER_LONG;
568 end_off = (first_page + nr_pages) & (BITS_PER_LONG-1);
570 if (curr_idx == end_idx) {
571 contiguous_bitmap[curr_idx] &=
572 -(1UL<<end_off) | ((1UL<<start_off)-1);
574 contiguous_bitmap[curr_idx] &= (1UL<<start_off)-1;
575 while ( ++curr_idx != end_idx )
576 contiguous_bitmap[curr_idx] = 0;
577 contiguous_bitmap[curr_idx] &= -(1UL<<end_off);
582 /* Ensure multi-page extents are contiguous in machine memory. */
584 xen_create_contiguous_region(vm_page_t pages, int npages)
586 unsigned long mfn, i, flags;
588 struct xen_memory_reservation reservation = {
593 set_xen_guest_handle(reservation.extent_start, &mfn);
597 /* can currently only handle power of two allocation */
598 PANIC_IF(ffs(npages) != fls(npages));
600 /* 0. determine order */
601 order = (ffs(npages) == fls(npages)) ? fls(npages) - 1 : fls(npages);
603 /* 1. give away machine pages. */
604 for (i = 0; i < (1 << order); i++) {
606 pfn = VM_PAGE_TO_PHYS(&pages[i]) >> PAGE_SHIFT;
608 PFNTOMFN(pfn) = INVALID_P2M_ENTRY;
609 PANIC_IF(HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation) != 1);
613 /* 2. Get a new contiguous memory extent. */
614 reservation.extent_order = order;
615 /* xenlinux hardcodes this because of aacraid - maybe set to 0 if we're not
616 * running with a broxen driver XXXEN
618 reservation.address_bits = 31;
619 if (HYPERVISOR_memory_op(XENMEM_increase_reservation, &reservation) != 1)
622 /* 3. Map the new extent in place of old pages. */
623 for (i = 0; i < (1 << order); i++) {
625 pfn = VM_PAGE_TO_PHYS(&pages[i]) >> PAGE_SHIFT;
626 xen_machphys_update(mfn+i, pfn);
627 PFNTOMFN(pfn) = mfn+i;
633 contiguous_bitmap_set(VM_PAGE_TO_PHYS(&pages[0]) >> PAGE_SHIFT, 1UL << order);
636 balloon_unlock(flags);
641 reservation.extent_order = 0;
642 reservation.address_bits = 0;
644 for (i = 0; i < (1 << order); i++) {
646 pfn = VM_PAGE_TO_PHYS(&pages[i]) >> PAGE_SHIFT;
647 PANIC_IF(HYPERVISOR_memory_op(
648 XENMEM_increase_reservation, &reservation) != 1);
649 xen_machphys_update(mfn, pfn);
655 balloon_unlock(flags);
661 xen_destroy_contiguous_region(void *addr, int npages)
663 unsigned long mfn, i, flags, order, pfn0;
664 struct xen_memory_reservation reservation = {
669 set_xen_guest_handle(reservation.extent_start, &mfn);
671 pfn0 = vtophys(addr) >> PAGE_SHIFT;
673 scrub_pages(vstart, 1 << order);
675 /* can currently only handle power of two allocation */
676 PANIC_IF(ffs(npages) != fls(npages));
678 /* 0. determine order */
679 order = (ffs(npages) == fls(npages)) ? fls(npages) - 1 : fls(npages);
684 contiguous_bitmap_clear(vtophys(addr) >> PAGE_SHIFT, 1UL << order);
687 /* 1. Zap current PTEs, giving away the underlying pages. */
688 for (i = 0; i < (1 << order); i++) {
690 uint64_t new_val = 0;
691 pfn = vtomach((char *)addr + i*PAGE_SIZE) >> PAGE_SHIFT;
693 PANIC_IF(HYPERVISOR_update_va_mapping((vm_offset_t)((char *)addr + (i * PAGE_SIZE)), new_val, 0));
694 PFNTOMFN(pfn) = INVALID_P2M_ENTRY;
695 PANIC_IF(HYPERVISOR_memory_op(
696 XENMEM_decrease_reservation, &reservation) != 1);
699 /* 2. Map new pages in place of old pages. */
700 for (i = 0; i < (1 << order); i++) {
704 PANIC_IF(HYPERVISOR_memory_op(XENMEM_increase_reservation, &reservation) != 1);
706 new_val = mfn << PAGE_SHIFT;
707 PANIC_IF(HYPERVISOR_update_va_mapping((vm_offset_t)addr + (i * PAGE_SIZE),
708 new_val, PG_KERNEL));
709 xen_machphys_update(mfn, pfn);
715 balloon_unlock(flags);
718 extern unsigned long cpu0prvpage;
719 extern unsigned long *SMPpt;
720 extern struct user *proc0uarea;
721 extern vm_offset_t proc0kstack;
722 extern int vm86paddr, vm86phystk;
723 char *bootmem_start, *bootmem_current, *bootmem_end;
725 pteinfo_t *pteinfo_list;
726 void initvalues(start_info_t *startinfo);
728 struct ringbuf_head *xen_store; /* XXX move me */
732 bootmem_alloc(unsigned int size)
736 retptr = bootmem_current;
737 PANIC_IF(retptr + size > bootmem_end);
738 bootmem_current += size;
744 bootmem_free(void *ptr, unsigned int size)
749 PANIC_IF(tptr != bootmem_current - size ||
750 bootmem_current - size < bootmem_start);
752 bootmem_current -= size;
757 xpmap_mtop2(vm_paddr_t mpa)
759 return ((machine_to_phys_mapping[mpa >> PAGE_SHIFT] << PAGE_SHIFT)
760 ) | (mpa & ~PG_FRAME);
764 xpmap_get_bootpde(vm_paddr_t va)
767 return ((pd_entry_t *)xen_start_info->pt_base)[va >> 22];
771 xpmap_get_vbootpde(vm_paddr_t va)
775 pde = xpmap_get_bootpde(va);
776 if ((pde & PG_V) == 0)
777 return (pde & ~PG_FRAME);
778 return (pde & ~PG_FRAME) |
779 (xpmap_mtop2(pde & PG_FRAME) + KERNBASE);
783 xpmap_get_bootptep(vm_paddr_t va)
787 pde = xpmap_get_vbootpde(va);
788 if ((pde & PG_V) == 0)
790 #define PT_MASK 0x003ff000 /* page table address bits */
791 return &(((pt_entry_t *)(pde & PG_FRAME))[(va & PT_MASK) >> PAGE_SHIFT]);
795 xpmap_get_bootpte(vm_paddr_t va)
798 return xpmap_get_bootptep(va)[0];
805 shift_phys_machine(unsigned long *phys_machine, int nr_pages)
808 unsigned long *tmp_page, *current_page, *next_page;
811 tmp_page = bootmem_alloc(PAGE_SIZE);
812 current_page = phys_machine + nr_pages - (PAGE_SIZE/sizeof(unsigned long));
813 next_page = current_page - (PAGE_SIZE/sizeof(unsigned long));
814 bcopy(phys_machine, tmp_page, PAGE_SIZE);
816 while (current_page > phys_machine) {
818 bcopy(next_page, tmp_page, PAGE_SIZE);
819 /* shift down page */
820 bcopy(current_page, next_page, PAGE_SIZE);
822 bcopy(tmp_page, current_page, PAGE_SIZE);
824 current_page -= (PAGE_SIZE/sizeof(unsigned long));
825 next_page -= (PAGE_SIZE/sizeof(unsigned long));
827 bootmem_free(tmp_page, PAGE_SIZE);
829 for (i = 0; i < nr_pages; i++) {
830 xen_machphys_update(phys_machine[i], i);
832 memset(phys_machine, INVALID_P2M_ENTRY, PAGE_SIZE);
835 #endif /* ADD_ISA_HOLE */
838 * Build a directory of the pages that make up our Physical to Machine
839 * mapping table. The Xen suspend/restore code uses this to find our
843 init_frame_list_list(void *arg)
845 unsigned long nr_pages = xen_start_info->nr_pages;
846 #define FPP (PAGE_SIZE/sizeof(xen_pfn_t))
849 xen_pfn_to_mfn_frame_list_list = malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK);
850 for (i = 0, j = 0, k = -1; i < nr_pages;
852 if ((j & (FPP - 1)) == 0) {
854 xen_pfn_to_mfn_frame_list[k] =
855 malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK);
856 xen_pfn_to_mfn_frame_list_list[k] =
857 VTOMFN(xen_pfn_to_mfn_frame_list[k]);
860 xen_pfn_to_mfn_frame_list[k][j] =
861 VTOMFN(&xen_phys_machine[i]);
864 HYPERVISOR_shared_info->arch.max_pfn = nr_pages;
865 HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list
866 = VTOMFN(xen_pfn_to_mfn_frame_list_list);
868 SYSINIT(init_fll, SI_SUB_DEVFS, SI_ORDER_ANY, init_frame_list_list, NULL);
870 extern unsigned long physfree;
876 initvalues(start_info_t *startinfo)
878 int l3_pages, l2_pages, l1_pages, offset;
879 vm_offset_t cur_space, cur_space_pt;
880 struct physdev_set_iopl set_iopl;
882 vm_paddr_t KPTphys, IdlePTDma;
883 vm_paddr_t console_page_ma, xen_store_ma;
884 vm_offset_t KPTphysoff, tmpva;
887 vm_paddr_t IdlePDPTma, IdlePDPTnewma;
888 vm_paddr_t IdlePTDnewma[4];
889 pd_entry_t *IdlePDPTnew, *IdlePTDnew;
891 vm_paddr_t pdir_shadow_ma;
898 max((startinfo->nr_pages >> NPGPTD_SHIFT), nkpt),
899 NPGPTD*NPDEPG - KPTDI),
900 (HYPERVISOR_VIRT_START - KERNBASE) >> PDRSHIFT);
902 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments);
905 * need to install handler
907 HYPERVISOR_vm_assist(VMASST_CMD_enable, VMASST_TYPE_4gb_segments_notify);
909 xen_start_info = startinfo;
910 xen_phys_machine = (xen_pfn_t *)startinfo->mfn_list;
912 IdlePTD = (pd_entry_t *)((uint8_t *)startinfo->pt_base + PAGE_SIZE);
918 IdlePDPT = (pd_entry_t *)startinfo->pt_base;
919 IdlePDPTma = xpmap_ptom(VTOP(startinfo->pt_base));
920 for (i = (KERNBASE >> 30);
921 (i < 4) && (IdlePDPT[i] != 0); i++)
924 * Note that only one page directory has been allocated at this point.
928 for (i = 0; i < l2_pages; i++)
929 IdlePTDma[i] = xpmap_ptom(VTOP(IdlePTD + i*PAGE_SIZE));
932 l2_pages = (l2_pages == 0) ? 1 : l2_pages;
937 for (i = (((KERNBASE>>18) & PAGE_MASK)>>PAGE_SHIFT);
938 (i<l2_pages*NPDEPG) && (i<(VM_MAX_KERNEL_ADDRESS>>PDRSHIFT)); i++) {
945 /* number of pages allocated after the pts + 1*/;
946 cur_space = xen_start_info->pt_base +
947 ((xen_start_info->nr_pt_frames) + 3 )*PAGE_SIZE;
948 printk("initvalues(): wooh - availmem=%x,%x\n", avail_space, cur_space);
950 printk("KERNBASE=%x,pt_base=%x, VTOPFN(base)=%x, nr_pt_frames=%x\n",
951 KERNBASE,xen_start_info->pt_base, VTOPFN(xen_start_info->pt_base),
952 xen_start_info->nr_pt_frames);
953 xendebug_flags = 0; /* 0xffffffff; */
955 /* allocate 4 pages for bootmem allocator */
956 bootmem_start = bootmem_current = (char *)cur_space;
957 cur_space += (4 * PAGE_SIZE);
958 bootmem_end = (char *)cur_space;
960 /* allocate page for gdt */
961 gdt = (union descriptor *)cur_space;
962 cur_space += PAGE_SIZE*ncpus;
964 /* allocate page for ldt */
965 ldt = (union descriptor *)cur_space; cur_space += PAGE_SIZE;
966 cur_space += PAGE_SIZE;
968 HYPERVISOR_shared_info = (shared_info_t *)cur_space;
969 cur_space += PAGE_SIZE;
971 xen_store = (struct ringbuf_head *)cur_space;
972 cur_space += PAGE_SIZE;
974 console_page = (char *)cur_space;
975 cur_space += PAGE_SIZE;
978 shift_phys_machine(xen_phys_machine, xen_start_info->nr_pages);
981 * pre-zero unused mapped pages - mapped on 4MB boundary
984 IdlePDPT = (pd_entry_t *)startinfo->pt_base;
985 IdlePDPTma = xpmap_ptom(VTOP(startinfo->pt_base));
987 * Note that only one page directory has been allocated at this point.
990 IdlePTD = (pd_entry_t *)((uint8_t *)startinfo->pt_base + PAGE_SIZE);
991 IdlePTDma = xpmap_ptom(VTOP(IdlePTD));
994 IdlePTD = (pd_entry_t *)startinfo->pt_base;
995 IdlePTDma = xpmap_ptom(VTOP(startinfo->pt_base));
999 l1_pages = xen_start_info->nr_pt_frames - l2_pages - l3_pages;
1001 KPTphysoff = (l2_pages + l3_pages)*PAGE_SIZE;
1003 KPTphys = xpmap_ptom(VTOP(startinfo->pt_base + KPTphysoff));
1004 XENPRINTF("IdlePTD %p\n", IdlePTD);
1005 XENPRINTF("nr_pages: %ld shared_info: 0x%lx flags: 0x%lx pt_base: 0x%lx "
1006 "mod_start: 0x%lx mod_len: 0x%lx\n",
1007 xen_start_info->nr_pages, xen_start_info->shared_info,
1008 xen_start_info->flags, xen_start_info->pt_base,
1009 xen_start_info->mod_start, xen_start_info->mod_len);
1010 /* Map proc0's KSTACK */
1012 proc0kstack = cur_space; cur_space += (KSTACK_PAGES * PAGE_SIZE);
1013 printk("proc0kstack=%u\n", proc0kstack);
1015 /* vm86/bios stack */
1016 cur_space += PAGE_SIZE;
1018 /* Map space for the vm86 region */
1019 vm86paddr = (vm_offset_t)cur_space;
1020 cur_space += (PAGE_SIZE * 3);
1023 IdlePDPTnew = (pd_entry_t *)cur_space; cur_space += PAGE_SIZE;
1024 bzero(IdlePDPTnew, PAGE_SIZE);
1026 IdlePDPTnewma = xpmap_ptom(VTOP(IdlePDPTnew));
1027 IdlePTDnew = (pd_entry_t *)cur_space; cur_space += 4*PAGE_SIZE;
1028 bzero(IdlePTDnew, 4*PAGE_SIZE);
1030 for (i = 0; i < 4; i++)
1032 xpmap_ptom(VTOP((uint8_t *)IdlePTDnew + i*PAGE_SIZE));
1036 * Copy the 4 machine addresses of the new PTDs in to the PDPT
1039 for (i = 0; i < 4; i++)
1040 IdlePDPTnew[i] = IdlePTDnewma[i] | PG_V;
1045 * re-map the new PDPT read-only
1047 PT_SET_MA(IdlePDPTnew, IdlePDPTnewma | PG_V);
1050 * Unpin the current PDPT
1052 xen_pt_unpin(IdlePDPTma);
1054 for (i = 0; i < 20; i++) {
1055 int startidx = ((KERNBASE >> 18) & PAGE_MASK) >> 3;
1057 if (IdlePTD[startidx + i] == 0) {
1065 /* unmap remaining pages from initial 4MB chunk
1068 for (tmpva = cur_space; (tmpva & ((1<<22)-1)) != 0; tmpva += PAGE_SIZE) {
1069 bzero((char *)tmpva, PAGE_SIZE);
1070 PT_SET_MA(tmpva, (vm_paddr_t)0);
1075 memcpy(((uint8_t *)IdlePTDnew) + ((unsigned int)(KERNBASE >> 18)),
1076 ((uint8_t *)IdlePTD) + ((KERNBASE >> 18) & PAGE_MASK),
1077 l1_pages*sizeof(pt_entry_t));
1079 for (i = 0; i < 4; i++) {
1080 PT_SET_MA((uint8_t *)IdlePTDnew + i*PAGE_SIZE,
1081 IdlePTDnewma[i] | PG_V);
1083 xen_load_cr3(VTOP(IdlePDPTnew));
1084 xen_pgdpt_pin(xpmap_ptom(VTOP(IdlePDPTnew)));
1086 /* allocate remainder of nkpt pages */
1087 cur_space_pt = cur_space;
1088 for (offset = (KERNBASE >> PDRSHIFT), i = l1_pages; i < nkpt;
1089 i++, cur_space += PAGE_SIZE) {
1090 pdir = (offset + i) / NPDEPG;
1091 curoffset = ((offset + i) % NPDEPG);
1092 if (((offset + i) << PDRSHIFT) == VM_MAX_KERNEL_ADDRESS)
1096 * make sure that all the initial page table pages
1099 PT_SET_MA(cur_space_pt,
1100 xpmap_ptom(VTOP(cur_space)) | PG_V | PG_RW);
1101 bzero((char *)cur_space_pt, PAGE_SIZE);
1102 PT_SET_MA(cur_space_pt, (vm_paddr_t)0);
1103 xen_pt_pin(xpmap_ptom(VTOP(cur_space)));
1104 xen_queue_pt_update((vm_paddr_t)(IdlePTDnewma[pdir] +
1105 curoffset*sizeof(vm_paddr_t)),
1106 xpmap_ptom(VTOP(cur_space)) | PG_KERNEL);
1110 for (i = 0; i < 4; i++) {
1111 pdir = (PTDPTDI + i) / NPDEPG;
1112 curoffset = (PTDPTDI + i) % NPDEPG;
1114 xen_queue_pt_update((vm_paddr_t)(IdlePTDnewma[pdir] +
1115 curoffset*sizeof(vm_paddr_t)),
1116 IdlePTDnewma[i] | PG_V);
1121 IdlePTD = IdlePTDnew;
1122 IdlePDPT = IdlePDPTnew;
1123 IdlePDPTma = IdlePDPTnewma;
1126 * shared_info is an unsigned long so this will randomly break if
1127 * it is allocated above 4GB - I guess people are used to that
1128 * sort of thing with Xen ... sigh
1130 shinfo = xen_start_info->shared_info;
1131 PT_SET_MA(HYPERVISOR_shared_info, shinfo | PG_KERNEL);
1135 xen_store_ma = (((vm_paddr_t)xen_start_info->store_mfn) << PAGE_SHIFT);
1136 PT_SET_MA(xen_store, xen_store_ma | PG_KERNEL);
1137 console_page_ma = (((vm_paddr_t)xen_start_info->console.domU.mfn) << PAGE_SHIFT);
1138 PT_SET_MA(console_page, console_page_ma | PG_KERNEL);
1143 PANIC_IF(HYPERVISOR_physdev_op(PHYSDEVOP_SET_IOPL, &set_iopl));
1146 /* add page table for KERNBASE */
1147 xen_queue_pt_update(IdlePTDma + KPTDI*sizeof(vm_paddr_t),
1148 xpmap_ptom(VTOP(cur_space) | PG_KERNEL));
1151 xen_queue_pt_update(pdir_shadow_ma[3] + KPTDI*sizeof(vm_paddr_t),
1152 xpmap_ptom(VTOP(cur_space) | PG_V | PG_A));
1154 xen_queue_pt_update(pdir_shadow_ma + KPTDI*sizeof(vm_paddr_t),
1155 xpmap_ptom(VTOP(cur_space) | PG_V | PG_A));
1158 cur_space += PAGE_SIZE;
1162 if (xen_start_info->flags & SIF_INITDOMAIN) {
1163 /* Map first megabyte */
1164 for (i = 0; i < (256 << PAGE_SHIFT); i += PAGE_SIZE)
1165 PT_SET_MA(KERNBASE + i, i | PG_KERNEL | PG_NC_PCD);
1170 * re-map kernel text read-only
1173 for (i = (((vm_offset_t)&btext) & ~PAGE_MASK);
1174 i < (((vm_offset_t)&etext) & ~PAGE_MASK); i += PAGE_SIZE)
1175 PT_SET_MA(i, xpmap_ptom(VTOP(i)) | PG_V | PG_A);
1178 physfree = VTOP(cur_space);
1179 init_first = physfree >> PAGE_SHIFT;
1180 IdlePTD = (pd_entry_t *)VTOP(IdlePTD);
1181 IdlePDPT = (pd_entry_t *)VTOP(IdlePDPT);
1182 setup_xen_features();
1183 printk("#8, proc0kstack=%u\n", proc0kstack);
1187 trap_info_t trap_table[] = {
1188 { 0, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(div)},
1189 { 1, 0|4, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(dbg)},
1190 { 3, 3|4, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(bpt)},
1191 { 4, 3, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(ofl)},
1192 /* This is UPL on Linux and KPL on BSD */
1193 { 5, 3, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(bnd)},
1194 { 6, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(ill)},
1195 { 7, 0|4, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(dna)},
1197 * { 8, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(XXX)},
1198 * no handler for double fault
1200 { 9, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(fpusegm)},
1201 {10, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(tss)},
1202 {11, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(missing)},
1203 {12, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(stk)},
1204 {13, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(prot)},
1205 {14, 0|4, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(page)},
1206 {15, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(rsvd)},
1207 {16, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(fpu)},
1208 {17, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(align)},
1209 {18, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(mchk)},
1210 {19, 0, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(xmm)},
1211 {0x80, 3, GSEL(GCODE_SEL, SEL_KPL), (unsigned long) &IDTVEC(int0x80_syscall)},
1216 /********** CODE WORTH KEEPING ABOVE HERE *****************/
1218 void xen_failsafe_handler(void);
1221 xen_failsafe_handler(void)
1224 panic("xen_failsafe_handler called!\n");
1227 void xen_handle_thread_switch(struct pcb *pcb);
1229 /* This is called by cpu_switch() when switching threads. */
1230 /* The pcb arg refers to the process control block of the */
1231 /* next thread which is to run */
1233 xen_handle_thread_switch(struct pcb *pcb)
1235 uint32_t *a = (uint32_t *)&PCPU_GET(fsgs_gdt)[0];
1236 uint32_t *b = (uint32_t *)&pcb->pcb_fsd;
1237 multicall_entry_t mcl[3];
1240 /* Notify Xen of task switch */
1241 mcl[i].op = __HYPERVISOR_stack_switch;
1242 mcl[i].args[0] = GSEL(GDATA_SEL, SEL_KPL);
1243 mcl[i++].args[1] = (unsigned long)pcb;
1245 /* Check for update of fsd */
1246 if (*a != *b || *(a+1) != *(b+1)) {
1247 mcl[i].op = __HYPERVISOR_update_descriptor;
1248 *(uint64_t *)&mcl[i].args[0] = vtomach((vm_offset_t)a);
1249 *(uint64_t *)&mcl[i++].args[2] = *(uint64_t *)b;
1255 /* Check for update of gsd */
1256 if (*a != *b || *(a+1) != *(b+1)) {
1257 mcl[i].op = __HYPERVISOR_update_descriptor;
1258 *(uint64_t *)&mcl[i].args[0] = vtomach((vm_offset_t)a);
1259 *(uint64_t *)&mcl[i++].args[2] = *(uint64_t *)b;
1262 (void)HYPERVISOR_multicall(mcl, i);