pgtable.h 37 KB

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