page_tables.c 37 KB

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