pgtable.h 45 KB

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