mmu.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415
  1. /*
  2. * Xen mmu operations
  3. *
  4. * This file contains the various mmu fetch and update operations.
  5. * The most important job they must perform is the mapping between the
  6. * domain's pfn and the overall machine mfns.
  7. *
  8. * Xen allows guests to directly update the pagetable, in a controlled
  9. * fashion. In other words, the guest modifies the same pagetable
  10. * that the CPU actually uses, which eliminates the overhead of having
  11. * a separate shadow pagetable.
  12. *
  13. * In order to allow this, it falls on the guest domain to map its
  14. * notion of a "physical" pfn - which is just a domain-local linear
  15. * address - into a real "machine address" which the CPU's MMU can
  16. * use.
  17. *
  18. * A pgd_t/pmd_t/pte_t will typically contain an mfn, and so can be
  19. * inserted directly into the pagetable. When creating a new
  20. * pte/pmd/pgd, it converts the passed pfn into an mfn. Conversely,
  21. * when reading the content back with __(pgd|pmd|pte)_val, it converts
  22. * the mfn back into a pfn.
  23. *
  24. * The other constraint is that all pages which make up a pagetable
  25. * must be mapped read-only in the guest. This prevents uncontrolled
  26. * guest updates to the pagetable. Xen strictly enforces this, and
  27. * will disallow any pagetable update which will end up mapping a
  28. * pagetable page RW, and will disallow using any writable page as a
  29. * pagetable.
  30. *
  31. * Naively, when loading %cr3 with the base of a new pagetable, Xen
  32. * would need to validate the whole pagetable before going on.
  33. * Naturally, this is quite slow. The solution is to "pin" a
  34. * pagetable, which enforces all the constraints on the pagetable even
  35. * when it is not actively in use. This menas that Xen can be assured
  36. * that it is still valid when you do load it into %cr3, and doesn't
  37. * need to revalidate it.
  38. *
  39. * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
  40. */
  41. #include <linux/sched.h>
  42. #include <linux/highmem.h>
  43. #include <linux/debugfs.h>
  44. #include <linux/bug.h>
  45. #include <linux/vmalloc.h>
  46. #include <linux/module.h>
  47. #include <linux/gfp.h>
  48. #include <linux/memblock.h>
  49. #include <asm/pgtable.h>
  50. #include <asm/tlbflush.h>
  51. #include <asm/fixmap.h>
  52. #include <asm/mmu_context.h>
  53. #include <asm/setup.h>
  54. #include <asm/paravirt.h>
  55. #include <asm/e820.h>
  56. #include <asm/linkage.h>
  57. #include <asm/page.h>
  58. #include <asm/init.h>
  59. #include <asm/pat.h>
  60. #include <asm/xen/hypercall.h>
  61. #include <asm/xen/hypervisor.h>
  62. #include <xen/xen.h>
  63. #include <xen/page.h>
  64. #include <xen/interface/xen.h>
  65. #include <xen/interface/hvm/hvm_op.h>
  66. #include <xen/interface/version.h>
  67. #include <xen/interface/memory.h>
  68. #include <xen/hvc-console.h>
  69. #include "multicalls.h"
  70. #include "mmu.h"
  71. #include "debugfs.h"
  72. #define MMU_UPDATE_HISTO 30
  73. /*
  74. * Protects atomic reservation decrease/increase against concurrent increases.
  75. * Also protects non-atomic updates of current_pages and driver_pages, and
  76. * balloon lists.
  77. */
  78. DEFINE_SPINLOCK(xen_reservation_lock);
  79. #ifdef CONFIG_XEN_DEBUG_FS
  80. static struct {
  81. u32 pgd_update;
  82. u32 pgd_update_pinned;
  83. u32 pgd_update_batched;
  84. u32 pud_update;
  85. u32 pud_update_pinned;
  86. u32 pud_update_batched;
  87. u32 pmd_update;
  88. u32 pmd_update_pinned;
  89. u32 pmd_update_batched;
  90. u32 pte_update;
  91. u32 pte_update_pinned;
  92. u32 pte_update_batched;
  93. u32 mmu_update;
  94. u32 mmu_update_extended;
  95. u32 mmu_update_histo[MMU_UPDATE_HISTO];
  96. u32 prot_commit;
  97. u32 prot_commit_batched;
  98. u32 set_pte_at;
  99. u32 set_pte_at_batched;
  100. u32 set_pte_at_pinned;
  101. u32 set_pte_at_current;
  102. u32 set_pte_at_kernel;
  103. } mmu_stats;
  104. static u8 zero_stats;
  105. static inline void check_zero(void)
  106. {
  107. if (unlikely(zero_stats)) {
  108. memset(&mmu_stats, 0, sizeof(mmu_stats));
  109. zero_stats = 0;
  110. }
  111. }
  112. #define ADD_STATS(elem, val) \
  113. do { check_zero(); mmu_stats.elem += (val); } while(0)
  114. #else /* !CONFIG_XEN_DEBUG_FS */
  115. #define ADD_STATS(elem, val) do { (void)(val); } while(0)
  116. #endif /* CONFIG_XEN_DEBUG_FS */
  117. /*
  118. * Identity map, in addition to plain kernel map. This needs to be
  119. * large enough to allocate page table pages to allocate the rest.
  120. * Each page can map 2MB.
  121. */
  122. #define LEVEL1_IDENT_ENTRIES (PTRS_PER_PTE * 4)
  123. static RESERVE_BRK_ARRAY(pte_t, level1_ident_pgt, LEVEL1_IDENT_ENTRIES);
  124. #ifdef CONFIG_X86_64
  125. /* l3 pud for userspace vsyscall mapping */
  126. static pud_t level3_user_vsyscall[PTRS_PER_PUD] __page_aligned_bss;
  127. #endif /* CONFIG_X86_64 */
  128. /*
  129. * Note about cr3 (pagetable base) values:
  130. *
  131. * xen_cr3 contains the current logical cr3 value; it contains the
  132. * last set cr3. This may not be the current effective cr3, because
  133. * its update may be being lazily deferred. However, a vcpu looking
  134. * at its own cr3 can use this value knowing that it everything will
  135. * be self-consistent.
  136. *
  137. * xen_current_cr3 contains the actual vcpu cr3; it is set once the
  138. * hypercall to set the vcpu cr3 is complete (so it may be a little
  139. * out of date, but it will never be set early). If one vcpu is
  140. * looking at another vcpu's cr3 value, it should use this variable.
  141. */
  142. DEFINE_PER_CPU(unsigned long, xen_cr3); /* cr3 stored as physaddr */
  143. DEFINE_PER_CPU(unsigned long, xen_current_cr3); /* actual vcpu cr3 */
  144. /*
  145. * Just beyond the highest usermode address. STACK_TOP_MAX has a
  146. * redzone above it, so round it up to a PGD boundary.
  147. */
  148. #define USER_LIMIT ((STACK_TOP_MAX + PGDIR_SIZE - 1) & PGDIR_MASK)
  149. unsigned long arbitrary_virt_to_mfn(void *vaddr)
  150. {
  151. xmaddr_t maddr = arbitrary_virt_to_machine(vaddr);
  152. return PFN_DOWN(maddr.maddr);
  153. }
  154. xmaddr_t arbitrary_virt_to_machine(void *vaddr)
  155. {
  156. unsigned long address = (unsigned long)vaddr;
  157. unsigned int level;
  158. pte_t *pte;
  159. unsigned offset;
  160. /*
  161. * if the PFN is in the linear mapped vaddr range, we can just use
  162. * the (quick) virt_to_machine() p2m lookup
  163. */
  164. if (virt_addr_valid(vaddr))
  165. return virt_to_machine(vaddr);
  166. /* otherwise we have to do a (slower) full page-table walk */
  167. pte = lookup_address(address, &level);
  168. BUG_ON(pte == NULL);
  169. offset = address & ~PAGE_MASK;
  170. return XMADDR(((phys_addr_t)pte_mfn(*pte) << PAGE_SHIFT) + offset);
  171. }
  172. EXPORT_SYMBOL_GPL(arbitrary_virt_to_machine);
  173. void make_lowmem_page_readonly(void *vaddr)
  174. {
  175. pte_t *pte, ptev;
  176. unsigned long address = (unsigned long)vaddr;
  177. unsigned int level;
  178. pte = lookup_address(address, &level);
  179. if (pte == NULL)
  180. return; /* vaddr missing */
  181. ptev = pte_wrprotect(*pte);
  182. if (HYPERVISOR_update_va_mapping(address, ptev, 0))
  183. BUG();
  184. }
  185. void make_lowmem_page_readwrite(void *vaddr)
  186. {
  187. pte_t *pte, ptev;
  188. unsigned long address = (unsigned long)vaddr;
  189. unsigned int level;
  190. pte = lookup_address(address, &level);
  191. if (pte == NULL)
  192. return; /* vaddr missing */
  193. ptev = pte_mkwrite(*pte);
  194. if (HYPERVISOR_update_va_mapping(address, ptev, 0))
  195. BUG();
  196. }
  197. static bool xen_page_pinned(void *ptr)
  198. {
  199. struct page *page = virt_to_page(ptr);
  200. return PagePinned(page);
  201. }
  202. static bool xen_iomap_pte(pte_t pte)
  203. {
  204. return pte_flags(pte) & _PAGE_IOMAP;
  205. }
  206. void xen_set_domain_pte(pte_t *ptep, pte_t pteval, unsigned domid)
  207. {
  208. struct multicall_space mcs;
  209. struct mmu_update *u;
  210. mcs = xen_mc_entry(sizeof(*u));
  211. u = mcs.args;
  212. /* ptep might be kmapped when using 32-bit HIGHPTE */
  213. u->ptr = arbitrary_virt_to_machine(ptep).maddr;
  214. u->val = pte_val_ma(pteval);
  215. MULTI_mmu_update(mcs.mc, mcs.args, 1, NULL, domid);
  216. xen_mc_issue(PARAVIRT_LAZY_MMU);
  217. }
  218. EXPORT_SYMBOL_GPL(xen_set_domain_pte);
  219. static void xen_set_iomap_pte(pte_t *ptep, pte_t pteval)
  220. {
  221. xen_set_domain_pte(ptep, pteval, DOMID_IO);
  222. }
  223. static void xen_extend_mmu_update(const struct mmu_update *update)
  224. {
  225. struct multicall_space mcs;
  226. struct mmu_update *u;
  227. mcs = xen_mc_extend_args(__HYPERVISOR_mmu_update, sizeof(*u));
  228. if (mcs.mc != NULL) {
  229. ADD_STATS(mmu_update_extended, 1);
  230. ADD_STATS(mmu_update_histo[mcs.mc->args[1]], -1);
  231. mcs.mc->args[1]++;
  232. if (mcs.mc->args[1] < MMU_UPDATE_HISTO)
  233. ADD_STATS(mmu_update_histo[mcs.mc->args[1]], 1);
  234. else
  235. ADD_STATS(mmu_update_histo[0], 1);
  236. } else {
  237. ADD_STATS(mmu_update, 1);
  238. mcs = __xen_mc_entry(sizeof(*u));
  239. MULTI_mmu_update(mcs.mc, mcs.args, 1, NULL, DOMID_SELF);
  240. ADD_STATS(mmu_update_histo[1], 1);
  241. }
  242. u = mcs.args;
  243. *u = *update;
  244. }
  245. void xen_set_pmd_hyper(pmd_t *ptr, pmd_t val)
  246. {
  247. struct mmu_update u;
  248. preempt_disable();
  249. xen_mc_batch();
  250. /* ptr may be ioremapped for 64-bit pagetable setup */
  251. u.ptr = arbitrary_virt_to_machine(ptr).maddr;
  252. u.val = pmd_val_ma(val);
  253. xen_extend_mmu_update(&u);
  254. ADD_STATS(pmd_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
  255. xen_mc_issue(PARAVIRT_LAZY_MMU);
  256. preempt_enable();
  257. }
  258. void xen_set_pmd(pmd_t *ptr, pmd_t val)
  259. {
  260. ADD_STATS(pmd_update, 1);
  261. /* If page is not pinned, we can just update the entry
  262. directly */
  263. if (!xen_page_pinned(ptr)) {
  264. *ptr = val;
  265. return;
  266. }
  267. ADD_STATS(pmd_update_pinned, 1);
  268. xen_set_pmd_hyper(ptr, val);
  269. }
  270. /*
  271. * Associate a virtual page frame with a given physical page frame
  272. * and protection flags for that frame.
  273. */
  274. void set_pte_mfn(unsigned long vaddr, unsigned long mfn, pgprot_t flags)
  275. {
  276. set_pte_vaddr(vaddr, mfn_pte(mfn, flags));
  277. }
  278. void xen_set_pte_at(struct mm_struct *mm, unsigned long addr,
  279. pte_t *ptep, pte_t pteval)
  280. {
  281. if (xen_iomap_pte(pteval)) {
  282. xen_set_iomap_pte(ptep, pteval);
  283. goto out;
  284. }
  285. ADD_STATS(set_pte_at, 1);
  286. // ADD_STATS(set_pte_at_pinned, xen_page_pinned(ptep));
  287. ADD_STATS(set_pte_at_current, mm == current->mm);
  288. ADD_STATS(set_pte_at_kernel, mm == &init_mm);
  289. if (mm == current->mm || mm == &init_mm) {
  290. if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU) {
  291. struct multicall_space mcs;
  292. mcs = xen_mc_entry(0);
  293. MULTI_update_va_mapping(mcs.mc, addr, pteval, 0);
  294. ADD_STATS(set_pte_at_batched, 1);
  295. xen_mc_issue(PARAVIRT_LAZY_MMU);
  296. goto out;
  297. } else
  298. if (HYPERVISOR_update_va_mapping(addr, pteval, 0) == 0)
  299. goto out;
  300. }
  301. xen_set_pte(ptep, pteval);
  302. out: return;
  303. }
  304. pte_t xen_ptep_modify_prot_start(struct mm_struct *mm,
  305. unsigned long addr, pte_t *ptep)
  306. {
  307. /* Just return the pte as-is. We preserve the bits on commit */
  308. return *ptep;
  309. }
  310. void xen_ptep_modify_prot_commit(struct mm_struct *mm, unsigned long addr,
  311. pte_t *ptep, pte_t pte)
  312. {
  313. struct mmu_update u;
  314. xen_mc_batch();
  315. u.ptr = arbitrary_virt_to_machine(ptep).maddr | MMU_PT_UPDATE_PRESERVE_AD;
  316. u.val = pte_val_ma(pte);
  317. xen_extend_mmu_update(&u);
  318. ADD_STATS(prot_commit, 1);
  319. ADD_STATS(prot_commit_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
  320. xen_mc_issue(PARAVIRT_LAZY_MMU);
  321. }
  322. /* Assume pteval_t is equivalent to all the other *val_t types. */
  323. static pteval_t pte_mfn_to_pfn(pteval_t val)
  324. {
  325. if (val & _PAGE_PRESENT) {
  326. unsigned long mfn = (val & PTE_PFN_MASK) >> PAGE_SHIFT;
  327. pteval_t flags = val & PTE_FLAGS_MASK;
  328. val = ((pteval_t)mfn_to_pfn(mfn) << PAGE_SHIFT) | flags;
  329. }
  330. return val;
  331. }
  332. static pteval_t pte_pfn_to_mfn(pteval_t val)
  333. {
  334. if (val & _PAGE_PRESENT) {
  335. unsigned long pfn = (val & PTE_PFN_MASK) >> PAGE_SHIFT;
  336. pteval_t flags = val & PTE_FLAGS_MASK;
  337. unsigned long mfn = pfn_to_mfn(pfn);
  338. /*
  339. * If there's no mfn for the pfn, then just create an
  340. * empty non-present pte. Unfortunately this loses
  341. * information about the original pfn, so
  342. * pte_mfn_to_pfn is asymmetric.
  343. */
  344. if (unlikely(mfn == INVALID_P2M_ENTRY)) {
  345. mfn = 0;
  346. flags = 0;
  347. }
  348. val = ((pteval_t)mfn << PAGE_SHIFT) | flags;
  349. }
  350. return val;
  351. }
  352. static pteval_t iomap_pte(pteval_t val)
  353. {
  354. if (val & _PAGE_PRESENT) {
  355. unsigned long pfn = (val & PTE_PFN_MASK) >> PAGE_SHIFT;
  356. pteval_t flags = val & PTE_FLAGS_MASK;
  357. /* We assume the pte frame number is a MFN, so
  358. just use it as-is. */
  359. val = ((pteval_t)pfn << PAGE_SHIFT) | flags;
  360. }
  361. return val;
  362. }
  363. pteval_t xen_pte_val(pte_t pte)
  364. {
  365. pteval_t pteval = pte.pte;
  366. /* If this is a WC pte, convert back from Xen WC to Linux WC */
  367. if ((pteval & (_PAGE_PAT | _PAGE_PCD | _PAGE_PWT)) == _PAGE_PAT) {
  368. WARN_ON(!pat_enabled);
  369. pteval = (pteval & ~_PAGE_PAT) | _PAGE_PWT;
  370. }
  371. if (xen_initial_domain() && (pteval & _PAGE_IOMAP))
  372. return pteval;
  373. return pte_mfn_to_pfn(pteval);
  374. }
  375. PV_CALLEE_SAVE_REGS_THUNK(xen_pte_val);
  376. pgdval_t xen_pgd_val(pgd_t pgd)
  377. {
  378. return pte_mfn_to_pfn(pgd.pgd);
  379. }
  380. PV_CALLEE_SAVE_REGS_THUNK(xen_pgd_val);
  381. /*
  382. * Xen's PAT setup is part of its ABI, though I assume entries 6 & 7
  383. * are reserved for now, to correspond to the Intel-reserved PAT
  384. * types.
  385. *
  386. * We expect Linux's PAT set as follows:
  387. *
  388. * Idx PTE flags Linux Xen Default
  389. * 0 WB WB WB
  390. * 1 PWT WC WT WT
  391. * 2 PCD UC- UC- UC-
  392. * 3 PCD PWT UC UC UC
  393. * 4 PAT WB WC WB
  394. * 5 PAT PWT WC WP WT
  395. * 6 PAT PCD UC- UC UC-
  396. * 7 PAT PCD PWT UC UC UC
  397. */
  398. void xen_set_pat(u64 pat)
  399. {
  400. /* We expect Linux to use a PAT setting of
  401. * UC UC- WC WB (ignoring the PAT flag) */
  402. WARN_ON(pat != 0x0007010600070106ull);
  403. }
  404. pte_t xen_make_pte(pteval_t pte)
  405. {
  406. phys_addr_t addr = (pte & PTE_PFN_MASK);
  407. /* If Linux is trying to set a WC pte, then map to the Xen WC.
  408. * If _PAGE_PAT is set, then it probably means it is really
  409. * _PAGE_PSE, so avoid fiddling with the PAT mapping and hope
  410. * things work out OK...
  411. *
  412. * (We should never see kernel mappings with _PAGE_PSE set,
  413. * but we could see hugetlbfs mappings, I think.).
  414. */
  415. if (pat_enabled && !WARN_ON(pte & _PAGE_PAT)) {
  416. if ((pte & (_PAGE_PCD | _PAGE_PWT)) == _PAGE_PWT)
  417. pte = (pte & ~(_PAGE_PCD | _PAGE_PWT)) | _PAGE_PAT;
  418. }
  419. /*
  420. * Unprivileged domains are allowed to do IOMAPpings for
  421. * PCI passthrough, but not map ISA space. The ISA
  422. * mappings are just dummy local mappings to keep other
  423. * parts of the kernel happy.
  424. */
  425. if (unlikely(pte & _PAGE_IOMAP) &&
  426. (xen_initial_domain() || addr >= ISA_END_ADDRESS)) {
  427. pte = iomap_pte(pte);
  428. } else {
  429. pte &= ~_PAGE_IOMAP;
  430. pte = pte_pfn_to_mfn(pte);
  431. }
  432. return native_make_pte(pte);
  433. }
  434. PV_CALLEE_SAVE_REGS_THUNK(xen_make_pte);
  435. pgd_t xen_make_pgd(pgdval_t pgd)
  436. {
  437. pgd = pte_pfn_to_mfn(pgd);
  438. return native_make_pgd(pgd);
  439. }
  440. PV_CALLEE_SAVE_REGS_THUNK(xen_make_pgd);
  441. pmdval_t xen_pmd_val(pmd_t pmd)
  442. {
  443. return pte_mfn_to_pfn(pmd.pmd);
  444. }
  445. PV_CALLEE_SAVE_REGS_THUNK(xen_pmd_val);
  446. void xen_set_pud_hyper(pud_t *ptr, pud_t val)
  447. {
  448. struct mmu_update u;
  449. preempt_disable();
  450. xen_mc_batch();
  451. /* ptr may be ioremapped for 64-bit pagetable setup */
  452. u.ptr = arbitrary_virt_to_machine(ptr).maddr;
  453. u.val = pud_val_ma(val);
  454. xen_extend_mmu_update(&u);
  455. ADD_STATS(pud_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
  456. xen_mc_issue(PARAVIRT_LAZY_MMU);
  457. preempt_enable();
  458. }
  459. void xen_set_pud(pud_t *ptr, pud_t val)
  460. {
  461. ADD_STATS(pud_update, 1);
  462. /* If page is not pinned, we can just update the entry
  463. directly */
  464. if (!xen_page_pinned(ptr)) {
  465. *ptr = val;
  466. return;
  467. }
  468. ADD_STATS(pud_update_pinned, 1);
  469. xen_set_pud_hyper(ptr, val);
  470. }
  471. void xen_set_pte(pte_t *ptep, pte_t pte)
  472. {
  473. if (xen_iomap_pte(pte)) {
  474. xen_set_iomap_pte(ptep, pte);
  475. return;
  476. }
  477. ADD_STATS(pte_update, 1);
  478. // ADD_STATS(pte_update_pinned, xen_page_pinned(ptep));
  479. ADD_STATS(pte_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
  480. #ifdef CONFIG_X86_PAE
  481. ptep->pte_high = pte.pte_high;
  482. smp_wmb();
  483. ptep->pte_low = pte.pte_low;
  484. #else
  485. *ptep = pte;
  486. #endif
  487. }
  488. #ifdef CONFIG_X86_PAE
  489. void xen_set_pte_atomic(pte_t *ptep, pte_t pte)
  490. {
  491. if (xen_iomap_pte(pte)) {
  492. xen_set_iomap_pte(ptep, pte);
  493. return;
  494. }
  495. set_64bit((u64 *)ptep, native_pte_val(pte));
  496. }
  497. void xen_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  498. {
  499. ptep->pte_low = 0;
  500. smp_wmb(); /* make sure low gets written first */
  501. ptep->pte_high = 0;
  502. }
  503. void xen_pmd_clear(pmd_t *pmdp)
  504. {
  505. set_pmd(pmdp, __pmd(0));
  506. }
  507. #endif /* CONFIG_X86_PAE */
  508. pmd_t xen_make_pmd(pmdval_t pmd)
  509. {
  510. pmd = pte_pfn_to_mfn(pmd);
  511. return native_make_pmd(pmd);
  512. }
  513. PV_CALLEE_SAVE_REGS_THUNK(xen_make_pmd);
  514. #if PAGETABLE_LEVELS == 4
  515. pudval_t xen_pud_val(pud_t pud)
  516. {
  517. return pte_mfn_to_pfn(pud.pud);
  518. }
  519. PV_CALLEE_SAVE_REGS_THUNK(xen_pud_val);
  520. pud_t xen_make_pud(pudval_t pud)
  521. {
  522. pud = pte_pfn_to_mfn(pud);
  523. return native_make_pud(pud);
  524. }
  525. PV_CALLEE_SAVE_REGS_THUNK(xen_make_pud);
  526. pgd_t *xen_get_user_pgd(pgd_t *pgd)
  527. {
  528. pgd_t *pgd_page = (pgd_t *)(((unsigned long)pgd) & PAGE_MASK);
  529. unsigned offset = pgd - pgd_page;
  530. pgd_t *user_ptr = NULL;
  531. if (offset < pgd_index(USER_LIMIT)) {
  532. struct page *page = virt_to_page(pgd_page);
  533. user_ptr = (pgd_t *)page->private;
  534. if (user_ptr)
  535. user_ptr += offset;
  536. }
  537. return user_ptr;
  538. }
  539. static void __xen_set_pgd_hyper(pgd_t *ptr, pgd_t val)
  540. {
  541. struct mmu_update u;
  542. u.ptr = virt_to_machine(ptr).maddr;
  543. u.val = pgd_val_ma(val);
  544. xen_extend_mmu_update(&u);
  545. }
  546. /*
  547. * Raw hypercall-based set_pgd, intended for in early boot before
  548. * there's a page structure. This implies:
  549. * 1. The only existing pagetable is the kernel's
  550. * 2. It is always pinned
  551. * 3. It has no user pagetable attached to it
  552. */
  553. void __init xen_set_pgd_hyper(pgd_t *ptr, pgd_t val)
  554. {
  555. preempt_disable();
  556. xen_mc_batch();
  557. __xen_set_pgd_hyper(ptr, val);
  558. xen_mc_issue(PARAVIRT_LAZY_MMU);
  559. preempt_enable();
  560. }
  561. void xen_set_pgd(pgd_t *ptr, pgd_t val)
  562. {
  563. pgd_t *user_ptr = xen_get_user_pgd(ptr);
  564. ADD_STATS(pgd_update, 1);
  565. /* If page is not pinned, we can just update the entry
  566. directly */
  567. if (!xen_page_pinned(ptr)) {
  568. *ptr = val;
  569. if (user_ptr) {
  570. WARN_ON(xen_page_pinned(user_ptr));
  571. *user_ptr = val;
  572. }
  573. return;
  574. }
  575. ADD_STATS(pgd_update_pinned, 1);
  576. ADD_STATS(pgd_update_batched, paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU);
  577. /* If it's pinned, then we can at least batch the kernel and
  578. user updates together. */
  579. xen_mc_batch();
  580. __xen_set_pgd_hyper(ptr, val);
  581. if (user_ptr)
  582. __xen_set_pgd_hyper(user_ptr, val);
  583. xen_mc_issue(PARAVIRT_LAZY_MMU);
  584. }
  585. #endif /* PAGETABLE_LEVELS == 4 */
  586. /*
  587. * (Yet another) pagetable walker. This one is intended for pinning a
  588. * pagetable. This means that it walks a pagetable and calls the
  589. * callback function on each page it finds making up the page table,
  590. * at every level. It walks the entire pagetable, but it only bothers
  591. * pinning pte pages which are below limit. In the normal case this
  592. * will be STACK_TOP_MAX, but at boot we need to pin up to
  593. * FIXADDR_TOP.
  594. *
  595. * For 32-bit the important bit is that we don't pin beyond there,
  596. * because then we start getting into Xen's ptes.
  597. *
  598. * For 64-bit, we must skip the Xen hole in the middle of the address
  599. * space, just after the big x86-64 virtual hole.
  600. */
  601. static int __xen_pgd_walk(struct mm_struct *mm, pgd_t *pgd,
  602. int (*func)(struct mm_struct *mm, struct page *,
  603. enum pt_level),
  604. unsigned long limit)
  605. {
  606. int flush = 0;
  607. unsigned hole_low, hole_high;
  608. unsigned pgdidx_limit, pudidx_limit, pmdidx_limit;
  609. unsigned pgdidx, pudidx, pmdidx;
  610. /* The limit is the last byte to be touched */
  611. limit--;
  612. BUG_ON(limit >= FIXADDR_TOP);
  613. if (xen_feature(XENFEAT_auto_translated_physmap))
  614. return 0;
  615. /*
  616. * 64-bit has a great big hole in the middle of the address
  617. * space, which contains the Xen mappings. On 32-bit these
  618. * will end up making a zero-sized hole and so is a no-op.
  619. */
  620. hole_low = pgd_index(USER_LIMIT);
  621. hole_high = pgd_index(PAGE_OFFSET);
  622. pgdidx_limit = pgd_index(limit);
  623. #if PTRS_PER_PUD > 1
  624. pudidx_limit = pud_index(limit);
  625. #else
  626. pudidx_limit = 0;
  627. #endif
  628. #if PTRS_PER_PMD > 1
  629. pmdidx_limit = pmd_index(limit);
  630. #else
  631. pmdidx_limit = 0;
  632. #endif
  633. for (pgdidx = 0; pgdidx <= pgdidx_limit; pgdidx++) {
  634. pud_t *pud;
  635. if (pgdidx >= hole_low && pgdidx < hole_high)
  636. continue;
  637. if (!pgd_val(pgd[pgdidx]))
  638. continue;
  639. pud = pud_offset(&pgd[pgdidx], 0);
  640. if (PTRS_PER_PUD > 1) /* not folded */
  641. flush |= (*func)(mm, virt_to_page(pud), PT_PUD);
  642. for (pudidx = 0; pudidx < PTRS_PER_PUD; pudidx++) {
  643. pmd_t *pmd;
  644. if (pgdidx == pgdidx_limit &&
  645. pudidx > pudidx_limit)
  646. goto out;
  647. if (pud_none(pud[pudidx]))
  648. continue;
  649. pmd = pmd_offset(&pud[pudidx], 0);
  650. if (PTRS_PER_PMD > 1) /* not folded */
  651. flush |= (*func)(mm, virt_to_page(pmd), PT_PMD);
  652. for (pmdidx = 0; pmdidx < PTRS_PER_PMD; pmdidx++) {
  653. struct page *pte;
  654. if (pgdidx == pgdidx_limit &&
  655. pudidx == pudidx_limit &&
  656. pmdidx > pmdidx_limit)
  657. goto out;
  658. if (pmd_none(pmd[pmdidx]))
  659. continue;
  660. pte = pmd_page(pmd[pmdidx]);
  661. flush |= (*func)(mm, pte, PT_PTE);
  662. }
  663. }
  664. }
  665. out:
  666. /* Do the top level last, so that the callbacks can use it as
  667. a cue to do final things like tlb flushes. */
  668. flush |= (*func)(mm, virt_to_page(pgd), PT_PGD);
  669. return flush;
  670. }
  671. static int xen_pgd_walk(struct mm_struct *mm,
  672. int (*func)(struct mm_struct *mm, struct page *,
  673. enum pt_level),
  674. unsigned long limit)
  675. {
  676. return __xen_pgd_walk(mm, mm->pgd, func, limit);
  677. }
  678. /* If we're using split pte locks, then take the page's lock and
  679. return a pointer to it. Otherwise return NULL. */
  680. static spinlock_t *xen_pte_lock(struct page *page, struct mm_struct *mm)
  681. {
  682. spinlock_t *ptl = NULL;
  683. #if USE_SPLIT_PTLOCKS
  684. ptl = __pte_lockptr(page);
  685. spin_lock_nest_lock(ptl, &mm->page_table_lock);
  686. #endif
  687. return ptl;
  688. }
  689. static void xen_pte_unlock(void *v)
  690. {
  691. spinlock_t *ptl = v;
  692. spin_unlock(ptl);
  693. }
  694. static void xen_do_pin(unsigned level, unsigned long pfn)
  695. {
  696. struct mmuext_op *op;
  697. struct multicall_space mcs;
  698. mcs = __xen_mc_entry(sizeof(*op));
  699. op = mcs.args;
  700. op->cmd = level;
  701. op->arg1.mfn = pfn_to_mfn(pfn);
  702. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  703. }
  704. static int xen_pin_page(struct mm_struct *mm, struct page *page,
  705. enum pt_level level)
  706. {
  707. unsigned pgfl = TestSetPagePinned(page);
  708. int flush;
  709. if (pgfl)
  710. flush = 0; /* already pinned */
  711. else if (PageHighMem(page))
  712. /* kmaps need flushing if we found an unpinned
  713. highpage */
  714. flush = 1;
  715. else {
  716. void *pt = lowmem_page_address(page);
  717. unsigned long pfn = page_to_pfn(page);
  718. struct multicall_space mcs = __xen_mc_entry(0);
  719. spinlock_t *ptl;
  720. flush = 0;
  721. /*
  722. * We need to hold the pagetable lock between the time
  723. * we make the pagetable RO and when we actually pin
  724. * it. If we don't, then other users may come in and
  725. * attempt to update the pagetable by writing it,
  726. * which will fail because the memory is RO but not
  727. * pinned, so Xen won't do the trap'n'emulate.
  728. *
  729. * If we're using split pte locks, we can't hold the
  730. * entire pagetable's worth of locks during the
  731. * traverse, because we may wrap the preempt count (8
  732. * bits). The solution is to mark RO and pin each PTE
  733. * page while holding the lock. This means the number
  734. * of locks we end up holding is never more than a
  735. * batch size (~32 entries, at present).
  736. *
  737. * If we're not using split pte locks, we needn't pin
  738. * the PTE pages independently, because we're
  739. * protected by the overall pagetable lock.
  740. */
  741. ptl = NULL;
  742. if (level == PT_PTE)
  743. ptl = xen_pte_lock(page, mm);
  744. MULTI_update_va_mapping(mcs.mc, (unsigned long)pt,
  745. pfn_pte(pfn, PAGE_KERNEL_RO),
  746. level == PT_PGD ? UVMF_TLB_FLUSH : 0);
  747. if (ptl) {
  748. xen_do_pin(MMUEXT_PIN_L1_TABLE, pfn);
  749. /* Queue a deferred unlock for when this batch
  750. is completed. */
  751. xen_mc_callback(xen_pte_unlock, ptl);
  752. }
  753. }
  754. return flush;
  755. }
  756. /* This is called just after a mm has been created, but it has not
  757. been used yet. We need to make sure that its pagetable is all
  758. read-only, and can be pinned. */
  759. static void __xen_pgd_pin(struct mm_struct *mm, pgd_t *pgd)
  760. {
  761. xen_mc_batch();
  762. if (__xen_pgd_walk(mm, pgd, xen_pin_page, USER_LIMIT)) {
  763. /* re-enable interrupts for flushing */
  764. xen_mc_issue(0);
  765. kmap_flush_unused();
  766. xen_mc_batch();
  767. }
  768. #ifdef CONFIG_X86_64
  769. {
  770. pgd_t *user_pgd = xen_get_user_pgd(pgd);
  771. xen_do_pin(MMUEXT_PIN_L4_TABLE, PFN_DOWN(__pa(pgd)));
  772. if (user_pgd) {
  773. xen_pin_page(mm, virt_to_page(user_pgd), PT_PGD);
  774. xen_do_pin(MMUEXT_PIN_L4_TABLE,
  775. PFN_DOWN(__pa(user_pgd)));
  776. }
  777. }
  778. #else /* CONFIG_X86_32 */
  779. #ifdef CONFIG_X86_PAE
  780. /* Need to make sure unshared kernel PMD is pinnable */
  781. xen_pin_page(mm, pgd_page(pgd[pgd_index(TASK_SIZE)]),
  782. PT_PMD);
  783. #endif
  784. xen_do_pin(MMUEXT_PIN_L3_TABLE, PFN_DOWN(__pa(pgd)));
  785. #endif /* CONFIG_X86_64 */
  786. xen_mc_issue(0);
  787. }
  788. static void xen_pgd_pin(struct mm_struct *mm)
  789. {
  790. __xen_pgd_pin(mm, mm->pgd);
  791. }
  792. /*
  793. * On save, we need to pin all pagetables to make sure they get their
  794. * mfns turned into pfns. Search the list for any unpinned pgds and pin
  795. * them (unpinned pgds are not currently in use, probably because the
  796. * process is under construction or destruction).
  797. *
  798. * Expected to be called in stop_machine() ("equivalent to taking
  799. * every spinlock in the system"), so the locking doesn't really
  800. * matter all that much.
  801. */
  802. void xen_mm_pin_all(void)
  803. {
  804. unsigned long flags;
  805. struct page *page;
  806. spin_lock_irqsave(&pgd_lock, flags);
  807. list_for_each_entry(page, &pgd_list, lru) {
  808. if (!PagePinned(page)) {
  809. __xen_pgd_pin(&init_mm, (pgd_t *)page_address(page));
  810. SetPageSavePinned(page);
  811. }
  812. }
  813. spin_unlock_irqrestore(&pgd_lock, flags);
  814. }
  815. /*
  816. * The init_mm pagetable is really pinned as soon as its created, but
  817. * that's before we have page structures to store the bits. So do all
  818. * the book-keeping now.
  819. */
  820. static __init int xen_mark_pinned(struct mm_struct *mm, struct page *page,
  821. enum pt_level level)
  822. {
  823. SetPagePinned(page);
  824. return 0;
  825. }
  826. static void __init xen_mark_init_mm_pinned(void)
  827. {
  828. xen_pgd_walk(&init_mm, xen_mark_pinned, FIXADDR_TOP);
  829. }
  830. static int xen_unpin_page(struct mm_struct *mm, struct page *page,
  831. enum pt_level level)
  832. {
  833. unsigned pgfl = TestClearPagePinned(page);
  834. if (pgfl && !PageHighMem(page)) {
  835. void *pt = lowmem_page_address(page);
  836. unsigned long pfn = page_to_pfn(page);
  837. spinlock_t *ptl = NULL;
  838. struct multicall_space mcs;
  839. /*
  840. * Do the converse to pin_page. If we're using split
  841. * pte locks, we must be holding the lock for while
  842. * the pte page is unpinned but still RO to prevent
  843. * concurrent updates from seeing it in this
  844. * partially-pinned state.
  845. */
  846. if (level == PT_PTE) {
  847. ptl = xen_pte_lock(page, mm);
  848. if (ptl)
  849. xen_do_pin(MMUEXT_UNPIN_TABLE, pfn);
  850. }
  851. mcs = __xen_mc_entry(0);
  852. MULTI_update_va_mapping(mcs.mc, (unsigned long)pt,
  853. pfn_pte(pfn, PAGE_KERNEL),
  854. level == PT_PGD ? UVMF_TLB_FLUSH : 0);
  855. if (ptl) {
  856. /* unlock when batch completed */
  857. xen_mc_callback(xen_pte_unlock, ptl);
  858. }
  859. }
  860. return 0; /* never need to flush on unpin */
  861. }
  862. /* Release a pagetables pages back as normal RW */
  863. static void __xen_pgd_unpin(struct mm_struct *mm, pgd_t *pgd)
  864. {
  865. xen_mc_batch();
  866. xen_do_pin(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
  867. #ifdef CONFIG_X86_64
  868. {
  869. pgd_t *user_pgd = xen_get_user_pgd(pgd);
  870. if (user_pgd) {
  871. xen_do_pin(MMUEXT_UNPIN_TABLE,
  872. PFN_DOWN(__pa(user_pgd)));
  873. xen_unpin_page(mm, virt_to_page(user_pgd), PT_PGD);
  874. }
  875. }
  876. #endif
  877. #ifdef CONFIG_X86_PAE
  878. /* Need to make sure unshared kernel PMD is unpinned */
  879. xen_unpin_page(mm, pgd_page(pgd[pgd_index(TASK_SIZE)]),
  880. PT_PMD);
  881. #endif
  882. __xen_pgd_walk(mm, pgd, xen_unpin_page, USER_LIMIT);
  883. xen_mc_issue(0);
  884. }
  885. static void xen_pgd_unpin(struct mm_struct *mm)
  886. {
  887. __xen_pgd_unpin(mm, mm->pgd);
  888. }
  889. /*
  890. * On resume, undo any pinning done at save, so that the rest of the
  891. * kernel doesn't see any unexpected pinned pagetables.
  892. */
  893. void xen_mm_unpin_all(void)
  894. {
  895. unsigned long flags;
  896. struct page *page;
  897. spin_lock_irqsave(&pgd_lock, flags);
  898. list_for_each_entry(page, &pgd_list, lru) {
  899. if (PageSavePinned(page)) {
  900. BUG_ON(!PagePinned(page));
  901. __xen_pgd_unpin(&init_mm, (pgd_t *)page_address(page));
  902. ClearPageSavePinned(page);
  903. }
  904. }
  905. spin_unlock_irqrestore(&pgd_lock, flags);
  906. }
  907. void xen_activate_mm(struct mm_struct *prev, struct mm_struct *next)
  908. {
  909. spin_lock(&next->page_table_lock);
  910. xen_pgd_pin(next);
  911. spin_unlock(&next->page_table_lock);
  912. }
  913. void xen_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm)
  914. {
  915. spin_lock(&mm->page_table_lock);
  916. xen_pgd_pin(mm);
  917. spin_unlock(&mm->page_table_lock);
  918. }
  919. #ifdef CONFIG_SMP
  920. /* Another cpu may still have their %cr3 pointing at the pagetable, so
  921. we need to repoint it somewhere else before we can unpin it. */
  922. static void drop_other_mm_ref(void *info)
  923. {
  924. struct mm_struct *mm = info;
  925. struct mm_struct *active_mm;
  926. active_mm = percpu_read(cpu_tlbstate.active_mm);
  927. if (active_mm == mm)
  928. leave_mm(smp_processor_id());
  929. /* If this cpu still has a stale cr3 reference, then make sure
  930. it has been flushed. */
  931. if (percpu_read(xen_current_cr3) == __pa(mm->pgd))
  932. load_cr3(swapper_pg_dir);
  933. }
  934. static void xen_drop_mm_ref(struct mm_struct *mm)
  935. {
  936. cpumask_var_t mask;
  937. unsigned cpu;
  938. if (current->active_mm == mm) {
  939. if (current->mm == mm)
  940. load_cr3(swapper_pg_dir);
  941. else
  942. leave_mm(smp_processor_id());
  943. }
  944. /* Get the "official" set of cpus referring to our pagetable. */
  945. if (!alloc_cpumask_var(&mask, GFP_ATOMIC)) {
  946. for_each_online_cpu(cpu) {
  947. if (!cpumask_test_cpu(cpu, mm_cpumask(mm))
  948. && per_cpu(xen_current_cr3, cpu) != __pa(mm->pgd))
  949. continue;
  950. smp_call_function_single(cpu, drop_other_mm_ref, mm, 1);
  951. }
  952. return;
  953. }
  954. cpumask_copy(mask, mm_cpumask(mm));
  955. /* It's possible that a vcpu may have a stale reference to our
  956. cr3, because its in lazy mode, and it hasn't yet flushed
  957. its set of pending hypercalls yet. In this case, we can
  958. look at its actual current cr3 value, and force it to flush
  959. if needed. */
  960. for_each_online_cpu(cpu) {
  961. if (per_cpu(xen_current_cr3, cpu) == __pa(mm->pgd))
  962. cpumask_set_cpu(cpu, mask);
  963. }
  964. if (!cpumask_empty(mask))
  965. smp_call_function_many(mask, drop_other_mm_ref, mm, 1);
  966. free_cpumask_var(mask);
  967. }
  968. #else
  969. static void xen_drop_mm_ref(struct mm_struct *mm)
  970. {
  971. if (current->active_mm == mm)
  972. load_cr3(swapper_pg_dir);
  973. }
  974. #endif
  975. /*
  976. * While a process runs, Xen pins its pagetables, which means that the
  977. * hypervisor forces it to be read-only, and it controls all updates
  978. * to it. This means that all pagetable updates have to go via the
  979. * hypervisor, which is moderately expensive.
  980. *
  981. * Since we're pulling the pagetable down, we switch to use init_mm,
  982. * unpin old process pagetable and mark it all read-write, which
  983. * allows further operations on it to be simple memory accesses.
  984. *
  985. * The only subtle point is that another CPU may be still using the
  986. * pagetable because of lazy tlb flushing. This means we need need to
  987. * switch all CPUs off this pagetable before we can unpin it.
  988. */
  989. void xen_exit_mmap(struct mm_struct *mm)
  990. {
  991. get_cpu(); /* make sure we don't move around */
  992. xen_drop_mm_ref(mm);
  993. put_cpu();
  994. spin_lock(&mm->page_table_lock);
  995. /* pgd may not be pinned in the error exit path of execve */
  996. if (xen_page_pinned(mm->pgd))
  997. xen_pgd_unpin(mm);
  998. spin_unlock(&mm->page_table_lock);
  999. }
  1000. static __init void xen_pagetable_setup_start(pgd_t *base)
  1001. {
  1002. }
  1003. static void xen_post_allocator_init(void);
  1004. static __init void xen_pagetable_setup_done(pgd_t *base)
  1005. {
  1006. xen_setup_shared_info();
  1007. xen_post_allocator_init();
  1008. }
  1009. static void xen_write_cr2(unsigned long cr2)
  1010. {
  1011. percpu_read(xen_vcpu)->arch.cr2 = cr2;
  1012. }
  1013. static unsigned long xen_read_cr2(void)
  1014. {
  1015. return percpu_read(xen_vcpu)->arch.cr2;
  1016. }
  1017. unsigned long xen_read_cr2_direct(void)
  1018. {
  1019. return percpu_read(xen_vcpu_info.arch.cr2);
  1020. }
  1021. static void xen_flush_tlb(void)
  1022. {
  1023. struct mmuext_op *op;
  1024. struct multicall_space mcs;
  1025. preempt_disable();
  1026. mcs = xen_mc_entry(sizeof(*op));
  1027. op = mcs.args;
  1028. op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
  1029. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  1030. xen_mc_issue(PARAVIRT_LAZY_MMU);
  1031. preempt_enable();
  1032. }
  1033. static void xen_flush_tlb_single(unsigned long addr)
  1034. {
  1035. struct mmuext_op *op;
  1036. struct multicall_space mcs;
  1037. preempt_disable();
  1038. mcs = xen_mc_entry(sizeof(*op));
  1039. op = mcs.args;
  1040. op->cmd = MMUEXT_INVLPG_LOCAL;
  1041. op->arg1.linear_addr = addr & PAGE_MASK;
  1042. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  1043. xen_mc_issue(PARAVIRT_LAZY_MMU);
  1044. preempt_enable();
  1045. }
  1046. static void xen_flush_tlb_others(const struct cpumask *cpus,
  1047. struct mm_struct *mm, unsigned long va)
  1048. {
  1049. struct {
  1050. struct mmuext_op op;
  1051. DECLARE_BITMAP(mask, NR_CPUS);
  1052. } *args;
  1053. struct multicall_space mcs;
  1054. if (cpumask_empty(cpus))
  1055. return; /* nothing to do */
  1056. mcs = xen_mc_entry(sizeof(*args));
  1057. args = mcs.args;
  1058. args->op.arg2.vcpumask = to_cpumask(args->mask);
  1059. /* Remove us, and any offline CPUS. */
  1060. cpumask_and(to_cpumask(args->mask), cpus, cpu_online_mask);
  1061. cpumask_clear_cpu(smp_processor_id(), to_cpumask(args->mask));
  1062. if (va == TLB_FLUSH_ALL) {
  1063. args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
  1064. } else {
  1065. args->op.cmd = MMUEXT_INVLPG_MULTI;
  1066. args->op.arg1.linear_addr = va;
  1067. }
  1068. MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
  1069. xen_mc_issue(PARAVIRT_LAZY_MMU);
  1070. }
  1071. static unsigned long xen_read_cr3(void)
  1072. {
  1073. return percpu_read(xen_cr3);
  1074. }
  1075. static void set_current_cr3(void *v)
  1076. {
  1077. percpu_write(xen_current_cr3, (unsigned long)v);
  1078. }
  1079. static void __xen_write_cr3(bool kernel, unsigned long cr3)
  1080. {
  1081. struct mmuext_op *op;
  1082. struct multicall_space mcs;
  1083. unsigned long mfn;
  1084. if (cr3)
  1085. mfn = pfn_to_mfn(PFN_DOWN(cr3));
  1086. else
  1087. mfn = 0;
  1088. WARN_ON(mfn == 0 && kernel);
  1089. mcs = __xen_mc_entry(sizeof(*op));
  1090. op = mcs.args;
  1091. op->cmd = kernel ? MMUEXT_NEW_BASEPTR : MMUEXT_NEW_USER_BASEPTR;
  1092. op->arg1.mfn = mfn;
  1093. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  1094. if (kernel) {
  1095. percpu_write(xen_cr3, cr3);
  1096. /* Update xen_current_cr3 once the batch has actually
  1097. been submitted. */
  1098. xen_mc_callback(set_current_cr3, (void *)cr3);
  1099. }
  1100. }
  1101. static void xen_write_cr3(unsigned long cr3)
  1102. {
  1103. BUG_ON(preemptible());
  1104. xen_mc_batch(); /* disables interrupts */
  1105. /* Update while interrupts are disabled, so its atomic with
  1106. respect to ipis */
  1107. percpu_write(xen_cr3, cr3);
  1108. __xen_write_cr3(true, cr3);
  1109. #ifdef CONFIG_X86_64
  1110. {
  1111. pgd_t *user_pgd = xen_get_user_pgd(__va(cr3));
  1112. if (user_pgd)
  1113. __xen_write_cr3(false, __pa(user_pgd));
  1114. else
  1115. __xen_write_cr3(false, 0);
  1116. }
  1117. #endif
  1118. xen_mc_issue(PARAVIRT_LAZY_CPU); /* interrupts restored */
  1119. }
  1120. static int xen_pgd_alloc(struct mm_struct *mm)
  1121. {
  1122. pgd_t *pgd = mm->pgd;
  1123. int ret = 0;
  1124. BUG_ON(PagePinned(virt_to_page(pgd)));
  1125. #ifdef CONFIG_X86_64
  1126. {
  1127. struct page *page = virt_to_page(pgd);
  1128. pgd_t *user_pgd;
  1129. BUG_ON(page->private != 0);
  1130. ret = -ENOMEM;
  1131. user_pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  1132. page->private = (unsigned long)user_pgd;
  1133. if (user_pgd != NULL) {
  1134. user_pgd[pgd_index(VSYSCALL_START)] =
  1135. __pgd(__pa(level3_user_vsyscall) | _PAGE_TABLE);
  1136. ret = 0;
  1137. }
  1138. BUG_ON(PagePinned(virt_to_page(xen_get_user_pgd(pgd))));
  1139. }
  1140. #endif
  1141. return ret;
  1142. }
  1143. static void xen_pgd_free(struct mm_struct *mm, pgd_t *pgd)
  1144. {
  1145. #ifdef CONFIG_X86_64
  1146. pgd_t *user_pgd = xen_get_user_pgd(pgd);
  1147. if (user_pgd)
  1148. free_page((unsigned long)user_pgd);
  1149. #endif
  1150. }
  1151. static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
  1152. {
  1153. unsigned long pfn = pte_pfn(pte);
  1154. #ifdef CONFIG_X86_32
  1155. /* If there's an existing pte, then don't allow _PAGE_RW to be set */
  1156. if (pte_val_ma(*ptep) & _PAGE_PRESENT)
  1157. pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
  1158. pte_val_ma(pte));
  1159. #endif
  1160. /*
  1161. * If the new pfn is within the range of the newly allocated
  1162. * kernel pagetable, and it isn't being mapped into an
  1163. * early_ioremap fixmap slot, make sure it is RO.
  1164. */
  1165. if (!is_early_ioremap_ptep(ptep) &&
  1166. pfn >= e820_table_start && pfn < e820_table_end)
  1167. pte = pte_wrprotect(pte);
  1168. return pte;
  1169. }
  1170. /* Init-time set_pte while constructing initial pagetables, which
  1171. doesn't allow RO pagetable pages to be remapped RW */
  1172. static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
  1173. {
  1174. pte = mask_rw_pte(ptep, pte);
  1175. xen_set_pte(ptep, pte);
  1176. }
  1177. static void pin_pagetable_pfn(unsigned cmd, unsigned long pfn)
  1178. {
  1179. struct mmuext_op op;
  1180. op.cmd = cmd;
  1181. op.arg1.mfn = pfn_to_mfn(pfn);
  1182. if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
  1183. BUG();
  1184. }
  1185. /* Early in boot, while setting up the initial pagetable, assume
  1186. everything is pinned. */
  1187. static __init void xen_alloc_pte_init(struct mm_struct *mm, unsigned long pfn)
  1188. {
  1189. #ifdef CONFIG_FLATMEM
  1190. BUG_ON(mem_map); /* should only be used early */
  1191. #endif
  1192. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  1193. pin_pagetable_pfn(MMUEXT_PIN_L1_TABLE, pfn);
  1194. }
  1195. /* Used for pmd and pud */
  1196. static __init void xen_alloc_pmd_init(struct mm_struct *mm, unsigned long pfn)
  1197. {
  1198. #ifdef CONFIG_FLATMEM
  1199. BUG_ON(mem_map); /* should only be used early */
  1200. #endif
  1201. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  1202. }
  1203. /* Early release_pte assumes that all pts are pinned, since there's
  1204. only init_mm and anything attached to that is pinned. */
  1205. static __init void xen_release_pte_init(unsigned long pfn)
  1206. {
  1207. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, pfn);
  1208. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  1209. }
  1210. static __init void xen_release_pmd_init(unsigned long pfn)
  1211. {
  1212. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  1213. }
  1214. /* This needs to make sure the new pte page is pinned iff its being
  1215. attached to a pinned pagetable. */
  1216. static void xen_alloc_ptpage(struct mm_struct *mm, unsigned long pfn, unsigned level)
  1217. {
  1218. struct page *page = pfn_to_page(pfn);
  1219. if (PagePinned(virt_to_page(mm->pgd))) {
  1220. SetPagePinned(page);
  1221. if (!PageHighMem(page)) {
  1222. make_lowmem_page_readonly(__va(PFN_PHYS((unsigned long)pfn)));
  1223. if (level == PT_PTE && USE_SPLIT_PTLOCKS)
  1224. pin_pagetable_pfn(MMUEXT_PIN_L1_TABLE, pfn);
  1225. } else {
  1226. /* make sure there are no stray mappings of
  1227. this page */
  1228. kmap_flush_unused();
  1229. }
  1230. }
  1231. }
  1232. static void xen_alloc_pte(struct mm_struct *mm, unsigned long pfn)
  1233. {
  1234. xen_alloc_ptpage(mm, pfn, PT_PTE);
  1235. }
  1236. static void xen_alloc_pmd(struct mm_struct *mm, unsigned long pfn)
  1237. {
  1238. xen_alloc_ptpage(mm, pfn, PT_PMD);
  1239. }
  1240. /* This should never happen until we're OK to use struct page */
  1241. static void xen_release_ptpage(unsigned long pfn, unsigned level)
  1242. {
  1243. struct page *page = pfn_to_page(pfn);
  1244. if (PagePinned(page)) {
  1245. if (!PageHighMem(page)) {
  1246. if (level == PT_PTE && USE_SPLIT_PTLOCKS)
  1247. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, pfn);
  1248. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  1249. }
  1250. ClearPagePinned(page);
  1251. }
  1252. }
  1253. static void xen_release_pte(unsigned long pfn)
  1254. {
  1255. xen_release_ptpage(pfn, PT_PTE);
  1256. }
  1257. static void xen_release_pmd(unsigned long pfn)
  1258. {
  1259. xen_release_ptpage(pfn, PT_PMD);
  1260. }
  1261. #if PAGETABLE_LEVELS == 4
  1262. static void xen_alloc_pud(struct mm_struct *mm, unsigned long pfn)
  1263. {
  1264. xen_alloc_ptpage(mm, pfn, PT_PUD);
  1265. }
  1266. static void xen_release_pud(unsigned long pfn)
  1267. {
  1268. xen_release_ptpage(pfn, PT_PUD);
  1269. }
  1270. #endif
  1271. void __init xen_reserve_top(void)
  1272. {
  1273. #ifdef CONFIG_X86_32
  1274. unsigned long top = HYPERVISOR_VIRT_START;
  1275. struct xen_platform_parameters pp;
  1276. if (HYPERVISOR_xen_version(XENVER_platform_parameters, &pp) == 0)
  1277. top = pp.virt_start;
  1278. reserve_top_address(-top);
  1279. #endif /* CONFIG_X86_32 */
  1280. }
  1281. /*
  1282. * Like __va(), but returns address in the kernel mapping (which is
  1283. * all we have until the physical memory mapping has been set up.
  1284. */
  1285. static void *__ka(phys_addr_t paddr)
  1286. {
  1287. #ifdef CONFIG_X86_64
  1288. return (void *)(paddr + __START_KERNEL_map);
  1289. #else
  1290. return __va(paddr);
  1291. #endif
  1292. }
  1293. /* Convert a machine address to physical address */
  1294. static unsigned long m2p(phys_addr_t maddr)
  1295. {
  1296. phys_addr_t paddr;
  1297. maddr &= PTE_PFN_MASK;
  1298. paddr = mfn_to_pfn(maddr >> PAGE_SHIFT) << PAGE_SHIFT;
  1299. return paddr;
  1300. }
  1301. /* Convert a machine address to kernel virtual */
  1302. static void *m2v(phys_addr_t maddr)
  1303. {
  1304. return __ka(m2p(maddr));
  1305. }
  1306. /* Set the page permissions on an identity-mapped pages */
  1307. static void set_page_prot(void *addr, pgprot_t prot)
  1308. {
  1309. unsigned long pfn = __pa(addr) >> PAGE_SHIFT;
  1310. pte_t pte = pfn_pte(pfn, prot);
  1311. if (HYPERVISOR_update_va_mapping((unsigned long)addr, pte, 0))
  1312. BUG();
  1313. }
  1314. static __init void xen_map_identity_early(pmd_t *pmd, unsigned long max_pfn)
  1315. {
  1316. unsigned pmdidx, pteidx;
  1317. unsigned ident_pte;
  1318. unsigned long pfn;
  1319. level1_ident_pgt = extend_brk(sizeof(pte_t) * LEVEL1_IDENT_ENTRIES,
  1320. PAGE_SIZE);
  1321. ident_pte = 0;
  1322. pfn = 0;
  1323. for (pmdidx = 0; pmdidx < PTRS_PER_PMD && pfn < max_pfn; pmdidx++) {
  1324. pte_t *pte_page;
  1325. /* Reuse or allocate a page of ptes */
  1326. if (pmd_present(pmd[pmdidx]))
  1327. pte_page = m2v(pmd[pmdidx].pmd);
  1328. else {
  1329. /* Check for free pte pages */
  1330. if (ident_pte == LEVEL1_IDENT_ENTRIES)
  1331. break;
  1332. pte_page = &level1_ident_pgt[ident_pte];
  1333. ident_pte += PTRS_PER_PTE;
  1334. pmd[pmdidx] = __pmd(__pa(pte_page) | _PAGE_TABLE);
  1335. }
  1336. /* Install mappings */
  1337. for (pteidx = 0; pteidx < PTRS_PER_PTE; pteidx++, pfn++) {
  1338. pte_t pte;
  1339. if (pfn > max_pfn_mapped)
  1340. max_pfn_mapped = pfn;
  1341. if (!pte_none(pte_page[pteidx]))
  1342. continue;
  1343. pte = pfn_pte(pfn, PAGE_KERNEL_EXEC);
  1344. pte_page[pteidx] = pte;
  1345. }
  1346. }
  1347. for (pteidx = 0; pteidx < ident_pte; pteidx += PTRS_PER_PTE)
  1348. set_page_prot(&level1_ident_pgt[pteidx], PAGE_KERNEL_RO);
  1349. set_page_prot(pmd, PAGE_KERNEL_RO);
  1350. }
  1351. void __init xen_setup_machphys_mapping(void)
  1352. {
  1353. struct xen_machphys_mapping mapping;
  1354. unsigned long machine_to_phys_nr_ents;
  1355. if (HYPERVISOR_memory_op(XENMEM_machphys_mapping, &mapping) == 0) {
  1356. machine_to_phys_mapping = (unsigned long *)mapping.v_start;
  1357. machine_to_phys_nr_ents = mapping.max_mfn + 1;
  1358. } else {
  1359. machine_to_phys_nr_ents = MACH2PHYS_NR_ENTRIES;
  1360. }
  1361. machine_to_phys_order = fls(machine_to_phys_nr_ents - 1);
  1362. }
  1363. #ifdef CONFIG_X86_64
  1364. static void convert_pfn_mfn(void *v)
  1365. {
  1366. pte_t *pte = v;
  1367. int i;
  1368. /* All levels are converted the same way, so just treat them
  1369. as ptes. */
  1370. for (i = 0; i < PTRS_PER_PTE; i++)
  1371. pte[i] = xen_make_pte(pte[i].pte);
  1372. }
  1373. /*
  1374. * Set up the inital kernel pagetable.
  1375. *
  1376. * We can construct this by grafting the Xen provided pagetable into
  1377. * head_64.S's preconstructed pagetables. We copy the Xen L2's into
  1378. * level2_ident_pgt, level2_kernel_pgt and level2_fixmap_pgt. This
  1379. * means that only the kernel has a physical mapping to start with -
  1380. * but that's enough to get __va working. We need to fill in the rest
  1381. * of the physical mapping once some sort of allocator has been set
  1382. * up.
  1383. */
  1384. __init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd,
  1385. unsigned long max_pfn)
  1386. {
  1387. pud_t *l3;
  1388. pmd_t *l2;
  1389. /* Zap identity mapping */
  1390. init_level4_pgt[0] = __pgd(0);
  1391. /* Pre-constructed entries are in pfn, so convert to mfn */
  1392. convert_pfn_mfn(init_level4_pgt);
  1393. convert_pfn_mfn(level3_ident_pgt);
  1394. convert_pfn_mfn(level3_kernel_pgt);
  1395. l3 = m2v(pgd[pgd_index(__START_KERNEL_map)].pgd);
  1396. l2 = m2v(l3[pud_index(__START_KERNEL_map)].pud);
  1397. memcpy(level2_ident_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1398. memcpy(level2_kernel_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1399. l3 = m2v(pgd[pgd_index(__START_KERNEL_map + PMD_SIZE)].pgd);
  1400. l2 = m2v(l3[pud_index(__START_KERNEL_map + PMD_SIZE)].pud);
  1401. memcpy(level2_fixmap_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1402. /* Set up identity map */
  1403. xen_map_identity_early(level2_ident_pgt, max_pfn);
  1404. /* Make pagetable pieces RO */
  1405. set_page_prot(init_level4_pgt, PAGE_KERNEL_RO);
  1406. set_page_prot(level3_ident_pgt, PAGE_KERNEL_RO);
  1407. set_page_prot(level3_kernel_pgt, PAGE_KERNEL_RO);
  1408. set_page_prot(level3_user_vsyscall, PAGE_KERNEL_RO);
  1409. set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO);
  1410. set_page_prot(level2_fixmap_pgt, PAGE_KERNEL_RO);
  1411. /* Pin down new L4 */
  1412. pin_pagetable_pfn(MMUEXT_PIN_L4_TABLE,
  1413. PFN_DOWN(__pa_symbol(init_level4_pgt)));
  1414. /* Unpin Xen-provided one */
  1415. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
  1416. /* Switch over */
  1417. pgd = init_level4_pgt;
  1418. /*
  1419. * At this stage there can be no user pgd, and no page
  1420. * structure to attach it to, so make sure we just set kernel
  1421. * pgd.
  1422. */
  1423. xen_mc_batch();
  1424. __xen_write_cr3(true, __pa(pgd));
  1425. xen_mc_issue(PARAVIRT_LAZY_CPU);
  1426. memblock_x86_reserve_range(__pa(xen_start_info->pt_base),
  1427. __pa(xen_start_info->pt_base +
  1428. xen_start_info->nr_pt_frames * PAGE_SIZE),
  1429. "XEN PAGETABLES");
  1430. return pgd;
  1431. }
  1432. #else /* !CONFIG_X86_64 */
  1433. static RESERVE_BRK_ARRAY(pmd_t, initial_kernel_pmd, PTRS_PER_PMD);
  1434. static RESERVE_BRK_ARRAY(pmd_t, swapper_kernel_pmd, PTRS_PER_PMD);
  1435. static __init void xen_write_cr3_init(unsigned long cr3)
  1436. {
  1437. unsigned long pfn = PFN_DOWN(__pa(swapper_pg_dir));
  1438. BUG_ON(read_cr3() != __pa(initial_page_table));
  1439. BUG_ON(cr3 != __pa(swapper_pg_dir));
  1440. /*
  1441. * We are switching to swapper_pg_dir for the first time (from
  1442. * initial_page_table) and therefore need to mark that page
  1443. * read-only and then pin it.
  1444. *
  1445. * Xen disallows sharing of kernel PMDs for PAE
  1446. * guests. Therefore we must copy the kernel PMD from
  1447. * initial_page_table into a new kernel PMD to be used in
  1448. * swapper_pg_dir.
  1449. */
  1450. swapper_kernel_pmd =
  1451. extend_brk(sizeof(pmd_t) * PTRS_PER_PMD, PAGE_SIZE);
  1452. memcpy(swapper_kernel_pmd, initial_kernel_pmd,
  1453. sizeof(pmd_t) * PTRS_PER_PMD);
  1454. swapper_pg_dir[KERNEL_PGD_BOUNDARY] =
  1455. __pgd(__pa(swapper_kernel_pmd) | _PAGE_PRESENT);
  1456. set_page_prot(swapper_kernel_pmd, PAGE_KERNEL_RO);
  1457. set_page_prot(swapper_pg_dir, PAGE_KERNEL_RO);
  1458. xen_write_cr3(cr3);
  1459. pin_pagetable_pfn(MMUEXT_PIN_L3_TABLE, pfn);
  1460. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE,
  1461. PFN_DOWN(__pa(initial_page_table)));
  1462. set_page_prot(initial_page_table, PAGE_KERNEL);
  1463. set_page_prot(initial_kernel_pmd, PAGE_KERNEL);
  1464. pv_mmu_ops.write_cr3 = &xen_write_cr3;
  1465. }
  1466. __init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd,
  1467. unsigned long max_pfn)
  1468. {
  1469. pmd_t *kernel_pmd;
  1470. initial_kernel_pmd =
  1471. extend_brk(sizeof(pmd_t) * PTRS_PER_PMD, PAGE_SIZE);
  1472. max_pfn_mapped = PFN_DOWN(__pa(xen_start_info->pt_base) +
  1473. xen_start_info->nr_pt_frames * PAGE_SIZE +
  1474. 512*1024);
  1475. kernel_pmd = m2v(pgd[KERNEL_PGD_BOUNDARY].pgd);
  1476. memcpy(initial_kernel_pmd, kernel_pmd, sizeof(pmd_t) * PTRS_PER_PMD);
  1477. xen_map_identity_early(initial_kernel_pmd, max_pfn);
  1478. memcpy(initial_page_table, pgd, sizeof(pgd_t) * PTRS_PER_PGD);
  1479. initial_page_table[KERNEL_PGD_BOUNDARY] =
  1480. __pgd(__pa(initial_kernel_pmd) | _PAGE_PRESENT);
  1481. set_page_prot(initial_kernel_pmd, PAGE_KERNEL_RO);
  1482. set_page_prot(initial_page_table, PAGE_KERNEL_RO);
  1483. set_page_prot(empty_zero_page, PAGE_KERNEL_RO);
  1484. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
  1485. pin_pagetable_pfn(MMUEXT_PIN_L3_TABLE,
  1486. PFN_DOWN(__pa(initial_page_table)));
  1487. xen_write_cr3(__pa(initial_page_table));
  1488. memblock_x86_reserve_range(__pa(xen_start_info->pt_base),
  1489. __pa(xen_start_info->pt_base +
  1490. xen_start_info->nr_pt_frames * PAGE_SIZE),
  1491. "XEN PAGETABLES");
  1492. return initial_page_table;
  1493. }
  1494. #endif /* CONFIG_X86_64 */
  1495. static unsigned char dummy_mapping[PAGE_SIZE] __page_aligned_bss;
  1496. static void xen_set_fixmap(unsigned idx, phys_addr_t phys, pgprot_t prot)
  1497. {
  1498. pte_t pte;
  1499. phys >>= PAGE_SHIFT;
  1500. switch (idx) {
  1501. case FIX_BTMAP_END ... FIX_BTMAP_BEGIN:
  1502. #ifdef CONFIG_X86_F00F_BUG
  1503. case FIX_F00F_IDT:
  1504. #endif
  1505. #ifdef CONFIG_X86_32
  1506. case FIX_WP_TEST:
  1507. case FIX_VDSO:
  1508. # ifdef CONFIG_HIGHMEM
  1509. case FIX_KMAP_BEGIN ... FIX_KMAP_END:
  1510. # endif
  1511. #else
  1512. case VSYSCALL_LAST_PAGE ... VSYSCALL_FIRST_PAGE:
  1513. #endif
  1514. case FIX_TEXT_POKE0:
  1515. case FIX_TEXT_POKE1:
  1516. /* All local page mappings */
  1517. pte = pfn_pte(phys, prot);
  1518. break;
  1519. #ifdef CONFIG_X86_LOCAL_APIC
  1520. case FIX_APIC_BASE: /* maps dummy local APIC */
  1521. pte = pfn_pte(PFN_DOWN(__pa(dummy_mapping)), PAGE_KERNEL);
  1522. break;
  1523. #endif
  1524. #ifdef CONFIG_X86_IO_APIC
  1525. case FIX_IO_APIC_BASE_0 ... FIX_IO_APIC_BASE_END:
  1526. /*
  1527. * We just don't map the IO APIC - all access is via
  1528. * hypercalls. Keep the address in the pte for reference.
  1529. */
  1530. pte = pfn_pte(PFN_DOWN(__pa(dummy_mapping)), PAGE_KERNEL);
  1531. break;
  1532. #endif
  1533. case FIX_PARAVIRT_BOOTMAP:
  1534. /* This is an MFN, but it isn't an IO mapping from the
  1535. IO domain */
  1536. pte = mfn_pte(phys, prot);
  1537. break;
  1538. default:
  1539. /* By default, set_fixmap is used for hardware mappings */
  1540. pte = mfn_pte(phys, __pgprot(pgprot_val(prot) | _PAGE_IOMAP));
  1541. break;
  1542. }
  1543. __native_set_fixmap(idx, pte);
  1544. #ifdef CONFIG_X86_64
  1545. /* Replicate changes to map the vsyscall page into the user
  1546. pagetable vsyscall mapping. */
  1547. if (idx >= VSYSCALL_LAST_PAGE && idx <= VSYSCALL_FIRST_PAGE) {
  1548. unsigned long vaddr = __fix_to_virt(idx);
  1549. set_pte_vaddr_pud(level3_user_vsyscall, vaddr, pte);
  1550. }
  1551. #endif
  1552. }
  1553. __init void xen_ident_map_ISA(void)
  1554. {
  1555. unsigned long pa;
  1556. /*
  1557. * If we're dom0, then linear map the ISA machine addresses into
  1558. * the kernel's address space.
  1559. */
  1560. if (!xen_initial_domain())
  1561. return;
  1562. xen_raw_printk("Xen: setup ISA identity maps\n");
  1563. for (pa = ISA_START_ADDRESS; pa < ISA_END_ADDRESS; pa += PAGE_SIZE) {
  1564. pte_t pte = mfn_pte(PFN_DOWN(pa), PAGE_KERNEL_IO);
  1565. if (HYPERVISOR_update_va_mapping(PAGE_OFFSET + pa, pte, 0))
  1566. BUG();
  1567. }
  1568. xen_flush_tlb();
  1569. }
  1570. static __init void xen_post_allocator_init(void)
  1571. {
  1572. pv_mmu_ops.set_pte = xen_set_pte;
  1573. pv_mmu_ops.set_pmd = xen_set_pmd;
  1574. pv_mmu_ops.set_pud = xen_set_pud;
  1575. #if PAGETABLE_LEVELS == 4
  1576. pv_mmu_ops.set_pgd = xen_set_pgd;
  1577. #endif
  1578. /* This will work as long as patching hasn't happened yet
  1579. (which it hasn't) */
  1580. pv_mmu_ops.alloc_pte = xen_alloc_pte;
  1581. pv_mmu_ops.alloc_pmd = xen_alloc_pmd;
  1582. pv_mmu_ops.release_pte = xen_release_pte;
  1583. pv_mmu_ops.release_pmd = xen_release_pmd;
  1584. #if PAGETABLE_LEVELS == 4
  1585. pv_mmu_ops.alloc_pud = xen_alloc_pud;
  1586. pv_mmu_ops.release_pud = xen_release_pud;
  1587. #endif
  1588. #ifdef CONFIG_X86_64
  1589. SetPagePinned(virt_to_page(level3_user_vsyscall));
  1590. #endif
  1591. xen_mark_init_mm_pinned();
  1592. }
  1593. static void xen_leave_lazy_mmu(void)
  1594. {
  1595. preempt_disable();
  1596. xen_mc_flush();
  1597. paravirt_leave_lazy_mmu();
  1598. preempt_enable();
  1599. }
  1600. static const struct pv_mmu_ops xen_mmu_ops __initdata = {
  1601. .read_cr2 = xen_read_cr2,
  1602. .write_cr2 = xen_write_cr2,
  1603. .read_cr3 = xen_read_cr3,
  1604. #ifdef CONFIG_X86_32
  1605. .write_cr3 = xen_write_cr3_init,
  1606. #else
  1607. .write_cr3 = xen_write_cr3,
  1608. #endif
  1609. .flush_tlb_user = xen_flush_tlb,
  1610. .flush_tlb_kernel = xen_flush_tlb,
  1611. .flush_tlb_single = xen_flush_tlb_single,
  1612. .flush_tlb_others = xen_flush_tlb_others,
  1613. .pte_update = paravirt_nop,
  1614. .pte_update_defer = paravirt_nop,
  1615. .pgd_alloc = xen_pgd_alloc,
  1616. .pgd_free = xen_pgd_free,
  1617. .alloc_pte = xen_alloc_pte_init,
  1618. .release_pte = xen_release_pte_init,
  1619. .alloc_pmd = xen_alloc_pmd_init,
  1620. .release_pmd = xen_release_pmd_init,
  1621. .set_pte = xen_set_pte_init,
  1622. .set_pte_at = xen_set_pte_at,
  1623. .set_pmd = xen_set_pmd_hyper,
  1624. .ptep_modify_prot_start = __ptep_modify_prot_start,
  1625. .ptep_modify_prot_commit = __ptep_modify_prot_commit,
  1626. .pte_val = PV_CALLEE_SAVE(xen_pte_val),
  1627. .pgd_val = PV_CALLEE_SAVE(xen_pgd_val),
  1628. .make_pte = PV_CALLEE_SAVE(xen_make_pte),
  1629. .make_pgd = PV_CALLEE_SAVE(xen_make_pgd),
  1630. #ifdef CONFIG_X86_PAE
  1631. .set_pte_atomic = xen_set_pte_atomic,
  1632. .pte_clear = xen_pte_clear,
  1633. .pmd_clear = xen_pmd_clear,
  1634. #endif /* CONFIG_X86_PAE */
  1635. .set_pud = xen_set_pud_hyper,
  1636. .make_pmd = PV_CALLEE_SAVE(xen_make_pmd),
  1637. .pmd_val = PV_CALLEE_SAVE(xen_pmd_val),
  1638. #if PAGETABLE_LEVELS == 4
  1639. .pud_val = PV_CALLEE_SAVE(xen_pud_val),
  1640. .make_pud = PV_CALLEE_SAVE(xen_make_pud),
  1641. .set_pgd = xen_set_pgd_hyper,
  1642. .alloc_pud = xen_alloc_pmd_init,
  1643. .release_pud = xen_release_pmd_init,
  1644. #endif /* PAGETABLE_LEVELS == 4 */
  1645. .activate_mm = xen_activate_mm,
  1646. .dup_mmap = xen_dup_mmap,
  1647. .exit_mmap = xen_exit_mmap,
  1648. .lazy_mode = {
  1649. .enter = paravirt_enter_lazy_mmu,
  1650. .leave = xen_leave_lazy_mmu,
  1651. },
  1652. .set_fixmap = xen_set_fixmap,
  1653. };
  1654. void __init xen_init_mmu_ops(void)
  1655. {
  1656. x86_init.paging.pagetable_setup_start = xen_pagetable_setup_start;
  1657. x86_init.paging.pagetable_setup_done = xen_pagetable_setup_done;
  1658. pv_mmu_ops = xen_mmu_ops;
  1659. memset(dummy_mapping, 0xff, PAGE_SIZE);
  1660. }
  1661. /* Protected by xen_reservation_lock. */
  1662. #define MAX_CONTIG_ORDER 9 /* 2MB */
  1663. static unsigned long discontig_frames[1<<MAX_CONTIG_ORDER];
  1664. #define VOID_PTE (mfn_pte(0, __pgprot(0)))
  1665. static void xen_zap_pfn_range(unsigned long vaddr, unsigned int order,
  1666. unsigned long *in_frames,
  1667. unsigned long *out_frames)
  1668. {
  1669. int i;
  1670. struct multicall_space mcs;
  1671. xen_mc_batch();
  1672. for (i = 0; i < (1UL<<order); i++, vaddr += PAGE_SIZE) {
  1673. mcs = __xen_mc_entry(0);
  1674. if (in_frames)
  1675. in_frames[i] = virt_to_mfn(vaddr);
  1676. MULTI_update_va_mapping(mcs.mc, vaddr, VOID_PTE, 0);
  1677. set_phys_to_machine(virt_to_pfn(vaddr), INVALID_P2M_ENTRY);
  1678. if (out_frames)
  1679. out_frames[i] = virt_to_pfn(vaddr);
  1680. }
  1681. xen_mc_issue(0);
  1682. }
  1683. /*
  1684. * Update the pfn-to-mfn mappings for a virtual address range, either to
  1685. * point to an array of mfns, or contiguously from a single starting
  1686. * mfn.
  1687. */
  1688. static void xen_remap_exchanged_ptes(unsigned long vaddr, int order,
  1689. unsigned long *mfns,
  1690. unsigned long first_mfn)
  1691. {
  1692. unsigned i, limit;
  1693. unsigned long mfn;
  1694. xen_mc_batch();
  1695. limit = 1u << order;
  1696. for (i = 0; i < limit; i++, vaddr += PAGE_SIZE) {
  1697. struct multicall_space mcs;
  1698. unsigned flags;
  1699. mcs = __xen_mc_entry(0);
  1700. if (mfns)
  1701. mfn = mfns[i];
  1702. else
  1703. mfn = first_mfn + i;
  1704. if (i < (limit - 1))
  1705. flags = 0;
  1706. else {
  1707. if (order == 0)
  1708. flags = UVMF_INVLPG | UVMF_ALL;
  1709. else
  1710. flags = UVMF_TLB_FLUSH | UVMF_ALL;
  1711. }
  1712. MULTI_update_va_mapping(mcs.mc, vaddr,
  1713. mfn_pte(mfn, PAGE_KERNEL), flags);
  1714. set_phys_to_machine(virt_to_pfn(vaddr), mfn);
  1715. }
  1716. xen_mc_issue(0);
  1717. }
  1718. /*
  1719. * Perform the hypercall to exchange a region of our pfns to point to
  1720. * memory with the required contiguous alignment. Takes the pfns as
  1721. * input, and populates mfns as output.
  1722. *
  1723. * Returns a success code indicating whether the hypervisor was able to
  1724. * satisfy the request or not.
  1725. */
  1726. static int xen_exchange_memory(unsigned long extents_in, unsigned int order_in,
  1727. unsigned long *pfns_in,
  1728. unsigned long extents_out,
  1729. unsigned int order_out,
  1730. unsigned long *mfns_out,
  1731. unsigned int address_bits)
  1732. {
  1733. long rc;
  1734. int success;
  1735. struct xen_memory_exchange exchange = {
  1736. .in = {
  1737. .nr_extents = extents_in,
  1738. .extent_order = order_in,
  1739. .extent_start = pfns_in,
  1740. .domid = DOMID_SELF
  1741. },
  1742. .out = {
  1743. .nr_extents = extents_out,
  1744. .extent_order = order_out,
  1745. .extent_start = mfns_out,
  1746. .address_bits = address_bits,
  1747. .domid = DOMID_SELF
  1748. }
  1749. };
  1750. BUG_ON(extents_in << order_in != extents_out << order_out);
  1751. rc = HYPERVISOR_memory_op(XENMEM_exchange, &exchange);
  1752. success = (exchange.nr_exchanged == extents_in);
  1753. BUG_ON(!success && ((exchange.nr_exchanged != 0) || (rc == 0)));
  1754. BUG_ON(success && (rc != 0));
  1755. return success;
  1756. }
  1757. int xen_create_contiguous_region(unsigned long vstart, unsigned int order,
  1758. unsigned int address_bits)
  1759. {
  1760. unsigned long *in_frames = discontig_frames, out_frame;
  1761. unsigned long flags;
  1762. int success;
  1763. /*
  1764. * Currently an auto-translated guest will not perform I/O, nor will
  1765. * it require PAE page directories below 4GB. Therefore any calls to
  1766. * this function are redundant and can be ignored.
  1767. */
  1768. if (xen_feature(XENFEAT_auto_translated_physmap))
  1769. return 0;
  1770. if (unlikely(order > MAX_CONTIG_ORDER))
  1771. return -ENOMEM;
  1772. memset((void *) vstart, 0, PAGE_SIZE << order);
  1773. spin_lock_irqsave(&xen_reservation_lock, flags);
  1774. /* 1. Zap current PTEs, remembering MFNs. */
  1775. xen_zap_pfn_range(vstart, order, in_frames, NULL);
  1776. /* 2. Get a new contiguous memory extent. */
  1777. out_frame = virt_to_pfn(vstart);
  1778. success = xen_exchange_memory(1UL << order, 0, in_frames,
  1779. 1, order, &out_frame,
  1780. address_bits);
  1781. /* 3. Map the new extent in place of old pages. */
  1782. if (success)
  1783. xen_remap_exchanged_ptes(vstart, order, NULL, out_frame);
  1784. else
  1785. xen_remap_exchanged_ptes(vstart, order, in_frames, 0);
  1786. spin_unlock_irqrestore(&xen_reservation_lock, flags);
  1787. return success ? 0 : -ENOMEM;
  1788. }
  1789. EXPORT_SYMBOL_GPL(xen_create_contiguous_region);
  1790. void xen_destroy_contiguous_region(unsigned long vstart, unsigned int order)
  1791. {
  1792. unsigned long *out_frames = discontig_frames, in_frame;
  1793. unsigned long flags;
  1794. int success;
  1795. if (xen_feature(XENFEAT_auto_translated_physmap))
  1796. return;
  1797. if (unlikely(order > MAX_CONTIG_ORDER))
  1798. return;
  1799. memset((void *) vstart, 0, PAGE_SIZE << order);
  1800. spin_lock_irqsave(&xen_reservation_lock, flags);
  1801. /* 1. Find start MFN of contiguous extent. */
  1802. in_frame = virt_to_mfn(vstart);
  1803. /* 2. Zap current PTEs. */
  1804. xen_zap_pfn_range(vstart, order, NULL, out_frames);
  1805. /* 3. Do the exchange for non-contiguous MFNs. */
  1806. success = xen_exchange_memory(1, order, &in_frame, 1UL << order,
  1807. 0, out_frames, 0);
  1808. /* 4. Map new pages in place of old pages. */
  1809. if (success)
  1810. xen_remap_exchanged_ptes(vstart, order, out_frames, 0);
  1811. else
  1812. xen_remap_exchanged_ptes(vstart, order, NULL, in_frame);
  1813. spin_unlock_irqrestore(&xen_reservation_lock, flags);
  1814. }
  1815. EXPORT_SYMBOL_GPL(xen_destroy_contiguous_region);
  1816. #ifdef CONFIG_XEN_PVHVM
  1817. static void xen_hvm_exit_mmap(struct mm_struct *mm)
  1818. {
  1819. struct xen_hvm_pagetable_dying a;
  1820. int rc;
  1821. a.domid = DOMID_SELF;
  1822. a.gpa = __pa(mm->pgd);
  1823. rc = HYPERVISOR_hvm_op(HVMOP_pagetable_dying, &a);
  1824. WARN_ON_ONCE(rc < 0);
  1825. }
  1826. static int is_pagetable_dying_supported(void)
  1827. {
  1828. struct xen_hvm_pagetable_dying a;
  1829. int rc = 0;
  1830. a.domid = DOMID_SELF;
  1831. a.gpa = 0x00;
  1832. rc = HYPERVISOR_hvm_op(HVMOP_pagetable_dying, &a);
  1833. if (rc < 0) {
  1834. printk(KERN_DEBUG "HVMOP_pagetable_dying not supported\n");
  1835. return 0;
  1836. }
  1837. return 1;
  1838. }
  1839. void __init xen_hvm_init_mmu_ops(void)
  1840. {
  1841. if (is_pagetable_dying_supported())
  1842. pv_mmu_ops.exit_mmap = xen_hvm_exit_mmap;
  1843. }
  1844. #endif
  1845. #define REMAP_BATCH_SIZE 16
  1846. struct remap_data {
  1847. unsigned long mfn;
  1848. pgprot_t prot;
  1849. struct mmu_update *mmu_update;
  1850. };
  1851. static int remap_area_mfn_pte_fn(pte_t *ptep, pgtable_t token,
  1852. unsigned long addr, void *data)
  1853. {
  1854. struct remap_data *rmd = data;
  1855. pte_t pte = pte_mkspecial(pfn_pte(rmd->mfn++, rmd->prot));
  1856. rmd->mmu_update->ptr = arbitrary_virt_to_machine(ptep).maddr;
  1857. rmd->mmu_update->val = pte_val_ma(pte);
  1858. rmd->mmu_update++;
  1859. return 0;
  1860. }
  1861. int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
  1862. unsigned long addr,
  1863. unsigned long mfn, int nr,
  1864. pgprot_t prot, unsigned domid)
  1865. {
  1866. struct remap_data rmd;
  1867. struct mmu_update mmu_update[REMAP_BATCH_SIZE];
  1868. int batch;
  1869. unsigned long range;
  1870. int err = 0;
  1871. prot = __pgprot(pgprot_val(prot) | _PAGE_IOMAP);
  1872. BUG_ON(!((vma->vm_flags & (VM_PFNMAP | VM_RESERVED | VM_IO)) ==
  1873. (VM_PFNMAP | VM_RESERVED | VM_IO)));
  1874. rmd.mfn = mfn;
  1875. rmd.prot = prot;
  1876. while (nr) {
  1877. batch = min(REMAP_BATCH_SIZE, nr);
  1878. range = (unsigned long)batch << PAGE_SHIFT;
  1879. rmd.mmu_update = mmu_update;
  1880. err = apply_to_page_range(vma->vm_mm, addr, range,
  1881. remap_area_mfn_pte_fn, &rmd);
  1882. if (err)
  1883. goto out;
  1884. err = -EFAULT;
  1885. if (HYPERVISOR_mmu_update(mmu_update, batch, NULL, domid) < 0)
  1886. goto out;
  1887. nr -= batch;
  1888. addr += range;
  1889. }
  1890. err = 0;
  1891. out:
  1892. flush_tlb_all();
  1893. return err;
  1894. }
  1895. EXPORT_SYMBOL_GPL(xen_remap_domain_mfn_range);
  1896. #ifdef CONFIG_XEN_DEBUG_FS
  1897. static struct dentry *d_mmu_debug;
  1898. static int __init xen_mmu_debugfs(void)
  1899. {
  1900. struct dentry *d_xen = xen_init_debugfs();
  1901. if (d_xen == NULL)
  1902. return -ENOMEM;
  1903. d_mmu_debug = debugfs_create_dir("mmu", d_xen);
  1904. debugfs_create_u8("zero_stats", 0644, d_mmu_debug, &zero_stats);
  1905. debugfs_create_u32("pgd_update", 0444, d_mmu_debug, &mmu_stats.pgd_update);
  1906. debugfs_create_u32("pgd_update_pinned", 0444, d_mmu_debug,
  1907. &mmu_stats.pgd_update_pinned);
  1908. debugfs_create_u32("pgd_update_batched", 0444, d_mmu_debug,
  1909. &mmu_stats.pgd_update_pinned);
  1910. debugfs_create_u32("pud_update", 0444, d_mmu_debug, &mmu_stats.pud_update);
  1911. debugfs_create_u32("pud_update_pinned", 0444, d_mmu_debug,
  1912. &mmu_stats.pud_update_pinned);
  1913. debugfs_create_u32("pud_update_batched", 0444, d_mmu_debug,
  1914. &mmu_stats.pud_update_pinned);
  1915. debugfs_create_u32("pmd_update", 0444, d_mmu_debug, &mmu_stats.pmd_update);
  1916. debugfs_create_u32("pmd_update_pinned", 0444, d_mmu_debug,
  1917. &mmu_stats.pmd_update_pinned);
  1918. debugfs_create_u32("pmd_update_batched", 0444, d_mmu_debug,
  1919. &mmu_stats.pmd_update_pinned);
  1920. debugfs_create_u32("pte_update", 0444, d_mmu_debug, &mmu_stats.pte_update);
  1921. // debugfs_create_u32("pte_update_pinned", 0444, d_mmu_debug,
  1922. // &mmu_stats.pte_update_pinned);
  1923. debugfs_create_u32("pte_update_batched", 0444, d_mmu_debug,
  1924. &mmu_stats.pte_update_pinned);
  1925. debugfs_create_u32("mmu_update", 0444, d_mmu_debug, &mmu_stats.mmu_update);
  1926. debugfs_create_u32("mmu_update_extended", 0444, d_mmu_debug,
  1927. &mmu_stats.mmu_update_extended);
  1928. xen_debugfs_create_u32_array("mmu_update_histo", 0444, d_mmu_debug,
  1929. mmu_stats.mmu_update_histo, 20);
  1930. debugfs_create_u32("set_pte_at", 0444, d_mmu_debug, &mmu_stats.set_pte_at);
  1931. debugfs_create_u32("set_pte_at_batched", 0444, d_mmu_debug,
  1932. &mmu_stats.set_pte_at_batched);
  1933. debugfs_create_u32("set_pte_at_current", 0444, d_mmu_debug,
  1934. &mmu_stats.set_pte_at_current);
  1935. debugfs_create_u32("set_pte_at_kernel", 0444, d_mmu_debug,
  1936. &mmu_stats.set_pte_at_kernel);
  1937. debugfs_create_u32("prot_commit", 0444, d_mmu_debug, &mmu_stats.prot_commit);
  1938. debugfs_create_u32("prot_commit_batched", 0444, d_mmu_debug,
  1939. &mmu_stats.prot_commit_batched);
  1940. return 0;
  1941. }
  1942. fs_initcall(xen_mmu_debugfs);
  1943. #endif /* CONFIG_XEN_DEBUG_FS */