pgtable.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /*
  2. * include/asm-s390/pgtable.h
  3. *
  4. * S390 version
  5. * Copyright (C) 1999,2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
  6. * Author(s): Hartmut Penner (hp@de.ibm.com)
  7. * Ulrich Weigand (weigand@de.ibm.com)
  8. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  9. *
  10. * Derived from "include/asm-i386/pgtable.h"
  11. */
  12. #ifndef _ASM_S390_PGTABLE_H
  13. #define _ASM_S390_PGTABLE_H
  14. #include <asm-generic/4level-fixup.h>
  15. /*
  16. * The Linux memory management assumes a three-level page table setup. For
  17. * s390 31 bit we "fold" the mid level into the top-level page table, so
  18. * that we physically have the same two-level page table as the s390 mmu
  19. * expects in 31 bit mode. For s390 64 bit we use three of the five levels
  20. * the hardware provides (region first and region second tables are not
  21. * used).
  22. *
  23. * The "pgd_xxx()" functions are trivial for a folded two-level
  24. * setup: the pgd is never bad, and a pmd always exists (as it's folded
  25. * into the pgd entry)
  26. *
  27. * This file contains the functions and defines necessary to modify and use
  28. * the S390 page table tree.
  29. */
  30. #ifndef __ASSEMBLY__
  31. #include <linux/mm_types.h>
  32. #include <asm/bug.h>
  33. #include <asm/processor.h>
  34. struct vm_area_struct; /* forward declaration (include/linux/mm.h) */
  35. struct mm_struct;
  36. extern pgd_t swapper_pg_dir[] __attribute__ ((aligned (4096)));
  37. extern void paging_init(void);
  38. /*
  39. * The S390 doesn't have any external MMU info: the kernel page
  40. * tables contain all the necessary information.
  41. */
  42. #define update_mmu_cache(vma, address, pte) do { } while (0)
  43. /*
  44. * ZERO_PAGE is a global shared page that is always zero: used
  45. * for zero-mapped memory areas etc..
  46. */
  47. extern char empty_zero_page[PAGE_SIZE];
  48. #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
  49. #endif /* !__ASSEMBLY__ */
  50. /*
  51. * PMD_SHIFT determines the size of the area a second-level page
  52. * table can map
  53. * PGDIR_SHIFT determines what a third-level page table entry can map
  54. */
  55. #ifndef __s390x__
  56. # define PMD_SHIFT 22
  57. # define PGDIR_SHIFT 22
  58. #else /* __s390x__ */
  59. # define PMD_SHIFT 21
  60. # define PGDIR_SHIFT 31
  61. #endif /* __s390x__ */
  62. #define PMD_SIZE (1UL << PMD_SHIFT)
  63. #define PMD_MASK (~(PMD_SIZE-1))
  64. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  65. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  66. /*
  67. * entries per page directory level: the S390 is two-level, so
  68. * we don't really have any PMD directory physically.
  69. * for S390 segment-table entries are combined to one PGD
  70. * that leads to 1024 pte per pgd
  71. */
  72. #ifndef __s390x__
  73. # define PTRS_PER_PTE 1024
  74. # define PTRS_PER_PMD 1
  75. # define PTRS_PER_PGD 512
  76. #else /* __s390x__ */
  77. # define PTRS_PER_PTE 512
  78. # define PTRS_PER_PMD 1024
  79. # define PTRS_PER_PGD 2048
  80. #endif /* __s390x__ */
  81. #define FIRST_USER_ADDRESS 0
  82. #define pte_ERROR(e) \
  83. printk("%s:%d: bad pte %p.\n", __FILE__, __LINE__, (void *) pte_val(e))
  84. #define pmd_ERROR(e) \
  85. printk("%s:%d: bad pmd %p.\n", __FILE__, __LINE__, (void *) pmd_val(e))
  86. #define pgd_ERROR(e) \
  87. printk("%s:%d: bad pgd %p.\n", __FILE__, __LINE__, (void *) pgd_val(e))
  88. #ifndef __ASSEMBLY__
  89. /*
  90. * Just any arbitrary offset to the start of the vmalloc VM area: the
  91. * current 8MB value just means that there will be a 8MB "hole" after the
  92. * physical memory until the kernel virtual memory starts. That means that
  93. * any out-of-bounds memory accesses will hopefully be caught.
  94. * The vmalloc() routines leaves a hole of 4kB between each vmalloced
  95. * area for the same reason. ;)
  96. */
  97. #define VMALLOC_OFFSET (8*1024*1024)
  98. #define VMALLOC_START (((unsigned long) high_memory + VMALLOC_OFFSET) \
  99. & ~(VMALLOC_OFFSET-1))
  100. #ifndef __s390x__
  101. # define VMALLOC_END (0x7fffffffL)
  102. #else /* __s390x__ */
  103. # define VMALLOC_END (0x40000000000L)
  104. #endif /* __s390x__ */
  105. /*
  106. * A 31 bit pagetable entry of S390 has following format:
  107. * | PFRA | | OS |
  108. * 0 0IP0
  109. * 00000000001111111111222222222233
  110. * 01234567890123456789012345678901
  111. *
  112. * I Page-Invalid Bit: Page is not available for address-translation
  113. * P Page-Protection Bit: Store access not possible for page
  114. *
  115. * A 31 bit segmenttable entry of S390 has following format:
  116. * | P-table origin | |PTL
  117. * 0 IC
  118. * 00000000001111111111222222222233
  119. * 01234567890123456789012345678901
  120. *
  121. * I Segment-Invalid Bit: Segment is not available for address-translation
  122. * C Common-Segment Bit: Segment is not private (PoP 3-30)
  123. * PTL Page-Table-Length: Page-table length (PTL+1*16 entries -> up to 256)
  124. *
  125. * The 31 bit segmenttable origin of S390 has following format:
  126. *
  127. * |S-table origin | | STL |
  128. * X **GPS
  129. * 00000000001111111111222222222233
  130. * 01234567890123456789012345678901
  131. *
  132. * X Space-Switch event:
  133. * G Segment-Invalid Bit: *
  134. * P Private-Space Bit: Segment is not private (PoP 3-30)
  135. * S Storage-Alteration:
  136. * STL Segment-Table-Length: Segment-table length (STL+1*16 entries -> up to 2048)
  137. *
  138. * A 64 bit pagetable entry of S390 has following format:
  139. * | PFRA |0IP0| OS |
  140. * 0000000000111111111122222222223333333333444444444455555555556666
  141. * 0123456789012345678901234567890123456789012345678901234567890123
  142. *
  143. * I Page-Invalid Bit: Page is not available for address-translation
  144. * P Page-Protection Bit: Store access not possible for page
  145. *
  146. * A 64 bit segmenttable entry of S390 has following format:
  147. * | P-table origin | TT
  148. * 0000000000111111111122222222223333333333444444444455555555556666
  149. * 0123456789012345678901234567890123456789012345678901234567890123
  150. *
  151. * I Segment-Invalid Bit: Segment is not available for address-translation
  152. * C Common-Segment Bit: Segment is not private (PoP 3-30)
  153. * P Page-Protection Bit: Store access not possible for page
  154. * TT Type 00
  155. *
  156. * A 64 bit region table entry of S390 has following format:
  157. * | S-table origin | TF TTTL
  158. * 0000000000111111111122222222223333333333444444444455555555556666
  159. * 0123456789012345678901234567890123456789012345678901234567890123
  160. *
  161. * I Segment-Invalid Bit: Segment is not available for address-translation
  162. * TT Type 01
  163. * TF
  164. * TL Table lenght
  165. *
  166. * The 64 bit regiontable origin of S390 has following format:
  167. * | region table origon | DTTL
  168. * 0000000000111111111122222222223333333333444444444455555555556666
  169. * 0123456789012345678901234567890123456789012345678901234567890123
  170. *
  171. * X Space-Switch event:
  172. * G Segment-Invalid Bit:
  173. * P Private-Space Bit:
  174. * S Storage-Alteration:
  175. * R Real space
  176. * TL Table-Length:
  177. *
  178. * A storage key has the following format:
  179. * | ACC |F|R|C|0|
  180. * 0 3 4 5 6 7
  181. * ACC: access key
  182. * F : fetch protection bit
  183. * R : referenced bit
  184. * C : changed bit
  185. */
  186. /* Hardware bits in the page table entry */
  187. #define _PAGE_RO 0x200 /* HW read-only bit */
  188. #define _PAGE_INVALID 0x400 /* HW invalid bit */
  189. #define _PAGE_SWT 0x001 /* SW pte type bit t */
  190. #define _PAGE_SWX 0x002 /* SW pte type bit x */
  191. /* Six different types of pages. */
  192. #define _PAGE_TYPE_EMPTY 0x400
  193. #define _PAGE_TYPE_NONE 0x401
  194. #define _PAGE_TYPE_SWAP 0x403
  195. #define _PAGE_TYPE_FILE 0x601 /* bit 0x002 is used for offset !! */
  196. #define _PAGE_TYPE_RO 0x200
  197. #define _PAGE_TYPE_RW 0x000
  198. /*
  199. * PTE type bits are rather complicated. handle_pte_fault uses pte_present,
  200. * pte_none and pte_file to find out the pte type WITHOUT holding the page
  201. * table lock. ptep_clear_flush on the other hand uses ptep_clear_flush to
  202. * invalidate a given pte. ipte sets the hw invalid bit and clears all tlbs
  203. * for the page. The page table entry is set to _PAGE_TYPE_EMPTY afterwards.
  204. * This change is done while holding the lock, but the intermediate step
  205. * of a previously valid pte with the hw invalid bit set can be observed by
  206. * handle_pte_fault. That makes it necessary that all valid pte types with
  207. * the hw invalid bit set must be distinguishable from the four pte types
  208. * empty, none, swap and file.
  209. *
  210. * irxt ipte irxt
  211. * _PAGE_TYPE_EMPTY 1000 -> 1000
  212. * _PAGE_TYPE_NONE 1001 -> 1001
  213. * _PAGE_TYPE_SWAP 1011 -> 1011
  214. * _PAGE_TYPE_FILE 11?1 -> 11?1
  215. * _PAGE_TYPE_RO 0100 -> 1100
  216. * _PAGE_TYPE_RW 0000 -> 1000
  217. *
  218. * pte_none is true for bits combinations 1000, 1100
  219. * pte_present is true for bits combinations 0000, 0010, 0100, 0110, 1001
  220. * pte_file is true for bits combinations 1101, 1111
  221. * swap pte is 1011 and 0001, 0011, 0101, 0111, 1010 and 1110 are invalid.
  222. */
  223. #ifndef __s390x__
  224. /* Bits in the segment table entry */
  225. #define _PAGE_TABLE_LEN 0xf /* only full page-tables */
  226. #define _PAGE_TABLE_COM 0x10 /* common page-table */
  227. #define _PAGE_TABLE_INV 0x20 /* invalid page-table */
  228. #define _SEG_PRESENT 0x001 /* Software (overlap with PTL) */
  229. /* Bits int the storage key */
  230. #define _PAGE_CHANGED 0x02 /* HW changed bit */
  231. #define _PAGE_REFERENCED 0x04 /* HW referenced bit */
  232. #define _USER_SEG_TABLE_LEN 0x7f /* user-segment-table up to 2 GB */
  233. #define _KERNEL_SEG_TABLE_LEN 0x7f /* kernel-segment-table up to 2 GB */
  234. /*
  235. * User and Kernel pagetables are identical
  236. */
  237. #define _PAGE_TABLE _PAGE_TABLE_LEN
  238. #define _KERNPG_TABLE _PAGE_TABLE_LEN
  239. /*
  240. * The Kernel segment-tables includes the User segment-table
  241. */
  242. #define _SEGMENT_TABLE (_USER_SEG_TABLE_LEN|0x80000000|0x100)
  243. #define _KERNSEG_TABLE _KERNEL_SEG_TABLE_LEN
  244. #define USER_STD_MASK 0x00000080UL
  245. #else /* __s390x__ */
  246. /* Bits in the segment table entry */
  247. #define _PMD_ENTRY_INV 0x20 /* invalid segment table entry */
  248. #define _PMD_ENTRY 0x00
  249. /* Bits in the region third table entry */
  250. #define _PGD_ENTRY_INV 0x20 /* invalid region table entry */
  251. #define _PGD_ENTRY 0x07
  252. /*
  253. * User and kernel page directory
  254. */
  255. #define _REGION_THIRD 0x4
  256. #define _REGION_THIRD_LEN 0x3
  257. #define _REGION_TABLE (_REGION_THIRD|_REGION_THIRD_LEN|0x40|0x100)
  258. #define _KERN_REGION_TABLE (_REGION_THIRD|_REGION_THIRD_LEN)
  259. #define USER_STD_MASK 0x0000000000000080UL
  260. /* Bits in the storage key */
  261. #define _PAGE_CHANGED 0x02 /* HW changed bit */
  262. #define _PAGE_REFERENCED 0x04 /* HW referenced bit */
  263. #endif /* __s390x__ */
  264. /*
  265. * Page protection definitions.
  266. */
  267. #define PAGE_NONE __pgprot(_PAGE_TYPE_NONE)
  268. #define PAGE_RO __pgprot(_PAGE_TYPE_RO)
  269. #define PAGE_RW __pgprot(_PAGE_TYPE_RW)
  270. #define PAGE_KERNEL PAGE_RW
  271. #define PAGE_COPY PAGE_RO
  272. /*
  273. * The S390 can't do page protection for execute, and considers that the
  274. * same are read. Also, write permissions imply read permissions. This is
  275. * the closest we can get..
  276. */
  277. /*xwr*/
  278. #define __P000 PAGE_NONE
  279. #define __P001 PAGE_RO
  280. #define __P010 PAGE_RO
  281. #define __P011 PAGE_RO
  282. #define __P100 PAGE_RO
  283. #define __P101 PAGE_RO
  284. #define __P110 PAGE_RO
  285. #define __P111 PAGE_RO
  286. #define __S000 PAGE_NONE
  287. #define __S001 PAGE_RO
  288. #define __S010 PAGE_RW
  289. #define __S011 PAGE_RW
  290. #define __S100 PAGE_RO
  291. #define __S101 PAGE_RO
  292. #define __S110 PAGE_RW
  293. #define __S111 PAGE_RW
  294. /*
  295. * Certain architectures need to do special things when PTEs
  296. * within a page table are directly modified. Thus, the following
  297. * hook is made available.
  298. */
  299. static inline void set_pte(pte_t *pteptr, pte_t pteval)
  300. {
  301. *pteptr = pteval;
  302. }
  303. #define set_pte_at(mm,addr,ptep,pteval) set_pte(ptep,pteval)
  304. /*
  305. * pgd/pmd/pte query functions
  306. */
  307. #ifndef __s390x__
  308. static inline int pgd_present(pgd_t pgd) { return 1; }
  309. static inline int pgd_none(pgd_t pgd) { return 0; }
  310. static inline int pgd_bad(pgd_t pgd) { return 0; }
  311. static inline int pmd_present(pmd_t pmd) { return pmd_val(pmd) & _SEG_PRESENT; }
  312. static inline int pmd_none(pmd_t pmd) { return pmd_val(pmd) & _PAGE_TABLE_INV; }
  313. static inline int pmd_bad(pmd_t pmd)
  314. {
  315. return (pmd_val(pmd) & (~PAGE_MASK & ~_PAGE_TABLE_INV)) != _PAGE_TABLE;
  316. }
  317. #else /* __s390x__ */
  318. static inline int pgd_present(pgd_t pgd)
  319. {
  320. return (pgd_val(pgd) & ~PAGE_MASK) == _PGD_ENTRY;
  321. }
  322. static inline int pgd_none(pgd_t pgd)
  323. {
  324. return pgd_val(pgd) & _PGD_ENTRY_INV;
  325. }
  326. static inline int pgd_bad(pgd_t pgd)
  327. {
  328. return (pgd_val(pgd) & (~PAGE_MASK & ~_PGD_ENTRY_INV)) != _PGD_ENTRY;
  329. }
  330. static inline int pmd_present(pmd_t pmd)
  331. {
  332. return (pmd_val(pmd) & ~PAGE_MASK) == _PMD_ENTRY;
  333. }
  334. static inline int pmd_none(pmd_t pmd)
  335. {
  336. return pmd_val(pmd) & _PMD_ENTRY_INV;
  337. }
  338. static inline int pmd_bad(pmd_t pmd)
  339. {
  340. return (pmd_val(pmd) & (~PAGE_MASK & ~_PMD_ENTRY_INV)) != _PMD_ENTRY;
  341. }
  342. #endif /* __s390x__ */
  343. static inline int pte_none(pte_t pte)
  344. {
  345. return (pte_val(pte) & _PAGE_INVALID) && !(pte_val(pte) & _PAGE_SWT);
  346. }
  347. static inline int pte_present(pte_t pte)
  348. {
  349. unsigned long mask = _PAGE_RO | _PAGE_INVALID | _PAGE_SWT | _PAGE_SWX;
  350. return (pte_val(pte) & mask) == _PAGE_TYPE_NONE ||
  351. (!(pte_val(pte) & _PAGE_INVALID) &&
  352. !(pte_val(pte) & _PAGE_SWT));
  353. }
  354. static inline int pte_file(pte_t pte)
  355. {
  356. unsigned long mask = _PAGE_RO | _PAGE_INVALID | _PAGE_SWT;
  357. return (pte_val(pte) & mask) == _PAGE_TYPE_FILE;
  358. }
  359. #define pte_same(a,b) (pte_val(a) == pte_val(b))
  360. /*
  361. * query functions pte_write/pte_dirty/pte_young only work if
  362. * pte_present() is true. Undefined behaviour if not..
  363. */
  364. static inline int pte_write(pte_t pte)
  365. {
  366. return (pte_val(pte) & _PAGE_RO) == 0;
  367. }
  368. static inline int pte_dirty(pte_t pte)
  369. {
  370. /* A pte is neither clean nor dirty on s/390. The dirty bit
  371. * is in the storage key. See page_test_and_clear_dirty for
  372. * details.
  373. */
  374. return 0;
  375. }
  376. static inline int pte_young(pte_t pte)
  377. {
  378. /* A pte is neither young nor old on s/390. The young bit
  379. * is in the storage key. See page_test_and_clear_young for
  380. * details.
  381. */
  382. return 0;
  383. }
  384. static inline int pte_read(pte_t pte)
  385. {
  386. /* All pages are readable since we don't use the fetch
  387. * protection bit in the storage key.
  388. */
  389. return 1;
  390. }
  391. /*
  392. * pgd/pmd/pte modification functions
  393. */
  394. #ifndef __s390x__
  395. static inline void pgd_clear(pgd_t * pgdp) { }
  396. static inline void pmd_clear(pmd_t * pmdp)
  397. {
  398. pmd_val(pmdp[0]) = _PAGE_TABLE_INV;
  399. pmd_val(pmdp[1]) = _PAGE_TABLE_INV;
  400. pmd_val(pmdp[2]) = _PAGE_TABLE_INV;
  401. pmd_val(pmdp[3]) = _PAGE_TABLE_INV;
  402. }
  403. #else /* __s390x__ */
  404. static inline void pgd_clear(pgd_t * pgdp)
  405. {
  406. pgd_val(*pgdp) = _PGD_ENTRY_INV | _PGD_ENTRY;
  407. }
  408. static inline void pmd_clear(pmd_t * pmdp)
  409. {
  410. pmd_val(*pmdp) = _PMD_ENTRY_INV | _PMD_ENTRY;
  411. pmd_val1(*pmdp) = _PMD_ENTRY_INV | _PMD_ENTRY;
  412. }
  413. #endif /* __s390x__ */
  414. static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  415. {
  416. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  417. }
  418. /*
  419. * The following pte modification functions only work if
  420. * pte_present() is true. Undefined behaviour if not..
  421. */
  422. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  423. {
  424. pte_val(pte) &= PAGE_MASK;
  425. pte_val(pte) |= pgprot_val(newprot);
  426. return pte;
  427. }
  428. static inline pte_t pte_wrprotect(pte_t pte)
  429. {
  430. /* Do not clobber _PAGE_TYPE_NONE pages! */
  431. if (!(pte_val(pte) & _PAGE_INVALID))
  432. pte_val(pte) |= _PAGE_RO;
  433. return pte;
  434. }
  435. static inline pte_t pte_mkwrite(pte_t pte)
  436. {
  437. pte_val(pte) &= ~_PAGE_RO;
  438. return pte;
  439. }
  440. static inline pte_t pte_mkclean(pte_t pte)
  441. {
  442. /* The only user of pte_mkclean is the fork() code.
  443. We must *not* clear the *physical* page dirty bit
  444. just because fork() wants to clear the dirty bit in
  445. *one* of the page's mappings. So we just do nothing. */
  446. return pte;
  447. }
  448. static inline pte_t pte_mkdirty(pte_t pte)
  449. {
  450. /* We do not explicitly set the dirty bit because the
  451. * sske instruction is slow. It is faster to let the
  452. * next instruction set the dirty bit.
  453. */
  454. return pte;
  455. }
  456. static inline pte_t pte_mkold(pte_t pte)
  457. {
  458. /* S/390 doesn't keep its dirty/referenced bit in the pte.
  459. * There is no point in clearing the real referenced bit.
  460. */
  461. return pte;
  462. }
  463. static inline pte_t pte_mkyoung(pte_t pte)
  464. {
  465. /* S/390 doesn't keep its dirty/referenced bit in the pte.
  466. * There is no point in setting the real referenced bit.
  467. */
  468. return pte;
  469. }
  470. static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
  471. {
  472. return 0;
  473. }
  474. static inline int
  475. ptep_clear_flush_young(struct vm_area_struct *vma,
  476. unsigned long address, pte_t *ptep)
  477. {
  478. /* No need to flush TLB; bits are in storage key */
  479. return ptep_test_and_clear_young(vma, address, ptep);
  480. }
  481. static inline int ptep_test_and_clear_dirty(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
  482. {
  483. return 0;
  484. }
  485. static inline int
  486. ptep_clear_flush_dirty(struct vm_area_struct *vma,
  487. unsigned long address, pte_t *ptep)
  488. {
  489. /* No need to flush TLB; bits are in storage key */
  490. return ptep_test_and_clear_dirty(vma, address, ptep);
  491. }
  492. static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  493. {
  494. pte_t pte = *ptep;
  495. pte_clear(mm, addr, ptep);
  496. return pte;
  497. }
  498. static inline void __ptep_ipte(unsigned long address, pte_t *ptep)
  499. {
  500. if (!(pte_val(*ptep) & _PAGE_INVALID)) {
  501. #ifndef __s390x__
  502. /* S390 has 1mb segments, we are emulating 4MB segments */
  503. pte_t *pto = (pte_t *) (((unsigned long) ptep) & 0x7ffffc00);
  504. #else
  505. /* ipte in zarch mode can do the math */
  506. pte_t *pto = ptep;
  507. #endif
  508. asm volatile(
  509. " ipte %2,%3"
  510. : "=m" (*ptep) : "m" (*ptep),
  511. "a" (pto), "a" (address));
  512. }
  513. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  514. }
  515. static inline pte_t
  516. ptep_clear_flush(struct vm_area_struct *vma,
  517. unsigned long address, pte_t *ptep)
  518. {
  519. pte_t pte = *ptep;
  520. __ptep_ipte(address, ptep);
  521. return pte;
  522. }
  523. static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  524. {
  525. pte_t old_pte = *ptep;
  526. set_pte_at(mm, addr, ptep, pte_wrprotect(old_pte));
  527. }
  528. static inline void
  529. ptep_establish(struct vm_area_struct *vma,
  530. unsigned long address, pte_t *ptep,
  531. pte_t entry)
  532. {
  533. ptep_clear_flush(vma, address, ptep);
  534. set_pte(ptep, entry);
  535. }
  536. #define ptep_set_access_flags(__vma, __address, __ptep, __entry, __dirty) \
  537. ptep_establish(__vma, __address, __ptep, __entry)
  538. /*
  539. * Test and clear dirty bit in storage key.
  540. * We can't clear the changed bit atomically. This is a potential
  541. * race against modification of the referenced bit. This function
  542. * should therefore only be called if it is not mapped in any
  543. * address space.
  544. */
  545. static inline int page_test_and_clear_dirty(struct page *page)
  546. {
  547. unsigned long physpage = page_to_phys(page);
  548. int skey = page_get_storage_key(physpage);
  549. if (skey & _PAGE_CHANGED)
  550. page_set_storage_key(physpage, skey & ~_PAGE_CHANGED);
  551. return skey & _PAGE_CHANGED;
  552. }
  553. /*
  554. * Test and clear referenced bit in storage key.
  555. */
  556. static inline int page_test_and_clear_young(struct page *page)
  557. {
  558. unsigned long physpage = page_to_phys(page);
  559. int ccode;
  560. asm volatile(
  561. " rrbe 0,%1\n"
  562. " ipm %0\n"
  563. " srl %0,28\n"
  564. : "=d" (ccode) : "a" (physpage) : "cc" );
  565. return ccode & 2;
  566. }
  567. /*
  568. * Conversion functions: convert a page and protection to a page entry,
  569. * and a page entry and page directory to the page they refer to.
  570. */
  571. static inline pte_t mk_pte_phys(unsigned long physpage, pgprot_t pgprot)
  572. {
  573. pte_t __pte;
  574. pte_val(__pte) = physpage + pgprot_val(pgprot);
  575. return __pte;
  576. }
  577. static inline pte_t mk_pte(struct page *page, pgprot_t pgprot)
  578. {
  579. unsigned long physpage = page_to_phys(page);
  580. return mk_pte_phys(physpage, pgprot);
  581. }
  582. static inline pte_t pfn_pte(unsigned long pfn, pgprot_t pgprot)
  583. {
  584. unsigned long physpage = __pa((pfn) << PAGE_SHIFT);
  585. return mk_pte_phys(physpage, pgprot);
  586. }
  587. #ifdef __s390x__
  588. static inline pmd_t pfn_pmd(unsigned long pfn, pgprot_t pgprot)
  589. {
  590. unsigned long physpage = __pa((pfn) << PAGE_SHIFT);
  591. return __pmd(physpage + pgprot_val(pgprot));
  592. }
  593. #endif /* __s390x__ */
  594. #define pte_pfn(x) (pte_val(x) >> PAGE_SHIFT)
  595. #define pte_page(x) pfn_to_page(pte_pfn(x))
  596. #define pmd_page_vaddr(pmd) (pmd_val(pmd) & PAGE_MASK)
  597. #define pmd_page(pmd) pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT)
  598. #define pgd_page_vaddr(pgd) (pgd_val(pgd) & PAGE_MASK)
  599. #define pgd_page(pgd) pfn_to_page(pgd_val(pgd) >> PAGE_SHIFT)
  600. /* to find an entry in a page-table-directory */
  601. #define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
  602. #define pgd_offset(mm, address) ((mm)->pgd+pgd_index(address))
  603. /* to find an entry in a kernel page-table-directory */
  604. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  605. #ifndef __s390x__
  606. /* Find an entry in the second-level page table.. */
  607. static inline pmd_t * pmd_offset(pgd_t * dir, unsigned long address)
  608. {
  609. return (pmd_t *) dir;
  610. }
  611. #else /* __s390x__ */
  612. /* Find an entry in the second-level page table.. */
  613. #define pmd_index(address) (((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
  614. #define pmd_offset(dir,addr) \
  615. ((pmd_t *) pgd_page_vaddr(*(dir)) + pmd_index(addr))
  616. #endif /* __s390x__ */
  617. /* Find an entry in the third-level page table.. */
  618. #define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE-1))
  619. #define pte_offset_kernel(pmd, address) \
  620. ((pte_t *) pmd_page_vaddr(*(pmd)) + pte_index(address))
  621. #define pte_offset_map(pmd, address) pte_offset_kernel(pmd, address)
  622. #define pte_offset_map_nested(pmd, address) pte_offset_kernel(pmd, address)
  623. #define pte_unmap(pte) do { } while (0)
  624. #define pte_unmap_nested(pte) do { } while (0)
  625. /*
  626. * 31 bit swap entry format:
  627. * A page-table entry has some bits we have to treat in a special way.
  628. * Bits 0, 20 and bit 23 have to be zero, otherwise an specification
  629. * exception will occur instead of a page translation exception. The
  630. * specifiation exception has the bad habit not to store necessary
  631. * information in the lowcore.
  632. * Bit 21 and bit 22 are the page invalid bit and the page protection
  633. * bit. We set both to indicate a swapped page.
  634. * Bit 30 and 31 are used to distinguish the different page types. For
  635. * a swapped page these bits need to be zero.
  636. * This leaves the bits 1-19 and bits 24-29 to store type and offset.
  637. * We use the 5 bits from 25-29 for the type and the 20 bits from 1-19
  638. * plus 24 for the offset.
  639. * 0| offset |0110|o|type |00|
  640. * 0 0000000001111111111 2222 2 22222 33
  641. * 0 1234567890123456789 0123 4 56789 01
  642. *
  643. * 64 bit swap entry format:
  644. * A page-table entry has some bits we have to treat in a special way.
  645. * Bits 52 and bit 55 have to be zero, otherwise an specification
  646. * exception will occur instead of a page translation exception. The
  647. * specifiation exception has the bad habit not to store necessary
  648. * information in the lowcore.
  649. * Bit 53 and bit 54 are the page invalid bit and the page protection
  650. * bit. We set both to indicate a swapped page.
  651. * Bit 62 and 63 are used to distinguish the different page types. For
  652. * a swapped page these bits need to be zero.
  653. * This leaves the bits 0-51 and bits 56-61 to store type and offset.
  654. * We use the 5 bits from 57-61 for the type and the 53 bits from 0-51
  655. * plus 56 for the offset.
  656. * | offset |0110|o|type |00|
  657. * 0000000000111111111122222222223333333333444444444455 5555 5 55566 66
  658. * 0123456789012345678901234567890123456789012345678901 2345 6 78901 23
  659. */
  660. #ifndef __s390x__
  661. #define __SWP_OFFSET_MASK (~0UL >> 12)
  662. #else
  663. #define __SWP_OFFSET_MASK (~0UL >> 11)
  664. #endif
  665. static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset)
  666. {
  667. pte_t pte;
  668. offset &= __SWP_OFFSET_MASK;
  669. pte_val(pte) = _PAGE_TYPE_SWAP | ((type & 0x1f) << 2) |
  670. ((offset & 1UL) << 7) | ((offset & ~1UL) << 11);
  671. return pte;
  672. }
  673. #define __swp_type(entry) (((entry).val >> 2) & 0x1f)
  674. #define __swp_offset(entry) (((entry).val >> 11) | (((entry).val >> 7) & 1))
  675. #define __swp_entry(type,offset) ((swp_entry_t) { pte_val(mk_swap_pte((type),(offset))) })
  676. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  677. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  678. #ifndef __s390x__
  679. # define PTE_FILE_MAX_BITS 26
  680. #else /* __s390x__ */
  681. # define PTE_FILE_MAX_BITS 59
  682. #endif /* __s390x__ */
  683. #define pte_to_pgoff(__pte) \
  684. ((((__pte).pte >> 12) << 7) + (((__pte).pte >> 1) & 0x7f))
  685. #define pgoff_to_pte(__off) \
  686. ((pte_t) { ((((__off) & 0x7f) << 1) + (((__off) >> 7) << 12)) \
  687. | _PAGE_TYPE_FILE })
  688. #endif /* !__ASSEMBLY__ */
  689. #define kern_addr_valid(addr) (1)
  690. /*
  691. * No page table caches to initialise
  692. */
  693. #define pgtable_cache_init() do { } while (0)
  694. #define __HAVE_ARCH_PTEP_ESTABLISH
  695. #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  696. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  697. #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  698. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_DIRTY
  699. #define __HAVE_ARCH_PTEP_CLEAR_DIRTY_FLUSH
  700. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  701. #define __HAVE_ARCH_PTEP_CLEAR_FLUSH
  702. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  703. #define __HAVE_ARCH_PTE_SAME
  704. #define __HAVE_ARCH_PAGE_TEST_AND_CLEAR_DIRTY
  705. #define __HAVE_ARCH_PAGE_TEST_AND_CLEAR_YOUNG
  706. #include <asm-generic/pgtable.h>
  707. #endif /* _S390_PAGE_H */