pgtable.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  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. #ifndef __s390x__
  270. /* Bits in the segment table address-space-control-element */
  271. #define _ASCE_SPACE_SWITCH 0x80000000UL /* space switch event */
  272. #define _ASCE_ORIGIN_MASK 0x7ffff000UL /* segment table origin */
  273. #define _ASCE_PRIVATE_SPACE 0x100 /* private space control */
  274. #define _ASCE_ALT_EVENT 0x80 /* storage alteration event control */
  275. #define _ASCE_TABLE_LENGTH 0x7f /* 128 x 64 entries = 8k */
  276. /* Bits in the segment table entry */
  277. #define _SEGMENT_ENTRY_ORIGIN 0x7fffffc0UL /* page table origin */
  278. #define _SEGMENT_ENTRY_RO 0x200 /* page protection bit */
  279. #define _SEGMENT_ENTRY_INV 0x20 /* invalid segment table entry */
  280. #define _SEGMENT_ENTRY_COMMON 0x10 /* common segment bit */
  281. #define _SEGMENT_ENTRY_PTL 0x0f /* page table length */
  282. #define _SEGMENT_ENTRY (_SEGMENT_ENTRY_PTL)
  283. #define _SEGMENT_ENTRY_EMPTY (_SEGMENT_ENTRY_INV)
  284. /* Page status table bits for virtualization */
  285. #define RCP_ACC_BITS 0xf0000000UL
  286. #define RCP_FP_BIT 0x08000000UL
  287. #define RCP_PCL_BIT 0x00800000UL
  288. #define RCP_HR_BIT 0x00400000UL
  289. #define RCP_HC_BIT 0x00200000UL
  290. #define RCP_GR_BIT 0x00040000UL
  291. #define RCP_GC_BIT 0x00020000UL
  292. /* User dirty / referenced bit for KVM's migration feature */
  293. #define KVM_UR_BIT 0x00008000UL
  294. #define KVM_UC_BIT 0x00004000UL
  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. /* Page status table bits for virtualization */
  331. #define RCP_ACC_BITS 0xf000000000000000UL
  332. #define RCP_FP_BIT 0x0800000000000000UL
  333. #define RCP_PCL_BIT 0x0080000000000000UL
  334. #define RCP_HR_BIT 0x0040000000000000UL
  335. #define RCP_HC_BIT 0x0020000000000000UL
  336. #define RCP_GR_BIT 0x0004000000000000UL
  337. #define RCP_GC_BIT 0x0002000000000000UL
  338. /* User dirty / referenced bit for KVM's migration feature */
  339. #define KVM_UR_BIT 0x0000800000000000UL
  340. #define KVM_UC_BIT 0x0000400000000000UL
  341. #endif /* __s390x__ */
  342. /*
  343. * A user page table pointer has the space-switch-event bit, the
  344. * private-space-control bit and the storage-alteration-event-control
  345. * bit set. A kernel page table pointer doesn't need them.
  346. */
  347. #define _ASCE_USER_BITS (_ASCE_SPACE_SWITCH | _ASCE_PRIVATE_SPACE | \
  348. _ASCE_ALT_EVENT)
  349. /*
  350. * Page protection definitions.
  351. */
  352. #define PAGE_NONE __pgprot(_PAGE_TYPE_NONE)
  353. #define PAGE_RO __pgprot(_PAGE_TYPE_RO)
  354. #define PAGE_RW __pgprot(_PAGE_TYPE_RW)
  355. #define PAGE_KERNEL PAGE_RW
  356. #define PAGE_COPY PAGE_RO
  357. /*
  358. * On s390 the page table entry has an invalid bit and a read-only bit.
  359. * Read permission implies execute permission and write permission
  360. * implies read permission.
  361. */
  362. /*xwr*/
  363. #define __P000 PAGE_NONE
  364. #define __P001 PAGE_RO
  365. #define __P010 PAGE_RO
  366. #define __P011 PAGE_RO
  367. #define __P100 PAGE_RO
  368. #define __P101 PAGE_RO
  369. #define __P110 PAGE_RO
  370. #define __P111 PAGE_RO
  371. #define __S000 PAGE_NONE
  372. #define __S001 PAGE_RO
  373. #define __S010 PAGE_RW
  374. #define __S011 PAGE_RW
  375. #define __S100 PAGE_RO
  376. #define __S101 PAGE_RO
  377. #define __S110 PAGE_RW
  378. #define __S111 PAGE_RW
  379. static inline int mm_exclusive(struct mm_struct *mm)
  380. {
  381. return likely(mm == current->active_mm &&
  382. atomic_read(&mm->context.attach_count) <= 1);
  383. }
  384. static inline int mm_has_pgste(struct mm_struct *mm)
  385. {
  386. #ifdef CONFIG_PGSTE
  387. if (unlikely(mm->context.has_pgste))
  388. return 1;
  389. #endif
  390. return 0;
  391. }
  392. /*
  393. * pgd/pmd/pte query functions
  394. */
  395. #ifndef __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) { return 1; }
  400. static inline int pud_none(pud_t pud) { return 0; }
  401. static inline int pud_bad(pud_t pud) { return 0; }
  402. #else /* __s390x__ */
  403. static inline int pgd_present(pgd_t pgd)
  404. {
  405. if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R2)
  406. return 1;
  407. return (pgd_val(pgd) & _REGION_ENTRY_ORIGIN) != 0UL;
  408. }
  409. static inline int pgd_none(pgd_t pgd)
  410. {
  411. if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R2)
  412. return 0;
  413. return (pgd_val(pgd) & _REGION_ENTRY_INV) != 0UL;
  414. }
  415. static inline int pgd_bad(pgd_t pgd)
  416. {
  417. /*
  418. * With dynamic page table levels the pgd can be a region table
  419. * entry or a segment table entry. Check for the bit that are
  420. * invalid for either table entry.
  421. */
  422. unsigned long mask =
  423. ~_SEGMENT_ENTRY_ORIGIN & ~_REGION_ENTRY_INV &
  424. ~_REGION_ENTRY_TYPE_MASK & ~_REGION_ENTRY_LENGTH;
  425. return (pgd_val(pgd) & mask) != 0;
  426. }
  427. static inline int pud_present(pud_t pud)
  428. {
  429. if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R3)
  430. return 1;
  431. return (pud_val(pud) & _REGION_ENTRY_ORIGIN) != 0UL;
  432. }
  433. static inline int pud_none(pud_t pud)
  434. {
  435. if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R3)
  436. return 0;
  437. return (pud_val(pud) & _REGION_ENTRY_INV) != 0UL;
  438. }
  439. static inline int pud_bad(pud_t pud)
  440. {
  441. /*
  442. * With dynamic page table levels the pud can be a region table
  443. * entry or a segment table entry. Check for the bit that are
  444. * invalid for either table entry.
  445. */
  446. unsigned long mask =
  447. ~_SEGMENT_ENTRY_ORIGIN & ~_REGION_ENTRY_INV &
  448. ~_REGION_ENTRY_TYPE_MASK & ~_REGION_ENTRY_LENGTH;
  449. return (pud_val(pud) & mask) != 0;
  450. }
  451. #endif /* __s390x__ */
  452. static inline int pmd_present(pmd_t pmd)
  453. {
  454. return (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN) != 0UL;
  455. }
  456. static inline int pmd_none(pmd_t pmd)
  457. {
  458. return (pmd_val(pmd) & _SEGMENT_ENTRY_INV) != 0UL;
  459. }
  460. static inline int pmd_bad(pmd_t pmd)
  461. {
  462. unsigned long mask = ~_SEGMENT_ENTRY_ORIGIN & ~_SEGMENT_ENTRY_INV;
  463. return (pmd_val(pmd) & mask) != _SEGMENT_ENTRY;
  464. }
  465. static inline int pte_none(pte_t pte)
  466. {
  467. return (pte_val(pte) & _PAGE_INVALID) && !(pte_val(pte) & _PAGE_SWT);
  468. }
  469. static inline int pte_present(pte_t pte)
  470. {
  471. unsigned long mask = _PAGE_RO | _PAGE_INVALID | _PAGE_SWT | _PAGE_SWX;
  472. return (pte_val(pte) & mask) == _PAGE_TYPE_NONE ||
  473. (!(pte_val(pte) & _PAGE_INVALID) &&
  474. !(pte_val(pte) & _PAGE_SWT));
  475. }
  476. static inline int pte_file(pte_t pte)
  477. {
  478. unsigned long mask = _PAGE_RO | _PAGE_INVALID | _PAGE_SWT;
  479. return (pte_val(pte) & mask) == _PAGE_TYPE_FILE;
  480. }
  481. static inline int pte_special(pte_t pte)
  482. {
  483. return (pte_val(pte) & _PAGE_SPECIAL);
  484. }
  485. #define __HAVE_ARCH_PTE_SAME
  486. static inline int pte_same(pte_t a, pte_t b)
  487. {
  488. return pte_val(a) == pte_val(b);
  489. }
  490. static inline pgste_t pgste_get_lock(pte_t *ptep)
  491. {
  492. unsigned long new = 0;
  493. #ifdef CONFIG_PGSTE
  494. unsigned long old;
  495. preempt_disable();
  496. asm(
  497. " lg %0,%2\n"
  498. "0: lgr %1,%0\n"
  499. " nihh %0,0xff7f\n" /* clear RCP_PCL_BIT in old */
  500. " oihh %1,0x0080\n" /* set RCP_PCL_BIT in new */
  501. " csg %0,%1,%2\n"
  502. " jl 0b\n"
  503. : "=&d" (old), "=&d" (new), "=Q" (ptep[PTRS_PER_PTE])
  504. : "Q" (ptep[PTRS_PER_PTE]) : "cc");
  505. #endif
  506. return __pgste(new);
  507. }
  508. static inline void pgste_set_unlock(pte_t *ptep, pgste_t pgste)
  509. {
  510. #ifdef CONFIG_PGSTE
  511. asm(
  512. " nihh %1,0xff7f\n" /* clear RCP_PCL_BIT */
  513. " stg %1,%0\n"
  514. : "=Q" (ptep[PTRS_PER_PTE])
  515. : "d" (pgste_val(pgste)), "Q" (ptep[PTRS_PER_PTE]) : "cc");
  516. preempt_enable();
  517. #endif
  518. }
  519. static inline pgste_t pgste_update_all(pte_t *ptep, pgste_t pgste)
  520. {
  521. #ifdef CONFIG_PGSTE
  522. unsigned long address, bits;
  523. unsigned char skey;
  524. address = pte_val(*ptep) & PAGE_MASK;
  525. skey = page_get_storage_key(address);
  526. bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
  527. /* Clear page changed & referenced bit in the storage key */
  528. if (bits) {
  529. skey ^= bits;
  530. page_set_storage_key(address, skey, 1);
  531. }
  532. /* Transfer page changed & referenced bit to guest bits in pgste */
  533. pgste_val(pgste) |= bits << 48; /* RCP_GR_BIT & RCP_GC_BIT */
  534. /* Get host changed & referenced bits from pgste */
  535. bits |= (pgste_val(pgste) & (RCP_HR_BIT | RCP_HC_BIT)) >> 52;
  536. /* Clear host bits in pgste. */
  537. pgste_val(pgste) &= ~(RCP_HR_BIT | RCP_HC_BIT);
  538. pgste_val(pgste) &= ~(RCP_ACC_BITS | RCP_FP_BIT);
  539. /* Copy page access key and fetch protection bit to pgste */
  540. pgste_val(pgste) |=
  541. (unsigned long) (skey & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
  542. /* Transfer changed and referenced to kvm user bits */
  543. pgste_val(pgste) |= bits << 45; /* KVM_UR_BIT & KVM_UC_BIT */
  544. /* Transfer changed & referenced to pte sofware bits */
  545. pte_val(*ptep) |= bits << 1; /* _PAGE_SWR & _PAGE_SWC */
  546. #endif
  547. return pgste;
  548. }
  549. static inline pgste_t pgste_update_young(pte_t *ptep, pgste_t pgste)
  550. {
  551. #ifdef CONFIG_PGSTE
  552. int young;
  553. young = page_reset_referenced(pte_val(*ptep) & PAGE_MASK);
  554. /* Transfer page referenced bit to pte software bit (host view) */
  555. if (young || (pgste_val(pgste) & RCP_HR_BIT))
  556. pte_val(*ptep) |= _PAGE_SWR;
  557. /* Clear host referenced bit in pgste. */
  558. pgste_val(pgste) &= ~RCP_HR_BIT;
  559. /* Transfer page referenced bit to guest bit in pgste */
  560. pgste_val(pgste) |= (unsigned long) young << 50; /* set RCP_GR_BIT */
  561. #endif
  562. return pgste;
  563. }
  564. static inline void pgste_set_pte(pte_t *ptep, pgste_t pgste)
  565. {
  566. #ifdef CONFIG_PGSTE
  567. unsigned long address;
  568. unsigned long okey, nkey;
  569. address = pte_val(*ptep) & PAGE_MASK;
  570. okey = nkey = page_get_storage_key(address);
  571. nkey &= ~(_PAGE_ACC_BITS | _PAGE_FP_BIT);
  572. /* Set page access key and fetch protection bit from pgste */
  573. nkey |= (pgste_val(pgste) & (RCP_ACC_BITS | RCP_FP_BIT)) >> 56;
  574. if (okey != nkey)
  575. page_set_storage_key(address, nkey, 1);
  576. #endif
  577. }
  578. /**
  579. * struct gmap_struct - guest address space
  580. * @mm: pointer to the parent mm_struct
  581. * @table: pointer to the page directory
  582. * @asce: address space control element for gmap page table
  583. * @crst_list: list of all crst tables used in the guest address space
  584. */
  585. struct gmap {
  586. struct list_head list;
  587. struct mm_struct *mm;
  588. unsigned long *table;
  589. unsigned long asce;
  590. struct list_head crst_list;
  591. };
  592. /**
  593. * struct gmap_rmap - reverse mapping for segment table entries
  594. * @next: pointer to the next gmap_rmap structure in the list
  595. * @entry: pointer to a segment table entry
  596. */
  597. struct gmap_rmap {
  598. struct list_head list;
  599. unsigned long *entry;
  600. };
  601. /**
  602. * struct gmap_pgtable - gmap information attached to a page table
  603. * @vmaddr: address of the 1MB segment in the process virtual memory
  604. * @mapper: list of segment table entries maping a page table
  605. */
  606. struct gmap_pgtable {
  607. unsigned long vmaddr;
  608. struct list_head mapper;
  609. };
  610. struct gmap *gmap_alloc(struct mm_struct *mm);
  611. void gmap_free(struct gmap *gmap);
  612. void gmap_enable(struct gmap *gmap);
  613. void gmap_disable(struct gmap *gmap);
  614. int gmap_map_segment(struct gmap *gmap, unsigned long from,
  615. unsigned long to, unsigned long length);
  616. int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len);
  617. unsigned long gmap_fault(unsigned long address, struct gmap *);
  618. /*
  619. * Certain architectures need to do special things when PTEs
  620. * within a page table are directly modified. Thus, the following
  621. * hook is made available.
  622. */
  623. static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
  624. pte_t *ptep, pte_t entry)
  625. {
  626. pgste_t pgste;
  627. if (mm_has_pgste(mm)) {
  628. pgste = pgste_get_lock(ptep);
  629. pgste_set_pte(ptep, pgste);
  630. *ptep = entry;
  631. pgste_set_unlock(ptep, pgste);
  632. } else
  633. *ptep = entry;
  634. }
  635. /*
  636. * query functions pte_write/pte_dirty/pte_young only work if
  637. * pte_present() is true. Undefined behaviour if not..
  638. */
  639. static inline int pte_write(pte_t pte)
  640. {
  641. return (pte_val(pte) & _PAGE_RO) == 0;
  642. }
  643. static inline int pte_dirty(pte_t pte)
  644. {
  645. #ifdef CONFIG_PGSTE
  646. if (pte_val(pte) & _PAGE_SWC)
  647. return 1;
  648. #endif
  649. return 0;
  650. }
  651. static inline int pte_young(pte_t pte)
  652. {
  653. #ifdef CONFIG_PGSTE
  654. if (pte_val(pte) & _PAGE_SWR)
  655. return 1;
  656. #endif
  657. return 0;
  658. }
  659. /*
  660. * pgd/pmd/pte modification functions
  661. */
  662. static inline void pgd_clear(pgd_t *pgd)
  663. {
  664. #ifdef __s390x__
  665. if ((pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
  666. pgd_val(*pgd) = _REGION2_ENTRY_EMPTY;
  667. #endif
  668. }
  669. static inline void pud_clear(pud_t *pud)
  670. {
  671. #ifdef __s390x__
  672. if ((pud_val(*pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
  673. pud_val(*pud) = _REGION3_ENTRY_EMPTY;
  674. #endif
  675. }
  676. static inline void pmd_clear(pmd_t *pmdp)
  677. {
  678. pmd_val(*pmdp) = _SEGMENT_ENTRY_EMPTY;
  679. }
  680. static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  681. {
  682. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  683. }
  684. /*
  685. * The following pte modification functions only work if
  686. * pte_present() is true. Undefined behaviour if not..
  687. */
  688. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  689. {
  690. pte_val(pte) &= _PAGE_CHG_MASK;
  691. pte_val(pte) |= pgprot_val(newprot);
  692. return pte;
  693. }
  694. static inline pte_t pte_wrprotect(pte_t pte)
  695. {
  696. /* Do not clobber _PAGE_TYPE_NONE pages! */
  697. if (!(pte_val(pte) & _PAGE_INVALID))
  698. pte_val(pte) |= _PAGE_RO;
  699. return pte;
  700. }
  701. static inline pte_t pte_mkwrite(pte_t pte)
  702. {
  703. pte_val(pte) &= ~_PAGE_RO;
  704. return pte;
  705. }
  706. static inline pte_t pte_mkclean(pte_t pte)
  707. {
  708. #ifdef CONFIG_PGSTE
  709. pte_val(pte) &= ~_PAGE_SWC;
  710. #endif
  711. return pte;
  712. }
  713. static inline pte_t pte_mkdirty(pte_t pte)
  714. {
  715. return pte;
  716. }
  717. static inline pte_t pte_mkold(pte_t pte)
  718. {
  719. #ifdef CONFIG_PGSTE
  720. pte_val(pte) &= ~_PAGE_SWR;
  721. #endif
  722. return pte;
  723. }
  724. static inline pte_t pte_mkyoung(pte_t pte)
  725. {
  726. return pte;
  727. }
  728. static inline pte_t pte_mkspecial(pte_t pte)
  729. {
  730. pte_val(pte) |= _PAGE_SPECIAL;
  731. return pte;
  732. }
  733. #ifdef CONFIG_HUGETLB_PAGE
  734. static inline pte_t pte_mkhuge(pte_t pte)
  735. {
  736. /*
  737. * PROT_NONE needs to be remapped from the pte type to the ste type.
  738. * The HW invalid bit is also different for pte and ste. The pte
  739. * invalid bit happens to be the same as the ste _SEGMENT_ENTRY_LARGE
  740. * bit, so we don't have to clear it.
  741. */
  742. if (pte_val(pte) & _PAGE_INVALID) {
  743. if (pte_val(pte) & _PAGE_SWT)
  744. pte_val(pte) |= _HPAGE_TYPE_NONE;
  745. pte_val(pte) |= _SEGMENT_ENTRY_INV;
  746. }
  747. /*
  748. * Clear SW pte bits SWT and SWX, there are no SW bits in a segment
  749. * table entry.
  750. */
  751. pte_val(pte) &= ~(_PAGE_SWT | _PAGE_SWX);
  752. /*
  753. * Also set the change-override bit because we don't need dirty bit
  754. * tracking for hugetlbfs pages.
  755. */
  756. pte_val(pte) |= (_SEGMENT_ENTRY_LARGE | _SEGMENT_ENTRY_CO);
  757. return pte;
  758. }
  759. #endif
  760. /*
  761. * Get (and clear) the user dirty bit for a pte.
  762. */
  763. static inline int ptep_test_and_clear_user_dirty(struct mm_struct *mm,
  764. pte_t *ptep)
  765. {
  766. pgste_t pgste;
  767. int dirty = 0;
  768. if (mm_has_pgste(mm)) {
  769. pgste = pgste_get_lock(ptep);
  770. pgste = pgste_update_all(ptep, pgste);
  771. dirty = !!(pgste_val(pgste) & KVM_UC_BIT);
  772. pgste_val(pgste) &= ~KVM_UC_BIT;
  773. pgste_set_unlock(ptep, pgste);
  774. return dirty;
  775. }
  776. return dirty;
  777. }
  778. /*
  779. * Get (and clear) the user referenced bit for a pte.
  780. */
  781. static inline int ptep_test_and_clear_user_young(struct mm_struct *mm,
  782. pte_t *ptep)
  783. {
  784. pgste_t pgste;
  785. int young = 0;
  786. if (mm_has_pgste(mm)) {
  787. pgste = pgste_get_lock(ptep);
  788. pgste = pgste_update_young(ptep, pgste);
  789. young = !!(pgste_val(pgste) & KVM_UR_BIT);
  790. pgste_val(pgste) &= ~KVM_UR_BIT;
  791. pgste_set_unlock(ptep, pgste);
  792. }
  793. return young;
  794. }
  795. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  796. static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
  797. unsigned long addr, pte_t *ptep)
  798. {
  799. pgste_t pgste;
  800. pte_t pte;
  801. if (mm_has_pgste(vma->vm_mm)) {
  802. pgste = pgste_get_lock(ptep);
  803. pgste = pgste_update_young(ptep, pgste);
  804. pte = *ptep;
  805. *ptep = pte_mkold(pte);
  806. pgste_set_unlock(ptep, pgste);
  807. return pte_young(pte);
  808. }
  809. return 0;
  810. }
  811. #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  812. static inline int ptep_clear_flush_young(struct vm_area_struct *vma,
  813. unsigned long address, pte_t *ptep)
  814. {
  815. /* No need to flush TLB
  816. * On s390 reference bits are in storage key and never in TLB
  817. * With virtualization we handle the reference bit, without we
  818. * we can simply return */
  819. return ptep_test_and_clear_young(vma, address, ptep);
  820. }
  821. static inline void __ptep_ipte(unsigned long address, pte_t *ptep)
  822. {
  823. if (!(pte_val(*ptep) & _PAGE_INVALID)) {
  824. #ifndef __s390x__
  825. /* pto must point to the start of the segment table */
  826. pte_t *pto = (pte_t *) (((unsigned long) ptep) & 0x7ffffc00);
  827. #else
  828. /* ipte in zarch mode can do the math */
  829. pte_t *pto = ptep;
  830. #endif
  831. asm volatile(
  832. " ipte %2,%3"
  833. : "=m" (*ptep) : "m" (*ptep),
  834. "a" (pto), "a" (address));
  835. }
  836. }
  837. /*
  838. * This is hard to understand. ptep_get_and_clear and ptep_clear_flush
  839. * both clear the TLB for the unmapped pte. The reason is that
  840. * ptep_get_and_clear is used in common code (e.g. change_pte_range)
  841. * to modify an active pte. The sequence is
  842. * 1) ptep_get_and_clear
  843. * 2) set_pte_at
  844. * 3) flush_tlb_range
  845. * On s390 the tlb needs to get flushed with the modification of the pte
  846. * if the pte is active. The only way how this can be implemented is to
  847. * have ptep_get_and_clear do the tlb flush. In exchange flush_tlb_range
  848. * is a nop.
  849. */
  850. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  851. static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
  852. unsigned long address, pte_t *ptep)
  853. {
  854. pgste_t pgste;
  855. pte_t pte;
  856. mm->context.flush_mm = 1;
  857. if (mm_has_pgste(mm))
  858. pgste = pgste_get_lock(ptep);
  859. pte = *ptep;
  860. if (!mm_exclusive(mm))
  861. __ptep_ipte(address, ptep);
  862. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  863. if (mm_has_pgste(mm)) {
  864. pgste = pgste_update_all(&pte, pgste);
  865. pgste_set_unlock(ptep, pgste);
  866. }
  867. return pte;
  868. }
  869. #define __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
  870. static inline pte_t ptep_modify_prot_start(struct mm_struct *mm,
  871. unsigned long address,
  872. pte_t *ptep)
  873. {
  874. pte_t pte;
  875. mm->context.flush_mm = 1;
  876. if (mm_has_pgste(mm))
  877. pgste_get_lock(ptep);
  878. pte = *ptep;
  879. if (!mm_exclusive(mm))
  880. __ptep_ipte(address, ptep);
  881. return pte;
  882. }
  883. static inline void ptep_modify_prot_commit(struct mm_struct *mm,
  884. unsigned long address,
  885. pte_t *ptep, pte_t pte)
  886. {
  887. *ptep = pte;
  888. if (mm_has_pgste(mm))
  889. pgste_set_unlock(ptep, *(pgste_t *)(ptep + PTRS_PER_PTE));
  890. }
  891. #define __HAVE_ARCH_PTEP_CLEAR_FLUSH
  892. static inline pte_t ptep_clear_flush(struct vm_area_struct *vma,
  893. unsigned long address, pte_t *ptep)
  894. {
  895. pgste_t pgste;
  896. pte_t pte;
  897. if (mm_has_pgste(vma->vm_mm))
  898. pgste = pgste_get_lock(ptep);
  899. pte = *ptep;
  900. __ptep_ipte(address, ptep);
  901. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  902. if (mm_has_pgste(vma->vm_mm)) {
  903. pgste = pgste_update_all(&pte, pgste);
  904. pgste_set_unlock(ptep, pgste);
  905. }
  906. return pte;
  907. }
  908. /*
  909. * The batched pte unmap code uses ptep_get_and_clear_full to clear the
  910. * ptes. Here an optimization is possible. tlb_gather_mmu flushes all
  911. * tlbs of an mm if it can guarantee that the ptes of the mm_struct
  912. * cannot be accessed while the batched unmap is running. In this case
  913. * full==1 and a simple pte_clear is enough. See tlb.h.
  914. */
  915. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
  916. static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
  917. unsigned long address,
  918. pte_t *ptep, int full)
  919. {
  920. pgste_t pgste;
  921. pte_t pte;
  922. if (mm_has_pgste(mm))
  923. pgste = pgste_get_lock(ptep);
  924. pte = *ptep;
  925. if (!full)
  926. __ptep_ipte(address, ptep);
  927. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  928. if (mm_has_pgste(mm)) {
  929. pgste = pgste_update_all(&pte, pgste);
  930. pgste_set_unlock(ptep, pgste);
  931. }
  932. return pte;
  933. }
  934. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  935. static inline pte_t ptep_set_wrprotect(struct mm_struct *mm,
  936. unsigned long address, pte_t *ptep)
  937. {
  938. pgste_t pgste;
  939. pte_t pte = *ptep;
  940. if (pte_write(pte)) {
  941. mm->context.flush_mm = 1;
  942. if (mm_has_pgste(mm))
  943. pgste = pgste_get_lock(ptep);
  944. if (!mm_exclusive(mm))
  945. __ptep_ipte(address, ptep);
  946. *ptep = pte_wrprotect(pte);
  947. if (mm_has_pgste(mm))
  948. pgste_set_unlock(ptep, pgste);
  949. }
  950. return pte;
  951. }
  952. #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  953. static inline int ptep_set_access_flags(struct vm_area_struct *vma,
  954. unsigned long address, pte_t *ptep,
  955. pte_t entry, int dirty)
  956. {
  957. pgste_t pgste;
  958. if (pte_same(*ptep, entry))
  959. return 0;
  960. if (mm_has_pgste(vma->vm_mm))
  961. pgste = pgste_get_lock(ptep);
  962. __ptep_ipte(address, ptep);
  963. *ptep = entry;
  964. if (mm_has_pgste(vma->vm_mm))
  965. pgste_set_unlock(ptep, pgste);
  966. return 1;
  967. }
  968. /*
  969. * Conversion functions: convert a page and protection to a page entry,
  970. * and a page entry and page directory to the page they refer to.
  971. */
  972. static inline pte_t mk_pte_phys(unsigned long physpage, pgprot_t pgprot)
  973. {
  974. pte_t __pte;
  975. pte_val(__pte) = physpage + pgprot_val(pgprot);
  976. return __pte;
  977. }
  978. static inline pte_t mk_pte(struct page *page, pgprot_t pgprot)
  979. {
  980. unsigned long physpage = page_to_phys(page);
  981. return mk_pte_phys(physpage, pgprot);
  982. }
  983. #define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
  984. #define pud_index(address) (((address) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
  985. #define pmd_index(address) (((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
  986. #define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE-1))
  987. #define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
  988. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  989. #ifndef __s390x__
  990. #define pmd_deref(pmd) (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN)
  991. #define pud_deref(pmd) ({ BUG(); 0UL; })
  992. #define pgd_deref(pmd) ({ BUG(); 0UL; })
  993. #define pud_offset(pgd, address) ((pud_t *) pgd)
  994. #define pmd_offset(pud, address) ((pmd_t *) pud + pmd_index(address))
  995. #else /* __s390x__ */
  996. #define pmd_deref(pmd) (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN)
  997. #define pud_deref(pud) (pud_val(pud) & _REGION_ENTRY_ORIGIN)
  998. #define pgd_deref(pgd) (pgd_val(pgd) & _REGION_ENTRY_ORIGIN)
  999. static inline pud_t *pud_offset(pgd_t *pgd, unsigned long address)
  1000. {
  1001. pud_t *pud = (pud_t *) pgd;
  1002. if ((pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
  1003. pud = (pud_t *) pgd_deref(*pgd);
  1004. return pud + pud_index(address);
  1005. }
  1006. static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address)
  1007. {
  1008. pmd_t *pmd = (pmd_t *) pud;
  1009. if ((pud_val(*pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
  1010. pmd = (pmd_t *) pud_deref(*pud);
  1011. return pmd + pmd_index(address);
  1012. }
  1013. #endif /* __s390x__ */
  1014. #define pfn_pte(pfn,pgprot) mk_pte_phys(__pa((pfn) << PAGE_SHIFT),(pgprot))
  1015. #define pte_pfn(x) (pte_val(x) >> PAGE_SHIFT)
  1016. #define pte_page(x) pfn_to_page(pte_pfn(x))
  1017. #define pmd_page(pmd) pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT)
  1018. /* Find an entry in the lowest level page table.. */
  1019. #define pte_offset(pmd, addr) ((pte_t *) pmd_deref(*(pmd)) + pte_index(addr))
  1020. #define pte_offset_kernel(pmd, address) pte_offset(pmd,address)
  1021. #define pte_offset_map(pmd, address) pte_offset_kernel(pmd, address)
  1022. #define pte_unmap(pte) do { } while (0)
  1023. /*
  1024. * 31 bit swap entry format:
  1025. * A page-table entry has some bits we have to treat in a special way.
  1026. * Bits 0, 20 and bit 23 have to be zero, otherwise an specification
  1027. * exception will occur instead of a page translation exception. The
  1028. * specifiation exception has the bad habit not to store necessary
  1029. * information in the lowcore.
  1030. * Bit 21 and bit 22 are the page invalid bit and the page protection
  1031. * bit. We set both to indicate a swapped page.
  1032. * Bit 30 and 31 are used to distinguish the different page types. For
  1033. * a swapped page these bits need to be zero.
  1034. * This leaves the bits 1-19 and bits 24-29 to store type and offset.
  1035. * We use the 5 bits from 25-29 for the type and the 20 bits from 1-19
  1036. * plus 24 for the offset.
  1037. * 0| offset |0110|o|type |00|
  1038. * 0 0000000001111111111 2222 2 22222 33
  1039. * 0 1234567890123456789 0123 4 56789 01
  1040. *
  1041. * 64 bit swap entry format:
  1042. * A page-table entry has some bits we have to treat in a special way.
  1043. * Bits 52 and bit 55 have to be zero, otherwise an specification
  1044. * exception will occur instead of a page translation exception. The
  1045. * specifiation exception has the bad habit not to store necessary
  1046. * information in the lowcore.
  1047. * Bit 53 and bit 54 are the page invalid bit and the page protection
  1048. * bit. We set both to indicate a swapped page.
  1049. * Bit 62 and 63 are used to distinguish the different page types. For
  1050. * a swapped page these bits need to be zero.
  1051. * This leaves the bits 0-51 and bits 56-61 to store type and offset.
  1052. * We use the 5 bits from 57-61 for the type and the 53 bits from 0-51
  1053. * plus 56 for the offset.
  1054. * | offset |0110|o|type |00|
  1055. * 0000000000111111111122222222223333333333444444444455 5555 5 55566 66
  1056. * 0123456789012345678901234567890123456789012345678901 2345 6 78901 23
  1057. */
  1058. #ifndef __s390x__
  1059. #define __SWP_OFFSET_MASK (~0UL >> 12)
  1060. #else
  1061. #define __SWP_OFFSET_MASK (~0UL >> 11)
  1062. #endif
  1063. static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset)
  1064. {
  1065. pte_t pte;
  1066. offset &= __SWP_OFFSET_MASK;
  1067. pte_val(pte) = _PAGE_TYPE_SWAP | ((type & 0x1f) << 2) |
  1068. ((offset & 1UL) << 7) | ((offset & ~1UL) << 11);
  1069. return pte;
  1070. }
  1071. #define __swp_type(entry) (((entry).val >> 2) & 0x1f)
  1072. #define __swp_offset(entry) (((entry).val >> 11) | (((entry).val >> 7) & 1))
  1073. #define __swp_entry(type,offset) ((swp_entry_t) { pte_val(mk_swap_pte((type),(offset))) })
  1074. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  1075. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  1076. #ifndef __s390x__
  1077. # define PTE_FILE_MAX_BITS 26
  1078. #else /* __s390x__ */
  1079. # define PTE_FILE_MAX_BITS 59
  1080. #endif /* __s390x__ */
  1081. #define pte_to_pgoff(__pte) \
  1082. ((((__pte).pte >> 12) << 7) + (((__pte).pte >> 1) & 0x7f))
  1083. #define pgoff_to_pte(__off) \
  1084. ((pte_t) { ((((__off) & 0x7f) << 1) + (((__off) >> 7) << 12)) \
  1085. | _PAGE_TYPE_FILE })
  1086. #endif /* !__ASSEMBLY__ */
  1087. #define kern_addr_valid(addr) (1)
  1088. extern int vmem_add_mapping(unsigned long start, unsigned long size);
  1089. extern int vmem_remove_mapping(unsigned long start, unsigned long size);
  1090. extern int s390_enable_sie(void);
  1091. /*
  1092. * No page table caches to initialise
  1093. */
  1094. #define pgtable_cache_init() do { } while (0)
  1095. #include <asm-generic/pgtable.h>
  1096. #endif /* _S390_PAGE_H */