page_tables.c 39 KB

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