enlighten.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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 <asm/paravirt.h>
  35. #include <asm/page.h>
  36. #include <asm/xen/hypercall.h>
  37. #include <asm/xen/hypervisor.h>
  38. #include <asm/fixmap.h>
  39. #include <asm/processor.h>
  40. #include <asm/setup.h>
  41. #include <asm/desc.h>
  42. #include <asm/pgtable.h>
  43. #include <asm/tlbflush.h>
  44. #include <asm/reboot.h>
  45. #include "xen-ops.h"
  46. #include "mmu.h"
  47. #include "multicalls.h"
  48. EXPORT_SYMBOL_GPL(hypercall_page);
  49. DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
  50. DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
  51. /*
  52. * Note about cr3 (pagetable base) values:
  53. *
  54. * xen_cr3 contains the current logical cr3 value; it contains the
  55. * last set cr3. This may not be the current effective cr3, because
  56. * its update may be being lazily deferred. However, a vcpu looking
  57. * at its own cr3 can use this value knowing that it everything will
  58. * be self-consistent.
  59. *
  60. * xen_current_cr3 contains the actual vcpu cr3; it is set once the
  61. * hypercall to set the vcpu cr3 is complete (so it may be a little
  62. * out of date, but it will never be set early). If one vcpu is
  63. * looking at another vcpu's cr3 value, it should use this variable.
  64. */
  65. DEFINE_PER_CPU(unsigned long, xen_cr3); /* cr3 stored as physaddr */
  66. DEFINE_PER_CPU(unsigned long, xen_current_cr3); /* actual vcpu cr3 */
  67. struct start_info *xen_start_info;
  68. EXPORT_SYMBOL_GPL(xen_start_info);
  69. static /* __initdata */ struct shared_info dummy_shared_info;
  70. /*
  71. * Point at some empty memory to start with. We map the real shared_info
  72. * page as soon as fixmap is up and running.
  73. */
  74. struct shared_info *HYPERVISOR_shared_info = (void *)&dummy_shared_info;
  75. /*
  76. * Flag to determine whether vcpu info placement is available on all
  77. * VCPUs. We assume it is to start with, and then set it to zero on
  78. * the first failure. This is because it can succeed on some VCPUs
  79. * and not others, since it can involve hypervisor memory allocation,
  80. * or because the guest failed to guarantee all the appropriate
  81. * constraints on all VCPUs (ie buffer can't cross a page boundary).
  82. *
  83. * Note that any particular CPU may be using a placed vcpu structure,
  84. * but we can only optimise if the all are.
  85. *
  86. * 0: not available, 1: available
  87. */
  88. static int have_vcpu_info_placement = 1;
  89. static void __init xen_vcpu_setup(int cpu)
  90. {
  91. struct vcpu_register_vcpu_info info;
  92. int err;
  93. struct vcpu_info *vcpup;
  94. BUG_ON(HYPERVISOR_shared_info == &dummy_shared_info);
  95. per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
  96. if (!have_vcpu_info_placement)
  97. return; /* already tested, not available */
  98. vcpup = &per_cpu(xen_vcpu_info, cpu);
  99. info.mfn = virt_to_mfn(vcpup);
  100. info.offset = offset_in_page(vcpup);
  101. printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %llx, offset %d\n",
  102. cpu, vcpup, info.mfn, info.offset);
  103. /* Check to see if the hypervisor will put the vcpu_info
  104. structure where we want it, which allows direct access via
  105. a percpu-variable. */
  106. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
  107. if (err) {
  108. printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
  109. have_vcpu_info_placement = 0;
  110. } else {
  111. /* This cpu is using the registered vcpu info, even if
  112. later ones fail to. */
  113. per_cpu(xen_vcpu, cpu) = vcpup;
  114. printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
  115. cpu, vcpup);
  116. }
  117. }
  118. static void __init xen_banner(void)
  119. {
  120. printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
  121. pv_info.name);
  122. printk(KERN_INFO "Hypervisor signature: %s\n", xen_start_info->magic);
  123. }
  124. static void xen_cpuid(unsigned int *ax, unsigned int *bx,
  125. unsigned int *cx, unsigned int *dx)
  126. {
  127. unsigned maskedx = ~0;
  128. /*
  129. * Mask out inconvenient features, to try and disable as many
  130. * unsupported kernel subsystems as possible.
  131. */
  132. if (*ax == 1)
  133. maskedx = ~((1 << X86_FEATURE_APIC) | /* disable APIC */
  134. (1 << X86_FEATURE_ACPI) | /* disable ACPI */
  135. (1 << X86_FEATURE_MCE) | /* disable MCE */
  136. (1 << X86_FEATURE_MCA) | /* disable MCA */
  137. (1 << X86_FEATURE_ACC)); /* thermal monitoring */
  138. asm(XEN_EMULATE_PREFIX "cpuid"
  139. : "=a" (*ax),
  140. "=b" (*bx),
  141. "=c" (*cx),
  142. "=d" (*dx)
  143. : "0" (*ax), "2" (*cx));
  144. *dx &= maskedx;
  145. }
  146. static void xen_set_debugreg(int reg, unsigned long val)
  147. {
  148. HYPERVISOR_set_debugreg(reg, val);
  149. }
  150. static unsigned long xen_get_debugreg(int reg)
  151. {
  152. return HYPERVISOR_get_debugreg(reg);
  153. }
  154. static unsigned long xen_save_fl(void)
  155. {
  156. struct vcpu_info *vcpu;
  157. unsigned long flags;
  158. vcpu = x86_read_percpu(xen_vcpu);
  159. /* flag has opposite sense of mask */
  160. flags = !vcpu->evtchn_upcall_mask;
  161. /* convert to IF type flag
  162. -0 -> 0x00000000
  163. -1 -> 0xffffffff
  164. */
  165. return (-flags) & X86_EFLAGS_IF;
  166. }
  167. static void xen_restore_fl(unsigned long flags)
  168. {
  169. struct vcpu_info *vcpu;
  170. /* convert from IF type flag */
  171. flags = !(flags & X86_EFLAGS_IF);
  172. /* There's a one instruction preempt window here. We need to
  173. make sure we're don't switch CPUs between getting the vcpu
  174. pointer and updating the mask. */
  175. preempt_disable();
  176. vcpu = x86_read_percpu(xen_vcpu);
  177. vcpu->evtchn_upcall_mask = flags;
  178. preempt_enable_no_resched();
  179. /* Doesn't matter if we get preempted here, because any
  180. pending event will get dealt with anyway. */
  181. if (flags == 0) {
  182. preempt_check_resched();
  183. barrier(); /* unmask then check (avoid races) */
  184. if (unlikely(vcpu->evtchn_upcall_pending))
  185. force_evtchn_callback();
  186. }
  187. }
  188. static void xen_irq_disable(void)
  189. {
  190. /* There's a one instruction preempt window here. We need to
  191. make sure we're don't switch CPUs between getting the vcpu
  192. pointer and updating the mask. */
  193. preempt_disable();
  194. x86_read_percpu(xen_vcpu)->evtchn_upcall_mask = 1;
  195. preempt_enable_no_resched();
  196. }
  197. static void xen_irq_enable(void)
  198. {
  199. struct vcpu_info *vcpu;
  200. /* There's a one instruction preempt window here. We need to
  201. make sure we're don't switch CPUs between getting the vcpu
  202. pointer and updating the mask. */
  203. preempt_disable();
  204. vcpu = x86_read_percpu(xen_vcpu);
  205. vcpu->evtchn_upcall_mask = 0;
  206. preempt_enable_no_resched();
  207. /* Doesn't matter if we get preempted here, because any
  208. pending event will get dealt with anyway. */
  209. barrier(); /* unmask then check (avoid races) */
  210. if (unlikely(vcpu->evtchn_upcall_pending))
  211. force_evtchn_callback();
  212. }
  213. static void xen_safe_halt(void)
  214. {
  215. /* Blocking includes an implicit local_irq_enable(). */
  216. if (HYPERVISOR_sched_op(SCHEDOP_block, 0) != 0)
  217. BUG();
  218. }
  219. static void xen_halt(void)
  220. {
  221. if (irqs_disabled())
  222. HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
  223. else
  224. xen_safe_halt();
  225. }
  226. static void xen_leave_lazy(void)
  227. {
  228. paravirt_leave_lazy(paravirt_get_lazy_mode());
  229. xen_mc_flush();
  230. }
  231. static unsigned long xen_store_tr(void)
  232. {
  233. return 0;
  234. }
  235. static void xen_set_ldt(const void *addr, unsigned entries)
  236. {
  237. struct mmuext_op *op;
  238. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  239. op = mcs.args;
  240. op->cmd = MMUEXT_SET_LDT;
  241. op->arg1.linear_addr = (unsigned long)addr;
  242. op->arg2.nr_ents = entries;
  243. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  244. xen_mc_issue(PARAVIRT_LAZY_CPU);
  245. }
  246. static void xen_load_gdt(const struct desc_ptr *dtr)
  247. {
  248. unsigned long *frames;
  249. unsigned long va = dtr->address;
  250. unsigned int size = dtr->size + 1;
  251. unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  252. int f;
  253. struct multicall_space mcs;
  254. /* A GDT can be up to 64k in size, which corresponds to 8192
  255. 8-byte entries, or 16 4k pages.. */
  256. BUG_ON(size > 65536);
  257. BUG_ON(va & ~PAGE_MASK);
  258. mcs = xen_mc_entry(sizeof(*frames) * pages);
  259. frames = mcs.args;
  260. for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
  261. frames[f] = virt_to_mfn(va);
  262. make_lowmem_page_readonly((void *)va);
  263. }
  264. MULTI_set_gdt(mcs.mc, frames, size / sizeof(struct desc_struct));
  265. xen_mc_issue(PARAVIRT_LAZY_CPU);
  266. }
  267. static void load_TLS_descriptor(struct thread_struct *t,
  268. unsigned int cpu, unsigned int i)
  269. {
  270. struct desc_struct *gdt = get_cpu_gdt_table(cpu);
  271. xmaddr_t maddr = virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
  272. struct multicall_space mc = __xen_mc_entry(0);
  273. MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
  274. }
  275. static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
  276. {
  277. xen_mc_batch();
  278. load_TLS_descriptor(t, cpu, 0);
  279. load_TLS_descriptor(t, cpu, 1);
  280. load_TLS_descriptor(t, cpu, 2);
  281. xen_mc_issue(PARAVIRT_LAZY_CPU);
  282. /*
  283. * XXX sleazy hack: If we're being called in a lazy-cpu zone,
  284. * it means we're in a context switch, and %gs has just been
  285. * saved. This means we can zero it out to prevent faults on
  286. * exit from the hypervisor if the next process has no %gs.
  287. * Either way, it has been saved, and the new value will get
  288. * loaded properly. This will go away as soon as Xen has been
  289. * modified to not save/restore %gs for normal hypercalls.
  290. */
  291. if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU)
  292. loadsegment(gs, 0);
  293. }
  294. static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
  295. const void *ptr)
  296. {
  297. unsigned long lp = (unsigned long)&dt[entrynum];
  298. xmaddr_t mach_lp = virt_to_machine(lp);
  299. u64 entry = *(u64 *)ptr;
  300. preempt_disable();
  301. xen_mc_flush();
  302. if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
  303. BUG();
  304. preempt_enable();
  305. }
  306. static int cvt_gate_to_trap(int vector, u32 low, u32 high,
  307. struct trap_info *info)
  308. {
  309. u8 type, dpl;
  310. type = (high >> 8) & 0x1f;
  311. dpl = (high >> 13) & 3;
  312. if (type != 0xf && type != 0xe)
  313. return 0;
  314. info->vector = vector;
  315. info->address = (high & 0xffff0000) | (low & 0x0000ffff);
  316. info->cs = low >> 16;
  317. info->flags = dpl;
  318. /* interrupt gates clear IF */
  319. if (type == 0xe)
  320. info->flags |= 4;
  321. return 1;
  322. }
  323. /* Locations of each CPU's IDT */
  324. static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
  325. /* Set an IDT entry. If the entry is part of the current IDT, then
  326. also update Xen. */
  327. static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
  328. {
  329. unsigned long p = (unsigned long)&dt[entrynum];
  330. unsigned long start, end;
  331. preempt_disable();
  332. start = __get_cpu_var(idt_desc).address;
  333. end = start + __get_cpu_var(idt_desc).size + 1;
  334. xen_mc_flush();
  335. native_write_idt_entry(dt, entrynum, g);
  336. if (p >= start && (p + 8) <= end) {
  337. struct trap_info info[2];
  338. u32 *desc = (u32 *)g;
  339. info[1].address = 0;
  340. if (cvt_gate_to_trap(entrynum, desc[0], desc[1], &info[0]))
  341. if (HYPERVISOR_set_trap_table(info))
  342. BUG();
  343. }
  344. preempt_enable();
  345. }
  346. static void xen_convert_trap_info(const struct desc_ptr *desc,
  347. struct trap_info *traps)
  348. {
  349. unsigned in, out, count;
  350. count = (desc->size+1) / 8;
  351. BUG_ON(count > 256);
  352. for (in = out = 0; in < count; in++) {
  353. const u32 *entry = (u32 *)(desc->address + in * 8);
  354. if (cvt_gate_to_trap(in, entry[0], entry[1], &traps[out]))
  355. out++;
  356. }
  357. traps[out].address = 0;
  358. }
  359. void xen_copy_trap_info(struct trap_info *traps)
  360. {
  361. const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
  362. xen_convert_trap_info(desc, traps);
  363. }
  364. /* Load a new IDT into Xen. In principle this can be per-CPU, so we
  365. hold a spinlock to protect the static traps[] array (static because
  366. it avoids allocation, and saves stack space). */
  367. static void xen_load_idt(const struct desc_ptr *desc)
  368. {
  369. static DEFINE_SPINLOCK(lock);
  370. static struct trap_info traps[257];
  371. spin_lock(&lock);
  372. __get_cpu_var(idt_desc) = *desc;
  373. xen_convert_trap_info(desc, traps);
  374. xen_mc_flush();
  375. if (HYPERVISOR_set_trap_table(traps))
  376. BUG();
  377. spin_unlock(&lock);
  378. }
  379. /* Write a GDT descriptor entry. Ignore LDT descriptors, since
  380. they're handled differently. */
  381. static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
  382. const void *desc, int type)
  383. {
  384. preempt_disable();
  385. switch (type) {
  386. case DESC_LDT:
  387. case DESC_TSS:
  388. /* ignore */
  389. break;
  390. default: {
  391. xmaddr_t maddr = virt_to_machine(&dt[entry]);
  392. xen_mc_flush();
  393. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  394. BUG();
  395. }
  396. }
  397. preempt_enable();
  398. }
  399. static void xen_load_sp0(struct tss_struct *tss,
  400. struct thread_struct *thread)
  401. {
  402. struct multicall_space mcs = xen_mc_entry(0);
  403. MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
  404. xen_mc_issue(PARAVIRT_LAZY_CPU);
  405. }
  406. static void xen_set_iopl_mask(unsigned mask)
  407. {
  408. struct physdev_set_iopl set_iopl;
  409. /* Force the change at ring 0. */
  410. set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
  411. HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  412. }
  413. static void xen_io_delay(void)
  414. {
  415. }
  416. #ifdef CONFIG_X86_LOCAL_APIC
  417. static u32 xen_apic_read(unsigned long reg)
  418. {
  419. return 0;
  420. }
  421. static void xen_apic_write(unsigned long reg, u32 val)
  422. {
  423. /* Warn to see if there's any stray references */
  424. WARN_ON(1);
  425. }
  426. #endif
  427. static void xen_flush_tlb(void)
  428. {
  429. struct mmuext_op *op;
  430. struct multicall_space mcs;
  431. preempt_disable();
  432. mcs = xen_mc_entry(sizeof(*op));
  433. op = mcs.args;
  434. op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
  435. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  436. xen_mc_issue(PARAVIRT_LAZY_MMU);
  437. preempt_enable();
  438. }
  439. static void xen_flush_tlb_single(unsigned long addr)
  440. {
  441. struct mmuext_op *op;
  442. struct multicall_space mcs;
  443. preempt_disable();
  444. mcs = xen_mc_entry(sizeof(*op));
  445. op = mcs.args;
  446. op->cmd = MMUEXT_INVLPG_LOCAL;
  447. op->arg1.linear_addr = addr & PAGE_MASK;
  448. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  449. xen_mc_issue(PARAVIRT_LAZY_MMU);
  450. preempt_enable();
  451. }
  452. static void xen_flush_tlb_others(const cpumask_t *cpus, struct mm_struct *mm,
  453. unsigned long va)
  454. {
  455. struct {
  456. struct mmuext_op op;
  457. cpumask_t mask;
  458. } *args;
  459. cpumask_t cpumask = *cpus;
  460. struct multicall_space mcs;
  461. /*
  462. * A couple of (to be removed) sanity checks:
  463. *
  464. * - current CPU must not be in mask
  465. * - mask must exist :)
  466. */
  467. BUG_ON(cpus_empty(cpumask));
  468. BUG_ON(cpu_isset(smp_processor_id(), cpumask));
  469. BUG_ON(!mm);
  470. /* If a CPU which we ran on has gone down, OK. */
  471. cpus_and(cpumask, cpumask, cpu_online_map);
  472. if (cpus_empty(cpumask))
  473. return;
  474. mcs = xen_mc_entry(sizeof(*args));
  475. args = mcs.args;
  476. args->mask = cpumask;
  477. args->op.arg2.vcpumask = &args->mask;
  478. if (va == TLB_FLUSH_ALL) {
  479. args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
  480. } else {
  481. args->op.cmd = MMUEXT_INVLPG_MULTI;
  482. args->op.arg1.linear_addr = va;
  483. }
  484. MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
  485. xen_mc_issue(PARAVIRT_LAZY_MMU);
  486. }
  487. static void xen_write_cr2(unsigned long cr2)
  488. {
  489. x86_read_percpu(xen_vcpu)->arch.cr2 = cr2;
  490. }
  491. static unsigned long xen_read_cr2(void)
  492. {
  493. return x86_read_percpu(xen_vcpu)->arch.cr2;
  494. }
  495. static unsigned long xen_read_cr2_direct(void)
  496. {
  497. return x86_read_percpu(xen_vcpu_info.arch.cr2);
  498. }
  499. static void xen_write_cr4(unsigned long cr4)
  500. {
  501. /* Just ignore cr4 changes; Xen doesn't allow us to do
  502. anything anyway. */
  503. }
  504. static unsigned long xen_read_cr3(void)
  505. {
  506. return x86_read_percpu(xen_cr3);
  507. }
  508. static void set_current_cr3(void *v)
  509. {
  510. x86_write_percpu(xen_current_cr3, (unsigned long)v);
  511. }
  512. static void xen_write_cr3(unsigned long cr3)
  513. {
  514. struct mmuext_op *op;
  515. struct multicall_space mcs;
  516. unsigned long mfn = pfn_to_mfn(PFN_DOWN(cr3));
  517. BUG_ON(preemptible());
  518. mcs = xen_mc_entry(sizeof(*op)); /* disables interrupts */
  519. /* Update while interrupts are disabled, so its atomic with
  520. respect to ipis */
  521. x86_write_percpu(xen_cr3, cr3);
  522. op = mcs.args;
  523. op->cmd = MMUEXT_NEW_BASEPTR;
  524. op->arg1.mfn = mfn;
  525. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  526. /* Update xen_update_cr3 once the batch has actually
  527. been submitted. */
  528. xen_mc_callback(set_current_cr3, (void *)cr3);
  529. xen_mc_issue(PARAVIRT_LAZY_CPU); /* interrupts restored */
  530. }
  531. /* Early in boot, while setting up the initial pagetable, assume
  532. everything is pinned. */
  533. static __init void xen_alloc_pte_init(struct mm_struct *mm, u32 pfn)
  534. {
  535. #ifdef CONFIG_FLATMEM
  536. BUG_ON(mem_map); /* should only be used early */
  537. #endif
  538. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  539. }
  540. /* Early release_pte assumes that all pts are pinned, since there's
  541. only init_mm and anything attached to that is pinned. */
  542. static void xen_release_pte_init(u32 pfn)
  543. {
  544. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  545. }
  546. static void pin_pagetable_pfn(unsigned cmd, unsigned long pfn)
  547. {
  548. struct mmuext_op op;
  549. op.cmd = cmd;
  550. op.arg1.mfn = pfn_to_mfn(pfn);
  551. if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
  552. BUG();
  553. }
  554. /* This needs to make sure the new pte page is pinned iff its being
  555. attached to a pinned pagetable. */
  556. static void xen_alloc_ptpage(struct mm_struct *mm, u32 pfn, unsigned level)
  557. {
  558. struct page *page = pfn_to_page(pfn);
  559. if (PagePinned(virt_to_page(mm->pgd))) {
  560. SetPagePinned(page);
  561. if (!PageHighMem(page)) {
  562. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  563. if (level == PT_PTE)
  564. pin_pagetable_pfn(MMUEXT_PIN_L1_TABLE, pfn);
  565. } else
  566. /* make sure there are no stray mappings of
  567. this page */
  568. kmap_flush_unused();
  569. }
  570. }
  571. static void xen_alloc_pte(struct mm_struct *mm, u32 pfn)
  572. {
  573. xen_alloc_ptpage(mm, pfn, PT_PTE);
  574. }
  575. static void xen_alloc_pmd(struct mm_struct *mm, u32 pfn)
  576. {
  577. xen_alloc_ptpage(mm, pfn, PT_PMD);
  578. }
  579. /* This should never happen until we're OK to use struct page */
  580. static void xen_release_ptpage(u32 pfn, unsigned level)
  581. {
  582. struct page *page = pfn_to_page(pfn);
  583. if (PagePinned(page)) {
  584. if (!PageHighMem(page)) {
  585. if (level == PT_PTE)
  586. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, pfn);
  587. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  588. }
  589. ClearPagePinned(page);
  590. }
  591. }
  592. static void xen_release_pte(u32 pfn)
  593. {
  594. xen_release_ptpage(pfn, PT_PTE);
  595. }
  596. static void xen_release_pmd(u32 pfn)
  597. {
  598. xen_release_ptpage(pfn, PT_PMD);
  599. }
  600. #ifdef CONFIG_HIGHPTE
  601. static void *xen_kmap_atomic_pte(struct page *page, enum km_type type)
  602. {
  603. pgprot_t prot = PAGE_KERNEL;
  604. if (PagePinned(page))
  605. prot = PAGE_KERNEL_RO;
  606. if (0 && PageHighMem(page))
  607. printk("mapping highpte %lx type %d prot %s\n",
  608. page_to_pfn(page), type,
  609. (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ");
  610. return kmap_atomic_prot(page, type, prot);
  611. }
  612. #endif
  613. static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
  614. {
  615. /* If there's an existing pte, then don't allow _PAGE_RW to be set */
  616. if (pte_val_ma(*ptep) & _PAGE_PRESENT)
  617. pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
  618. pte_val_ma(pte));
  619. return pte;
  620. }
  621. /* Init-time set_pte while constructing initial pagetables, which
  622. doesn't allow RO pagetable pages to be remapped RW */
  623. static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
  624. {
  625. pte = mask_rw_pte(ptep, pte);
  626. xen_set_pte(ptep, pte);
  627. }
  628. static __init void xen_pagetable_setup_start(pgd_t *base)
  629. {
  630. pgd_t *xen_pgd = (pgd_t *)xen_start_info->pt_base;
  631. int i;
  632. /* special set_pte for pagetable initialization */
  633. pv_mmu_ops.set_pte = xen_set_pte_init;
  634. init_mm.pgd = base;
  635. /*
  636. * copy top-level of Xen-supplied pagetable into place. This
  637. * is a stand-in while we copy the pmd pages.
  638. */
  639. memcpy(base, xen_pgd, PTRS_PER_PGD * sizeof(pgd_t));
  640. /*
  641. * For PAE, need to allocate new pmds, rather than
  642. * share Xen's, since Xen doesn't like pmd's being
  643. * shared between address spaces.
  644. */
  645. for (i = 0; i < PTRS_PER_PGD; i++) {
  646. if (pgd_val_ma(xen_pgd[i]) & _PAGE_PRESENT) {
  647. pmd_t *pmd = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
  648. memcpy(pmd, (void *)pgd_page_vaddr(xen_pgd[i]),
  649. PAGE_SIZE);
  650. make_lowmem_page_readonly(pmd);
  651. set_pgd(&base[i], __pgd(1 + __pa(pmd)));
  652. } else
  653. pgd_clear(&base[i]);
  654. }
  655. /* make sure zero_page is mapped RO so we can use it in pagetables */
  656. make_lowmem_page_readonly(empty_zero_page);
  657. make_lowmem_page_readonly(base);
  658. /*
  659. * Switch to new pagetable. This is done before
  660. * pagetable_init has done anything so that the new pages
  661. * added to the table can be prepared properly for Xen.
  662. */
  663. xen_write_cr3(__pa(base));
  664. /* Unpin initial Xen pagetable */
  665. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE,
  666. PFN_DOWN(__pa(xen_start_info->pt_base)));
  667. }
  668. static __init void setup_shared_info(void)
  669. {
  670. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  671. unsigned long addr = fix_to_virt(FIX_PARAVIRT_BOOTMAP);
  672. /*
  673. * Create a mapping for the shared info page.
  674. * Should be set_fixmap(), but shared_info is a machine
  675. * address with no corresponding pseudo-phys address.
  676. */
  677. set_pte_mfn(addr,
  678. PFN_DOWN(xen_start_info->shared_info),
  679. PAGE_KERNEL);
  680. HYPERVISOR_shared_info = (struct shared_info *)addr;
  681. } else
  682. HYPERVISOR_shared_info =
  683. (struct shared_info *)__va(xen_start_info->shared_info);
  684. #ifndef CONFIG_SMP
  685. /* In UP this is as good a place as any to set up shared info */
  686. xen_setup_vcpu_info_placement();
  687. #endif
  688. }
  689. static __init void xen_pagetable_setup_done(pgd_t *base)
  690. {
  691. /* This will work as long as patching hasn't happened yet
  692. (which it hasn't) */
  693. pv_mmu_ops.alloc_pte = xen_alloc_pte;
  694. pv_mmu_ops.alloc_pmd = xen_alloc_pmd;
  695. pv_mmu_ops.release_pte = xen_release_pte;
  696. pv_mmu_ops.release_pmd = xen_release_pmd;
  697. pv_mmu_ops.set_pte = xen_set_pte;
  698. setup_shared_info();
  699. /* Actually pin the pagetable down, but we can't set PG_pinned
  700. yet because the page structures don't exist yet. */
  701. pin_pagetable_pfn(MMUEXT_PIN_L3_TABLE, PFN_DOWN(__pa(base)));
  702. }
  703. /* This is called once we have the cpu_possible_map */
  704. void __init xen_setup_vcpu_info_placement(void)
  705. {
  706. int cpu;
  707. for_each_possible_cpu(cpu)
  708. xen_vcpu_setup(cpu);
  709. /* xen_vcpu_setup managed to place the vcpu_info within the
  710. percpu area for all cpus, so make use of it */
  711. if (have_vcpu_info_placement) {
  712. printk(KERN_INFO "Xen: using vcpu_info placement\n");
  713. pv_irq_ops.save_fl = xen_save_fl_direct;
  714. pv_irq_ops.restore_fl = xen_restore_fl_direct;
  715. pv_irq_ops.irq_disable = xen_irq_disable_direct;
  716. pv_irq_ops.irq_enable = xen_irq_enable_direct;
  717. pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
  718. }
  719. }
  720. static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
  721. unsigned long addr, unsigned len)
  722. {
  723. char *start, *end, *reloc;
  724. unsigned ret;
  725. start = end = reloc = NULL;
  726. #define SITE(op, x) \
  727. case PARAVIRT_PATCH(op.x): \
  728. if (have_vcpu_info_placement) { \
  729. start = (char *)xen_##x##_direct; \
  730. end = xen_##x##_direct_end; \
  731. reloc = xen_##x##_direct_reloc; \
  732. } \
  733. goto patch_site
  734. switch (type) {
  735. SITE(pv_irq_ops, irq_enable);
  736. SITE(pv_irq_ops, irq_disable);
  737. SITE(pv_irq_ops, save_fl);
  738. SITE(pv_irq_ops, restore_fl);
  739. #undef SITE
  740. patch_site:
  741. if (start == NULL || (end-start) > len)
  742. goto default_patch;
  743. ret = paravirt_patch_insns(insnbuf, len, start, end);
  744. /* Note: because reloc is assigned from something that
  745. appears to be an array, gcc assumes it's non-null,
  746. but doesn't know its relationship with start and
  747. end. */
  748. if (reloc > start && reloc < end) {
  749. int reloc_off = reloc - start;
  750. long *relocp = (long *)(insnbuf + reloc_off);
  751. long delta = start - (char *)addr;
  752. *relocp += delta;
  753. }
  754. break;
  755. default_patch:
  756. default:
  757. ret = paravirt_patch_default(type, clobbers, insnbuf,
  758. addr, len);
  759. break;
  760. }
  761. return ret;
  762. }
  763. static const struct pv_info xen_info __initdata = {
  764. .paravirt_enabled = 1,
  765. .shared_kernel_pmd = 0,
  766. .name = "Xen",
  767. };
  768. static const struct pv_init_ops xen_init_ops __initdata = {
  769. .patch = xen_patch,
  770. .banner = xen_banner,
  771. .memory_setup = xen_memory_setup,
  772. .arch_setup = xen_arch_setup,
  773. .post_allocator_init = xen_mark_init_mm_pinned,
  774. };
  775. static const struct pv_time_ops xen_time_ops __initdata = {
  776. .time_init = xen_time_init,
  777. .set_wallclock = xen_set_wallclock,
  778. .get_wallclock = xen_get_wallclock,
  779. .get_cpu_khz = xen_cpu_khz,
  780. .sched_clock = xen_sched_clock,
  781. };
  782. static const struct pv_cpu_ops xen_cpu_ops __initdata = {
  783. .cpuid = xen_cpuid,
  784. .set_debugreg = xen_set_debugreg,
  785. .get_debugreg = xen_get_debugreg,
  786. .clts = native_clts,
  787. .read_cr0 = native_read_cr0,
  788. .write_cr0 = native_write_cr0,
  789. .read_cr4 = native_read_cr4,
  790. .read_cr4_safe = native_read_cr4_safe,
  791. .write_cr4 = xen_write_cr4,
  792. .wbinvd = native_wbinvd,
  793. .read_msr = native_read_msr_safe,
  794. .write_msr = native_write_msr_safe,
  795. .read_tsc = native_read_tsc,
  796. .read_pmc = native_read_pmc,
  797. .iret = xen_iret,
  798. .irq_enable_syscall_ret = xen_sysexit,
  799. .load_tr_desc = paravirt_nop,
  800. .set_ldt = xen_set_ldt,
  801. .load_gdt = xen_load_gdt,
  802. .load_idt = xen_load_idt,
  803. .load_tls = xen_load_tls,
  804. .store_gdt = native_store_gdt,
  805. .store_idt = native_store_idt,
  806. .store_tr = xen_store_tr,
  807. .write_ldt_entry = xen_write_ldt_entry,
  808. .write_gdt_entry = xen_write_gdt_entry,
  809. .write_idt_entry = xen_write_idt_entry,
  810. .load_sp0 = xen_load_sp0,
  811. .set_iopl_mask = xen_set_iopl_mask,
  812. .io_delay = xen_io_delay,
  813. .lazy_mode = {
  814. .enter = paravirt_enter_lazy_cpu,
  815. .leave = xen_leave_lazy,
  816. },
  817. };
  818. static const struct pv_irq_ops xen_irq_ops __initdata = {
  819. .init_IRQ = xen_init_IRQ,
  820. .save_fl = xen_save_fl,
  821. .restore_fl = xen_restore_fl,
  822. .irq_disable = xen_irq_disable,
  823. .irq_enable = xen_irq_enable,
  824. .safe_halt = xen_safe_halt,
  825. .halt = xen_halt,
  826. };
  827. static const struct pv_apic_ops xen_apic_ops __initdata = {
  828. #ifdef CONFIG_X86_LOCAL_APIC
  829. .apic_write = xen_apic_write,
  830. .apic_write_atomic = xen_apic_write,
  831. .apic_read = xen_apic_read,
  832. .setup_boot_clock = paravirt_nop,
  833. .setup_secondary_clock = paravirt_nop,
  834. .startup_ipi_hook = paravirt_nop,
  835. #endif
  836. };
  837. static const struct pv_mmu_ops xen_mmu_ops __initdata = {
  838. .pagetable_setup_start = xen_pagetable_setup_start,
  839. .pagetable_setup_done = xen_pagetable_setup_done,
  840. .read_cr2 = xen_read_cr2,
  841. .write_cr2 = xen_write_cr2,
  842. .read_cr3 = xen_read_cr3,
  843. .write_cr3 = xen_write_cr3,
  844. .flush_tlb_user = xen_flush_tlb,
  845. .flush_tlb_kernel = xen_flush_tlb,
  846. .flush_tlb_single = xen_flush_tlb_single,
  847. .flush_tlb_others = xen_flush_tlb_others,
  848. .pte_update = paravirt_nop,
  849. .pte_update_defer = paravirt_nop,
  850. .alloc_pte = xen_alloc_pte_init,
  851. .release_pte = xen_release_pte_init,
  852. .alloc_pmd = xen_alloc_pte_init,
  853. .alloc_pmd_clone = paravirt_nop,
  854. .release_pmd = xen_release_pte_init,
  855. #ifdef CONFIG_HIGHPTE
  856. .kmap_atomic_pte = xen_kmap_atomic_pte,
  857. #endif
  858. .set_pte = NULL, /* see xen_pagetable_setup_* */
  859. .set_pte_at = xen_set_pte_at,
  860. .set_pmd = xen_set_pmd,
  861. .pte_val = xen_pte_val,
  862. .pgd_val = xen_pgd_val,
  863. .make_pte = xen_make_pte,
  864. .make_pgd = xen_make_pgd,
  865. .set_pte_atomic = xen_set_pte_atomic,
  866. .set_pte_present = xen_set_pte_at,
  867. .set_pud = xen_set_pud,
  868. .pte_clear = xen_pte_clear,
  869. .pmd_clear = xen_pmd_clear,
  870. .make_pmd = xen_make_pmd,
  871. .pmd_val = xen_pmd_val,
  872. .activate_mm = xen_activate_mm,
  873. .dup_mmap = xen_dup_mmap,
  874. .exit_mmap = xen_exit_mmap,
  875. .lazy_mode = {
  876. .enter = paravirt_enter_lazy_mmu,
  877. .leave = xen_leave_lazy,
  878. },
  879. };
  880. #ifdef CONFIG_SMP
  881. static const struct smp_ops xen_smp_ops __initdata = {
  882. .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
  883. .smp_prepare_cpus = xen_smp_prepare_cpus,
  884. .cpu_up = xen_cpu_up,
  885. .smp_cpus_done = xen_smp_cpus_done,
  886. .smp_send_stop = xen_smp_send_stop,
  887. .smp_send_reschedule = xen_smp_send_reschedule,
  888. .smp_call_function_mask = xen_smp_call_function_mask,
  889. };
  890. #endif /* CONFIG_SMP */
  891. static void xen_reboot(int reason)
  892. {
  893. #ifdef CONFIG_SMP
  894. smp_send_stop();
  895. #endif
  896. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, reason))
  897. BUG();
  898. }
  899. static void xen_restart(char *msg)
  900. {
  901. xen_reboot(SHUTDOWN_reboot);
  902. }
  903. static void xen_emergency_restart(void)
  904. {
  905. xen_reboot(SHUTDOWN_reboot);
  906. }
  907. static void xen_machine_halt(void)
  908. {
  909. xen_reboot(SHUTDOWN_poweroff);
  910. }
  911. static void xen_crash_shutdown(struct pt_regs *regs)
  912. {
  913. xen_reboot(SHUTDOWN_crash);
  914. }
  915. static const struct machine_ops __initdata xen_machine_ops = {
  916. .restart = xen_restart,
  917. .halt = xen_machine_halt,
  918. .power_off = xen_machine_halt,
  919. .shutdown = xen_machine_halt,
  920. .crash_shutdown = xen_crash_shutdown,
  921. .emergency_restart = xen_emergency_restart,
  922. };
  923. static void __init xen_reserve_top(void)
  924. {
  925. unsigned long top = HYPERVISOR_VIRT_START;
  926. struct xen_platform_parameters pp;
  927. if (HYPERVISOR_xen_version(XENVER_platform_parameters, &pp) == 0)
  928. top = pp.virt_start;
  929. reserve_top_address(-top + 2 * PAGE_SIZE);
  930. }
  931. /* First C function to be called on Xen boot */
  932. asmlinkage void __init xen_start_kernel(void)
  933. {
  934. pgd_t *pgd;
  935. if (!xen_start_info)
  936. return;
  937. BUG_ON(memcmp(xen_start_info->magic, "xen-3", 5) != 0);
  938. /* Install Xen paravirt ops */
  939. pv_info = xen_info;
  940. pv_init_ops = xen_init_ops;
  941. pv_time_ops = xen_time_ops;
  942. pv_cpu_ops = xen_cpu_ops;
  943. pv_irq_ops = xen_irq_ops;
  944. pv_apic_ops = xen_apic_ops;
  945. pv_mmu_ops = xen_mmu_ops;
  946. machine_ops = xen_machine_ops;
  947. #ifdef CONFIG_SMP
  948. smp_ops = xen_smp_ops;
  949. #endif
  950. xen_setup_features();
  951. /* Get mfn list */
  952. if (!xen_feature(XENFEAT_auto_translated_physmap))
  953. phys_to_machine_mapping = (unsigned long *)xen_start_info->mfn_list;
  954. pgd = (pgd_t *)xen_start_info->pt_base;
  955. init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE;
  956. init_mm.pgd = pgd; /* use the Xen pagetables to start */
  957. /* keep using Xen gdt for now; no urgent need to change it */
  958. x86_write_percpu(xen_cr3, __pa(pgd));
  959. x86_write_percpu(xen_current_cr3, __pa(pgd));
  960. /* Don't do the full vcpu_info placement stuff until we have a
  961. possible map and a non-dummy shared_info. */
  962. per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
  963. pv_info.kernel_rpl = 1;
  964. if (xen_feature(XENFEAT_supervisor_mode_kernel))
  965. pv_info.kernel_rpl = 0;
  966. /* Prevent unwanted bits from being set in PTEs. */
  967. __supported_pte_mask &= ~_PAGE_GLOBAL;
  968. if (!is_initial_xendomain())
  969. __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
  970. /* set the limit of our address space */
  971. xen_reserve_top();
  972. /* set up basic CPUID stuff */
  973. cpu_detect(&new_cpu_data);
  974. new_cpu_data.hard_math = 1;
  975. new_cpu_data.x86_capability[0] = cpuid_edx(1);
  976. /* Poke various useful things into boot_params */
  977. boot_params.hdr.type_of_loader = (9 << 4) | 0;
  978. boot_params.hdr.ramdisk_image = xen_start_info->mod_start
  979. ? __pa(xen_start_info->mod_start) : 0;
  980. boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
  981. if (!is_initial_xendomain())
  982. add_preferred_console("hvc", 0, NULL);
  983. /* Start the world */
  984. start_kernel();
  985. }