pgtable.h 42 KB

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