enlighten.c 27 KB

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