pgtable.h 28 KB

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