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>
31 #include <xen/interface/xen.h>
32 #include <xen/interface/version.h>
33 #include <xen/interface/physdev.h>
34 #include <xen/interface/vcpu.h>
35 #include <xen/features.h>
37 #include <xen/hvc-console.h>
39 #include <asm/paravirt.h>
42 #include <asm/xen/hypercall.h>
43 #include <asm/xen/hypervisor.h>
44 #include <asm/fixmap.h>
45 #include <asm/processor.h>
46 #include <asm/proto.h>
47 #include <asm/msr-index.h>
48 #include <asm/traps.h>
49 #include <asm/setup.h>
51 #include <asm/pgtable.h>
52 #include <asm/tlbflush.h>
53 #include <asm/reboot.h>
54 #include <asm/stackprotector.h>
58 #include "multicalls.h"
60 EXPORT_SYMBOL_GPL(hypercall_page);
62 DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
63 DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
65 enum xen_domain_type xen_domain_type = XEN_NATIVE;
66 EXPORT_SYMBOL_GPL(xen_domain_type);
68 struct start_info *xen_start_info;
69 EXPORT_SYMBOL_GPL(xen_start_info);
71 struct shared_info xen_dummy_shared_info;
73 void *xen_initial_gdt;
76 * Point at some empty memory to start with. We map the real shared_info
77 * page as soon as fixmap is up and running.
79 struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
82 * Flag to determine whether vcpu info placement is available on all
83 * VCPUs. We assume it is to start with, and then set it to zero on
84 * the first failure. This is because it can succeed on some VCPUs
85 * and not others, since it can involve hypervisor memory allocation,
86 * or because the guest failed to guarantee all the appropriate
87 * constraints on all VCPUs (ie buffer can't cross a page boundary).
89 * Note that any particular CPU may be using a placed vcpu structure,
90 * but we can only optimise if the all are.
92 * 0: not available, 1: available
94 static int have_vcpu_info_placement = 1;
96 static void xen_vcpu_setup(int cpu)
98 struct vcpu_register_vcpu_info info;
100 struct vcpu_info *vcpup;
102 BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
103 per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
105 if (!have_vcpu_info_placement)
106 return; /* already tested, not available */
108 vcpup = &per_cpu(xen_vcpu_info, cpu);
110 info.mfn = arbitrary_virt_to_mfn(vcpup);
111 info.offset = offset_in_page(vcpup);
113 printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %llx, offset %d\n",
114 cpu, vcpup, info.mfn, info.offset);
116 /* Check to see if the hypervisor will put the vcpu_info
117 structure where we want it, which allows direct access via
118 a percpu-variable. */
119 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
122 printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
123 have_vcpu_info_placement = 0;
125 /* This cpu is using the registered vcpu info, even if
126 later ones fail to. */
127 per_cpu(xen_vcpu, cpu) = vcpup;
129 printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
135 * On restore, set the vcpu placement up again.
136 * If it fails, then we're in a bad state, since
137 * we can't back out from using it...
139 void xen_vcpu_restore(void)
141 if (have_vcpu_info_placement) {
144 for_each_online_cpu(cpu) {
145 bool other_cpu = (cpu != smp_processor_id());
148 HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
154 HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
158 BUG_ON(!have_vcpu_info_placement);
162 static void __init xen_banner(void)
164 unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
165 struct xen_extraversion extra;
166 HYPERVISOR_xen_version(XENVER_extraversion, &extra);
168 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
170 printk(KERN_INFO "Xen version: %d.%d%s%s\n",
171 version >> 16, version & 0xffff, extra.extraversion,
172 xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
175 static __read_mostly unsigned int cpuid_leaf1_edx_mask = ~0;
176 static __read_mostly unsigned int cpuid_leaf1_ecx_mask = ~0;
178 static void xen_cpuid(unsigned int *ax, unsigned int *bx,
179 unsigned int *cx, unsigned int *dx)
181 unsigned maskebx = ~0;
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.
191 maskecx = cpuid_leaf1_ecx_mask;
192 maskedx = cpuid_leaf1_edx_mask;
196 /* Suppress extended topology stuff */
201 asm(XEN_EMULATE_PREFIX "cpuid"
206 : "0" (*ax), "2" (*cx));
213 static __init void xen_init_cpuid_mask(void)
215 unsigned int ax, bx, cx, dx;
217 cpuid_leaf1_edx_mask =
218 ~((1 << X86_FEATURE_MCE) | /* disable MCE */
219 (1 << X86_FEATURE_MCA) | /* disable MCA */
220 (1 << X86_FEATURE_ACC)); /* thermal monitoring */
222 if (!xen_initial_domain())
223 cpuid_leaf1_edx_mask &=
224 ~((1 << X86_FEATURE_APIC) | /* disable local APIC */
225 (1 << X86_FEATURE_ACPI)); /* disable ACPI */
229 xen_cpuid(&ax, &bx, &cx, &dx);
231 /* cpuid claims we support xsave; try enabling it to see what happens */
232 if (cx & (1 << (X86_FEATURE_XSAVE % 32))) {
235 set_in_cr4(X86_CR4_OSXSAVE);
239 if ((cr4 & X86_CR4_OSXSAVE) == 0)
240 cpuid_leaf1_ecx_mask &= ~(1 << (X86_FEATURE_XSAVE % 32));
242 clear_in_cr4(X86_CR4_OSXSAVE);
246 static void xen_set_debugreg(int reg, unsigned long val)
248 HYPERVISOR_set_debugreg(reg, val);
251 static unsigned long xen_get_debugreg(int reg)
253 return HYPERVISOR_get_debugreg(reg);
256 static void xen_end_context_switch(struct task_struct *next)
259 paravirt_end_context_switch(next);
262 static unsigned long xen_store_tr(void)
268 * Set the page permissions for a particular virtual address. If the
269 * address is a vmalloc mapping (or other non-linear mapping), then
270 * find the linear mapping of the page and also set its protections to
273 static void set_aliased_prot(void *v, pgprot_t prot)
281 ptep = lookup_address((unsigned long)v, &level);
282 BUG_ON(ptep == NULL);
284 pfn = pte_pfn(*ptep);
285 page = pfn_to_page(pfn);
287 pte = pfn_pte(pfn, prot);
289 if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
292 if (!PageHighMem(page)) {
293 void *av = __va(PFN_PHYS(pfn));
296 if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
302 static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
304 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
307 for(i = 0; i < entries; i += entries_per_page)
308 set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
311 static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
313 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
316 for(i = 0; i < entries; i += entries_per_page)
317 set_aliased_prot(ldt + i, PAGE_KERNEL);
320 static void xen_set_ldt(const void *addr, unsigned entries)
322 struct mmuext_op *op;
323 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
326 op->cmd = MMUEXT_SET_LDT;
327 op->arg1.linear_addr = (unsigned long)addr;
328 op->arg2.nr_ents = entries;
330 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
332 xen_mc_issue(PARAVIRT_LAZY_CPU);
335 static void xen_load_gdt(const struct desc_ptr *dtr)
337 unsigned long va = dtr->address;
338 unsigned int size = dtr->size + 1;
339 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
340 unsigned long frames[pages];
344 * A GDT can be up to 64k in size, which corresponds to 8192
345 * 8-byte entries, or 16 4k pages..
348 BUG_ON(size > 65536);
349 BUG_ON(va & ~PAGE_MASK);
351 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
354 unsigned long pfn, mfn;
358 * The GDT is per-cpu and is in the percpu data area.
359 * That can be virtually mapped, so we need to do a
360 * page-walk to get the underlying MFN for the
361 * hypercall. The page can also be in the kernel's
362 * linear range, so we need to RO that mapping too.
364 ptep = lookup_address(va, &level);
365 BUG_ON(ptep == NULL);
367 pfn = pte_pfn(*ptep);
368 mfn = pfn_to_mfn(pfn);
369 virt = __va(PFN_PHYS(pfn));
373 make_lowmem_page_readonly((void *)va);
374 make_lowmem_page_readonly(virt);
377 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
382 * load_gdt for early boot, when the gdt is only mapped once
384 static __init void xen_load_gdt_boot(const struct desc_ptr *dtr)
386 unsigned long va = dtr->address;
387 unsigned int size = dtr->size + 1;
388 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
389 unsigned long frames[pages];
393 * A GDT can be up to 64k in size, which corresponds to 8192
394 * 8-byte entries, or 16 4k pages..
397 BUG_ON(size > 65536);
398 BUG_ON(va & ~PAGE_MASK);
400 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
402 unsigned long pfn, mfn;
404 pfn = virt_to_pfn(va);
405 mfn = pfn_to_mfn(pfn);
407 pte = pfn_pte(pfn, PAGE_KERNEL_RO);
409 if (HYPERVISOR_update_va_mapping((unsigned long)va, pte, 0))
415 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
419 static void load_TLS_descriptor(struct thread_struct *t,
420 unsigned int cpu, unsigned int i)
422 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
423 xmaddr_t maddr = arbitrary_virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
424 struct multicall_space mc = __xen_mc_entry(0);
426 MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
429 static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
432 * XXX sleazy hack: If we're being called in a lazy-cpu zone
433 * and lazy gs handling is enabled, it means we're in a
434 * context switch, and %gs has just been saved. This means we
435 * can zero it out to prevent faults on exit from the
436 * hypervisor if the next process has no %gs. Either way, it
437 * has been saved, and the new value will get loaded properly.
438 * This will go away as soon as Xen has been modified to not
439 * save/restore %gs for normal hypercalls.
441 * On x86_64, this hack is not used for %gs, because gs points
442 * to KERNEL_GS_BASE (and uses it for PDA references), so we
443 * must not zero %gs on x86_64
445 * For x86_64, we need to zero %fs, otherwise we may get an
446 * exception between the new %fs descriptor being loaded and
447 * %fs being effectively cleared at __switch_to().
449 if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
459 load_TLS_descriptor(t, cpu, 0);
460 load_TLS_descriptor(t, cpu, 1);
461 load_TLS_descriptor(t, cpu, 2);
463 xen_mc_issue(PARAVIRT_LAZY_CPU);
467 static void xen_load_gs_index(unsigned int idx)
469 if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
474 static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
477 xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
478 u64 entry = *(u64 *)ptr;
483 if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
489 static int cvt_gate_to_trap(int vector, const gate_desc *val,
490 struct trap_info *info)
494 if (val->type != GATE_TRAP && val->type != GATE_INTERRUPT)
497 info->vector = vector;
499 addr = gate_offset(*val);
502 * Look for known traps using IST, and substitute them
503 * appropriately. The debugger ones are the only ones we care
504 * about. Xen will handle faults like double_fault and
505 * machine_check, so we should never see them. Warn if
506 * there's an unexpected IST-using fault handler.
508 if (addr == (unsigned long)debug)
509 addr = (unsigned long)xen_debug;
510 else if (addr == (unsigned long)int3)
511 addr = (unsigned long)xen_int3;
512 else if (addr == (unsigned long)stack_segment)
513 addr = (unsigned long)xen_stack_segment;
514 else if (addr == (unsigned long)double_fault ||
515 addr == (unsigned long)nmi) {
516 /* Don't need to handle these */
518 #ifdef CONFIG_X86_MCE
519 } else if (addr == (unsigned long)machine_check) {
523 /* Some other trap using IST? */
524 if (WARN_ON(val->ist != 0))
527 #endif /* CONFIG_X86_64 */
528 info->address = addr;
530 info->cs = gate_segment(*val);
531 info->flags = val->dpl;
532 /* interrupt gates clear IF */
533 if (val->type == GATE_INTERRUPT)
534 info->flags |= 1 << 2;
539 /* Locations of each CPU's IDT */
540 static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
542 /* Set an IDT entry. If the entry is part of the current IDT, then
544 static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
546 unsigned long p = (unsigned long)&dt[entrynum];
547 unsigned long start, end;
551 start = __get_cpu_var(idt_desc).address;
552 end = start + __get_cpu_var(idt_desc).size + 1;
556 native_write_idt_entry(dt, entrynum, g);
558 if (p >= start && (p + 8) <= end) {
559 struct trap_info info[2];
563 if (cvt_gate_to_trap(entrynum, g, &info[0]))
564 if (HYPERVISOR_set_trap_table(info))
571 static void xen_convert_trap_info(const struct desc_ptr *desc,
572 struct trap_info *traps)
574 unsigned in, out, count;
576 count = (desc->size+1) / sizeof(gate_desc);
579 for (in = out = 0; in < count; in++) {
580 gate_desc *entry = (gate_desc*)(desc->address) + in;
582 if (cvt_gate_to_trap(in, entry, &traps[out]))
585 traps[out].address = 0;
588 void xen_copy_trap_info(struct trap_info *traps)
590 const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
592 xen_convert_trap_info(desc, traps);
595 /* Load a new IDT into Xen. In principle this can be per-CPU, so we
596 hold a spinlock to protect the static traps[] array (static because
597 it avoids allocation, and saves stack space). */
598 static void xen_load_idt(const struct desc_ptr *desc)
600 static DEFINE_SPINLOCK(lock);
601 static struct trap_info traps[257];
605 __get_cpu_var(idt_desc) = *desc;
607 xen_convert_trap_info(desc, traps);
610 if (HYPERVISOR_set_trap_table(traps))
616 /* Write a GDT descriptor entry. Ignore LDT descriptors, since
617 they're handled differently. */
618 static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
619 const void *desc, int type)
630 xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
633 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
643 * Version of write_gdt_entry for use at early boot-time needed to
644 * update an entry as simply as possible.
646 static __init void xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
647 const void *desc, int type)
656 xmaddr_t maddr = virt_to_machine(&dt[entry]);
658 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
659 dt[entry] = *(struct desc_struct *)desc;
665 static void xen_load_sp0(struct tss_struct *tss,
666 struct thread_struct *thread)
668 struct multicall_space mcs = xen_mc_entry(0);
669 MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
670 xen_mc_issue(PARAVIRT_LAZY_CPU);
673 static void xen_set_iopl_mask(unsigned mask)
675 struct physdev_set_iopl set_iopl;
677 /* Force the change at ring 0. */
678 set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
679 HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
682 static void xen_io_delay(void)
686 #ifdef CONFIG_X86_LOCAL_APIC
687 static u32 xen_apic_read(u32 reg)
692 static void xen_apic_write(u32 reg, u32 val)
694 /* Warn to see if there's any stray references */
698 static u64 xen_apic_icr_read(void)
703 static void xen_apic_icr_write(u32 low, u32 id)
705 /* Warn to see if there's any stray references */
709 static void xen_apic_wait_icr_idle(void)
714 static u32 xen_safe_apic_wait_icr_idle(void)
719 static void set_xen_basic_apic_ops(void)
721 apic->read = xen_apic_read;
722 apic->write = xen_apic_write;
723 apic->icr_read = xen_apic_icr_read;
724 apic->icr_write = xen_apic_icr_write;
725 apic->wait_icr_idle = xen_apic_wait_icr_idle;
726 apic->safe_wait_icr_idle = xen_safe_apic_wait_icr_idle;
732 static void xen_clts(void)
734 struct multicall_space mcs;
736 mcs = xen_mc_entry(0);
738 MULTI_fpu_taskswitch(mcs.mc, 0);
740 xen_mc_issue(PARAVIRT_LAZY_CPU);
743 static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
745 static unsigned long xen_read_cr0(void)
747 unsigned long cr0 = percpu_read(xen_cr0_value);
749 if (unlikely(cr0 == 0)) {
750 cr0 = native_read_cr0();
751 percpu_write(xen_cr0_value, cr0);
757 static void xen_write_cr0(unsigned long cr0)
759 struct multicall_space mcs;
761 percpu_write(xen_cr0_value, cr0);
763 /* Only pay attention to cr0.TS; everything else is
765 mcs = xen_mc_entry(0);
767 MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
769 xen_mc_issue(PARAVIRT_LAZY_CPU);
772 static void xen_write_cr4(unsigned long cr4)
777 native_write_cr4(cr4);
780 static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
791 case MSR_FS_BASE: which = SEGBASE_FS; goto set;
792 case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
793 case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
796 base = ((u64)high << 32) | low;
797 if (HYPERVISOR_set_segment_base(which, base) != 0)
805 case MSR_SYSCALL_MASK:
806 case MSR_IA32_SYSENTER_CS:
807 case MSR_IA32_SYSENTER_ESP:
808 case MSR_IA32_SYSENTER_EIP:
809 /* Fast syscall setup is all done in hypercalls, so
810 these are all ignored. Stub them out here to stop
811 Xen console noise. */
815 ret = native_write_msr_safe(msr, low, high);
821 void xen_setup_shared_info(void)
823 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
824 set_fixmap(FIX_PARAVIRT_BOOTMAP,
825 xen_start_info->shared_info);
827 HYPERVISOR_shared_info =
828 (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
830 HYPERVISOR_shared_info =
831 (struct shared_info *)__va(xen_start_info->shared_info);
834 /* In UP this is as good a place as any to set up shared info */
835 xen_setup_vcpu_info_placement();
838 xen_setup_mfn_list_list();
841 /* This is called once we have the cpu_possible_map */
842 void xen_setup_vcpu_info_placement(void)
846 for_each_possible_cpu(cpu)
849 /* xen_vcpu_setup managed to place the vcpu_info within the
850 percpu area for all cpus, so make use of it */
851 if (have_vcpu_info_placement) {
852 printk(KERN_INFO "Xen: using vcpu_info placement\n");
854 pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
855 pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
856 pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
857 pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
858 pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
862 static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
863 unsigned long addr, unsigned len)
865 char *start, *end, *reloc;
868 start = end = reloc = NULL;
870 #define SITE(op, x) \
871 case PARAVIRT_PATCH(op.x): \
872 if (have_vcpu_info_placement) { \
873 start = (char *)xen_##x##_direct; \
874 end = xen_##x##_direct_end; \
875 reloc = xen_##x##_direct_reloc; \
880 SITE(pv_irq_ops, irq_enable);
881 SITE(pv_irq_ops, irq_disable);
882 SITE(pv_irq_ops, save_fl);
883 SITE(pv_irq_ops, restore_fl);
887 if (start == NULL || (end-start) > len)
890 ret = paravirt_patch_insns(insnbuf, len, start, end);
892 /* Note: because reloc is assigned from something that
893 appears to be an array, gcc assumes it's non-null,
894 but doesn't know its relationship with start and
896 if (reloc > start && reloc < end) {
897 int reloc_off = reloc - start;
898 long *relocp = (long *)(insnbuf + reloc_off);
899 long delta = start - (char *)addr;
907 ret = paravirt_patch_default(type, clobbers, insnbuf,
915 static const struct pv_info xen_info __initdata = {
916 .paravirt_enabled = 1,
917 .shared_kernel_pmd = 0,
922 static const struct pv_init_ops xen_init_ops __initdata = {
926 static const struct pv_time_ops xen_time_ops __initdata = {
927 .sched_clock = xen_sched_clock,
930 static const struct pv_cpu_ops xen_cpu_ops __initdata = {
933 .set_debugreg = xen_set_debugreg,
934 .get_debugreg = xen_get_debugreg,
938 .read_cr0 = xen_read_cr0,
939 .write_cr0 = xen_write_cr0,
941 .read_cr4 = native_read_cr4,
942 .read_cr4_safe = native_read_cr4_safe,
943 .write_cr4 = xen_write_cr4,
945 .wbinvd = native_wbinvd,
947 .read_msr = native_read_msr_safe,
948 .write_msr = xen_write_msr_safe,
949 .read_tsc = native_read_tsc,
950 .read_pmc = native_read_pmc,
953 .irq_enable_sysexit = xen_sysexit,
955 .usergs_sysret32 = xen_sysret32,
956 .usergs_sysret64 = xen_sysret64,
959 .load_tr_desc = paravirt_nop,
960 .set_ldt = xen_set_ldt,
961 .load_gdt = xen_load_gdt,
962 .load_idt = xen_load_idt,
963 .load_tls = xen_load_tls,
965 .load_gs_index = xen_load_gs_index,
968 .alloc_ldt = xen_alloc_ldt,
969 .free_ldt = xen_free_ldt,
971 .store_gdt = native_store_gdt,
972 .store_idt = native_store_idt,
973 .store_tr = xen_store_tr,
975 .write_ldt_entry = xen_write_ldt_entry,
976 .write_gdt_entry = xen_write_gdt_entry,
977 .write_idt_entry = xen_write_idt_entry,
978 .load_sp0 = xen_load_sp0,
980 .set_iopl_mask = xen_set_iopl_mask,
981 .io_delay = xen_io_delay,
983 /* Xen takes care of %gs when switching to usermode for us */
984 .swapgs = paravirt_nop,
986 .start_context_switch = paravirt_start_context_switch,
987 .end_context_switch = xen_end_context_switch,
990 static const struct pv_apic_ops xen_apic_ops __initdata = {
991 #ifdef CONFIG_X86_LOCAL_APIC
992 .startup_ipi_hook = paravirt_nop,
996 static void xen_reboot(int reason)
998 struct sched_shutdown r = { .reason = reason };
1004 if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
1008 static void xen_restart(char *msg)
1010 xen_reboot(SHUTDOWN_reboot);
1013 static void xen_emergency_restart(void)
1015 xen_reboot(SHUTDOWN_reboot);
1018 static void xen_machine_halt(void)
1020 xen_reboot(SHUTDOWN_poweroff);
1023 static void xen_crash_shutdown(struct pt_regs *regs)
1025 xen_reboot(SHUTDOWN_crash);
1028 static const struct machine_ops __initdata xen_machine_ops = {
1029 .restart = xen_restart,
1030 .halt = xen_machine_halt,
1031 .power_off = xen_machine_halt,
1032 .shutdown = xen_machine_halt,
1033 .crash_shutdown = xen_crash_shutdown,
1034 .emergency_restart = xen_emergency_restart,
1038 * Set up the GDT and segment registers for -fstack-protector. Until
1039 * we do this, we have to be careful not to call any stack-protected
1040 * function, which is most of the kernel.
1042 static void __init xen_setup_stackprotector(void)
1044 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
1045 pv_cpu_ops.load_gdt = xen_load_gdt_boot;
1047 setup_stack_canary_segment(0);
1048 switch_to_new_gdt(0);
1050 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
1051 pv_cpu_ops.load_gdt = xen_load_gdt;
1054 /* First C function to be called on Xen boot */
1055 asmlinkage void __init xen_start_kernel(void)
1059 if (!xen_start_info)
1062 xen_domain_type = XEN_PV_DOMAIN;
1064 /* Install Xen paravirt ops */
1066 pv_init_ops = xen_init_ops;
1067 pv_time_ops = xen_time_ops;
1068 pv_cpu_ops = xen_cpu_ops;
1069 pv_apic_ops = xen_apic_ops;
1071 x86_init.resources.memory_setup = xen_memory_setup;
1072 x86_init.oem.arch_setup = xen_arch_setup;
1073 x86_init.oem.banner = xen_banner;
1075 x86_init.timers.timer_init = xen_time_init;
1076 x86_init.timers.setup_percpu_clockev = x86_init_noop;
1077 x86_cpuinit.setup_percpu_clockev = x86_init_noop;
1079 x86_platform.calibrate_tsc = xen_tsc_khz;
1080 x86_platform.get_wallclock = xen_get_wallclock;
1081 x86_platform.set_wallclock = xen_set_wallclock;
1084 * Set up some pagetable state before starting to set any ptes.
1089 /* Prevent unwanted bits from being set in PTEs. */
1090 __supported_pte_mask &= ~_PAGE_GLOBAL;
1091 if (!xen_initial_domain())
1092 __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
1094 __supported_pte_mask |= _PAGE_IOMAP;
1096 #ifdef CONFIG_X86_64
1097 /* Work out if we support NX */
1101 xen_setup_features();
1104 if (!xen_feature(XENFEAT_auto_translated_physmap))
1105 xen_build_dynamic_phys_to_machine();
1108 * Set up kernel GDT and segment registers, mainly so that
1109 * -fstack-protector code can be executed.
1111 xen_setup_stackprotector();
1114 xen_init_cpuid_mask();
1116 #ifdef CONFIG_X86_LOCAL_APIC
1118 * set up the basic apic ops.
1120 set_xen_basic_apic_ops();
1123 if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
1124 pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
1125 pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
1128 machine_ops = xen_machine_ops;
1131 * The only reliable way to retain the initial address of the
1132 * percpu gdt_page is to remember it here, so we can go and
1133 * mark it RW later, when the initial percpu area is freed.
1135 xen_initial_gdt = &per_cpu(gdt_page, 0);
1139 pgd = (pgd_t *)xen_start_info->pt_base;
1141 /* Don't do the full vcpu_info placement stuff until we have a
1142 possible map and a non-dummy shared_info. */
1143 per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
1145 local_irq_disable();
1146 early_boot_irqs_off();
1148 xen_raw_console_write("mapping kernel into physical memory\n");
1149 pgd = xen_setup_kernel_pagetable(pgd, xen_start_info->nr_pages);
1153 /* keep using Xen gdt for now; no urgent need to change it */
1155 pv_info.kernel_rpl = 1;
1156 if (xen_feature(XENFEAT_supervisor_mode_kernel))
1157 pv_info.kernel_rpl = 0;
1159 /* set the limit of our address space */
1162 #ifdef CONFIG_X86_32
1163 /* set up basic CPUID stuff */
1164 cpu_detect(&new_cpu_data);
1165 new_cpu_data.hard_math = 1;
1166 new_cpu_data.wp_works_ok = 1;
1167 new_cpu_data.x86_capability[0] = cpuid_edx(1);
1170 /* Poke various useful things into boot_params */
1171 boot_params.hdr.type_of_loader = (9 << 4) | 0;
1172 boot_params.hdr.ramdisk_image = xen_start_info->mod_start
1173 ? __pa(xen_start_info->mod_start) : 0;
1174 boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
1175 boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
1177 if (!xen_initial_domain()) {
1178 add_preferred_console("xenboot", 0, NULL);
1179 add_preferred_console("tty", 0, NULL);
1180 add_preferred_console("hvc", 0, NULL);
1183 xen_raw_console_write("about to get started...\n");
1185 /* Start the world */
1186 #ifdef CONFIG_X86_32
1187 i386_start_kernel();
1189 x86_64_start_reservations((char *)__pa_symbol(&boot_params));