enlighten.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  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, u32 low, u32 high,
  332. struct trap_info *info)
  333. {
  334. u8 type, dpl;
  335. type = (high >> 8) & 0x1f;
  336. dpl = (high >> 13) & 3;
  337. if (type != 0xf && type != 0xe)
  338. return 0;
  339. info->vector = vector;
  340. info->address = (high & 0xffff0000) | (low & 0x0000ffff);
  341. info->cs = low >> 16;
  342. info->flags = dpl;
  343. /* interrupt gates clear IF */
  344. if (type == 0xe)
  345. info->flags |= 4;
  346. return 1;
  347. }
  348. /* Locations of each CPU's IDT */
  349. static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
  350. /* Set an IDT entry. If the entry is part of the current IDT, then
  351. also update Xen. */
  352. static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
  353. {
  354. unsigned long p = (unsigned long)&dt[entrynum];
  355. unsigned long start, end;
  356. preempt_disable();
  357. start = __get_cpu_var(idt_desc).address;
  358. end = start + __get_cpu_var(idt_desc).size + 1;
  359. xen_mc_flush();
  360. native_write_idt_entry(dt, entrynum, g);
  361. if (p >= start && (p + 8) <= end) {
  362. struct trap_info info[2];
  363. u32 *desc = (u32 *)g;
  364. info[1].address = 0;
  365. if (cvt_gate_to_trap(entrynum, desc[0], desc[1], &info[0]))
  366. if (HYPERVISOR_set_trap_table(info))
  367. BUG();
  368. }
  369. preempt_enable();
  370. }
  371. static void xen_convert_trap_info(const struct desc_ptr *desc,
  372. struct trap_info *traps)
  373. {
  374. unsigned in, out, count;
  375. count = (desc->size+1) / 8;
  376. BUG_ON(count > 256);
  377. for (in = out = 0; in < count; in++) {
  378. const u32 *entry = (u32 *)(desc->address + in * 8);
  379. if (cvt_gate_to_trap(in, entry[0], entry[1], &traps[out]))
  380. out++;
  381. }
  382. traps[out].address = 0;
  383. }
  384. void xen_copy_trap_info(struct trap_info *traps)
  385. {
  386. const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
  387. xen_convert_trap_info(desc, traps);
  388. }
  389. /* Load a new IDT into Xen. In principle this can be per-CPU, so we
  390. hold a spinlock to protect the static traps[] array (static because
  391. it avoids allocation, and saves stack space). */
  392. static void xen_load_idt(const struct desc_ptr *desc)
  393. {
  394. static DEFINE_SPINLOCK(lock);
  395. static struct trap_info traps[257];
  396. spin_lock(&lock);
  397. __get_cpu_var(idt_desc) = *desc;
  398. xen_convert_trap_info(desc, traps);
  399. xen_mc_flush();
  400. if (HYPERVISOR_set_trap_table(traps))
  401. BUG();
  402. spin_unlock(&lock);
  403. }
  404. /* Write a GDT descriptor entry. Ignore LDT descriptors, since
  405. they're handled differently. */
  406. static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
  407. const void *desc, int type)
  408. {
  409. preempt_disable();
  410. switch (type) {
  411. case DESC_LDT:
  412. case DESC_TSS:
  413. /* ignore */
  414. break;
  415. default: {
  416. xmaddr_t maddr = virt_to_machine(&dt[entry]);
  417. xen_mc_flush();
  418. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  419. BUG();
  420. }
  421. }
  422. preempt_enable();
  423. }
  424. static void xen_load_sp0(struct tss_struct *tss,
  425. struct thread_struct *thread)
  426. {
  427. struct multicall_space mcs = xen_mc_entry(0);
  428. MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
  429. xen_mc_issue(PARAVIRT_LAZY_CPU);
  430. }
  431. static void xen_set_iopl_mask(unsigned mask)
  432. {
  433. struct physdev_set_iopl set_iopl;
  434. /* Force the change at ring 0. */
  435. set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
  436. HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  437. }
  438. static void xen_io_delay(void)
  439. {
  440. }
  441. #ifdef CONFIG_X86_LOCAL_APIC
  442. static u32 xen_apic_read(unsigned long reg)
  443. {
  444. return 0;
  445. }
  446. static void xen_apic_write(unsigned long reg, u32 val)
  447. {
  448. /* Warn to see if there's any stray references */
  449. WARN_ON(1);
  450. }
  451. #endif
  452. static void xen_flush_tlb(void)
  453. {
  454. struct mmuext_op *op;
  455. struct multicall_space mcs;
  456. preempt_disable();
  457. mcs = xen_mc_entry(sizeof(*op));
  458. op = mcs.args;
  459. op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
  460. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  461. xen_mc_issue(PARAVIRT_LAZY_MMU);
  462. preempt_enable();
  463. }
  464. static void xen_flush_tlb_single(unsigned long addr)
  465. {
  466. struct mmuext_op *op;
  467. struct multicall_space mcs;
  468. preempt_disable();
  469. mcs = xen_mc_entry(sizeof(*op));
  470. op = mcs.args;
  471. op->cmd = MMUEXT_INVLPG_LOCAL;
  472. op->arg1.linear_addr = addr & PAGE_MASK;
  473. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  474. xen_mc_issue(PARAVIRT_LAZY_MMU);
  475. preempt_enable();
  476. }
  477. static void xen_flush_tlb_others(const cpumask_t *cpus, struct mm_struct *mm,
  478. unsigned long va)
  479. {
  480. struct {
  481. struct mmuext_op op;
  482. cpumask_t mask;
  483. } *args;
  484. cpumask_t cpumask = *cpus;
  485. struct multicall_space mcs;
  486. /*
  487. * A couple of (to be removed) sanity checks:
  488. *
  489. * - current CPU must not be in mask
  490. * - mask must exist :)
  491. */
  492. BUG_ON(cpus_empty(cpumask));
  493. BUG_ON(cpu_isset(smp_processor_id(), cpumask));
  494. BUG_ON(!mm);
  495. /* If a CPU which we ran on has gone down, OK. */
  496. cpus_and(cpumask, cpumask, cpu_online_map);
  497. if (cpus_empty(cpumask))
  498. return;
  499. mcs = xen_mc_entry(sizeof(*args));
  500. args = mcs.args;
  501. args->mask = cpumask;
  502. args->op.arg2.vcpumask = &args->mask;
  503. if (va == TLB_FLUSH_ALL) {
  504. args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
  505. } else {
  506. args->op.cmd = MMUEXT_INVLPG_MULTI;
  507. args->op.arg1.linear_addr = va;
  508. }
  509. MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
  510. xen_mc_issue(PARAVIRT_LAZY_MMU);
  511. }
  512. static void xen_clts(void)
  513. {
  514. struct multicall_space mcs;
  515. mcs = xen_mc_entry(0);
  516. MULTI_fpu_taskswitch(mcs.mc, 0);
  517. xen_mc_issue(PARAVIRT_LAZY_CPU);
  518. }
  519. static void xen_write_cr0(unsigned long cr0)
  520. {
  521. struct multicall_space mcs;
  522. /* Only pay attention to cr0.TS; everything else is
  523. ignored. */
  524. mcs = xen_mc_entry(0);
  525. MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
  526. xen_mc_issue(PARAVIRT_LAZY_CPU);
  527. }
  528. static void xen_write_cr2(unsigned long cr2)
  529. {
  530. x86_read_percpu(xen_vcpu)->arch.cr2 = cr2;
  531. }
  532. static unsigned long xen_read_cr2(void)
  533. {
  534. return x86_read_percpu(xen_vcpu)->arch.cr2;
  535. }
  536. static unsigned long xen_read_cr2_direct(void)
  537. {
  538. return x86_read_percpu(xen_vcpu_info.arch.cr2);
  539. }
  540. static void xen_write_cr4(unsigned long cr4)
  541. {
  542. cr4 &= ~X86_CR4_PGE;
  543. cr4 &= ~X86_CR4_PSE;
  544. native_write_cr4(cr4);
  545. }
  546. static unsigned long xen_read_cr3(void)
  547. {
  548. return x86_read_percpu(xen_cr3);
  549. }
  550. static void set_current_cr3(void *v)
  551. {
  552. x86_write_percpu(xen_current_cr3, (unsigned long)v);
  553. }
  554. static void xen_write_cr3(unsigned long cr3)
  555. {
  556. struct mmuext_op *op;
  557. struct multicall_space mcs;
  558. unsigned long mfn = pfn_to_mfn(PFN_DOWN(cr3));
  559. BUG_ON(preemptible());
  560. mcs = xen_mc_entry(sizeof(*op)); /* disables interrupts */
  561. /* Update while interrupts are disabled, so its atomic with
  562. respect to ipis */
  563. x86_write_percpu(xen_cr3, cr3);
  564. op = mcs.args;
  565. op->cmd = MMUEXT_NEW_BASEPTR;
  566. op->arg1.mfn = mfn;
  567. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  568. /* Update xen_update_cr3 once the batch has actually
  569. been submitted. */
  570. xen_mc_callback(set_current_cr3, (void *)cr3);
  571. xen_mc_issue(PARAVIRT_LAZY_CPU); /* interrupts restored */
  572. }
  573. /* Early in boot, while setting up the initial pagetable, assume
  574. everything is pinned. */
  575. static __init void xen_alloc_pte_init(struct mm_struct *mm, u32 pfn)
  576. {
  577. #ifdef CONFIG_FLATMEM
  578. BUG_ON(mem_map); /* should only be used early */
  579. #endif
  580. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  581. }
  582. /* Early release_pte assumes that all pts are pinned, since there's
  583. only init_mm and anything attached to that is pinned. */
  584. static void xen_release_pte_init(u32 pfn)
  585. {
  586. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  587. }
  588. static void pin_pagetable_pfn(unsigned cmd, unsigned long pfn)
  589. {
  590. struct mmuext_op op;
  591. op.cmd = cmd;
  592. op.arg1.mfn = pfn_to_mfn(pfn);
  593. if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
  594. BUG();
  595. }
  596. /* This needs to make sure the new pte page is pinned iff its being
  597. attached to a pinned pagetable. */
  598. static void xen_alloc_ptpage(struct mm_struct *mm, u32 pfn, unsigned level)
  599. {
  600. struct page *page = pfn_to_page(pfn);
  601. if (PagePinned(virt_to_page(mm->pgd))) {
  602. SetPagePinned(page);
  603. if (!PageHighMem(page)) {
  604. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  605. if (level == PT_PTE)
  606. pin_pagetable_pfn(MMUEXT_PIN_L1_TABLE, pfn);
  607. } else
  608. /* make sure there are no stray mappings of
  609. this page */
  610. kmap_flush_unused();
  611. }
  612. }
  613. static void xen_alloc_pte(struct mm_struct *mm, u32 pfn)
  614. {
  615. xen_alloc_ptpage(mm, pfn, PT_PTE);
  616. }
  617. static void xen_alloc_pmd(struct mm_struct *mm, u32 pfn)
  618. {
  619. xen_alloc_ptpage(mm, pfn, PT_PMD);
  620. }
  621. /* This should never happen until we're OK to use struct page */
  622. static void xen_release_ptpage(u32 pfn, unsigned level)
  623. {
  624. struct page *page = pfn_to_page(pfn);
  625. if (PagePinned(page)) {
  626. if (!PageHighMem(page)) {
  627. if (level == PT_PTE)
  628. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, pfn);
  629. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  630. }
  631. ClearPagePinned(page);
  632. }
  633. }
  634. static void xen_release_pte(u32 pfn)
  635. {
  636. xen_release_ptpage(pfn, PT_PTE);
  637. }
  638. static void xen_release_pmd(u32 pfn)
  639. {
  640. xen_release_ptpage(pfn, PT_PMD);
  641. }
  642. #if PAGETABLE_LEVELS == 4
  643. static void xen_alloc_pud(struct mm_struct *mm, u32 pfn)
  644. {
  645. xen_alloc_ptpage(mm, pfn, PT_PUD);
  646. }
  647. static void xen_release_pud(u32 pfn)
  648. {
  649. xen_release_ptpage(pfn, PT_PUD);
  650. }
  651. #endif
  652. #ifdef CONFIG_HIGHPTE
  653. static void *xen_kmap_atomic_pte(struct page *page, enum km_type type)
  654. {
  655. pgprot_t prot = PAGE_KERNEL;
  656. if (PagePinned(page))
  657. prot = PAGE_KERNEL_RO;
  658. if (0 && PageHighMem(page))
  659. printk("mapping highpte %lx type %d prot %s\n",
  660. page_to_pfn(page), type,
  661. (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ");
  662. return kmap_atomic_prot(page, type, prot);
  663. }
  664. #endif
  665. static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
  666. {
  667. /* If there's an existing pte, then don't allow _PAGE_RW to be set */
  668. if (pte_val_ma(*ptep) & _PAGE_PRESENT)
  669. pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
  670. pte_val_ma(pte));
  671. return pte;
  672. }
  673. /* Init-time set_pte while constructing initial pagetables, which
  674. doesn't allow RO pagetable pages to be remapped RW */
  675. static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
  676. {
  677. pte = mask_rw_pte(ptep, pte);
  678. xen_set_pte(ptep, pte);
  679. }
  680. static __init void xen_pagetable_setup_start(pgd_t *base)
  681. {
  682. #ifdef CONFIG_X86_32
  683. pgd_t *xen_pgd = (pgd_t *)xen_start_info->pt_base;
  684. int i;
  685. init_mm.pgd = base;
  686. /*
  687. * copy top-level of Xen-supplied pagetable into place. This
  688. * is a stand-in while we copy the pmd pages.
  689. */
  690. memcpy(base, xen_pgd, PTRS_PER_PGD * sizeof(pgd_t));
  691. /*
  692. * For PAE, need to allocate new pmds, rather than
  693. * share Xen's, since Xen doesn't like pmd's being
  694. * shared between address spaces.
  695. */
  696. for (i = 0; i < PTRS_PER_PGD; i++) {
  697. if (pgd_val_ma(xen_pgd[i]) & _PAGE_PRESENT) {
  698. pmd_t *pmd = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
  699. memcpy(pmd, (void *)pgd_page_vaddr(xen_pgd[i]),
  700. PAGE_SIZE);
  701. make_lowmem_page_readonly(pmd);
  702. set_pgd(&base[i], __pgd(1 + __pa(pmd)));
  703. } else
  704. pgd_clear(&base[i]);
  705. }
  706. /* make sure zero_page is mapped RO so we can use it in pagetables */
  707. make_lowmem_page_readonly(empty_zero_page);
  708. make_lowmem_page_readonly(base);
  709. /*
  710. * Switch to new pagetable. This is done before
  711. * pagetable_init has done anything so that the new pages
  712. * added to the table can be prepared properly for Xen.
  713. */
  714. xen_write_cr3(__pa(base));
  715. /* Unpin initial Xen pagetable */
  716. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE,
  717. PFN_DOWN(__pa(xen_start_info->pt_base)));
  718. #endif /* CONFIG_X86_32 */
  719. }
  720. void xen_setup_shared_info(void)
  721. {
  722. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  723. set_fixmap(FIX_PARAVIRT_BOOTMAP,
  724. xen_start_info->shared_info);
  725. HYPERVISOR_shared_info =
  726. (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
  727. } else
  728. HYPERVISOR_shared_info =
  729. (struct shared_info *)__va(xen_start_info->shared_info);
  730. #ifndef CONFIG_SMP
  731. /* In UP this is as good a place as any to set up shared info */
  732. xen_setup_vcpu_info_placement();
  733. #endif
  734. xen_setup_mfn_list_list();
  735. }
  736. static __init void xen_pagetable_setup_done(pgd_t *base)
  737. {
  738. /* This will work as long as patching hasn't happened yet
  739. (which it hasn't) */
  740. pv_mmu_ops.alloc_pte = xen_alloc_pte;
  741. pv_mmu_ops.alloc_pmd = xen_alloc_pmd;
  742. pv_mmu_ops.release_pte = xen_release_pte;
  743. pv_mmu_ops.release_pmd = xen_release_pmd;
  744. #if PAGETABLE_LEVELS == 4
  745. pv_mmu_ops.alloc_pud = xen_alloc_pud;
  746. pv_mmu_ops.release_pud = xen_release_pud;
  747. #endif
  748. pv_mmu_ops.set_pte = xen_set_pte;
  749. xen_setup_shared_info();
  750. #ifdef CONFIG_X86_32
  751. /* Actually pin the pagetable down, but we can't set PG_pinned
  752. yet because the page structures don't exist yet. */
  753. pin_pagetable_pfn(MMUEXT_PIN_L3_TABLE, PFN_DOWN(__pa(base)));
  754. #endif
  755. }
  756. static __init void xen_post_allocator_init(void)
  757. {
  758. pv_mmu_ops.set_pmd = xen_set_pmd;
  759. pv_mmu_ops.set_pud = xen_set_pud;
  760. #if PAGETABLE_LEVELS == 4
  761. pv_mmu_ops.set_pgd = xen_set_pgd;
  762. #endif
  763. xen_mark_init_mm_pinned();
  764. }
  765. /* This is called once we have the cpu_possible_map */
  766. void xen_setup_vcpu_info_placement(void)
  767. {
  768. int cpu;
  769. for_each_possible_cpu(cpu)
  770. xen_vcpu_setup(cpu);
  771. /* xen_vcpu_setup managed to place the vcpu_info within the
  772. percpu area for all cpus, so make use of it */
  773. #ifdef CONFIG_X86_32
  774. if (have_vcpu_info_placement) {
  775. printk(KERN_INFO "Xen: using vcpu_info placement\n");
  776. pv_irq_ops.save_fl = xen_save_fl_direct;
  777. pv_irq_ops.restore_fl = xen_restore_fl_direct;
  778. pv_irq_ops.irq_disable = xen_irq_disable_direct;
  779. pv_irq_ops.irq_enable = xen_irq_enable_direct;
  780. pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
  781. }
  782. #endif
  783. }
  784. static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
  785. unsigned long addr, unsigned len)
  786. {
  787. char *start, *end, *reloc;
  788. unsigned ret;
  789. start = end = reloc = NULL;
  790. #define SITE(op, x) \
  791. case PARAVIRT_PATCH(op.x): \
  792. if (have_vcpu_info_placement) { \
  793. start = (char *)xen_##x##_direct; \
  794. end = xen_##x##_direct_end; \
  795. reloc = xen_##x##_direct_reloc; \
  796. } \
  797. goto patch_site
  798. switch (type) {
  799. #ifdef CONFIG_X86_32
  800. SITE(pv_irq_ops, irq_enable);
  801. SITE(pv_irq_ops, irq_disable);
  802. SITE(pv_irq_ops, save_fl);
  803. SITE(pv_irq_ops, restore_fl);
  804. #endif /* CONFIG_X86_32 */
  805. #undef SITE
  806. patch_site:
  807. if (start == NULL || (end-start) > len)
  808. goto default_patch;
  809. ret = paravirt_patch_insns(insnbuf, len, start, end);
  810. /* Note: because reloc is assigned from something that
  811. appears to be an array, gcc assumes it's non-null,
  812. but doesn't know its relationship with start and
  813. end. */
  814. if (reloc > start && reloc < end) {
  815. int reloc_off = reloc - start;
  816. long *relocp = (long *)(insnbuf + reloc_off);
  817. long delta = start - (char *)addr;
  818. *relocp += delta;
  819. }
  820. break;
  821. default_patch:
  822. default:
  823. ret = paravirt_patch_default(type, clobbers, insnbuf,
  824. addr, len);
  825. break;
  826. }
  827. return ret;
  828. }
  829. static void xen_set_fixmap(unsigned idx, unsigned long phys, pgprot_t prot)
  830. {
  831. pte_t pte;
  832. phys >>= PAGE_SHIFT;
  833. switch (idx) {
  834. case FIX_BTMAP_END ... FIX_BTMAP_BEGIN:
  835. #ifdef CONFIG_X86_F00F_BUG
  836. case FIX_F00F_IDT:
  837. #endif
  838. #ifdef CONFIG_X86_32
  839. case FIX_WP_TEST:
  840. case FIX_VDSO:
  841. case FIX_KMAP_BEGIN ... FIX_KMAP_END:
  842. #else
  843. case VSYSCALL_LAST_PAGE ... VSYSCALL_FIRST_PAGE:
  844. #endif
  845. #ifdef CONFIG_X86_LOCAL_APIC
  846. case FIX_APIC_BASE: /* maps dummy local APIC */
  847. #endif
  848. pte = pfn_pte(phys, prot);
  849. break;
  850. default:
  851. pte = mfn_pte(phys, prot);
  852. break;
  853. }
  854. __native_set_fixmap(idx, pte);
  855. }
  856. static const struct pv_info xen_info __initdata = {
  857. .paravirt_enabled = 1,
  858. .shared_kernel_pmd = 0,
  859. .name = "Xen",
  860. };
  861. static const struct pv_init_ops xen_init_ops __initdata = {
  862. .patch = xen_patch,
  863. .banner = xen_banner,
  864. .memory_setup = xen_memory_setup,
  865. .arch_setup = xen_arch_setup,
  866. .post_allocator_init = xen_post_allocator_init,
  867. };
  868. static const struct pv_time_ops xen_time_ops __initdata = {
  869. .time_init = xen_time_init,
  870. .set_wallclock = xen_set_wallclock,
  871. .get_wallclock = xen_get_wallclock,
  872. .get_tsc_khz = xen_tsc_khz,
  873. .sched_clock = xen_sched_clock,
  874. };
  875. static const struct pv_cpu_ops xen_cpu_ops __initdata = {
  876. .cpuid = xen_cpuid,
  877. .set_debugreg = xen_set_debugreg,
  878. .get_debugreg = xen_get_debugreg,
  879. .clts = xen_clts,
  880. .read_cr0 = native_read_cr0,
  881. .write_cr0 = xen_write_cr0,
  882. .read_cr4 = native_read_cr4,
  883. .read_cr4_safe = native_read_cr4_safe,
  884. .write_cr4 = xen_write_cr4,
  885. .wbinvd = native_wbinvd,
  886. .read_msr = native_read_msr_safe,
  887. .write_msr = native_write_msr_safe,
  888. .read_tsc = native_read_tsc,
  889. .read_pmc = native_read_pmc,
  890. .iret = xen_iret,
  891. .irq_enable_sysexit = xen_sysexit,
  892. .load_tr_desc = paravirt_nop,
  893. .set_ldt = xen_set_ldt,
  894. .load_gdt = xen_load_gdt,
  895. .load_idt = xen_load_idt,
  896. .load_tls = xen_load_tls,
  897. .store_gdt = native_store_gdt,
  898. .store_idt = native_store_idt,
  899. .store_tr = xen_store_tr,
  900. .write_ldt_entry = xen_write_ldt_entry,
  901. .write_gdt_entry = xen_write_gdt_entry,
  902. .write_idt_entry = xen_write_idt_entry,
  903. .load_sp0 = xen_load_sp0,
  904. .set_iopl_mask = xen_set_iopl_mask,
  905. .io_delay = xen_io_delay,
  906. .lazy_mode = {
  907. .enter = paravirt_enter_lazy_cpu,
  908. .leave = xen_leave_lazy,
  909. },
  910. };
  911. static const struct pv_irq_ops xen_irq_ops __initdata = {
  912. .init_IRQ = xen_init_IRQ,
  913. .save_fl = xen_save_fl,
  914. .restore_fl = xen_restore_fl,
  915. .irq_disable = xen_irq_disable,
  916. .irq_enable = xen_irq_enable,
  917. .safe_halt = xen_safe_halt,
  918. .halt = xen_halt,
  919. #ifdef CONFIG_X86_64
  920. .adjust_exception_frame = paravirt_nop,
  921. #endif
  922. };
  923. static const struct pv_apic_ops xen_apic_ops __initdata = {
  924. #ifdef CONFIG_X86_LOCAL_APIC
  925. .apic_write = xen_apic_write,
  926. .apic_write_atomic = xen_apic_write,
  927. .apic_read = xen_apic_read,
  928. .setup_boot_clock = paravirt_nop,
  929. .setup_secondary_clock = paravirt_nop,
  930. .startup_ipi_hook = paravirt_nop,
  931. #endif
  932. };
  933. static const struct pv_mmu_ops xen_mmu_ops __initdata = {
  934. .pagetable_setup_start = xen_pagetable_setup_start,
  935. .pagetable_setup_done = xen_pagetable_setup_done,
  936. .read_cr2 = xen_read_cr2,
  937. .write_cr2 = xen_write_cr2,
  938. .read_cr3 = xen_read_cr3,
  939. .write_cr3 = xen_write_cr3,
  940. .flush_tlb_user = xen_flush_tlb,
  941. .flush_tlb_kernel = xen_flush_tlb,
  942. .flush_tlb_single = xen_flush_tlb_single,
  943. .flush_tlb_others = xen_flush_tlb_others,
  944. .pte_update = paravirt_nop,
  945. .pte_update_defer = paravirt_nop,
  946. .pgd_alloc = __paravirt_pgd_alloc,
  947. .pgd_free = paravirt_nop,
  948. .alloc_pte = xen_alloc_pte_init,
  949. .release_pte = xen_release_pte_init,
  950. .alloc_pmd = xen_alloc_pte_init,
  951. .alloc_pmd_clone = paravirt_nop,
  952. .release_pmd = xen_release_pte_init,
  953. #ifdef CONFIG_HIGHPTE
  954. .kmap_atomic_pte = xen_kmap_atomic_pte,
  955. #endif
  956. #ifdef CONFIG_X86_64
  957. .set_pte = xen_set_pte,
  958. #else
  959. .set_pte = xen_set_pte_init,
  960. #endif
  961. .set_pte_at = xen_set_pte_at,
  962. .set_pmd = xen_set_pmd_hyper,
  963. .ptep_modify_prot_start = __ptep_modify_prot_start,
  964. .ptep_modify_prot_commit = __ptep_modify_prot_commit,
  965. .pte_val = xen_pte_val,
  966. .pte_flags = native_pte_val,
  967. .pgd_val = xen_pgd_val,
  968. .make_pte = xen_make_pte,
  969. .make_pgd = xen_make_pgd,
  970. #ifdef CONFIG_X86_PAE
  971. .set_pte_atomic = xen_set_pte_atomic,
  972. .set_pte_present = xen_set_pte_at,
  973. .pte_clear = xen_pte_clear,
  974. .pmd_clear = xen_pmd_clear,
  975. #endif /* CONFIG_X86_PAE */
  976. .set_pud = xen_set_pud_hyper,
  977. .make_pmd = xen_make_pmd,
  978. .pmd_val = xen_pmd_val,
  979. #if PAGETABLE_LEVELS == 4
  980. .pud_val = xen_pud_val,
  981. .make_pud = xen_make_pud,
  982. .set_pgd = xen_set_pgd_hyper,
  983. .alloc_pud = xen_alloc_pte_init,
  984. .release_pud = xen_release_pte_init,
  985. #endif /* PAGETABLE_LEVELS == 4 */
  986. .activate_mm = xen_activate_mm,
  987. .dup_mmap = xen_dup_mmap,
  988. .exit_mmap = xen_exit_mmap,
  989. .lazy_mode = {
  990. .enter = paravirt_enter_lazy_mmu,
  991. .leave = xen_leave_lazy,
  992. },
  993. .set_fixmap = xen_set_fixmap,
  994. };
  995. static void xen_reboot(int reason)
  996. {
  997. struct sched_shutdown r = { .reason = reason };
  998. #ifdef CONFIG_SMP
  999. smp_send_stop();
  1000. #endif
  1001. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
  1002. BUG();
  1003. }
  1004. static void xen_restart(char *msg)
  1005. {
  1006. xen_reboot(SHUTDOWN_reboot);
  1007. }
  1008. static void xen_emergency_restart(void)
  1009. {
  1010. xen_reboot(SHUTDOWN_reboot);
  1011. }
  1012. static void xen_machine_halt(void)
  1013. {
  1014. xen_reboot(SHUTDOWN_poweroff);
  1015. }
  1016. static void xen_crash_shutdown(struct pt_regs *regs)
  1017. {
  1018. xen_reboot(SHUTDOWN_crash);
  1019. }
  1020. static const struct machine_ops __initdata xen_machine_ops = {
  1021. .restart = xen_restart,
  1022. .halt = xen_machine_halt,
  1023. .power_off = xen_machine_halt,
  1024. .shutdown = xen_machine_halt,
  1025. .crash_shutdown = xen_crash_shutdown,
  1026. .emergency_restart = xen_emergency_restart,
  1027. };
  1028. static void __init xen_reserve_top(void)
  1029. {
  1030. #ifdef CONFIG_X86_32
  1031. unsigned long top = HYPERVISOR_VIRT_START;
  1032. struct xen_platform_parameters pp;
  1033. if (HYPERVISOR_xen_version(XENVER_platform_parameters, &pp) == 0)
  1034. top = pp.virt_start;
  1035. reserve_top_address(-top + 2 * PAGE_SIZE);
  1036. #endif /* CONFIG_X86_32 */
  1037. }
  1038. #ifdef CONFIG_X86_64
  1039. /*
  1040. * Like __va(), but returns address in the kernel mapping (which is
  1041. * all we have until the physical memory mapping has been set up.
  1042. */
  1043. static void *__ka(phys_addr_t paddr)
  1044. {
  1045. return (void *)(paddr + __START_KERNEL_map);
  1046. }
  1047. /* Convert a machine address to physical address */
  1048. static unsigned long m2p(phys_addr_t maddr)
  1049. {
  1050. phys_addr_t paddr;
  1051. maddr &= PTE_MASK;
  1052. paddr = mfn_to_pfn(maddr >> PAGE_SHIFT) << PAGE_SHIFT;
  1053. return paddr;
  1054. }
  1055. /* Convert a machine address to kernel virtual */
  1056. static void *m2v(phys_addr_t maddr)
  1057. {
  1058. return __ka(m2p(maddr));
  1059. }
  1060. static void walk(pgd_t *pgd, unsigned long addr)
  1061. {
  1062. unsigned l4idx = pgd_index(addr);
  1063. unsigned l3idx = pud_index(addr);
  1064. unsigned l2idx = pmd_index(addr);
  1065. unsigned l1idx = pte_index(addr);
  1066. pgd_t l4;
  1067. pud_t l3;
  1068. pmd_t l2;
  1069. pte_t l1;
  1070. xen_raw_printk("walk %p, %lx -> %d %d %d %d\n",
  1071. pgd, addr, l4idx, l3idx, l2idx, l1idx);
  1072. l4 = pgd[l4idx];
  1073. xen_raw_printk(" l4: %016lx\n", l4.pgd);
  1074. xen_raw_printk(" %016lx\n", pgd_val(l4));
  1075. l3 = ((pud_t *)(m2v(l4.pgd)))[l3idx];
  1076. xen_raw_printk(" l3: %016lx\n", l3.pud);
  1077. xen_raw_printk(" %016lx\n", pud_val(l3));
  1078. l2 = ((pmd_t *)(m2v(l3.pud)))[l2idx];
  1079. xen_raw_printk(" l2: %016lx\n", l2.pmd);
  1080. xen_raw_printk(" %016lx\n", pmd_val(l2));
  1081. l1 = ((pte_t *)(m2v(l2.pmd)))[l1idx];
  1082. xen_raw_printk(" l1: %016lx\n", l1.pte);
  1083. xen_raw_printk(" %016lx\n", pte_val(l1));
  1084. }
  1085. static void set_page_prot(void *addr, pgprot_t prot)
  1086. {
  1087. unsigned long pfn = __pa(addr) >> PAGE_SHIFT;
  1088. pte_t pte = pfn_pte(pfn, prot);
  1089. xen_raw_printk("addr=%p pfn=%lx mfn=%lx prot=%016x pte=%016x\n",
  1090. addr, pfn, get_phys_to_machine(pfn),
  1091. pgprot_val(prot), pte.pte);
  1092. if (HYPERVISOR_update_va_mapping((unsigned long)addr, pte, 0))
  1093. BUG();
  1094. }
  1095. static void convert_pfn_mfn(void *v)
  1096. {
  1097. pte_t *pte = v;
  1098. int i;
  1099. /* All levels are converted the same way, so just treat them
  1100. as ptes. */
  1101. for(i = 0; i < PTRS_PER_PTE; i++)
  1102. pte[i] = xen_make_pte(pte[i].pte);
  1103. }
  1104. /*
  1105. * Identity map, in addition to plain kernel map. This needs to be
  1106. * large enough to allocate page table pages to allocate the rest.
  1107. * Each page can map 2MB.
  1108. */
  1109. static pte_t level1_ident_pgt[PTRS_PER_PTE * 4] __page_aligned_bss;
  1110. static __init void xen_map_identity_early(unsigned long max_pfn)
  1111. {
  1112. unsigned pmdidx, pteidx;
  1113. unsigned ident_pte;
  1114. unsigned long pfn;
  1115. ident_pte = 0;
  1116. pfn = 0;
  1117. for(pmdidx = 0; pmdidx < PTRS_PER_PMD && pfn < max_pfn; pmdidx++) {
  1118. pte_t *pte_page;
  1119. BUG_ON(level2_ident_pgt[pmdidx].pmd != level2_kernel_pgt[pmdidx].pmd);
  1120. /* Reuse or allocate a page of ptes */
  1121. if (pmd_present(level2_ident_pgt[pmdidx]))
  1122. pte_page = m2v(level2_ident_pgt[pmdidx].pmd);
  1123. else {
  1124. /* Check for free pte pages */
  1125. if (ident_pte == ARRAY_SIZE(level1_ident_pgt))
  1126. break;
  1127. pte_page = &level1_ident_pgt[ident_pte];
  1128. ident_pte += PTRS_PER_PTE;
  1129. /* Install new l1 in l2(s) */
  1130. level2_ident_pgt[pmdidx] = __pmd(__pa(pte_page) | _PAGE_TABLE);
  1131. level2_kernel_pgt[pmdidx] = level2_ident_pgt[pmdidx];
  1132. }
  1133. /* Install mappings */
  1134. for(pteidx = 0; pteidx < PTRS_PER_PTE; pteidx++, pfn++) {
  1135. pte_t pte;
  1136. if (pfn > max_pfn_mapped)
  1137. max_pfn_mapped = pfn;
  1138. if (!pte_none(pte_page[pteidx]))
  1139. continue;
  1140. pte = pfn_pte(pfn, PAGE_KERNEL_EXEC);
  1141. pte_page[pteidx] = pte;
  1142. }
  1143. }
  1144. for(pteidx = 0; pteidx < ident_pte; pteidx += PTRS_PER_PTE)
  1145. set_page_prot(&level1_ident_pgt[pteidx], PAGE_KERNEL_RO);
  1146. }
  1147. /*
  1148. * Set up the inital kernel pagetable.
  1149. *
  1150. * We can construct this by grafting the Xen provided pagetable into
  1151. * head_64.S's preconstructed pagetables. We copy the Xen L2's into
  1152. * level2_ident_pgt, level2_kernel_pgt and level2_fixmap_pgt. This
  1153. * means that only the kernel has a physical mapping to start with -
  1154. * but that's enough to get __va working. We need to fill in the rest
  1155. * of the physical mapping once some sort of allocator has been set
  1156. * up.
  1157. */
  1158. static __init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd, unsigned long max_pfn)
  1159. {
  1160. pud_t *l3;
  1161. pmd_t *l2;
  1162. /* Zap identity mapping */
  1163. init_level4_pgt[0] = __pgd(0);
  1164. /* Pre-constructed entries are in pfn, so convert to mfn */
  1165. convert_pfn_mfn(init_level4_pgt);
  1166. convert_pfn_mfn(level3_ident_pgt);
  1167. convert_pfn_mfn(level3_kernel_pgt);
  1168. l3 = m2v(pgd[pgd_index(__START_KERNEL_map)].pgd);
  1169. l2 = m2v(l3[pud_index(__START_KERNEL_map)].pud);
  1170. memcpy(level2_ident_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1171. memcpy(level2_kernel_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1172. l3 = m2v(pgd[pgd_index(__START_KERNEL_map + PMD_SIZE)].pgd);
  1173. l2 = m2v(l3[pud_index(__START_KERNEL_map + PMD_SIZE)].pud);
  1174. memcpy(level2_fixmap_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1175. /* Set up identity map */
  1176. xen_map_identity_early(max_pfn);
  1177. /* Make pagetable pieces RO */
  1178. set_page_prot(init_level4_pgt, PAGE_KERNEL_RO);
  1179. set_page_prot(level3_ident_pgt, PAGE_KERNEL_RO);
  1180. set_page_prot(level3_kernel_pgt, PAGE_KERNEL_RO);
  1181. set_page_prot(level2_ident_pgt, PAGE_KERNEL_RO);
  1182. set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO);
  1183. set_page_prot(level2_fixmap_pgt, PAGE_KERNEL_RO);
  1184. /* Pin down new L4 */
  1185. pin_pagetable_pfn(MMUEXT_PIN_L4_TABLE, PFN_DOWN(__pa_symbol(init_level4_pgt)));
  1186. /* Unpin Xen-provided one */
  1187. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
  1188. /* Switch over */
  1189. pgd = init_level4_pgt;
  1190. xen_write_cr3(__pa(pgd));
  1191. reserve_early(__pa(xen_start_info->pt_base),
  1192. __pa(xen_start_info->pt_base +
  1193. xen_start_info->nr_pt_frames * PAGE_SIZE),
  1194. "XEN PAGETABLES");
  1195. return pgd;
  1196. }
  1197. #else
  1198. static __init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd, unsigned long max_pfn)
  1199. {
  1200. init_pg_tables_start = __pa(pgd);
  1201. init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE;
  1202. max_pfn_mapped = PFN_DOWN(init_pg_tables_end + 512*1024);
  1203. x86_write_percpu(xen_cr3, __pa(pgd));
  1204. x86_write_percpu(xen_current_cr3, __pa(pgd));
  1205. return pgd;
  1206. }
  1207. #endif /* CONFIG_X86_64 */
  1208. /* First C function to be called on Xen boot */
  1209. asmlinkage void __init xen_start_kernel(void)
  1210. {
  1211. pgd_t *pgd;
  1212. if (!xen_start_info)
  1213. return;
  1214. BUG_ON(memcmp(xen_start_info->magic, "xen-3", 5) != 0);
  1215. xen_setup_features();
  1216. /* Install Xen paravirt ops */
  1217. pv_info = xen_info;
  1218. pv_init_ops = xen_init_ops;
  1219. pv_time_ops = xen_time_ops;
  1220. pv_cpu_ops = xen_cpu_ops;
  1221. pv_irq_ops = xen_irq_ops;
  1222. pv_apic_ops = xen_apic_ops;
  1223. pv_mmu_ops = xen_mmu_ops;
  1224. if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
  1225. pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
  1226. pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
  1227. }
  1228. machine_ops = xen_machine_ops;
  1229. #ifdef CONFIG_X86_64
  1230. /* Disable until direct per-cpu data access. */
  1231. have_vcpu_info_placement = 0;
  1232. x86_64_init_pda();
  1233. #endif
  1234. xen_smp_init();
  1235. /* Get mfn list */
  1236. if (!xen_feature(XENFEAT_auto_translated_physmap))
  1237. xen_build_dynamic_phys_to_machine();
  1238. pgd = (pgd_t *)xen_start_info->pt_base;
  1239. /* Prevent unwanted bits from being set in PTEs. */
  1240. __supported_pte_mask &= ~_PAGE_GLOBAL;
  1241. if (!is_initial_xendomain())
  1242. __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
  1243. /* Don't do the full vcpu_info placement stuff until we have a
  1244. possible map and a non-dummy shared_info. */
  1245. per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
  1246. xen_raw_console_write("mapping kernel into physical memory\n");
  1247. pgd = xen_setup_kernel_pagetable(pgd, xen_start_info->nr_pages);
  1248. init_mm.pgd = pgd;
  1249. /* keep using Xen gdt for now; no urgent need to change it */
  1250. pv_info.kernel_rpl = 1;
  1251. if (xen_feature(XENFEAT_supervisor_mode_kernel))
  1252. pv_info.kernel_rpl = 0;
  1253. /* set the limit of our address space */
  1254. xen_reserve_top();
  1255. #ifdef CONFIG_X86_32
  1256. /* set up basic CPUID stuff */
  1257. cpu_detect(&new_cpu_data);
  1258. new_cpu_data.hard_math = 1;
  1259. new_cpu_data.x86_capability[0] = cpuid_edx(1);
  1260. #endif
  1261. /* Poke various useful things into boot_params */
  1262. boot_params.hdr.type_of_loader = (9 << 4) | 0;
  1263. boot_params.hdr.ramdisk_image = xen_start_info->mod_start
  1264. ? __pa(xen_start_info->mod_start) : 0;
  1265. boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
  1266. if (!is_initial_xendomain()) {
  1267. add_preferred_console("xenboot", 0, NULL);
  1268. add_preferred_console("tty", 0, NULL);
  1269. add_preferred_console("hvc", 0, NULL);
  1270. }
  1271. xen_raw_console_write("about to get started...\n");
  1272. #if 0
  1273. xen_raw_printk("&boot_params=%p __pa(&boot_params)=%lx __va(__pa(&boot_params))=%lx\n",
  1274. &boot_params, __pa_symbol(&boot_params),
  1275. __va(__pa_symbol(&boot_params)));
  1276. walk(pgd, &boot_params);
  1277. walk(pgd, __va(__pa(&boot_params)));
  1278. #endif
  1279. /* Start the world */
  1280. #ifdef CONFIG_X86_32
  1281. i386_start_kernel();
  1282. #else
  1283. x86_64_start_reservations((char *)__pa_symbol(&boot_params));
  1284. #endif
  1285. }