enlighten.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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 = 0;
  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 *ax, unsigned int *bx,
  123. unsigned int *cx, unsigned int *dx)
  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 (*ax == 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" (*ax),
  136. "=b" (*bx),
  137. "=c" (*cx),
  138. "=d" (*dx)
  139. : "0" (*ax), "2" (*cx));
  140. *dx &= 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. struct mmuext_op *op;
  234. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  235. op = mcs.args;
  236. op->cmd = MMUEXT_SET_LDT;
  237. op->arg1.linear_addr = (unsigned long)addr;
  238. op->arg2.nr_ents = entries;
  239. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  240. xen_mc_issue(PARAVIRT_LAZY_CPU);
  241. }
  242. static void xen_load_gdt(const struct desc_ptr *dtr)
  243. {
  244. unsigned long *frames;
  245. unsigned long va = dtr->address;
  246. unsigned int size = dtr->size + 1;
  247. unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  248. int f;
  249. struct multicall_space mcs;
  250. /* A GDT can be up to 64k in size, which corresponds to 8192
  251. 8-byte entries, or 16 4k pages.. */
  252. BUG_ON(size > 65536);
  253. BUG_ON(va & ~PAGE_MASK);
  254. mcs = xen_mc_entry(sizeof(*frames) * pages);
  255. frames = mcs.args;
  256. for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
  257. frames[f] = virt_to_mfn(va);
  258. make_lowmem_page_readonly((void *)va);
  259. }
  260. MULTI_set_gdt(mcs.mc, frames, size / sizeof(struct desc_struct));
  261. xen_mc_issue(PARAVIRT_LAZY_CPU);
  262. }
  263. static void load_TLS_descriptor(struct thread_struct *t,
  264. unsigned int cpu, unsigned int i)
  265. {
  266. struct desc_struct *gdt = get_cpu_gdt_table(cpu);
  267. xmaddr_t maddr = virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
  268. struct multicall_space mc = __xen_mc_entry(0);
  269. MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
  270. }
  271. static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
  272. {
  273. xen_mc_batch();
  274. load_TLS_descriptor(t, cpu, 0);
  275. load_TLS_descriptor(t, cpu, 1);
  276. load_TLS_descriptor(t, cpu, 2);
  277. xen_mc_issue(PARAVIRT_LAZY_CPU);
  278. /*
  279. * XXX sleazy hack: If we're being called in a lazy-cpu zone,
  280. * it means we're in a context switch, and %gs has just been
  281. * saved. This means we can zero it out to prevent faults on
  282. * exit from the hypervisor if the next process has no %gs.
  283. * Either way, it has been saved, and the new value will get
  284. * loaded properly. This will go away as soon as Xen has been
  285. * modified to not save/restore %gs for normal hypercalls.
  286. */
  287. if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU)
  288. loadsegment(gs, 0);
  289. }
  290. static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
  291. const void *ptr)
  292. {
  293. unsigned long lp = (unsigned long)&dt[entrynum];
  294. xmaddr_t mach_lp = virt_to_machine(lp);
  295. u64 entry = *(u64 *)ptr;
  296. preempt_disable();
  297. xen_mc_flush();
  298. if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
  299. BUG();
  300. preempt_enable();
  301. }
  302. static int cvt_gate_to_trap(int vector, u32 low, u32 high,
  303. struct trap_info *info)
  304. {
  305. u8 type, dpl;
  306. type = (high >> 8) & 0x1f;
  307. dpl = (high >> 13) & 3;
  308. if (type != 0xf && type != 0xe)
  309. return 0;
  310. info->vector = vector;
  311. info->address = (high & 0xffff0000) | (low & 0x0000ffff);
  312. info->cs = low >> 16;
  313. info->flags = dpl;
  314. /* interrupt gates clear IF */
  315. if (type == 0xe)
  316. info->flags |= 4;
  317. return 1;
  318. }
  319. /* Locations of each CPU's IDT */
  320. static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
  321. /* Set an IDT entry. If the entry is part of the current IDT, then
  322. also update Xen. */
  323. static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
  324. {
  325. unsigned long p = (unsigned long)&dt[entrynum];
  326. unsigned long start, end;
  327. preempt_disable();
  328. start = __get_cpu_var(idt_desc).address;
  329. end = start + __get_cpu_var(idt_desc).size + 1;
  330. xen_mc_flush();
  331. native_write_idt_entry(dt, entrynum, g);
  332. if (p >= start && (p + 8) <= end) {
  333. struct trap_info info[2];
  334. u32 *desc = (u32 *)g;
  335. info[1].address = 0;
  336. if (cvt_gate_to_trap(entrynum, desc[0], desc[1], &info[0]))
  337. if (HYPERVISOR_set_trap_table(info))
  338. BUG();
  339. }
  340. preempt_enable();
  341. }
  342. static void xen_convert_trap_info(const struct desc_ptr *desc,
  343. struct trap_info *traps)
  344. {
  345. unsigned in, out, count;
  346. count = (desc->size+1) / 8;
  347. BUG_ON(count > 256);
  348. for (in = out = 0; in < count; in++) {
  349. const u32 *entry = (u32 *)(desc->address + in * 8);
  350. if (cvt_gate_to_trap(in, entry[0], entry[1], &traps[out]))
  351. out++;
  352. }
  353. traps[out].address = 0;
  354. }
  355. void xen_copy_trap_info(struct trap_info *traps)
  356. {
  357. const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
  358. xen_convert_trap_info(desc, traps);
  359. }
  360. /* Load a new IDT into Xen. In principle this can be per-CPU, so we
  361. hold a spinlock to protect the static traps[] array (static because
  362. it avoids allocation, and saves stack space). */
  363. static void xen_load_idt(const struct desc_ptr *desc)
  364. {
  365. static DEFINE_SPINLOCK(lock);
  366. static struct trap_info traps[257];
  367. spin_lock(&lock);
  368. __get_cpu_var(idt_desc) = *desc;
  369. xen_convert_trap_info(desc, traps);
  370. xen_mc_flush();
  371. if (HYPERVISOR_set_trap_table(traps))
  372. BUG();
  373. spin_unlock(&lock);
  374. }
  375. /* Write a GDT descriptor entry. Ignore LDT descriptors, since
  376. they're handled differently. */
  377. static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
  378. const void *desc, int type)
  379. {
  380. preempt_disable();
  381. switch (type) {
  382. case DESC_LDT:
  383. case DESC_TSS:
  384. /* ignore */
  385. break;
  386. default: {
  387. xmaddr_t maddr = virt_to_machine(&dt[entry]);
  388. xen_mc_flush();
  389. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  390. BUG();
  391. }
  392. }
  393. preempt_enable();
  394. }
  395. static void xen_load_sp0(struct tss_struct *tss,
  396. struct thread_struct *thread)
  397. {
  398. struct multicall_space mcs = xen_mc_entry(0);
  399. MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
  400. xen_mc_issue(PARAVIRT_LAZY_CPU);
  401. }
  402. static void xen_set_iopl_mask(unsigned mask)
  403. {
  404. struct physdev_set_iopl set_iopl;
  405. /* Force the change at ring 0. */
  406. set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
  407. HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  408. }
  409. static void xen_io_delay(void)
  410. {
  411. }
  412. #ifdef CONFIG_X86_LOCAL_APIC
  413. static u32 xen_apic_read(unsigned long reg)
  414. {
  415. return 0;
  416. }
  417. static void xen_apic_write(unsigned long reg, u32 val)
  418. {
  419. /* Warn to see if there's any stray references */
  420. WARN_ON(1);
  421. }
  422. #endif
  423. static void xen_flush_tlb(void)
  424. {
  425. struct mmuext_op *op;
  426. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  427. op = mcs.args;
  428. op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
  429. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  430. xen_mc_issue(PARAVIRT_LAZY_MMU);
  431. }
  432. static void xen_flush_tlb_single(unsigned long addr)
  433. {
  434. struct mmuext_op *op;
  435. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  436. op = mcs.args;
  437. op->cmd = MMUEXT_INVLPG_LOCAL;
  438. op->arg1.linear_addr = addr & PAGE_MASK;
  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_others(const cpumask_t *cpus, struct mm_struct *mm,
  443. unsigned long va)
  444. {
  445. struct {
  446. struct mmuext_op op;
  447. cpumask_t mask;
  448. } *args;
  449. cpumask_t cpumask = *cpus;
  450. struct multicall_space mcs;
  451. /*
  452. * A couple of (to be removed) sanity checks:
  453. *
  454. * - current CPU must not be in mask
  455. * - mask must exist :)
  456. */
  457. BUG_ON(cpus_empty(cpumask));
  458. BUG_ON(cpu_isset(smp_processor_id(), cpumask));
  459. BUG_ON(!mm);
  460. /* If a CPU which we ran on has gone down, OK. */
  461. cpus_and(cpumask, cpumask, cpu_online_map);
  462. if (cpus_empty(cpumask))
  463. return;
  464. mcs = xen_mc_entry(sizeof(*args));
  465. args = mcs.args;
  466. args->mask = cpumask;
  467. args->op.arg2.vcpumask = &args->mask;
  468. if (va == TLB_FLUSH_ALL) {
  469. args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
  470. } else {
  471. args->op.cmd = MMUEXT_INVLPG_MULTI;
  472. args->op.arg1.linear_addr = va;
  473. }
  474. MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
  475. xen_mc_issue(PARAVIRT_LAZY_MMU);
  476. }
  477. static void xen_write_cr2(unsigned long cr2)
  478. {
  479. x86_read_percpu(xen_vcpu)->arch.cr2 = cr2;
  480. }
  481. static unsigned long xen_read_cr2(void)
  482. {
  483. return x86_read_percpu(xen_vcpu)->arch.cr2;
  484. }
  485. static unsigned long xen_read_cr2_direct(void)
  486. {
  487. return x86_read_percpu(xen_vcpu_info.arch.cr2);
  488. }
  489. static void xen_write_cr4(unsigned long cr4)
  490. {
  491. /* Just ignore cr4 changes; Xen doesn't allow us to do
  492. anything anyway. */
  493. }
  494. static unsigned long xen_read_cr3(void)
  495. {
  496. return x86_read_percpu(xen_cr3);
  497. }
  498. static void set_current_cr3(void *v)
  499. {
  500. x86_write_percpu(xen_current_cr3, (unsigned long)v);
  501. }
  502. static void xen_write_cr3(unsigned long cr3)
  503. {
  504. struct mmuext_op *op;
  505. struct multicall_space mcs;
  506. unsigned long mfn = pfn_to_mfn(PFN_DOWN(cr3));
  507. BUG_ON(preemptible());
  508. mcs = xen_mc_entry(sizeof(*op)); /* disables interrupts */
  509. /* Update while interrupts are disabled, so its atomic with
  510. respect to ipis */
  511. x86_write_percpu(xen_cr3, cr3);
  512. op = mcs.args;
  513. op->cmd = MMUEXT_NEW_BASEPTR;
  514. op->arg1.mfn = mfn;
  515. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  516. /* Update xen_update_cr3 once the batch has actually
  517. been submitted. */
  518. xen_mc_callback(set_current_cr3, (void *)cr3);
  519. xen_mc_issue(PARAVIRT_LAZY_CPU); /* interrupts restored */
  520. }
  521. /* Early in boot, while setting up the initial pagetable, assume
  522. everything is pinned. */
  523. static __init void xen_alloc_pt_init(struct mm_struct *mm, u32 pfn)
  524. {
  525. BUG_ON(mem_map); /* should only be used early */
  526. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  527. }
  528. static void pin_pagetable_pfn(unsigned level, unsigned long pfn)
  529. {
  530. struct mmuext_op op;
  531. op.cmd = level;
  532. op.arg1.mfn = pfn_to_mfn(pfn);
  533. if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
  534. BUG();
  535. }
  536. /* This needs to make sure the new pte page is pinned iff its being
  537. attached to a pinned pagetable. */
  538. static void xen_alloc_pt(struct mm_struct *mm, u32 pfn)
  539. {
  540. struct page *page = pfn_to_page(pfn);
  541. if (PagePinned(virt_to_page(mm->pgd))) {
  542. SetPagePinned(page);
  543. if (!PageHighMem(page)) {
  544. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  545. pin_pagetable_pfn(MMUEXT_PIN_L1_TABLE, pfn);
  546. } else
  547. /* make sure there are no stray mappings of
  548. this page */
  549. kmap_flush_unused();
  550. }
  551. }
  552. /* This should never happen until we're OK to use struct page */
  553. static void xen_release_pt(u32 pfn)
  554. {
  555. struct page *page = pfn_to_page(pfn);
  556. if (PagePinned(page)) {
  557. if (!PageHighMem(page)) {
  558. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, pfn);
  559. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  560. }
  561. }
  562. }
  563. #ifdef CONFIG_HIGHPTE
  564. static void *xen_kmap_atomic_pte(struct page *page, enum km_type type)
  565. {
  566. pgprot_t prot = PAGE_KERNEL;
  567. if (PagePinned(page))
  568. prot = PAGE_KERNEL_RO;
  569. if (0 && PageHighMem(page))
  570. printk("mapping highpte %lx type %d prot %s\n",
  571. page_to_pfn(page), type,
  572. (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ");
  573. return kmap_atomic_prot(page, type, prot);
  574. }
  575. #endif
  576. static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
  577. {
  578. /* If there's an existing pte, then don't allow _PAGE_RW to be set */
  579. if (pte_val_ma(*ptep) & _PAGE_PRESENT)
  580. pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
  581. pte_val_ma(pte));
  582. return pte;
  583. }
  584. /* Init-time set_pte while constructing initial pagetables, which
  585. doesn't allow RO pagetable pages to be remapped RW */
  586. static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
  587. {
  588. pte = mask_rw_pte(ptep, pte);
  589. xen_set_pte(ptep, pte);
  590. }
  591. static __init void xen_pagetable_setup_start(pgd_t *base)
  592. {
  593. pgd_t *xen_pgd = (pgd_t *)xen_start_info->pt_base;
  594. /* special set_pte for pagetable initialization */
  595. pv_mmu_ops.set_pte = xen_set_pte_init;
  596. init_mm.pgd = base;
  597. /*
  598. * copy top-level of Xen-supplied pagetable into place. For
  599. * !PAE we can use this as-is, but for PAE it is a stand-in
  600. * while we copy the pmd pages.
  601. */
  602. memcpy(base, xen_pgd, PTRS_PER_PGD * sizeof(pgd_t));
  603. if (PTRS_PER_PMD > 1) {
  604. int i;
  605. /*
  606. * For PAE, need to allocate new pmds, rather than
  607. * share Xen's, since Xen doesn't like pmd's being
  608. * shared between address spaces.
  609. */
  610. for (i = 0; i < PTRS_PER_PGD; i++) {
  611. if (pgd_val_ma(xen_pgd[i]) & _PAGE_PRESENT) {
  612. pmd_t *pmd = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
  613. memcpy(pmd, (void *)pgd_page_vaddr(xen_pgd[i]),
  614. PAGE_SIZE);
  615. make_lowmem_page_readonly(pmd);
  616. set_pgd(&base[i], __pgd(1 + __pa(pmd)));
  617. } else
  618. pgd_clear(&base[i]);
  619. }
  620. }
  621. /* make sure zero_page is mapped RO so we can use it in pagetables */
  622. make_lowmem_page_readonly(empty_zero_page);
  623. make_lowmem_page_readonly(base);
  624. /*
  625. * Switch to new pagetable. This is done before
  626. * pagetable_init has done anything so that the new pages
  627. * added to the table can be prepared properly for Xen.
  628. */
  629. xen_write_cr3(__pa(base));
  630. }
  631. static __init void xen_pagetable_setup_done(pgd_t *base)
  632. {
  633. /* This will work as long as patching hasn't happened yet
  634. (which it hasn't) */
  635. pv_mmu_ops.alloc_pt = xen_alloc_pt;
  636. pv_mmu_ops.set_pte = xen_set_pte;
  637. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  638. /*
  639. * Create a mapping for the shared info page.
  640. * Should be set_fixmap(), but shared_info is a machine
  641. * address with no corresponding pseudo-phys address.
  642. */
  643. set_pte_mfn(fix_to_virt(FIX_PARAVIRT_BOOTMAP),
  644. PFN_DOWN(xen_start_info->shared_info),
  645. PAGE_KERNEL);
  646. HYPERVISOR_shared_info =
  647. (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
  648. } else
  649. HYPERVISOR_shared_info =
  650. (struct shared_info *)__va(xen_start_info->shared_info);
  651. /* Actually pin the pagetable down, but we can't set PG_pinned
  652. yet because the page structures don't exist yet. */
  653. {
  654. unsigned level;
  655. #ifdef CONFIG_X86_PAE
  656. level = MMUEXT_PIN_L3_TABLE;
  657. #else
  658. level = MMUEXT_PIN_L2_TABLE;
  659. #endif
  660. pin_pagetable_pfn(level, PFN_DOWN(__pa(base)));
  661. }
  662. }
  663. /* This is called once we have the cpu_possible_map */
  664. void __init xen_setup_vcpu_info_placement(void)
  665. {
  666. int cpu;
  667. for_each_possible_cpu(cpu)
  668. xen_vcpu_setup(cpu);
  669. /* xen_vcpu_setup managed to place the vcpu_info within the
  670. percpu area for all cpus, so make use of it */
  671. if (have_vcpu_info_placement) {
  672. printk(KERN_INFO "Xen: using vcpu_info placement\n");
  673. pv_irq_ops.save_fl = xen_save_fl_direct;
  674. pv_irq_ops.restore_fl = xen_restore_fl_direct;
  675. pv_irq_ops.irq_disable = xen_irq_disable_direct;
  676. pv_irq_ops.irq_enable = xen_irq_enable_direct;
  677. pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
  678. pv_cpu_ops.iret = xen_iret_direct;
  679. }
  680. }
  681. static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
  682. unsigned long addr, unsigned len)
  683. {
  684. char *start, *end, *reloc;
  685. unsigned ret;
  686. start = end = reloc = NULL;
  687. #define SITE(op, x) \
  688. case PARAVIRT_PATCH(op.x): \
  689. if (have_vcpu_info_placement) { \
  690. start = (char *)xen_##x##_direct; \
  691. end = xen_##x##_direct_end; \
  692. reloc = xen_##x##_direct_reloc; \
  693. } \
  694. goto patch_site
  695. switch (type) {
  696. SITE(pv_irq_ops, irq_enable);
  697. SITE(pv_irq_ops, irq_disable);
  698. SITE(pv_irq_ops, save_fl);
  699. SITE(pv_irq_ops, restore_fl);
  700. #undef SITE
  701. patch_site:
  702. if (start == NULL || (end-start) > len)
  703. goto default_patch;
  704. ret = paravirt_patch_insns(insnbuf, len, start, end);
  705. /* Note: because reloc is assigned from something that
  706. appears to be an array, gcc assumes it's non-null,
  707. but doesn't know its relationship with start and
  708. end. */
  709. if (reloc > start && reloc < end) {
  710. int reloc_off = reloc - start;
  711. long *relocp = (long *)(insnbuf + reloc_off);
  712. long delta = start - (char *)addr;
  713. *relocp += delta;
  714. }
  715. break;
  716. default_patch:
  717. default:
  718. ret = paravirt_patch_default(type, clobbers, insnbuf,
  719. addr, len);
  720. break;
  721. }
  722. return ret;
  723. }
  724. static const struct pv_info xen_info __initdata = {
  725. .paravirt_enabled = 1,
  726. .shared_kernel_pmd = 0,
  727. .name = "Xen",
  728. };
  729. static const struct pv_init_ops xen_init_ops __initdata = {
  730. .patch = xen_patch,
  731. .banner = xen_banner,
  732. .memory_setup = xen_memory_setup,
  733. .arch_setup = xen_arch_setup,
  734. .post_allocator_init = xen_mark_init_mm_pinned,
  735. };
  736. static const struct pv_time_ops xen_time_ops __initdata = {
  737. .time_init = xen_time_init,
  738. .set_wallclock = xen_set_wallclock,
  739. .get_wallclock = xen_get_wallclock,
  740. .get_cpu_khz = xen_cpu_khz,
  741. .sched_clock = xen_sched_clock,
  742. };
  743. static const struct pv_cpu_ops xen_cpu_ops __initdata = {
  744. .cpuid = xen_cpuid,
  745. .set_debugreg = xen_set_debugreg,
  746. .get_debugreg = xen_get_debugreg,
  747. .clts = native_clts,
  748. .read_cr0 = native_read_cr0,
  749. .write_cr0 = native_write_cr0,
  750. .read_cr4 = native_read_cr4,
  751. .read_cr4_safe = native_read_cr4_safe,
  752. .write_cr4 = xen_write_cr4,
  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_syscall_ret = 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_sp0 = xen_load_sp0,
  772. .set_iopl_mask = xen_set_iopl_mask,
  773. .io_delay = xen_io_delay,
  774. .lazy_mode = {
  775. .enter = paravirt_enter_lazy_cpu,
  776. .leave = xen_leave_lazy,
  777. },
  778. };
  779. static const struct pv_irq_ops xen_irq_ops __initdata = {
  780. .init_IRQ = xen_init_IRQ,
  781. .save_fl = xen_save_fl,
  782. .restore_fl = xen_restore_fl,
  783. .irq_disable = xen_irq_disable,
  784. .irq_enable = xen_irq_enable,
  785. .safe_halt = xen_safe_halt,
  786. .halt = xen_halt,
  787. };
  788. static const struct pv_apic_ops xen_apic_ops __initdata = {
  789. #ifdef CONFIG_X86_LOCAL_APIC
  790. .apic_write = xen_apic_write,
  791. .apic_write_atomic = xen_apic_write,
  792. .apic_read = xen_apic_read,
  793. .setup_boot_clock = paravirt_nop,
  794. .setup_secondary_clock = paravirt_nop,
  795. .startup_ipi_hook = paravirt_nop,
  796. #endif
  797. };
  798. static const struct pv_mmu_ops xen_mmu_ops __initdata = {
  799. .pagetable_setup_start = xen_pagetable_setup_start,
  800. .pagetable_setup_done = xen_pagetable_setup_done,
  801. .read_cr2 = xen_read_cr2,
  802. .write_cr2 = xen_write_cr2,
  803. .read_cr3 = xen_read_cr3,
  804. .write_cr3 = xen_write_cr3,
  805. .flush_tlb_user = xen_flush_tlb,
  806. .flush_tlb_kernel = xen_flush_tlb,
  807. .flush_tlb_single = xen_flush_tlb_single,
  808. .flush_tlb_others = xen_flush_tlb_others,
  809. .pte_update = paravirt_nop,
  810. .pte_update_defer = paravirt_nop,
  811. .alloc_pt = xen_alloc_pt_init,
  812. .release_pt = xen_release_pt,
  813. .alloc_pd = paravirt_nop,
  814. .alloc_pd_clone = paravirt_nop,
  815. .release_pd = paravirt_nop,
  816. #ifdef CONFIG_HIGHPTE
  817. .kmap_atomic_pte = xen_kmap_atomic_pte,
  818. #endif
  819. .set_pte = NULL, /* see xen_pagetable_setup_* */
  820. .set_pte_at = xen_set_pte_at,
  821. .set_pmd = xen_set_pmd,
  822. .pte_val = xen_pte_val,
  823. .pgd_val = xen_pgd_val,
  824. .make_pte = xen_make_pte,
  825. .make_pgd = xen_make_pgd,
  826. #ifdef CONFIG_X86_PAE
  827. .set_pte_atomic = xen_set_pte_atomic,
  828. .set_pte_present = xen_set_pte_at,
  829. .set_pud = xen_set_pud,
  830. .pte_clear = xen_pte_clear,
  831. .pmd_clear = xen_pmd_clear,
  832. .make_pmd = xen_make_pmd,
  833. .pmd_val = xen_pmd_val,
  834. #endif /* PAE */
  835. .activate_mm = xen_activate_mm,
  836. .dup_mmap = xen_dup_mmap,
  837. .exit_mmap = xen_exit_mmap,
  838. .lazy_mode = {
  839. .enter = paravirt_enter_lazy_mmu,
  840. .leave = xen_leave_lazy,
  841. },
  842. };
  843. #ifdef CONFIG_SMP
  844. static const struct smp_ops xen_smp_ops __initdata = {
  845. .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
  846. .smp_prepare_cpus = xen_smp_prepare_cpus,
  847. .cpu_up = xen_cpu_up,
  848. .smp_cpus_done = xen_smp_cpus_done,
  849. .smp_send_stop = xen_smp_send_stop,
  850. .smp_send_reschedule = xen_smp_send_reschedule,
  851. .smp_call_function_mask = xen_smp_call_function_mask,
  852. };
  853. #endif /* CONFIG_SMP */
  854. static void xen_reboot(int reason)
  855. {
  856. #ifdef CONFIG_SMP
  857. smp_send_stop();
  858. #endif
  859. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, reason))
  860. BUG();
  861. }
  862. static void xen_restart(char *msg)
  863. {
  864. xen_reboot(SHUTDOWN_reboot);
  865. }
  866. static void xen_emergency_restart(void)
  867. {
  868. xen_reboot(SHUTDOWN_reboot);
  869. }
  870. static void xen_machine_halt(void)
  871. {
  872. xen_reboot(SHUTDOWN_poweroff);
  873. }
  874. static void xen_crash_shutdown(struct pt_regs *regs)
  875. {
  876. xen_reboot(SHUTDOWN_crash);
  877. }
  878. static const struct machine_ops __initdata xen_machine_ops = {
  879. .restart = xen_restart,
  880. .halt = xen_machine_halt,
  881. .power_off = xen_machine_halt,
  882. .shutdown = xen_machine_halt,
  883. .crash_shutdown = xen_crash_shutdown,
  884. .emergency_restart = xen_emergency_restart,
  885. };
  886. static void __init xen_reserve_top(void)
  887. {
  888. unsigned long top = HYPERVISOR_VIRT_START;
  889. struct xen_platform_parameters pp;
  890. if (HYPERVISOR_xen_version(XENVER_platform_parameters, &pp) == 0)
  891. top = pp.virt_start;
  892. reserve_top_address(-top + 2 * PAGE_SIZE);
  893. }
  894. /* First C function to be called on Xen boot */
  895. asmlinkage void __init xen_start_kernel(void)
  896. {
  897. pgd_t *pgd;
  898. if (!xen_start_info)
  899. return;
  900. BUG_ON(memcmp(xen_start_info->magic, "xen-3", 5) != 0);
  901. /* Install Xen paravirt ops */
  902. pv_info = xen_info;
  903. pv_init_ops = xen_init_ops;
  904. pv_time_ops = xen_time_ops;
  905. pv_cpu_ops = xen_cpu_ops;
  906. pv_irq_ops = xen_irq_ops;
  907. pv_apic_ops = xen_apic_ops;
  908. pv_mmu_ops = xen_mmu_ops;
  909. machine_ops = xen_machine_ops;
  910. #ifdef CONFIG_SMP
  911. smp_ops = xen_smp_ops;
  912. #endif
  913. xen_setup_features();
  914. /* Get mfn list */
  915. if (!xen_feature(XENFEAT_auto_translated_physmap))
  916. phys_to_machine_mapping = (unsigned long *)xen_start_info->mfn_list;
  917. pgd = (pgd_t *)xen_start_info->pt_base;
  918. init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE;
  919. init_mm.pgd = pgd; /* use the Xen pagetables to start */
  920. /* keep using Xen gdt for now; no urgent need to change it */
  921. x86_write_percpu(xen_cr3, __pa(pgd));
  922. x86_write_percpu(xen_current_cr3, __pa(pgd));
  923. #ifdef CONFIG_SMP
  924. /* Don't do the full vcpu_info placement stuff until we have a
  925. possible map. */
  926. per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
  927. #else
  928. /* May as well do it now, since there's no good time to call
  929. it later on UP. */
  930. xen_setup_vcpu_info_placement();
  931. #endif
  932. pv_info.kernel_rpl = 1;
  933. if (xen_feature(XENFEAT_supervisor_mode_kernel))
  934. pv_info.kernel_rpl = 0;
  935. /* set the limit of our address space */
  936. xen_reserve_top();
  937. /* set up basic CPUID stuff */
  938. cpu_detect(&new_cpu_data);
  939. new_cpu_data.hard_math = 1;
  940. new_cpu_data.x86_capability[0] = cpuid_edx(1);
  941. /* Poke various useful things into boot_params */
  942. boot_params.hdr.type_of_loader = (9 << 4) | 0;
  943. boot_params.hdr.ramdisk_image = xen_start_info->mod_start
  944. ? __pa(xen_start_info->mod_start) : 0;
  945. boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
  946. /* Start the world */
  947. start_kernel();
  948. }