pgtable.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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/sched.h>
  31. #include <linux/mm_types.h>
  32. #include <asm/bug.h>
  33. #include <asm/page.h>
  34. extern pgd_t swapper_pg_dir[] __attribute__ ((aligned (4096)));
  35. extern void paging_init(void);
  36. extern void vmem_map_init(void);
  37. extern void fault_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, ptep) 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 unsigned long empty_zero_page;
  48. extern unsigned long zero_page_mask;
  49. #define ZERO_PAGE(vaddr) \
  50. (virt_to_page((void *)(empty_zero_page + \
  51. (((unsigned long)(vaddr)) &zero_page_mask))))
  52. #define is_zero_pfn is_zero_pfn
  53. static inline int is_zero_pfn(unsigned long pfn)
  54. {
  55. extern unsigned long zero_pfn;
  56. unsigned long offset_from_zero_pfn = pfn - zero_pfn;
  57. return offset_from_zero_pfn <= (zero_page_mask >> PAGE_SHIFT);
  58. }
  59. #define my_zero_pfn(addr) page_to_pfn(ZERO_PAGE(addr))
  60. #endif /* !__ASSEMBLY__ */
  61. /*
  62. * PMD_SHIFT determines the size of the area a second-level page
  63. * table can map
  64. * PGDIR_SHIFT determines what a third-level page table entry can map
  65. */
  66. #ifndef __s390x__
  67. # define PMD_SHIFT 20
  68. # define PUD_SHIFT 20
  69. # define PGDIR_SHIFT 20
  70. #else /* __s390x__ */
  71. # define PMD_SHIFT 20
  72. # define PUD_SHIFT 31
  73. # define PGDIR_SHIFT 42
  74. #endif /* __s390x__ */
  75. #define PMD_SIZE (1UL << PMD_SHIFT)
  76. #define PMD_MASK (~(PMD_SIZE-1))
  77. #define PUD_SIZE (1UL << PUD_SHIFT)
  78. #define PUD_MASK (~(PUD_SIZE-1))
  79. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  80. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  81. /*
  82. * entries per page directory level: the S390 is two-level, so
  83. * we don't really have any PMD directory physically.
  84. * for S390 segment-table entries are combined to one PGD
  85. * that leads to 1024 pte per pgd
  86. */
  87. #define PTRS_PER_PTE 256
  88. #ifndef __s390x__
  89. #define PTRS_PER_PMD 1
  90. #define PTRS_PER_PUD 1
  91. #else /* __s390x__ */
  92. #define PTRS_PER_PMD 2048
  93. #define PTRS_PER_PUD 2048
  94. #endif /* __s390x__ */
  95. #define PTRS_PER_PGD 2048
  96. #define FIRST_USER_ADDRESS 0
  97. #define pte_ERROR(e) \
  98. printk("%s:%d: bad pte %p.\n", __FILE__, __LINE__, (void *) pte_val(e))
  99. #define pmd_ERROR(e) \
  100. printk("%s:%d: bad pmd %p.\n", __FILE__, __LINE__, (void *) pmd_val(e))
  101. #define pud_ERROR(e) \
  102. printk("%s:%d: bad pud %p.\n", __FILE__, __LINE__, (void *) pud_val(e))
  103. #define pgd_ERROR(e) \
  104. printk("%s:%d: bad pgd %p.\n", __FILE__, __LINE__, (void *) pgd_val(e))
  105. #ifndef __ASSEMBLY__
  106. /*
  107. * The vmalloc area will always be on the topmost area of the kernel
  108. * mapping. We reserve 96MB (31bit) / 128GB (64bit) for vmalloc,
  109. * which should be enough for any sane case.
  110. * By putting vmalloc at the top, we maximise the gap between physical
  111. * memory and vmalloc to catch misplaced memory accesses. As a side
  112. * effect, this also makes sure that 64 bit module code cannot be used
  113. * as system call address.
  114. */
  115. extern unsigned long VMALLOC_START;
  116. #ifndef __s390x__
  117. #define VMALLOC_SIZE (96UL << 20)
  118. #define VMALLOC_END 0x7e000000UL
  119. #define VMEM_MAP_END 0x80000000UL
  120. #else /* __s390x__ */
  121. #define VMALLOC_SIZE (128UL << 30)
  122. #define VMALLOC_END 0x3e000000000UL
  123. #define VMEM_MAP_END 0x40000000000UL
  124. #endif /* __s390x__ */
  125. /*
  126. * VMEM_MAX_PHYS is the highest physical address that can be added to the 1:1
  127. * mapping. This needs to be calculated at compile time since the size of the
  128. * VMEM_MAP is static but the size of struct page can change.
  129. */
  130. #define VMEM_MAX_PAGES ((VMEM_MAP_END - VMALLOC_END) / sizeof(struct page))
  131. #define VMEM_MAX_PFN min(VMALLOC_START >> PAGE_SHIFT, VMEM_MAX_PAGES)
  132. #define VMEM_MAX_PHYS ((VMEM_MAX_PFN << PAGE_SHIFT) & ~((16 << 20) - 1))
  133. #define vmemmap ((struct page *) VMALLOC_END)
  134. /*
  135. * A 31 bit pagetable entry of S390 has following format:
  136. * | PFRA | | OS |
  137. * 0 0IP0
  138. * 00000000001111111111222222222233
  139. * 01234567890123456789012345678901
  140. *
  141. * I Page-Invalid Bit: Page is not available for address-translation
  142. * P Page-Protection Bit: Store access not possible for page
  143. *
  144. * A 31 bit segmenttable entry of S390 has following format:
  145. * | P-table origin | |PTL
  146. * 0 IC
  147. * 00000000001111111111222222222233
  148. * 01234567890123456789012345678901
  149. *
  150. * I Segment-Invalid Bit: Segment is not available for address-translation
  151. * C Common-Segment Bit: Segment is not private (PoP 3-30)
  152. * PTL Page-Table-Length: Page-table length (PTL+1*16 entries -> up to 256)
  153. *
  154. * The 31 bit segmenttable origin of S390 has following format:
  155. *
  156. * |S-table origin | | STL |
  157. * X **GPS
  158. * 00000000001111111111222222222233
  159. * 01234567890123456789012345678901
  160. *
  161. * X Space-Switch event:
  162. * G Segment-Invalid Bit: *
  163. * P Private-Space Bit: Segment is not private (PoP 3-30)
  164. * S Storage-Alteration:
  165. * STL Segment-Table-Length: Segment-table length (STL+1*16 entries -> up to 2048)
  166. *
  167. * A 64 bit pagetable entry of S390 has following format:
  168. * | PFRA |0IPC| OS |
  169. * 0000000000111111111122222222223333333333444444444455555555556666
  170. * 0123456789012345678901234567890123456789012345678901234567890123
  171. *
  172. * I Page-Invalid Bit: Page is not available for address-translation
  173. * P Page-Protection Bit: Store access not possible for page
  174. * C Change-bit override: HW is not required to set change bit
  175. *
  176. * A 64 bit segmenttable entry of S390 has following format:
  177. * | P-table origin | TT
  178. * 0000000000111111111122222222223333333333444444444455555555556666
  179. * 0123456789012345678901234567890123456789012345678901234567890123
  180. *
  181. * I Segment-Invalid Bit: Segment is not available for address-translation
  182. * C Common-Segment Bit: Segment is not private (PoP 3-30)
  183. * P Page-Protection Bit: Store access not possible for page
  184. * TT Type 00
  185. *
  186. * A 64 bit region table entry of S390 has following format:
  187. * | S-table origin | TF TTTL
  188. * 0000000000111111111122222222223333333333444444444455555555556666
  189. * 0123456789012345678901234567890123456789012345678901234567890123
  190. *
  191. * I Segment-Invalid Bit: Segment is not available for address-translation
  192. * TT Type 01
  193. * TF
  194. * TL Table length
  195. *
  196. * The 64 bit regiontable origin of S390 has following format:
  197. * | region table origon | DTTL
  198. * 0000000000111111111122222222223333333333444444444455555555556666
  199. * 0123456789012345678901234567890123456789012345678901234567890123
  200. *
  201. * X Space-Switch event:
  202. * G Segment-Invalid Bit:
  203. * P Private-Space Bit:
  204. * S Storage-Alteration:
  205. * R Real space
  206. * TL Table-Length:
  207. *
  208. * A storage key has the following format:
  209. * | ACC |F|R|C|0|
  210. * 0 3 4 5 6 7
  211. * ACC: access key
  212. * F : fetch protection bit
  213. * R : referenced bit
  214. * C : changed bit
  215. */
  216. /* Hardware bits in the page table entry */
  217. #define _PAGE_CO 0x100 /* HW Change-bit override */
  218. #define _PAGE_RO 0x200 /* HW read-only bit */
  219. #define _PAGE_INVALID 0x400 /* HW invalid bit */
  220. /* Software bits in the page table entry */
  221. #define _PAGE_SWT 0x001 /* SW pte type bit t */
  222. #define _PAGE_SWX 0x002 /* SW pte type bit x */
  223. #define _PAGE_SWC 0x004 /* SW pte changed bit (for KVM) */
  224. #define _PAGE_SWR 0x008 /* SW pte referenced bit (for KVM) */
  225. #define _PAGE_SPECIAL 0x010 /* SW associated with special page */
  226. #define __HAVE_ARCH_PTE_SPECIAL
  227. /* Set of bits not changed in pte_modify */
  228. #define _PAGE_CHG_MASK (PAGE_MASK | _PAGE_SPECIAL | _PAGE_SWC | _PAGE_SWR)
  229. /* Six different types of pages. */
  230. #define _PAGE_TYPE_EMPTY 0x400
  231. #define _PAGE_TYPE_NONE 0x401
  232. #define _PAGE_TYPE_SWAP 0x403
  233. #define _PAGE_TYPE_FILE 0x601 /* bit 0x002 is used for offset !! */
  234. #define _PAGE_TYPE_RO 0x200
  235. #define _PAGE_TYPE_RW 0x000
  236. /*
  237. * Only four types for huge pages, using the invalid bit and protection bit
  238. * of a segment table entry.
  239. */
  240. #define _HPAGE_TYPE_EMPTY 0x020 /* _SEGMENT_ENTRY_INV */
  241. #define _HPAGE_TYPE_NONE 0x220
  242. #define _HPAGE_TYPE_RO 0x200 /* _SEGMENT_ENTRY_RO */
  243. #define _HPAGE_TYPE_RW 0x000
  244. /*
  245. * PTE type bits are rather complicated. handle_pte_fault uses pte_present,
  246. * pte_none and pte_file to find out the pte type WITHOUT holding the page
  247. * table lock. ptep_clear_flush on the other hand uses ptep_clear_flush to
  248. * invalidate a given pte. ipte sets the hw invalid bit and clears all tlbs
  249. * for the page. The page table entry is set to _PAGE_TYPE_EMPTY afterwards.
  250. * This change is done while holding the lock, but the intermediate step
  251. * of a previously valid pte with the hw invalid bit set can be observed by
  252. * handle_pte_fault. That makes it necessary that all valid pte types with
  253. * the hw invalid bit set must be distinguishable from the four pte types
  254. * empty, none, swap and file.
  255. *
  256. * irxt ipte irxt
  257. * _PAGE_TYPE_EMPTY 1000 -> 1000
  258. * _PAGE_TYPE_NONE 1001 -> 1001
  259. * _PAGE_TYPE_SWAP 1011 -> 1011
  260. * _PAGE_TYPE_FILE 11?1 -> 11?1
  261. * _PAGE_TYPE_RO 0100 -> 1100
  262. * _PAGE_TYPE_RW 0000 -> 1000
  263. *
  264. * pte_none is true for bits combinations 1000, 1010, 1100, 1110
  265. * pte_present is true for bits combinations 0000, 0010, 0100, 0110, 1001
  266. * pte_file is true for bits combinations 1101, 1111
  267. * swap pte is 1011 and 0001, 0011, 0101, 0111 are invalid.
  268. */
  269. /* Page status table bits for virtualization */
  270. #define RCP_ACC_BITS 0xf000000000000000UL
  271. #define RCP_FP_BIT 0x0800000000000000UL
  272. #define RCP_PCL_BIT 0x0080000000000000UL
  273. #define RCP_HR_BIT 0x0040000000000000UL
  274. #define RCP_HC_BIT 0x0020000000000000UL
  275. #define RCP_GR_BIT 0x0004000000000000UL
  276. #define RCP_GC_BIT 0x0002000000000000UL
  277. /* User dirty / referenced bit for KVM's migration feature */
  278. #define KVM_UR_BIT 0x0000800000000000UL
  279. #define KVM_UC_BIT 0x0000400000000000UL
  280. #ifndef __s390x__
  281. /* Bits in the segment table address-space-control-element */
  282. #define _ASCE_SPACE_SWITCH 0x80000000UL /* space switch event */
  283. #define _ASCE_ORIGIN_MASK 0x7ffff000UL /* segment table origin */
  284. #define _ASCE_PRIVATE_SPACE 0x100 /* private space control */
  285. #define _ASCE_ALT_EVENT 0x80 /* storage alteration event control */
  286. #define _ASCE_TABLE_LENGTH 0x7f /* 128 x 64 entries = 8k */
  287. /* Bits in the segment table entry */
  288. #define _SEGMENT_ENTRY_ORIGIN 0x7fffffc0UL /* page table origin */
  289. #define _SEGMENT_ENTRY_RO 0x200 /* page protection bit */
  290. #define _SEGMENT_ENTRY_INV 0x20 /* invalid segment table entry */
  291. #define _SEGMENT_ENTRY_COMMON 0x10 /* common segment bit */
  292. #define _SEGMENT_ENTRY_PTL 0x0f /* page table length */
  293. #define _SEGMENT_ENTRY (_SEGMENT_ENTRY_PTL)
  294. #define _SEGMENT_ENTRY_EMPTY (_SEGMENT_ENTRY_INV)
  295. #else /* __s390x__ */
  296. /* Bits in the segment/region table address-space-control-element */
  297. #define _ASCE_ORIGIN ~0xfffUL/* segment table origin */
  298. #define _ASCE_PRIVATE_SPACE 0x100 /* private space control */
  299. #define _ASCE_ALT_EVENT 0x80 /* storage alteration event control */
  300. #define _ASCE_SPACE_SWITCH 0x40 /* space switch event */
  301. #define _ASCE_REAL_SPACE 0x20 /* real space control */
  302. #define _ASCE_TYPE_MASK 0x0c /* asce table type mask */
  303. #define _ASCE_TYPE_REGION1 0x0c /* region first table type */
  304. #define _ASCE_TYPE_REGION2 0x08 /* region second table type */
  305. #define _ASCE_TYPE_REGION3 0x04 /* region third table type */
  306. #define _ASCE_TYPE_SEGMENT 0x00 /* segment table type */
  307. #define _ASCE_TABLE_LENGTH 0x03 /* region table length */
  308. /* Bits in the region table entry */
  309. #define _REGION_ENTRY_ORIGIN ~0xfffUL/* region/segment table origin */
  310. #define _REGION_ENTRY_INV 0x20 /* invalid region table entry */
  311. #define _REGION_ENTRY_TYPE_MASK 0x0c /* region/segment table type mask */
  312. #define _REGION_ENTRY_TYPE_R1 0x0c /* region first table type */
  313. #define _REGION_ENTRY_TYPE_R2 0x08 /* region second table type */
  314. #define _REGION_ENTRY_TYPE_R3 0x04 /* region third table type */
  315. #define _REGION_ENTRY_LENGTH 0x03 /* region third length */
  316. #define _REGION1_ENTRY (_REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_LENGTH)
  317. #define _REGION1_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INV)
  318. #define _REGION2_ENTRY (_REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_LENGTH)
  319. #define _REGION2_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INV)
  320. #define _REGION3_ENTRY (_REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_LENGTH)
  321. #define _REGION3_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INV)
  322. /* Bits in the segment table entry */
  323. #define _SEGMENT_ENTRY_ORIGIN ~0x7ffUL/* segment table origin */
  324. #define _SEGMENT_ENTRY_RO 0x200 /* page protection bit */
  325. #define _SEGMENT_ENTRY_INV 0x20 /* invalid segment table entry */
  326. #define _SEGMENT_ENTRY (0)
  327. #define _SEGMENT_ENTRY_EMPTY (_SEGMENT_ENTRY_INV)
  328. #define _SEGMENT_ENTRY_LARGE 0x400 /* STE-format control, large page */
  329. #define _SEGMENT_ENTRY_CO 0x100 /* change-recording override */
  330. #endif /* __s390x__ */
  331. /*
  332. * A user page table pointer has the space-switch-event bit, the
  333. * private-space-control bit and the storage-alteration-event-control
  334. * bit set. A kernel page table pointer doesn't need them.
  335. */
  336. #define _ASCE_USER_BITS (_ASCE_SPACE_SWITCH | _ASCE_PRIVATE_SPACE | \
  337. _ASCE_ALT_EVENT)
  338. /*
  339. * Page protection definitions.
  340. */
  341. #define PAGE_NONE __pgprot(_PAGE_TYPE_NONE)
  342. #define PAGE_RO __pgprot(_PAGE_TYPE_RO)
  343. #define PAGE_RW __pgprot(_PAGE_TYPE_RW)
  344. #define PAGE_KERNEL PAGE_RW
  345. #define PAGE_COPY PAGE_RO
  346. /*
  347. * On s390 the page table entry has an invalid bit and a read-only bit.
  348. * Read permission implies execute permission and write permission
  349. * implies read permission.
  350. */
  351. /*xwr*/
  352. #define __P000 PAGE_NONE
  353. #define __P001 PAGE_RO
  354. #define __P010 PAGE_RO
  355. #define __P011 PAGE_RO
  356. #define __P100 PAGE_RO
  357. #define __P101 PAGE_RO
  358. #define __P110 PAGE_RO
  359. #define __P111 PAGE_RO
  360. #define __S000 PAGE_NONE
  361. #define __S001 PAGE_RO
  362. #define __S010 PAGE_RW
  363. #define __S011 PAGE_RW
  364. #define __S100 PAGE_RO
  365. #define __S101 PAGE_RO
  366. #define __S110 PAGE_RW
  367. #define __S111 PAGE_RW
  368. static inline int mm_exclusive(struct mm_struct *mm)
  369. {
  370. return likely(mm == current->active_mm &&
  371. atomic_read(&mm->context.attach_count) <= 1);
  372. }
  373. static inline int mm_has_pgste(struct mm_struct *mm)
  374. {
  375. #ifdef CONFIG_PGSTE
  376. if (unlikely(mm->context.has_pgste))
  377. return 1;
  378. #endif
  379. return 0;
  380. }
  381. /*
  382. * pgd/pmd/pte query functions
  383. */
  384. #ifndef __s390x__
  385. static inline int pgd_present(pgd_t pgd) { return 1; }
  386. static inline int pgd_none(pgd_t pgd) { return 0; }
  387. static inline int pgd_bad(pgd_t pgd) { return 0; }
  388. static inline int pud_present(pud_t pud) { return 1; }
  389. static inline int pud_none(pud_t pud) { return 0; }
  390. static inline int pud_bad(pud_t pud) { return 0; }
  391. #else /* __s390x__ */
  392. static inline int pgd_present(pgd_t pgd)
  393. {
  394. if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R2)
  395. return 1;
  396. return (pgd_val(pgd) & _REGION_ENTRY_ORIGIN) != 0UL;
  397. }
  398. static inline int pgd_none(pgd_t pgd)
  399. {
  400. if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R2)
  401. return 0;
  402. return (pgd_val(pgd) & _REGION_ENTRY_INV) != 0UL;
  403. }
  404. static inline int pgd_bad(pgd_t pgd)
  405. {
  406. /*
  407. * With dynamic page table levels the pgd can be a region table
  408. * entry or a segment table entry. Check for the bit that are
  409. * invalid for either table entry.
  410. */
  411. unsigned long mask =
  412. ~_SEGMENT_ENTRY_ORIGIN & ~_REGION_ENTRY_INV &
  413. ~_REGION_ENTRY_TYPE_MASK & ~_REGION_ENTRY_LENGTH;
  414. return (pgd_val(pgd) & mask) != 0;
  415. }
  416. static inline int pud_present(pud_t pud)
  417. {
  418. if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R3)
  419. return 1;
  420. return (pud_val(pud) & _REGION_ENTRY_ORIGIN) != 0UL;
  421. }
  422. static inline int pud_none(pud_t pud)
  423. {
  424. if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R3)
  425. return 0;
  426. return (pud_val(pud) & _REGION_ENTRY_INV) != 0UL;
  427. }
  428. static inline int pud_bad(pud_t pud)
  429. {
  430. /*
  431. * With dynamic page table levels the pud can be a region table
  432. * entry or a segment table entry. Check for the bit that are
  433. * invalid for either table entry.
  434. */
  435. unsigned long mask =
  436. ~_SEGMENT_ENTRY_ORIGIN & ~_REGION_ENTRY_INV &
  437. ~_REGION_ENTRY_TYPE_MASK & ~_REGION_ENTRY_LENGTH;
  438. return (pud_val(pud) & mask) != 0;
  439. }
  440. #endif /* __s390x__ */
  441. static inline int pmd_present(pmd_t pmd)
  442. {
  443. return (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN) != 0UL;
  444. }
  445. static inline int pmd_none(pmd_t pmd)
  446. {
  447. return (pmd_val(pmd) & _SEGMENT_ENTRY_INV) != 0UL;
  448. }
  449. static inline int pmd_bad(pmd_t pmd)
  450. {
  451. unsigned long mask = ~_SEGMENT_ENTRY_ORIGIN & ~_SEGMENT_ENTRY_INV;
  452. return (pmd_val(pmd) & mask) != _SEGMENT_ENTRY;
  453. }
  454. static inline int pte_none(pte_t pte)
  455. {
  456. return (pte_val(pte) & _PAGE_INVALID) && !(pte_val(pte) & _PAGE_SWT);
  457. }
  458. static inline int pte_present(pte_t pte)
  459. {
  460. unsigned long mask = _PAGE_RO | _PAGE_INVALID | _PAGE_SWT | _PAGE_SWX;
  461. return (pte_val(pte) & mask) == _PAGE_TYPE_NONE ||
  462. (!(pte_val(pte) & _PAGE_INVALID) &&
  463. !(pte_val(pte) & _PAGE_SWT));
  464. }
  465. static inline int pte_file(pte_t pte)
  466. {
  467. unsigned long mask = _PAGE_RO | _PAGE_INVALID | _PAGE_SWT;
  468. return (pte_val(pte) & mask) == _PAGE_TYPE_FILE;
  469. }
  470. static inline int pte_special(pte_t pte)
  471. {
  472. return (pte_val(pte) & _PAGE_SPECIAL);
  473. }
  474. #define __HAVE_ARCH_PTE_SAME
  475. static inline int pte_same(pte_t a, pte_t b)
  476. {
  477. return pte_val(a) == pte_val(b);
  478. }
  479. static inline pgste_t pgste_get_lock(pte_t *ptep)
  480. {
  481. unsigned long new = 0;
  482. #ifdef CONFIG_PGSTE
  483. unsigned long old;
  484. preempt_disable();
  485. asm(
  486. " lg %0,%2\n"
  487. "0: lgr %1,%0\n"
  488. " nihh %0,0xff7f\n" /* clear RCP_PCL_BIT in old */
  489. " oihh %1,0x0080\n" /* set RCP_PCL_BIT in new */
  490. " csg %0,%1,%2\n"
  491. " jl 0b\n"
  492. : "=&d" (old), "=&d" (new), "=Q" (ptep[PTRS_PER_PTE])
  493. : "Q" (ptep[PTRS_PER_PTE]) : "cc");
  494. #endif
  495. return __pgste(new);
  496. }
  497. static inline void pgste_set_unlock(pte_t *ptep, pgste_t pgste)
  498. {
  499. #ifdef CONFIG_PGSTE
  500. asm(
  501. " nihh %1,0xff7f\n" /* clear RCP_PCL_BIT */
  502. " stg %1,%0\n"
  503. : "=Q" (ptep[PTRS_PER_PTE])
  504. : "d" (pgste_val(pgste)), "Q" (ptep[PTRS_PER_PTE]) : "cc");
  505. preempt_enable();
  506. #endif
  507. }
  508. static inline pgste_t pgste_update_all(pte_t *ptep, pgste_t pgste)
  509. {
  510. #ifdef CONFIG_PGSTE
  511. unsigned long address, bits;
  512. unsigned char skey;
  513. address = pte_val(*ptep) & PAGE_MASK;
  514. skey = page_get_storage_key(address);
  515. bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
  516. /* Clear page changed & referenced bit in the storage key */
  517. if (bits) {
  518. skey ^= bits;
  519. page_set_storage_key(address, skey, 1);
  520. }
  521. /* Transfer page changed & referenced bit to guest bits in pgste */
  522. pgste_val(pgste) |= bits << 48; /* RCP_GR_BIT & RCP_GC_BIT */
  523. /* Get host changed & referenced bits from pgste */
  524. bits |= (pgste_val(pgste) & (RCP_HR_BIT | RCP_HC_BIT)) >> 52;
  525. /* Clear host bits in pgste. */
  526. pgste_val(pgste) &= ~(RCP_HR_BIT | RCP_HC_BIT);
  527. pgste_val(pgste) &= ~(RCP_ACC_BITS | RCP_FP_BIT);
  528. /* Copy page access key and fetch protection bit to pgste */
  529. pgste_val(pgste) |=
  530. (unsigned long) (skey & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
  531. /* Transfer changed and referenced to kvm user bits */
  532. pgste_val(pgste) |= bits << 45; /* KVM_UR_BIT & KVM_UC_BIT */
  533. /* Transfer changed & referenced to pte sofware bits */
  534. pte_val(*ptep) |= bits << 1; /* _PAGE_SWR & _PAGE_SWC */
  535. #endif
  536. return pgste;
  537. }
  538. static inline pgste_t pgste_update_young(pte_t *ptep, pgste_t pgste)
  539. {
  540. #ifdef CONFIG_PGSTE
  541. int young;
  542. young = page_reset_referenced(pte_val(*ptep) & PAGE_MASK);
  543. /* Transfer page referenced bit to pte software bit (host view) */
  544. if (young || (pgste_val(pgste) & RCP_HR_BIT))
  545. pte_val(*ptep) |= _PAGE_SWR;
  546. /* Clear host referenced bit in pgste. */
  547. pgste_val(pgste) &= ~RCP_HR_BIT;
  548. /* Transfer page referenced bit to guest bit in pgste */
  549. pgste_val(pgste) |= (unsigned long) young << 50; /* set RCP_GR_BIT */
  550. #endif
  551. return pgste;
  552. }
  553. static inline void pgste_set_pte(pte_t *ptep, pgste_t pgste)
  554. {
  555. #ifdef CONFIG_PGSTE
  556. unsigned long address;
  557. unsigned long okey, nkey;
  558. address = pte_val(*ptep) & PAGE_MASK;
  559. okey = nkey = page_get_storage_key(address);
  560. nkey &= ~(_PAGE_ACC_BITS | _PAGE_FP_BIT);
  561. /* Set page access key and fetch protection bit from pgste */
  562. nkey |= (pgste_val(pgste) & (RCP_ACC_BITS | RCP_FP_BIT)) >> 56;
  563. if (okey != nkey)
  564. page_set_storage_key(address, nkey, 1);
  565. #endif
  566. }
  567. /*
  568. * Certain architectures need to do special things when PTEs
  569. * within a page table are directly modified. Thus, the following
  570. * hook is made available.
  571. */
  572. static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
  573. pte_t *ptep, pte_t entry)
  574. {
  575. pgste_t pgste;
  576. if (mm_has_pgste(mm)) {
  577. pgste = pgste_get_lock(ptep);
  578. pgste_set_pte(ptep, pgste);
  579. *ptep = entry;
  580. pgste_set_unlock(ptep, pgste);
  581. } else
  582. *ptep = entry;
  583. }
  584. /*
  585. * query functions pte_write/pte_dirty/pte_young only work if
  586. * pte_present() is true. Undefined behaviour if not..
  587. */
  588. static inline int pte_write(pte_t pte)
  589. {
  590. return (pte_val(pte) & _PAGE_RO) == 0;
  591. }
  592. static inline int pte_dirty(pte_t pte)
  593. {
  594. #ifdef CONFIG_PGSTE
  595. if (pte_val(pte) & _PAGE_SWC)
  596. return 1;
  597. #endif
  598. return 0;
  599. }
  600. static inline int pte_young(pte_t pte)
  601. {
  602. #ifdef CONFIG_PGSTE
  603. if (pte_val(pte) & _PAGE_SWR)
  604. return 1;
  605. #endif
  606. return 0;
  607. }
  608. /*
  609. * pgd/pmd/pte modification functions
  610. */
  611. static inline void pgd_clear(pgd_t *pgd)
  612. {
  613. #ifdef __s390x__
  614. if ((pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
  615. pgd_val(*pgd) = _REGION2_ENTRY_EMPTY;
  616. #endif
  617. }
  618. static inline void pud_clear(pud_t *pud)
  619. {
  620. #ifdef __s390x__
  621. if ((pud_val(*pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
  622. pud_val(*pud) = _REGION3_ENTRY_EMPTY;
  623. #endif
  624. }
  625. static inline void pmd_clear(pmd_t *pmdp)
  626. {
  627. pmd_val(*pmdp) = _SEGMENT_ENTRY_EMPTY;
  628. }
  629. static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  630. {
  631. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  632. }
  633. /*
  634. * The following pte modification functions only work if
  635. * pte_present() is true. Undefined behaviour if not..
  636. */
  637. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  638. {
  639. pte_val(pte) &= _PAGE_CHG_MASK;
  640. pte_val(pte) |= pgprot_val(newprot);
  641. return pte;
  642. }
  643. static inline pte_t pte_wrprotect(pte_t pte)
  644. {
  645. /* Do not clobber _PAGE_TYPE_NONE pages! */
  646. if (!(pte_val(pte) & _PAGE_INVALID))
  647. pte_val(pte) |= _PAGE_RO;
  648. return pte;
  649. }
  650. static inline pte_t pte_mkwrite(pte_t pte)
  651. {
  652. pte_val(pte) &= ~_PAGE_RO;
  653. return pte;
  654. }
  655. static inline pte_t pte_mkclean(pte_t pte)
  656. {
  657. #ifdef CONFIG_PGSTE
  658. pte_val(pte) &= ~_PAGE_SWC;
  659. #endif
  660. return pte;
  661. }
  662. static inline pte_t pte_mkdirty(pte_t pte)
  663. {
  664. return pte;
  665. }
  666. static inline pte_t pte_mkold(pte_t pte)
  667. {
  668. #ifdef CONFIG_PGSTE
  669. pte_val(pte) &= ~_PAGE_SWR;
  670. #endif
  671. return pte;
  672. }
  673. static inline pte_t pte_mkyoung(pte_t pte)
  674. {
  675. return pte;
  676. }
  677. static inline pte_t pte_mkspecial(pte_t pte)
  678. {
  679. pte_val(pte) |= _PAGE_SPECIAL;
  680. return pte;
  681. }
  682. #ifdef CONFIG_HUGETLB_PAGE
  683. static inline pte_t pte_mkhuge(pte_t pte)
  684. {
  685. /*
  686. * PROT_NONE needs to be remapped from the pte type to the ste type.
  687. * The HW invalid bit is also different for pte and ste. The pte
  688. * invalid bit happens to be the same as the ste _SEGMENT_ENTRY_LARGE
  689. * bit, so we don't have to clear it.
  690. */
  691. if (pte_val(pte) & _PAGE_INVALID) {
  692. if (pte_val(pte) & _PAGE_SWT)
  693. pte_val(pte) |= _HPAGE_TYPE_NONE;
  694. pte_val(pte) |= _SEGMENT_ENTRY_INV;
  695. }
  696. /*
  697. * Clear SW pte bits SWT and SWX, there are no SW bits in a segment
  698. * table entry.
  699. */
  700. pte_val(pte) &= ~(_PAGE_SWT | _PAGE_SWX);
  701. /*
  702. * Also set the change-override bit because we don't need dirty bit
  703. * tracking for hugetlbfs pages.
  704. */
  705. pte_val(pte) |= (_SEGMENT_ENTRY_LARGE | _SEGMENT_ENTRY_CO);
  706. return pte;
  707. }
  708. #endif
  709. /*
  710. * Get (and clear) the user dirty bit for a pte.
  711. */
  712. static inline int ptep_test_and_clear_user_dirty(struct mm_struct *mm,
  713. pte_t *ptep)
  714. {
  715. pgste_t pgste;
  716. int dirty = 0;
  717. if (mm_has_pgste(mm)) {
  718. pgste = pgste_get_lock(ptep);
  719. pgste = pgste_update_all(ptep, pgste);
  720. dirty = !!(pgste_val(pgste) & KVM_UC_BIT);
  721. pgste_val(pgste) &= ~KVM_UC_BIT;
  722. pgste_set_unlock(ptep, pgste);
  723. return dirty;
  724. }
  725. return dirty;
  726. }
  727. /*
  728. * Get (and clear) the user referenced bit for a pte.
  729. */
  730. static inline int ptep_test_and_clear_user_young(struct mm_struct *mm,
  731. pte_t *ptep)
  732. {
  733. pgste_t pgste;
  734. int young = 0;
  735. if (mm_has_pgste(mm)) {
  736. pgste = pgste_get_lock(ptep);
  737. pgste = pgste_update_young(ptep, pgste);
  738. young = !!(pgste_val(pgste) & KVM_UR_BIT);
  739. pgste_val(pgste) &= ~KVM_UR_BIT;
  740. pgste_set_unlock(ptep, pgste);
  741. }
  742. return young;
  743. }
  744. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  745. static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
  746. unsigned long addr, pte_t *ptep)
  747. {
  748. pgste_t pgste;
  749. pte_t pte;
  750. if (mm_has_pgste(vma->vm_mm)) {
  751. pgste = pgste_get_lock(ptep);
  752. pgste = pgste_update_young(ptep, pgste);
  753. pte = *ptep;
  754. *ptep = pte_mkold(pte);
  755. pgste_set_unlock(ptep, pgste);
  756. return pte_young(pte);
  757. }
  758. return 0;
  759. }
  760. #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  761. static inline int ptep_clear_flush_young(struct vm_area_struct *vma,
  762. unsigned long address, pte_t *ptep)
  763. {
  764. /* No need to flush TLB
  765. * On s390 reference bits are in storage key and never in TLB
  766. * With virtualization we handle the reference bit, without we
  767. * we can simply return */
  768. return ptep_test_and_clear_young(vma, address, ptep);
  769. }
  770. static inline void __ptep_ipte(unsigned long address, pte_t *ptep)
  771. {
  772. if (!(pte_val(*ptep) & _PAGE_INVALID)) {
  773. #ifndef __s390x__
  774. /* pto must point to the start of the segment table */
  775. pte_t *pto = (pte_t *) (((unsigned long) ptep) & 0x7ffffc00);
  776. #else
  777. /* ipte in zarch mode can do the math */
  778. pte_t *pto = ptep;
  779. #endif
  780. asm volatile(
  781. " ipte %2,%3"
  782. : "=m" (*ptep) : "m" (*ptep),
  783. "a" (pto), "a" (address));
  784. }
  785. }
  786. /*
  787. * This is hard to understand. ptep_get_and_clear and ptep_clear_flush
  788. * both clear the TLB for the unmapped pte. The reason is that
  789. * ptep_get_and_clear is used in common code (e.g. change_pte_range)
  790. * to modify an active pte. The sequence is
  791. * 1) ptep_get_and_clear
  792. * 2) set_pte_at
  793. * 3) flush_tlb_range
  794. * On s390 the tlb needs to get flushed with the modification of the pte
  795. * if the pte is active. The only way how this can be implemented is to
  796. * have ptep_get_and_clear do the tlb flush. In exchange flush_tlb_range
  797. * is a nop.
  798. */
  799. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  800. static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
  801. unsigned long address, pte_t *ptep)
  802. {
  803. pgste_t pgste;
  804. pte_t pte;
  805. mm->context.flush_mm = 1;
  806. if (mm_has_pgste(mm))
  807. pgste = pgste_get_lock(ptep);
  808. pte = *ptep;
  809. if (!mm_exclusive(mm))
  810. __ptep_ipte(address, ptep);
  811. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  812. if (mm_has_pgste(mm)) {
  813. pgste = pgste_update_all(&pte, pgste);
  814. pgste_set_unlock(ptep, pgste);
  815. }
  816. return pte;
  817. }
  818. #define __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
  819. static inline pte_t ptep_modify_prot_start(struct mm_struct *mm,
  820. unsigned long address,
  821. pte_t *ptep)
  822. {
  823. pte_t pte;
  824. mm->context.flush_mm = 1;
  825. if (mm_has_pgste(mm))
  826. pgste_get_lock(ptep);
  827. pte = *ptep;
  828. if (!mm_exclusive(mm))
  829. __ptep_ipte(address, ptep);
  830. return pte;
  831. }
  832. static inline void ptep_modify_prot_commit(struct mm_struct *mm,
  833. unsigned long address,
  834. pte_t *ptep, pte_t pte)
  835. {
  836. *ptep = pte;
  837. if (mm_has_pgste(mm))
  838. pgste_set_unlock(ptep, *(pgste_t *)(ptep + PTRS_PER_PTE));
  839. }
  840. #define __HAVE_ARCH_PTEP_CLEAR_FLUSH
  841. static inline pte_t ptep_clear_flush(struct vm_area_struct *vma,
  842. unsigned long address, pte_t *ptep)
  843. {
  844. pgste_t pgste;
  845. pte_t pte;
  846. if (mm_has_pgste(vma->vm_mm))
  847. pgste = pgste_get_lock(ptep);
  848. pte = *ptep;
  849. __ptep_ipte(address, ptep);
  850. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  851. if (mm_has_pgste(vma->vm_mm)) {
  852. pgste = pgste_update_all(&pte, pgste);
  853. pgste_set_unlock(ptep, pgste);
  854. }
  855. return pte;
  856. }
  857. /*
  858. * The batched pte unmap code uses ptep_get_and_clear_full to clear the
  859. * ptes. Here an optimization is possible. tlb_gather_mmu flushes all
  860. * tlbs of an mm if it can guarantee that the ptes of the mm_struct
  861. * cannot be accessed while the batched unmap is running. In this case
  862. * full==1 and a simple pte_clear is enough. See tlb.h.
  863. */
  864. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
  865. static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
  866. unsigned long address,
  867. pte_t *ptep, int full)
  868. {
  869. pgste_t pgste;
  870. pte_t pte;
  871. if (mm_has_pgste(mm))
  872. pgste = pgste_get_lock(ptep);
  873. pte = *ptep;
  874. if (!full)
  875. __ptep_ipte(address, ptep);
  876. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  877. if (mm_has_pgste(mm)) {
  878. pgste = pgste_update_all(&pte, pgste);
  879. pgste_set_unlock(ptep, pgste);
  880. }
  881. return pte;
  882. }
  883. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  884. static inline pte_t ptep_set_wrprotect(struct mm_struct *mm,
  885. unsigned long address, pte_t *ptep)
  886. {
  887. pgste_t pgste;
  888. pte_t pte = *ptep;
  889. if (pte_write(pte)) {
  890. mm->context.flush_mm = 1;
  891. if (mm_has_pgste(mm))
  892. pgste = pgste_get_lock(ptep);
  893. if (!mm_exclusive(mm))
  894. __ptep_ipte(address, ptep);
  895. *ptep = pte_wrprotect(pte);
  896. if (mm_has_pgste(mm))
  897. pgste_set_unlock(ptep, pgste);
  898. }
  899. return pte;
  900. }
  901. #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  902. static inline int ptep_set_access_flags(struct vm_area_struct *vma,
  903. unsigned long address, pte_t *ptep,
  904. pte_t entry, int dirty)
  905. {
  906. pgste_t pgste;
  907. if (pte_same(*ptep, entry))
  908. return 0;
  909. if (mm_has_pgste(vma->vm_mm))
  910. pgste = pgste_get_lock(ptep);
  911. __ptep_ipte(address, ptep);
  912. *ptep = entry;
  913. if (mm_has_pgste(vma->vm_mm))
  914. pgste_set_unlock(ptep, pgste);
  915. return 1;
  916. }
  917. /*
  918. * Conversion functions: convert a page and protection to a page entry,
  919. * and a page entry and page directory to the page they refer to.
  920. */
  921. static inline pte_t mk_pte_phys(unsigned long physpage, pgprot_t pgprot)
  922. {
  923. pte_t __pte;
  924. pte_val(__pte) = physpage + pgprot_val(pgprot);
  925. return __pte;
  926. }
  927. static inline pte_t mk_pte(struct page *page, pgprot_t pgprot)
  928. {
  929. unsigned long physpage = page_to_phys(page);
  930. return mk_pte_phys(physpage, pgprot);
  931. }
  932. #define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
  933. #define pud_index(address) (((address) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
  934. #define pmd_index(address) (((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
  935. #define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE-1))
  936. #define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
  937. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  938. #ifndef __s390x__
  939. #define pmd_deref(pmd) (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN)
  940. #define pud_deref(pmd) ({ BUG(); 0UL; })
  941. #define pgd_deref(pmd) ({ BUG(); 0UL; })
  942. #define pud_offset(pgd, address) ((pud_t *) pgd)
  943. #define pmd_offset(pud, address) ((pmd_t *) pud + pmd_index(address))
  944. #else /* __s390x__ */
  945. #define pmd_deref(pmd) (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN)
  946. #define pud_deref(pud) (pud_val(pud) & _REGION_ENTRY_ORIGIN)
  947. #define pgd_deref(pgd) (pgd_val(pgd) & _REGION_ENTRY_ORIGIN)
  948. static inline pud_t *pud_offset(pgd_t *pgd, unsigned long address)
  949. {
  950. pud_t *pud = (pud_t *) pgd;
  951. if ((pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
  952. pud = (pud_t *) pgd_deref(*pgd);
  953. return pud + pud_index(address);
  954. }
  955. static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address)
  956. {
  957. pmd_t *pmd = (pmd_t *) pud;
  958. if ((pud_val(*pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
  959. pmd = (pmd_t *) pud_deref(*pud);
  960. return pmd + pmd_index(address);
  961. }
  962. #endif /* __s390x__ */
  963. #define pfn_pte(pfn,pgprot) mk_pte_phys(__pa((pfn) << PAGE_SHIFT),(pgprot))
  964. #define pte_pfn(x) (pte_val(x) >> PAGE_SHIFT)
  965. #define pte_page(x) pfn_to_page(pte_pfn(x))
  966. #define pmd_page(pmd) pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT)
  967. /* Find an entry in the lowest level page table.. */
  968. #define pte_offset(pmd, addr) ((pte_t *) pmd_deref(*(pmd)) + pte_index(addr))
  969. #define pte_offset_kernel(pmd, address) pte_offset(pmd,address)
  970. #define pte_offset_map(pmd, address) pte_offset_kernel(pmd, address)
  971. #define pte_unmap(pte) do { } while (0)
  972. /*
  973. * 31 bit swap entry format:
  974. * A page-table entry has some bits we have to treat in a special way.
  975. * Bits 0, 20 and bit 23 have to be zero, otherwise an specification
  976. * exception will occur instead of a page translation exception. The
  977. * specifiation exception has the bad habit not to store necessary
  978. * information in the lowcore.
  979. * Bit 21 and bit 22 are the page invalid bit and the page protection
  980. * bit. We set both to indicate a swapped page.
  981. * Bit 30 and 31 are used to distinguish the different page types. For
  982. * a swapped page these bits need to be zero.
  983. * This leaves the bits 1-19 and bits 24-29 to store type and offset.
  984. * We use the 5 bits from 25-29 for the type and the 20 bits from 1-19
  985. * plus 24 for the offset.
  986. * 0| offset |0110|o|type |00|
  987. * 0 0000000001111111111 2222 2 22222 33
  988. * 0 1234567890123456789 0123 4 56789 01
  989. *
  990. * 64 bit swap entry format:
  991. * A page-table entry has some bits we have to treat in a special way.
  992. * Bits 52 and bit 55 have to be zero, otherwise an specification
  993. * exception will occur instead of a page translation exception. The
  994. * specifiation exception has the bad habit not to store necessary
  995. * information in the lowcore.
  996. * Bit 53 and bit 54 are the page invalid bit and the page protection
  997. * bit. We set both to indicate a swapped page.
  998. * Bit 62 and 63 are used to distinguish the different page types. For
  999. * a swapped page these bits need to be zero.
  1000. * This leaves the bits 0-51 and bits 56-61 to store type and offset.
  1001. * We use the 5 bits from 57-61 for the type and the 53 bits from 0-51
  1002. * plus 56 for the offset.
  1003. * | offset |0110|o|type |00|
  1004. * 0000000000111111111122222222223333333333444444444455 5555 5 55566 66
  1005. * 0123456789012345678901234567890123456789012345678901 2345 6 78901 23
  1006. */
  1007. #ifndef __s390x__
  1008. #define __SWP_OFFSET_MASK (~0UL >> 12)
  1009. #else
  1010. #define __SWP_OFFSET_MASK (~0UL >> 11)
  1011. #endif
  1012. static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset)
  1013. {
  1014. pte_t pte;
  1015. offset &= __SWP_OFFSET_MASK;
  1016. pte_val(pte) = _PAGE_TYPE_SWAP | ((type & 0x1f) << 2) |
  1017. ((offset & 1UL) << 7) | ((offset & ~1UL) << 11);
  1018. return pte;
  1019. }
  1020. #define __swp_type(entry) (((entry).val >> 2) & 0x1f)
  1021. #define __swp_offset(entry) (((entry).val >> 11) | (((entry).val >> 7) & 1))
  1022. #define __swp_entry(type,offset) ((swp_entry_t) { pte_val(mk_swap_pte((type),(offset))) })
  1023. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  1024. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  1025. #ifndef __s390x__
  1026. # define PTE_FILE_MAX_BITS 26
  1027. #else /* __s390x__ */
  1028. # define PTE_FILE_MAX_BITS 59
  1029. #endif /* __s390x__ */
  1030. #define pte_to_pgoff(__pte) \
  1031. ((((__pte).pte >> 12) << 7) + (((__pte).pte >> 1) & 0x7f))
  1032. #define pgoff_to_pte(__off) \
  1033. ((pte_t) { ((((__off) & 0x7f) << 1) + (((__off) >> 7) << 12)) \
  1034. | _PAGE_TYPE_FILE })
  1035. #endif /* !__ASSEMBLY__ */
  1036. #define kern_addr_valid(addr) (1)
  1037. extern int vmem_add_mapping(unsigned long start, unsigned long size);
  1038. extern int vmem_remove_mapping(unsigned long start, unsigned long size);
  1039. extern int s390_enable_sie(void);
  1040. /*
  1041. * No page table caches to initialise
  1042. */
  1043. #define pgtable_cache_init() do { } while (0)
  1044. #include <asm-generic/pgtable.h>
  1045. #endif /* _S390_PAGE_H */