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>
30 #include <linux/pci.h>
33 #include <xen/interface/xen.h>
34 #include <xen/interface/version.h>
35 #include <xen/interface/physdev.h>
36 #include <xen/interface/vcpu.h>
37 #include <xen/features.h>
39 #include <xen/hvc-console.h>
41 #include <asm/paravirt.h>
44 #include <asm/xen/hypercall.h>
45 #include <asm/xen/hypervisor.h>
46 #include <asm/fixmap.h>
47 #include <asm/processor.h>
48 #include <asm/proto.h>
49 #include <asm/msr-index.h>
50 #include <asm/traps.h>
51 #include <asm/setup.h>
53 #include <asm/pgtable.h>
54 #include <asm/tlbflush.h>
55 #include <asm/reboot.h>
56 #include <asm/stackprotector.h>
60 #include "multicalls.h"
62 EXPORT_SYMBOL_GPL(hypercall_page);
64 DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
65 DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
67 enum xen_domain_type xen_domain_type = XEN_NATIVE;
68 EXPORT_SYMBOL_GPL(xen_domain_type);
70 struct start_info *xen_start_info;
71 EXPORT_SYMBOL_GPL(xen_start_info);
73 struct shared_info xen_dummy_shared_info;
75 void *xen_initial_gdt;
78 * Point at some empty memory to start with. We map the real shared_info
79 * page as soon as fixmap is up and running.
81 struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
84 * Flag to determine whether vcpu info placement is available on all
85 * VCPUs. We assume it is to start with, and then set it to zero on
86 * the first failure. This is because it can succeed on some VCPUs
87 * and not others, since it can involve hypervisor memory allocation,
88 * or because the guest failed to guarantee all the appropriate
89 * constraints on all VCPUs (ie buffer can't cross a page boundary).
91 * Note that any particular CPU may be using a placed vcpu structure,
92 * but we can only optimise if the all are.
94 * 0: not available, 1: available
96 static int have_vcpu_info_placement = 1;
98 static void xen_vcpu_setup(int cpu)
100 struct vcpu_register_vcpu_info info;
102 struct vcpu_info *vcpup;
104 BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
105 per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
107 if (!have_vcpu_info_placement)
108 return; /* already tested, not available */
110 vcpup = &per_cpu(xen_vcpu_info, cpu);
112 info.mfn = arbitrary_virt_to_mfn(vcpup);
113 info.offset = offset_in_page(vcpup);
115 printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %llx, offset %d\n",
116 cpu, vcpup, info.mfn, info.offset);
118 /* Check to see if the hypervisor will put the vcpu_info
119 structure where we want it, which allows direct access via
120 a percpu-variable. */
121 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
124 printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
125 have_vcpu_info_placement = 0;
127 /* This cpu is using the registered vcpu info, even if
128 later ones fail to. */
129 per_cpu(xen_vcpu, cpu) = vcpup;
131 printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
137 * On restore, set the vcpu placement up again.
138 * If it fails, then we're in a bad state, since
139 * we can't back out from using it...
141 void xen_vcpu_restore(void)
143 if (have_vcpu_info_placement) {
146 for_each_online_cpu(cpu) {
147 bool other_cpu = (cpu != smp_processor_id());
150 HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
156 HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
160 BUG_ON(!have_vcpu_info_placement);
164 static void __init xen_banner(void)
166 unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
167 struct xen_extraversion extra;
168 HYPERVISOR_xen_version(XENVER_extraversion, &extra);
170 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
172 printk(KERN_INFO "Xen version: %d.%d%s%s\n",
173 version >> 16, version & 0xffff, extra.extraversion,
174 xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
177 static __read_mostly unsigned int cpuid_leaf1_edx_mask = ~0;
178 static __read_mostly unsigned int cpuid_leaf1_ecx_mask = ~0;
180 static void xen_cpuid(unsigned int *ax, unsigned int *bx,
181 unsigned int *cx, unsigned int *dx)
183 unsigned maskecx = ~0;
184 unsigned maskedx = ~0;
187 * Mask out inconvenient features, to try and disable as many
188 * unsupported kernel subsystems as possible.
191 maskecx = cpuid_leaf1_ecx_mask;
192 maskedx = cpuid_leaf1_edx_mask;
195 asm(XEN_EMULATE_PREFIX "cpuid"
200 : "0" (*ax), "2" (*cx));
206 static __init void xen_init_cpuid_mask(void)
208 unsigned int ax, bx, cx, dx;
210 cpuid_leaf1_edx_mask =
211 ~((1 << X86_FEATURE_MCE) | /* disable MCE */
212 (1 << X86_FEATURE_MCA) | /* disable MCA */
213 (1 << X86_FEATURE_ACC)); /* thermal monitoring */
215 if (!xen_initial_domain())
216 cpuid_leaf1_edx_mask &=
217 ~((1 << X86_FEATURE_APIC) | /* disable local APIC */
218 (1 << X86_FEATURE_ACPI)); /* disable ACPI */
222 xen_cpuid(&ax, &bx, &cx, &dx);
224 /* cpuid claims we support xsave; try enabling it to see what happens */
225 if (cx & (1 << (X86_FEATURE_XSAVE % 32))) {
228 set_in_cr4(X86_CR4_OSXSAVE);
232 if ((cr4 & X86_CR4_OSXSAVE) == 0)
233 cpuid_leaf1_ecx_mask &= ~(1 << (X86_FEATURE_XSAVE % 32));
235 clear_in_cr4(X86_CR4_OSXSAVE);
239 static void xen_set_debugreg(int reg, unsigned long val)
241 HYPERVISOR_set_debugreg(reg, val);
244 static unsigned long xen_get_debugreg(int reg)
246 return HYPERVISOR_get_debugreg(reg);
249 static void xen_end_context_switch(struct task_struct *next)
252 paravirt_end_context_switch(next);
255 static unsigned long xen_store_tr(void)
261 * Set the page permissions for a particular virtual address. If the
262 * address is a vmalloc mapping (or other non-linear mapping), then
263 * find the linear mapping of the page and also set its protections to
266 static void set_aliased_prot(void *v, pgprot_t prot)
274 ptep = lookup_address((unsigned long)v, &level);
275 BUG_ON(ptep == NULL);
277 pfn = pte_pfn(*ptep);
278 page = pfn_to_page(pfn);
280 pte = pfn_pte(pfn, prot);
282 if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
285 if (!PageHighMem(page)) {
286 void *av = __va(PFN_PHYS(pfn));
289 if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
295 static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
297 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
300 for(i = 0; i < entries; i += entries_per_page)
301 set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
304 static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
306 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
309 for(i = 0; i < entries; i += entries_per_page)
310 set_aliased_prot(ldt + i, PAGE_KERNEL);
313 static void xen_set_ldt(const void *addr, unsigned entries)
315 struct mmuext_op *op;
316 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
319 op->cmd = MMUEXT_SET_LDT;
320 op->arg1.linear_addr = (unsigned long)addr;
321 op->arg2.nr_ents = entries;
323 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
325 xen_mc_issue(PARAVIRT_LAZY_CPU);
328 static void xen_load_gdt(const struct desc_ptr *dtr)
330 unsigned long va = dtr->address;
331 unsigned int size = dtr->size + 1;
332 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
333 unsigned long frames[pages];
337 * A GDT can be up to 64k in size, which corresponds to 8192
338 * 8-byte entries, or 16 4k pages..
341 BUG_ON(size > 65536);
342 BUG_ON(va & ~PAGE_MASK);
344 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
347 unsigned long pfn, mfn;
351 * The GDT is per-cpu and is in the percpu data area.
352 * That can be virtually mapped, so we need to do a
353 * page-walk to get the underlying MFN for the
354 * hypercall. The page can also be in the kernel's
355 * linear range, so we need to RO that mapping too.
357 ptep = lookup_address(va, &level);
358 BUG_ON(ptep == NULL);
360 pfn = pte_pfn(*ptep);
361 mfn = pfn_to_mfn(pfn);
362 virt = __va(PFN_PHYS(pfn));
366 make_lowmem_page_readonly((void *)va);
367 make_lowmem_page_readonly(virt);
370 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
375 * load_gdt for early boot, when the gdt is only mapped once
377 static __init void xen_load_gdt_boot(const struct desc_ptr *dtr)
379 unsigned long va = dtr->address;
380 unsigned int size = dtr->size + 1;
381 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
382 unsigned long frames[pages];
386 * A GDT can be up to 64k in size, which corresponds to 8192
387 * 8-byte entries, or 16 4k pages..
390 BUG_ON(size > 65536);
391 BUG_ON(va & ~PAGE_MASK);
393 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
395 unsigned long pfn, mfn;
397 pfn = virt_to_pfn(va);
398 mfn = pfn_to_mfn(pfn);
400 pte = pfn_pte(pfn, PAGE_KERNEL_RO);
402 if (HYPERVISOR_update_va_mapping((unsigned long)va, pte, 0))
408 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
412 static void load_TLS_descriptor(struct thread_struct *t,
413 unsigned int cpu, unsigned int i)
415 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
416 xmaddr_t maddr = arbitrary_virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
417 struct multicall_space mc = __xen_mc_entry(0);
419 MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
422 static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
425 * XXX sleazy hack: If we're being called in a lazy-cpu zone
426 * and lazy gs handling is enabled, it means we're in a
427 * context switch, and %gs has just been saved. This means we
428 * can zero it out to prevent faults on exit from the
429 * hypervisor if the next process has no %gs. Either way, it
430 * has been saved, and the new value will get loaded properly.
431 * This will go away as soon as Xen has been modified to not
432 * save/restore %gs for normal hypercalls.
434 * On x86_64, this hack is not used for %gs, because gs points
435 * to KERNEL_GS_BASE (and uses it for PDA references), so we
436 * must not zero %gs on x86_64
438 * For x86_64, we need to zero %fs, otherwise we may get an
439 * exception between the new %fs descriptor being loaded and
440 * %fs being effectively cleared at __switch_to().
442 if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
452 load_TLS_descriptor(t, cpu, 0);
453 load_TLS_descriptor(t, cpu, 1);
454 load_TLS_descriptor(t, cpu, 2);
456 xen_mc_issue(PARAVIRT_LAZY_CPU);
460 static void xen_load_gs_index(unsigned int idx)
462 if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
467 static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
470 xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
471 u64 entry = *(u64 *)ptr;
476 if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
482 static int cvt_gate_to_trap(int vector, const gate_desc *val,
483 struct trap_info *info)
487 if (val->type != GATE_TRAP && val->type != GATE_INTERRUPT)
490 info->vector = vector;
492 addr = gate_offset(*val);
495 * Look for known traps using IST, and substitute them
496 * appropriately. The debugger ones are the only ones we care
497 * about. Xen will handle faults like double_fault and
498 * machine_check, so we should never see them. Warn if
499 * there's an unexpected IST-using fault handler.
501 if (addr == (unsigned long)debug)
502 addr = (unsigned long)xen_debug;
503 else if (addr == (unsigned long)int3)
504 addr = (unsigned long)xen_int3;
505 else if (addr == (unsigned long)stack_segment)
506 addr = (unsigned long)xen_stack_segment;
507 else if (addr == (unsigned long)double_fault ||
508 addr == (unsigned long)nmi) {
509 /* Don't need to handle these */
511 #ifdef CONFIG_X86_MCE
512 } else if (addr == (unsigned long)machine_check) {
516 /* Some other trap using IST? */
517 if (WARN_ON(val->ist != 0))
520 #endif /* CONFIG_X86_64 */
521 info->address = addr;
523 info->cs = gate_segment(*val);
524 info->flags = val->dpl;
525 /* interrupt gates clear IF */
526 if (val->type == GATE_INTERRUPT)
527 info->flags |= 1 << 2;
532 /* Locations of each CPU's IDT */
533 static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
535 /* Set an IDT entry. If the entry is part of the current IDT, then
537 static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
539 unsigned long p = (unsigned long)&dt[entrynum];
540 unsigned long start, end;
544 start = __get_cpu_var(idt_desc).address;
545 end = start + __get_cpu_var(idt_desc).size + 1;
549 native_write_idt_entry(dt, entrynum, g);
551 if (p >= start && (p + 8) <= end) {
552 struct trap_info info[2];
556 if (cvt_gate_to_trap(entrynum, g, &info[0]))
557 if (HYPERVISOR_set_trap_table(info))
564 static void xen_convert_trap_info(const struct desc_ptr *desc,
565 struct trap_info *traps)
567 unsigned in, out, count;
569 count = (desc->size+1) / sizeof(gate_desc);
572 for (in = out = 0; in < count; in++) {
573 gate_desc *entry = (gate_desc*)(desc->address) + in;
575 if (cvt_gate_to_trap(in, entry, &traps[out]))
578 traps[out].address = 0;
581 void xen_copy_trap_info(struct trap_info *traps)
583 const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
585 xen_convert_trap_info(desc, traps);
588 /* Load a new IDT into Xen. In principle this can be per-CPU, so we
589 hold a spinlock to protect the static traps[] array (static because
590 it avoids allocation, and saves stack space). */
591 static void xen_load_idt(const struct desc_ptr *desc)
593 static DEFINE_SPINLOCK(lock);
594 static struct trap_info traps[257];
598 __get_cpu_var(idt_desc) = *desc;
600 xen_convert_trap_info(desc, traps);
603 if (HYPERVISOR_set_trap_table(traps))
609 /* Write a GDT descriptor entry. Ignore LDT descriptors, since
610 they're handled differently. */
611 static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
612 const void *desc, int type)
623 xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
626 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
636 * Version of write_gdt_entry for use at early boot-time needed to
637 * update an entry as simply as possible.
639 static __init void xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
640 const void *desc, int type)
649 xmaddr_t maddr = virt_to_machine(&dt[entry]);
651 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
652 dt[entry] = *(struct desc_struct *)desc;
658 static void xen_load_sp0(struct tss_struct *tss,
659 struct thread_struct *thread)
661 struct multicall_space mcs = xen_mc_entry(0);
662 MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
663 xen_mc_issue(PARAVIRT_LAZY_CPU);
666 static void xen_set_iopl_mask(unsigned mask)
668 struct physdev_set_iopl set_iopl;
670 /* Force the change at ring 0. */
671 set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
672 HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
675 static void xen_io_delay(void)
679 #ifdef CONFIG_X86_LOCAL_APIC
680 static u32 xen_apic_read(u32 reg)
685 static void xen_apic_write(u32 reg, u32 val)
687 /* Warn to see if there's any stray references */
691 static u64 xen_apic_icr_read(void)
696 static void xen_apic_icr_write(u32 low, u32 id)
698 /* Warn to see if there's any stray references */
702 static void xen_apic_wait_icr_idle(void)
707 static u32 xen_safe_apic_wait_icr_idle(void)
712 static void set_xen_basic_apic_ops(void)
714 apic->read = xen_apic_read;
715 apic->write = xen_apic_write;
716 apic->icr_read = xen_apic_icr_read;
717 apic->icr_write = xen_apic_icr_write;
718 apic->wait_icr_idle = xen_apic_wait_icr_idle;
719 apic->safe_wait_icr_idle = xen_safe_apic_wait_icr_idle;
725 static void xen_clts(void)
727 struct multicall_space mcs;
729 mcs = xen_mc_entry(0);
731 MULTI_fpu_taskswitch(mcs.mc, 0);
733 xen_mc_issue(PARAVIRT_LAZY_CPU);
736 static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
738 static unsigned long xen_read_cr0(void)
740 unsigned long cr0 = percpu_read(xen_cr0_value);
742 if (unlikely(cr0 == 0)) {
743 cr0 = native_read_cr0();
744 percpu_write(xen_cr0_value, cr0);
750 static void xen_write_cr0(unsigned long cr0)
752 struct multicall_space mcs;
754 percpu_write(xen_cr0_value, cr0);
756 /* Only pay attention to cr0.TS; everything else is
758 mcs = xen_mc_entry(0);
760 MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
762 xen_mc_issue(PARAVIRT_LAZY_CPU);
765 static void xen_write_cr4(unsigned long cr4)
770 native_write_cr4(cr4);
773 static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
784 case MSR_FS_BASE: which = SEGBASE_FS; goto set;
785 case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
786 case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
789 base = ((u64)high << 32) | low;
790 if (HYPERVISOR_set_segment_base(which, base) != 0)
798 case MSR_SYSCALL_MASK:
799 case MSR_IA32_SYSENTER_CS:
800 case MSR_IA32_SYSENTER_ESP:
801 case MSR_IA32_SYSENTER_EIP:
802 /* Fast syscall setup is all done in hypercalls, so
803 these are all ignored. Stub them out here to stop
804 Xen console noise. */
808 ret = native_write_msr_safe(msr, low, high);
814 void xen_setup_shared_info(void)
816 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
817 set_fixmap(FIX_PARAVIRT_BOOTMAP,
818 xen_start_info->shared_info);
820 HYPERVISOR_shared_info =
821 (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
823 HYPERVISOR_shared_info =
824 (struct shared_info *)__va(xen_start_info->shared_info);
827 /* In UP this is as good a place as any to set up shared info */
828 xen_setup_vcpu_info_placement();
831 xen_setup_mfn_list_list();
834 /* This is called once we have the cpu_possible_map */
835 void xen_setup_vcpu_info_placement(void)
839 for_each_possible_cpu(cpu)
842 /* xen_vcpu_setup managed to place the vcpu_info within the
843 percpu area for all cpus, so make use of it */
844 if (have_vcpu_info_placement) {
845 printk(KERN_INFO "Xen: using vcpu_info placement\n");
847 pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
848 pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
849 pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
850 pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
851 pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
855 static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
856 unsigned long addr, unsigned len)
858 char *start, *end, *reloc;
861 start = end = reloc = NULL;
863 #define SITE(op, x) \
864 case PARAVIRT_PATCH(op.x): \
865 if (have_vcpu_info_placement) { \
866 start = (char *)xen_##x##_direct; \
867 end = xen_##x##_direct_end; \
868 reloc = xen_##x##_direct_reloc; \
873 SITE(pv_irq_ops, irq_enable);
874 SITE(pv_irq_ops, irq_disable);
875 SITE(pv_irq_ops, save_fl);
876 SITE(pv_irq_ops, restore_fl);
880 if (start == NULL || (end-start) > len)
883 ret = paravirt_patch_insns(insnbuf, len, start, end);
885 /* Note: because reloc is assigned from something that
886 appears to be an array, gcc assumes it's non-null,
887 but doesn't know its relationship with start and
889 if (reloc > start && reloc < end) {
890 int reloc_off = reloc - start;
891 long *relocp = (long *)(insnbuf + reloc_off);
892 long delta = start - (char *)addr;
900 ret = paravirt_patch_default(type, clobbers, insnbuf,
908 static const struct pv_info xen_info __initdata = {
909 .paravirt_enabled = 1,
910 .shared_kernel_pmd = 0,
915 static const struct pv_init_ops xen_init_ops __initdata = {
919 static const struct pv_time_ops xen_time_ops __initdata = {
920 .sched_clock = xen_sched_clock,
923 static const struct pv_cpu_ops xen_cpu_ops __initdata = {
926 .set_debugreg = xen_set_debugreg,
927 .get_debugreg = xen_get_debugreg,
931 .read_cr0 = xen_read_cr0,
932 .write_cr0 = xen_write_cr0,
934 .read_cr4 = native_read_cr4,
935 .read_cr4_safe = native_read_cr4_safe,
936 .write_cr4 = xen_write_cr4,
938 .wbinvd = native_wbinvd,
940 .read_msr = native_read_msr_safe,
941 .write_msr = xen_write_msr_safe,
942 .read_tsc = native_read_tsc,
943 .read_pmc = native_read_pmc,
946 .irq_enable_sysexit = xen_sysexit,
948 .usergs_sysret32 = xen_sysret32,
949 .usergs_sysret64 = xen_sysret64,
952 .load_tr_desc = paravirt_nop,
953 .set_ldt = xen_set_ldt,
954 .load_gdt = xen_load_gdt,
955 .load_idt = xen_load_idt,
956 .load_tls = xen_load_tls,
958 .load_gs_index = xen_load_gs_index,
961 .alloc_ldt = xen_alloc_ldt,
962 .free_ldt = xen_free_ldt,
964 .store_gdt = native_store_gdt,
965 .store_idt = native_store_idt,
966 .store_tr = xen_store_tr,
968 .write_ldt_entry = xen_write_ldt_entry,
969 .write_gdt_entry = xen_write_gdt_entry,
970 .write_idt_entry = xen_write_idt_entry,
971 .load_sp0 = xen_load_sp0,
973 .set_iopl_mask = xen_set_iopl_mask,
974 .io_delay = xen_io_delay,
976 /* Xen takes care of %gs when switching to usermode for us */
977 .swapgs = paravirt_nop,
979 .start_context_switch = paravirt_start_context_switch,
980 .end_context_switch = xen_end_context_switch,
983 static const struct pv_apic_ops xen_apic_ops __initdata = {
984 #ifdef CONFIG_X86_LOCAL_APIC
985 .startup_ipi_hook = paravirt_nop,
989 static void xen_reboot(int reason)
991 struct sched_shutdown r = { .reason = reason };
997 if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
1001 static void xen_restart(char *msg)
1003 xen_reboot(SHUTDOWN_reboot);
1006 static void xen_emergency_restart(void)
1008 xen_reboot(SHUTDOWN_reboot);
1011 static void xen_machine_halt(void)
1013 xen_reboot(SHUTDOWN_poweroff);
1016 static void xen_crash_shutdown(struct pt_regs *regs)
1018 xen_reboot(SHUTDOWN_crash);
1021 static const struct machine_ops __initdata xen_machine_ops = {
1022 .restart = xen_restart,
1023 .halt = xen_machine_halt,
1024 .power_off = xen_machine_halt,
1025 .shutdown = xen_machine_halt,
1026 .crash_shutdown = xen_crash_shutdown,
1027 .emergency_restart = xen_emergency_restart,
1031 * Set up the GDT and segment registers for -fstack-protector. Until
1032 * we do this, we have to be careful not to call any stack-protected
1033 * function, which is most of the kernel.
1035 static void __init xen_setup_stackprotector(void)
1037 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
1038 pv_cpu_ops.load_gdt = xen_load_gdt_boot;
1040 setup_stack_canary_segment(0);
1041 switch_to_new_gdt(0);
1043 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
1044 pv_cpu_ops.load_gdt = xen_load_gdt;
1047 /* First C function to be called on Xen boot */
1048 asmlinkage void __init xen_start_kernel(void)
1052 if (!xen_start_info)
1055 xen_domain_type = XEN_PV_DOMAIN;
1057 /* Install Xen paravirt ops */
1059 pv_init_ops = xen_init_ops;
1060 pv_time_ops = xen_time_ops;
1061 pv_cpu_ops = xen_cpu_ops;
1062 pv_apic_ops = xen_apic_ops;
1064 x86_init.resources.memory_setup = xen_memory_setup;
1065 x86_init.oem.arch_setup = xen_arch_setup;
1066 x86_init.oem.banner = xen_banner;
1068 x86_init.timers.timer_init = xen_time_init;
1069 x86_init.timers.setup_percpu_clockev = x86_init_noop;
1070 x86_cpuinit.setup_percpu_clockev = x86_init_noop;
1072 x86_platform.calibrate_tsc = xen_tsc_khz;
1073 x86_platform.get_wallclock = xen_get_wallclock;
1074 x86_platform.set_wallclock = xen_set_wallclock;
1077 * Set up some pagetable state before starting to set any ptes.
1082 /* Prevent unwanted bits from being set in PTEs. */
1083 __supported_pte_mask &= ~_PAGE_GLOBAL;
1084 if (!xen_initial_domain())
1085 __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
1087 __supported_pte_mask |= _PAGE_IOMAP;
1089 #ifdef CONFIG_X86_64
1090 /* Work out if we support NX */
1094 xen_setup_features();
1097 if (!xen_feature(XENFEAT_auto_translated_physmap))
1098 xen_build_dynamic_phys_to_machine();
1101 * Set up kernel GDT and segment registers, mainly so that
1102 * -fstack-protector code can be executed.
1104 xen_setup_stackprotector();
1107 xen_init_cpuid_mask();
1109 #ifdef CONFIG_X86_LOCAL_APIC
1111 * set up the basic apic ops.
1113 set_xen_basic_apic_ops();
1116 if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
1117 pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
1118 pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
1121 machine_ops = xen_machine_ops;
1124 * The only reliable way to retain the initial address of the
1125 * percpu gdt_page is to remember it here, so we can go and
1126 * mark it RW later, when the initial percpu area is freed.
1128 xen_initial_gdt = &per_cpu(gdt_page, 0);
1132 pgd = (pgd_t *)xen_start_info->pt_base;
1134 /* Don't do the full vcpu_info placement stuff until we have a
1135 possible map and a non-dummy shared_info. */
1136 per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
1138 local_irq_disable();
1139 early_boot_irqs_off();
1141 xen_raw_console_write("mapping kernel into physical memory\n");
1142 pgd = xen_setup_kernel_pagetable(pgd, xen_start_info->nr_pages);
1146 /* keep using Xen gdt for now; no urgent need to change it */
1148 pv_info.kernel_rpl = 1;
1149 if (xen_feature(XENFEAT_supervisor_mode_kernel))
1150 pv_info.kernel_rpl = 0;
1152 /* set the limit of our address space */
1155 #ifdef CONFIG_X86_32
1156 /* set up basic CPUID stuff */
1157 cpu_detect(&new_cpu_data);
1158 new_cpu_data.hard_math = 1;
1159 new_cpu_data.wp_works_ok = 1;
1160 new_cpu_data.x86_capability[0] = cpuid_edx(1);
1163 /* Poke various useful things into boot_params */
1164 boot_params.hdr.type_of_loader = (9 << 4) | 0;
1165 boot_params.hdr.ramdisk_image = xen_start_info->mod_start
1166 ? __pa(xen_start_info->mod_start) : 0;
1167 boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
1168 boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
1170 if (!xen_initial_domain()) {
1171 add_preferred_console("xenboot", 0, NULL);
1172 add_preferred_console("tty", 0, NULL);
1173 add_preferred_console("hvc", 0, NULL);
1175 /* Make sure ACS will be enabled */
1180 xen_raw_console_write("about to get started...\n");
1182 /* Start the world */
1183 #ifdef CONFIG_X86_32
1184 i386_start_kernel();
1186 x86_64_start_reservations((char *)__pa_symbol(&boot_params));