enlighten.c 46 KB

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