enlighten_pv.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  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. { double_fault, xen_double_fault, true },
  498. #ifdef CONFIG_X86_MCE
  499. { machine_check, xen_machine_check, true },
  500. #endif
  501. { nmi, xen_xennmi, true },
  502. { int3, xen_int3, false },
  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. val &= ~X2APIC_ENABLE;
  733. break;
  734. }
  735. return val;
  736. }
  737. static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
  738. {
  739. int ret;
  740. #ifdef CONFIG_X86_64
  741. unsigned int which;
  742. u64 base;
  743. #endif
  744. ret = 0;
  745. switch (msr) {
  746. #ifdef CONFIG_X86_64
  747. case MSR_FS_BASE: which = SEGBASE_FS; goto set;
  748. case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
  749. case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
  750. set:
  751. base = ((u64)high << 32) | low;
  752. if (HYPERVISOR_set_segment_base(which, base) != 0)
  753. ret = -EIO;
  754. break;
  755. #endif
  756. case MSR_STAR:
  757. case MSR_CSTAR:
  758. case MSR_LSTAR:
  759. case MSR_SYSCALL_MASK:
  760. case MSR_IA32_SYSENTER_CS:
  761. case MSR_IA32_SYSENTER_ESP:
  762. case MSR_IA32_SYSENTER_EIP:
  763. /* Fast syscall setup is all done in hypercalls, so
  764. these are all ignored. Stub them out here to stop
  765. Xen console noise. */
  766. break;
  767. default:
  768. if (!pmu_msr_write(msr, low, high, &ret))
  769. ret = native_write_msr_safe(msr, low, high);
  770. }
  771. return ret;
  772. }
  773. static u64 xen_read_msr(unsigned int msr)
  774. {
  775. /*
  776. * This will silently swallow a #GP from RDMSR. It may be worth
  777. * changing that.
  778. */
  779. int err;
  780. return xen_read_msr_safe(msr, &err);
  781. }
  782. static void xen_write_msr(unsigned int msr, unsigned low, unsigned high)
  783. {
  784. /*
  785. * This will silently swallow a #GP from WRMSR. It may be worth
  786. * changing that.
  787. */
  788. xen_write_msr_safe(msr, low, high);
  789. }
  790. /* This is called once we have the cpu_possible_mask */
  791. void __init xen_setup_vcpu_info_placement(void)
  792. {
  793. int cpu;
  794. for_each_possible_cpu(cpu) {
  795. /* Set up direct vCPU id mapping for PV guests. */
  796. per_cpu(xen_vcpu_id, cpu) = cpu;
  797. /*
  798. * xen_vcpu_setup(cpu) can fail -- in which case it
  799. * falls back to the shared_info version for cpus
  800. * where xen_vcpu_nr(cpu) < MAX_VIRT_CPUS.
  801. *
  802. * xen_cpu_up_prepare_pv() handles the rest by failing
  803. * them in hotplug.
  804. */
  805. (void) xen_vcpu_setup(cpu);
  806. }
  807. /*
  808. * xen_vcpu_setup managed to place the vcpu_info within the
  809. * percpu area for all cpus, so make use of it.
  810. */
  811. if (xen_have_vcpu_info_placement) {
  812. pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
  813. pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
  814. pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
  815. pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
  816. pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
  817. }
  818. }
  819. static const struct pv_info xen_info __initconst = {
  820. .shared_kernel_pmd = 0,
  821. #ifdef CONFIG_X86_64
  822. .extra_user_64bit_cs = FLAT_USER_CS64,
  823. #endif
  824. .name = "Xen",
  825. };
  826. static const struct pv_cpu_ops xen_cpu_ops __initconst = {
  827. .cpuid = xen_cpuid,
  828. .set_debugreg = xen_set_debugreg,
  829. .get_debugreg = xen_get_debugreg,
  830. .read_cr0 = xen_read_cr0,
  831. .write_cr0 = xen_write_cr0,
  832. .write_cr4 = xen_write_cr4,
  833. #ifdef CONFIG_X86_64
  834. .read_cr8 = xen_read_cr8,
  835. .write_cr8 = xen_write_cr8,
  836. #endif
  837. .wbinvd = native_wbinvd,
  838. .read_msr = xen_read_msr,
  839. .write_msr = xen_write_msr,
  840. .read_msr_safe = xen_read_msr_safe,
  841. .write_msr_safe = xen_write_msr_safe,
  842. .read_pmc = xen_read_pmc,
  843. .iret = xen_iret,
  844. #ifdef CONFIG_X86_64
  845. .usergs_sysret64 = xen_sysret64,
  846. #endif
  847. .load_tr_desc = paravirt_nop,
  848. .set_ldt = xen_set_ldt,
  849. .load_gdt = xen_load_gdt,
  850. .load_idt = xen_load_idt,
  851. .load_tls = xen_load_tls,
  852. #ifdef CONFIG_X86_64
  853. .load_gs_index = xen_load_gs_index,
  854. #endif
  855. .alloc_ldt = xen_alloc_ldt,
  856. .free_ldt = xen_free_ldt,
  857. .store_tr = xen_store_tr,
  858. .write_ldt_entry = xen_write_ldt_entry,
  859. .write_gdt_entry = xen_write_gdt_entry,
  860. .write_idt_entry = xen_write_idt_entry,
  861. .load_sp0 = xen_load_sp0,
  862. .set_iopl_mask = xen_set_iopl_mask,
  863. .io_delay = xen_io_delay,
  864. /* Xen takes care of %gs when switching to usermode for us */
  865. .swapgs = paravirt_nop,
  866. .start_context_switch = paravirt_start_context_switch,
  867. .end_context_switch = xen_end_context_switch,
  868. };
  869. static void xen_restart(char *msg)
  870. {
  871. xen_reboot(SHUTDOWN_reboot);
  872. }
  873. static void xen_machine_halt(void)
  874. {
  875. xen_reboot(SHUTDOWN_poweroff);
  876. }
  877. static void xen_machine_power_off(void)
  878. {
  879. if (pm_power_off)
  880. pm_power_off();
  881. xen_reboot(SHUTDOWN_poweroff);
  882. }
  883. static void xen_crash_shutdown(struct pt_regs *regs)
  884. {
  885. xen_reboot(SHUTDOWN_crash);
  886. }
  887. static const struct machine_ops xen_machine_ops __initconst = {
  888. .restart = xen_restart,
  889. .halt = xen_machine_halt,
  890. .power_off = xen_machine_power_off,
  891. .shutdown = xen_machine_halt,
  892. .crash_shutdown = xen_crash_shutdown,
  893. .emergency_restart = xen_emergency_restart,
  894. };
  895. static unsigned char xen_get_nmi_reason(void)
  896. {
  897. unsigned char reason = 0;
  898. /* Construct a value which looks like it came from port 0x61. */
  899. if (test_bit(_XEN_NMIREASON_io_error,
  900. &HYPERVISOR_shared_info->arch.nmi_reason))
  901. reason |= NMI_REASON_IOCHK;
  902. if (test_bit(_XEN_NMIREASON_pci_serr,
  903. &HYPERVISOR_shared_info->arch.nmi_reason))
  904. reason |= NMI_REASON_SERR;
  905. return reason;
  906. }
  907. static void __init xen_boot_params_init_edd(void)
  908. {
  909. #if IS_ENABLED(CONFIG_EDD)
  910. struct xen_platform_op op;
  911. struct edd_info *edd_info;
  912. u32 *mbr_signature;
  913. unsigned nr;
  914. int ret;
  915. edd_info = boot_params.eddbuf;
  916. mbr_signature = boot_params.edd_mbr_sig_buffer;
  917. op.cmd = XENPF_firmware_info;
  918. op.u.firmware_info.type = XEN_FW_DISK_INFO;
  919. for (nr = 0; nr < EDDMAXNR; nr++) {
  920. struct edd_info *info = edd_info + nr;
  921. op.u.firmware_info.index = nr;
  922. info->params.length = sizeof(info->params);
  923. set_xen_guest_handle(op.u.firmware_info.u.disk_info.edd_params,
  924. &info->params);
  925. ret = HYPERVISOR_platform_op(&op);
  926. if (ret)
  927. break;
  928. #define C(x) info->x = op.u.firmware_info.u.disk_info.x
  929. C(device);
  930. C(version);
  931. C(interface_support);
  932. C(legacy_max_cylinder);
  933. C(legacy_max_head);
  934. C(legacy_sectors_per_track);
  935. #undef C
  936. }
  937. boot_params.eddbuf_entries = nr;
  938. op.u.firmware_info.type = XEN_FW_DISK_MBR_SIGNATURE;
  939. for (nr = 0; nr < EDD_MBR_SIG_MAX; nr++) {
  940. op.u.firmware_info.index = nr;
  941. ret = HYPERVISOR_platform_op(&op);
  942. if (ret)
  943. break;
  944. mbr_signature[nr] = op.u.firmware_info.u.disk_mbr_signature.mbr_signature;
  945. }
  946. boot_params.edd_mbr_sig_buf_entries = nr;
  947. #endif
  948. }
  949. /*
  950. * Set up the GDT and segment registers for -fstack-protector. Until
  951. * we do this, we have to be careful not to call any stack-protected
  952. * function, which is most of the kernel.
  953. */
  954. static void __init xen_setup_gdt(int cpu)
  955. {
  956. pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
  957. pv_cpu_ops.load_gdt = xen_load_gdt_boot;
  958. setup_stack_canary_segment(cpu);
  959. switch_to_new_gdt(cpu);
  960. pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
  961. pv_cpu_ops.load_gdt = xen_load_gdt;
  962. }
  963. static void __init xen_dom0_set_legacy_features(void)
  964. {
  965. x86_platform.legacy.rtc = 1;
  966. }
  967. /* First C function to be called on Xen boot */
  968. asmlinkage __visible void __init xen_start_kernel(void)
  969. {
  970. struct physdev_set_iopl set_iopl;
  971. unsigned long initrd_start = 0;
  972. int rc;
  973. if (!xen_start_info)
  974. return;
  975. xen_domain_type = XEN_PV_DOMAIN;
  976. xen_start_flags = xen_start_info->flags;
  977. xen_setup_features();
  978. /* Install Xen paravirt ops */
  979. pv_info = xen_info;
  980. pv_init_ops.patch = paravirt_patch_default;
  981. pv_cpu_ops = xen_cpu_ops;
  982. xen_init_irq_ops();
  983. /*
  984. * Setup xen_vcpu early because it is needed for
  985. * local_irq_disable(), irqs_disabled(), e.g. in printk().
  986. *
  987. * Don't do the full vcpu_info placement stuff until we have
  988. * the cpu_possible_mask and a non-dummy shared_info.
  989. */
  990. xen_vcpu_info_reset(0);
  991. x86_platform.get_nmi_reason = xen_get_nmi_reason;
  992. x86_init.resources.memory_setup = xen_memory_setup;
  993. x86_init.irqs.intr_mode_init = x86_init_noop;
  994. x86_init.oem.arch_setup = xen_arch_setup;
  995. x86_init.oem.banner = xen_banner;
  996. x86_init.hyper.init_platform = xen_pv_init_platform;
  997. x86_init.hyper.guest_late_init = xen_pv_guest_late_init;
  998. /*
  999. * Set up some pagetable state before starting to set any ptes.
  1000. */
  1001. xen_setup_machphys_mapping();
  1002. xen_init_mmu_ops();
  1003. /* Prevent unwanted bits from being set in PTEs. */
  1004. __supported_pte_mask &= ~_PAGE_GLOBAL;
  1005. __default_kernel_pte_mask &= ~_PAGE_GLOBAL;
  1006. /*
  1007. * Prevent page tables from being allocated in highmem, even
  1008. * if CONFIG_HIGHPTE is enabled.
  1009. */
  1010. __userpte_alloc_gfp &= ~__GFP_HIGHMEM;
  1011. /* Get mfn list */
  1012. xen_build_dynamic_phys_to_machine();
  1013. /*
  1014. * Set up kernel GDT and segment registers, mainly so that
  1015. * -fstack-protector code can be executed.
  1016. */
  1017. xen_setup_gdt(0);
  1018. /* Work out if we support NX */
  1019. get_cpu_cap(&boot_cpu_data);
  1020. x86_configure_nx();
  1021. /* Determine virtual and physical address sizes */
  1022. get_cpu_address_sizes(&boot_cpu_data);
  1023. /* Let's presume PV guests always boot on vCPU with id 0. */
  1024. per_cpu(xen_vcpu_id, 0) = 0;
  1025. idt_setup_early_handler();
  1026. xen_init_capabilities();
  1027. #ifdef CONFIG_X86_LOCAL_APIC
  1028. /*
  1029. * set up the basic apic ops.
  1030. */
  1031. xen_init_apic();
  1032. #endif
  1033. if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
  1034. pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
  1035. pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
  1036. }
  1037. machine_ops = xen_machine_ops;
  1038. /*
  1039. * The only reliable way to retain the initial address of the
  1040. * percpu gdt_page is to remember it here, so we can go and
  1041. * mark it RW later, when the initial percpu area is freed.
  1042. */
  1043. xen_initial_gdt = &per_cpu(gdt_page, 0);
  1044. xen_smp_init();
  1045. #ifdef CONFIG_ACPI_NUMA
  1046. /*
  1047. * The pages we from Xen are not related to machine pages, so
  1048. * any NUMA information the kernel tries to get from ACPI will
  1049. * be meaningless. Prevent it from trying.
  1050. */
  1051. acpi_numa = -1;
  1052. #endif
  1053. WARN_ON(xen_cpuhp_setup(xen_cpu_up_prepare_pv, xen_cpu_dead_pv));
  1054. local_irq_disable();
  1055. early_boot_irqs_disabled = true;
  1056. xen_raw_console_write("mapping kernel into physical memory\n");
  1057. xen_setup_kernel_pagetable((pgd_t *)xen_start_info->pt_base,
  1058. xen_start_info->nr_pages);
  1059. xen_reserve_special_pages();
  1060. /* keep using Xen gdt for now; no urgent need to change it */
  1061. #ifdef CONFIG_X86_32
  1062. pv_info.kernel_rpl = 1;
  1063. if (xen_feature(XENFEAT_supervisor_mode_kernel))
  1064. pv_info.kernel_rpl = 0;
  1065. #else
  1066. pv_info.kernel_rpl = 0;
  1067. #endif
  1068. /* set the limit of our address space */
  1069. xen_reserve_top();
  1070. /*
  1071. * We used to do this in xen_arch_setup, but that is too late
  1072. * on AMD were early_cpu_init (run before ->arch_setup()) calls
  1073. * early_amd_init which pokes 0xcf8 port.
  1074. */
  1075. set_iopl.iopl = 1;
  1076. rc = HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  1077. if (rc != 0)
  1078. xen_raw_printk("physdev_op failed %d\n", rc);
  1079. #ifdef CONFIG_X86_32
  1080. /* set up basic CPUID stuff */
  1081. cpu_detect(&new_cpu_data);
  1082. set_cpu_cap(&new_cpu_data, X86_FEATURE_FPU);
  1083. new_cpu_data.x86_capability[CPUID_1_EDX] = cpuid_edx(1);
  1084. #endif
  1085. if (xen_start_info->mod_start) {
  1086. if (xen_start_info->flags & SIF_MOD_START_PFN)
  1087. initrd_start = PFN_PHYS(xen_start_info->mod_start);
  1088. else
  1089. initrd_start = __pa(xen_start_info->mod_start);
  1090. }
  1091. /* Poke various useful things into boot_params */
  1092. boot_params.hdr.type_of_loader = (9 << 4) | 0;
  1093. boot_params.hdr.ramdisk_image = initrd_start;
  1094. boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
  1095. boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
  1096. boot_params.hdr.hardware_subarch = X86_SUBARCH_XEN;
  1097. if (!xen_initial_domain()) {
  1098. add_preferred_console("xenboot", 0, NULL);
  1099. if (pci_xen)
  1100. x86_init.pci.arch_init = pci_xen_init;
  1101. } else {
  1102. const struct dom0_vga_console_info *info =
  1103. (void *)((char *)xen_start_info +
  1104. xen_start_info->console.dom0.info_off);
  1105. struct xen_platform_op op = {
  1106. .cmd = XENPF_firmware_info,
  1107. .interface_version = XENPF_INTERFACE_VERSION,
  1108. .u.firmware_info.type = XEN_FW_KBD_SHIFT_FLAGS,
  1109. };
  1110. x86_platform.set_legacy_features =
  1111. xen_dom0_set_legacy_features;
  1112. xen_init_vga(info, xen_start_info->console.dom0.info_size);
  1113. xen_start_info->console.domU.mfn = 0;
  1114. xen_start_info->console.domU.evtchn = 0;
  1115. if (HYPERVISOR_platform_op(&op) == 0)
  1116. boot_params.kbd_status = op.u.firmware_info.u.kbd_shift_flags;
  1117. /* Make sure ACS will be enabled */
  1118. pci_request_acs();
  1119. xen_acpi_sleep_register();
  1120. /* Avoid searching for BIOS MP tables */
  1121. x86_init.mpparse.find_smp_config = x86_init_noop;
  1122. x86_init.mpparse.get_smp_config = x86_init_uint_noop;
  1123. xen_boot_params_init_edd();
  1124. #ifdef CONFIG_ACPI
  1125. /*
  1126. * Disable selecting "Firmware First mode" for correctable
  1127. * memory errors, as this is the duty of the hypervisor to
  1128. * decide.
  1129. */
  1130. acpi_disable_cmcff = 1;
  1131. #endif
  1132. }
  1133. if (!boot_params.screen_info.orig_video_isVGA)
  1134. add_preferred_console("tty", 0, NULL);
  1135. add_preferred_console("hvc", 0, NULL);
  1136. if (boot_params.screen_info.orig_video_isVGA)
  1137. add_preferred_console("tty", 0, NULL);
  1138. #ifdef CONFIG_PCI
  1139. /* PCI BIOS service won't work from a PV guest. */
  1140. pci_probe &= ~PCI_PROBE_BIOS;
  1141. #endif
  1142. xen_raw_console_write("about to get started...\n");
  1143. /* We need this for printk timestamps */
  1144. xen_setup_runstate_info(0);
  1145. xen_efi_init(&boot_params);
  1146. /* Start the world */
  1147. #ifdef CONFIG_X86_32
  1148. i386_start_kernel();
  1149. #else
  1150. cr4_init_shadow(); /* 32b kernel does this in i386_start_kernel() */
  1151. x86_64_start_reservations((char *)__pa_symbol(&boot_params));
  1152. #endif
  1153. }
  1154. static int xen_cpu_up_prepare_pv(unsigned int cpu)
  1155. {
  1156. int rc;
  1157. if (per_cpu(xen_vcpu, cpu) == NULL)
  1158. return -ENODEV;
  1159. xen_setup_timer(cpu);
  1160. rc = xen_smp_intr_init(cpu);
  1161. if (rc) {
  1162. WARN(1, "xen_smp_intr_init() for CPU %d failed: %d\n",
  1163. cpu, rc);
  1164. return rc;
  1165. }
  1166. rc = xen_smp_intr_init_pv(cpu);
  1167. if (rc) {
  1168. WARN(1, "xen_smp_intr_init_pv() for CPU %d failed: %d\n",
  1169. cpu, rc);
  1170. return rc;
  1171. }
  1172. return 0;
  1173. }
  1174. static int xen_cpu_dead_pv(unsigned int cpu)
  1175. {
  1176. xen_smp_intr_free(cpu);
  1177. xen_smp_intr_free_pv(cpu);
  1178. xen_teardown_timer(cpu);
  1179. return 0;
  1180. }
  1181. static uint32_t __init xen_platform_pv(void)
  1182. {
  1183. if (xen_pv_domain())
  1184. return xen_cpuid_base();
  1185. return 0;
  1186. }
  1187. const __initconst struct hypervisor_x86 x86_hyper_xen_pv = {
  1188. .name = "Xen PV",
  1189. .detect = xen_platform_pv,
  1190. .type = X86_HYPER_XEN_PV,
  1191. .runtime.pin_vcpu = xen_pin_vcpu,
  1192. };