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