enlighten.c 40 KB

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