enlighten.c 40 KB

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