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