enlighten_pv.c 36 KB

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