enlighten.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  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 <xen/hvc-console.h>
  35. #include <asm/paravirt.h>
  36. #include <asm/page.h>
  37. #include <asm/xen/hypercall.h>
  38. #include <asm/xen/hypervisor.h>
  39. #include <asm/fixmap.h>
  40. #include <asm/processor.h>
  41. #include <asm/setup.h>
  42. #include <asm/desc.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/tlbflush.h>
  45. #include <asm/reboot.h>
  46. #include <asm/pgalloc.h>
  47. #include "xen-ops.h"
  48. #include "mmu.h"
  49. #include "multicalls.h"
  50. EXPORT_SYMBOL_GPL(hypercall_page);
  51. DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
  52. DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
  53. /*
  54. * Note about cr3 (pagetable base) values:
  55. *
  56. * xen_cr3 contains the current logical cr3 value; it contains the
  57. * last set cr3. This may not be the current effective cr3, because
  58. * its update may be being lazily deferred. However, a vcpu looking
  59. * at its own cr3 can use this value knowing that it everything will
  60. * be self-consistent.
  61. *
  62. * xen_current_cr3 contains the actual vcpu cr3; it is set once the
  63. * hypercall to set the vcpu cr3 is complete (so it may be a little
  64. * out of date, but it will never be set early). If one vcpu is
  65. * looking at another vcpu's cr3 value, it should use this variable.
  66. */
  67. DEFINE_PER_CPU(unsigned long, xen_cr3); /* cr3 stored as physaddr */
  68. DEFINE_PER_CPU(unsigned long, xen_current_cr3); /* actual vcpu cr3 */
  69. struct start_info *xen_start_info;
  70. EXPORT_SYMBOL_GPL(xen_start_info);
  71. struct shared_info xen_dummy_shared_info;
  72. /*
  73. * Point at some empty memory to start with. We map the real shared_info
  74. * page as soon as fixmap is up and running.
  75. */
  76. struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
  77. /*
  78. * Flag to determine whether vcpu info placement is available on all
  79. * VCPUs. We assume it is to start with, and then set it to zero on
  80. * the first failure. This is because it can succeed on some VCPUs
  81. * and not others, since it can involve hypervisor memory allocation,
  82. * or because the guest failed to guarantee all the appropriate
  83. * constraints on all VCPUs (ie buffer can't cross a page boundary).
  84. *
  85. * Note that any particular CPU may be using a placed vcpu structure,
  86. * but we can only optimise if the all are.
  87. *
  88. * 0: not available, 1: available
  89. */
  90. static int have_vcpu_info_placement = 1;
  91. static void xen_vcpu_setup(int cpu)
  92. {
  93. struct vcpu_register_vcpu_info info;
  94. int err;
  95. struct vcpu_info *vcpup;
  96. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  97. per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
  98. if (!have_vcpu_info_placement)
  99. return; /* already tested, not available */
  100. vcpup = &per_cpu(xen_vcpu_info, cpu);
  101. info.mfn = virt_to_mfn(vcpup);
  102. info.offset = offset_in_page(vcpup);
  103. printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %llx, offset %d\n",
  104. cpu, vcpup, info.mfn, info.offset);
  105. /* Check to see if the hypervisor will put the vcpu_info
  106. structure where we want it, which allows direct access via
  107. a percpu-variable. */
  108. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
  109. if (err) {
  110. printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
  111. have_vcpu_info_placement = 0;
  112. } else {
  113. /* This cpu is using the registered vcpu info, even if
  114. later ones fail to. */
  115. per_cpu(xen_vcpu, cpu) = vcpup;
  116. printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
  117. cpu, vcpup);
  118. }
  119. }
  120. /*
  121. * On restore, set the vcpu placement up again.
  122. * If it fails, then we're in a bad state, since
  123. * we can't back out from using it...
  124. */
  125. void xen_vcpu_restore(void)
  126. {
  127. if (have_vcpu_info_placement) {
  128. int cpu;
  129. for_each_online_cpu(cpu) {
  130. bool other_cpu = (cpu != smp_processor_id());
  131. if (other_cpu &&
  132. HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
  133. BUG();
  134. xen_vcpu_setup(cpu);
  135. if (other_cpu &&
  136. HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
  137. BUG();
  138. }
  139. BUG_ON(!have_vcpu_info_placement);
  140. }
  141. }
  142. static void __init xen_banner(void)
  143. {
  144. printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
  145. pv_info.name);
  146. printk(KERN_INFO "Hypervisor signature: %s%s\n",
  147. xen_start_info->magic,
  148. xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
  149. }
  150. static void xen_cpuid(unsigned int *ax, unsigned int *bx,
  151. unsigned int *cx, unsigned int *dx)
  152. {
  153. unsigned maskedx = ~0;
  154. /*
  155. * Mask out inconvenient features, to try and disable as many
  156. * unsupported kernel subsystems as possible.
  157. */
  158. if (*ax == 1)
  159. maskedx = ~((1 << X86_FEATURE_APIC) | /* disable APIC */
  160. (1 << X86_FEATURE_ACPI) | /* disable ACPI */
  161. (1 << X86_FEATURE_MCE) | /* disable MCE */
  162. (1 << X86_FEATURE_MCA) | /* disable MCA */
  163. (1 << X86_FEATURE_ACC)); /* thermal monitoring */
  164. asm(XEN_EMULATE_PREFIX "cpuid"
  165. : "=a" (*ax),
  166. "=b" (*bx),
  167. "=c" (*cx),
  168. "=d" (*dx)
  169. : "0" (*ax), "2" (*cx));
  170. *dx &= maskedx;
  171. }
  172. static void xen_set_debugreg(int reg, unsigned long val)
  173. {
  174. HYPERVISOR_set_debugreg(reg, val);
  175. }
  176. static unsigned long xen_get_debugreg(int reg)
  177. {
  178. return HYPERVISOR_get_debugreg(reg);
  179. }
  180. static unsigned long xen_save_fl(void)
  181. {
  182. struct vcpu_info *vcpu;
  183. unsigned long flags;
  184. vcpu = x86_read_percpu(xen_vcpu);
  185. /* flag has opposite sense of mask */
  186. flags = !vcpu->evtchn_upcall_mask;
  187. /* convert to IF type flag
  188. -0 -> 0x00000000
  189. -1 -> 0xffffffff
  190. */
  191. return (-flags) & X86_EFLAGS_IF;
  192. }
  193. static void xen_restore_fl(unsigned long flags)
  194. {
  195. struct vcpu_info *vcpu;
  196. /* convert from IF type flag */
  197. flags = !(flags & X86_EFLAGS_IF);
  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 = flags;
  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. if (flags == 0) {
  208. preempt_check_resched();
  209. barrier(); /* unmask then check (avoid races) */
  210. if (unlikely(vcpu->evtchn_upcall_pending))
  211. force_evtchn_callback();
  212. }
  213. }
  214. static void xen_irq_disable(void)
  215. {
  216. /* There's a one instruction preempt window here. We need to
  217. make sure we're don't switch CPUs between getting the vcpu
  218. pointer and updating the mask. */
  219. preempt_disable();
  220. x86_read_percpu(xen_vcpu)->evtchn_upcall_mask = 1;
  221. preempt_enable_no_resched();
  222. }
  223. static void xen_irq_enable(void)
  224. {
  225. struct vcpu_info *vcpu;
  226. /* We don't need to worry about being preempted here, since
  227. either a) interrupts are disabled, so no preemption, or b)
  228. the caller is confused and is trying to re-enable interrupts
  229. on an indeterminate processor. */
  230. vcpu = x86_read_percpu(xen_vcpu);
  231. vcpu->evtchn_upcall_mask = 0;
  232. /* Doesn't matter if we get preempted here, because any
  233. pending event will get dealt with anyway. */
  234. barrier(); /* unmask then check (avoid races) */
  235. if (unlikely(vcpu->evtchn_upcall_pending))
  236. force_evtchn_callback();
  237. }
  238. static void xen_safe_halt(void)
  239. {
  240. /* Blocking includes an implicit local_irq_enable(). */
  241. if (HYPERVISOR_sched_op(SCHEDOP_block, NULL) != 0)
  242. BUG();
  243. }
  244. static void xen_halt(void)
  245. {
  246. if (irqs_disabled())
  247. HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
  248. else
  249. xen_safe_halt();
  250. }
  251. static void xen_leave_lazy(void)
  252. {
  253. paravirt_leave_lazy(paravirt_get_lazy_mode());
  254. xen_mc_flush();
  255. }
  256. static unsigned long xen_store_tr(void)
  257. {
  258. return 0;
  259. }
  260. static void xen_set_ldt(const void *addr, unsigned entries)
  261. {
  262. struct mmuext_op *op;
  263. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  264. op = mcs.args;
  265. op->cmd = MMUEXT_SET_LDT;
  266. op->arg1.linear_addr = (unsigned long)addr;
  267. op->arg2.nr_ents = entries;
  268. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  269. xen_mc_issue(PARAVIRT_LAZY_CPU);
  270. }
  271. static void xen_load_gdt(const struct desc_ptr *dtr)
  272. {
  273. unsigned long *frames;
  274. unsigned long va = dtr->address;
  275. unsigned int size = dtr->size + 1;
  276. unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  277. int f;
  278. struct multicall_space mcs;
  279. /* A GDT can be up to 64k in size, which corresponds to 8192
  280. 8-byte entries, or 16 4k pages.. */
  281. BUG_ON(size > 65536);
  282. BUG_ON(va & ~PAGE_MASK);
  283. mcs = xen_mc_entry(sizeof(*frames) * pages);
  284. frames = mcs.args;
  285. for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
  286. frames[f] = virt_to_mfn(va);
  287. make_lowmem_page_readonly((void *)va);
  288. }
  289. MULTI_set_gdt(mcs.mc, frames, size / sizeof(struct desc_struct));
  290. xen_mc_issue(PARAVIRT_LAZY_CPU);
  291. }
  292. static void load_TLS_descriptor(struct thread_struct *t,
  293. unsigned int cpu, unsigned int i)
  294. {
  295. struct desc_struct *gdt = get_cpu_gdt_table(cpu);
  296. xmaddr_t maddr = virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
  297. struct multicall_space mc = __xen_mc_entry(0);
  298. MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
  299. }
  300. static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
  301. {
  302. xen_mc_batch();
  303. load_TLS_descriptor(t, cpu, 0);
  304. load_TLS_descriptor(t, cpu, 1);
  305. load_TLS_descriptor(t, cpu, 2);
  306. xen_mc_issue(PARAVIRT_LAZY_CPU);
  307. /*
  308. * XXX sleazy hack: If we're being called in a lazy-cpu zone,
  309. * it means we're in a context switch, and %gs has just been
  310. * saved. This means we can zero it out to prevent faults on
  311. * exit from the hypervisor if the next process has no %gs.
  312. * Either way, it has been saved, and the new value will get
  313. * loaded properly. This will go away as soon as Xen has been
  314. * modified to not save/restore %gs for normal hypercalls.
  315. */
  316. if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU)
  317. loadsegment(gs, 0);
  318. }
  319. static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
  320. const void *ptr)
  321. {
  322. unsigned long lp = (unsigned long)&dt[entrynum];
  323. xmaddr_t mach_lp = virt_to_machine(lp);
  324. u64 entry = *(u64 *)ptr;
  325. preempt_disable();
  326. xen_mc_flush();
  327. if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
  328. BUG();
  329. preempt_enable();
  330. }
  331. static int cvt_gate_to_trap(int vector, const gate_desc *val,
  332. struct trap_info *info)
  333. {
  334. if (val->type != 0xf && val->type != 0xe)
  335. return 0;
  336. info->vector = vector;
  337. info->address = gate_offset(*val);
  338. info->cs = gate_segment(*val);
  339. info->flags = val->dpl;
  340. /* interrupt gates clear IF */
  341. if (val->type == 0xe)
  342. info->flags |= 4;
  343. return 1;
  344. }
  345. /* Locations of each CPU's IDT */
  346. static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
  347. /* Set an IDT entry. If the entry is part of the current IDT, then
  348. also update Xen. */
  349. static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
  350. {
  351. unsigned long p = (unsigned long)&dt[entrynum];
  352. unsigned long start, end;
  353. preempt_disable();
  354. start = __get_cpu_var(idt_desc).address;
  355. end = start + __get_cpu_var(idt_desc).size + 1;
  356. xen_mc_flush();
  357. native_write_idt_entry(dt, entrynum, g);
  358. if (p >= start && (p + 8) <= end) {
  359. struct trap_info info[2];
  360. info[1].address = 0;
  361. if (cvt_gate_to_trap(entrynum, g, &info[0]))
  362. if (HYPERVISOR_set_trap_table(info))
  363. BUG();
  364. }
  365. preempt_enable();
  366. }
  367. static void xen_convert_trap_info(const struct desc_ptr *desc,
  368. struct trap_info *traps)
  369. {
  370. unsigned in, out, count;
  371. count = (desc->size+1) / sizeof(gate_desc);
  372. BUG_ON(count > 256);
  373. for (in = out = 0; in < count; in++) {
  374. gate_desc *entry = (gate_desc*)(desc->address) + in;
  375. if (cvt_gate_to_trap(in, entry, &traps[out]))
  376. out++;
  377. }
  378. traps[out].address = 0;
  379. }
  380. void xen_copy_trap_info(struct trap_info *traps)
  381. {
  382. const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
  383. xen_convert_trap_info(desc, traps);
  384. }
  385. /* Load a new IDT into Xen. In principle this can be per-CPU, so we
  386. hold a spinlock to protect the static traps[] array (static because
  387. it avoids allocation, and saves stack space). */
  388. static void xen_load_idt(const struct desc_ptr *desc)
  389. {
  390. static DEFINE_SPINLOCK(lock);
  391. static struct trap_info traps[257];
  392. spin_lock(&lock);
  393. __get_cpu_var(idt_desc) = *desc;
  394. xen_convert_trap_info(desc, traps);
  395. xen_mc_flush();
  396. if (HYPERVISOR_set_trap_table(traps))
  397. BUG();
  398. spin_unlock(&lock);
  399. }
  400. /* Write a GDT descriptor entry. Ignore LDT descriptors, since
  401. they're handled differently. */
  402. static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
  403. const void *desc, int type)
  404. {
  405. preempt_disable();
  406. switch (type) {
  407. case DESC_LDT:
  408. case DESC_TSS:
  409. /* ignore */
  410. break;
  411. default: {
  412. xmaddr_t maddr = virt_to_machine(&dt[entry]);
  413. xen_mc_flush();
  414. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  415. BUG();
  416. }
  417. }
  418. preempt_enable();
  419. }
  420. static void xen_load_sp0(struct tss_struct *tss,
  421. struct thread_struct *thread)
  422. {
  423. struct multicall_space mcs = xen_mc_entry(0);
  424. MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
  425. xen_mc_issue(PARAVIRT_LAZY_CPU);
  426. }
  427. static void xen_set_iopl_mask(unsigned mask)
  428. {
  429. struct physdev_set_iopl set_iopl;
  430. /* Force the change at ring 0. */
  431. set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
  432. HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  433. }
  434. static void xen_io_delay(void)
  435. {
  436. }
  437. #ifdef CONFIG_X86_LOCAL_APIC
  438. static u32 xen_apic_read(unsigned long reg)
  439. {
  440. return 0;
  441. }
  442. static void xen_apic_write(unsigned long reg, u32 val)
  443. {
  444. /* Warn to see if there's any stray references */
  445. WARN_ON(1);
  446. }
  447. #endif
  448. static void xen_flush_tlb(void)
  449. {
  450. struct mmuext_op *op;
  451. struct multicall_space mcs;
  452. preempt_disable();
  453. mcs = xen_mc_entry(sizeof(*op));
  454. op = mcs.args;
  455. op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
  456. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  457. xen_mc_issue(PARAVIRT_LAZY_MMU);
  458. preempt_enable();
  459. }
  460. static void xen_flush_tlb_single(unsigned long addr)
  461. {
  462. struct mmuext_op *op;
  463. struct multicall_space mcs;
  464. preempt_disable();
  465. mcs = xen_mc_entry(sizeof(*op));
  466. op = mcs.args;
  467. op->cmd = MMUEXT_INVLPG_LOCAL;
  468. op->arg1.linear_addr = addr & PAGE_MASK;
  469. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  470. xen_mc_issue(PARAVIRT_LAZY_MMU);
  471. preempt_enable();
  472. }
  473. static void xen_flush_tlb_others(const cpumask_t *cpus, struct mm_struct *mm,
  474. unsigned long va)
  475. {
  476. struct {
  477. struct mmuext_op op;
  478. cpumask_t mask;
  479. } *args;
  480. cpumask_t cpumask = *cpus;
  481. struct multicall_space mcs;
  482. /*
  483. * A couple of (to be removed) sanity checks:
  484. *
  485. * - current CPU must not be in mask
  486. * - mask must exist :)
  487. */
  488. BUG_ON(cpus_empty(cpumask));
  489. BUG_ON(cpu_isset(smp_processor_id(), cpumask));
  490. BUG_ON(!mm);
  491. /* If a CPU which we ran on has gone down, OK. */
  492. cpus_and(cpumask, cpumask, cpu_online_map);
  493. if (cpus_empty(cpumask))
  494. return;
  495. mcs = xen_mc_entry(sizeof(*args));
  496. args = mcs.args;
  497. args->mask = cpumask;
  498. args->op.arg2.vcpumask = &args->mask;
  499. if (va == TLB_FLUSH_ALL) {
  500. args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
  501. } else {
  502. args->op.cmd = MMUEXT_INVLPG_MULTI;
  503. args->op.arg1.linear_addr = va;
  504. }
  505. MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
  506. xen_mc_issue(PARAVIRT_LAZY_MMU);
  507. }
  508. static void xen_clts(void)
  509. {
  510. struct multicall_space mcs;
  511. mcs = xen_mc_entry(0);
  512. MULTI_fpu_taskswitch(mcs.mc, 0);
  513. xen_mc_issue(PARAVIRT_LAZY_CPU);
  514. }
  515. static void xen_write_cr0(unsigned long cr0)
  516. {
  517. struct multicall_space mcs;
  518. /* Only pay attention to cr0.TS; everything else is
  519. ignored. */
  520. mcs = xen_mc_entry(0);
  521. MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
  522. xen_mc_issue(PARAVIRT_LAZY_CPU);
  523. }
  524. static void xen_write_cr2(unsigned long cr2)
  525. {
  526. x86_read_percpu(xen_vcpu)->arch.cr2 = cr2;
  527. }
  528. static unsigned long xen_read_cr2(void)
  529. {
  530. return x86_read_percpu(xen_vcpu)->arch.cr2;
  531. }
  532. static unsigned long xen_read_cr2_direct(void)
  533. {
  534. return x86_read_percpu(xen_vcpu_info.arch.cr2);
  535. }
  536. static void xen_write_cr4(unsigned long cr4)
  537. {
  538. cr4 &= ~X86_CR4_PGE;
  539. cr4 &= ~X86_CR4_PSE;
  540. native_write_cr4(cr4);
  541. }
  542. static unsigned long xen_read_cr3(void)
  543. {
  544. return x86_read_percpu(xen_cr3);
  545. }
  546. static void set_current_cr3(void *v)
  547. {
  548. x86_write_percpu(xen_current_cr3, (unsigned long)v);
  549. }
  550. static void xen_write_cr3(unsigned long cr3)
  551. {
  552. struct mmuext_op *op;
  553. struct multicall_space mcs;
  554. unsigned long mfn = pfn_to_mfn(PFN_DOWN(cr3));
  555. BUG_ON(preemptible());
  556. mcs = xen_mc_entry(sizeof(*op)); /* disables interrupts */
  557. /* Update while interrupts are disabled, so its atomic with
  558. respect to ipis */
  559. x86_write_percpu(xen_cr3, cr3);
  560. op = mcs.args;
  561. op->cmd = MMUEXT_NEW_BASEPTR;
  562. op->arg1.mfn = mfn;
  563. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  564. /* Update xen_update_cr3 once the batch has actually
  565. been submitted. */
  566. xen_mc_callback(set_current_cr3, (void *)cr3);
  567. xen_mc_issue(PARAVIRT_LAZY_CPU); /* interrupts restored */
  568. }
  569. /* Early in boot, while setting up the initial pagetable, assume
  570. everything is pinned. */
  571. static __init void xen_alloc_pte_init(struct mm_struct *mm, u32 pfn)
  572. {
  573. #ifdef CONFIG_FLATMEM
  574. BUG_ON(mem_map); /* should only be used early */
  575. #endif
  576. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  577. }
  578. /* Early release_pte assumes that all pts are pinned, since there's
  579. only init_mm and anything attached to that is pinned. */
  580. static void xen_release_pte_init(u32 pfn)
  581. {
  582. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  583. }
  584. static void pin_pagetable_pfn(unsigned cmd, unsigned long pfn)
  585. {
  586. struct mmuext_op op;
  587. op.cmd = cmd;
  588. op.arg1.mfn = pfn_to_mfn(pfn);
  589. if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
  590. BUG();
  591. }
  592. /* This needs to make sure the new pte page is pinned iff its being
  593. attached to a pinned pagetable. */
  594. static void xen_alloc_ptpage(struct mm_struct *mm, u32 pfn, unsigned level)
  595. {
  596. struct page *page = pfn_to_page(pfn);
  597. if (PagePinned(virt_to_page(mm->pgd))) {
  598. SetPagePinned(page);
  599. if (!PageHighMem(page)) {
  600. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  601. if (level == PT_PTE)
  602. pin_pagetable_pfn(MMUEXT_PIN_L1_TABLE, pfn);
  603. } else
  604. /* make sure there are no stray mappings of
  605. this page */
  606. kmap_flush_unused();
  607. }
  608. }
  609. static void xen_alloc_pte(struct mm_struct *mm, u32 pfn)
  610. {
  611. xen_alloc_ptpage(mm, pfn, PT_PTE);
  612. }
  613. static void xen_alloc_pmd(struct mm_struct *mm, u32 pfn)
  614. {
  615. xen_alloc_ptpage(mm, pfn, PT_PMD);
  616. }
  617. /* This should never happen until we're OK to use struct page */
  618. static void xen_release_ptpage(u32 pfn, unsigned level)
  619. {
  620. struct page *page = pfn_to_page(pfn);
  621. if (PagePinned(page)) {
  622. if (!PageHighMem(page)) {
  623. if (level == PT_PTE)
  624. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, pfn);
  625. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  626. }
  627. ClearPagePinned(page);
  628. }
  629. }
  630. static void xen_release_pte(u32 pfn)
  631. {
  632. xen_release_ptpage(pfn, PT_PTE);
  633. }
  634. static void xen_release_pmd(u32 pfn)
  635. {
  636. xen_release_ptpage(pfn, PT_PMD);
  637. }
  638. #if PAGETABLE_LEVELS == 4
  639. static void xen_alloc_pud(struct mm_struct *mm, u32 pfn)
  640. {
  641. xen_alloc_ptpage(mm, pfn, PT_PUD);
  642. }
  643. static void xen_release_pud(u32 pfn)
  644. {
  645. xen_release_ptpage(pfn, PT_PUD);
  646. }
  647. #endif
  648. #ifdef CONFIG_HIGHPTE
  649. static void *xen_kmap_atomic_pte(struct page *page, enum km_type type)
  650. {
  651. pgprot_t prot = PAGE_KERNEL;
  652. if (PagePinned(page))
  653. prot = PAGE_KERNEL_RO;
  654. if (0 && PageHighMem(page))
  655. printk("mapping highpte %lx type %d prot %s\n",
  656. page_to_pfn(page), type,
  657. (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ");
  658. return kmap_atomic_prot(page, type, prot);
  659. }
  660. #endif
  661. static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
  662. {
  663. /* If there's an existing pte, then don't allow _PAGE_RW to be set */
  664. if (pte_val_ma(*ptep) & _PAGE_PRESENT)
  665. pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
  666. pte_val_ma(pte));
  667. return pte;
  668. }
  669. /* Init-time set_pte while constructing initial pagetables, which
  670. doesn't allow RO pagetable pages to be remapped RW */
  671. static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
  672. {
  673. pte = mask_rw_pte(ptep, pte);
  674. xen_set_pte(ptep, pte);
  675. }
  676. static __init void xen_pagetable_setup_start(pgd_t *base)
  677. {
  678. }
  679. void xen_setup_shared_info(void)
  680. {
  681. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  682. set_fixmap(FIX_PARAVIRT_BOOTMAP,
  683. xen_start_info->shared_info);
  684. HYPERVISOR_shared_info =
  685. (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
  686. } else
  687. HYPERVISOR_shared_info =
  688. (struct shared_info *)__va(xen_start_info->shared_info);
  689. #ifndef CONFIG_SMP
  690. /* In UP this is as good a place as any to set up shared info */
  691. xen_setup_vcpu_info_placement();
  692. #endif
  693. xen_setup_mfn_list_list();
  694. }
  695. static __init void xen_pagetable_setup_done(pgd_t *base)
  696. {
  697. xen_setup_shared_info();
  698. }
  699. static __init void xen_post_allocator_init(void)
  700. {
  701. pv_mmu_ops.set_pte = xen_set_pte;
  702. pv_mmu_ops.set_pmd = xen_set_pmd;
  703. pv_mmu_ops.set_pud = xen_set_pud;
  704. #if PAGETABLE_LEVELS == 4
  705. pv_mmu_ops.set_pgd = xen_set_pgd;
  706. #endif
  707. /* This will work as long as patching hasn't happened yet
  708. (which it hasn't) */
  709. pv_mmu_ops.alloc_pte = xen_alloc_pte;
  710. pv_mmu_ops.alloc_pmd = xen_alloc_pmd;
  711. pv_mmu_ops.release_pte = xen_release_pte;
  712. pv_mmu_ops.release_pmd = xen_release_pmd;
  713. #if PAGETABLE_LEVELS == 4
  714. pv_mmu_ops.alloc_pud = xen_alloc_pud;
  715. pv_mmu_ops.release_pud = xen_release_pud;
  716. #endif
  717. xen_mark_init_mm_pinned();
  718. }
  719. /* This is called once we have the cpu_possible_map */
  720. void xen_setup_vcpu_info_placement(void)
  721. {
  722. int cpu;
  723. for_each_possible_cpu(cpu)
  724. xen_vcpu_setup(cpu);
  725. /* xen_vcpu_setup managed to place the vcpu_info within the
  726. percpu area for all cpus, so make use of it */
  727. #ifdef CONFIG_X86_32
  728. if (have_vcpu_info_placement) {
  729. printk(KERN_INFO "Xen: using vcpu_info placement\n");
  730. pv_irq_ops.save_fl = xen_save_fl_direct;
  731. pv_irq_ops.restore_fl = xen_restore_fl_direct;
  732. pv_irq_ops.irq_disable = xen_irq_disable_direct;
  733. pv_irq_ops.irq_enable = xen_irq_enable_direct;
  734. pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
  735. }
  736. #endif
  737. }
  738. static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
  739. unsigned long addr, unsigned len)
  740. {
  741. char *start, *end, *reloc;
  742. unsigned ret;
  743. start = end = reloc = NULL;
  744. #define SITE(op, x) \
  745. case PARAVIRT_PATCH(op.x): \
  746. if (have_vcpu_info_placement) { \
  747. start = (char *)xen_##x##_direct; \
  748. end = xen_##x##_direct_end; \
  749. reloc = xen_##x##_direct_reloc; \
  750. } \
  751. goto patch_site
  752. switch (type) {
  753. #ifdef CONFIG_X86_32
  754. SITE(pv_irq_ops, irq_enable);
  755. SITE(pv_irq_ops, irq_disable);
  756. SITE(pv_irq_ops, save_fl);
  757. SITE(pv_irq_ops, restore_fl);
  758. #endif /* CONFIG_X86_32 */
  759. #undef SITE
  760. patch_site:
  761. if (start == NULL || (end-start) > len)
  762. goto default_patch;
  763. ret = paravirt_patch_insns(insnbuf, len, start, end);
  764. /* Note: because reloc is assigned from something that
  765. appears to be an array, gcc assumes it's non-null,
  766. but doesn't know its relationship with start and
  767. end. */
  768. if (reloc > start && reloc < end) {
  769. int reloc_off = reloc - start;
  770. long *relocp = (long *)(insnbuf + reloc_off);
  771. long delta = start - (char *)addr;
  772. *relocp += delta;
  773. }
  774. break;
  775. default_patch:
  776. default:
  777. ret = paravirt_patch_default(type, clobbers, insnbuf,
  778. addr, len);
  779. break;
  780. }
  781. return ret;
  782. }
  783. static void xen_set_fixmap(unsigned idx, unsigned long phys, pgprot_t prot)
  784. {
  785. pte_t pte;
  786. phys >>= PAGE_SHIFT;
  787. switch (idx) {
  788. case FIX_BTMAP_END ... FIX_BTMAP_BEGIN:
  789. #ifdef CONFIG_X86_F00F_BUG
  790. case FIX_F00F_IDT:
  791. #endif
  792. #ifdef CONFIG_X86_32
  793. case FIX_WP_TEST:
  794. case FIX_VDSO:
  795. case FIX_KMAP_BEGIN ... FIX_KMAP_END:
  796. #else
  797. case VSYSCALL_LAST_PAGE ... VSYSCALL_FIRST_PAGE:
  798. #endif
  799. #ifdef CONFIG_X86_LOCAL_APIC
  800. case FIX_APIC_BASE: /* maps dummy local APIC */
  801. #endif
  802. pte = pfn_pte(phys, prot);
  803. break;
  804. default:
  805. pte = mfn_pte(phys, prot);
  806. break;
  807. }
  808. __native_set_fixmap(idx, pte);
  809. }
  810. static const struct pv_info xen_info __initdata = {
  811. .paravirt_enabled = 1,
  812. .shared_kernel_pmd = 0,
  813. .name = "Xen",
  814. };
  815. static const struct pv_init_ops xen_init_ops __initdata = {
  816. .patch = xen_patch,
  817. .banner = xen_banner,
  818. .memory_setup = xen_memory_setup,
  819. .arch_setup = xen_arch_setup,
  820. .post_allocator_init = xen_post_allocator_init,
  821. };
  822. static const struct pv_time_ops xen_time_ops __initdata = {
  823. .time_init = xen_time_init,
  824. .set_wallclock = xen_set_wallclock,
  825. .get_wallclock = xen_get_wallclock,
  826. .get_tsc_khz = xen_tsc_khz,
  827. .sched_clock = xen_sched_clock,
  828. };
  829. static const struct pv_cpu_ops xen_cpu_ops __initdata = {
  830. .cpuid = xen_cpuid,
  831. .set_debugreg = xen_set_debugreg,
  832. .get_debugreg = xen_get_debugreg,
  833. .clts = xen_clts,
  834. .read_cr0 = native_read_cr0,
  835. .write_cr0 = xen_write_cr0,
  836. .read_cr4 = native_read_cr4,
  837. .read_cr4_safe = native_read_cr4_safe,
  838. .write_cr4 = xen_write_cr4,
  839. .wbinvd = native_wbinvd,
  840. .read_msr = native_read_msr_safe,
  841. .write_msr = native_write_msr_safe,
  842. .read_tsc = native_read_tsc,
  843. .read_pmc = native_read_pmc,
  844. .iret = xen_iret,
  845. .irq_enable_sysexit = xen_sysexit,
  846. .load_tr_desc = paravirt_nop,
  847. .set_ldt = xen_set_ldt,
  848. .load_gdt = xen_load_gdt,
  849. .load_idt = xen_load_idt,
  850. .load_tls = xen_load_tls,
  851. .store_gdt = native_store_gdt,
  852. .store_idt = native_store_idt,
  853. .store_tr = xen_store_tr,
  854. .write_ldt_entry = xen_write_ldt_entry,
  855. .write_gdt_entry = xen_write_gdt_entry,
  856. .write_idt_entry = xen_write_idt_entry,
  857. .load_sp0 = xen_load_sp0,
  858. .set_iopl_mask = xen_set_iopl_mask,
  859. .io_delay = xen_io_delay,
  860. .lazy_mode = {
  861. .enter = paravirt_enter_lazy_cpu,
  862. .leave = xen_leave_lazy,
  863. },
  864. };
  865. static const struct pv_irq_ops xen_irq_ops __initdata = {
  866. .init_IRQ = xen_init_IRQ,
  867. .save_fl = xen_save_fl,
  868. .restore_fl = xen_restore_fl,
  869. .irq_disable = xen_irq_disable,
  870. .irq_enable = xen_irq_enable,
  871. .safe_halt = xen_safe_halt,
  872. .halt = xen_halt,
  873. #ifdef CONFIG_X86_64
  874. .adjust_exception_frame = paravirt_nop,
  875. #endif
  876. };
  877. static const struct pv_apic_ops xen_apic_ops __initdata = {
  878. #ifdef CONFIG_X86_LOCAL_APIC
  879. .apic_write = xen_apic_write,
  880. .apic_write_atomic = xen_apic_write,
  881. .apic_read = xen_apic_read,
  882. .setup_boot_clock = paravirt_nop,
  883. .setup_secondary_clock = paravirt_nop,
  884. .startup_ipi_hook = paravirt_nop,
  885. #endif
  886. };
  887. static const struct pv_mmu_ops xen_mmu_ops __initdata = {
  888. .pagetable_setup_start = xen_pagetable_setup_start,
  889. .pagetable_setup_done = xen_pagetable_setup_done,
  890. .read_cr2 = xen_read_cr2,
  891. .write_cr2 = xen_write_cr2,
  892. .read_cr3 = xen_read_cr3,
  893. .write_cr3 = xen_write_cr3,
  894. .flush_tlb_user = xen_flush_tlb,
  895. .flush_tlb_kernel = xen_flush_tlb,
  896. .flush_tlb_single = xen_flush_tlb_single,
  897. .flush_tlb_others = xen_flush_tlb_others,
  898. .pte_update = paravirt_nop,
  899. .pte_update_defer = paravirt_nop,
  900. .pgd_alloc = __paravirt_pgd_alloc,
  901. .pgd_free = paravirt_nop,
  902. .alloc_pte = xen_alloc_pte_init,
  903. .release_pte = xen_release_pte_init,
  904. .alloc_pmd = xen_alloc_pte_init,
  905. .alloc_pmd_clone = paravirt_nop,
  906. .release_pmd = xen_release_pte_init,
  907. #ifdef CONFIG_HIGHPTE
  908. .kmap_atomic_pte = xen_kmap_atomic_pte,
  909. #endif
  910. #ifdef CONFIG_X86_64
  911. .set_pte = xen_set_pte,
  912. #else
  913. .set_pte = xen_set_pte_init,
  914. #endif
  915. .set_pte_at = xen_set_pte_at,
  916. .set_pmd = xen_set_pmd_hyper,
  917. .ptep_modify_prot_start = __ptep_modify_prot_start,
  918. .ptep_modify_prot_commit = __ptep_modify_prot_commit,
  919. .pte_val = xen_pte_val,
  920. .pte_flags = native_pte_val,
  921. .pgd_val = xen_pgd_val,
  922. .make_pte = xen_make_pte,
  923. .make_pgd = xen_make_pgd,
  924. #ifdef CONFIG_X86_PAE
  925. .set_pte_atomic = xen_set_pte_atomic,
  926. .set_pte_present = xen_set_pte_at,
  927. .pte_clear = xen_pte_clear,
  928. .pmd_clear = xen_pmd_clear,
  929. #endif /* CONFIG_X86_PAE */
  930. .set_pud = xen_set_pud_hyper,
  931. .make_pmd = xen_make_pmd,
  932. .pmd_val = xen_pmd_val,
  933. #if PAGETABLE_LEVELS == 4
  934. .pud_val = xen_pud_val,
  935. .make_pud = xen_make_pud,
  936. .set_pgd = xen_set_pgd_hyper,
  937. .alloc_pud = xen_alloc_pte_init,
  938. .release_pud = xen_release_pte_init,
  939. #endif /* PAGETABLE_LEVELS == 4 */
  940. .activate_mm = xen_activate_mm,
  941. .dup_mmap = xen_dup_mmap,
  942. .exit_mmap = xen_exit_mmap,
  943. .lazy_mode = {
  944. .enter = paravirt_enter_lazy_mmu,
  945. .leave = xen_leave_lazy,
  946. },
  947. .set_fixmap = xen_set_fixmap,
  948. };
  949. static void xen_reboot(int reason)
  950. {
  951. struct sched_shutdown r = { .reason = reason };
  952. #ifdef CONFIG_SMP
  953. smp_send_stop();
  954. #endif
  955. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
  956. BUG();
  957. }
  958. static void xen_restart(char *msg)
  959. {
  960. xen_reboot(SHUTDOWN_reboot);
  961. }
  962. static void xen_emergency_restart(void)
  963. {
  964. xen_reboot(SHUTDOWN_reboot);
  965. }
  966. static void xen_machine_halt(void)
  967. {
  968. xen_reboot(SHUTDOWN_poweroff);
  969. }
  970. static void xen_crash_shutdown(struct pt_regs *regs)
  971. {
  972. xen_reboot(SHUTDOWN_crash);
  973. }
  974. static const struct machine_ops __initdata xen_machine_ops = {
  975. .restart = xen_restart,
  976. .halt = xen_machine_halt,
  977. .power_off = xen_machine_halt,
  978. .shutdown = xen_machine_halt,
  979. .crash_shutdown = xen_crash_shutdown,
  980. .emergency_restart = xen_emergency_restart,
  981. };
  982. static void __init xen_reserve_top(void)
  983. {
  984. #ifdef CONFIG_X86_32
  985. unsigned long top = HYPERVISOR_VIRT_START;
  986. struct xen_platform_parameters pp;
  987. if (HYPERVISOR_xen_version(XENVER_platform_parameters, &pp) == 0)
  988. top = pp.virt_start;
  989. reserve_top_address(-top + 2 * PAGE_SIZE);
  990. #endif /* CONFIG_X86_32 */
  991. }
  992. /*
  993. * Like __va(), but returns address in the kernel mapping (which is
  994. * all we have until the physical memory mapping has been set up.
  995. */
  996. static void *__ka(phys_addr_t paddr)
  997. {
  998. #ifdef CONFIG_X86_64
  999. return (void *)(paddr + __START_KERNEL_map);
  1000. #else
  1001. return __va(paddr);
  1002. #endif
  1003. }
  1004. /* Convert a machine address to physical address */
  1005. static unsigned long m2p(phys_addr_t maddr)
  1006. {
  1007. phys_addr_t paddr;
  1008. maddr &= PTE_MASK;
  1009. paddr = mfn_to_pfn(maddr >> PAGE_SHIFT) << PAGE_SHIFT;
  1010. return paddr;
  1011. }
  1012. /* Convert a machine address to kernel virtual */
  1013. static void *m2v(phys_addr_t maddr)
  1014. {
  1015. return __ka(m2p(maddr));
  1016. }
  1017. #ifdef CONFIG_X86_64
  1018. static void walk(pgd_t *pgd, unsigned long addr)
  1019. {
  1020. unsigned l4idx = pgd_index(addr);
  1021. unsigned l3idx = pud_index(addr);
  1022. unsigned l2idx = pmd_index(addr);
  1023. unsigned l1idx = pte_index(addr);
  1024. pgd_t l4;
  1025. pud_t l3;
  1026. pmd_t l2;
  1027. pte_t l1;
  1028. xen_raw_printk("walk %p, %lx -> %d %d %d %d\n",
  1029. pgd, addr, l4idx, l3idx, l2idx, l1idx);
  1030. l4 = pgd[l4idx];
  1031. xen_raw_printk(" l4: %016lx\n", l4.pgd);
  1032. xen_raw_printk(" %016lx\n", pgd_val(l4));
  1033. l3 = ((pud_t *)(m2v(l4.pgd)))[l3idx];
  1034. xen_raw_printk(" l3: %016lx\n", l3.pud);
  1035. xen_raw_printk(" %016lx\n", pud_val(l3));
  1036. l2 = ((pmd_t *)(m2v(l3.pud)))[l2idx];
  1037. xen_raw_printk(" l2: %016lx\n", l2.pmd);
  1038. xen_raw_printk(" %016lx\n", pmd_val(l2));
  1039. l1 = ((pte_t *)(m2v(l2.pmd)))[l1idx];
  1040. xen_raw_printk(" l1: %016lx\n", l1.pte);
  1041. xen_raw_printk(" %016lx\n", pte_val(l1));
  1042. }
  1043. #endif
  1044. static void set_page_prot(void *addr, pgprot_t prot)
  1045. {
  1046. unsigned long pfn = __pa(addr) >> PAGE_SHIFT;
  1047. pte_t pte = pfn_pte(pfn, prot);
  1048. xen_raw_printk("addr=%p pfn=%lx mfn=%lx prot=%016llx pte=%016llx\n",
  1049. addr, pfn, get_phys_to_machine(pfn),
  1050. pgprot_val(prot), pte.pte);
  1051. if (HYPERVISOR_update_va_mapping((unsigned long)addr, pte, 0))
  1052. BUG();
  1053. }
  1054. /*
  1055. * Identity map, in addition to plain kernel map. This needs to be
  1056. * large enough to allocate page table pages to allocate the rest.
  1057. * Each page can map 2MB.
  1058. */
  1059. static pte_t level1_ident_pgt[PTRS_PER_PTE * 4] __page_aligned_bss;
  1060. static __init void xen_map_identity_early(pmd_t *pmd, unsigned long max_pfn)
  1061. {
  1062. unsigned pmdidx, pteidx;
  1063. unsigned ident_pte;
  1064. unsigned long pfn;
  1065. ident_pte = 0;
  1066. pfn = 0;
  1067. for(pmdidx = 0; pmdidx < PTRS_PER_PMD && pfn < max_pfn; pmdidx++) {
  1068. pte_t *pte_page;
  1069. /* Reuse or allocate a page of ptes */
  1070. if (pmd_present(pmd[pmdidx]))
  1071. pte_page = m2v(pmd[pmdidx].pmd);
  1072. else {
  1073. /* Check for free pte pages */
  1074. if (ident_pte == ARRAY_SIZE(level1_ident_pgt))
  1075. break;
  1076. pte_page = &level1_ident_pgt[ident_pte];
  1077. ident_pte += PTRS_PER_PTE;
  1078. pmd[pmdidx] = __pmd(__pa(pte_page) | _PAGE_TABLE);
  1079. }
  1080. /* Install mappings */
  1081. for(pteidx = 0; pteidx < PTRS_PER_PTE; pteidx++, pfn++) {
  1082. pte_t pte;
  1083. if (pfn > max_pfn_mapped)
  1084. max_pfn_mapped = pfn;
  1085. if (!pte_none(pte_page[pteidx]))
  1086. continue;
  1087. pte = pfn_pte(pfn, PAGE_KERNEL_EXEC);
  1088. pte_page[pteidx] = pte;
  1089. }
  1090. }
  1091. for(pteidx = 0; pteidx < ident_pte; pteidx += PTRS_PER_PTE)
  1092. set_page_prot(&level1_ident_pgt[pteidx], PAGE_KERNEL_RO);
  1093. set_page_prot(pmd, PAGE_KERNEL_RO);
  1094. }
  1095. #ifdef CONFIG_X86_64
  1096. static void convert_pfn_mfn(void *v)
  1097. {
  1098. pte_t *pte = v;
  1099. int i;
  1100. /* All levels are converted the same way, so just treat them
  1101. as ptes. */
  1102. for(i = 0; i < PTRS_PER_PTE; i++)
  1103. pte[i] = xen_make_pte(pte[i].pte);
  1104. }
  1105. /*
  1106. * Set up the inital kernel pagetable.
  1107. *
  1108. * We can construct this by grafting the Xen provided pagetable into
  1109. * head_64.S's preconstructed pagetables. We copy the Xen L2's into
  1110. * level2_ident_pgt, level2_kernel_pgt and level2_fixmap_pgt. This
  1111. * means that only the kernel has a physical mapping to start with -
  1112. * but that's enough to get __va working. We need to fill in the rest
  1113. * of the physical mapping once some sort of allocator has been set
  1114. * up.
  1115. */
  1116. static __init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd, unsigned long max_pfn)
  1117. {
  1118. pud_t *l3;
  1119. pmd_t *l2;
  1120. /* Zap identity mapping */
  1121. init_level4_pgt[0] = __pgd(0);
  1122. /* Pre-constructed entries are in pfn, so convert to mfn */
  1123. convert_pfn_mfn(init_level4_pgt);
  1124. convert_pfn_mfn(level3_ident_pgt);
  1125. convert_pfn_mfn(level3_kernel_pgt);
  1126. l3 = m2v(pgd[pgd_index(__START_KERNEL_map)].pgd);
  1127. l2 = m2v(l3[pud_index(__START_KERNEL_map)].pud);
  1128. memcpy(level2_ident_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1129. memcpy(level2_kernel_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1130. l3 = m2v(pgd[pgd_index(__START_KERNEL_map + PMD_SIZE)].pgd);
  1131. l2 = m2v(l3[pud_index(__START_KERNEL_map + PMD_SIZE)].pud);
  1132. memcpy(level2_fixmap_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1133. /* Set up identity map */
  1134. xen_map_identity_early(level2_ident_pgt, max_pfn);
  1135. /* Make pagetable pieces RO */
  1136. set_page_prot(init_level4_pgt, PAGE_KERNEL_RO);
  1137. set_page_prot(level3_ident_pgt, PAGE_KERNEL_RO);
  1138. set_page_prot(level3_kernel_pgt, PAGE_KERNEL_RO);
  1139. set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO);
  1140. set_page_prot(level2_fixmap_pgt, PAGE_KERNEL_RO);
  1141. /* Pin down new L4 */
  1142. pin_pagetable_pfn(MMUEXT_PIN_L4_TABLE,
  1143. PFN_DOWN(__pa_symbol(init_level4_pgt)));
  1144. /* Unpin Xen-provided one */
  1145. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
  1146. /* Switch over */
  1147. pgd = init_level4_pgt;
  1148. xen_write_cr3(__pa(pgd));
  1149. reserve_early(__pa(xen_start_info->pt_base),
  1150. __pa(xen_start_info->pt_base +
  1151. xen_start_info->nr_pt_frames * PAGE_SIZE),
  1152. "XEN PAGETABLES");
  1153. return pgd;
  1154. }
  1155. #else /* !CONFIG_X86_64 */
  1156. static pmd_t level2_kernel_pgt[PTRS_PER_PMD] __page_aligned_bss;
  1157. static __init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd, unsigned long max_pfn)
  1158. {
  1159. pmd_t *kernel_pmd;
  1160. init_pg_tables_start = __pa(pgd);
  1161. init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE;
  1162. max_pfn_mapped = PFN_DOWN(init_pg_tables_end + 512*1024);
  1163. kernel_pmd = m2v(pgd[KERNEL_PGD_BOUNDARY].pgd);
  1164. memcpy(level2_kernel_pgt, kernel_pmd, sizeof(pmd_t) * PTRS_PER_PMD);
  1165. xen_map_identity_early(level2_kernel_pgt, max_pfn);
  1166. memcpy(swapper_pg_dir, pgd, sizeof(pgd_t) * PTRS_PER_PGD);
  1167. set_pgd(&swapper_pg_dir[KERNEL_PGD_BOUNDARY],
  1168. __pgd(__pa(level2_kernel_pgt) | _PAGE_PRESENT));
  1169. set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO);
  1170. set_page_prot(swapper_pg_dir, PAGE_KERNEL_RO);
  1171. set_page_prot(empty_zero_page, PAGE_KERNEL_RO);
  1172. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
  1173. xen_write_cr3(__pa(swapper_pg_dir));
  1174. pin_pagetable_pfn(MMUEXT_PIN_L3_TABLE, PFN_DOWN(__pa(swapper_pg_dir)));
  1175. return swapper_pg_dir;
  1176. }
  1177. #endif /* CONFIG_X86_64 */
  1178. /* First C function to be called on Xen boot */
  1179. asmlinkage void __init xen_start_kernel(void)
  1180. {
  1181. pgd_t *pgd;
  1182. if (!xen_start_info)
  1183. return;
  1184. BUG_ON(memcmp(xen_start_info->magic, "xen-3", 5) != 0);
  1185. xen_setup_features();
  1186. /* Install Xen paravirt ops */
  1187. pv_info = xen_info;
  1188. pv_init_ops = xen_init_ops;
  1189. pv_time_ops = xen_time_ops;
  1190. pv_cpu_ops = xen_cpu_ops;
  1191. pv_irq_ops = xen_irq_ops;
  1192. pv_apic_ops = xen_apic_ops;
  1193. pv_mmu_ops = xen_mmu_ops;
  1194. if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
  1195. pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
  1196. pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
  1197. }
  1198. machine_ops = xen_machine_ops;
  1199. #ifdef CONFIG_X86_64
  1200. /* Disable until direct per-cpu data access. */
  1201. have_vcpu_info_placement = 0;
  1202. x86_64_init_pda();
  1203. #endif
  1204. xen_smp_init();
  1205. /* Get mfn list */
  1206. if (!xen_feature(XENFEAT_auto_translated_physmap))
  1207. xen_build_dynamic_phys_to_machine();
  1208. pgd = (pgd_t *)xen_start_info->pt_base;
  1209. /* Prevent unwanted bits from being set in PTEs. */
  1210. __supported_pte_mask &= ~_PAGE_GLOBAL;
  1211. if (!is_initial_xendomain())
  1212. __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
  1213. /* Don't do the full vcpu_info placement stuff until we have a
  1214. possible map and a non-dummy shared_info. */
  1215. per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
  1216. xen_raw_console_write("mapping kernel into physical memory\n");
  1217. pgd = xen_setup_kernel_pagetable(pgd, xen_start_info->nr_pages);
  1218. init_mm.pgd = pgd;
  1219. /* keep using Xen gdt for now; no urgent need to change it */
  1220. pv_info.kernel_rpl = 1;
  1221. if (xen_feature(XENFEAT_supervisor_mode_kernel))
  1222. pv_info.kernel_rpl = 0;
  1223. /* set the limit of our address space */
  1224. xen_reserve_top();
  1225. #ifdef CONFIG_X86_32
  1226. /* set up basic CPUID stuff */
  1227. cpu_detect(&new_cpu_data);
  1228. new_cpu_data.hard_math = 1;
  1229. new_cpu_data.x86_capability[0] = cpuid_edx(1);
  1230. #endif
  1231. /* Poke various useful things into boot_params */
  1232. boot_params.hdr.type_of_loader = (9 << 4) | 0;
  1233. boot_params.hdr.ramdisk_image = xen_start_info->mod_start
  1234. ? __pa(xen_start_info->mod_start) : 0;
  1235. boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
  1236. if (!is_initial_xendomain()) {
  1237. add_preferred_console("xenboot", 0, NULL);
  1238. add_preferred_console("tty", 0, NULL);
  1239. add_preferred_console("hvc", 0, NULL);
  1240. }
  1241. xen_raw_console_write("about to get started...\n");
  1242. #if 0
  1243. xen_raw_printk("&boot_params=%p __pa(&boot_params)=%lx __va(__pa(&boot_params))=%lx\n",
  1244. &boot_params, __pa_symbol(&boot_params),
  1245. __va(__pa_symbol(&boot_params)));
  1246. walk(pgd, &boot_params);
  1247. walk(pgd, __va(__pa(&boot_params)));
  1248. #endif
  1249. /* Start the world */
  1250. #ifdef CONFIG_X86_32
  1251. i386_start_kernel();
  1252. #else
  1253. x86_64_start_reservations((char *)__pa_symbol(&boot_params));
  1254. #endif
  1255. }