page_tables.c 38 KB

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