page_tables.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*P:700 The pagetable code, on the other hand, still shows the scars of
  2. * previous encounters. It's functional, and as neat as it can be in the
  3. * circumstances, but be wary, for these things are subtle and break easily.
  4. * The Guest provides a virtual to physical mapping, but we can neither trust
  5. * it nor use it: we verify and convert it here to point the hardware to the
  6. * actual Guest pages when running the Guest. :*/
  7. /* Copyright (C) Rusty Russell IBM Corporation 2006.
  8. * GPL v2 and any later version */
  9. #include <linux/mm.h>
  10. #include <linux/types.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/random.h>
  13. #include <linux/percpu.h>
  14. #include <asm/tlbflush.h>
  15. #include "lg.h"
  16. /*M:008 We hold reference to pages, which prevents them from being swapped.
  17. * It'd be nice to have a callback in the "struct mm_struct" when Linux wants
  18. * to swap out. If we had this, and a shrinker callback to trim PTE pages, we
  19. * could probably consider launching Guests as non-root. :*/
  20. /*H:300
  21. * The Page Table Code
  22. *
  23. * We use two-level page tables for the Guest. If you're not entirely
  24. * comfortable with virtual addresses, physical addresses and page tables then
  25. * I recommend you review lguest.c's "Page Table Handling" (with diagrams!).
  26. *
  27. * The Guest keeps page tables, but we maintain the actual ones here: these are
  28. * called "shadow" page tables. Which is a very Guest-centric name: these are
  29. * the real page tables the CPU uses, although we keep them up to date to
  30. * reflect the Guest's. (See what I mean about weird naming? Since when do
  31. * shadows reflect anything?)
  32. *
  33. * Anyway, this is the most complicated part of the Host code. There are seven
  34. * parts to this:
  35. * (i) Setting up a page table entry for the Guest when it faults,
  36. * (ii) Setting up the page table entry for the Guest stack,
  37. * (iii) Setting up a page table entry when the Guest tells us it has changed,
  38. * (iv) Switching page tables,
  39. * (v) Flushing (thowing away) page tables,
  40. * (vi) Mapping the Switcher when the Guest is about to run,
  41. * (vii) Setting up the page tables initially.
  42. :*/
  43. /* Pages a 4k long, and each page table entry is 4 bytes long, giving us 1024
  44. * (or 2^10) entries per page. */
  45. #define PTES_PER_PAGE_SHIFT 10
  46. #define PTES_PER_PAGE (1 << PTES_PER_PAGE_SHIFT)
  47. /* 1024 entries in a page table page maps 1024 pages: 4MB. The Switcher is
  48. * conveniently placed at the top 4MB, so it uses a separate, complete PTE
  49. * page. */
  50. #define SWITCHER_PGD_INDEX (PTES_PER_PAGE - 1)
  51. /* We actually need a separate PTE page for each CPU. Remember that after the
  52. * Switcher code itself comes two pages for each CPU, and we don't want this
  53. * CPU's guest to see the pages of any other CPU. */
  54. static DEFINE_PER_CPU(spte_t *, switcher_pte_pages);
  55. #define switcher_pte_page(cpu) per_cpu(switcher_pte_pages, cpu)
  56. /*H:320 With our shadow and Guest types established, we need to deal with
  57. * them: the page table code is curly enough to need helper functions to keep
  58. * it clear and clean.
  59. *
  60. * The first helper takes a virtual address, and says which entry in the top
  61. * level page table deals with that address. Since each top level entry deals
  62. * with 4M, this effectively divides by 4M. */
  63. static unsigned vaddr_to_pgd_index(unsigned long vaddr)
  64. {
  65. return vaddr >> (PAGE_SHIFT + PTES_PER_PAGE_SHIFT);
  66. }
  67. /* There are two functions which return pointers to the shadow (aka "real")
  68. * page tables.
  69. *
  70. * spgd_addr() takes the virtual address and returns a pointer to the top-level
  71. * page directory entry for that address. Since we keep track of several page
  72. * tables, the "i" argument tells us which one we're interested in (it's
  73. * usually the current one). */
  74. static spgd_t *spgd_addr(struct lguest *lg, u32 i, unsigned long vaddr)
  75. {
  76. unsigned int index = vaddr_to_pgd_index(vaddr);
  77. /* We kill any Guest trying to touch the Switcher addresses. */
  78. if (index >= SWITCHER_PGD_INDEX) {
  79. kill_guest(lg, "attempt to access switcher pages");
  80. index = 0;
  81. }
  82. /* Return a pointer index'th pgd entry for the i'th page table. */
  83. return &lg->pgdirs[i].pgdir[index];
  84. }
  85. /* This routine then takes the PGD entry given above, which contains the
  86. * address of the PTE page. It then returns a pointer to the PTE entry for the
  87. * given address. */
  88. static spte_t *spte_addr(struct lguest *lg, spgd_t spgd, unsigned long vaddr)
  89. {
  90. spte_t *page = __va(spgd.pfn << PAGE_SHIFT);
  91. /* You should never call this if the PGD entry wasn't valid */
  92. BUG_ON(!(spgd.flags & _PAGE_PRESENT));
  93. return &page[(vaddr >> PAGE_SHIFT) % PTES_PER_PAGE];
  94. }
  95. /* These two functions just like the above two, except they access the Guest
  96. * page tables. Hence they return a Guest address. */
  97. static unsigned long gpgd_addr(struct lguest *lg, unsigned long vaddr)
  98. {
  99. unsigned int index = vaddr >> (PAGE_SHIFT + PTES_PER_PAGE_SHIFT);
  100. return lg->pgdirs[lg->pgdidx].cr3 + index * sizeof(gpgd_t);
  101. }
  102. static unsigned long gpte_addr(struct lguest *lg,
  103. gpgd_t gpgd, unsigned long vaddr)
  104. {
  105. unsigned long gpage = gpgd.pfn << PAGE_SHIFT;
  106. BUG_ON(!(gpgd.flags & _PAGE_PRESENT));
  107. return gpage + ((vaddr>>PAGE_SHIFT) % PTES_PER_PAGE) * sizeof(gpte_t);
  108. }
  109. /*H:350 This routine takes a page number given by the Guest and converts it to
  110. * an actual, physical page number. It can fail for several reasons: the
  111. * virtual address might not be mapped by the Launcher, the write flag is set
  112. * and the page is read-only, or the write flag was set and the page was
  113. * shared so had to be copied, but we ran out of memory.
  114. *
  115. * This holds a reference to the page, so release_pte() is careful to
  116. * put that back. */
  117. static unsigned long get_pfn(unsigned long virtpfn, int write)
  118. {
  119. struct page *page;
  120. /* This value indicates failure. */
  121. unsigned long ret = -1UL;
  122. /* get_user_pages() is a complex interface: it gets the "struct
  123. * vm_area_struct" and "struct page" assocated with a range of pages.
  124. * It also needs the task's mmap_sem held, and is not very quick.
  125. * It returns the number of pages it got. */
  126. down_read(&current->mm->mmap_sem);
  127. if (get_user_pages(current, current->mm, virtpfn << PAGE_SHIFT,
  128. 1, write, 1, &page, NULL) == 1)
  129. ret = page_to_pfn(page);
  130. up_read(&current->mm->mmap_sem);
  131. return ret;
  132. }
  133. /*H:340 Converting a Guest page table entry to a shadow (ie. real) page table
  134. * entry can be a little tricky. The flags are (almost) the same, but the
  135. * Guest PTE contains a virtual page number: the CPU needs the real page
  136. * number. */
  137. static spte_t gpte_to_spte(struct lguest *lg, gpte_t gpte, int write)
  138. {
  139. spte_t spte;
  140. unsigned long pfn;
  141. /* The Guest sets the global flag, because it thinks that it is using
  142. * PGE. We only told it to use PGE so it would tell us whether it was
  143. * flushing a kernel mapping or a userspace mapping. We don't actually
  144. * use the global bit, so throw it away. */
  145. spte.flags = (gpte.flags & ~_PAGE_GLOBAL);
  146. /* We need a temporary "unsigned long" variable to hold the answer from
  147. * get_pfn(), because it returns 0xFFFFFFFF on failure, which wouldn't
  148. * fit in spte.pfn. get_pfn() finds the real physical number of the
  149. * page, given the virtual number. */
  150. pfn = get_pfn(gpte.pfn, write);
  151. if (pfn == -1UL) {
  152. kill_guest(lg, "failed to get page %u", gpte.pfn);
  153. /* When we destroy the Guest, we'll go through the shadow page
  154. * tables and release_pte() them. Make sure we don't think
  155. * this one is valid! */
  156. spte.flags = 0;
  157. }
  158. /* Now we assign the page number, and our shadow PTE is complete. */
  159. spte.pfn = pfn;
  160. return spte;
  161. }
  162. /*H:460 And to complete the chain, release_pte() looks like this: */
  163. static void release_pte(spte_t pte)
  164. {
  165. /* Remember that get_user_pages() took a reference to the page, in
  166. * get_pfn()? We have to put it back now. */
  167. if (pte.flags & _PAGE_PRESENT)
  168. put_page(pfn_to_page(pte.pfn));
  169. }
  170. /*:*/
  171. static void check_gpte(struct lguest *lg, gpte_t gpte)
  172. {
  173. if ((gpte.flags & (_PAGE_PWT|_PAGE_PSE)) || gpte.pfn >= lg->pfn_limit)
  174. kill_guest(lg, "bad page table entry");
  175. }
  176. static void check_gpgd(struct lguest *lg, gpgd_t gpgd)
  177. {
  178. if ((gpgd.flags & ~_PAGE_TABLE) || gpgd.pfn >= lg->pfn_limit)
  179. kill_guest(lg, "bad page directory entry");
  180. }
  181. /*H:330
  182. * (i) Setting up a page table entry for the Guest when it faults
  183. *
  184. * We saw this call in run_guest(): when we see a page fault in the Guest, we
  185. * come here. That's because we only set up the shadow page tables lazily as
  186. * they're needed, so we get page faults all the time and quietly fix them up
  187. * and return to the Guest without it knowing.
  188. *
  189. * If we fixed up the fault (ie. we mapped the address), this routine returns
  190. * true. */
  191. int demand_page(struct lguest *lg, unsigned long vaddr, int errcode)
  192. {
  193. gpgd_t gpgd;
  194. spgd_t *spgd;
  195. unsigned long gpte_ptr;
  196. gpte_t gpte;
  197. spte_t *spte;
  198. /* First step: get the top-level Guest page table entry. */
  199. gpgd = mkgpgd(lgread_u32(lg, gpgd_addr(lg, vaddr)));
  200. /* Toplevel not present? We can't map it in. */
  201. if (!(gpgd.flags & _PAGE_PRESENT))
  202. return 0;
  203. /* Now look at the matching shadow entry. */
  204. spgd = spgd_addr(lg, lg->pgdidx, vaddr);
  205. if (!(spgd->flags & _PAGE_PRESENT)) {
  206. /* No shadow entry: allocate a new shadow PTE page. */
  207. unsigned long ptepage = get_zeroed_page(GFP_KERNEL);
  208. /* This is not really the Guest's fault, but killing it is
  209. * simple for this corner case. */
  210. if (!ptepage) {
  211. kill_guest(lg, "out of memory allocating pte page");
  212. return 0;
  213. }
  214. /* We check that the Guest pgd is OK. */
  215. check_gpgd(lg, gpgd);
  216. /* And we copy the flags to the shadow PGD entry. The page
  217. * number in the shadow PGD is the page we just allocated. */
  218. spgd->raw.val = (__pa(ptepage) | gpgd.flags);
  219. }
  220. /* OK, now we look at the lower level in the Guest page table: keep its
  221. * address, because we might update it later. */
  222. gpte_ptr = gpte_addr(lg, gpgd, vaddr);
  223. gpte = mkgpte(lgread_u32(lg, gpte_ptr));
  224. /* If this page isn't in the Guest page tables, we can't page it in. */
  225. if (!(gpte.flags & _PAGE_PRESENT))
  226. return 0;
  227. /* Check they're not trying to write to a page the Guest wants
  228. * read-only (bit 2 of errcode == write). */
  229. if ((errcode & 2) && !(gpte.flags & _PAGE_RW))
  230. return 0;
  231. /* User access to a kernel page? (bit 3 == user access) */
  232. if ((errcode & 4) && !(gpte.flags & _PAGE_USER))
  233. return 0;
  234. /* Check that the Guest PTE flags are OK, and the page number is below
  235. * the pfn_limit (ie. not mapping the Launcher binary). */
  236. check_gpte(lg, gpte);
  237. /* Add the _PAGE_ACCESSED and (for a write) _PAGE_DIRTY flag */
  238. gpte.flags |= _PAGE_ACCESSED;
  239. if (errcode & 2)
  240. gpte.flags |= _PAGE_DIRTY;
  241. /* Get the pointer to the shadow PTE entry we're going to set. */
  242. spte = spte_addr(lg, *spgd, vaddr);
  243. /* If there was a valid shadow PTE entry here before, we release it.
  244. * This can happen with a write to a previously read-only entry. */
  245. release_pte(*spte);
  246. /* If this is a write, we insist that the Guest page is writable (the
  247. * final arg to gpte_to_spte()). */
  248. if (gpte.flags & _PAGE_DIRTY)
  249. *spte = gpte_to_spte(lg, gpte, 1);
  250. else {
  251. /* If this is a read, don't set the "writable" bit in the page
  252. * table entry, even if the Guest says it's writable. That way
  253. * we come back here when a write does actually ocur, so we can
  254. * update the Guest's _PAGE_DIRTY flag. */
  255. gpte_t ro_gpte = gpte;
  256. ro_gpte.flags &= ~_PAGE_RW;
  257. *spte = gpte_to_spte(lg, ro_gpte, 0);
  258. }
  259. /* Finally, we write the Guest PTE entry back: we've set the
  260. * _PAGE_ACCESSED and maybe the _PAGE_DIRTY flags. */
  261. lgwrite_u32(lg, gpte_ptr, gpte.raw.val);
  262. /* We succeeded in mapping the page! */
  263. return 1;
  264. }
  265. /*H:360 (ii) Setting up the page table entry for the Guest stack.
  266. *
  267. * Remember pin_stack_pages() which makes sure the stack is mapped? It could
  268. * simply call demand_page(), but as we've seen that logic is quite long, and
  269. * usually the stack pages are already mapped anyway, so it's not required.
  270. *
  271. * This is a quick version which answers the question: is this virtual address
  272. * mapped by the shadow page tables, and is it writable? */
  273. static int page_writable(struct lguest *lg, unsigned long vaddr)
  274. {
  275. spgd_t *spgd;
  276. unsigned long flags;
  277. /* Look at the top level entry: is it present? */
  278. spgd = spgd_addr(lg, lg->pgdidx, vaddr);
  279. if (!(spgd->flags & _PAGE_PRESENT))
  280. return 0;
  281. /* Check the flags on the pte entry itself: it must be present and
  282. * writable. */
  283. flags = spte_addr(lg, *spgd, vaddr)->flags;
  284. return (flags & (_PAGE_PRESENT|_PAGE_RW)) == (_PAGE_PRESENT|_PAGE_RW);
  285. }
  286. /* So, when pin_stack_pages() asks us to pin a page, we check if it's already
  287. * in the page tables, and if not, we call demand_page() with error code 2
  288. * (meaning "write"). */
  289. void pin_page(struct lguest *lg, unsigned long vaddr)
  290. {
  291. if (!page_writable(lg, vaddr) && !demand_page(lg, vaddr, 2))
  292. kill_guest(lg, "bad stack page %#lx", vaddr);
  293. }
  294. /*H:450 If we chase down the release_pgd() code, it looks like this: */
  295. static void release_pgd(struct lguest *lg, spgd_t *spgd)
  296. {
  297. /* If the entry's not present, there's nothing to release. */
  298. if (spgd->flags & _PAGE_PRESENT) {
  299. unsigned int i;
  300. /* Converting the pfn to find the actual PTE page is easy: turn
  301. * the page number into a physical address, then convert to a
  302. * virtual address (easy for kernel pages like this one). */
  303. spte_t *ptepage = __va(spgd->pfn << PAGE_SHIFT);
  304. /* For each entry in the page, we might need to release it. */
  305. for (i = 0; i < PTES_PER_PAGE; i++)
  306. release_pte(ptepage[i]);
  307. /* Now we can free the page of PTEs */
  308. free_page((long)ptepage);
  309. /* And zero out the PGD entry we we never release it twice. */
  310. spgd->raw.val = 0;
  311. }
  312. }
  313. /*H:440 (v) Flushing (thowing away) page tables,
  314. *
  315. * We saw flush_user_mappings() called when we re-used a top-level pgdir page.
  316. * It simply releases every PTE page from 0 up to the kernel address. */
  317. static void flush_user_mappings(struct lguest *lg, int idx)
  318. {
  319. unsigned int i;
  320. /* Release every pgd entry up to the kernel's address. */
  321. for (i = 0; i < vaddr_to_pgd_index(lg->page_offset); i++)
  322. release_pgd(lg, lg->pgdirs[idx].pgdir + i);
  323. }
  324. /* The Guest also has a hypercall to do this manually: it's used when a large
  325. * number of mappings have been changed. */
  326. void guest_pagetable_flush_user(struct lguest *lg)
  327. {
  328. /* Drop the userspace part of the current page table. */
  329. flush_user_mappings(lg, lg->pgdidx);
  330. }
  331. /*:*/
  332. /* We keep several page tables. This is a simple routine to find the page
  333. * table (if any) corresponding to this top-level address the Guest has given
  334. * us. */
  335. static unsigned int find_pgdir(struct lguest *lg, unsigned long pgtable)
  336. {
  337. unsigned int i;
  338. for (i = 0; i < ARRAY_SIZE(lg->pgdirs); i++)
  339. if (lg->pgdirs[i].cr3 == pgtable)
  340. break;
  341. return i;
  342. }
  343. /*H:435 And this is us, creating the new page directory. If we really do
  344. * allocate a new one (and so the kernel parts are not there), we set
  345. * blank_pgdir. */
  346. static unsigned int new_pgdir(struct lguest *lg,
  347. unsigned long cr3,
  348. int *blank_pgdir)
  349. {
  350. unsigned int next;
  351. /* We pick one entry at random to throw out. Choosing the Least
  352. * Recently Used might be better, but this is easy. */
  353. next = random32() % ARRAY_SIZE(lg->pgdirs);
  354. /* If it's never been allocated at all before, try now. */
  355. if (!lg->pgdirs[next].pgdir) {
  356. lg->pgdirs[next].pgdir = (spgd_t *)get_zeroed_page(GFP_KERNEL);
  357. /* If the allocation fails, just keep using the one we have */
  358. if (!lg->pgdirs[next].pgdir)
  359. next = lg->pgdidx;
  360. else
  361. /* This is a blank page, so there are no kernel
  362. * mappings: caller must map the stack! */
  363. *blank_pgdir = 1;
  364. }
  365. /* Record which Guest toplevel this shadows. */
  366. lg->pgdirs[next].cr3 = cr3;
  367. /* Release all the non-kernel mappings. */
  368. flush_user_mappings(lg, next);
  369. return next;
  370. }
  371. /*H:430 (iv) Switching page tables
  372. *
  373. * This is what happens when the Guest changes page tables (ie. changes the
  374. * top-level pgdir). This happens on almost every context switch. */
  375. void guest_new_pagetable(struct lguest *lg, unsigned long pgtable)
  376. {
  377. int newpgdir, repin = 0;
  378. /* Look to see if we have this one already. */
  379. newpgdir = find_pgdir(lg, pgtable);
  380. /* If not, we allocate or mug an existing one: if it's a fresh one,
  381. * repin gets set to 1. */
  382. if (newpgdir == ARRAY_SIZE(lg->pgdirs))
  383. newpgdir = new_pgdir(lg, pgtable, &repin);
  384. /* Change the current pgd index to the new one. */
  385. lg->pgdidx = newpgdir;
  386. /* If it was completely blank, we map in the Guest kernel stack */
  387. if (repin)
  388. pin_stack_pages(lg);
  389. }
  390. /*H:470 Finally, a routine which throws away everything: all PGD entries in all
  391. * the shadow page tables. This is used when we destroy the Guest. */
  392. static void release_all_pagetables(struct lguest *lg)
  393. {
  394. unsigned int i, j;
  395. /* Every shadow pagetable this Guest has */
  396. for (i = 0; i < ARRAY_SIZE(lg->pgdirs); i++)
  397. if (lg->pgdirs[i].pgdir)
  398. /* Every PGD entry except the Switcher at the top */
  399. for (j = 0; j < SWITCHER_PGD_INDEX; j++)
  400. release_pgd(lg, lg->pgdirs[i].pgdir + j);
  401. }
  402. /* We also throw away everything when a Guest tells us it's changed a kernel
  403. * mapping. Since kernel mappings are in every page table, it's easiest to
  404. * throw them all away. This is amazingly slow, but thankfully rare. */
  405. void guest_pagetable_clear_all(struct lguest *lg)
  406. {
  407. release_all_pagetables(lg);
  408. /* We need the Guest kernel stack mapped again. */
  409. pin_stack_pages(lg);
  410. }
  411. /*H:420 This is the routine which actually sets the page table entry for then
  412. * "idx"'th shadow page table.
  413. *
  414. * Normally, we can just throw out the old entry and replace it with 0: if they
  415. * use it demand_page() will put the new entry in. We need to do this anyway:
  416. * The Guest expects _PAGE_ACCESSED to be set on its PTE the first time a page
  417. * is read from, and _PAGE_DIRTY when it's written to.
  418. *
  419. * But Avi Kivity pointed out that most Operating Systems (Linux included) set
  420. * these bits on PTEs immediately anyway. This is done to save the CPU from
  421. * having to update them, but it helps us the same way: if they set
  422. * _PAGE_ACCESSED then we can put a read-only PTE entry in immediately, and if
  423. * they set _PAGE_DIRTY then we can put a writable PTE entry in immediately.
  424. */
  425. static void do_set_pte(struct lguest *lg, int idx,
  426. unsigned long vaddr, gpte_t gpte)
  427. {
  428. /* Look up the matching shadow page directot entry. */
  429. spgd_t *spgd = spgd_addr(lg, idx, vaddr);
  430. /* If the top level isn't present, there's no entry to update. */
  431. if (spgd->flags & _PAGE_PRESENT) {
  432. /* Otherwise, we start by releasing the existing entry. */
  433. spte_t *spte = spte_addr(lg, *spgd, vaddr);
  434. release_pte(*spte);
  435. /* If they're setting this entry as dirty or accessed, we might
  436. * as well put that entry they've given us in now. This shaves
  437. * 10% off a copy-on-write micro-benchmark. */
  438. if (gpte.flags & (_PAGE_DIRTY | _PAGE_ACCESSED)) {
  439. check_gpte(lg, gpte);
  440. *spte = gpte_to_spte(lg, gpte, gpte.flags&_PAGE_DIRTY);
  441. } else
  442. /* Otherwise we can demand_page() it in later. */
  443. spte->raw.val = 0;
  444. }
  445. }
  446. /*H:410 Updating a PTE entry is a little trickier.
  447. *
  448. * We keep track of several different page tables (the Guest uses one for each
  449. * process, so it makes sense to cache at least a few). Each of these have
  450. * identical kernel parts: ie. every mapping above PAGE_OFFSET is the same for
  451. * all processes. So when the page table above that address changes, we update
  452. * all the page tables, not just the current one. This is rare.
  453. *
  454. * The benefit is that when we have to track a new page table, we can copy keep
  455. * all the kernel mappings. This speeds up context switch immensely. */
  456. void guest_set_pte(struct lguest *lg,
  457. unsigned long cr3, unsigned long vaddr, gpte_t gpte)
  458. {
  459. /* Kernel mappings must be changed on all top levels. Slow, but
  460. * doesn't happen often. */
  461. if (vaddr >= lg->page_offset) {
  462. unsigned int i;
  463. for (i = 0; i < ARRAY_SIZE(lg->pgdirs); i++)
  464. if (lg->pgdirs[i].pgdir)
  465. do_set_pte(lg, i, vaddr, gpte);
  466. } else {
  467. /* Is this page table one we have a shadow for? */
  468. int pgdir = find_pgdir(lg, cr3);
  469. if (pgdir != ARRAY_SIZE(lg->pgdirs))
  470. /* If so, do the update. */
  471. do_set_pte(lg, pgdir, vaddr, gpte);
  472. }
  473. }
  474. /*H:400
  475. * (iii) Setting up a page table entry when the Guest tells us it has changed.
  476. *
  477. * Just like we did in interrupts_and_traps.c, it makes sense for us to deal
  478. * with the other side of page tables while we're here: what happens when the
  479. * Guest asks for a page table to be updated?
  480. *
  481. * We already saw that demand_page() will fill in the shadow page tables when
  482. * needed, so we can simply remove shadow page table entries whenever the Guest
  483. * tells us they've changed. When the Guest tries to use the new entry it will
  484. * fault and demand_page() will fix it up.
  485. *
  486. * So with that in mind here's our code to to update a (top-level) PGD entry:
  487. */
  488. void guest_set_pmd(struct lguest *lg, unsigned long cr3, u32 idx)
  489. {
  490. int pgdir;
  491. /* The kernel seems to try to initialize this early on: we ignore its
  492. * attempts to map over the Switcher. */
  493. if (idx >= SWITCHER_PGD_INDEX)
  494. return;
  495. /* If they're talking about a page table we have a shadow for... */
  496. pgdir = find_pgdir(lg, cr3);
  497. if (pgdir < ARRAY_SIZE(lg->pgdirs))
  498. /* ... throw it away. */
  499. release_pgd(lg, lg->pgdirs[pgdir].pgdir + idx);
  500. }
  501. /*H:500 (vii) Setting up the page tables initially.
  502. *
  503. * When a Guest is first created, the Launcher tells us where the toplevel of
  504. * its first page table is. We set some things up here: */
  505. int init_guest_pagetable(struct lguest *lg, unsigned long pgtable)
  506. {
  507. /* In flush_user_mappings() we loop from 0 to
  508. * "vaddr_to_pgd_index(lg->page_offset)". This assumes it won't hit
  509. * the Switcher mappings, so check that now. */
  510. if (vaddr_to_pgd_index(lg->page_offset) >= SWITCHER_PGD_INDEX)
  511. return -EINVAL;
  512. /* We start on the first shadow page table, and give it a blank PGD
  513. * page. */
  514. lg->pgdidx = 0;
  515. lg->pgdirs[lg->pgdidx].cr3 = pgtable;
  516. lg->pgdirs[lg->pgdidx].pgdir = (spgd_t*)get_zeroed_page(GFP_KERNEL);
  517. if (!lg->pgdirs[lg->pgdidx].pgdir)
  518. return -ENOMEM;
  519. return 0;
  520. }
  521. /* When a Guest dies, our cleanup is fairly simple. */
  522. void free_guest_pagetable(struct lguest *lg)
  523. {
  524. unsigned int i;
  525. /* Throw away all page table pages. */
  526. release_all_pagetables(lg);
  527. /* Now free the top levels: free_page() can handle 0 just fine. */
  528. for (i = 0; i < ARRAY_SIZE(lg->pgdirs); i++)
  529. free_page((long)lg->pgdirs[i].pgdir);
  530. }
  531. /*H:480 (vi) Mapping the Switcher when the Guest is about to run.
  532. *
  533. * The Switcher and the two pages for this CPU need to be available to the
  534. * Guest (and not the pages for other CPUs). We have the appropriate PTE pages
  535. * for each CPU already set up, we just need to hook them in. */
  536. void map_switcher_in_guest(struct lguest *lg, struct lguest_pages *pages)
  537. {
  538. spte_t *switcher_pte_page = __get_cpu_var(switcher_pte_pages);
  539. spgd_t switcher_pgd;
  540. spte_t regs_pte;
  541. /* Make the last PGD entry for this Guest point to the Switcher's PTE
  542. * page for this CPU (with appropriate flags). */
  543. switcher_pgd.pfn = __pa(switcher_pte_page) >> PAGE_SHIFT;
  544. switcher_pgd.flags = _PAGE_KERNEL;
  545. lg->pgdirs[lg->pgdidx].pgdir[SWITCHER_PGD_INDEX] = switcher_pgd;
  546. /* We also change the Switcher PTE page. When we're running the Guest,
  547. * we want the Guest's "regs" page to appear where the first Switcher
  548. * page for this CPU is. This is an optimization: when the Switcher
  549. * saves the Guest registers, it saves them into the first page of this
  550. * CPU's "struct lguest_pages": if we make sure the Guest's register
  551. * page is already mapped there, we don't have to copy them out
  552. * again. */
  553. regs_pte.pfn = __pa(lg->regs_page) >> PAGE_SHIFT;
  554. regs_pte.flags = _PAGE_KERNEL;
  555. switcher_pte_page[(unsigned long)pages/PAGE_SIZE%PTES_PER_PAGE]
  556. = regs_pte;
  557. }
  558. /*:*/
  559. static void free_switcher_pte_pages(void)
  560. {
  561. unsigned int i;
  562. for_each_possible_cpu(i)
  563. free_page((long)switcher_pte_page(i));
  564. }
  565. /*H:520 Setting up the Switcher PTE page for given CPU is fairly easy, given
  566. * the CPU number and the "struct page"s for the Switcher code itself.
  567. *
  568. * Currently the Switcher is less than a page long, so "pages" is always 1. */
  569. static __init void populate_switcher_pte_page(unsigned int cpu,
  570. struct page *switcher_page[],
  571. unsigned int pages)
  572. {
  573. unsigned int i;
  574. spte_t *pte = switcher_pte_page(cpu);
  575. /* The first entries are easy: they map the Switcher code. */
  576. for (i = 0; i < pages; i++) {
  577. pte[i].pfn = page_to_pfn(switcher_page[i]);
  578. pte[i].flags = _PAGE_PRESENT|_PAGE_ACCESSED;
  579. }
  580. /* The only other thing we map is this CPU's pair of pages. */
  581. i = pages + cpu*2;
  582. /* First page (Guest registers) is writable from the Guest */
  583. pte[i].pfn = page_to_pfn(switcher_page[i]);
  584. pte[i].flags = _PAGE_PRESENT|_PAGE_ACCESSED|_PAGE_RW;
  585. /* The second page contains the "struct lguest_ro_state", and is
  586. * read-only. */
  587. pte[i+1].pfn = page_to_pfn(switcher_page[i+1]);
  588. pte[i+1].flags = _PAGE_PRESENT|_PAGE_ACCESSED;
  589. }
  590. /*H:510 At boot or module load time, init_pagetables() allocates and populates
  591. * the Switcher PTE page for each CPU. */
  592. __init int init_pagetables(struct page **switcher_page, unsigned int pages)
  593. {
  594. unsigned int i;
  595. for_each_possible_cpu(i) {
  596. switcher_pte_page(i) = (spte_t *)get_zeroed_page(GFP_KERNEL);
  597. if (!switcher_pte_page(i)) {
  598. free_switcher_pte_pages();
  599. return -ENOMEM;
  600. }
  601. populate_switcher_pte_page(i, switcher_page, pages);
  602. }
  603. return 0;
  604. }
  605. /*:*/
  606. /* Cleaning up simply involves freeing the PTE page for each CPU. */
  607. void free_pagetables(void)
  608. {
  609. free_switcher_pte_pages();
  610. }