enlighten.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  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/bootmem.h>
  23. #include <linux/module.h>
  24. #include <linux/mm.h>
  25. #include <linux/page-flags.h>
  26. #include <linux/highmem.h>
  27. #include <linux/console.h>
  28. #include <xen/interface/xen.h>
  29. #include <xen/interface/physdev.h>
  30. #include <xen/interface/vcpu.h>
  31. #include <xen/interface/sched.h>
  32. #include <xen/features.h>
  33. #include <xen/page.h>
  34. #include <xen/hvc-console.h>
  35. #include <asm/paravirt.h>
  36. #include <asm/page.h>
  37. #include <asm/xen/hypercall.h>
  38. #include <asm/xen/hypervisor.h>
  39. #include <asm/fixmap.h>
  40. #include <asm/processor.h>
  41. #include <asm/msr-index.h>
  42. #include <asm/setup.h>
  43. #include <asm/desc.h>
  44. #include <asm/pgtable.h>
  45. #include <asm/tlbflush.h>
  46. #include <asm/reboot.h>
  47. #include "xen-ops.h"
  48. #include "mmu.h"
  49. #include "multicalls.h"
  50. EXPORT_SYMBOL_GPL(hypercall_page);
  51. DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
  52. DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
  53. /*
  54. * Identity map, in addition to plain kernel map. This needs to be
  55. * large enough to allocate page table pages to allocate the rest.
  56. * Each page can map 2MB.
  57. */
  58. static pte_t level1_ident_pgt[PTRS_PER_PTE * 4] __page_aligned_bss;
  59. #ifdef CONFIG_X86_64
  60. /* l3 pud for userspace vsyscall mapping */
  61. static pud_t level3_user_vsyscall[PTRS_PER_PUD] __page_aligned_bss;
  62. #endif /* CONFIG_X86_64 */
  63. /*
  64. * Note about cr3 (pagetable base) values:
  65. *
  66. * xen_cr3 contains the current logical cr3 value; it contains the
  67. * last set cr3. This may not be the current effective cr3, because
  68. * its update may be being lazily deferred. However, a vcpu looking
  69. * at its own cr3 can use this value knowing that it everything will
  70. * be self-consistent.
  71. *
  72. * xen_current_cr3 contains the actual vcpu cr3; it is set once the
  73. * hypercall to set the vcpu cr3 is complete (so it may be a little
  74. * out of date, but it will never be set early). If one vcpu is
  75. * looking at another vcpu's cr3 value, it should use this variable.
  76. */
  77. DEFINE_PER_CPU(unsigned long, xen_cr3); /* cr3 stored as physaddr */
  78. DEFINE_PER_CPU(unsigned long, xen_current_cr3); /* actual vcpu cr3 */
  79. struct start_info *xen_start_info;
  80. EXPORT_SYMBOL_GPL(xen_start_info);
  81. struct shared_info xen_dummy_shared_info;
  82. /*
  83. * Point at some empty memory to start with. We map the real shared_info
  84. * page as soon as fixmap is up and running.
  85. */
  86. struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
  87. /*
  88. * Flag to determine whether vcpu info placement is available on all
  89. * VCPUs. We assume it is to start with, and then set it to zero on
  90. * the first failure. This is because it can succeed on some VCPUs
  91. * and not others, since it can involve hypervisor memory allocation,
  92. * or because the guest failed to guarantee all the appropriate
  93. * constraints on all VCPUs (ie buffer can't cross a page boundary).
  94. *
  95. * Note that any particular CPU may be using a placed vcpu structure,
  96. * but we can only optimise if the all are.
  97. *
  98. * 0: not available, 1: available
  99. */
  100. static int have_vcpu_info_placement = 1;
  101. static void xen_vcpu_setup(int cpu)
  102. {
  103. struct vcpu_register_vcpu_info info;
  104. int err;
  105. struct vcpu_info *vcpup;
  106. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  107. per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
  108. if (!have_vcpu_info_placement)
  109. return; /* already tested, not available */
  110. vcpup = &per_cpu(xen_vcpu_info, cpu);
  111. info.mfn = virt_to_mfn(vcpup);
  112. info.offset = offset_in_page(vcpup);
  113. printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %llx, offset %d\n",
  114. cpu, vcpup, info.mfn, info.offset);
  115. /* Check to see if the hypervisor will put the vcpu_info
  116. structure where we want it, which allows direct access via
  117. a percpu-variable. */
  118. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
  119. if (err) {
  120. printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
  121. have_vcpu_info_placement = 0;
  122. } else {
  123. /* This cpu is using the registered vcpu info, even if
  124. later ones fail to. */
  125. per_cpu(xen_vcpu, cpu) = vcpup;
  126. printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
  127. cpu, vcpup);
  128. }
  129. }
  130. /*
  131. * On restore, set the vcpu placement up again.
  132. * If it fails, then we're in a bad state, since
  133. * we can't back out from using it...
  134. */
  135. void xen_vcpu_restore(void)
  136. {
  137. if (have_vcpu_info_placement) {
  138. int cpu;
  139. for_each_online_cpu(cpu) {
  140. bool other_cpu = (cpu != smp_processor_id());
  141. if (other_cpu &&
  142. HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
  143. BUG();
  144. xen_vcpu_setup(cpu);
  145. if (other_cpu &&
  146. HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
  147. BUG();
  148. }
  149. BUG_ON(!have_vcpu_info_placement);
  150. }
  151. }
  152. static void __init xen_banner(void)
  153. {
  154. unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
  155. struct xen_extraversion extra;
  156. HYPERVISOR_xen_version(XENVER_extraversion, &extra);
  157. printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
  158. pv_info.name);
  159. printk(KERN_INFO "Xen version: %d.%d%s%s\n",
  160. version >> 16, version & 0xffff, extra.extraversion,
  161. xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
  162. }
  163. static void xen_cpuid(unsigned int *ax, unsigned int *bx,
  164. unsigned int *cx, unsigned int *dx)
  165. {
  166. unsigned maskedx = ~0;
  167. /*
  168. * Mask out inconvenient features, to try and disable as many
  169. * unsupported kernel subsystems as possible.
  170. */
  171. if (*ax == 1)
  172. maskedx = ~((1 << X86_FEATURE_APIC) | /* disable APIC */
  173. (1 << X86_FEATURE_ACPI) | /* disable ACPI */
  174. (1 << X86_FEATURE_MCE) | /* disable MCE */
  175. (1 << X86_FEATURE_MCA) | /* disable MCA */
  176. (1 << X86_FEATURE_ACC)); /* thermal monitoring */
  177. asm(XEN_EMULATE_PREFIX "cpuid"
  178. : "=a" (*ax),
  179. "=b" (*bx),
  180. "=c" (*cx),
  181. "=d" (*dx)
  182. : "0" (*ax), "2" (*cx));
  183. *dx &= maskedx;
  184. }
  185. static void xen_set_debugreg(int reg, unsigned long val)
  186. {
  187. HYPERVISOR_set_debugreg(reg, val);
  188. }
  189. static unsigned long xen_get_debugreg(int reg)
  190. {
  191. return HYPERVISOR_get_debugreg(reg);
  192. }
  193. static unsigned long xen_save_fl(void)
  194. {
  195. struct vcpu_info *vcpu;
  196. unsigned long flags;
  197. vcpu = x86_read_percpu(xen_vcpu);
  198. /* flag has opposite sense of mask */
  199. flags = !vcpu->evtchn_upcall_mask;
  200. /* convert to IF type flag
  201. -0 -> 0x00000000
  202. -1 -> 0xffffffff
  203. */
  204. return (-flags) & X86_EFLAGS_IF;
  205. }
  206. static void xen_restore_fl(unsigned long flags)
  207. {
  208. struct vcpu_info *vcpu;
  209. /* convert from IF type flag */
  210. flags = !(flags & X86_EFLAGS_IF);
  211. /* There's a one instruction preempt window here. We need to
  212. make sure we're don't switch CPUs between getting the vcpu
  213. pointer and updating the mask. */
  214. preempt_disable();
  215. vcpu = x86_read_percpu(xen_vcpu);
  216. vcpu->evtchn_upcall_mask = flags;
  217. preempt_enable_no_resched();
  218. /* Doesn't matter if we get preempted here, because any
  219. pending event will get dealt with anyway. */
  220. if (flags == 0) {
  221. preempt_check_resched();
  222. barrier(); /* unmask then check (avoid races) */
  223. if (unlikely(vcpu->evtchn_upcall_pending))
  224. force_evtchn_callback();
  225. }
  226. }
  227. static void xen_irq_disable(void)
  228. {
  229. /* There's a one instruction preempt window here. We need to
  230. make sure we're don't switch CPUs between getting the vcpu
  231. pointer and updating the mask. */
  232. preempt_disable();
  233. x86_read_percpu(xen_vcpu)->evtchn_upcall_mask = 1;
  234. preempt_enable_no_resched();
  235. }
  236. static void xen_irq_enable(void)
  237. {
  238. struct vcpu_info *vcpu;
  239. /* We don't need to worry about being preempted here, since
  240. either a) interrupts are disabled, so no preemption, or b)
  241. the caller is confused and is trying to re-enable interrupts
  242. on an indeterminate processor. */
  243. vcpu = x86_read_percpu(xen_vcpu);
  244. vcpu->evtchn_upcall_mask = 0;
  245. /* Doesn't matter if we get preempted here, because any
  246. pending event will get dealt with anyway. */
  247. barrier(); /* unmask then check (avoid races) */
  248. if (unlikely(vcpu->evtchn_upcall_pending))
  249. force_evtchn_callback();
  250. }
  251. static void xen_safe_halt(void)
  252. {
  253. /* Blocking includes an implicit local_irq_enable(). */
  254. if (HYPERVISOR_sched_op(SCHEDOP_block, NULL) != 0)
  255. BUG();
  256. }
  257. static void xen_halt(void)
  258. {
  259. if (irqs_disabled())
  260. HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
  261. else
  262. xen_safe_halt();
  263. }
  264. static void xen_leave_lazy(void)
  265. {
  266. paravirt_leave_lazy(paravirt_get_lazy_mode());
  267. xen_mc_flush();
  268. }
  269. static unsigned long xen_store_tr(void)
  270. {
  271. return 0;
  272. }
  273. /*
  274. * If 'v' is a vmalloc mapping, then find the linear mapping of the
  275. * page (if any) and also set its protections to match:
  276. */
  277. static void set_aliased_prot(void *v, pgprot_t prot)
  278. {
  279. int level;
  280. pte_t *ptep;
  281. pte_t pte;
  282. unsigned long pfn;
  283. struct page *page;
  284. ptep = lookup_address((unsigned long)v, &level);
  285. BUG_ON(ptep == NULL);
  286. pfn = pte_pfn(*ptep);
  287. page = pfn_to_page(pfn);
  288. pte = pfn_pte(pfn, prot);
  289. if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
  290. BUG();
  291. if (!PageHighMem(page)) {
  292. void *av = __va(PFN_PHYS(pfn));
  293. if (av != v)
  294. if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
  295. BUG();
  296. } else
  297. kmap_flush_unused();
  298. }
  299. static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
  300. {
  301. const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
  302. int i;
  303. for(i = 0; i < entries; i += entries_per_page)
  304. set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
  305. }
  306. static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
  307. {
  308. const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
  309. int i;
  310. for(i = 0; i < entries; i += entries_per_page)
  311. set_aliased_prot(ldt + i, PAGE_KERNEL);
  312. }
  313. static void xen_set_ldt(const void *addr, unsigned entries)
  314. {
  315. struct mmuext_op *op;
  316. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  317. op = mcs.args;
  318. op->cmd = MMUEXT_SET_LDT;
  319. op->arg1.linear_addr = (unsigned long)addr;
  320. op->arg2.nr_ents = entries;
  321. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  322. xen_mc_issue(PARAVIRT_LAZY_CPU);
  323. }
  324. static void xen_load_gdt(const struct desc_ptr *dtr)
  325. {
  326. unsigned long *frames;
  327. unsigned long va = dtr->address;
  328. unsigned int size = dtr->size + 1;
  329. unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  330. int f;
  331. struct multicall_space mcs;
  332. /* A GDT can be up to 64k in size, which corresponds to 8192
  333. 8-byte entries, or 16 4k pages.. */
  334. BUG_ON(size > 65536);
  335. BUG_ON(va & ~PAGE_MASK);
  336. mcs = xen_mc_entry(sizeof(*frames) * pages);
  337. frames = mcs.args;
  338. for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
  339. frames[f] = virt_to_mfn(va);
  340. make_lowmem_page_readonly((void *)va);
  341. }
  342. MULTI_set_gdt(mcs.mc, frames, size / sizeof(struct desc_struct));
  343. xen_mc_issue(PARAVIRT_LAZY_CPU);
  344. }
  345. static void load_TLS_descriptor(struct thread_struct *t,
  346. unsigned int cpu, unsigned int i)
  347. {
  348. struct desc_struct *gdt = get_cpu_gdt_table(cpu);
  349. xmaddr_t maddr = virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
  350. struct multicall_space mc = __xen_mc_entry(0);
  351. MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
  352. }
  353. static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
  354. {
  355. /*
  356. * XXX sleazy hack: If we're being called in a lazy-cpu zone,
  357. * it means we're in a context switch, and %gs has just been
  358. * saved. This means we can zero it out to prevent faults on
  359. * exit from the hypervisor if the next process has no %gs.
  360. * Either way, it has been saved, and the new value will get
  361. * loaded properly. This will go away as soon as Xen has been
  362. * modified to not save/restore %gs for normal hypercalls.
  363. *
  364. * On x86_64, this hack is not used for %gs, because gs points
  365. * to KERNEL_GS_BASE (and uses it for PDA references), so we
  366. * must not zero %gs on x86_64
  367. *
  368. * For x86_64, we need to zero %fs, otherwise we may get an
  369. * exception between the new %fs descriptor being loaded and
  370. * %fs being effectively cleared at __switch_to().
  371. */
  372. if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
  373. #ifdef CONFIG_X86_32
  374. loadsegment(gs, 0);
  375. #else
  376. loadsegment(fs, 0);
  377. #endif
  378. }
  379. xen_mc_batch();
  380. load_TLS_descriptor(t, cpu, 0);
  381. load_TLS_descriptor(t, cpu, 1);
  382. load_TLS_descriptor(t, cpu, 2);
  383. xen_mc_issue(PARAVIRT_LAZY_CPU);
  384. }
  385. #ifdef CONFIG_X86_64
  386. static void xen_load_gs_index(unsigned int idx)
  387. {
  388. if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
  389. BUG();
  390. }
  391. #endif
  392. static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
  393. const void *ptr)
  394. {
  395. unsigned long lp = (unsigned long)&dt[entrynum];
  396. xmaddr_t mach_lp = arbitrary_virt_to_machine(lp);
  397. u64 entry = *(u64 *)ptr;
  398. preempt_disable();
  399. xen_mc_flush();
  400. if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
  401. BUG();
  402. preempt_enable();
  403. }
  404. static int cvt_gate_to_trap(int vector, const gate_desc *val,
  405. struct trap_info *info)
  406. {
  407. if (val->type != 0xf && val->type != 0xe)
  408. return 0;
  409. info->vector = vector;
  410. info->address = gate_offset(*val);
  411. info->cs = gate_segment(*val);
  412. info->flags = val->dpl;
  413. /* interrupt gates clear IF */
  414. if (val->type == 0xe)
  415. info->flags |= 4;
  416. return 1;
  417. }
  418. /* Locations of each CPU's IDT */
  419. static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
  420. /* Set an IDT entry. If the entry is part of the current IDT, then
  421. also update Xen. */
  422. static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
  423. {
  424. unsigned long p = (unsigned long)&dt[entrynum];
  425. unsigned long start, end;
  426. preempt_disable();
  427. start = __get_cpu_var(idt_desc).address;
  428. end = start + __get_cpu_var(idt_desc).size + 1;
  429. xen_mc_flush();
  430. native_write_idt_entry(dt, entrynum, g);
  431. if (p >= start && (p + 8) <= end) {
  432. struct trap_info info[2];
  433. info[1].address = 0;
  434. if (cvt_gate_to_trap(entrynum, g, &info[0]))
  435. if (HYPERVISOR_set_trap_table(info))
  436. BUG();
  437. }
  438. preempt_enable();
  439. }
  440. static void xen_convert_trap_info(const struct desc_ptr *desc,
  441. struct trap_info *traps)
  442. {
  443. unsigned in, out, count;
  444. count = (desc->size+1) / sizeof(gate_desc);
  445. BUG_ON(count > 256);
  446. for (in = out = 0; in < count; in++) {
  447. gate_desc *entry = (gate_desc*)(desc->address) + in;
  448. if (cvt_gate_to_trap(in, entry, &traps[out]))
  449. out++;
  450. }
  451. traps[out].address = 0;
  452. }
  453. void xen_copy_trap_info(struct trap_info *traps)
  454. {
  455. const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
  456. xen_convert_trap_info(desc, traps);
  457. }
  458. /* Load a new IDT into Xen. In principle this can be per-CPU, so we
  459. hold a spinlock to protect the static traps[] array (static because
  460. it avoids allocation, and saves stack space). */
  461. static void xen_load_idt(const struct desc_ptr *desc)
  462. {
  463. static DEFINE_SPINLOCK(lock);
  464. static struct trap_info traps[257];
  465. spin_lock(&lock);
  466. __get_cpu_var(idt_desc) = *desc;
  467. xen_convert_trap_info(desc, traps);
  468. xen_mc_flush();
  469. if (HYPERVISOR_set_trap_table(traps))
  470. BUG();
  471. spin_unlock(&lock);
  472. }
  473. /* Write a GDT descriptor entry. Ignore LDT descriptors, since
  474. they're handled differently. */
  475. static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
  476. const void *desc, int type)
  477. {
  478. preempt_disable();
  479. switch (type) {
  480. case DESC_LDT:
  481. case DESC_TSS:
  482. /* ignore */
  483. break;
  484. default: {
  485. xmaddr_t maddr = virt_to_machine(&dt[entry]);
  486. xen_mc_flush();
  487. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  488. BUG();
  489. }
  490. }
  491. preempt_enable();
  492. }
  493. static void xen_load_sp0(struct tss_struct *tss,
  494. struct thread_struct *thread)
  495. {
  496. struct multicall_space mcs = xen_mc_entry(0);
  497. MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
  498. xen_mc_issue(PARAVIRT_LAZY_CPU);
  499. }
  500. static void xen_set_iopl_mask(unsigned mask)
  501. {
  502. struct physdev_set_iopl set_iopl;
  503. /* Force the change at ring 0. */
  504. set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
  505. HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  506. }
  507. static void xen_io_delay(void)
  508. {
  509. }
  510. #ifdef CONFIG_X86_LOCAL_APIC
  511. static u32 xen_apic_read(unsigned long reg)
  512. {
  513. return 0;
  514. }
  515. static void xen_apic_write(unsigned long reg, u32 val)
  516. {
  517. /* Warn to see if there's any stray references */
  518. WARN_ON(1);
  519. }
  520. #endif
  521. static void xen_flush_tlb(void)
  522. {
  523. struct mmuext_op *op;
  524. struct multicall_space mcs;
  525. preempt_disable();
  526. mcs = xen_mc_entry(sizeof(*op));
  527. op = mcs.args;
  528. op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
  529. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  530. xen_mc_issue(PARAVIRT_LAZY_MMU);
  531. preempt_enable();
  532. }
  533. static void xen_flush_tlb_single(unsigned long addr)
  534. {
  535. struct mmuext_op *op;
  536. struct multicall_space mcs;
  537. preempt_disable();
  538. mcs = xen_mc_entry(sizeof(*op));
  539. op = mcs.args;
  540. op->cmd = MMUEXT_INVLPG_LOCAL;
  541. op->arg1.linear_addr = addr & PAGE_MASK;
  542. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  543. xen_mc_issue(PARAVIRT_LAZY_MMU);
  544. preempt_enable();
  545. }
  546. static void xen_flush_tlb_others(const cpumask_t *cpus, struct mm_struct *mm,
  547. unsigned long va)
  548. {
  549. struct {
  550. struct mmuext_op op;
  551. cpumask_t mask;
  552. } *args;
  553. cpumask_t cpumask = *cpus;
  554. struct multicall_space mcs;
  555. /*
  556. * A couple of (to be removed) sanity checks:
  557. *
  558. * - current CPU must not be in mask
  559. * - mask must exist :)
  560. */
  561. BUG_ON(cpus_empty(cpumask));
  562. BUG_ON(cpu_isset(smp_processor_id(), cpumask));
  563. BUG_ON(!mm);
  564. /* If a CPU which we ran on has gone down, OK. */
  565. cpus_and(cpumask, cpumask, cpu_online_map);
  566. if (cpus_empty(cpumask))
  567. return;
  568. mcs = xen_mc_entry(sizeof(*args));
  569. args = mcs.args;
  570. args->mask = cpumask;
  571. args->op.arg2.vcpumask = &args->mask;
  572. if (va == TLB_FLUSH_ALL) {
  573. args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
  574. } else {
  575. args->op.cmd = MMUEXT_INVLPG_MULTI;
  576. args->op.arg1.linear_addr = va;
  577. }
  578. MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
  579. xen_mc_issue(PARAVIRT_LAZY_MMU);
  580. }
  581. static void xen_clts(void)
  582. {
  583. struct multicall_space mcs;
  584. mcs = xen_mc_entry(0);
  585. MULTI_fpu_taskswitch(mcs.mc, 0);
  586. xen_mc_issue(PARAVIRT_LAZY_CPU);
  587. }
  588. static void xen_write_cr0(unsigned long cr0)
  589. {
  590. struct multicall_space mcs;
  591. /* Only pay attention to cr0.TS; everything else is
  592. ignored. */
  593. mcs = xen_mc_entry(0);
  594. MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
  595. xen_mc_issue(PARAVIRT_LAZY_CPU);
  596. }
  597. static void xen_write_cr2(unsigned long cr2)
  598. {
  599. x86_read_percpu(xen_vcpu)->arch.cr2 = cr2;
  600. }
  601. static unsigned long xen_read_cr2(void)
  602. {
  603. return x86_read_percpu(xen_vcpu)->arch.cr2;
  604. }
  605. static unsigned long xen_read_cr2_direct(void)
  606. {
  607. return x86_read_percpu(xen_vcpu_info.arch.cr2);
  608. }
  609. static void xen_write_cr4(unsigned long cr4)
  610. {
  611. cr4 &= ~X86_CR4_PGE;
  612. cr4 &= ~X86_CR4_PSE;
  613. native_write_cr4(cr4);
  614. }
  615. static unsigned long xen_read_cr3(void)
  616. {
  617. return x86_read_percpu(xen_cr3);
  618. }
  619. static void set_current_cr3(void *v)
  620. {
  621. x86_write_percpu(xen_current_cr3, (unsigned long)v);
  622. }
  623. static void __xen_write_cr3(bool kernel, unsigned long cr3)
  624. {
  625. struct mmuext_op *op;
  626. struct multicall_space mcs;
  627. unsigned long mfn;
  628. if (cr3)
  629. mfn = pfn_to_mfn(PFN_DOWN(cr3));
  630. else
  631. mfn = 0;
  632. WARN_ON(mfn == 0 && kernel);
  633. mcs = __xen_mc_entry(sizeof(*op));
  634. op = mcs.args;
  635. op->cmd = kernel ? MMUEXT_NEW_BASEPTR : MMUEXT_NEW_USER_BASEPTR;
  636. op->arg1.mfn = mfn;
  637. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  638. if (kernel) {
  639. x86_write_percpu(xen_cr3, cr3);
  640. /* Update xen_current_cr3 once the batch has actually
  641. been submitted. */
  642. xen_mc_callback(set_current_cr3, (void *)cr3);
  643. }
  644. }
  645. static void xen_write_cr3(unsigned long cr3)
  646. {
  647. BUG_ON(preemptible());
  648. xen_mc_batch(); /* disables interrupts */
  649. /* Update while interrupts are disabled, so its atomic with
  650. respect to ipis */
  651. x86_write_percpu(xen_cr3, cr3);
  652. __xen_write_cr3(true, cr3);
  653. #ifdef CONFIG_X86_64
  654. {
  655. pgd_t *user_pgd = xen_get_user_pgd(__va(cr3));
  656. if (user_pgd)
  657. __xen_write_cr3(false, __pa(user_pgd));
  658. else
  659. __xen_write_cr3(false, 0);
  660. }
  661. #endif
  662. xen_mc_issue(PARAVIRT_LAZY_CPU); /* interrupts restored */
  663. }
  664. static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
  665. {
  666. int ret;
  667. ret = 0;
  668. switch(msr) {
  669. #ifdef CONFIG_X86_64
  670. unsigned which;
  671. u64 base;
  672. case MSR_FS_BASE: which = SEGBASE_FS; goto set;
  673. case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
  674. case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
  675. set:
  676. base = ((u64)high << 32) | low;
  677. if (HYPERVISOR_set_segment_base(which, base) != 0)
  678. ret = -EFAULT;
  679. break;
  680. #endif
  681. case MSR_STAR:
  682. case MSR_CSTAR:
  683. case MSR_LSTAR:
  684. case MSR_SYSCALL_MASK:
  685. case MSR_IA32_SYSENTER_CS:
  686. case MSR_IA32_SYSENTER_ESP:
  687. case MSR_IA32_SYSENTER_EIP:
  688. /* Fast syscall setup is all done in hypercalls, so
  689. these are all ignored. Stub them out here to stop
  690. Xen console noise. */
  691. break;
  692. default:
  693. ret = native_write_msr_safe(msr, low, high);
  694. }
  695. return ret;
  696. }
  697. /* Early in boot, while setting up the initial pagetable, assume
  698. everything is pinned. */
  699. static __init void xen_alloc_pte_init(struct mm_struct *mm, u32 pfn)
  700. {
  701. #ifdef CONFIG_FLATMEM
  702. BUG_ON(mem_map); /* should only be used early */
  703. #endif
  704. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  705. }
  706. /* Early release_pte assumes that all pts are pinned, since there's
  707. only init_mm and anything attached to that is pinned. */
  708. static void xen_release_pte_init(u32 pfn)
  709. {
  710. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  711. }
  712. static void pin_pagetable_pfn(unsigned cmd, unsigned long pfn)
  713. {
  714. struct mmuext_op op;
  715. op.cmd = cmd;
  716. op.arg1.mfn = pfn_to_mfn(pfn);
  717. if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
  718. BUG();
  719. }
  720. /* This needs to make sure the new pte page is pinned iff its being
  721. attached to a pinned pagetable. */
  722. static void xen_alloc_ptpage(struct mm_struct *mm, u32 pfn, unsigned level)
  723. {
  724. struct page *page = pfn_to_page(pfn);
  725. if (PagePinned(virt_to_page(mm->pgd))) {
  726. SetPagePinned(page);
  727. if (!PageHighMem(page)) {
  728. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  729. if (level == PT_PTE)
  730. pin_pagetable_pfn(MMUEXT_PIN_L1_TABLE, pfn);
  731. } else
  732. /* make sure there are no stray mappings of
  733. this page */
  734. kmap_flush_unused();
  735. }
  736. }
  737. static void xen_alloc_pte(struct mm_struct *mm, u32 pfn)
  738. {
  739. xen_alloc_ptpage(mm, pfn, PT_PTE);
  740. }
  741. static void xen_alloc_pmd(struct mm_struct *mm, u32 pfn)
  742. {
  743. xen_alloc_ptpage(mm, pfn, PT_PMD);
  744. }
  745. static int xen_pgd_alloc(struct mm_struct *mm)
  746. {
  747. pgd_t *pgd = mm->pgd;
  748. int ret = 0;
  749. BUG_ON(PagePinned(virt_to_page(pgd)));
  750. #ifdef CONFIG_X86_64
  751. {
  752. struct page *page = virt_to_page(pgd);
  753. pgd_t *user_pgd;
  754. BUG_ON(page->private != 0);
  755. ret = -ENOMEM;
  756. user_pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  757. page->private = (unsigned long)user_pgd;
  758. if (user_pgd != NULL) {
  759. user_pgd[pgd_index(VSYSCALL_START)] =
  760. __pgd(__pa(level3_user_vsyscall) | _PAGE_TABLE);
  761. ret = 0;
  762. }
  763. BUG_ON(PagePinned(virt_to_page(xen_get_user_pgd(pgd))));
  764. }
  765. #endif
  766. return ret;
  767. }
  768. static void xen_pgd_free(struct mm_struct *mm, pgd_t *pgd)
  769. {
  770. #ifdef CONFIG_X86_64
  771. pgd_t *user_pgd = xen_get_user_pgd(pgd);
  772. if (user_pgd)
  773. free_page((unsigned long)user_pgd);
  774. #endif
  775. }
  776. /* This should never happen until we're OK to use struct page */
  777. static void xen_release_ptpage(u32 pfn, unsigned level)
  778. {
  779. struct page *page = pfn_to_page(pfn);
  780. if (PagePinned(page)) {
  781. if (!PageHighMem(page)) {
  782. if (level == PT_PTE)
  783. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, pfn);
  784. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  785. }
  786. ClearPagePinned(page);
  787. }
  788. }
  789. static void xen_release_pte(u32 pfn)
  790. {
  791. xen_release_ptpage(pfn, PT_PTE);
  792. }
  793. static void xen_release_pmd(u32 pfn)
  794. {
  795. xen_release_ptpage(pfn, PT_PMD);
  796. }
  797. #if PAGETABLE_LEVELS == 4
  798. static void xen_alloc_pud(struct mm_struct *mm, u32 pfn)
  799. {
  800. xen_alloc_ptpage(mm, pfn, PT_PUD);
  801. }
  802. static void xen_release_pud(u32 pfn)
  803. {
  804. xen_release_ptpage(pfn, PT_PUD);
  805. }
  806. #endif
  807. #ifdef CONFIG_HIGHPTE
  808. static void *xen_kmap_atomic_pte(struct page *page, enum km_type type)
  809. {
  810. pgprot_t prot = PAGE_KERNEL;
  811. if (PagePinned(page))
  812. prot = PAGE_KERNEL_RO;
  813. if (0 && PageHighMem(page))
  814. printk("mapping highpte %lx type %d prot %s\n",
  815. page_to_pfn(page), type,
  816. (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ");
  817. return kmap_atomic_prot(page, type, prot);
  818. }
  819. #endif
  820. static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
  821. {
  822. /* If there's an existing pte, then don't allow _PAGE_RW to be set */
  823. if (pte_val_ma(*ptep) & _PAGE_PRESENT)
  824. pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
  825. pte_val_ma(pte));
  826. return pte;
  827. }
  828. /* Init-time set_pte while constructing initial pagetables, which
  829. doesn't allow RO pagetable pages to be remapped RW */
  830. static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
  831. {
  832. pte = mask_rw_pte(ptep, pte);
  833. xen_set_pte(ptep, pte);
  834. }
  835. static __init void xen_pagetable_setup_start(pgd_t *base)
  836. {
  837. }
  838. void xen_setup_shared_info(void)
  839. {
  840. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  841. set_fixmap(FIX_PARAVIRT_BOOTMAP,
  842. xen_start_info->shared_info);
  843. HYPERVISOR_shared_info =
  844. (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
  845. } else
  846. HYPERVISOR_shared_info =
  847. (struct shared_info *)__va(xen_start_info->shared_info);
  848. #ifndef CONFIG_SMP
  849. /* In UP this is as good a place as any to set up shared info */
  850. xen_setup_vcpu_info_placement();
  851. #endif
  852. xen_setup_mfn_list_list();
  853. }
  854. static __init void xen_pagetable_setup_done(pgd_t *base)
  855. {
  856. xen_setup_shared_info();
  857. }
  858. static __init void xen_post_allocator_init(void)
  859. {
  860. pv_mmu_ops.set_pte = xen_set_pte;
  861. pv_mmu_ops.set_pmd = xen_set_pmd;
  862. pv_mmu_ops.set_pud = xen_set_pud;
  863. #if PAGETABLE_LEVELS == 4
  864. pv_mmu_ops.set_pgd = xen_set_pgd;
  865. #endif
  866. /* This will work as long as patching hasn't happened yet
  867. (which it hasn't) */
  868. pv_mmu_ops.alloc_pte = xen_alloc_pte;
  869. pv_mmu_ops.alloc_pmd = xen_alloc_pmd;
  870. pv_mmu_ops.release_pte = xen_release_pte;
  871. pv_mmu_ops.release_pmd = xen_release_pmd;
  872. #if PAGETABLE_LEVELS == 4
  873. pv_mmu_ops.alloc_pud = xen_alloc_pud;
  874. pv_mmu_ops.release_pud = xen_release_pud;
  875. #endif
  876. #ifdef CONFIG_X86_64
  877. SetPagePinned(virt_to_page(level3_user_vsyscall));
  878. #endif
  879. xen_mark_init_mm_pinned();
  880. }
  881. /* This is called once we have the cpu_possible_map */
  882. void xen_setup_vcpu_info_placement(void)
  883. {
  884. int cpu;
  885. for_each_possible_cpu(cpu)
  886. xen_vcpu_setup(cpu);
  887. /* xen_vcpu_setup managed to place the vcpu_info within the
  888. percpu area for all cpus, so make use of it */
  889. #ifdef CONFIG_X86_32
  890. if (have_vcpu_info_placement) {
  891. printk(KERN_INFO "Xen: using vcpu_info placement\n");
  892. pv_irq_ops.save_fl = xen_save_fl_direct;
  893. pv_irq_ops.restore_fl = xen_restore_fl_direct;
  894. pv_irq_ops.irq_disable = xen_irq_disable_direct;
  895. pv_irq_ops.irq_enable = xen_irq_enable_direct;
  896. pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
  897. }
  898. #endif
  899. }
  900. static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
  901. unsigned long addr, unsigned len)
  902. {
  903. char *start, *end, *reloc;
  904. unsigned ret;
  905. start = end = reloc = NULL;
  906. #define SITE(op, x) \
  907. case PARAVIRT_PATCH(op.x): \
  908. if (have_vcpu_info_placement) { \
  909. start = (char *)xen_##x##_direct; \
  910. end = xen_##x##_direct_end; \
  911. reloc = xen_##x##_direct_reloc; \
  912. } \
  913. goto patch_site
  914. switch (type) {
  915. #ifdef CONFIG_X86_32
  916. SITE(pv_irq_ops, irq_enable);
  917. SITE(pv_irq_ops, irq_disable);
  918. SITE(pv_irq_ops, save_fl);
  919. SITE(pv_irq_ops, restore_fl);
  920. #endif /* CONFIG_X86_32 */
  921. #undef SITE
  922. patch_site:
  923. if (start == NULL || (end-start) > len)
  924. goto default_patch;
  925. ret = paravirt_patch_insns(insnbuf, len, start, end);
  926. /* Note: because reloc is assigned from something that
  927. appears to be an array, gcc assumes it's non-null,
  928. but doesn't know its relationship with start and
  929. end. */
  930. if (reloc > start && reloc < end) {
  931. int reloc_off = reloc - start;
  932. long *relocp = (long *)(insnbuf + reloc_off);
  933. long delta = start - (char *)addr;
  934. *relocp += delta;
  935. }
  936. break;
  937. default_patch:
  938. default:
  939. ret = paravirt_patch_default(type, clobbers, insnbuf,
  940. addr, len);
  941. break;
  942. }
  943. return ret;
  944. }
  945. static void xen_set_fixmap(unsigned idx, unsigned long phys, pgprot_t prot)
  946. {
  947. pte_t pte;
  948. phys >>= PAGE_SHIFT;
  949. switch (idx) {
  950. case FIX_BTMAP_END ... FIX_BTMAP_BEGIN:
  951. #ifdef CONFIG_X86_F00F_BUG
  952. case FIX_F00F_IDT:
  953. #endif
  954. #ifdef CONFIG_X86_32
  955. case FIX_WP_TEST:
  956. case FIX_VDSO:
  957. # ifdef CONFIG_HIGHMEM
  958. case FIX_KMAP_BEGIN ... FIX_KMAP_END:
  959. # endif
  960. #else
  961. case VSYSCALL_LAST_PAGE ... VSYSCALL_FIRST_PAGE:
  962. #endif
  963. #ifdef CONFIG_X86_LOCAL_APIC
  964. case FIX_APIC_BASE: /* maps dummy local APIC */
  965. #endif
  966. pte = pfn_pte(phys, prot);
  967. break;
  968. default:
  969. pte = mfn_pte(phys, prot);
  970. break;
  971. }
  972. __native_set_fixmap(idx, pte);
  973. #ifdef CONFIG_X86_64
  974. /* Replicate changes to map the vsyscall page into the user
  975. pagetable vsyscall mapping. */
  976. if (idx >= VSYSCALL_LAST_PAGE && idx <= VSYSCALL_FIRST_PAGE) {
  977. unsigned long vaddr = __fix_to_virt(idx);
  978. set_pte_vaddr_pud(level3_user_vsyscall, vaddr, pte);
  979. }
  980. #endif
  981. }
  982. static const struct pv_info xen_info __initdata = {
  983. .paravirt_enabled = 1,
  984. .shared_kernel_pmd = 0,
  985. .name = "Xen",
  986. };
  987. static const struct pv_init_ops xen_init_ops __initdata = {
  988. .patch = xen_patch,
  989. .banner = xen_banner,
  990. .memory_setup = xen_memory_setup,
  991. .arch_setup = xen_arch_setup,
  992. .post_allocator_init = xen_post_allocator_init,
  993. };
  994. static const struct pv_time_ops xen_time_ops __initdata = {
  995. .time_init = xen_time_init,
  996. .set_wallclock = xen_set_wallclock,
  997. .get_wallclock = xen_get_wallclock,
  998. .get_tsc_khz = xen_tsc_khz,
  999. .sched_clock = xen_sched_clock,
  1000. };
  1001. static const struct pv_cpu_ops xen_cpu_ops __initdata = {
  1002. .cpuid = xen_cpuid,
  1003. .set_debugreg = xen_set_debugreg,
  1004. .get_debugreg = xen_get_debugreg,
  1005. .clts = xen_clts,
  1006. .read_cr0 = native_read_cr0,
  1007. .write_cr0 = xen_write_cr0,
  1008. .read_cr4 = native_read_cr4,
  1009. .read_cr4_safe = native_read_cr4_safe,
  1010. .write_cr4 = xen_write_cr4,
  1011. .wbinvd = native_wbinvd,
  1012. .read_msr = native_read_msr_safe,
  1013. .write_msr = xen_write_msr_safe,
  1014. .read_tsc = native_read_tsc,
  1015. .read_pmc = native_read_pmc,
  1016. .iret = xen_iret,
  1017. .irq_enable_sysexit = xen_sysexit,
  1018. #ifdef CONFIG_X86_64
  1019. .usergs_sysret32 = xen_sysret32,
  1020. .usergs_sysret64 = xen_sysret64,
  1021. #endif
  1022. .load_tr_desc = paravirt_nop,
  1023. .set_ldt = xen_set_ldt,
  1024. .load_gdt = xen_load_gdt,
  1025. .load_idt = xen_load_idt,
  1026. .load_tls = xen_load_tls,
  1027. #ifdef CONFIG_X86_64
  1028. .load_gs_index = xen_load_gs_index,
  1029. #endif
  1030. .alloc_ldt = xen_alloc_ldt,
  1031. .free_ldt = xen_free_ldt,
  1032. .store_gdt = native_store_gdt,
  1033. .store_idt = native_store_idt,
  1034. .store_tr = xen_store_tr,
  1035. .write_ldt_entry = xen_write_ldt_entry,
  1036. .write_gdt_entry = xen_write_gdt_entry,
  1037. .write_idt_entry = xen_write_idt_entry,
  1038. .load_sp0 = xen_load_sp0,
  1039. .set_iopl_mask = xen_set_iopl_mask,
  1040. .io_delay = xen_io_delay,
  1041. /* Xen takes care of %gs when switching to usermode for us */
  1042. .swapgs = paravirt_nop,
  1043. .lazy_mode = {
  1044. .enter = paravirt_enter_lazy_cpu,
  1045. .leave = xen_leave_lazy,
  1046. },
  1047. };
  1048. static void __init __xen_init_IRQ(void)
  1049. {
  1050. #ifdef CONFIG_X86_64
  1051. int i;
  1052. /* Create identity vector->irq map */
  1053. for(i = 0; i < NR_VECTORS; i++) {
  1054. int cpu;
  1055. for_each_possible_cpu(cpu)
  1056. per_cpu(vector_irq, cpu)[i] = i;
  1057. }
  1058. #endif /* CONFIG_X86_64 */
  1059. xen_init_IRQ();
  1060. }
  1061. static const struct pv_irq_ops xen_irq_ops __initdata = {
  1062. .init_IRQ = __xen_init_IRQ,
  1063. .save_fl = xen_save_fl,
  1064. .restore_fl = xen_restore_fl,
  1065. .irq_disable = xen_irq_disable,
  1066. .irq_enable = xen_irq_enable,
  1067. .safe_halt = xen_safe_halt,
  1068. .halt = xen_halt,
  1069. #ifdef CONFIG_X86_64
  1070. .adjust_exception_frame = xen_adjust_exception_frame,
  1071. #endif
  1072. };
  1073. static const struct pv_apic_ops xen_apic_ops __initdata = {
  1074. #ifdef CONFIG_X86_LOCAL_APIC
  1075. .apic_write = xen_apic_write,
  1076. .apic_read = xen_apic_read,
  1077. .setup_boot_clock = paravirt_nop,
  1078. .setup_secondary_clock = paravirt_nop,
  1079. .startup_ipi_hook = paravirt_nop,
  1080. #endif
  1081. };
  1082. static const struct pv_mmu_ops xen_mmu_ops __initdata = {
  1083. .pagetable_setup_start = xen_pagetable_setup_start,
  1084. .pagetable_setup_done = xen_pagetable_setup_done,
  1085. .read_cr2 = xen_read_cr2,
  1086. .write_cr2 = xen_write_cr2,
  1087. .read_cr3 = xen_read_cr3,
  1088. .write_cr3 = xen_write_cr3,
  1089. .flush_tlb_user = xen_flush_tlb,
  1090. .flush_tlb_kernel = xen_flush_tlb,
  1091. .flush_tlb_single = xen_flush_tlb_single,
  1092. .flush_tlb_others = xen_flush_tlb_others,
  1093. .pte_update = paravirt_nop,
  1094. .pte_update_defer = paravirt_nop,
  1095. .pgd_alloc = xen_pgd_alloc,
  1096. .pgd_free = xen_pgd_free,
  1097. .alloc_pte = xen_alloc_pte_init,
  1098. .release_pte = xen_release_pte_init,
  1099. .alloc_pmd = xen_alloc_pte_init,
  1100. .alloc_pmd_clone = paravirt_nop,
  1101. .release_pmd = xen_release_pte_init,
  1102. #ifdef CONFIG_HIGHPTE
  1103. .kmap_atomic_pte = xen_kmap_atomic_pte,
  1104. #endif
  1105. #ifdef CONFIG_X86_64
  1106. .set_pte = xen_set_pte,
  1107. #else
  1108. .set_pte = xen_set_pte_init,
  1109. #endif
  1110. .set_pte_at = xen_set_pte_at,
  1111. .set_pmd = xen_set_pmd_hyper,
  1112. .ptep_modify_prot_start = __ptep_modify_prot_start,
  1113. .ptep_modify_prot_commit = __ptep_modify_prot_commit,
  1114. .pte_val = xen_pte_val,
  1115. .pte_flags = native_pte_flags,
  1116. .pgd_val = xen_pgd_val,
  1117. .make_pte = xen_make_pte,
  1118. .make_pgd = xen_make_pgd,
  1119. #ifdef CONFIG_X86_PAE
  1120. .set_pte_atomic = xen_set_pte_atomic,
  1121. .set_pte_present = xen_set_pte_at,
  1122. .pte_clear = xen_pte_clear,
  1123. .pmd_clear = xen_pmd_clear,
  1124. #endif /* CONFIG_X86_PAE */
  1125. .set_pud = xen_set_pud_hyper,
  1126. .make_pmd = xen_make_pmd,
  1127. .pmd_val = xen_pmd_val,
  1128. #if PAGETABLE_LEVELS == 4
  1129. .pud_val = xen_pud_val,
  1130. .make_pud = xen_make_pud,
  1131. .set_pgd = xen_set_pgd_hyper,
  1132. .alloc_pud = xen_alloc_pte_init,
  1133. .release_pud = xen_release_pte_init,
  1134. #endif /* PAGETABLE_LEVELS == 4 */
  1135. .activate_mm = xen_activate_mm,
  1136. .dup_mmap = xen_dup_mmap,
  1137. .exit_mmap = xen_exit_mmap,
  1138. .lazy_mode = {
  1139. .enter = paravirt_enter_lazy_mmu,
  1140. .leave = xen_leave_lazy,
  1141. },
  1142. .set_fixmap = xen_set_fixmap,
  1143. };
  1144. static void xen_reboot(int reason)
  1145. {
  1146. struct sched_shutdown r = { .reason = reason };
  1147. #ifdef CONFIG_SMP
  1148. smp_send_stop();
  1149. #endif
  1150. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
  1151. BUG();
  1152. }
  1153. static void xen_restart(char *msg)
  1154. {
  1155. xen_reboot(SHUTDOWN_reboot);
  1156. }
  1157. static void xen_emergency_restart(void)
  1158. {
  1159. xen_reboot(SHUTDOWN_reboot);
  1160. }
  1161. static void xen_machine_halt(void)
  1162. {
  1163. xen_reboot(SHUTDOWN_poweroff);
  1164. }
  1165. static void xen_crash_shutdown(struct pt_regs *regs)
  1166. {
  1167. xen_reboot(SHUTDOWN_crash);
  1168. }
  1169. static const struct machine_ops __initdata xen_machine_ops = {
  1170. .restart = xen_restart,
  1171. .halt = xen_machine_halt,
  1172. .power_off = xen_machine_halt,
  1173. .shutdown = xen_machine_halt,
  1174. .crash_shutdown = xen_crash_shutdown,
  1175. .emergency_restart = xen_emergency_restart,
  1176. };
  1177. static void __init xen_reserve_top(void)
  1178. {
  1179. #ifdef CONFIG_X86_32
  1180. unsigned long top = HYPERVISOR_VIRT_START;
  1181. struct xen_platform_parameters pp;
  1182. if (HYPERVISOR_xen_version(XENVER_platform_parameters, &pp) == 0)
  1183. top = pp.virt_start;
  1184. reserve_top_address(-top + 2 * PAGE_SIZE);
  1185. #endif /* CONFIG_X86_32 */
  1186. }
  1187. /*
  1188. * Like __va(), but returns address in the kernel mapping (which is
  1189. * all we have until the physical memory mapping has been set up.
  1190. */
  1191. static void *__ka(phys_addr_t paddr)
  1192. {
  1193. #ifdef CONFIG_X86_64
  1194. return (void *)(paddr + __START_KERNEL_map);
  1195. #else
  1196. return __va(paddr);
  1197. #endif
  1198. }
  1199. /* Convert a machine address to physical address */
  1200. static unsigned long m2p(phys_addr_t maddr)
  1201. {
  1202. phys_addr_t paddr;
  1203. maddr &= PTE_PFN_MASK;
  1204. paddr = mfn_to_pfn(maddr >> PAGE_SHIFT) << PAGE_SHIFT;
  1205. return paddr;
  1206. }
  1207. /* Convert a machine address to kernel virtual */
  1208. static void *m2v(phys_addr_t maddr)
  1209. {
  1210. return __ka(m2p(maddr));
  1211. }
  1212. #ifdef CONFIG_X86_64
  1213. static void walk(pgd_t *pgd, unsigned long addr)
  1214. {
  1215. unsigned l4idx = pgd_index(addr);
  1216. unsigned l3idx = pud_index(addr);
  1217. unsigned l2idx = pmd_index(addr);
  1218. unsigned l1idx = pte_index(addr);
  1219. pgd_t l4;
  1220. pud_t l3;
  1221. pmd_t l2;
  1222. pte_t l1;
  1223. xen_raw_printk("walk %p, %lx -> %d %d %d %d\n",
  1224. pgd, addr, l4idx, l3idx, l2idx, l1idx);
  1225. l4 = pgd[l4idx];
  1226. xen_raw_printk(" l4: %016lx\n", l4.pgd);
  1227. xen_raw_printk(" %016lx\n", pgd_val(l4));
  1228. l3 = ((pud_t *)(m2v(l4.pgd)))[l3idx];
  1229. xen_raw_printk(" l3: %016lx\n", l3.pud);
  1230. xen_raw_printk(" %016lx\n", pud_val(l3));
  1231. l2 = ((pmd_t *)(m2v(l3.pud)))[l2idx];
  1232. xen_raw_printk(" l2: %016lx\n", l2.pmd);
  1233. xen_raw_printk(" %016lx\n", pmd_val(l2));
  1234. l1 = ((pte_t *)(m2v(l2.pmd)))[l1idx];
  1235. xen_raw_printk(" l1: %016lx\n", l1.pte);
  1236. xen_raw_printk(" %016lx\n", pte_val(l1));
  1237. }
  1238. #endif
  1239. static void set_page_prot(void *addr, pgprot_t prot)
  1240. {
  1241. unsigned long pfn = __pa(addr) >> PAGE_SHIFT;
  1242. pte_t pte = pfn_pte(pfn, prot);
  1243. xen_raw_printk("addr=%p pfn=%lx mfn=%lx prot=%016llx pte=%016llx\n",
  1244. addr, pfn, get_phys_to_machine(pfn),
  1245. pgprot_val(prot), pte.pte);
  1246. if (HYPERVISOR_update_va_mapping((unsigned long)addr, pte, 0))
  1247. BUG();
  1248. }
  1249. static __init void xen_map_identity_early(pmd_t *pmd, unsigned long max_pfn)
  1250. {
  1251. unsigned pmdidx, pteidx;
  1252. unsigned ident_pte;
  1253. unsigned long pfn;
  1254. ident_pte = 0;
  1255. pfn = 0;
  1256. for(pmdidx = 0; pmdidx < PTRS_PER_PMD && pfn < max_pfn; pmdidx++) {
  1257. pte_t *pte_page;
  1258. /* Reuse or allocate a page of ptes */
  1259. if (pmd_present(pmd[pmdidx]))
  1260. pte_page = m2v(pmd[pmdidx].pmd);
  1261. else {
  1262. /* Check for free pte pages */
  1263. if (ident_pte == ARRAY_SIZE(level1_ident_pgt))
  1264. break;
  1265. pte_page = &level1_ident_pgt[ident_pte];
  1266. ident_pte += PTRS_PER_PTE;
  1267. pmd[pmdidx] = __pmd(__pa(pte_page) | _PAGE_TABLE);
  1268. }
  1269. /* Install mappings */
  1270. for(pteidx = 0; pteidx < PTRS_PER_PTE; pteidx++, pfn++) {
  1271. pte_t pte;
  1272. if (pfn > max_pfn_mapped)
  1273. max_pfn_mapped = pfn;
  1274. if (!pte_none(pte_page[pteidx]))
  1275. continue;
  1276. pte = pfn_pte(pfn, PAGE_KERNEL_EXEC);
  1277. pte_page[pteidx] = pte;
  1278. }
  1279. }
  1280. for(pteidx = 0; pteidx < ident_pte; pteidx += PTRS_PER_PTE)
  1281. set_page_prot(&level1_ident_pgt[pteidx], PAGE_KERNEL_RO);
  1282. set_page_prot(pmd, PAGE_KERNEL_RO);
  1283. }
  1284. #ifdef CONFIG_X86_64
  1285. static void convert_pfn_mfn(void *v)
  1286. {
  1287. pte_t *pte = v;
  1288. int i;
  1289. /* All levels are converted the same way, so just treat them
  1290. as ptes. */
  1291. for(i = 0; i < PTRS_PER_PTE; i++)
  1292. pte[i] = xen_make_pte(pte[i].pte);
  1293. }
  1294. /*
  1295. * Set up the inital kernel pagetable.
  1296. *
  1297. * We can construct this by grafting the Xen provided pagetable into
  1298. * head_64.S's preconstructed pagetables. We copy the Xen L2's into
  1299. * level2_ident_pgt, level2_kernel_pgt and level2_fixmap_pgt. This
  1300. * means that only the kernel has a physical mapping to start with -
  1301. * but that's enough to get __va working. We need to fill in the rest
  1302. * of the physical mapping once some sort of allocator has been set
  1303. * up.
  1304. */
  1305. static __init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd, unsigned long max_pfn)
  1306. {
  1307. pud_t *l3;
  1308. pmd_t *l2;
  1309. /* Zap identity mapping */
  1310. init_level4_pgt[0] = __pgd(0);
  1311. /* Pre-constructed entries are in pfn, so convert to mfn */
  1312. convert_pfn_mfn(init_level4_pgt);
  1313. convert_pfn_mfn(level3_ident_pgt);
  1314. convert_pfn_mfn(level3_kernel_pgt);
  1315. l3 = m2v(pgd[pgd_index(__START_KERNEL_map)].pgd);
  1316. l2 = m2v(l3[pud_index(__START_KERNEL_map)].pud);
  1317. memcpy(level2_ident_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1318. memcpy(level2_kernel_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1319. l3 = m2v(pgd[pgd_index(__START_KERNEL_map + PMD_SIZE)].pgd);
  1320. l2 = m2v(l3[pud_index(__START_KERNEL_map + PMD_SIZE)].pud);
  1321. memcpy(level2_fixmap_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1322. /* Set up identity map */
  1323. xen_map_identity_early(level2_ident_pgt, max_pfn);
  1324. /* Make pagetable pieces RO */
  1325. set_page_prot(init_level4_pgt, PAGE_KERNEL_RO);
  1326. set_page_prot(level3_ident_pgt, PAGE_KERNEL_RO);
  1327. set_page_prot(level3_kernel_pgt, PAGE_KERNEL_RO);
  1328. set_page_prot(level3_user_vsyscall, PAGE_KERNEL_RO);
  1329. set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO);
  1330. set_page_prot(level2_fixmap_pgt, PAGE_KERNEL_RO);
  1331. /* Pin down new L4 */
  1332. pin_pagetable_pfn(MMUEXT_PIN_L4_TABLE,
  1333. PFN_DOWN(__pa_symbol(init_level4_pgt)));
  1334. /* Unpin Xen-provided one */
  1335. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
  1336. /* Switch over */
  1337. pgd = init_level4_pgt;
  1338. /*
  1339. * At this stage there can be no user pgd, and no page
  1340. * structure to attach it to, so make sure we just set kernel
  1341. * pgd.
  1342. */
  1343. xen_mc_batch();
  1344. __xen_write_cr3(true, __pa(pgd));
  1345. xen_mc_issue(PARAVIRT_LAZY_CPU);
  1346. reserve_early(__pa(xen_start_info->pt_base),
  1347. __pa(xen_start_info->pt_base +
  1348. xen_start_info->nr_pt_frames * PAGE_SIZE),
  1349. "XEN PAGETABLES");
  1350. return pgd;
  1351. }
  1352. #else /* !CONFIG_X86_64 */
  1353. static pmd_t level2_kernel_pgt[PTRS_PER_PMD] __page_aligned_bss;
  1354. static __init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd, unsigned long max_pfn)
  1355. {
  1356. pmd_t *kernel_pmd;
  1357. init_pg_tables_start = __pa(pgd);
  1358. init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE;
  1359. max_pfn_mapped = PFN_DOWN(init_pg_tables_end + 512*1024);
  1360. kernel_pmd = m2v(pgd[KERNEL_PGD_BOUNDARY].pgd);
  1361. memcpy(level2_kernel_pgt, kernel_pmd, sizeof(pmd_t) * PTRS_PER_PMD);
  1362. xen_map_identity_early(level2_kernel_pgt, max_pfn);
  1363. memcpy(swapper_pg_dir, pgd, sizeof(pgd_t) * PTRS_PER_PGD);
  1364. set_pgd(&swapper_pg_dir[KERNEL_PGD_BOUNDARY],
  1365. __pgd(__pa(level2_kernel_pgt) | _PAGE_PRESENT));
  1366. set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO);
  1367. set_page_prot(swapper_pg_dir, PAGE_KERNEL_RO);
  1368. set_page_prot(empty_zero_page, PAGE_KERNEL_RO);
  1369. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
  1370. xen_write_cr3(__pa(swapper_pg_dir));
  1371. pin_pagetable_pfn(MMUEXT_PIN_L3_TABLE, PFN_DOWN(__pa(swapper_pg_dir)));
  1372. return swapper_pg_dir;
  1373. }
  1374. #endif /* CONFIG_X86_64 */
  1375. /* First C function to be called on Xen boot */
  1376. asmlinkage void __init xen_start_kernel(void)
  1377. {
  1378. pgd_t *pgd;
  1379. if (!xen_start_info)
  1380. return;
  1381. BUG_ON(memcmp(xen_start_info->magic, "xen-3", 5) != 0);
  1382. xen_setup_features();
  1383. /* Install Xen paravirt ops */
  1384. pv_info = xen_info;
  1385. pv_init_ops = xen_init_ops;
  1386. pv_time_ops = xen_time_ops;
  1387. pv_cpu_ops = xen_cpu_ops;
  1388. pv_irq_ops = xen_irq_ops;
  1389. pv_apic_ops = xen_apic_ops;
  1390. pv_mmu_ops = xen_mmu_ops;
  1391. if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
  1392. pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
  1393. pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
  1394. }
  1395. machine_ops = xen_machine_ops;
  1396. #ifdef CONFIG_X86_64
  1397. /* Disable until direct per-cpu data access. */
  1398. have_vcpu_info_placement = 0;
  1399. x86_64_init_pda();
  1400. #endif
  1401. xen_smp_init();
  1402. /* Get mfn list */
  1403. if (!xen_feature(XENFEAT_auto_translated_physmap))
  1404. xen_build_dynamic_phys_to_machine();
  1405. pgd = (pgd_t *)xen_start_info->pt_base;
  1406. /* Prevent unwanted bits from being set in PTEs. */
  1407. __supported_pte_mask &= ~_PAGE_GLOBAL;
  1408. if (!is_initial_xendomain())
  1409. __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
  1410. /* Don't do the full vcpu_info placement stuff until we have a
  1411. possible map and a non-dummy shared_info. */
  1412. per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
  1413. xen_raw_console_write("mapping kernel into physical memory\n");
  1414. pgd = xen_setup_kernel_pagetable(pgd, xen_start_info->nr_pages);
  1415. init_mm.pgd = pgd;
  1416. /* keep using Xen gdt for now; no urgent need to change it */
  1417. pv_info.kernel_rpl = 1;
  1418. if (xen_feature(XENFEAT_supervisor_mode_kernel))
  1419. pv_info.kernel_rpl = 0;
  1420. /* set the limit of our address space */
  1421. xen_reserve_top();
  1422. #ifdef CONFIG_X86_32
  1423. /* set up basic CPUID stuff */
  1424. cpu_detect(&new_cpu_data);
  1425. new_cpu_data.hard_math = 1;
  1426. new_cpu_data.x86_capability[0] = cpuid_edx(1);
  1427. #endif
  1428. /* Poke various useful things into boot_params */
  1429. boot_params.hdr.type_of_loader = (9 << 4) | 0;
  1430. boot_params.hdr.ramdisk_image = xen_start_info->mod_start
  1431. ? __pa(xen_start_info->mod_start) : 0;
  1432. boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
  1433. boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
  1434. if (!is_initial_xendomain()) {
  1435. add_preferred_console("xenboot", 0, NULL);
  1436. add_preferred_console("tty", 0, NULL);
  1437. add_preferred_console("hvc", 0, NULL);
  1438. }
  1439. xen_raw_console_write("about to get started...\n");
  1440. #if 0
  1441. xen_raw_printk("&boot_params=%p __pa(&boot_params)=%lx __va(__pa(&boot_params))=%lx\n",
  1442. &boot_params, __pa_symbol(&boot_params),
  1443. __va(__pa_symbol(&boot_params)));
  1444. walk(pgd, &boot_params);
  1445. walk(pgd, __va(__pa(&boot_params)));
  1446. #endif
  1447. /* Start the world */
  1448. #ifdef CONFIG_X86_32
  1449. i386_start_kernel();
  1450. #else
  1451. x86_64_start_reservations((char *)__pa_symbol(&boot_params));
  1452. #endif
  1453. }