enlighten.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  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/module.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 <xen/xen.h>
  34. #include <xen/interface/xen.h>
  35. #include <xen/interface/version.h>
  36. #include <xen/interface/physdev.h>
  37. #include <xen/interface/vcpu.h>
  38. #include <xen/interface/memory.h>
  39. #include <xen/features.h>
  40. #include <xen/page.h>
  41. #include <xen/hvm.h>
  42. #include <xen/hvc-console.h>
  43. #include <xen/acpi.h>
  44. #include <asm/paravirt.h>
  45. #include <asm/apic.h>
  46. #include <asm/page.h>
  47. #include <asm/xen/pci.h>
  48. #include <asm/xen/hypercall.h>
  49. #include <asm/xen/hypervisor.h>
  50. #include <asm/fixmap.h>
  51. #include <asm/processor.h>
  52. #include <asm/proto.h>
  53. #include <asm/msr-index.h>
  54. #include <asm/traps.h>
  55. #include <asm/setup.h>
  56. #include <asm/desc.h>
  57. #include <asm/pgalloc.h>
  58. #include <asm/pgtable.h>
  59. #include <asm/tlbflush.h>
  60. #include <asm/reboot.h>
  61. #include <asm/stackprotector.h>
  62. #include <asm/hypervisor.h>
  63. #include <asm/mwait.h>
  64. #include <asm/pci_x86.h>
  65. #ifdef CONFIG_ACPI
  66. #include <linux/acpi.h>
  67. #include <asm/acpi.h>
  68. #include <acpi/pdc_intel.h>
  69. #include <acpi/processor.h>
  70. #include <xen/interface/platform.h>
  71. #endif
  72. #include "xen-ops.h"
  73. #include "mmu.h"
  74. #include "smp.h"
  75. #include "multicalls.h"
  76. EXPORT_SYMBOL_GPL(hypercall_page);
  77. DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
  78. DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
  79. enum xen_domain_type xen_domain_type = XEN_NATIVE;
  80. EXPORT_SYMBOL_GPL(xen_domain_type);
  81. unsigned long *machine_to_phys_mapping = (void *)MACH2PHYS_VIRT_START;
  82. EXPORT_SYMBOL(machine_to_phys_mapping);
  83. unsigned long machine_to_phys_nr;
  84. EXPORT_SYMBOL(machine_to_phys_nr);
  85. struct start_info *xen_start_info;
  86. EXPORT_SYMBOL_GPL(xen_start_info);
  87. struct shared_info xen_dummy_shared_info;
  88. void *xen_initial_gdt;
  89. RESERVE_BRK(shared_info_page_brk, PAGE_SIZE);
  90. __read_mostly int xen_have_vector_callback;
  91. EXPORT_SYMBOL_GPL(xen_have_vector_callback);
  92. /*
  93. * Point at some empty memory to start with. We map the real shared_info
  94. * page as soon as fixmap is up and running.
  95. */
  96. struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
  97. /*
  98. * Flag to determine whether vcpu info placement is available on all
  99. * VCPUs. We assume it is to start with, and then set it to zero on
  100. * the first failure. This is because it can succeed on some VCPUs
  101. * and not others, since it can involve hypervisor memory allocation,
  102. * or because the guest failed to guarantee all the appropriate
  103. * constraints on all VCPUs (ie buffer can't cross a page boundary).
  104. *
  105. * Note that any particular CPU may be using a placed vcpu structure,
  106. * but we can only optimise if the all are.
  107. *
  108. * 0: not available, 1: available
  109. */
  110. static int have_vcpu_info_placement = 1;
  111. static void clamp_max_cpus(void)
  112. {
  113. #ifdef CONFIG_SMP
  114. if (setup_max_cpus > MAX_VIRT_CPUS)
  115. setup_max_cpus = MAX_VIRT_CPUS;
  116. #endif
  117. }
  118. static void xen_vcpu_setup(int cpu)
  119. {
  120. struct vcpu_register_vcpu_info info;
  121. int err;
  122. struct vcpu_info *vcpup;
  123. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  124. if (cpu < MAX_VIRT_CPUS)
  125. per_cpu(xen_vcpu,cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
  126. if (!have_vcpu_info_placement) {
  127. if (cpu >= MAX_VIRT_CPUS)
  128. clamp_max_cpus();
  129. return;
  130. }
  131. vcpup = &per_cpu(xen_vcpu_info, cpu);
  132. info.mfn = arbitrary_virt_to_mfn(vcpup);
  133. info.offset = offset_in_page(vcpup);
  134. /* Check to see if the hypervisor will put the vcpu_info
  135. structure where we want it, which allows direct access via
  136. a percpu-variable. */
  137. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
  138. if (err) {
  139. printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
  140. have_vcpu_info_placement = 0;
  141. clamp_max_cpus();
  142. } else {
  143. /* This cpu is using the registered vcpu info, even if
  144. later ones fail to. */
  145. per_cpu(xen_vcpu, cpu) = vcpup;
  146. }
  147. }
  148. /*
  149. * On restore, set the vcpu placement up again.
  150. * If it fails, then we're in a bad state, since
  151. * we can't back out from using it...
  152. */
  153. void xen_vcpu_restore(void)
  154. {
  155. int cpu;
  156. for_each_online_cpu(cpu) {
  157. bool other_cpu = (cpu != smp_processor_id());
  158. if (other_cpu &&
  159. HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
  160. BUG();
  161. xen_setup_runstate_info(cpu);
  162. if (have_vcpu_info_placement)
  163. xen_vcpu_setup(cpu);
  164. if (other_cpu &&
  165. HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
  166. BUG();
  167. }
  168. }
  169. static void __init xen_banner(void)
  170. {
  171. unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
  172. struct xen_extraversion extra;
  173. HYPERVISOR_xen_version(XENVER_extraversion, &extra);
  174. printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
  175. pv_info.name);
  176. printk(KERN_INFO "Xen version: %d.%d%s%s\n",
  177. version >> 16, version & 0xffff, extra.extraversion,
  178. xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
  179. }
  180. static __read_mostly unsigned int cpuid_leaf1_edx_mask = ~0;
  181. static __read_mostly unsigned int cpuid_leaf1_ecx_mask = ~0;
  182. static __read_mostly unsigned int cpuid_leaf1_ecx_set_mask;
  183. static __read_mostly unsigned int cpuid_leaf5_ecx_val;
  184. static __read_mostly unsigned int cpuid_leaf5_edx_val;
  185. static void xen_cpuid(unsigned int *ax, unsigned int *bx,
  186. unsigned int *cx, unsigned int *dx)
  187. {
  188. unsigned maskebx = ~0;
  189. unsigned maskecx = ~0;
  190. unsigned maskedx = ~0;
  191. unsigned setecx = 0;
  192. /*
  193. * Mask out inconvenient features, to try and disable as many
  194. * unsupported kernel subsystems as possible.
  195. */
  196. switch (*ax) {
  197. case 1:
  198. maskecx = cpuid_leaf1_ecx_mask;
  199. setecx = cpuid_leaf1_ecx_set_mask;
  200. maskedx = cpuid_leaf1_edx_mask;
  201. break;
  202. case CPUID_MWAIT_LEAF:
  203. /* Synthesize the values.. */
  204. *ax = 0;
  205. *bx = 0;
  206. *cx = cpuid_leaf5_ecx_val;
  207. *dx = cpuid_leaf5_edx_val;
  208. return;
  209. case 0xb:
  210. /* Suppress extended topology stuff */
  211. maskebx = 0;
  212. break;
  213. }
  214. asm(XEN_EMULATE_PREFIX "cpuid"
  215. : "=a" (*ax),
  216. "=b" (*bx),
  217. "=c" (*cx),
  218. "=d" (*dx)
  219. : "0" (*ax), "2" (*cx));
  220. *bx &= maskebx;
  221. *cx &= maskecx;
  222. *cx |= setecx;
  223. *dx &= maskedx;
  224. }
  225. static bool __init xen_check_mwait(void)
  226. {
  227. #if defined(CONFIG_ACPI) && !defined(CONFIG_ACPI_PROCESSOR_AGGREGATOR) && \
  228. !defined(CONFIG_ACPI_PROCESSOR_AGGREGATOR_MODULE)
  229. struct xen_platform_op op = {
  230. .cmd = XENPF_set_processor_pminfo,
  231. .u.set_pminfo.id = -1,
  232. .u.set_pminfo.type = XEN_PM_PDC,
  233. };
  234. uint32_t buf[3];
  235. unsigned int ax, bx, cx, dx;
  236. unsigned int mwait_mask;
  237. /* We need to determine whether it is OK to expose the MWAIT
  238. * capability to the kernel to harvest deeper than C3 states from ACPI
  239. * _CST using the processor_harvest_xen.c module. For this to work, we
  240. * need to gather the MWAIT_LEAF values (which the cstate.c code
  241. * checks against). The hypervisor won't expose the MWAIT flag because
  242. * it would break backwards compatibility; so we will find out directly
  243. * from the hardware and hypercall.
  244. */
  245. if (!xen_initial_domain())
  246. return false;
  247. ax = 1;
  248. cx = 0;
  249. native_cpuid(&ax, &bx, &cx, &dx);
  250. mwait_mask = (1 << (X86_FEATURE_EST % 32)) |
  251. (1 << (X86_FEATURE_MWAIT % 32));
  252. if ((cx & mwait_mask) != mwait_mask)
  253. return false;
  254. /* We need to emulate the MWAIT_LEAF and for that we need both
  255. * ecx and edx. The hypercall provides only partial information.
  256. */
  257. ax = CPUID_MWAIT_LEAF;
  258. bx = 0;
  259. cx = 0;
  260. dx = 0;
  261. native_cpuid(&ax, &bx, &cx, &dx);
  262. /* Ask the Hypervisor whether to clear ACPI_PDC_C_C2C3_FFH. If so,
  263. * don't expose MWAIT_LEAF and let ACPI pick the IOPORT version of C3.
  264. */
  265. buf[0] = ACPI_PDC_REVISION_ID;
  266. buf[1] = 1;
  267. buf[2] = (ACPI_PDC_C_CAPABILITY_SMP | ACPI_PDC_EST_CAPABILITY_SWSMP);
  268. set_xen_guest_handle(op.u.set_pminfo.pdc, buf);
  269. if ((HYPERVISOR_dom0_op(&op) == 0) &&
  270. (buf[2] & (ACPI_PDC_C_C1_FFH | ACPI_PDC_C_C2C3_FFH))) {
  271. cpuid_leaf5_ecx_val = cx;
  272. cpuid_leaf5_edx_val = dx;
  273. }
  274. return true;
  275. #else
  276. return false;
  277. #endif
  278. }
  279. static void __init xen_init_cpuid_mask(void)
  280. {
  281. unsigned int ax, bx, cx, dx;
  282. unsigned int xsave_mask;
  283. cpuid_leaf1_edx_mask =
  284. ~((1 << X86_FEATURE_MCE) | /* disable MCE */
  285. (1 << X86_FEATURE_MCA) | /* disable MCA */
  286. (1 << X86_FEATURE_MTRR) | /* disable MTRR */
  287. (1 << X86_FEATURE_ACC)); /* thermal monitoring */
  288. if (!xen_initial_domain())
  289. cpuid_leaf1_edx_mask &=
  290. ~((1 << X86_FEATURE_APIC) | /* disable local APIC */
  291. (1 << X86_FEATURE_ACPI)); /* disable ACPI */
  292. ax = 1;
  293. cx = 0;
  294. xen_cpuid(&ax, &bx, &cx, &dx);
  295. xsave_mask =
  296. (1 << (X86_FEATURE_XSAVE % 32)) |
  297. (1 << (X86_FEATURE_OSXSAVE % 32));
  298. /* Xen will set CR4.OSXSAVE if supported and not disabled by force */
  299. if ((cx & xsave_mask) != xsave_mask)
  300. cpuid_leaf1_ecx_mask &= ~xsave_mask; /* disable XSAVE & OSXSAVE */
  301. if (xen_check_mwait())
  302. cpuid_leaf1_ecx_set_mask = (1 << (X86_FEATURE_MWAIT % 32));
  303. }
  304. static void xen_set_debugreg(int reg, unsigned long val)
  305. {
  306. HYPERVISOR_set_debugreg(reg, val);
  307. }
  308. static unsigned long xen_get_debugreg(int reg)
  309. {
  310. return HYPERVISOR_get_debugreg(reg);
  311. }
  312. static void xen_end_context_switch(struct task_struct *next)
  313. {
  314. xen_mc_flush();
  315. paravirt_end_context_switch(next);
  316. }
  317. static unsigned long xen_store_tr(void)
  318. {
  319. return 0;
  320. }
  321. /*
  322. * Set the page permissions for a particular virtual address. If the
  323. * address is a vmalloc mapping (or other non-linear mapping), then
  324. * find the linear mapping of the page and also set its protections to
  325. * match.
  326. */
  327. static void set_aliased_prot(void *v, pgprot_t prot)
  328. {
  329. int level;
  330. pte_t *ptep;
  331. pte_t pte;
  332. unsigned long pfn;
  333. struct page *page;
  334. ptep = lookup_address((unsigned long)v, &level);
  335. BUG_ON(ptep == NULL);
  336. pfn = pte_pfn(*ptep);
  337. page = pfn_to_page(pfn);
  338. pte = pfn_pte(pfn, prot);
  339. if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
  340. BUG();
  341. if (!PageHighMem(page)) {
  342. void *av = __va(PFN_PHYS(pfn));
  343. if (av != v)
  344. if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
  345. BUG();
  346. } else
  347. kmap_flush_unused();
  348. }
  349. static void xen_alloc_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_RO);
  355. }
  356. static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
  357. {
  358. const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
  359. int i;
  360. for(i = 0; i < entries; i += entries_per_page)
  361. set_aliased_prot(ldt + i, PAGE_KERNEL);
  362. }
  363. static void xen_set_ldt(const void *addr, unsigned entries)
  364. {
  365. struct mmuext_op *op;
  366. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  367. trace_xen_cpu_set_ldt(addr, entries);
  368. op = mcs.args;
  369. op->cmd = MMUEXT_SET_LDT;
  370. op->arg1.linear_addr = (unsigned long)addr;
  371. op->arg2.nr_ents = entries;
  372. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  373. xen_mc_issue(PARAVIRT_LAZY_CPU);
  374. }
  375. static void xen_load_gdt(const struct desc_ptr *dtr)
  376. {
  377. unsigned long va = dtr->address;
  378. unsigned int size = dtr->size + 1;
  379. unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  380. unsigned long frames[pages];
  381. int f;
  382. /*
  383. * A GDT can be up to 64k in size, which corresponds to 8192
  384. * 8-byte entries, or 16 4k pages..
  385. */
  386. BUG_ON(size > 65536);
  387. BUG_ON(va & ~PAGE_MASK);
  388. for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
  389. int level;
  390. pte_t *ptep;
  391. unsigned long pfn, mfn;
  392. void *virt;
  393. /*
  394. * The GDT is per-cpu and is in the percpu data area.
  395. * That can be virtually mapped, so we need to do a
  396. * page-walk to get the underlying MFN for the
  397. * hypercall. The page can also be in the kernel's
  398. * linear range, so we need to RO that mapping too.
  399. */
  400. ptep = lookup_address(va, &level);
  401. BUG_ON(ptep == NULL);
  402. pfn = pte_pfn(*ptep);
  403. mfn = pfn_to_mfn(pfn);
  404. virt = __va(PFN_PHYS(pfn));
  405. frames[f] = mfn;
  406. make_lowmem_page_readonly((void *)va);
  407. make_lowmem_page_readonly(virt);
  408. }
  409. if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
  410. BUG();
  411. }
  412. /*
  413. * load_gdt for early boot, when the gdt is only mapped once
  414. */
  415. static void __init xen_load_gdt_boot(const struct desc_ptr *dtr)
  416. {
  417. unsigned long va = dtr->address;
  418. unsigned int size = dtr->size + 1;
  419. unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  420. unsigned long frames[pages];
  421. int f;
  422. /*
  423. * A GDT can be up to 64k in size, which corresponds to 8192
  424. * 8-byte entries, or 16 4k pages..
  425. */
  426. BUG_ON(size > 65536);
  427. BUG_ON(va & ~PAGE_MASK);
  428. for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
  429. pte_t pte;
  430. unsigned long pfn, mfn;
  431. pfn = virt_to_pfn(va);
  432. mfn = pfn_to_mfn(pfn);
  433. pte = pfn_pte(pfn, PAGE_KERNEL_RO);
  434. if (HYPERVISOR_update_va_mapping((unsigned long)va, pte, 0))
  435. BUG();
  436. frames[f] = mfn;
  437. }
  438. if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
  439. BUG();
  440. }
  441. static void load_TLS_descriptor(struct thread_struct *t,
  442. unsigned int cpu, unsigned int i)
  443. {
  444. struct desc_struct *gdt = get_cpu_gdt_table(cpu);
  445. xmaddr_t maddr = arbitrary_virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
  446. struct multicall_space mc = __xen_mc_entry(0);
  447. MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
  448. }
  449. static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
  450. {
  451. /*
  452. * XXX sleazy hack: If we're being called in a lazy-cpu zone
  453. * and lazy gs handling is enabled, it means we're in a
  454. * context switch, and %gs has just been saved. This means we
  455. * can zero it out to prevent faults on exit from the
  456. * hypervisor if the next process has no %gs. Either way, it
  457. * has been saved, and the new value will get loaded properly.
  458. * This will go away as soon as Xen has been modified to not
  459. * save/restore %gs for normal hypercalls.
  460. *
  461. * On x86_64, this hack is not used for %gs, because gs points
  462. * to KERNEL_GS_BASE (and uses it for PDA references), so we
  463. * must not zero %gs on x86_64
  464. *
  465. * For x86_64, we need to zero %fs, otherwise we may get an
  466. * exception between the new %fs descriptor being loaded and
  467. * %fs being effectively cleared at __switch_to().
  468. */
  469. if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
  470. #ifdef CONFIG_X86_32
  471. lazy_load_gs(0);
  472. #else
  473. loadsegment(fs, 0);
  474. #endif
  475. }
  476. xen_mc_batch();
  477. load_TLS_descriptor(t, cpu, 0);
  478. load_TLS_descriptor(t, cpu, 1);
  479. load_TLS_descriptor(t, cpu, 2);
  480. xen_mc_issue(PARAVIRT_LAZY_CPU);
  481. }
  482. #ifdef CONFIG_X86_64
  483. static void xen_load_gs_index(unsigned int idx)
  484. {
  485. if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
  486. BUG();
  487. }
  488. #endif
  489. static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
  490. const void *ptr)
  491. {
  492. xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
  493. u64 entry = *(u64 *)ptr;
  494. trace_xen_cpu_write_ldt_entry(dt, entrynum, entry);
  495. preempt_disable();
  496. xen_mc_flush();
  497. if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
  498. BUG();
  499. preempt_enable();
  500. }
  501. static int cvt_gate_to_trap(int vector, const gate_desc *val,
  502. struct trap_info *info)
  503. {
  504. unsigned long addr;
  505. if (val->type != GATE_TRAP && val->type != GATE_INTERRUPT)
  506. return 0;
  507. info->vector = vector;
  508. addr = gate_offset(*val);
  509. #ifdef CONFIG_X86_64
  510. /*
  511. * Look for known traps using IST, and substitute them
  512. * appropriately. The debugger ones are the only ones we care
  513. * about. Xen will handle faults like double_fault and
  514. * machine_check, so we should never see them. Warn if
  515. * there's an unexpected IST-using fault handler.
  516. */
  517. if (addr == (unsigned long)debug)
  518. addr = (unsigned long)xen_debug;
  519. else if (addr == (unsigned long)int3)
  520. addr = (unsigned long)xen_int3;
  521. else if (addr == (unsigned long)stack_segment)
  522. addr = (unsigned long)xen_stack_segment;
  523. else if (addr == (unsigned long)double_fault ||
  524. addr == (unsigned long)nmi) {
  525. /* Don't need to handle these */
  526. return 0;
  527. #ifdef CONFIG_X86_MCE
  528. } else if (addr == (unsigned long)machine_check) {
  529. return 0;
  530. #endif
  531. } else {
  532. /* Some other trap using IST? */
  533. if (WARN_ON(val->ist != 0))
  534. return 0;
  535. }
  536. #endif /* CONFIG_X86_64 */
  537. info->address = addr;
  538. info->cs = gate_segment(*val);
  539. info->flags = val->dpl;
  540. /* interrupt gates clear IF */
  541. if (val->type == GATE_INTERRUPT)
  542. info->flags |= 1 << 2;
  543. return 1;
  544. }
  545. /* Locations of each CPU's IDT */
  546. static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
  547. /* Set an IDT entry. If the entry is part of the current IDT, then
  548. also update Xen. */
  549. static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
  550. {
  551. unsigned long p = (unsigned long)&dt[entrynum];
  552. unsigned long start, end;
  553. trace_xen_cpu_write_idt_entry(dt, entrynum, g);
  554. preempt_disable();
  555. start = __this_cpu_read(idt_desc.address);
  556. end = start + __this_cpu_read(idt_desc.size) + 1;
  557. xen_mc_flush();
  558. native_write_idt_entry(dt, entrynum, g);
  559. if (p >= start && (p + 8) <= end) {
  560. struct trap_info info[2];
  561. info[1].address = 0;
  562. if (cvt_gate_to_trap(entrynum, g, &info[0]))
  563. if (HYPERVISOR_set_trap_table(info))
  564. BUG();
  565. }
  566. preempt_enable();
  567. }
  568. static void xen_convert_trap_info(const struct desc_ptr *desc,
  569. struct trap_info *traps)
  570. {
  571. unsigned in, out, count;
  572. count = (desc->size+1) / sizeof(gate_desc);
  573. BUG_ON(count > 256);
  574. for (in = out = 0; in < count; in++) {
  575. gate_desc *entry = (gate_desc*)(desc->address) + in;
  576. if (cvt_gate_to_trap(in, entry, &traps[out]))
  577. out++;
  578. }
  579. traps[out].address = 0;
  580. }
  581. void xen_copy_trap_info(struct trap_info *traps)
  582. {
  583. const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
  584. xen_convert_trap_info(desc, traps);
  585. }
  586. /* Load a new IDT into Xen. In principle this can be per-CPU, so we
  587. hold a spinlock to protect the static traps[] array (static because
  588. it avoids allocation, and saves stack space). */
  589. static void xen_load_idt(const struct desc_ptr *desc)
  590. {
  591. static DEFINE_SPINLOCK(lock);
  592. static struct trap_info traps[257];
  593. trace_xen_cpu_load_idt(desc);
  594. spin_lock(&lock);
  595. __get_cpu_var(idt_desc) = *desc;
  596. xen_convert_trap_info(desc, traps);
  597. xen_mc_flush();
  598. if (HYPERVISOR_set_trap_table(traps))
  599. BUG();
  600. spin_unlock(&lock);
  601. }
  602. /* Write a GDT descriptor entry. Ignore LDT descriptors, since
  603. they're handled differently. */
  604. static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
  605. const void *desc, int type)
  606. {
  607. trace_xen_cpu_write_gdt_entry(dt, entry, desc, type);
  608. preempt_disable();
  609. switch (type) {
  610. case DESC_LDT:
  611. case DESC_TSS:
  612. /* ignore */
  613. break;
  614. default: {
  615. xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
  616. xen_mc_flush();
  617. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  618. BUG();
  619. }
  620. }
  621. preempt_enable();
  622. }
  623. /*
  624. * Version of write_gdt_entry for use at early boot-time needed to
  625. * update an entry as simply as possible.
  626. */
  627. static void __init xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
  628. const void *desc, int type)
  629. {
  630. trace_xen_cpu_write_gdt_entry(dt, entry, desc, type);
  631. switch (type) {
  632. case DESC_LDT:
  633. case DESC_TSS:
  634. /* ignore */
  635. break;
  636. default: {
  637. xmaddr_t maddr = virt_to_machine(&dt[entry]);
  638. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  639. dt[entry] = *(struct desc_struct *)desc;
  640. }
  641. }
  642. }
  643. static void xen_load_sp0(struct tss_struct *tss,
  644. struct thread_struct *thread)
  645. {
  646. struct multicall_space mcs;
  647. mcs = xen_mc_entry(0);
  648. MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
  649. xen_mc_issue(PARAVIRT_LAZY_CPU);
  650. }
  651. static void xen_set_iopl_mask(unsigned mask)
  652. {
  653. struct physdev_set_iopl set_iopl;
  654. /* Force the change at ring 0. */
  655. set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
  656. HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  657. }
  658. static void xen_io_delay(void)
  659. {
  660. }
  661. #ifdef CONFIG_X86_LOCAL_APIC
  662. static unsigned long xen_set_apic_id(unsigned int x)
  663. {
  664. WARN_ON(1);
  665. return x;
  666. }
  667. static unsigned int xen_get_apic_id(unsigned long x)
  668. {
  669. return ((x)>>24) & 0xFFu;
  670. }
  671. static u32 xen_apic_read(u32 reg)
  672. {
  673. struct xen_platform_op op = {
  674. .cmd = XENPF_get_cpuinfo,
  675. .interface_version = XENPF_INTERFACE_VERSION,
  676. .u.pcpu_info.xen_cpuid = 0,
  677. };
  678. int ret = 0;
  679. /* Shouldn't need this as APIC is turned off for PV, and we only
  680. * get called on the bootup processor. But just in case. */
  681. if (!xen_initial_domain() || smp_processor_id())
  682. return 0;
  683. if (reg == APIC_LVR)
  684. return 0x10;
  685. if (reg != APIC_ID)
  686. return 0;
  687. ret = HYPERVISOR_dom0_op(&op);
  688. if (ret)
  689. return 0;
  690. return op.u.pcpu_info.apic_id << 24;
  691. }
  692. static void xen_apic_write(u32 reg, u32 val)
  693. {
  694. /* Warn to see if there's any stray references */
  695. WARN_ON(1);
  696. }
  697. static u64 xen_apic_icr_read(void)
  698. {
  699. return 0;
  700. }
  701. static void xen_apic_icr_write(u32 low, u32 id)
  702. {
  703. /* Warn to see if there's any stray references */
  704. WARN_ON(1);
  705. }
  706. static void xen_apic_wait_icr_idle(void)
  707. {
  708. return;
  709. }
  710. static u32 xen_safe_apic_wait_icr_idle(void)
  711. {
  712. return 0;
  713. }
  714. static void set_xen_basic_apic_ops(void)
  715. {
  716. apic->read = xen_apic_read;
  717. apic->write = xen_apic_write;
  718. apic->icr_read = xen_apic_icr_read;
  719. apic->icr_write = xen_apic_icr_write;
  720. apic->wait_icr_idle = xen_apic_wait_icr_idle;
  721. apic->safe_wait_icr_idle = xen_safe_apic_wait_icr_idle;
  722. apic->set_apic_id = xen_set_apic_id;
  723. apic->get_apic_id = xen_get_apic_id;
  724. #ifdef CONFIG_SMP
  725. apic->send_IPI_allbutself = xen_send_IPI_allbutself;
  726. apic->send_IPI_mask_allbutself = xen_send_IPI_mask_allbutself;
  727. apic->send_IPI_mask = xen_send_IPI_mask;
  728. apic->send_IPI_all = xen_send_IPI_all;
  729. apic->send_IPI_self = xen_send_IPI_self;
  730. #endif
  731. }
  732. #endif
  733. static void xen_clts(void)
  734. {
  735. struct multicall_space mcs;
  736. mcs = xen_mc_entry(0);
  737. MULTI_fpu_taskswitch(mcs.mc, 0);
  738. xen_mc_issue(PARAVIRT_LAZY_CPU);
  739. }
  740. static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
  741. static unsigned long xen_read_cr0(void)
  742. {
  743. unsigned long cr0 = this_cpu_read(xen_cr0_value);
  744. if (unlikely(cr0 == 0)) {
  745. cr0 = native_read_cr0();
  746. this_cpu_write(xen_cr0_value, cr0);
  747. }
  748. return cr0;
  749. }
  750. static void xen_write_cr0(unsigned long cr0)
  751. {
  752. struct multicall_space mcs;
  753. this_cpu_write(xen_cr0_value, cr0);
  754. /* Only pay attention to cr0.TS; everything else is
  755. ignored. */
  756. mcs = xen_mc_entry(0);
  757. MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
  758. xen_mc_issue(PARAVIRT_LAZY_CPU);
  759. }
  760. static void xen_write_cr4(unsigned long cr4)
  761. {
  762. cr4 &= ~X86_CR4_PGE;
  763. cr4 &= ~X86_CR4_PSE;
  764. native_write_cr4(cr4);
  765. }
  766. static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
  767. {
  768. int ret;
  769. ret = 0;
  770. switch (msr) {
  771. #ifdef CONFIG_X86_64
  772. unsigned which;
  773. u64 base;
  774. case MSR_FS_BASE: which = SEGBASE_FS; goto set;
  775. case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
  776. case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
  777. set:
  778. base = ((u64)high << 32) | low;
  779. if (HYPERVISOR_set_segment_base(which, base) != 0)
  780. ret = -EIO;
  781. break;
  782. #endif
  783. case MSR_STAR:
  784. case MSR_CSTAR:
  785. case MSR_LSTAR:
  786. case MSR_SYSCALL_MASK:
  787. case MSR_IA32_SYSENTER_CS:
  788. case MSR_IA32_SYSENTER_ESP:
  789. case MSR_IA32_SYSENTER_EIP:
  790. /* Fast syscall setup is all done in hypercalls, so
  791. these are all ignored. Stub them out here to stop
  792. Xen console noise. */
  793. break;
  794. case MSR_IA32_CR_PAT:
  795. if (smp_processor_id() == 0)
  796. xen_set_pat(((u64)high << 32) | low);
  797. break;
  798. default:
  799. ret = native_write_msr_safe(msr, low, high);
  800. }
  801. return ret;
  802. }
  803. void xen_setup_shared_info(void)
  804. {
  805. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  806. set_fixmap(FIX_PARAVIRT_BOOTMAP,
  807. xen_start_info->shared_info);
  808. HYPERVISOR_shared_info =
  809. (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
  810. } else
  811. HYPERVISOR_shared_info =
  812. (struct shared_info *)__va(xen_start_info->shared_info);
  813. #ifndef CONFIG_SMP
  814. /* In UP this is as good a place as any to set up shared info */
  815. xen_setup_vcpu_info_placement();
  816. #endif
  817. xen_setup_mfn_list_list();
  818. }
  819. /* This is called once we have the cpu_possible_mask */
  820. void xen_setup_vcpu_info_placement(void)
  821. {
  822. int cpu;
  823. for_each_possible_cpu(cpu)
  824. xen_vcpu_setup(cpu);
  825. /* xen_vcpu_setup managed to place the vcpu_info within the
  826. percpu area for all cpus, so make use of it */
  827. if (have_vcpu_info_placement) {
  828. pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
  829. pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
  830. pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
  831. pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
  832. pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
  833. }
  834. }
  835. static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
  836. unsigned long addr, unsigned len)
  837. {
  838. char *start, *end, *reloc;
  839. unsigned ret;
  840. start = end = reloc = NULL;
  841. #define SITE(op, x) \
  842. case PARAVIRT_PATCH(op.x): \
  843. if (have_vcpu_info_placement) { \
  844. start = (char *)xen_##x##_direct; \
  845. end = xen_##x##_direct_end; \
  846. reloc = xen_##x##_direct_reloc; \
  847. } \
  848. goto patch_site
  849. switch (type) {
  850. SITE(pv_irq_ops, irq_enable);
  851. SITE(pv_irq_ops, irq_disable);
  852. SITE(pv_irq_ops, save_fl);
  853. SITE(pv_irq_ops, restore_fl);
  854. #undef SITE
  855. patch_site:
  856. if (start == NULL || (end-start) > len)
  857. goto default_patch;
  858. ret = paravirt_patch_insns(insnbuf, len, start, end);
  859. /* Note: because reloc is assigned from something that
  860. appears to be an array, gcc assumes it's non-null,
  861. but doesn't know its relationship with start and
  862. end. */
  863. if (reloc > start && reloc < end) {
  864. int reloc_off = reloc - start;
  865. long *relocp = (long *)(insnbuf + reloc_off);
  866. long delta = start - (char *)addr;
  867. *relocp += delta;
  868. }
  869. break;
  870. default_patch:
  871. default:
  872. ret = paravirt_patch_default(type, clobbers, insnbuf,
  873. addr, len);
  874. break;
  875. }
  876. return ret;
  877. }
  878. static const struct pv_info xen_info __initconst = {
  879. .paravirt_enabled = 1,
  880. .shared_kernel_pmd = 0,
  881. #ifdef CONFIG_X86_64
  882. .extra_user_64bit_cs = FLAT_USER_CS64,
  883. #endif
  884. .name = "Xen",
  885. };
  886. static const struct pv_init_ops xen_init_ops __initconst = {
  887. .patch = xen_patch,
  888. };
  889. static const struct pv_cpu_ops xen_cpu_ops __initconst = {
  890. .cpuid = xen_cpuid,
  891. .set_debugreg = xen_set_debugreg,
  892. .get_debugreg = xen_get_debugreg,
  893. .clts = xen_clts,
  894. .read_cr0 = xen_read_cr0,
  895. .write_cr0 = xen_write_cr0,
  896. .read_cr4 = native_read_cr4,
  897. .read_cr4_safe = native_read_cr4_safe,
  898. .write_cr4 = xen_write_cr4,
  899. .wbinvd = native_wbinvd,
  900. .read_msr = native_read_msr_safe,
  901. .rdmsr_regs = native_rdmsr_safe_regs,
  902. .write_msr = xen_write_msr_safe,
  903. .wrmsr_regs = native_wrmsr_safe_regs,
  904. .read_tsc = native_read_tsc,
  905. .read_pmc = native_read_pmc,
  906. .iret = xen_iret,
  907. .irq_enable_sysexit = xen_sysexit,
  908. #ifdef CONFIG_X86_64
  909. .usergs_sysret32 = xen_sysret32,
  910. .usergs_sysret64 = xen_sysret64,
  911. #endif
  912. .load_tr_desc = paravirt_nop,
  913. .set_ldt = xen_set_ldt,
  914. .load_gdt = xen_load_gdt,
  915. .load_idt = xen_load_idt,
  916. .load_tls = xen_load_tls,
  917. #ifdef CONFIG_X86_64
  918. .load_gs_index = xen_load_gs_index,
  919. #endif
  920. .alloc_ldt = xen_alloc_ldt,
  921. .free_ldt = xen_free_ldt,
  922. .store_gdt = native_store_gdt,
  923. .store_idt = native_store_idt,
  924. .store_tr = xen_store_tr,
  925. .write_ldt_entry = xen_write_ldt_entry,
  926. .write_gdt_entry = xen_write_gdt_entry,
  927. .write_idt_entry = xen_write_idt_entry,
  928. .load_sp0 = xen_load_sp0,
  929. .set_iopl_mask = xen_set_iopl_mask,
  930. .io_delay = xen_io_delay,
  931. /* Xen takes care of %gs when switching to usermode for us */
  932. .swapgs = paravirt_nop,
  933. .start_context_switch = paravirt_start_context_switch,
  934. .end_context_switch = xen_end_context_switch,
  935. };
  936. static const struct pv_apic_ops xen_apic_ops __initconst = {
  937. #ifdef CONFIG_X86_LOCAL_APIC
  938. .startup_ipi_hook = paravirt_nop,
  939. #endif
  940. };
  941. static void xen_reboot(int reason)
  942. {
  943. struct sched_shutdown r = { .reason = reason };
  944. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
  945. BUG();
  946. }
  947. static void xen_restart(char *msg)
  948. {
  949. xen_reboot(SHUTDOWN_reboot);
  950. }
  951. static void xen_emergency_restart(void)
  952. {
  953. xen_reboot(SHUTDOWN_reboot);
  954. }
  955. static void xen_machine_halt(void)
  956. {
  957. xen_reboot(SHUTDOWN_poweroff);
  958. }
  959. static void xen_machine_power_off(void)
  960. {
  961. if (pm_power_off)
  962. pm_power_off();
  963. xen_reboot(SHUTDOWN_poweroff);
  964. }
  965. static void xen_crash_shutdown(struct pt_regs *regs)
  966. {
  967. xen_reboot(SHUTDOWN_crash);
  968. }
  969. static int
  970. xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
  971. {
  972. xen_reboot(SHUTDOWN_crash);
  973. return NOTIFY_DONE;
  974. }
  975. static struct notifier_block xen_panic_block = {
  976. .notifier_call= xen_panic_event,
  977. };
  978. int xen_panic_handler_init(void)
  979. {
  980. atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block);
  981. return 0;
  982. }
  983. static const struct machine_ops xen_machine_ops __initconst = {
  984. .restart = xen_restart,
  985. .halt = xen_machine_halt,
  986. .power_off = xen_machine_power_off,
  987. .shutdown = xen_machine_halt,
  988. .crash_shutdown = xen_crash_shutdown,
  989. .emergency_restart = xen_emergency_restart,
  990. };
  991. /*
  992. * Set up the GDT and segment registers for -fstack-protector. Until
  993. * we do this, we have to be careful not to call any stack-protected
  994. * function, which is most of the kernel.
  995. */
  996. static void __init xen_setup_stackprotector(void)
  997. {
  998. pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
  999. pv_cpu_ops.load_gdt = xen_load_gdt_boot;
  1000. setup_stack_canary_segment(0);
  1001. switch_to_new_gdt(0);
  1002. pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
  1003. pv_cpu_ops.load_gdt = xen_load_gdt;
  1004. }
  1005. /* First C function to be called on Xen boot */
  1006. asmlinkage void __init xen_start_kernel(void)
  1007. {
  1008. struct physdev_set_iopl set_iopl;
  1009. int rc;
  1010. pgd_t *pgd;
  1011. if (!xen_start_info)
  1012. return;
  1013. xen_domain_type = XEN_PV_DOMAIN;
  1014. xen_setup_machphys_mapping();
  1015. /* Install Xen paravirt ops */
  1016. pv_info = xen_info;
  1017. pv_init_ops = xen_init_ops;
  1018. pv_cpu_ops = xen_cpu_ops;
  1019. pv_apic_ops = xen_apic_ops;
  1020. x86_init.resources.memory_setup = xen_memory_setup;
  1021. x86_init.oem.arch_setup = xen_arch_setup;
  1022. x86_init.oem.banner = xen_banner;
  1023. xen_init_time_ops();
  1024. /*
  1025. * Set up some pagetable state before starting to set any ptes.
  1026. */
  1027. xen_init_mmu_ops();
  1028. /* Prevent unwanted bits from being set in PTEs. */
  1029. __supported_pte_mask &= ~_PAGE_GLOBAL;
  1030. #if 0
  1031. if (!xen_initial_domain())
  1032. #endif
  1033. __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
  1034. __supported_pte_mask |= _PAGE_IOMAP;
  1035. /*
  1036. * Prevent page tables from being allocated in highmem, even
  1037. * if CONFIG_HIGHPTE is enabled.
  1038. */
  1039. __userpte_alloc_gfp &= ~__GFP_HIGHMEM;
  1040. /* Work out if we support NX */
  1041. x86_configure_nx();
  1042. xen_setup_features();
  1043. /* Get mfn list */
  1044. if (!xen_feature(XENFEAT_auto_translated_physmap))
  1045. xen_build_dynamic_phys_to_machine();
  1046. /*
  1047. * Set up kernel GDT and segment registers, mainly so that
  1048. * -fstack-protector code can be executed.
  1049. */
  1050. xen_setup_stackprotector();
  1051. xen_init_irq_ops();
  1052. xen_init_cpuid_mask();
  1053. #ifdef CONFIG_X86_LOCAL_APIC
  1054. /*
  1055. * set up the basic apic ops.
  1056. */
  1057. set_xen_basic_apic_ops();
  1058. #endif
  1059. if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
  1060. pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
  1061. pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
  1062. }
  1063. machine_ops = xen_machine_ops;
  1064. /*
  1065. * The only reliable way to retain the initial address of the
  1066. * percpu gdt_page is to remember it here, so we can go and
  1067. * mark it RW later, when the initial percpu area is freed.
  1068. */
  1069. xen_initial_gdt = &per_cpu(gdt_page, 0);
  1070. xen_smp_init();
  1071. #ifdef CONFIG_ACPI_NUMA
  1072. /*
  1073. * The pages we from Xen are not related to machine pages, so
  1074. * any NUMA information the kernel tries to get from ACPI will
  1075. * be meaningless. Prevent it from trying.
  1076. */
  1077. acpi_numa = -1;
  1078. #endif
  1079. pgd = (pgd_t *)xen_start_info->pt_base;
  1080. /* Don't do the full vcpu_info placement stuff until we have a
  1081. possible map and a non-dummy shared_info. */
  1082. per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
  1083. local_irq_disable();
  1084. early_boot_irqs_disabled = true;
  1085. xen_raw_console_write("mapping kernel into physical memory\n");
  1086. pgd = xen_setup_kernel_pagetable(pgd, xen_start_info->nr_pages);
  1087. /* Allocate and initialize top and mid mfn levels for p2m structure */
  1088. xen_build_mfn_list_list();
  1089. /* keep using Xen gdt for now; no urgent need to change it */
  1090. #ifdef CONFIG_X86_32
  1091. pv_info.kernel_rpl = 1;
  1092. if (xen_feature(XENFEAT_supervisor_mode_kernel))
  1093. pv_info.kernel_rpl = 0;
  1094. #else
  1095. pv_info.kernel_rpl = 0;
  1096. #endif
  1097. /* set the limit of our address space */
  1098. xen_reserve_top();
  1099. /* We used to do this in xen_arch_setup, but that is too late on AMD
  1100. * were early_cpu_init (run before ->arch_setup()) calls early_amd_init
  1101. * 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. new_cpu_data.hard_math = 1;
  1111. new_cpu_data.wp_works_ok = 1;
  1112. new_cpu_data.x86_capability[0] = cpuid_edx(1);
  1113. #endif
  1114. /* Poke various useful things into boot_params */
  1115. boot_params.hdr.type_of_loader = (9 << 4) | 0;
  1116. boot_params.hdr.ramdisk_image = xen_start_info->mod_start
  1117. ? __pa(xen_start_info->mod_start) : 0;
  1118. boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
  1119. boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
  1120. if (!xen_initial_domain()) {
  1121. add_preferred_console("xenboot", 0, NULL);
  1122. add_preferred_console("tty", 0, NULL);
  1123. add_preferred_console("hvc", 0, NULL);
  1124. if (pci_xen)
  1125. x86_init.pci.arch_init = pci_xen_init;
  1126. } else {
  1127. const struct dom0_vga_console_info *info =
  1128. (void *)((char *)xen_start_info +
  1129. xen_start_info->console.dom0.info_off);
  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. xen_init_apic();
  1134. /* Make sure ACS will be enabled */
  1135. pci_request_acs();
  1136. xen_acpi_sleep_register();
  1137. }
  1138. #ifdef CONFIG_PCI
  1139. /* PCI BIOS service won't work from a PV guest. */
  1140. pci_probe &= ~PCI_PROBE_BIOS;
  1141. #endif
  1142. xen_raw_console_write("about to get started...\n");
  1143. xen_setup_runstate_info(0);
  1144. /* Start the world */
  1145. #ifdef CONFIG_X86_32
  1146. i386_start_kernel();
  1147. #else
  1148. x86_64_start_reservations((char *)__pa_symbol(&boot_params));
  1149. #endif
  1150. }
  1151. static int init_hvm_pv_info(int *major, int *minor)
  1152. {
  1153. uint32_t eax, ebx, ecx, edx, pages, msr, base;
  1154. u64 pfn;
  1155. base = xen_cpuid_base();
  1156. cpuid(base + 1, &eax, &ebx, &ecx, &edx);
  1157. *major = eax >> 16;
  1158. *minor = eax & 0xffff;
  1159. printk(KERN_INFO "Xen version %d.%d.\n", *major, *minor);
  1160. cpuid(base + 2, &pages, &msr, &ecx, &edx);
  1161. pfn = __pa(hypercall_page);
  1162. wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32));
  1163. xen_setup_features();
  1164. pv_info.name = "Xen HVM";
  1165. xen_domain_type = XEN_HVM_DOMAIN;
  1166. return 0;
  1167. }
  1168. void __ref xen_hvm_init_shared_info(void)
  1169. {
  1170. int cpu;
  1171. struct xen_add_to_physmap xatp;
  1172. static struct shared_info *shared_info_page = 0;
  1173. if (!shared_info_page)
  1174. shared_info_page = (struct shared_info *)
  1175. extend_brk(PAGE_SIZE, PAGE_SIZE);
  1176. xatp.domid = DOMID_SELF;
  1177. xatp.idx = 0;
  1178. xatp.space = XENMAPSPACE_shared_info;
  1179. xatp.gpfn = __pa(shared_info_page) >> PAGE_SHIFT;
  1180. if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
  1181. BUG();
  1182. HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
  1183. /* xen_vcpu is a pointer to the vcpu_info struct in the shared_info
  1184. * page, we use it in the event channel upcall and in some pvclock
  1185. * related functions. We don't need the vcpu_info placement
  1186. * optimizations because we don't use any pv_mmu or pv_irq op on
  1187. * HVM.
  1188. * When xen_hvm_init_shared_info is run at boot time only vcpu 0 is
  1189. * online but xen_hvm_init_shared_info is run at resume time too and
  1190. * in that case multiple vcpus might be online. */
  1191. for_each_online_cpu(cpu) {
  1192. per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
  1193. }
  1194. }
  1195. #ifdef CONFIG_XEN_PVHVM
  1196. static int __cpuinit xen_hvm_cpu_notify(struct notifier_block *self,
  1197. unsigned long action, void *hcpu)
  1198. {
  1199. int cpu = (long)hcpu;
  1200. switch (action) {
  1201. case CPU_UP_PREPARE:
  1202. xen_vcpu_setup(cpu);
  1203. if (xen_have_vector_callback)
  1204. xen_init_lock_cpu(cpu);
  1205. break;
  1206. default:
  1207. break;
  1208. }
  1209. return NOTIFY_OK;
  1210. }
  1211. static struct notifier_block xen_hvm_cpu_notifier __cpuinitdata = {
  1212. .notifier_call = xen_hvm_cpu_notify,
  1213. };
  1214. static void __init xen_hvm_guest_init(void)
  1215. {
  1216. int r;
  1217. int major, minor;
  1218. r = init_hvm_pv_info(&major, &minor);
  1219. if (r < 0)
  1220. return;
  1221. xen_hvm_init_shared_info();
  1222. if (xen_feature(XENFEAT_hvm_callback_vector))
  1223. xen_have_vector_callback = 1;
  1224. xen_hvm_smp_init();
  1225. register_cpu_notifier(&xen_hvm_cpu_notifier);
  1226. xen_unplug_emulated_devices();
  1227. x86_init.irqs.intr_init = xen_init_IRQ;
  1228. xen_hvm_init_time_ops();
  1229. xen_hvm_init_mmu_ops();
  1230. }
  1231. static bool __init xen_hvm_platform(void)
  1232. {
  1233. if (xen_pv_domain())
  1234. return false;
  1235. if (!xen_cpuid_base())
  1236. return false;
  1237. return true;
  1238. }
  1239. bool xen_hvm_need_lapic(void)
  1240. {
  1241. if (xen_pv_domain())
  1242. return false;
  1243. if (!xen_hvm_domain())
  1244. return false;
  1245. if (xen_feature(XENFEAT_hvm_pirqs) && xen_have_vector_callback)
  1246. return false;
  1247. return true;
  1248. }
  1249. EXPORT_SYMBOL_GPL(xen_hvm_need_lapic);
  1250. const struct hypervisor_x86 x86_hyper_xen_hvm __refconst = {
  1251. .name = "Xen HVM",
  1252. .detect = xen_hvm_platform,
  1253. .init_platform = xen_hvm_guest_init,
  1254. };
  1255. EXPORT_SYMBOL(x86_hyper_xen_hvm);
  1256. #endif