enlighten.c 42 KB

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