enlighten_pv.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  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 <jeremy@xensource.com>, 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/bootmem.h>
  26. #include <linux/export.h>
  27. #include <linux/mm.h>
  28. #include <linux/page-flags.h>
  29. #include <linux/highmem.h>
  30. #include <linux/console.h>
  31. #include <linux/pci.h>
  32. #include <linux/gfp.h>
  33. #include <linux/memblock.h>
  34. #include <linux/edd.h>
  35. #include <linux/frame.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/hvc-console.h>
  48. #include <xen/acpi.h>
  49. #include <asm/paravirt.h>
  50. #include <asm/apic.h>
  51. #include <asm/page.h>
  52. #include <asm/xen/pci.h>
  53. #include <asm/xen/hypercall.h>
  54. #include <asm/xen/hypervisor.h>
  55. #include <asm/xen/cpuid.h>
  56. #include <asm/fixmap.h>
  57. #include <asm/processor.h>
  58. #include <asm/proto.h>
  59. #include <asm/msr-index.h>
  60. #include <asm/traps.h>
  61. #include <asm/setup.h>
  62. #include <asm/desc.h>
  63. #include <asm/pgalloc.h>
  64. #include <asm/pgtable.h>
  65. #include <asm/tlbflush.h>
  66. #include <asm/reboot.h>
  67. #include <asm/stackprotector.h>
  68. #include <asm/hypervisor.h>
  69. #include <asm/mach_traps.h>
  70. #include <asm/mwait.h>
  71. #include <asm/pci_x86.h>
  72. #include <asm/cpu.h>
  73. #ifdef CONFIG_ACPI
  74. #include <linux/acpi.h>
  75. #include <asm/acpi.h>
  76. #include <acpi/pdc_intel.h>
  77. #include <acpi/processor.h>
  78. #include <xen/interface/platform.h>
  79. #endif
  80. #include "xen-ops.h"
  81. #include "mmu.h"
  82. #include "smp.h"
  83. #include "multicalls.h"
  84. #include "pmu.h"
  85. #include "../kernel/cpu/cpu.h" /* get_cpu_cap() */
  86. void *xen_initial_gdt;
  87. static int xen_cpu_up_prepare_pv(unsigned int cpu);
  88. static int xen_cpu_dead_pv(unsigned int cpu);
  89. struct tls_descs {
  90. struct desc_struct desc[3];
  91. };
  92. /*
  93. * Updating the 3 TLS descriptors in the GDT on every task switch is
  94. * surprisingly expensive so we avoid updating them if they haven't
  95. * changed. Since Xen writes different descriptors than the one
  96. * passed in the update_descriptor hypercall we keep shadow copies to
  97. * compare against.
  98. */
  99. static DEFINE_PER_CPU(struct tls_descs, shadow_tls_desc);
  100. static void __init xen_banner(void)
  101. {
  102. unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
  103. struct xen_extraversion extra;
  104. HYPERVISOR_xen_version(XENVER_extraversion, &extra);
  105. pr_info("Booting paravirtualized kernel on %s\n", pv_info.name);
  106. printk(KERN_INFO "Xen version: %d.%d%s%s\n",
  107. version >> 16, version & 0xffff, extra.extraversion,
  108. xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
  109. }
  110. /* Check if running on Xen version (major, minor) or later */
  111. bool
  112. xen_running_on_version_or_later(unsigned int major, unsigned int minor)
  113. {
  114. unsigned int version;
  115. if (!xen_domain())
  116. return false;
  117. version = HYPERVISOR_xen_version(XENVER_version, NULL);
  118. if ((((version >> 16) == major) && ((version & 0xffff) >= minor)) ||
  119. ((version >> 16) > major))
  120. return true;
  121. return false;
  122. }
  123. static __read_mostly unsigned int cpuid_leaf5_ecx_val;
  124. static __read_mostly unsigned int cpuid_leaf5_edx_val;
  125. static void xen_cpuid(unsigned int *ax, unsigned int *bx,
  126. unsigned int *cx, unsigned int *dx)
  127. {
  128. unsigned maskebx = ~0;
  129. /*
  130. * Mask out inconvenient features, to try and disable as many
  131. * unsupported kernel subsystems as possible.
  132. */
  133. switch (*ax) {
  134. case CPUID_MWAIT_LEAF:
  135. /* Synthesize the values.. */
  136. *ax = 0;
  137. *bx = 0;
  138. *cx = cpuid_leaf5_ecx_val;
  139. *dx = cpuid_leaf5_edx_val;
  140. return;
  141. case 0xb:
  142. /* Suppress extended topology stuff */
  143. maskebx = 0;
  144. break;
  145. }
  146. asm(XEN_EMULATE_PREFIX "cpuid"
  147. : "=a" (*ax),
  148. "=b" (*bx),
  149. "=c" (*cx),
  150. "=d" (*dx)
  151. : "0" (*ax), "2" (*cx));
  152. *bx &= maskebx;
  153. }
  154. STACK_FRAME_NON_STANDARD(xen_cpuid); /* XEN_EMULATE_PREFIX */
  155. static bool __init xen_check_mwait(void)
  156. {
  157. #ifdef CONFIG_ACPI
  158. struct xen_platform_op op = {
  159. .cmd = XENPF_set_processor_pminfo,
  160. .u.set_pminfo.id = -1,
  161. .u.set_pminfo.type = XEN_PM_PDC,
  162. };
  163. uint32_t buf[3];
  164. unsigned int ax, bx, cx, dx;
  165. unsigned int mwait_mask;
  166. /* We need to determine whether it is OK to expose the MWAIT
  167. * capability to the kernel to harvest deeper than C3 states from ACPI
  168. * _CST using the processor_harvest_xen.c module. For this to work, we
  169. * need to gather the MWAIT_LEAF values (which the cstate.c code
  170. * checks against). The hypervisor won't expose the MWAIT flag because
  171. * it would break backwards compatibility; so we will find out directly
  172. * from the hardware and hypercall.
  173. */
  174. if (!xen_initial_domain())
  175. return false;
  176. /*
  177. * When running under platform earlier than Xen4.2, do not expose
  178. * mwait, to avoid the risk of loading native acpi pad driver
  179. */
  180. if (!xen_running_on_version_or_later(4, 2))
  181. return false;
  182. ax = 1;
  183. cx = 0;
  184. native_cpuid(&ax, &bx, &cx, &dx);
  185. mwait_mask = (1 << (X86_FEATURE_EST % 32)) |
  186. (1 << (X86_FEATURE_MWAIT % 32));
  187. if ((cx & mwait_mask) != mwait_mask)
  188. return false;
  189. /* We need to emulate the MWAIT_LEAF and for that we need both
  190. * ecx and edx. The hypercall provides only partial information.
  191. */
  192. ax = CPUID_MWAIT_LEAF;
  193. bx = 0;
  194. cx = 0;
  195. dx = 0;
  196. native_cpuid(&ax, &bx, &cx, &dx);
  197. /* Ask the Hypervisor whether to clear ACPI_PDC_C_C2C3_FFH. If so,
  198. * don't expose MWAIT_LEAF and let ACPI pick the IOPORT version of C3.
  199. */
  200. buf[0] = ACPI_PDC_REVISION_ID;
  201. buf[1] = 1;
  202. buf[2] = (ACPI_PDC_C_CAPABILITY_SMP | ACPI_PDC_EST_CAPABILITY_SWSMP);
  203. set_xen_guest_handle(op.u.set_pminfo.pdc, buf);
  204. if ((HYPERVISOR_platform_op(&op) == 0) &&
  205. (buf[2] & (ACPI_PDC_C_C1_FFH | ACPI_PDC_C_C2C3_FFH))) {
  206. cpuid_leaf5_ecx_val = cx;
  207. cpuid_leaf5_edx_val = dx;
  208. }
  209. return true;
  210. #else
  211. return false;
  212. #endif
  213. }
  214. static bool __init xen_check_xsave(void)
  215. {
  216. unsigned int cx, xsave_mask;
  217. cx = cpuid_ecx(1);
  218. xsave_mask = (1 << (X86_FEATURE_XSAVE % 32)) |
  219. (1 << (X86_FEATURE_OSXSAVE % 32));
  220. /* Xen will set CR4.OSXSAVE if supported and not disabled by force */
  221. return (cx & xsave_mask) == xsave_mask;
  222. }
  223. static void __init xen_init_capabilities(void)
  224. {
  225. setup_force_cpu_cap(X86_FEATURE_XENPV);
  226. setup_clear_cpu_cap(X86_FEATURE_DCA);
  227. setup_clear_cpu_cap(X86_FEATURE_APERFMPERF);
  228. setup_clear_cpu_cap(X86_FEATURE_MTRR);
  229. setup_clear_cpu_cap(X86_FEATURE_ACC);
  230. setup_clear_cpu_cap(X86_FEATURE_X2APIC);
  231. setup_clear_cpu_cap(X86_FEATURE_SME);
  232. /*
  233. * Xen PV would need some work to support PCID: CR3 handling as well
  234. * as xen_flush_tlb_others() would need updating.
  235. */
  236. setup_clear_cpu_cap(X86_FEATURE_PCID);
  237. if (!xen_initial_domain())
  238. setup_clear_cpu_cap(X86_FEATURE_ACPI);
  239. if (xen_check_mwait())
  240. setup_force_cpu_cap(X86_FEATURE_MWAIT);
  241. else
  242. setup_clear_cpu_cap(X86_FEATURE_MWAIT);
  243. if (!xen_check_xsave()) {
  244. setup_clear_cpu_cap(X86_FEATURE_XSAVE);
  245. setup_clear_cpu_cap(X86_FEATURE_OSXSAVE);
  246. }
  247. }
  248. static void xen_set_debugreg(int reg, unsigned long val)
  249. {
  250. HYPERVISOR_set_debugreg(reg, val);
  251. }
  252. static unsigned long xen_get_debugreg(int reg)
  253. {
  254. return HYPERVISOR_get_debugreg(reg);
  255. }
  256. static void xen_end_context_switch(struct task_struct *next)
  257. {
  258. xen_mc_flush();
  259. paravirt_end_context_switch(next);
  260. }
  261. static unsigned long xen_store_tr(void)
  262. {
  263. return 0;
  264. }
  265. /*
  266. * Set the page permissions for a particular virtual address. If the
  267. * address is a vmalloc mapping (or other non-linear mapping), then
  268. * find the linear mapping of the page and also set its protections to
  269. * match.
  270. */
  271. static void set_aliased_prot(void *v, pgprot_t prot)
  272. {
  273. int level;
  274. pte_t *ptep;
  275. pte_t pte;
  276. unsigned long pfn;
  277. struct page *page;
  278. unsigned char dummy;
  279. ptep = lookup_address((unsigned long)v, &level);
  280. BUG_ON(ptep == NULL);
  281. pfn = pte_pfn(*ptep);
  282. page = pfn_to_page(pfn);
  283. pte = pfn_pte(pfn, prot);
  284. /*
  285. * Careful: update_va_mapping() will fail if the virtual address
  286. * we're poking isn't populated in the page tables. We don't
  287. * need to worry about the direct map (that's always in the page
  288. * tables), but we need to be careful about vmap space. In
  289. * particular, the top level page table can lazily propagate
  290. * entries between processes, so if we've switched mms since we
  291. * vmapped the target in the first place, we might not have the
  292. * top-level page table entry populated.
  293. *
  294. * We disable preemption because we want the same mm active when
  295. * we probe the target and when we issue the hypercall. We'll
  296. * have the same nominal mm, but if we're a kernel thread, lazy
  297. * mm dropping could change our pgd.
  298. *
  299. * Out of an abundance of caution, this uses __get_user() to fault
  300. * in the target address just in case there's some obscure case
  301. * in which the target address isn't readable.
  302. */
  303. preempt_disable();
  304. probe_kernel_read(&dummy, v, 1);
  305. if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
  306. BUG();
  307. if (!PageHighMem(page)) {
  308. void *av = __va(PFN_PHYS(pfn));
  309. if (av != v)
  310. if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
  311. BUG();
  312. } else
  313. kmap_flush_unused();
  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. * XXX sleazy hack: If we're being called in a lazy-cpu zone
  424. * and lazy gs handling is enabled, it means we're in a
  425. * context switch, and %gs has just been saved. This means we
  426. * can zero it out to prevent faults on exit from the
  427. * hypervisor if the next process has no %gs. Either way, it
  428. * has been saved, and the new value will get loaded properly.
  429. * This will go away as soon as Xen has been modified to not
  430. * save/restore %gs for normal hypercalls.
  431. *
  432. * On x86_64, this hack is not used for %gs, because gs points
  433. * to KERNEL_GS_BASE (and uses it for PDA references), so we
  434. * must not zero %gs on x86_64
  435. *
  436. * For x86_64, we need to zero %fs, otherwise we may get an
  437. * exception between the new %fs descriptor being loaded and
  438. * %fs being effectively cleared at __switch_to().
  439. */
  440. if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
  441. #ifdef CONFIG_X86_32
  442. lazy_load_gs(0);
  443. #else
  444. loadsegment(fs, 0);
  445. #endif
  446. }
  447. xen_mc_batch();
  448. load_TLS_descriptor(t, cpu, 0);
  449. load_TLS_descriptor(t, cpu, 1);
  450. load_TLS_descriptor(t, cpu, 2);
  451. xen_mc_issue(PARAVIRT_LAZY_CPU);
  452. }
  453. #ifdef CONFIG_X86_64
  454. static void xen_load_gs_index(unsigned int idx)
  455. {
  456. if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
  457. BUG();
  458. }
  459. #endif
  460. static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
  461. const void *ptr)
  462. {
  463. xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
  464. u64 entry = *(u64 *)ptr;
  465. trace_xen_cpu_write_ldt_entry(dt, entrynum, entry);
  466. preempt_disable();
  467. xen_mc_flush();
  468. if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
  469. BUG();
  470. preempt_enable();
  471. }
  472. #ifdef CONFIG_X86_64
  473. struct trap_array_entry {
  474. void (*orig)(void);
  475. void (*xen)(void);
  476. bool ist_okay;
  477. };
  478. static struct trap_array_entry trap_array[] = {
  479. { debug, xen_xendebug, true },
  480. { int3, xen_xenint3, true },
  481. { double_fault, xen_double_fault, true },
  482. #ifdef CONFIG_X86_MCE
  483. { machine_check, xen_machine_check, true },
  484. #endif
  485. { nmi, xen_xennmi, true },
  486. { overflow, xen_overflow, false },
  487. #ifdef CONFIG_IA32_EMULATION
  488. { entry_INT80_compat, xen_entry_INT80_compat, false },
  489. #endif
  490. { page_fault, xen_page_fault, false },
  491. { divide_error, xen_divide_error, false },
  492. { bounds, xen_bounds, false },
  493. { invalid_op, xen_invalid_op, false },
  494. { device_not_available, xen_device_not_available, false },
  495. { coprocessor_segment_overrun, xen_coprocessor_segment_overrun, false },
  496. { invalid_TSS, xen_invalid_TSS, false },
  497. { segment_not_present, xen_segment_not_present, false },
  498. { stack_segment, xen_stack_segment, false },
  499. { general_protection, xen_general_protection, false },
  500. { spurious_interrupt_bug, xen_spurious_interrupt_bug, false },
  501. { coprocessor_error, xen_coprocessor_error, false },
  502. { alignment_check, xen_alignment_check, false },
  503. { simd_coprocessor_error, xen_simd_coprocessor_error, false },
  504. };
  505. static bool __ref get_trap_addr(void **addr, unsigned int ist)
  506. {
  507. unsigned int nr;
  508. bool ist_okay = false;
  509. /*
  510. * Replace trap handler addresses by Xen specific ones.
  511. * Check for known traps using IST and whitelist them.
  512. * The debugger ones are the only ones we care about.
  513. * Xen will handle faults like double_fault, * so we should never see
  514. * them. Warn if there's an unexpected IST-using fault handler.
  515. */
  516. for (nr = 0; nr < ARRAY_SIZE(trap_array); nr++) {
  517. struct trap_array_entry *entry = trap_array + nr;
  518. if (*addr == entry->orig) {
  519. *addr = entry->xen;
  520. ist_okay = entry->ist_okay;
  521. break;
  522. }
  523. }
  524. if (nr == ARRAY_SIZE(trap_array) &&
  525. *addr >= (void *)early_idt_handler_array[0] &&
  526. *addr < (void *)early_idt_handler_array[NUM_EXCEPTION_VECTORS]) {
  527. nr = (*addr - (void *)early_idt_handler_array[0]) /
  528. EARLY_IDT_HANDLER_SIZE;
  529. *addr = (void *)xen_early_idt_handler_array[nr];
  530. }
  531. if (WARN_ON(ist != 0 && !ist_okay))
  532. return false;
  533. return true;
  534. }
  535. #endif
  536. static int cvt_gate_to_trap(int vector, const gate_desc *val,
  537. struct trap_info *info)
  538. {
  539. unsigned long addr;
  540. if (val->bits.type != GATE_TRAP && val->bits.type != GATE_INTERRUPT)
  541. return 0;
  542. info->vector = vector;
  543. addr = gate_offset(val);
  544. #ifdef CONFIG_X86_64
  545. if (!get_trap_addr((void **)&addr, val->bits.ist))
  546. return 0;
  547. #endif /* CONFIG_X86_64 */
  548. info->address = addr;
  549. info->cs = gate_segment(val);
  550. info->flags = val->bits.dpl;
  551. /* interrupt gates clear IF */
  552. if (val->bits.type == GATE_INTERRUPT)
  553. info->flags |= 1 << 2;
  554. return 1;
  555. }
  556. /* Locations of each CPU's IDT */
  557. static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
  558. /* Set an IDT entry. If the entry is part of the current IDT, then
  559. also update Xen. */
  560. static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
  561. {
  562. unsigned long p = (unsigned long)&dt[entrynum];
  563. unsigned long start, end;
  564. trace_xen_cpu_write_idt_entry(dt, entrynum, g);
  565. preempt_disable();
  566. start = __this_cpu_read(idt_desc.address);
  567. end = start + __this_cpu_read(idt_desc.size) + 1;
  568. xen_mc_flush();
  569. native_write_idt_entry(dt, entrynum, g);
  570. if (p >= start && (p + 8) <= end) {
  571. struct trap_info info[2];
  572. info[1].address = 0;
  573. if (cvt_gate_to_trap(entrynum, g, &info[0]))
  574. if (HYPERVISOR_set_trap_table(info))
  575. BUG();
  576. }
  577. preempt_enable();
  578. }
  579. static void xen_convert_trap_info(const struct desc_ptr *desc,
  580. struct trap_info *traps)
  581. {
  582. unsigned in, out, count;
  583. count = (desc->size+1) / sizeof(gate_desc);
  584. BUG_ON(count > 256);
  585. for (in = out = 0; in < count; in++) {
  586. gate_desc *entry = (gate_desc *)(desc->address) + in;
  587. if (cvt_gate_to_trap(in, entry, &traps[out]))
  588. out++;
  589. }
  590. traps[out].address = 0;
  591. }
  592. void xen_copy_trap_info(struct trap_info *traps)
  593. {
  594. const struct desc_ptr *desc = this_cpu_ptr(&idt_desc);
  595. xen_convert_trap_info(desc, traps);
  596. }
  597. /* Load a new IDT into Xen. In principle this can be per-CPU, so we
  598. hold a spinlock to protect the static traps[] array (static because
  599. it avoids allocation, and saves stack space). */
  600. static void xen_load_idt(const struct desc_ptr *desc)
  601. {
  602. static DEFINE_SPINLOCK(lock);
  603. static struct trap_info traps[257];
  604. trace_xen_cpu_load_idt(desc);
  605. spin_lock(&lock);
  606. memcpy(this_cpu_ptr(&idt_desc), desc, sizeof(idt_desc));
  607. xen_convert_trap_info(desc, traps);
  608. xen_mc_flush();
  609. if (HYPERVISOR_set_trap_table(traps))
  610. BUG();
  611. spin_unlock(&lock);
  612. }
  613. /* Write a GDT descriptor entry. Ignore LDT descriptors, since
  614. they're handled differently. */
  615. static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
  616. const void *desc, int type)
  617. {
  618. trace_xen_cpu_write_gdt_entry(dt, entry, desc, type);
  619. preempt_disable();
  620. switch (type) {
  621. case DESC_LDT:
  622. case DESC_TSS:
  623. /* ignore */
  624. break;
  625. default: {
  626. xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
  627. xen_mc_flush();
  628. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  629. BUG();
  630. }
  631. }
  632. preempt_enable();
  633. }
  634. /*
  635. * Version of write_gdt_entry for use at early boot-time needed to
  636. * update an entry as simply as possible.
  637. */
  638. static void __init xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
  639. const void *desc, int type)
  640. {
  641. trace_xen_cpu_write_gdt_entry(dt, entry, desc, type);
  642. switch (type) {
  643. case DESC_LDT:
  644. case DESC_TSS:
  645. /* ignore */
  646. break;
  647. default: {
  648. xmaddr_t maddr = virt_to_machine(&dt[entry]);
  649. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  650. dt[entry] = *(struct desc_struct *)desc;
  651. }
  652. }
  653. }
  654. static void xen_load_sp0(unsigned long sp0)
  655. {
  656. struct multicall_space mcs;
  657. mcs = xen_mc_entry(0);
  658. MULTI_stack_switch(mcs.mc, __KERNEL_DS, sp0);
  659. xen_mc_issue(PARAVIRT_LAZY_CPU);
  660. this_cpu_write(cpu_tss_rw.x86_tss.sp0, sp0);
  661. }
  662. void xen_set_iopl_mask(unsigned mask)
  663. {
  664. struct physdev_set_iopl set_iopl;
  665. /* Force the change at ring 0. */
  666. set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
  667. HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  668. }
  669. static void xen_io_delay(void)
  670. {
  671. }
  672. static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
  673. static unsigned long xen_read_cr0(void)
  674. {
  675. unsigned long cr0 = this_cpu_read(xen_cr0_value);
  676. if (unlikely(cr0 == 0)) {
  677. cr0 = native_read_cr0();
  678. this_cpu_write(xen_cr0_value, cr0);
  679. }
  680. return cr0;
  681. }
  682. static void xen_write_cr0(unsigned long cr0)
  683. {
  684. struct multicall_space mcs;
  685. this_cpu_write(xen_cr0_value, cr0);
  686. /* Only pay attention to cr0.TS; everything else is
  687. ignored. */
  688. mcs = xen_mc_entry(0);
  689. MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
  690. xen_mc_issue(PARAVIRT_LAZY_CPU);
  691. }
  692. static void xen_write_cr4(unsigned long cr4)
  693. {
  694. cr4 &= ~(X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PCE);
  695. native_write_cr4(cr4);
  696. }
  697. #ifdef CONFIG_X86_64
  698. static inline unsigned long xen_read_cr8(void)
  699. {
  700. return 0;
  701. }
  702. static inline void xen_write_cr8(unsigned long val)
  703. {
  704. BUG_ON(val);
  705. }
  706. #endif
  707. static u64 xen_read_msr_safe(unsigned int msr, int *err)
  708. {
  709. u64 val;
  710. if (pmu_msr_read(msr, &val, err))
  711. return val;
  712. val = native_read_msr_safe(msr, err);
  713. switch (msr) {
  714. case MSR_IA32_APICBASE:
  715. #ifdef CONFIG_X86_X2APIC
  716. if (!(cpuid_ecx(1) & (1 << (X86_FEATURE_X2APIC & 31))))
  717. #endif
  718. val &= ~X2APIC_ENABLE;
  719. break;
  720. }
  721. return val;
  722. }
  723. static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
  724. {
  725. int ret;
  726. ret = 0;
  727. switch (msr) {
  728. #ifdef CONFIG_X86_64
  729. unsigned which;
  730. u64 base;
  731. case MSR_FS_BASE: which = SEGBASE_FS; goto set;
  732. case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
  733. case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
  734. set:
  735. base = ((u64)high << 32) | low;
  736. if (HYPERVISOR_set_segment_base(which, base) != 0)
  737. ret = -EIO;
  738. break;
  739. #endif
  740. case MSR_STAR:
  741. case MSR_CSTAR:
  742. case MSR_LSTAR:
  743. case MSR_SYSCALL_MASK:
  744. case MSR_IA32_SYSENTER_CS:
  745. case MSR_IA32_SYSENTER_ESP:
  746. case MSR_IA32_SYSENTER_EIP:
  747. /* Fast syscall setup is all done in hypercalls, so
  748. these are all ignored. Stub them out here to stop
  749. Xen console noise. */
  750. break;
  751. default:
  752. if (!pmu_msr_write(msr, low, high, &ret))
  753. ret = native_write_msr_safe(msr, low, high);
  754. }
  755. return ret;
  756. }
  757. static u64 xen_read_msr(unsigned int msr)
  758. {
  759. /*
  760. * This will silently swallow a #GP from RDMSR. It may be worth
  761. * changing that.
  762. */
  763. int err;
  764. return xen_read_msr_safe(msr, &err);
  765. }
  766. static void xen_write_msr(unsigned int msr, unsigned low, unsigned high)
  767. {
  768. /*
  769. * This will silently swallow a #GP from WRMSR. It may be worth
  770. * changing that.
  771. */
  772. xen_write_msr_safe(msr, low, high);
  773. }
  774. void xen_setup_shared_info(void)
  775. {
  776. set_fixmap(FIX_PARAVIRT_BOOTMAP, xen_start_info->shared_info);
  777. HYPERVISOR_shared_info =
  778. (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
  779. xen_setup_mfn_list_list();
  780. if (system_state == SYSTEM_BOOTING) {
  781. #ifndef CONFIG_SMP
  782. /*
  783. * In UP this is as good a place as any to set up shared info.
  784. * Limit this to boot only, at restore vcpu setup is done via
  785. * xen_vcpu_restore().
  786. */
  787. xen_setup_vcpu_info_placement();
  788. #endif
  789. /*
  790. * Now that shared info is set up we can start using routines
  791. * that point to pvclock area.
  792. */
  793. xen_init_time_ops();
  794. }
  795. }
  796. /* This is called once we have the cpu_possible_mask */
  797. void __ref xen_setup_vcpu_info_placement(void)
  798. {
  799. int cpu;
  800. for_each_possible_cpu(cpu) {
  801. /* Set up direct vCPU id mapping for PV guests. */
  802. per_cpu(xen_vcpu_id, cpu) = cpu;
  803. /*
  804. * xen_vcpu_setup(cpu) can fail -- in which case it
  805. * falls back to the shared_info version for cpus
  806. * where xen_vcpu_nr(cpu) < MAX_VIRT_CPUS.
  807. *
  808. * xen_cpu_up_prepare_pv() handles the rest by failing
  809. * them in hotplug.
  810. */
  811. (void) xen_vcpu_setup(cpu);
  812. }
  813. /*
  814. * xen_vcpu_setup managed to place the vcpu_info within the
  815. * percpu area for all cpus, so make use of it.
  816. */
  817. if (xen_have_vcpu_info_placement) {
  818. pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
  819. pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
  820. pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
  821. pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
  822. pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
  823. }
  824. }
  825. static const struct pv_info xen_info __initconst = {
  826. .shared_kernel_pmd = 0,
  827. #ifdef CONFIG_X86_64
  828. .extra_user_64bit_cs = FLAT_USER_CS64,
  829. #endif
  830. .name = "Xen",
  831. };
  832. static const struct pv_cpu_ops xen_cpu_ops __initconst = {
  833. .cpuid = xen_cpuid,
  834. .set_debugreg = xen_set_debugreg,
  835. .get_debugreg = xen_get_debugreg,
  836. .read_cr0 = xen_read_cr0,
  837. .write_cr0 = xen_write_cr0,
  838. .write_cr4 = xen_write_cr4,
  839. #ifdef CONFIG_X86_64
  840. .read_cr8 = xen_read_cr8,
  841. .write_cr8 = xen_write_cr8,
  842. #endif
  843. .wbinvd = native_wbinvd,
  844. .read_msr = xen_read_msr,
  845. .write_msr = xen_write_msr,
  846. .read_msr_safe = xen_read_msr_safe,
  847. .write_msr_safe = xen_write_msr_safe,
  848. .read_pmc = xen_read_pmc,
  849. .iret = xen_iret,
  850. #ifdef CONFIG_X86_64
  851. .usergs_sysret64 = xen_sysret64,
  852. #endif
  853. .load_tr_desc = paravirt_nop,
  854. .set_ldt = xen_set_ldt,
  855. .load_gdt = xen_load_gdt,
  856. .load_idt = xen_load_idt,
  857. .load_tls = xen_load_tls,
  858. #ifdef CONFIG_X86_64
  859. .load_gs_index = xen_load_gs_index,
  860. #endif
  861. .alloc_ldt = xen_alloc_ldt,
  862. .free_ldt = xen_free_ldt,
  863. .store_tr = xen_store_tr,
  864. .write_ldt_entry = xen_write_ldt_entry,
  865. .write_gdt_entry = xen_write_gdt_entry,
  866. .write_idt_entry = xen_write_idt_entry,
  867. .load_sp0 = xen_load_sp0,
  868. .set_iopl_mask = xen_set_iopl_mask,
  869. .io_delay = xen_io_delay,
  870. /* Xen takes care of %gs when switching to usermode for us */
  871. .swapgs = paravirt_nop,
  872. .start_context_switch = paravirt_start_context_switch,
  873. .end_context_switch = xen_end_context_switch,
  874. };
  875. static void xen_restart(char *msg)
  876. {
  877. xen_reboot(SHUTDOWN_reboot);
  878. }
  879. static void xen_machine_halt(void)
  880. {
  881. xen_reboot(SHUTDOWN_poweroff);
  882. }
  883. static void xen_machine_power_off(void)
  884. {
  885. if (pm_power_off)
  886. pm_power_off();
  887. xen_reboot(SHUTDOWN_poweroff);
  888. }
  889. static void xen_crash_shutdown(struct pt_regs *regs)
  890. {
  891. xen_reboot(SHUTDOWN_crash);
  892. }
  893. static const struct machine_ops xen_machine_ops __initconst = {
  894. .restart = xen_restart,
  895. .halt = xen_machine_halt,
  896. .power_off = xen_machine_power_off,
  897. .shutdown = xen_machine_halt,
  898. .crash_shutdown = xen_crash_shutdown,
  899. .emergency_restart = xen_emergency_restart,
  900. };
  901. static unsigned char xen_get_nmi_reason(void)
  902. {
  903. unsigned char reason = 0;
  904. /* Construct a value which looks like it came from port 0x61. */
  905. if (test_bit(_XEN_NMIREASON_io_error,
  906. &HYPERVISOR_shared_info->arch.nmi_reason))
  907. reason |= NMI_REASON_IOCHK;
  908. if (test_bit(_XEN_NMIREASON_pci_serr,
  909. &HYPERVISOR_shared_info->arch.nmi_reason))
  910. reason |= NMI_REASON_SERR;
  911. return reason;
  912. }
  913. static void __init xen_boot_params_init_edd(void)
  914. {
  915. #if IS_ENABLED(CONFIG_EDD)
  916. struct xen_platform_op op;
  917. struct edd_info *edd_info;
  918. u32 *mbr_signature;
  919. unsigned nr;
  920. int ret;
  921. edd_info = boot_params.eddbuf;
  922. mbr_signature = boot_params.edd_mbr_sig_buffer;
  923. op.cmd = XENPF_firmware_info;
  924. op.u.firmware_info.type = XEN_FW_DISK_INFO;
  925. for (nr = 0; nr < EDDMAXNR; nr++) {
  926. struct edd_info *info = edd_info + nr;
  927. op.u.firmware_info.index = nr;
  928. info->params.length = sizeof(info->params);
  929. set_xen_guest_handle(op.u.firmware_info.u.disk_info.edd_params,
  930. &info->params);
  931. ret = HYPERVISOR_platform_op(&op);
  932. if (ret)
  933. break;
  934. #define C(x) info->x = op.u.firmware_info.u.disk_info.x
  935. C(device);
  936. C(version);
  937. C(interface_support);
  938. C(legacy_max_cylinder);
  939. C(legacy_max_head);
  940. C(legacy_sectors_per_track);
  941. #undef C
  942. }
  943. boot_params.eddbuf_entries = nr;
  944. op.u.firmware_info.type = XEN_FW_DISK_MBR_SIGNATURE;
  945. for (nr = 0; nr < EDD_MBR_SIG_MAX; nr++) {
  946. op.u.firmware_info.index = nr;
  947. ret = HYPERVISOR_platform_op(&op);
  948. if (ret)
  949. break;
  950. mbr_signature[nr] = op.u.firmware_info.u.disk_mbr_signature.mbr_signature;
  951. }
  952. boot_params.edd_mbr_sig_buf_entries = nr;
  953. #endif
  954. }
  955. /*
  956. * Set up the GDT and segment registers for -fstack-protector. Until
  957. * we do this, we have to be careful not to call any stack-protected
  958. * function, which is most of the kernel.
  959. */
  960. static void xen_setup_gdt(int cpu)
  961. {
  962. pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
  963. pv_cpu_ops.load_gdt = xen_load_gdt_boot;
  964. setup_stack_canary_segment(0);
  965. switch_to_new_gdt(0);
  966. pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
  967. pv_cpu_ops.load_gdt = xen_load_gdt;
  968. }
  969. static void __init xen_dom0_set_legacy_features(void)
  970. {
  971. x86_platform.legacy.rtc = 1;
  972. }
  973. /* First C function to be called on Xen boot */
  974. asmlinkage __visible void __init xen_start_kernel(void)
  975. {
  976. struct physdev_set_iopl set_iopl;
  977. unsigned long initrd_start = 0;
  978. int rc;
  979. if (!xen_start_info)
  980. return;
  981. xen_domain_type = XEN_PV_DOMAIN;
  982. xen_start_flags = xen_start_info->flags;
  983. xen_setup_features();
  984. xen_setup_machphys_mapping();
  985. /* Install Xen paravirt ops */
  986. pv_info = xen_info;
  987. pv_init_ops.patch = paravirt_patch_default;
  988. pv_cpu_ops = xen_cpu_ops;
  989. x86_platform.get_nmi_reason = xen_get_nmi_reason;
  990. x86_init.resources.memory_setup = xen_memory_setup;
  991. x86_init.irqs.intr_mode_init = x86_init_noop;
  992. x86_init.oem.arch_setup = xen_arch_setup;
  993. x86_init.oem.banner = xen_banner;
  994. /*
  995. * Set up some pagetable state before starting to set any ptes.
  996. */
  997. xen_init_mmu_ops();
  998. /* Prevent unwanted bits from being set in PTEs. */
  999. __supported_pte_mask &= ~_PAGE_GLOBAL;
  1000. /*
  1001. * Prevent page tables from being allocated in highmem, even
  1002. * if CONFIG_HIGHPTE is enabled.
  1003. */
  1004. __userpte_alloc_gfp &= ~__GFP_HIGHMEM;
  1005. /* Get mfn list */
  1006. xen_build_dynamic_phys_to_machine();
  1007. /*
  1008. * Set up kernel GDT and segment registers, mainly so that
  1009. * -fstack-protector code can be executed.
  1010. */
  1011. xen_setup_gdt(0);
  1012. /* Work out if we support NX */
  1013. get_cpu_cap(&boot_cpu_data);
  1014. x86_configure_nx();
  1015. xen_init_irq_ops();
  1016. /* Let's presume PV guests always boot on vCPU with id 0. */
  1017. per_cpu(xen_vcpu_id, 0) = 0;
  1018. /*
  1019. * Setup xen_vcpu early because idt_setup_early_handler needs it for
  1020. * local_irq_disable(), irqs_disabled().
  1021. *
  1022. * Don't do the full vcpu_info placement stuff until we have
  1023. * the cpu_possible_mask and a non-dummy shared_info.
  1024. */
  1025. xen_vcpu_info_reset(0);
  1026. idt_setup_early_handler();
  1027. xen_init_capabilities();
  1028. #ifdef CONFIG_X86_LOCAL_APIC
  1029. /*
  1030. * set up the basic apic ops.
  1031. */
  1032. xen_init_apic();
  1033. #endif
  1034. if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
  1035. pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
  1036. pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
  1037. }
  1038. machine_ops = xen_machine_ops;
  1039. /*
  1040. * The only reliable way to retain the initial address of the
  1041. * percpu gdt_page is to remember it here, so we can go and
  1042. * mark it RW later, when the initial percpu area is freed.
  1043. */
  1044. xen_initial_gdt = &per_cpu(gdt_page, 0);
  1045. xen_smp_init();
  1046. #ifdef CONFIG_ACPI_NUMA
  1047. /*
  1048. * The pages we from Xen are not related to machine pages, so
  1049. * any NUMA information the kernel tries to get from ACPI will
  1050. * be meaningless. Prevent it from trying.
  1051. */
  1052. acpi_numa = -1;
  1053. #endif
  1054. WARN_ON(xen_cpuhp_setup(xen_cpu_up_prepare_pv, xen_cpu_dead_pv));
  1055. local_irq_disable();
  1056. early_boot_irqs_disabled = true;
  1057. xen_raw_console_write("mapping kernel into physical memory\n");
  1058. xen_setup_kernel_pagetable((pgd_t *)xen_start_info->pt_base,
  1059. xen_start_info->nr_pages);
  1060. xen_reserve_special_pages();
  1061. /* keep using Xen gdt for now; no urgent need to change it */
  1062. #ifdef CONFIG_X86_32
  1063. pv_info.kernel_rpl = 1;
  1064. if (xen_feature(XENFEAT_supervisor_mode_kernel))
  1065. pv_info.kernel_rpl = 0;
  1066. #else
  1067. pv_info.kernel_rpl = 0;
  1068. #endif
  1069. /* set the limit of our address space */
  1070. xen_reserve_top();
  1071. /*
  1072. * We used to do this in xen_arch_setup, but that is too late
  1073. * on AMD were early_cpu_init (run before ->arch_setup()) calls
  1074. * early_amd_init which pokes 0xcf8 port.
  1075. */
  1076. set_iopl.iopl = 1;
  1077. rc = HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  1078. if (rc != 0)
  1079. xen_raw_printk("physdev_op failed %d\n", rc);
  1080. #ifdef CONFIG_X86_32
  1081. /* set up basic CPUID stuff */
  1082. cpu_detect(&new_cpu_data);
  1083. set_cpu_cap(&new_cpu_data, X86_FEATURE_FPU);
  1084. new_cpu_data.x86_capability[CPUID_1_EDX] = cpuid_edx(1);
  1085. #endif
  1086. if (xen_start_info->mod_start) {
  1087. if (xen_start_info->flags & SIF_MOD_START_PFN)
  1088. initrd_start = PFN_PHYS(xen_start_info->mod_start);
  1089. else
  1090. initrd_start = __pa(xen_start_info->mod_start);
  1091. }
  1092. /* Poke various useful things into boot_params */
  1093. boot_params.hdr.type_of_loader = (9 << 4) | 0;
  1094. boot_params.hdr.ramdisk_image = initrd_start;
  1095. boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
  1096. boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
  1097. boot_params.hdr.hardware_subarch = X86_SUBARCH_XEN;
  1098. if (!xen_initial_domain()) {
  1099. add_preferred_console("xenboot", 0, NULL);
  1100. if (pci_xen)
  1101. x86_init.pci.arch_init = pci_xen_init;
  1102. } else {
  1103. const struct dom0_vga_console_info *info =
  1104. (void *)((char *)xen_start_info +
  1105. xen_start_info->console.dom0.info_off);
  1106. struct xen_platform_op op = {
  1107. .cmd = XENPF_firmware_info,
  1108. .interface_version = XENPF_INTERFACE_VERSION,
  1109. .u.firmware_info.type = XEN_FW_KBD_SHIFT_FLAGS,
  1110. };
  1111. x86_platform.set_legacy_features =
  1112. xen_dom0_set_legacy_features;
  1113. xen_init_vga(info, xen_start_info->console.dom0.info_size);
  1114. xen_start_info->console.domU.mfn = 0;
  1115. xen_start_info->console.domU.evtchn = 0;
  1116. if (HYPERVISOR_platform_op(&op) == 0)
  1117. boot_params.kbd_status = op.u.firmware_info.u.kbd_shift_flags;
  1118. /* Make sure ACS will be enabled */
  1119. pci_request_acs();
  1120. xen_acpi_sleep_register();
  1121. /* Avoid searching for BIOS MP tables */
  1122. x86_init.mpparse.find_smp_config = x86_init_noop;
  1123. x86_init.mpparse.get_smp_config = x86_init_uint_noop;
  1124. xen_boot_params_init_edd();
  1125. }
  1126. add_preferred_console("tty", 0, NULL);
  1127. add_preferred_console("hvc", 0, NULL);
  1128. #ifdef CONFIG_PCI
  1129. /* PCI BIOS service won't work from a PV guest. */
  1130. pci_probe &= ~PCI_PROBE_BIOS;
  1131. #endif
  1132. xen_raw_console_write("about to get started...\n");
  1133. /* We need this for printk timestamps */
  1134. xen_setup_runstate_info(0);
  1135. xen_efi_init();
  1136. /* Start the world */
  1137. #ifdef CONFIG_X86_32
  1138. i386_start_kernel();
  1139. #else
  1140. cr4_init_shadow(); /* 32b kernel does this in i386_start_kernel() */
  1141. x86_64_start_reservations((char *)__pa_symbol(&boot_params));
  1142. #endif
  1143. }
  1144. static int xen_cpu_up_prepare_pv(unsigned int cpu)
  1145. {
  1146. int rc;
  1147. if (per_cpu(xen_vcpu, cpu) == NULL)
  1148. return -ENODEV;
  1149. xen_setup_timer(cpu);
  1150. rc = xen_smp_intr_init(cpu);
  1151. if (rc) {
  1152. WARN(1, "xen_smp_intr_init() for CPU %d failed: %d\n",
  1153. cpu, rc);
  1154. return rc;
  1155. }
  1156. rc = xen_smp_intr_init_pv(cpu);
  1157. if (rc) {
  1158. WARN(1, "xen_smp_intr_init_pv() for CPU %d failed: %d\n",
  1159. cpu, rc);
  1160. return rc;
  1161. }
  1162. return 0;
  1163. }
  1164. static int xen_cpu_dead_pv(unsigned int cpu)
  1165. {
  1166. xen_smp_intr_free(cpu);
  1167. xen_smp_intr_free_pv(cpu);
  1168. xen_teardown_timer(cpu);
  1169. return 0;
  1170. }
  1171. static uint32_t __init xen_platform_pv(void)
  1172. {
  1173. if (xen_pv_domain())
  1174. return xen_cpuid_base();
  1175. return 0;
  1176. }
  1177. const __initconst struct hypervisor_x86 x86_hyper_xen_pv = {
  1178. .name = "Xen PV",
  1179. .detect = xen_platform_pv,
  1180. .type = X86_HYPER_XEN_PV,
  1181. .runtime.pin_vcpu = xen_pin_vcpu,
  1182. };