mmu.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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 <asm/pgtable.h>
  46. #include <asm/tlbflush.h>
  47. #include <asm/fixmap.h>
  48. #include <asm/mmu_context.h>
  49. #include <asm/paravirt.h>
  50. #include <asm/linkage.h>
  51. #include <asm/xen/hypercall.h>
  52. #include <asm/xen/hypervisor.h>
  53. #include <xen/page.h>
  54. #include <xen/interface/xen.h>
  55. #include "multicalls.h"
  56. #include "mmu.h"
  57. #include "debugfs.h"
  58. #define MMU_UPDATE_HISTO 30
  59. #ifdef CONFIG_XEN_DEBUG_FS
  60. static struct {
  61. u32 pgd_update;
  62. u32 pgd_update_pinned;
  63. u32 pgd_update_batched;
  64. u32 pud_update;
  65. u32 pud_update_pinned;
  66. u32 pud_update_batched;
  67. u32 pmd_update;
  68. u32 pmd_update_pinned;
  69. u32 pmd_update_batched;
  70. u32 pte_update;
  71. u32 pte_update_pinned;
  72. u32 pte_update_batched;
  73. u32 mmu_update;
  74. u32 mmu_update_extended;
  75. u32 mmu_update_histo[MMU_UPDATE_HISTO];
  76. u32 prot_commit;
  77. u32 prot_commit_batched;
  78. u32 set_pte_at;
  79. u32 set_pte_at_batched;
  80. u32 set_pte_at_pinned;
  81. u32 set_pte_at_current;
  82. u32 set_pte_at_kernel;
  83. } mmu_stats;
  84. static u8 zero_stats;
  85. static inline void check_zero(void)
  86. {
  87. if (unlikely(zero_stats)) {
  88. memset(&mmu_stats, 0, sizeof(mmu_stats));
  89. zero_stats = 0;
  90. }
  91. }
  92. #define ADD_STATS(elem, val) \
  93. do { check_zero(); mmu_stats.elem += (val); } while(0)
  94. #else /* !CONFIG_XEN_DEBUG_FS */
  95. #define ADD_STATS(elem, val) do { (void)(val); } while(0)
  96. #endif /* CONFIG_XEN_DEBUG_FS */
  97. /*
  98. * Just beyond the highest usermode address. STACK_TOP_MAX has a
  99. * redzone above it, so round it up to a PGD boundary.
  100. */
  101. #define USER_LIMIT ((STACK_TOP_MAX + PGDIR_SIZE - 1) & PGDIR_MASK)
  102. #define P2M_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(unsigned long))
  103. #define TOP_ENTRIES (MAX_DOMAIN_PAGES / P2M_ENTRIES_PER_PAGE)
  104. /* Placeholder for holes in the address space */
  105. static unsigned long p2m_missing[P2M_ENTRIES_PER_PAGE] __page_aligned_data =
  106. { [ 0 ... P2M_ENTRIES_PER_PAGE-1 ] = ~0UL };
  107. /* Array of pointers to pages containing p2m entries */
  108. static unsigned long *p2m_top[TOP_ENTRIES] __page_aligned_data =
  109. { [ 0 ... TOP_ENTRIES - 1] = &p2m_missing[0] };
  110. /* Arrays of p2m arrays expressed in mfns used for save/restore */
  111. static unsigned long p2m_top_mfn[TOP_ENTRIES] __page_aligned_bss;
  112. static unsigned long p2m_top_mfn_list[TOP_ENTRIES / P2M_ENTRIES_PER_PAGE]
  113. __page_aligned_bss;
  114. static inline unsigned p2m_top_index(unsigned long pfn)
  115. {
  116. BUG_ON(pfn >= MAX_DOMAIN_PAGES);
  117. return pfn / P2M_ENTRIES_PER_PAGE;
  118. }
  119. static inline unsigned p2m_index(unsigned long pfn)
  120. {
  121. return pfn % P2M_ENTRIES_PER_PAGE;
  122. }
  123. /* Build the parallel p2m_top_mfn structures */
  124. void xen_setup_mfn_list_list(void)
  125. {
  126. unsigned pfn, idx;
  127. for(pfn = 0; pfn < MAX_DOMAIN_PAGES; pfn += P2M_ENTRIES_PER_PAGE) {
  128. unsigned topidx = p2m_top_index(pfn);
  129. p2m_top_mfn[topidx] = virt_to_mfn(p2m_top[topidx]);
  130. }
  131. for(idx = 0; idx < ARRAY_SIZE(p2m_top_mfn_list); idx++) {
  132. unsigned topidx = idx * P2M_ENTRIES_PER_PAGE;
  133. p2m_top_mfn_list[idx] = virt_to_mfn(&p2m_top_mfn[topidx]);
  134. }
  135. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  136. HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list =
  137. virt_to_mfn(p2m_top_mfn_list);
  138. HYPERVISOR_shared_info->arch.max_pfn = xen_start_info->nr_pages;
  139. }
  140. /* Set up p2m_top to point to the domain-builder provided p2m pages */
  141. void __init xen_build_dynamic_phys_to_machine(void)
  142. {
  143. unsigned long *mfn_list = (unsigned long *)xen_start_info->mfn_list;
  144. unsigned long max_pfn = min(MAX_DOMAIN_PAGES, xen_start_info->nr_pages);
  145. unsigned pfn;
  146. for(pfn = 0; pfn < max_pfn; pfn += P2M_ENTRIES_PER_PAGE) {
  147. unsigned topidx = p2m_top_index(pfn);
  148. p2m_top[topidx] = &mfn_list[pfn];
  149. }
  150. }
  151. unsigned long get_phys_to_machine(unsigned long pfn)
  152. {
  153. unsigned topidx, idx;
  154. if (unlikely(pfn >= MAX_DOMAIN_PAGES))
  155. return INVALID_P2M_ENTRY;
  156. topidx = p2m_top_index(pfn);
  157. idx = p2m_index(pfn);
  158. return p2m_top[topidx][idx];
  159. }
  160. EXPORT_SYMBOL_GPL(get_phys_to_machine);
  161. static void alloc_p2m(unsigned long **pp, unsigned long *mfnp)
  162. {
  163. unsigned long *p;
  164. unsigned i;
  165. p = (void *)__get_free_page(GFP_KERNEL | __GFP_NOFAIL);
  166. BUG_ON(p == NULL);
  167. for(i = 0; i < P2M_ENTRIES_PER_PAGE; i++)
  168. p[i] = INVALID_P2M_ENTRY;
  169. if (cmpxchg(pp, p2m_missing, p) != p2m_missing)
  170. free_page((unsigned long)p);
  171. else
  172. *mfnp = virt_to_mfn(p);
  173. }
  174. void set_phys_to_machine(unsigned long pfn, unsigned long mfn)
  175. {
  176. unsigned topidx, idx;
  177. if (unlikely(xen_feature(XENFEAT_auto_translated_physmap))) {
  178. BUG_ON(pfn != mfn && mfn != INVALID_P2M_ENTRY);
  179. return;
  180. }
  181. if (unlikely(pfn >= MAX_DOMAIN_PAGES)) {
  182. BUG_ON(mfn != INVALID_P2M_ENTRY);
  183. return;
  184. }
  185. topidx = p2m_top_index(pfn);
  186. if (p2m_top[topidx] == p2m_missing) {
  187. /* no need to allocate a page to store an invalid entry */
  188. if (mfn == INVALID_P2M_ENTRY)
  189. return;
  190. alloc_p2m(&p2m_top[topidx], &p2m_top_mfn[topidx]);
  191. }
  192. idx = p2m_index(pfn);
  193. p2m_top[topidx][idx] = mfn;
  194. }
  195. xmaddr_t arbitrary_virt_to_machine(void *vaddr)
  196. {
  197. unsigned long address = (unsigned long)vaddr;
  198. unsigned int level;
  199. pte_t *pte;
  200. unsigned offset;
  201. /*
  202. * if the PFN is in the linear mapped vaddr range, we can just use
  203. * the (quick) virt_to_machine() p2m lookup
  204. */
  205. if (virt_addr_valid(vaddr))
  206. return virt_to_machine(vaddr);
  207. /* otherwise we have to do a (slower) full page-table walk */
  208. pte = lookup_address(address, &level);
  209. BUG_ON(pte == NULL);
  210. offset = address & ~PAGE_MASK;
  211. return XMADDR(((phys_addr_t)pte_mfn(*pte) << PAGE_SHIFT) + offset);
  212. }
  213. void make_lowmem_page_readonly(void *vaddr)
  214. {
  215. pte_t *pte, ptev;
  216. unsigned long address = (unsigned long)vaddr;
  217. unsigned int level;
  218. pte = lookup_address(address, &level);
  219. BUG_ON(pte == NULL);
  220. ptev = pte_wrprotect(*pte);
  221. if (HYPERVISOR_update_va_mapping(address, ptev, 0))
  222. BUG();
  223. }
  224. void make_lowmem_page_readwrite(void *vaddr)
  225. {
  226. pte_t *pte, ptev;
  227. unsigned long address = (unsigned long)vaddr;
  228. unsigned int level;
  229. pte = lookup_address(address, &level);
  230. BUG_ON(pte == NULL);
  231. ptev = pte_mkwrite(*pte);
  232. if (HYPERVISOR_update_va_mapping(address, ptev, 0))
  233. BUG();
  234. }
  235. static bool xen_page_pinned(void *ptr)
  236. {
  237. struct page *page = virt_to_page(ptr);
  238. return PagePinned(page);
  239. }
  240. static void xen_extend_mmu_update(const struct mmu_update *update)
  241. {
  242. struct multicall_space mcs;
  243. struct mmu_update *u;
  244. mcs = xen_mc_extend_args(__HYPERVISOR_mmu_update, sizeof(*u));
  245. if (mcs.mc != NULL) {
  246. ADD_STATS(mmu_update_extended, 1);
  247. ADD_STATS(mmu_update_histo[mcs.mc->args[1]], -1);
  248. mcs.mc->args[1]++;
  249. if (mcs.mc->args[1] < MMU_UPDATE_HISTO)
  250. ADD_STATS(mmu_update_histo[mcs.mc->args[1]], 1);
  251. else
  252. ADD_STATS(mmu_update_histo[0], 1);
  253. } else {
  254. ADD_STATS(mmu_update, 1);
  255. mcs = __xen_mc_entry(sizeof(*u));
  256. MULTI_mmu_update(mcs.mc, mcs.args, 1, NULL, DOMID_SELF);
  257. ADD_STATS(mmu_update_histo[1], 1);
  258. }
  259. u = mcs.args;
  260. *u = *update;
  261. }
  262. void xen_set_pmd_hyper(pmd_t *ptr, pmd_t val)
  263. {
  264. struct mmu_update u;
  265. preempt_disable();
  266. xen_mc_batch();
  267. /* ptr may be ioremapped for 64-bit pagetable setup */
  268. u.ptr = arbitrary_virt_to_machine(ptr).maddr;
  269. u.val = pmd_val_ma(val);
  270. xen_extend_mmu_update(&u);
  271. ADD_STATS(pmd_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
  272. xen_mc_issue(PARAVIRT_LAZY_MMU);
  273. preempt_enable();
  274. }
  275. void xen_set_pmd(pmd_t *ptr, pmd_t val)
  276. {
  277. ADD_STATS(pmd_update, 1);
  278. /* If page is not pinned, we can just update the entry
  279. directly */
  280. if (!xen_page_pinned(ptr)) {
  281. *ptr = val;
  282. return;
  283. }
  284. ADD_STATS(pmd_update_pinned, 1);
  285. xen_set_pmd_hyper(ptr, val);
  286. }
  287. /*
  288. * Associate a virtual page frame with a given physical page frame
  289. * and protection flags for that frame.
  290. */
  291. void set_pte_mfn(unsigned long vaddr, unsigned long mfn, pgprot_t flags)
  292. {
  293. set_pte_vaddr(vaddr, mfn_pte(mfn, flags));
  294. }
  295. void xen_set_pte_at(struct mm_struct *mm, unsigned long addr,
  296. pte_t *ptep, pte_t pteval)
  297. {
  298. /* updates to init_mm may be done without lock */
  299. if (mm == &init_mm)
  300. preempt_disable();
  301. ADD_STATS(set_pte_at, 1);
  302. // ADD_STATS(set_pte_at_pinned, xen_page_pinned(ptep));
  303. ADD_STATS(set_pte_at_current, mm == current->mm);
  304. ADD_STATS(set_pte_at_kernel, mm == &init_mm);
  305. if (mm == current->mm || mm == &init_mm) {
  306. if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU) {
  307. struct multicall_space mcs;
  308. mcs = xen_mc_entry(0);
  309. MULTI_update_va_mapping(mcs.mc, addr, pteval, 0);
  310. ADD_STATS(set_pte_at_batched, 1);
  311. xen_mc_issue(PARAVIRT_LAZY_MMU);
  312. goto out;
  313. } else
  314. if (HYPERVISOR_update_va_mapping(addr, pteval, 0) == 0)
  315. goto out;
  316. }
  317. xen_set_pte(ptep, pteval);
  318. out:
  319. if (mm == &init_mm)
  320. preempt_enable();
  321. }
  322. pte_t xen_ptep_modify_prot_start(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  323. {
  324. /* Just return the pte as-is. We preserve the bits on commit */
  325. return *ptep;
  326. }
  327. void xen_ptep_modify_prot_commit(struct mm_struct *mm, unsigned long addr,
  328. pte_t *ptep, pte_t pte)
  329. {
  330. struct mmu_update u;
  331. xen_mc_batch();
  332. u.ptr = arbitrary_virt_to_machine(ptep).maddr | MMU_PT_UPDATE_PRESERVE_AD;
  333. u.val = pte_val_ma(pte);
  334. xen_extend_mmu_update(&u);
  335. ADD_STATS(prot_commit, 1);
  336. ADD_STATS(prot_commit_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
  337. xen_mc_issue(PARAVIRT_LAZY_MMU);
  338. }
  339. /* Assume pteval_t is equivalent to all the other *val_t types. */
  340. static pteval_t pte_mfn_to_pfn(pteval_t val)
  341. {
  342. if (val & _PAGE_PRESENT) {
  343. unsigned long mfn = (val & PTE_PFN_MASK) >> PAGE_SHIFT;
  344. pteval_t flags = val & PTE_FLAGS_MASK;
  345. val = ((pteval_t)mfn_to_pfn(mfn) << PAGE_SHIFT) | flags;
  346. }
  347. return val;
  348. }
  349. static pteval_t pte_pfn_to_mfn(pteval_t val)
  350. {
  351. if (val & _PAGE_PRESENT) {
  352. unsigned long pfn = (val & PTE_PFN_MASK) >> PAGE_SHIFT;
  353. pteval_t flags = val & PTE_FLAGS_MASK;
  354. val = ((pteval_t)pfn_to_mfn(pfn) << PAGE_SHIFT) | flags;
  355. }
  356. return val;
  357. }
  358. pteval_t xen_pte_val(pte_t pte)
  359. {
  360. return pte_mfn_to_pfn(pte.pte);
  361. }
  362. pgdval_t xen_pgd_val(pgd_t pgd)
  363. {
  364. return pte_mfn_to_pfn(pgd.pgd);
  365. }
  366. pte_t xen_make_pte(pteval_t pte)
  367. {
  368. pte = pte_pfn_to_mfn(pte);
  369. return native_make_pte(pte);
  370. }
  371. pgd_t xen_make_pgd(pgdval_t pgd)
  372. {
  373. pgd = pte_pfn_to_mfn(pgd);
  374. return native_make_pgd(pgd);
  375. }
  376. pmdval_t xen_pmd_val(pmd_t pmd)
  377. {
  378. return pte_mfn_to_pfn(pmd.pmd);
  379. }
  380. void xen_set_pud_hyper(pud_t *ptr, pud_t val)
  381. {
  382. struct mmu_update u;
  383. preempt_disable();
  384. xen_mc_batch();
  385. /* ptr may be ioremapped for 64-bit pagetable setup */
  386. u.ptr = arbitrary_virt_to_machine(ptr).maddr;
  387. u.val = pud_val_ma(val);
  388. xen_extend_mmu_update(&u);
  389. ADD_STATS(pud_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
  390. xen_mc_issue(PARAVIRT_LAZY_MMU);
  391. preempt_enable();
  392. }
  393. void xen_set_pud(pud_t *ptr, pud_t val)
  394. {
  395. ADD_STATS(pud_update, 1);
  396. /* If page is not pinned, we can just update the entry
  397. directly */
  398. if (!xen_page_pinned(ptr)) {
  399. *ptr = val;
  400. return;
  401. }
  402. ADD_STATS(pud_update_pinned, 1);
  403. xen_set_pud_hyper(ptr, val);
  404. }
  405. void xen_set_pte(pte_t *ptep, pte_t pte)
  406. {
  407. ADD_STATS(pte_update, 1);
  408. // ADD_STATS(pte_update_pinned, xen_page_pinned(ptep));
  409. ADD_STATS(pte_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
  410. #ifdef CONFIG_X86_PAE
  411. ptep->pte_high = pte.pte_high;
  412. smp_wmb();
  413. ptep->pte_low = pte.pte_low;
  414. #else
  415. *ptep = pte;
  416. #endif
  417. }
  418. #ifdef CONFIG_X86_PAE
  419. void xen_set_pte_atomic(pte_t *ptep, pte_t pte)
  420. {
  421. set_64bit((u64 *)ptep, native_pte_val(pte));
  422. }
  423. void xen_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  424. {
  425. ptep->pte_low = 0;
  426. smp_wmb(); /* make sure low gets written first */
  427. ptep->pte_high = 0;
  428. }
  429. void xen_pmd_clear(pmd_t *pmdp)
  430. {
  431. set_pmd(pmdp, __pmd(0));
  432. }
  433. #endif /* CONFIG_X86_PAE */
  434. pmd_t xen_make_pmd(pmdval_t pmd)
  435. {
  436. pmd = pte_pfn_to_mfn(pmd);
  437. return native_make_pmd(pmd);
  438. }
  439. #if PAGETABLE_LEVELS == 4
  440. pudval_t xen_pud_val(pud_t pud)
  441. {
  442. return pte_mfn_to_pfn(pud.pud);
  443. }
  444. pud_t xen_make_pud(pudval_t pud)
  445. {
  446. pud = pte_pfn_to_mfn(pud);
  447. return native_make_pud(pud);
  448. }
  449. pgd_t *xen_get_user_pgd(pgd_t *pgd)
  450. {
  451. pgd_t *pgd_page = (pgd_t *)(((unsigned long)pgd) & PAGE_MASK);
  452. unsigned offset = pgd - pgd_page;
  453. pgd_t *user_ptr = NULL;
  454. if (offset < pgd_index(USER_LIMIT)) {
  455. struct page *page = virt_to_page(pgd_page);
  456. user_ptr = (pgd_t *)page->private;
  457. if (user_ptr)
  458. user_ptr += offset;
  459. }
  460. return user_ptr;
  461. }
  462. static void __xen_set_pgd_hyper(pgd_t *ptr, pgd_t val)
  463. {
  464. struct mmu_update u;
  465. u.ptr = virt_to_machine(ptr).maddr;
  466. u.val = pgd_val_ma(val);
  467. xen_extend_mmu_update(&u);
  468. }
  469. /*
  470. * Raw hypercall-based set_pgd, intended for in early boot before
  471. * there's a page structure. This implies:
  472. * 1. The only existing pagetable is the kernel's
  473. * 2. It is always pinned
  474. * 3. It has no user pagetable attached to it
  475. */
  476. void __init xen_set_pgd_hyper(pgd_t *ptr, pgd_t val)
  477. {
  478. preempt_disable();
  479. xen_mc_batch();
  480. __xen_set_pgd_hyper(ptr, val);
  481. xen_mc_issue(PARAVIRT_LAZY_MMU);
  482. preempt_enable();
  483. }
  484. void xen_set_pgd(pgd_t *ptr, pgd_t val)
  485. {
  486. pgd_t *user_ptr = xen_get_user_pgd(ptr);
  487. ADD_STATS(pgd_update, 1);
  488. /* If page is not pinned, we can just update the entry
  489. directly */
  490. if (!xen_page_pinned(ptr)) {
  491. *ptr = val;
  492. if (user_ptr) {
  493. WARN_ON(xen_page_pinned(user_ptr));
  494. *user_ptr = val;
  495. }
  496. return;
  497. }
  498. ADD_STATS(pgd_update_pinned, 1);
  499. ADD_STATS(pgd_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
  500. /* If it's pinned, then we can at least batch the kernel and
  501. user updates together. */
  502. xen_mc_batch();
  503. __xen_set_pgd_hyper(ptr, val);
  504. if (user_ptr)
  505. __xen_set_pgd_hyper(user_ptr, val);
  506. xen_mc_issue(PARAVIRT_LAZY_MMU);
  507. }
  508. #endif /* PAGETABLE_LEVELS == 4 */
  509. /*
  510. * (Yet another) pagetable walker. This one is intended for pinning a
  511. * pagetable. This means that it walks a pagetable and calls the
  512. * callback function on each page it finds making up the page table,
  513. * at every level. It walks the entire pagetable, but it only bothers
  514. * pinning pte pages which are below limit. In the normal case this
  515. * will be STACK_TOP_MAX, but at boot we need to pin up to
  516. * FIXADDR_TOP.
  517. *
  518. * For 32-bit the important bit is that we don't pin beyond there,
  519. * because then we start getting into Xen's ptes.
  520. *
  521. * For 64-bit, we must skip the Xen hole in the middle of the address
  522. * space, just after the big x86-64 virtual hole.
  523. */
  524. static int xen_pgd_walk(struct mm_struct *mm,
  525. int (*func)(struct mm_struct *mm, struct page *,
  526. enum pt_level),
  527. unsigned long limit)
  528. {
  529. pgd_t *pgd = mm->pgd;
  530. int flush = 0;
  531. unsigned hole_low, hole_high;
  532. unsigned pgdidx_limit, pudidx_limit, pmdidx_limit;
  533. unsigned pgdidx, pudidx, pmdidx;
  534. /* The limit is the last byte to be touched */
  535. limit--;
  536. BUG_ON(limit >= FIXADDR_TOP);
  537. if (xen_feature(XENFEAT_auto_translated_physmap))
  538. return 0;
  539. /*
  540. * 64-bit has a great big hole in the middle of the address
  541. * space, which contains the Xen mappings. On 32-bit these
  542. * will end up making a zero-sized hole and so is a no-op.
  543. */
  544. hole_low = pgd_index(USER_LIMIT);
  545. hole_high = pgd_index(PAGE_OFFSET);
  546. pgdidx_limit = pgd_index(limit);
  547. #if PTRS_PER_PUD > 1
  548. pudidx_limit = pud_index(limit);
  549. #else
  550. pudidx_limit = 0;
  551. #endif
  552. #if PTRS_PER_PMD > 1
  553. pmdidx_limit = pmd_index(limit);
  554. #else
  555. pmdidx_limit = 0;
  556. #endif
  557. for (pgdidx = 0; pgdidx <= pgdidx_limit; pgdidx++) {
  558. pud_t *pud;
  559. if (pgdidx >= hole_low && pgdidx < hole_high)
  560. continue;
  561. if (!pgd_val(pgd[pgdidx]))
  562. continue;
  563. pud = pud_offset(&pgd[pgdidx], 0);
  564. if (PTRS_PER_PUD > 1) /* not folded */
  565. flush |= (*func)(mm, virt_to_page(pud), PT_PUD);
  566. for (pudidx = 0; pudidx < PTRS_PER_PUD; pudidx++) {
  567. pmd_t *pmd;
  568. if (pgdidx == pgdidx_limit &&
  569. pudidx > pudidx_limit)
  570. goto out;
  571. if (pud_none(pud[pudidx]))
  572. continue;
  573. pmd = pmd_offset(&pud[pudidx], 0);
  574. if (PTRS_PER_PMD > 1) /* not folded */
  575. flush |= (*func)(mm, virt_to_page(pmd), PT_PMD);
  576. for (pmdidx = 0; pmdidx < PTRS_PER_PMD; pmdidx++) {
  577. struct page *pte;
  578. if (pgdidx == pgdidx_limit &&
  579. pudidx == pudidx_limit &&
  580. pmdidx > pmdidx_limit)
  581. goto out;
  582. if (pmd_none(pmd[pmdidx]))
  583. continue;
  584. pte = pmd_page(pmd[pmdidx]);
  585. flush |= (*func)(mm, pte, PT_PTE);
  586. }
  587. }
  588. }
  589. out:
  590. /* Do the top level last, so that the callbacks can use it as
  591. a cue to do final things like tlb flushes. */
  592. flush |= (*func)(mm, virt_to_page(pgd), PT_PGD);
  593. return flush;
  594. }
  595. /* If we're using split pte locks, then take the page's lock and
  596. return a pointer to it. Otherwise return NULL. */
  597. static spinlock_t *xen_pte_lock(struct page *page, struct mm_struct *mm)
  598. {
  599. spinlock_t *ptl = NULL;
  600. #if USE_SPLIT_PTLOCKS
  601. ptl = __pte_lockptr(page);
  602. spin_lock_nest_lock(ptl, &mm->page_table_lock);
  603. #endif
  604. return ptl;
  605. }
  606. static void xen_pte_unlock(void *v)
  607. {
  608. spinlock_t *ptl = v;
  609. spin_unlock(ptl);
  610. }
  611. static void xen_do_pin(unsigned level, unsigned long pfn)
  612. {
  613. struct mmuext_op *op;
  614. struct multicall_space mcs;
  615. mcs = __xen_mc_entry(sizeof(*op));
  616. op = mcs.args;
  617. op->cmd = level;
  618. op->arg1.mfn = pfn_to_mfn(pfn);
  619. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  620. }
  621. static int xen_pin_page(struct mm_struct *mm, struct page *page,
  622. enum pt_level level)
  623. {
  624. unsigned pgfl = TestSetPagePinned(page);
  625. int flush;
  626. if (pgfl)
  627. flush = 0; /* already pinned */
  628. else if (PageHighMem(page))
  629. /* kmaps need flushing if we found an unpinned
  630. highpage */
  631. flush = 1;
  632. else {
  633. void *pt = lowmem_page_address(page);
  634. unsigned long pfn = page_to_pfn(page);
  635. struct multicall_space mcs = __xen_mc_entry(0);
  636. spinlock_t *ptl;
  637. flush = 0;
  638. /*
  639. * We need to hold the pagetable lock between the time
  640. * we make the pagetable RO and when we actually pin
  641. * it. If we don't, then other users may come in and
  642. * attempt to update the pagetable by writing it,
  643. * which will fail because the memory is RO but not
  644. * pinned, so Xen won't do the trap'n'emulate.
  645. *
  646. * If we're using split pte locks, we can't hold the
  647. * entire pagetable's worth of locks during the
  648. * traverse, because we may wrap the preempt count (8
  649. * bits). The solution is to mark RO and pin each PTE
  650. * page while holding the lock. This means the number
  651. * of locks we end up holding is never more than a
  652. * batch size (~32 entries, at present).
  653. *
  654. * If we're not using split pte locks, we needn't pin
  655. * the PTE pages independently, because we're
  656. * protected by the overall pagetable lock.
  657. */
  658. ptl = NULL;
  659. if (level == PT_PTE)
  660. ptl = xen_pte_lock(page, mm);
  661. MULTI_update_va_mapping(mcs.mc, (unsigned long)pt,
  662. pfn_pte(pfn, PAGE_KERNEL_RO),
  663. level == PT_PGD ? UVMF_TLB_FLUSH : 0);
  664. if (ptl) {
  665. xen_do_pin(MMUEXT_PIN_L1_TABLE, pfn);
  666. /* Queue a deferred unlock for when this batch
  667. is completed. */
  668. xen_mc_callback(xen_pte_unlock, ptl);
  669. }
  670. }
  671. return flush;
  672. }
  673. /* This is called just after a mm has been created, but it has not
  674. been used yet. We need to make sure that its pagetable is all
  675. read-only, and can be pinned. */
  676. static void __xen_pgd_pin(struct mm_struct *mm, pgd_t *pgd)
  677. {
  678. vm_unmap_aliases();
  679. xen_mc_batch();
  680. if (xen_pgd_walk(mm, xen_pin_page, USER_LIMIT)) {
  681. /* re-enable interrupts for flushing */
  682. xen_mc_issue(0);
  683. kmap_flush_unused();
  684. xen_mc_batch();
  685. }
  686. #ifdef CONFIG_X86_64
  687. {
  688. pgd_t *user_pgd = xen_get_user_pgd(pgd);
  689. xen_do_pin(MMUEXT_PIN_L4_TABLE, PFN_DOWN(__pa(pgd)));
  690. if (user_pgd) {
  691. xen_pin_page(mm, virt_to_page(user_pgd), PT_PGD);
  692. xen_do_pin(MMUEXT_PIN_L4_TABLE, PFN_DOWN(__pa(user_pgd)));
  693. }
  694. }
  695. #else /* CONFIG_X86_32 */
  696. #ifdef CONFIG_X86_PAE
  697. /* Need to make sure unshared kernel PMD is pinnable */
  698. xen_pin_page(mm, pgd_page(pgd[pgd_index(TASK_SIZE)]),
  699. PT_PMD);
  700. #endif
  701. xen_do_pin(MMUEXT_PIN_L3_TABLE, PFN_DOWN(__pa(pgd)));
  702. #endif /* CONFIG_X86_64 */
  703. xen_mc_issue(0);
  704. }
  705. static void xen_pgd_pin(struct mm_struct *mm)
  706. {
  707. __xen_pgd_pin(mm, mm->pgd);
  708. }
  709. /*
  710. * On save, we need to pin all pagetables to make sure they get their
  711. * mfns turned into pfns. Search the list for any unpinned pgds and pin
  712. * them (unpinned pgds are not currently in use, probably because the
  713. * process is under construction or destruction).
  714. *
  715. * Expected to be called in stop_machine() ("equivalent to taking
  716. * every spinlock in the system"), so the locking doesn't really
  717. * matter all that much.
  718. */
  719. void xen_mm_pin_all(void)
  720. {
  721. unsigned long flags;
  722. struct page *page;
  723. spin_lock_irqsave(&pgd_lock, flags);
  724. list_for_each_entry(page, &pgd_list, lru) {
  725. if (!PagePinned(page)) {
  726. __xen_pgd_pin(&init_mm, (pgd_t *)page_address(page));
  727. SetPageSavePinned(page);
  728. }
  729. }
  730. spin_unlock_irqrestore(&pgd_lock, flags);
  731. }
  732. /*
  733. * The init_mm pagetable is really pinned as soon as its created, but
  734. * that's before we have page structures to store the bits. So do all
  735. * the book-keeping now.
  736. */
  737. static __init int xen_mark_pinned(struct mm_struct *mm, struct page *page,
  738. enum pt_level level)
  739. {
  740. SetPagePinned(page);
  741. return 0;
  742. }
  743. void __init xen_mark_init_mm_pinned(void)
  744. {
  745. xen_pgd_walk(&init_mm, xen_mark_pinned, FIXADDR_TOP);
  746. }
  747. static int xen_unpin_page(struct mm_struct *mm, struct page *page,
  748. enum pt_level level)
  749. {
  750. unsigned pgfl = TestClearPagePinned(page);
  751. if (pgfl && !PageHighMem(page)) {
  752. void *pt = lowmem_page_address(page);
  753. unsigned long pfn = page_to_pfn(page);
  754. spinlock_t *ptl = NULL;
  755. struct multicall_space mcs;
  756. /*
  757. * Do the converse to pin_page. If we're using split
  758. * pte locks, we must be holding the lock for while
  759. * the pte page is unpinned but still RO to prevent
  760. * concurrent updates from seeing it in this
  761. * partially-pinned state.
  762. */
  763. if (level == PT_PTE) {
  764. ptl = xen_pte_lock(page, mm);
  765. if (ptl)
  766. xen_do_pin(MMUEXT_UNPIN_TABLE, pfn);
  767. }
  768. mcs = __xen_mc_entry(0);
  769. MULTI_update_va_mapping(mcs.mc, (unsigned long)pt,
  770. pfn_pte(pfn, PAGE_KERNEL),
  771. level == PT_PGD ? UVMF_TLB_FLUSH : 0);
  772. if (ptl) {
  773. /* unlock when batch completed */
  774. xen_mc_callback(xen_pte_unlock, ptl);
  775. }
  776. }
  777. return 0; /* never need to flush on unpin */
  778. }
  779. /* Release a pagetables pages back as normal RW */
  780. static void __xen_pgd_unpin(struct mm_struct *mm, pgd_t *pgd)
  781. {
  782. xen_mc_batch();
  783. xen_do_pin(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
  784. #ifdef CONFIG_X86_64
  785. {
  786. pgd_t *user_pgd = xen_get_user_pgd(pgd);
  787. if (user_pgd) {
  788. xen_do_pin(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(user_pgd)));
  789. xen_unpin_page(mm, virt_to_page(user_pgd), PT_PGD);
  790. }
  791. }
  792. #endif
  793. #ifdef CONFIG_X86_PAE
  794. /* Need to make sure unshared kernel PMD is unpinned */
  795. xen_unpin_page(mm, pgd_page(pgd[pgd_index(TASK_SIZE)]),
  796. PT_PMD);
  797. #endif
  798. xen_pgd_walk(mm, xen_unpin_page, USER_LIMIT);
  799. xen_mc_issue(0);
  800. }
  801. static void xen_pgd_unpin(struct mm_struct *mm)
  802. {
  803. __xen_pgd_unpin(mm, mm->pgd);
  804. }
  805. /*
  806. * On resume, undo any pinning done at save, so that the rest of the
  807. * kernel doesn't see any unexpected pinned pagetables.
  808. */
  809. void xen_mm_unpin_all(void)
  810. {
  811. unsigned long flags;
  812. struct page *page;
  813. spin_lock_irqsave(&pgd_lock, flags);
  814. list_for_each_entry(page, &pgd_list, lru) {
  815. if (PageSavePinned(page)) {
  816. BUG_ON(!PagePinned(page));
  817. __xen_pgd_unpin(&init_mm, (pgd_t *)page_address(page));
  818. ClearPageSavePinned(page);
  819. }
  820. }
  821. spin_unlock_irqrestore(&pgd_lock, flags);
  822. }
  823. void xen_activate_mm(struct mm_struct *prev, struct mm_struct *next)
  824. {
  825. spin_lock(&next->page_table_lock);
  826. xen_pgd_pin(next);
  827. spin_unlock(&next->page_table_lock);
  828. }
  829. void xen_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm)
  830. {
  831. spin_lock(&mm->page_table_lock);
  832. xen_pgd_pin(mm);
  833. spin_unlock(&mm->page_table_lock);
  834. }
  835. #ifdef CONFIG_SMP
  836. /* Another cpu may still have their %cr3 pointing at the pagetable, so
  837. we need to repoint it somewhere else before we can unpin it. */
  838. static void drop_other_mm_ref(void *info)
  839. {
  840. struct mm_struct *mm = info;
  841. struct mm_struct *active_mm;
  842. #ifdef CONFIG_X86_64
  843. active_mm = read_pda(active_mm);
  844. #else
  845. active_mm = __get_cpu_var(cpu_tlbstate).active_mm;
  846. #endif
  847. if (active_mm == mm)
  848. leave_mm(smp_processor_id());
  849. /* If this cpu still has a stale cr3 reference, then make sure
  850. it has been flushed. */
  851. if (x86_read_percpu(xen_current_cr3) == __pa(mm->pgd)) {
  852. load_cr3(swapper_pg_dir);
  853. arch_flush_lazy_cpu_mode();
  854. }
  855. }
  856. static void xen_drop_mm_ref(struct mm_struct *mm)
  857. {
  858. cpumask_t mask;
  859. unsigned cpu;
  860. if (current->active_mm == mm) {
  861. if (current->mm == mm)
  862. load_cr3(swapper_pg_dir);
  863. else
  864. leave_mm(smp_processor_id());
  865. arch_flush_lazy_cpu_mode();
  866. }
  867. /* Get the "official" set of cpus referring to our pagetable. */
  868. mask = mm->cpu_vm_mask;
  869. /* It's possible that a vcpu may have a stale reference to our
  870. cr3, because its in lazy mode, and it hasn't yet flushed
  871. its set of pending hypercalls yet. In this case, we can
  872. look at its actual current cr3 value, and force it to flush
  873. if needed. */
  874. for_each_online_cpu(cpu) {
  875. if (per_cpu(xen_current_cr3, cpu) == __pa(mm->pgd))
  876. cpu_set(cpu, mask);
  877. }
  878. if (!cpus_empty(mask))
  879. smp_call_function_mask(mask, drop_other_mm_ref, mm, 1);
  880. }
  881. #else
  882. static void xen_drop_mm_ref(struct mm_struct *mm)
  883. {
  884. if (current->active_mm == mm)
  885. load_cr3(swapper_pg_dir);
  886. }
  887. #endif
  888. /*
  889. * While a process runs, Xen pins its pagetables, which means that the
  890. * hypervisor forces it to be read-only, and it controls all updates
  891. * to it. This means that all pagetable updates have to go via the
  892. * hypervisor, which is moderately expensive.
  893. *
  894. * Since we're pulling the pagetable down, we switch to use init_mm,
  895. * unpin old process pagetable and mark it all read-write, which
  896. * allows further operations on it to be simple memory accesses.
  897. *
  898. * The only subtle point is that another CPU may be still using the
  899. * pagetable because of lazy tlb flushing. This means we need need to
  900. * switch all CPUs off this pagetable before we can unpin it.
  901. */
  902. void xen_exit_mmap(struct mm_struct *mm)
  903. {
  904. get_cpu(); /* make sure we don't move around */
  905. xen_drop_mm_ref(mm);
  906. put_cpu();
  907. spin_lock(&mm->page_table_lock);
  908. /* pgd may not be pinned in the error exit path of execve */
  909. if (xen_page_pinned(mm->pgd))
  910. xen_pgd_unpin(mm);
  911. spin_unlock(&mm->page_table_lock);
  912. }
  913. #ifdef CONFIG_XEN_DEBUG_FS
  914. static struct dentry *d_mmu_debug;
  915. static int __init xen_mmu_debugfs(void)
  916. {
  917. struct dentry *d_xen = xen_init_debugfs();
  918. if (d_xen == NULL)
  919. return -ENOMEM;
  920. d_mmu_debug = debugfs_create_dir("mmu", d_xen);
  921. debugfs_create_u8("zero_stats", 0644, d_mmu_debug, &zero_stats);
  922. debugfs_create_u32("pgd_update", 0444, d_mmu_debug, &mmu_stats.pgd_update);
  923. debugfs_create_u32("pgd_update_pinned", 0444, d_mmu_debug,
  924. &mmu_stats.pgd_update_pinned);
  925. debugfs_create_u32("pgd_update_batched", 0444, d_mmu_debug,
  926. &mmu_stats.pgd_update_pinned);
  927. debugfs_create_u32("pud_update", 0444, d_mmu_debug, &mmu_stats.pud_update);
  928. debugfs_create_u32("pud_update_pinned", 0444, d_mmu_debug,
  929. &mmu_stats.pud_update_pinned);
  930. debugfs_create_u32("pud_update_batched", 0444, d_mmu_debug,
  931. &mmu_stats.pud_update_pinned);
  932. debugfs_create_u32("pmd_update", 0444, d_mmu_debug, &mmu_stats.pmd_update);
  933. debugfs_create_u32("pmd_update_pinned", 0444, d_mmu_debug,
  934. &mmu_stats.pmd_update_pinned);
  935. debugfs_create_u32("pmd_update_batched", 0444, d_mmu_debug,
  936. &mmu_stats.pmd_update_pinned);
  937. debugfs_create_u32("pte_update", 0444, d_mmu_debug, &mmu_stats.pte_update);
  938. // debugfs_create_u32("pte_update_pinned", 0444, d_mmu_debug,
  939. // &mmu_stats.pte_update_pinned);
  940. debugfs_create_u32("pte_update_batched", 0444, d_mmu_debug,
  941. &mmu_stats.pte_update_pinned);
  942. debugfs_create_u32("mmu_update", 0444, d_mmu_debug, &mmu_stats.mmu_update);
  943. debugfs_create_u32("mmu_update_extended", 0444, d_mmu_debug,
  944. &mmu_stats.mmu_update_extended);
  945. xen_debugfs_create_u32_array("mmu_update_histo", 0444, d_mmu_debug,
  946. mmu_stats.mmu_update_histo, 20);
  947. debugfs_create_u32("set_pte_at", 0444, d_mmu_debug, &mmu_stats.set_pte_at);
  948. debugfs_create_u32("set_pte_at_batched", 0444, d_mmu_debug,
  949. &mmu_stats.set_pte_at_batched);
  950. debugfs_create_u32("set_pte_at_current", 0444, d_mmu_debug,
  951. &mmu_stats.set_pte_at_current);
  952. debugfs_create_u32("set_pte_at_kernel", 0444, d_mmu_debug,
  953. &mmu_stats.set_pte_at_kernel);
  954. debugfs_create_u32("prot_commit", 0444, d_mmu_debug, &mmu_stats.prot_commit);
  955. debugfs_create_u32("prot_commit_batched", 0444, d_mmu_debug,
  956. &mmu_stats.prot_commit_batched);
  957. return 0;
  958. }
  959. fs_initcall(xen_mmu_debugfs);
  960. #endif /* CONFIG_XEN_DEBUG_FS */