enlighten.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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 unsigned long xen_save_fl_direct(void)
  151. {
  152. unsigned long flags;
  153. /* flag has opposite sense of mask */
  154. flags = !x86_read_percpu(xen_vcpu_info.evtchn_upcall_mask);
  155. /* convert to IF type flag
  156. -0 -> 0x00000000
  157. -1 -> 0xffffffff
  158. */
  159. return (-flags) & X86_EFLAGS_IF;
  160. }
  161. static void xen_restore_fl(unsigned long flags)
  162. {
  163. struct vcpu_info *vcpu;
  164. /* convert from IF type flag */
  165. flags = !(flags & X86_EFLAGS_IF);
  166. /* There's a one instruction preempt window here. We need to
  167. make sure we're don't switch CPUs between getting the vcpu
  168. pointer and updating the mask. */
  169. preempt_disable();
  170. vcpu = x86_read_percpu(xen_vcpu);
  171. vcpu->evtchn_upcall_mask = flags;
  172. preempt_enable_no_resched();
  173. /* Doesn't matter if we get preempted here, because any
  174. pending event will get dealt with anyway. */
  175. if (flags == 0) {
  176. preempt_check_resched();
  177. barrier(); /* unmask then check (avoid races) */
  178. if (unlikely(vcpu->evtchn_upcall_pending))
  179. force_evtchn_callback();
  180. }
  181. }
  182. static void xen_restore_fl_direct(unsigned long flags)
  183. {
  184. /* convert from IF type flag */
  185. flags = !(flags & X86_EFLAGS_IF);
  186. /* This is an atomic update, so no need to worry about
  187. preemption. */
  188. x86_write_percpu(xen_vcpu_info.evtchn_upcall_mask, flags);
  189. /* If we get preempted here, then any pending event will be
  190. handled anyway. */
  191. if (flags == 0) {
  192. barrier(); /* unmask then check (avoid races) */
  193. if (unlikely(x86_read_percpu(xen_vcpu_info.evtchn_upcall_pending)))
  194. force_evtchn_callback();
  195. }
  196. }
  197. static void xen_irq_disable(void)
  198. {
  199. /* There's a one instruction preempt window here. We need to
  200. make sure we're don't switch CPUs between getting the vcpu
  201. pointer and updating the mask. */
  202. preempt_disable();
  203. x86_read_percpu(xen_vcpu)->evtchn_upcall_mask = 1;
  204. preempt_enable_no_resched();
  205. }
  206. static void xen_irq_disable_direct(void)
  207. {
  208. /* Atomic update, so preemption not a concern. */
  209. x86_write_percpu(xen_vcpu_info.evtchn_upcall_mask, 1);
  210. }
  211. static void xen_irq_enable(void)
  212. {
  213. struct vcpu_info *vcpu;
  214. /* There's a one instruction preempt window here. We need to
  215. make sure we're don't switch CPUs between getting the vcpu
  216. pointer and updating the mask. */
  217. preempt_disable();
  218. vcpu = x86_read_percpu(xen_vcpu);
  219. vcpu->evtchn_upcall_mask = 0;
  220. preempt_enable_no_resched();
  221. /* Doesn't matter if we get preempted here, because any
  222. pending event will get dealt with anyway. */
  223. barrier(); /* unmask then check (avoid races) */
  224. if (unlikely(vcpu->evtchn_upcall_pending))
  225. force_evtchn_callback();
  226. }
  227. static void xen_irq_enable_direct(void)
  228. {
  229. /* Atomic update, so preemption not a concern. */
  230. x86_write_percpu(xen_vcpu_info.evtchn_upcall_mask, 0);
  231. /* Doesn't matter if we get preempted here, because any
  232. pending event will get dealt with anyway. */
  233. barrier(); /* unmask then check (avoid races) */
  234. if (unlikely(x86_read_percpu(xen_vcpu_info.evtchn_upcall_pending)))
  235. force_evtchn_callback();
  236. }
  237. static void xen_safe_halt(void)
  238. {
  239. /* Blocking includes an implicit local_irq_enable(). */
  240. if (HYPERVISOR_sched_op(SCHEDOP_block, 0) != 0)
  241. BUG();
  242. }
  243. static void xen_halt(void)
  244. {
  245. if (irqs_disabled())
  246. HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
  247. else
  248. xen_safe_halt();
  249. }
  250. static void xen_set_lazy_mode(enum paravirt_lazy_mode mode)
  251. {
  252. BUG_ON(preemptible());
  253. switch (mode) {
  254. case PARAVIRT_LAZY_NONE:
  255. BUG_ON(x86_read_percpu(xen_lazy_mode) == PARAVIRT_LAZY_NONE);
  256. break;
  257. case PARAVIRT_LAZY_MMU:
  258. case PARAVIRT_LAZY_CPU:
  259. BUG_ON(x86_read_percpu(xen_lazy_mode) != PARAVIRT_LAZY_NONE);
  260. break;
  261. case PARAVIRT_LAZY_FLUSH:
  262. /* flush if necessary, but don't change state */
  263. if (x86_read_percpu(xen_lazy_mode) != PARAVIRT_LAZY_NONE)
  264. xen_mc_flush();
  265. return;
  266. }
  267. xen_mc_flush();
  268. x86_write_percpu(xen_lazy_mode, mode);
  269. }
  270. static unsigned long xen_store_tr(void)
  271. {
  272. return 0;
  273. }
  274. static void xen_set_ldt(const void *addr, unsigned entries)
  275. {
  276. unsigned long linear_addr = (unsigned long)addr;
  277. struct mmuext_op *op;
  278. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  279. op = mcs.args;
  280. op->cmd = MMUEXT_SET_LDT;
  281. if (linear_addr) {
  282. /* ldt my be vmalloced, use arbitrary_virt_to_machine */
  283. xmaddr_t maddr;
  284. maddr = arbitrary_virt_to_machine((unsigned long)addr);
  285. linear_addr = (unsigned long)maddr.maddr;
  286. }
  287. op->arg1.linear_addr = linear_addr;
  288. op->arg2.nr_ents = entries;
  289. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  290. xen_mc_issue(PARAVIRT_LAZY_CPU);
  291. }
  292. static void xen_load_gdt(const struct Xgt_desc_struct *dtr)
  293. {
  294. unsigned long *frames;
  295. unsigned long va = dtr->address;
  296. unsigned int size = dtr->size + 1;
  297. unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  298. int f;
  299. struct multicall_space mcs;
  300. /* A GDT can be up to 64k in size, which corresponds to 8192
  301. 8-byte entries, or 16 4k pages.. */
  302. BUG_ON(size > 65536);
  303. BUG_ON(va & ~PAGE_MASK);
  304. mcs = xen_mc_entry(sizeof(*frames) * pages);
  305. frames = mcs.args;
  306. for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
  307. frames[f] = virt_to_mfn(va);
  308. make_lowmem_page_readonly((void *)va);
  309. }
  310. MULTI_set_gdt(mcs.mc, frames, size / sizeof(struct desc_struct));
  311. xen_mc_issue(PARAVIRT_LAZY_CPU);
  312. }
  313. static void load_TLS_descriptor(struct thread_struct *t,
  314. unsigned int cpu, unsigned int i)
  315. {
  316. struct desc_struct *gdt = get_cpu_gdt_table(cpu);
  317. xmaddr_t maddr = virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
  318. struct multicall_space mc = __xen_mc_entry(0);
  319. MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
  320. }
  321. static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
  322. {
  323. xen_mc_batch();
  324. load_TLS_descriptor(t, cpu, 0);
  325. load_TLS_descriptor(t, cpu, 1);
  326. load_TLS_descriptor(t, cpu, 2);
  327. xen_mc_issue(PARAVIRT_LAZY_CPU);
  328. /*
  329. * XXX sleazy hack: If we're being called in a lazy-cpu zone,
  330. * it means we're in a context switch, and %gs has just been
  331. * saved. This means we can zero it out to prevent faults on
  332. * exit from the hypervisor if the next process has no %gs.
  333. * Either way, it has been saved, and the new value will get
  334. * loaded properly. This will go away as soon as Xen has been
  335. * modified to not save/restore %gs for normal hypercalls.
  336. */
  337. if (xen_get_lazy_mode() == PARAVIRT_LAZY_CPU)
  338. loadsegment(gs, 0);
  339. }
  340. static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
  341. u32 low, u32 high)
  342. {
  343. unsigned long lp = (unsigned long)&dt[entrynum];
  344. xmaddr_t mach_lp = virt_to_machine(lp);
  345. u64 entry = (u64)high << 32 | low;
  346. preempt_disable();
  347. xen_mc_flush();
  348. if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
  349. BUG();
  350. preempt_enable();
  351. }
  352. static int cvt_gate_to_trap(int vector, u32 low, u32 high,
  353. struct trap_info *info)
  354. {
  355. u8 type, dpl;
  356. type = (high >> 8) & 0x1f;
  357. dpl = (high >> 13) & 3;
  358. if (type != 0xf && type != 0xe)
  359. return 0;
  360. info->vector = vector;
  361. info->address = (high & 0xffff0000) | (low & 0x0000ffff);
  362. info->cs = low >> 16;
  363. info->flags = dpl;
  364. /* interrupt gates clear IF */
  365. if (type == 0xe)
  366. info->flags |= 4;
  367. return 1;
  368. }
  369. /* Locations of each CPU's IDT */
  370. static DEFINE_PER_CPU(struct Xgt_desc_struct, idt_desc);
  371. /* Set an IDT entry. If the entry is part of the current IDT, then
  372. also update Xen. */
  373. static void xen_write_idt_entry(struct desc_struct *dt, int entrynum,
  374. u32 low, u32 high)
  375. {
  376. unsigned long p = (unsigned long)&dt[entrynum];
  377. unsigned long start, end;
  378. preempt_disable();
  379. start = __get_cpu_var(idt_desc).address;
  380. end = start + __get_cpu_var(idt_desc).size + 1;
  381. xen_mc_flush();
  382. write_dt_entry(dt, entrynum, low, high);
  383. if (p >= start && (p + 8) <= end) {
  384. struct trap_info info[2];
  385. info[1].address = 0;
  386. if (cvt_gate_to_trap(entrynum, low, high, &info[0]))
  387. if (HYPERVISOR_set_trap_table(info))
  388. BUG();
  389. }
  390. preempt_enable();
  391. }
  392. static void xen_convert_trap_info(const struct Xgt_desc_struct *desc,
  393. struct trap_info *traps)
  394. {
  395. unsigned in, out, count;
  396. count = (desc->size+1) / 8;
  397. BUG_ON(count > 256);
  398. for (in = out = 0; in < count; in++) {
  399. const u32 *entry = (u32 *)(desc->address + in * 8);
  400. if (cvt_gate_to_trap(in, entry[0], entry[1], &traps[out]))
  401. out++;
  402. }
  403. traps[out].address = 0;
  404. }
  405. void xen_copy_trap_info(struct trap_info *traps)
  406. {
  407. const struct Xgt_desc_struct *desc = &__get_cpu_var(idt_desc);
  408. xen_convert_trap_info(desc, traps);
  409. }
  410. /* Load a new IDT into Xen. In principle this can be per-CPU, so we
  411. hold a spinlock to protect the static traps[] array (static because
  412. it avoids allocation, and saves stack space). */
  413. static void xen_load_idt(const struct Xgt_desc_struct *desc)
  414. {
  415. static DEFINE_SPINLOCK(lock);
  416. static struct trap_info traps[257];
  417. spin_lock(&lock);
  418. __get_cpu_var(idt_desc) = *desc;
  419. xen_convert_trap_info(desc, traps);
  420. xen_mc_flush();
  421. if (HYPERVISOR_set_trap_table(traps))
  422. BUG();
  423. spin_unlock(&lock);
  424. }
  425. /* Write a GDT descriptor entry. Ignore LDT descriptors, since
  426. they're handled differently. */
  427. static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
  428. u32 low, u32 high)
  429. {
  430. preempt_disable();
  431. switch ((high >> 8) & 0xff) {
  432. case DESCTYPE_LDT:
  433. case DESCTYPE_TSS:
  434. /* ignore */
  435. break;
  436. default: {
  437. xmaddr_t maddr = virt_to_machine(&dt[entry]);
  438. u64 desc = (u64)high << 32 | low;
  439. xen_mc_flush();
  440. if (HYPERVISOR_update_descriptor(maddr.maddr, desc))
  441. BUG();
  442. }
  443. }
  444. preempt_enable();
  445. }
  446. static void xen_load_esp0(struct tss_struct *tss,
  447. struct thread_struct *thread)
  448. {
  449. struct multicall_space mcs = xen_mc_entry(0);
  450. MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->esp0);
  451. xen_mc_issue(PARAVIRT_LAZY_CPU);
  452. }
  453. static void xen_set_iopl_mask(unsigned mask)
  454. {
  455. struct physdev_set_iopl set_iopl;
  456. /* Force the change at ring 0. */
  457. set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
  458. HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  459. }
  460. static void xen_io_delay(void)
  461. {
  462. }
  463. #ifdef CONFIG_X86_LOCAL_APIC
  464. static unsigned long xen_apic_read(unsigned long reg)
  465. {
  466. return 0;
  467. }
  468. static void xen_apic_write(unsigned long reg, unsigned long val)
  469. {
  470. /* Warn to see if there's any stray references */
  471. WARN_ON(1);
  472. }
  473. #endif
  474. static void xen_flush_tlb(void)
  475. {
  476. struct mmuext_op *op;
  477. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  478. op = mcs.args;
  479. op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
  480. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  481. xen_mc_issue(PARAVIRT_LAZY_MMU);
  482. }
  483. static void xen_flush_tlb_single(unsigned long addr)
  484. {
  485. struct mmuext_op *op;
  486. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  487. op = mcs.args;
  488. op->cmd = MMUEXT_INVLPG_LOCAL;
  489. op->arg1.linear_addr = addr & PAGE_MASK;
  490. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  491. xen_mc_issue(PARAVIRT_LAZY_MMU);
  492. }
  493. static void xen_flush_tlb_others(const cpumask_t *cpus, struct mm_struct *mm,
  494. unsigned long va)
  495. {
  496. struct {
  497. struct mmuext_op op;
  498. cpumask_t mask;
  499. } *args;
  500. cpumask_t cpumask = *cpus;
  501. struct multicall_space mcs;
  502. /*
  503. * A couple of (to be removed) sanity checks:
  504. *
  505. * - current CPU must not be in mask
  506. * - mask must exist :)
  507. */
  508. BUG_ON(cpus_empty(cpumask));
  509. BUG_ON(cpu_isset(smp_processor_id(), cpumask));
  510. BUG_ON(!mm);
  511. /* If a CPU which we ran on has gone down, OK. */
  512. cpus_and(cpumask, cpumask, cpu_online_map);
  513. if (cpus_empty(cpumask))
  514. return;
  515. mcs = xen_mc_entry(sizeof(*args));
  516. args = mcs.args;
  517. args->mask = cpumask;
  518. args->op.arg2.vcpumask = &args->mask;
  519. if (va == TLB_FLUSH_ALL) {
  520. args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
  521. } else {
  522. args->op.cmd = MMUEXT_INVLPG_MULTI;
  523. args->op.arg1.linear_addr = va;
  524. }
  525. MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
  526. xen_mc_issue(PARAVIRT_LAZY_MMU);
  527. }
  528. static void xen_write_cr2(unsigned long cr2)
  529. {
  530. x86_read_percpu(xen_vcpu)->arch.cr2 = cr2;
  531. }
  532. static unsigned long xen_read_cr2(void)
  533. {
  534. return x86_read_percpu(xen_vcpu)->arch.cr2;
  535. }
  536. static unsigned long xen_read_cr2_direct(void)
  537. {
  538. return x86_read_percpu(xen_vcpu_info.arch.cr2);
  539. }
  540. static void xen_write_cr4(unsigned long cr4)
  541. {
  542. /* never allow TSC to be disabled */
  543. native_write_cr4(cr4 & ~X86_CR4_TSD);
  544. }
  545. static unsigned long xen_read_cr3(void)
  546. {
  547. return x86_read_percpu(xen_cr3);
  548. }
  549. static void xen_write_cr3(unsigned long cr3)
  550. {
  551. BUG_ON(preemptible());
  552. if (cr3 == x86_read_percpu(xen_cr3)) {
  553. /* just a simple tlb flush */
  554. xen_flush_tlb();
  555. return;
  556. }
  557. x86_write_percpu(xen_cr3, cr3);
  558. {
  559. struct mmuext_op *op;
  560. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  561. unsigned long mfn = pfn_to_mfn(PFN_DOWN(cr3));
  562. op = mcs.args;
  563. op->cmd = MMUEXT_NEW_BASEPTR;
  564. op->arg1.mfn = mfn;
  565. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  566. xen_mc_issue(PARAVIRT_LAZY_CPU);
  567. }
  568. }
  569. /* Early in boot, while setting up the initial pagetable, assume
  570. everything is pinned. */
  571. static __init void xen_alloc_pt_init(struct mm_struct *mm, u32 pfn)
  572. {
  573. BUG_ON(mem_map); /* should only be used early */
  574. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  575. }
  576. /* This needs to make sure the new pte page is pinned iff its being
  577. attached to a pinned pagetable. */
  578. static void xen_alloc_pt(struct mm_struct *mm, u32 pfn)
  579. {
  580. struct page *page = pfn_to_page(pfn);
  581. if (PagePinned(virt_to_page(mm->pgd))) {
  582. SetPagePinned(page);
  583. if (!PageHighMem(page))
  584. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  585. else
  586. /* make sure there are no stray mappings of
  587. this page */
  588. kmap_flush_unused();
  589. }
  590. }
  591. /* This should never happen until we're OK to use struct page */
  592. static void xen_release_pt(u32 pfn)
  593. {
  594. struct page *page = pfn_to_page(pfn);
  595. if (PagePinned(page)) {
  596. if (!PageHighMem(page))
  597. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  598. }
  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. /* special set_pte for pagetable initialization */
  632. paravirt_ops.set_pte = xen_set_pte_init;
  633. init_mm.pgd = base;
  634. /*
  635. * copy top-level of Xen-supplied pagetable into place. For
  636. * !PAE we can use this as-is, but for PAE it is a stand-in
  637. * while we copy the pmd pages.
  638. */
  639. memcpy(base, xen_pgd, PTRS_PER_PGD * sizeof(pgd_t));
  640. if (PTRS_PER_PMD > 1) {
  641. int i;
  642. /*
  643. * For PAE, need to allocate new pmds, rather than
  644. * share Xen's, since Xen doesn't like pmd's being
  645. * shared between address spaces.
  646. */
  647. for (i = 0; i < PTRS_PER_PGD; i++) {
  648. if (pgd_val_ma(xen_pgd[i]) & _PAGE_PRESENT) {
  649. pmd_t *pmd = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
  650. memcpy(pmd, (void *)pgd_page_vaddr(xen_pgd[i]),
  651. PAGE_SIZE);
  652. make_lowmem_page_readonly(pmd);
  653. set_pgd(&base[i], __pgd(1 + __pa(pmd)));
  654. } else
  655. pgd_clear(&base[i]);
  656. }
  657. }
  658. /* make sure zero_page is mapped RO so we can use it in pagetables */
  659. make_lowmem_page_readonly(empty_zero_page);
  660. make_lowmem_page_readonly(base);
  661. /*
  662. * Switch to new pagetable. This is done before
  663. * pagetable_init has done anything so that the new pages
  664. * added to the table can be prepared properly for Xen.
  665. */
  666. xen_write_cr3(__pa(base));
  667. }
  668. static __init void xen_pagetable_setup_done(pgd_t *base)
  669. {
  670. /* This will work as long as patching hasn't happened yet
  671. (which it hasn't) */
  672. paravirt_ops.alloc_pt = xen_alloc_pt;
  673. paravirt_ops.set_pte = xen_set_pte;
  674. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  675. /*
  676. * Create a mapping for the shared info page.
  677. * Should be set_fixmap(), but shared_info is a machine
  678. * address with no corresponding pseudo-phys address.
  679. */
  680. set_pte_mfn(fix_to_virt(FIX_PARAVIRT_BOOTMAP),
  681. PFN_DOWN(xen_start_info->shared_info),
  682. PAGE_KERNEL);
  683. HYPERVISOR_shared_info =
  684. (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
  685. } else
  686. HYPERVISOR_shared_info =
  687. (struct shared_info *)__va(xen_start_info->shared_info);
  688. /* Actually pin the pagetable down, but we can't set PG_pinned
  689. yet because the page structures don't exist yet. */
  690. {
  691. struct mmuext_op op;
  692. #ifdef CONFIG_X86_PAE
  693. op.cmd = MMUEXT_PIN_L3_TABLE;
  694. #else
  695. op.cmd = MMUEXT_PIN_L3_TABLE;
  696. #endif
  697. op.arg1.mfn = pfn_to_mfn(PFN_DOWN(__pa(base)));
  698. if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
  699. BUG();
  700. }
  701. }
  702. /* This is called once we have the cpu_possible_map */
  703. void __init xen_setup_vcpu_info_placement(void)
  704. {
  705. int cpu;
  706. for_each_possible_cpu(cpu)
  707. xen_vcpu_setup(cpu);
  708. /* xen_vcpu_setup managed to place the vcpu_info within the
  709. percpu area for all cpus, so make use of it */
  710. if (have_vcpu_info_placement) {
  711. printk(KERN_INFO "Xen: using vcpu_info placement\n");
  712. paravirt_ops.save_fl = xen_save_fl_direct;
  713. paravirt_ops.restore_fl = xen_restore_fl_direct;
  714. paravirt_ops.irq_disable = xen_irq_disable_direct;
  715. paravirt_ops.irq_enable = xen_irq_enable_direct;
  716. paravirt_ops.read_cr2 = xen_read_cr2_direct;
  717. }
  718. }
  719. static const struct paravirt_ops xen_paravirt_ops __initdata = {
  720. .paravirt_enabled = 1,
  721. .shared_kernel_pmd = 0,
  722. .name = "Xen",
  723. .banner = xen_banner,
  724. .patch = paravirt_patch_default,
  725. .memory_setup = xen_memory_setup,
  726. .arch_setup = xen_arch_setup,
  727. .init_IRQ = xen_init_IRQ,
  728. .post_allocator_init = xen_mark_init_mm_pinned,
  729. .time_init = xen_time_init,
  730. .set_wallclock = xen_set_wallclock,
  731. .get_wallclock = xen_get_wallclock,
  732. .get_cpu_khz = xen_cpu_khz,
  733. .sched_clock = xen_sched_clock,
  734. .cpuid = xen_cpuid,
  735. .set_debugreg = xen_set_debugreg,
  736. .get_debugreg = xen_get_debugreg,
  737. .clts = native_clts,
  738. .read_cr0 = native_read_cr0,
  739. .write_cr0 = native_write_cr0,
  740. .read_cr2 = xen_read_cr2,
  741. .write_cr2 = xen_write_cr2,
  742. .read_cr3 = xen_read_cr3,
  743. .write_cr3 = xen_write_cr3,
  744. .read_cr4 = native_read_cr4,
  745. .read_cr4_safe = native_read_cr4_safe,
  746. .write_cr4 = xen_write_cr4,
  747. .save_fl = xen_save_fl,
  748. .restore_fl = xen_restore_fl,
  749. .irq_disable = xen_irq_disable,
  750. .irq_enable = xen_irq_enable,
  751. .safe_halt = xen_safe_halt,
  752. .halt = xen_halt,
  753. .wbinvd = native_wbinvd,
  754. .read_msr = native_read_msr_safe,
  755. .write_msr = native_write_msr_safe,
  756. .read_tsc = native_read_tsc,
  757. .read_pmc = native_read_pmc,
  758. .iret = (void *)&hypercall_page[__HYPERVISOR_iret],
  759. .irq_enable_sysexit = NULL, /* never called */
  760. .load_tr_desc = paravirt_nop,
  761. .set_ldt = xen_set_ldt,
  762. .load_gdt = xen_load_gdt,
  763. .load_idt = xen_load_idt,
  764. .load_tls = xen_load_tls,
  765. .store_gdt = native_store_gdt,
  766. .store_idt = native_store_idt,
  767. .store_tr = xen_store_tr,
  768. .write_ldt_entry = xen_write_ldt_entry,
  769. .write_gdt_entry = xen_write_gdt_entry,
  770. .write_idt_entry = xen_write_idt_entry,
  771. .load_esp0 = xen_load_esp0,
  772. .set_iopl_mask = xen_set_iopl_mask,
  773. .io_delay = xen_io_delay,
  774. #ifdef CONFIG_X86_LOCAL_APIC
  775. .apic_write = xen_apic_write,
  776. .apic_write_atomic = xen_apic_write,
  777. .apic_read = xen_apic_read,
  778. .setup_boot_clock = paravirt_nop,
  779. .setup_secondary_clock = paravirt_nop,
  780. .startup_ipi_hook = paravirt_nop,
  781. #endif
  782. .flush_tlb_user = xen_flush_tlb,
  783. .flush_tlb_kernel = xen_flush_tlb,
  784. .flush_tlb_single = xen_flush_tlb_single,
  785. .flush_tlb_others = xen_flush_tlb_others,
  786. .pte_update = paravirt_nop,
  787. .pte_update_defer = paravirt_nop,
  788. .pagetable_setup_start = xen_pagetable_setup_start,
  789. .pagetable_setup_done = xen_pagetable_setup_done,
  790. .alloc_pt = xen_alloc_pt_init,
  791. .release_pt = xen_release_pt,
  792. .alloc_pd = paravirt_nop,
  793. .alloc_pd_clone = paravirt_nop,
  794. .release_pd = paravirt_nop,
  795. #ifdef CONFIG_HIGHPTE
  796. .kmap_atomic_pte = xen_kmap_atomic_pte,
  797. #endif
  798. .set_pte = NULL, /* see xen_pagetable_setup_* */
  799. .set_pte_at = xen_set_pte_at,
  800. .set_pmd = xen_set_pmd,
  801. .pte_val = xen_pte_val,
  802. .pgd_val = xen_pgd_val,
  803. .make_pte = xen_make_pte,
  804. .make_pgd = xen_make_pgd,
  805. #ifdef CONFIG_X86_PAE
  806. .set_pte_atomic = xen_set_pte_atomic,
  807. .set_pte_present = xen_set_pte_at,
  808. .set_pud = xen_set_pud,
  809. .pte_clear = xen_pte_clear,
  810. .pmd_clear = xen_pmd_clear,
  811. .make_pmd = xen_make_pmd,
  812. .pmd_val = xen_pmd_val,
  813. #endif /* PAE */
  814. .activate_mm = xen_activate_mm,
  815. .dup_mmap = xen_dup_mmap,
  816. .exit_mmap = xen_exit_mmap,
  817. .set_lazy_mode = xen_set_lazy_mode,
  818. };
  819. #ifdef CONFIG_SMP
  820. static const struct smp_ops xen_smp_ops __initdata = {
  821. .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
  822. .smp_prepare_cpus = xen_smp_prepare_cpus,
  823. .cpu_up = xen_cpu_up,
  824. .smp_cpus_done = xen_smp_cpus_done,
  825. .smp_send_stop = xen_smp_send_stop,
  826. .smp_send_reschedule = xen_smp_send_reschedule,
  827. .smp_call_function_mask = xen_smp_call_function_mask,
  828. };
  829. #endif /* CONFIG_SMP */
  830. static void xen_reboot(int reason)
  831. {
  832. #ifdef CONFIG_SMP
  833. smp_send_stop();
  834. #endif
  835. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, reason))
  836. BUG();
  837. }
  838. static void xen_restart(char *msg)
  839. {
  840. xen_reboot(SHUTDOWN_reboot);
  841. }
  842. static void xen_emergency_restart(void)
  843. {
  844. xen_reboot(SHUTDOWN_reboot);
  845. }
  846. static void xen_machine_halt(void)
  847. {
  848. xen_reboot(SHUTDOWN_poweroff);
  849. }
  850. static void xen_crash_shutdown(struct pt_regs *regs)
  851. {
  852. xen_reboot(SHUTDOWN_crash);
  853. }
  854. static const struct machine_ops __initdata xen_machine_ops = {
  855. .restart = xen_restart,
  856. .halt = xen_machine_halt,
  857. .power_off = xen_machine_halt,
  858. .shutdown = xen_machine_halt,
  859. .crash_shutdown = xen_crash_shutdown,
  860. .emergency_restart = xen_emergency_restart,
  861. };
  862. /* First C function to be called on Xen boot */
  863. asmlinkage void __init xen_start_kernel(void)
  864. {
  865. pgd_t *pgd;
  866. if (!xen_start_info)
  867. return;
  868. BUG_ON(memcmp(xen_start_info->magic, "xen-3.0", 7) != 0);
  869. /* Install Xen paravirt ops */
  870. paravirt_ops = xen_paravirt_ops;
  871. machine_ops = xen_machine_ops;
  872. #ifdef CONFIG_SMP
  873. smp_ops = xen_smp_ops;
  874. #endif
  875. xen_setup_features();
  876. /* Get mfn list */
  877. if (!xen_feature(XENFEAT_auto_translated_physmap))
  878. phys_to_machine_mapping = (unsigned long *)xen_start_info->mfn_list;
  879. pgd = (pgd_t *)xen_start_info->pt_base;
  880. init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE;
  881. init_mm.pgd = pgd; /* use the Xen pagetables to start */
  882. /* keep using Xen gdt for now; no urgent need to change it */
  883. x86_write_percpu(xen_cr3, __pa(pgd));
  884. #ifdef CONFIG_SMP
  885. /* Don't do the full vcpu_info placement stuff until we have a
  886. possible map. */
  887. per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
  888. #else
  889. /* May as well do it now, since there's no good time to call
  890. it later on UP. */
  891. xen_setup_vcpu_info_placement();
  892. #endif
  893. paravirt_ops.kernel_rpl = 1;
  894. if (xen_feature(XENFEAT_supervisor_mode_kernel))
  895. paravirt_ops.kernel_rpl = 0;
  896. /* set the limit of our address space */
  897. reserve_top_address(-HYPERVISOR_VIRT_START + 2 * PAGE_SIZE);
  898. /* set up basic CPUID stuff */
  899. cpu_detect(&new_cpu_data);
  900. new_cpu_data.hard_math = 1;
  901. new_cpu_data.x86_capability[0] = cpuid_edx(1);
  902. /* Poke various useful things into boot_params */
  903. LOADER_TYPE = (9 << 4) | 0;
  904. INITRD_START = xen_start_info->mod_start ? __pa(xen_start_info->mod_start) : 0;
  905. INITRD_SIZE = xen_start_info->mod_len;
  906. /* Start the world */
  907. start_kernel();
  908. }