pgtable.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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. #include <asm-generic/4level-fixup.h>
  15. /*
  16. * The Linux memory management assumes a three-level page table setup. For
  17. * s390 31 bit we "fold" the mid level into the top-level page table, so
  18. * that we physically have the same two-level page table as the s390 mmu
  19. * expects in 31 bit mode. For s390 64 bit we use three of the five levels
  20. * the hardware provides (region first and region second tables are not
  21. * used).
  22. *
  23. * The "pgd_xxx()" functions are trivial for a folded two-level
  24. * setup: the pgd is never bad, and a pmd always exists (as it's folded
  25. * into the pgd entry)
  26. *
  27. * This file contains the functions and defines necessary to modify and use
  28. * the S390 page table tree.
  29. */
  30. #ifndef __ASSEMBLY__
  31. #include <linux/mm_types.h>
  32. #include <asm/bug.h>
  33. #include <asm/processor.h>
  34. struct vm_area_struct; /* forward declaration (include/linux/mm.h) */
  35. struct mm_struct;
  36. extern pgd_t swapper_pg_dir[] __attribute__ ((aligned (4096)));
  37. extern void paging_init(void);
  38. extern void vmem_map_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, pte) 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 char empty_zero_page[PAGE_SIZE];
  49. #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
  50. #endif /* !__ASSEMBLY__ */
  51. /*
  52. * PMD_SHIFT determines the size of the area a second-level page
  53. * table can map
  54. * PGDIR_SHIFT determines what a third-level page table entry can map
  55. */
  56. #ifndef __s390x__
  57. # define PMD_SHIFT 22
  58. # define PGDIR_SHIFT 22
  59. #else /* __s390x__ */
  60. # define PMD_SHIFT 21
  61. # define PGDIR_SHIFT 31
  62. #endif /* __s390x__ */
  63. #define PMD_SIZE (1UL << PMD_SHIFT)
  64. #define PMD_MASK (~(PMD_SIZE-1))
  65. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  66. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  67. /*
  68. * entries per page directory level: the S390 is two-level, so
  69. * we don't really have any PMD directory physically.
  70. * for S390 segment-table entries are combined to one PGD
  71. * that leads to 1024 pte per pgd
  72. */
  73. #ifndef __s390x__
  74. # define PTRS_PER_PTE 1024
  75. # define PTRS_PER_PMD 1
  76. # define PTRS_PER_PGD 512
  77. #else /* __s390x__ */
  78. # define PTRS_PER_PTE 512
  79. # define PTRS_PER_PMD 1024
  80. # define PTRS_PER_PGD 2048
  81. #endif /* __s390x__ */
  82. #define FIRST_USER_ADDRESS 0
  83. #define pte_ERROR(e) \
  84. printk("%s:%d: bad pte %p.\n", __FILE__, __LINE__, (void *) pte_val(e))
  85. #define pmd_ERROR(e) \
  86. printk("%s:%d: bad pmd %p.\n", __FILE__, __LINE__, (void *) pmd_val(e))
  87. #define pgd_ERROR(e) \
  88. printk("%s:%d: bad pgd %p.\n", __FILE__, __LINE__, (void *) pgd_val(e))
  89. #ifndef __ASSEMBLY__
  90. /*
  91. * Just any arbitrary offset to the start of the vmalloc VM area: the
  92. * current 8MB value just means that there will be a 8MB "hole" after the
  93. * physical memory until the kernel virtual memory starts. That means that
  94. * any out-of-bounds memory accesses will hopefully be caught.
  95. * The vmalloc() routines leaves a hole of 4kB between each vmalloced
  96. * area for the same reason. ;)
  97. */
  98. extern unsigned long vmalloc_end;
  99. #define VMALLOC_OFFSET (8*1024*1024)
  100. #define VMALLOC_START (((unsigned long) high_memory + VMALLOC_OFFSET) \
  101. & ~(VMALLOC_OFFSET-1))
  102. #define VMALLOC_END vmalloc_end
  103. /*
  104. * We need some free virtual space to be able to do vmalloc.
  105. * VMALLOC_MIN_SIZE defines the minimum size of the vmalloc
  106. * area. On a machine with 2GB memory we make sure that we
  107. * have at least 128MB free space for vmalloc. On a machine
  108. * with 4TB we make sure we have at least 128GB.
  109. */
  110. #ifndef __s390x__
  111. #define VMALLOC_MIN_SIZE 0x8000000UL
  112. #define VMALLOC_END_INIT 0x80000000UL
  113. #else /* __s390x__ */
  114. #define VMALLOC_MIN_SIZE 0x2000000000UL
  115. #define VMALLOC_END_INIT 0x40000000000UL
  116. #endif /* __s390x__ */
  117. /*
  118. * A 31 bit pagetable entry of S390 has following format:
  119. * | PFRA | | OS |
  120. * 0 0IP0
  121. * 00000000001111111111222222222233
  122. * 01234567890123456789012345678901
  123. *
  124. * I Page-Invalid Bit: Page is not available for address-translation
  125. * P Page-Protection Bit: Store access not possible for page
  126. *
  127. * A 31 bit segmenttable entry of S390 has following format:
  128. * | P-table origin | |PTL
  129. * 0 IC
  130. * 00000000001111111111222222222233
  131. * 01234567890123456789012345678901
  132. *
  133. * I Segment-Invalid Bit: Segment is not available for address-translation
  134. * C Common-Segment Bit: Segment is not private (PoP 3-30)
  135. * PTL Page-Table-Length: Page-table length (PTL+1*16 entries -> up to 256)
  136. *
  137. * The 31 bit segmenttable origin of S390 has following format:
  138. *
  139. * |S-table origin | | STL |
  140. * X **GPS
  141. * 00000000001111111111222222222233
  142. * 01234567890123456789012345678901
  143. *
  144. * X Space-Switch event:
  145. * G Segment-Invalid Bit: *
  146. * P Private-Space Bit: Segment is not private (PoP 3-30)
  147. * S Storage-Alteration:
  148. * STL Segment-Table-Length: Segment-table length (STL+1*16 entries -> up to 2048)
  149. *
  150. * A 64 bit pagetable entry of S390 has following format:
  151. * | PFRA |0IP0| OS |
  152. * 0000000000111111111122222222223333333333444444444455555555556666
  153. * 0123456789012345678901234567890123456789012345678901234567890123
  154. *
  155. * I Page-Invalid Bit: Page is not available for address-translation
  156. * P Page-Protection Bit: Store access not possible for page
  157. *
  158. * A 64 bit segmenttable entry of S390 has following format:
  159. * | P-table origin | TT
  160. * 0000000000111111111122222222223333333333444444444455555555556666
  161. * 0123456789012345678901234567890123456789012345678901234567890123
  162. *
  163. * I Segment-Invalid Bit: Segment is not available for address-translation
  164. * C Common-Segment Bit: Segment is not private (PoP 3-30)
  165. * P Page-Protection Bit: Store access not possible for page
  166. * TT Type 00
  167. *
  168. * A 64 bit region table entry of S390 has following format:
  169. * | S-table origin | TF TTTL
  170. * 0000000000111111111122222222223333333333444444444455555555556666
  171. * 0123456789012345678901234567890123456789012345678901234567890123
  172. *
  173. * I Segment-Invalid Bit: Segment is not available for address-translation
  174. * TT Type 01
  175. * TF
  176. * TL Table lenght
  177. *
  178. * The 64 bit regiontable origin of S390 has following format:
  179. * | region table origon | DTTL
  180. * 0000000000111111111122222222223333333333444444444455555555556666
  181. * 0123456789012345678901234567890123456789012345678901234567890123
  182. *
  183. * X Space-Switch event:
  184. * G Segment-Invalid Bit:
  185. * P Private-Space Bit:
  186. * S Storage-Alteration:
  187. * R Real space
  188. * TL Table-Length:
  189. *
  190. * A storage key has the following format:
  191. * | ACC |F|R|C|0|
  192. * 0 3 4 5 6 7
  193. * ACC: access key
  194. * F : fetch protection bit
  195. * R : referenced bit
  196. * C : changed bit
  197. */
  198. /* Hardware bits in the page table entry */
  199. #define _PAGE_RO 0x200 /* HW read-only bit */
  200. #define _PAGE_INVALID 0x400 /* HW invalid bit */
  201. #define _PAGE_SWT 0x001 /* SW pte type bit t */
  202. #define _PAGE_SWX 0x002 /* SW pte type bit x */
  203. /* Six different types of pages. */
  204. #define _PAGE_TYPE_EMPTY 0x400
  205. #define _PAGE_TYPE_NONE 0x401
  206. #define _PAGE_TYPE_SWAP 0x403
  207. #define _PAGE_TYPE_FILE 0x601 /* bit 0x002 is used for offset !! */
  208. #define _PAGE_TYPE_RO 0x200
  209. #define _PAGE_TYPE_RW 0x000
  210. #define _PAGE_TYPE_EX_RO 0x202
  211. #define _PAGE_TYPE_EX_RW 0x002
  212. /*
  213. * PTE type bits are rather complicated. handle_pte_fault uses pte_present,
  214. * pte_none and pte_file to find out the pte type WITHOUT holding the page
  215. * table lock. ptep_clear_flush on the other hand uses ptep_clear_flush to
  216. * invalidate a given pte. ipte sets the hw invalid bit and clears all tlbs
  217. * for the page. The page table entry is set to _PAGE_TYPE_EMPTY afterwards.
  218. * This change is done while holding the lock, but the intermediate step
  219. * of a previously valid pte with the hw invalid bit set can be observed by
  220. * handle_pte_fault. That makes it necessary that all valid pte types with
  221. * the hw invalid bit set must be distinguishable from the four pte types
  222. * empty, none, swap and file.
  223. *
  224. * irxt ipte irxt
  225. * _PAGE_TYPE_EMPTY 1000 -> 1000
  226. * _PAGE_TYPE_NONE 1001 -> 1001
  227. * _PAGE_TYPE_SWAP 1011 -> 1011
  228. * _PAGE_TYPE_FILE 11?1 -> 11?1
  229. * _PAGE_TYPE_RO 0100 -> 1100
  230. * _PAGE_TYPE_RW 0000 -> 1000
  231. * _PAGE_TYPE_EX_RO 0110 -> 1110
  232. * _PAGE_TYPE_EX_RW 0010 -> 1010
  233. *
  234. * pte_none is true for bits combinations 1000, 1010, 1100, 1110
  235. * pte_present is true for bits combinations 0000, 0010, 0100, 0110, 1001
  236. * pte_file is true for bits combinations 1101, 1111
  237. * swap pte is 1011 and 0001, 0011, 0101, 0111 are invalid.
  238. */
  239. #ifndef __s390x__
  240. /* Bits in the segment table entry */
  241. #define _PAGE_TABLE_LEN 0xf /* only full page-tables */
  242. #define _PAGE_TABLE_COM 0x10 /* common page-table */
  243. #define _PAGE_TABLE_INV 0x20 /* invalid page-table */
  244. #define _SEG_PRESENT 0x001 /* Software (overlap with PTL) */
  245. /* Bits int the storage key */
  246. #define _PAGE_CHANGED 0x02 /* HW changed bit */
  247. #define _PAGE_REFERENCED 0x04 /* HW referenced bit */
  248. #define _USER_SEG_TABLE_LEN 0x7f /* user-segment-table up to 2 GB */
  249. #define _KERNEL_SEG_TABLE_LEN 0x7f /* kernel-segment-table up to 2 GB */
  250. /*
  251. * User and Kernel pagetables are identical
  252. */
  253. #define _PAGE_TABLE _PAGE_TABLE_LEN
  254. #define _KERNPG_TABLE _PAGE_TABLE_LEN
  255. /*
  256. * The Kernel segment-tables includes the User segment-table
  257. */
  258. #define _SEGMENT_TABLE (_USER_SEG_TABLE_LEN|0x80000000|0x100)
  259. #define _KERNSEG_TABLE _KERNEL_SEG_TABLE_LEN
  260. #define USER_STD_MASK 0x00000080UL
  261. #else /* __s390x__ */
  262. /* Bits in the segment table entry */
  263. #define _PMD_ENTRY_INV 0x20 /* invalid segment table entry */
  264. #define _PMD_ENTRY 0x00
  265. /* Bits in the region third table entry */
  266. #define _PGD_ENTRY_INV 0x20 /* invalid region table entry */
  267. #define _PGD_ENTRY 0x07
  268. /*
  269. * User and kernel page directory
  270. */
  271. #define _REGION_THIRD 0x4
  272. #define _REGION_THIRD_LEN 0x3
  273. #define _REGION_TABLE (_REGION_THIRD|_REGION_THIRD_LEN|0x40|0x100)
  274. #define _KERN_REGION_TABLE (_REGION_THIRD|_REGION_THIRD_LEN)
  275. #define USER_STD_MASK 0x0000000000000080UL
  276. /* Bits in the storage key */
  277. #define _PAGE_CHANGED 0x02 /* HW changed bit */
  278. #define _PAGE_REFERENCED 0x04 /* HW referenced bit */
  279. #endif /* __s390x__ */
  280. /*
  281. * Page protection definitions.
  282. */
  283. #define PAGE_NONE __pgprot(_PAGE_TYPE_NONE)
  284. #define PAGE_RO __pgprot(_PAGE_TYPE_RO)
  285. #define PAGE_RW __pgprot(_PAGE_TYPE_RW)
  286. #define PAGE_EX_RO __pgprot(_PAGE_TYPE_EX_RO)
  287. #define PAGE_EX_RW __pgprot(_PAGE_TYPE_EX_RW)
  288. #define PAGE_KERNEL PAGE_RW
  289. #define PAGE_COPY PAGE_RO
  290. /*
  291. * Dependent on the EXEC_PROTECT option s390 can do execute protection.
  292. * Write permission always implies read permission. In theory with a
  293. * primary/secondary page table execute only can be implemented but
  294. * it would cost an additional bit in the pte to distinguish all the
  295. * different pte types. To avoid that execute permission currently
  296. * implies read permission as well.
  297. */
  298. /*xwr*/
  299. #define __P000 PAGE_NONE
  300. #define __P001 PAGE_RO
  301. #define __P010 PAGE_RO
  302. #define __P011 PAGE_RO
  303. #define __P100 PAGE_EX_RO
  304. #define __P101 PAGE_EX_RO
  305. #define __P110 PAGE_EX_RO
  306. #define __P111 PAGE_EX_RO
  307. #define __S000 PAGE_NONE
  308. #define __S001 PAGE_RO
  309. #define __S010 PAGE_RW
  310. #define __S011 PAGE_RW
  311. #define __S100 PAGE_EX_RO
  312. #define __S101 PAGE_EX_RO
  313. #define __S110 PAGE_EX_RW
  314. #define __S111 PAGE_EX_RW
  315. #ifndef __s390x__
  316. # define PMD_SHADOW_SHIFT 1
  317. # define PGD_SHADOW_SHIFT 1
  318. #else /* __s390x__ */
  319. # define PMD_SHADOW_SHIFT 2
  320. # define PGD_SHADOW_SHIFT 2
  321. #endif /* __s390x__ */
  322. static inline struct page *get_shadow_page(struct page *page)
  323. {
  324. if (s390_noexec && !list_empty(&page->lru))
  325. return virt_to_page(page->lru.next);
  326. return NULL;
  327. }
  328. static inline pte_t *get_shadow_pte(pte_t *ptep)
  329. {
  330. unsigned long pteptr = (unsigned long) (ptep);
  331. if (s390_noexec) {
  332. unsigned long offset = pteptr & (PAGE_SIZE - 1);
  333. void *addr = (void *) (pteptr ^ offset);
  334. struct page *page = virt_to_page(addr);
  335. if (!list_empty(&page->lru))
  336. return (pte_t *) ((unsigned long) page->lru.next |
  337. offset);
  338. }
  339. return NULL;
  340. }
  341. static inline pmd_t *get_shadow_pmd(pmd_t *pmdp)
  342. {
  343. unsigned long pmdptr = (unsigned long) (pmdp);
  344. if (s390_noexec) {
  345. unsigned long offset = pmdptr &
  346. ((PAGE_SIZE << PMD_SHADOW_SHIFT) - 1);
  347. void *addr = (void *) (pmdptr ^ offset);
  348. struct page *page = virt_to_page(addr);
  349. if (!list_empty(&page->lru))
  350. return (pmd_t *) ((unsigned long) page->lru.next |
  351. offset);
  352. }
  353. return NULL;
  354. }
  355. static inline pgd_t *get_shadow_pgd(pgd_t *pgdp)
  356. {
  357. unsigned long pgdptr = (unsigned long) (pgdp);
  358. if (s390_noexec) {
  359. unsigned long offset = pgdptr &
  360. ((PAGE_SIZE << PGD_SHADOW_SHIFT) - 1);
  361. void *addr = (void *) (pgdptr ^ offset);
  362. struct page *page = virt_to_page(addr);
  363. if (!list_empty(&page->lru))
  364. return (pgd_t *) ((unsigned long) page->lru.next |
  365. offset);
  366. }
  367. return NULL;
  368. }
  369. /*
  370. * Certain architectures need to do special things when PTEs
  371. * within a page table are directly modified. Thus, the following
  372. * hook is made available.
  373. */
  374. static inline void set_pte(pte_t *pteptr, pte_t pteval)
  375. {
  376. pte_t *shadow_pte = get_shadow_pte(pteptr);
  377. *pteptr = pteval;
  378. if (shadow_pte) {
  379. if (!(pte_val(pteval) & _PAGE_INVALID) &&
  380. (pte_val(pteval) & _PAGE_SWX))
  381. pte_val(*shadow_pte) = pte_val(pteval) | _PAGE_RO;
  382. else
  383. pte_val(*shadow_pte) = _PAGE_TYPE_EMPTY;
  384. }
  385. }
  386. #define set_pte_at(mm,addr,ptep,pteval) set_pte(ptep,pteval)
  387. /*
  388. * pgd/pmd/pte query functions
  389. */
  390. #ifndef __s390x__
  391. static inline int pgd_present(pgd_t pgd) { return 1; }
  392. static inline int pgd_none(pgd_t pgd) { return 0; }
  393. static inline int pgd_bad(pgd_t pgd) { return 0; }
  394. static inline int pmd_present(pmd_t pmd) { return pmd_val(pmd) & _SEG_PRESENT; }
  395. static inline int pmd_none(pmd_t pmd) { return pmd_val(pmd) & _PAGE_TABLE_INV; }
  396. static inline int pmd_bad(pmd_t pmd)
  397. {
  398. return (pmd_val(pmd) & (~PAGE_MASK & ~_PAGE_TABLE_INV)) != _PAGE_TABLE;
  399. }
  400. #else /* __s390x__ */
  401. static inline int pgd_present(pgd_t pgd)
  402. {
  403. return (pgd_val(pgd) & ~PAGE_MASK) == _PGD_ENTRY;
  404. }
  405. static inline int pgd_none(pgd_t pgd)
  406. {
  407. return pgd_val(pgd) & _PGD_ENTRY_INV;
  408. }
  409. static inline int pgd_bad(pgd_t pgd)
  410. {
  411. return (pgd_val(pgd) & (~PAGE_MASK & ~_PGD_ENTRY_INV)) != _PGD_ENTRY;
  412. }
  413. static inline int pmd_present(pmd_t pmd)
  414. {
  415. return (pmd_val(pmd) & ~PAGE_MASK) == _PMD_ENTRY;
  416. }
  417. static inline int pmd_none(pmd_t pmd)
  418. {
  419. return pmd_val(pmd) & _PMD_ENTRY_INV;
  420. }
  421. static inline int pmd_bad(pmd_t pmd)
  422. {
  423. return (pmd_val(pmd) & (~PAGE_MASK & ~_PMD_ENTRY_INV)) != _PMD_ENTRY;
  424. }
  425. #endif /* __s390x__ */
  426. static inline int pte_none(pte_t pte)
  427. {
  428. return (pte_val(pte) & _PAGE_INVALID) && !(pte_val(pte) & _PAGE_SWT);
  429. }
  430. static inline int pte_present(pte_t pte)
  431. {
  432. unsigned long mask = _PAGE_RO | _PAGE_INVALID | _PAGE_SWT | _PAGE_SWX;
  433. return (pte_val(pte) & mask) == _PAGE_TYPE_NONE ||
  434. (!(pte_val(pte) & _PAGE_INVALID) &&
  435. !(pte_val(pte) & _PAGE_SWT));
  436. }
  437. static inline int pte_file(pte_t pte)
  438. {
  439. unsigned long mask = _PAGE_RO | _PAGE_INVALID | _PAGE_SWT;
  440. return (pte_val(pte) & mask) == _PAGE_TYPE_FILE;
  441. }
  442. #define pte_same(a,b) (pte_val(a) == pte_val(b))
  443. /*
  444. * query functions pte_write/pte_dirty/pte_young only work if
  445. * pte_present() is true. Undefined behaviour if not..
  446. */
  447. static inline int pte_write(pte_t pte)
  448. {
  449. return (pte_val(pte) & _PAGE_RO) == 0;
  450. }
  451. static inline int pte_dirty(pte_t pte)
  452. {
  453. /* A pte is neither clean nor dirty on s/390. The dirty bit
  454. * is in the storage key. See page_test_and_clear_dirty for
  455. * details.
  456. */
  457. return 0;
  458. }
  459. static inline int pte_young(pte_t pte)
  460. {
  461. /* A pte is neither young nor old on s/390. The young bit
  462. * is in the storage key. See page_test_and_clear_young for
  463. * details.
  464. */
  465. return 0;
  466. }
  467. static inline int pte_read(pte_t pte)
  468. {
  469. /* All pages are readable since we don't use the fetch
  470. * protection bit in the storage key.
  471. */
  472. return 1;
  473. }
  474. /*
  475. * pgd/pmd/pte modification functions
  476. */
  477. #ifndef __s390x__
  478. static inline void pgd_clear(pgd_t * pgdp) { }
  479. static inline void pmd_clear_kernel(pmd_t * pmdp)
  480. {
  481. pmd_val(pmdp[0]) = _PAGE_TABLE_INV;
  482. pmd_val(pmdp[1]) = _PAGE_TABLE_INV;
  483. pmd_val(pmdp[2]) = _PAGE_TABLE_INV;
  484. pmd_val(pmdp[3]) = _PAGE_TABLE_INV;
  485. }
  486. static inline void pmd_clear(pmd_t * pmdp)
  487. {
  488. pmd_t *shadow_pmd = get_shadow_pmd(pmdp);
  489. pmd_clear_kernel(pmdp);
  490. if (shadow_pmd)
  491. pmd_clear_kernel(shadow_pmd);
  492. }
  493. #else /* __s390x__ */
  494. static inline void pgd_clear_kernel(pgd_t * pgdp)
  495. {
  496. pgd_val(*pgdp) = _PGD_ENTRY_INV | _PGD_ENTRY;
  497. }
  498. static inline void pgd_clear(pgd_t * pgdp)
  499. {
  500. pgd_t *shadow_pgd = get_shadow_pgd(pgdp);
  501. pgd_clear_kernel(pgdp);
  502. if (shadow_pgd)
  503. pgd_clear_kernel(shadow_pgd);
  504. }
  505. static inline void pmd_clear_kernel(pmd_t * pmdp)
  506. {
  507. pmd_val(*pmdp) = _PMD_ENTRY_INV | _PMD_ENTRY;
  508. pmd_val1(*pmdp) = _PMD_ENTRY_INV | _PMD_ENTRY;
  509. }
  510. static inline void pmd_clear(pmd_t * pmdp)
  511. {
  512. pmd_t *shadow_pmd = get_shadow_pmd(pmdp);
  513. pmd_clear_kernel(pmdp);
  514. if (shadow_pmd)
  515. pmd_clear_kernel(shadow_pmd);
  516. }
  517. #endif /* __s390x__ */
  518. static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  519. {
  520. pte_t *shadow_pte = get_shadow_pte(ptep);
  521. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  522. if (shadow_pte)
  523. pte_val(*shadow_pte) = _PAGE_TYPE_EMPTY;
  524. }
  525. /*
  526. * The following pte modification functions only work if
  527. * pte_present() is true. Undefined behaviour if not..
  528. */
  529. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  530. {
  531. pte_val(pte) &= PAGE_MASK;
  532. pte_val(pte) |= pgprot_val(newprot);
  533. return pte;
  534. }
  535. static inline pte_t pte_wrprotect(pte_t pte)
  536. {
  537. /* Do not clobber _PAGE_TYPE_NONE pages! */
  538. if (!(pte_val(pte) & _PAGE_INVALID))
  539. pte_val(pte) |= _PAGE_RO;
  540. return pte;
  541. }
  542. static inline pte_t pte_mkwrite(pte_t pte)
  543. {
  544. pte_val(pte) &= ~_PAGE_RO;
  545. return pte;
  546. }
  547. static inline pte_t pte_mkclean(pte_t pte)
  548. {
  549. /* The only user of pte_mkclean is the fork() code.
  550. We must *not* clear the *physical* page dirty bit
  551. just because fork() wants to clear the dirty bit in
  552. *one* of the page's mappings. So we just do nothing. */
  553. return pte;
  554. }
  555. static inline pte_t pte_mkdirty(pte_t pte)
  556. {
  557. /* We do not explicitly set the dirty bit because the
  558. * sske instruction is slow. It is faster to let the
  559. * next instruction set the dirty bit.
  560. */
  561. return pte;
  562. }
  563. static inline pte_t pte_mkold(pte_t pte)
  564. {
  565. /* S/390 doesn't keep its dirty/referenced bit in the pte.
  566. * There is no point in clearing the real referenced bit.
  567. */
  568. return pte;
  569. }
  570. static inline pte_t pte_mkyoung(pte_t pte)
  571. {
  572. /* S/390 doesn't keep its dirty/referenced bit in the pte.
  573. * There is no point in setting the real referenced bit.
  574. */
  575. return pte;
  576. }
  577. static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
  578. {
  579. return 0;
  580. }
  581. static inline int
  582. ptep_clear_flush_young(struct vm_area_struct *vma,
  583. unsigned long address, pte_t *ptep)
  584. {
  585. /* No need to flush TLB; bits are in storage key */
  586. return ptep_test_and_clear_young(vma, address, ptep);
  587. }
  588. static inline int ptep_test_and_clear_dirty(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
  589. {
  590. return 0;
  591. }
  592. static inline int
  593. ptep_clear_flush_dirty(struct vm_area_struct *vma,
  594. unsigned long address, pte_t *ptep)
  595. {
  596. /* No need to flush TLB; bits are in storage key */
  597. return ptep_test_and_clear_dirty(vma, address, ptep);
  598. }
  599. static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  600. {
  601. pte_t pte = *ptep;
  602. pte_clear(mm, addr, ptep);
  603. return pte;
  604. }
  605. static inline void __ptep_ipte(unsigned long address, pte_t *ptep)
  606. {
  607. if (!(pte_val(*ptep) & _PAGE_INVALID)) {
  608. #ifndef __s390x__
  609. /* S390 has 1mb segments, we are emulating 4MB segments */
  610. pte_t *pto = (pte_t *) (((unsigned long) ptep) & 0x7ffffc00);
  611. #else
  612. /* ipte in zarch mode can do the math */
  613. pte_t *pto = ptep;
  614. #endif
  615. asm volatile(
  616. " ipte %2,%3"
  617. : "=m" (*ptep) : "m" (*ptep),
  618. "a" (pto), "a" (address));
  619. }
  620. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  621. }
  622. static inline pte_t
  623. ptep_clear_flush(struct vm_area_struct *vma,
  624. unsigned long address, pte_t *ptep)
  625. {
  626. pte_t pte = *ptep;
  627. pte_t *shadow_pte = get_shadow_pte(ptep);
  628. __ptep_ipte(address, ptep);
  629. if (shadow_pte)
  630. __ptep_ipte(address, shadow_pte);
  631. return pte;
  632. }
  633. static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  634. {
  635. pte_t old_pte = *ptep;
  636. set_pte_at(mm, addr, ptep, pte_wrprotect(old_pte));
  637. }
  638. static inline void
  639. ptep_establish(struct vm_area_struct *vma,
  640. unsigned long address, pte_t *ptep,
  641. pte_t entry)
  642. {
  643. ptep_clear_flush(vma, address, ptep);
  644. set_pte(ptep, entry);
  645. }
  646. #define ptep_set_access_flags(__vma, __address, __ptep, __entry, __dirty) \
  647. ptep_establish(__vma, __address, __ptep, __entry)
  648. /*
  649. * Test and clear dirty bit in storage key.
  650. * We can't clear the changed bit atomically. This is a potential
  651. * race against modification of the referenced bit. This function
  652. * should therefore only be called if it is not mapped in any
  653. * address space.
  654. */
  655. static inline int page_test_dirty(struct page *page)
  656. {
  657. return (page_get_storage_key(page_to_phys(page)) & _PAGE_CHANGED) != 0;
  658. }
  659. static inline void page_clear_dirty(struct page *page)
  660. {
  661. page_set_storage_key(page_to_phys(page), PAGE_DEFAULT_KEY);
  662. }
  663. /*
  664. * Test and clear referenced bit in storage key.
  665. */
  666. static inline int page_test_and_clear_young(struct page *page)
  667. {
  668. unsigned long physpage = page_to_phys(page);
  669. int ccode;
  670. asm volatile(
  671. " rrbe 0,%1\n"
  672. " ipm %0\n"
  673. " srl %0,28\n"
  674. : "=d" (ccode) : "a" (physpage) : "cc" );
  675. return ccode & 2;
  676. }
  677. /*
  678. * Conversion functions: convert a page and protection to a page entry,
  679. * and a page entry and page directory to the page they refer to.
  680. */
  681. static inline pte_t mk_pte_phys(unsigned long physpage, pgprot_t pgprot)
  682. {
  683. pte_t __pte;
  684. pte_val(__pte) = physpage + pgprot_val(pgprot);
  685. return __pte;
  686. }
  687. static inline pte_t mk_pte(struct page *page, pgprot_t pgprot)
  688. {
  689. unsigned long physpage = page_to_phys(page);
  690. return mk_pte_phys(physpage, pgprot);
  691. }
  692. static inline pte_t pfn_pte(unsigned long pfn, pgprot_t pgprot)
  693. {
  694. unsigned long physpage = __pa((pfn) << PAGE_SHIFT);
  695. return mk_pte_phys(physpage, pgprot);
  696. }
  697. #ifdef __s390x__
  698. static inline pmd_t pfn_pmd(unsigned long pfn, pgprot_t pgprot)
  699. {
  700. unsigned long physpage = __pa((pfn) << PAGE_SHIFT);
  701. return __pmd(physpage + pgprot_val(pgprot));
  702. }
  703. #endif /* __s390x__ */
  704. #define pte_pfn(x) (pte_val(x) >> PAGE_SHIFT)
  705. #define pte_page(x) pfn_to_page(pte_pfn(x))
  706. #define pmd_page_vaddr(pmd) (pmd_val(pmd) & PAGE_MASK)
  707. #define pmd_page(pmd) pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT)
  708. #define pgd_page_vaddr(pgd) (pgd_val(pgd) & PAGE_MASK)
  709. #define pgd_page(pgd) pfn_to_page(pgd_val(pgd) >> PAGE_SHIFT)
  710. /* to find an entry in a page-table-directory */
  711. #define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
  712. #define pgd_offset(mm, address) ((mm)->pgd+pgd_index(address))
  713. /* to find an entry in a kernel page-table-directory */
  714. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  715. #ifndef __s390x__
  716. /* Find an entry in the second-level page table.. */
  717. static inline pmd_t * pmd_offset(pgd_t * dir, unsigned long address)
  718. {
  719. return (pmd_t *) dir;
  720. }
  721. #else /* __s390x__ */
  722. /* Find an entry in the second-level page table.. */
  723. #define pmd_index(address) (((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
  724. #define pmd_offset(dir,addr) \
  725. ((pmd_t *) pgd_page_vaddr(*(dir)) + pmd_index(addr))
  726. #endif /* __s390x__ */
  727. /* Find an entry in the third-level page table.. */
  728. #define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE-1))
  729. #define pte_offset_kernel(pmd, address) \
  730. ((pte_t *) pmd_page_vaddr(*(pmd)) + pte_index(address))
  731. #define pte_offset_map(pmd, address) pte_offset_kernel(pmd, address)
  732. #define pte_offset_map_nested(pmd, address) pte_offset_kernel(pmd, address)
  733. #define pte_unmap(pte) do { } while (0)
  734. #define pte_unmap_nested(pte) do { } while (0)
  735. /*
  736. * 31 bit swap entry format:
  737. * A page-table entry has some bits we have to treat in a special way.
  738. * Bits 0, 20 and bit 23 have to be zero, otherwise an specification
  739. * exception will occur instead of a page translation exception. The
  740. * specifiation exception has the bad habit not to store necessary
  741. * information in the lowcore.
  742. * Bit 21 and bit 22 are the page invalid bit and the page protection
  743. * bit. We set both to indicate a swapped page.
  744. * Bit 30 and 31 are used to distinguish the different page types. For
  745. * a swapped page these bits need to be zero.
  746. * This leaves the bits 1-19 and bits 24-29 to store type and offset.
  747. * We use the 5 bits from 25-29 for the type and the 20 bits from 1-19
  748. * plus 24 for the offset.
  749. * 0| offset |0110|o|type |00|
  750. * 0 0000000001111111111 2222 2 22222 33
  751. * 0 1234567890123456789 0123 4 56789 01
  752. *
  753. * 64 bit swap entry format:
  754. * A page-table entry has some bits we have to treat in a special way.
  755. * Bits 52 and bit 55 have to be zero, otherwise an specification
  756. * exception will occur instead of a page translation exception. The
  757. * specifiation exception has the bad habit not to store necessary
  758. * information in the lowcore.
  759. * Bit 53 and bit 54 are the page invalid bit and the page protection
  760. * bit. We set both to indicate a swapped page.
  761. * Bit 62 and 63 are used to distinguish the different page types. For
  762. * a swapped page these bits need to be zero.
  763. * This leaves the bits 0-51 and bits 56-61 to store type and offset.
  764. * We use the 5 bits from 57-61 for the type and the 53 bits from 0-51
  765. * plus 56 for the offset.
  766. * | offset |0110|o|type |00|
  767. * 0000000000111111111122222222223333333333444444444455 5555 5 55566 66
  768. * 0123456789012345678901234567890123456789012345678901 2345 6 78901 23
  769. */
  770. #ifndef __s390x__
  771. #define __SWP_OFFSET_MASK (~0UL >> 12)
  772. #else
  773. #define __SWP_OFFSET_MASK (~0UL >> 11)
  774. #endif
  775. static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset)
  776. {
  777. pte_t pte;
  778. offset &= __SWP_OFFSET_MASK;
  779. pte_val(pte) = _PAGE_TYPE_SWAP | ((type & 0x1f) << 2) |
  780. ((offset & 1UL) << 7) | ((offset & ~1UL) << 11);
  781. return pte;
  782. }
  783. #define __swp_type(entry) (((entry).val >> 2) & 0x1f)
  784. #define __swp_offset(entry) (((entry).val >> 11) | (((entry).val >> 7) & 1))
  785. #define __swp_entry(type,offset) ((swp_entry_t) { pte_val(mk_swap_pte((type),(offset))) })
  786. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  787. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  788. #ifndef __s390x__
  789. # define PTE_FILE_MAX_BITS 26
  790. #else /* __s390x__ */
  791. # define PTE_FILE_MAX_BITS 59
  792. #endif /* __s390x__ */
  793. #define pte_to_pgoff(__pte) \
  794. ((((__pte).pte >> 12) << 7) + (((__pte).pte >> 1) & 0x7f))
  795. #define pgoff_to_pte(__off) \
  796. ((pte_t) { ((((__off) & 0x7f) << 1) + (((__off) >> 7) << 12)) \
  797. | _PAGE_TYPE_FILE })
  798. #endif /* !__ASSEMBLY__ */
  799. #define kern_addr_valid(addr) (1)
  800. extern int add_shared_memory(unsigned long start, unsigned long size);
  801. extern int remove_shared_memory(unsigned long start, unsigned long size);
  802. /*
  803. * No page table caches to initialise
  804. */
  805. #define pgtable_cache_init() do { } while (0)
  806. #define __HAVE_ARCH_MEMMAP_INIT
  807. extern void memmap_init(unsigned long, int, unsigned long, unsigned long);
  808. #define __HAVE_ARCH_PTEP_ESTABLISH
  809. #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  810. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  811. #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  812. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_DIRTY
  813. #define __HAVE_ARCH_PTEP_CLEAR_DIRTY_FLUSH
  814. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  815. #define __HAVE_ARCH_PTEP_CLEAR_FLUSH
  816. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  817. #define __HAVE_ARCH_PTE_SAME
  818. #define __HAVE_ARCH_PAGE_TEST_DIRTY
  819. #define __HAVE_ARCH_PAGE_CLEAR_DIRTY
  820. #define __HAVE_ARCH_PAGE_TEST_AND_CLEAR_YOUNG
  821. #include <asm-generic/pgtable.h>
  822. #endif /* _S390_PAGE_H */