pgtable.h 42 KB

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