page_tables.c 36 KB

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