enlighten.c 49 KB

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