pgtable.h 30 KB

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