mmu.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020
  1. /*
  2. * Xen mmu operations
  3. *
  4. * This file contains the various mmu fetch and update operations.
  5. * The most important job they must perform is the mapping between the
  6. * domain's pfn and the overall machine mfns.
  7. *
  8. * Xen allows guests to directly update the pagetable, in a controlled
  9. * fashion. In other words, the guest modifies the same pagetable
  10. * that the CPU actually uses, which eliminates the overhead of having
  11. * a separate shadow pagetable.
  12. *
  13. * In order to allow this, it falls on the guest domain to map its
  14. * notion of a "physical" pfn - which is just a domain-local linear
  15. * address - into a real "machine address" which the CPU's MMU can
  16. * use.
  17. *
  18. * A pgd_t/pmd_t/pte_t will typically contain an mfn, and so can be
  19. * inserted directly into the pagetable. When creating a new
  20. * pte/pmd/pgd, it converts the passed pfn into an mfn. Conversely,
  21. * when reading the content back with __(pgd|pmd|pte)_val, it converts
  22. * the mfn back into a pfn.
  23. *
  24. * The other constraint is that all pages which make up a pagetable
  25. * must be mapped read-only in the guest. This prevents uncontrolled
  26. * guest updates to the pagetable. Xen strictly enforces this, and
  27. * will disallow any pagetable update which will end up mapping a
  28. * pagetable page RW, and will disallow using any writable page as a
  29. * pagetable.
  30. *
  31. * Naively, when loading %cr3 with the base of a new pagetable, Xen
  32. * would need to validate the whole pagetable before going on.
  33. * Naturally, this is quite slow. The solution is to "pin" a
  34. * pagetable, which enforces all the constraints on the pagetable even
  35. * when it is not actively in use. This menas that Xen can be assured
  36. * that it is still valid when you do load it into %cr3, and doesn't
  37. * need to revalidate it.
  38. *
  39. * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
  40. */
  41. #include <linux/sched.h>
  42. #include <linux/highmem.h>
  43. #include <linux/debugfs.h>
  44. #include <linux/bug.h>
  45. #include <linux/module.h>
  46. #include <asm/pgtable.h>
  47. #include <asm/tlbflush.h>
  48. #include <asm/fixmap.h>
  49. #include <asm/mmu_context.h>
  50. #include <asm/setup.h>
  51. #include <asm/paravirt.h>
  52. #include <asm/linkage.h>
  53. #include <asm/xen/hypercall.h>
  54. #include <asm/xen/hypervisor.h>
  55. #include <xen/page.h>
  56. #include <xen/interface/xen.h>
  57. #include <xen/interface/version.h>
  58. #include <xen/hvc-console.h>
  59. #include "multicalls.h"
  60. #include "mmu.h"
  61. #include "debugfs.h"
  62. #define MMU_UPDATE_HISTO 30
  63. #ifdef CONFIG_XEN_DEBUG_FS
  64. static struct {
  65. u32 pgd_update;
  66. u32 pgd_update_pinned;
  67. u32 pgd_update_batched;
  68. u32 pud_update;
  69. u32 pud_update_pinned;
  70. u32 pud_update_batched;
  71. u32 pmd_update;
  72. u32 pmd_update_pinned;
  73. u32 pmd_update_batched;
  74. u32 pte_update;
  75. u32 pte_update_pinned;
  76. u32 pte_update_batched;
  77. u32 mmu_update;
  78. u32 mmu_update_extended;
  79. u32 mmu_update_histo[MMU_UPDATE_HISTO];
  80. u32 prot_commit;
  81. u32 prot_commit_batched;
  82. u32 set_pte_at;
  83. u32 set_pte_at_batched;
  84. u32 set_pte_at_pinned;
  85. u32 set_pte_at_current;
  86. u32 set_pte_at_kernel;
  87. } mmu_stats;
  88. static u8 zero_stats;
  89. static inline void check_zero(void)
  90. {
  91. if (unlikely(zero_stats)) {
  92. memset(&mmu_stats, 0, sizeof(mmu_stats));
  93. zero_stats = 0;
  94. }
  95. }
  96. #define ADD_STATS(elem, val) \
  97. do { check_zero(); mmu_stats.elem += (val); } while(0)
  98. #else /* !CONFIG_XEN_DEBUG_FS */
  99. #define ADD_STATS(elem, val) do { (void)(val); } while(0)
  100. #endif /* CONFIG_XEN_DEBUG_FS */
  101. /*
  102. * Identity map, in addition to plain kernel map. This needs to be
  103. * large enough to allocate page table pages to allocate the rest.
  104. * Each page can map 2MB.
  105. */
  106. static pte_t level1_ident_pgt[PTRS_PER_PTE * 4] __page_aligned_bss;
  107. #ifdef CONFIG_X86_64
  108. /* l3 pud for userspace vsyscall mapping */
  109. static pud_t level3_user_vsyscall[PTRS_PER_PUD] __page_aligned_bss;
  110. #endif /* CONFIG_X86_64 */
  111. /*
  112. * Note about cr3 (pagetable base) values:
  113. *
  114. * xen_cr3 contains the current logical cr3 value; it contains the
  115. * last set cr3. This may not be the current effective cr3, because
  116. * its update may be being lazily deferred. However, a vcpu looking
  117. * at its own cr3 can use this value knowing that it everything will
  118. * be self-consistent.
  119. *
  120. * xen_current_cr3 contains the actual vcpu cr3; it is set once the
  121. * hypercall to set the vcpu cr3 is complete (so it may be a little
  122. * out of date, but it will never be set early). If one vcpu is
  123. * looking at another vcpu's cr3 value, it should use this variable.
  124. */
  125. DEFINE_PER_CPU(unsigned long, xen_cr3); /* cr3 stored as physaddr */
  126. DEFINE_PER_CPU(unsigned long, xen_current_cr3); /* actual vcpu cr3 */
  127. /*
  128. * Just beyond the highest usermode address. STACK_TOP_MAX has a
  129. * redzone above it, so round it up to a PGD boundary.
  130. */
  131. #define USER_LIMIT ((STACK_TOP_MAX + PGDIR_SIZE - 1) & PGDIR_MASK)
  132. #define P2M_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(unsigned long))
  133. #define TOP_ENTRIES (MAX_DOMAIN_PAGES / P2M_ENTRIES_PER_PAGE)
  134. /* Placeholder for holes in the address space */
  135. static unsigned long p2m_missing[P2M_ENTRIES_PER_PAGE] __page_aligned_data =
  136. { [ 0 ... P2M_ENTRIES_PER_PAGE-1 ] = ~0UL };
  137. /* Array of pointers to pages containing p2m entries */
  138. static unsigned long *p2m_top[TOP_ENTRIES] __page_aligned_data =
  139. { [ 0 ... TOP_ENTRIES - 1] = &p2m_missing[0] };
  140. /* Arrays of p2m arrays expressed in mfns used for save/restore */
  141. static unsigned long p2m_top_mfn[TOP_ENTRIES] __page_aligned_bss;
  142. static unsigned long p2m_top_mfn_list[TOP_ENTRIES / P2M_ENTRIES_PER_PAGE]
  143. __page_aligned_bss;
  144. static inline unsigned p2m_top_index(unsigned long pfn)
  145. {
  146. BUG_ON(pfn >= MAX_DOMAIN_PAGES);
  147. return pfn / P2M_ENTRIES_PER_PAGE;
  148. }
  149. static inline unsigned p2m_index(unsigned long pfn)
  150. {
  151. return pfn % P2M_ENTRIES_PER_PAGE;
  152. }
  153. /* Build the parallel p2m_top_mfn structures */
  154. static void __init xen_build_mfn_list_list(void)
  155. {
  156. unsigned pfn, idx;
  157. for (pfn = 0; pfn < MAX_DOMAIN_PAGES; pfn += P2M_ENTRIES_PER_PAGE) {
  158. unsigned topidx = p2m_top_index(pfn);
  159. p2m_top_mfn[topidx] = virt_to_mfn(p2m_top[topidx]);
  160. }
  161. for (idx = 0; idx < ARRAY_SIZE(p2m_top_mfn_list); idx++) {
  162. unsigned topidx = idx * P2M_ENTRIES_PER_PAGE;
  163. p2m_top_mfn_list[idx] = virt_to_mfn(&p2m_top_mfn[topidx]);
  164. }
  165. }
  166. void xen_setup_mfn_list_list(void)
  167. {
  168. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  169. HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list =
  170. virt_to_mfn(p2m_top_mfn_list);
  171. HYPERVISOR_shared_info->arch.max_pfn = xen_start_info->nr_pages;
  172. }
  173. /* Set up p2m_top to point to the domain-builder provided p2m pages */
  174. void __init xen_build_dynamic_phys_to_machine(void)
  175. {
  176. unsigned long *mfn_list = (unsigned long *)xen_start_info->mfn_list;
  177. unsigned long max_pfn = min(MAX_DOMAIN_PAGES, xen_start_info->nr_pages);
  178. unsigned pfn;
  179. for (pfn = 0; pfn < max_pfn; pfn += P2M_ENTRIES_PER_PAGE) {
  180. unsigned topidx = p2m_top_index(pfn);
  181. p2m_top[topidx] = &mfn_list[pfn];
  182. }
  183. xen_build_mfn_list_list();
  184. }
  185. unsigned long get_phys_to_machine(unsigned long pfn)
  186. {
  187. unsigned topidx, idx;
  188. if (unlikely(pfn >= MAX_DOMAIN_PAGES))
  189. return INVALID_P2M_ENTRY;
  190. topidx = p2m_top_index(pfn);
  191. idx = p2m_index(pfn);
  192. return p2m_top[topidx][idx];
  193. }
  194. EXPORT_SYMBOL_GPL(get_phys_to_machine);
  195. /* install a new p2m_top page */
  196. bool install_p2mtop_page(unsigned long pfn, unsigned long *p)
  197. {
  198. unsigned topidx = p2m_top_index(pfn);
  199. unsigned long **pfnp, *mfnp;
  200. unsigned i;
  201. pfnp = &p2m_top[topidx];
  202. mfnp = &p2m_top_mfn[topidx];
  203. for (i = 0; i < P2M_ENTRIES_PER_PAGE; i++)
  204. p[i] = INVALID_P2M_ENTRY;
  205. if (cmpxchg(pfnp, p2m_missing, p) == p2m_missing) {
  206. *mfnp = virt_to_mfn(p);
  207. return true;
  208. }
  209. return false;
  210. }
  211. static void alloc_p2m(unsigned long pfn)
  212. {
  213. unsigned long *p;
  214. p = (void *)__get_free_page(GFP_KERNEL | __GFP_NOFAIL);
  215. BUG_ON(p == NULL);
  216. if (!install_p2mtop_page(pfn, p))
  217. free_page((unsigned long)p);
  218. }
  219. /* Try to install p2m mapping; fail if intermediate bits missing */
  220. bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn)
  221. {
  222. unsigned topidx, idx;
  223. if (unlikely(pfn >= MAX_DOMAIN_PAGES)) {
  224. BUG_ON(mfn != INVALID_P2M_ENTRY);
  225. return true;
  226. }
  227. topidx = p2m_top_index(pfn);
  228. if (p2m_top[topidx] == p2m_missing) {
  229. if (mfn == INVALID_P2M_ENTRY)
  230. return true;
  231. return false;
  232. }
  233. idx = p2m_index(pfn);
  234. p2m_top[topidx][idx] = mfn;
  235. return true;
  236. }
  237. void set_phys_to_machine(unsigned long pfn, unsigned long mfn)
  238. {
  239. if (unlikely(xen_feature(XENFEAT_auto_translated_physmap))) {
  240. BUG_ON(pfn != mfn && mfn != INVALID_P2M_ENTRY);
  241. return;
  242. }
  243. if (unlikely(!__set_phys_to_machine(pfn, mfn))) {
  244. alloc_p2m(pfn);
  245. if (!__set_phys_to_machine(pfn, mfn))
  246. BUG();
  247. }
  248. }
  249. unsigned long arbitrary_virt_to_mfn(void *vaddr)
  250. {
  251. xmaddr_t maddr = arbitrary_virt_to_machine(vaddr);
  252. return PFN_DOWN(maddr.maddr);
  253. }
  254. xmaddr_t arbitrary_virt_to_machine(void *vaddr)
  255. {
  256. unsigned long address = (unsigned long)vaddr;
  257. unsigned int level;
  258. pte_t *pte;
  259. unsigned offset;
  260. /*
  261. * if the PFN is in the linear mapped vaddr range, we can just use
  262. * the (quick) virt_to_machine() p2m lookup
  263. */
  264. if (virt_addr_valid(vaddr))
  265. return virt_to_machine(vaddr);
  266. /* otherwise we have to do a (slower) full page-table walk */
  267. pte = lookup_address(address, &level);
  268. BUG_ON(pte == NULL);
  269. offset = address & ~PAGE_MASK;
  270. return XMADDR(((phys_addr_t)pte_mfn(*pte) << PAGE_SHIFT) + offset);
  271. }
  272. void make_lowmem_page_readonly(void *vaddr)
  273. {
  274. pte_t *pte, ptev;
  275. unsigned long address = (unsigned long)vaddr;
  276. unsigned int level;
  277. pte = lookup_address(address, &level);
  278. BUG_ON(pte == NULL);
  279. ptev = pte_wrprotect(*pte);
  280. if (HYPERVISOR_update_va_mapping(address, ptev, 0))
  281. BUG();
  282. }
  283. void make_lowmem_page_readwrite(void *vaddr)
  284. {
  285. pte_t *pte, ptev;
  286. unsigned long address = (unsigned long)vaddr;
  287. unsigned int level;
  288. pte = lookup_address(address, &level);
  289. BUG_ON(pte == NULL);
  290. ptev = pte_mkwrite(*pte);
  291. if (HYPERVISOR_update_va_mapping(address, ptev, 0))
  292. BUG();
  293. }
  294. static bool xen_page_pinned(void *ptr)
  295. {
  296. struct page *page = virt_to_page(ptr);
  297. return PagePinned(page);
  298. }
  299. static void xen_extend_mmu_update(const struct mmu_update *update)
  300. {
  301. struct multicall_space mcs;
  302. struct mmu_update *u;
  303. mcs = xen_mc_extend_args(__HYPERVISOR_mmu_update, sizeof(*u));
  304. if (mcs.mc != NULL) {
  305. ADD_STATS(mmu_update_extended, 1);
  306. ADD_STATS(mmu_update_histo[mcs.mc->args[1]], -1);
  307. mcs.mc->args[1]++;
  308. if (mcs.mc->args[1] < MMU_UPDATE_HISTO)
  309. ADD_STATS(mmu_update_histo[mcs.mc->args[1]], 1);
  310. else
  311. ADD_STATS(mmu_update_histo[0], 1);
  312. } else {
  313. ADD_STATS(mmu_update, 1);
  314. mcs = __xen_mc_entry(sizeof(*u));
  315. MULTI_mmu_update(mcs.mc, mcs.args, 1, NULL, DOMID_SELF);
  316. ADD_STATS(mmu_update_histo[1], 1);
  317. }
  318. u = mcs.args;
  319. *u = *update;
  320. }
  321. void xen_set_pmd_hyper(pmd_t *ptr, pmd_t val)
  322. {
  323. struct mmu_update u;
  324. preempt_disable();
  325. xen_mc_batch();
  326. /* ptr may be ioremapped for 64-bit pagetable setup */
  327. u.ptr = arbitrary_virt_to_machine(ptr).maddr;
  328. u.val = pmd_val_ma(val);
  329. xen_extend_mmu_update(&u);
  330. ADD_STATS(pmd_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
  331. xen_mc_issue(PARAVIRT_LAZY_MMU);
  332. preempt_enable();
  333. }
  334. void xen_set_pmd(pmd_t *ptr, pmd_t val)
  335. {
  336. ADD_STATS(pmd_update, 1);
  337. /* If page is not pinned, we can just update the entry
  338. directly */
  339. if (!xen_page_pinned(ptr)) {
  340. *ptr = val;
  341. return;
  342. }
  343. ADD_STATS(pmd_update_pinned, 1);
  344. xen_set_pmd_hyper(ptr, val);
  345. }
  346. /*
  347. * Associate a virtual page frame with a given physical page frame
  348. * and protection flags for that frame.
  349. */
  350. void set_pte_mfn(unsigned long vaddr, unsigned long mfn, pgprot_t flags)
  351. {
  352. set_pte_vaddr(vaddr, mfn_pte(mfn, flags));
  353. }
  354. void xen_set_pte_at(struct mm_struct *mm, unsigned long addr,
  355. pte_t *ptep, pte_t pteval)
  356. {
  357. /* updates to init_mm may be done without lock */
  358. if (mm == &init_mm)
  359. preempt_disable();
  360. ADD_STATS(set_pte_at, 1);
  361. // ADD_STATS(set_pte_at_pinned, xen_page_pinned(ptep));
  362. ADD_STATS(set_pte_at_current, mm == current->mm);
  363. ADD_STATS(set_pte_at_kernel, mm == &init_mm);
  364. if (mm == current->mm || mm == &init_mm) {
  365. if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU) {
  366. struct multicall_space mcs;
  367. mcs = xen_mc_entry(0);
  368. MULTI_update_va_mapping(mcs.mc, addr, pteval, 0);
  369. ADD_STATS(set_pte_at_batched, 1);
  370. xen_mc_issue(PARAVIRT_LAZY_MMU);
  371. goto out;
  372. } else
  373. if (HYPERVISOR_update_va_mapping(addr, pteval, 0) == 0)
  374. goto out;
  375. }
  376. xen_set_pte(ptep, pteval);
  377. out:
  378. if (mm == &init_mm)
  379. preempt_enable();
  380. }
  381. pte_t xen_ptep_modify_prot_start(struct mm_struct *mm,
  382. unsigned long addr, pte_t *ptep)
  383. {
  384. /* Just return the pte as-is. We preserve the bits on commit */
  385. return *ptep;
  386. }
  387. void xen_ptep_modify_prot_commit(struct mm_struct *mm, unsigned long addr,
  388. pte_t *ptep, pte_t pte)
  389. {
  390. struct mmu_update u;
  391. xen_mc_batch();
  392. u.ptr = arbitrary_virt_to_machine(ptep).maddr | MMU_PT_UPDATE_PRESERVE_AD;
  393. u.val = pte_val_ma(pte);
  394. xen_extend_mmu_update(&u);
  395. ADD_STATS(prot_commit, 1);
  396. ADD_STATS(prot_commit_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
  397. xen_mc_issue(PARAVIRT_LAZY_MMU);
  398. }
  399. /* Assume pteval_t is equivalent to all the other *val_t types. */
  400. static pteval_t pte_mfn_to_pfn(pteval_t val)
  401. {
  402. if (val & _PAGE_PRESENT) {
  403. unsigned long mfn = (val & PTE_PFN_MASK) >> PAGE_SHIFT;
  404. pteval_t flags = val & PTE_FLAGS_MASK;
  405. val = ((pteval_t)mfn_to_pfn(mfn) << PAGE_SHIFT) | flags;
  406. }
  407. return val;
  408. }
  409. static pteval_t pte_pfn_to_mfn(pteval_t val)
  410. {
  411. if (val & _PAGE_PRESENT) {
  412. unsigned long pfn = (val & PTE_PFN_MASK) >> PAGE_SHIFT;
  413. pteval_t flags = val & PTE_FLAGS_MASK;
  414. val = ((pteval_t)pfn_to_mfn(pfn) << PAGE_SHIFT) | flags;
  415. }
  416. return val;
  417. }
  418. pteval_t xen_pte_val(pte_t pte)
  419. {
  420. return pte_mfn_to_pfn(pte.pte);
  421. }
  422. PV_CALLEE_SAVE_REGS_THUNK(xen_pte_val);
  423. pgdval_t xen_pgd_val(pgd_t pgd)
  424. {
  425. return pte_mfn_to_pfn(pgd.pgd);
  426. }
  427. PV_CALLEE_SAVE_REGS_THUNK(xen_pgd_val);
  428. pte_t xen_make_pte(pteval_t pte)
  429. {
  430. pte = pte_pfn_to_mfn(pte);
  431. return native_make_pte(pte);
  432. }
  433. PV_CALLEE_SAVE_REGS_THUNK(xen_make_pte);
  434. pgd_t xen_make_pgd(pgdval_t pgd)
  435. {
  436. pgd = pte_pfn_to_mfn(pgd);
  437. return native_make_pgd(pgd);
  438. }
  439. PV_CALLEE_SAVE_REGS_THUNK(xen_make_pgd);
  440. pmdval_t xen_pmd_val(pmd_t pmd)
  441. {
  442. return pte_mfn_to_pfn(pmd.pmd);
  443. }
  444. PV_CALLEE_SAVE_REGS_THUNK(xen_pmd_val);
  445. void xen_set_pud_hyper(pud_t *ptr, pud_t val)
  446. {
  447. struct mmu_update u;
  448. preempt_disable();
  449. xen_mc_batch();
  450. /* ptr may be ioremapped for 64-bit pagetable setup */
  451. u.ptr = arbitrary_virt_to_machine(ptr).maddr;
  452. u.val = pud_val_ma(val);
  453. xen_extend_mmu_update(&u);
  454. ADD_STATS(pud_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
  455. xen_mc_issue(PARAVIRT_LAZY_MMU);
  456. preempt_enable();
  457. }
  458. void xen_set_pud(pud_t *ptr, pud_t val)
  459. {
  460. ADD_STATS(pud_update, 1);
  461. /* If page is not pinned, we can just update the entry
  462. directly */
  463. if (!xen_page_pinned(ptr)) {
  464. *ptr = val;
  465. return;
  466. }
  467. ADD_STATS(pud_update_pinned, 1);
  468. xen_set_pud_hyper(ptr, val);
  469. }
  470. void xen_set_pte(pte_t *ptep, pte_t pte)
  471. {
  472. ADD_STATS(pte_update, 1);
  473. // ADD_STATS(pte_update_pinned, xen_page_pinned(ptep));
  474. ADD_STATS(pte_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
  475. #ifdef CONFIG_X86_PAE
  476. ptep->pte_high = pte.pte_high;
  477. smp_wmb();
  478. ptep->pte_low = pte.pte_low;
  479. #else
  480. *ptep = pte;
  481. #endif
  482. }
  483. #ifdef CONFIG_X86_PAE
  484. void xen_set_pte_atomic(pte_t *ptep, pte_t pte)
  485. {
  486. set_64bit((u64 *)ptep, native_pte_val(pte));
  487. }
  488. void xen_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  489. {
  490. ptep->pte_low = 0;
  491. smp_wmb(); /* make sure low gets written first */
  492. ptep->pte_high = 0;
  493. }
  494. void xen_pmd_clear(pmd_t *pmdp)
  495. {
  496. set_pmd(pmdp, __pmd(0));
  497. }
  498. #endif /* CONFIG_X86_PAE */
  499. pmd_t xen_make_pmd(pmdval_t pmd)
  500. {
  501. pmd = pte_pfn_to_mfn(pmd);
  502. return native_make_pmd(pmd);
  503. }
  504. PV_CALLEE_SAVE_REGS_THUNK(xen_make_pmd);
  505. #if PAGETABLE_LEVELS == 4
  506. pudval_t xen_pud_val(pud_t pud)
  507. {
  508. return pte_mfn_to_pfn(pud.pud);
  509. }
  510. PV_CALLEE_SAVE_REGS_THUNK(xen_pud_val);
  511. pud_t xen_make_pud(pudval_t pud)
  512. {
  513. pud = pte_pfn_to_mfn(pud);
  514. return native_make_pud(pud);
  515. }
  516. PV_CALLEE_SAVE_REGS_THUNK(xen_make_pud);
  517. pgd_t *xen_get_user_pgd(pgd_t *pgd)
  518. {
  519. pgd_t *pgd_page = (pgd_t *)(((unsigned long)pgd) & PAGE_MASK);
  520. unsigned offset = pgd - pgd_page;
  521. pgd_t *user_ptr = NULL;
  522. if (offset < pgd_index(USER_LIMIT)) {
  523. struct page *page = virt_to_page(pgd_page);
  524. user_ptr = (pgd_t *)page->private;
  525. if (user_ptr)
  526. user_ptr += offset;
  527. }
  528. return user_ptr;
  529. }
  530. static void __xen_set_pgd_hyper(pgd_t *ptr, pgd_t val)
  531. {
  532. struct mmu_update u;
  533. u.ptr = virt_to_machine(ptr).maddr;
  534. u.val = pgd_val_ma(val);
  535. xen_extend_mmu_update(&u);
  536. }
  537. /*
  538. * Raw hypercall-based set_pgd, intended for in early boot before
  539. * there's a page structure. This implies:
  540. * 1. The only existing pagetable is the kernel's
  541. * 2. It is always pinned
  542. * 3. It has no user pagetable attached to it
  543. */
  544. void __init xen_set_pgd_hyper(pgd_t *ptr, pgd_t val)
  545. {
  546. preempt_disable();
  547. xen_mc_batch();
  548. __xen_set_pgd_hyper(ptr, val);
  549. xen_mc_issue(PARAVIRT_LAZY_MMU);
  550. preempt_enable();
  551. }
  552. void xen_set_pgd(pgd_t *ptr, pgd_t val)
  553. {
  554. pgd_t *user_ptr = xen_get_user_pgd(ptr);
  555. ADD_STATS(pgd_update, 1);
  556. /* If page is not pinned, we can just update the entry
  557. directly */
  558. if (!xen_page_pinned(ptr)) {
  559. *ptr = val;
  560. if (user_ptr) {
  561. WARN_ON(xen_page_pinned(user_ptr));
  562. *user_ptr = val;
  563. }
  564. return;
  565. }
  566. ADD_STATS(pgd_update_pinned, 1);
  567. ADD_STATS(pgd_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
  568. /* If it's pinned, then we can at least batch the kernel and
  569. user updates together. */
  570. xen_mc_batch();
  571. __xen_set_pgd_hyper(ptr, val);
  572. if (user_ptr)
  573. __xen_set_pgd_hyper(user_ptr, val);
  574. xen_mc_issue(PARAVIRT_LAZY_MMU);
  575. }
  576. #endif /* PAGETABLE_LEVELS == 4 */
  577. /*
  578. * (Yet another) pagetable walker. This one is intended for pinning a
  579. * pagetable. This means that it walks a pagetable and calls the
  580. * callback function on each page it finds making up the page table,
  581. * at every level. It walks the entire pagetable, but it only bothers
  582. * pinning pte pages which are below limit. In the normal case this
  583. * will be STACK_TOP_MAX, but at boot we need to pin up to
  584. * FIXADDR_TOP.
  585. *
  586. * For 32-bit the important bit is that we don't pin beyond there,
  587. * because then we start getting into Xen's ptes.
  588. *
  589. * For 64-bit, we must skip the Xen hole in the middle of the address
  590. * space, just after the big x86-64 virtual hole.
  591. */
  592. static int __xen_pgd_walk(struct mm_struct *mm, pgd_t *pgd,
  593. int (*func)(struct mm_struct *mm, struct page *,
  594. enum pt_level),
  595. unsigned long limit)
  596. {
  597. int flush = 0;
  598. unsigned hole_low, hole_high;
  599. unsigned pgdidx_limit, pudidx_limit, pmdidx_limit;
  600. unsigned pgdidx, pudidx, pmdidx;
  601. /* The limit is the last byte to be touched */
  602. limit--;
  603. BUG_ON(limit >= FIXADDR_TOP);
  604. if (xen_feature(XENFEAT_auto_translated_physmap))
  605. return 0;
  606. /*
  607. * 64-bit has a great big hole in the middle of the address
  608. * space, which contains the Xen mappings. On 32-bit these
  609. * will end up making a zero-sized hole and so is a no-op.
  610. */
  611. hole_low = pgd_index(USER_LIMIT);
  612. hole_high = pgd_index(PAGE_OFFSET);
  613. pgdidx_limit = pgd_index(limit);
  614. #if PTRS_PER_PUD > 1
  615. pudidx_limit = pud_index(limit);
  616. #else
  617. pudidx_limit = 0;
  618. #endif
  619. #if PTRS_PER_PMD > 1
  620. pmdidx_limit = pmd_index(limit);
  621. #else
  622. pmdidx_limit = 0;
  623. #endif
  624. for (pgdidx = 0; pgdidx <= pgdidx_limit; pgdidx++) {
  625. pud_t *pud;
  626. if (pgdidx >= hole_low && pgdidx < hole_high)
  627. continue;
  628. if (!pgd_val(pgd[pgdidx]))
  629. continue;
  630. pud = pud_offset(&pgd[pgdidx], 0);
  631. if (PTRS_PER_PUD > 1) /* not folded */
  632. flush |= (*func)(mm, virt_to_page(pud), PT_PUD);
  633. for (pudidx = 0; pudidx < PTRS_PER_PUD; pudidx++) {
  634. pmd_t *pmd;
  635. if (pgdidx == pgdidx_limit &&
  636. pudidx > pudidx_limit)
  637. goto out;
  638. if (pud_none(pud[pudidx]))
  639. continue;
  640. pmd = pmd_offset(&pud[pudidx], 0);
  641. if (PTRS_PER_PMD > 1) /* not folded */
  642. flush |= (*func)(mm, virt_to_page(pmd), PT_PMD);
  643. for (pmdidx = 0; pmdidx < PTRS_PER_PMD; pmdidx++) {
  644. struct page *pte;
  645. if (pgdidx == pgdidx_limit &&
  646. pudidx == pudidx_limit &&
  647. pmdidx > pmdidx_limit)
  648. goto out;
  649. if (pmd_none(pmd[pmdidx]))
  650. continue;
  651. pte = pmd_page(pmd[pmdidx]);
  652. flush |= (*func)(mm, pte, PT_PTE);
  653. }
  654. }
  655. }
  656. out:
  657. /* Do the top level last, so that the callbacks can use it as
  658. a cue to do final things like tlb flushes. */
  659. flush |= (*func)(mm, virt_to_page(pgd), PT_PGD);
  660. return flush;
  661. }
  662. static int xen_pgd_walk(struct mm_struct *mm,
  663. int (*func)(struct mm_struct *mm, struct page *,
  664. enum pt_level),
  665. unsigned long limit)
  666. {
  667. return __xen_pgd_walk(mm, mm->pgd, func, limit);
  668. }
  669. /* If we're using split pte locks, then take the page's lock and
  670. return a pointer to it. Otherwise return NULL. */
  671. static spinlock_t *xen_pte_lock(struct page *page, struct mm_struct *mm)
  672. {
  673. spinlock_t *ptl = NULL;
  674. #if USE_SPLIT_PTLOCKS
  675. ptl = __pte_lockptr(page);
  676. spin_lock_nest_lock(ptl, &mm->page_table_lock);
  677. #endif
  678. return ptl;
  679. }
  680. static void xen_pte_unlock(void *v)
  681. {
  682. spinlock_t *ptl = v;
  683. spin_unlock(ptl);
  684. }
  685. static void xen_do_pin(unsigned level, unsigned long pfn)
  686. {
  687. struct mmuext_op *op;
  688. struct multicall_space mcs;
  689. mcs = __xen_mc_entry(sizeof(*op));
  690. op = mcs.args;
  691. op->cmd = level;
  692. op->arg1.mfn = pfn_to_mfn(pfn);
  693. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  694. }
  695. static int xen_pin_page(struct mm_struct *mm, struct page *page,
  696. enum pt_level level)
  697. {
  698. unsigned pgfl = TestSetPagePinned(page);
  699. int flush;
  700. if (pgfl)
  701. flush = 0; /* already pinned */
  702. else if (PageHighMem(page))
  703. /* kmaps need flushing if we found an unpinned
  704. highpage */
  705. flush = 1;
  706. else {
  707. void *pt = lowmem_page_address(page);
  708. unsigned long pfn = page_to_pfn(page);
  709. struct multicall_space mcs = __xen_mc_entry(0);
  710. spinlock_t *ptl;
  711. flush = 0;
  712. /*
  713. * We need to hold the pagetable lock between the time
  714. * we make the pagetable RO and when we actually pin
  715. * it. If we don't, then other users may come in and
  716. * attempt to update the pagetable by writing it,
  717. * which will fail because the memory is RO but not
  718. * pinned, so Xen won't do the trap'n'emulate.
  719. *
  720. * If we're using split pte locks, we can't hold the
  721. * entire pagetable's worth of locks during the
  722. * traverse, because we may wrap the preempt count (8
  723. * bits). The solution is to mark RO and pin each PTE
  724. * page while holding the lock. This means the number
  725. * of locks we end up holding is never more than a
  726. * batch size (~32 entries, at present).
  727. *
  728. * If we're not using split pte locks, we needn't pin
  729. * the PTE pages independently, because we're
  730. * protected by the overall pagetable lock.
  731. */
  732. ptl = NULL;
  733. if (level == PT_PTE)
  734. ptl = xen_pte_lock(page, mm);
  735. MULTI_update_va_mapping(mcs.mc, (unsigned long)pt,
  736. pfn_pte(pfn, PAGE_KERNEL_RO),
  737. level == PT_PGD ? UVMF_TLB_FLUSH : 0);
  738. if (ptl) {
  739. xen_do_pin(MMUEXT_PIN_L1_TABLE, pfn);
  740. /* Queue a deferred unlock for when this batch
  741. is completed. */
  742. xen_mc_callback(xen_pte_unlock, ptl);
  743. }
  744. }
  745. return flush;
  746. }
  747. /* This is called just after a mm has been created, but it has not
  748. been used yet. We need to make sure that its pagetable is all
  749. read-only, and can be pinned. */
  750. static void __xen_pgd_pin(struct mm_struct *mm, pgd_t *pgd)
  751. {
  752. vm_unmap_aliases();
  753. xen_mc_batch();
  754. if (__xen_pgd_walk(mm, pgd, xen_pin_page, USER_LIMIT)) {
  755. /* re-enable interrupts for flushing */
  756. xen_mc_issue(0);
  757. kmap_flush_unused();
  758. xen_mc_batch();
  759. }
  760. #ifdef CONFIG_X86_64
  761. {
  762. pgd_t *user_pgd = xen_get_user_pgd(pgd);
  763. xen_do_pin(MMUEXT_PIN_L4_TABLE, PFN_DOWN(__pa(pgd)));
  764. if (user_pgd) {
  765. xen_pin_page(mm, virt_to_page(user_pgd), PT_PGD);
  766. xen_do_pin(MMUEXT_PIN_L4_TABLE,
  767. PFN_DOWN(__pa(user_pgd)));
  768. }
  769. }
  770. #else /* CONFIG_X86_32 */
  771. #ifdef CONFIG_X86_PAE
  772. /* Need to make sure unshared kernel PMD is pinnable */
  773. xen_pin_page(mm, pgd_page(pgd[pgd_index(TASK_SIZE)]),
  774. PT_PMD);
  775. #endif
  776. xen_do_pin(MMUEXT_PIN_L3_TABLE, PFN_DOWN(__pa(pgd)));
  777. #endif /* CONFIG_X86_64 */
  778. xen_mc_issue(0);
  779. }
  780. static void xen_pgd_pin(struct mm_struct *mm)
  781. {
  782. __xen_pgd_pin(mm, mm->pgd);
  783. }
  784. /*
  785. * On save, we need to pin all pagetables to make sure they get their
  786. * mfns turned into pfns. Search the list for any unpinned pgds and pin
  787. * them (unpinned pgds are not currently in use, probably because the
  788. * process is under construction or destruction).
  789. *
  790. * Expected to be called in stop_machine() ("equivalent to taking
  791. * every spinlock in the system"), so the locking doesn't really
  792. * matter all that much.
  793. */
  794. void xen_mm_pin_all(void)
  795. {
  796. unsigned long flags;
  797. struct page *page;
  798. spin_lock_irqsave(&pgd_lock, flags);
  799. list_for_each_entry(page, &pgd_list, lru) {
  800. if (!PagePinned(page)) {
  801. __xen_pgd_pin(&init_mm, (pgd_t *)page_address(page));
  802. SetPageSavePinned(page);
  803. }
  804. }
  805. spin_unlock_irqrestore(&pgd_lock, flags);
  806. }
  807. /*
  808. * The init_mm pagetable is really pinned as soon as its created, but
  809. * that's before we have page structures to store the bits. So do all
  810. * the book-keeping now.
  811. */
  812. static __init int xen_mark_pinned(struct mm_struct *mm, struct page *page,
  813. enum pt_level level)
  814. {
  815. SetPagePinned(page);
  816. return 0;
  817. }
  818. static void __init xen_mark_init_mm_pinned(void)
  819. {
  820. xen_pgd_walk(&init_mm, xen_mark_pinned, FIXADDR_TOP);
  821. }
  822. static int xen_unpin_page(struct mm_struct *mm, struct page *page,
  823. enum pt_level level)
  824. {
  825. unsigned pgfl = TestClearPagePinned(page);
  826. if (pgfl && !PageHighMem(page)) {
  827. void *pt = lowmem_page_address(page);
  828. unsigned long pfn = page_to_pfn(page);
  829. spinlock_t *ptl = NULL;
  830. struct multicall_space mcs;
  831. /*
  832. * Do the converse to pin_page. If we're using split
  833. * pte locks, we must be holding the lock for while
  834. * the pte page is unpinned but still RO to prevent
  835. * concurrent updates from seeing it in this
  836. * partially-pinned state.
  837. */
  838. if (level == PT_PTE) {
  839. ptl = xen_pte_lock(page, mm);
  840. if (ptl)
  841. xen_do_pin(MMUEXT_UNPIN_TABLE, pfn);
  842. }
  843. mcs = __xen_mc_entry(0);
  844. MULTI_update_va_mapping(mcs.mc, (unsigned long)pt,
  845. pfn_pte(pfn, PAGE_KERNEL),
  846. level == PT_PGD ? UVMF_TLB_FLUSH : 0);
  847. if (ptl) {
  848. /* unlock when batch completed */
  849. xen_mc_callback(xen_pte_unlock, ptl);
  850. }
  851. }
  852. return 0; /* never need to flush on unpin */
  853. }
  854. /* Release a pagetables pages back as normal RW */
  855. static void __xen_pgd_unpin(struct mm_struct *mm, pgd_t *pgd)
  856. {
  857. xen_mc_batch();
  858. xen_do_pin(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
  859. #ifdef CONFIG_X86_64
  860. {
  861. pgd_t *user_pgd = xen_get_user_pgd(pgd);
  862. if (user_pgd) {
  863. xen_do_pin(MMUEXT_UNPIN_TABLE,
  864. PFN_DOWN(__pa(user_pgd)));
  865. xen_unpin_page(mm, virt_to_page(user_pgd), PT_PGD);
  866. }
  867. }
  868. #endif
  869. #ifdef CONFIG_X86_PAE
  870. /* Need to make sure unshared kernel PMD is unpinned */
  871. xen_unpin_page(mm, pgd_page(pgd[pgd_index(TASK_SIZE)]),
  872. PT_PMD);
  873. #endif
  874. __xen_pgd_walk(mm, pgd, xen_unpin_page, USER_LIMIT);
  875. xen_mc_issue(0);
  876. }
  877. static void xen_pgd_unpin(struct mm_struct *mm)
  878. {
  879. __xen_pgd_unpin(mm, mm->pgd);
  880. }
  881. /*
  882. * On resume, undo any pinning done at save, so that the rest of the
  883. * kernel doesn't see any unexpected pinned pagetables.
  884. */
  885. void xen_mm_unpin_all(void)
  886. {
  887. unsigned long flags;
  888. struct page *page;
  889. spin_lock_irqsave(&pgd_lock, flags);
  890. list_for_each_entry(page, &pgd_list, lru) {
  891. if (PageSavePinned(page)) {
  892. BUG_ON(!PagePinned(page));
  893. __xen_pgd_unpin(&init_mm, (pgd_t *)page_address(page));
  894. ClearPageSavePinned(page);
  895. }
  896. }
  897. spin_unlock_irqrestore(&pgd_lock, flags);
  898. }
  899. void xen_activate_mm(struct mm_struct *prev, struct mm_struct *next)
  900. {
  901. spin_lock(&next->page_table_lock);
  902. xen_pgd_pin(next);
  903. spin_unlock(&next->page_table_lock);
  904. }
  905. void xen_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm)
  906. {
  907. spin_lock(&mm->page_table_lock);
  908. xen_pgd_pin(mm);
  909. spin_unlock(&mm->page_table_lock);
  910. }
  911. #ifdef CONFIG_SMP
  912. /* Another cpu may still have their %cr3 pointing at the pagetable, so
  913. we need to repoint it somewhere else before we can unpin it. */
  914. static void drop_other_mm_ref(void *info)
  915. {
  916. struct mm_struct *mm = info;
  917. struct mm_struct *active_mm;
  918. active_mm = percpu_read(cpu_tlbstate.active_mm);
  919. if (active_mm == mm)
  920. leave_mm(smp_processor_id());
  921. /* If this cpu still has a stale cr3 reference, then make sure
  922. it has been flushed. */
  923. if (percpu_read(xen_current_cr3) == __pa(mm->pgd)) {
  924. load_cr3(swapper_pg_dir);
  925. arch_flush_lazy_cpu_mode();
  926. }
  927. }
  928. static void xen_drop_mm_ref(struct mm_struct *mm)
  929. {
  930. cpumask_var_t mask;
  931. unsigned cpu;
  932. if (current->active_mm == mm) {
  933. if (current->mm == mm)
  934. load_cr3(swapper_pg_dir);
  935. else
  936. leave_mm(smp_processor_id());
  937. arch_flush_lazy_cpu_mode();
  938. }
  939. /* Get the "official" set of cpus referring to our pagetable. */
  940. if (!alloc_cpumask_var(&mask, GFP_ATOMIC)) {
  941. for_each_online_cpu(cpu) {
  942. if (!cpumask_test_cpu(cpu, &mm->cpu_vm_mask)
  943. && per_cpu(xen_current_cr3, cpu) != __pa(mm->pgd))
  944. continue;
  945. smp_call_function_single(cpu, drop_other_mm_ref, mm, 1);
  946. }
  947. return;
  948. }
  949. cpumask_copy(mask, &mm->cpu_vm_mask);
  950. /* It's possible that a vcpu may have a stale reference to our
  951. cr3, because its in lazy mode, and it hasn't yet flushed
  952. its set of pending hypercalls yet. In this case, we can
  953. look at its actual current cr3 value, and force it to flush
  954. if needed. */
  955. for_each_online_cpu(cpu) {
  956. if (per_cpu(xen_current_cr3, cpu) == __pa(mm->pgd))
  957. cpumask_set_cpu(cpu, mask);
  958. }
  959. if (!cpumask_empty(mask))
  960. smp_call_function_many(mask, drop_other_mm_ref, mm, 1);
  961. free_cpumask_var(mask);
  962. }
  963. #else
  964. static void xen_drop_mm_ref(struct mm_struct *mm)
  965. {
  966. if (current->active_mm == mm)
  967. load_cr3(swapper_pg_dir);
  968. }
  969. #endif
  970. /*
  971. * While a process runs, Xen pins its pagetables, which means that the
  972. * hypervisor forces it to be read-only, and it controls all updates
  973. * to it. This means that all pagetable updates have to go via the
  974. * hypervisor, which is moderately expensive.
  975. *
  976. * Since we're pulling the pagetable down, we switch to use init_mm,
  977. * unpin old process pagetable and mark it all read-write, which
  978. * allows further operations on it to be simple memory accesses.
  979. *
  980. * The only subtle point is that another CPU may be still using the
  981. * pagetable because of lazy tlb flushing. This means we need need to
  982. * switch all CPUs off this pagetable before we can unpin it.
  983. */
  984. void xen_exit_mmap(struct mm_struct *mm)
  985. {
  986. get_cpu(); /* make sure we don't move around */
  987. xen_drop_mm_ref(mm);
  988. put_cpu();
  989. spin_lock(&mm->page_table_lock);
  990. /* pgd may not be pinned in the error exit path of execve */
  991. if (xen_page_pinned(mm->pgd))
  992. xen_pgd_unpin(mm);
  993. spin_unlock(&mm->page_table_lock);
  994. }
  995. static __init void xen_pagetable_setup_start(pgd_t *base)
  996. {
  997. }
  998. static __init void xen_pagetable_setup_done(pgd_t *base)
  999. {
  1000. xen_setup_shared_info();
  1001. }
  1002. static void xen_write_cr2(unsigned long cr2)
  1003. {
  1004. percpu_read(xen_vcpu)->arch.cr2 = cr2;
  1005. }
  1006. static unsigned long xen_read_cr2(void)
  1007. {
  1008. return percpu_read(xen_vcpu)->arch.cr2;
  1009. }
  1010. unsigned long xen_read_cr2_direct(void)
  1011. {
  1012. return percpu_read(xen_vcpu_info.arch.cr2);
  1013. }
  1014. static void xen_flush_tlb(void)
  1015. {
  1016. struct mmuext_op *op;
  1017. struct multicall_space mcs;
  1018. preempt_disable();
  1019. mcs = xen_mc_entry(sizeof(*op));
  1020. op = mcs.args;
  1021. op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
  1022. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  1023. xen_mc_issue(PARAVIRT_LAZY_MMU);
  1024. preempt_enable();
  1025. }
  1026. static void xen_flush_tlb_single(unsigned long addr)
  1027. {
  1028. struct mmuext_op *op;
  1029. struct multicall_space mcs;
  1030. preempt_disable();
  1031. mcs = xen_mc_entry(sizeof(*op));
  1032. op = mcs.args;
  1033. op->cmd = MMUEXT_INVLPG_LOCAL;
  1034. op->arg1.linear_addr = addr & PAGE_MASK;
  1035. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  1036. xen_mc_issue(PARAVIRT_LAZY_MMU);
  1037. preempt_enable();
  1038. }
  1039. static void xen_flush_tlb_others(const struct cpumask *cpus,
  1040. struct mm_struct *mm, unsigned long va)
  1041. {
  1042. struct {
  1043. struct mmuext_op op;
  1044. DECLARE_BITMAP(mask, NR_CPUS);
  1045. } *args;
  1046. struct multicall_space mcs;
  1047. if (cpumask_empty(cpus))
  1048. return; /* nothing to do */
  1049. mcs = xen_mc_entry(sizeof(*args));
  1050. args = mcs.args;
  1051. args->op.arg2.vcpumask = to_cpumask(args->mask);
  1052. /* Remove us, and any offline CPUS. */
  1053. cpumask_and(to_cpumask(args->mask), cpus, cpu_online_mask);
  1054. cpumask_clear_cpu(smp_processor_id(), to_cpumask(args->mask));
  1055. if (va == TLB_FLUSH_ALL) {
  1056. args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
  1057. } else {
  1058. args->op.cmd = MMUEXT_INVLPG_MULTI;
  1059. args->op.arg1.linear_addr = va;
  1060. }
  1061. MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
  1062. xen_mc_issue(PARAVIRT_LAZY_MMU);
  1063. }
  1064. static unsigned long xen_read_cr3(void)
  1065. {
  1066. return percpu_read(xen_cr3);
  1067. }
  1068. static void set_current_cr3(void *v)
  1069. {
  1070. percpu_write(xen_current_cr3, (unsigned long)v);
  1071. }
  1072. static void __xen_write_cr3(bool kernel, unsigned long cr3)
  1073. {
  1074. struct mmuext_op *op;
  1075. struct multicall_space mcs;
  1076. unsigned long mfn;
  1077. if (cr3)
  1078. mfn = pfn_to_mfn(PFN_DOWN(cr3));
  1079. else
  1080. mfn = 0;
  1081. WARN_ON(mfn == 0 && kernel);
  1082. mcs = __xen_mc_entry(sizeof(*op));
  1083. op = mcs.args;
  1084. op->cmd = kernel ? MMUEXT_NEW_BASEPTR : MMUEXT_NEW_USER_BASEPTR;
  1085. op->arg1.mfn = mfn;
  1086. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  1087. if (kernel) {
  1088. percpu_write(xen_cr3, cr3);
  1089. /* Update xen_current_cr3 once the batch has actually
  1090. been submitted. */
  1091. xen_mc_callback(set_current_cr3, (void *)cr3);
  1092. }
  1093. }
  1094. static void xen_write_cr3(unsigned long cr3)
  1095. {
  1096. BUG_ON(preemptible());
  1097. xen_mc_batch(); /* disables interrupts */
  1098. /* Update while interrupts are disabled, so its atomic with
  1099. respect to ipis */
  1100. percpu_write(xen_cr3, cr3);
  1101. __xen_write_cr3(true, cr3);
  1102. #ifdef CONFIG_X86_64
  1103. {
  1104. pgd_t *user_pgd = xen_get_user_pgd(__va(cr3));
  1105. if (user_pgd)
  1106. __xen_write_cr3(false, __pa(user_pgd));
  1107. else
  1108. __xen_write_cr3(false, 0);
  1109. }
  1110. #endif
  1111. xen_mc_issue(PARAVIRT_LAZY_CPU); /* interrupts restored */
  1112. }
  1113. static int xen_pgd_alloc(struct mm_struct *mm)
  1114. {
  1115. pgd_t *pgd = mm->pgd;
  1116. int ret = 0;
  1117. BUG_ON(PagePinned(virt_to_page(pgd)));
  1118. #ifdef CONFIG_X86_64
  1119. {
  1120. struct page *page = virt_to_page(pgd);
  1121. pgd_t *user_pgd;
  1122. BUG_ON(page->private != 0);
  1123. ret = -ENOMEM;
  1124. user_pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  1125. page->private = (unsigned long)user_pgd;
  1126. if (user_pgd != NULL) {
  1127. user_pgd[pgd_index(VSYSCALL_START)] =
  1128. __pgd(__pa(level3_user_vsyscall) | _PAGE_TABLE);
  1129. ret = 0;
  1130. }
  1131. BUG_ON(PagePinned(virt_to_page(xen_get_user_pgd(pgd))));
  1132. }
  1133. #endif
  1134. return ret;
  1135. }
  1136. static void xen_pgd_free(struct mm_struct *mm, pgd_t *pgd)
  1137. {
  1138. #ifdef CONFIG_X86_64
  1139. pgd_t *user_pgd = xen_get_user_pgd(pgd);
  1140. if (user_pgd)
  1141. free_page((unsigned long)user_pgd);
  1142. #endif
  1143. }
  1144. #ifdef CONFIG_HIGHPTE
  1145. static void *xen_kmap_atomic_pte(struct page *page, enum km_type type)
  1146. {
  1147. pgprot_t prot = PAGE_KERNEL;
  1148. if (PagePinned(page))
  1149. prot = PAGE_KERNEL_RO;
  1150. if (0 && PageHighMem(page))
  1151. printk("mapping highpte %lx type %d prot %s\n",
  1152. page_to_pfn(page), type,
  1153. (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ");
  1154. return kmap_atomic_prot(page, type, prot);
  1155. }
  1156. #endif
  1157. #ifdef CONFIG_X86_32
  1158. static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
  1159. {
  1160. /* If there's an existing pte, then don't allow _PAGE_RW to be set */
  1161. if (pte_val_ma(*ptep) & _PAGE_PRESENT)
  1162. pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
  1163. pte_val_ma(pte));
  1164. return pte;
  1165. }
  1166. /* Init-time set_pte while constructing initial pagetables, which
  1167. doesn't allow RO pagetable pages to be remapped RW */
  1168. static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
  1169. {
  1170. pte = mask_rw_pte(ptep, pte);
  1171. xen_set_pte(ptep, pte);
  1172. }
  1173. #endif
  1174. static void pin_pagetable_pfn(unsigned cmd, unsigned long pfn)
  1175. {
  1176. struct mmuext_op op;
  1177. op.cmd = cmd;
  1178. op.arg1.mfn = pfn_to_mfn(pfn);
  1179. if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
  1180. BUG();
  1181. }
  1182. /* Early in boot, while setting up the initial pagetable, assume
  1183. everything is pinned. */
  1184. static __init void xen_alloc_pte_init(struct mm_struct *mm, unsigned long pfn)
  1185. {
  1186. #ifdef CONFIG_FLATMEM
  1187. BUG_ON(mem_map); /* should only be used early */
  1188. #endif
  1189. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  1190. pin_pagetable_pfn(MMUEXT_PIN_L1_TABLE, pfn);
  1191. }
  1192. /* Used for pmd and pud */
  1193. static __init void xen_alloc_pmd_init(struct mm_struct *mm, unsigned long pfn)
  1194. {
  1195. #ifdef CONFIG_FLATMEM
  1196. BUG_ON(mem_map); /* should only be used early */
  1197. #endif
  1198. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  1199. }
  1200. /* Early release_pte assumes that all pts are pinned, since there's
  1201. only init_mm and anything attached to that is pinned. */
  1202. static __init void xen_release_pte_init(unsigned long pfn)
  1203. {
  1204. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, pfn);
  1205. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  1206. }
  1207. static __init void xen_release_pmd_init(unsigned long pfn)
  1208. {
  1209. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  1210. }
  1211. /* This needs to make sure the new pte page is pinned iff its being
  1212. attached to a pinned pagetable. */
  1213. static void xen_alloc_ptpage(struct mm_struct *mm, unsigned long pfn, unsigned level)
  1214. {
  1215. struct page *page = pfn_to_page(pfn);
  1216. if (PagePinned(virt_to_page(mm->pgd))) {
  1217. SetPagePinned(page);
  1218. vm_unmap_aliases();
  1219. if (!PageHighMem(page)) {
  1220. make_lowmem_page_readonly(__va(PFN_PHYS((unsigned long)pfn)));
  1221. if (level == PT_PTE && USE_SPLIT_PTLOCKS)
  1222. pin_pagetable_pfn(MMUEXT_PIN_L1_TABLE, pfn);
  1223. } else {
  1224. /* make sure there are no stray mappings of
  1225. this page */
  1226. kmap_flush_unused();
  1227. }
  1228. }
  1229. }
  1230. static void xen_alloc_pte(struct mm_struct *mm, unsigned long pfn)
  1231. {
  1232. xen_alloc_ptpage(mm, pfn, PT_PTE);
  1233. }
  1234. static void xen_alloc_pmd(struct mm_struct *mm, unsigned long pfn)
  1235. {
  1236. xen_alloc_ptpage(mm, pfn, PT_PMD);
  1237. }
  1238. /* This should never happen until we're OK to use struct page */
  1239. static void xen_release_ptpage(unsigned long pfn, unsigned level)
  1240. {
  1241. struct page *page = pfn_to_page(pfn);
  1242. if (PagePinned(page)) {
  1243. if (!PageHighMem(page)) {
  1244. if (level == PT_PTE && USE_SPLIT_PTLOCKS)
  1245. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, pfn);
  1246. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  1247. }
  1248. ClearPagePinned(page);
  1249. }
  1250. }
  1251. static void xen_release_pte(unsigned long pfn)
  1252. {
  1253. xen_release_ptpage(pfn, PT_PTE);
  1254. }
  1255. static void xen_release_pmd(unsigned long pfn)
  1256. {
  1257. xen_release_ptpage(pfn, PT_PMD);
  1258. }
  1259. #if PAGETABLE_LEVELS == 4
  1260. static void xen_alloc_pud(struct mm_struct *mm, unsigned long pfn)
  1261. {
  1262. xen_alloc_ptpage(mm, pfn, PT_PUD);
  1263. }
  1264. static void xen_release_pud(unsigned long pfn)
  1265. {
  1266. xen_release_ptpage(pfn, PT_PUD);
  1267. }
  1268. #endif
  1269. void __init xen_reserve_top(void)
  1270. {
  1271. #ifdef CONFIG_X86_32
  1272. unsigned long top = HYPERVISOR_VIRT_START;
  1273. struct xen_platform_parameters pp;
  1274. if (HYPERVISOR_xen_version(XENVER_platform_parameters, &pp) == 0)
  1275. top = pp.virt_start;
  1276. reserve_top_address(-top);
  1277. #endif /* CONFIG_X86_32 */
  1278. }
  1279. /*
  1280. * Like __va(), but returns address in the kernel mapping (which is
  1281. * all we have until the physical memory mapping has been set up.
  1282. */
  1283. static void *__ka(phys_addr_t paddr)
  1284. {
  1285. #ifdef CONFIG_X86_64
  1286. return (void *)(paddr + __START_KERNEL_map);
  1287. #else
  1288. return __va(paddr);
  1289. #endif
  1290. }
  1291. /* Convert a machine address to physical address */
  1292. static unsigned long m2p(phys_addr_t maddr)
  1293. {
  1294. phys_addr_t paddr;
  1295. maddr &= PTE_PFN_MASK;
  1296. paddr = mfn_to_pfn(maddr >> PAGE_SHIFT) << PAGE_SHIFT;
  1297. return paddr;
  1298. }
  1299. /* Convert a machine address to kernel virtual */
  1300. static void *m2v(phys_addr_t maddr)
  1301. {
  1302. return __ka(m2p(maddr));
  1303. }
  1304. static void set_page_prot(void *addr, pgprot_t prot)
  1305. {
  1306. unsigned long pfn = __pa(addr) >> PAGE_SHIFT;
  1307. pte_t pte = pfn_pte(pfn, prot);
  1308. if (HYPERVISOR_update_va_mapping((unsigned long)addr, pte, 0))
  1309. BUG();
  1310. }
  1311. static __init void xen_map_identity_early(pmd_t *pmd, unsigned long max_pfn)
  1312. {
  1313. unsigned pmdidx, pteidx;
  1314. unsigned ident_pte;
  1315. unsigned long pfn;
  1316. ident_pte = 0;
  1317. pfn = 0;
  1318. for (pmdidx = 0; pmdidx < PTRS_PER_PMD && pfn < max_pfn; pmdidx++) {
  1319. pte_t *pte_page;
  1320. /* Reuse or allocate a page of ptes */
  1321. if (pmd_present(pmd[pmdidx]))
  1322. pte_page = m2v(pmd[pmdidx].pmd);
  1323. else {
  1324. /* Check for free pte pages */
  1325. if (ident_pte == ARRAY_SIZE(level1_ident_pgt))
  1326. break;
  1327. pte_page = &level1_ident_pgt[ident_pte];
  1328. ident_pte += PTRS_PER_PTE;
  1329. pmd[pmdidx] = __pmd(__pa(pte_page) | _PAGE_TABLE);
  1330. }
  1331. /* Install mappings */
  1332. for (pteidx = 0; pteidx < PTRS_PER_PTE; pteidx++, pfn++) {
  1333. pte_t pte;
  1334. if (pfn > max_pfn_mapped)
  1335. max_pfn_mapped = pfn;
  1336. if (!pte_none(pte_page[pteidx]))
  1337. continue;
  1338. pte = pfn_pte(pfn, PAGE_KERNEL_EXEC);
  1339. pte_page[pteidx] = pte;
  1340. }
  1341. }
  1342. for (pteidx = 0; pteidx < ident_pte; pteidx += PTRS_PER_PTE)
  1343. set_page_prot(&level1_ident_pgt[pteidx], PAGE_KERNEL_RO);
  1344. set_page_prot(pmd, PAGE_KERNEL_RO);
  1345. }
  1346. #ifdef CONFIG_X86_64
  1347. static void convert_pfn_mfn(void *v)
  1348. {
  1349. pte_t *pte = v;
  1350. int i;
  1351. /* All levels are converted the same way, so just treat them
  1352. as ptes. */
  1353. for (i = 0; i < PTRS_PER_PTE; i++)
  1354. pte[i] = xen_make_pte(pte[i].pte);
  1355. }
  1356. /*
  1357. * Set up the inital kernel pagetable.
  1358. *
  1359. * We can construct this by grafting the Xen provided pagetable into
  1360. * head_64.S's preconstructed pagetables. We copy the Xen L2's into
  1361. * level2_ident_pgt, level2_kernel_pgt and level2_fixmap_pgt. This
  1362. * means that only the kernel has a physical mapping to start with -
  1363. * but that's enough to get __va working. We need to fill in the rest
  1364. * of the physical mapping once some sort of allocator has been set
  1365. * up.
  1366. */
  1367. __init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd,
  1368. unsigned long max_pfn)
  1369. {
  1370. pud_t *l3;
  1371. pmd_t *l2;
  1372. /* Zap identity mapping */
  1373. init_level4_pgt[0] = __pgd(0);
  1374. /* Pre-constructed entries are in pfn, so convert to mfn */
  1375. convert_pfn_mfn(init_level4_pgt);
  1376. convert_pfn_mfn(level3_ident_pgt);
  1377. convert_pfn_mfn(level3_kernel_pgt);
  1378. l3 = m2v(pgd[pgd_index(__START_KERNEL_map)].pgd);
  1379. l2 = m2v(l3[pud_index(__START_KERNEL_map)].pud);
  1380. memcpy(level2_ident_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1381. memcpy(level2_kernel_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1382. l3 = m2v(pgd[pgd_index(__START_KERNEL_map + PMD_SIZE)].pgd);
  1383. l2 = m2v(l3[pud_index(__START_KERNEL_map + PMD_SIZE)].pud);
  1384. memcpy(level2_fixmap_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1385. /* Set up identity map */
  1386. xen_map_identity_early(level2_ident_pgt, max_pfn);
  1387. /* Make pagetable pieces RO */
  1388. set_page_prot(init_level4_pgt, PAGE_KERNEL_RO);
  1389. set_page_prot(level3_ident_pgt, PAGE_KERNEL_RO);
  1390. set_page_prot(level3_kernel_pgt, PAGE_KERNEL_RO);
  1391. set_page_prot(level3_user_vsyscall, PAGE_KERNEL_RO);
  1392. set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO);
  1393. set_page_prot(level2_fixmap_pgt, PAGE_KERNEL_RO);
  1394. /* Pin down new L4 */
  1395. pin_pagetable_pfn(MMUEXT_PIN_L4_TABLE,
  1396. PFN_DOWN(__pa_symbol(init_level4_pgt)));
  1397. /* Unpin Xen-provided one */
  1398. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
  1399. /* Switch over */
  1400. pgd = init_level4_pgt;
  1401. /*
  1402. * At this stage there can be no user pgd, and no page
  1403. * structure to attach it to, so make sure we just set kernel
  1404. * pgd.
  1405. */
  1406. xen_mc_batch();
  1407. __xen_write_cr3(true, __pa(pgd));
  1408. xen_mc_issue(PARAVIRT_LAZY_CPU);
  1409. reserve_early(__pa(xen_start_info->pt_base),
  1410. __pa(xen_start_info->pt_base +
  1411. xen_start_info->nr_pt_frames * PAGE_SIZE),
  1412. "XEN PAGETABLES");
  1413. return pgd;
  1414. }
  1415. #else /* !CONFIG_X86_64 */
  1416. static pmd_t level2_kernel_pgt[PTRS_PER_PMD] __page_aligned_bss;
  1417. __init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd,
  1418. unsigned long max_pfn)
  1419. {
  1420. pmd_t *kernel_pmd;
  1421. max_pfn_mapped = PFN_DOWN(__pa(xen_start_info->pt_base) +
  1422. xen_start_info->nr_pt_frames * PAGE_SIZE +
  1423. 512*1024);
  1424. kernel_pmd = m2v(pgd[KERNEL_PGD_BOUNDARY].pgd);
  1425. memcpy(level2_kernel_pgt, kernel_pmd, sizeof(pmd_t) * PTRS_PER_PMD);
  1426. xen_map_identity_early(level2_kernel_pgt, max_pfn);
  1427. memcpy(swapper_pg_dir, pgd, sizeof(pgd_t) * PTRS_PER_PGD);
  1428. set_pgd(&swapper_pg_dir[KERNEL_PGD_BOUNDARY],
  1429. __pgd(__pa(level2_kernel_pgt) | _PAGE_PRESENT));
  1430. set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO);
  1431. set_page_prot(swapper_pg_dir, PAGE_KERNEL_RO);
  1432. set_page_prot(empty_zero_page, PAGE_KERNEL_RO);
  1433. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
  1434. xen_write_cr3(__pa(swapper_pg_dir));
  1435. pin_pagetable_pfn(MMUEXT_PIN_L3_TABLE, PFN_DOWN(__pa(swapper_pg_dir)));
  1436. reserve_early(__pa(xen_start_info->pt_base),
  1437. __pa(xen_start_info->pt_base +
  1438. xen_start_info->nr_pt_frames * PAGE_SIZE),
  1439. "XEN PAGETABLES");
  1440. return swapper_pg_dir;
  1441. }
  1442. #endif /* CONFIG_X86_64 */
  1443. static void xen_set_fixmap(unsigned idx, phys_addr_t phys, pgprot_t prot)
  1444. {
  1445. pte_t pte;
  1446. phys >>= PAGE_SHIFT;
  1447. switch (idx) {
  1448. case FIX_BTMAP_END ... FIX_BTMAP_BEGIN:
  1449. #ifdef CONFIG_X86_F00F_BUG
  1450. case FIX_F00F_IDT:
  1451. #endif
  1452. #ifdef CONFIG_X86_32
  1453. case FIX_WP_TEST:
  1454. case FIX_VDSO:
  1455. # ifdef CONFIG_HIGHMEM
  1456. case FIX_KMAP_BEGIN ... FIX_KMAP_END:
  1457. # endif
  1458. #else
  1459. case VSYSCALL_LAST_PAGE ... VSYSCALL_FIRST_PAGE:
  1460. #endif
  1461. #ifdef CONFIG_X86_LOCAL_APIC
  1462. case FIX_APIC_BASE: /* maps dummy local APIC */
  1463. #endif
  1464. case FIX_TEXT_POKE0:
  1465. case FIX_TEXT_POKE1:
  1466. /* All local page mappings */
  1467. pte = pfn_pte(phys, prot);
  1468. break;
  1469. default:
  1470. pte = mfn_pte(phys, prot);
  1471. break;
  1472. }
  1473. __native_set_fixmap(idx, pte);
  1474. #ifdef CONFIG_X86_64
  1475. /* Replicate changes to map the vsyscall page into the user
  1476. pagetable vsyscall mapping. */
  1477. if (idx >= VSYSCALL_LAST_PAGE && idx <= VSYSCALL_FIRST_PAGE) {
  1478. unsigned long vaddr = __fix_to_virt(idx);
  1479. set_pte_vaddr_pud(level3_user_vsyscall, vaddr, pte);
  1480. }
  1481. #endif
  1482. }
  1483. __init void xen_post_allocator_init(void)
  1484. {
  1485. pv_mmu_ops.set_pte = xen_set_pte;
  1486. pv_mmu_ops.set_pmd = xen_set_pmd;
  1487. pv_mmu_ops.set_pud = xen_set_pud;
  1488. #if PAGETABLE_LEVELS == 4
  1489. pv_mmu_ops.set_pgd = xen_set_pgd;
  1490. #endif
  1491. /* This will work as long as patching hasn't happened yet
  1492. (which it hasn't) */
  1493. pv_mmu_ops.alloc_pte = xen_alloc_pte;
  1494. pv_mmu_ops.alloc_pmd = xen_alloc_pmd;
  1495. pv_mmu_ops.release_pte = xen_release_pte;
  1496. pv_mmu_ops.release_pmd = xen_release_pmd;
  1497. #if PAGETABLE_LEVELS == 4
  1498. pv_mmu_ops.alloc_pud = xen_alloc_pud;
  1499. pv_mmu_ops.release_pud = xen_release_pud;
  1500. #endif
  1501. #ifdef CONFIG_X86_64
  1502. SetPagePinned(virt_to_page(level3_user_vsyscall));
  1503. #endif
  1504. xen_mark_init_mm_pinned();
  1505. }
  1506. const struct pv_mmu_ops xen_mmu_ops __initdata = {
  1507. .pagetable_setup_start = xen_pagetable_setup_start,
  1508. .pagetable_setup_done = xen_pagetable_setup_done,
  1509. .read_cr2 = xen_read_cr2,
  1510. .write_cr2 = xen_write_cr2,
  1511. .read_cr3 = xen_read_cr3,
  1512. .write_cr3 = xen_write_cr3,
  1513. .flush_tlb_user = xen_flush_tlb,
  1514. .flush_tlb_kernel = xen_flush_tlb,
  1515. .flush_tlb_single = xen_flush_tlb_single,
  1516. .flush_tlb_others = xen_flush_tlb_others,
  1517. .pte_update = paravirt_nop,
  1518. .pte_update_defer = paravirt_nop,
  1519. .pgd_alloc = xen_pgd_alloc,
  1520. .pgd_free = xen_pgd_free,
  1521. .alloc_pte = xen_alloc_pte_init,
  1522. .release_pte = xen_release_pte_init,
  1523. .alloc_pmd = xen_alloc_pmd_init,
  1524. .alloc_pmd_clone = paravirt_nop,
  1525. .release_pmd = xen_release_pmd_init,
  1526. #ifdef CONFIG_HIGHPTE
  1527. .kmap_atomic_pte = xen_kmap_atomic_pte,
  1528. #endif
  1529. #ifdef CONFIG_X86_64
  1530. .set_pte = xen_set_pte,
  1531. #else
  1532. .set_pte = xen_set_pte_init,
  1533. #endif
  1534. .set_pte_at = xen_set_pte_at,
  1535. .set_pmd = xen_set_pmd_hyper,
  1536. .ptep_modify_prot_start = __ptep_modify_prot_start,
  1537. .ptep_modify_prot_commit = __ptep_modify_prot_commit,
  1538. .pte_val = PV_CALLEE_SAVE(xen_pte_val),
  1539. .pgd_val = PV_CALLEE_SAVE(xen_pgd_val),
  1540. .make_pte = PV_CALLEE_SAVE(xen_make_pte),
  1541. .make_pgd = PV_CALLEE_SAVE(xen_make_pgd),
  1542. #ifdef CONFIG_X86_PAE
  1543. .set_pte_atomic = xen_set_pte_atomic,
  1544. .pte_clear = xen_pte_clear,
  1545. .pmd_clear = xen_pmd_clear,
  1546. #endif /* CONFIG_X86_PAE */
  1547. .set_pud = xen_set_pud_hyper,
  1548. .make_pmd = PV_CALLEE_SAVE(xen_make_pmd),
  1549. .pmd_val = PV_CALLEE_SAVE(xen_pmd_val),
  1550. #if PAGETABLE_LEVELS == 4
  1551. .pud_val = PV_CALLEE_SAVE(xen_pud_val),
  1552. .make_pud = PV_CALLEE_SAVE(xen_make_pud),
  1553. .set_pgd = xen_set_pgd_hyper,
  1554. .alloc_pud = xen_alloc_pmd_init,
  1555. .release_pud = xen_release_pmd_init,
  1556. #endif /* PAGETABLE_LEVELS == 4 */
  1557. .activate_mm = xen_activate_mm,
  1558. .dup_mmap = xen_dup_mmap,
  1559. .exit_mmap = xen_exit_mmap,
  1560. .lazy_mode = {
  1561. .enter = paravirt_enter_lazy_mmu,
  1562. .leave = xen_leave_lazy,
  1563. },
  1564. .set_fixmap = xen_set_fixmap,
  1565. };
  1566. #ifdef CONFIG_XEN_DEBUG_FS
  1567. static struct dentry *d_mmu_debug;
  1568. static int __init xen_mmu_debugfs(void)
  1569. {
  1570. struct dentry *d_xen = xen_init_debugfs();
  1571. if (d_xen == NULL)
  1572. return -ENOMEM;
  1573. d_mmu_debug = debugfs_create_dir("mmu", d_xen);
  1574. debugfs_create_u8("zero_stats", 0644, d_mmu_debug, &zero_stats);
  1575. debugfs_create_u32("pgd_update", 0444, d_mmu_debug, &mmu_stats.pgd_update);
  1576. debugfs_create_u32("pgd_update_pinned", 0444, d_mmu_debug,
  1577. &mmu_stats.pgd_update_pinned);
  1578. debugfs_create_u32("pgd_update_batched", 0444, d_mmu_debug,
  1579. &mmu_stats.pgd_update_pinned);
  1580. debugfs_create_u32("pud_update", 0444, d_mmu_debug, &mmu_stats.pud_update);
  1581. debugfs_create_u32("pud_update_pinned", 0444, d_mmu_debug,
  1582. &mmu_stats.pud_update_pinned);
  1583. debugfs_create_u32("pud_update_batched", 0444, d_mmu_debug,
  1584. &mmu_stats.pud_update_pinned);
  1585. debugfs_create_u32("pmd_update", 0444, d_mmu_debug, &mmu_stats.pmd_update);
  1586. debugfs_create_u32("pmd_update_pinned", 0444, d_mmu_debug,
  1587. &mmu_stats.pmd_update_pinned);
  1588. debugfs_create_u32("pmd_update_batched", 0444, d_mmu_debug,
  1589. &mmu_stats.pmd_update_pinned);
  1590. debugfs_create_u32("pte_update", 0444, d_mmu_debug, &mmu_stats.pte_update);
  1591. // debugfs_create_u32("pte_update_pinned", 0444, d_mmu_debug,
  1592. // &mmu_stats.pte_update_pinned);
  1593. debugfs_create_u32("pte_update_batched", 0444, d_mmu_debug,
  1594. &mmu_stats.pte_update_pinned);
  1595. debugfs_create_u32("mmu_update", 0444, d_mmu_debug, &mmu_stats.mmu_update);
  1596. debugfs_create_u32("mmu_update_extended", 0444, d_mmu_debug,
  1597. &mmu_stats.mmu_update_extended);
  1598. xen_debugfs_create_u32_array("mmu_update_histo", 0444, d_mmu_debug,
  1599. mmu_stats.mmu_update_histo, 20);
  1600. debugfs_create_u32("set_pte_at", 0444, d_mmu_debug, &mmu_stats.set_pte_at);
  1601. debugfs_create_u32("set_pte_at_batched", 0444, d_mmu_debug,
  1602. &mmu_stats.set_pte_at_batched);
  1603. debugfs_create_u32("set_pte_at_current", 0444, d_mmu_debug,
  1604. &mmu_stats.set_pte_at_current);
  1605. debugfs_create_u32("set_pte_at_kernel", 0444, d_mmu_debug,
  1606. &mmu_stats.set_pte_at_kernel);
  1607. debugfs_create_u32("prot_commit", 0444, d_mmu_debug, &mmu_stats.prot_commit);
  1608. debugfs_create_u32("prot_commit_batched", 0444, d_mmu_debug,
  1609. &mmu_stats.prot_commit_batched);
  1610. return 0;
  1611. }
  1612. fs_initcall(xen_mmu_debugfs);
  1613. #endif /* CONFIG_XEN_DEBUG_FS */