enlighten.c 40 KB

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