pgtable.h 44 KB

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