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