page_tables.c 39 KB

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