15 #include <linux/kernel.h>
23 #include <linux/sched.h>
26 #include <linux/module.h>
31 #include <linux/pci.h>
36 #include <xen/events.h>
49 #include <asm/paravirt.h>
53 #include <asm/xen/hypercall.h>
54 #include <asm/xen/hypervisor.h>
55 #include <asm/fixmap.h>
56 #include <asm/processor.h>
59 #include <asm/traps.h>
60 #include <asm/setup.h>
62 #include <asm/pgalloc.h>
63 #include <asm/pgtable.h>
64 #include <asm/tlbflush.h>
65 #include <asm/reboot.h>
66 #include <asm/stackprotector.h>
67 #include <asm/hypervisor.h>
127 static int have_vcpu_info_placement = 1;
142 static void clamp_max_cpus(
void)
150 static void xen_vcpu_setup(
int cpu)
161 if (!have_vcpu_info_placement) {
167 vcpup = &
per_cpu(xen_vcpu_info, cpu);
178 have_vcpu_info_placement = 0;
183 per_cpu(xen_vcpu, cpu) = vcpup;
205 if (have_vcpu_info_placement)
214 static void __init xen_banner(
void)
223 version >> 16, version & 0xffff,
extra.extraversion,
227 #define CPUID_THERM_POWER_LEAF 6
228 #define APERFMPERF_PRESENT 0
237 static void xen_cpuid(
unsigned int *ax,
unsigned int *bx,
238 unsigned int *cx,
unsigned int *dx)
240 unsigned maskebx = ~0;
241 unsigned maskecx = ~0;
242 unsigned maskedx = ~0;
250 maskecx = cpuid_leaf1_ecx_mask;
251 setecx = cpuid_leaf1_ecx_set_mask;
252 maskedx = cpuid_leaf1_edx_mask;
259 *cx = cpuid_leaf5_ecx_val;
260 *dx = cpuid_leaf5_edx_val;
279 :
"0" (*ax),
"2" (*cx));
288 static bool __init xen_check_mwait(
void)
290 #if defined(CONFIG_ACPI) && !defined(CONFIG_ACPI_PROCESSOR_AGGREGATOR) && \
291 !defined(CONFIG_ACPI_PROCESSOR_AGGREGATOR_MODULE)
294 .u.set_pminfo.id = -1,
298 unsigned int ax, bx, cx, dx;
299 unsigned int mwait_mask;
315 native_cpuid(&ax, &bx, &cx, &dx);
320 if ((cx & mwait_mask) != mwait_mask)
332 native_cpuid(&ax, &bx, &cx, &dx);
343 if ((HYPERVISOR_dom0_op(&op) == 0) &&
345 cpuid_leaf5_ecx_val = cx;
346 cpuid_leaf5_edx_val = dx;
353 static void __init xen_init_cpuid_mask(
void)
355 unsigned int ax, bx, cx, dx;
356 unsigned int xsave_mask;
358 cpuid_leaf1_edx_mask =
363 cpuid_leaf1_edx_mask &=
368 xen_cpuid(&ax, &bx, &cx, &dx);
375 if ((cx & xsave_mask) != xsave_mask)
376 cpuid_leaf1_ecx_mask &= ~xsave_mask;
377 if (xen_check_mwait())
381 static void xen_set_debugreg(
int reg,
unsigned long val)
383 HYPERVISOR_set_debugreg(reg, val);
386 static unsigned long xen_get_debugreg(
int reg)
388 return HYPERVISOR_get_debugreg(reg);
397 static unsigned long xen_store_tr(
void)
408 static void set_aliased_prot(
void *
v,
pgprot_t prot)
427 if (!PageHighMem(page)) {
442 for(i = 0; i <
entries; i += entries_per_page)
446 static void xen_free_ldt(
struct desc_struct *ldt,
unsigned entries)
451 for(i = 0; i <
entries; i += entries_per_page)
452 set_aliased_prot(ldt + i, PAGE_KERNEL);
455 static void xen_set_ldt(
const void *
addr,
unsigned entries)
460 trace_xen_cpu_set_ldt(addr, entries);
472 static void xen_load_gdt(
const struct desc_ptr *
dtr)
477 unsigned long frames[
pages];
491 unsigned long pfn, mfn;
514 if (HYPERVISOR_set_gdt(frames, size /
sizeof(
struct desc_struct)))
523 unsigned long va = dtr->
address;
524 unsigned int size = dtr->
size + 1;
526 unsigned long frames[
pages];
539 unsigned long pfn, mfn;
552 if (HYPERVISOR_set_gdt(frames, size /
sizeof(
struct desc_struct)))
559 return d1->
a == d2->
a && d1->
b == d2->
b;
563 unsigned int cpu,
unsigned int i)
570 if (desc_equal(shadow, &t->
tls_array[i]))
575 gdt = get_cpu_gdt_table(cpu);
582 static void xen_load_tls(
struct thread_struct *t,
unsigned int cpu)
612 load_TLS_descriptor(t, cpu, 0);
613 load_TLS_descriptor(t, cpu, 1);
614 load_TLS_descriptor(t, cpu, 2);
620 static void xen_load_gs_index(
unsigned int idx)
627 static void xen_write_ldt_entry(
struct desc_struct *dt,
int entrynum,
633 trace_xen_cpu_write_ldt_entry(dt, entrynum, entry);
638 if (HYPERVISOR_update_descriptor(mach_lp.
maddr, entry))
663 if (addr == (
unsigned long)
debug)
665 else if (addr == (
unsigned long)
int3)
673 #ifdef CONFIG_X86_MCE
674 }
else if (addr == (
unsigned long)machine_check) {
693 info->
flags |= 1 << 2;
705 unsigned long p = (
unsigned long)&dt[entrynum];
708 trace_xen_cpu_write_idt_entry(dt, entrynum, g);
717 native_write_idt_entry(dt, entrynum, g);
719 if (p >= start && (p + 8) <= end) {
724 if (cvt_gate_to_trap(entrynum, g, &info[0]))
725 if (HYPERVISOR_set_trap_table(info))
732 static void xen_convert_trap_info(
const struct desc_ptr *
desc,
740 for (in = out = 0; in <
count; in++) {
743 if (cvt_gate_to_trap(in, entry, &traps[out]))
753 xen_convert_trap_info(desc, traps);
759 static void xen_load_idt(
const struct desc_ptr *desc)
764 trace_xen_cpu_load_idt(desc);
770 xen_convert_trap_info(desc, traps);
773 if (HYPERVISOR_set_trap_table(traps))
781 static void xen_write_gdt_entry(
struct desc_struct *dt,
int entry,
782 const void *desc,
int type)
784 trace_xen_cpu_write_gdt_entry(dt, entry, desc, type);
798 if (HYPERVISOR_update_descriptor(maddr.
maddr, *(
u64 *)desc))
812 const void *desc,
int type)
814 trace_xen_cpu_write_gdt_entry(dt, entry, desc, type);
825 if (HYPERVISOR_update_descriptor(maddr.
maddr, *(
u64 *)desc))
837 mcs = xen_mc_entry(0);
842 static void xen_set_iopl_mask(
unsigned mask)
847 set_iopl.
iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
851 static void xen_io_delay(
void)
855 #ifdef CONFIG_X86_LOCAL_APIC
856 static unsigned long xen_set_apic_id(
unsigned int x)
861 static unsigned int xen_get_apic_id(
unsigned long x)
863 return ((x)>>24) & 0xFF
u;
870 .u.pcpu_info.xen_cpuid = 0,
885 ret = HYPERVISOR_dom0_op(&op);
892 static void xen_apic_write(
u32 reg,
u32 val)
898 static u64 xen_apic_icr_read(
void)
903 static void xen_apic_icr_write(
u32 low,
u32 id)
909 static void xen_apic_wait_icr_idle(
void)
914 static u32 xen_safe_apic_wait_icr_idle(
void)
919 static void set_xen_basic_apic_ops(
void)
941 static void xen_clts(
void)
945 mcs = xen_mc_entry(0);
947 MULTI_fpu_taskswitch(mcs.mc, 0);
954 static unsigned long xen_read_cr0(
void)
959 cr0 = native_read_cr0();
966 static void xen_write_cr0(
unsigned long cr0)
974 mcs = xen_mc_entry(0);
981 static void xen_write_cr4(
unsigned long cr4)
986 native_write_cr4(cr4);
989 static inline unsigned long xen_read_cr8(
void)
993 static inline void xen_write_cr8(
unsigned long val)
998 static int xen_write_msr_safe(
unsigned int msr,
unsigned low,
unsigned high)
1005 #ifdef CONFIG_X86_64
1014 base = ((
u64)high << 32) |
low;
1015 if (HYPERVISOR_set_segment_base(which, base) != 0)
1038 ret = native_write_msr_safe(msr, low, high);
1050 HYPERVISOR_shared_info =
1051 (
struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
1053 HYPERVISOR_shared_info =
1070 xen_vcpu_setup(cpu);
1074 if (have_vcpu_info_placement) {
1083 static unsigned xen_patch(
u8 type,
u16 clobbers,
void *insnbuf,
1084 unsigned long addr,
unsigned len)
1089 start = end = reloc =
NULL;
1091 #define SITE(op, x) \
1092 case PARAVIRT_PATCH(op.x): \
1093 if (have_vcpu_info_placement) { \
1094 start = (char *)xen_##x##_direct; \
1095 end = xen_##x##_direct_end; \
1096 reloc = xen_##x##_direct_reloc; \
1108 if (start ==
NULL || (end-start) > len)
1117 if (reloc > start && reloc < end) {
1118 int reloc_off = reloc -
start;
1119 long *relocp = (
long *)(insnbuf + reloc_off);
1120 long delta = start - (
char *)addr;
1137 .paravirt_enabled = 1,
1138 .shared_kernel_pmd = 0,
1140 #ifdef CONFIG_X86_64
1147 static const struct pv_init_ops xen_init_ops __initconst = {
1151 static const struct pv_cpu_ops xen_cpu_ops __initconst = {
1154 .set_debugreg = xen_set_debugreg,
1155 .get_debugreg = xen_get_debugreg,
1159 .read_cr0 = xen_read_cr0,
1160 .write_cr0 = xen_write_cr0,
1162 .read_cr4 = native_read_cr4,
1163 .read_cr4_safe = native_read_cr4_safe,
1164 .write_cr4 = xen_write_cr4,
1166 #ifdef CONFIG_X86_64
1167 .read_cr8 = xen_read_cr8,
1168 .write_cr8 = xen_write_cr8,
1171 .wbinvd = native_wbinvd,
1173 .read_msr = native_read_msr_safe,
1174 .write_msr = xen_write_msr_safe,
1177 .read_pmc = native_read_pmc,
1179 .read_tscp = native_read_tscp,
1183 #ifdef CONFIG_X86_64
1189 .set_ldt = xen_set_ldt,
1190 .load_gdt = xen_load_gdt,
1191 .load_idt = xen_load_idt,
1192 .load_tls = xen_load_tls,
1193 #ifdef CONFIG_X86_64
1194 .load_gs_index = xen_load_gs_index,
1197 .alloc_ldt = xen_alloc_ldt,
1198 .free_ldt = xen_free_ldt,
1200 .store_gdt = native_store_gdt,
1201 .store_idt = native_store_idt,
1202 .store_tr = xen_store_tr,
1204 .write_ldt_entry = xen_write_ldt_entry,
1205 .write_gdt_entry = xen_write_gdt_entry,
1206 .write_idt_entry = xen_write_idt_entry,
1207 .load_sp0 = xen_load_sp0,
1209 .set_iopl_mask = xen_set_iopl_mask,
1210 .io_delay = xen_io_delay,
1216 .end_context_switch = xen_end_context_switch,
1219 static const struct pv_apic_ops xen_apic_ops __initconst = {
1220 #ifdef CONFIG_X86_LOCAL_APIC
1225 static void xen_reboot(
int reason)
1233 static void xen_restart(
char *
msg)
1238 static void xen_emergency_restart(
void)
1243 static void xen_machine_halt(
void)
1248 static void xen_machine_power_off(
void)
1255 static void xen_crash_shutdown(
struct pt_regs *
regs)
1268 .notifier_call= xen_panic_event,
1277 static const struct machine_ops xen_machine_ops __initconst = {
1278 .restart = xen_restart,
1279 .halt = xen_machine_halt,
1280 .power_off = xen_machine_power_off,
1281 .shutdown = xen_machine_halt,
1282 .crash_shutdown = xen_crash_shutdown,
1283 .emergency_restart = xen_emergency_restart,
1291 static void __init xen_setup_stackprotector(
void)
1296 setup_stack_canary_segment(0);
1309 if (!xen_start_info)
1362 xen_setup_stackprotector();
1365 xen_init_cpuid_mask();
1367 #ifdef CONFIG_X86_LOCAL_APIC
1371 set_xen_basic_apic_ops();
1390 #ifdef CONFIG_ACPI_NUMA
1414 #ifdef CONFIG_X86_32
1433 #ifdef CONFIG_X86_32
1456 (
void *)((
char *)xen_start_info +
1468 if (HYPERVISOR_dom0_op(&op) == 0)
1476 xen_acpi_sleep_register();
1491 #ifdef CONFIG_X86_32
1504 if (!shared_info_page)
1514 HYPERVISOR_shared_info = (
struct shared_info *)shared_info_page;
1529 #ifdef CONFIG_XEN_PVHVM
1530 static void __init init_hvm_pv_info(
void)
1536 base = xen_cpuid_base();
1537 cpuid(base + 1, &eax, &ebx, &ecx, &edx);
1540 minor = eax & 0xffff;
1543 cpuid(base + 2, &pages, &msr, &ecx, &edx);
1546 wrmsr_safe(msr, (
u32)pfn, (
u32)(pfn >> 32));
1556 unsigned long action,
void *hcpu)
1558 int cpu = (
long)hcpu;
1561 xen_vcpu_setup(cpu);
1572 .notifier_call = xen_hvm_cpu_notify,
1575 static void __init xen_hvm_guest_init(
void)
1584 register_cpu_notifier(&xen_hvm_cpu_notifier);
1591 static bool __init xen_hvm_platform(
void)
1596 if (!xen_cpuid_base())
1602 bool xen_hvm_need_lapic(
void)
1616 .detect = xen_hvm_platform,
1617 .init_platform = xen_hvm_guest_init,