page_tables.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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. return -1UL;
  599. }
  600. gpte = lgread(cpu, gpte_addr(cpu, gpmd, vaddr), pte_t);
  601. #else
  602. gpte = lgread(cpu, gpte_addr(cpu, gpgd, vaddr), pte_t);
  603. #endif
  604. if (!(pte_flags(gpte) & _PAGE_PRESENT))
  605. kill_guest(cpu, "Bad address %#lx", vaddr);
  606. return pte_pfn(gpte) * PAGE_SIZE | (vaddr & ~PAGE_MASK);
  607. }
  608. /*
  609. * We keep several page tables. This is a simple routine to find the page
  610. * table (if any) corresponding to this top-level address the Guest has given
  611. * us.
  612. */
  613. static unsigned int find_pgdir(struct lguest *lg, unsigned long pgtable)
  614. {
  615. unsigned int i;
  616. for (i = 0; i < ARRAY_SIZE(lg->pgdirs); i++)
  617. if (lg->pgdirs[i].pgdir && lg->pgdirs[i].gpgdir == pgtable)
  618. break;
  619. return i;
  620. }
  621. /*H:435
  622. * And this is us, creating the new page directory. If we really do
  623. * allocate a new one (and so the kernel parts are not there), we set
  624. * blank_pgdir.
  625. */
  626. static unsigned int new_pgdir(struct lg_cpu *cpu,
  627. unsigned long gpgdir,
  628. int *blank_pgdir)
  629. {
  630. unsigned int next;
  631. /*
  632. * We pick one entry at random to throw out. Choosing the Least
  633. * Recently Used might be better, but this is easy.
  634. */
  635. next = prandom_u32() % ARRAY_SIZE(cpu->lg->pgdirs);
  636. /* If it's never been allocated at all before, try now. */
  637. if (!cpu->lg->pgdirs[next].pgdir) {
  638. cpu->lg->pgdirs[next].pgdir =
  639. (pgd_t *)get_zeroed_page(GFP_KERNEL);
  640. /* If the allocation fails, just keep using the one we have */
  641. if (!cpu->lg->pgdirs[next].pgdir)
  642. next = cpu->cpu_pgd;
  643. else {
  644. /*
  645. * This is a blank page, so there are no kernel
  646. * mappings: caller must map the stack!
  647. */
  648. *blank_pgdir = 1;
  649. }
  650. }
  651. /* Record which Guest toplevel this shadows. */
  652. cpu->lg->pgdirs[next].gpgdir = gpgdir;
  653. /* Release all the non-kernel mappings. */
  654. flush_user_mappings(cpu->lg, next);
  655. /* This hasn't run on any CPU at all. */
  656. cpu->lg->pgdirs[next].last_host_cpu = -1;
  657. return next;
  658. }
  659. /*H:501
  660. * We do need the Switcher code mapped at all times, so we allocate that
  661. * part of the Guest page table here. We map the Switcher code immediately,
  662. * but defer mapping of the guest register page and IDT/LDT etc page until
  663. * just before we run the guest in map_switcher_in_guest().
  664. *
  665. * We *could* do this setup in map_switcher_in_guest(), but at that point
  666. * we've interrupts disabled, and allocating pages like that is fraught: we
  667. * can't sleep if we need to free up some memory.
  668. */
  669. static bool allocate_switcher_mapping(struct lg_cpu *cpu)
  670. {
  671. int i;
  672. for (i = 0; i < TOTAL_SWITCHER_PAGES; i++) {
  673. pte_t *pte = find_spte(cpu, switcher_addr + i * PAGE_SIZE, true,
  674. CHECK_GPGD_MASK, _PAGE_TABLE);
  675. if (!pte)
  676. return false;
  677. /*
  678. * Map the switcher page if not already there. It might
  679. * already be there because we call allocate_switcher_mapping()
  680. * in guest_set_pgd() just in case it did discard our Switcher
  681. * mapping, but it probably didn't.
  682. */
  683. if (i == 0 && !(pte_flags(*pte) & _PAGE_PRESENT)) {
  684. /* Get a reference to the Switcher page. */
  685. get_page(lg_switcher_pages[0]);
  686. /* Create a read-only, exectuable, kernel-style PTE */
  687. set_pte(pte,
  688. mk_pte(lg_switcher_pages[0], PAGE_KERNEL_RX));
  689. }
  690. }
  691. cpu->lg->pgdirs[cpu->cpu_pgd].switcher_mapped = true;
  692. return true;
  693. }
  694. /*H:470
  695. * Finally, a routine which throws away everything: all PGD entries in all
  696. * the shadow page tables, including the Guest's kernel mappings. This is used
  697. * when we destroy the Guest.
  698. */
  699. static void release_all_pagetables(struct lguest *lg)
  700. {
  701. unsigned int i, j;
  702. /* Every shadow pagetable this Guest has */
  703. for (i = 0; i < ARRAY_SIZE(lg->pgdirs); i++) {
  704. if (!lg->pgdirs[i].pgdir)
  705. continue;
  706. /* Every PGD entry. */
  707. for (j = 0; j < PTRS_PER_PGD; j++)
  708. release_pgd(lg->pgdirs[i].pgdir + j);
  709. lg->pgdirs[i].switcher_mapped = false;
  710. lg->pgdirs[i].last_host_cpu = -1;
  711. }
  712. }
  713. /*
  714. * We also throw away everything when a Guest tells us it's changed a kernel
  715. * mapping. Since kernel mappings are in every page table, it's easiest to
  716. * throw them all away. This traps the Guest in amber for a while as
  717. * everything faults back in, but it's rare.
  718. */
  719. void guest_pagetable_clear_all(struct lg_cpu *cpu)
  720. {
  721. release_all_pagetables(cpu->lg);
  722. /* We need the Guest kernel stack mapped again. */
  723. pin_stack_pages(cpu);
  724. /* And we need Switcher allocated. */
  725. if (!allocate_switcher_mapping(cpu))
  726. kill_guest(cpu, "Cannot populate switcher mapping");
  727. }
  728. /*H:430
  729. * (iv) Switching page tables
  730. *
  731. * Now we've seen all the page table setting and manipulation, let's see
  732. * what happens when the Guest changes page tables (ie. changes the top-level
  733. * pgdir). This occurs on almost every context switch.
  734. */
  735. void guest_new_pagetable(struct lg_cpu *cpu, unsigned long pgtable)
  736. {
  737. int newpgdir, repin = 0;
  738. /*
  739. * The very first time they call this, we're actually running without
  740. * any page tables; we've been making it up. Throw them away now.
  741. */
  742. if (unlikely(cpu->linear_pages)) {
  743. release_all_pagetables(cpu->lg);
  744. cpu->linear_pages = false;
  745. /* Force allocation of a new pgdir. */
  746. newpgdir = ARRAY_SIZE(cpu->lg->pgdirs);
  747. } else {
  748. /* Look to see if we have this one already. */
  749. newpgdir = find_pgdir(cpu->lg, pgtable);
  750. }
  751. /*
  752. * If not, we allocate or mug an existing one: if it's a fresh one,
  753. * repin gets set to 1.
  754. */
  755. if (newpgdir == ARRAY_SIZE(cpu->lg->pgdirs))
  756. newpgdir = new_pgdir(cpu, pgtable, &repin);
  757. /* Change the current pgd index to the new one. */
  758. cpu->cpu_pgd = newpgdir;
  759. /*
  760. * If it was completely blank, we map in the Guest kernel stack and
  761. * the Switcher.
  762. */
  763. if (repin)
  764. pin_stack_pages(cpu);
  765. if (!cpu->lg->pgdirs[cpu->cpu_pgd].switcher_mapped) {
  766. if (!allocate_switcher_mapping(cpu))
  767. kill_guest(cpu, "Cannot populate switcher mapping");
  768. }
  769. }
  770. /*:*/
  771. /*M:009
  772. * Since we throw away all mappings when a kernel mapping changes, our
  773. * performance sucks for guests using highmem. In fact, a guest with
  774. * PAGE_OFFSET 0xc0000000 (the default) and more than about 700MB of RAM is
  775. * usually slower than a Guest with less memory.
  776. *
  777. * This, of course, cannot be fixed. It would take some kind of... well, I
  778. * don't know, but the term "puissant code-fu" comes to mind.
  779. :*/
  780. /*H:420
  781. * This is the routine which actually sets the page table entry for then
  782. * "idx"'th shadow page table.
  783. *
  784. * Normally, we can just throw out the old entry and replace it with 0: if they
  785. * use it demand_page() will put the new entry in. We need to do this anyway:
  786. * The Guest expects _PAGE_ACCESSED to be set on its PTE the first time a page
  787. * is read from, and _PAGE_DIRTY when it's written to.
  788. *
  789. * But Avi Kivity pointed out that most Operating Systems (Linux included) set
  790. * these bits on PTEs immediately anyway. This is done to save the CPU from
  791. * having to update them, but it helps us the same way: if they set
  792. * _PAGE_ACCESSED then we can put a read-only PTE entry in immediately, and if
  793. * they set _PAGE_DIRTY then we can put a writable PTE entry in immediately.
  794. */
  795. static void do_set_pte(struct lg_cpu *cpu, int idx,
  796. unsigned long vaddr, pte_t gpte)
  797. {
  798. /* Look up the matching shadow page directory entry. */
  799. pgd_t *spgd = spgd_addr(cpu, idx, vaddr);
  800. #ifdef CONFIG_X86_PAE
  801. pmd_t *spmd;
  802. #endif
  803. /* If the top level isn't present, there's no entry to update. */
  804. if (pgd_flags(*spgd) & _PAGE_PRESENT) {
  805. #ifdef CONFIG_X86_PAE
  806. spmd = spmd_addr(cpu, *spgd, vaddr);
  807. if (pmd_flags(*spmd) & _PAGE_PRESENT) {
  808. #endif
  809. /* Otherwise, start by releasing the existing entry. */
  810. pte_t *spte = spte_addr(cpu, *spgd, vaddr);
  811. release_pte(*spte);
  812. /*
  813. * If they're setting this entry as dirty or accessed,
  814. * we might as well put that entry they've given us in
  815. * now. This shaves 10% off a copy-on-write
  816. * micro-benchmark.
  817. */
  818. if (pte_flags(gpte) & (_PAGE_DIRTY | _PAGE_ACCESSED)) {
  819. if (!check_gpte(cpu, gpte))
  820. return;
  821. set_pte(spte,
  822. gpte_to_spte(cpu, gpte,
  823. pte_flags(gpte) & _PAGE_DIRTY));
  824. } else {
  825. /*
  826. * Otherwise kill it and we can demand_page()
  827. * it in later.
  828. */
  829. set_pte(spte, __pte(0));
  830. }
  831. #ifdef CONFIG_X86_PAE
  832. }
  833. #endif
  834. }
  835. }
  836. /*H:410
  837. * Updating a PTE entry is a little trickier.
  838. *
  839. * We keep track of several different page tables (the Guest uses one for each
  840. * process, so it makes sense to cache at least a few). Each of these have
  841. * identical kernel parts: ie. every mapping above PAGE_OFFSET is the same for
  842. * all processes. So when the page table above that address changes, we update
  843. * all the page tables, not just the current one. This is rare.
  844. *
  845. * The benefit is that when we have to track a new page table, we can keep all
  846. * the kernel mappings. This speeds up context switch immensely.
  847. */
  848. void guest_set_pte(struct lg_cpu *cpu,
  849. unsigned long gpgdir, unsigned long vaddr, pte_t gpte)
  850. {
  851. /* We don't let you remap the Switcher; we need it to get back! */
  852. if (vaddr >= switcher_addr) {
  853. kill_guest(cpu, "attempt to set pte into Switcher pages");
  854. return;
  855. }
  856. /*
  857. * Kernel mappings must be changed on all top levels. Slow, but doesn't
  858. * happen often.
  859. */
  860. if (vaddr >= cpu->lg->kernel_address) {
  861. unsigned int i;
  862. for (i = 0; i < ARRAY_SIZE(cpu->lg->pgdirs); i++)
  863. if (cpu->lg->pgdirs[i].pgdir)
  864. do_set_pte(cpu, i, vaddr, gpte);
  865. } else {
  866. /* Is this page table one we have a shadow for? */
  867. int pgdir = find_pgdir(cpu->lg, gpgdir);
  868. if (pgdir != ARRAY_SIZE(cpu->lg->pgdirs))
  869. /* If so, do the update. */
  870. do_set_pte(cpu, pgdir, vaddr, gpte);
  871. }
  872. }
  873. /*H:400
  874. * (iii) Setting up a page table entry when the Guest tells us one has changed.
  875. *
  876. * Just like we did in interrupts_and_traps.c, it makes sense for us to deal
  877. * with the other side of page tables while we're here: what happens when the
  878. * Guest asks for a page table to be updated?
  879. *
  880. * We already saw that demand_page() will fill in the shadow page tables when
  881. * needed, so we can simply remove shadow page table entries whenever the Guest
  882. * tells us they've changed. When the Guest tries to use the new entry it will
  883. * fault and demand_page() will fix it up.
  884. *
  885. * So with that in mind here's our code to update a (top-level) PGD entry:
  886. */
  887. void guest_set_pgd(struct lguest *lg, unsigned long gpgdir, u32 idx)
  888. {
  889. int pgdir;
  890. if (idx > PTRS_PER_PGD) {
  891. kill_guest(&lg->cpus[0], "Attempt to set pgd %u/%u",
  892. idx, PTRS_PER_PGD);
  893. return;
  894. }
  895. /* If they're talking about a page table we have a shadow for... */
  896. pgdir = find_pgdir(lg, gpgdir);
  897. if (pgdir < ARRAY_SIZE(lg->pgdirs)) {
  898. /* ... throw it away. */
  899. release_pgd(lg->pgdirs[pgdir].pgdir + idx);
  900. /* That might have been the Switcher mapping, remap it. */
  901. if (!allocate_switcher_mapping(&lg->cpus[0])) {
  902. kill_guest(&lg->cpus[0],
  903. "Cannot populate switcher mapping");
  904. }
  905. lg->pgdirs[pgdir].last_host_cpu = -1;
  906. }
  907. }
  908. #ifdef CONFIG_X86_PAE
  909. /* For setting a mid-level, we just throw everything away. It's easy. */
  910. void guest_set_pmd(struct lguest *lg, unsigned long pmdp, u32 idx)
  911. {
  912. guest_pagetable_clear_all(&lg->cpus[0]);
  913. }
  914. #endif
  915. /*H:500
  916. * (vii) Setting up the page tables initially.
  917. *
  918. * When a Guest is first created, set initialize a shadow page table which
  919. * we will populate on future faults. The Guest doesn't have any actual
  920. * pagetables yet, so we set linear_pages to tell demand_page() to fake it
  921. * for the moment.
  922. *
  923. * We do need the Switcher to be mapped at all times, so we allocate that
  924. * part of the Guest page table here.
  925. */
  926. int init_guest_pagetable(struct lguest *lg)
  927. {
  928. struct lg_cpu *cpu = &lg->cpus[0];
  929. int allocated = 0;
  930. /* lg (and lg->cpus[]) starts zeroed: this allocates a new pgdir */
  931. cpu->cpu_pgd = new_pgdir(cpu, 0, &allocated);
  932. if (!allocated)
  933. return -ENOMEM;
  934. /* We start with a linear mapping until the initialize. */
  935. cpu->linear_pages = true;
  936. /* Allocate the page tables for the Switcher. */
  937. if (!allocate_switcher_mapping(cpu)) {
  938. release_all_pagetables(lg);
  939. return -ENOMEM;
  940. }
  941. return 0;
  942. }
  943. /*H:508 When the Guest calls LHCALL_LGUEST_INIT we do more setup. */
  944. void page_table_guest_data_init(struct lg_cpu *cpu)
  945. {
  946. /*
  947. * We tell the Guest that it can't use the virtual addresses
  948. * used by the Switcher. This trick is equivalent to 4GB -
  949. * switcher_addr.
  950. */
  951. u32 top = ~switcher_addr + 1;
  952. /* We get the kernel address: above this is all kernel memory. */
  953. if (get_user(cpu->lg->kernel_address,
  954. &cpu->lg->lguest_data->kernel_address)
  955. /*
  956. * We tell the Guest that it can't use the top virtual
  957. * addresses (used by the Switcher).
  958. */
  959. || put_user(top, &cpu->lg->lguest_data->reserve_mem)) {
  960. kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
  961. return;
  962. }
  963. /*
  964. * In flush_user_mappings() we loop from 0 to
  965. * "pgd_index(lg->kernel_address)". This assumes it won't hit the
  966. * Switcher mappings, so check that now.
  967. */
  968. if (cpu->lg->kernel_address >= switcher_addr)
  969. kill_guest(cpu, "bad kernel address %#lx",
  970. cpu->lg->kernel_address);
  971. }
  972. /* When a Guest dies, our cleanup is fairly simple. */
  973. void free_guest_pagetable(struct lguest *lg)
  974. {
  975. unsigned int i;
  976. /* Throw away all page table pages. */
  977. release_all_pagetables(lg);
  978. /* Now free the top levels: free_page() can handle 0 just fine. */
  979. for (i = 0; i < ARRAY_SIZE(lg->pgdirs); i++)
  980. free_page((long)lg->pgdirs[i].pgdir);
  981. }
  982. /*H:481
  983. * This clears the Switcher mappings for cpu #i.
  984. */
  985. static void remove_switcher_percpu_map(struct lg_cpu *cpu, unsigned int i)
  986. {
  987. unsigned long base = switcher_addr + PAGE_SIZE + i * PAGE_SIZE*2;
  988. pte_t *pte;
  989. /* Clear the mappings for both pages. */
  990. pte = find_spte(cpu, base, false, 0, 0);
  991. release_pte(*pte);
  992. set_pte(pte, __pte(0));
  993. pte = find_spte(cpu, base + PAGE_SIZE, false, 0, 0);
  994. release_pte(*pte);
  995. set_pte(pte, __pte(0));
  996. }
  997. /*H:480
  998. * (vi) Mapping the Switcher when the Guest is about to run.
  999. *
  1000. * The Switcher and the two pages for this CPU need to be visible in the Guest
  1001. * (and not the pages for other CPUs).
  1002. *
  1003. * The pages for the pagetables have all been allocated before: we just need
  1004. * to make sure the actual PTEs are up-to-date for the CPU we're about to run
  1005. * on.
  1006. */
  1007. void map_switcher_in_guest(struct lg_cpu *cpu, struct lguest_pages *pages)
  1008. {
  1009. unsigned long base;
  1010. struct page *percpu_switcher_page, *regs_page;
  1011. pte_t *pte;
  1012. struct pgdir *pgdir = &cpu->lg->pgdirs[cpu->cpu_pgd];
  1013. /* Switcher page should always be mapped by now! */
  1014. BUG_ON(!pgdir->switcher_mapped);
  1015. /*
  1016. * Remember that we have two pages for each Host CPU, so we can run a
  1017. * Guest on each CPU without them interfering. We need to make sure
  1018. * those pages are mapped correctly in the Guest, but since we usually
  1019. * run on the same CPU, we cache that, and only update the mappings
  1020. * when we move.
  1021. */
  1022. if (pgdir->last_host_cpu == raw_smp_processor_id())
  1023. return;
  1024. /* -1 means unknown so we remove everything. */
  1025. if (pgdir->last_host_cpu == -1) {
  1026. unsigned int i;
  1027. for_each_possible_cpu(i)
  1028. remove_switcher_percpu_map(cpu, i);
  1029. } else {
  1030. /* We know exactly what CPU mapping to remove. */
  1031. remove_switcher_percpu_map(cpu, pgdir->last_host_cpu);
  1032. }
  1033. /*
  1034. * When we're running the Guest, we want the Guest's "regs" page to
  1035. * appear where the first Switcher page for this CPU is. This is an
  1036. * optimization: when the Switcher saves the Guest registers, it saves
  1037. * them into the first page of this CPU's "struct lguest_pages": if we
  1038. * make sure the Guest's register page is already mapped there, we
  1039. * don't have to copy them out again.
  1040. */
  1041. /* Find the shadow PTE for this regs page. */
  1042. base = switcher_addr + PAGE_SIZE
  1043. + raw_smp_processor_id() * sizeof(struct lguest_pages);
  1044. pte = find_spte(cpu, base, false, 0, 0);
  1045. regs_page = pfn_to_page(__pa(cpu->regs_page) >> PAGE_SHIFT);
  1046. get_page(regs_page);
  1047. set_pte(pte, mk_pte(regs_page, __pgprot(__PAGE_KERNEL & ~_PAGE_GLOBAL)));
  1048. /*
  1049. * We map the second page of the struct lguest_pages read-only in
  1050. * the Guest: the IDT, GDT and other things it's not supposed to
  1051. * change.
  1052. */
  1053. pte = find_spte(cpu, base + PAGE_SIZE, false, 0, 0);
  1054. percpu_switcher_page
  1055. = lg_switcher_pages[1 + raw_smp_processor_id()*2 + 1];
  1056. get_page(percpu_switcher_page);
  1057. set_pte(pte, mk_pte(percpu_switcher_page,
  1058. __pgprot(__PAGE_KERNEL_RO & ~_PAGE_GLOBAL)));
  1059. pgdir->last_host_cpu = raw_smp_processor_id();
  1060. }
  1061. /*H:490
  1062. * We've made it through the page table code. Perhaps our tired brains are
  1063. * still processing the details, or perhaps we're simply glad it's over.
  1064. *
  1065. * If nothing else, note that all this complexity in juggling shadow page tables
  1066. * in sync with the Guest's page tables is for one reason: for most Guests this
  1067. * page table dance determines how bad performance will be. This is why Xen
  1068. * uses exotic direct Guest pagetable manipulation, and why both Intel and AMD
  1069. * have implemented shadow page table support directly into hardware.
  1070. *
  1071. * There is just one file remaining in the Host.
  1072. */