enlighten_pv.c 36 KB

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