pgtable.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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. /*
  15. * The Linux memory management assumes a three-level page table setup. For
  16. * s390 31 bit we "fold" the mid level into the top-level page table, so
  17. * that we physically have the same two-level page table as the s390 mmu
  18. * expects in 31 bit mode. For s390 64 bit we use three of the five levels
  19. * the hardware provides (region first and region second tables are not
  20. * used).
  21. *
  22. * The "pgd_xxx()" functions are trivial for a folded two-level
  23. * setup: the pgd is never bad, and a pmd always exists (as it's folded
  24. * into the pgd entry)
  25. *
  26. * This file contains the functions and defines necessary to modify and use
  27. * the S390 page table tree.
  28. */
  29. #ifndef __ASSEMBLY__
  30. #include <linux/mm_types.h>
  31. #include <asm/bug.h>
  32. #include <asm/processor.h>
  33. extern pgd_t swapper_pg_dir[] __attribute__ ((aligned (4096)));
  34. extern void paging_init(void);
  35. extern void vmem_map_init(void);
  36. /*
  37. * The S390 doesn't have any external MMU info: the kernel page
  38. * tables contain all the necessary information.
  39. */
  40. #define update_mmu_cache(vma, address, pte) do { } while (0)
  41. /*
  42. * ZERO_PAGE is a global shared page that is always zero: used
  43. * for zero-mapped memory areas etc..
  44. */
  45. extern char empty_zero_page[PAGE_SIZE];
  46. #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
  47. #endif /* !__ASSEMBLY__ */
  48. /*
  49. * PMD_SHIFT determines the size of the area a second-level page
  50. * table can map
  51. * PGDIR_SHIFT determines what a third-level page table entry can map
  52. */
  53. #ifndef __s390x__
  54. # define PMD_SHIFT 22
  55. # define PUD_SHIFT 22
  56. # define PGDIR_SHIFT 22
  57. #else /* __s390x__ */
  58. # define PMD_SHIFT 21
  59. # define PUD_SHIFT 31
  60. # define PGDIR_SHIFT 31
  61. #endif /* __s390x__ */
  62. #define PMD_SIZE (1UL << PMD_SHIFT)
  63. #define PMD_MASK (~(PMD_SIZE-1))
  64. #define PUD_SIZE (1UL << PUD_SHIFT)
  65. #define PUD_MASK (~(PUD_SIZE-1))
  66. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  67. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  68. /*
  69. * entries per page directory level: the S390 is two-level, so
  70. * we don't really have any PMD directory physically.
  71. * for S390 segment-table entries are combined to one PGD
  72. * that leads to 1024 pte per pgd
  73. */
  74. #ifndef __s390x__
  75. # define PTRS_PER_PTE 1024
  76. # define PTRS_PER_PMD 1
  77. # define PTRS_PER_PUD 1
  78. # define PTRS_PER_PGD 512
  79. #else /* __s390x__ */
  80. # define PTRS_PER_PTE 512
  81. # define PTRS_PER_PMD 1024
  82. # define PTRS_PER_PUD 1
  83. # define PTRS_PER_PGD 2048
  84. #endif /* __s390x__ */
  85. #define FIRST_USER_ADDRESS 0
  86. #define pte_ERROR(e) \
  87. printk("%s:%d: bad pte %p.\n", __FILE__, __LINE__, (void *) pte_val(e))
  88. #define pmd_ERROR(e) \
  89. printk("%s:%d: bad pmd %p.\n", __FILE__, __LINE__, (void *) pmd_val(e))
  90. #define pud_ERROR(e) \
  91. printk("%s:%d: bad pud %p.\n", __FILE__, __LINE__, (void *) pud_val(e))
  92. #define pgd_ERROR(e) \
  93. printk("%s:%d: bad pgd %p.\n", __FILE__, __LINE__, (void *) pgd_val(e))
  94. #ifndef __ASSEMBLY__
  95. /*
  96. * The vmalloc area will always be on the topmost area of the kernel
  97. * mapping. We reserve 96MB (31bit) / 1GB (64bit) for vmalloc,
  98. * which should be enough for any sane case.
  99. * By putting vmalloc at the top, we maximise the gap between physical
  100. * memory and vmalloc to catch misplaced memory accesses. As a side
  101. * effect, this also makes sure that 64 bit module code cannot be used
  102. * as system call address.
  103. */
  104. #ifndef __s390x__
  105. #define VMALLOC_START 0x78000000UL
  106. #define VMALLOC_END 0x7e000000UL
  107. #define VMEM_MAP_END 0x80000000UL
  108. #else /* __s390x__ */
  109. #define VMALLOC_START 0x3e000000000UL
  110. #define VMALLOC_END 0x3e040000000UL
  111. #define VMEM_MAP_END 0x40000000000UL
  112. #endif /* __s390x__ */
  113. /*
  114. * VMEM_MAX_PHYS is the highest physical address that can be added to the 1:1
  115. * mapping. This needs to be calculated at compile time since the size of the
  116. * VMEM_MAP is static but the size of struct page can change.
  117. */
  118. #define VMEM_MAX_PAGES ((VMEM_MAP_END - VMALLOC_END) / sizeof(struct page))
  119. #define VMEM_MAX_PFN min(VMALLOC_START >> PAGE_SHIFT, VMEM_MAX_PAGES)
  120. #define VMEM_MAX_PHYS ((VMEM_MAX_PFN << PAGE_SHIFT) & ~((16 << 20) - 1))
  121. #define VMEM_MAP ((struct page *) VMALLOC_END)
  122. /*
  123. * A 31 bit pagetable entry of S390 has following format:
  124. * | PFRA | | OS |
  125. * 0 0IP0
  126. * 00000000001111111111222222222233
  127. * 01234567890123456789012345678901
  128. *
  129. * I Page-Invalid Bit: Page is not available for address-translation
  130. * P Page-Protection Bit: Store access not possible for page
  131. *
  132. * A 31 bit segmenttable entry of S390 has following format:
  133. * | P-table origin | |PTL
  134. * 0 IC
  135. * 00000000001111111111222222222233
  136. * 01234567890123456789012345678901
  137. *
  138. * I Segment-Invalid Bit: Segment is not available for address-translation
  139. * C Common-Segment Bit: Segment is not private (PoP 3-30)
  140. * PTL Page-Table-Length: Page-table length (PTL+1*16 entries -> up to 256)
  141. *
  142. * The 31 bit segmenttable origin of S390 has following format:
  143. *
  144. * |S-table origin | | STL |
  145. * X **GPS
  146. * 00000000001111111111222222222233
  147. * 01234567890123456789012345678901
  148. *
  149. * X Space-Switch event:
  150. * G Segment-Invalid Bit: *
  151. * P Private-Space Bit: Segment is not private (PoP 3-30)
  152. * S Storage-Alteration:
  153. * STL Segment-Table-Length: Segment-table length (STL+1*16 entries -> up to 2048)
  154. *
  155. * A 64 bit pagetable entry of S390 has following format:
  156. * | PFRA |0IP0| OS |
  157. * 0000000000111111111122222222223333333333444444444455555555556666
  158. * 0123456789012345678901234567890123456789012345678901234567890123
  159. *
  160. * I Page-Invalid Bit: Page is not available for address-translation
  161. * P Page-Protection Bit: Store access not possible for page
  162. *
  163. * A 64 bit segmenttable entry of S390 has following format:
  164. * | P-table origin | TT
  165. * 0000000000111111111122222222223333333333444444444455555555556666
  166. * 0123456789012345678901234567890123456789012345678901234567890123
  167. *
  168. * I Segment-Invalid Bit: Segment is not available for address-translation
  169. * C Common-Segment Bit: Segment is not private (PoP 3-30)
  170. * P Page-Protection Bit: Store access not possible for page
  171. * TT Type 00
  172. *
  173. * A 64 bit region table entry of S390 has following format:
  174. * | S-table origin | TF TTTL
  175. * 0000000000111111111122222222223333333333444444444455555555556666
  176. * 0123456789012345678901234567890123456789012345678901234567890123
  177. *
  178. * I Segment-Invalid Bit: Segment is not available for address-translation
  179. * TT Type 01
  180. * TF
  181. * TL Table length
  182. *
  183. * The 64 bit regiontable origin of S390 has following format:
  184. * | region table origon | DTTL
  185. * 0000000000111111111122222222223333333333444444444455555555556666
  186. * 0123456789012345678901234567890123456789012345678901234567890123
  187. *
  188. * X Space-Switch event:
  189. * G Segment-Invalid Bit:
  190. * P Private-Space Bit:
  191. * S Storage-Alteration:
  192. * R Real space
  193. * TL Table-Length:
  194. *
  195. * A storage key has the following format:
  196. * | ACC |F|R|C|0|
  197. * 0 3 4 5 6 7
  198. * ACC: access key
  199. * F : fetch protection bit
  200. * R : referenced bit
  201. * C : changed bit
  202. */
  203. /* Hardware bits in the page table entry */
  204. #define _PAGE_RO 0x200 /* HW read-only bit */
  205. #define _PAGE_INVALID 0x400 /* HW invalid bit */
  206. /* Software bits in the page table entry */
  207. #define _PAGE_SWT 0x001 /* SW pte type bit t */
  208. #define _PAGE_SWX 0x002 /* SW pte type bit x */
  209. /* Six different types of pages. */
  210. #define _PAGE_TYPE_EMPTY 0x400
  211. #define _PAGE_TYPE_NONE 0x401
  212. #define _PAGE_TYPE_SWAP 0x403
  213. #define _PAGE_TYPE_FILE 0x601 /* bit 0x002 is used for offset !! */
  214. #define _PAGE_TYPE_RO 0x200
  215. #define _PAGE_TYPE_RW 0x000
  216. #define _PAGE_TYPE_EX_RO 0x202
  217. #define _PAGE_TYPE_EX_RW 0x002
  218. /*
  219. * PTE type bits are rather complicated. handle_pte_fault uses pte_present,
  220. * pte_none and pte_file to find out the pte type WITHOUT holding the page
  221. * table lock. ptep_clear_flush on the other hand uses ptep_clear_flush to
  222. * invalidate a given pte. ipte sets the hw invalid bit and clears all tlbs
  223. * for the page. The page table entry is set to _PAGE_TYPE_EMPTY afterwards.
  224. * This change is done while holding the lock, but the intermediate step
  225. * of a previously valid pte with the hw invalid bit set can be observed by
  226. * handle_pte_fault. That makes it necessary that all valid pte types with
  227. * the hw invalid bit set must be distinguishable from the four pte types
  228. * empty, none, swap and file.
  229. *
  230. * irxt ipte irxt
  231. * _PAGE_TYPE_EMPTY 1000 -> 1000
  232. * _PAGE_TYPE_NONE 1001 -> 1001
  233. * _PAGE_TYPE_SWAP 1011 -> 1011
  234. * _PAGE_TYPE_FILE 11?1 -> 11?1
  235. * _PAGE_TYPE_RO 0100 -> 1100
  236. * _PAGE_TYPE_RW 0000 -> 1000
  237. * _PAGE_TYPE_EX_RO 0110 -> 1110
  238. * _PAGE_TYPE_EX_RW 0010 -> 1010
  239. *
  240. * pte_none is true for bits combinations 1000, 1010, 1100, 1110
  241. * pte_present is true for bits combinations 0000, 0010, 0100, 0110, 1001
  242. * pte_file is true for bits combinations 1101, 1111
  243. * swap pte is 1011 and 0001, 0011, 0101, 0111 are invalid.
  244. */
  245. #ifndef __s390x__
  246. /* Bits in the segment table address-space-control-element */
  247. #define _ASCE_SPACE_SWITCH 0x80000000UL /* space switch event */
  248. #define _ASCE_ORIGIN_MASK 0x7ffff000UL /* segment table origin */
  249. #define _ASCE_PRIVATE_SPACE 0x100 /* private space control */
  250. #define _ASCE_ALT_EVENT 0x80 /* storage alteration event control */
  251. #define _ASCE_TABLE_LENGTH 0x7f /* 128 x 64 entries = 8k */
  252. /* Bits in the segment table entry */
  253. #define _SEGMENT_ENTRY_ORIGIN 0x7fffffc0UL /* page table origin */
  254. #define _SEGMENT_ENTRY_INV 0x20 /* invalid segment table entry */
  255. #define _SEGMENT_ENTRY_COMMON 0x10 /* common segment bit */
  256. #define _SEGMENT_ENTRY_PTL 0x0f /* page table length */
  257. #define _SEGMENT_ENTRY (_SEGMENT_ENTRY_PTL)
  258. #define _SEGMENT_ENTRY_EMPTY (_SEGMENT_ENTRY_INV)
  259. #else /* __s390x__ */
  260. /* Bits in the segment/region table address-space-control-element */
  261. #define _ASCE_ORIGIN ~0xfffUL/* segment table origin */
  262. #define _ASCE_PRIVATE_SPACE 0x100 /* private space control */
  263. #define _ASCE_ALT_EVENT 0x80 /* storage alteration event control */
  264. #define _ASCE_SPACE_SWITCH 0x40 /* space switch event */
  265. #define _ASCE_REAL_SPACE 0x20 /* real space control */
  266. #define _ASCE_TYPE_MASK 0x0c /* asce table type mask */
  267. #define _ASCE_TYPE_REGION1 0x0c /* region first table type */
  268. #define _ASCE_TYPE_REGION2 0x08 /* region second table type */
  269. #define _ASCE_TYPE_REGION3 0x04 /* region third table type */
  270. #define _ASCE_TYPE_SEGMENT 0x00 /* segment table type */
  271. #define _ASCE_TABLE_LENGTH 0x03 /* region table length */
  272. /* Bits in the region table entry */
  273. #define _REGION_ENTRY_ORIGIN ~0xfffUL/* region/segment table origin */
  274. #define _REGION_ENTRY_INV 0x20 /* invalid region table entry */
  275. #define _REGION_ENTRY_TYPE_MASK 0x0c /* region/segment table type mask */
  276. #define _REGION_ENTRY_TYPE_R1 0x0c /* region first table type */
  277. #define _REGION_ENTRY_TYPE_R2 0x08 /* region second table type */
  278. #define _REGION_ENTRY_TYPE_R3 0x04 /* region third table type */
  279. #define _REGION_ENTRY_LENGTH 0x03 /* region third length */
  280. #define _REGION1_ENTRY (_REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_LENGTH)
  281. #define _REGION1_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INV)
  282. #define _REGION2_ENTRY (_REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_LENGTH)
  283. #define _REGION2_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INV)
  284. #define _REGION3_ENTRY (_REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_LENGTH)
  285. #define _REGION3_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INV)
  286. /* Bits in the segment table entry */
  287. #define _SEGMENT_ENTRY_ORIGIN ~0x7ffUL/* segment table origin */
  288. #define _SEGMENT_ENTRY_RO 0x200 /* page protection bit */
  289. #define _SEGMENT_ENTRY_INV 0x20 /* invalid segment table entry */
  290. #define _SEGMENT_ENTRY (0)
  291. #define _SEGMENT_ENTRY_EMPTY (_SEGMENT_ENTRY_INV)
  292. #endif /* __s390x__ */
  293. /*
  294. * A user page table pointer has the space-switch-event bit, the
  295. * private-space-control bit and the storage-alteration-event-control
  296. * bit set. A kernel page table pointer doesn't need them.
  297. */
  298. #define _ASCE_USER_BITS (_ASCE_SPACE_SWITCH | _ASCE_PRIVATE_SPACE | \
  299. _ASCE_ALT_EVENT)
  300. /* Bits int the storage key */
  301. #define _PAGE_CHANGED 0x02 /* HW changed bit */
  302. #define _PAGE_REFERENCED 0x04 /* HW referenced bit */
  303. /*
  304. * Page protection definitions.
  305. */
  306. #define PAGE_NONE __pgprot(_PAGE_TYPE_NONE)
  307. #define PAGE_RO __pgprot(_PAGE_TYPE_RO)
  308. #define PAGE_RW __pgprot(_PAGE_TYPE_RW)
  309. #define PAGE_EX_RO __pgprot(_PAGE_TYPE_EX_RO)
  310. #define PAGE_EX_RW __pgprot(_PAGE_TYPE_EX_RW)
  311. #define PAGE_KERNEL PAGE_RW
  312. #define PAGE_COPY PAGE_RO
  313. /*
  314. * Dependent on the EXEC_PROTECT option s390 can do execute protection.
  315. * Write permission always implies read permission. In theory with a
  316. * primary/secondary page table execute only can be implemented but
  317. * it would cost an additional bit in the pte to distinguish all the
  318. * different pte types. To avoid that execute permission currently
  319. * implies read permission as well.
  320. */
  321. /*xwr*/
  322. #define __P000 PAGE_NONE
  323. #define __P001 PAGE_RO
  324. #define __P010 PAGE_RO
  325. #define __P011 PAGE_RO
  326. #define __P100 PAGE_EX_RO
  327. #define __P101 PAGE_EX_RO
  328. #define __P110 PAGE_EX_RO
  329. #define __P111 PAGE_EX_RO
  330. #define __S000 PAGE_NONE
  331. #define __S001 PAGE_RO
  332. #define __S010 PAGE_RW
  333. #define __S011 PAGE_RW
  334. #define __S100 PAGE_EX_RO
  335. #define __S101 PAGE_EX_RO
  336. #define __S110 PAGE_EX_RW
  337. #define __S111 PAGE_EX_RW
  338. #ifndef __s390x__
  339. # define PxD_SHADOW_SHIFT 1
  340. #else /* __s390x__ */
  341. # define PxD_SHADOW_SHIFT 2
  342. #endif /* __s390x__ */
  343. static inline struct page *get_shadow_page(struct page *page)
  344. {
  345. if (s390_noexec && page->index)
  346. return virt_to_page((void *)(addr_t) page->index);
  347. return NULL;
  348. }
  349. static inline void *get_shadow_pte(void *table)
  350. {
  351. unsigned long addr, offset;
  352. struct page *page;
  353. addr = (unsigned long) table;
  354. offset = addr & (PAGE_SIZE - 1);
  355. page = virt_to_page((void *)(addr ^ offset));
  356. return (void *)(addr_t)(page->index ? (page->index | offset) : 0UL);
  357. }
  358. static inline void *get_shadow_table(void *table)
  359. {
  360. unsigned long addr, offset;
  361. struct page *page;
  362. addr = (unsigned long) table;
  363. offset = addr & ((PAGE_SIZE << PxD_SHADOW_SHIFT) - 1);
  364. page = virt_to_page((void *)(addr ^ offset));
  365. return (void *)(addr_t)(page->index ? (page->index | offset) : 0UL);
  366. }
  367. /*
  368. * Certain architectures need to do special things when PTEs
  369. * within a page table are directly modified. Thus, the following
  370. * hook is made available.
  371. */
  372. static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
  373. pte_t *pteptr, pte_t pteval)
  374. {
  375. pte_t *shadow_pte = get_shadow_pte(pteptr);
  376. *pteptr = pteval;
  377. if (shadow_pte) {
  378. if (!(pte_val(pteval) & _PAGE_INVALID) &&
  379. (pte_val(pteval) & _PAGE_SWX))
  380. pte_val(*shadow_pte) = pte_val(pteval) | _PAGE_RO;
  381. else
  382. pte_val(*shadow_pte) = _PAGE_TYPE_EMPTY;
  383. }
  384. }
  385. /*
  386. * pgd/pmd/pte query functions
  387. */
  388. #ifndef __s390x__
  389. static inline int pgd_present(pgd_t pgd) { return 1; }
  390. static inline int pgd_none(pgd_t pgd) { return 0; }
  391. static inline int pgd_bad(pgd_t pgd) { return 0; }
  392. static inline int pud_present(pud_t pud) { return 1; }
  393. static inline int pud_none(pud_t pud) { return 0; }
  394. static inline int pud_bad(pud_t pud) { return 0; }
  395. #else /* __s390x__ */
  396. static inline int pgd_present(pgd_t pgd) { return 1; }
  397. static inline int pgd_none(pgd_t pgd) { return 0; }
  398. static inline int pgd_bad(pgd_t pgd) { return 0; }
  399. static inline int pud_present(pud_t pud)
  400. {
  401. return (pud_val(pud) & _REGION_ENTRY_ORIGIN) != 0UL;
  402. }
  403. static inline int pud_none(pud_t pud)
  404. {
  405. return (pud_val(pud) & _REGION_ENTRY_INV) != 0UL;
  406. }
  407. static inline int pud_bad(pud_t pud)
  408. {
  409. unsigned long mask = ~_REGION_ENTRY_ORIGIN & ~_REGION_ENTRY_INV;
  410. return (pud_val(pud) & mask) != _REGION3_ENTRY;
  411. }
  412. #endif /* __s390x__ */
  413. static inline int pmd_present(pmd_t pmd)
  414. {
  415. return (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN) != 0UL;
  416. }
  417. static inline int pmd_none(pmd_t pmd)
  418. {
  419. return (pmd_val(pmd) & _SEGMENT_ENTRY_INV) != 0UL;
  420. }
  421. static inline int pmd_bad(pmd_t pmd)
  422. {
  423. unsigned long mask = ~_SEGMENT_ENTRY_ORIGIN & ~_SEGMENT_ENTRY_INV;
  424. return (pmd_val(pmd) & mask) != _SEGMENT_ENTRY;
  425. }
  426. static inline int pte_none(pte_t pte)
  427. {
  428. return (pte_val(pte) & _PAGE_INVALID) && !(pte_val(pte) & _PAGE_SWT);
  429. }
  430. static inline int pte_present(pte_t pte)
  431. {
  432. unsigned long mask = _PAGE_RO | _PAGE_INVALID | _PAGE_SWT | _PAGE_SWX;
  433. return (pte_val(pte) & mask) == _PAGE_TYPE_NONE ||
  434. (!(pte_val(pte) & _PAGE_INVALID) &&
  435. !(pte_val(pte) & _PAGE_SWT));
  436. }
  437. static inline int pte_file(pte_t pte)
  438. {
  439. unsigned long mask = _PAGE_RO | _PAGE_INVALID | _PAGE_SWT;
  440. return (pte_val(pte) & mask) == _PAGE_TYPE_FILE;
  441. }
  442. #define __HAVE_ARCH_PTE_SAME
  443. #define pte_same(a,b) (pte_val(a) == pte_val(b))
  444. /*
  445. * query functions pte_write/pte_dirty/pte_young only work if
  446. * pte_present() is true. Undefined behaviour if not..
  447. */
  448. static inline int pte_write(pte_t pte)
  449. {
  450. return (pte_val(pte) & _PAGE_RO) == 0;
  451. }
  452. static inline int pte_dirty(pte_t pte)
  453. {
  454. /* A pte is neither clean nor dirty on s/390. The dirty bit
  455. * is in the storage key. See page_test_and_clear_dirty for
  456. * details.
  457. */
  458. return 0;
  459. }
  460. static inline int pte_young(pte_t pte)
  461. {
  462. /* A pte is neither young nor old on s/390. The young bit
  463. * is in the storage key. See page_test_and_clear_young for
  464. * details.
  465. */
  466. return 0;
  467. }
  468. /*
  469. * pgd/pmd/pte modification functions
  470. */
  471. #ifndef __s390x__
  472. #define pgd_clear(pgd) do { } while (0)
  473. #define pud_clear(pud) do { } while (0)
  474. static inline void pmd_clear_kernel(pmd_t * pmdp)
  475. {
  476. pmd_val(pmdp[0]) = _SEGMENT_ENTRY_EMPTY;
  477. pmd_val(pmdp[1]) = _SEGMENT_ENTRY_EMPTY;
  478. pmd_val(pmdp[2]) = _SEGMENT_ENTRY_EMPTY;
  479. pmd_val(pmdp[3]) = _SEGMENT_ENTRY_EMPTY;
  480. }
  481. #else /* __s390x__ */
  482. #define pgd_clear(pgd) do { } while (0)
  483. static inline void pud_clear_kernel(pud_t *pud)
  484. {
  485. pud_val(*pud) = _REGION3_ENTRY_EMPTY;
  486. }
  487. static inline void pud_clear(pud_t * pud)
  488. {
  489. pud_t *shadow = get_shadow_table(pud);
  490. pud_clear_kernel(pud);
  491. if (shadow)
  492. pud_clear_kernel(shadow);
  493. }
  494. static inline void pmd_clear_kernel(pmd_t * pmdp)
  495. {
  496. pmd_val(*pmdp) = _SEGMENT_ENTRY_EMPTY;
  497. pmd_val1(*pmdp) = _SEGMENT_ENTRY_EMPTY;
  498. }
  499. #endif /* __s390x__ */
  500. static inline void pmd_clear(pmd_t * pmdp)
  501. {
  502. pmd_t *shadow_pmd = get_shadow_table(pmdp);
  503. pmd_clear_kernel(pmdp);
  504. if (shadow_pmd)
  505. pmd_clear_kernel(shadow_pmd);
  506. }
  507. static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  508. {
  509. pte_t *shadow_pte = get_shadow_pte(ptep);
  510. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  511. if (shadow_pte)
  512. pte_val(*shadow_pte) = _PAGE_TYPE_EMPTY;
  513. }
  514. /*
  515. * The following pte modification functions only work if
  516. * pte_present() is true. Undefined behaviour if not..
  517. */
  518. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  519. {
  520. pte_val(pte) &= PAGE_MASK;
  521. pte_val(pte) |= pgprot_val(newprot);
  522. return pte;
  523. }
  524. static inline pte_t pte_wrprotect(pte_t pte)
  525. {
  526. /* Do not clobber _PAGE_TYPE_NONE pages! */
  527. if (!(pte_val(pte) & _PAGE_INVALID))
  528. pte_val(pte) |= _PAGE_RO;
  529. return pte;
  530. }
  531. static inline pte_t pte_mkwrite(pte_t pte)
  532. {
  533. pte_val(pte) &= ~_PAGE_RO;
  534. return pte;
  535. }
  536. static inline pte_t pte_mkclean(pte_t pte)
  537. {
  538. /* The only user of pte_mkclean is the fork() code.
  539. We must *not* clear the *physical* page dirty bit
  540. just because fork() wants to clear the dirty bit in
  541. *one* of the page's mappings. So we just do nothing. */
  542. return pte;
  543. }
  544. static inline pte_t pte_mkdirty(pte_t pte)
  545. {
  546. /* We do not explicitly set the dirty bit because the
  547. * sske instruction is slow. It is faster to let the
  548. * next instruction set the dirty bit.
  549. */
  550. return pte;
  551. }
  552. static inline pte_t pte_mkold(pte_t pte)
  553. {
  554. /* S/390 doesn't keep its dirty/referenced bit in the pte.
  555. * There is no point in clearing the real referenced bit.
  556. */
  557. return pte;
  558. }
  559. static inline pte_t pte_mkyoung(pte_t pte)
  560. {
  561. /* S/390 doesn't keep its dirty/referenced bit in the pte.
  562. * There is no point in setting the real referenced bit.
  563. */
  564. return pte;
  565. }
  566. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  567. static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
  568. unsigned long addr, pte_t *ptep)
  569. {
  570. return 0;
  571. }
  572. #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  573. static inline int ptep_clear_flush_young(struct vm_area_struct *vma,
  574. unsigned long address, pte_t *ptep)
  575. {
  576. /* No need to flush TLB; bits are in storage key */
  577. return 0;
  578. }
  579. static inline void __ptep_ipte(unsigned long address, pte_t *ptep)
  580. {
  581. if (!(pte_val(*ptep) & _PAGE_INVALID)) {
  582. #ifndef __s390x__
  583. /* S390 has 1mb segments, we are emulating 4MB segments */
  584. pte_t *pto = (pte_t *) (((unsigned long) ptep) & 0x7ffffc00);
  585. #else
  586. /* ipte in zarch mode can do the math */
  587. pte_t *pto = ptep;
  588. #endif
  589. asm volatile(
  590. " ipte %2,%3"
  591. : "=m" (*ptep) : "m" (*ptep),
  592. "a" (pto), "a" (address));
  593. }
  594. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  595. }
  596. static inline void ptep_invalidate(unsigned long address, pte_t *ptep)
  597. {
  598. __ptep_ipte(address, ptep);
  599. ptep = get_shadow_pte(ptep);
  600. if (ptep)
  601. __ptep_ipte(address, ptep);
  602. }
  603. /*
  604. * This is hard to understand. ptep_get_and_clear and ptep_clear_flush
  605. * both clear the TLB for the unmapped pte. The reason is that
  606. * ptep_get_and_clear is used in common code (e.g. change_pte_range)
  607. * to modify an active pte. The sequence is
  608. * 1) ptep_get_and_clear
  609. * 2) set_pte_at
  610. * 3) flush_tlb_range
  611. * On s390 the tlb needs to get flushed with the modification of the pte
  612. * if the pte is active. The only way how this can be implemented is to
  613. * have ptep_get_and_clear do the tlb flush. In exchange flush_tlb_range
  614. * is a nop.
  615. */
  616. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  617. #define ptep_get_and_clear(__mm, __address, __ptep) \
  618. ({ \
  619. pte_t __pte = *(__ptep); \
  620. if (atomic_read(&(__mm)->mm_users) > 1 || \
  621. (__mm) != current->active_mm) \
  622. ptep_invalidate(__address, __ptep); \
  623. else \
  624. pte_clear((__mm), (__address), (__ptep)); \
  625. __pte; \
  626. })
  627. #define __HAVE_ARCH_PTEP_CLEAR_FLUSH
  628. static inline pte_t ptep_clear_flush(struct vm_area_struct *vma,
  629. unsigned long address, pte_t *ptep)
  630. {
  631. pte_t pte = *ptep;
  632. ptep_invalidate(address, ptep);
  633. return pte;
  634. }
  635. /*
  636. * The batched pte unmap code uses ptep_get_and_clear_full to clear the
  637. * ptes. Here an optimization is possible. tlb_gather_mmu flushes all
  638. * tlbs of an mm if it can guarantee that the ptes of the mm_struct
  639. * cannot be accessed while the batched unmap is running. In this case
  640. * full==1 and a simple pte_clear is enough. See tlb.h.
  641. */
  642. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
  643. static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
  644. unsigned long addr,
  645. pte_t *ptep, int full)
  646. {
  647. pte_t pte = *ptep;
  648. if (full)
  649. pte_clear(mm, addr, ptep);
  650. else
  651. ptep_invalidate(addr, ptep);
  652. return pte;
  653. }
  654. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  655. #define ptep_set_wrprotect(__mm, __addr, __ptep) \
  656. ({ \
  657. pte_t __pte = *(__ptep); \
  658. if (pte_write(__pte)) { \
  659. if (atomic_read(&(__mm)->mm_users) > 1 || \
  660. (__mm) != current->active_mm) \
  661. ptep_invalidate(__addr, __ptep); \
  662. set_pte_at(__mm, __addr, __ptep, pte_wrprotect(__pte)); \
  663. } \
  664. })
  665. #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  666. #define ptep_set_access_flags(__vma, __addr, __ptep, __entry, __dirty) \
  667. ({ \
  668. int __changed = !pte_same(*(__ptep), __entry); \
  669. if (__changed) { \
  670. ptep_invalidate(__addr, __ptep); \
  671. set_pte_at((__vma)->vm_mm, __addr, __ptep, __entry); \
  672. } \
  673. __changed; \
  674. })
  675. /*
  676. * Test and clear dirty bit in storage key.
  677. * We can't clear the changed bit atomically. This is a potential
  678. * race against modification of the referenced bit. This function
  679. * should therefore only be called if it is not mapped in any
  680. * address space.
  681. */
  682. #define __HAVE_ARCH_PAGE_TEST_DIRTY
  683. static inline int page_test_dirty(struct page *page)
  684. {
  685. return (page_get_storage_key(page_to_phys(page)) & _PAGE_CHANGED) != 0;
  686. }
  687. #define __HAVE_ARCH_PAGE_CLEAR_DIRTY
  688. static inline void page_clear_dirty(struct page *page)
  689. {
  690. page_set_storage_key(page_to_phys(page), PAGE_DEFAULT_KEY);
  691. }
  692. /*
  693. * Test and clear referenced bit in storage key.
  694. */
  695. #define __HAVE_ARCH_PAGE_TEST_AND_CLEAR_YOUNG
  696. static inline int page_test_and_clear_young(struct page *page)
  697. {
  698. unsigned long physpage = page_to_phys(page);
  699. int ccode;
  700. asm volatile(
  701. " rrbe 0,%1\n"
  702. " ipm %0\n"
  703. " srl %0,28\n"
  704. : "=d" (ccode) : "a" (physpage) : "cc" );
  705. return ccode & 2;
  706. }
  707. /*
  708. * Conversion functions: convert a page and protection to a page entry,
  709. * and a page entry and page directory to the page they refer to.
  710. */
  711. static inline pte_t mk_pte_phys(unsigned long physpage, pgprot_t pgprot)
  712. {
  713. pte_t __pte;
  714. pte_val(__pte) = physpage + pgprot_val(pgprot);
  715. return __pte;
  716. }
  717. static inline pte_t mk_pte(struct page *page, pgprot_t pgprot)
  718. {
  719. unsigned long physpage = page_to_phys(page);
  720. return mk_pte_phys(physpage, pgprot);
  721. }
  722. #define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
  723. #define pud_index(address) (((address) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
  724. #define pmd_index(address) (((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
  725. #define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE-1))
  726. #define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
  727. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  728. #ifndef __s390x__
  729. #define pmd_deref(pmd) (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN)
  730. #define pud_deref(pmd) ({ BUG(); 0UL; })
  731. #define pgd_deref(pmd) ({ BUG(); 0UL; })
  732. #define pud_offset(pgd, address) ((pud_t *) pgd)
  733. #define pmd_offset(pud, address) ((pmd_t *) pud + pmd_index(address))
  734. #else /* __s390x__ */
  735. #define pmd_deref(pmd) (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN)
  736. #define pud_deref(pud) (pud_val(pud) & _REGION_ENTRY_ORIGIN)
  737. #define pgd_deref(pgd) ({ BUG(); 0UL; })
  738. #define pud_offset(pgd, address) ((pud_t *) pgd)
  739. static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address)
  740. {
  741. pmd_t *pmd = (pmd_t *) pud_deref(*pud);
  742. return pmd + pmd_index(address);
  743. }
  744. #endif /* __s390x__ */
  745. #define pfn_pte(pfn,pgprot) mk_pte_phys(__pa((pfn) << PAGE_SHIFT),(pgprot))
  746. #define pte_pfn(x) (pte_val(x) >> PAGE_SHIFT)
  747. #define pte_page(x) pfn_to_page(pte_pfn(x))
  748. #define pmd_page(pmd) pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT)
  749. /* Find an entry in the lowest level page table.. */
  750. #define pte_offset(pmd, addr) ((pte_t *) pmd_deref(*(pmd)) + pte_index(addr))
  751. #define pte_offset_kernel(pmd, address) pte_offset(pmd,address)
  752. #define pte_offset_map(pmd, address) pte_offset_kernel(pmd, address)
  753. #define pte_offset_map_nested(pmd, address) pte_offset_kernel(pmd, address)
  754. #define pte_unmap(pte) do { } while (0)
  755. #define pte_unmap_nested(pte) do { } while (0)
  756. /*
  757. * 31 bit swap entry format:
  758. * A page-table entry has some bits we have to treat in a special way.
  759. * Bits 0, 20 and bit 23 have to be zero, otherwise an specification
  760. * exception will occur instead of a page translation exception. The
  761. * specifiation exception has the bad habit not to store necessary
  762. * information in the lowcore.
  763. * Bit 21 and bit 22 are the page invalid bit and the page protection
  764. * bit. We set both to indicate a swapped page.
  765. * Bit 30 and 31 are used to distinguish the different page types. For
  766. * a swapped page these bits need to be zero.
  767. * This leaves the bits 1-19 and bits 24-29 to store type and offset.
  768. * We use the 5 bits from 25-29 for the type and the 20 bits from 1-19
  769. * plus 24 for the offset.
  770. * 0| offset |0110|o|type |00|
  771. * 0 0000000001111111111 2222 2 22222 33
  772. * 0 1234567890123456789 0123 4 56789 01
  773. *
  774. * 64 bit swap entry format:
  775. * A page-table entry has some bits we have to treat in a special way.
  776. * Bits 52 and bit 55 have to be zero, otherwise an specification
  777. * exception will occur instead of a page translation exception. The
  778. * specifiation exception has the bad habit not to store necessary
  779. * information in the lowcore.
  780. * Bit 53 and bit 54 are the page invalid bit and the page protection
  781. * bit. We set both to indicate a swapped page.
  782. * Bit 62 and 63 are used to distinguish the different page types. For
  783. * a swapped page these bits need to be zero.
  784. * This leaves the bits 0-51 and bits 56-61 to store type and offset.
  785. * We use the 5 bits from 57-61 for the type and the 53 bits from 0-51
  786. * plus 56 for the offset.
  787. * | offset |0110|o|type |00|
  788. * 0000000000111111111122222222223333333333444444444455 5555 5 55566 66
  789. * 0123456789012345678901234567890123456789012345678901 2345 6 78901 23
  790. */
  791. #ifndef __s390x__
  792. #define __SWP_OFFSET_MASK (~0UL >> 12)
  793. #else
  794. #define __SWP_OFFSET_MASK (~0UL >> 11)
  795. #endif
  796. static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset)
  797. {
  798. pte_t pte;
  799. offset &= __SWP_OFFSET_MASK;
  800. pte_val(pte) = _PAGE_TYPE_SWAP | ((type & 0x1f) << 2) |
  801. ((offset & 1UL) << 7) | ((offset & ~1UL) << 11);
  802. return pte;
  803. }
  804. #define __swp_type(entry) (((entry).val >> 2) & 0x1f)
  805. #define __swp_offset(entry) (((entry).val >> 11) | (((entry).val >> 7) & 1))
  806. #define __swp_entry(type,offset) ((swp_entry_t) { pte_val(mk_swap_pte((type),(offset))) })
  807. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  808. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  809. #ifndef __s390x__
  810. # define PTE_FILE_MAX_BITS 26
  811. #else /* __s390x__ */
  812. # define PTE_FILE_MAX_BITS 59
  813. #endif /* __s390x__ */
  814. #define pte_to_pgoff(__pte) \
  815. ((((__pte).pte >> 12) << 7) + (((__pte).pte >> 1) & 0x7f))
  816. #define pgoff_to_pte(__off) \
  817. ((pte_t) { ((((__off) & 0x7f) << 1) + (((__off) >> 7) << 12)) \
  818. | _PAGE_TYPE_FILE })
  819. #endif /* !__ASSEMBLY__ */
  820. #define kern_addr_valid(addr) (1)
  821. extern int add_shared_memory(unsigned long start, unsigned long size);
  822. extern int remove_shared_memory(unsigned long start, unsigned long size);
  823. /*
  824. * No page table caches to initialise
  825. */
  826. #define pgtable_cache_init() do { } while (0)
  827. #define __HAVE_ARCH_MEMMAP_INIT
  828. extern void memmap_init(unsigned long, int, unsigned long, unsigned long);
  829. #include <asm-generic/pgtable.h>
  830. #endif /* _S390_PAGE_H */