page_tables.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. /*P:700
  2. * The pagetable code, on the other hand, still shows the scars of
  3. * previous encounters. It's functional, and as neat as it can be in the
  4. * circumstances, but be wary, for these things are subtle and break easily.
  5. * The Guest provides a virtual to physical mapping, but we can neither trust
  6. * it nor use it: we verify and convert it here then point the CPU to the
  7. * converted Guest pages when running the Guest.
  8. :*/
  9. /* Copyright (C) Rusty Russell IBM Corporation 2006.
  10. * GPL v2 and any later version */
  11. #include <linux/mm.h>
  12. #include <linux/types.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/random.h>
  15. #include <linux/percpu.h>
  16. #include <asm/tlbflush.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/bootparam.h>
  19. #include "lg.h"
  20. /*M:008
  21. * We hold reference to pages, which prevents them from being swapped.
  22. * It'd be nice to have a callback in the "struct mm_struct" when Linux wants
  23. * to swap out. If we had this, and a shrinker callback to trim PTE pages, we
  24. * could probably consider launching Guests as non-root.
  25. :*/
  26. /*H:300
  27. * The Page Table Code
  28. *
  29. * We use two-level page tables for the Guest, or three-level with PAE. If
  30. * you're not entirely comfortable with virtual addresses, physical addresses
  31. * and page tables then I recommend you review arch/x86/lguest/boot.c's "Page
  32. * Table Handling" (with diagrams!).
  33. *
  34. * The Guest keeps page tables, but we maintain the actual ones here: these are
  35. * called "shadow" page tables. Which is a very Guest-centric name: these are
  36. * the real page tables the CPU uses, although we keep them up to date to
  37. * reflect the Guest's. (See what I mean about weird naming? Since when do
  38. * shadows reflect anything?)
  39. *
  40. * Anyway, this is the most complicated part of the Host code. There are seven
  41. * parts to this:
  42. * (i) Looking up a page table entry when the Guest faults,
  43. * (ii) Making sure the Guest stack is mapped,
  44. * (iii) Setting up a page table entry when the Guest tells us one has changed,
  45. * (iv) Switching page tables,
  46. * (v) Flushing (throwing away) page tables,
  47. * (vi) Mapping the Switcher when the Guest is about to run,
  48. * (vii) Setting up the page tables initially.
  49. :*/
  50. /*
  51. * The Switcher uses the complete top PTE page. That's 1024 PTE entries (4MB)
  52. * or 512 PTE entries with PAE (2MB).
  53. */
  54. #define SWITCHER_PGD_INDEX (PTRS_PER_PGD - 1)
  55. /*
  56. * For PAE we need the PMD index as well. We use the last 2MB, so we
  57. * will need the last pmd entry of the last pmd page.
  58. */
  59. #ifdef CONFIG_X86_PAE
  60. #define SWITCHER_PMD_INDEX (PTRS_PER_PMD - 1)
  61. #define RESERVE_MEM 2U
  62. #define CHECK_GPGD_MASK _PAGE_PRESENT
  63. #else
  64. #define RESERVE_MEM 4U
  65. #define CHECK_GPGD_MASK _PAGE_TABLE
  66. #endif
  67. /*
  68. * We actually need a separate PTE page for each CPU. Remember that after the
  69. * Switcher code itself comes two pages for each CPU, and we don't want this
  70. * CPU's guest to see the pages of any other CPU.
  71. */
  72. static DEFINE_PER_CPU(pte_t *, switcher_pte_pages);
  73. #define switcher_pte_page(cpu) per_cpu(switcher_pte_pages, cpu)
  74. /*H:320
  75. * The page table code is curly enough to need helper functions to keep it
  76. * clear and clean. The kernel itself provides many of them; one advantage
  77. * of insisting that the Guest and Host use the same CONFIG_PAE setting.
  78. *
  79. * There are two functions which return pointers to the shadow (aka "real")
  80. * page tables.
  81. *
  82. * spgd_addr() takes the virtual address and returns a pointer to the top-level
  83. * page directory entry (PGD) for that address. Since we keep track of several
  84. * page tables, the "i" argument tells us which one we're interested in (it's
  85. * usually the current one).
  86. */
  87. static pgd_t *spgd_addr(struct lg_cpu *cpu, u32 i, unsigned long vaddr)
  88. {
  89. unsigned int index = pgd_index(vaddr);
  90. #ifndef CONFIG_X86_PAE
  91. /* We kill any Guest trying to touch the Switcher addresses. */
  92. if (index >= SWITCHER_PGD_INDEX) {
  93. kill_guest(cpu, "attempt to access switcher pages");
  94. index = 0;
  95. }
  96. #endif
  97. /* Return a pointer index'th pgd entry for the i'th page table. */
  98. return &cpu->lg->pgdirs[i].pgdir[index];
  99. }
  100. #ifdef CONFIG_X86_PAE
  101. /*
  102. * This routine then takes the PGD entry given above, which contains the
  103. * address of the PMD page. It then returns a pointer to the PMD entry for the
  104. * given address.
  105. */
  106. static pmd_t *spmd_addr(struct lg_cpu *cpu, pgd_t spgd, unsigned long vaddr)
  107. {
  108. unsigned int index = pmd_index(vaddr);
  109. pmd_t *page;
  110. /* We kill any Guest trying to touch the Switcher addresses. */
  111. if (pgd_index(vaddr) == SWITCHER_PGD_INDEX &&
  112. index >= SWITCHER_PMD_INDEX) {
  113. kill_guest(cpu, "attempt to access switcher pages");
  114. index = 0;
  115. }
  116. /* You should never call this if the PGD entry wasn't valid */
  117. BUG_ON(!(pgd_flags(spgd) & _PAGE_PRESENT));
  118. page = __va(pgd_pfn(spgd) << PAGE_SHIFT);
  119. return &page[index];
  120. }
  121. #endif
  122. /*
  123. * This routine then takes the page directory entry returned above, which
  124. * contains the address of the page table entry (PTE) page. It then returns a
  125. * pointer to the PTE entry for the given address.
  126. */
  127. static pte_t *spte_addr(struct lg_cpu *cpu, pgd_t spgd, unsigned long vaddr)
  128. {
  129. #ifdef CONFIG_X86_PAE
  130. pmd_t *pmd = spmd_addr(cpu, spgd, vaddr);
  131. pte_t *page = __va(pmd_pfn(*pmd) << PAGE_SHIFT);
  132. /* You should never call this if the PMD entry wasn't valid */
  133. BUG_ON(!(pmd_flags(*pmd) & _PAGE_PRESENT));
  134. #else
  135. pte_t *page = __va(pgd_pfn(spgd) << PAGE_SHIFT);
  136. /* You should never call this if the PGD entry wasn't valid */
  137. BUG_ON(!(pgd_flags(spgd) & _PAGE_PRESENT));
  138. #endif
  139. return &page[pte_index(vaddr)];
  140. }
  141. /*
  142. * These functions are just like the above two, except they access the Guest
  143. * page tables. Hence they return a Guest address.
  144. */
  145. static unsigned long gpgd_addr(struct lg_cpu *cpu, unsigned long vaddr)
  146. {
  147. unsigned int index = vaddr >> (PGDIR_SHIFT);
  148. return cpu->lg->pgdirs[cpu->cpu_pgd].gpgdir + index * sizeof(pgd_t);
  149. }
  150. #ifdef CONFIG_X86_PAE
  151. /* Follow the PGD to the PMD. */
  152. static unsigned long gpmd_addr(pgd_t gpgd, unsigned long vaddr)
  153. {
  154. unsigned long gpage = pgd_pfn(gpgd) << PAGE_SHIFT;
  155. BUG_ON(!(pgd_flags(gpgd) & _PAGE_PRESENT));
  156. return gpage + pmd_index(vaddr) * sizeof(pmd_t);
  157. }
  158. /* Follow the PMD to the PTE. */
  159. static unsigned long gpte_addr(struct lg_cpu *cpu,
  160. pmd_t gpmd, unsigned long vaddr)
  161. {
  162. unsigned long gpage = pmd_pfn(gpmd) << PAGE_SHIFT;
  163. BUG_ON(!(pmd_flags(gpmd) & _PAGE_PRESENT));
  164. return gpage + pte_index(vaddr) * sizeof(pte_t);
  165. }
  166. #else
  167. /* Follow the PGD to the PTE (no mid-level for !PAE). */
  168. static unsigned long gpte_addr(struct lg_cpu *cpu,
  169. pgd_t gpgd, unsigned long vaddr)
  170. {
  171. unsigned long gpage = pgd_pfn(gpgd) << PAGE_SHIFT;
  172. BUG_ON(!(pgd_flags(gpgd) & _PAGE_PRESENT));
  173. return gpage + pte_index(vaddr) * sizeof(pte_t);
  174. }
  175. #endif
  176. /*:*/
  177. /*M:014
  178. * get_pfn is slow: we could probably try to grab batches of pages here as
  179. * an optimization (ie. pre-faulting).
  180. :*/
  181. /*H:350
  182. * This routine takes a page number given by the Guest and converts it to
  183. * an actual, physical page number. It can fail for several reasons: the
  184. * virtual address might not be mapped by the Launcher, the write flag is set
  185. * and the page is read-only, or the write flag was set and the page was
  186. * shared so had to be copied, but we ran out of memory.
  187. *
  188. * This holds a reference to the page, so release_pte() is careful to put that
  189. * back.
  190. */
  191. static unsigned long get_pfn(unsigned long virtpfn, int write)
  192. {
  193. struct page *page;
  194. /* gup me one page at this address please! */
  195. if (get_user_pages_fast(virtpfn << PAGE_SHIFT, 1, write, &page) == 1)
  196. return page_to_pfn(page);
  197. /* This value indicates failure. */
  198. return -1UL;
  199. }
  200. /*H:340
  201. * Converting a Guest page table entry to a shadow (ie. real) page table
  202. * entry can be a little tricky. The flags are (almost) the same, but the
  203. * Guest PTE contains a virtual page number: the CPU needs the real page
  204. * number.
  205. */
  206. static pte_t gpte_to_spte(struct lg_cpu *cpu, pte_t gpte, int write)
  207. {
  208. unsigned long pfn, base, flags;
  209. /*
  210. * The Guest sets the global flag, because it thinks that it is using
  211. * PGE. We only told it to use PGE so it would tell us whether it was
  212. * flushing a kernel mapping or a userspace mapping. We don't actually
  213. * use the global bit, so throw it away.
  214. */
  215. flags = (pte_flags(gpte) & ~_PAGE_GLOBAL);
  216. /* The Guest's pages are offset inside the Launcher. */
  217. base = (unsigned long)cpu->lg->mem_base / PAGE_SIZE;
  218. /*
  219. * We need a temporary "unsigned long" variable to hold the answer from
  220. * get_pfn(), because it returns 0xFFFFFFFF on failure, which wouldn't
  221. * fit in spte.pfn. get_pfn() finds the real physical number of the
  222. * page, given the virtual number.
  223. */
  224. pfn = get_pfn(base + pte_pfn(gpte), write);
  225. if (pfn == -1UL) {
  226. kill_guest(cpu, "failed to get page %lu", pte_pfn(gpte));
  227. /*
  228. * When we destroy the Guest, we'll go through the shadow page
  229. * tables and release_pte() them. Make sure we don't think
  230. * this one is valid!
  231. */
  232. flags = 0;
  233. }
  234. /* Now we assemble our shadow PTE from the page number and flags. */
  235. return pfn_pte(pfn, __pgprot(flags));
  236. }
  237. /*H:460 And to complete the chain, release_pte() looks like this: */
  238. static void release_pte(pte_t pte)
  239. {
  240. /*
  241. * Remember that get_user_pages_fast() took a reference to the page, in
  242. * get_pfn()? We have to put it back now.
  243. */
  244. if (pte_flags(pte) & _PAGE_PRESENT)
  245. put_page(pte_page(pte));
  246. }
  247. /*:*/
  248. static void check_gpte(struct lg_cpu *cpu, pte_t gpte)
  249. {
  250. if ((pte_flags(gpte) & _PAGE_PSE) ||
  251. pte_pfn(gpte) >= cpu->lg->pfn_limit)
  252. kill_guest(cpu, "bad page table entry");
  253. }
  254. static void check_gpgd(struct lg_cpu *cpu, pgd_t gpgd)
  255. {
  256. if ((pgd_flags(gpgd) & ~CHECK_GPGD_MASK) ||
  257. (pgd_pfn(gpgd) >= cpu->lg->pfn_limit))
  258. kill_guest(cpu, "bad page directory entry");
  259. }
  260. #ifdef CONFIG_X86_PAE
  261. static void check_gpmd(struct lg_cpu *cpu, pmd_t gpmd)
  262. {
  263. if ((pmd_flags(gpmd) & ~_PAGE_TABLE) ||
  264. (pmd_pfn(gpmd) >= cpu->lg->pfn_limit))
  265. kill_guest(cpu, "bad page middle directory entry");
  266. }
  267. #endif
  268. /*H:330
  269. * (i) Looking up a page table entry when the Guest faults.
  270. *
  271. * We saw this call in run_guest(): when we see a page fault in the Guest, we
  272. * come here. That's because we only set up the shadow page tables lazily as
  273. * they're needed, so we get page faults all the time and quietly fix them up
  274. * and return to the Guest without it knowing.
  275. *
  276. * If we fixed up the fault (ie. we mapped the address), this routine returns
  277. * true. Otherwise, it was a real fault and we need to tell the Guest.
  278. */
  279. bool demand_page(struct lg_cpu *cpu, unsigned long vaddr, int errcode)
  280. {
  281. pgd_t gpgd;
  282. pgd_t *spgd;
  283. unsigned long gpte_ptr;
  284. pte_t gpte;
  285. pte_t *spte;
  286. /* Mid level for PAE. */
  287. #ifdef CONFIG_X86_PAE
  288. pmd_t *spmd;
  289. pmd_t gpmd;
  290. #endif
  291. /* First step: get the top-level Guest page table entry. */
  292. gpgd = lgread(cpu, gpgd_addr(cpu, vaddr), pgd_t);
  293. /* Toplevel not present? We can't map it in. */
  294. if (!(pgd_flags(gpgd) & _PAGE_PRESENT))
  295. return false;
  296. /* Now look at the matching shadow entry. */
  297. spgd = spgd_addr(cpu, cpu->cpu_pgd, vaddr);
  298. if (!(pgd_flags(*spgd) & _PAGE_PRESENT)) {
  299. /* No shadow entry: allocate a new shadow PTE page. */
  300. unsigned long ptepage = get_zeroed_page(GFP_KERNEL);
  301. /*
  302. * This is not really the Guest's fault, but killing it is
  303. * simple for this corner case.
  304. */
  305. if (!ptepage) {
  306. kill_guest(cpu, "out of memory allocating pte page");
  307. return false;
  308. }
  309. /* We check that the Guest pgd is OK. */
  310. check_gpgd(cpu, gpgd);
  311. /*
  312. * And we copy the flags to the shadow PGD entry. The page
  313. * number in the shadow PGD is the page we just allocated.
  314. */
  315. set_pgd(spgd, __pgd(__pa(ptepage) | pgd_flags(gpgd)));
  316. }
  317. #ifdef CONFIG_X86_PAE
  318. gpmd = lgread(cpu, gpmd_addr(gpgd, vaddr), pmd_t);
  319. /* Middle level not present? We can't map it in. */
  320. if (!(pmd_flags(gpmd) & _PAGE_PRESENT))
  321. return false;
  322. /* Now look at the matching shadow entry. */
  323. spmd = spmd_addr(cpu, *spgd, vaddr);
  324. if (!(pmd_flags(*spmd) & _PAGE_PRESENT)) {
  325. /* No shadow entry: allocate a new shadow PTE page. */
  326. unsigned long ptepage = get_zeroed_page(GFP_KERNEL);
  327. /*
  328. * This is not really the Guest's fault, but killing it is
  329. * simple for this corner case.
  330. */
  331. if (!ptepage) {
  332. kill_guest(cpu, "out of memory allocating pte page");
  333. return false;
  334. }
  335. /* We check that the Guest pmd is OK. */
  336. check_gpmd(cpu, gpmd);
  337. /*
  338. * And we copy the flags to the shadow PMD entry. The page
  339. * number in the shadow PMD is the page we just allocated.
  340. */
  341. set_pmd(spmd, __pmd(__pa(ptepage) | pmd_flags(gpmd)));
  342. }
  343. /*
  344. * OK, now we look at the lower level in the Guest page table: keep its
  345. * address, because we might update it later.
  346. */
  347. gpte_ptr = gpte_addr(cpu, gpmd, vaddr);
  348. #else
  349. /*
  350. * OK, now we look at the lower level in the Guest page table: keep its
  351. * address, because we might update it later.
  352. */
  353. gpte_ptr = gpte_addr(cpu, gpgd, vaddr);
  354. #endif
  355. /* Read the actual PTE value. */
  356. gpte = lgread(cpu, gpte_ptr, pte_t);
  357. /* If this page isn't in the Guest page tables, we can't page it in. */
  358. if (!(pte_flags(gpte) & _PAGE_PRESENT))
  359. return false;
  360. /*
  361. * Check they're not trying to write to a page the Guest wants
  362. * read-only (bit 2 of errcode == write).
  363. */
  364. if ((errcode & 2) && !(pte_flags(gpte) & _PAGE_RW))
  365. return false;
  366. /* User access to a kernel-only page? (bit 3 == user access) */
  367. if ((errcode & 4) && !(pte_flags(gpte) & _PAGE_USER))
  368. return false;
  369. /*
  370. * Check that the Guest PTE flags are OK, and the page number is below
  371. * the pfn_limit (ie. not mapping the Launcher binary).
  372. */
  373. check_gpte(cpu, gpte);
  374. /* Add the _PAGE_ACCESSED and (for a write) _PAGE_DIRTY flag */
  375. gpte = pte_mkyoung(gpte);
  376. if (errcode & 2)
  377. gpte = pte_mkdirty(gpte);
  378. /* Get the pointer to the shadow PTE entry we're going to set. */
  379. spte = spte_addr(cpu, *spgd, vaddr);
  380. /*
  381. * If there was a valid shadow PTE entry here before, we release it.
  382. * This can happen with a write to a previously read-only entry.
  383. */
  384. release_pte(*spte);
  385. /*
  386. * If this is a write, we insist that the Guest page is writable (the
  387. * final arg to gpte_to_spte()).
  388. */
  389. if (pte_dirty(gpte))
  390. *spte = gpte_to_spte(cpu, gpte, 1);
  391. else
  392. /*
  393. * If this is a read, don't set the "writable" bit in the page
  394. * table entry, even if the Guest says it's writable. That way
  395. * we will come back here when a write does actually occur, so
  396. * we can update the Guest's _PAGE_DIRTY flag.
  397. */
  398. set_pte(spte, gpte_to_spte(cpu, pte_wrprotect(gpte), 0));
  399. /*
  400. * Finally, we write the Guest PTE entry back: we've set the
  401. * _PAGE_ACCESSED and maybe the _PAGE_DIRTY flags.
  402. */
  403. lgwrite(cpu, gpte_ptr, pte_t, gpte);
  404. /*
  405. * The fault is fixed, the page table is populated, the mapping
  406. * manipulated, the result returned and the code complete. A small
  407. * delay and a trace of alliteration are the only indications the Guest
  408. * has that a page fault occurred at all.
  409. */
  410. return true;
  411. }
  412. /*H:360
  413. * (ii) Making sure the Guest stack is mapped.
  414. *
  415. * Remember that direct traps into the Guest need a mapped Guest kernel stack.
  416. * pin_stack_pages() calls us here: we could simply call demand_page(), but as
  417. * we've seen that logic is quite long, and usually the stack pages are already
  418. * mapped, so it's overkill.
  419. *
  420. * This is a quick version which answers the question: is this virtual address
  421. * mapped by the shadow page tables, and is it writable?
  422. */
  423. static bool page_writable(struct lg_cpu *cpu, unsigned long vaddr)
  424. {
  425. pgd_t *spgd;
  426. unsigned long flags;
  427. #ifdef CONFIG_X86_PAE
  428. pmd_t *spmd;
  429. #endif
  430. /* Look at the current top level entry: is it present? */
  431. spgd = spgd_addr(cpu, cpu->cpu_pgd, vaddr);
  432. if (!(pgd_flags(*spgd) & _PAGE_PRESENT))
  433. return false;
  434. #ifdef CONFIG_X86_PAE
  435. spmd = spmd_addr(cpu, *spgd, vaddr);
  436. if (!(pmd_flags(*spmd) & _PAGE_PRESENT))
  437. return false;
  438. #endif
  439. /*
  440. * Check the flags on the pte entry itself: it must be present and
  441. * writable.
  442. */
  443. flags = pte_flags(*(spte_addr(cpu, *spgd, vaddr)));
  444. return (flags & (_PAGE_PRESENT|_PAGE_RW)) == (_PAGE_PRESENT|_PAGE_RW);
  445. }
  446. /*
  447. * So, when pin_stack_pages() asks us to pin a page, we check if it's already
  448. * in the page tables, and if not, we call demand_page() with error code 2
  449. * (meaning "write").
  450. */
  451. void pin_page(struct lg_cpu *cpu, unsigned long vaddr)
  452. {
  453. if (!page_writable(cpu, vaddr) && !demand_page(cpu, vaddr, 2))
  454. kill_guest(cpu, "bad stack page %#lx", vaddr);
  455. }
  456. /*:*/
  457. #ifdef CONFIG_X86_PAE
  458. static void release_pmd(pmd_t *spmd)
  459. {
  460. /* If the entry's not present, there's nothing to release. */
  461. if (pmd_flags(*spmd) & _PAGE_PRESENT) {
  462. unsigned int i;
  463. pte_t *ptepage = __va(pmd_pfn(*spmd) << PAGE_SHIFT);
  464. /* For each entry in the page, we might need to release it. */
  465. for (i = 0; i < PTRS_PER_PTE; i++)
  466. release_pte(ptepage[i]);
  467. /* Now we can free the page of PTEs */
  468. free_page((long)ptepage);
  469. /* And zero out the PMD entry so we never release it twice. */
  470. set_pmd(spmd, __pmd(0));
  471. }
  472. }
  473. static void release_pgd(pgd_t *spgd)
  474. {
  475. /* If the entry's not present, there's nothing to release. */
  476. if (pgd_flags(*spgd) & _PAGE_PRESENT) {
  477. unsigned int i;
  478. pmd_t *pmdpage = __va(pgd_pfn(*spgd) << PAGE_SHIFT);
  479. for (i = 0; i < PTRS_PER_PMD; i++)
  480. release_pmd(&pmdpage[i]);
  481. /* Now we can free the page of PMDs */
  482. free_page((long)pmdpage);
  483. /* And zero out the PGD entry so we never release it twice. */
  484. set_pgd(spgd, __pgd(0));
  485. }
  486. }
  487. #else /* !CONFIG_X86_PAE */
  488. /*H:450
  489. * If we chase down the release_pgd() code, the non-PAE version looks like
  490. * this. The PAE version is almost identical, but instead of calling
  491. * release_pte it calls release_pmd(), which looks much like this.
  492. */
  493. static void release_pgd(pgd_t *spgd)
  494. {
  495. /* If the entry's not present, there's nothing to release. */
  496. if (pgd_flags(*spgd) & _PAGE_PRESENT) {
  497. unsigned int i;
  498. /*
  499. * Converting the pfn to find the actual PTE page is easy: turn
  500. * the page number into a physical address, then convert to a
  501. * virtual address (easy for kernel pages like this one).
  502. */
  503. pte_t *ptepage = __va(pgd_pfn(*spgd) << PAGE_SHIFT);
  504. /* For each entry in the page, we might need to release it. */
  505. for (i = 0; i < PTRS_PER_PTE; i++)
  506. release_pte(ptepage[i]);
  507. /* Now we can free the page of PTEs */
  508. free_page((long)ptepage);
  509. /* And zero out the PGD entry so we never release it twice. */
  510. *spgd = __pgd(0);
  511. }
  512. }
  513. #endif
  514. /*H:445
  515. * We saw flush_user_mappings() twice: once from the flush_user_mappings()
  516. * hypercall and once in new_pgdir() when we re-used a top-level pgdir page.
  517. * It simply releases every PTE page from 0 up to the Guest's kernel address.
  518. */
  519. static void flush_user_mappings(struct lguest *lg, int idx)
  520. {
  521. unsigned int i;
  522. /* Release every pgd entry up to the kernel's address. */
  523. for (i = 0; i < pgd_index(lg->kernel_address); i++)
  524. release_pgd(lg->pgdirs[idx].pgdir + i);
  525. }
  526. /*H:440
  527. * (v) Flushing (throwing away) page tables,
  528. *
  529. * The Guest has a hypercall to throw away the page tables: it's used when a
  530. * large number of mappings have been changed.
  531. */
  532. void guest_pagetable_flush_user(struct lg_cpu *cpu)
  533. {
  534. /* Drop the userspace part of the current page table. */
  535. flush_user_mappings(cpu->lg, cpu->cpu_pgd);
  536. }
  537. /*:*/
  538. /* We walk down the guest page tables to get a guest-physical address */
  539. unsigned long guest_pa(struct lg_cpu *cpu, unsigned long vaddr)
  540. {
  541. pgd_t gpgd;
  542. pte_t gpte;
  543. #ifdef CONFIG_X86_PAE
  544. pmd_t gpmd;
  545. #endif
  546. /* First step: get the top-level Guest page table entry. */
  547. gpgd = lgread(cpu, gpgd_addr(cpu, vaddr), pgd_t);
  548. /* Toplevel not present? We can't map it in. */
  549. if (!(pgd_flags(gpgd) & _PAGE_PRESENT)) {
  550. kill_guest(cpu, "Bad address %#lx", vaddr);
  551. return -1UL;
  552. }
  553. #ifdef CONFIG_X86_PAE
  554. gpmd = lgread(cpu, gpmd_addr(gpgd, vaddr), pmd_t);
  555. if (!(pmd_flags(gpmd) & _PAGE_PRESENT))
  556. kill_guest(cpu, "Bad address %#lx", vaddr);
  557. gpte = lgread(cpu, gpte_addr(cpu, gpmd, vaddr), pte_t);
  558. #else
  559. gpte = lgread(cpu, gpte_addr(cpu, gpgd, vaddr), pte_t);
  560. #endif
  561. if (!(pte_flags(gpte) & _PAGE_PRESENT))
  562. kill_guest(cpu, "Bad address %#lx", vaddr);
  563. return pte_pfn(gpte) * PAGE_SIZE | (vaddr & ~PAGE_MASK);
  564. }
  565. /*
  566. * We keep several page tables. This is a simple routine to find the page
  567. * table (if any) corresponding to this top-level address the Guest has given
  568. * us.
  569. */
  570. static unsigned int find_pgdir(struct lguest *lg, unsigned long pgtable)
  571. {
  572. unsigned int i;
  573. for (i = 0; i < ARRAY_SIZE(lg->pgdirs); i++)
  574. if (lg->pgdirs[i].pgdir && lg->pgdirs[i].gpgdir == pgtable)
  575. break;
  576. return i;
  577. }
  578. /*H:435
  579. * And this is us, creating the new page directory. If we really do
  580. * allocate a new one (and so the kernel parts are not there), we set
  581. * blank_pgdir.
  582. */
  583. static unsigned int new_pgdir(struct lg_cpu *cpu,
  584. unsigned long gpgdir,
  585. int *blank_pgdir)
  586. {
  587. unsigned int next;
  588. #ifdef CONFIG_X86_PAE
  589. pmd_t *pmd_table;
  590. #endif
  591. /*
  592. * We pick one entry at random to throw out. Choosing the Least
  593. * Recently Used might be better, but this is easy.
  594. */
  595. next = random32() % ARRAY_SIZE(cpu->lg->pgdirs);
  596. /* If it's never been allocated at all before, try now. */
  597. if (!cpu->lg->pgdirs[next].pgdir) {
  598. cpu->lg->pgdirs[next].pgdir =
  599. (pgd_t *)get_zeroed_page(GFP_KERNEL);
  600. /* If the allocation fails, just keep using the one we have */
  601. if (!cpu->lg->pgdirs[next].pgdir)
  602. next = cpu->cpu_pgd;
  603. else {
  604. #ifdef CONFIG_X86_PAE
  605. /*
  606. * In PAE mode, allocate a pmd page and populate the
  607. * last pgd entry.
  608. */
  609. pmd_table = (pmd_t *)get_zeroed_page(GFP_KERNEL);
  610. if (!pmd_table) {
  611. free_page((long)cpu->lg->pgdirs[next].pgdir);
  612. set_pgd(cpu->lg->pgdirs[next].pgdir, __pgd(0));
  613. next = cpu->cpu_pgd;
  614. } else {
  615. set_pgd(cpu->lg->pgdirs[next].pgdir +
  616. SWITCHER_PGD_INDEX,
  617. __pgd(__pa(pmd_table) | _PAGE_PRESENT));
  618. /*
  619. * This is a blank page, so there are no kernel
  620. * mappings: caller must map the stack!
  621. */
  622. *blank_pgdir = 1;
  623. }
  624. #else
  625. *blank_pgdir = 1;
  626. #endif
  627. }
  628. }
  629. /* Record which Guest toplevel this shadows. */
  630. cpu->lg->pgdirs[next].gpgdir = gpgdir;
  631. /* Release all the non-kernel mappings. */
  632. flush_user_mappings(cpu->lg, next);
  633. return next;
  634. }
  635. /*H:430
  636. * (iv) Switching page tables
  637. *
  638. * Now we've seen all the page table setting and manipulation, let's see
  639. * what happens when the Guest changes page tables (ie. changes the top-level
  640. * pgdir). This occurs on almost every context switch.
  641. */
  642. void guest_new_pagetable(struct lg_cpu *cpu, unsigned long pgtable)
  643. {
  644. int newpgdir, repin = 0;
  645. /* Look to see if we have this one already. */
  646. newpgdir = find_pgdir(cpu->lg, pgtable);
  647. /*
  648. * If not, we allocate or mug an existing one: if it's a fresh one,
  649. * repin gets set to 1.
  650. */
  651. if (newpgdir == ARRAY_SIZE(cpu->lg->pgdirs))
  652. newpgdir = new_pgdir(cpu, pgtable, &repin);
  653. /* Change the current pgd index to the new one. */
  654. cpu->cpu_pgd = newpgdir;
  655. /* If it was completely blank, we map in the Guest kernel stack */
  656. if (repin)
  657. pin_stack_pages(cpu);
  658. }
  659. /*H:470
  660. * Finally, a routine which throws away everything: all PGD entries in all
  661. * the shadow page tables, including the Guest's kernel mappings. This is used
  662. * when we destroy the Guest.
  663. */
  664. static void release_all_pagetables(struct lguest *lg)
  665. {
  666. unsigned int i, j;
  667. /* Every shadow pagetable this Guest has */
  668. for (i = 0; i < ARRAY_SIZE(lg->pgdirs); i++)
  669. if (lg->pgdirs[i].pgdir) {
  670. #ifdef CONFIG_X86_PAE
  671. pgd_t *spgd;
  672. pmd_t *pmdpage;
  673. unsigned int k;
  674. /* Get the last pmd page. */
  675. spgd = lg->pgdirs[i].pgdir + SWITCHER_PGD_INDEX;
  676. pmdpage = __va(pgd_pfn(*spgd) << PAGE_SHIFT);
  677. /*
  678. * And release the pmd entries of that pmd page,
  679. * except for the switcher pmd.
  680. */
  681. for (k = 0; k < SWITCHER_PMD_INDEX; k++)
  682. release_pmd(&pmdpage[k]);
  683. #endif
  684. /* Every PGD entry except the Switcher at the top */
  685. for (j = 0; j < SWITCHER_PGD_INDEX; j++)
  686. release_pgd(lg->pgdirs[i].pgdir + j);
  687. }
  688. }
  689. /*
  690. * We also throw away everything when a Guest tells us it's changed a kernel
  691. * mapping. Since kernel mappings are in every page table, it's easiest to
  692. * throw them all away. This traps the Guest in amber for a while as
  693. * everything faults back in, but it's rare.
  694. */
  695. void guest_pagetable_clear_all(struct lg_cpu *cpu)
  696. {
  697. release_all_pagetables(cpu->lg);
  698. /* We need the Guest kernel stack mapped again. */
  699. pin_stack_pages(cpu);
  700. }
  701. /*:*/
  702. /*M:009
  703. * Since we throw away all mappings when a kernel mapping changes, our
  704. * performance sucks for guests using highmem. In fact, a guest with
  705. * PAGE_OFFSET 0xc0000000 (the default) and more than about 700MB of RAM is
  706. * usually slower than a Guest with less memory.
  707. *
  708. * This, of course, cannot be fixed. It would take some kind of... well, I
  709. * don't know, but the term "puissant code-fu" comes to mind.
  710. :*/
  711. /*H:420
  712. * This is the routine which actually sets the page table entry for then
  713. * "idx"'th shadow page table.
  714. *
  715. * Normally, we can just throw out the old entry and replace it with 0: if they
  716. * use it demand_page() will put the new entry in. We need to do this anyway:
  717. * The Guest expects _PAGE_ACCESSED to be set on its PTE the first time a page
  718. * is read from, and _PAGE_DIRTY when it's written to.
  719. *
  720. * But Avi Kivity pointed out that most Operating Systems (Linux included) set
  721. * these bits on PTEs immediately anyway. This is done to save the CPU from
  722. * having to update them, but it helps us the same way: if they set
  723. * _PAGE_ACCESSED then we can put a read-only PTE entry in immediately, and if
  724. * they set _PAGE_DIRTY then we can put a writable PTE entry in immediately.
  725. */
  726. static void do_set_pte(struct lg_cpu *cpu, int idx,
  727. unsigned long vaddr, pte_t gpte)
  728. {
  729. /* Look up the matching shadow page directory entry. */
  730. pgd_t *spgd = spgd_addr(cpu, idx, vaddr);
  731. #ifdef CONFIG_X86_PAE
  732. pmd_t *spmd;
  733. #endif
  734. /* If the top level isn't present, there's no entry to update. */
  735. if (pgd_flags(*spgd) & _PAGE_PRESENT) {
  736. #ifdef CONFIG_X86_PAE
  737. spmd = spmd_addr(cpu, *spgd, vaddr);
  738. if (pmd_flags(*spmd) & _PAGE_PRESENT) {
  739. #endif
  740. /* Otherwise, start by releasing the existing entry. */
  741. pte_t *spte = spte_addr(cpu, *spgd, vaddr);
  742. release_pte(*spte);
  743. /*
  744. * If they're setting this entry as dirty or accessed,
  745. * we might as well put that entry they've given us in
  746. * now. This shaves 10% off a copy-on-write
  747. * micro-benchmark.
  748. */
  749. if (pte_flags(gpte) & (_PAGE_DIRTY | _PAGE_ACCESSED)) {
  750. check_gpte(cpu, gpte);
  751. set_pte(spte,
  752. gpte_to_spte(cpu, gpte,
  753. pte_flags(gpte) & _PAGE_DIRTY));
  754. } else {
  755. /*
  756. * Otherwise kill it and we can demand_page()
  757. * it in later.
  758. */
  759. set_pte(spte, __pte(0));
  760. }
  761. #ifdef CONFIG_X86_PAE
  762. }
  763. #endif
  764. }
  765. }
  766. /*H:410
  767. * Updating a PTE entry is a little trickier.
  768. *
  769. * We keep track of several different page tables (the Guest uses one for each
  770. * process, so it makes sense to cache at least a few). Each of these have
  771. * identical kernel parts: ie. every mapping above PAGE_OFFSET is the same for
  772. * all processes. So when the page table above that address changes, we update
  773. * all the page tables, not just the current one. This is rare.
  774. *
  775. * The benefit is that when we have to track a new page table, we can keep all
  776. * the kernel mappings. This speeds up context switch immensely.
  777. */
  778. void guest_set_pte(struct lg_cpu *cpu,
  779. unsigned long gpgdir, unsigned long vaddr, pte_t gpte)
  780. {
  781. /*
  782. * Kernel mappings must be changed on all top levels. Slow, but doesn't
  783. * happen often.
  784. */
  785. if (vaddr >= cpu->lg->kernel_address) {
  786. unsigned int i;
  787. for (i = 0; i < ARRAY_SIZE(cpu->lg->pgdirs); i++)
  788. if (cpu->lg->pgdirs[i].pgdir)
  789. do_set_pte(cpu, i, vaddr, gpte);
  790. } else {
  791. /* Is this page table one we have a shadow for? */
  792. int pgdir = find_pgdir(cpu->lg, gpgdir);
  793. if (pgdir != ARRAY_SIZE(cpu->lg->pgdirs))
  794. /* If so, do the update. */
  795. do_set_pte(cpu, pgdir, vaddr, gpte);
  796. }
  797. }
  798. /*H:400
  799. * (iii) Setting up a page table entry when the Guest tells us one has changed.
  800. *
  801. * Just like we did in interrupts_and_traps.c, it makes sense for us to deal
  802. * with the other side of page tables while we're here: what happens when the
  803. * Guest asks for a page table to be updated?
  804. *
  805. * We already saw that demand_page() will fill in the shadow page tables when
  806. * needed, so we can simply remove shadow page table entries whenever the Guest
  807. * tells us they've changed. When the Guest tries to use the new entry it will
  808. * fault and demand_page() will fix it up.
  809. *
  810. * So with that in mind here's our code to update a (top-level) PGD entry:
  811. */
  812. void guest_set_pgd(struct lguest *lg, unsigned long gpgdir, u32 idx)
  813. {
  814. int pgdir;
  815. if (idx >= SWITCHER_PGD_INDEX)
  816. return;
  817. /* If they're talking about a page table we have a shadow for... */
  818. pgdir = find_pgdir(lg, gpgdir);
  819. if (pgdir < ARRAY_SIZE(lg->pgdirs))
  820. /* ... throw it away. */
  821. release_pgd(lg->pgdirs[pgdir].pgdir + idx);
  822. }
  823. #ifdef CONFIG_X86_PAE
  824. /* For setting a mid-level, we just throw everything away. It's easy. */
  825. void guest_set_pmd(struct lguest *lg, unsigned long pmdp, u32 idx)
  826. {
  827. guest_pagetable_clear_all(&lg->cpus[0]);
  828. }
  829. #endif
  830. /*H:505
  831. * To get through boot, we construct simple identity page mappings (which
  832. * set virtual == physical) and linear mappings which will get the Guest far
  833. * enough into the boot to create its own. The linear mapping means we
  834. * simplify the Guest boot, but it makes assumptions about their PAGE_OFFSET,
  835. * as you'll see.
  836. *
  837. * We lay them out of the way, just below the initrd (which is why we need to
  838. * know its size here).
  839. */
  840. static unsigned long setup_pagetables(struct lguest *lg,
  841. unsigned long mem,
  842. unsigned long initrd_size)
  843. {
  844. pgd_t __user *pgdir;
  845. pte_t __user *linear;
  846. unsigned long mem_base = (unsigned long)lg->mem_base;
  847. unsigned int mapped_pages, i, linear_pages;
  848. #ifdef CONFIG_X86_PAE
  849. pmd_t __user *pmds;
  850. unsigned int j;
  851. pgd_t pgd;
  852. pmd_t pmd;
  853. #else
  854. unsigned int phys_linear;
  855. #endif
  856. /*
  857. * We have mapped_pages frames to map, so we need linear_pages page
  858. * tables to map them.
  859. */
  860. mapped_pages = mem / PAGE_SIZE;
  861. linear_pages = (mapped_pages + PTRS_PER_PTE - 1) / PTRS_PER_PTE;
  862. /* We put the toplevel page directory page at the top of memory. */
  863. pgdir = (pgd_t *)(mem + mem_base - initrd_size - PAGE_SIZE);
  864. /* Now we use the next linear_pages pages as pte pages */
  865. linear = (void *)pgdir - linear_pages * PAGE_SIZE;
  866. #ifdef CONFIG_X86_PAE
  867. /*
  868. * And the single mid page goes below that. We only use one, but
  869. * that's enough to map 1G, which definitely gets us through boot.
  870. */
  871. pmds = (void *)linear - PAGE_SIZE;
  872. #endif
  873. /*
  874. * Linear mapping is easy: put every page's address into the
  875. * mapping in order.
  876. */
  877. for (i = 0; i < mapped_pages; i++) {
  878. pte_t pte;
  879. pte = pfn_pte(i, __pgprot(_PAGE_PRESENT|_PAGE_RW|_PAGE_USER));
  880. if (copy_to_user(&linear[i], &pte, sizeof(pte)) != 0)
  881. return -EFAULT;
  882. }
  883. #ifdef CONFIG_X86_PAE
  884. /*
  885. * Make the Guest PMD entries point to the corresponding place in the
  886. * linear mapping (up to one page worth of PMD).
  887. */
  888. for (i = j = 0; i < mapped_pages && j < PTRS_PER_PMD;
  889. i += PTRS_PER_PTE, j++) {
  890. pmd = pfn_pmd(((unsigned long)&linear[i] - mem_base)/PAGE_SIZE,
  891. __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_USER));
  892. if (copy_to_user(&pmds[j], &pmd, sizeof(pmd)) != 0)
  893. return -EFAULT;
  894. }
  895. /* One PGD entry, pointing to that PMD page. */
  896. pgd = __pgd(((unsigned long)pmds - mem_base) | _PAGE_PRESENT);
  897. /* Copy it in as the first PGD entry (ie. addresses 0-1G). */
  898. if (copy_to_user(&pgdir[0], &pgd, sizeof(pgd)) != 0)
  899. return -EFAULT;
  900. /*
  901. * And the other PGD entry to make the linear mapping at PAGE_OFFSET
  902. */
  903. if (copy_to_user(&pgdir[KERNEL_PGD_BOUNDARY], &pgd, sizeof(pgd)))
  904. return -EFAULT;
  905. #else
  906. /*
  907. * The top level points to the linear page table pages above.
  908. * We setup the identity and linear mappings here.
  909. */
  910. phys_linear = (unsigned long)linear - mem_base;
  911. for (i = 0; i < mapped_pages; i += PTRS_PER_PTE) {
  912. pgd_t pgd;
  913. /*
  914. * Create a PGD entry which points to the right part of the
  915. * linear PTE pages.
  916. */
  917. pgd = __pgd((phys_linear + i * sizeof(pte_t)) |
  918. (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER));
  919. /*
  920. * Copy it into the PGD page at 0 and PAGE_OFFSET.
  921. */
  922. if (copy_to_user(&pgdir[i / PTRS_PER_PTE], &pgd, sizeof(pgd))
  923. || copy_to_user(&pgdir[pgd_index(PAGE_OFFSET)
  924. + i / PTRS_PER_PTE],
  925. &pgd, sizeof(pgd)))
  926. return -EFAULT;
  927. }
  928. #endif
  929. /*
  930. * We return the top level (guest-physical) address: we remember where
  931. * this is to write it into lguest_data when the Guest initializes.
  932. */
  933. return (unsigned long)pgdir - mem_base;
  934. }
  935. /*H:500
  936. * (vii) Setting up the page tables initially.
  937. *
  938. * When a Guest is first created, the Launcher tells us where the toplevel of
  939. * its first page table is. We set some things up here:
  940. */
  941. int init_guest_pagetable(struct lguest *lg)
  942. {
  943. u64 mem;
  944. u32 initrd_size;
  945. struct boot_params __user *boot = (struct boot_params *)lg->mem_base;
  946. #ifdef CONFIG_X86_PAE
  947. pgd_t *pgd;
  948. pmd_t *pmd_table;
  949. #endif
  950. /*
  951. * Get the Guest memory size and the ramdisk size from the boot header
  952. * located at lg->mem_base (Guest address 0).
  953. */
  954. if (copy_from_user(&mem, &boot->e820_map[0].size, sizeof(mem))
  955. || get_user(initrd_size, &boot->hdr.ramdisk_size))
  956. return -EFAULT;
  957. /*
  958. * We start on the first shadow page table, and give it a blank PGD
  959. * page.
  960. */
  961. lg->pgdirs[0].gpgdir = setup_pagetables(lg, mem, initrd_size);
  962. if (IS_ERR_VALUE(lg->pgdirs[0].gpgdir))
  963. return lg->pgdirs[0].gpgdir;
  964. lg->pgdirs[0].pgdir = (pgd_t *)get_zeroed_page(GFP_KERNEL);
  965. if (!lg->pgdirs[0].pgdir)
  966. return -ENOMEM;
  967. #ifdef CONFIG_X86_PAE
  968. /* For PAE, we also create the initial mid-level. */
  969. pgd = lg->pgdirs[0].pgdir;
  970. pmd_table = (pmd_t *) get_zeroed_page(GFP_KERNEL);
  971. if (!pmd_table)
  972. return -ENOMEM;
  973. set_pgd(pgd + SWITCHER_PGD_INDEX,
  974. __pgd(__pa(pmd_table) | _PAGE_PRESENT));
  975. #endif
  976. /* This is the current page table. */
  977. lg->cpus[0].cpu_pgd = 0;
  978. return 0;
  979. }
  980. /*H:508 When the Guest calls LHCALL_LGUEST_INIT we do more setup. */
  981. void page_table_guest_data_init(struct lg_cpu *cpu)
  982. {
  983. /* We get the kernel address: above this is all kernel memory. */
  984. if (get_user(cpu->lg->kernel_address,
  985. &cpu->lg->lguest_data->kernel_address)
  986. /*
  987. * We tell the Guest that it can't use the top 2 or 4 MB
  988. * of virtual addresses used by the Switcher.
  989. */
  990. || put_user(RESERVE_MEM * 1024 * 1024,
  991. &cpu->lg->lguest_data->reserve_mem)
  992. || put_user(cpu->lg->pgdirs[0].gpgdir,
  993. &cpu->lg->lguest_data->pgdir))
  994. kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
  995. /*
  996. * In flush_user_mappings() we loop from 0 to
  997. * "pgd_index(lg->kernel_address)". This assumes it won't hit the
  998. * Switcher mappings, so check that now.
  999. */
  1000. #ifdef CONFIG_X86_PAE
  1001. if (pgd_index(cpu->lg->kernel_address) == SWITCHER_PGD_INDEX &&
  1002. pmd_index(cpu->lg->kernel_address) == SWITCHER_PMD_INDEX)
  1003. #else
  1004. if (pgd_index(cpu->lg->kernel_address) >= SWITCHER_PGD_INDEX)
  1005. #endif
  1006. kill_guest(cpu, "bad kernel address %#lx",
  1007. cpu->lg->kernel_address);
  1008. }
  1009. /* When a Guest dies, our cleanup is fairly simple. */
  1010. void free_guest_pagetable(struct lguest *lg)
  1011. {
  1012. unsigned int i;
  1013. /* Throw away all page table pages. */
  1014. release_all_pagetables(lg);
  1015. /* Now free the top levels: free_page() can handle 0 just fine. */
  1016. for (i = 0; i < ARRAY_SIZE(lg->pgdirs); i++)
  1017. free_page((long)lg->pgdirs[i].pgdir);
  1018. }
  1019. /*H:480
  1020. * (vi) Mapping the Switcher when the Guest is about to run.
  1021. *
  1022. * The Switcher and the two pages for this CPU need to be visible in the
  1023. * Guest (and not the pages for other CPUs). We have the appropriate PTE pages
  1024. * for each CPU already set up, we just need to hook them in now we know which
  1025. * Guest is about to run on this CPU.
  1026. */
  1027. void map_switcher_in_guest(struct lg_cpu *cpu, struct lguest_pages *pages)
  1028. {
  1029. pte_t *switcher_pte_page = __get_cpu_var(switcher_pte_pages);
  1030. pte_t regs_pte;
  1031. #ifdef CONFIG_X86_PAE
  1032. pmd_t switcher_pmd;
  1033. pmd_t *pmd_table;
  1034. switcher_pmd = pfn_pmd(__pa(switcher_pte_page) >> PAGE_SHIFT,
  1035. PAGE_KERNEL_EXEC);
  1036. /* Figure out where the pmd page is, by reading the PGD, and converting
  1037. * it to a virtual address. */
  1038. pmd_table = __va(pgd_pfn(cpu->lg->
  1039. pgdirs[cpu->cpu_pgd].pgdir[SWITCHER_PGD_INDEX])
  1040. << PAGE_SHIFT);
  1041. /* Now write it into the shadow page table. */
  1042. set_pmd(&pmd_table[SWITCHER_PMD_INDEX], switcher_pmd);
  1043. #else
  1044. pgd_t switcher_pgd;
  1045. /*
  1046. * Make the last PGD entry for this Guest point to the Switcher's PTE
  1047. * page for this CPU (with appropriate flags).
  1048. */
  1049. switcher_pgd = __pgd(__pa(switcher_pte_page) | __PAGE_KERNEL_EXEC);
  1050. cpu->lg->pgdirs[cpu->cpu_pgd].pgdir[SWITCHER_PGD_INDEX] = switcher_pgd;
  1051. #endif
  1052. /*
  1053. * We also change the Switcher PTE page. When we're running the Guest,
  1054. * we want the Guest's "regs" page to appear where the first Switcher
  1055. * page for this CPU is. This is an optimization: when the Switcher
  1056. * saves the Guest registers, it saves them into the first page of this
  1057. * CPU's "struct lguest_pages": if we make sure the Guest's register
  1058. * page is already mapped there, we don't have to copy them out
  1059. * again.
  1060. */
  1061. regs_pte = pfn_pte(__pa(cpu->regs_page) >> PAGE_SHIFT, PAGE_KERNEL);
  1062. set_pte(&switcher_pte_page[pte_index((unsigned long)pages)], regs_pte);
  1063. }
  1064. /*:*/
  1065. static void free_switcher_pte_pages(void)
  1066. {
  1067. unsigned int i;
  1068. for_each_possible_cpu(i)
  1069. free_page((long)switcher_pte_page(i));
  1070. }
  1071. /*H:520
  1072. * Setting up the Switcher PTE page for given CPU is fairly easy, given
  1073. * the CPU number and the "struct page"s for the Switcher code itself.
  1074. *
  1075. * Currently the Switcher is less than a page long, so "pages" is always 1.
  1076. */
  1077. static __init void populate_switcher_pte_page(unsigned int cpu,
  1078. struct page *switcher_page[],
  1079. unsigned int pages)
  1080. {
  1081. unsigned int i;
  1082. pte_t *pte = switcher_pte_page(cpu);
  1083. /* The first entries are easy: they map the Switcher code. */
  1084. for (i = 0; i < pages; i++) {
  1085. set_pte(&pte[i], mk_pte(switcher_page[i],
  1086. __pgprot(_PAGE_PRESENT|_PAGE_ACCESSED)));
  1087. }
  1088. /* The only other thing we map is this CPU's pair of pages. */
  1089. i = pages + cpu*2;
  1090. /* First page (Guest registers) is writable from the Guest */
  1091. set_pte(&pte[i], pfn_pte(page_to_pfn(switcher_page[i]),
  1092. __pgprot(_PAGE_PRESENT|_PAGE_ACCESSED|_PAGE_RW)));
  1093. /*
  1094. * The second page contains the "struct lguest_ro_state", and is
  1095. * read-only.
  1096. */
  1097. set_pte(&pte[i+1], pfn_pte(page_to_pfn(switcher_page[i+1]),
  1098. __pgprot(_PAGE_PRESENT|_PAGE_ACCESSED)));
  1099. }
  1100. /*
  1101. * We've made it through the page table code. Perhaps our tired brains are
  1102. * still processing the details, or perhaps we're simply glad it's over.
  1103. *
  1104. * If nothing else, note that all this complexity in juggling shadow page tables
  1105. * in sync with the Guest's page tables is for one reason: for most Guests this
  1106. * page table dance determines how bad performance will be. This is why Xen
  1107. * uses exotic direct Guest pagetable manipulation, and why both Intel and AMD
  1108. * have implemented shadow page table support directly into hardware.
  1109. *
  1110. * There is just one file remaining in the Host.
  1111. */
  1112. /*H:510
  1113. * At boot or module load time, init_pagetables() allocates and populates
  1114. * the Switcher PTE page for each CPU.
  1115. */
  1116. __init int init_pagetables(struct page **switcher_page, unsigned int pages)
  1117. {
  1118. unsigned int i;
  1119. for_each_possible_cpu(i) {
  1120. switcher_pte_page(i) = (pte_t *)get_zeroed_page(GFP_KERNEL);
  1121. if (!switcher_pte_page(i)) {
  1122. free_switcher_pte_pages();
  1123. return -ENOMEM;
  1124. }
  1125. populate_switcher_pte_page(i, switcher_page, pages);
  1126. }
  1127. return 0;
  1128. }
  1129. /*:*/
  1130. /* Cleaning up simply involves freeing the PTE page for each CPU. */
  1131. void free_pagetables(void)
  1132. {
  1133. free_switcher_pte_pages();
  1134. }