pgtable.h 52 KB

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