pgtable.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. /*
  2. * S390 version
  3. * Copyright IBM Corp. 1999, 2000
  4. * Author(s): Hartmut Penner (hp@de.ibm.com)
  5. * Ulrich Weigand (weigand@de.ibm.com)
  6. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  7. *
  8. * Derived from "include/asm-i386/pgtable.h"
  9. */
  10. #ifndef _ASM_S390_PGTABLE_H
  11. #define _ASM_S390_PGTABLE_H
  12. /*
  13. * The Linux memory management assumes a three-level page table setup.
  14. * For s390 64 bit we use up to four of the five levels the hardware
  15. * provides (region first tables are not used).
  16. *
  17. * The "pgd_xxx()" functions are trivial for a folded two-level
  18. * setup: the pgd is never bad, and a pmd always exists (as it's folded
  19. * into the pgd entry)
  20. *
  21. * This file contains the functions and defines necessary to modify and use
  22. * the S390 page table tree.
  23. */
  24. #ifndef __ASSEMBLY__
  25. #include <linux/sched.h>
  26. #include <linux/mm_types.h>
  27. #include <linux/page-flags.h>
  28. #include <linux/radix-tree.h>
  29. #include <asm/bug.h>
  30. #include <asm/page.h>
  31. extern pgd_t swapper_pg_dir[] __attribute__ ((aligned (4096)));
  32. extern void paging_init(void);
  33. extern void vmem_map_init(void);
  34. /*
  35. * The S390 doesn't have any external MMU info: the kernel page
  36. * tables contain all the necessary information.
  37. */
  38. #define update_mmu_cache(vma, address, ptep) do { } while (0)
  39. #define update_mmu_cache_pmd(vma, address, ptep) do { } while (0)
  40. /*
  41. * ZERO_PAGE is a global shared page that is always zero; used
  42. * for zero-mapped memory areas etc..
  43. */
  44. extern unsigned long empty_zero_page;
  45. extern unsigned long zero_page_mask;
  46. #define ZERO_PAGE(vaddr) \
  47. (virt_to_page((void *)(empty_zero_page + \
  48. (((unsigned long)(vaddr)) &zero_page_mask))))
  49. #define __HAVE_COLOR_ZERO_PAGE
  50. /* TODO: s390 cannot support io_remap_pfn_range... */
  51. #endif /* !__ASSEMBLY__ */
  52. /*
  53. * PMD_SHIFT determines the size of the area a second-level page
  54. * table can map
  55. * PGDIR_SHIFT determines what a third-level page table entry can map
  56. */
  57. #define PMD_SHIFT 20
  58. #define PUD_SHIFT 31
  59. #define PGDIR_SHIFT 42
  60. #define PMD_SIZE (1UL << PMD_SHIFT)
  61. #define PMD_MASK (~(PMD_SIZE-1))
  62. #define PUD_SIZE (1UL << PUD_SHIFT)
  63. #define PUD_MASK (~(PUD_SIZE-1))
  64. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  65. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  66. /*
  67. * entries per page directory level: the S390 is two-level, so
  68. * we don't really have any PMD directory physically.
  69. * for S390 segment-table entries are combined to one PGD
  70. * that leads to 1024 pte per pgd
  71. */
  72. #define PTRS_PER_PTE 256
  73. #define PTRS_PER_PMD 2048
  74. #define PTRS_PER_PUD 2048
  75. #define PTRS_PER_PGD 2048
  76. #define FIRST_USER_ADDRESS 0UL
  77. #define pte_ERROR(e) \
  78. printk("%s:%d: bad pte %p.\n", __FILE__, __LINE__, (void *) pte_val(e))
  79. #define pmd_ERROR(e) \
  80. printk("%s:%d: bad pmd %p.\n", __FILE__, __LINE__, (void *) pmd_val(e))
  81. #define pud_ERROR(e) \
  82. printk("%s:%d: bad pud %p.\n", __FILE__, __LINE__, (void *) pud_val(e))
  83. #define pgd_ERROR(e) \
  84. printk("%s:%d: bad pgd %p.\n", __FILE__, __LINE__, (void *) pgd_val(e))
  85. #ifndef __ASSEMBLY__
  86. /*
  87. * The vmalloc and module area will always be on the topmost area of the
  88. * kernel mapping. We reserve 128GB (64bit) for vmalloc and modules.
  89. * On 64 bit kernels we have a 2GB area at the top of the vmalloc area where
  90. * modules will reside. That makes sure that inter module branches always
  91. * happen without trampolines and in addition the placement within a 2GB frame
  92. * is branch prediction unit friendly.
  93. */
  94. extern unsigned long VMALLOC_START;
  95. extern unsigned long VMALLOC_END;
  96. extern struct page *vmemmap;
  97. #define VMEM_MAX_PHYS ((unsigned long) vmemmap)
  98. extern unsigned long MODULES_VADDR;
  99. extern unsigned long MODULES_END;
  100. #define MODULES_VADDR MODULES_VADDR
  101. #define MODULES_END MODULES_END
  102. #define MODULES_LEN (1UL << 31)
  103. static inline int is_module_addr(void *addr)
  104. {
  105. BUILD_BUG_ON(MODULES_LEN > (1UL << 31));
  106. if (addr < (void *)MODULES_VADDR)
  107. return 0;
  108. if (addr > (void *)MODULES_END)
  109. return 0;
  110. return 1;
  111. }
  112. /*
  113. * A 64 bit pagetable entry of S390 has following format:
  114. * | PFRA |0IPC| OS |
  115. * 0000000000111111111122222222223333333333444444444455555555556666
  116. * 0123456789012345678901234567890123456789012345678901234567890123
  117. *
  118. * I Page-Invalid Bit: Page is not available for address-translation
  119. * P Page-Protection Bit: Store access not possible for page
  120. * C Change-bit override: HW is not required to set change bit
  121. *
  122. * A 64 bit segmenttable entry of S390 has following format:
  123. * | P-table origin | TT
  124. * 0000000000111111111122222222223333333333444444444455555555556666
  125. * 0123456789012345678901234567890123456789012345678901234567890123
  126. *
  127. * I Segment-Invalid Bit: Segment is not available for address-translation
  128. * C Common-Segment Bit: Segment is not private (PoP 3-30)
  129. * P Page-Protection Bit: Store access not possible for page
  130. * TT Type 00
  131. *
  132. * A 64 bit region table entry of S390 has following format:
  133. * | S-table origin | TF TTTL
  134. * 0000000000111111111122222222223333333333444444444455555555556666
  135. * 0123456789012345678901234567890123456789012345678901234567890123
  136. *
  137. * I Segment-Invalid Bit: Segment is not available for address-translation
  138. * TT Type 01
  139. * TF
  140. * TL Table length
  141. *
  142. * The 64 bit regiontable origin of S390 has following format:
  143. * | region table origon | DTTL
  144. * 0000000000111111111122222222223333333333444444444455555555556666
  145. * 0123456789012345678901234567890123456789012345678901234567890123
  146. *
  147. * X Space-Switch event:
  148. * G Segment-Invalid Bit:
  149. * P Private-Space Bit:
  150. * S Storage-Alteration:
  151. * R Real space
  152. * TL Table-Length:
  153. *
  154. * A storage key has the following format:
  155. * | ACC |F|R|C|0|
  156. * 0 3 4 5 6 7
  157. * ACC: access key
  158. * F : fetch protection bit
  159. * R : referenced bit
  160. * C : changed bit
  161. */
  162. /* Hardware bits in the page table entry */
  163. #define _PAGE_PROTECT 0x200 /* HW read-only bit */
  164. #define _PAGE_INVALID 0x400 /* HW invalid bit */
  165. #define _PAGE_LARGE 0x800 /* Bit to mark a large pte */
  166. /* Software bits in the page table entry */
  167. #define _PAGE_PRESENT 0x001 /* SW pte present bit */
  168. #define _PAGE_YOUNG 0x004 /* SW pte young bit */
  169. #define _PAGE_DIRTY 0x008 /* SW pte dirty bit */
  170. #define _PAGE_READ 0x010 /* SW pte read bit */
  171. #define _PAGE_WRITE 0x020 /* SW pte write bit */
  172. #define _PAGE_SPECIAL 0x040 /* SW associated with special page */
  173. #define _PAGE_UNUSED 0x080 /* SW bit for pgste usage state */
  174. #define __HAVE_ARCH_PTE_SPECIAL
  175. #ifdef CONFIG_MEM_SOFT_DIRTY
  176. #define _PAGE_SOFT_DIRTY 0x002 /* SW pte soft dirty bit */
  177. #else
  178. #define _PAGE_SOFT_DIRTY 0x000
  179. #endif
  180. /* Set of bits not changed in pte_modify */
  181. #define _PAGE_CHG_MASK (PAGE_MASK | _PAGE_SPECIAL | _PAGE_DIRTY | \
  182. _PAGE_YOUNG | _PAGE_SOFT_DIRTY)
  183. /*
  184. * handle_pte_fault uses pte_present and pte_none to find out the pte type
  185. * WITHOUT holding the page table lock. The _PAGE_PRESENT bit is used to
  186. * distinguish present from not-present ptes. It is changed only with the page
  187. * table lock held.
  188. *
  189. * The following table gives the different possible bit combinations for
  190. * the pte hardware and software bits in the last 12 bits of a pte
  191. * (. unassigned bit, x don't care, t swap type):
  192. *
  193. * 842100000000
  194. * 000084210000
  195. * 000000008421
  196. * .IR.uswrdy.p
  197. * empty .10.00000000
  198. * swap .11..ttttt.0
  199. * prot-none, clean, old .11.xx0000.1
  200. * prot-none, clean, young .11.xx0001.1
  201. * prot-none, dirty, old .10.xx0010.1
  202. * prot-none, dirty, young .10.xx0011.1
  203. * read-only, clean, old .11.xx0100.1
  204. * read-only, clean, young .01.xx0101.1
  205. * read-only, dirty, old .11.xx0110.1
  206. * read-only, dirty, young .01.xx0111.1
  207. * read-write, clean, old .11.xx1100.1
  208. * read-write, clean, young .01.xx1101.1
  209. * read-write, dirty, old .10.xx1110.1
  210. * read-write, dirty, young .00.xx1111.1
  211. * HW-bits: R read-only, I invalid
  212. * SW-bits: p present, y young, d dirty, r read, w write, s special,
  213. * u unused, l large
  214. *
  215. * pte_none is true for the bit pattern .10.00000000, pte == 0x400
  216. * pte_swap is true for the bit pattern .11..ooooo.0, (pte & 0x201) == 0x200
  217. * pte_present is true for the bit pattern .xx.xxxxxx.1, (pte & 0x001) == 0x001
  218. */
  219. /* Bits in the segment/region table address-space-control-element */
  220. #define _ASCE_ORIGIN ~0xfffUL/* segment table origin */
  221. #define _ASCE_PRIVATE_SPACE 0x100 /* private space control */
  222. #define _ASCE_ALT_EVENT 0x80 /* storage alteration event control */
  223. #define _ASCE_SPACE_SWITCH 0x40 /* space switch event */
  224. #define _ASCE_REAL_SPACE 0x20 /* real space control */
  225. #define _ASCE_TYPE_MASK 0x0c /* asce table type mask */
  226. #define _ASCE_TYPE_REGION1 0x0c /* region first table type */
  227. #define _ASCE_TYPE_REGION2 0x08 /* region second table type */
  228. #define _ASCE_TYPE_REGION3 0x04 /* region third table type */
  229. #define _ASCE_TYPE_SEGMENT 0x00 /* segment table type */
  230. #define _ASCE_TABLE_LENGTH 0x03 /* region table length */
  231. /* Bits in the region table entry */
  232. #define _REGION_ENTRY_ORIGIN ~0xfffUL/* region/segment table origin */
  233. #define _REGION_ENTRY_PROTECT 0x200 /* region protection bit */
  234. #define _REGION_ENTRY_INVALID 0x20 /* invalid region table entry */
  235. #define _REGION_ENTRY_TYPE_MASK 0x0c /* region/segment table type mask */
  236. #define _REGION_ENTRY_TYPE_R1 0x0c /* region first table type */
  237. #define _REGION_ENTRY_TYPE_R2 0x08 /* region second table type */
  238. #define _REGION_ENTRY_TYPE_R3 0x04 /* region third table type */
  239. #define _REGION_ENTRY_LENGTH 0x03 /* region third length */
  240. #define _REGION1_ENTRY (_REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_LENGTH)
  241. #define _REGION1_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INVALID)
  242. #define _REGION2_ENTRY (_REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_LENGTH)
  243. #define _REGION2_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INVALID)
  244. #define _REGION3_ENTRY (_REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_LENGTH)
  245. #define _REGION3_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INVALID)
  246. #define _REGION3_ENTRY_LARGE 0x400 /* RTTE-format control, large page */
  247. #define _REGION3_ENTRY_RO 0x200 /* page protection bit */
  248. /* Bits in the segment table entry */
  249. #define _SEGMENT_ENTRY_BITS 0xfffffffffffffe33UL
  250. #define _SEGMENT_ENTRY_BITS_LARGE 0xfffffffffff0ff33UL
  251. #define _SEGMENT_ENTRY_ORIGIN_LARGE ~0xfffffUL /* large page address */
  252. #define _SEGMENT_ENTRY_ORIGIN ~0x7ffUL/* segment table origin */
  253. #define _SEGMENT_ENTRY_PROTECT 0x200 /* page protection bit */
  254. #define _SEGMENT_ENTRY_INVALID 0x20 /* invalid segment table entry */
  255. #define _SEGMENT_ENTRY (0)
  256. #define _SEGMENT_ENTRY_EMPTY (_SEGMENT_ENTRY_INVALID)
  257. #define _SEGMENT_ENTRY_DIRTY 0x2000 /* SW segment dirty bit */
  258. #define _SEGMENT_ENTRY_YOUNG 0x1000 /* SW segment young bit */
  259. #define _SEGMENT_ENTRY_LARGE 0x0400 /* STE-format control, large page */
  260. #define _SEGMENT_ENTRY_READ 0x0002 /* SW segment read bit */
  261. #define _SEGMENT_ENTRY_WRITE 0x0001 /* SW segment write bit */
  262. #ifdef CONFIG_MEM_SOFT_DIRTY
  263. #define _SEGMENT_ENTRY_SOFT_DIRTY 0x4000 /* SW segment soft dirty bit */
  264. #else
  265. #define _SEGMENT_ENTRY_SOFT_DIRTY 0x0000 /* SW segment soft dirty bit */
  266. #endif
  267. /*
  268. * Segment table entry encoding (R = read-only, I = invalid, y = young bit):
  269. * dy..R...I...rw
  270. * prot-none, clean, old 00..1...1...00
  271. * prot-none, clean, young 01..1...1...00
  272. * prot-none, dirty, old 10..1...1...00
  273. * prot-none, dirty, young 11..1...1...00
  274. * read-only, clean, old 00..1...1...10
  275. * read-only, clean, young 01..1...0...10
  276. * read-only, dirty, old 10..1...1...10
  277. * read-only, dirty, young 11..1...0...10
  278. * read-write, clean, old 00..1...1...11
  279. * read-write, clean, young 01..1...0...11
  280. * read-write, dirty, old 10..0...1...11
  281. * read-write, dirty, young 11..0...0...11
  282. * The segment table origin is used to distinguish empty (origin==0) from
  283. * read-write, old segment table entries (origin!=0)
  284. * HW-bits: R read-only, I invalid
  285. * SW-bits: y young, d dirty, r read, w write
  286. */
  287. /* Page status table bits for virtualization */
  288. #define PGSTE_ACC_BITS 0xf000000000000000UL
  289. #define PGSTE_FP_BIT 0x0800000000000000UL
  290. #define PGSTE_PCL_BIT 0x0080000000000000UL
  291. #define PGSTE_HR_BIT 0x0040000000000000UL
  292. #define PGSTE_HC_BIT 0x0020000000000000UL
  293. #define PGSTE_GR_BIT 0x0004000000000000UL
  294. #define PGSTE_GC_BIT 0x0002000000000000UL
  295. #define PGSTE_UC_BIT 0x0000800000000000UL /* user dirty (migration) */
  296. #define PGSTE_IN_BIT 0x0000400000000000UL /* IPTE notify bit */
  297. /* Guest Page State used for virtualization */
  298. #define _PGSTE_GPS_ZERO 0x0000000080000000UL
  299. #define _PGSTE_GPS_USAGE_MASK 0x0000000003000000UL
  300. #define _PGSTE_GPS_USAGE_STABLE 0x0000000000000000UL
  301. #define _PGSTE_GPS_USAGE_UNUSED 0x0000000001000000UL
  302. /*
  303. * A user page table pointer has the space-switch-event bit, the
  304. * private-space-control bit and the storage-alteration-event-control
  305. * bit set. A kernel page table pointer doesn't need them.
  306. */
  307. #define _ASCE_USER_BITS (_ASCE_SPACE_SWITCH | _ASCE_PRIVATE_SPACE | \
  308. _ASCE_ALT_EVENT)
  309. /*
  310. * Page protection definitions.
  311. */
  312. #define PAGE_NONE __pgprot(_PAGE_PRESENT | _PAGE_INVALID)
  313. #define PAGE_READ __pgprot(_PAGE_PRESENT | _PAGE_READ | \
  314. _PAGE_INVALID | _PAGE_PROTECT)
  315. #define PAGE_WRITE __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE | \
  316. _PAGE_INVALID | _PAGE_PROTECT)
  317. #define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE | \
  318. _PAGE_YOUNG | _PAGE_DIRTY)
  319. #define PAGE_KERNEL __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE | \
  320. _PAGE_YOUNG | _PAGE_DIRTY)
  321. #define PAGE_KERNEL_RO __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_YOUNG | \
  322. _PAGE_PROTECT)
  323. /*
  324. * On s390 the page table entry has an invalid bit and a read-only bit.
  325. * Read permission implies execute permission and write permission
  326. * implies read permission.
  327. */
  328. /*xwr*/
  329. #define __P000 PAGE_NONE
  330. #define __P001 PAGE_READ
  331. #define __P010 PAGE_READ
  332. #define __P011 PAGE_READ
  333. #define __P100 PAGE_READ
  334. #define __P101 PAGE_READ
  335. #define __P110 PAGE_READ
  336. #define __P111 PAGE_READ
  337. #define __S000 PAGE_NONE
  338. #define __S001 PAGE_READ
  339. #define __S010 PAGE_WRITE
  340. #define __S011 PAGE_WRITE
  341. #define __S100 PAGE_READ
  342. #define __S101 PAGE_READ
  343. #define __S110 PAGE_WRITE
  344. #define __S111 PAGE_WRITE
  345. /*
  346. * Segment entry (large page) protection definitions.
  347. */
  348. #define SEGMENT_NONE __pgprot(_SEGMENT_ENTRY_INVALID | \
  349. _SEGMENT_ENTRY_PROTECT)
  350. #define SEGMENT_READ __pgprot(_SEGMENT_ENTRY_PROTECT | \
  351. _SEGMENT_ENTRY_READ)
  352. #define SEGMENT_WRITE __pgprot(_SEGMENT_ENTRY_READ | \
  353. _SEGMENT_ENTRY_WRITE)
  354. static inline int mm_has_pgste(struct mm_struct *mm)
  355. {
  356. #ifdef CONFIG_PGSTE
  357. if (unlikely(mm->context.has_pgste))
  358. return 1;
  359. #endif
  360. return 0;
  361. }
  362. static inline int mm_alloc_pgste(struct mm_struct *mm)
  363. {
  364. #ifdef CONFIG_PGSTE
  365. if (unlikely(mm->context.alloc_pgste))
  366. return 1;
  367. #endif
  368. return 0;
  369. }
  370. /*
  371. * In the case that a guest uses storage keys
  372. * faults should no longer be backed by zero pages
  373. */
  374. #define mm_forbids_zeropage mm_use_skey
  375. static inline int mm_use_skey(struct mm_struct *mm)
  376. {
  377. #ifdef CONFIG_PGSTE
  378. if (mm->context.use_skey)
  379. return 1;
  380. #endif
  381. return 0;
  382. }
  383. /*
  384. * pgd/pmd/pte query functions
  385. */
  386. static inline int pgd_present(pgd_t pgd)
  387. {
  388. if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R2)
  389. return 1;
  390. return (pgd_val(pgd) & _REGION_ENTRY_ORIGIN) != 0UL;
  391. }
  392. static inline int pgd_none(pgd_t pgd)
  393. {
  394. if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R2)
  395. return 0;
  396. return (pgd_val(pgd) & _REGION_ENTRY_INVALID) != 0UL;
  397. }
  398. static inline int pgd_bad(pgd_t pgd)
  399. {
  400. /*
  401. * With dynamic page table levels the pgd can be a region table
  402. * entry or a segment table entry. Check for the bit that are
  403. * invalid for either table entry.
  404. */
  405. unsigned long mask =
  406. ~_SEGMENT_ENTRY_ORIGIN & ~_REGION_ENTRY_INVALID &
  407. ~_REGION_ENTRY_TYPE_MASK & ~_REGION_ENTRY_LENGTH;
  408. return (pgd_val(pgd) & mask) != 0;
  409. }
  410. static inline int pud_present(pud_t pud)
  411. {
  412. if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R3)
  413. return 1;
  414. return (pud_val(pud) & _REGION_ENTRY_ORIGIN) != 0UL;
  415. }
  416. static inline int pud_none(pud_t pud)
  417. {
  418. if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R3)
  419. return 0;
  420. return (pud_val(pud) & _REGION_ENTRY_INVALID) != 0UL;
  421. }
  422. static inline int pud_large(pud_t pud)
  423. {
  424. if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) != _REGION_ENTRY_TYPE_R3)
  425. return 0;
  426. return !!(pud_val(pud) & _REGION3_ENTRY_LARGE);
  427. }
  428. static inline int pud_bad(pud_t pud)
  429. {
  430. /*
  431. * With dynamic page table levels the pud can be a region table
  432. * entry or a segment table entry. Check for the bit that are
  433. * invalid for either table entry.
  434. */
  435. unsigned long mask =
  436. ~_SEGMENT_ENTRY_ORIGIN & ~_REGION_ENTRY_INVALID &
  437. ~_REGION_ENTRY_TYPE_MASK & ~_REGION_ENTRY_LENGTH;
  438. return (pud_val(pud) & mask) != 0;
  439. }
  440. static inline int pmd_present(pmd_t pmd)
  441. {
  442. return pmd_val(pmd) != _SEGMENT_ENTRY_INVALID;
  443. }
  444. static inline int pmd_none(pmd_t pmd)
  445. {
  446. return pmd_val(pmd) == _SEGMENT_ENTRY_INVALID;
  447. }
  448. static inline int pmd_large(pmd_t pmd)
  449. {
  450. return (pmd_val(pmd) & _SEGMENT_ENTRY_LARGE) != 0;
  451. }
  452. static inline unsigned long pmd_pfn(pmd_t pmd)
  453. {
  454. unsigned long origin_mask;
  455. origin_mask = _SEGMENT_ENTRY_ORIGIN;
  456. if (pmd_large(pmd))
  457. origin_mask = _SEGMENT_ENTRY_ORIGIN_LARGE;
  458. return (pmd_val(pmd) & origin_mask) >> PAGE_SHIFT;
  459. }
  460. static inline int pmd_bad(pmd_t pmd)
  461. {
  462. if (pmd_large(pmd))
  463. return (pmd_val(pmd) & ~_SEGMENT_ENTRY_BITS_LARGE) != 0;
  464. return (pmd_val(pmd) & ~_SEGMENT_ENTRY_BITS) != 0;
  465. }
  466. #define __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
  467. extern int pmdp_set_access_flags(struct vm_area_struct *vma,
  468. unsigned long address, pmd_t *pmdp,
  469. pmd_t entry, int dirty);
  470. #define __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
  471. extern int pmdp_clear_flush_young(struct vm_area_struct *vma,
  472. unsigned long address, pmd_t *pmdp);
  473. #define __HAVE_ARCH_PMD_WRITE
  474. static inline int pmd_write(pmd_t pmd)
  475. {
  476. return (pmd_val(pmd) & _SEGMENT_ENTRY_WRITE) != 0;
  477. }
  478. static inline int pmd_dirty(pmd_t pmd)
  479. {
  480. int dirty = 1;
  481. if (pmd_large(pmd))
  482. dirty = (pmd_val(pmd) & _SEGMENT_ENTRY_DIRTY) != 0;
  483. return dirty;
  484. }
  485. static inline int pmd_young(pmd_t pmd)
  486. {
  487. int young = 1;
  488. if (pmd_large(pmd))
  489. young = (pmd_val(pmd) & _SEGMENT_ENTRY_YOUNG) != 0;
  490. return young;
  491. }
  492. static inline int pte_present(pte_t pte)
  493. {
  494. /* Bit pattern: (pte & 0x001) == 0x001 */
  495. return (pte_val(pte) & _PAGE_PRESENT) != 0;
  496. }
  497. static inline int pte_none(pte_t pte)
  498. {
  499. /* Bit pattern: pte == 0x400 */
  500. return pte_val(pte) == _PAGE_INVALID;
  501. }
  502. static inline int pte_swap(pte_t pte)
  503. {
  504. /* Bit pattern: (pte & 0x201) == 0x200 */
  505. return (pte_val(pte) & (_PAGE_PROTECT | _PAGE_PRESENT))
  506. == _PAGE_PROTECT;
  507. }
  508. static inline int pte_special(pte_t pte)
  509. {
  510. return (pte_val(pte) & _PAGE_SPECIAL);
  511. }
  512. #define __HAVE_ARCH_PTE_SAME
  513. static inline int pte_same(pte_t a, pte_t b)
  514. {
  515. return pte_val(a) == pte_val(b);
  516. }
  517. #ifdef CONFIG_NUMA_BALANCING
  518. static inline int pte_protnone(pte_t pte)
  519. {
  520. return pte_present(pte) && !(pte_val(pte) & _PAGE_READ);
  521. }
  522. static inline int pmd_protnone(pmd_t pmd)
  523. {
  524. /* pmd_large(pmd) implies pmd_present(pmd) */
  525. return pmd_large(pmd) && !(pmd_val(pmd) & _SEGMENT_ENTRY_READ);
  526. }
  527. #endif
  528. static inline int pte_soft_dirty(pte_t pte)
  529. {
  530. return pte_val(pte) & _PAGE_SOFT_DIRTY;
  531. }
  532. #define pte_swp_soft_dirty pte_soft_dirty
  533. static inline pte_t pte_mksoft_dirty(pte_t pte)
  534. {
  535. pte_val(pte) |= _PAGE_SOFT_DIRTY;
  536. return pte;
  537. }
  538. #define pte_swp_mksoft_dirty pte_mksoft_dirty
  539. static inline pte_t pte_clear_soft_dirty(pte_t pte)
  540. {
  541. pte_val(pte) &= ~_PAGE_SOFT_DIRTY;
  542. return pte;
  543. }
  544. #define pte_swp_clear_soft_dirty pte_clear_soft_dirty
  545. static inline int pmd_soft_dirty(pmd_t pmd)
  546. {
  547. return pmd_val(pmd) & _SEGMENT_ENTRY_SOFT_DIRTY;
  548. }
  549. static inline pmd_t pmd_mksoft_dirty(pmd_t pmd)
  550. {
  551. pmd_val(pmd) |= _SEGMENT_ENTRY_SOFT_DIRTY;
  552. return pmd;
  553. }
  554. static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd)
  555. {
  556. pmd_val(pmd) &= ~_SEGMENT_ENTRY_SOFT_DIRTY;
  557. return pmd;
  558. }
  559. static inline pgste_t pgste_get_lock(pte_t *ptep)
  560. {
  561. unsigned long new = 0;
  562. #ifdef CONFIG_PGSTE
  563. unsigned long old;
  564. preempt_disable();
  565. asm(
  566. " lg %0,%2\n"
  567. "0: lgr %1,%0\n"
  568. " nihh %0,0xff7f\n" /* clear PCL bit in old */
  569. " oihh %1,0x0080\n" /* set PCL bit in new */
  570. " csg %0,%1,%2\n"
  571. " jl 0b\n"
  572. : "=&d" (old), "=&d" (new), "=Q" (ptep[PTRS_PER_PTE])
  573. : "Q" (ptep[PTRS_PER_PTE]) : "cc", "memory");
  574. #endif
  575. return __pgste(new);
  576. }
  577. static inline void pgste_set_unlock(pte_t *ptep, pgste_t pgste)
  578. {
  579. #ifdef CONFIG_PGSTE
  580. asm(
  581. " nihh %1,0xff7f\n" /* clear PCL bit */
  582. " stg %1,%0\n"
  583. : "=Q" (ptep[PTRS_PER_PTE])
  584. : "d" (pgste_val(pgste)), "Q" (ptep[PTRS_PER_PTE])
  585. : "cc", "memory");
  586. preempt_enable();
  587. #endif
  588. }
  589. static inline pgste_t pgste_get(pte_t *ptep)
  590. {
  591. unsigned long pgste = 0;
  592. #ifdef CONFIG_PGSTE
  593. pgste = *(unsigned long *)(ptep + PTRS_PER_PTE);
  594. #endif
  595. return __pgste(pgste);
  596. }
  597. static inline void pgste_set(pte_t *ptep, pgste_t pgste)
  598. {
  599. #ifdef CONFIG_PGSTE
  600. *(pgste_t *)(ptep + PTRS_PER_PTE) = pgste;
  601. #endif
  602. }
  603. bool pgste_test_and_clear_dirty(struct mm_struct *, unsigned long address);
  604. void ptep_ipte_notify(struct mm_struct *, unsigned long addr, pte_t *);
  605. /**
  606. * struct gmap_struct - guest address space
  607. * @crst_list: list of all crst tables used in the guest address space
  608. * @mm: pointer to the parent mm_struct
  609. * @guest_to_host: radix tree with guest to host address translation
  610. * @host_to_guest: radix tree with pointer to segment table entries
  611. * @guest_table_lock: spinlock to protect all entries in the guest page table
  612. * @table: pointer to the page directory
  613. * @asce: address space control element for gmap page table
  614. * @pfault_enabled: defines if pfaults are applicable for the guest
  615. */
  616. struct gmap {
  617. struct list_head list;
  618. struct list_head crst_list;
  619. struct mm_struct *mm;
  620. struct radix_tree_root guest_to_host;
  621. struct radix_tree_root host_to_guest;
  622. spinlock_t guest_table_lock;
  623. unsigned long *table;
  624. unsigned long asce;
  625. unsigned long asce_end;
  626. void *private;
  627. bool pfault_enabled;
  628. };
  629. /**
  630. * struct gmap_notifier - notify function block for page invalidation
  631. * @notifier_call: address of callback function
  632. */
  633. struct gmap_notifier {
  634. struct list_head list;
  635. void (*notifier_call)(struct gmap *gmap, unsigned long gaddr);
  636. };
  637. struct gmap *gmap_alloc(struct mm_struct *mm, unsigned long limit);
  638. void gmap_free(struct gmap *gmap);
  639. void gmap_enable(struct gmap *gmap);
  640. void gmap_disable(struct gmap *gmap);
  641. int gmap_map_segment(struct gmap *gmap, unsigned long from,
  642. unsigned long to, unsigned long len);
  643. int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len);
  644. unsigned long __gmap_translate(struct gmap *, unsigned long gaddr);
  645. unsigned long gmap_translate(struct gmap *, unsigned long gaddr);
  646. int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr);
  647. int gmap_fault(struct gmap *, unsigned long gaddr, unsigned int fault_flags);
  648. void gmap_discard(struct gmap *, unsigned long from, unsigned long to);
  649. void __gmap_zap(struct gmap *, unsigned long gaddr);
  650. void gmap_register_ipte_notifier(struct gmap_notifier *);
  651. void gmap_unregister_ipte_notifier(struct gmap_notifier *);
  652. int gmap_ipte_notify(struct gmap *, unsigned long start, unsigned long len);
  653. /*
  654. * query functions pte_write/pte_dirty/pte_young only work if
  655. * pte_present() is true. Undefined behaviour if not..
  656. */
  657. static inline int pte_write(pte_t pte)
  658. {
  659. return (pte_val(pte) & _PAGE_WRITE) != 0;
  660. }
  661. static inline int pte_dirty(pte_t pte)
  662. {
  663. return (pte_val(pte) & _PAGE_DIRTY) != 0;
  664. }
  665. static inline int pte_young(pte_t pte)
  666. {
  667. return (pte_val(pte) & _PAGE_YOUNG) != 0;
  668. }
  669. #define __HAVE_ARCH_PTE_UNUSED
  670. static inline int pte_unused(pte_t pte)
  671. {
  672. return pte_val(pte) & _PAGE_UNUSED;
  673. }
  674. /*
  675. * pgd/pmd/pte modification functions
  676. */
  677. static inline void pgd_clear(pgd_t *pgd)
  678. {
  679. if ((pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
  680. pgd_val(*pgd) = _REGION2_ENTRY_EMPTY;
  681. }
  682. static inline void pud_clear(pud_t *pud)
  683. {
  684. if ((pud_val(*pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
  685. pud_val(*pud) = _REGION3_ENTRY_EMPTY;
  686. }
  687. static inline void pmd_clear(pmd_t *pmdp)
  688. {
  689. pmd_val(*pmdp) = _SEGMENT_ENTRY_INVALID;
  690. }
  691. static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  692. {
  693. pte_val(*ptep) = _PAGE_INVALID;
  694. }
  695. /*
  696. * The following pte modification functions only work if
  697. * pte_present() is true. Undefined behaviour if not..
  698. */
  699. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  700. {
  701. pte_val(pte) &= _PAGE_CHG_MASK;
  702. pte_val(pte) |= pgprot_val(newprot);
  703. /*
  704. * newprot for PAGE_NONE, PAGE_READ and PAGE_WRITE has the
  705. * invalid bit set, clear it again for readable, young pages
  706. */
  707. if ((pte_val(pte) & _PAGE_YOUNG) && (pte_val(pte) & _PAGE_READ))
  708. pte_val(pte) &= ~_PAGE_INVALID;
  709. /*
  710. * newprot for PAGE_READ and PAGE_WRITE has the page protection
  711. * bit set, clear it again for writable, dirty pages
  712. */
  713. if ((pte_val(pte) & _PAGE_DIRTY) && (pte_val(pte) & _PAGE_WRITE))
  714. pte_val(pte) &= ~_PAGE_PROTECT;
  715. return pte;
  716. }
  717. static inline pte_t pte_wrprotect(pte_t pte)
  718. {
  719. pte_val(pte) &= ~_PAGE_WRITE;
  720. pte_val(pte) |= _PAGE_PROTECT;
  721. return pte;
  722. }
  723. static inline pte_t pte_mkwrite(pte_t pte)
  724. {
  725. pte_val(pte) |= _PAGE_WRITE;
  726. if (pte_val(pte) & _PAGE_DIRTY)
  727. pte_val(pte) &= ~_PAGE_PROTECT;
  728. return pte;
  729. }
  730. static inline pte_t pte_mkclean(pte_t pte)
  731. {
  732. pte_val(pte) &= ~_PAGE_DIRTY;
  733. pte_val(pte) |= _PAGE_PROTECT;
  734. return pte;
  735. }
  736. static inline pte_t pte_mkdirty(pte_t pte)
  737. {
  738. pte_val(pte) |= _PAGE_DIRTY | _PAGE_SOFT_DIRTY;
  739. if (pte_val(pte) & _PAGE_WRITE)
  740. pte_val(pte) &= ~_PAGE_PROTECT;
  741. return pte;
  742. }
  743. static inline pte_t pte_mkold(pte_t pte)
  744. {
  745. pte_val(pte) &= ~_PAGE_YOUNG;
  746. pte_val(pte) |= _PAGE_INVALID;
  747. return pte;
  748. }
  749. static inline pte_t pte_mkyoung(pte_t pte)
  750. {
  751. pte_val(pte) |= _PAGE_YOUNG;
  752. if (pte_val(pte) & _PAGE_READ)
  753. pte_val(pte) &= ~_PAGE_INVALID;
  754. return pte;
  755. }
  756. static inline pte_t pte_mkspecial(pte_t pte)
  757. {
  758. pte_val(pte) |= _PAGE_SPECIAL;
  759. return pte;
  760. }
  761. #ifdef CONFIG_HUGETLB_PAGE
  762. static inline pte_t pte_mkhuge(pte_t pte)
  763. {
  764. pte_val(pte) |= _PAGE_LARGE;
  765. return pte;
  766. }
  767. #endif
  768. static inline void __ptep_ipte(unsigned long address, pte_t *ptep)
  769. {
  770. unsigned long pto = (unsigned long) ptep;
  771. /* Invalidation + global TLB flush for the pte */
  772. asm volatile(
  773. " ipte %2,%3"
  774. : "=m" (*ptep) : "m" (*ptep), "a" (pto), "a" (address));
  775. }
  776. static inline void __ptep_ipte_local(unsigned long address, pte_t *ptep)
  777. {
  778. unsigned long pto = (unsigned long) ptep;
  779. /* Invalidation + local TLB flush for the pte */
  780. asm volatile(
  781. " .insn rrf,0xb2210000,%2,%3,0,1"
  782. : "=m" (*ptep) : "m" (*ptep), "a" (pto), "a" (address));
  783. }
  784. static inline void __ptep_ipte_range(unsigned long address, int nr, pte_t *ptep)
  785. {
  786. unsigned long pto = (unsigned long) ptep;
  787. /* Invalidate a range of ptes + global TLB flush of the ptes */
  788. do {
  789. asm volatile(
  790. " .insn rrf,0xb2210000,%2,%0,%1,0"
  791. : "+a" (address), "+a" (nr) : "a" (pto) : "memory");
  792. } while (nr != 255);
  793. }
  794. /*
  795. * This is hard to understand. ptep_get_and_clear and ptep_clear_flush
  796. * both clear the TLB for the unmapped pte. The reason is that
  797. * ptep_get_and_clear is used in common code (e.g. change_pte_range)
  798. * to modify an active pte. The sequence is
  799. * 1) ptep_get_and_clear
  800. * 2) set_pte_at
  801. * 3) flush_tlb_range
  802. * On s390 the tlb needs to get flushed with the modification of the pte
  803. * if the pte is active. The only way how this can be implemented is to
  804. * have ptep_get_and_clear do the tlb flush. In exchange flush_tlb_range
  805. * is a nop.
  806. */
  807. pte_t ptep_xchg_direct(struct mm_struct *, unsigned long, pte_t *, pte_t);
  808. pte_t ptep_xchg_lazy(struct mm_struct *, unsigned long, pte_t *, pte_t);
  809. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  810. static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
  811. unsigned long addr, pte_t *ptep)
  812. {
  813. pte_t pte = *ptep;
  814. pte = ptep_xchg_direct(vma->vm_mm, addr, ptep, pte_mkold(pte));
  815. return pte_young(pte);
  816. }
  817. #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  818. static inline int ptep_clear_flush_young(struct vm_area_struct *vma,
  819. unsigned long address, pte_t *ptep)
  820. {
  821. return ptep_test_and_clear_young(vma, address, ptep);
  822. }
  823. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  824. static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
  825. unsigned long addr, pte_t *ptep)
  826. {
  827. return ptep_xchg_lazy(mm, addr, ptep, __pte(_PAGE_INVALID));
  828. }
  829. #define __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
  830. pte_t ptep_modify_prot_start(struct mm_struct *, unsigned long, pte_t *);
  831. void ptep_modify_prot_commit(struct mm_struct *, unsigned long, pte_t *, pte_t);
  832. #define __HAVE_ARCH_PTEP_CLEAR_FLUSH
  833. static inline pte_t ptep_clear_flush(struct vm_area_struct *vma,
  834. unsigned long addr, pte_t *ptep)
  835. {
  836. return ptep_xchg_direct(vma->vm_mm, addr, ptep, __pte(_PAGE_INVALID));
  837. }
  838. /*
  839. * The batched pte unmap code uses ptep_get_and_clear_full to clear the
  840. * ptes. Here an optimization is possible. tlb_gather_mmu flushes all
  841. * tlbs of an mm if it can guarantee that the ptes of the mm_struct
  842. * cannot be accessed while the batched unmap is running. In this case
  843. * full==1 and a simple pte_clear is enough. See tlb.h.
  844. */
  845. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
  846. static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
  847. unsigned long addr,
  848. pte_t *ptep, int full)
  849. {
  850. if (full) {
  851. pte_t pte = *ptep;
  852. *ptep = __pte(_PAGE_INVALID);
  853. return pte;
  854. }
  855. return ptep_xchg_lazy(mm, addr, ptep, __pte(_PAGE_INVALID));
  856. }
  857. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  858. static inline void ptep_set_wrprotect(struct mm_struct *mm,
  859. unsigned long addr, pte_t *ptep)
  860. {
  861. pte_t pte = *ptep;
  862. if (pte_write(pte))
  863. ptep_xchg_lazy(mm, addr, ptep, pte_wrprotect(pte));
  864. }
  865. #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  866. static inline int ptep_set_access_flags(struct vm_area_struct *vma,
  867. unsigned long addr, pte_t *ptep,
  868. pte_t entry, int dirty)
  869. {
  870. if (pte_same(*ptep, entry))
  871. return 0;
  872. ptep_xchg_direct(vma->vm_mm, addr, ptep, entry);
  873. return 1;
  874. }
  875. void set_pte_pgste_at(struct mm_struct *, unsigned long, pte_t *, pte_t);
  876. /*
  877. * Certain architectures need to do special things when PTEs
  878. * within a page table are directly modified. Thus, the following
  879. * hook is made available.
  880. */
  881. static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
  882. pte_t *ptep, pte_t entry)
  883. {
  884. if (mm_has_pgste(mm))
  885. set_pte_pgste_at(mm, addr, ptep, entry);
  886. else
  887. *ptep = entry;
  888. }
  889. /*
  890. * Conversion functions: convert a page and protection to a page entry,
  891. * and a page entry and page directory to the page they refer to.
  892. */
  893. static inline pte_t mk_pte_phys(unsigned long physpage, pgprot_t pgprot)
  894. {
  895. pte_t __pte;
  896. pte_val(__pte) = physpage + pgprot_val(pgprot);
  897. return pte_mkyoung(__pte);
  898. }
  899. static inline pte_t mk_pte(struct page *page, pgprot_t pgprot)
  900. {
  901. unsigned long physpage = page_to_phys(page);
  902. pte_t __pte = mk_pte_phys(physpage, pgprot);
  903. if (pte_write(__pte) && PageDirty(page))
  904. __pte = pte_mkdirty(__pte);
  905. return __pte;
  906. }
  907. #define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
  908. #define pud_index(address) (((address) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
  909. #define pmd_index(address) (((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
  910. #define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE-1))
  911. #define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
  912. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  913. #define pmd_deref(pmd) (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN)
  914. #define pud_deref(pud) (pud_val(pud) & _REGION_ENTRY_ORIGIN)
  915. #define pgd_deref(pgd) (pgd_val(pgd) & _REGION_ENTRY_ORIGIN)
  916. static inline pud_t *pud_offset(pgd_t *pgd, unsigned long address)
  917. {
  918. pud_t *pud = (pud_t *) pgd;
  919. if ((pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
  920. pud = (pud_t *) pgd_deref(*pgd);
  921. return pud + pud_index(address);
  922. }
  923. static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address)
  924. {
  925. pmd_t *pmd = (pmd_t *) pud;
  926. if ((pud_val(*pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
  927. pmd = (pmd_t *) pud_deref(*pud);
  928. return pmd + pmd_index(address);
  929. }
  930. #define pfn_pte(pfn,pgprot) mk_pte_phys(__pa((pfn) << PAGE_SHIFT),(pgprot))
  931. #define pte_pfn(x) (pte_val(x) >> PAGE_SHIFT)
  932. #define pte_page(x) pfn_to_page(pte_pfn(x))
  933. #define pmd_page(pmd) pfn_to_page(pmd_pfn(pmd))
  934. /* Find an entry in the lowest level page table.. */
  935. #define pte_offset(pmd, addr) ((pte_t *) pmd_deref(*(pmd)) + pte_index(addr))
  936. #define pte_offset_kernel(pmd, address) pte_offset(pmd,address)
  937. #define pte_offset_map(pmd, address) pte_offset_kernel(pmd, address)
  938. #define pte_unmap(pte) do { } while (0)
  939. #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLB_PAGE)
  940. static inline unsigned long massage_pgprot_pmd(pgprot_t pgprot)
  941. {
  942. /*
  943. * pgprot is PAGE_NONE, PAGE_READ, or PAGE_WRITE (see __Pxxx / __Sxxx)
  944. * Convert to segment table entry format.
  945. */
  946. if (pgprot_val(pgprot) == pgprot_val(PAGE_NONE))
  947. return pgprot_val(SEGMENT_NONE);
  948. if (pgprot_val(pgprot) == pgprot_val(PAGE_READ))
  949. return pgprot_val(SEGMENT_READ);
  950. return pgprot_val(SEGMENT_WRITE);
  951. }
  952. static inline pmd_t pmd_wrprotect(pmd_t pmd)
  953. {
  954. pmd_val(pmd) &= ~_SEGMENT_ENTRY_WRITE;
  955. pmd_val(pmd) |= _SEGMENT_ENTRY_PROTECT;
  956. return pmd;
  957. }
  958. static inline pmd_t pmd_mkwrite(pmd_t pmd)
  959. {
  960. pmd_val(pmd) |= _SEGMENT_ENTRY_WRITE;
  961. if (pmd_large(pmd) && !(pmd_val(pmd) & _SEGMENT_ENTRY_DIRTY))
  962. return pmd;
  963. pmd_val(pmd) &= ~_SEGMENT_ENTRY_PROTECT;
  964. return pmd;
  965. }
  966. static inline pmd_t pmd_mkclean(pmd_t pmd)
  967. {
  968. if (pmd_large(pmd)) {
  969. pmd_val(pmd) &= ~_SEGMENT_ENTRY_DIRTY;
  970. pmd_val(pmd) |= _SEGMENT_ENTRY_PROTECT;
  971. }
  972. return pmd;
  973. }
  974. static inline pmd_t pmd_mkdirty(pmd_t pmd)
  975. {
  976. if (pmd_large(pmd)) {
  977. pmd_val(pmd) |= _SEGMENT_ENTRY_DIRTY |
  978. _SEGMENT_ENTRY_SOFT_DIRTY;
  979. if (pmd_val(pmd) & _SEGMENT_ENTRY_WRITE)
  980. pmd_val(pmd) &= ~_SEGMENT_ENTRY_PROTECT;
  981. }
  982. return pmd;
  983. }
  984. static inline pmd_t pmd_mkyoung(pmd_t pmd)
  985. {
  986. if (pmd_large(pmd)) {
  987. pmd_val(pmd) |= _SEGMENT_ENTRY_YOUNG;
  988. if (pmd_val(pmd) & _SEGMENT_ENTRY_READ)
  989. pmd_val(pmd) &= ~_SEGMENT_ENTRY_INVALID;
  990. }
  991. return pmd;
  992. }
  993. static inline pmd_t pmd_mkold(pmd_t pmd)
  994. {
  995. if (pmd_large(pmd)) {
  996. pmd_val(pmd) &= ~_SEGMENT_ENTRY_YOUNG;
  997. pmd_val(pmd) |= _SEGMENT_ENTRY_INVALID;
  998. }
  999. return pmd;
  1000. }
  1001. static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
  1002. {
  1003. if (pmd_large(pmd)) {
  1004. pmd_val(pmd) &= _SEGMENT_ENTRY_ORIGIN_LARGE |
  1005. _SEGMENT_ENTRY_DIRTY | _SEGMENT_ENTRY_YOUNG |
  1006. _SEGMENT_ENTRY_LARGE | _SEGMENT_ENTRY_SOFT_DIRTY;
  1007. pmd_val(pmd) |= massage_pgprot_pmd(newprot);
  1008. if (!(pmd_val(pmd) & _SEGMENT_ENTRY_DIRTY))
  1009. pmd_val(pmd) |= _SEGMENT_ENTRY_PROTECT;
  1010. if (!(pmd_val(pmd) & _SEGMENT_ENTRY_YOUNG))
  1011. pmd_val(pmd) |= _SEGMENT_ENTRY_INVALID;
  1012. return pmd;
  1013. }
  1014. pmd_val(pmd) &= _SEGMENT_ENTRY_ORIGIN;
  1015. pmd_val(pmd) |= massage_pgprot_pmd(newprot);
  1016. return pmd;
  1017. }
  1018. static inline pmd_t mk_pmd_phys(unsigned long physpage, pgprot_t pgprot)
  1019. {
  1020. pmd_t __pmd;
  1021. pmd_val(__pmd) = physpage + massage_pgprot_pmd(pgprot);
  1022. return __pmd;
  1023. }
  1024. #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLB_PAGE */
  1025. static inline void __pmdp_csp(pmd_t *pmdp)
  1026. {
  1027. register unsigned long reg2 asm("2") = pmd_val(*pmdp);
  1028. register unsigned long reg3 asm("3") = pmd_val(*pmdp) |
  1029. _SEGMENT_ENTRY_INVALID;
  1030. register unsigned long reg4 asm("4") = ((unsigned long) pmdp) + 5;
  1031. asm volatile(
  1032. " csp %1,%3"
  1033. : "=m" (*pmdp)
  1034. : "d" (reg2), "d" (reg3), "d" (reg4), "m" (*pmdp) : "cc");
  1035. }
  1036. static inline void __pmdp_idte(unsigned long address, pmd_t *pmdp)
  1037. {
  1038. unsigned long sto;
  1039. sto = (unsigned long) pmdp - pmd_index(address) * sizeof(pmd_t);
  1040. asm volatile(
  1041. " .insn rrf,0xb98e0000,%2,%3,0,0"
  1042. : "=m" (*pmdp)
  1043. : "m" (*pmdp), "a" (sto), "a" ((address & HPAGE_MASK))
  1044. : "cc" );
  1045. }
  1046. static inline void __pmdp_idte_local(unsigned long address, pmd_t *pmdp)
  1047. {
  1048. unsigned long sto;
  1049. sto = (unsigned long) pmdp - pmd_index(address) * sizeof(pmd_t);
  1050. asm volatile(
  1051. " .insn rrf,0xb98e0000,%2,%3,0,1"
  1052. : "=m" (*pmdp)
  1053. : "m" (*pmdp), "a" (sto), "a" ((address & HPAGE_MASK))
  1054. : "cc" );
  1055. }
  1056. static inline void pmdp_flush_direct(struct mm_struct *mm,
  1057. unsigned long address, pmd_t *pmdp)
  1058. {
  1059. int active, count;
  1060. if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
  1061. return;
  1062. if (!MACHINE_HAS_IDTE) {
  1063. __pmdp_csp(pmdp);
  1064. return;
  1065. }
  1066. active = (mm == current->active_mm) ? 1 : 0;
  1067. count = atomic_add_return(0x10000, &mm->context.attach_count);
  1068. if (MACHINE_HAS_TLB_LC && (count & 0xffff) <= active &&
  1069. cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
  1070. __pmdp_idte_local(address, pmdp);
  1071. else
  1072. __pmdp_idte(address, pmdp);
  1073. atomic_sub(0x10000, &mm->context.attach_count);
  1074. }
  1075. static inline void pmdp_flush_lazy(struct mm_struct *mm,
  1076. unsigned long address, pmd_t *pmdp)
  1077. {
  1078. int active, count;
  1079. if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
  1080. return;
  1081. active = (mm == current->active_mm) ? 1 : 0;
  1082. count = atomic_add_return(0x10000, &mm->context.attach_count);
  1083. if ((count & 0xffff) <= active) {
  1084. pmd_val(*pmdp) |= _SEGMENT_ENTRY_INVALID;
  1085. mm->context.flush_mm = 1;
  1086. } else if (MACHINE_HAS_IDTE)
  1087. __pmdp_idte(address, pmdp);
  1088. else
  1089. __pmdp_csp(pmdp);
  1090. atomic_sub(0x10000, &mm->context.attach_count);
  1091. }
  1092. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1093. #define __HAVE_ARCH_PGTABLE_DEPOSIT
  1094. extern void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  1095. pgtable_t pgtable);
  1096. #define __HAVE_ARCH_PGTABLE_WITHDRAW
  1097. extern pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp);
  1098. static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
  1099. pmd_t *pmdp, pmd_t entry)
  1100. {
  1101. *pmdp = entry;
  1102. }
  1103. static inline pmd_t pmd_mkhuge(pmd_t pmd)
  1104. {
  1105. pmd_val(pmd) |= _SEGMENT_ENTRY_LARGE;
  1106. pmd_val(pmd) |= _SEGMENT_ENTRY_YOUNG;
  1107. pmd_val(pmd) |= _SEGMENT_ENTRY_PROTECT;
  1108. return pmd;
  1109. }
  1110. #define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
  1111. static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
  1112. unsigned long address, pmd_t *pmdp)
  1113. {
  1114. pmd_t pmd;
  1115. pmd = *pmdp;
  1116. pmdp_flush_direct(vma->vm_mm, address, pmdp);
  1117. *pmdp = pmd_mkold(pmd);
  1118. return pmd_young(pmd);
  1119. }
  1120. #define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR
  1121. static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
  1122. unsigned long address, pmd_t *pmdp)
  1123. {
  1124. pmd_t pmd = *pmdp;
  1125. pmdp_flush_direct(mm, address, pmdp);
  1126. pmd_clear(pmdp);
  1127. return pmd;
  1128. }
  1129. #define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR_FULL
  1130. static inline pmd_t pmdp_huge_get_and_clear_full(struct mm_struct *mm,
  1131. unsigned long address,
  1132. pmd_t *pmdp, int full)
  1133. {
  1134. pmd_t pmd = *pmdp;
  1135. if (!full)
  1136. pmdp_flush_lazy(mm, address, pmdp);
  1137. pmd_clear(pmdp);
  1138. return pmd;
  1139. }
  1140. #define __HAVE_ARCH_PMDP_HUGE_CLEAR_FLUSH
  1141. static inline pmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma,
  1142. unsigned long address, pmd_t *pmdp)
  1143. {
  1144. return pmdp_huge_get_and_clear(vma->vm_mm, address, pmdp);
  1145. }
  1146. #define __HAVE_ARCH_PMDP_INVALIDATE
  1147. static inline void pmdp_invalidate(struct vm_area_struct *vma,
  1148. unsigned long address, pmd_t *pmdp)
  1149. {
  1150. pmdp_flush_direct(vma->vm_mm, address, pmdp);
  1151. }
  1152. #define __HAVE_ARCH_PMDP_SET_WRPROTECT
  1153. static inline void pmdp_set_wrprotect(struct mm_struct *mm,
  1154. unsigned long address, pmd_t *pmdp)
  1155. {
  1156. pmd_t pmd = *pmdp;
  1157. if (pmd_write(pmd)) {
  1158. pmdp_flush_direct(mm, address, pmdp);
  1159. set_pmd_at(mm, address, pmdp, pmd_wrprotect(pmd));
  1160. }
  1161. }
  1162. static inline pmd_t pmdp_collapse_flush(struct vm_area_struct *vma,
  1163. unsigned long address,
  1164. pmd_t *pmdp)
  1165. {
  1166. return pmdp_huge_get_and_clear(vma->vm_mm, address, pmdp);
  1167. }
  1168. #define pmdp_collapse_flush pmdp_collapse_flush
  1169. #define pfn_pmd(pfn, pgprot) mk_pmd_phys(__pa((pfn) << PAGE_SHIFT), (pgprot))
  1170. #define mk_pmd(page, pgprot) pfn_pmd(page_to_pfn(page), (pgprot))
  1171. static inline int pmd_trans_huge(pmd_t pmd)
  1172. {
  1173. return pmd_val(pmd) & _SEGMENT_ENTRY_LARGE;
  1174. }
  1175. static inline int has_transparent_hugepage(void)
  1176. {
  1177. return MACHINE_HAS_HPAGE ? 1 : 0;
  1178. }
  1179. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  1180. /*
  1181. * 64 bit swap entry format:
  1182. * A page-table entry has some bits we have to treat in a special way.
  1183. * Bits 52 and bit 55 have to be zero, otherwise a specification
  1184. * exception will occur instead of a page translation exception. The
  1185. * specification exception has the bad habit not to store necessary
  1186. * information in the lowcore.
  1187. * Bits 54 and 63 are used to indicate the page type.
  1188. * A swap pte is indicated by bit pattern (pte & 0x201) == 0x200
  1189. * This leaves the bits 0-51 and bits 56-62 to store type and offset.
  1190. * We use the 5 bits from 57-61 for the type and the 52 bits from 0-51
  1191. * for the offset.
  1192. * | offset |01100|type |00|
  1193. * |0000000000111111111122222222223333333333444444444455|55555|55566|66|
  1194. * |0123456789012345678901234567890123456789012345678901|23456|78901|23|
  1195. */
  1196. #define __SWP_OFFSET_MASK ((1UL << 52) - 1)
  1197. #define __SWP_OFFSET_SHIFT 12
  1198. #define __SWP_TYPE_MASK ((1UL << 5) - 1)
  1199. #define __SWP_TYPE_SHIFT 2
  1200. static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset)
  1201. {
  1202. pte_t pte;
  1203. pte_val(pte) = _PAGE_INVALID | _PAGE_PROTECT;
  1204. pte_val(pte) |= (offset & __SWP_OFFSET_MASK) << __SWP_OFFSET_SHIFT;
  1205. pte_val(pte) |= (type & __SWP_TYPE_MASK) << __SWP_TYPE_SHIFT;
  1206. return pte;
  1207. }
  1208. static inline unsigned long __swp_type(swp_entry_t entry)
  1209. {
  1210. return (entry.val >> __SWP_TYPE_SHIFT) & __SWP_TYPE_MASK;
  1211. }
  1212. static inline unsigned long __swp_offset(swp_entry_t entry)
  1213. {
  1214. return (entry.val >> __SWP_OFFSET_SHIFT) & __SWP_OFFSET_MASK;
  1215. }
  1216. static inline swp_entry_t __swp_entry(unsigned long type, unsigned long offset)
  1217. {
  1218. return (swp_entry_t) { pte_val(mk_swap_pte(type, offset)) };
  1219. }
  1220. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  1221. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  1222. #endif /* !__ASSEMBLY__ */
  1223. #define kern_addr_valid(addr) (1)
  1224. extern int vmem_add_mapping(unsigned long start, unsigned long size);
  1225. extern int vmem_remove_mapping(unsigned long start, unsigned long size);
  1226. extern int s390_enable_sie(void);
  1227. extern int s390_enable_skey(void);
  1228. extern void s390_reset_cmma(struct mm_struct *mm);
  1229. /* s390 has a private copy of get unmapped area to deal with cache synonyms */
  1230. #define HAVE_ARCH_UNMAPPED_AREA
  1231. #define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
  1232. /*
  1233. * No page table caches to initialise
  1234. */
  1235. static inline void pgtable_cache_init(void) { }
  1236. static inline void check_pgt_cache(void) { }
  1237. #include <asm-generic/pgtable.h>
  1238. #endif /* _S390_PAGE_H */