enlighten.c 30 KB

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