enlighten.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/smp.h>
  16. #include <linux/preempt.h>
  17. #include <linux/hardirq.h>
  18. #include <linux/percpu.h>
  19. #include <linux/delay.h>
  20. #include <linux/start_kernel.h>
  21. #include <linux/sched.h>
  22. #include <linux/kprobes.h>
  23. #include <linux/bootmem.h>
  24. #include <linux/module.h>
  25. #include <linux/mm.h>
  26. #include <linux/page-flags.h>
  27. #include <linux/highmem.h>
  28. #include <linux/console.h>
  29. #include <linux/pci.h>
  30. #include <linux/gfp.h>
  31. #include <xen/xen.h>
  32. #include <xen/interface/xen.h>
  33. #include <xen/interface/version.h>
  34. #include <xen/interface/physdev.h>
  35. #include <xen/interface/vcpu.h>
  36. #include <xen/features.h>
  37. #include <xen/page.h>
  38. #include <xen/hvc-console.h>
  39. #include <asm/paravirt.h>
  40. #include <asm/apic.h>
  41. #include <asm/page.h>
  42. #include <asm/xen/hypercall.h>
  43. #include <asm/xen/hypervisor.h>
  44. #include <asm/fixmap.h>
  45. #include <asm/processor.h>
  46. #include <asm/proto.h>
  47. #include <asm/msr-index.h>
  48. #include <asm/traps.h>
  49. #include <asm/setup.h>
  50. #include <asm/desc.h>
  51. #include <asm/pgalloc.h>
  52. #include <asm/pgtable.h>
  53. #include <asm/tlbflush.h>
  54. #include <asm/reboot.h>
  55. #include <asm/stackprotector.h>
  56. #include "xen-ops.h"
  57. #include "mmu.h"
  58. #include "multicalls.h"
  59. EXPORT_SYMBOL_GPL(hypercall_page);
  60. DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
  61. DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
  62. enum xen_domain_type xen_domain_type = XEN_NATIVE;
  63. EXPORT_SYMBOL_GPL(xen_domain_type);
  64. struct start_info *xen_start_info;
  65. EXPORT_SYMBOL_GPL(xen_start_info);
  66. struct shared_info xen_dummy_shared_info;
  67. void *xen_initial_gdt;
  68. /*
  69. * Point at some empty memory to start with. We map the real shared_info
  70. * page as soon as fixmap is up and running.
  71. */
  72. struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
  73. /*
  74. * Flag to determine whether vcpu info placement is available on all
  75. * VCPUs. We assume it is to start with, and then set it to zero on
  76. * the first failure. This is because it can succeed on some VCPUs
  77. * and not others, since it can involve hypervisor memory allocation,
  78. * or because the guest failed to guarantee all the appropriate
  79. * constraints on all VCPUs (ie buffer can't cross a page boundary).
  80. *
  81. * Note that any particular CPU may be using a placed vcpu structure,
  82. * but we can only optimise if the all are.
  83. *
  84. * 0: not available, 1: available
  85. */
  86. static int have_vcpu_info_placement = 1;
  87. static void xen_vcpu_setup(int cpu)
  88. {
  89. struct vcpu_register_vcpu_info info;
  90. int err;
  91. struct vcpu_info *vcpup;
  92. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  93. per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
  94. if (!have_vcpu_info_placement)
  95. return; /* already tested, not available */
  96. vcpup = &per_cpu(xen_vcpu_info, cpu);
  97. info.mfn = arbitrary_virt_to_mfn(vcpup);
  98. info.offset = offset_in_page(vcpup);
  99. printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %llx, offset %d\n",
  100. cpu, vcpup, info.mfn, info.offset);
  101. /* Check to see if the hypervisor will put the vcpu_info
  102. structure where we want it, which allows direct access via
  103. a percpu-variable. */
  104. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
  105. if (err) {
  106. printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
  107. have_vcpu_info_placement = 0;
  108. } else {
  109. /* This cpu is using the registered vcpu info, even if
  110. later ones fail to. */
  111. per_cpu(xen_vcpu, cpu) = vcpup;
  112. printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
  113. cpu, vcpup);
  114. }
  115. }
  116. /*
  117. * On restore, set the vcpu placement up again.
  118. * If it fails, then we're in a bad state, since
  119. * we can't back out from using it...
  120. */
  121. void xen_vcpu_restore(void)
  122. {
  123. int cpu;
  124. for_each_online_cpu(cpu) {
  125. bool other_cpu = (cpu != smp_processor_id());
  126. if (other_cpu &&
  127. HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
  128. BUG();
  129. xen_setup_runstate_info(cpu);
  130. if (have_vcpu_info_placement)
  131. xen_vcpu_setup(cpu);
  132. if (other_cpu &&
  133. HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
  134. BUG();
  135. }
  136. }
  137. static void __init xen_banner(void)
  138. {
  139. unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
  140. struct xen_extraversion extra;
  141. HYPERVISOR_xen_version(XENVER_extraversion, &extra);
  142. printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
  143. pv_info.name);
  144. printk(KERN_INFO "Xen version: %d.%d%s%s\n",
  145. version >> 16, version & 0xffff, extra.extraversion,
  146. xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
  147. }
  148. static __read_mostly unsigned int cpuid_leaf1_edx_mask = ~0;
  149. static __read_mostly unsigned int cpuid_leaf1_ecx_mask = ~0;
  150. static void xen_cpuid(unsigned int *ax, unsigned int *bx,
  151. unsigned int *cx, unsigned int *dx)
  152. {
  153. unsigned maskebx = ~0;
  154. unsigned maskecx = ~0;
  155. unsigned maskedx = ~0;
  156. /*
  157. * Mask out inconvenient features, to try and disable as many
  158. * unsupported kernel subsystems as possible.
  159. */
  160. switch (*ax) {
  161. case 1:
  162. maskecx = cpuid_leaf1_ecx_mask;
  163. maskedx = cpuid_leaf1_edx_mask;
  164. break;
  165. case 0xb:
  166. /* Suppress extended topology stuff */
  167. maskebx = 0;
  168. break;
  169. }
  170. asm(XEN_EMULATE_PREFIX "cpuid"
  171. : "=a" (*ax),
  172. "=b" (*bx),
  173. "=c" (*cx),
  174. "=d" (*dx)
  175. : "0" (*ax), "2" (*cx));
  176. *bx &= maskebx;
  177. *cx &= maskecx;
  178. *dx &= maskedx;
  179. }
  180. static __init void xen_init_cpuid_mask(void)
  181. {
  182. unsigned int ax, bx, cx, dx;
  183. cpuid_leaf1_edx_mask =
  184. ~((1 << X86_FEATURE_MCE) | /* disable MCE */
  185. (1 << X86_FEATURE_MCA) | /* disable MCA */
  186. (1 << X86_FEATURE_ACC)); /* thermal monitoring */
  187. if (!xen_initial_domain())
  188. cpuid_leaf1_edx_mask &=
  189. ~((1 << X86_FEATURE_APIC) | /* disable local APIC */
  190. (1 << X86_FEATURE_ACPI)); /* disable ACPI */
  191. ax = 1;
  192. cx = 0;
  193. xen_cpuid(&ax, &bx, &cx, &dx);
  194. /* cpuid claims we support xsave; try enabling it to see what happens */
  195. if (cx & (1 << (X86_FEATURE_XSAVE % 32))) {
  196. unsigned long cr4;
  197. set_in_cr4(X86_CR4_OSXSAVE);
  198. cr4 = read_cr4();
  199. if ((cr4 & X86_CR4_OSXSAVE) == 0)
  200. cpuid_leaf1_ecx_mask &= ~(1 << (X86_FEATURE_XSAVE % 32));
  201. clear_in_cr4(X86_CR4_OSXSAVE);
  202. }
  203. }
  204. static void xen_set_debugreg(int reg, unsigned long val)
  205. {
  206. HYPERVISOR_set_debugreg(reg, val);
  207. }
  208. static unsigned long xen_get_debugreg(int reg)
  209. {
  210. return HYPERVISOR_get_debugreg(reg);
  211. }
  212. static void xen_end_context_switch(struct task_struct *next)
  213. {
  214. xen_mc_flush();
  215. paravirt_end_context_switch(next);
  216. }
  217. static unsigned long xen_store_tr(void)
  218. {
  219. return 0;
  220. }
  221. /*
  222. * Set the page permissions for a particular virtual address. If the
  223. * address is a vmalloc mapping (or other non-linear mapping), then
  224. * find the linear mapping of the page and also set its protections to
  225. * match.
  226. */
  227. static void set_aliased_prot(void *v, pgprot_t prot)
  228. {
  229. int level;
  230. pte_t *ptep;
  231. pte_t pte;
  232. unsigned long pfn;
  233. struct page *page;
  234. ptep = lookup_address((unsigned long)v, &level);
  235. BUG_ON(ptep == NULL);
  236. pfn = pte_pfn(*ptep);
  237. page = pfn_to_page(pfn);
  238. pte = pfn_pte(pfn, prot);
  239. if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
  240. BUG();
  241. if (!PageHighMem(page)) {
  242. void *av = __va(PFN_PHYS(pfn));
  243. if (av != v)
  244. if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
  245. BUG();
  246. } else
  247. kmap_flush_unused();
  248. }
  249. static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
  250. {
  251. const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
  252. int i;
  253. for(i = 0; i < entries; i += entries_per_page)
  254. set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
  255. }
  256. static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
  257. {
  258. const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
  259. int i;
  260. for(i = 0; i < entries; i += entries_per_page)
  261. set_aliased_prot(ldt + i, PAGE_KERNEL);
  262. }
  263. static void xen_set_ldt(const void *addr, unsigned entries)
  264. {
  265. struct mmuext_op *op;
  266. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  267. op = mcs.args;
  268. op->cmd = MMUEXT_SET_LDT;
  269. op->arg1.linear_addr = (unsigned long)addr;
  270. op->arg2.nr_ents = entries;
  271. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  272. xen_mc_issue(PARAVIRT_LAZY_CPU);
  273. }
  274. static void xen_load_gdt(const struct desc_ptr *dtr)
  275. {
  276. unsigned long va = dtr->address;
  277. unsigned int size = dtr->size + 1;
  278. unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  279. unsigned long frames[pages];
  280. int f;
  281. /*
  282. * A GDT can be up to 64k in size, which corresponds to 8192
  283. * 8-byte entries, or 16 4k pages..
  284. */
  285. BUG_ON(size > 65536);
  286. BUG_ON(va & ~PAGE_MASK);
  287. for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
  288. int level;
  289. pte_t *ptep;
  290. unsigned long pfn, mfn;
  291. void *virt;
  292. /*
  293. * The GDT is per-cpu and is in the percpu data area.
  294. * That can be virtually mapped, so we need to do a
  295. * page-walk to get the underlying MFN for the
  296. * hypercall. The page can also be in the kernel's
  297. * linear range, so we need to RO that mapping too.
  298. */
  299. ptep = lookup_address(va, &level);
  300. BUG_ON(ptep == NULL);
  301. pfn = pte_pfn(*ptep);
  302. mfn = pfn_to_mfn(pfn);
  303. virt = __va(PFN_PHYS(pfn));
  304. frames[f] = mfn;
  305. make_lowmem_page_readonly((void *)va);
  306. make_lowmem_page_readonly(virt);
  307. }
  308. if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
  309. BUG();
  310. }
  311. /*
  312. * load_gdt for early boot, when the gdt is only mapped once
  313. */
  314. static __init void xen_load_gdt_boot(const struct desc_ptr *dtr)
  315. {
  316. unsigned long va = dtr->address;
  317. unsigned int size = dtr->size + 1;
  318. unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  319. unsigned long frames[pages];
  320. int f;
  321. /*
  322. * A GDT can be up to 64k in size, which corresponds to 8192
  323. * 8-byte entries, or 16 4k pages..
  324. */
  325. BUG_ON(size > 65536);
  326. BUG_ON(va & ~PAGE_MASK);
  327. for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
  328. pte_t pte;
  329. unsigned long pfn, mfn;
  330. pfn = virt_to_pfn(va);
  331. mfn = pfn_to_mfn(pfn);
  332. pte = pfn_pte(pfn, PAGE_KERNEL_RO);
  333. if (HYPERVISOR_update_va_mapping((unsigned long)va, pte, 0))
  334. BUG();
  335. frames[f] = mfn;
  336. }
  337. if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
  338. BUG();
  339. }
  340. static void load_TLS_descriptor(struct thread_struct *t,
  341. unsigned int cpu, unsigned int i)
  342. {
  343. struct desc_struct *gdt = get_cpu_gdt_table(cpu);
  344. xmaddr_t maddr = arbitrary_virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
  345. struct multicall_space mc = __xen_mc_entry(0);
  346. MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
  347. }
  348. static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
  349. {
  350. /*
  351. * XXX sleazy hack: If we're being called in a lazy-cpu zone
  352. * and lazy gs handling is enabled, it means we're in a
  353. * context switch, and %gs has just been saved. This means we
  354. * can zero it out to prevent faults on exit from the
  355. * hypervisor if the next process has no %gs. Either way, it
  356. * has been saved, and the new value will get loaded properly.
  357. * This will go away as soon as Xen has been modified to not
  358. * save/restore %gs for normal hypercalls.
  359. *
  360. * On x86_64, this hack is not used for %gs, because gs points
  361. * to KERNEL_GS_BASE (and uses it for PDA references), so we
  362. * must not zero %gs on x86_64
  363. *
  364. * For x86_64, we need to zero %fs, otherwise we may get an
  365. * exception between the new %fs descriptor being loaded and
  366. * %fs being effectively cleared at __switch_to().
  367. */
  368. if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
  369. #ifdef CONFIG_X86_32
  370. lazy_load_gs(0);
  371. #else
  372. loadsegment(fs, 0);
  373. #endif
  374. }
  375. xen_mc_batch();
  376. load_TLS_descriptor(t, cpu, 0);
  377. load_TLS_descriptor(t, cpu, 1);
  378. load_TLS_descriptor(t, cpu, 2);
  379. xen_mc_issue(PARAVIRT_LAZY_CPU);
  380. }
  381. #ifdef CONFIG_X86_64
  382. static void xen_load_gs_index(unsigned int idx)
  383. {
  384. if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
  385. BUG();
  386. }
  387. #endif
  388. static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
  389. const void *ptr)
  390. {
  391. xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
  392. u64 entry = *(u64 *)ptr;
  393. preempt_disable();
  394. xen_mc_flush();
  395. if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
  396. BUG();
  397. preempt_enable();
  398. }
  399. static int cvt_gate_to_trap(int vector, const gate_desc *val,
  400. struct trap_info *info)
  401. {
  402. unsigned long addr;
  403. if (val->type != GATE_TRAP && val->type != GATE_INTERRUPT)
  404. return 0;
  405. info->vector = vector;
  406. addr = gate_offset(*val);
  407. #ifdef CONFIG_X86_64
  408. /*
  409. * Look for known traps using IST, and substitute them
  410. * appropriately. The debugger ones are the only ones we care
  411. * about. Xen will handle faults like double_fault and
  412. * machine_check, so we should never see them. Warn if
  413. * there's an unexpected IST-using fault handler.
  414. */
  415. if (addr == (unsigned long)debug)
  416. addr = (unsigned long)xen_debug;
  417. else if (addr == (unsigned long)int3)
  418. addr = (unsigned long)xen_int3;
  419. else if (addr == (unsigned long)stack_segment)
  420. addr = (unsigned long)xen_stack_segment;
  421. else if (addr == (unsigned long)double_fault ||
  422. addr == (unsigned long)nmi) {
  423. /* Don't need to handle these */
  424. return 0;
  425. #ifdef CONFIG_X86_MCE
  426. } else if (addr == (unsigned long)machine_check) {
  427. return 0;
  428. #endif
  429. } else {
  430. /* Some other trap using IST? */
  431. if (WARN_ON(val->ist != 0))
  432. return 0;
  433. }
  434. #endif /* CONFIG_X86_64 */
  435. info->address = addr;
  436. info->cs = gate_segment(*val);
  437. info->flags = val->dpl;
  438. /* interrupt gates clear IF */
  439. if (val->type == GATE_INTERRUPT)
  440. info->flags |= 1 << 2;
  441. return 1;
  442. }
  443. /* Locations of each CPU's IDT */
  444. static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
  445. /* Set an IDT entry. If the entry is part of the current IDT, then
  446. also update Xen. */
  447. static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
  448. {
  449. unsigned long p = (unsigned long)&dt[entrynum];
  450. unsigned long start, end;
  451. preempt_disable();
  452. start = __get_cpu_var(idt_desc).address;
  453. end = start + __get_cpu_var(idt_desc).size + 1;
  454. xen_mc_flush();
  455. native_write_idt_entry(dt, entrynum, g);
  456. if (p >= start && (p + 8) <= end) {
  457. struct trap_info info[2];
  458. info[1].address = 0;
  459. if (cvt_gate_to_trap(entrynum, g, &info[0]))
  460. if (HYPERVISOR_set_trap_table(info))
  461. BUG();
  462. }
  463. preempt_enable();
  464. }
  465. static void xen_convert_trap_info(const struct desc_ptr *desc,
  466. struct trap_info *traps)
  467. {
  468. unsigned in, out, count;
  469. count = (desc->size+1) / sizeof(gate_desc);
  470. BUG_ON(count > 256);
  471. for (in = out = 0; in < count; in++) {
  472. gate_desc *entry = (gate_desc*)(desc->address) + in;
  473. if (cvt_gate_to_trap(in, entry, &traps[out]))
  474. out++;
  475. }
  476. traps[out].address = 0;
  477. }
  478. void xen_copy_trap_info(struct trap_info *traps)
  479. {
  480. const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
  481. xen_convert_trap_info(desc, traps);
  482. }
  483. /* Load a new IDT into Xen. In principle this can be per-CPU, so we
  484. hold a spinlock to protect the static traps[] array (static because
  485. it avoids allocation, and saves stack space). */
  486. static void xen_load_idt(const struct desc_ptr *desc)
  487. {
  488. static DEFINE_SPINLOCK(lock);
  489. static struct trap_info traps[257];
  490. spin_lock(&lock);
  491. __get_cpu_var(idt_desc) = *desc;
  492. xen_convert_trap_info(desc, traps);
  493. xen_mc_flush();
  494. if (HYPERVISOR_set_trap_table(traps))
  495. BUG();
  496. spin_unlock(&lock);
  497. }
  498. /* Write a GDT descriptor entry. Ignore LDT descriptors, since
  499. they're handled differently. */
  500. static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
  501. const void *desc, int type)
  502. {
  503. preempt_disable();
  504. switch (type) {
  505. case DESC_LDT:
  506. case DESC_TSS:
  507. /* ignore */
  508. break;
  509. default: {
  510. xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
  511. xen_mc_flush();
  512. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  513. BUG();
  514. }
  515. }
  516. preempt_enable();
  517. }
  518. /*
  519. * Version of write_gdt_entry for use at early boot-time needed to
  520. * update an entry as simply as possible.
  521. */
  522. static __init void xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
  523. const void *desc, int type)
  524. {
  525. switch (type) {
  526. case DESC_LDT:
  527. case DESC_TSS:
  528. /* ignore */
  529. break;
  530. default: {
  531. xmaddr_t maddr = virt_to_machine(&dt[entry]);
  532. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  533. dt[entry] = *(struct desc_struct *)desc;
  534. }
  535. }
  536. }
  537. static void xen_load_sp0(struct tss_struct *tss,
  538. struct thread_struct *thread)
  539. {
  540. struct multicall_space mcs = xen_mc_entry(0);
  541. MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
  542. xen_mc_issue(PARAVIRT_LAZY_CPU);
  543. }
  544. static void xen_set_iopl_mask(unsigned mask)
  545. {
  546. struct physdev_set_iopl set_iopl;
  547. /* Force the change at ring 0. */
  548. set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
  549. HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  550. }
  551. static void xen_io_delay(void)
  552. {
  553. }
  554. #ifdef CONFIG_X86_LOCAL_APIC
  555. static u32 xen_apic_read(u32 reg)
  556. {
  557. return 0;
  558. }
  559. static void xen_apic_write(u32 reg, u32 val)
  560. {
  561. /* Warn to see if there's any stray references */
  562. WARN_ON(1);
  563. }
  564. static u64 xen_apic_icr_read(void)
  565. {
  566. return 0;
  567. }
  568. static void xen_apic_icr_write(u32 low, u32 id)
  569. {
  570. /* Warn to see if there's any stray references */
  571. WARN_ON(1);
  572. }
  573. static void xen_apic_wait_icr_idle(void)
  574. {
  575. return;
  576. }
  577. static u32 xen_safe_apic_wait_icr_idle(void)
  578. {
  579. return 0;
  580. }
  581. static void set_xen_basic_apic_ops(void)
  582. {
  583. apic->read = xen_apic_read;
  584. apic->write = xen_apic_write;
  585. apic->icr_read = xen_apic_icr_read;
  586. apic->icr_write = xen_apic_icr_write;
  587. apic->wait_icr_idle = xen_apic_wait_icr_idle;
  588. apic->safe_wait_icr_idle = xen_safe_apic_wait_icr_idle;
  589. }
  590. #endif
  591. static void xen_clts(void)
  592. {
  593. struct multicall_space mcs;
  594. mcs = xen_mc_entry(0);
  595. MULTI_fpu_taskswitch(mcs.mc, 0);
  596. xen_mc_issue(PARAVIRT_LAZY_CPU);
  597. }
  598. static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
  599. static unsigned long xen_read_cr0(void)
  600. {
  601. unsigned long cr0 = percpu_read(xen_cr0_value);
  602. if (unlikely(cr0 == 0)) {
  603. cr0 = native_read_cr0();
  604. percpu_write(xen_cr0_value, cr0);
  605. }
  606. return cr0;
  607. }
  608. static void xen_write_cr0(unsigned long cr0)
  609. {
  610. struct multicall_space mcs;
  611. percpu_write(xen_cr0_value, cr0);
  612. /* Only pay attention to cr0.TS; everything else is
  613. ignored. */
  614. mcs = xen_mc_entry(0);
  615. MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
  616. xen_mc_issue(PARAVIRT_LAZY_CPU);
  617. }
  618. static void xen_write_cr4(unsigned long cr4)
  619. {
  620. cr4 &= ~X86_CR4_PGE;
  621. cr4 &= ~X86_CR4_PSE;
  622. native_write_cr4(cr4);
  623. }
  624. static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
  625. {
  626. int ret;
  627. ret = 0;
  628. switch (msr) {
  629. #ifdef CONFIG_X86_64
  630. unsigned which;
  631. u64 base;
  632. case MSR_FS_BASE: which = SEGBASE_FS; goto set;
  633. case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
  634. case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
  635. set:
  636. base = ((u64)high << 32) | low;
  637. if (HYPERVISOR_set_segment_base(which, base) != 0)
  638. ret = -EIO;
  639. break;
  640. #endif
  641. case MSR_STAR:
  642. case MSR_CSTAR:
  643. case MSR_LSTAR:
  644. case MSR_SYSCALL_MASK:
  645. case MSR_IA32_SYSENTER_CS:
  646. case MSR_IA32_SYSENTER_ESP:
  647. case MSR_IA32_SYSENTER_EIP:
  648. /* Fast syscall setup is all done in hypercalls, so
  649. these are all ignored. Stub them out here to stop
  650. Xen console noise. */
  651. break;
  652. default:
  653. ret = native_write_msr_safe(msr, low, high);
  654. }
  655. return ret;
  656. }
  657. void xen_setup_shared_info(void)
  658. {
  659. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  660. set_fixmap(FIX_PARAVIRT_BOOTMAP,
  661. xen_start_info->shared_info);
  662. HYPERVISOR_shared_info =
  663. (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
  664. } else
  665. HYPERVISOR_shared_info =
  666. (struct shared_info *)__va(xen_start_info->shared_info);
  667. #ifndef CONFIG_SMP
  668. /* In UP this is as good a place as any to set up shared info */
  669. xen_setup_vcpu_info_placement();
  670. #endif
  671. xen_setup_mfn_list_list();
  672. }
  673. /* This is called once we have the cpu_possible_map */
  674. void xen_setup_vcpu_info_placement(void)
  675. {
  676. int cpu;
  677. for_each_possible_cpu(cpu)
  678. xen_vcpu_setup(cpu);
  679. /* xen_vcpu_setup managed to place the vcpu_info within the
  680. percpu area for all cpus, so make use of it */
  681. if (have_vcpu_info_placement) {
  682. printk(KERN_INFO "Xen: using vcpu_info placement\n");
  683. pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
  684. pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
  685. pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
  686. pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
  687. pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
  688. }
  689. }
  690. static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
  691. unsigned long addr, unsigned len)
  692. {
  693. char *start, *end, *reloc;
  694. unsigned ret;
  695. start = end = reloc = NULL;
  696. #define SITE(op, x) \
  697. case PARAVIRT_PATCH(op.x): \
  698. if (have_vcpu_info_placement) { \
  699. start = (char *)xen_##x##_direct; \
  700. end = xen_##x##_direct_end; \
  701. reloc = xen_##x##_direct_reloc; \
  702. } \
  703. goto patch_site
  704. switch (type) {
  705. SITE(pv_irq_ops, irq_enable);
  706. SITE(pv_irq_ops, irq_disable);
  707. SITE(pv_irq_ops, save_fl);
  708. SITE(pv_irq_ops, restore_fl);
  709. #undef SITE
  710. patch_site:
  711. if (start == NULL || (end-start) > len)
  712. goto default_patch;
  713. ret = paravirt_patch_insns(insnbuf, len, start, end);
  714. /* Note: because reloc is assigned from something that
  715. appears to be an array, gcc assumes it's non-null,
  716. but doesn't know its relationship with start and
  717. end. */
  718. if (reloc > start && reloc < end) {
  719. int reloc_off = reloc - start;
  720. long *relocp = (long *)(insnbuf + reloc_off);
  721. long delta = start - (char *)addr;
  722. *relocp += delta;
  723. }
  724. break;
  725. default_patch:
  726. default:
  727. ret = paravirt_patch_default(type, clobbers, insnbuf,
  728. addr, len);
  729. break;
  730. }
  731. return ret;
  732. }
  733. static const struct pv_info xen_info __initdata = {
  734. .paravirt_enabled = 1,
  735. .shared_kernel_pmd = 0,
  736. .name = "Xen",
  737. };
  738. static const struct pv_init_ops xen_init_ops __initdata = {
  739. .patch = xen_patch,
  740. };
  741. static const struct pv_time_ops xen_time_ops __initdata = {
  742. .sched_clock = xen_sched_clock,
  743. };
  744. static const struct pv_cpu_ops xen_cpu_ops __initdata = {
  745. .cpuid = xen_cpuid,
  746. .set_debugreg = xen_set_debugreg,
  747. .get_debugreg = xen_get_debugreg,
  748. .clts = xen_clts,
  749. .read_cr0 = xen_read_cr0,
  750. .write_cr0 = xen_write_cr0,
  751. .read_cr4 = native_read_cr4,
  752. .read_cr4_safe = native_read_cr4_safe,
  753. .write_cr4 = xen_write_cr4,
  754. .wbinvd = native_wbinvd,
  755. .read_msr = native_read_msr_safe,
  756. .write_msr = xen_write_msr_safe,
  757. .read_tsc = native_read_tsc,
  758. .read_pmc = native_read_pmc,
  759. .iret = xen_iret,
  760. .irq_enable_sysexit = xen_sysexit,
  761. #ifdef CONFIG_X86_64
  762. .usergs_sysret32 = xen_sysret32,
  763. .usergs_sysret64 = xen_sysret64,
  764. #endif
  765. .load_tr_desc = paravirt_nop,
  766. .set_ldt = xen_set_ldt,
  767. .load_gdt = xen_load_gdt,
  768. .load_idt = xen_load_idt,
  769. .load_tls = xen_load_tls,
  770. #ifdef CONFIG_X86_64
  771. .load_gs_index = xen_load_gs_index,
  772. #endif
  773. .alloc_ldt = xen_alloc_ldt,
  774. .free_ldt = xen_free_ldt,
  775. .store_gdt = native_store_gdt,
  776. .store_idt = native_store_idt,
  777. .store_tr = xen_store_tr,
  778. .write_ldt_entry = xen_write_ldt_entry,
  779. .write_gdt_entry = xen_write_gdt_entry,
  780. .write_idt_entry = xen_write_idt_entry,
  781. .load_sp0 = xen_load_sp0,
  782. .set_iopl_mask = xen_set_iopl_mask,
  783. .io_delay = xen_io_delay,
  784. /* Xen takes care of %gs when switching to usermode for us */
  785. .swapgs = paravirt_nop,
  786. .start_context_switch = paravirt_start_context_switch,
  787. .end_context_switch = xen_end_context_switch,
  788. };
  789. static const struct pv_apic_ops xen_apic_ops __initdata = {
  790. #ifdef CONFIG_X86_LOCAL_APIC
  791. .startup_ipi_hook = paravirt_nop,
  792. #endif
  793. };
  794. static void xen_reboot(int reason)
  795. {
  796. struct sched_shutdown r = { .reason = reason };
  797. #ifdef CONFIG_SMP
  798. smp_send_stop();
  799. #endif
  800. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
  801. BUG();
  802. }
  803. static void xen_restart(char *msg)
  804. {
  805. xen_reboot(SHUTDOWN_reboot);
  806. }
  807. static void xen_emergency_restart(void)
  808. {
  809. xen_reboot(SHUTDOWN_reboot);
  810. }
  811. static void xen_machine_halt(void)
  812. {
  813. xen_reboot(SHUTDOWN_poweroff);
  814. }
  815. static void xen_crash_shutdown(struct pt_regs *regs)
  816. {
  817. xen_reboot(SHUTDOWN_crash);
  818. }
  819. static const struct machine_ops __initdata xen_machine_ops = {
  820. .restart = xen_restart,
  821. .halt = xen_machine_halt,
  822. .power_off = xen_machine_halt,
  823. .shutdown = xen_machine_halt,
  824. .crash_shutdown = xen_crash_shutdown,
  825. .emergency_restart = xen_emergency_restart,
  826. };
  827. /*
  828. * Set up the GDT and segment registers for -fstack-protector. Until
  829. * we do this, we have to be careful not to call any stack-protected
  830. * function, which is most of the kernel.
  831. */
  832. static void __init xen_setup_stackprotector(void)
  833. {
  834. pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
  835. pv_cpu_ops.load_gdt = xen_load_gdt_boot;
  836. setup_stack_canary_segment(0);
  837. switch_to_new_gdt(0);
  838. pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
  839. pv_cpu_ops.load_gdt = xen_load_gdt;
  840. }
  841. /* First C function to be called on Xen boot */
  842. asmlinkage void __init xen_start_kernel(void)
  843. {
  844. pgd_t *pgd;
  845. if (!xen_start_info)
  846. return;
  847. xen_domain_type = XEN_PV_DOMAIN;
  848. /* Install Xen paravirt ops */
  849. pv_info = xen_info;
  850. pv_init_ops = xen_init_ops;
  851. pv_time_ops = xen_time_ops;
  852. pv_cpu_ops = xen_cpu_ops;
  853. pv_apic_ops = xen_apic_ops;
  854. x86_init.resources.memory_setup = xen_memory_setup;
  855. x86_init.oem.arch_setup = xen_arch_setup;
  856. x86_init.oem.banner = xen_banner;
  857. x86_init.timers.timer_init = xen_time_init;
  858. x86_init.timers.setup_percpu_clockev = x86_init_noop;
  859. x86_cpuinit.setup_percpu_clockev = x86_init_noop;
  860. x86_platform.calibrate_tsc = xen_tsc_khz;
  861. x86_platform.get_wallclock = xen_get_wallclock;
  862. x86_platform.set_wallclock = xen_set_wallclock;
  863. /*
  864. * Set up some pagetable state before starting to set any ptes.
  865. */
  866. xen_init_mmu_ops();
  867. /* Prevent unwanted bits from being set in PTEs. */
  868. __supported_pte_mask &= ~_PAGE_GLOBAL;
  869. if (!xen_initial_domain())
  870. __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
  871. __supported_pte_mask |= _PAGE_IOMAP;
  872. /*
  873. * Prevent page tables from being allocated in highmem, even
  874. * if CONFIG_HIGHPTE is enabled.
  875. */
  876. __userpte_alloc_gfp &= ~__GFP_HIGHMEM;
  877. /* Work out if we support NX */
  878. x86_configure_nx();
  879. xen_setup_features();
  880. /* Get mfn list */
  881. if (!xen_feature(XENFEAT_auto_translated_physmap))
  882. xen_build_dynamic_phys_to_machine();
  883. /*
  884. * Set up kernel GDT and segment registers, mainly so that
  885. * -fstack-protector code can be executed.
  886. */
  887. xen_setup_stackprotector();
  888. xen_init_irq_ops();
  889. xen_init_cpuid_mask();
  890. #ifdef CONFIG_X86_LOCAL_APIC
  891. /*
  892. * set up the basic apic ops.
  893. */
  894. set_xen_basic_apic_ops();
  895. #endif
  896. if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
  897. pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
  898. pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
  899. }
  900. machine_ops = xen_machine_ops;
  901. /*
  902. * The only reliable way to retain the initial address of the
  903. * percpu gdt_page is to remember it here, so we can go and
  904. * mark it RW later, when the initial percpu area is freed.
  905. */
  906. xen_initial_gdt = &per_cpu(gdt_page, 0);
  907. xen_smp_init();
  908. pgd = (pgd_t *)xen_start_info->pt_base;
  909. /* Don't do the full vcpu_info placement stuff until we have a
  910. possible map and a non-dummy shared_info. */
  911. per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
  912. local_irq_disable();
  913. early_boot_irqs_off();
  914. xen_raw_console_write("mapping kernel into physical memory\n");
  915. pgd = xen_setup_kernel_pagetable(pgd, xen_start_info->nr_pages);
  916. init_mm.pgd = pgd;
  917. /* keep using Xen gdt for now; no urgent need to change it */
  918. #ifdef CONFIG_X86_32
  919. pv_info.kernel_rpl = 1;
  920. if (xen_feature(XENFEAT_supervisor_mode_kernel))
  921. pv_info.kernel_rpl = 0;
  922. #else
  923. pv_info.kernel_rpl = 0;
  924. #endif
  925. /* set the limit of our address space */
  926. xen_reserve_top();
  927. #ifdef CONFIG_X86_32
  928. /* set up basic CPUID stuff */
  929. cpu_detect(&new_cpu_data);
  930. new_cpu_data.hard_math = 1;
  931. new_cpu_data.wp_works_ok = 1;
  932. new_cpu_data.x86_capability[0] = cpuid_edx(1);
  933. #endif
  934. /* Poke various useful things into boot_params */
  935. boot_params.hdr.type_of_loader = (9 << 4) | 0;
  936. boot_params.hdr.ramdisk_image = xen_start_info->mod_start
  937. ? __pa(xen_start_info->mod_start) : 0;
  938. boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
  939. boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
  940. if (!xen_initial_domain()) {
  941. add_preferred_console("xenboot", 0, NULL);
  942. add_preferred_console("tty", 0, NULL);
  943. add_preferred_console("hvc", 0, NULL);
  944. } else {
  945. /* Make sure ACS will be enabled */
  946. pci_request_acs();
  947. }
  948. xen_raw_console_write("about to get started...\n");
  949. xen_setup_runstate_info(0);
  950. /* Start the world */
  951. #ifdef CONFIG_X86_32
  952. i386_start_kernel();
  953. #else
  954. x86_64_start_reservations((char *)__pa_symbol(&boot_params));
  955. #endif
  956. }