2 * Core of Xen paravirt_ops implementation.
4 * This file contains the xen_paravirt_ops structure itself, and the
6 * - privileged instructions
11 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/smp.h>
17 #include <linux/preempt.h>
18 #include <linux/hardirq.h>
19 #include <linux/percpu.h>
20 #include <linux/delay.h>
21 #include <linux/start_kernel.h>
22 #include <linux/sched.h>
23 #include <linux/bootmem.h>
24 #include <linux/module.h>
26 #include <linux/page-flags.h>
27 #include <linux/highmem.h>
29 #include <xen/interface/xen.h>
30 #include <xen/interface/physdev.h>
31 #include <xen/interface/vcpu.h>
32 #include <xen/interface/sched.h>
33 #include <xen/features.h>
36 #include <asm/paravirt.h>
38 #include <asm/xen/hypercall.h>
39 #include <asm/xen/hypervisor.h>
40 #include <asm/fixmap.h>
41 #include <asm/processor.h>
42 #include <asm/setup.h>
44 #include <asm/pgtable.h>
45 #include <asm/tlbflush.h>
46 #include <asm/reboot.h>
50 #include "multicalls.h"
52 EXPORT_SYMBOL_GPL(hypercall_page);
54 DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
55 DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
56 DEFINE_PER_CPU(unsigned long, xen_cr3);
58 struct start_info *xen_start_info;
59 EXPORT_SYMBOL_GPL(xen_start_info);
61 static /* __initdata */ struct shared_info dummy_shared_info;
64 * Point at some empty memory to start with. We map the real shared_info
65 * page as soon as fixmap is up and running.
67 struct shared_info *HYPERVISOR_shared_info = (void *)&dummy_shared_info;
70 * Flag to determine whether vcpu info placement is available on all
71 * VCPUs. We assume it is to start with, and then set it to zero on
72 * the first failure. This is because it can succeed on some VCPUs
73 * and not others, since it can involve hypervisor memory allocation,
74 * or because the guest failed to guarantee all the appropriate
75 * constraints on all VCPUs (ie buffer can't cross a page boundary).
77 * Note that any particular CPU may be using a placed vcpu structure,
78 * but we can only optimise if the all are.
80 * 0: not available, 1: available
82 static int have_vcpu_info_placement = 1;
84 static void __init xen_vcpu_setup(int cpu)
86 struct vcpu_register_vcpu_info info;
88 struct vcpu_info *vcpup;
90 per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
92 if (!have_vcpu_info_placement)
93 return; /* already tested, not available */
95 vcpup = &per_cpu(xen_vcpu_info, cpu);
97 info.mfn = virt_to_mfn(vcpup);
98 info.offset = offset_in_page(vcpup);
100 printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %x, offset %d\n",
101 cpu, vcpup, info.mfn, info.offset);
103 /* Check to see if the hypervisor will put the vcpu_info
104 structure where we want it, which allows direct access via
105 a percpu-variable. */
106 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
109 printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
110 have_vcpu_info_placement = 0;
112 /* This cpu is using the registered vcpu info, even if
113 later ones fail to. */
114 per_cpu(xen_vcpu, cpu) = vcpup;
116 printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
121 static void __init xen_banner(void)
123 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
125 printk(KERN_INFO "Hypervisor signature: %s\n", xen_start_info->magic);
128 static void xen_cpuid(unsigned int *eax, unsigned int *ebx,
129 unsigned int *ecx, unsigned int *edx)
131 unsigned maskedx = ~0;
134 * Mask out inconvenient features, to try and disable as many
135 * unsupported kernel subsystems as possible.
138 maskedx = ~((1 << X86_FEATURE_APIC) | /* disable APIC */
139 (1 << X86_FEATURE_ACPI) | /* disable ACPI */
140 (1 << X86_FEATURE_ACC)); /* thermal monitoring */
142 asm(XEN_EMULATE_PREFIX "cpuid"
147 : "0" (*eax), "2" (*ecx));
151 static void xen_set_debugreg(int reg, unsigned long val)
153 HYPERVISOR_set_debugreg(reg, val);
156 static unsigned long xen_get_debugreg(int reg)
158 return HYPERVISOR_get_debugreg(reg);
161 static unsigned long xen_save_fl(void)
163 struct vcpu_info *vcpu;
166 vcpu = x86_read_percpu(xen_vcpu);
168 /* flag has opposite sense of mask */
169 flags = !vcpu->evtchn_upcall_mask;
171 /* convert to IF type flag
175 return (-flags) & X86_EFLAGS_IF;
178 static void xen_restore_fl(unsigned long flags)
180 struct vcpu_info *vcpu;
182 /* convert from IF type flag */
183 flags = !(flags & X86_EFLAGS_IF);
185 /* There's a one instruction preempt window here. We need to
186 make sure we're don't switch CPUs between getting the vcpu
187 pointer and updating the mask. */
189 vcpu = x86_read_percpu(xen_vcpu);
190 vcpu->evtchn_upcall_mask = flags;
191 preempt_enable_no_resched();
193 /* Doesn't matter if we get preempted here, because any
194 pending event will get dealt with anyway. */
197 preempt_check_resched();
198 barrier(); /* unmask then check (avoid races) */
199 if (unlikely(vcpu->evtchn_upcall_pending))
200 force_evtchn_callback();
204 static void xen_irq_disable(void)
206 /* There's a one instruction preempt window here. We need to
207 make sure we're don't switch CPUs between getting the vcpu
208 pointer and updating the mask. */
210 x86_read_percpu(xen_vcpu)->evtchn_upcall_mask = 1;
211 preempt_enable_no_resched();
214 static void xen_irq_enable(void)
216 struct vcpu_info *vcpu;
218 /* There's a one instruction preempt window here. We need to
219 make sure we're don't switch CPUs between getting the vcpu
220 pointer and updating the mask. */
222 vcpu = x86_read_percpu(xen_vcpu);
223 vcpu->evtchn_upcall_mask = 0;
224 preempt_enable_no_resched();
226 /* Doesn't matter if we get preempted here, because any
227 pending event will get dealt with anyway. */
229 barrier(); /* unmask then check (avoid races) */
230 if (unlikely(vcpu->evtchn_upcall_pending))
231 force_evtchn_callback();
234 static void xen_safe_halt(void)
236 /* Blocking includes an implicit local_irq_enable(). */
237 if (HYPERVISOR_sched_op(SCHEDOP_block, 0) != 0)
241 static void xen_halt(void)
244 HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
249 static void xen_leave_lazy(void)
251 paravirt_leave_lazy(paravirt_get_lazy_mode());
255 static unsigned long xen_store_tr(void)
260 static void xen_set_ldt(const void *addr, unsigned entries)
262 unsigned long linear_addr = (unsigned long)addr;
263 struct mmuext_op *op;
264 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
267 op->cmd = MMUEXT_SET_LDT;
269 /* ldt my be vmalloced, use arbitrary_virt_to_machine */
271 maddr = arbitrary_virt_to_machine((unsigned long)addr);
272 linear_addr = (unsigned long)maddr.maddr;
274 op->arg1.linear_addr = linear_addr;
275 op->arg2.nr_ents = entries;
277 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
279 xen_mc_issue(PARAVIRT_LAZY_CPU);
282 static void xen_load_gdt(const struct Xgt_desc_struct *dtr)
284 unsigned long *frames;
285 unsigned long va = dtr->address;
286 unsigned int size = dtr->size + 1;
287 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
289 struct multicall_space mcs;
291 /* A GDT can be up to 64k in size, which corresponds to 8192
292 8-byte entries, or 16 4k pages.. */
294 BUG_ON(size > 65536);
295 BUG_ON(va & ~PAGE_MASK);
297 mcs = xen_mc_entry(sizeof(*frames) * pages);
300 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
301 frames[f] = virt_to_mfn(va);
302 make_lowmem_page_readonly((void *)va);
305 MULTI_set_gdt(mcs.mc, frames, size / sizeof(struct desc_struct));
307 xen_mc_issue(PARAVIRT_LAZY_CPU);
310 static void load_TLS_descriptor(struct thread_struct *t,
311 unsigned int cpu, unsigned int i)
313 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
314 xmaddr_t maddr = virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
315 struct multicall_space mc = __xen_mc_entry(0);
317 MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
320 static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
324 load_TLS_descriptor(t, cpu, 0);
325 load_TLS_descriptor(t, cpu, 1);
326 load_TLS_descriptor(t, cpu, 2);
328 xen_mc_issue(PARAVIRT_LAZY_CPU);
331 * XXX sleazy hack: If we're being called in a lazy-cpu zone,
332 * it means we're in a context switch, and %gs has just been
333 * saved. This means we can zero it out to prevent faults on
334 * exit from the hypervisor if the next process has no %gs.
335 * Either way, it has been saved, and the new value will get
336 * loaded properly. This will go away as soon as Xen has been
337 * modified to not save/restore %gs for normal hypercalls.
339 if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU)
343 static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
346 unsigned long lp = (unsigned long)&dt[entrynum];
347 xmaddr_t mach_lp = virt_to_machine(lp);
348 u64 entry = (u64)high << 32 | low;
353 if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
359 static int cvt_gate_to_trap(int vector, u32 low, u32 high,
360 struct trap_info *info)
364 type = (high >> 8) & 0x1f;
365 dpl = (high >> 13) & 3;
367 if (type != 0xf && type != 0xe)
370 info->vector = vector;
371 info->address = (high & 0xffff0000) | (low & 0x0000ffff);
372 info->cs = low >> 16;
374 /* interrupt gates clear IF */
381 /* Locations of each CPU's IDT */
382 static DEFINE_PER_CPU(struct Xgt_desc_struct, idt_desc);
384 /* Set an IDT entry. If the entry is part of the current IDT, then
386 static void xen_write_idt_entry(struct desc_struct *dt, int entrynum,
389 unsigned long p = (unsigned long)&dt[entrynum];
390 unsigned long start, end;
394 start = __get_cpu_var(idt_desc).address;
395 end = start + __get_cpu_var(idt_desc).size + 1;
399 write_dt_entry(dt, entrynum, low, high);
401 if (p >= start && (p + 8) <= end) {
402 struct trap_info info[2];
406 if (cvt_gate_to_trap(entrynum, low, high, &info[0]))
407 if (HYPERVISOR_set_trap_table(info))
414 static void xen_convert_trap_info(const struct Xgt_desc_struct *desc,
415 struct trap_info *traps)
417 unsigned in, out, count;
419 count = (desc->size+1) / 8;
422 for (in = out = 0; in < count; in++) {
423 const u32 *entry = (u32 *)(desc->address + in * 8);
425 if (cvt_gate_to_trap(in, entry[0], entry[1], &traps[out]))
428 traps[out].address = 0;
431 void xen_copy_trap_info(struct trap_info *traps)
433 const struct Xgt_desc_struct *desc = &__get_cpu_var(idt_desc);
435 xen_convert_trap_info(desc, traps);
438 /* Load a new IDT into Xen. In principle this can be per-CPU, so we
439 hold a spinlock to protect the static traps[] array (static because
440 it avoids allocation, and saves stack space). */
441 static void xen_load_idt(const struct Xgt_desc_struct *desc)
443 static DEFINE_SPINLOCK(lock);
444 static struct trap_info traps[257];
448 __get_cpu_var(idt_desc) = *desc;
450 xen_convert_trap_info(desc, traps);
453 if (HYPERVISOR_set_trap_table(traps))
459 /* Write a GDT descriptor entry. Ignore LDT descriptors, since
460 they're handled differently. */
461 static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
466 switch ((high >> 8) & 0xff) {
473 xmaddr_t maddr = virt_to_machine(&dt[entry]);
474 u64 desc = (u64)high << 32 | low;
477 if (HYPERVISOR_update_descriptor(maddr.maddr, desc))
486 static void xen_load_esp0(struct tss_struct *tss,
487 struct thread_struct *thread)
489 struct multicall_space mcs = xen_mc_entry(0);
490 MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->esp0);
491 xen_mc_issue(PARAVIRT_LAZY_CPU);
494 static void xen_set_iopl_mask(unsigned mask)
496 struct physdev_set_iopl set_iopl;
498 /* Force the change at ring 0. */
499 set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
500 HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
503 static void xen_io_delay(void)
507 #ifdef CONFIG_X86_LOCAL_APIC
508 static unsigned long xen_apic_read(unsigned long reg)
513 static void xen_apic_write(unsigned long reg, unsigned long val)
515 /* Warn to see if there's any stray references */
520 static void xen_flush_tlb(void)
522 struct mmuext_op *op;
523 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
526 op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
527 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
529 xen_mc_issue(PARAVIRT_LAZY_MMU);
532 static void xen_flush_tlb_single(unsigned long addr)
534 struct mmuext_op *op;
535 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
538 op->cmd = MMUEXT_INVLPG_LOCAL;
539 op->arg1.linear_addr = addr & PAGE_MASK;
540 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
542 xen_mc_issue(PARAVIRT_LAZY_MMU);
545 static void xen_flush_tlb_others(const cpumask_t *cpus, struct mm_struct *mm,
552 cpumask_t cpumask = *cpus;
553 struct multicall_space mcs;
556 * A couple of (to be removed) sanity checks:
558 * - current CPU must not be in mask
559 * - mask must exist :)
561 BUG_ON(cpus_empty(cpumask));
562 BUG_ON(cpu_isset(smp_processor_id(), cpumask));
565 /* If a CPU which we ran on has gone down, OK. */
566 cpus_and(cpumask, cpumask, cpu_online_map);
567 if (cpus_empty(cpumask))
570 mcs = xen_mc_entry(sizeof(*args));
572 args->mask = cpumask;
573 args->op.arg2.vcpumask = &args->mask;
575 if (va == TLB_FLUSH_ALL) {
576 args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
578 args->op.cmd = MMUEXT_INVLPG_MULTI;
579 args->op.arg1.linear_addr = va;
582 MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
584 xen_mc_issue(PARAVIRT_LAZY_MMU);
587 static void xen_write_cr2(unsigned long cr2)
589 x86_read_percpu(xen_vcpu)->arch.cr2 = cr2;
592 static unsigned long xen_read_cr2(void)
594 return x86_read_percpu(xen_vcpu)->arch.cr2;
597 static unsigned long xen_read_cr2_direct(void)
599 return x86_read_percpu(xen_vcpu_info.arch.cr2);
602 static void xen_write_cr4(unsigned long cr4)
604 /* Just ignore cr4 changes; Xen doesn't allow us to do
608 static unsigned long xen_read_cr3(void)
610 return x86_read_percpu(xen_cr3);
613 static void xen_write_cr3(unsigned long cr3)
615 BUG_ON(preemptible());
617 if (cr3 == x86_read_percpu(xen_cr3)) {
618 /* just a simple tlb flush */
623 x86_write_percpu(xen_cr3, cr3);
627 struct mmuext_op *op;
628 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
629 unsigned long mfn = pfn_to_mfn(PFN_DOWN(cr3));
632 op->cmd = MMUEXT_NEW_BASEPTR;
635 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
637 xen_mc_issue(PARAVIRT_LAZY_CPU);
641 /* Early in boot, while setting up the initial pagetable, assume
642 everything is pinned. */
643 static __init void xen_alloc_pt_init(struct mm_struct *mm, u32 pfn)
645 BUG_ON(mem_map); /* should only be used early */
646 make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
649 /* This needs to make sure the new pte page is pinned iff its being
650 attached to a pinned pagetable. */
651 static void xen_alloc_pt(struct mm_struct *mm, u32 pfn)
653 struct page *page = pfn_to_page(pfn);
655 if (PagePinned(virt_to_page(mm->pgd))) {
658 if (!PageHighMem(page))
659 make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
661 /* make sure there are no stray mappings of
667 /* This should never happen until we're OK to use struct page */
668 static void xen_release_pt(u32 pfn)
670 struct page *page = pfn_to_page(pfn);
672 if (PagePinned(page)) {
673 if (!PageHighMem(page))
674 make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
678 #ifdef CONFIG_HIGHPTE
679 static void *xen_kmap_atomic_pte(struct page *page, enum km_type type)
681 pgprot_t prot = PAGE_KERNEL;
683 if (PagePinned(page))
684 prot = PAGE_KERNEL_RO;
686 if (0 && PageHighMem(page))
687 printk("mapping highpte %lx type %d prot %s\n",
688 page_to_pfn(page), type,
689 (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ");
691 return kmap_atomic_prot(page, type, prot);
695 static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
697 /* If there's an existing pte, then don't allow _PAGE_RW to be set */
698 if (pte_val_ma(*ptep) & _PAGE_PRESENT)
699 pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
705 /* Init-time set_pte while constructing initial pagetables, which
706 doesn't allow RO pagetable pages to be remapped RW */
707 static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
709 pte = mask_rw_pte(ptep, pte);
711 xen_set_pte(ptep, pte);
714 static __init void xen_pagetable_setup_start(pgd_t *base)
716 pgd_t *xen_pgd = (pgd_t *)xen_start_info->pt_base;
718 /* special set_pte for pagetable initialization */
719 pv_mmu_ops.set_pte = xen_set_pte_init;
723 * copy top-level of Xen-supplied pagetable into place. For
724 * !PAE we can use this as-is, but for PAE it is a stand-in
725 * while we copy the pmd pages.
727 memcpy(base, xen_pgd, PTRS_PER_PGD * sizeof(pgd_t));
729 if (PTRS_PER_PMD > 1) {
732 * For PAE, need to allocate new pmds, rather than
733 * share Xen's, since Xen doesn't like pmd's being
734 * shared between address spaces.
736 for (i = 0; i < PTRS_PER_PGD; i++) {
737 if (pgd_val_ma(xen_pgd[i]) & _PAGE_PRESENT) {
738 pmd_t *pmd = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
740 memcpy(pmd, (void *)pgd_page_vaddr(xen_pgd[i]),
743 make_lowmem_page_readonly(pmd);
745 set_pgd(&base[i], __pgd(1 + __pa(pmd)));
751 /* make sure zero_page is mapped RO so we can use it in pagetables */
752 make_lowmem_page_readonly(empty_zero_page);
753 make_lowmem_page_readonly(base);
755 * Switch to new pagetable. This is done before
756 * pagetable_init has done anything so that the new pages
757 * added to the table can be prepared properly for Xen.
759 xen_write_cr3(__pa(base));
762 static __init void xen_pagetable_setup_done(pgd_t *base)
764 /* This will work as long as patching hasn't happened yet
766 pv_mmu_ops.alloc_pt = xen_alloc_pt;
767 pv_mmu_ops.set_pte = xen_set_pte;
769 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
771 * Create a mapping for the shared info page.
772 * Should be set_fixmap(), but shared_info is a machine
773 * address with no corresponding pseudo-phys address.
775 set_pte_mfn(fix_to_virt(FIX_PARAVIRT_BOOTMAP),
776 PFN_DOWN(xen_start_info->shared_info),
779 HYPERVISOR_shared_info =
780 (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
783 HYPERVISOR_shared_info =
784 (struct shared_info *)__va(xen_start_info->shared_info);
786 /* Actually pin the pagetable down, but we can't set PG_pinned
787 yet because the page structures don't exist yet. */
790 #ifdef CONFIG_X86_PAE
791 op.cmd = MMUEXT_PIN_L3_TABLE;
793 op.cmd = MMUEXT_PIN_L3_TABLE;
795 op.arg1.mfn = pfn_to_mfn(PFN_DOWN(__pa(base)));
796 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
801 /* This is called once we have the cpu_possible_map */
802 void __init xen_setup_vcpu_info_placement(void)
806 for_each_possible_cpu(cpu)
809 /* xen_vcpu_setup managed to place the vcpu_info within the
810 percpu area for all cpus, so make use of it */
811 if (have_vcpu_info_placement) {
812 printk(KERN_INFO "Xen: using vcpu_info placement\n");
814 pv_irq_ops.save_fl = xen_save_fl_direct;
815 pv_irq_ops.restore_fl = xen_restore_fl_direct;
816 pv_irq_ops.irq_disable = xen_irq_disable_direct;
817 pv_irq_ops.irq_enable = xen_irq_enable_direct;
818 pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
819 pv_cpu_ops.iret = xen_iret_direct;
823 static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
824 unsigned long addr, unsigned len)
826 char *start, *end, *reloc;
829 start = end = reloc = NULL;
831 #define SITE(op, x) \
832 case PARAVIRT_PATCH(op.x): \
833 if (have_vcpu_info_placement) { \
834 start = (char *)xen_##x##_direct; \
835 end = xen_##x##_direct_end; \
836 reloc = xen_##x##_direct_reloc; \
841 SITE(pv_irq_ops, irq_enable);
842 SITE(pv_irq_ops, irq_disable);
843 SITE(pv_irq_ops, save_fl);
844 SITE(pv_irq_ops, restore_fl);
848 if (start == NULL || (end-start) > len)
851 ret = paravirt_patch_insns(insnbuf, len, start, end);
853 /* Note: because reloc is assigned from something that
854 appears to be an array, gcc assumes it's non-null,
855 but doesn't know its relationship with start and
857 if (reloc > start && reloc < end) {
858 int reloc_off = reloc - start;
859 long *relocp = (long *)(insnbuf + reloc_off);
860 long delta = start - (char *)addr;
868 ret = paravirt_patch_default(type, clobbers, insnbuf,
876 static const struct pv_info xen_info __initdata = {
877 .paravirt_enabled = 1,
878 .shared_kernel_pmd = 0,
883 static const struct pv_init_ops xen_init_ops __initdata = {
886 .banner = xen_banner,
887 .memory_setup = xen_memory_setup,
888 .arch_setup = xen_arch_setup,
889 .post_allocator_init = xen_mark_init_mm_pinned,
892 static const struct pv_time_ops xen_time_ops __initdata = {
893 .time_init = xen_time_init,
895 .set_wallclock = xen_set_wallclock,
896 .get_wallclock = xen_get_wallclock,
897 .get_cpu_khz = xen_cpu_khz,
898 .sched_clock = xen_sched_clock,
901 static const struct pv_cpu_ops xen_cpu_ops __initdata = {
904 .set_debugreg = xen_set_debugreg,
905 .get_debugreg = xen_get_debugreg,
909 .read_cr0 = native_read_cr0,
910 .write_cr0 = native_write_cr0,
912 .read_cr4 = native_read_cr4,
913 .read_cr4_safe = native_read_cr4_safe,
914 .write_cr4 = xen_write_cr4,
916 .wbinvd = native_wbinvd,
918 .read_msr = native_read_msr_safe,
919 .write_msr = native_write_msr_safe,
920 .read_tsc = native_read_tsc,
921 .read_pmc = native_read_pmc,
923 .iret = (void *)&hypercall_page[__HYPERVISOR_iret],
924 .irq_enable_sysexit = NULL, /* never called */
926 .load_tr_desc = paravirt_nop,
927 .set_ldt = xen_set_ldt,
928 .load_gdt = xen_load_gdt,
929 .load_idt = xen_load_idt,
930 .load_tls = xen_load_tls,
932 .store_gdt = native_store_gdt,
933 .store_idt = native_store_idt,
934 .store_tr = xen_store_tr,
936 .write_ldt_entry = xen_write_ldt_entry,
937 .write_gdt_entry = xen_write_gdt_entry,
938 .write_idt_entry = xen_write_idt_entry,
939 .load_esp0 = xen_load_esp0,
941 .set_iopl_mask = xen_set_iopl_mask,
942 .io_delay = xen_io_delay,
945 .enter = paravirt_enter_lazy_cpu,
946 .leave = xen_leave_lazy,
950 static const struct pv_irq_ops xen_irq_ops __initdata = {
951 .init_IRQ = xen_init_IRQ,
952 .save_fl = xen_save_fl,
953 .restore_fl = xen_restore_fl,
954 .irq_disable = xen_irq_disable,
955 .irq_enable = xen_irq_enable,
956 .safe_halt = xen_safe_halt,
960 static const struct pv_apic_ops xen_apic_ops __initdata = {
961 #ifdef CONFIG_X86_LOCAL_APIC
962 .apic_write = xen_apic_write,
963 .apic_write_atomic = xen_apic_write,
964 .apic_read = xen_apic_read,
965 .setup_boot_clock = paravirt_nop,
966 .setup_secondary_clock = paravirt_nop,
967 .startup_ipi_hook = paravirt_nop,
971 static const struct pv_mmu_ops xen_mmu_ops __initdata = {
972 .pagetable_setup_start = xen_pagetable_setup_start,
973 .pagetable_setup_done = xen_pagetable_setup_done,
975 .read_cr2 = xen_read_cr2,
976 .write_cr2 = xen_write_cr2,
978 .read_cr3 = xen_read_cr3,
979 .write_cr3 = xen_write_cr3,
981 .flush_tlb_user = xen_flush_tlb,
982 .flush_tlb_kernel = xen_flush_tlb,
983 .flush_tlb_single = xen_flush_tlb_single,
984 .flush_tlb_others = xen_flush_tlb_others,
986 .pte_update = paravirt_nop,
987 .pte_update_defer = paravirt_nop,
989 .alloc_pt = xen_alloc_pt_init,
990 .release_pt = xen_release_pt,
991 .alloc_pd = paravirt_nop,
992 .alloc_pd_clone = paravirt_nop,
993 .release_pd = paravirt_nop,
995 #ifdef CONFIG_HIGHPTE
996 .kmap_atomic_pte = xen_kmap_atomic_pte,
999 .set_pte = NULL, /* see xen_pagetable_setup_* */
1000 .set_pte_at = xen_set_pte_at,
1001 .set_pmd = xen_set_pmd,
1003 .pte_val = xen_pte_val,
1004 .pgd_val = xen_pgd_val,
1006 .make_pte = xen_make_pte,
1007 .make_pgd = xen_make_pgd,
1009 #ifdef CONFIG_X86_PAE
1010 .set_pte_atomic = xen_set_pte_atomic,
1011 .set_pte_present = xen_set_pte_at,
1012 .set_pud = xen_set_pud,
1013 .pte_clear = xen_pte_clear,
1014 .pmd_clear = xen_pmd_clear,
1016 .make_pmd = xen_make_pmd,
1017 .pmd_val = xen_pmd_val,
1020 .activate_mm = xen_activate_mm,
1021 .dup_mmap = xen_dup_mmap,
1022 .exit_mmap = xen_exit_mmap,
1025 .enter = paravirt_enter_lazy_mmu,
1026 .leave = xen_leave_lazy,
1031 static const struct smp_ops xen_smp_ops __initdata = {
1032 .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
1033 .smp_prepare_cpus = xen_smp_prepare_cpus,
1034 .cpu_up = xen_cpu_up,
1035 .smp_cpus_done = xen_smp_cpus_done,
1037 .smp_send_stop = xen_smp_send_stop,
1038 .smp_send_reschedule = xen_smp_send_reschedule,
1039 .smp_call_function_mask = xen_smp_call_function_mask,
1041 #endif /* CONFIG_SMP */
1043 static void xen_reboot(int reason)
1049 if (HYPERVISOR_sched_op(SCHEDOP_shutdown, reason))
1053 static void xen_restart(char *msg)
1055 xen_reboot(SHUTDOWN_reboot);
1058 static void xen_emergency_restart(void)
1060 xen_reboot(SHUTDOWN_reboot);
1063 static void xen_machine_halt(void)
1065 xen_reboot(SHUTDOWN_poweroff);
1068 static void xen_crash_shutdown(struct pt_regs *regs)
1070 xen_reboot(SHUTDOWN_crash);
1073 static const struct machine_ops __initdata xen_machine_ops = {
1074 .restart = xen_restart,
1075 .halt = xen_machine_halt,
1076 .power_off = xen_machine_halt,
1077 .shutdown = xen_machine_halt,
1078 .crash_shutdown = xen_crash_shutdown,
1079 .emergency_restart = xen_emergency_restart,
1083 /* First C function to be called on Xen boot */
1084 asmlinkage void __init xen_start_kernel(void)
1088 if (!xen_start_info)
1091 BUG_ON(memcmp(xen_start_info->magic, "xen-3.0", 7) != 0);
1093 /* Install Xen paravirt ops */
1095 pv_init_ops = xen_init_ops;
1096 pv_time_ops = xen_time_ops;
1097 pv_cpu_ops = xen_cpu_ops;
1098 pv_irq_ops = xen_irq_ops;
1099 pv_apic_ops = xen_apic_ops;
1100 pv_mmu_ops = xen_mmu_ops;
1102 machine_ops = xen_machine_ops;
1105 smp_ops = xen_smp_ops;
1108 xen_setup_features();
1111 if (!xen_feature(XENFEAT_auto_translated_physmap))
1112 phys_to_machine_mapping = (unsigned long *)xen_start_info->mfn_list;
1114 pgd = (pgd_t *)xen_start_info->pt_base;
1116 init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE;
1118 init_mm.pgd = pgd; /* use the Xen pagetables to start */
1120 /* keep using Xen gdt for now; no urgent need to change it */
1122 x86_write_percpu(xen_cr3, __pa(pgd));
1125 /* Don't do the full vcpu_info placement stuff until we have a
1127 per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
1129 /* May as well do it now, since there's no good time to call
1131 xen_setup_vcpu_info_placement();
1134 pv_info.kernel_rpl = 1;
1135 if (xen_feature(XENFEAT_supervisor_mode_kernel))
1136 pv_info.kernel_rpl = 0;
1138 /* set the limit of our address space */
1139 reserve_top_address(-HYPERVISOR_VIRT_START + 2 * PAGE_SIZE);
1141 /* set up basic CPUID stuff */
1142 cpu_detect(&new_cpu_data);
1143 new_cpu_data.hard_math = 1;
1144 new_cpu_data.x86_capability[0] = cpuid_edx(1);
1146 /* Poke various useful things into boot_params */
1147 LOADER_TYPE = (9 << 4) | 0;
1148 INITRD_START = xen_start_info->mod_start ? __pa(xen_start_info->mod_start) : 0;
1149 INITRD_SIZE = xen_start_info->mod_len;
1151 /* Start the world */