pgtable.h 36 KB

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