mmu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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/bug.h>
  44. #include <linux/sched.h>
  45. #include <asm/pgtable.h>
  46. #include <asm/tlbflush.h>
  47. #include <asm/mmu_context.h>
  48. #include <asm/paravirt.h>
  49. #include <asm/xen/hypercall.h>
  50. #include <asm/xen/hypervisor.h>
  51. #include <xen/page.h>
  52. #include <xen/interface/xen.h>
  53. #include "multicalls.h"
  54. #include "mmu.h"
  55. xmaddr_t arbitrary_virt_to_machine(unsigned long address)
  56. {
  57. pte_t *pte = lookup_address(address);
  58. unsigned offset = address & PAGE_MASK;
  59. BUG_ON(pte == NULL);
  60. return XMADDR((pte_mfn(*pte) << PAGE_SHIFT) + offset);
  61. }
  62. void make_lowmem_page_readonly(void *vaddr)
  63. {
  64. pte_t *pte, ptev;
  65. unsigned long address = (unsigned long)vaddr;
  66. pte = lookup_address(address);
  67. BUG_ON(pte == NULL);
  68. ptev = pte_wrprotect(*pte);
  69. if (HYPERVISOR_update_va_mapping(address, ptev, 0))
  70. BUG();
  71. }
  72. void make_lowmem_page_readwrite(void *vaddr)
  73. {
  74. pte_t *pte, ptev;
  75. unsigned long address = (unsigned long)vaddr;
  76. pte = lookup_address(address);
  77. BUG_ON(pte == NULL);
  78. ptev = pte_mkwrite(*pte);
  79. if (HYPERVISOR_update_va_mapping(address, ptev, 0))
  80. BUG();
  81. }
  82. void xen_set_pmd(pmd_t *ptr, pmd_t val)
  83. {
  84. struct multicall_space mcs;
  85. struct mmu_update *u;
  86. preempt_disable();
  87. mcs = xen_mc_entry(sizeof(*u));
  88. u = mcs.args;
  89. u->ptr = virt_to_machine(ptr).maddr;
  90. u->val = pmd_val_ma(val);
  91. MULTI_mmu_update(mcs.mc, u, 1, NULL, DOMID_SELF);
  92. xen_mc_issue(PARAVIRT_LAZY_MMU);
  93. preempt_enable();
  94. }
  95. /*
  96. * Associate a virtual page frame with a given physical page frame
  97. * and protection flags for that frame.
  98. */
  99. void set_pte_mfn(unsigned long vaddr, unsigned long mfn, pgprot_t flags)
  100. {
  101. pgd_t *pgd;
  102. pud_t *pud;
  103. pmd_t *pmd;
  104. pte_t *pte;
  105. pgd = swapper_pg_dir + pgd_index(vaddr);
  106. if (pgd_none(*pgd)) {
  107. BUG();
  108. return;
  109. }
  110. pud = pud_offset(pgd, vaddr);
  111. if (pud_none(*pud)) {
  112. BUG();
  113. return;
  114. }
  115. pmd = pmd_offset(pud, vaddr);
  116. if (pmd_none(*pmd)) {
  117. BUG();
  118. return;
  119. }
  120. pte = pte_offset_kernel(pmd, vaddr);
  121. /* <mfn,flags> stored as-is, to permit clearing entries */
  122. xen_set_pte(pte, mfn_pte(mfn, flags));
  123. /*
  124. * It's enough to flush this one mapping.
  125. * (PGE mappings get flushed as well)
  126. */
  127. __flush_tlb_one(vaddr);
  128. }
  129. void xen_set_pte_at(struct mm_struct *mm, unsigned long addr,
  130. pte_t *ptep, pte_t pteval)
  131. {
  132. if (mm == current->mm || mm == &init_mm) {
  133. if (xen_get_lazy_mode() == PARAVIRT_LAZY_MMU) {
  134. struct multicall_space mcs;
  135. mcs = xen_mc_entry(0);
  136. MULTI_update_va_mapping(mcs.mc, addr, pteval, 0);
  137. xen_mc_issue(PARAVIRT_LAZY_MMU);
  138. return;
  139. } else
  140. if (HYPERVISOR_update_va_mapping(addr, pteval, 0) == 0)
  141. return;
  142. }
  143. xen_set_pte(ptep, pteval);
  144. }
  145. #ifdef CONFIG_X86_PAE
  146. void xen_set_pud(pud_t *ptr, pud_t val)
  147. {
  148. struct multicall_space mcs;
  149. struct mmu_update *u;
  150. preempt_disable();
  151. mcs = xen_mc_entry(sizeof(*u));
  152. u = mcs.args;
  153. u->ptr = virt_to_machine(ptr).maddr;
  154. u->val = pud_val_ma(val);
  155. MULTI_mmu_update(mcs.mc, u, 1, NULL, DOMID_SELF);
  156. xen_mc_issue(PARAVIRT_LAZY_MMU);
  157. preempt_enable();
  158. }
  159. void xen_set_pte(pte_t *ptep, pte_t pte)
  160. {
  161. ptep->pte_high = pte.pte_high;
  162. smp_wmb();
  163. ptep->pte_low = pte.pte_low;
  164. }
  165. void xen_set_pte_atomic(pte_t *ptep, pte_t pte)
  166. {
  167. set_64bit((u64 *)ptep, pte_val_ma(pte));
  168. }
  169. void xen_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  170. {
  171. ptep->pte_low = 0;
  172. smp_wmb(); /* make sure low gets written first */
  173. ptep->pte_high = 0;
  174. }
  175. void xen_pmd_clear(pmd_t *pmdp)
  176. {
  177. xen_set_pmd(pmdp, __pmd(0));
  178. }
  179. unsigned long long xen_pte_val(pte_t pte)
  180. {
  181. unsigned long long ret = 0;
  182. if (pte.pte_low) {
  183. ret = ((unsigned long long)pte.pte_high << 32) | pte.pte_low;
  184. ret = machine_to_phys(XMADDR(ret)).paddr | 1;
  185. }
  186. return ret;
  187. }
  188. unsigned long long xen_pmd_val(pmd_t pmd)
  189. {
  190. unsigned long long ret = pmd.pmd;
  191. if (ret)
  192. ret = machine_to_phys(XMADDR(ret)).paddr | 1;
  193. return ret;
  194. }
  195. unsigned long long xen_pgd_val(pgd_t pgd)
  196. {
  197. unsigned long long ret = pgd.pgd;
  198. if (ret)
  199. ret = machine_to_phys(XMADDR(ret)).paddr | 1;
  200. return ret;
  201. }
  202. pte_t xen_make_pte(unsigned long long pte)
  203. {
  204. if (pte & 1)
  205. pte = phys_to_machine(XPADDR(pte)).maddr;
  206. return (pte_t){ pte, pte >> 32 };
  207. }
  208. pmd_t xen_make_pmd(unsigned long long pmd)
  209. {
  210. if (pmd & 1)
  211. pmd = phys_to_machine(XPADDR(pmd)).maddr;
  212. return (pmd_t){ pmd };
  213. }
  214. pgd_t xen_make_pgd(unsigned long long pgd)
  215. {
  216. if (pgd & _PAGE_PRESENT)
  217. pgd = phys_to_machine(XPADDR(pgd)).maddr;
  218. return (pgd_t){ pgd };
  219. }
  220. #else /* !PAE */
  221. void xen_set_pte(pte_t *ptep, pte_t pte)
  222. {
  223. *ptep = pte;
  224. }
  225. unsigned long xen_pte_val(pte_t pte)
  226. {
  227. unsigned long ret = pte.pte_low;
  228. if (ret & _PAGE_PRESENT)
  229. ret = machine_to_phys(XMADDR(ret)).paddr;
  230. return ret;
  231. }
  232. unsigned long xen_pgd_val(pgd_t pgd)
  233. {
  234. unsigned long ret = pgd.pgd;
  235. if (ret)
  236. ret = machine_to_phys(XMADDR(ret)).paddr | 1;
  237. return ret;
  238. }
  239. pte_t xen_make_pte(unsigned long pte)
  240. {
  241. if (pte & _PAGE_PRESENT)
  242. pte = phys_to_machine(XPADDR(pte)).maddr;
  243. return (pte_t){ pte };
  244. }
  245. pgd_t xen_make_pgd(unsigned long pgd)
  246. {
  247. if (pgd & _PAGE_PRESENT)
  248. pgd = phys_to_machine(XPADDR(pgd)).maddr;
  249. return (pgd_t){ pgd };
  250. }
  251. #endif /* CONFIG_X86_PAE */
  252. /*
  253. (Yet another) pagetable walker. This one is intended for pinning a
  254. pagetable. This means that it walks a pagetable and calls the
  255. callback function on each page it finds making up the page table,
  256. at every level. It walks the entire pagetable, but it only bothers
  257. pinning pte pages which are below pte_limit. In the normal case
  258. this will be TASK_SIZE, but at boot we need to pin up to
  259. FIXADDR_TOP. But the important bit is that we don't pin beyond
  260. there, because then we start getting into Xen's ptes.
  261. */
  262. static int pgd_walk(pgd_t *pgd_base, int (*func)(struct page *, unsigned),
  263. unsigned long limit)
  264. {
  265. pgd_t *pgd = pgd_base;
  266. int flush = 0;
  267. unsigned long addr = 0;
  268. unsigned long pgd_next;
  269. BUG_ON(limit > FIXADDR_TOP);
  270. if (xen_feature(XENFEAT_auto_translated_physmap))
  271. return 0;
  272. for (; addr != FIXADDR_TOP; pgd++, addr = pgd_next) {
  273. pud_t *pud;
  274. unsigned long pud_limit, pud_next;
  275. pgd_next = pud_limit = pgd_addr_end(addr, FIXADDR_TOP);
  276. if (!pgd_val(*pgd))
  277. continue;
  278. pud = pud_offset(pgd, 0);
  279. if (PTRS_PER_PUD > 1) /* not folded */
  280. flush |= (*func)(virt_to_page(pud), 0);
  281. for (; addr != pud_limit; pud++, addr = pud_next) {
  282. pmd_t *pmd;
  283. unsigned long pmd_limit;
  284. pud_next = pud_addr_end(addr, pud_limit);
  285. if (pud_next < limit)
  286. pmd_limit = pud_next;
  287. else
  288. pmd_limit = limit;
  289. if (pud_none(*pud))
  290. continue;
  291. pmd = pmd_offset(pud, 0);
  292. if (PTRS_PER_PMD > 1) /* not folded */
  293. flush |= (*func)(virt_to_page(pmd), 0);
  294. for (; addr != pmd_limit; pmd++) {
  295. addr += (PAGE_SIZE * PTRS_PER_PTE);
  296. if ((pmd_limit-1) < (addr-1)) {
  297. addr = pmd_limit;
  298. break;
  299. }
  300. if (pmd_none(*pmd))
  301. continue;
  302. flush |= (*func)(pmd_page(*pmd), 0);
  303. }
  304. }
  305. }
  306. flush |= (*func)(virt_to_page(pgd_base), UVMF_TLB_FLUSH);
  307. return flush;
  308. }
  309. static int pin_page(struct page *page, unsigned flags)
  310. {
  311. unsigned pgfl = test_and_set_bit(PG_pinned, &page->flags);
  312. int flush;
  313. if (pgfl)
  314. flush = 0; /* already pinned */
  315. else if (PageHighMem(page))
  316. /* kmaps need flushing if we found an unpinned
  317. highpage */
  318. flush = 1;
  319. else {
  320. void *pt = lowmem_page_address(page);
  321. unsigned long pfn = page_to_pfn(page);
  322. struct multicall_space mcs = __xen_mc_entry(0);
  323. flush = 0;
  324. MULTI_update_va_mapping(mcs.mc, (unsigned long)pt,
  325. pfn_pte(pfn, PAGE_KERNEL_RO),
  326. flags);
  327. }
  328. return flush;
  329. }
  330. /* This is called just after a mm has been created, but it has not
  331. been used yet. We need to make sure that its pagetable is all
  332. read-only, and can be pinned. */
  333. void xen_pgd_pin(pgd_t *pgd)
  334. {
  335. struct multicall_space mcs;
  336. struct mmuext_op *op;
  337. xen_mc_batch();
  338. if (pgd_walk(pgd, pin_page, TASK_SIZE)) {
  339. /* re-enable interrupts for kmap_flush_unused */
  340. xen_mc_issue(0);
  341. kmap_flush_unused();
  342. xen_mc_batch();
  343. }
  344. mcs = __xen_mc_entry(sizeof(*op));
  345. op = mcs.args;
  346. #ifdef CONFIG_X86_PAE
  347. op->cmd = MMUEXT_PIN_L3_TABLE;
  348. #else
  349. op->cmd = MMUEXT_PIN_L2_TABLE;
  350. #endif
  351. op->arg1.mfn = pfn_to_mfn(PFN_DOWN(__pa(pgd)));
  352. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  353. xen_mc_issue(0);
  354. }
  355. /* The init_mm pagetable is really pinned as soon as its created, but
  356. that's before we have page structures to store the bits. So do all
  357. the book-keeping now. */
  358. static __init int mark_pinned(struct page *page, unsigned flags)
  359. {
  360. SetPagePinned(page);
  361. return 0;
  362. }
  363. void __init xen_mark_init_mm_pinned(void)
  364. {
  365. pgd_walk(init_mm.pgd, mark_pinned, FIXADDR_TOP);
  366. }
  367. static int unpin_page(struct page *page, unsigned flags)
  368. {
  369. unsigned pgfl = test_and_clear_bit(PG_pinned, &page->flags);
  370. if (pgfl && !PageHighMem(page)) {
  371. void *pt = lowmem_page_address(page);
  372. unsigned long pfn = page_to_pfn(page);
  373. struct multicall_space mcs = __xen_mc_entry(0);
  374. MULTI_update_va_mapping(mcs.mc, (unsigned long)pt,
  375. pfn_pte(pfn, PAGE_KERNEL),
  376. flags);
  377. }
  378. return 0; /* never need to flush on unpin */
  379. }
  380. /* Release a pagetables pages back as normal RW */
  381. static void xen_pgd_unpin(pgd_t *pgd)
  382. {
  383. struct mmuext_op *op;
  384. struct multicall_space mcs;
  385. xen_mc_batch();
  386. mcs = __xen_mc_entry(sizeof(*op));
  387. op = mcs.args;
  388. op->cmd = MMUEXT_UNPIN_TABLE;
  389. op->arg1.mfn = pfn_to_mfn(PFN_DOWN(__pa(pgd)));
  390. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  391. pgd_walk(pgd, unpin_page, TASK_SIZE);
  392. xen_mc_issue(0);
  393. }
  394. void xen_activate_mm(struct mm_struct *prev, struct mm_struct *next)
  395. {
  396. spin_lock(&next->page_table_lock);
  397. xen_pgd_pin(next->pgd);
  398. spin_unlock(&next->page_table_lock);
  399. }
  400. void xen_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm)
  401. {
  402. spin_lock(&mm->page_table_lock);
  403. xen_pgd_pin(mm->pgd);
  404. spin_unlock(&mm->page_table_lock);
  405. }
  406. #ifdef CONFIG_SMP
  407. /* Another cpu may still have their %cr3 pointing at the pagetable, so
  408. we need to repoint it somewhere else before we can unpin it. */
  409. static void drop_other_mm_ref(void *info)
  410. {
  411. struct mm_struct *mm = info;
  412. if (__get_cpu_var(cpu_tlbstate).active_mm == mm)
  413. leave_mm(smp_processor_id());
  414. }
  415. static void drop_mm_ref(struct mm_struct *mm)
  416. {
  417. if (current->active_mm == mm) {
  418. if (current->mm == mm)
  419. load_cr3(swapper_pg_dir);
  420. else
  421. leave_mm(smp_processor_id());
  422. }
  423. if (!cpus_empty(mm->cpu_vm_mask))
  424. xen_smp_call_function_mask(mm->cpu_vm_mask, drop_other_mm_ref,
  425. mm, 1);
  426. }
  427. #else
  428. static void drop_mm_ref(struct mm_struct *mm)
  429. {
  430. if (current->active_mm == mm)
  431. load_cr3(swapper_pg_dir);
  432. }
  433. #endif
  434. /*
  435. * While a process runs, Xen pins its pagetables, which means that the
  436. * hypervisor forces it to be read-only, and it controls all updates
  437. * to it. This means that all pagetable updates have to go via the
  438. * hypervisor, which is moderately expensive.
  439. *
  440. * Since we're pulling the pagetable down, we switch to use init_mm,
  441. * unpin old process pagetable and mark it all read-write, which
  442. * allows further operations on it to be simple memory accesses.
  443. *
  444. * The only subtle point is that another CPU may be still using the
  445. * pagetable because of lazy tlb flushing. This means we need need to
  446. * switch all CPUs off this pagetable before we can unpin it.
  447. */
  448. void xen_exit_mmap(struct mm_struct *mm)
  449. {
  450. get_cpu(); /* make sure we don't move around */
  451. drop_mm_ref(mm);
  452. put_cpu();
  453. spin_lock(&mm->page_table_lock);
  454. /* pgd may not be pinned in the error exit path of execve */
  455. if (PagePinned(virt_to_page(mm->pgd)))
  456. xen_pgd_unpin(mm->pgd);
  457. spin_unlock(&mm->page_table_lock);
  458. }