enlighten.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  1. /*
  2. * Core of Xen paravirt_ops implementation.
  3. *
  4. * This file contains the xen_paravirt_ops structure itself, and the
  5. * implementations for:
  6. * - privileged instructions
  7. * - interrupt flags
  8. * - segment operations
  9. * - booting and setup
  10. *
  11. * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
  12. */
  13. #include <linux/cpu.h>
  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>
  26. #include <linux/mm.h>
  27. #include <linux/page-flags.h>
  28. #include <linux/highmem.h>
  29. #include <linux/console.h>
  30. #include <linux/pci.h>
  31. #include <linux/gfp.h>
  32. #include <linux/memblock.h>
  33. #include <linux/edd.h>
  34. #include <xen/xen.h>
  35. #include <xen/events.h>
  36. #include <xen/interface/xen.h>
  37. #include <xen/interface/version.h>
  38. #include <xen/interface/physdev.h>
  39. #include <xen/interface/vcpu.h>
  40. #include <xen/interface/memory.h>
  41. #include <xen/interface/nmi.h>
  42. #include <xen/interface/xen-mca.h>
  43. #include <xen/features.h>
  44. #include <xen/page.h>
  45. #include <xen/hvm.h>
  46. #include <xen/hvc-console.h>
  47. #include <xen/acpi.h>
  48. #include <asm/paravirt.h>
  49. #include <asm/apic.h>
  50. #include <asm/page.h>
  51. #include <asm/xen/pci.h>
  52. #include <asm/xen/hypercall.h>
  53. #include <asm/xen/hypervisor.h>
  54. #include <asm/fixmap.h>
  55. #include <asm/processor.h>
  56. #include <asm/proto.h>
  57. #include <asm/msr-index.h>
  58. #include <asm/traps.h>
  59. #include <asm/setup.h>
  60. #include <asm/desc.h>
  61. #include <asm/pgalloc.h>
  62. #include <asm/pgtable.h>
  63. #include <asm/tlbflush.h>
  64. #include <asm/reboot.h>
  65. #include <asm/stackprotector.h>
  66. #include <asm/hypervisor.h>
  67. #include <asm/mach_traps.h>
  68. #include <asm/mwait.h>
  69. #include <asm/pci_x86.h>
  70. #include <asm/pat.h>
  71. #ifdef CONFIG_ACPI
  72. #include <linux/acpi.h>
  73. #include <asm/acpi.h>
  74. #include <acpi/pdc_intel.h>
  75. #include <acpi/processor.h>
  76. #include <xen/interface/platform.h>
  77. #endif
  78. #include "xen-ops.h"
  79. #include "mmu.h"
  80. #include "smp.h"
  81. #include "multicalls.h"
  82. EXPORT_SYMBOL_GPL(hypercall_page);
  83. /*
  84. * Pointer to the xen_vcpu_info structure or
  85. * &HYPERVISOR_shared_info->vcpu_info[cpu]. See xen_hvm_init_shared_info
  86. * and xen_vcpu_setup for details. By default it points to share_info->vcpu_info
  87. * but if the hypervisor supports VCPUOP_register_vcpu_info then it can point
  88. * to xen_vcpu_info. The pointer is used in __xen_evtchn_do_upcall to
  89. * acknowledge pending events.
  90. * Also more subtly it is used by the patched version of irq enable/disable
  91. * e.g. xen_irq_enable_direct and xen_iret in PV mode.
  92. *
  93. * The desire to be able to do those mask/unmask operations as a single
  94. * instruction by using the per-cpu offset held in %gs is the real reason
  95. * vcpu info is in a per-cpu pointer and the original reason for this
  96. * hypercall.
  97. *
  98. */
  99. DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
  100. /*
  101. * Per CPU pages used if hypervisor supports VCPUOP_register_vcpu_info
  102. * hypercall. This can be used both in PV and PVHVM mode. The structure
  103. * overrides the default per_cpu(xen_vcpu, cpu) value.
  104. */
  105. DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
  106. enum xen_domain_type xen_domain_type = XEN_NATIVE;
  107. EXPORT_SYMBOL_GPL(xen_domain_type);
  108. unsigned long *machine_to_phys_mapping = (void *)MACH2PHYS_VIRT_START;
  109. EXPORT_SYMBOL(machine_to_phys_mapping);
  110. unsigned long machine_to_phys_nr;
  111. EXPORT_SYMBOL(machine_to_phys_nr);
  112. struct start_info *xen_start_info;
  113. EXPORT_SYMBOL_GPL(xen_start_info);
  114. struct shared_info xen_dummy_shared_info;
  115. void *xen_initial_gdt;
  116. RESERVE_BRK(shared_info_page_brk, PAGE_SIZE);
  117. __read_mostly int xen_have_vector_callback;
  118. EXPORT_SYMBOL_GPL(xen_have_vector_callback);
  119. /*
  120. * Point at some empty memory to start with. We map the real shared_info
  121. * page as soon as fixmap is up and running.
  122. */
  123. struct shared_info *HYPERVISOR_shared_info = &xen_dummy_shared_info;
  124. /*
  125. * Flag to determine whether vcpu info placement is available on all
  126. * VCPUs. We assume it is to start with, and then set it to zero on
  127. * the first failure. This is because it can succeed on some VCPUs
  128. * and not others, since it can involve hypervisor memory allocation,
  129. * or because the guest failed to guarantee all the appropriate
  130. * constraints on all VCPUs (ie buffer can't cross a page boundary).
  131. *
  132. * Note that any particular CPU may be using a placed vcpu structure,
  133. * but we can only optimise if the all are.
  134. *
  135. * 0: not available, 1: available
  136. */
  137. static int have_vcpu_info_placement = 1;
  138. struct tls_descs {
  139. struct desc_struct desc[3];
  140. };
  141. /*
  142. * Updating the 3 TLS descriptors in the GDT on every task switch is
  143. * surprisingly expensive so we avoid updating them if they haven't
  144. * changed. Since Xen writes different descriptors than the one
  145. * passed in the update_descriptor hypercall we keep shadow copies to
  146. * compare against.
  147. */
  148. static DEFINE_PER_CPU(struct tls_descs, shadow_tls_desc);
  149. static void clamp_max_cpus(void)
  150. {
  151. #ifdef CONFIG_SMP
  152. if (setup_max_cpus > MAX_VIRT_CPUS)
  153. setup_max_cpus = MAX_VIRT_CPUS;
  154. #endif
  155. }
  156. static void xen_vcpu_setup(int cpu)
  157. {
  158. struct vcpu_register_vcpu_info info;
  159. int err;
  160. struct vcpu_info *vcpup;
  161. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  162. /*
  163. * This path is called twice on PVHVM - first during bootup via
  164. * smp_init -> xen_hvm_cpu_notify, and then if the VCPU is being
  165. * hotplugged: cpu_up -> xen_hvm_cpu_notify.
  166. * As we can only do the VCPUOP_register_vcpu_info once lets
  167. * not over-write its result.
  168. *
  169. * For PV it is called during restore (xen_vcpu_restore) and bootup
  170. * (xen_setup_vcpu_info_placement). The hotplug mechanism does not
  171. * use this function.
  172. */
  173. if (xen_hvm_domain()) {
  174. if (per_cpu(xen_vcpu, cpu) == &per_cpu(xen_vcpu_info, cpu))
  175. return;
  176. }
  177. if (cpu < MAX_VIRT_CPUS)
  178. per_cpu(xen_vcpu,cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
  179. if (!have_vcpu_info_placement) {
  180. if (cpu >= MAX_VIRT_CPUS)
  181. clamp_max_cpus();
  182. return;
  183. }
  184. vcpup = &per_cpu(xen_vcpu_info, cpu);
  185. info.mfn = arbitrary_virt_to_mfn(vcpup);
  186. info.offset = offset_in_page(vcpup);
  187. /* Check to see if the hypervisor will put the vcpu_info
  188. structure where we want it, which allows direct access via
  189. a percpu-variable.
  190. N.B. This hypercall can _only_ be called once per CPU. Subsequent
  191. calls will error out with -EINVAL. This is due to the fact that
  192. hypervisor has no unregister variant and this hypercall does not
  193. allow to over-write info.mfn and info.offset.
  194. */
  195. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
  196. if (err) {
  197. printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
  198. have_vcpu_info_placement = 0;
  199. clamp_max_cpus();
  200. } else {
  201. /* This cpu is using the registered vcpu info, even if
  202. later ones fail to. */
  203. per_cpu(xen_vcpu, cpu) = vcpup;
  204. }
  205. }
  206. /*
  207. * On restore, set the vcpu placement up again.
  208. * If it fails, then we're in a bad state, since
  209. * we can't back out from using it...
  210. */
  211. void xen_vcpu_restore(void)
  212. {
  213. int cpu;
  214. for_each_possible_cpu(cpu) {
  215. bool other_cpu = (cpu != smp_processor_id());
  216. bool is_up = HYPERVISOR_vcpu_op(VCPUOP_is_up, cpu, NULL);
  217. if (other_cpu && is_up &&
  218. HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
  219. BUG();
  220. xen_setup_runstate_info(cpu);
  221. if (have_vcpu_info_placement)
  222. xen_vcpu_setup(cpu);
  223. if (other_cpu && is_up &&
  224. HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
  225. BUG();
  226. }
  227. }
  228. static void __init xen_banner(void)
  229. {
  230. unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
  231. struct xen_extraversion extra;
  232. HYPERVISOR_xen_version(XENVER_extraversion, &extra);
  233. pr_info("Booting paravirtualized kernel %son %s\n",
  234. xen_feature(XENFEAT_auto_translated_physmap) ?
  235. "with PVH extensions " : "", pv_info.name);
  236. printk(KERN_INFO "Xen version: %d.%d%s%s\n",
  237. version >> 16, version & 0xffff, extra.extraversion,
  238. xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
  239. }
  240. /* Check if running on Xen version (major, minor) or later */
  241. bool
  242. xen_running_on_version_or_later(unsigned int major, unsigned int minor)
  243. {
  244. unsigned int version;
  245. if (!xen_domain())
  246. return false;
  247. version = HYPERVISOR_xen_version(XENVER_version, NULL);
  248. if ((((version >> 16) == major) && ((version & 0xffff) >= minor)) ||
  249. ((version >> 16) > major))
  250. return true;
  251. return false;
  252. }
  253. #define CPUID_THERM_POWER_LEAF 6
  254. #define APERFMPERF_PRESENT 0
  255. static __read_mostly unsigned int cpuid_leaf1_edx_mask = ~0;
  256. static __read_mostly unsigned int cpuid_leaf1_ecx_mask = ~0;
  257. static __read_mostly unsigned int cpuid_leaf1_ecx_set_mask;
  258. static __read_mostly unsigned int cpuid_leaf5_ecx_val;
  259. static __read_mostly unsigned int cpuid_leaf5_edx_val;
  260. static void xen_cpuid(unsigned int *ax, unsigned int *bx,
  261. unsigned int *cx, unsigned int *dx)
  262. {
  263. unsigned maskebx = ~0;
  264. unsigned maskecx = ~0;
  265. unsigned maskedx = ~0;
  266. unsigned setecx = 0;
  267. /*
  268. * Mask out inconvenient features, to try and disable as many
  269. * unsupported kernel subsystems as possible.
  270. */
  271. switch (*ax) {
  272. case 1:
  273. maskecx = cpuid_leaf1_ecx_mask;
  274. setecx = cpuid_leaf1_ecx_set_mask;
  275. maskedx = cpuid_leaf1_edx_mask;
  276. break;
  277. case CPUID_MWAIT_LEAF:
  278. /* Synthesize the values.. */
  279. *ax = 0;
  280. *bx = 0;
  281. *cx = cpuid_leaf5_ecx_val;
  282. *dx = cpuid_leaf5_edx_val;
  283. return;
  284. case CPUID_THERM_POWER_LEAF:
  285. /* Disabling APERFMPERF for kernel usage */
  286. maskecx = ~(1 << APERFMPERF_PRESENT);
  287. break;
  288. case 0xb:
  289. /* Suppress extended topology stuff */
  290. maskebx = 0;
  291. break;
  292. }
  293. asm(XEN_EMULATE_PREFIX "cpuid"
  294. : "=a" (*ax),
  295. "=b" (*bx),
  296. "=c" (*cx),
  297. "=d" (*dx)
  298. : "0" (*ax), "2" (*cx));
  299. *bx &= maskebx;
  300. *cx &= maskecx;
  301. *cx |= setecx;
  302. *dx &= maskedx;
  303. }
  304. static bool __init xen_check_mwait(void)
  305. {
  306. #ifdef CONFIG_ACPI
  307. struct xen_platform_op op = {
  308. .cmd = XENPF_set_processor_pminfo,
  309. .u.set_pminfo.id = -1,
  310. .u.set_pminfo.type = XEN_PM_PDC,
  311. };
  312. uint32_t buf[3];
  313. unsigned int ax, bx, cx, dx;
  314. unsigned int mwait_mask;
  315. /* We need to determine whether it is OK to expose the MWAIT
  316. * capability to the kernel to harvest deeper than C3 states from ACPI
  317. * _CST using the processor_harvest_xen.c module. For this to work, we
  318. * need to gather the MWAIT_LEAF values (which the cstate.c code
  319. * checks against). The hypervisor won't expose the MWAIT flag because
  320. * it would break backwards compatibility; so we will find out directly
  321. * from the hardware and hypercall.
  322. */
  323. if (!xen_initial_domain())
  324. return false;
  325. /*
  326. * When running under platform earlier than Xen4.2, do not expose
  327. * mwait, to avoid the risk of loading native acpi pad driver
  328. */
  329. if (!xen_running_on_version_or_later(4, 2))
  330. return false;
  331. ax = 1;
  332. cx = 0;
  333. native_cpuid(&ax, &bx, &cx, &dx);
  334. mwait_mask = (1 << (X86_FEATURE_EST % 32)) |
  335. (1 << (X86_FEATURE_MWAIT % 32));
  336. if ((cx & mwait_mask) != mwait_mask)
  337. return false;
  338. /* We need to emulate the MWAIT_LEAF and for that we need both
  339. * ecx and edx. The hypercall provides only partial information.
  340. */
  341. ax = CPUID_MWAIT_LEAF;
  342. bx = 0;
  343. cx = 0;
  344. dx = 0;
  345. native_cpuid(&ax, &bx, &cx, &dx);
  346. /* Ask the Hypervisor whether to clear ACPI_PDC_C_C2C3_FFH. If so,
  347. * don't expose MWAIT_LEAF and let ACPI pick the IOPORT version of C3.
  348. */
  349. buf[0] = ACPI_PDC_REVISION_ID;
  350. buf[1] = 1;
  351. buf[2] = (ACPI_PDC_C_CAPABILITY_SMP | ACPI_PDC_EST_CAPABILITY_SWSMP);
  352. set_xen_guest_handle(op.u.set_pminfo.pdc, buf);
  353. if ((HYPERVISOR_dom0_op(&op) == 0) &&
  354. (buf[2] & (ACPI_PDC_C_C1_FFH | ACPI_PDC_C_C2C3_FFH))) {
  355. cpuid_leaf5_ecx_val = cx;
  356. cpuid_leaf5_edx_val = dx;
  357. }
  358. return true;
  359. #else
  360. return false;
  361. #endif
  362. }
  363. static void __init xen_init_cpuid_mask(void)
  364. {
  365. unsigned int ax, bx, cx, dx;
  366. unsigned int xsave_mask;
  367. cpuid_leaf1_edx_mask =
  368. ~((1 << X86_FEATURE_MTRR) | /* disable MTRR */
  369. (1 << X86_FEATURE_ACC)); /* thermal monitoring */
  370. if (!xen_initial_domain())
  371. cpuid_leaf1_edx_mask &=
  372. ~((1 << X86_FEATURE_ACPI)); /* disable ACPI */
  373. cpuid_leaf1_ecx_mask &= ~(1 << (X86_FEATURE_X2APIC % 32));
  374. ax = 1;
  375. cx = 0;
  376. cpuid(1, &ax, &bx, &cx, &dx);
  377. xsave_mask =
  378. (1 << (X86_FEATURE_XSAVE % 32)) |
  379. (1 << (X86_FEATURE_OSXSAVE % 32));
  380. /* Xen will set CR4.OSXSAVE if supported and not disabled by force */
  381. if ((cx & xsave_mask) != xsave_mask)
  382. cpuid_leaf1_ecx_mask &= ~xsave_mask; /* disable XSAVE & OSXSAVE */
  383. if (xen_check_mwait())
  384. cpuid_leaf1_ecx_set_mask = (1 << (X86_FEATURE_MWAIT % 32));
  385. }
  386. static void xen_set_debugreg(int reg, unsigned long val)
  387. {
  388. HYPERVISOR_set_debugreg(reg, val);
  389. }
  390. static unsigned long xen_get_debugreg(int reg)
  391. {
  392. return HYPERVISOR_get_debugreg(reg);
  393. }
  394. static void xen_end_context_switch(struct task_struct *next)
  395. {
  396. xen_mc_flush();
  397. paravirt_end_context_switch(next);
  398. }
  399. static unsigned long xen_store_tr(void)
  400. {
  401. return 0;
  402. }
  403. /*
  404. * Set the page permissions for a particular virtual address. If the
  405. * address is a vmalloc mapping (or other non-linear mapping), then
  406. * find the linear mapping of the page and also set its protections to
  407. * match.
  408. */
  409. static void set_aliased_prot(void *v, pgprot_t prot)
  410. {
  411. int level;
  412. pte_t *ptep;
  413. pte_t pte;
  414. unsigned long pfn;
  415. struct page *page;
  416. ptep = lookup_address((unsigned long)v, &level);
  417. BUG_ON(ptep == NULL);
  418. pfn = pte_pfn(*ptep);
  419. page = pfn_to_page(pfn);
  420. pte = pfn_pte(pfn, prot);
  421. if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
  422. BUG();
  423. if (!PageHighMem(page)) {
  424. void *av = __va(PFN_PHYS(pfn));
  425. if (av != v)
  426. if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
  427. BUG();
  428. } else
  429. kmap_flush_unused();
  430. }
  431. static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
  432. {
  433. const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
  434. int i;
  435. for(i = 0; i < entries; i += entries_per_page)
  436. set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
  437. }
  438. static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
  439. {
  440. const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
  441. int i;
  442. for(i = 0; i < entries; i += entries_per_page)
  443. set_aliased_prot(ldt + i, PAGE_KERNEL);
  444. }
  445. static void xen_set_ldt(const void *addr, unsigned entries)
  446. {
  447. struct mmuext_op *op;
  448. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  449. trace_xen_cpu_set_ldt(addr, entries);
  450. op = mcs.args;
  451. op->cmd = MMUEXT_SET_LDT;
  452. op->arg1.linear_addr = (unsigned long)addr;
  453. op->arg2.nr_ents = entries;
  454. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  455. xen_mc_issue(PARAVIRT_LAZY_CPU);
  456. }
  457. static void xen_load_gdt(const struct desc_ptr *dtr)
  458. {
  459. unsigned long va = dtr->address;
  460. unsigned int size = dtr->size + 1;
  461. unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  462. unsigned long frames[pages];
  463. int f;
  464. /*
  465. * A GDT can be up to 64k in size, which corresponds to 8192
  466. * 8-byte entries, or 16 4k pages..
  467. */
  468. BUG_ON(size > 65536);
  469. BUG_ON(va & ~PAGE_MASK);
  470. for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
  471. int level;
  472. pte_t *ptep;
  473. unsigned long pfn, mfn;
  474. void *virt;
  475. /*
  476. * The GDT is per-cpu and is in the percpu data area.
  477. * That can be virtually mapped, so we need to do a
  478. * page-walk to get the underlying MFN for the
  479. * hypercall. The page can also be in the kernel's
  480. * linear range, so we need to RO that mapping too.
  481. */
  482. ptep = lookup_address(va, &level);
  483. BUG_ON(ptep == NULL);
  484. pfn = pte_pfn(*ptep);
  485. mfn = pfn_to_mfn(pfn);
  486. virt = __va(PFN_PHYS(pfn));
  487. frames[f] = mfn;
  488. make_lowmem_page_readonly((void *)va);
  489. make_lowmem_page_readonly(virt);
  490. }
  491. if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
  492. BUG();
  493. }
  494. /*
  495. * load_gdt for early boot, when the gdt is only mapped once
  496. */
  497. static void __init xen_load_gdt_boot(const struct desc_ptr *dtr)
  498. {
  499. unsigned long va = dtr->address;
  500. unsigned int size = dtr->size + 1;
  501. unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  502. unsigned long frames[pages];
  503. int f;
  504. /*
  505. * A GDT can be up to 64k in size, which corresponds to 8192
  506. * 8-byte entries, or 16 4k pages..
  507. */
  508. BUG_ON(size > 65536);
  509. BUG_ON(va & ~PAGE_MASK);
  510. for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
  511. pte_t pte;
  512. unsigned long pfn, mfn;
  513. pfn = virt_to_pfn(va);
  514. mfn = pfn_to_mfn(pfn);
  515. pte = pfn_pte(pfn, PAGE_KERNEL_RO);
  516. if (HYPERVISOR_update_va_mapping((unsigned long)va, pte, 0))
  517. BUG();
  518. frames[f] = mfn;
  519. }
  520. if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
  521. BUG();
  522. }
  523. static inline bool desc_equal(const struct desc_struct *d1,
  524. const struct desc_struct *d2)
  525. {
  526. return d1->a == d2->a && d1->b == d2->b;
  527. }
  528. static void load_TLS_descriptor(struct thread_struct *t,
  529. unsigned int cpu, unsigned int i)
  530. {
  531. struct desc_struct *shadow = &per_cpu(shadow_tls_desc, cpu).desc[i];
  532. struct desc_struct *gdt;
  533. xmaddr_t maddr;
  534. struct multicall_space mc;
  535. if (desc_equal(shadow, &t->tls_array[i]))
  536. return;
  537. *shadow = t->tls_array[i];
  538. gdt = get_cpu_gdt_table(cpu);
  539. maddr = arbitrary_virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
  540. mc = __xen_mc_entry(0);
  541. MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
  542. }
  543. static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
  544. {
  545. /*
  546. * XXX sleazy hack: If we're being called in a lazy-cpu zone
  547. * and lazy gs handling is enabled, it means we're in a
  548. * context switch, and %gs has just been saved. This means we
  549. * can zero it out to prevent faults on exit from the
  550. * hypervisor if the next process has no %gs. Either way, it
  551. * has been saved, and the new value will get loaded properly.
  552. * This will go away as soon as Xen has been modified to not
  553. * save/restore %gs for normal hypercalls.
  554. *
  555. * On x86_64, this hack is not used for %gs, because gs points
  556. * to KERNEL_GS_BASE (and uses it for PDA references), so we
  557. * must not zero %gs on x86_64
  558. *
  559. * For x86_64, we need to zero %fs, otherwise we may get an
  560. * exception between the new %fs descriptor being loaded and
  561. * %fs being effectively cleared at __switch_to().
  562. */
  563. if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
  564. #ifdef CONFIG_X86_32
  565. lazy_load_gs(0);
  566. #else
  567. loadsegment(fs, 0);
  568. #endif
  569. }
  570. xen_mc_batch();
  571. load_TLS_descriptor(t, cpu, 0);
  572. load_TLS_descriptor(t, cpu, 1);
  573. load_TLS_descriptor(t, cpu, 2);
  574. xen_mc_issue(PARAVIRT_LAZY_CPU);
  575. }
  576. #ifdef CONFIG_X86_64
  577. static void xen_load_gs_index(unsigned int idx)
  578. {
  579. if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
  580. BUG();
  581. }
  582. #endif
  583. static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
  584. const void *ptr)
  585. {
  586. xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
  587. u64 entry = *(u64 *)ptr;
  588. trace_xen_cpu_write_ldt_entry(dt, entrynum, entry);
  589. preempt_disable();
  590. xen_mc_flush();
  591. if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
  592. BUG();
  593. preempt_enable();
  594. }
  595. static int cvt_gate_to_trap(int vector, const gate_desc *val,
  596. struct trap_info *info)
  597. {
  598. unsigned long addr;
  599. if (val->type != GATE_TRAP && val->type != GATE_INTERRUPT)
  600. return 0;
  601. info->vector = vector;
  602. addr = gate_offset(*val);
  603. #ifdef CONFIG_X86_64
  604. /*
  605. * Look for known traps using IST, and substitute them
  606. * appropriately. The debugger ones are the only ones we care
  607. * about. Xen will handle faults like double_fault,
  608. * so we should never see them. Warn if
  609. * there's an unexpected IST-using fault handler.
  610. */
  611. if (addr == (unsigned long)debug)
  612. addr = (unsigned long)xen_debug;
  613. else if (addr == (unsigned long)int3)
  614. addr = (unsigned long)xen_int3;
  615. else if (addr == (unsigned long)stack_segment)
  616. addr = (unsigned long)xen_stack_segment;
  617. else if (addr == (unsigned long)double_fault) {
  618. /* Don't need to handle these */
  619. return 0;
  620. #ifdef CONFIG_X86_MCE
  621. } else if (addr == (unsigned long)machine_check) {
  622. /*
  623. * when xen hypervisor inject vMCE to guest,
  624. * use native mce handler to handle it
  625. */
  626. ;
  627. #endif
  628. } else if (addr == (unsigned long)nmi)
  629. /*
  630. * Use the native version as well.
  631. */
  632. ;
  633. else {
  634. /* Some other trap using IST? */
  635. if (WARN_ON(val->ist != 0))
  636. return 0;
  637. }
  638. #endif /* CONFIG_X86_64 */
  639. info->address = addr;
  640. info->cs = gate_segment(*val);
  641. info->flags = val->dpl;
  642. /* interrupt gates clear IF */
  643. if (val->type == GATE_INTERRUPT)
  644. info->flags |= 1 << 2;
  645. return 1;
  646. }
  647. /* Locations of each CPU's IDT */
  648. static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
  649. /* Set an IDT entry. If the entry is part of the current IDT, then
  650. also update Xen. */
  651. static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
  652. {
  653. unsigned long p = (unsigned long)&dt[entrynum];
  654. unsigned long start, end;
  655. trace_xen_cpu_write_idt_entry(dt, entrynum, g);
  656. preempt_disable();
  657. start = __this_cpu_read(idt_desc.address);
  658. end = start + __this_cpu_read(idt_desc.size) + 1;
  659. xen_mc_flush();
  660. native_write_idt_entry(dt, entrynum, g);
  661. if (p >= start && (p + 8) <= end) {
  662. struct trap_info info[2];
  663. info[1].address = 0;
  664. if (cvt_gate_to_trap(entrynum, g, &info[0]))
  665. if (HYPERVISOR_set_trap_table(info))
  666. BUG();
  667. }
  668. preempt_enable();
  669. }
  670. static void xen_convert_trap_info(const struct desc_ptr *desc,
  671. struct trap_info *traps)
  672. {
  673. unsigned in, out, count;
  674. count = (desc->size+1) / sizeof(gate_desc);
  675. BUG_ON(count > 256);
  676. for (in = out = 0; in < count; in++) {
  677. gate_desc *entry = (gate_desc*)(desc->address) + in;
  678. if (cvt_gate_to_trap(in, entry, &traps[out]))
  679. out++;
  680. }
  681. traps[out].address = 0;
  682. }
  683. void xen_copy_trap_info(struct trap_info *traps)
  684. {
  685. const struct desc_ptr *desc = this_cpu_ptr(&idt_desc);
  686. xen_convert_trap_info(desc, traps);
  687. }
  688. /* Load a new IDT into Xen. In principle this can be per-CPU, so we
  689. hold a spinlock to protect the static traps[] array (static because
  690. it avoids allocation, and saves stack space). */
  691. static void xen_load_idt(const struct desc_ptr *desc)
  692. {
  693. static DEFINE_SPINLOCK(lock);
  694. static struct trap_info traps[257];
  695. trace_xen_cpu_load_idt(desc);
  696. spin_lock(&lock);
  697. memcpy(this_cpu_ptr(&idt_desc), desc, sizeof(idt_desc));
  698. xen_convert_trap_info(desc, traps);
  699. xen_mc_flush();
  700. if (HYPERVISOR_set_trap_table(traps))
  701. BUG();
  702. spin_unlock(&lock);
  703. }
  704. /* Write a GDT descriptor entry. Ignore LDT descriptors, since
  705. they're handled differently. */
  706. static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
  707. const void *desc, int type)
  708. {
  709. trace_xen_cpu_write_gdt_entry(dt, entry, desc, type);
  710. preempt_disable();
  711. switch (type) {
  712. case DESC_LDT:
  713. case DESC_TSS:
  714. /* ignore */
  715. break;
  716. default: {
  717. xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
  718. xen_mc_flush();
  719. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  720. BUG();
  721. }
  722. }
  723. preempt_enable();
  724. }
  725. /*
  726. * Version of write_gdt_entry for use at early boot-time needed to
  727. * update an entry as simply as possible.
  728. */
  729. static void __init xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
  730. const void *desc, int type)
  731. {
  732. trace_xen_cpu_write_gdt_entry(dt, entry, desc, type);
  733. switch (type) {
  734. case DESC_LDT:
  735. case DESC_TSS:
  736. /* ignore */
  737. break;
  738. default: {
  739. xmaddr_t maddr = virt_to_machine(&dt[entry]);
  740. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  741. dt[entry] = *(struct desc_struct *)desc;
  742. }
  743. }
  744. }
  745. static void xen_load_sp0(struct tss_struct *tss,
  746. struct thread_struct *thread)
  747. {
  748. struct multicall_space mcs;
  749. mcs = xen_mc_entry(0);
  750. MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
  751. xen_mc_issue(PARAVIRT_LAZY_CPU);
  752. tss->x86_tss.sp0 = thread->sp0;
  753. }
  754. static void xen_set_iopl_mask(unsigned mask)
  755. {
  756. struct physdev_set_iopl set_iopl;
  757. /* Force the change at ring 0. */
  758. set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
  759. HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  760. }
  761. static void xen_io_delay(void)
  762. {
  763. }
  764. static void xen_clts(void)
  765. {
  766. struct multicall_space mcs;
  767. mcs = xen_mc_entry(0);
  768. MULTI_fpu_taskswitch(mcs.mc, 0);
  769. xen_mc_issue(PARAVIRT_LAZY_CPU);
  770. }
  771. static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
  772. static unsigned long xen_read_cr0(void)
  773. {
  774. unsigned long cr0 = this_cpu_read(xen_cr0_value);
  775. if (unlikely(cr0 == 0)) {
  776. cr0 = native_read_cr0();
  777. this_cpu_write(xen_cr0_value, cr0);
  778. }
  779. return cr0;
  780. }
  781. static void xen_write_cr0(unsigned long cr0)
  782. {
  783. struct multicall_space mcs;
  784. this_cpu_write(xen_cr0_value, cr0);
  785. /* Only pay attention to cr0.TS; everything else is
  786. ignored. */
  787. mcs = xen_mc_entry(0);
  788. MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
  789. xen_mc_issue(PARAVIRT_LAZY_CPU);
  790. }
  791. static void xen_write_cr4(unsigned long cr4)
  792. {
  793. cr4 &= ~X86_CR4_PGE;
  794. cr4 &= ~X86_CR4_PSE;
  795. native_write_cr4(cr4);
  796. }
  797. #ifdef CONFIG_X86_64
  798. static inline unsigned long xen_read_cr8(void)
  799. {
  800. return 0;
  801. }
  802. static inline void xen_write_cr8(unsigned long val)
  803. {
  804. BUG_ON(val);
  805. }
  806. #endif
  807. static u64 xen_read_msr_safe(unsigned int msr, int *err)
  808. {
  809. u64 val;
  810. val = native_read_msr_safe(msr, err);
  811. switch (msr) {
  812. case MSR_IA32_APICBASE:
  813. #ifdef CONFIG_X86_X2APIC
  814. if (!(cpuid_ecx(1) & (1 << (X86_FEATURE_X2APIC & 31))))
  815. #endif
  816. val &= ~X2APIC_ENABLE;
  817. break;
  818. }
  819. return val;
  820. }
  821. static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
  822. {
  823. int ret;
  824. ret = 0;
  825. switch (msr) {
  826. #ifdef CONFIG_X86_64
  827. unsigned which;
  828. u64 base;
  829. case MSR_FS_BASE: which = SEGBASE_FS; goto set;
  830. case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
  831. case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
  832. set:
  833. base = ((u64)high << 32) | low;
  834. if (HYPERVISOR_set_segment_base(which, base) != 0)
  835. ret = -EIO;
  836. break;
  837. #endif
  838. case MSR_STAR:
  839. case MSR_CSTAR:
  840. case MSR_LSTAR:
  841. case MSR_SYSCALL_MASK:
  842. case MSR_IA32_SYSENTER_CS:
  843. case MSR_IA32_SYSENTER_ESP:
  844. case MSR_IA32_SYSENTER_EIP:
  845. /* Fast syscall setup is all done in hypercalls, so
  846. these are all ignored. Stub them out here to stop
  847. Xen console noise. */
  848. default:
  849. ret = native_write_msr_safe(msr, low, high);
  850. }
  851. return ret;
  852. }
  853. void xen_setup_shared_info(void)
  854. {
  855. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  856. set_fixmap(FIX_PARAVIRT_BOOTMAP,
  857. xen_start_info->shared_info);
  858. HYPERVISOR_shared_info =
  859. (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
  860. } else
  861. HYPERVISOR_shared_info =
  862. (struct shared_info *)__va(xen_start_info->shared_info);
  863. #ifndef CONFIG_SMP
  864. /* In UP this is as good a place as any to set up shared info */
  865. xen_setup_vcpu_info_placement();
  866. #endif
  867. xen_setup_mfn_list_list();
  868. }
  869. /* This is called once we have the cpu_possible_mask */
  870. void xen_setup_vcpu_info_placement(void)
  871. {
  872. int cpu;
  873. for_each_possible_cpu(cpu)
  874. xen_vcpu_setup(cpu);
  875. /* xen_vcpu_setup managed to place the vcpu_info within the
  876. * percpu area for all cpus, so make use of it. Note that for
  877. * PVH we want to use native IRQ mechanism. */
  878. if (have_vcpu_info_placement && !xen_pvh_domain()) {
  879. pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
  880. pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
  881. pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
  882. pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
  883. pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
  884. }
  885. }
  886. static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
  887. unsigned long addr, unsigned len)
  888. {
  889. char *start, *end, *reloc;
  890. unsigned ret;
  891. start = end = reloc = NULL;
  892. #define SITE(op, x) \
  893. case PARAVIRT_PATCH(op.x): \
  894. if (have_vcpu_info_placement) { \
  895. start = (char *)xen_##x##_direct; \
  896. end = xen_##x##_direct_end; \
  897. reloc = xen_##x##_direct_reloc; \
  898. } \
  899. goto patch_site
  900. switch (type) {
  901. SITE(pv_irq_ops, irq_enable);
  902. SITE(pv_irq_ops, irq_disable);
  903. SITE(pv_irq_ops, save_fl);
  904. SITE(pv_irq_ops, restore_fl);
  905. #undef SITE
  906. patch_site:
  907. if (start == NULL || (end-start) > len)
  908. goto default_patch;
  909. ret = paravirt_patch_insns(insnbuf, len, start, end);
  910. /* Note: because reloc is assigned from something that
  911. appears to be an array, gcc assumes it's non-null,
  912. but doesn't know its relationship with start and
  913. end. */
  914. if (reloc > start && reloc < end) {
  915. int reloc_off = reloc - start;
  916. long *relocp = (long *)(insnbuf + reloc_off);
  917. long delta = start - (char *)addr;
  918. *relocp += delta;
  919. }
  920. break;
  921. default_patch:
  922. default:
  923. ret = paravirt_patch_default(type, clobbers, insnbuf,
  924. addr, len);
  925. break;
  926. }
  927. return ret;
  928. }
  929. static const struct pv_info xen_info __initconst = {
  930. .paravirt_enabled = 1,
  931. .shared_kernel_pmd = 0,
  932. #ifdef CONFIG_X86_64
  933. .extra_user_64bit_cs = FLAT_USER_CS64,
  934. #endif
  935. .name = "Xen",
  936. };
  937. static const struct pv_init_ops xen_init_ops __initconst = {
  938. .patch = xen_patch,
  939. };
  940. static const struct pv_cpu_ops xen_cpu_ops __initconst = {
  941. .cpuid = xen_cpuid,
  942. .set_debugreg = xen_set_debugreg,
  943. .get_debugreg = xen_get_debugreg,
  944. .clts = xen_clts,
  945. .read_cr0 = xen_read_cr0,
  946. .write_cr0 = xen_write_cr0,
  947. .read_cr4 = native_read_cr4,
  948. .read_cr4_safe = native_read_cr4_safe,
  949. .write_cr4 = xen_write_cr4,
  950. #ifdef CONFIG_X86_64
  951. .read_cr8 = xen_read_cr8,
  952. .write_cr8 = xen_write_cr8,
  953. #endif
  954. .wbinvd = native_wbinvd,
  955. .read_msr = xen_read_msr_safe,
  956. .write_msr = xen_write_msr_safe,
  957. .read_tsc = native_read_tsc,
  958. .read_pmc = native_read_pmc,
  959. .read_tscp = native_read_tscp,
  960. .iret = xen_iret,
  961. #ifdef CONFIG_X86_64
  962. .usergs_sysret32 = xen_sysret32,
  963. .usergs_sysret64 = xen_sysret64,
  964. #else
  965. .irq_enable_sysexit = xen_sysexit,
  966. #endif
  967. .load_tr_desc = paravirt_nop,
  968. .set_ldt = xen_set_ldt,
  969. .load_gdt = xen_load_gdt,
  970. .load_idt = xen_load_idt,
  971. .load_tls = xen_load_tls,
  972. #ifdef CONFIG_X86_64
  973. .load_gs_index = xen_load_gs_index,
  974. #endif
  975. .alloc_ldt = xen_alloc_ldt,
  976. .free_ldt = xen_free_ldt,
  977. .store_idt = native_store_idt,
  978. .store_tr = xen_store_tr,
  979. .write_ldt_entry = xen_write_ldt_entry,
  980. .write_gdt_entry = xen_write_gdt_entry,
  981. .write_idt_entry = xen_write_idt_entry,
  982. .load_sp0 = xen_load_sp0,
  983. .set_iopl_mask = xen_set_iopl_mask,
  984. .io_delay = xen_io_delay,
  985. /* Xen takes care of %gs when switching to usermode for us */
  986. .swapgs = paravirt_nop,
  987. .start_context_switch = paravirt_start_context_switch,
  988. .end_context_switch = xen_end_context_switch,
  989. };
  990. static const struct pv_apic_ops xen_apic_ops __initconst = {
  991. #ifdef CONFIG_X86_LOCAL_APIC
  992. .startup_ipi_hook = paravirt_nop,
  993. #endif
  994. };
  995. static void xen_reboot(int reason)
  996. {
  997. struct sched_shutdown r = { .reason = reason };
  998. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
  999. BUG();
  1000. }
  1001. static void xen_restart(char *msg)
  1002. {
  1003. xen_reboot(SHUTDOWN_reboot);
  1004. }
  1005. static void xen_emergency_restart(void)
  1006. {
  1007. xen_reboot(SHUTDOWN_reboot);
  1008. }
  1009. static void xen_machine_halt(void)
  1010. {
  1011. xen_reboot(SHUTDOWN_poweroff);
  1012. }
  1013. static void xen_machine_power_off(void)
  1014. {
  1015. if (pm_power_off)
  1016. pm_power_off();
  1017. xen_reboot(SHUTDOWN_poweroff);
  1018. }
  1019. static void xen_crash_shutdown(struct pt_regs *regs)
  1020. {
  1021. xen_reboot(SHUTDOWN_crash);
  1022. }
  1023. static int
  1024. xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
  1025. {
  1026. xen_reboot(SHUTDOWN_crash);
  1027. return NOTIFY_DONE;
  1028. }
  1029. static struct notifier_block xen_panic_block = {
  1030. .notifier_call= xen_panic_event,
  1031. .priority = INT_MIN
  1032. };
  1033. int xen_panic_handler_init(void)
  1034. {
  1035. atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block);
  1036. return 0;
  1037. }
  1038. static const struct machine_ops xen_machine_ops __initconst = {
  1039. .restart = xen_restart,
  1040. .halt = xen_machine_halt,
  1041. .power_off = xen_machine_power_off,
  1042. .shutdown = xen_machine_halt,
  1043. .crash_shutdown = xen_crash_shutdown,
  1044. .emergency_restart = xen_emergency_restart,
  1045. };
  1046. static unsigned char xen_get_nmi_reason(void)
  1047. {
  1048. unsigned char reason = 0;
  1049. /* Construct a value which looks like it came from port 0x61. */
  1050. if (test_bit(_XEN_NMIREASON_io_error,
  1051. &HYPERVISOR_shared_info->arch.nmi_reason))
  1052. reason |= NMI_REASON_IOCHK;
  1053. if (test_bit(_XEN_NMIREASON_pci_serr,
  1054. &HYPERVISOR_shared_info->arch.nmi_reason))
  1055. reason |= NMI_REASON_SERR;
  1056. return reason;
  1057. }
  1058. static void __init xen_boot_params_init_edd(void)
  1059. {
  1060. #if IS_ENABLED(CONFIG_EDD)
  1061. struct xen_platform_op op;
  1062. struct edd_info *edd_info;
  1063. u32 *mbr_signature;
  1064. unsigned nr;
  1065. int ret;
  1066. edd_info = boot_params.eddbuf;
  1067. mbr_signature = boot_params.edd_mbr_sig_buffer;
  1068. op.cmd = XENPF_firmware_info;
  1069. op.u.firmware_info.type = XEN_FW_DISK_INFO;
  1070. for (nr = 0; nr < EDDMAXNR; nr++) {
  1071. struct edd_info *info = edd_info + nr;
  1072. op.u.firmware_info.index = nr;
  1073. info->params.length = sizeof(info->params);
  1074. set_xen_guest_handle(op.u.firmware_info.u.disk_info.edd_params,
  1075. &info->params);
  1076. ret = HYPERVISOR_dom0_op(&op);
  1077. if (ret)
  1078. break;
  1079. #define C(x) info->x = op.u.firmware_info.u.disk_info.x
  1080. C(device);
  1081. C(version);
  1082. C(interface_support);
  1083. C(legacy_max_cylinder);
  1084. C(legacy_max_head);
  1085. C(legacy_sectors_per_track);
  1086. #undef C
  1087. }
  1088. boot_params.eddbuf_entries = nr;
  1089. op.u.firmware_info.type = XEN_FW_DISK_MBR_SIGNATURE;
  1090. for (nr = 0; nr < EDD_MBR_SIG_MAX; nr++) {
  1091. op.u.firmware_info.index = nr;
  1092. ret = HYPERVISOR_dom0_op(&op);
  1093. if (ret)
  1094. break;
  1095. mbr_signature[nr] = op.u.firmware_info.u.disk_mbr_signature.mbr_signature;
  1096. }
  1097. boot_params.edd_mbr_sig_buf_entries = nr;
  1098. #endif
  1099. }
  1100. /*
  1101. * Set up the GDT and segment registers for -fstack-protector. Until
  1102. * we do this, we have to be careful not to call any stack-protected
  1103. * function, which is most of the kernel.
  1104. *
  1105. * Note, that it is __ref because the only caller of this after init
  1106. * is PVH which is not going to use xen_load_gdt_boot or other
  1107. * __init functions.
  1108. */
  1109. static void __ref xen_setup_gdt(int cpu)
  1110. {
  1111. if (xen_feature(XENFEAT_auto_translated_physmap)) {
  1112. #ifdef CONFIG_X86_64
  1113. unsigned long dummy;
  1114. load_percpu_segment(cpu); /* We need to access per-cpu area */
  1115. switch_to_new_gdt(cpu); /* GDT and GS set */
  1116. /* We are switching of the Xen provided GDT to our HVM mode
  1117. * GDT. The new GDT has __KERNEL_CS with CS.L = 1
  1118. * and we are jumping to reload it.
  1119. */
  1120. asm volatile ("pushq %0\n"
  1121. "leaq 1f(%%rip),%0\n"
  1122. "pushq %0\n"
  1123. "lretq\n"
  1124. "1:\n"
  1125. : "=&r" (dummy) : "0" (__KERNEL_CS));
  1126. /*
  1127. * While not needed, we also set the %es, %ds, and %fs
  1128. * to zero. We don't care about %ss as it is NULL.
  1129. * Strictly speaking this is not needed as Xen zeros those
  1130. * out (and also MSR_FS_BASE, MSR_GS_BASE, MSR_KERNEL_GS_BASE)
  1131. *
  1132. * Linux zeros them in cpu_init() and in secondary_startup_64
  1133. * (for BSP).
  1134. */
  1135. loadsegment(es, 0);
  1136. loadsegment(ds, 0);
  1137. loadsegment(fs, 0);
  1138. #else
  1139. /* PVH: TODO Implement. */
  1140. BUG();
  1141. #endif
  1142. return; /* PVH does not need any PV GDT ops. */
  1143. }
  1144. pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
  1145. pv_cpu_ops.load_gdt = xen_load_gdt_boot;
  1146. setup_stack_canary_segment(0);
  1147. switch_to_new_gdt(0);
  1148. pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
  1149. pv_cpu_ops.load_gdt = xen_load_gdt;
  1150. }
  1151. #ifdef CONFIG_XEN_PVH
  1152. /*
  1153. * A PV guest starts with default flags that are not set for PVH, set them
  1154. * here asap.
  1155. */
  1156. static void xen_pvh_set_cr_flags(int cpu)
  1157. {
  1158. /* Some of these are setup in 'secondary_startup_64'. The others:
  1159. * X86_CR0_TS, X86_CR0_PE, X86_CR0_ET are set by Xen for HVM guests
  1160. * (which PVH shared codepaths), while X86_CR0_PG is for PVH. */
  1161. write_cr0(read_cr0() | X86_CR0_MP | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM);
  1162. if (!cpu)
  1163. return;
  1164. /*
  1165. * For BSP, PSE PGE are set in probe_page_size_mask(), for APs
  1166. * set them here. For all, OSFXSR OSXMMEXCPT are set in fpu__init_cpu().
  1167. */
  1168. if (cpu_has_pse)
  1169. cr4_set_bits_and_update_boot(X86_CR4_PSE);
  1170. if (cpu_has_pge)
  1171. cr4_set_bits_and_update_boot(X86_CR4_PGE);
  1172. }
  1173. /*
  1174. * Note, that it is ref - because the only caller of this after init
  1175. * is PVH which is not going to use xen_load_gdt_boot or other
  1176. * __init functions.
  1177. */
  1178. void __ref xen_pvh_secondary_vcpu_init(int cpu)
  1179. {
  1180. xen_setup_gdt(cpu);
  1181. xen_pvh_set_cr_flags(cpu);
  1182. }
  1183. static void __init xen_pvh_early_guest_init(void)
  1184. {
  1185. if (!xen_feature(XENFEAT_auto_translated_physmap))
  1186. return;
  1187. if (!xen_feature(XENFEAT_hvm_callback_vector))
  1188. return;
  1189. xen_have_vector_callback = 1;
  1190. xen_pvh_early_cpu_init(0, false);
  1191. xen_pvh_set_cr_flags(0);
  1192. #ifdef CONFIG_X86_32
  1193. BUG(); /* PVH: Implement proper support. */
  1194. #endif
  1195. }
  1196. #endif /* CONFIG_XEN_PVH */
  1197. /* First C function to be called on Xen boot */
  1198. asmlinkage __visible void __init xen_start_kernel(void)
  1199. {
  1200. struct physdev_set_iopl set_iopl;
  1201. unsigned long initrd_start = 0;
  1202. u64 pat;
  1203. int rc;
  1204. if (!xen_start_info)
  1205. return;
  1206. xen_domain_type = XEN_PV_DOMAIN;
  1207. xen_setup_features();
  1208. #ifdef CONFIG_XEN_PVH
  1209. xen_pvh_early_guest_init();
  1210. #endif
  1211. xen_setup_machphys_mapping();
  1212. /* Install Xen paravirt ops */
  1213. pv_info = xen_info;
  1214. pv_init_ops = xen_init_ops;
  1215. pv_apic_ops = xen_apic_ops;
  1216. if (!xen_pvh_domain()) {
  1217. pv_cpu_ops = xen_cpu_ops;
  1218. x86_platform.get_nmi_reason = xen_get_nmi_reason;
  1219. }
  1220. if (xen_feature(XENFEAT_auto_translated_physmap))
  1221. x86_init.resources.memory_setup = xen_auto_xlated_memory_setup;
  1222. else
  1223. x86_init.resources.memory_setup = xen_memory_setup;
  1224. x86_init.oem.arch_setup = xen_arch_setup;
  1225. x86_init.oem.banner = xen_banner;
  1226. xen_init_time_ops();
  1227. /*
  1228. * Set up some pagetable state before starting to set any ptes.
  1229. */
  1230. xen_init_mmu_ops();
  1231. /* Prevent unwanted bits from being set in PTEs. */
  1232. __supported_pte_mask &= ~_PAGE_GLOBAL;
  1233. /*
  1234. * Prevent page tables from being allocated in highmem, even
  1235. * if CONFIG_HIGHPTE is enabled.
  1236. */
  1237. __userpte_alloc_gfp &= ~__GFP_HIGHMEM;
  1238. /* Work out if we support NX */
  1239. x86_configure_nx();
  1240. /* Get mfn list */
  1241. xen_build_dynamic_phys_to_machine();
  1242. /*
  1243. * Set up kernel GDT and segment registers, mainly so that
  1244. * -fstack-protector code can be executed.
  1245. */
  1246. xen_setup_gdt(0);
  1247. xen_init_irq_ops();
  1248. xen_init_cpuid_mask();
  1249. #ifdef CONFIG_X86_LOCAL_APIC
  1250. /*
  1251. * set up the basic apic ops.
  1252. */
  1253. xen_init_apic();
  1254. #endif
  1255. if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
  1256. pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
  1257. pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
  1258. }
  1259. machine_ops = xen_machine_ops;
  1260. /*
  1261. * The only reliable way to retain the initial address of the
  1262. * percpu gdt_page is to remember it here, so we can go and
  1263. * mark it RW later, when the initial percpu area is freed.
  1264. */
  1265. xen_initial_gdt = &per_cpu(gdt_page, 0);
  1266. xen_smp_init();
  1267. #ifdef CONFIG_ACPI_NUMA
  1268. /*
  1269. * The pages we from Xen are not related to machine pages, so
  1270. * any NUMA information the kernel tries to get from ACPI will
  1271. * be meaningless. Prevent it from trying.
  1272. */
  1273. acpi_numa = -1;
  1274. #endif
  1275. /* Don't do the full vcpu_info placement stuff until we have a
  1276. possible map and a non-dummy shared_info. */
  1277. per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
  1278. local_irq_disable();
  1279. early_boot_irqs_disabled = true;
  1280. xen_raw_console_write("mapping kernel into physical memory\n");
  1281. xen_setup_kernel_pagetable((pgd_t *)xen_start_info->pt_base, xen_start_info->nr_pages);
  1282. /*
  1283. * Modify the cache mode translation tables to match Xen's PAT
  1284. * configuration.
  1285. */
  1286. rdmsrl(MSR_IA32_CR_PAT, pat);
  1287. pat_init_cache_modes(pat);
  1288. /* keep using Xen gdt for now; no urgent need to change it */
  1289. #ifdef CONFIG_X86_32
  1290. pv_info.kernel_rpl = 1;
  1291. if (xen_feature(XENFEAT_supervisor_mode_kernel))
  1292. pv_info.kernel_rpl = 0;
  1293. #else
  1294. pv_info.kernel_rpl = 0;
  1295. #endif
  1296. /* set the limit of our address space */
  1297. xen_reserve_top();
  1298. /* PVH: runs at default kernel iopl of 0 */
  1299. if (!xen_pvh_domain()) {
  1300. /*
  1301. * We used to do this in xen_arch_setup, but that is too late
  1302. * on AMD were early_cpu_init (run before ->arch_setup()) calls
  1303. * early_amd_init which pokes 0xcf8 port.
  1304. */
  1305. set_iopl.iopl = 1;
  1306. rc = HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  1307. if (rc != 0)
  1308. xen_raw_printk("physdev_op failed %d\n", rc);
  1309. }
  1310. #ifdef CONFIG_X86_32
  1311. /* set up basic CPUID stuff */
  1312. cpu_detect(&new_cpu_data);
  1313. set_cpu_cap(&new_cpu_data, X86_FEATURE_FPU);
  1314. new_cpu_data.wp_works_ok = 1;
  1315. new_cpu_data.x86_capability[0] = cpuid_edx(1);
  1316. #endif
  1317. if (xen_start_info->mod_start) {
  1318. if (xen_start_info->flags & SIF_MOD_START_PFN)
  1319. initrd_start = PFN_PHYS(xen_start_info->mod_start);
  1320. else
  1321. initrd_start = __pa(xen_start_info->mod_start);
  1322. }
  1323. /* Poke various useful things into boot_params */
  1324. boot_params.hdr.type_of_loader = (9 << 4) | 0;
  1325. boot_params.hdr.ramdisk_image = initrd_start;
  1326. boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
  1327. boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
  1328. if (!xen_initial_domain()) {
  1329. add_preferred_console("xenboot", 0, NULL);
  1330. add_preferred_console("tty", 0, NULL);
  1331. add_preferred_console("hvc", 0, NULL);
  1332. if (pci_xen)
  1333. x86_init.pci.arch_init = pci_xen_init;
  1334. } else {
  1335. const struct dom0_vga_console_info *info =
  1336. (void *)((char *)xen_start_info +
  1337. xen_start_info->console.dom0.info_off);
  1338. struct xen_platform_op op = {
  1339. .cmd = XENPF_firmware_info,
  1340. .interface_version = XENPF_INTERFACE_VERSION,
  1341. .u.firmware_info.type = XEN_FW_KBD_SHIFT_FLAGS,
  1342. };
  1343. xen_init_vga(info, xen_start_info->console.dom0.info_size);
  1344. xen_start_info->console.domU.mfn = 0;
  1345. xen_start_info->console.domU.evtchn = 0;
  1346. if (HYPERVISOR_dom0_op(&op) == 0)
  1347. boot_params.kbd_status = op.u.firmware_info.u.kbd_shift_flags;
  1348. /* Make sure ACS will be enabled */
  1349. pci_request_acs();
  1350. xen_acpi_sleep_register();
  1351. /* Avoid searching for BIOS MP tables */
  1352. x86_init.mpparse.find_smp_config = x86_init_noop;
  1353. x86_init.mpparse.get_smp_config = x86_init_uint_noop;
  1354. xen_boot_params_init_edd();
  1355. }
  1356. #ifdef CONFIG_PCI
  1357. /* PCI BIOS service won't work from a PV guest. */
  1358. pci_probe &= ~PCI_PROBE_BIOS;
  1359. #endif
  1360. xen_raw_console_write("about to get started...\n");
  1361. xen_setup_runstate_info(0);
  1362. xen_efi_init();
  1363. /* Start the world */
  1364. #ifdef CONFIG_X86_32
  1365. i386_start_kernel();
  1366. #else
  1367. cr4_init_shadow(); /* 32b kernel does this in i386_start_kernel() */
  1368. x86_64_start_reservations((char *)__pa_symbol(&boot_params));
  1369. #endif
  1370. }
  1371. void __ref xen_hvm_init_shared_info(void)
  1372. {
  1373. int cpu;
  1374. struct xen_add_to_physmap xatp;
  1375. static struct shared_info *shared_info_page = 0;
  1376. if (!shared_info_page)
  1377. shared_info_page = (struct shared_info *)
  1378. extend_brk(PAGE_SIZE, PAGE_SIZE);
  1379. xatp.domid = DOMID_SELF;
  1380. xatp.idx = 0;
  1381. xatp.space = XENMAPSPACE_shared_info;
  1382. xatp.gpfn = __pa(shared_info_page) >> PAGE_SHIFT;
  1383. if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
  1384. BUG();
  1385. HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
  1386. /* xen_vcpu is a pointer to the vcpu_info struct in the shared_info
  1387. * page, we use it in the event channel upcall and in some pvclock
  1388. * related functions. We don't need the vcpu_info placement
  1389. * optimizations because we don't use any pv_mmu or pv_irq op on
  1390. * HVM.
  1391. * When xen_hvm_init_shared_info is run at boot time only vcpu 0 is
  1392. * online but xen_hvm_init_shared_info is run at resume time too and
  1393. * in that case multiple vcpus might be online. */
  1394. for_each_online_cpu(cpu) {
  1395. /* Leave it to be NULL. */
  1396. if (cpu >= MAX_VIRT_CPUS)
  1397. continue;
  1398. per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
  1399. }
  1400. }
  1401. #ifdef CONFIG_XEN_PVHVM
  1402. static void __init init_hvm_pv_info(void)
  1403. {
  1404. int major, minor;
  1405. uint32_t eax, ebx, ecx, edx, pages, msr, base;
  1406. u64 pfn;
  1407. base = xen_cpuid_base();
  1408. cpuid(base + 1, &eax, &ebx, &ecx, &edx);
  1409. major = eax >> 16;
  1410. minor = eax & 0xffff;
  1411. printk(KERN_INFO "Xen version %d.%d.\n", major, minor);
  1412. cpuid(base + 2, &pages, &msr, &ecx, &edx);
  1413. pfn = __pa(hypercall_page);
  1414. wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32));
  1415. xen_setup_features();
  1416. pv_info.name = "Xen HVM";
  1417. xen_domain_type = XEN_HVM_DOMAIN;
  1418. }
  1419. static int xen_hvm_cpu_notify(struct notifier_block *self, unsigned long action,
  1420. void *hcpu)
  1421. {
  1422. int cpu = (long)hcpu;
  1423. switch (action) {
  1424. case CPU_UP_PREPARE:
  1425. xen_vcpu_setup(cpu);
  1426. if (xen_have_vector_callback) {
  1427. if (xen_feature(XENFEAT_hvm_safe_pvclock))
  1428. xen_setup_timer(cpu);
  1429. }
  1430. break;
  1431. default:
  1432. break;
  1433. }
  1434. return NOTIFY_OK;
  1435. }
  1436. static struct notifier_block xen_hvm_cpu_notifier = {
  1437. .notifier_call = xen_hvm_cpu_notify,
  1438. };
  1439. static void __init xen_hvm_guest_init(void)
  1440. {
  1441. if (xen_pv_domain())
  1442. return;
  1443. init_hvm_pv_info();
  1444. xen_hvm_init_shared_info();
  1445. xen_panic_handler_init();
  1446. if (xen_feature(XENFEAT_hvm_callback_vector))
  1447. xen_have_vector_callback = 1;
  1448. xen_hvm_smp_init();
  1449. register_cpu_notifier(&xen_hvm_cpu_notifier);
  1450. xen_unplug_emulated_devices();
  1451. x86_init.irqs.intr_init = xen_init_IRQ;
  1452. xen_hvm_init_time_ops();
  1453. xen_hvm_init_mmu_ops();
  1454. }
  1455. #endif
  1456. static bool xen_nopv = false;
  1457. static __init int xen_parse_nopv(char *arg)
  1458. {
  1459. xen_nopv = true;
  1460. return 0;
  1461. }
  1462. early_param("xen_nopv", xen_parse_nopv);
  1463. static uint32_t __init xen_platform(void)
  1464. {
  1465. if (xen_nopv)
  1466. return 0;
  1467. return xen_cpuid_base();
  1468. }
  1469. bool xen_hvm_need_lapic(void)
  1470. {
  1471. if (xen_nopv)
  1472. return false;
  1473. if (xen_pv_domain())
  1474. return false;
  1475. if (!xen_hvm_domain())
  1476. return false;
  1477. if (xen_feature(XENFEAT_hvm_pirqs) && xen_have_vector_callback)
  1478. return false;
  1479. return true;
  1480. }
  1481. EXPORT_SYMBOL_GPL(xen_hvm_need_lapic);
  1482. static void xen_set_cpu_features(struct cpuinfo_x86 *c)
  1483. {
  1484. if (xen_pv_domain())
  1485. clear_cpu_bug(c, X86_BUG_SYSRET_SS_ATTRS);
  1486. }
  1487. const struct hypervisor_x86 x86_hyper_xen = {
  1488. .name = "Xen",
  1489. .detect = xen_platform,
  1490. #ifdef CONFIG_XEN_PVHVM
  1491. .init_platform = xen_hvm_guest_init,
  1492. #endif
  1493. .x2apic_available = xen_x2apic_para_available,
  1494. .set_cpu_features = xen_set_cpu_features,
  1495. };
  1496. EXPORT_SYMBOL(x86_hyper_xen);