enlighten_pv.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  1. /*
  2. * Core of Xen paravirt_ops implementation.
  3. *
  4. * This file contains the xen_paravirt_ops structure itself, and the
  5. * implementations for:
  6. * - privileged instructions
  7. * - interrupt flags
  8. * - segment operations
  9. * - booting and setup
  10. *
  11. * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
  12. */
  13. #include <linux/cpu.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/smp.h>
  17. #include <linux/preempt.h>
  18. #include <linux/hardirq.h>
  19. #include <linux/percpu.h>
  20. #include <linux/delay.h>
  21. #include <linux/start_kernel.h>
  22. #include <linux/sched.h>
  23. #include <linux/kprobes.h>
  24. #include <linux/bootmem.h>
  25. #include <linux/export.h>
  26. #include <linux/mm.h>
  27. #include <linux/page-flags.h>
  28. #include <linux/highmem.h>
  29. #include <linux/console.h>
  30. #include <linux/pci.h>
  31. #include <linux/gfp.h>
  32. #include <linux/memblock.h>
  33. #include <linux/edd.h>
  34. #include <linux/frame.h>
  35. #include <xen/xen.h>
  36. #include <xen/events.h>
  37. #include <xen/interface/xen.h>
  38. #include <xen/interface/version.h>
  39. #include <xen/interface/physdev.h>
  40. #include <xen/interface/vcpu.h>
  41. #include <xen/interface/memory.h>
  42. #include <xen/interface/nmi.h>
  43. #include <xen/interface/xen-mca.h>
  44. #include <xen/features.h>
  45. #include <xen/page.h>
  46. #include <xen/hvc-console.h>
  47. #include <xen/acpi.h>
  48. #include <asm/paravirt.h>
  49. #include <asm/apic.h>
  50. #include <asm/page.h>
  51. #include <asm/xen/pci.h>
  52. #include <asm/xen/hypercall.h>
  53. #include <asm/xen/hypervisor.h>
  54. #include <asm/xen/cpuid.h>
  55. #include <asm/fixmap.h>
  56. #include <asm/processor.h>
  57. #include <asm/proto.h>
  58. #include <asm/msr-index.h>
  59. #include <asm/traps.h>
  60. #include <asm/setup.h>
  61. #include <asm/desc.h>
  62. #include <asm/pgalloc.h>
  63. #include <asm/pgtable.h>
  64. #include <asm/tlbflush.h>
  65. #include <asm/reboot.h>
  66. #include <asm/stackprotector.h>
  67. #include <asm/hypervisor.h>
  68. #include <asm/mach_traps.h>
  69. #include <asm/mwait.h>
  70. #include <asm/pci_x86.h>
  71. #include <asm/cpu.h>
  72. #ifdef CONFIG_ACPI
  73. #include <linux/acpi.h>
  74. #include <asm/acpi.h>
  75. #include <acpi/pdc_intel.h>
  76. #include <acpi/processor.h>
  77. #include <xen/interface/platform.h>
  78. #endif
  79. #include "xen-ops.h"
  80. #include "mmu.h"
  81. #include "smp.h"
  82. #include "multicalls.h"
  83. #include "pmu.h"
  84. #include "../kernel/cpu/cpu.h" /* get_cpu_cap() */
  85. void *xen_initial_gdt;
  86. static int xen_cpu_up_prepare_pv(unsigned int cpu);
  87. static int xen_cpu_dead_pv(unsigned int cpu);
  88. struct tls_descs {
  89. struct desc_struct desc[3];
  90. };
  91. /*
  92. * Updating the 3 TLS descriptors in the GDT on every task switch is
  93. * surprisingly expensive so we avoid updating them if they haven't
  94. * changed. Since Xen writes different descriptors than the one
  95. * passed in the update_descriptor hypercall we keep shadow copies to
  96. * compare against.
  97. */
  98. static DEFINE_PER_CPU(struct tls_descs, shadow_tls_desc);
  99. static void __init xen_banner(void)
  100. {
  101. unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
  102. struct xen_extraversion extra;
  103. HYPERVISOR_xen_version(XENVER_extraversion, &extra);
  104. pr_info("Booting paravirtualized kernel on %s\n", pv_info.name);
  105. printk(KERN_INFO "Xen version: %d.%d%s%s\n",
  106. version >> 16, version & 0xffff, extra.extraversion,
  107. xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
  108. }
  109. /* Check if running on Xen version (major, minor) or later */
  110. bool
  111. xen_running_on_version_or_later(unsigned int major, unsigned int minor)
  112. {
  113. unsigned int version;
  114. if (!xen_domain())
  115. return false;
  116. version = HYPERVISOR_xen_version(XENVER_version, NULL);
  117. if ((((version >> 16) == major) && ((version & 0xffff) >= minor)) ||
  118. ((version >> 16) > major))
  119. return true;
  120. return false;
  121. }
  122. static __read_mostly unsigned int cpuid_leaf5_ecx_val;
  123. static __read_mostly unsigned int cpuid_leaf5_edx_val;
  124. static void xen_cpuid(unsigned int *ax, unsigned int *bx,
  125. unsigned int *cx, unsigned int *dx)
  126. {
  127. unsigned maskebx = ~0;
  128. /*
  129. * Mask out inconvenient features, to try and disable as many
  130. * unsupported kernel subsystems as possible.
  131. */
  132. switch (*ax) {
  133. case CPUID_MWAIT_LEAF:
  134. /* Synthesize the values.. */
  135. *ax = 0;
  136. *bx = 0;
  137. *cx = cpuid_leaf5_ecx_val;
  138. *dx = cpuid_leaf5_edx_val;
  139. return;
  140. case 0xb:
  141. /* Suppress extended topology stuff */
  142. maskebx = 0;
  143. break;
  144. }
  145. asm(XEN_EMULATE_PREFIX "cpuid"
  146. : "=a" (*ax),
  147. "=b" (*bx),
  148. "=c" (*cx),
  149. "=d" (*dx)
  150. : "0" (*ax), "2" (*cx));
  151. *bx &= maskebx;
  152. }
  153. STACK_FRAME_NON_STANDARD(xen_cpuid); /* XEN_EMULATE_PREFIX */
  154. static bool __init xen_check_mwait(void)
  155. {
  156. #ifdef CONFIG_ACPI
  157. struct xen_platform_op op = {
  158. .cmd = XENPF_set_processor_pminfo,
  159. .u.set_pminfo.id = -1,
  160. .u.set_pminfo.type = XEN_PM_PDC,
  161. };
  162. uint32_t buf[3];
  163. unsigned int ax, bx, cx, dx;
  164. unsigned int mwait_mask;
  165. /* We need to determine whether it is OK to expose the MWAIT
  166. * capability to the kernel to harvest deeper than C3 states from ACPI
  167. * _CST using the processor_harvest_xen.c module. For this to work, we
  168. * need to gather the MWAIT_LEAF values (which the cstate.c code
  169. * checks against). The hypervisor won't expose the MWAIT flag because
  170. * it would break backwards compatibility; so we will find out directly
  171. * from the hardware and hypercall.
  172. */
  173. if (!xen_initial_domain())
  174. return false;
  175. /*
  176. * When running under platform earlier than Xen4.2, do not expose
  177. * mwait, to avoid the risk of loading native acpi pad driver
  178. */
  179. if (!xen_running_on_version_or_later(4, 2))
  180. return false;
  181. ax = 1;
  182. cx = 0;
  183. native_cpuid(&ax, &bx, &cx, &dx);
  184. mwait_mask = (1 << (X86_FEATURE_EST % 32)) |
  185. (1 << (X86_FEATURE_MWAIT % 32));
  186. if ((cx & mwait_mask) != mwait_mask)
  187. return false;
  188. /* We need to emulate the MWAIT_LEAF and for that we need both
  189. * ecx and edx. The hypercall provides only partial information.
  190. */
  191. ax = CPUID_MWAIT_LEAF;
  192. bx = 0;
  193. cx = 0;
  194. dx = 0;
  195. native_cpuid(&ax, &bx, &cx, &dx);
  196. /* Ask the Hypervisor whether to clear ACPI_PDC_C_C2C3_FFH. If so,
  197. * don't expose MWAIT_LEAF and let ACPI pick the IOPORT version of C3.
  198. */
  199. buf[0] = ACPI_PDC_REVISION_ID;
  200. buf[1] = 1;
  201. buf[2] = (ACPI_PDC_C_CAPABILITY_SMP | ACPI_PDC_EST_CAPABILITY_SWSMP);
  202. set_xen_guest_handle(op.u.set_pminfo.pdc, buf);
  203. if ((HYPERVISOR_platform_op(&op) == 0) &&
  204. (buf[2] & (ACPI_PDC_C_C1_FFH | ACPI_PDC_C_C2C3_FFH))) {
  205. cpuid_leaf5_ecx_val = cx;
  206. cpuid_leaf5_edx_val = dx;
  207. }
  208. return true;
  209. #else
  210. return false;
  211. #endif
  212. }
  213. static bool __init xen_check_xsave(void)
  214. {
  215. unsigned int cx, xsave_mask;
  216. cx = cpuid_ecx(1);
  217. xsave_mask = (1 << (X86_FEATURE_XSAVE % 32)) |
  218. (1 << (X86_FEATURE_OSXSAVE % 32));
  219. /* Xen will set CR4.OSXSAVE if supported and not disabled by force */
  220. return (cx & xsave_mask) == xsave_mask;
  221. }
  222. static void __init xen_init_capabilities(void)
  223. {
  224. setup_force_cpu_cap(X86_FEATURE_XENPV);
  225. setup_clear_cpu_cap(X86_FEATURE_DCA);
  226. setup_clear_cpu_cap(X86_FEATURE_APERFMPERF);
  227. setup_clear_cpu_cap(X86_FEATURE_MTRR);
  228. setup_clear_cpu_cap(X86_FEATURE_ACC);
  229. setup_clear_cpu_cap(X86_FEATURE_X2APIC);
  230. setup_clear_cpu_cap(X86_FEATURE_SME);
  231. /*
  232. * Xen PV would need some work to support PCID: CR3 handling as well
  233. * as xen_flush_tlb_others() would need updating.
  234. */
  235. setup_clear_cpu_cap(X86_FEATURE_PCID);
  236. if (!xen_initial_domain())
  237. setup_clear_cpu_cap(X86_FEATURE_ACPI);
  238. if (xen_check_mwait())
  239. setup_force_cpu_cap(X86_FEATURE_MWAIT);
  240. else
  241. setup_clear_cpu_cap(X86_FEATURE_MWAIT);
  242. if (!xen_check_xsave()) {
  243. setup_clear_cpu_cap(X86_FEATURE_XSAVE);
  244. setup_clear_cpu_cap(X86_FEATURE_OSXSAVE);
  245. }
  246. }
  247. static void xen_set_debugreg(int reg, unsigned long val)
  248. {
  249. HYPERVISOR_set_debugreg(reg, val);
  250. }
  251. static unsigned long xen_get_debugreg(int reg)
  252. {
  253. return HYPERVISOR_get_debugreg(reg);
  254. }
  255. static void xen_end_context_switch(struct task_struct *next)
  256. {
  257. xen_mc_flush();
  258. paravirt_end_context_switch(next);
  259. }
  260. static unsigned long xen_store_tr(void)
  261. {
  262. return 0;
  263. }
  264. /*
  265. * Set the page permissions for a particular virtual address. If the
  266. * address is a vmalloc mapping (or other non-linear mapping), then
  267. * find the linear mapping of the page and also set its protections to
  268. * match.
  269. */
  270. static void set_aliased_prot(void *v, pgprot_t prot)
  271. {
  272. int level;
  273. pte_t *ptep;
  274. pte_t pte;
  275. unsigned long pfn;
  276. struct page *page;
  277. unsigned char dummy;
  278. ptep = lookup_address((unsigned long)v, &level);
  279. BUG_ON(ptep == NULL);
  280. pfn = pte_pfn(*ptep);
  281. page = pfn_to_page(pfn);
  282. pte = pfn_pte(pfn, prot);
  283. /*
  284. * Careful: update_va_mapping() will fail if the virtual address
  285. * we're poking isn't populated in the page tables. We don't
  286. * need to worry about the direct map (that's always in the page
  287. * tables), but we need to be careful about vmap space. In
  288. * particular, the top level page table can lazily propagate
  289. * entries between processes, so if we've switched mms since we
  290. * vmapped the target in the first place, we might not have the
  291. * top-level page table entry populated.
  292. *
  293. * We disable preemption because we want the same mm active when
  294. * we probe the target and when we issue the hypercall. We'll
  295. * have the same nominal mm, but if we're a kernel thread, lazy
  296. * mm dropping could change our pgd.
  297. *
  298. * Out of an abundance of caution, this uses __get_user() to fault
  299. * in the target address just in case there's some obscure case
  300. * in which the target address isn't readable.
  301. */
  302. preempt_disable();
  303. probe_kernel_read(&dummy, v, 1);
  304. if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
  305. BUG();
  306. if (!PageHighMem(page)) {
  307. void *av = __va(PFN_PHYS(pfn));
  308. if (av != v)
  309. if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
  310. BUG();
  311. } else
  312. kmap_flush_unused();
  313. preempt_enable();
  314. }
  315. static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
  316. {
  317. const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
  318. int i;
  319. /*
  320. * We need to mark the all aliases of the LDT pages RO. We
  321. * don't need to call vm_flush_aliases(), though, since that's
  322. * only responsible for flushing aliases out the TLBs, not the
  323. * page tables, and Xen will flush the TLB for us if needed.
  324. *
  325. * To avoid confusing future readers: none of this is necessary
  326. * to load the LDT. The hypervisor only checks this when the
  327. * LDT is faulted in due to subsequent descriptor access.
  328. */
  329. for (i = 0; i < entries; i += entries_per_page)
  330. set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
  331. }
  332. static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
  333. {
  334. const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
  335. int i;
  336. for (i = 0; i < entries; i += entries_per_page)
  337. set_aliased_prot(ldt + i, PAGE_KERNEL);
  338. }
  339. static void xen_set_ldt(const void *addr, unsigned entries)
  340. {
  341. struct mmuext_op *op;
  342. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  343. trace_xen_cpu_set_ldt(addr, entries);
  344. op = mcs.args;
  345. op->cmd = MMUEXT_SET_LDT;
  346. op->arg1.linear_addr = (unsigned long)addr;
  347. op->arg2.nr_ents = entries;
  348. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  349. xen_mc_issue(PARAVIRT_LAZY_CPU);
  350. }
  351. static void xen_load_gdt(const struct desc_ptr *dtr)
  352. {
  353. unsigned long va = dtr->address;
  354. unsigned int size = dtr->size + 1;
  355. unsigned pages = DIV_ROUND_UP(size, PAGE_SIZE);
  356. unsigned long frames[pages];
  357. int f;
  358. /*
  359. * A GDT can be up to 64k in size, which corresponds to 8192
  360. * 8-byte entries, or 16 4k pages..
  361. */
  362. BUG_ON(size > 65536);
  363. BUG_ON(va & ~PAGE_MASK);
  364. for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
  365. int level;
  366. pte_t *ptep;
  367. unsigned long pfn, mfn;
  368. void *virt;
  369. /*
  370. * The GDT is per-cpu and is in the percpu data area.
  371. * That can be virtually mapped, so we need to do a
  372. * page-walk to get the underlying MFN for the
  373. * hypercall. The page can also be in the kernel's
  374. * linear range, so we need to RO that mapping too.
  375. */
  376. ptep = lookup_address(va, &level);
  377. BUG_ON(ptep == NULL);
  378. pfn = pte_pfn(*ptep);
  379. mfn = pfn_to_mfn(pfn);
  380. virt = __va(PFN_PHYS(pfn));
  381. frames[f] = mfn;
  382. make_lowmem_page_readonly((void *)va);
  383. make_lowmem_page_readonly(virt);
  384. }
  385. if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
  386. BUG();
  387. }
  388. /*
  389. * load_gdt for early boot, when the gdt is only mapped once
  390. */
  391. static void __init xen_load_gdt_boot(const struct desc_ptr *dtr)
  392. {
  393. unsigned long va = dtr->address;
  394. unsigned int size = dtr->size + 1;
  395. unsigned pages = DIV_ROUND_UP(size, PAGE_SIZE);
  396. unsigned long frames[pages];
  397. int f;
  398. /*
  399. * A GDT can be up to 64k in size, which corresponds to 8192
  400. * 8-byte entries, or 16 4k pages..
  401. */
  402. BUG_ON(size > 65536);
  403. BUG_ON(va & ~PAGE_MASK);
  404. for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
  405. pte_t pte;
  406. unsigned long pfn, mfn;
  407. pfn = virt_to_pfn(va);
  408. mfn = pfn_to_mfn(pfn);
  409. pte = pfn_pte(pfn, PAGE_KERNEL_RO);
  410. if (HYPERVISOR_update_va_mapping((unsigned long)va, pte, 0))
  411. BUG();
  412. frames[f] = mfn;
  413. }
  414. if (HYPERVISOR_set_gdt(frames, 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_nmi, 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 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 (WARN_ON(ist != 0 && !ist_okay))
  542. return false;
  543. return true;
  544. }
  545. #endif
  546. static int cvt_gate_to_trap(int vector, const gate_desc *val,
  547. struct trap_info *info)
  548. {
  549. unsigned long addr;
  550. if (val->bits.type != GATE_TRAP && val->bits.type != GATE_INTERRUPT)
  551. return 0;
  552. info->vector = vector;
  553. addr = gate_offset(val);
  554. #ifdef CONFIG_X86_64
  555. if (!get_trap_addr((void **)&addr, val->bits.ist))
  556. return 0;
  557. #endif /* CONFIG_X86_64 */
  558. info->address = addr;
  559. info->cs = gate_segment(val);
  560. info->flags = val->bits.dpl;
  561. /* interrupt gates clear IF */
  562. if (val->bits.type == GATE_INTERRUPT)
  563. info->flags |= 1 << 2;
  564. return 1;
  565. }
  566. /* Locations of each CPU's IDT */
  567. static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
  568. /* Set an IDT entry. If the entry is part of the current IDT, then
  569. also update Xen. */
  570. static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
  571. {
  572. unsigned long p = (unsigned long)&dt[entrynum];
  573. unsigned long start, end;
  574. trace_xen_cpu_write_idt_entry(dt, entrynum, g);
  575. preempt_disable();
  576. start = __this_cpu_read(idt_desc.address);
  577. end = start + __this_cpu_read(idt_desc.size) + 1;
  578. xen_mc_flush();
  579. native_write_idt_entry(dt, entrynum, g);
  580. if (p >= start && (p + 8) <= end) {
  581. struct trap_info info[2];
  582. info[1].address = 0;
  583. if (cvt_gate_to_trap(entrynum, g, &info[0]))
  584. if (HYPERVISOR_set_trap_table(info))
  585. BUG();
  586. }
  587. preempt_enable();
  588. }
  589. static void xen_convert_trap_info(const struct desc_ptr *desc,
  590. struct trap_info *traps)
  591. {
  592. unsigned in, out, count;
  593. count = (desc->size+1) / sizeof(gate_desc);
  594. BUG_ON(count > 256);
  595. for (in = out = 0; in < count; in++) {
  596. gate_desc *entry = (gate_desc *)(desc->address) + in;
  597. if (cvt_gate_to_trap(in, entry, &traps[out]))
  598. out++;
  599. }
  600. traps[out].address = 0;
  601. }
  602. void xen_copy_trap_info(struct trap_info *traps)
  603. {
  604. const struct desc_ptr *desc = this_cpu_ptr(&idt_desc);
  605. xen_convert_trap_info(desc, traps);
  606. }
  607. /* Load a new IDT into Xen. In principle this can be per-CPU, so we
  608. hold a spinlock to protect the static traps[] array (static because
  609. it avoids allocation, and saves stack space). */
  610. static void xen_load_idt(const struct desc_ptr *desc)
  611. {
  612. static DEFINE_SPINLOCK(lock);
  613. static struct trap_info traps[257];
  614. trace_xen_cpu_load_idt(desc);
  615. spin_lock(&lock);
  616. memcpy(this_cpu_ptr(&idt_desc), desc, sizeof(idt_desc));
  617. xen_convert_trap_info(desc, traps);
  618. xen_mc_flush();
  619. if (HYPERVISOR_set_trap_table(traps))
  620. BUG();
  621. spin_unlock(&lock);
  622. }
  623. /* Write a GDT descriptor entry. Ignore LDT descriptors, since
  624. they're handled differently. */
  625. static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
  626. const void *desc, int type)
  627. {
  628. trace_xen_cpu_write_gdt_entry(dt, entry, desc, type);
  629. preempt_disable();
  630. switch (type) {
  631. case DESC_LDT:
  632. case DESC_TSS:
  633. /* ignore */
  634. break;
  635. default: {
  636. xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
  637. xen_mc_flush();
  638. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  639. BUG();
  640. }
  641. }
  642. preempt_enable();
  643. }
  644. /*
  645. * Version of write_gdt_entry for use at early boot-time needed to
  646. * update an entry as simply as possible.
  647. */
  648. static void __init xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
  649. const void *desc, int type)
  650. {
  651. trace_xen_cpu_write_gdt_entry(dt, entry, desc, type);
  652. switch (type) {
  653. case DESC_LDT:
  654. case DESC_TSS:
  655. /* ignore */
  656. break;
  657. default: {
  658. xmaddr_t maddr = virt_to_machine(&dt[entry]);
  659. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  660. dt[entry] = *(struct desc_struct *)desc;
  661. }
  662. }
  663. }
  664. static void xen_load_sp0(struct tss_struct *tss,
  665. struct thread_struct *thread)
  666. {
  667. struct multicall_space mcs;
  668. mcs = xen_mc_entry(0);
  669. MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
  670. xen_mc_issue(PARAVIRT_LAZY_CPU);
  671. tss->x86_tss.sp0 = thread->sp0;
  672. }
  673. void xen_set_iopl_mask(unsigned mask)
  674. {
  675. struct physdev_set_iopl set_iopl;
  676. /* Force the change at ring 0. */
  677. set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
  678. HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  679. }
  680. static void xen_io_delay(void)
  681. {
  682. }
  683. static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
  684. static unsigned long xen_read_cr0(void)
  685. {
  686. unsigned long cr0 = this_cpu_read(xen_cr0_value);
  687. if (unlikely(cr0 == 0)) {
  688. cr0 = native_read_cr0();
  689. this_cpu_write(xen_cr0_value, cr0);
  690. }
  691. return cr0;
  692. }
  693. static void xen_write_cr0(unsigned long cr0)
  694. {
  695. struct multicall_space mcs;
  696. this_cpu_write(xen_cr0_value, cr0);
  697. /* Only pay attention to cr0.TS; everything else is
  698. ignored. */
  699. mcs = xen_mc_entry(0);
  700. MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
  701. xen_mc_issue(PARAVIRT_LAZY_CPU);
  702. }
  703. static void xen_write_cr4(unsigned long cr4)
  704. {
  705. cr4 &= ~(X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PCE);
  706. native_write_cr4(cr4);
  707. }
  708. #ifdef CONFIG_X86_64
  709. static inline unsigned long xen_read_cr8(void)
  710. {
  711. return 0;
  712. }
  713. static inline void xen_write_cr8(unsigned long val)
  714. {
  715. BUG_ON(val);
  716. }
  717. #endif
  718. static u64 xen_read_msr_safe(unsigned int msr, int *err)
  719. {
  720. u64 val;
  721. if (pmu_msr_read(msr, &val, err))
  722. return val;
  723. val = native_read_msr_safe(msr, err);
  724. switch (msr) {
  725. case MSR_IA32_APICBASE:
  726. #ifdef CONFIG_X86_X2APIC
  727. if (!(cpuid_ecx(1) & (1 << (X86_FEATURE_X2APIC & 31))))
  728. #endif
  729. val &= ~X2APIC_ENABLE;
  730. break;
  731. }
  732. return val;
  733. }
  734. static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
  735. {
  736. int ret;
  737. ret = 0;
  738. switch (msr) {
  739. #ifdef CONFIG_X86_64
  740. unsigned which;
  741. u64 base;
  742. case MSR_FS_BASE: which = SEGBASE_FS; goto set;
  743. case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
  744. case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
  745. set:
  746. base = ((u64)high << 32) | low;
  747. if (HYPERVISOR_set_segment_base(which, base) != 0)
  748. ret = -EIO;
  749. break;
  750. #endif
  751. case MSR_STAR:
  752. case MSR_CSTAR:
  753. case MSR_LSTAR:
  754. case MSR_SYSCALL_MASK:
  755. case MSR_IA32_SYSENTER_CS:
  756. case MSR_IA32_SYSENTER_ESP:
  757. case MSR_IA32_SYSENTER_EIP:
  758. /* Fast syscall setup is all done in hypercalls, so
  759. these are all ignored. Stub them out here to stop
  760. Xen console noise. */
  761. break;
  762. default:
  763. if (!pmu_msr_write(msr, low, high, &ret))
  764. ret = native_write_msr_safe(msr, low, high);
  765. }
  766. return ret;
  767. }
  768. static u64 xen_read_msr(unsigned int msr)
  769. {
  770. /*
  771. * This will silently swallow a #GP from RDMSR. It may be worth
  772. * changing that.
  773. */
  774. int err;
  775. return xen_read_msr_safe(msr, &err);
  776. }
  777. static void xen_write_msr(unsigned int msr, unsigned low, unsigned high)
  778. {
  779. /*
  780. * This will silently swallow a #GP from WRMSR. It may be worth
  781. * changing that.
  782. */
  783. xen_write_msr_safe(msr, low, high);
  784. }
  785. void xen_setup_shared_info(void)
  786. {
  787. set_fixmap(FIX_PARAVIRT_BOOTMAP, xen_start_info->shared_info);
  788. HYPERVISOR_shared_info =
  789. (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
  790. xen_setup_mfn_list_list();
  791. if (system_state == SYSTEM_BOOTING) {
  792. #ifndef CONFIG_SMP
  793. /*
  794. * In UP this is as good a place as any to set up shared info.
  795. * Limit this to boot only, at restore vcpu setup is done via
  796. * xen_vcpu_restore().
  797. */
  798. xen_setup_vcpu_info_placement();
  799. #endif
  800. /*
  801. * Now that shared info is set up we can start using routines
  802. * that point to pvclock area.
  803. */
  804. xen_init_time_ops();
  805. }
  806. }
  807. /* This is called once we have the cpu_possible_mask */
  808. void __ref xen_setup_vcpu_info_placement(void)
  809. {
  810. int cpu;
  811. for_each_possible_cpu(cpu) {
  812. /* Set up direct vCPU id mapping for PV guests. */
  813. per_cpu(xen_vcpu_id, cpu) = cpu;
  814. /*
  815. * xen_vcpu_setup(cpu) can fail -- in which case it
  816. * falls back to the shared_info version for cpus
  817. * where xen_vcpu_nr(cpu) < MAX_VIRT_CPUS.
  818. *
  819. * xen_cpu_up_prepare_pv() handles the rest by failing
  820. * them in hotplug.
  821. */
  822. (void) xen_vcpu_setup(cpu);
  823. }
  824. /*
  825. * xen_vcpu_setup managed to place the vcpu_info within the
  826. * percpu area for all cpus, so make use of it.
  827. */
  828. if (xen_have_vcpu_info_placement) {
  829. pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
  830. pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
  831. pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
  832. pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
  833. pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
  834. }
  835. }
  836. static const struct pv_info xen_info __initconst = {
  837. .shared_kernel_pmd = 0,
  838. #ifdef CONFIG_X86_64
  839. .extra_user_64bit_cs = FLAT_USER_CS64,
  840. #endif
  841. .name = "Xen",
  842. };
  843. static const struct pv_cpu_ops xen_cpu_ops __initconst = {
  844. .cpuid = xen_cpuid,
  845. .set_debugreg = xen_set_debugreg,
  846. .get_debugreg = xen_get_debugreg,
  847. .read_cr0 = xen_read_cr0,
  848. .write_cr0 = xen_write_cr0,
  849. .write_cr4 = xen_write_cr4,
  850. #ifdef CONFIG_X86_64
  851. .read_cr8 = xen_read_cr8,
  852. .write_cr8 = xen_write_cr8,
  853. #endif
  854. .wbinvd = native_wbinvd,
  855. .read_msr = xen_read_msr,
  856. .write_msr = xen_write_msr,
  857. .read_msr_safe = xen_read_msr_safe,
  858. .write_msr_safe = xen_write_msr_safe,
  859. .read_pmc = xen_read_pmc,
  860. .iret = xen_iret,
  861. #ifdef CONFIG_X86_64
  862. .usergs_sysret64 = xen_sysret64,
  863. #endif
  864. .load_tr_desc = paravirt_nop,
  865. .set_ldt = xen_set_ldt,
  866. .load_gdt = xen_load_gdt,
  867. .load_idt = xen_load_idt,
  868. .load_tls = xen_load_tls,
  869. #ifdef CONFIG_X86_64
  870. .load_gs_index = xen_load_gs_index,
  871. #endif
  872. .alloc_ldt = xen_alloc_ldt,
  873. .free_ldt = xen_free_ldt,
  874. .store_tr = xen_store_tr,
  875. .write_ldt_entry = xen_write_ldt_entry,
  876. .write_gdt_entry = xen_write_gdt_entry,
  877. .write_idt_entry = xen_write_idt_entry,
  878. .load_sp0 = xen_load_sp0,
  879. .set_iopl_mask = xen_set_iopl_mask,
  880. .io_delay = xen_io_delay,
  881. /* Xen takes care of %gs when switching to usermode for us */
  882. .swapgs = paravirt_nop,
  883. .start_context_switch = paravirt_start_context_switch,
  884. .end_context_switch = xen_end_context_switch,
  885. };
  886. static void xen_restart(char *msg)
  887. {
  888. xen_reboot(SHUTDOWN_reboot);
  889. }
  890. static void xen_machine_halt(void)
  891. {
  892. xen_reboot(SHUTDOWN_poweroff);
  893. }
  894. static void xen_machine_power_off(void)
  895. {
  896. if (pm_power_off)
  897. pm_power_off();
  898. xen_reboot(SHUTDOWN_poweroff);
  899. }
  900. static void xen_crash_shutdown(struct pt_regs *regs)
  901. {
  902. xen_reboot(SHUTDOWN_crash);
  903. }
  904. static const struct machine_ops xen_machine_ops __initconst = {
  905. .restart = xen_restart,
  906. .halt = xen_machine_halt,
  907. .power_off = xen_machine_power_off,
  908. .shutdown = xen_machine_halt,
  909. .crash_shutdown = xen_crash_shutdown,
  910. .emergency_restart = xen_emergency_restart,
  911. };
  912. static unsigned char xen_get_nmi_reason(void)
  913. {
  914. unsigned char reason = 0;
  915. /* Construct a value which looks like it came from port 0x61. */
  916. if (test_bit(_XEN_NMIREASON_io_error,
  917. &HYPERVISOR_shared_info->arch.nmi_reason))
  918. reason |= NMI_REASON_IOCHK;
  919. if (test_bit(_XEN_NMIREASON_pci_serr,
  920. &HYPERVISOR_shared_info->arch.nmi_reason))
  921. reason |= NMI_REASON_SERR;
  922. return reason;
  923. }
  924. static void __init xen_boot_params_init_edd(void)
  925. {
  926. #if IS_ENABLED(CONFIG_EDD)
  927. struct xen_platform_op op;
  928. struct edd_info *edd_info;
  929. u32 *mbr_signature;
  930. unsigned nr;
  931. int ret;
  932. edd_info = boot_params.eddbuf;
  933. mbr_signature = boot_params.edd_mbr_sig_buffer;
  934. op.cmd = XENPF_firmware_info;
  935. op.u.firmware_info.type = XEN_FW_DISK_INFO;
  936. for (nr = 0; nr < EDDMAXNR; nr++) {
  937. struct edd_info *info = edd_info + nr;
  938. op.u.firmware_info.index = nr;
  939. info->params.length = sizeof(info->params);
  940. set_xen_guest_handle(op.u.firmware_info.u.disk_info.edd_params,
  941. &info->params);
  942. ret = HYPERVISOR_platform_op(&op);
  943. if (ret)
  944. break;
  945. #define C(x) info->x = op.u.firmware_info.u.disk_info.x
  946. C(device);
  947. C(version);
  948. C(interface_support);
  949. C(legacy_max_cylinder);
  950. C(legacy_max_head);
  951. C(legacy_sectors_per_track);
  952. #undef C
  953. }
  954. boot_params.eddbuf_entries = nr;
  955. op.u.firmware_info.type = XEN_FW_DISK_MBR_SIGNATURE;
  956. for (nr = 0; nr < EDD_MBR_SIG_MAX; nr++) {
  957. op.u.firmware_info.index = nr;
  958. ret = HYPERVISOR_platform_op(&op);
  959. if (ret)
  960. break;
  961. mbr_signature[nr] = op.u.firmware_info.u.disk_mbr_signature.mbr_signature;
  962. }
  963. boot_params.edd_mbr_sig_buf_entries = nr;
  964. #endif
  965. }
  966. /*
  967. * Set up the GDT and segment registers for -fstack-protector. Until
  968. * we do this, we have to be careful not to call any stack-protected
  969. * function, which is most of the kernel.
  970. */
  971. static void xen_setup_gdt(int cpu)
  972. {
  973. pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
  974. pv_cpu_ops.load_gdt = xen_load_gdt_boot;
  975. setup_stack_canary_segment(0);
  976. switch_to_new_gdt(0);
  977. pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
  978. pv_cpu_ops.load_gdt = xen_load_gdt;
  979. }
  980. static void __init xen_dom0_set_legacy_features(void)
  981. {
  982. x86_platform.legacy.rtc = 1;
  983. }
  984. /* First C function to be called on Xen boot */
  985. asmlinkage __visible void __init xen_start_kernel(void)
  986. {
  987. struct physdev_set_iopl set_iopl;
  988. unsigned long initrd_start = 0;
  989. int rc;
  990. if (!xen_start_info)
  991. return;
  992. xen_domain_type = XEN_PV_DOMAIN;
  993. xen_setup_features();
  994. xen_setup_machphys_mapping();
  995. /* Install Xen paravirt ops */
  996. pv_info = xen_info;
  997. pv_init_ops.patch = paravirt_patch_default;
  998. pv_cpu_ops = xen_cpu_ops;
  999. x86_platform.get_nmi_reason = xen_get_nmi_reason;
  1000. x86_init.resources.memory_setup = xen_memory_setup;
  1001. x86_init.oem.arch_setup = xen_arch_setup;
  1002. x86_init.oem.banner = xen_banner;
  1003. /*
  1004. * Set up some pagetable state before starting to set any ptes.
  1005. */
  1006. xen_init_mmu_ops();
  1007. /* Prevent unwanted bits from being set in PTEs. */
  1008. __supported_pte_mask &= ~_PAGE_GLOBAL;
  1009. /*
  1010. * Prevent page tables from being allocated in highmem, even
  1011. * if CONFIG_HIGHPTE is enabled.
  1012. */
  1013. __userpte_alloc_gfp &= ~__GFP_HIGHMEM;
  1014. /* Work out if we support NX */
  1015. get_cpu_cap(&boot_cpu_data);
  1016. x86_configure_nx();
  1017. /* Get mfn list */
  1018. xen_build_dynamic_phys_to_machine();
  1019. /*
  1020. * Set up kernel GDT and segment registers, mainly so that
  1021. * -fstack-protector code can be executed.
  1022. */
  1023. xen_setup_gdt(0);
  1024. xen_init_irq_ops();
  1025. xen_init_capabilities();
  1026. #ifdef CONFIG_X86_LOCAL_APIC
  1027. /*
  1028. * set up the basic apic ops.
  1029. */
  1030. xen_init_apic();
  1031. #endif
  1032. if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
  1033. pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
  1034. pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
  1035. }
  1036. machine_ops = xen_machine_ops;
  1037. /*
  1038. * The only reliable way to retain the initial address of the
  1039. * percpu gdt_page is to remember it here, so we can go and
  1040. * mark it RW later, when the initial percpu area is freed.
  1041. */
  1042. xen_initial_gdt = &per_cpu(gdt_page, 0);
  1043. xen_smp_init();
  1044. #ifdef CONFIG_ACPI_NUMA
  1045. /*
  1046. * The pages we from Xen are not related to machine pages, so
  1047. * any NUMA information the kernel tries to get from ACPI will
  1048. * be meaningless. Prevent it from trying.
  1049. */
  1050. acpi_numa = -1;
  1051. #endif
  1052. /* Let's presume PV guests always boot on vCPU with id 0. */
  1053. per_cpu(xen_vcpu_id, 0) = 0;
  1054. /*
  1055. * Setup xen_vcpu early because start_kernel needs it for
  1056. * local_irq_disable(), irqs_disabled().
  1057. *
  1058. * Don't do the full vcpu_info placement stuff until we have
  1059. * the cpu_possible_mask and a non-dummy shared_info.
  1060. */
  1061. xen_vcpu_info_reset(0);
  1062. WARN_ON(xen_cpuhp_setup(xen_cpu_up_prepare_pv, xen_cpu_dead_pv));
  1063. local_irq_disable();
  1064. early_boot_irqs_disabled = true;
  1065. xen_raw_console_write("mapping kernel into physical memory\n");
  1066. xen_setup_kernel_pagetable((pgd_t *)xen_start_info->pt_base,
  1067. xen_start_info->nr_pages);
  1068. xen_reserve_special_pages();
  1069. /* keep using Xen gdt for now; no urgent need to change it */
  1070. #ifdef CONFIG_X86_32
  1071. pv_info.kernel_rpl = 1;
  1072. if (xen_feature(XENFEAT_supervisor_mode_kernel))
  1073. pv_info.kernel_rpl = 0;
  1074. #else
  1075. pv_info.kernel_rpl = 0;
  1076. #endif
  1077. /* set the limit of our address space */
  1078. xen_reserve_top();
  1079. /*
  1080. * We used to do this in xen_arch_setup, but that is too late
  1081. * on AMD were early_cpu_init (run before ->arch_setup()) calls
  1082. * early_amd_init which pokes 0xcf8 port.
  1083. */
  1084. set_iopl.iopl = 1;
  1085. rc = HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  1086. if (rc != 0)
  1087. xen_raw_printk("physdev_op failed %d\n", rc);
  1088. #ifdef CONFIG_X86_32
  1089. /* set up basic CPUID stuff */
  1090. cpu_detect(&new_cpu_data);
  1091. set_cpu_cap(&new_cpu_data, X86_FEATURE_FPU);
  1092. new_cpu_data.x86_capability[CPUID_1_EDX] = cpuid_edx(1);
  1093. #endif
  1094. if (xen_start_info->mod_start) {
  1095. if (xen_start_info->flags & SIF_MOD_START_PFN)
  1096. initrd_start = PFN_PHYS(xen_start_info->mod_start);
  1097. else
  1098. initrd_start = __pa(xen_start_info->mod_start);
  1099. }
  1100. /* Poke various useful things into boot_params */
  1101. boot_params.hdr.type_of_loader = (9 << 4) | 0;
  1102. boot_params.hdr.ramdisk_image = initrd_start;
  1103. boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
  1104. boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
  1105. boot_params.hdr.hardware_subarch = X86_SUBARCH_XEN;
  1106. if (!xen_initial_domain()) {
  1107. add_preferred_console("xenboot", 0, NULL);
  1108. add_preferred_console("tty", 0, NULL);
  1109. add_preferred_console("hvc", 0, NULL);
  1110. if (pci_xen)
  1111. x86_init.pci.arch_init = pci_xen_init;
  1112. } else {
  1113. const struct dom0_vga_console_info *info =
  1114. (void *)((char *)xen_start_info +
  1115. xen_start_info->console.dom0.info_off);
  1116. struct xen_platform_op op = {
  1117. .cmd = XENPF_firmware_info,
  1118. .interface_version = XENPF_INTERFACE_VERSION,
  1119. .u.firmware_info.type = XEN_FW_KBD_SHIFT_FLAGS,
  1120. };
  1121. x86_platform.set_legacy_features =
  1122. xen_dom0_set_legacy_features;
  1123. xen_init_vga(info, xen_start_info->console.dom0.info_size);
  1124. xen_start_info->console.domU.mfn = 0;
  1125. xen_start_info->console.domU.evtchn = 0;
  1126. if (HYPERVISOR_platform_op(&op) == 0)
  1127. boot_params.kbd_status = op.u.firmware_info.u.kbd_shift_flags;
  1128. /* Make sure ACS will be enabled */
  1129. pci_request_acs();
  1130. xen_acpi_sleep_register();
  1131. /* Avoid searching for BIOS MP tables */
  1132. x86_init.mpparse.find_smp_config = x86_init_noop;
  1133. x86_init.mpparse.get_smp_config = x86_init_uint_noop;
  1134. xen_boot_params_init_edd();
  1135. }
  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 struct hypervisor_x86 x86_hyper_xen_pv = {
  1186. .name = "Xen PV",
  1187. .detect = xen_platform_pv,
  1188. .pin_vcpu = xen_pin_vcpu,
  1189. };
  1190. EXPORT_SYMBOL(x86_hyper_xen_pv);