pgtable.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. #ifndef _ASM_GENERIC_PGTABLE_H
  2. #define _ASM_GENERIC_PGTABLE_H
  3. #ifndef __ASSEMBLY__
  4. #ifdef CONFIG_MMU
  5. #include <linux/mm_types.h>
  6. #include <linux/bug.h>
  7. #include <linux/slab.h>
  8. #ifdef CONFIG_TIMA_RKP_L2_GROUP
  9. /* Structure of a grouped entry */
  10. typedef struct tima_l2group_entry {
  11. unsigned long addr;
  12. unsigned long linux_pte;
  13. unsigned long arm_pte;
  14. unsigned long padding;
  15. }tima_l2group_entry_t;
  16. #define RKP_MAX_PGT2_ENTRIES 0x100
  17. static inline void init_tima_rkp_group_buffers(unsigned long num_entries,
  18. pte_t *ptep,
  19. unsigned long *tima_l2group_flag_ptr,
  20. unsigned long *tima_l2group_buffer_index_ptr,
  21. tima_l2group_entry_t **buffer1,
  22. tima_l2group_entry_t **buffer2)
  23. {
  24. /* 0x200 = 512 bytes which is 2 L2 pages. If grouped
  25. * entries are <= 2, there is not much point in
  26. * grouping it, in which case follow the normal path.
  27. */
  28. if (num_entries > 2 && (num_entries <= (RKP_MAX_PGT2_ENTRIES<<1))
  29. && tima_is_pg_protected((unsigned long) ptep ) == 1) {
  30. *buffer1 = (tima_l2group_entry_t *)
  31. __get_free_pages(GFP_ATOMIC, 0);
  32. if (num_entries > RKP_MAX_PGT2_ENTRIES)
  33. *buffer2 = (tima_l2group_entry_t *)
  34. __get_free_pages(GFP_ATOMIC, 0);
  35. if (*buffer1 == NULL || ((num_entries > RKP_MAX_PGT2_ENTRIES)
  36. && (*buffer2 == NULL))) {
  37. printk(KERN_ERR"TIMA -> Could not group"
  38. "executing single L2 write %lx %s\n",
  39. num_entries, __FUNCTION__);
  40. if (*buffer1 != NULL)
  41. free_pages((unsigned long) *buffer1, 0);
  42. if (*buffer2 != NULL)
  43. free_pages((unsigned long) *buffer2, 0);
  44. } else {
  45. *tima_l2group_flag_ptr = 1;
  46. /* reset index here */
  47. *tima_l2group_buffer_index_ptr = 0;
  48. }
  49. }
  50. return;
  51. }
  52. static inline void write_tima_rkp_group_buffers(unsigned long num_entries,
  53. tima_l2group_entry_t **buffer1,
  54. tima_l2group_entry_t **buffer2)
  55. {
  56. /* Pass the buffer pointer and length to TIMA
  57. * to write the changes
  58. */
  59. if (num_entries) {
  60. if (num_entries > RKP_MAX_PGT2_ENTRIES) {
  61. timal2group_set_pte_commit(*buffer1, RKP_MAX_PGT2_ENTRIES);
  62. timal2group_set_pte_commit(*buffer2, (num_entries - RKP_MAX_PGT2_ENTRIES));
  63. } else
  64. timal2group_set_pte_commit(*buffer1, num_entries);
  65. }
  66. free_pages((unsigned long) *buffer1, 0);
  67. if (*buffer2 != NULL)
  68. free_pages((unsigned long) *buffer2, 0);
  69. }
  70. #endif /* CONFIG_TIMA_RKP_L2_GROUP */
  71. /*
  72. * On almost all architectures and configurations, 0 can be used as the
  73. * upper ceiling to free_pgtables(): on many architectures it has the same
  74. * effect as using TASK_SIZE. However, there is one configuration which
  75. * must impose a more careful limit, to avoid freeing kernel pgtables.
  76. */
  77. #ifndef USER_PGTABLES_CEILING
  78. #define USER_PGTABLES_CEILING 0UL
  79. #endif
  80. #ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  81. extern int ptep_set_access_flags(struct vm_area_struct *vma,
  82. unsigned long address, pte_t *ptep,
  83. pte_t entry, int dirty);
  84. #endif
  85. #ifndef __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
  86. extern int pmdp_set_access_flags(struct vm_area_struct *vma,
  87. unsigned long address, pmd_t *pmdp,
  88. pmd_t entry, int dirty);
  89. #endif
  90. #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  91. static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
  92. unsigned long address,
  93. pte_t *ptep)
  94. {
  95. pte_t pte = *ptep;
  96. int r = 1;
  97. if (!pte_young(pte))
  98. r = 0;
  99. else
  100. set_pte_at(vma->vm_mm, address, ptep, pte_mkold(pte));
  101. return r;
  102. }
  103. #endif
  104. #ifndef __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
  105. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  106. static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
  107. unsigned long address,
  108. pmd_t *pmdp)
  109. {
  110. pmd_t pmd = *pmdp;
  111. int r = 1;
  112. if (!pmd_young(pmd))
  113. r = 0;
  114. else
  115. set_pmd_at(vma->vm_mm, address, pmdp, pmd_mkold(pmd));
  116. return r;
  117. }
  118. #else /* CONFIG_TRANSPARENT_HUGEPAGE */
  119. static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
  120. unsigned long address,
  121. pmd_t *pmdp)
  122. {
  123. BUG();
  124. return 0;
  125. }
  126. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  127. #endif
  128. #ifndef __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  129. int ptep_clear_flush_young(struct vm_area_struct *vma,
  130. unsigned long address, pte_t *ptep);
  131. #endif
  132. #ifndef __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
  133. int pmdp_clear_flush_young(struct vm_area_struct *vma,
  134. unsigned long address, pmd_t *pmdp);
  135. #endif
  136. #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR
  137. #ifdef CONFIG_TIMA_RKP_L2_GROUP
  138. //extern unsigned long write_skip_count;
  139. static inline pte_t tima_l2group_ptep_get_and_clear(struct mm_struct *mm,
  140. unsigned long address,
  141. pte_t *ptep,
  142. unsigned long tima_l2group_entry_ptr,
  143. unsigned long *tima_l2group_buffer_index)
  144. {
  145. int ret;
  146. pte_t pte = *ptep;
  147. ret = timal2group_pte_clear(mm, address, ptep, tima_l2group_entry_ptr);
  148. if (ret == 0) {
  149. (*tima_l2group_buffer_index)++;
  150. }
  151. //pte_clear(mm, address, ptep); /* removed as grouping works */
  152. return pte;
  153. }
  154. #endif /* CONFIG_TIMA_RKP_L2_GROUP */
  155. static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
  156. unsigned long address,
  157. pte_t *ptep)
  158. {
  159. pte_t pte = *ptep;
  160. pte_clear(mm, address, ptep);
  161. return pte;
  162. }
  163. #endif
  164. #ifndef __HAVE_ARCH_PMDP_GET_AND_CLEAR
  165. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  166. static inline pmd_t pmdp_get_and_clear(struct mm_struct *mm,
  167. unsigned long address,
  168. pmd_t *pmdp)
  169. {
  170. pmd_t pmd = *pmdp;
  171. pmd_clear(mm, address, pmdp);
  172. return pmd;
  173. }
  174. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  175. #endif
  176. #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
  177. #ifdef CONFIG_TIMA_RKP_L2_GROUP
  178. static inline pte_t tima_l2group_ptep_get_and_clear_full(struct mm_struct *mm,
  179. unsigned long address, pte_t *ptep,
  180. int full,
  181. tima_l2group_entry_t *tima_l2group_buffer,
  182. unsigned long *tima_l2group_buffer_index,
  183. unsigned long tima_l2group_flag)
  184. {
  185. pte_t pte;
  186. if (tima_l2group_flag) {
  187. pte = tima_l2group_ptep_get_and_clear(mm, address, ptep,
  188. (((unsigned long) tima_l2group_buffer) +
  189. (sizeof(tima_l2group_entry_t)*(*tima_l2group_buffer_index))),
  190. tima_l2group_buffer_index);
  191. //(*tima_l2group_buffer_index)++;
  192. }
  193. else
  194. pte = ptep_get_and_clear(mm, address, ptep);
  195. return pte;
  196. }
  197. #endif /* CONFIG_TIMA_RKP_L2_GROUP */
  198. static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
  199. unsigned long address, pte_t *ptep,
  200. int full)
  201. {
  202. pte_t pte;
  203. pte = ptep_get_and_clear(mm, address, ptep);
  204. return pte;
  205. }
  206. #endif
  207. /*
  208. * Some architectures may be able to avoid expensive synchronization
  209. * primitives when modifications are made to PTE's which are already
  210. * not present, or in the process of an address space destruction.
  211. */
  212. #ifndef __HAVE_ARCH_PTE_CLEAR_NOT_PRESENT_FULL
  213. static inline void pte_clear_not_present_full(struct mm_struct *mm,
  214. unsigned long address,
  215. pte_t *ptep,
  216. int full)
  217. {
  218. pte_clear(mm, address, ptep);
  219. }
  220. #endif
  221. #ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH
  222. extern pte_t ptep_clear_flush(struct vm_area_struct *vma,
  223. unsigned long address,
  224. pte_t *ptep);
  225. #endif
  226. #ifndef __HAVE_ARCH_PMDP_CLEAR_FLUSH
  227. extern pmd_t pmdp_clear_flush(struct vm_area_struct *vma,
  228. unsigned long address,
  229. pmd_t *pmdp);
  230. #endif
  231. #ifndef __HAVE_ARCH_PTEP_SET_WRPROTECT
  232. struct mm_struct;
  233. static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep)
  234. {
  235. pte_t old_pte = *ptep;
  236. set_pte_at(mm, address, ptep, pte_wrprotect(old_pte));
  237. }
  238. #endif
  239. #ifndef __HAVE_ARCH_PMDP_SET_WRPROTECT
  240. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  241. static inline void pmdp_set_wrprotect(struct mm_struct *mm,
  242. unsigned long address, pmd_t *pmdp)
  243. {
  244. pmd_t old_pmd = *pmdp;
  245. set_pmd_at(mm, address, pmdp, pmd_wrprotect(old_pmd));
  246. }
  247. #else /* CONFIG_TRANSPARENT_HUGEPAGE */
  248. static inline void pmdp_set_wrprotect(struct mm_struct *mm,
  249. unsigned long address, pmd_t *pmdp)
  250. {
  251. BUG();
  252. }
  253. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  254. #endif
  255. #ifndef __HAVE_ARCH_PMDP_SPLITTING_FLUSH
  256. extern pmd_t pmdp_splitting_flush(struct vm_area_struct *vma,
  257. unsigned long address,
  258. pmd_t *pmdp);
  259. #endif
  260. #ifndef __HAVE_ARCH_PTE_SAME
  261. static inline int pte_same(pte_t pte_a, pte_t pte_b)
  262. {
  263. return pte_val(pte_a) == pte_val(pte_b);
  264. }
  265. #endif
  266. #ifndef __HAVE_ARCH_PMD_SAME
  267. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  268. static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
  269. {
  270. return pmd_val(pmd_a) == pmd_val(pmd_b);
  271. }
  272. #else /* CONFIG_TRANSPARENT_HUGEPAGE */
  273. static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
  274. {
  275. BUG();
  276. return 0;
  277. }
  278. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  279. #endif
  280. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_DIRTY
  281. #define page_test_and_clear_dirty(pfn, mapped) (0)
  282. #endif
  283. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_DIRTY
  284. #define pte_maybe_dirty(pte) pte_dirty(pte)
  285. #else
  286. #define pte_maybe_dirty(pte) (1)
  287. #endif
  288. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_YOUNG
  289. #define page_test_and_clear_young(pfn) (0)
  290. #endif
  291. #ifndef __HAVE_ARCH_PGD_OFFSET_GATE
  292. #define pgd_offset_gate(mm, addr) pgd_offset(mm, addr)
  293. #endif
  294. #ifndef __HAVE_ARCH_MOVE_PTE
  295. #define move_pte(pte, prot, old_addr, new_addr) (pte)
  296. #endif
  297. #ifndef flush_tlb_fix_spurious_fault
  298. #define flush_tlb_fix_spurious_fault(vma, address) flush_tlb_page(vma, address)
  299. #endif
  300. #ifndef pgprot_noncached
  301. #define pgprot_noncached(prot) (prot)
  302. #endif
  303. #ifndef pgprot_writecombine
  304. #define pgprot_writecombine pgprot_noncached
  305. #endif
  306. /*
  307. * When walking page tables, get the address of the next boundary,
  308. * or the end address of the range if that comes earlier. Although no
  309. * vma end wraps to 0, rounded up __boundary may wrap to 0 throughout.
  310. */
  311. #define pgd_addr_end(addr, end) \
  312. ({ unsigned long __boundary = ((addr) + PGDIR_SIZE) & PGDIR_MASK; \
  313. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  314. })
  315. #ifndef pud_addr_end
  316. #define pud_addr_end(addr, end) \
  317. ({ unsigned long __boundary = ((addr) + PUD_SIZE) & PUD_MASK; \
  318. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  319. })
  320. #endif
  321. #ifndef pmd_addr_end
  322. #define pmd_addr_end(addr, end) \
  323. ({ unsigned long __boundary = ((addr) + PMD_SIZE) & PMD_MASK; \
  324. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  325. })
  326. #endif
  327. /*
  328. * When walking page tables, we usually want to skip any p?d_none entries;
  329. * and any p?d_bad entries - reporting the error before resetting to none.
  330. * Do the tests inline, but report and clear the bad entry in mm/memory.c.
  331. */
  332. void pgd_clear_bad(pgd_t *);
  333. void pud_clear_bad(pud_t *);
  334. void pmd_clear_bad(pmd_t *);
  335. static inline int pgd_none_or_clear_bad(pgd_t *pgd)
  336. {
  337. if (pgd_none(*pgd))
  338. return 1;
  339. if (unlikely(pgd_bad(*pgd))) {
  340. pgd_clear_bad(pgd);
  341. return 1;
  342. }
  343. return 0;
  344. }
  345. static inline int pud_none_or_clear_bad(pud_t *pud)
  346. {
  347. if (pud_none(*pud))
  348. return 1;
  349. if (unlikely(pud_bad(*pud))) {
  350. pud_clear_bad(pud);
  351. return 1;
  352. }
  353. return 0;
  354. }
  355. static inline int pmd_none_or_clear_bad(pmd_t *pmd)
  356. {
  357. if (pmd_none(*pmd))
  358. return 1;
  359. if (unlikely(pmd_bad(*pmd))) {
  360. pmd_clear_bad(pmd);
  361. return 1;
  362. }
  363. return 0;
  364. }
  365. #ifdef CONFIG_TIMA_RKP_L2_GROUP
  366. static inline pte_t __tima_l2group_ptep_modify_prot_start(
  367. struct mm_struct *mm,
  368. unsigned long addr,
  369. pte_t *ptep,
  370. unsigned long tima_l2group_entry_ptr,
  371. unsigned long *tima_l2group_buffer_index)
  372. {
  373. return tima_l2group_ptep_get_and_clear(mm, addr, ptep,
  374. tima_l2group_entry_ptr,
  375. tima_l2group_buffer_index);
  376. }
  377. #endif /* CONFIG_TIMA_RKP_L2_GROUP */
  378. static inline pte_t __ptep_modify_prot_start(struct mm_struct *mm,
  379. unsigned long addr,
  380. pte_t *ptep)
  381. {
  382. /*
  383. * Get the current pte state, but zero it out to make it
  384. * non-present, preventing the hardware from asynchronously
  385. * updating it.
  386. */
  387. return ptep_get_and_clear(mm, addr, ptep);
  388. }
  389. #ifdef CONFIG_TIMA_RKP_L2_GROUP
  390. static inline void __tima_l2group_ptep_modify_prot_commit(
  391. struct mm_struct *mm,
  392. unsigned long addr,
  393. pte_t *ptep, pte_t pte,
  394. unsigned long tima_l2group_entry_ptr,
  395. unsigned long *tima_l2group_buffer_index)
  396. {
  397. timal2group_set_pte_at(ptep, pte, tima_l2group_entry_ptr, addr, tima_l2group_buffer_index);
  398. //set_pte_at(mm, addr, ptep, pte); /* removed as grouping works */
  399. }
  400. #endif /* CONFIG_TIMA_RKP_L2_GROUP */
  401. static inline void __ptep_modify_prot_commit(struct mm_struct *mm,
  402. unsigned long addr,
  403. pte_t *ptep, pte_t pte)
  404. {
  405. /*
  406. * The pte is non-present, so there's no hardware state to
  407. * preserve.
  408. */
  409. set_pte_at(mm, addr, ptep, pte);
  410. }
  411. #ifndef __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
  412. /*
  413. * Start a pte protection read-modify-write transaction, which
  414. * protects against asynchronous hardware modifications to the pte.
  415. * The intention is not to prevent the hardware from making pte
  416. * updates, but to prevent any updates it may make from being lost.
  417. *
  418. * This does not protect against other software modifications of the
  419. * pte; the appropriate pte lock must be held over the transation.
  420. *
  421. * Note that this interface is intended to be batchable, meaning that
  422. * ptep_modify_prot_commit may not actually update the pte, but merely
  423. * queue the update to be done at some later time. The update must be
  424. * actually committed before the pte lock is released, however.
  425. */
  426. #ifdef CONFIG_TIMA_RKP_L2_GROUP
  427. static inline pte_t tima_l2group_ptep_modify_prot_start(
  428. struct mm_struct *mm,
  429. unsigned long addr,
  430. pte_t *ptep,
  431. tima_l2group_entry_t *tima_l2group_buffer,
  432. unsigned long *tima_l2group_buffer_index,
  433. unsigned long tima_l2group_flag)
  434. {
  435. if(tima_l2group_flag) {
  436. pte_t ret_pte = __tima_l2group_ptep_modify_prot_start(mm, addr, ptep,
  437. (((unsigned long) tima_l2group_buffer) +
  438. (sizeof(tima_l2group_entry_t)*(*tima_l2group_buffer_index))),
  439. tima_l2group_buffer_index);
  440. (*tima_l2group_buffer_index)++;
  441. return ret_pte;
  442. }
  443. else
  444. return __ptep_modify_prot_start(mm, addr, ptep);
  445. }
  446. #endif /* CONFIG_TIMA_RKP_L2_GROUP */
  447. static inline pte_t ptep_modify_prot_start(struct mm_struct *mm,
  448. unsigned long addr,
  449. pte_t *ptep)
  450. {
  451. return __ptep_modify_prot_start(mm, addr, ptep);
  452. }
  453. /*
  454. * Commit an update to a pte, leaving any hardware-controlled bits in
  455. * the PTE unmodified.
  456. */
  457. #ifdef CONFIG_TIMA_RKP_L2_GROUP
  458. static inline void tima_l2group_ptep_modify_prot_commit(struct mm_struct *mm,
  459. unsigned long addr, pte_t *ptep, pte_t pte,
  460. tima_l2group_entry_t *tima_l2group_buffer1,
  461. tima_l2group_entry_t *tima_l2group_buffer2,
  462. unsigned long *tima_l2group_buffer_index,
  463. unsigned long tima_l2group_flag)
  464. {
  465. if(tima_l2group_flag) {
  466. if (*tima_l2group_buffer_index < RKP_MAX_PGT2_ENTRIES) {
  467. __tima_l2group_ptep_modify_prot_commit(mm, addr, ptep, pte,
  468. (((unsigned long) tima_l2group_buffer1) +
  469. (sizeof(tima_l2group_entry_t)*(*tima_l2group_buffer_index))),
  470. tima_l2group_buffer_index);
  471. } else {
  472. __tima_l2group_ptep_modify_prot_commit(mm, addr, ptep, pte,
  473. (((unsigned long) tima_l2group_buffer2) +
  474. (sizeof(tima_l2group_entry_t)*((*tima_l2group_buffer_index) - RKP_MAX_PGT2_ENTRIES))),
  475. tima_l2group_buffer_index);
  476. }
  477. }
  478. else
  479. __ptep_modify_prot_commit(mm, addr, ptep, pte);
  480. }
  481. #endif /* CONFIG_TIMA_RKP_L2_GROUP */
  482. static inline void ptep_modify_prot_commit(struct mm_struct *mm,
  483. unsigned long addr,
  484. pte_t *ptep, pte_t pte)
  485. {
  486. __ptep_modify_prot_commit(mm, addr, ptep, pte);
  487. }
  488. #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
  489. #endif /* CONFIG_MMU */
  490. /*
  491. * A facility to provide lazy MMU batching. This allows PTE updates and
  492. * page invalidations to be delayed until a call to leave lazy MMU mode
  493. * is issued. Some architectures may benefit from doing this, and it is
  494. * beneficial for both shadow and direct mode hypervisors, which may batch
  495. * the PTE updates which happen during this window. Note that using this
  496. * interface requires that read hazards be removed from the code. A read
  497. * hazard could result in the direct mode hypervisor case, since the actual
  498. * write to the page tables may not yet have taken place, so reads though
  499. * a raw PTE pointer after it has been modified are not guaranteed to be
  500. * up to date. This mode can only be entered and left under the protection of
  501. * the page table locks for all page tables which may be modified. In the UP
  502. * case, this is required so that preemption is disabled, and in the SMP case,
  503. * it must synchronize the delayed page table writes properly on other CPUs.
  504. */
  505. #ifndef __HAVE_ARCH_ENTER_LAZY_MMU_MODE
  506. #define arch_enter_lazy_mmu_mode() do {} while (0)
  507. #define arch_leave_lazy_mmu_mode() do {} while (0)
  508. #define arch_flush_lazy_mmu_mode() do {} while (0)
  509. #endif
  510. /*
  511. * A facility to provide batching of the reload of page tables and
  512. * other process state with the actual context switch code for
  513. * paravirtualized guests. By convention, only one of the batched
  514. * update (lazy) modes (CPU, MMU) should be active at any given time,
  515. * entry should never be nested, and entry and exits should always be
  516. * paired. This is for sanity of maintaining and reasoning about the
  517. * kernel code. In this case, the exit (end of the context switch) is
  518. * in architecture-specific code, and so doesn't need a generic
  519. * definition.
  520. */
  521. #ifndef __HAVE_ARCH_START_CONTEXT_SWITCH
  522. #define arch_start_context_switch(prev) do {} while (0)
  523. #endif
  524. #ifndef __HAVE_PFNMAP_TRACKING
  525. /*
  526. * Interface that can be used by architecture code to keep track of
  527. * memory type of pfn mappings (remap_pfn_range, vm_insert_pfn)
  528. *
  529. * track_pfn_vma_new is called when a _new_ pfn mapping is being established
  530. * for physical range indicated by pfn and size.
  531. */
  532. static inline int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
  533. unsigned long pfn, unsigned long size)
  534. {
  535. return 0;
  536. }
  537. /*
  538. * Interface that can be used by architecture code to keep track of
  539. * memory type of pfn mappings (remap_pfn_range, vm_insert_pfn)
  540. *
  541. * track_pfn_vma_copy is called when vma that is covering the pfnmap gets
  542. * copied through copy_page_range().
  543. */
  544. static inline int track_pfn_vma_copy(struct vm_area_struct *vma)
  545. {
  546. return 0;
  547. }
  548. /*
  549. * Interface that can be used by architecture code to keep track of
  550. * memory type of pfn mappings (remap_pfn_range, vm_insert_pfn)
  551. *
  552. * untrack_pfn_vma is called while unmapping a pfnmap for a region.
  553. * untrack can be called for a specific region indicated by pfn and size or
  554. * can be for the entire vma (in which case size can be zero).
  555. */
  556. static inline void untrack_pfn_vma(struct vm_area_struct *vma,
  557. unsigned long pfn, unsigned long size)
  558. {
  559. }
  560. #else
  561. extern int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
  562. unsigned long pfn, unsigned long size);
  563. extern int track_pfn_vma_copy(struct vm_area_struct *vma);
  564. extern void untrack_pfn_vma(struct vm_area_struct *vma, unsigned long pfn,
  565. unsigned long size);
  566. #endif
  567. #ifdef CONFIG_MMU
  568. #ifndef CONFIG_TRANSPARENT_HUGEPAGE
  569. static inline int pmd_trans_huge(pmd_t pmd)
  570. {
  571. return 0;
  572. }
  573. static inline int pmd_trans_splitting(pmd_t pmd)
  574. {
  575. return 0;
  576. }
  577. #ifndef __HAVE_ARCH_PMD_WRITE
  578. static inline int pmd_write(pmd_t pmd)
  579. {
  580. BUG();
  581. return 0;
  582. }
  583. #endif /* __HAVE_ARCH_PMD_WRITE */
  584. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  585. #ifndef pmd_read_atomic
  586. static inline pmd_t pmd_read_atomic(pmd_t *pmdp)
  587. {
  588. /*
  589. * Depend on compiler for an atomic pmd read. NOTE: this is
  590. * only going to work, if the pmdval_t isn't larger than
  591. * an unsigned long.
  592. */
  593. return *pmdp;
  594. }
  595. #endif
  596. /*
  597. * This function is meant to be used by sites walking pagetables with
  598. * the mmap_sem hold in read mode to protect against MADV_DONTNEED and
  599. * transhuge page faults. MADV_DONTNEED can convert a transhuge pmd
  600. * into a null pmd and the transhuge page fault can convert a null pmd
  601. * into an hugepmd or into a regular pmd (if the hugepage allocation
  602. * fails). While holding the mmap_sem in read mode the pmd becomes
  603. * stable and stops changing under us only if it's not null and not a
  604. * transhuge pmd. When those races occurs and this function makes a
  605. * difference vs the standard pmd_none_or_clear_bad, the result is
  606. * undefined so behaving like if the pmd was none is safe (because it
  607. * can return none anyway). The compiler level barrier() is critically
  608. * important to compute the two checks atomically on the same pmdval.
  609. *
  610. * For 32bit kernels with a 64bit large pmd_t this automatically takes
  611. * care of reading the pmd atomically to avoid SMP race conditions
  612. * against pmd_populate() when the mmap_sem is hold for reading by the
  613. * caller (a special atomic read not done by "gcc" as in the generic
  614. * version above, is also needed when THP is disabled because the page
  615. * fault can populate the pmd from under us).
  616. */
  617. static inline int pmd_none_or_trans_huge_or_clear_bad(pmd_t *pmd)
  618. {
  619. pmd_t pmdval = pmd_read_atomic(pmd);
  620. /*
  621. * The barrier will stabilize the pmdval in a register or on
  622. * the stack so that it will stop changing under the code.
  623. *
  624. * When CONFIG_TRANSPARENT_HUGEPAGE=y on x86 32bit PAE,
  625. * pmd_read_atomic is allowed to return a not atomic pmdval
  626. * (for example pointing to an hugepage that has never been
  627. * mapped in the pmd). The below checks will only care about
  628. * the low part of the pmd with 32bit PAE x86 anyway, with the
  629. * exception of pmd_none(). So the important thing is that if
  630. * the low part of the pmd is found null, the high part will
  631. * be also null or the pmd_none() check below would be
  632. * confused.
  633. */
  634. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  635. barrier();
  636. #endif
  637. if (pmd_none(pmdval) || pmd_trans_huge(pmdval))
  638. return 1;
  639. if (unlikely(pmd_bad(pmdval))) {
  640. pmd_clear_bad(pmd);
  641. return 1;
  642. }
  643. return 0;
  644. }
  645. /*
  646. * This is a noop if Transparent Hugepage Support is not built into
  647. * the kernel. Otherwise it is equivalent to
  648. * pmd_none_or_trans_huge_or_clear_bad(), and shall only be called in
  649. * places that already verified the pmd is not none and they want to
  650. * walk ptes while holding the mmap sem in read mode (write mode don't
  651. * need this). If THP is not enabled, the pmd can't go away under the
  652. * code even if MADV_DONTNEED runs, but if THP is enabled we need to
  653. * run a pmd_trans_unstable before walking the ptes after
  654. * split_huge_page_pmd returns (because it may have run when the pmd
  655. * become null, but then a page fault can map in a THP and not a
  656. * regular page).
  657. */
  658. static inline int pmd_trans_unstable(pmd_t *pmd)
  659. {
  660. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  661. return pmd_none_or_trans_huge_or_clear_bad(pmd);
  662. #else
  663. return 0;
  664. #endif
  665. }
  666. #endif /* CONFIG_MMU */
  667. #endif /* !__ASSEMBLY__ */
  668. #endif /* _ASM_GENERIC_PGTABLE_H */