page_tables.c 37 KB

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