enlighten_pv.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Core of Xen paravirt_ops implementation.
  4. *
  5. * This file contains the xen_paravirt_ops structure itself, and the
  6. * implementations for:
  7. * - privileged instructions
  8. * - interrupt flags
  9. * - segment operations
  10. * - booting and setup
  11. *
  12. * Jeremy Fitzhardinge <[email protected]>, XenSource Inc, 2007
  13. */
  14. #include <linux/cpu.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/smp.h>
  18. #include <linux/preempt.h>
  19. #include <linux/hardirq.h>
  20. #include <linux/percpu.h>
  21. #include <linux/delay.h>
  22. #include <linux/start_kernel.h>
  23. #include <linux/sched.h>
  24. #include <linux/kprobes.h>
  25. #include <linux/kstrtox.h>
  26. #include <linux/memblock.h>
  27. #include <linux/export.h>
  28. #include <linux/mm.h>
  29. #include <linux/page-flags.h>
  30. #include <linux/pci.h>
  31. #include <linux/gfp.h>
  32. #include <linux/edd.h>
  33. #include <linux/reboot.h>
  34. #include <linux/virtio_anchor.h>
  35. #include <xen/xen.h>
  36. #include <xen/events.h>
  37. #include <xen/interface/xen.h>
  38. #include <xen/interface/version.h>
  39. #include <xen/interface/physdev.h>
  40. #include <xen/interface/vcpu.h>
  41. #include <xen/interface/memory.h>
  42. #include <xen/interface/nmi.h>
  43. #include <xen/interface/xen-mca.h>
  44. #include <xen/features.h>
  45. #include <xen/page.h>
  46. #include <xen/hvc-console.h>
  47. #include <xen/acpi.h>
  48. #include <asm/paravirt.h>
  49. #include <asm/apic.h>
  50. #include <asm/page.h>
  51. #include <asm/xen/pci.h>
  52. #include <asm/xen/hypercall.h>
  53. #include <asm/xen/hypervisor.h>
  54. #include <asm/xen/cpuid.h>
  55. #include <asm/fixmap.h>
  56. #include <asm/processor.h>
  57. #include <asm/proto.h>
  58. #include <asm/msr-index.h>
  59. #include <asm/traps.h>
  60. #include <asm/setup.h>
  61. #include <asm/desc.h>
  62. #include <asm/pgalloc.h>
  63. #include <asm/tlbflush.h>
  64. #include <asm/reboot.h>
  65. #include <asm/stackprotector.h>
  66. #include <asm/hypervisor.h>
  67. #include <asm/mach_traps.h>
  68. #include <asm/mwait.h>
  69. #include <asm/pci_x86.h>
  70. #include <asm/cpu.h>
  71. #ifdef CONFIG_X86_IOPL_IOPERM
  72. #include <asm/io_bitmap.h>
  73. #endif
  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. #include "../kernel/cpu/cpu.h" /* get_cpu_cap() */
  87. void *xen_initial_gdt;
  88. static int xen_cpu_up_prepare_pv(unsigned int cpu);
  89. static int xen_cpu_dead_pv(unsigned int cpu);
  90. struct tls_descs {
  91. struct desc_struct desc[3];
  92. };
  93. /*
  94. * Updating the 3 TLS descriptors in the GDT on every task switch is
  95. * surprisingly expensive so we avoid updating them if they haven't
  96. * changed. Since Xen writes different descriptors than the one
  97. * passed in the update_descriptor hypercall we keep shadow copies to
  98. * compare against.
  99. */
  100. static DEFINE_PER_CPU(struct tls_descs, shadow_tls_desc);
  101. static __read_mostly bool xen_msr_safe = IS_ENABLED(CONFIG_XEN_PV_MSR_SAFE);
  102. static int __init parse_xen_msr_safe(char *str)
  103. {
  104. if (str)
  105. return kstrtobool(str, &xen_msr_safe);
  106. return -EINVAL;
  107. }
  108. early_param("xen_msr_safe", parse_xen_msr_safe);
  109. static void __init xen_pv_init_platform(void)
  110. {
  111. /* PV guests can't operate virtio devices without grants. */
  112. if (IS_ENABLED(CONFIG_XEN_VIRTIO))
  113. virtio_set_mem_acc_cb(xen_virtio_restricted_mem_acc);
  114. populate_extra_pte(fix_to_virt(FIX_PARAVIRT_BOOTMAP));
  115. set_fixmap(FIX_PARAVIRT_BOOTMAP, xen_start_info->shared_info);
  116. HYPERVISOR_shared_info = (void *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
  117. /* xen clock uses per-cpu vcpu_info, need to init it for boot cpu */
  118. xen_vcpu_info_reset(0);
  119. /* pvclock is in shared info area */
  120. xen_init_time_ops();
  121. }
  122. static void __init xen_pv_guest_late_init(void)
  123. {
  124. #ifndef CONFIG_SMP
  125. /* Setup shared vcpu info for non-smp configurations */
  126. xen_setup_vcpu_info_placement();
  127. #endif
  128. }
  129. static __read_mostly unsigned int cpuid_leaf5_ecx_val;
  130. static __read_mostly unsigned int cpuid_leaf5_edx_val;
  131. static void xen_cpuid(unsigned int *ax, unsigned int *bx,
  132. unsigned int *cx, unsigned int *dx)
  133. {
  134. unsigned maskebx = ~0;
  135. /*
  136. * Mask out inconvenient features, to try and disable as many
  137. * unsupported kernel subsystems as possible.
  138. */
  139. switch (*ax) {
  140. case CPUID_MWAIT_LEAF:
  141. /* Synthesize the values.. */
  142. *ax = 0;
  143. *bx = 0;
  144. *cx = cpuid_leaf5_ecx_val;
  145. *dx = cpuid_leaf5_edx_val;
  146. return;
  147. case 0xb:
  148. /* Suppress extended topology stuff */
  149. maskebx = 0;
  150. break;
  151. }
  152. asm(XEN_EMULATE_PREFIX "cpuid"
  153. : "=a" (*ax),
  154. "=b" (*bx),
  155. "=c" (*cx),
  156. "=d" (*dx)
  157. : "0" (*ax), "2" (*cx));
  158. *bx &= maskebx;
  159. }
  160. static bool __init xen_check_mwait(void)
  161. {
  162. #ifdef CONFIG_ACPI
  163. struct xen_platform_op op = {
  164. .cmd = XENPF_set_processor_pminfo,
  165. .u.set_pminfo.id = -1,
  166. .u.set_pminfo.type = XEN_PM_PDC,
  167. };
  168. uint32_t buf[3];
  169. unsigned int ax, bx, cx, dx;
  170. unsigned int mwait_mask;
  171. /* We need to determine whether it is OK to expose the MWAIT
  172. * capability to the kernel to harvest deeper than C3 states from ACPI
  173. * _CST using the processor_harvest_xen.c module. For this to work, we
  174. * need to gather the MWAIT_LEAF values (which the cstate.c code
  175. * checks against). The hypervisor won't expose the MWAIT flag because
  176. * it would break backwards compatibility; so we will find out directly
  177. * from the hardware and hypercall.
  178. */
  179. if (!xen_initial_domain())
  180. return false;
  181. /*
  182. * When running under platform earlier than Xen4.2, do not expose
  183. * mwait, to avoid the risk of loading native acpi pad driver
  184. */
  185. if (!xen_running_on_version_or_later(4, 2))
  186. return false;
  187. ax = 1;
  188. cx = 0;
  189. native_cpuid(&ax, &bx, &cx, &dx);
  190. mwait_mask = (1 << (X86_FEATURE_EST % 32)) |
  191. (1 << (X86_FEATURE_MWAIT % 32));
  192. if ((cx & mwait_mask) != mwait_mask)
  193. return false;
  194. /* We need to emulate the MWAIT_LEAF and for that we need both
  195. * ecx and edx. The hypercall provides only partial information.
  196. */
  197. ax = CPUID_MWAIT_LEAF;
  198. bx = 0;
  199. cx = 0;
  200. dx = 0;
  201. native_cpuid(&ax, &bx, &cx, &dx);
  202. /* Ask the Hypervisor whether to clear ACPI_PDC_C_C2C3_FFH. If so,
  203. * don't expose MWAIT_LEAF and let ACPI pick the IOPORT version of C3.
  204. */
  205. buf[0] = ACPI_PDC_REVISION_ID;
  206. buf[1] = 1;
  207. buf[2] = (ACPI_PDC_C_CAPABILITY_SMP | ACPI_PDC_EST_CAPABILITY_SWSMP);
  208. set_xen_guest_handle(op.u.set_pminfo.pdc, buf);
  209. if ((HYPERVISOR_platform_op(&op) == 0) &&
  210. (buf[2] & (ACPI_PDC_C_C1_FFH | ACPI_PDC_C_C2C3_FFH))) {
  211. cpuid_leaf5_ecx_val = cx;
  212. cpuid_leaf5_edx_val = dx;
  213. }
  214. return true;
  215. #else
  216. return false;
  217. #endif
  218. }
  219. static bool __init xen_check_xsave(void)
  220. {
  221. unsigned int cx, xsave_mask;
  222. cx = cpuid_ecx(1);
  223. xsave_mask = (1 << (X86_FEATURE_XSAVE % 32)) |
  224. (1 << (X86_FEATURE_OSXSAVE % 32));
  225. /* Xen will set CR4.OSXSAVE if supported and not disabled by force */
  226. return (cx & xsave_mask) == xsave_mask;
  227. }
  228. static void __init xen_init_capabilities(void)
  229. {
  230. setup_force_cpu_cap(X86_FEATURE_XENPV);
  231. setup_clear_cpu_cap(X86_FEATURE_DCA);
  232. setup_clear_cpu_cap(X86_FEATURE_APERFMPERF);
  233. setup_clear_cpu_cap(X86_FEATURE_MTRR);
  234. setup_clear_cpu_cap(X86_FEATURE_ACC);
  235. setup_clear_cpu_cap(X86_FEATURE_X2APIC);
  236. setup_clear_cpu_cap(X86_FEATURE_SME);
  237. /*
  238. * Xen PV would need some work to support PCID: CR3 handling as well
  239. * as xen_flush_tlb_others() would need updating.
  240. */
  241. setup_clear_cpu_cap(X86_FEATURE_PCID);
  242. if (!xen_initial_domain())
  243. setup_clear_cpu_cap(X86_FEATURE_ACPI);
  244. if (xen_check_mwait())
  245. setup_force_cpu_cap(X86_FEATURE_MWAIT);
  246. else
  247. setup_clear_cpu_cap(X86_FEATURE_MWAIT);
  248. if (!xen_check_xsave()) {
  249. setup_clear_cpu_cap(X86_FEATURE_XSAVE);
  250. setup_clear_cpu_cap(X86_FEATURE_OSXSAVE);
  251. }
  252. }
  253. static noinstr void xen_set_debugreg(int reg, unsigned long val)
  254. {
  255. HYPERVISOR_set_debugreg(reg, val);
  256. }
  257. static noinstr unsigned long xen_get_debugreg(int reg)
  258. {
  259. return HYPERVISOR_get_debugreg(reg);
  260. }
  261. static void xen_end_context_switch(struct task_struct *next)
  262. {
  263. xen_mc_flush();
  264. paravirt_end_context_switch(next);
  265. }
  266. static unsigned long xen_store_tr(void)
  267. {
  268. return 0;
  269. }
  270. /*
  271. * Set the page permissions for a particular virtual address. If the
  272. * address is a vmalloc mapping (or other non-linear mapping), then
  273. * find the linear mapping of the page and also set its protections to
  274. * match.
  275. */
  276. static void set_aliased_prot(void *v, pgprot_t prot)
  277. {
  278. int level;
  279. pte_t *ptep;
  280. pte_t pte;
  281. unsigned long pfn;
  282. unsigned char dummy;
  283. void *va;
  284. ptep = lookup_address((unsigned long)v, &level);
  285. BUG_ON(ptep == NULL);
  286. pfn = pte_pfn(*ptep);
  287. pte = pfn_pte(pfn, prot);
  288. /*
  289. * Careful: update_va_mapping() will fail if the virtual address
  290. * we're poking isn't populated in the page tables. We don't
  291. * need to worry about the direct map (that's always in the page
  292. * tables), but we need to be careful about vmap space. In
  293. * particular, the top level page table can lazily propagate
  294. * entries between processes, so if we've switched mms since we
  295. * vmapped the target in the first place, we might not have the
  296. * top-level page table entry populated.
  297. *
  298. * We disable preemption because we want the same mm active when
  299. * we probe the target and when we issue the hypercall. We'll
  300. * have the same nominal mm, but if we're a kernel thread, lazy
  301. * mm dropping could change our pgd.
  302. *
  303. * Out of an abundance of caution, this uses __get_user() to fault
  304. * in the target address just in case there's some obscure case
  305. * in which the target address isn't readable.
  306. */
  307. preempt_disable();
  308. copy_from_kernel_nofault(&dummy, v, 1);
  309. if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
  310. BUG();
  311. va = __va(PFN_PHYS(pfn));
  312. if (va != v && HYPERVISOR_update_va_mapping((unsigned long)va, pte, 0))
  313. BUG();
  314. preempt_enable();
  315. }
  316. static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
  317. {
  318. const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
  319. int i;
  320. /*
  321. * We need to mark the all aliases of the LDT pages RO. We
  322. * don't need to call vm_flush_aliases(), though, since that's
  323. * only responsible for flushing aliases out the TLBs, not the
  324. * page tables, and Xen will flush the TLB for us if needed.
  325. *
  326. * To avoid confusing future readers: none of this is necessary
  327. * to load the LDT. The hypervisor only checks this when the
  328. * LDT is faulted in due to subsequent descriptor access.
  329. */
  330. for (i = 0; i < entries; i += entries_per_page)
  331. set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
  332. }
  333. static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
  334. {
  335. const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
  336. int i;
  337. for (i = 0; i < entries; i += entries_per_page)
  338. set_aliased_prot(ldt + i, PAGE_KERNEL);
  339. }
  340. static void xen_set_ldt(const void *addr, unsigned entries)
  341. {
  342. struct mmuext_op *op;
  343. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  344. trace_xen_cpu_set_ldt(addr, entries);
  345. op = mcs.args;
  346. op->cmd = MMUEXT_SET_LDT;
  347. op->arg1.linear_addr = (unsigned long)addr;
  348. op->arg2.nr_ents = entries;
  349. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  350. xen_mc_issue(PARAVIRT_LAZY_CPU);
  351. }
  352. static void xen_load_gdt(const struct desc_ptr *dtr)
  353. {
  354. unsigned long va = dtr->address;
  355. unsigned int size = dtr->size + 1;
  356. unsigned long pfn, mfn;
  357. int level;
  358. pte_t *ptep;
  359. void *virt;
  360. /* @size should be at most GDT_SIZE which is smaller than PAGE_SIZE. */
  361. BUG_ON(size > PAGE_SIZE);
  362. BUG_ON(va & ~PAGE_MASK);
  363. /*
  364. * The GDT is per-cpu and is in the percpu data area.
  365. * That can be virtually mapped, so we need to do a
  366. * page-walk to get the underlying MFN for the
  367. * hypercall. The page can also be in the kernel's
  368. * linear range, so we need to RO that mapping too.
  369. */
  370. ptep = lookup_address(va, &level);
  371. BUG_ON(ptep == NULL);
  372. pfn = pte_pfn(*ptep);
  373. mfn = pfn_to_mfn(pfn);
  374. virt = __va(PFN_PHYS(pfn));
  375. make_lowmem_page_readonly((void *)va);
  376. make_lowmem_page_readonly(virt);
  377. if (HYPERVISOR_set_gdt(&mfn, size / sizeof(struct desc_struct)))
  378. BUG();
  379. }
  380. /*
  381. * load_gdt for early boot, when the gdt is only mapped once
  382. */
  383. static void __init xen_load_gdt_boot(const struct desc_ptr *dtr)
  384. {
  385. unsigned long va = dtr->address;
  386. unsigned int size = dtr->size + 1;
  387. unsigned long pfn, mfn;
  388. pte_t pte;
  389. /* @size should be at most GDT_SIZE which is smaller than PAGE_SIZE. */
  390. BUG_ON(size > PAGE_SIZE);
  391. BUG_ON(va & ~PAGE_MASK);
  392. pfn = virt_to_pfn(va);
  393. mfn = pfn_to_mfn(pfn);
  394. pte = pfn_pte(pfn, PAGE_KERNEL_RO);
  395. if (HYPERVISOR_update_va_mapping((unsigned long)va, pte, 0))
  396. BUG();
  397. if (HYPERVISOR_set_gdt(&mfn, size / sizeof(struct desc_struct)))
  398. BUG();
  399. }
  400. static inline bool desc_equal(const struct desc_struct *d1,
  401. const struct desc_struct *d2)
  402. {
  403. return !memcmp(d1, d2, sizeof(*d1));
  404. }
  405. static void load_TLS_descriptor(struct thread_struct *t,
  406. unsigned int cpu, unsigned int i)
  407. {
  408. struct desc_struct *shadow = &per_cpu(shadow_tls_desc, cpu).desc[i];
  409. struct desc_struct *gdt;
  410. xmaddr_t maddr;
  411. struct multicall_space mc;
  412. if (desc_equal(shadow, &t->tls_array[i]))
  413. return;
  414. *shadow = t->tls_array[i];
  415. gdt = get_cpu_gdt_rw(cpu);
  416. maddr = arbitrary_virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
  417. mc = __xen_mc_entry(0);
  418. MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
  419. }
  420. static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
  421. {
  422. /*
  423. * In lazy mode we need to zero %fs, otherwise we may get an
  424. * exception between the new %fs descriptor being loaded and
  425. * %fs being effectively cleared at __switch_to().
  426. */
  427. if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU)
  428. loadsegment(fs, 0);
  429. xen_mc_batch();
  430. load_TLS_descriptor(t, cpu, 0);
  431. load_TLS_descriptor(t, cpu, 1);
  432. load_TLS_descriptor(t, cpu, 2);
  433. xen_mc_issue(PARAVIRT_LAZY_CPU);
  434. }
  435. static void xen_load_gs_index(unsigned int idx)
  436. {
  437. if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
  438. BUG();
  439. }
  440. static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
  441. const void *ptr)
  442. {
  443. xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
  444. u64 entry = *(u64 *)ptr;
  445. trace_xen_cpu_write_ldt_entry(dt, entrynum, entry);
  446. preempt_disable();
  447. xen_mc_flush();
  448. if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
  449. BUG();
  450. preempt_enable();
  451. }
  452. void noist_exc_debug(struct pt_regs *regs);
  453. DEFINE_IDTENTRY_RAW(xenpv_exc_nmi)
  454. {
  455. /* On Xen PV, NMI doesn't use IST. The C part is the same as native. */
  456. exc_nmi(regs);
  457. }
  458. DEFINE_IDTENTRY_RAW_ERRORCODE(xenpv_exc_double_fault)
  459. {
  460. /* On Xen PV, DF doesn't use IST. The C part is the same as native. */
  461. exc_double_fault(regs, error_code);
  462. }
  463. DEFINE_IDTENTRY_RAW(xenpv_exc_debug)
  464. {
  465. /*
  466. * There's no IST on Xen PV, but we still need to dispatch
  467. * to the correct handler.
  468. */
  469. if (user_mode(regs))
  470. noist_exc_debug(regs);
  471. else
  472. exc_debug(regs);
  473. }
  474. DEFINE_IDTENTRY_RAW(exc_xen_unknown_trap)
  475. {
  476. /* This should never happen and there is no way to handle it. */
  477. instrumentation_begin();
  478. pr_err("Unknown trap in Xen PV mode.");
  479. BUG();
  480. instrumentation_end();
  481. }
  482. #ifdef CONFIG_X86_MCE
  483. DEFINE_IDTENTRY_RAW(xenpv_exc_machine_check)
  484. {
  485. /*
  486. * There's no IST on Xen PV, but we still need to dispatch
  487. * to the correct handler.
  488. */
  489. if (user_mode(regs))
  490. noist_exc_machine_check(regs);
  491. else
  492. exc_machine_check(regs);
  493. }
  494. #endif
  495. struct trap_array_entry {
  496. void (*orig)(void);
  497. void (*xen)(void);
  498. bool ist_okay;
  499. };
  500. #define TRAP_ENTRY(func, ist_ok) { \
  501. .orig = asm_##func, \
  502. .xen = xen_asm_##func, \
  503. .ist_okay = ist_ok }
  504. #define TRAP_ENTRY_REDIR(func, ist_ok) { \
  505. .orig = asm_##func, \
  506. .xen = xen_asm_xenpv_##func, \
  507. .ist_okay = ist_ok }
  508. static struct trap_array_entry trap_array[] = {
  509. TRAP_ENTRY_REDIR(exc_debug, true ),
  510. TRAP_ENTRY_REDIR(exc_double_fault, true ),
  511. #ifdef CONFIG_X86_MCE
  512. TRAP_ENTRY_REDIR(exc_machine_check, true ),
  513. #endif
  514. TRAP_ENTRY_REDIR(exc_nmi, true ),
  515. TRAP_ENTRY(exc_int3, false ),
  516. TRAP_ENTRY(exc_overflow, false ),
  517. #ifdef CONFIG_IA32_EMULATION
  518. TRAP_ENTRY(int80_emulation, false ),
  519. #endif
  520. TRAP_ENTRY(exc_page_fault, false ),
  521. TRAP_ENTRY(exc_divide_error, false ),
  522. TRAP_ENTRY(exc_bounds, false ),
  523. TRAP_ENTRY(exc_invalid_op, false ),
  524. TRAP_ENTRY(exc_device_not_available, false ),
  525. TRAP_ENTRY(exc_coproc_segment_overrun, false ),
  526. TRAP_ENTRY(exc_invalid_tss, false ),
  527. TRAP_ENTRY(exc_segment_not_present, false ),
  528. TRAP_ENTRY(exc_stack_segment, false ),
  529. TRAP_ENTRY(exc_general_protection, false ),
  530. TRAP_ENTRY(exc_spurious_interrupt_bug, false ),
  531. TRAP_ENTRY(exc_coprocessor_error, false ),
  532. TRAP_ENTRY(exc_alignment_check, false ),
  533. TRAP_ENTRY(exc_simd_coprocessor_error, false ),
  534. #ifdef CONFIG_X86_KERNEL_IBT
  535. TRAP_ENTRY(exc_control_protection, false ),
  536. #endif
  537. };
  538. static bool __ref get_trap_addr(void **addr, unsigned int ist)
  539. {
  540. unsigned int nr;
  541. bool ist_okay = false;
  542. bool found = false;
  543. /*
  544. * Replace trap handler addresses by Xen specific ones.
  545. * Check for known traps using IST and whitelist them.
  546. * The debugger ones are the only ones we care about.
  547. * Xen will handle faults like double_fault, so we should never see
  548. * them. Warn if there's an unexpected IST-using fault handler.
  549. */
  550. for (nr = 0; nr < ARRAY_SIZE(trap_array); nr++) {
  551. struct trap_array_entry *entry = trap_array + nr;
  552. if (*addr == entry->orig) {
  553. *addr = entry->xen;
  554. ist_okay = entry->ist_okay;
  555. found = true;
  556. break;
  557. }
  558. }
  559. if (nr == ARRAY_SIZE(trap_array) &&
  560. *addr >= (void *)early_idt_handler_array[0] &&
  561. *addr < (void *)early_idt_handler_array[NUM_EXCEPTION_VECTORS]) {
  562. nr = (*addr - (void *)early_idt_handler_array[0]) /
  563. EARLY_IDT_HANDLER_SIZE;
  564. *addr = (void *)xen_early_idt_handler_array[nr];
  565. found = true;
  566. }
  567. if (!found)
  568. *addr = (void *)xen_asm_exc_xen_unknown_trap;
  569. if (WARN_ON(found && ist != 0 && !ist_okay))
  570. return false;
  571. return true;
  572. }
  573. static int cvt_gate_to_trap(int vector, const gate_desc *val,
  574. struct trap_info *info)
  575. {
  576. unsigned long addr;
  577. if (val->bits.type != GATE_TRAP && val->bits.type != GATE_INTERRUPT)
  578. return 0;
  579. info->vector = vector;
  580. addr = gate_offset(val);
  581. if (!get_trap_addr((void **)&addr, val->bits.ist))
  582. return 0;
  583. info->address = addr;
  584. info->cs = gate_segment(val);
  585. info->flags = val->bits.dpl;
  586. /* interrupt gates clear IF */
  587. if (val->bits.type == GATE_INTERRUPT)
  588. info->flags |= 1 << 2;
  589. return 1;
  590. }
  591. /* Locations of each CPU's IDT */
  592. static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
  593. /* Set an IDT entry. If the entry is part of the current IDT, then
  594. also update Xen. */
  595. static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
  596. {
  597. unsigned long p = (unsigned long)&dt[entrynum];
  598. unsigned long start, end;
  599. trace_xen_cpu_write_idt_entry(dt, entrynum, g);
  600. preempt_disable();
  601. start = __this_cpu_read(idt_desc.address);
  602. end = start + __this_cpu_read(idt_desc.size) + 1;
  603. xen_mc_flush();
  604. native_write_idt_entry(dt, entrynum, g);
  605. if (p >= start && (p + 8) <= end) {
  606. struct trap_info info[2];
  607. info[1].address = 0;
  608. if (cvt_gate_to_trap(entrynum, g, &info[0]))
  609. if (HYPERVISOR_set_trap_table(info))
  610. BUG();
  611. }
  612. preempt_enable();
  613. }
  614. static unsigned xen_convert_trap_info(const struct desc_ptr *desc,
  615. struct trap_info *traps, bool full)
  616. {
  617. unsigned in, out, count;
  618. count = (desc->size+1) / sizeof(gate_desc);
  619. BUG_ON(count > 256);
  620. for (in = out = 0; in < count; in++) {
  621. gate_desc *entry = (gate_desc *)(desc->address) + in;
  622. if (cvt_gate_to_trap(in, entry, &traps[out]) || full)
  623. out++;
  624. }
  625. return out;
  626. }
  627. void xen_copy_trap_info(struct trap_info *traps)
  628. {
  629. const struct desc_ptr *desc = this_cpu_ptr(&idt_desc);
  630. xen_convert_trap_info(desc, traps, true);
  631. }
  632. /* Load a new IDT into Xen. In principle this can be per-CPU, so we
  633. hold a spinlock to protect the static traps[] array (static because
  634. it avoids allocation, and saves stack space). */
  635. static void xen_load_idt(const struct desc_ptr *desc)
  636. {
  637. static DEFINE_SPINLOCK(lock);
  638. static struct trap_info traps[257];
  639. static const struct trap_info zero = { };
  640. unsigned out;
  641. trace_xen_cpu_load_idt(desc);
  642. spin_lock(&lock);
  643. memcpy(this_cpu_ptr(&idt_desc), desc, sizeof(idt_desc));
  644. out = xen_convert_trap_info(desc, traps, false);
  645. traps[out] = zero;
  646. xen_mc_flush();
  647. if (HYPERVISOR_set_trap_table(traps))
  648. BUG();
  649. spin_unlock(&lock);
  650. }
  651. /* Write a GDT descriptor entry. Ignore LDT descriptors, since
  652. they're handled differently. */
  653. static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
  654. const void *desc, int type)
  655. {
  656. trace_xen_cpu_write_gdt_entry(dt, entry, desc, type);
  657. preempt_disable();
  658. switch (type) {
  659. case DESC_LDT:
  660. case DESC_TSS:
  661. /* ignore */
  662. break;
  663. default: {
  664. xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
  665. xen_mc_flush();
  666. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  667. BUG();
  668. }
  669. }
  670. preempt_enable();
  671. }
  672. /*
  673. * Version of write_gdt_entry for use at early boot-time needed to
  674. * update an entry as simply as possible.
  675. */
  676. static void __init xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
  677. const void *desc, int type)
  678. {
  679. trace_xen_cpu_write_gdt_entry(dt, entry, desc, type);
  680. switch (type) {
  681. case DESC_LDT:
  682. case DESC_TSS:
  683. /* ignore */
  684. break;
  685. default: {
  686. xmaddr_t maddr = virt_to_machine(&dt[entry]);
  687. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  688. dt[entry] = *(struct desc_struct *)desc;
  689. }
  690. }
  691. }
  692. static void xen_load_sp0(unsigned long sp0)
  693. {
  694. struct multicall_space mcs;
  695. mcs = xen_mc_entry(0);
  696. MULTI_stack_switch(mcs.mc, __KERNEL_DS, sp0);
  697. xen_mc_issue(PARAVIRT_LAZY_CPU);
  698. this_cpu_write(cpu_tss_rw.x86_tss.sp0, sp0);
  699. }
  700. #ifdef CONFIG_X86_IOPL_IOPERM
  701. static void xen_invalidate_io_bitmap(void)
  702. {
  703. struct physdev_set_iobitmap iobitmap = {
  704. .bitmap = NULL,
  705. .nr_ports = 0,
  706. };
  707. native_tss_invalidate_io_bitmap();
  708. HYPERVISOR_physdev_op(PHYSDEVOP_set_iobitmap, &iobitmap);
  709. }
  710. static void xen_update_io_bitmap(void)
  711. {
  712. struct physdev_set_iobitmap iobitmap;
  713. struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
  714. native_tss_update_io_bitmap();
  715. iobitmap.bitmap = (uint8_t *)(&tss->x86_tss) +
  716. tss->x86_tss.io_bitmap_base;
  717. if (tss->x86_tss.io_bitmap_base == IO_BITMAP_OFFSET_INVALID)
  718. iobitmap.nr_ports = 0;
  719. else
  720. iobitmap.nr_ports = IO_BITMAP_BITS;
  721. HYPERVISOR_physdev_op(PHYSDEVOP_set_iobitmap, &iobitmap);
  722. }
  723. #endif
  724. static void xen_io_delay(void)
  725. {
  726. }
  727. static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
  728. static unsigned long xen_read_cr0(void)
  729. {
  730. unsigned long cr0 = this_cpu_read(xen_cr0_value);
  731. if (unlikely(cr0 == 0)) {
  732. cr0 = native_read_cr0();
  733. this_cpu_write(xen_cr0_value, cr0);
  734. }
  735. return cr0;
  736. }
  737. static void xen_write_cr0(unsigned long cr0)
  738. {
  739. struct multicall_space mcs;
  740. this_cpu_write(xen_cr0_value, cr0);
  741. /* Only pay attention to cr0.TS; everything else is
  742. ignored. */
  743. mcs = xen_mc_entry(0);
  744. MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
  745. xen_mc_issue(PARAVIRT_LAZY_CPU);
  746. }
  747. static void xen_write_cr4(unsigned long cr4)
  748. {
  749. cr4 &= ~(X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PCE);
  750. native_write_cr4(cr4);
  751. }
  752. static u64 xen_do_read_msr(unsigned int msr, int *err)
  753. {
  754. u64 val = 0; /* Avoid uninitialized value for safe variant. */
  755. if (pmu_msr_read(msr, &val, err))
  756. return val;
  757. if (err)
  758. val = native_read_msr_safe(msr, err);
  759. else
  760. val = native_read_msr(msr);
  761. switch (msr) {
  762. case MSR_IA32_APICBASE:
  763. val &= ~X2APIC_ENABLE;
  764. break;
  765. }
  766. return val;
  767. }
  768. static void set_seg(unsigned int which, unsigned int low, unsigned int high,
  769. int *err)
  770. {
  771. u64 base = ((u64)high << 32) | low;
  772. if (HYPERVISOR_set_segment_base(which, base) == 0)
  773. return;
  774. if (err)
  775. *err = -EIO;
  776. else
  777. WARN(1, "Xen set_segment_base(%u, %llx) failed\n", which, base);
  778. }
  779. /*
  780. * Support write_msr_safe() and write_msr() semantics.
  781. * With err == NULL write_msr() semantics are selected.
  782. * Supplying an err pointer requires err to be pre-initialized with 0.
  783. */
  784. static void xen_do_write_msr(unsigned int msr, unsigned int low,
  785. unsigned int high, int *err)
  786. {
  787. switch (msr) {
  788. case MSR_FS_BASE:
  789. set_seg(SEGBASE_FS, low, high, err);
  790. break;
  791. case MSR_KERNEL_GS_BASE:
  792. set_seg(SEGBASE_GS_USER, low, high, err);
  793. break;
  794. case MSR_GS_BASE:
  795. set_seg(SEGBASE_GS_KERNEL, low, high, err);
  796. break;
  797. case MSR_STAR:
  798. case MSR_CSTAR:
  799. case MSR_LSTAR:
  800. case MSR_SYSCALL_MASK:
  801. case MSR_IA32_SYSENTER_CS:
  802. case MSR_IA32_SYSENTER_ESP:
  803. case MSR_IA32_SYSENTER_EIP:
  804. /* Fast syscall setup is all done in hypercalls, so
  805. these are all ignored. Stub them out here to stop
  806. Xen console noise. */
  807. break;
  808. default:
  809. if (!pmu_msr_write(msr, low, high, err)) {
  810. if (err)
  811. *err = native_write_msr_safe(msr, low, high);
  812. else
  813. native_write_msr(msr, low, high);
  814. }
  815. }
  816. }
  817. static u64 xen_read_msr_safe(unsigned int msr, int *err)
  818. {
  819. return xen_do_read_msr(msr, err);
  820. }
  821. static int xen_write_msr_safe(unsigned int msr, unsigned int low,
  822. unsigned int high)
  823. {
  824. int err = 0;
  825. xen_do_write_msr(msr, low, high, &err);
  826. return err;
  827. }
  828. static u64 xen_read_msr(unsigned int msr)
  829. {
  830. int err;
  831. return xen_do_read_msr(msr, xen_msr_safe ? &err : NULL);
  832. }
  833. static void xen_write_msr(unsigned int msr, unsigned low, unsigned high)
  834. {
  835. int err;
  836. xen_do_write_msr(msr, low, high, xen_msr_safe ? &err : NULL);
  837. }
  838. /* This is called once we have the cpu_possible_mask */
  839. void __init xen_setup_vcpu_info_placement(void)
  840. {
  841. int cpu;
  842. for_each_possible_cpu(cpu) {
  843. /* Set up direct vCPU id mapping for PV guests. */
  844. per_cpu(xen_vcpu_id, cpu) = cpu;
  845. xen_vcpu_setup(cpu);
  846. }
  847. pv_ops.irq.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
  848. pv_ops.irq.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
  849. pv_ops.irq.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
  850. pv_ops.mmu.read_cr2 = __PV_IS_CALLEE_SAVE(xen_read_cr2_direct);
  851. }
  852. static const struct pv_info xen_info __initconst = {
  853. .extra_user_64bit_cs = FLAT_USER_CS64,
  854. .name = "Xen",
  855. };
  856. static const typeof(pv_ops) xen_cpu_ops __initconst = {
  857. .cpu = {
  858. .cpuid = xen_cpuid,
  859. .set_debugreg = xen_set_debugreg,
  860. .get_debugreg = xen_get_debugreg,
  861. .read_cr0 = xen_read_cr0,
  862. .write_cr0 = xen_write_cr0,
  863. .write_cr4 = xen_write_cr4,
  864. .wbinvd = native_wbinvd,
  865. .read_msr = xen_read_msr,
  866. .write_msr = xen_write_msr,
  867. .read_msr_safe = xen_read_msr_safe,
  868. .write_msr_safe = xen_write_msr_safe,
  869. .read_pmc = xen_read_pmc,
  870. .load_tr_desc = paravirt_nop,
  871. .set_ldt = xen_set_ldt,
  872. .load_gdt = xen_load_gdt,
  873. .load_idt = xen_load_idt,
  874. .load_tls = xen_load_tls,
  875. .load_gs_index = xen_load_gs_index,
  876. .alloc_ldt = xen_alloc_ldt,
  877. .free_ldt = xen_free_ldt,
  878. .store_tr = xen_store_tr,
  879. .write_ldt_entry = xen_write_ldt_entry,
  880. .write_gdt_entry = xen_write_gdt_entry,
  881. .write_idt_entry = xen_write_idt_entry,
  882. .load_sp0 = xen_load_sp0,
  883. #ifdef CONFIG_X86_IOPL_IOPERM
  884. .invalidate_io_bitmap = xen_invalidate_io_bitmap,
  885. .update_io_bitmap = xen_update_io_bitmap,
  886. #endif
  887. .io_delay = xen_io_delay,
  888. .start_context_switch = paravirt_start_context_switch,
  889. .end_context_switch = xen_end_context_switch,
  890. },
  891. };
  892. static void xen_restart(char *msg)
  893. {
  894. xen_reboot(SHUTDOWN_reboot);
  895. }
  896. static void xen_machine_halt(void)
  897. {
  898. xen_reboot(SHUTDOWN_poweroff);
  899. }
  900. static void xen_machine_power_off(void)
  901. {
  902. do_kernel_power_off();
  903. xen_reboot(SHUTDOWN_poweroff);
  904. }
  905. static void xen_crash_shutdown(struct pt_regs *regs)
  906. {
  907. xen_reboot(SHUTDOWN_crash);
  908. }
  909. static const struct machine_ops xen_machine_ops __initconst = {
  910. .restart = xen_restart,
  911. .halt = xen_machine_halt,
  912. .power_off = xen_machine_power_off,
  913. .shutdown = xen_machine_halt,
  914. .crash_shutdown = xen_crash_shutdown,
  915. .emergency_restart = xen_emergency_restart,
  916. };
  917. static unsigned char xen_get_nmi_reason(void)
  918. {
  919. unsigned char reason = 0;
  920. /* Construct a value which looks like it came from port 0x61. */
  921. if (test_bit(_XEN_NMIREASON_io_error,
  922. &HYPERVISOR_shared_info->arch.nmi_reason))
  923. reason |= NMI_REASON_IOCHK;
  924. if (test_bit(_XEN_NMIREASON_pci_serr,
  925. &HYPERVISOR_shared_info->arch.nmi_reason))
  926. reason |= NMI_REASON_SERR;
  927. return reason;
  928. }
  929. static void __init xen_boot_params_init_edd(void)
  930. {
  931. #if IS_ENABLED(CONFIG_EDD)
  932. struct xen_platform_op op;
  933. struct edd_info *edd_info;
  934. u32 *mbr_signature;
  935. unsigned nr;
  936. int ret;
  937. edd_info = boot_params.eddbuf;
  938. mbr_signature = boot_params.edd_mbr_sig_buffer;
  939. op.cmd = XENPF_firmware_info;
  940. op.u.firmware_info.type = XEN_FW_DISK_INFO;
  941. for (nr = 0; nr < EDDMAXNR; nr++) {
  942. struct edd_info *info = edd_info + nr;
  943. op.u.firmware_info.index = nr;
  944. info->params.length = sizeof(info->params);
  945. set_xen_guest_handle(op.u.firmware_info.u.disk_info.edd_params,
  946. &info->params);
  947. ret = HYPERVISOR_platform_op(&op);
  948. if (ret)
  949. break;
  950. #define C(x) info->x = op.u.firmware_info.u.disk_info.x
  951. C(device);
  952. C(version);
  953. C(interface_support);
  954. C(legacy_max_cylinder);
  955. C(legacy_max_head);
  956. C(legacy_sectors_per_track);
  957. #undef C
  958. }
  959. boot_params.eddbuf_entries = nr;
  960. op.u.firmware_info.type = XEN_FW_DISK_MBR_SIGNATURE;
  961. for (nr = 0; nr < EDD_MBR_SIG_MAX; nr++) {
  962. op.u.firmware_info.index = nr;
  963. ret = HYPERVISOR_platform_op(&op);
  964. if (ret)
  965. break;
  966. mbr_signature[nr] = op.u.firmware_info.u.disk_mbr_signature.mbr_signature;
  967. }
  968. boot_params.edd_mbr_sig_buf_entries = nr;
  969. #endif
  970. }
  971. /*
  972. * Set up the GDT and segment registers for -fstack-protector. Until
  973. * we do this, we have to be careful not to call any stack-protected
  974. * function, which is most of the kernel.
  975. */
  976. static void __init xen_setup_gdt(int cpu)
  977. {
  978. pv_ops.cpu.write_gdt_entry = xen_write_gdt_entry_boot;
  979. pv_ops.cpu.load_gdt = xen_load_gdt_boot;
  980. switch_to_new_gdt(cpu);
  981. pv_ops.cpu.write_gdt_entry = xen_write_gdt_entry;
  982. pv_ops.cpu.load_gdt = xen_load_gdt;
  983. }
  984. static void __init xen_dom0_set_legacy_features(void)
  985. {
  986. x86_platform.legacy.rtc = 1;
  987. }
  988. static void __init xen_domu_set_legacy_features(void)
  989. {
  990. x86_platform.legacy.rtc = 0;
  991. }
  992. extern void early_xen_iret_patch(void);
  993. /* First C function to be called on Xen boot */
  994. asmlinkage __visible void __init xen_start_kernel(struct start_info *si)
  995. {
  996. struct physdev_set_iopl set_iopl;
  997. unsigned long initrd_start = 0;
  998. int rc;
  999. if (!si)
  1000. return;
  1001. clear_bss();
  1002. xen_start_info = si;
  1003. __text_gen_insn(&early_xen_iret_patch,
  1004. JMP32_INSN_OPCODE, &early_xen_iret_patch, &xen_iret,
  1005. JMP32_INSN_SIZE);
  1006. xen_domain_type = XEN_PV_DOMAIN;
  1007. xen_start_flags = xen_start_info->flags;
  1008. xen_setup_features();
  1009. /* Install Xen paravirt ops */
  1010. pv_info = xen_info;
  1011. pv_ops.cpu = xen_cpu_ops.cpu;
  1012. xen_init_irq_ops();
  1013. /*
  1014. * Setup xen_vcpu early because it is needed for
  1015. * local_irq_disable(), irqs_disabled(), e.g. in printk().
  1016. *
  1017. * Don't do the full vcpu_info placement stuff until we have
  1018. * the cpu_possible_mask and a non-dummy shared_info.
  1019. */
  1020. xen_vcpu_info_reset(0);
  1021. x86_platform.get_nmi_reason = xen_get_nmi_reason;
  1022. x86_platform.realmode_reserve = x86_init_noop;
  1023. x86_platform.realmode_init = x86_init_noop;
  1024. x86_init.resources.memory_setup = xen_memory_setup;
  1025. x86_init.irqs.intr_mode_select = x86_init_noop;
  1026. x86_init.irqs.intr_mode_init = x86_init_noop;
  1027. x86_init.oem.arch_setup = xen_arch_setup;
  1028. x86_init.oem.banner = xen_banner;
  1029. x86_init.hyper.init_platform = xen_pv_init_platform;
  1030. x86_init.hyper.guest_late_init = xen_pv_guest_late_init;
  1031. /*
  1032. * Set up some pagetable state before starting to set any ptes.
  1033. */
  1034. xen_setup_machphys_mapping();
  1035. xen_init_mmu_ops();
  1036. /* Prevent unwanted bits from being set in PTEs. */
  1037. __supported_pte_mask &= ~_PAGE_GLOBAL;
  1038. __default_kernel_pte_mask &= ~_PAGE_GLOBAL;
  1039. /* Get mfn list */
  1040. xen_build_dynamic_phys_to_machine();
  1041. /* Work out if we support NX */
  1042. get_cpu_cap(&boot_cpu_data);
  1043. x86_configure_nx();
  1044. /*
  1045. * Set up kernel GDT and segment registers, mainly so that
  1046. * -fstack-protector code can be executed.
  1047. */
  1048. xen_setup_gdt(0);
  1049. /* Determine virtual and physical address sizes */
  1050. get_cpu_address_sizes(&boot_cpu_data);
  1051. /* Let's presume PV guests always boot on vCPU with id 0. */
  1052. per_cpu(xen_vcpu_id, 0) = 0;
  1053. idt_setup_early_handler();
  1054. xen_init_capabilities();
  1055. #ifdef CONFIG_X86_LOCAL_APIC
  1056. /*
  1057. * set up the basic apic ops.
  1058. */
  1059. xen_init_apic();
  1060. #endif
  1061. machine_ops = xen_machine_ops;
  1062. /*
  1063. * The only reliable way to retain the initial address of the
  1064. * percpu gdt_page is to remember it here, so we can go and
  1065. * mark it RW later, when the initial percpu area is freed.
  1066. */
  1067. xen_initial_gdt = &per_cpu(gdt_page, 0);
  1068. xen_smp_init();
  1069. #ifdef CONFIG_ACPI_NUMA
  1070. /*
  1071. * The pages we from Xen are not related to machine pages, so
  1072. * any NUMA information the kernel tries to get from ACPI will
  1073. * be meaningless. Prevent it from trying.
  1074. */
  1075. disable_srat();
  1076. #endif
  1077. WARN_ON(xen_cpuhp_setup(xen_cpu_up_prepare_pv, xen_cpu_dead_pv));
  1078. local_irq_disable();
  1079. early_boot_irqs_disabled = true;
  1080. xen_raw_console_write("mapping kernel into physical memory\n");
  1081. xen_setup_kernel_pagetable((pgd_t *)xen_start_info->pt_base,
  1082. xen_start_info->nr_pages);
  1083. xen_reserve_special_pages();
  1084. /*
  1085. * We used to do this in xen_arch_setup, but that is too late
  1086. * on AMD were early_cpu_init (run before ->arch_setup()) calls
  1087. * early_amd_init which pokes 0xcf8 port.
  1088. */
  1089. set_iopl.iopl = 1;
  1090. rc = HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  1091. if (rc != 0)
  1092. xen_raw_printk("physdev_op failed %d\n", rc);
  1093. if (xen_start_info->mod_start) {
  1094. if (xen_start_info->flags & SIF_MOD_START_PFN)
  1095. initrd_start = PFN_PHYS(xen_start_info->mod_start);
  1096. else
  1097. initrd_start = __pa(xen_start_info->mod_start);
  1098. }
  1099. /* Poke various useful things into boot_params */
  1100. boot_params.hdr.type_of_loader = (9 << 4) | 0;
  1101. boot_params.hdr.ramdisk_image = initrd_start;
  1102. boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
  1103. boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
  1104. boot_params.hdr.hardware_subarch = X86_SUBARCH_XEN;
  1105. if (!xen_initial_domain()) {
  1106. if (pci_xen)
  1107. x86_init.pci.arch_init = pci_xen_init;
  1108. x86_platform.set_legacy_features =
  1109. xen_domu_set_legacy_features;
  1110. } else {
  1111. const struct dom0_vga_console_info *info =
  1112. (void *)((char *)xen_start_info +
  1113. xen_start_info->console.dom0.info_off);
  1114. struct xen_platform_op op = {
  1115. .cmd = XENPF_firmware_info,
  1116. .interface_version = XENPF_INTERFACE_VERSION,
  1117. .u.firmware_info.type = XEN_FW_KBD_SHIFT_FLAGS,
  1118. };
  1119. x86_platform.set_legacy_features =
  1120. xen_dom0_set_legacy_features;
  1121. xen_init_vga(info, xen_start_info->console.dom0.info_size,
  1122. &boot_params.screen_info);
  1123. xen_start_info->console.domU.mfn = 0;
  1124. xen_start_info->console.domU.evtchn = 0;
  1125. if (HYPERVISOR_platform_op(&op) == 0)
  1126. boot_params.kbd_status = op.u.firmware_info.u.kbd_shift_flags;
  1127. /* Make sure ACS will be enabled */
  1128. pci_request_acs();
  1129. xen_acpi_sleep_register();
  1130. xen_boot_params_init_edd();
  1131. #ifdef CONFIG_ACPI
  1132. /*
  1133. * Disable selecting "Firmware First mode" for correctable
  1134. * memory errors, as this is the duty of the hypervisor to
  1135. * decide.
  1136. */
  1137. acpi_disable_cmcff = 1;
  1138. #endif
  1139. }
  1140. xen_add_preferred_consoles();
  1141. #ifdef CONFIG_PCI
  1142. /* PCI BIOS service won't work from a PV guest. */
  1143. pci_probe &= ~PCI_PROBE_BIOS;
  1144. #endif
  1145. xen_raw_console_write("about to get started...\n");
  1146. /* We need this for printk timestamps */
  1147. xen_setup_runstate_info(0);
  1148. xen_efi_init(&boot_params);
  1149. /* Start the world */
  1150. cr4_init_shadow(); /* 32b kernel does this in i386_start_kernel() */
  1151. x86_64_start_reservations((char *)__pa_symbol(&boot_params));
  1152. }
  1153. static int xen_cpu_up_prepare_pv(unsigned int cpu)
  1154. {
  1155. int rc;
  1156. if (per_cpu(xen_vcpu, cpu) == NULL)
  1157. return -ENODEV;
  1158. xen_setup_timer(cpu);
  1159. rc = xen_smp_intr_init(cpu);
  1160. if (rc) {
  1161. WARN(1, "xen_smp_intr_init() for CPU %d failed: %d\n",
  1162. cpu, rc);
  1163. return rc;
  1164. }
  1165. rc = xen_smp_intr_init_pv(cpu);
  1166. if (rc) {
  1167. WARN(1, "xen_smp_intr_init_pv() for CPU %d failed: %d\n",
  1168. cpu, rc);
  1169. return rc;
  1170. }
  1171. return 0;
  1172. }
  1173. static int xen_cpu_dead_pv(unsigned int cpu)
  1174. {
  1175. xen_smp_intr_free(cpu);
  1176. xen_smp_intr_free_pv(cpu);
  1177. xen_teardown_timer(cpu);
  1178. return 0;
  1179. }
  1180. static uint32_t __init xen_platform_pv(void)
  1181. {
  1182. if (xen_pv_domain())
  1183. return xen_cpuid_base();
  1184. return 0;
  1185. }
  1186. const __initconst struct hypervisor_x86 x86_hyper_xen_pv = {
  1187. .name = "Xen PV",
  1188. .detect = xen_platform_pv,
  1189. .type = X86_HYPER_XEN_PV,
  1190. .runtime.pin_vcpu = xen_pin_vcpu,
  1191. .ignore_nopv = true,
  1192. };