enlighten_pv.c 36 KB

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