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/kprobes.h>
24 #include <linux/bootmem.h>
25 #include <linux/module.h>
27 #include <linux/page-flags.h>
28 #include <linux/highmem.h>
29 #include <linux/console.h>
32 #include <xen/interface/xen.h>
33 #include <xen/interface/version.h>
34 #include <xen/interface/physdev.h>
35 #include <xen/interface/vcpu.h>
36 #include <xen/features.h>
38 #include <xen/hvc-console.h>
40 #include <asm/paravirt.h>
43 #include <asm/xen/hypercall.h>
44 #include <asm/xen/hypervisor.h>
45 #include <asm/fixmap.h>
46 #include <asm/processor.h>
47 #include <asm/proto.h>
48 #include <asm/msr-index.h>
49 #include <asm/traps.h>
50 #include <asm/setup.h>
52 #include <asm/pgtable.h>
53 #include <asm/tlbflush.h>
54 #include <asm/reboot.h>
55 #include <asm/stackprotector.h>
59 #include "multicalls.h"
61 EXPORT_SYMBOL_GPL(hypercall_page);
63 DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
64 DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
66 enum xen_domain_type xen_domain_type = XEN_NATIVE;
67 EXPORT_SYMBOL_GPL(xen_domain_type);
69 struct start_info *xen_start_info;
70 EXPORT_SYMBOL_GPL(xen_start_info);
72 struct shared_info xen_dummy_shared_info;
74 void *xen_initial_gdt;
77 * Point at some empty memory to start with. We map the real shared_info
78 * page as soon as fixmap is up and running.
80 struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
83 * Flag to determine whether vcpu info placement is available on all
84 * VCPUs. We assume it is to start with, and then set it to zero on
85 * the first failure. This is because it can succeed on some VCPUs
86 * and not others, since it can involve hypervisor memory allocation,
87 * or because the guest failed to guarantee all the appropriate
88 * constraints on all VCPUs (ie buffer can't cross a page boundary).
90 * Note that any particular CPU may be using a placed vcpu structure,
91 * but we can only optimise if the all are.
93 * 0: not available, 1: available
95 static int have_vcpu_info_placement = 1;
97 static void xen_vcpu_setup(int cpu)
99 struct vcpu_register_vcpu_info info;
101 struct vcpu_info *vcpup;
103 BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
104 per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
106 if (!have_vcpu_info_placement)
107 return; /* already tested, not available */
109 vcpup = &per_cpu(xen_vcpu_info, cpu);
111 info.mfn = arbitrary_virt_to_mfn(vcpup);
112 info.offset = offset_in_page(vcpup);
114 printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %llx, offset %d\n",
115 cpu, vcpup, info.mfn, info.offset);
117 /* Check to see if the hypervisor will put the vcpu_info
118 structure where we want it, which allows direct access via
119 a percpu-variable. */
120 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
123 printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
124 have_vcpu_info_placement = 0;
126 /* This cpu is using the registered vcpu info, even if
127 later ones fail to. */
128 per_cpu(xen_vcpu, cpu) = vcpup;
130 printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
136 * On restore, set the vcpu placement up again.
137 * If it fails, then we're in a bad state, since
138 * we can't back out from using it...
140 void xen_vcpu_restore(void)
142 if (have_vcpu_info_placement) {
145 for_each_online_cpu(cpu) {
146 bool other_cpu = (cpu != smp_processor_id());
149 HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
155 HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
159 BUG_ON(!have_vcpu_info_placement);
163 static void __init xen_banner(void)
165 unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
166 struct xen_extraversion extra;
167 HYPERVISOR_xen_version(XENVER_extraversion, &extra);
169 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
171 printk(KERN_INFO "Xen version: %d.%d%s%s\n",
172 version >> 16, version & 0xffff, extra.extraversion,
173 xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
176 static __read_mostly unsigned int cpuid_leaf1_edx_mask = ~0;
177 static __read_mostly unsigned int cpuid_leaf1_ecx_mask = ~0;
179 static void xen_cpuid(unsigned int *ax, unsigned int *bx,
180 unsigned int *cx, unsigned int *dx)
182 unsigned maskecx = ~0;
183 unsigned maskedx = ~0;
186 * Mask out inconvenient features, to try and disable as many
187 * unsupported kernel subsystems as possible.
190 maskecx = cpuid_leaf1_ecx_mask;
191 maskedx = cpuid_leaf1_edx_mask;
194 asm(XEN_EMULATE_PREFIX "cpuid"
199 : "0" (*ax), "2" (*cx));
205 static __init void xen_init_cpuid_mask(void)
207 unsigned int ax, bx, cx, dx;
209 cpuid_leaf1_edx_mask =
210 ~((1 << X86_FEATURE_MCE) | /* disable MCE */
211 (1 << X86_FEATURE_MCA) | /* disable MCA */
212 (1 << X86_FEATURE_ACC)); /* thermal monitoring */
214 if (!xen_initial_domain())
215 cpuid_leaf1_edx_mask &=
216 ~((1 << X86_FEATURE_APIC) | /* disable local APIC */
217 (1 << X86_FEATURE_ACPI)); /* disable ACPI */
221 xen_cpuid(&ax, &bx, &cx, &dx);
223 /* cpuid claims we support xsave; try enabling it to see what happens */
224 if (cx & (1 << (X86_FEATURE_XSAVE % 32))) {
227 set_in_cr4(X86_CR4_OSXSAVE);
231 if ((cr4 & X86_CR4_OSXSAVE) == 0)
232 cpuid_leaf1_ecx_mask &= ~(1 << (X86_FEATURE_XSAVE % 32));
234 clear_in_cr4(X86_CR4_OSXSAVE);
238 static void xen_set_debugreg(int reg, unsigned long val)
240 HYPERVISOR_set_debugreg(reg, val);
243 static unsigned long xen_get_debugreg(int reg)
245 return HYPERVISOR_get_debugreg(reg);
248 static void xen_end_context_switch(struct task_struct *next)
251 paravirt_end_context_switch(next);
254 static unsigned long xen_store_tr(void)
260 * Set the page permissions for a particular virtual address. If the
261 * address is a vmalloc mapping (or other non-linear mapping), then
262 * find the linear mapping of the page and also set its protections to
265 static void set_aliased_prot(void *v, pgprot_t prot)
273 ptep = lookup_address((unsigned long)v, &level);
274 BUG_ON(ptep == NULL);
276 pfn = pte_pfn(*ptep);
277 page = pfn_to_page(pfn);
279 pte = pfn_pte(pfn, prot);
281 if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
284 if (!PageHighMem(page)) {
285 void *av = __va(PFN_PHYS(pfn));
288 if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
294 static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
296 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
299 for(i = 0; i < entries; i += entries_per_page)
300 set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
303 static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
305 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
308 for(i = 0; i < entries; i += entries_per_page)
309 set_aliased_prot(ldt + i, PAGE_KERNEL);
312 static void xen_set_ldt(const void *addr, unsigned entries)
314 struct mmuext_op *op;
315 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
318 op->cmd = MMUEXT_SET_LDT;
319 op->arg1.linear_addr = (unsigned long)addr;
320 op->arg2.nr_ents = entries;
322 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
324 xen_mc_issue(PARAVIRT_LAZY_CPU);
327 static void xen_load_gdt(const struct desc_ptr *dtr)
329 unsigned long va = dtr->address;
330 unsigned int size = dtr->size + 1;
331 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
332 unsigned long frames[pages];
336 * A GDT can be up to 64k in size, which corresponds to 8192
337 * 8-byte entries, or 16 4k pages..
340 BUG_ON(size > 65536);
341 BUG_ON(va & ~PAGE_MASK);
343 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
346 unsigned long pfn, mfn;
350 * The GDT is per-cpu and is in the percpu data area.
351 * That can be virtually mapped, so we need to do a
352 * page-walk to get the underlying MFN for the
353 * hypercall. The page can also be in the kernel's
354 * linear range, so we need to RO that mapping too.
356 ptep = lookup_address(va, &level);
357 BUG_ON(ptep == NULL);
359 pfn = pte_pfn(*ptep);
360 mfn = pfn_to_mfn(pfn);
361 virt = __va(PFN_PHYS(pfn));
365 make_lowmem_page_readonly((void *)va);
366 make_lowmem_page_readonly(virt);
369 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
374 * load_gdt for early boot, when the gdt is only mapped once
376 static __init void xen_load_gdt_boot(const struct desc_ptr *dtr)
378 unsigned long va = dtr->address;
379 unsigned int size = dtr->size + 1;
380 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
381 unsigned long frames[pages];
385 * A GDT can be up to 64k in size, which corresponds to 8192
386 * 8-byte entries, or 16 4k pages..
389 BUG_ON(size > 65536);
390 BUG_ON(va & ~PAGE_MASK);
392 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
394 unsigned long pfn, mfn;
396 pfn = virt_to_pfn(va);
397 mfn = pfn_to_mfn(pfn);
399 pte = pfn_pte(pfn, PAGE_KERNEL_RO);
401 if (HYPERVISOR_update_va_mapping((unsigned long)va, pte, 0))
407 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
411 static void load_TLS_descriptor(struct thread_struct *t,
412 unsigned int cpu, unsigned int i)
414 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
415 xmaddr_t maddr = arbitrary_virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
416 struct multicall_space mc = __xen_mc_entry(0);
418 MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
421 static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
424 * XXX sleazy hack: If we're being called in a lazy-cpu zone
425 * and lazy gs handling is enabled, it means we're in a
426 * context switch, and %gs has just been saved. This means we
427 * can zero it out to prevent faults on exit from the
428 * hypervisor if the next process has no %gs. Either way, it
429 * has been saved, and the new value will get loaded properly.
430 * This will go away as soon as Xen has been modified to not
431 * save/restore %gs for normal hypercalls.
433 * On x86_64, this hack is not used for %gs, because gs points
434 * to KERNEL_GS_BASE (and uses it for PDA references), so we
435 * must not zero %gs on x86_64
437 * For x86_64, we need to zero %fs, otherwise we may get an
438 * exception between the new %fs descriptor being loaded and
439 * %fs being effectively cleared at __switch_to().
441 if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
451 load_TLS_descriptor(t, cpu, 0);
452 load_TLS_descriptor(t, cpu, 1);
453 load_TLS_descriptor(t, cpu, 2);
455 xen_mc_issue(PARAVIRT_LAZY_CPU);
459 static void xen_load_gs_index(unsigned int idx)
461 if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
466 static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
469 xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
470 u64 entry = *(u64 *)ptr;
475 if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
481 static int cvt_gate_to_trap(int vector, const gate_desc *val,
482 struct trap_info *info)
486 if (val->type != GATE_TRAP && val->type != GATE_INTERRUPT)
489 info->vector = vector;
491 addr = gate_offset(*val);
494 * Look for known traps using IST, and substitute them
495 * appropriately. The debugger ones are the only ones we care
496 * about. Xen will handle faults like double_fault and
497 * machine_check, so we should never see them. Warn if
498 * there's an unexpected IST-using fault handler.
500 if (addr == (unsigned long)debug)
501 addr = (unsigned long)xen_debug;
502 else if (addr == (unsigned long)int3)
503 addr = (unsigned long)xen_int3;
504 else if (addr == (unsigned long)stack_segment)
505 addr = (unsigned long)xen_stack_segment;
506 else if (addr == (unsigned long)double_fault ||
507 addr == (unsigned long)nmi) {
508 /* Don't need to handle these */
510 #ifdef CONFIG_X86_MCE
511 } else if (addr == (unsigned long)machine_check) {
515 /* Some other trap using IST? */
516 if (WARN_ON(val->ist != 0))
519 #endif /* CONFIG_X86_64 */
520 info->address = addr;
522 info->cs = gate_segment(*val);
523 info->flags = val->dpl;
524 /* interrupt gates clear IF */
525 if (val->type == GATE_INTERRUPT)
526 info->flags |= 1 << 2;
531 /* Locations of each CPU's IDT */
532 static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
534 /* Set an IDT entry. If the entry is part of the current IDT, then
536 static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
538 unsigned long p = (unsigned long)&dt[entrynum];
539 unsigned long start, end;
543 start = __get_cpu_var(idt_desc).address;
544 end = start + __get_cpu_var(idt_desc).size + 1;
548 native_write_idt_entry(dt, entrynum, g);
550 if (p >= start && (p + 8) <= end) {
551 struct trap_info info[2];
555 if (cvt_gate_to_trap(entrynum, g, &info[0]))
556 if (HYPERVISOR_set_trap_table(info))
563 static void xen_convert_trap_info(const struct desc_ptr *desc,
564 struct trap_info *traps)
566 unsigned in, out, count;
568 count = (desc->size+1) / sizeof(gate_desc);
571 for (in = out = 0; in < count; in++) {
572 gate_desc *entry = (gate_desc*)(desc->address) + in;
574 if (cvt_gate_to_trap(in, entry, &traps[out]))
577 traps[out].address = 0;
580 void xen_copy_trap_info(struct trap_info *traps)
582 const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
584 xen_convert_trap_info(desc, traps);
587 /* Load a new IDT into Xen. In principle this can be per-CPU, so we
588 hold a spinlock to protect the static traps[] array (static because
589 it avoids allocation, and saves stack space). */
590 static void xen_load_idt(const struct desc_ptr *desc)
592 static DEFINE_SPINLOCK(lock);
593 static struct trap_info traps[257];
597 __get_cpu_var(idt_desc) = *desc;
599 xen_convert_trap_info(desc, traps);
602 if (HYPERVISOR_set_trap_table(traps))
608 /* Write a GDT descriptor entry. Ignore LDT descriptors, since
609 they're handled differently. */
610 static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
611 const void *desc, int type)
622 xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
625 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
635 * Version of write_gdt_entry for use at early boot-time needed to
636 * update an entry as simply as possible.
638 static __init void xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
639 const void *desc, int type)
648 xmaddr_t maddr = virt_to_machine(&dt[entry]);
650 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
651 dt[entry] = *(struct desc_struct *)desc;
657 static void xen_load_sp0(struct tss_struct *tss,
658 struct thread_struct *thread)
660 struct multicall_space mcs = xen_mc_entry(0);
661 MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
662 xen_mc_issue(PARAVIRT_LAZY_CPU);
665 static void xen_set_iopl_mask(unsigned mask)
667 struct physdev_set_iopl set_iopl;
669 /* Force the change at ring 0. */
670 set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
671 HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
674 static void xen_io_delay(void)
678 #ifdef CONFIG_X86_LOCAL_APIC
679 static u32 xen_apic_read(u32 reg)
684 static void xen_apic_write(u32 reg, u32 val)
686 /* Warn to see if there's any stray references */
690 static u64 xen_apic_icr_read(void)
695 static void xen_apic_icr_write(u32 low, u32 id)
697 /* Warn to see if there's any stray references */
701 static void xen_apic_wait_icr_idle(void)
706 static u32 xen_safe_apic_wait_icr_idle(void)
711 static void set_xen_basic_apic_ops(void)
713 apic->read = xen_apic_read;
714 apic->write = xen_apic_write;
715 apic->icr_read = xen_apic_icr_read;
716 apic->icr_write = xen_apic_icr_write;
717 apic->wait_icr_idle = xen_apic_wait_icr_idle;
718 apic->safe_wait_icr_idle = xen_safe_apic_wait_icr_idle;
724 static void xen_clts(void)
726 struct multicall_space mcs;
728 mcs = xen_mc_entry(0);
730 MULTI_fpu_taskswitch(mcs.mc, 0);
732 xen_mc_issue(PARAVIRT_LAZY_CPU);
735 static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
737 static unsigned long xen_read_cr0(void)
739 unsigned long cr0 = percpu_read(xen_cr0_value);
741 if (unlikely(cr0 == 0)) {
742 cr0 = native_read_cr0();
743 percpu_write(xen_cr0_value, cr0);
749 static void xen_write_cr0(unsigned long cr0)
751 struct multicall_space mcs;
753 percpu_write(xen_cr0_value, cr0);
755 /* Only pay attention to cr0.TS; everything else is
757 mcs = xen_mc_entry(0);
759 MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
761 xen_mc_issue(PARAVIRT_LAZY_CPU);
764 static void xen_write_cr4(unsigned long cr4)
769 native_write_cr4(cr4);
772 static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
783 case MSR_FS_BASE: which = SEGBASE_FS; goto set;
784 case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
785 case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
788 base = ((u64)high << 32) | low;
789 if (HYPERVISOR_set_segment_base(which, base) != 0)
797 case MSR_SYSCALL_MASK:
798 case MSR_IA32_SYSENTER_CS:
799 case MSR_IA32_SYSENTER_ESP:
800 case MSR_IA32_SYSENTER_EIP:
801 /* Fast syscall setup is all done in hypercalls, so
802 these are all ignored. Stub them out here to stop
803 Xen console noise. */
807 ret = native_write_msr_safe(msr, low, high);
813 void xen_setup_shared_info(void)
815 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
816 set_fixmap(FIX_PARAVIRT_BOOTMAP,
817 xen_start_info->shared_info);
819 HYPERVISOR_shared_info =
820 (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
822 HYPERVISOR_shared_info =
823 (struct shared_info *)__va(xen_start_info->shared_info);
826 /* In UP this is as good a place as any to set up shared info */
827 xen_setup_vcpu_info_placement();
830 xen_setup_mfn_list_list();
833 /* This is called once we have the cpu_possible_map */
834 void xen_setup_vcpu_info_placement(void)
838 for_each_possible_cpu(cpu)
841 /* xen_vcpu_setup managed to place the vcpu_info within the
842 percpu area for all cpus, so make use of it */
843 if (have_vcpu_info_placement) {
844 printk(KERN_INFO "Xen: using vcpu_info placement\n");
846 pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
847 pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
848 pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
849 pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
850 pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
854 static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
855 unsigned long addr, unsigned len)
857 char *start, *end, *reloc;
860 start = end = reloc = NULL;
862 #define SITE(op, x) \
863 case PARAVIRT_PATCH(op.x): \
864 if (have_vcpu_info_placement) { \
865 start = (char *)xen_##x##_direct; \
866 end = xen_##x##_direct_end; \
867 reloc = xen_##x##_direct_reloc; \
872 SITE(pv_irq_ops, irq_enable);
873 SITE(pv_irq_ops, irq_disable);
874 SITE(pv_irq_ops, save_fl);
875 SITE(pv_irq_ops, restore_fl);
879 if (start == NULL || (end-start) > len)
882 ret = paravirt_patch_insns(insnbuf, len, start, end);
884 /* Note: because reloc is assigned from something that
885 appears to be an array, gcc assumes it's non-null,
886 but doesn't know its relationship with start and
888 if (reloc > start && reloc < end) {
889 int reloc_off = reloc - start;
890 long *relocp = (long *)(insnbuf + reloc_off);
891 long delta = start - (char *)addr;
899 ret = paravirt_patch_default(type, clobbers, insnbuf,
907 static const struct pv_info xen_info __initdata = {
908 .paravirt_enabled = 1,
909 .shared_kernel_pmd = 0,
914 static const struct pv_init_ops xen_init_ops __initdata = {
918 static const struct pv_time_ops xen_time_ops __initdata = {
919 .sched_clock = xen_sched_clock,
922 static const struct pv_cpu_ops xen_cpu_ops __initdata = {
925 .set_debugreg = xen_set_debugreg,
926 .get_debugreg = xen_get_debugreg,
930 .read_cr0 = xen_read_cr0,
931 .write_cr0 = xen_write_cr0,
933 .read_cr4 = native_read_cr4,
934 .read_cr4_safe = native_read_cr4_safe,
935 .write_cr4 = xen_write_cr4,
937 .wbinvd = native_wbinvd,
939 .read_msr = native_read_msr_safe,
940 .write_msr = xen_write_msr_safe,
941 .read_tsc = native_read_tsc,
942 .read_pmc = native_read_pmc,
945 .irq_enable_sysexit = xen_sysexit,
947 .usergs_sysret32 = xen_sysret32,
948 .usergs_sysret64 = xen_sysret64,
951 .load_tr_desc = paravirt_nop,
952 .set_ldt = xen_set_ldt,
953 .load_gdt = xen_load_gdt,
954 .load_idt = xen_load_idt,
955 .load_tls = xen_load_tls,
957 .load_gs_index = xen_load_gs_index,
960 .alloc_ldt = xen_alloc_ldt,
961 .free_ldt = xen_free_ldt,
963 .store_gdt = native_store_gdt,
964 .store_idt = native_store_idt,
965 .store_tr = xen_store_tr,
967 .write_ldt_entry = xen_write_ldt_entry,
968 .write_gdt_entry = xen_write_gdt_entry,
969 .write_idt_entry = xen_write_idt_entry,
970 .load_sp0 = xen_load_sp0,
972 .set_iopl_mask = xen_set_iopl_mask,
973 .io_delay = xen_io_delay,
975 /* Xen takes care of %gs when switching to usermode for us */
976 .swapgs = paravirt_nop,
978 .start_context_switch = paravirt_start_context_switch,
979 .end_context_switch = xen_end_context_switch,
982 static const struct pv_apic_ops xen_apic_ops __initdata = {
983 #ifdef CONFIG_X86_LOCAL_APIC
984 .startup_ipi_hook = paravirt_nop,
988 static void xen_reboot(int reason)
990 struct sched_shutdown r = { .reason = reason };
996 if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
1000 static void xen_restart(char *msg)
1002 xen_reboot(SHUTDOWN_reboot);
1005 static void xen_emergency_restart(void)
1007 xen_reboot(SHUTDOWN_reboot);
1010 static void xen_machine_halt(void)
1012 xen_reboot(SHUTDOWN_poweroff);
1015 static void xen_crash_shutdown(struct pt_regs *regs)
1017 xen_reboot(SHUTDOWN_crash);
1020 static const struct machine_ops __initdata xen_machine_ops = {
1021 .restart = xen_restart,
1022 .halt = xen_machine_halt,
1023 .power_off = xen_machine_halt,
1024 .shutdown = xen_machine_halt,
1025 .crash_shutdown = xen_crash_shutdown,
1026 .emergency_restart = xen_emergency_restart,
1030 * Set up the GDT and segment registers for -fstack-protector. Until
1031 * we do this, we have to be careful not to call any stack-protected
1032 * function, which is most of the kernel.
1034 static void __init xen_setup_stackprotector(void)
1036 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
1037 pv_cpu_ops.load_gdt = xen_load_gdt_boot;
1039 setup_stack_canary_segment(0);
1040 switch_to_new_gdt(0);
1042 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
1043 pv_cpu_ops.load_gdt = xen_load_gdt;
1046 /* First C function to be called on Xen boot */
1047 asmlinkage void __init xen_start_kernel(void)
1051 if (!xen_start_info)
1054 xen_domain_type = XEN_PV_DOMAIN;
1056 /* Install Xen paravirt ops */
1058 pv_init_ops = xen_init_ops;
1059 pv_time_ops = xen_time_ops;
1060 pv_cpu_ops = xen_cpu_ops;
1061 pv_apic_ops = xen_apic_ops;
1063 x86_init.resources.memory_setup = xen_memory_setup;
1064 x86_init.oem.arch_setup = xen_arch_setup;
1065 x86_init.oem.banner = xen_banner;
1067 x86_init.timers.timer_init = xen_time_init;
1068 x86_init.timers.setup_percpu_clockev = x86_init_noop;
1069 x86_cpuinit.setup_percpu_clockev = x86_init_noop;
1071 x86_platform.calibrate_tsc = xen_tsc_khz;
1072 x86_platform.get_wallclock = xen_get_wallclock;
1073 x86_platform.set_wallclock = xen_set_wallclock;
1076 * Set up some pagetable state before starting to set any ptes.
1081 /* Prevent unwanted bits from being set in PTEs. */
1082 __supported_pte_mask &= ~_PAGE_GLOBAL;
1083 if (!xen_initial_domain())
1084 __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
1086 __supported_pte_mask |= _PAGE_IOMAP;
1088 #ifdef CONFIG_X86_64
1089 /* Work out if we support NX */
1093 xen_setup_features();
1096 if (!xen_feature(XENFEAT_auto_translated_physmap))
1097 xen_build_dynamic_phys_to_machine();
1100 * Set up kernel GDT and segment registers, mainly so that
1101 * -fstack-protector code can be executed.
1103 xen_setup_stackprotector();
1106 xen_init_cpuid_mask();
1108 #ifdef CONFIG_X86_LOCAL_APIC
1110 * set up the basic apic ops.
1112 set_xen_basic_apic_ops();
1115 if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
1116 pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
1117 pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
1120 machine_ops = xen_machine_ops;
1123 * The only reliable way to retain the initial address of the
1124 * percpu gdt_page is to remember it here, so we can go and
1125 * mark it RW later, when the initial percpu area is freed.
1127 xen_initial_gdt = &per_cpu(gdt_page, 0);
1131 pgd = (pgd_t *)xen_start_info->pt_base;
1133 /* Don't do the full vcpu_info placement stuff until we have a
1134 possible map and a non-dummy shared_info. */
1135 per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
1137 local_irq_disable();
1138 early_boot_irqs_off();
1140 xen_raw_console_write("mapping kernel into physical memory\n");
1141 pgd = xen_setup_kernel_pagetable(pgd, xen_start_info->nr_pages);
1145 /* keep using Xen gdt for now; no urgent need to change it */
1147 pv_info.kernel_rpl = 1;
1148 if (xen_feature(XENFEAT_supervisor_mode_kernel))
1149 pv_info.kernel_rpl = 0;
1151 /* set the limit of our address space */
1154 #ifdef CONFIG_X86_32
1155 /* set up basic CPUID stuff */
1156 cpu_detect(&new_cpu_data);
1157 new_cpu_data.hard_math = 1;
1158 new_cpu_data.wp_works_ok = 1;
1159 new_cpu_data.x86_capability[0] = cpuid_edx(1);
1162 /* Poke various useful things into boot_params */
1163 boot_params.hdr.type_of_loader = (9 << 4) | 0;
1164 boot_params.hdr.ramdisk_image = xen_start_info->mod_start
1165 ? __pa(xen_start_info->mod_start) : 0;
1166 boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
1167 boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
1169 if (!xen_initial_domain()) {
1170 add_preferred_console("xenboot", 0, NULL);
1171 add_preferred_console("tty", 0, NULL);
1172 add_preferred_console("hvc", 0, NULL);
1175 xen_raw_console_write("about to get started...\n");
1177 /* Start the world */
1178 #ifdef CONFIG_X86_32
1179 i386_start_kernel();
1181 x86_64_start_reservations((char *)__pa_symbol(&boot_params));