page_tables.c 35 KB

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