enlighten.c 29 KB

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