mmu.c 68 KB

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