pgtable.h 37 KB

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