enlighten_pv.c 35 KB

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