pgtable.h 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  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 <linux/page-flags.h>
  31. #include <asm/bug.h>
  32. #include <asm/page.h>
  33. extern pgd_t swapper_pg_dir[] __attribute__ ((aligned (4096)));
  34. extern void paging_init(void);
  35. extern void vmem_map_init(void);
  36. /*
  37. * The S390 doesn't have any external MMU info: the kernel page
  38. * tables contain all the necessary information.
  39. */
  40. #define update_mmu_cache(vma, address, ptep) do { } while (0)
  41. #define update_mmu_cache_pmd(vma, address, ptep) do { } while (0)
  42. /*
  43. * ZERO_PAGE is a global shared page that is always zero; used
  44. * for zero-mapped memory areas etc..
  45. */
  46. extern unsigned long empty_zero_page;
  47. extern unsigned long zero_page_mask;
  48. #define ZERO_PAGE(vaddr) \
  49. (virt_to_page((void *)(empty_zero_page + \
  50. (((unsigned long)(vaddr)) &zero_page_mask))))
  51. #define __HAVE_COLOR_ZERO_PAGE
  52. #endif /* !__ASSEMBLY__ */
  53. /*
  54. * PMD_SHIFT determines the size of the area a second-level page
  55. * table can map
  56. * PGDIR_SHIFT determines what a third-level page table entry can map
  57. */
  58. #ifndef CONFIG_64BIT
  59. # define PMD_SHIFT 20
  60. # define PUD_SHIFT 20
  61. # define PGDIR_SHIFT 20
  62. #else /* CONFIG_64BIT */
  63. # define PMD_SHIFT 20
  64. # define PUD_SHIFT 31
  65. # define PGDIR_SHIFT 42
  66. #endif /* CONFIG_64BIT */
  67. #define PMD_SIZE (1UL << PMD_SHIFT)
  68. #define PMD_MASK (~(PMD_SIZE-1))
  69. #define PUD_SIZE (1UL << PUD_SHIFT)
  70. #define PUD_MASK (~(PUD_SIZE-1))
  71. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  72. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  73. /*
  74. * entries per page directory level: the S390 is two-level, so
  75. * we don't really have any PMD directory physically.
  76. * for S390 segment-table entries are combined to one PGD
  77. * that leads to 1024 pte per pgd
  78. */
  79. #define PTRS_PER_PTE 256
  80. #ifndef CONFIG_64BIT
  81. #define PTRS_PER_PMD 1
  82. #define PTRS_PER_PUD 1
  83. #else /* CONFIG_64BIT */
  84. #define PTRS_PER_PMD 2048
  85. #define PTRS_PER_PUD 2048
  86. #endif /* CONFIG_64BIT */
  87. #define PTRS_PER_PGD 2048
  88. #define FIRST_USER_ADDRESS 0
  89. #define pte_ERROR(e) \
  90. printk("%s:%d: bad pte %p.\n", __FILE__, __LINE__, (void *) pte_val(e))
  91. #define pmd_ERROR(e) \
  92. printk("%s:%d: bad pmd %p.\n", __FILE__, __LINE__, (void *) pmd_val(e))
  93. #define pud_ERROR(e) \
  94. printk("%s:%d: bad pud %p.\n", __FILE__, __LINE__, (void *) pud_val(e))
  95. #define pgd_ERROR(e) \
  96. printk("%s:%d: bad pgd %p.\n", __FILE__, __LINE__, (void *) pgd_val(e))
  97. #ifndef __ASSEMBLY__
  98. /*
  99. * The vmalloc and module area will always be on the topmost area of the kernel
  100. * mapping. We reserve 96MB (31bit) / 128GB (64bit) for vmalloc and modules.
  101. * On 64 bit kernels we have a 2GB area at the top of the vmalloc area where
  102. * modules will reside. That makes sure that inter module branches always
  103. * happen without trampolines and in addition the placement within a 2GB frame
  104. * is branch prediction unit friendly.
  105. */
  106. extern unsigned long VMALLOC_START;
  107. extern unsigned long VMALLOC_END;
  108. extern struct page *vmemmap;
  109. #define VMEM_MAX_PHYS ((unsigned long) vmemmap)
  110. #ifdef CONFIG_64BIT
  111. extern unsigned long MODULES_VADDR;
  112. extern unsigned long MODULES_END;
  113. #define MODULES_VADDR MODULES_VADDR
  114. #define MODULES_END MODULES_END
  115. #define MODULES_LEN (1UL << 31)
  116. #endif
  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 */
  207. #define _PAGE_SWR 0x008 /* SW pte referenced bit */
  208. #define _PAGE_SWW 0x010 /* SW pte write bit */
  209. #define _PAGE_SPECIAL 0x020 /* SW associated with special page */
  210. #define __HAVE_ARCH_PTE_SPECIAL
  211. /* Set of bits not changed in pte_modify */
  212. #define _PAGE_CHG_MASK (PAGE_MASK | _PAGE_SPECIAL | _PAGE_CO | \
  213. _PAGE_SWC | _PAGE_SWR)
  214. /* Six different types of pages. */
  215. #define _PAGE_TYPE_EMPTY 0x400
  216. #define _PAGE_TYPE_NONE 0x401
  217. #define _PAGE_TYPE_SWAP 0x403
  218. #define _PAGE_TYPE_FILE 0x601 /* bit 0x002 is used for offset !! */
  219. #define _PAGE_TYPE_RO 0x200
  220. #define _PAGE_TYPE_RW 0x000
  221. /*
  222. * Only four types for huge pages, using the invalid bit and protection bit
  223. * of a segment table entry.
  224. */
  225. #define _HPAGE_TYPE_EMPTY 0x020 /* _SEGMENT_ENTRY_INV */
  226. #define _HPAGE_TYPE_NONE 0x220
  227. #define _HPAGE_TYPE_RO 0x200 /* _SEGMENT_ENTRY_RO */
  228. #define _HPAGE_TYPE_RW 0x000
  229. /*
  230. * PTE type bits are rather complicated. handle_pte_fault uses pte_present,
  231. * pte_none and pte_file to find out the pte type WITHOUT holding the page
  232. * table lock. ptep_clear_flush on the other hand uses ptep_clear_flush to
  233. * invalidate a given pte. ipte sets the hw invalid bit and clears all tlbs
  234. * for the page. The page table entry is set to _PAGE_TYPE_EMPTY afterwards.
  235. * This change is done while holding the lock, but the intermediate step
  236. * of a previously valid pte with the hw invalid bit set can be observed by
  237. * handle_pte_fault. That makes it necessary that all valid pte types with
  238. * the hw invalid bit set must be distinguishable from the four pte types
  239. * empty, none, swap and file.
  240. *
  241. * irxt ipte irxt
  242. * _PAGE_TYPE_EMPTY 1000 -> 1000
  243. * _PAGE_TYPE_NONE 1001 -> 1001
  244. * _PAGE_TYPE_SWAP 1011 -> 1011
  245. * _PAGE_TYPE_FILE 11?1 -> 11?1
  246. * _PAGE_TYPE_RO 0100 -> 1100
  247. * _PAGE_TYPE_RW 0000 -> 1000
  248. *
  249. * pte_none is true for bits combinations 1000, 1010, 1100, 1110
  250. * pte_present is true for bits combinations 0000, 0010, 0100, 0110, 1001
  251. * pte_file is true for bits combinations 1101, 1111
  252. * swap pte is 1011 and 0001, 0011, 0101, 0111 are invalid.
  253. */
  254. #ifndef CONFIG_64BIT
  255. /* Bits in the segment table address-space-control-element */
  256. #define _ASCE_SPACE_SWITCH 0x80000000UL /* space switch event */
  257. #define _ASCE_ORIGIN_MASK 0x7ffff000UL /* segment table origin */
  258. #define _ASCE_PRIVATE_SPACE 0x100 /* private space control */
  259. #define _ASCE_ALT_EVENT 0x80 /* storage alteration event control */
  260. #define _ASCE_TABLE_LENGTH 0x7f /* 128 x 64 entries = 8k */
  261. /* Bits in the segment table entry */
  262. #define _SEGMENT_ENTRY_ORIGIN 0x7fffffc0UL /* page table origin */
  263. #define _SEGMENT_ENTRY_RO 0x200 /* page protection bit */
  264. #define _SEGMENT_ENTRY_INV 0x20 /* invalid segment table entry */
  265. #define _SEGMENT_ENTRY_COMMON 0x10 /* common segment bit */
  266. #define _SEGMENT_ENTRY_PTL 0x0f /* page table length */
  267. #define _SEGMENT_ENTRY (_SEGMENT_ENTRY_PTL)
  268. #define _SEGMENT_ENTRY_EMPTY (_SEGMENT_ENTRY_INV)
  269. /* Page status table bits for virtualization */
  270. #define RCP_ACC_BITS 0xf0000000UL
  271. #define RCP_FP_BIT 0x08000000UL
  272. #define RCP_PCL_BIT 0x00800000UL
  273. #define RCP_HR_BIT 0x00400000UL
  274. #define RCP_HC_BIT 0x00200000UL
  275. #define RCP_GR_BIT 0x00040000UL
  276. #define RCP_GC_BIT 0x00020000UL
  277. /* User dirty / referenced bit for KVM's migration feature */
  278. #define KVM_UR_BIT 0x00008000UL
  279. #define KVM_UC_BIT 0x00004000UL
  280. #else /* CONFIG_64BIT */
  281. /* Bits in the segment/region table address-space-control-element */
  282. #define _ASCE_ORIGIN ~0xfffUL/* segment table origin */
  283. #define _ASCE_PRIVATE_SPACE 0x100 /* private space control */
  284. #define _ASCE_ALT_EVENT 0x80 /* storage alteration event control */
  285. #define _ASCE_SPACE_SWITCH 0x40 /* space switch event */
  286. #define _ASCE_REAL_SPACE 0x20 /* real space control */
  287. #define _ASCE_TYPE_MASK 0x0c /* asce table type mask */
  288. #define _ASCE_TYPE_REGION1 0x0c /* region first table type */
  289. #define _ASCE_TYPE_REGION2 0x08 /* region second table type */
  290. #define _ASCE_TYPE_REGION3 0x04 /* region third table type */
  291. #define _ASCE_TYPE_SEGMENT 0x00 /* segment table type */
  292. #define _ASCE_TABLE_LENGTH 0x03 /* region table length */
  293. /* Bits in the region table entry */
  294. #define _REGION_ENTRY_ORIGIN ~0xfffUL/* region/segment table origin */
  295. #define _REGION_ENTRY_RO 0x200 /* region protection bit */
  296. #define _REGION_ENTRY_INV 0x20 /* invalid region table entry */
  297. #define _REGION_ENTRY_TYPE_MASK 0x0c /* region/segment table type mask */
  298. #define _REGION_ENTRY_TYPE_R1 0x0c /* region first table type */
  299. #define _REGION_ENTRY_TYPE_R2 0x08 /* region second table type */
  300. #define _REGION_ENTRY_TYPE_R3 0x04 /* region third table type */
  301. #define _REGION_ENTRY_LENGTH 0x03 /* region third length */
  302. #define _REGION1_ENTRY (_REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_LENGTH)
  303. #define _REGION1_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INV)
  304. #define _REGION2_ENTRY (_REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_LENGTH)
  305. #define _REGION2_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INV)
  306. #define _REGION3_ENTRY (_REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_LENGTH)
  307. #define _REGION3_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INV)
  308. #define _REGION3_ENTRY_LARGE 0x400 /* RTTE-format control, large page */
  309. #define _REGION3_ENTRY_RO 0x200 /* page protection bit */
  310. #define _REGION3_ENTRY_CO 0x100 /* change-recording override */
  311. /* Bits in the segment table entry */
  312. #define _SEGMENT_ENTRY_ORIGIN_LARGE ~0xfffffUL /* large page address */
  313. #define _SEGMENT_ENTRY_ORIGIN ~0x7ffUL/* segment table origin */
  314. #define _SEGMENT_ENTRY_RO 0x200 /* page protection bit */
  315. #define _SEGMENT_ENTRY_INV 0x20 /* invalid segment table entry */
  316. #define _SEGMENT_ENTRY (0)
  317. #define _SEGMENT_ENTRY_EMPTY (_SEGMENT_ENTRY_INV)
  318. #define _SEGMENT_ENTRY_LARGE 0x400 /* STE-format control, large page */
  319. #define _SEGMENT_ENTRY_CO 0x100 /* change-recording override */
  320. #define _SEGMENT_ENTRY_SPLIT_BIT 0 /* THP splitting bit number */
  321. #define _SEGMENT_ENTRY_SPLIT (1UL << _SEGMENT_ENTRY_SPLIT_BIT)
  322. /* Set of bits not changed in pmd_modify */
  323. #define _SEGMENT_CHG_MASK (_SEGMENT_ENTRY_ORIGIN | _SEGMENT_ENTRY_LARGE \
  324. | _SEGMENT_ENTRY_SPLIT | _SEGMENT_ENTRY_CO)
  325. /* Page status table bits for virtualization */
  326. #define RCP_ACC_BITS 0xf000000000000000UL
  327. #define RCP_FP_BIT 0x0800000000000000UL
  328. #define RCP_PCL_BIT 0x0080000000000000UL
  329. #define RCP_HR_BIT 0x0040000000000000UL
  330. #define RCP_HC_BIT 0x0020000000000000UL
  331. #define RCP_GR_BIT 0x0004000000000000UL
  332. #define RCP_GC_BIT 0x0002000000000000UL
  333. /* User dirty / referenced bit for KVM's migration feature */
  334. #define KVM_UR_BIT 0x0000800000000000UL
  335. #define KVM_UC_BIT 0x0000400000000000UL
  336. #endif /* CONFIG_64BIT */
  337. /*
  338. * A user page table pointer has the space-switch-event bit, the
  339. * private-space-control bit and the storage-alteration-event-control
  340. * bit set. A kernel page table pointer doesn't need them.
  341. */
  342. #define _ASCE_USER_BITS (_ASCE_SPACE_SWITCH | _ASCE_PRIVATE_SPACE | \
  343. _ASCE_ALT_EVENT)
  344. /*
  345. * Page protection definitions.
  346. */
  347. #define PAGE_NONE __pgprot(_PAGE_TYPE_NONE)
  348. #define PAGE_RO __pgprot(_PAGE_TYPE_RO)
  349. #define PAGE_RW __pgprot(_PAGE_TYPE_RO | _PAGE_SWW)
  350. #define PAGE_RWC __pgprot(_PAGE_TYPE_RW | _PAGE_SWW | _PAGE_SWC)
  351. #define PAGE_KERNEL PAGE_RWC
  352. #define PAGE_SHARED PAGE_KERNEL
  353. #define PAGE_COPY PAGE_RO
  354. /*
  355. * On s390 the page table entry has an invalid bit and a read-only bit.
  356. * Read permission implies execute permission and write permission
  357. * implies read permission.
  358. */
  359. /*xwr*/
  360. #define __P000 PAGE_NONE
  361. #define __P001 PAGE_RO
  362. #define __P010 PAGE_RO
  363. #define __P011 PAGE_RO
  364. #define __P100 PAGE_RO
  365. #define __P101 PAGE_RO
  366. #define __P110 PAGE_RO
  367. #define __P111 PAGE_RO
  368. #define __S000 PAGE_NONE
  369. #define __S001 PAGE_RO
  370. #define __S010 PAGE_RW
  371. #define __S011 PAGE_RW
  372. #define __S100 PAGE_RO
  373. #define __S101 PAGE_RO
  374. #define __S110 PAGE_RW
  375. #define __S111 PAGE_RW
  376. static inline int mm_exclusive(struct mm_struct *mm)
  377. {
  378. return likely(mm == current->active_mm &&
  379. atomic_read(&mm->context.attach_count) <= 1);
  380. }
  381. static inline int mm_has_pgste(struct mm_struct *mm)
  382. {
  383. #ifdef CONFIG_PGSTE
  384. if (unlikely(mm->context.has_pgste))
  385. return 1;
  386. #endif
  387. return 0;
  388. }
  389. /*
  390. * pgd/pmd/pte query functions
  391. */
  392. #ifndef CONFIG_64BIT
  393. static inline int pgd_present(pgd_t pgd) { return 1; }
  394. static inline int pgd_none(pgd_t pgd) { return 0; }
  395. static inline int pgd_bad(pgd_t pgd) { return 0; }
  396. static inline int pud_present(pud_t pud) { return 1; }
  397. static inline int pud_none(pud_t pud) { return 0; }
  398. static inline int pud_large(pud_t pud) { return 0; }
  399. static inline int pud_bad(pud_t pud) { return 0; }
  400. #else /* CONFIG_64BIT */
  401. static inline int pgd_present(pgd_t pgd)
  402. {
  403. if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R2)
  404. return 1;
  405. return (pgd_val(pgd) & _REGION_ENTRY_ORIGIN) != 0UL;
  406. }
  407. static inline int pgd_none(pgd_t pgd)
  408. {
  409. if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R2)
  410. return 0;
  411. return (pgd_val(pgd) & _REGION_ENTRY_INV) != 0UL;
  412. }
  413. static inline int pgd_bad(pgd_t pgd)
  414. {
  415. /*
  416. * With dynamic page table levels the pgd can be a region table
  417. * entry or a segment table entry. Check for the bit that are
  418. * invalid for either table entry.
  419. */
  420. unsigned long mask =
  421. ~_SEGMENT_ENTRY_ORIGIN & ~_REGION_ENTRY_INV &
  422. ~_REGION_ENTRY_TYPE_MASK & ~_REGION_ENTRY_LENGTH;
  423. return (pgd_val(pgd) & mask) != 0;
  424. }
  425. static inline int pud_present(pud_t pud)
  426. {
  427. if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R3)
  428. return 1;
  429. return (pud_val(pud) & _REGION_ENTRY_ORIGIN) != 0UL;
  430. }
  431. static inline int pud_none(pud_t pud)
  432. {
  433. if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R3)
  434. return 0;
  435. return (pud_val(pud) & _REGION_ENTRY_INV) != 0UL;
  436. }
  437. static inline int pud_large(pud_t pud)
  438. {
  439. if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) != _REGION_ENTRY_TYPE_R3)
  440. return 0;
  441. return !!(pud_val(pud) & _REGION3_ENTRY_LARGE);
  442. }
  443. static inline int pud_bad(pud_t pud)
  444. {
  445. /*
  446. * With dynamic page table levels the pud can be a region table
  447. * entry or a segment table entry. Check for the bit that are
  448. * invalid for either table entry.
  449. */
  450. unsigned long mask =
  451. ~_SEGMENT_ENTRY_ORIGIN & ~_REGION_ENTRY_INV &
  452. ~_REGION_ENTRY_TYPE_MASK & ~_REGION_ENTRY_LENGTH;
  453. return (pud_val(pud) & mask) != 0;
  454. }
  455. #endif /* CONFIG_64BIT */
  456. static inline int pmd_present(pmd_t pmd)
  457. {
  458. unsigned long mask = _SEGMENT_ENTRY_INV | _SEGMENT_ENTRY_RO;
  459. return (pmd_val(pmd) & mask) == _HPAGE_TYPE_NONE ||
  460. !(pmd_val(pmd) & _SEGMENT_ENTRY_INV);
  461. }
  462. static inline int pmd_none(pmd_t pmd)
  463. {
  464. return (pmd_val(pmd) & _SEGMENT_ENTRY_INV) &&
  465. !(pmd_val(pmd) & _SEGMENT_ENTRY_RO);
  466. }
  467. static inline int pmd_large(pmd_t pmd)
  468. {
  469. #ifdef CONFIG_64BIT
  470. return !!(pmd_val(pmd) & _SEGMENT_ENTRY_LARGE);
  471. #else
  472. return 0;
  473. #endif
  474. }
  475. static inline int pmd_bad(pmd_t pmd)
  476. {
  477. unsigned long mask = ~_SEGMENT_ENTRY_ORIGIN & ~_SEGMENT_ENTRY_INV;
  478. return (pmd_val(pmd) & mask) != _SEGMENT_ENTRY;
  479. }
  480. #define __HAVE_ARCH_PMDP_SPLITTING_FLUSH
  481. extern void pmdp_splitting_flush(struct vm_area_struct *vma,
  482. unsigned long addr, pmd_t *pmdp);
  483. #define __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
  484. extern int pmdp_set_access_flags(struct vm_area_struct *vma,
  485. unsigned long address, pmd_t *pmdp,
  486. pmd_t entry, int dirty);
  487. #define __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
  488. extern int pmdp_clear_flush_young(struct vm_area_struct *vma,
  489. unsigned long address, pmd_t *pmdp);
  490. #define __HAVE_ARCH_PMD_WRITE
  491. static inline int pmd_write(pmd_t pmd)
  492. {
  493. return (pmd_val(pmd) & _SEGMENT_ENTRY_RO) == 0;
  494. }
  495. static inline int pmd_young(pmd_t pmd)
  496. {
  497. return 0;
  498. }
  499. static inline int pte_none(pte_t pte)
  500. {
  501. return (pte_val(pte) & _PAGE_INVALID) && !(pte_val(pte) & _PAGE_SWT);
  502. }
  503. static inline int pte_present(pte_t pte)
  504. {
  505. unsigned long mask = _PAGE_RO | _PAGE_INVALID | _PAGE_SWT | _PAGE_SWX;
  506. return (pte_val(pte) & mask) == _PAGE_TYPE_NONE ||
  507. (!(pte_val(pte) & _PAGE_INVALID) &&
  508. !(pte_val(pte) & _PAGE_SWT));
  509. }
  510. static inline int pte_file(pte_t pte)
  511. {
  512. unsigned long mask = _PAGE_RO | _PAGE_INVALID | _PAGE_SWT;
  513. return (pte_val(pte) & mask) == _PAGE_TYPE_FILE;
  514. }
  515. static inline int pte_special(pte_t pte)
  516. {
  517. return (pte_val(pte) & _PAGE_SPECIAL);
  518. }
  519. #define __HAVE_ARCH_PTE_SAME
  520. static inline int pte_same(pte_t a, pte_t b)
  521. {
  522. return pte_val(a) == pte_val(b);
  523. }
  524. static inline pgste_t pgste_get_lock(pte_t *ptep)
  525. {
  526. unsigned long new = 0;
  527. #ifdef CONFIG_PGSTE
  528. unsigned long old;
  529. preempt_disable();
  530. asm(
  531. " lg %0,%2\n"
  532. "0: lgr %1,%0\n"
  533. " nihh %0,0xff7f\n" /* clear RCP_PCL_BIT in old */
  534. " oihh %1,0x0080\n" /* set RCP_PCL_BIT in new */
  535. " csg %0,%1,%2\n"
  536. " jl 0b\n"
  537. : "=&d" (old), "=&d" (new), "=Q" (ptep[PTRS_PER_PTE])
  538. : "Q" (ptep[PTRS_PER_PTE]) : "cc");
  539. #endif
  540. return __pgste(new);
  541. }
  542. static inline void pgste_set_unlock(pte_t *ptep, pgste_t pgste)
  543. {
  544. #ifdef CONFIG_PGSTE
  545. asm(
  546. " nihh %1,0xff7f\n" /* clear RCP_PCL_BIT */
  547. " stg %1,%0\n"
  548. : "=Q" (ptep[PTRS_PER_PTE])
  549. : "d" (pgste_val(pgste)), "Q" (ptep[PTRS_PER_PTE]) : "cc");
  550. preempt_enable();
  551. #endif
  552. }
  553. static inline pgste_t pgste_update_all(pte_t *ptep, pgste_t pgste)
  554. {
  555. #ifdef CONFIG_PGSTE
  556. unsigned long address, bits;
  557. unsigned char skey;
  558. if (!pte_present(*ptep))
  559. return pgste;
  560. address = pte_val(*ptep) & PAGE_MASK;
  561. skey = page_get_storage_key(address);
  562. bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
  563. /* Clear page changed & referenced bit in the storage key */
  564. if (bits & _PAGE_CHANGED)
  565. page_set_storage_key(address, skey ^ bits, 0);
  566. else if (bits)
  567. page_reset_referenced(address);
  568. /* Transfer page changed & referenced bit to guest bits in pgste */
  569. pgste_val(pgste) |= bits << 48; /* RCP_GR_BIT & RCP_GC_BIT */
  570. /* Get host changed & referenced bits from pgste */
  571. bits |= (pgste_val(pgste) & (RCP_HR_BIT | RCP_HC_BIT)) >> 52;
  572. /* Transfer page changed & referenced bit to kvm user bits */
  573. pgste_val(pgste) |= bits << 45; /* KVM_UR_BIT & KVM_UC_BIT */
  574. /* Clear relevant host bits in pgste. */
  575. pgste_val(pgste) &= ~(RCP_HR_BIT | RCP_HC_BIT);
  576. pgste_val(pgste) &= ~(RCP_ACC_BITS | RCP_FP_BIT);
  577. /* Copy page access key and fetch protection bit to pgste */
  578. pgste_val(pgste) |=
  579. (unsigned long) (skey & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
  580. /* Transfer referenced bit to pte */
  581. pte_val(*ptep) |= (bits & _PAGE_REFERENCED) << 1;
  582. #endif
  583. return pgste;
  584. }
  585. static inline pgste_t pgste_update_young(pte_t *ptep, pgste_t pgste)
  586. {
  587. #ifdef CONFIG_PGSTE
  588. int young;
  589. if (!pte_present(*ptep))
  590. return pgste;
  591. /* Get referenced bit from storage key */
  592. young = page_reset_referenced(pte_val(*ptep) & PAGE_MASK);
  593. if (young)
  594. pgste_val(pgste) |= RCP_GR_BIT;
  595. /* Get host referenced bit from pgste */
  596. if (pgste_val(pgste) & RCP_HR_BIT) {
  597. pgste_val(pgste) &= ~RCP_HR_BIT;
  598. young = 1;
  599. }
  600. /* Transfer referenced bit to kvm user bits and pte */
  601. if (young) {
  602. pgste_val(pgste) |= KVM_UR_BIT;
  603. pte_val(*ptep) |= _PAGE_SWR;
  604. }
  605. #endif
  606. return pgste;
  607. }
  608. static inline void pgste_set_key(pte_t *ptep, pgste_t pgste, pte_t entry)
  609. {
  610. #ifdef CONFIG_PGSTE
  611. unsigned long address;
  612. unsigned long okey, nkey;
  613. if (!pte_present(entry))
  614. return;
  615. address = pte_val(entry) & PAGE_MASK;
  616. okey = nkey = page_get_storage_key(address);
  617. nkey &= ~(_PAGE_ACC_BITS | _PAGE_FP_BIT);
  618. /* Set page access key and fetch protection bit from pgste */
  619. nkey |= (pgste_val(pgste) & (RCP_ACC_BITS | RCP_FP_BIT)) >> 56;
  620. if (okey != nkey)
  621. page_set_storage_key(address, nkey, 0);
  622. #endif
  623. }
  624. static inline void pgste_set_pte(pte_t *ptep, pte_t entry)
  625. {
  626. if (!MACHINE_HAS_ESOP && (pte_val(entry) & _PAGE_SWW)) {
  627. /*
  628. * Without enhanced suppression-on-protection force
  629. * the dirty bit on for all writable ptes.
  630. */
  631. pte_val(entry) |= _PAGE_SWC;
  632. pte_val(entry) &= ~_PAGE_RO;
  633. }
  634. *ptep = entry;
  635. }
  636. /**
  637. * struct gmap_struct - guest address space
  638. * @mm: pointer to the parent mm_struct
  639. * @table: pointer to the page directory
  640. * @asce: address space control element for gmap page table
  641. * @crst_list: list of all crst tables used in the guest address space
  642. */
  643. struct gmap {
  644. struct list_head list;
  645. struct mm_struct *mm;
  646. unsigned long *table;
  647. unsigned long asce;
  648. struct list_head crst_list;
  649. };
  650. /**
  651. * struct gmap_rmap - reverse mapping for segment table entries
  652. * @next: pointer to the next gmap_rmap structure in the list
  653. * @entry: pointer to a segment table entry
  654. */
  655. struct gmap_rmap {
  656. struct list_head list;
  657. unsigned long *entry;
  658. };
  659. /**
  660. * struct gmap_pgtable - gmap information attached to a page table
  661. * @vmaddr: address of the 1MB segment in the process virtual memory
  662. * @mapper: list of segment table entries maping a page table
  663. */
  664. struct gmap_pgtable {
  665. unsigned long vmaddr;
  666. struct list_head mapper;
  667. };
  668. struct gmap *gmap_alloc(struct mm_struct *mm);
  669. void gmap_free(struct gmap *gmap);
  670. void gmap_enable(struct gmap *gmap);
  671. void gmap_disable(struct gmap *gmap);
  672. int gmap_map_segment(struct gmap *gmap, unsigned long from,
  673. unsigned long to, unsigned long length);
  674. int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len);
  675. unsigned long __gmap_fault(unsigned long address, struct gmap *);
  676. unsigned long gmap_fault(unsigned long address, struct gmap *);
  677. void gmap_discard(unsigned long from, unsigned long to, struct gmap *);
  678. /*
  679. * Certain architectures need to do special things when PTEs
  680. * within a page table are directly modified. Thus, the following
  681. * hook is made available.
  682. */
  683. static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
  684. pte_t *ptep, pte_t entry)
  685. {
  686. pgste_t pgste;
  687. if (mm_has_pgste(mm)) {
  688. pgste = pgste_get_lock(ptep);
  689. pgste_set_key(ptep, pgste, entry);
  690. pgste_set_pte(ptep, entry);
  691. pgste_set_unlock(ptep, pgste);
  692. } else {
  693. if (!(pte_val(entry) & _PAGE_INVALID) && MACHINE_HAS_EDAT1)
  694. pte_val(entry) |= _PAGE_CO;
  695. *ptep = entry;
  696. }
  697. }
  698. /*
  699. * query functions pte_write/pte_dirty/pte_young only work if
  700. * pte_present() is true. Undefined behaviour if not..
  701. */
  702. static inline int pte_write(pte_t pte)
  703. {
  704. return (pte_val(pte) & _PAGE_SWW) != 0;
  705. }
  706. static inline int pte_dirty(pte_t pte)
  707. {
  708. return (pte_val(pte) & _PAGE_SWC) != 0;
  709. }
  710. static inline int pte_young(pte_t pte)
  711. {
  712. #ifdef CONFIG_PGSTE
  713. if (pte_val(pte) & _PAGE_SWR)
  714. return 1;
  715. #endif
  716. return 0;
  717. }
  718. /*
  719. * pgd/pmd/pte modification functions
  720. */
  721. static inline void pgd_clear(pgd_t *pgd)
  722. {
  723. #ifdef CONFIG_64BIT
  724. if ((pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
  725. pgd_val(*pgd) = _REGION2_ENTRY_EMPTY;
  726. #endif
  727. }
  728. static inline void pud_clear(pud_t *pud)
  729. {
  730. #ifdef CONFIG_64BIT
  731. if ((pud_val(*pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
  732. pud_val(*pud) = _REGION3_ENTRY_EMPTY;
  733. #endif
  734. }
  735. static inline void pmd_clear(pmd_t *pmdp)
  736. {
  737. pmd_val(*pmdp) = _SEGMENT_ENTRY_EMPTY;
  738. }
  739. static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  740. {
  741. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  742. }
  743. /*
  744. * The following pte modification functions only work if
  745. * pte_present() is true. Undefined behaviour if not..
  746. */
  747. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  748. {
  749. pte_val(pte) &= _PAGE_CHG_MASK;
  750. pte_val(pte) |= pgprot_val(newprot);
  751. if ((pte_val(pte) & _PAGE_SWC) && (pte_val(pte) & _PAGE_SWW))
  752. pte_val(pte) &= ~_PAGE_RO;
  753. return pte;
  754. }
  755. static inline pte_t pte_wrprotect(pte_t pte)
  756. {
  757. pte_val(pte) &= ~_PAGE_SWW;
  758. /* Do not clobber _PAGE_TYPE_NONE pages! */
  759. if (!(pte_val(pte) & _PAGE_INVALID))
  760. pte_val(pte) |= _PAGE_RO;
  761. return pte;
  762. }
  763. static inline pte_t pte_mkwrite(pte_t pte)
  764. {
  765. pte_val(pte) |= _PAGE_SWW;
  766. if (pte_val(pte) & _PAGE_SWC)
  767. pte_val(pte) &= ~_PAGE_RO;
  768. return pte;
  769. }
  770. static inline pte_t pte_mkclean(pte_t pte)
  771. {
  772. pte_val(pte) &= ~_PAGE_SWC;
  773. /* Do not clobber _PAGE_TYPE_NONE pages! */
  774. if (!(pte_val(pte) & _PAGE_INVALID))
  775. pte_val(pte) |= _PAGE_RO;
  776. return pte;
  777. }
  778. static inline pte_t pte_mkdirty(pte_t pte)
  779. {
  780. pte_val(pte) |= _PAGE_SWC;
  781. if (pte_val(pte) & _PAGE_SWW)
  782. pte_val(pte) &= ~_PAGE_RO;
  783. return pte;
  784. }
  785. static inline pte_t pte_mkold(pte_t pte)
  786. {
  787. #ifdef CONFIG_PGSTE
  788. pte_val(pte) &= ~_PAGE_SWR;
  789. #endif
  790. return pte;
  791. }
  792. static inline pte_t pte_mkyoung(pte_t pte)
  793. {
  794. return pte;
  795. }
  796. static inline pte_t pte_mkspecial(pte_t pte)
  797. {
  798. pte_val(pte) |= _PAGE_SPECIAL;
  799. return pte;
  800. }
  801. #ifdef CONFIG_HUGETLB_PAGE
  802. static inline pte_t pte_mkhuge(pte_t pte)
  803. {
  804. /*
  805. * PROT_NONE needs to be remapped from the pte type to the ste type.
  806. * The HW invalid bit is also different for pte and ste. The pte
  807. * invalid bit happens to be the same as the ste _SEGMENT_ENTRY_LARGE
  808. * bit, so we don't have to clear it.
  809. */
  810. if (pte_val(pte) & _PAGE_INVALID) {
  811. if (pte_val(pte) & _PAGE_SWT)
  812. pte_val(pte) |= _HPAGE_TYPE_NONE;
  813. pte_val(pte) |= _SEGMENT_ENTRY_INV;
  814. }
  815. /*
  816. * Clear SW pte bits, there are no SW bits in a segment table entry.
  817. */
  818. pte_val(pte) &= ~(_PAGE_SWT | _PAGE_SWX | _PAGE_SWC |
  819. _PAGE_SWR | _PAGE_SWW);
  820. /*
  821. * Also set the change-override bit because we don't need dirty bit
  822. * tracking for hugetlbfs pages.
  823. */
  824. pte_val(pte) |= (_SEGMENT_ENTRY_LARGE | _SEGMENT_ENTRY_CO);
  825. return pte;
  826. }
  827. #endif
  828. /*
  829. * Get (and clear) the user dirty bit for a pte.
  830. */
  831. static inline int ptep_test_and_clear_user_dirty(struct mm_struct *mm,
  832. pte_t *ptep)
  833. {
  834. pgste_t pgste;
  835. int dirty = 0;
  836. if (mm_has_pgste(mm)) {
  837. pgste = pgste_get_lock(ptep);
  838. pgste = pgste_update_all(ptep, pgste);
  839. dirty = !!(pgste_val(pgste) & KVM_UC_BIT);
  840. pgste_val(pgste) &= ~KVM_UC_BIT;
  841. pgste_set_unlock(ptep, pgste);
  842. return dirty;
  843. }
  844. return dirty;
  845. }
  846. /*
  847. * Get (and clear) the user referenced bit for a pte.
  848. */
  849. static inline int ptep_test_and_clear_user_young(struct mm_struct *mm,
  850. pte_t *ptep)
  851. {
  852. pgste_t pgste;
  853. int young = 0;
  854. if (mm_has_pgste(mm)) {
  855. pgste = pgste_get_lock(ptep);
  856. pgste = pgste_update_young(ptep, pgste);
  857. young = !!(pgste_val(pgste) & KVM_UR_BIT);
  858. pgste_val(pgste) &= ~KVM_UR_BIT;
  859. pgste_set_unlock(ptep, pgste);
  860. }
  861. return young;
  862. }
  863. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  864. static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
  865. unsigned long addr, pte_t *ptep)
  866. {
  867. pgste_t pgste;
  868. pte_t pte;
  869. if (mm_has_pgste(vma->vm_mm)) {
  870. pgste = pgste_get_lock(ptep);
  871. pgste = pgste_update_young(ptep, pgste);
  872. pte = *ptep;
  873. *ptep = pte_mkold(pte);
  874. pgste_set_unlock(ptep, pgste);
  875. return pte_young(pte);
  876. }
  877. return 0;
  878. }
  879. #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  880. static inline int ptep_clear_flush_young(struct vm_area_struct *vma,
  881. unsigned long address, pte_t *ptep)
  882. {
  883. /* No need to flush TLB
  884. * On s390 reference bits are in storage key and never in TLB
  885. * With virtualization we handle the reference bit, without we
  886. * we can simply return */
  887. return ptep_test_and_clear_young(vma, address, ptep);
  888. }
  889. static inline void __ptep_ipte(unsigned long address, pte_t *ptep)
  890. {
  891. if (!(pte_val(*ptep) & _PAGE_INVALID)) {
  892. #ifndef CONFIG_64BIT
  893. /* pto must point to the start of the segment table */
  894. pte_t *pto = (pte_t *) (((unsigned long) ptep) & 0x7ffffc00);
  895. #else
  896. /* ipte in zarch mode can do the math */
  897. pte_t *pto = ptep;
  898. #endif
  899. asm volatile(
  900. " ipte %2,%3"
  901. : "=m" (*ptep) : "m" (*ptep),
  902. "a" (pto), "a" (address));
  903. }
  904. }
  905. /*
  906. * This is hard to understand. ptep_get_and_clear and ptep_clear_flush
  907. * both clear the TLB for the unmapped pte. The reason is that
  908. * ptep_get_and_clear is used in common code (e.g. change_pte_range)
  909. * to modify an active pte. The sequence is
  910. * 1) ptep_get_and_clear
  911. * 2) set_pte_at
  912. * 3) flush_tlb_range
  913. * On s390 the tlb needs to get flushed with the modification of the pte
  914. * if the pte is active. The only way how this can be implemented is to
  915. * have ptep_get_and_clear do the tlb flush. In exchange flush_tlb_range
  916. * is a nop.
  917. */
  918. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  919. static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
  920. unsigned long address, pte_t *ptep)
  921. {
  922. pgste_t pgste;
  923. pte_t pte;
  924. mm->context.flush_mm = 1;
  925. if (mm_has_pgste(mm))
  926. pgste = pgste_get_lock(ptep);
  927. pte = *ptep;
  928. if (!mm_exclusive(mm))
  929. __ptep_ipte(address, ptep);
  930. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  931. if (mm_has_pgste(mm)) {
  932. pgste = pgste_update_all(&pte, pgste);
  933. pgste_set_unlock(ptep, pgste);
  934. }
  935. return pte;
  936. }
  937. #define __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
  938. static inline pte_t ptep_modify_prot_start(struct mm_struct *mm,
  939. unsigned long address,
  940. pte_t *ptep)
  941. {
  942. pte_t pte;
  943. mm->context.flush_mm = 1;
  944. if (mm_has_pgste(mm))
  945. pgste_get_lock(ptep);
  946. pte = *ptep;
  947. if (!mm_exclusive(mm))
  948. __ptep_ipte(address, ptep);
  949. return pte;
  950. }
  951. static inline void ptep_modify_prot_commit(struct mm_struct *mm,
  952. unsigned long address,
  953. pte_t *ptep, pte_t pte)
  954. {
  955. if (mm_has_pgste(mm)) {
  956. pgste_set_pte(ptep, pte);
  957. pgste_set_unlock(ptep, *(pgste_t *)(ptep + PTRS_PER_PTE));
  958. } else
  959. *ptep = pte;
  960. }
  961. #define __HAVE_ARCH_PTEP_CLEAR_FLUSH
  962. static inline pte_t ptep_clear_flush(struct vm_area_struct *vma,
  963. unsigned long address, pte_t *ptep)
  964. {
  965. pgste_t pgste;
  966. pte_t pte;
  967. if (mm_has_pgste(vma->vm_mm))
  968. pgste = pgste_get_lock(ptep);
  969. pte = *ptep;
  970. __ptep_ipte(address, ptep);
  971. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  972. if (mm_has_pgste(vma->vm_mm)) {
  973. pgste = pgste_update_all(&pte, pgste);
  974. pgste_set_unlock(ptep, pgste);
  975. }
  976. return pte;
  977. }
  978. /*
  979. * The batched pte unmap code uses ptep_get_and_clear_full to clear the
  980. * ptes. Here an optimization is possible. tlb_gather_mmu flushes all
  981. * tlbs of an mm if it can guarantee that the ptes of the mm_struct
  982. * cannot be accessed while the batched unmap is running. In this case
  983. * full==1 and a simple pte_clear is enough. See tlb.h.
  984. */
  985. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
  986. static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
  987. unsigned long address,
  988. pte_t *ptep, int full)
  989. {
  990. pgste_t pgste;
  991. pte_t pte;
  992. if (mm_has_pgste(mm))
  993. pgste = pgste_get_lock(ptep);
  994. pte = *ptep;
  995. if (!full)
  996. __ptep_ipte(address, ptep);
  997. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  998. if (mm_has_pgste(mm)) {
  999. pgste = pgste_update_all(&pte, pgste);
  1000. pgste_set_unlock(ptep, pgste);
  1001. }
  1002. return pte;
  1003. }
  1004. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  1005. static inline pte_t ptep_set_wrprotect(struct mm_struct *mm,
  1006. unsigned long address, pte_t *ptep)
  1007. {
  1008. pgste_t pgste;
  1009. pte_t pte = *ptep;
  1010. if (pte_write(pte)) {
  1011. mm->context.flush_mm = 1;
  1012. if (mm_has_pgste(mm))
  1013. pgste = pgste_get_lock(ptep);
  1014. if (!mm_exclusive(mm))
  1015. __ptep_ipte(address, ptep);
  1016. pte = pte_wrprotect(pte);
  1017. if (mm_has_pgste(mm)) {
  1018. pgste_set_pte(ptep, pte);
  1019. pgste_set_unlock(ptep, pgste);
  1020. } else
  1021. *ptep = pte;
  1022. }
  1023. return pte;
  1024. }
  1025. #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  1026. static inline int ptep_set_access_flags(struct vm_area_struct *vma,
  1027. unsigned long address, pte_t *ptep,
  1028. pte_t entry, int dirty)
  1029. {
  1030. pgste_t pgste;
  1031. if (pte_same(*ptep, entry))
  1032. return 0;
  1033. if (mm_has_pgste(vma->vm_mm))
  1034. pgste = pgste_get_lock(ptep);
  1035. __ptep_ipte(address, ptep);
  1036. if (mm_has_pgste(vma->vm_mm)) {
  1037. pgste_set_pte(ptep, entry);
  1038. pgste_set_unlock(ptep, pgste);
  1039. } else
  1040. *ptep = entry;
  1041. return 1;
  1042. }
  1043. /*
  1044. * Conversion functions: convert a page and protection to a page entry,
  1045. * and a page entry and page directory to the page they refer to.
  1046. */
  1047. static inline pte_t mk_pte_phys(unsigned long physpage, pgprot_t pgprot)
  1048. {
  1049. pte_t __pte;
  1050. pte_val(__pte) = physpage + pgprot_val(pgprot);
  1051. return __pte;
  1052. }
  1053. static inline pte_t mk_pte(struct page *page, pgprot_t pgprot)
  1054. {
  1055. unsigned long physpage = page_to_phys(page);
  1056. pte_t __pte = mk_pte_phys(physpage, pgprot);
  1057. if ((pte_val(__pte) & _PAGE_SWW) && PageDirty(page)) {
  1058. pte_val(__pte) |= _PAGE_SWC;
  1059. pte_val(__pte) &= ~_PAGE_RO;
  1060. }
  1061. return __pte;
  1062. }
  1063. #define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
  1064. #define pud_index(address) (((address) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
  1065. #define pmd_index(address) (((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
  1066. #define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE-1))
  1067. #define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
  1068. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  1069. #ifndef CONFIG_64BIT
  1070. #define pmd_deref(pmd) (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN)
  1071. #define pud_deref(pmd) ({ BUG(); 0UL; })
  1072. #define pgd_deref(pmd) ({ BUG(); 0UL; })
  1073. #define pud_offset(pgd, address) ((pud_t *) pgd)
  1074. #define pmd_offset(pud, address) ((pmd_t *) pud + pmd_index(address))
  1075. #else /* CONFIG_64BIT */
  1076. #define pmd_deref(pmd) (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN)
  1077. #define pud_deref(pud) (pud_val(pud) & _REGION_ENTRY_ORIGIN)
  1078. #define pgd_deref(pgd) (pgd_val(pgd) & _REGION_ENTRY_ORIGIN)
  1079. static inline pud_t *pud_offset(pgd_t *pgd, unsigned long address)
  1080. {
  1081. pud_t *pud = (pud_t *) pgd;
  1082. if ((pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
  1083. pud = (pud_t *) pgd_deref(*pgd);
  1084. return pud + pud_index(address);
  1085. }
  1086. static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address)
  1087. {
  1088. pmd_t *pmd = (pmd_t *) pud;
  1089. if ((pud_val(*pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
  1090. pmd = (pmd_t *) pud_deref(*pud);
  1091. return pmd + pmd_index(address);
  1092. }
  1093. #endif /* CONFIG_64BIT */
  1094. #define pfn_pte(pfn,pgprot) mk_pte_phys(__pa((pfn) << PAGE_SHIFT),(pgprot))
  1095. #define pte_pfn(x) (pte_val(x) >> PAGE_SHIFT)
  1096. #define pte_page(x) pfn_to_page(pte_pfn(x))
  1097. #define pmd_page(pmd) pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT)
  1098. /* Find an entry in the lowest level page table.. */
  1099. #define pte_offset(pmd, addr) ((pte_t *) pmd_deref(*(pmd)) + pte_index(addr))
  1100. #define pte_offset_kernel(pmd, address) pte_offset(pmd,address)
  1101. #define pte_offset_map(pmd, address) pte_offset_kernel(pmd, address)
  1102. #define pte_unmap(pte) do { } while (0)
  1103. static inline void __pmd_idte(unsigned long address, pmd_t *pmdp)
  1104. {
  1105. unsigned long sto = (unsigned long) pmdp -
  1106. pmd_index(address) * sizeof(pmd_t);
  1107. if (!(pmd_val(*pmdp) & _SEGMENT_ENTRY_INV)) {
  1108. asm volatile(
  1109. " .insn rrf,0xb98e0000,%2,%3,0,0"
  1110. : "=m" (*pmdp)
  1111. : "m" (*pmdp), "a" (sto),
  1112. "a" ((address & HPAGE_MASK))
  1113. : "cc"
  1114. );
  1115. }
  1116. }
  1117. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1118. #define SEGMENT_NONE __pgprot(_HPAGE_TYPE_NONE)
  1119. #define SEGMENT_RO __pgprot(_HPAGE_TYPE_RO)
  1120. #define SEGMENT_RW __pgprot(_HPAGE_TYPE_RW)
  1121. #define __HAVE_ARCH_PGTABLE_DEPOSIT
  1122. extern void pgtable_trans_huge_deposit(struct mm_struct *mm, pgtable_t pgtable);
  1123. #define __HAVE_ARCH_PGTABLE_WITHDRAW
  1124. extern pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm);
  1125. static inline int pmd_trans_splitting(pmd_t pmd)
  1126. {
  1127. return pmd_val(pmd) & _SEGMENT_ENTRY_SPLIT;
  1128. }
  1129. static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
  1130. pmd_t *pmdp, pmd_t entry)
  1131. {
  1132. if (!(pmd_val(entry) & _SEGMENT_ENTRY_INV) && MACHINE_HAS_EDAT1)
  1133. pmd_val(entry) |= _SEGMENT_ENTRY_CO;
  1134. *pmdp = entry;
  1135. }
  1136. static inline unsigned long massage_pgprot_pmd(pgprot_t pgprot)
  1137. {
  1138. /*
  1139. * pgprot is PAGE_NONE, PAGE_RO, or PAGE_RW (see __Pxxx / __Sxxx)
  1140. * Convert to segment table entry format.
  1141. */
  1142. if (pgprot_val(pgprot) == pgprot_val(PAGE_NONE))
  1143. return pgprot_val(SEGMENT_NONE);
  1144. if (pgprot_val(pgprot) == pgprot_val(PAGE_RO))
  1145. return pgprot_val(SEGMENT_RO);
  1146. return pgprot_val(SEGMENT_RW);
  1147. }
  1148. static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
  1149. {
  1150. pmd_val(pmd) &= _SEGMENT_CHG_MASK;
  1151. pmd_val(pmd) |= massage_pgprot_pmd(newprot);
  1152. return pmd;
  1153. }
  1154. static inline pmd_t pmd_mkhuge(pmd_t pmd)
  1155. {
  1156. pmd_val(pmd) |= _SEGMENT_ENTRY_LARGE;
  1157. return pmd;
  1158. }
  1159. static inline pmd_t pmd_mkwrite(pmd_t pmd)
  1160. {
  1161. /* Do not clobber _HPAGE_TYPE_NONE pages! */
  1162. if (!(pmd_val(pmd) & _SEGMENT_ENTRY_INV))
  1163. pmd_val(pmd) &= ~_SEGMENT_ENTRY_RO;
  1164. return pmd;
  1165. }
  1166. static inline pmd_t pmd_wrprotect(pmd_t pmd)
  1167. {
  1168. pmd_val(pmd) |= _SEGMENT_ENTRY_RO;
  1169. return pmd;
  1170. }
  1171. static inline pmd_t pmd_mkdirty(pmd_t pmd)
  1172. {
  1173. /* No dirty bit in the segment table entry. */
  1174. return pmd;
  1175. }
  1176. static inline pmd_t pmd_mkold(pmd_t pmd)
  1177. {
  1178. /* No referenced bit in the segment table entry. */
  1179. return pmd;
  1180. }
  1181. static inline pmd_t pmd_mkyoung(pmd_t pmd)
  1182. {
  1183. /* No referenced bit in the segment table entry. */
  1184. return pmd;
  1185. }
  1186. #define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
  1187. static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
  1188. unsigned long address, pmd_t *pmdp)
  1189. {
  1190. unsigned long pmd_addr = pmd_val(*pmdp) & HPAGE_MASK;
  1191. long tmp, rc;
  1192. int counter;
  1193. rc = 0;
  1194. if (MACHINE_HAS_RRBM) {
  1195. counter = PTRS_PER_PTE >> 6;
  1196. asm volatile(
  1197. "0: .insn rre,0xb9ae0000,%0,%3\n" /* rrbm */
  1198. " ogr %1,%0\n"
  1199. " la %3,0(%4,%3)\n"
  1200. " brct %2,0b\n"
  1201. : "=&d" (tmp), "+&d" (rc), "+d" (counter),
  1202. "+a" (pmd_addr)
  1203. : "a" (64 * 4096UL) : "cc");
  1204. rc = !!rc;
  1205. } else {
  1206. counter = PTRS_PER_PTE;
  1207. asm volatile(
  1208. "0: rrbe 0,%2\n"
  1209. " la %2,0(%3,%2)\n"
  1210. " brc 12,1f\n"
  1211. " lhi %0,1\n"
  1212. "1: brct %1,0b\n"
  1213. : "+d" (rc), "+d" (counter), "+a" (pmd_addr)
  1214. : "a" (4096UL) : "cc");
  1215. }
  1216. return rc;
  1217. }
  1218. #define __HAVE_ARCH_PMDP_GET_AND_CLEAR
  1219. static inline pmd_t pmdp_get_and_clear(struct mm_struct *mm,
  1220. unsigned long address, pmd_t *pmdp)
  1221. {
  1222. pmd_t pmd = *pmdp;
  1223. __pmd_idte(address, pmdp);
  1224. pmd_clear(pmdp);
  1225. return pmd;
  1226. }
  1227. #define __HAVE_ARCH_PMDP_CLEAR_FLUSH
  1228. static inline pmd_t pmdp_clear_flush(struct vm_area_struct *vma,
  1229. unsigned long address, pmd_t *pmdp)
  1230. {
  1231. return pmdp_get_and_clear(vma->vm_mm, address, pmdp);
  1232. }
  1233. #define __HAVE_ARCH_PMDP_INVALIDATE
  1234. static inline void pmdp_invalidate(struct vm_area_struct *vma,
  1235. unsigned long address, pmd_t *pmdp)
  1236. {
  1237. __pmd_idte(address, pmdp);
  1238. }
  1239. #define __HAVE_ARCH_PMDP_SET_WRPROTECT
  1240. static inline void pmdp_set_wrprotect(struct mm_struct *mm,
  1241. unsigned long address, pmd_t *pmdp)
  1242. {
  1243. pmd_t pmd = *pmdp;
  1244. if (pmd_write(pmd)) {
  1245. __pmd_idte(address, pmdp);
  1246. set_pmd_at(mm, address, pmdp, pmd_wrprotect(pmd));
  1247. }
  1248. }
  1249. static inline pmd_t mk_pmd_phys(unsigned long physpage, pgprot_t pgprot)
  1250. {
  1251. pmd_t __pmd;
  1252. pmd_val(__pmd) = physpage + massage_pgprot_pmd(pgprot);
  1253. return __pmd;
  1254. }
  1255. #define pfn_pmd(pfn, pgprot) mk_pmd_phys(__pa((pfn) << PAGE_SHIFT), (pgprot))
  1256. #define mk_pmd(page, pgprot) pfn_pmd(page_to_pfn(page), (pgprot))
  1257. static inline int pmd_trans_huge(pmd_t pmd)
  1258. {
  1259. return pmd_val(pmd) & _SEGMENT_ENTRY_LARGE;
  1260. }
  1261. static inline int has_transparent_hugepage(void)
  1262. {
  1263. return MACHINE_HAS_HPAGE ? 1 : 0;
  1264. }
  1265. static inline unsigned long pmd_pfn(pmd_t pmd)
  1266. {
  1267. return pmd_val(pmd) >> PAGE_SHIFT;
  1268. }
  1269. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  1270. /*
  1271. * 31 bit swap entry format:
  1272. * A page-table entry has some bits we have to treat in a special way.
  1273. * Bits 0, 20 and bit 23 have to be zero, otherwise an specification
  1274. * exception will occur instead of a page translation exception. The
  1275. * specifiation exception has the bad habit not to store necessary
  1276. * information in the lowcore.
  1277. * Bit 21 and bit 22 are the page invalid bit and the page protection
  1278. * bit. We set both to indicate a swapped page.
  1279. * Bit 30 and 31 are used to distinguish the different page types. For
  1280. * a swapped page these bits need to be zero.
  1281. * This leaves the bits 1-19 and bits 24-29 to store type and offset.
  1282. * We use the 5 bits from 25-29 for the type and the 20 bits from 1-19
  1283. * plus 24 for the offset.
  1284. * 0| offset |0110|o|type |00|
  1285. * 0 0000000001111111111 2222 2 22222 33
  1286. * 0 1234567890123456789 0123 4 56789 01
  1287. *
  1288. * 64 bit swap entry format:
  1289. * A page-table entry has some bits we have to treat in a special way.
  1290. * Bits 52 and bit 55 have to be zero, otherwise an specification
  1291. * exception will occur instead of a page translation exception. The
  1292. * specifiation exception has the bad habit not to store necessary
  1293. * information in the lowcore.
  1294. * Bit 53 and bit 54 are the page invalid bit and the page protection
  1295. * bit. We set both to indicate a swapped page.
  1296. * Bit 62 and 63 are used to distinguish the different page types. For
  1297. * a swapped page these bits need to be zero.
  1298. * This leaves the bits 0-51 and bits 56-61 to store type and offset.
  1299. * We use the 5 bits from 57-61 for the type and the 53 bits from 0-51
  1300. * plus 56 for the offset.
  1301. * | offset |0110|o|type |00|
  1302. * 0000000000111111111122222222223333333333444444444455 5555 5 55566 66
  1303. * 0123456789012345678901234567890123456789012345678901 2345 6 78901 23
  1304. */
  1305. #ifndef CONFIG_64BIT
  1306. #define __SWP_OFFSET_MASK (~0UL >> 12)
  1307. #else
  1308. #define __SWP_OFFSET_MASK (~0UL >> 11)
  1309. #endif
  1310. static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset)
  1311. {
  1312. pte_t pte;
  1313. offset &= __SWP_OFFSET_MASK;
  1314. pte_val(pte) = _PAGE_TYPE_SWAP | ((type & 0x1f) << 2) |
  1315. ((offset & 1UL) << 7) | ((offset & ~1UL) << 11);
  1316. return pte;
  1317. }
  1318. #define __swp_type(entry) (((entry).val >> 2) & 0x1f)
  1319. #define __swp_offset(entry) (((entry).val >> 11) | (((entry).val >> 7) & 1))
  1320. #define __swp_entry(type,offset) ((swp_entry_t) { pte_val(mk_swap_pte((type),(offset))) })
  1321. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  1322. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  1323. #ifndef CONFIG_64BIT
  1324. # define PTE_FILE_MAX_BITS 26
  1325. #else /* CONFIG_64BIT */
  1326. # define PTE_FILE_MAX_BITS 59
  1327. #endif /* CONFIG_64BIT */
  1328. #define pte_to_pgoff(__pte) \
  1329. ((((__pte).pte >> 12) << 7) + (((__pte).pte >> 1) & 0x7f))
  1330. #define pgoff_to_pte(__off) \
  1331. ((pte_t) { ((((__off) & 0x7f) << 1) + (((__off) >> 7) << 12)) \
  1332. | _PAGE_TYPE_FILE })
  1333. #endif /* !__ASSEMBLY__ */
  1334. #define kern_addr_valid(addr) (1)
  1335. extern int vmem_add_mapping(unsigned long start, unsigned long size);
  1336. extern int vmem_remove_mapping(unsigned long start, unsigned long size);
  1337. extern int s390_enable_sie(void);
  1338. /*
  1339. * No page table caches to initialise
  1340. */
  1341. static inline void pgtable_cache_init(void) { }
  1342. static inline void check_pgt_cache(void) { }
  1343. #include <asm-generic/pgtable.h>
  1344. #endif /* _S390_PAGE_H */