blob: 0eefb9e192d18898b48ef254df1008ff4480bf34 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * S390 version
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02003 * Copyright IBM Corp. 1999, 2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Author(s): Hartmut Penner (hp@de.ibm.com)
5 * Ulrich Weigand (weigand@de.ibm.com)
6 * Martin Schwidefsky (schwidefsky@de.ibm.com)
7 *
8 * Derived from "include/asm-i386/pgtable.h"
9 */
10
11#ifndef _ASM_S390_PGTABLE_H
12#define _ASM_S390_PGTABLE_H
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/*
15 * The Linux memory management assumes a three-level page table setup. For
16 * s390 31 bit we "fold" the mid level into the top-level page table, so
17 * that we physically have the same two-level page table as the s390 mmu
18 * expects in 31 bit mode. For s390 64 bit we use three of the five levels
19 * the hardware provides (region first and region second tables are not
20 * used).
21 *
22 * The "pgd_xxx()" functions are trivial for a folded two-level
23 * setup: the pgd is never bad, and a pmd always exists (as it's folded
24 * into the pgd entry)
25 *
26 * This file contains the functions and defines necessary to modify and use
27 * the S390 page table tree.
28 */
29#ifndef __ASSEMBLY__
Heiko Carstens9789db02008-07-14 09:59:11 +020030#include <linux/sched.h>
Heiko Carstens2dcea572006-09-29 01:58:41 -070031#include <linux/mm_types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/bug.h>
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +020033#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035extern pgd_t swapper_pg_dir[] __attribute__ ((aligned (4096)));
36extern void paging_init(void);
Heiko Carstens2b67fc42007-02-05 21:16:47 +010037extern void vmem_map_init(void);
Martin Schwidefsky92f842e2010-10-25 16:10:13 +020038extern void fault_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * The S390 doesn't have any external MMU info: the kernel page
42 * tables contain all the necessary information.
43 */
Russell King4b3073e2009-12-18 16:40:18 +000044#define update_mmu_cache(vma, address, ptep) do { } while (0)
David Millerb113da62012-10-08 16:34:25 -070045#define update_mmu_cache_pmd(vma, address, ptep) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/*
Martin Schwidefsky238ec4e2010-10-25 16:10:07 +020048 * ZERO_PAGE is a global shared page that is always zero; used
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * for zero-mapped memory areas etc..
50 */
Martin Schwidefsky238ec4e2010-10-25 16:10:07 +020051
52extern unsigned long empty_zero_page;
53extern unsigned long zero_page_mask;
54
55#define ZERO_PAGE(vaddr) \
56 (virt_to_page((void *)(empty_zero_page + \
57 (((unsigned long)(vaddr)) &zero_page_mask))))
58
59#define is_zero_pfn is_zero_pfn
60static inline int is_zero_pfn(unsigned long pfn)
61{
62 extern unsigned long zero_pfn;
63 unsigned long offset_from_zero_pfn = pfn - zero_pfn;
64 return offset_from_zero_pfn <= (zero_page_mask >> PAGE_SHIFT);
65}
66
67#define my_zero_pfn(addr) page_to_pfn(ZERO_PAGE(addr))
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#endif /* !__ASSEMBLY__ */
70
71/*
72 * PMD_SHIFT determines the size of the area a second-level page
73 * table can map
74 * PGDIR_SHIFT determines what a third-level page table entry can map
75 */
Heiko Carstensf4815ac2012-05-23 16:24:51 +020076#ifndef CONFIG_64BIT
Martin Schwidefsky146e4b32008-02-09 18:24:35 +010077# define PMD_SHIFT 20
78# define PUD_SHIFT 20
79# define PGDIR_SHIFT 20
Heiko Carstensf4815ac2012-05-23 16:24:51 +020080#else /* CONFIG_64BIT */
Martin Schwidefsky146e4b32008-02-09 18:24:35 +010081# define PMD_SHIFT 20
Martin Schwidefsky190a1d72007-10-22 12:52:48 +020082# define PUD_SHIFT 31
Martin Schwidefsky5a216a22008-02-09 18:24:36 +010083# define PGDIR_SHIFT 42
Heiko Carstensf4815ac2012-05-23 16:24:51 +020084#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#define PMD_SIZE (1UL << PMD_SHIFT)
87#define PMD_MASK (~(PMD_SIZE-1))
Martin Schwidefsky190a1d72007-10-22 12:52:48 +020088#define PUD_SIZE (1UL << PUD_SHIFT)
89#define PUD_MASK (~(PUD_SIZE-1))
Martin Schwidefsky5a216a22008-02-09 18:24:36 +010090#define PGDIR_SIZE (1UL << PGDIR_SHIFT)
91#define PGDIR_MASK (~(PGDIR_SIZE-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93/*
94 * entries per page directory level: the S390 is two-level, so
95 * we don't really have any PMD directory physically.
96 * for S390 segment-table entries are combined to one PGD
97 * that leads to 1024 pte per pgd
98 */
Martin Schwidefsky146e4b32008-02-09 18:24:35 +010099#define PTRS_PER_PTE 256
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200100#ifndef CONFIG_64BIT
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100101#define PTRS_PER_PMD 1
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100102#define PTRS_PER_PUD 1
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200103#else /* CONFIG_64BIT */
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100104#define PTRS_PER_PMD 2048
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100105#define PTRS_PER_PUD 2048
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200106#endif /* CONFIG_64BIT */
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100107#define PTRS_PER_PGD 2048
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Hugh Dickinsd455a362005-04-19 13:29:23 -0700109#define FIRST_USER_ADDRESS 0
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#define pte_ERROR(e) \
112 printk("%s:%d: bad pte %p.\n", __FILE__, __LINE__, (void *) pte_val(e))
113#define pmd_ERROR(e) \
114 printk("%s:%d: bad pmd %p.\n", __FILE__, __LINE__, (void *) pmd_val(e))
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200115#define pud_ERROR(e) \
116 printk("%s:%d: bad pud %p.\n", __FILE__, __LINE__, (void *) pud_val(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#define pgd_ERROR(e) \
118 printk("%s:%d: bad pgd %p.\n", __FILE__, __LINE__, (void *) pgd_val(e))
119
120#ifndef __ASSEMBLY__
121/*
Heiko Carstensc972cc62012-10-05 16:52:18 +0200122 * The vmalloc and module area will always be on the topmost area of the kernel
123 * mapping. We reserve 96MB (31bit) / 128GB (64bit) for vmalloc and modules.
124 * On 64 bit kernels we have a 2GB area at the top of the vmalloc area where
125 * modules will reside. That makes sure that inter module branches always
126 * happen without trampolines and in addition the placement within a 2GB frame
127 * is branch prediction unit friendly.
Heiko Carstens8b62bc92006-12-04 15:40:56 +0100128 */
Heiko Carstens239a64252009-06-12 10:26:33 +0200129extern unsigned long VMALLOC_START;
Martin Schwidefsky14045eb2011-12-27 11:27:07 +0100130extern unsigned long VMALLOC_END;
131extern struct page *vmemmap;
Heiko Carstens239a64252009-06-12 10:26:33 +0200132
Martin Schwidefsky14045eb2011-12-27 11:27:07 +0100133#define VMEM_MAX_PHYS ((unsigned long) vmemmap)
Christian Borntraeger5fd9c6e2008-01-26 14:11:00 +0100134
Heiko Carstensc972cc62012-10-05 16:52:18 +0200135#ifdef CONFIG_64BIT
136extern unsigned long MODULES_VADDR;
137extern unsigned long MODULES_END;
138#define MODULES_VADDR MODULES_VADDR
139#define MODULES_END MODULES_END
140#define MODULES_LEN (1UL << 31)
141#endif
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/*
144 * A 31 bit pagetable entry of S390 has following format:
145 * | PFRA | | OS |
146 * 0 0IP0
147 * 00000000001111111111222222222233
148 * 01234567890123456789012345678901
149 *
150 * I Page-Invalid Bit: Page is not available for address-translation
151 * P Page-Protection Bit: Store access not possible for page
152 *
153 * A 31 bit segmenttable entry of S390 has following format:
154 * | P-table origin | |PTL
155 * 0 IC
156 * 00000000001111111111222222222233
157 * 01234567890123456789012345678901
158 *
159 * I Segment-Invalid Bit: Segment is not available for address-translation
160 * C Common-Segment Bit: Segment is not private (PoP 3-30)
161 * PTL Page-Table-Length: Page-table length (PTL+1*16 entries -> up to 256)
162 *
163 * The 31 bit segmenttable origin of S390 has following format:
164 *
165 * |S-table origin | | STL |
166 * X **GPS
167 * 00000000001111111111222222222233
168 * 01234567890123456789012345678901
169 *
170 * X Space-Switch event:
171 * G Segment-Invalid Bit: *
172 * P Private-Space Bit: Segment is not private (PoP 3-30)
173 * S Storage-Alteration:
174 * STL Segment-Table-Length: Segment-table length (STL+1*16 entries -> up to 2048)
175 *
176 * A 64 bit pagetable entry of S390 has following format:
Christian Borntraeger6a985c62009-12-07 12:52:11 +0100177 * | PFRA |0IPC| OS |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * 0000000000111111111122222222223333333333444444444455555555556666
179 * 0123456789012345678901234567890123456789012345678901234567890123
180 *
181 * I Page-Invalid Bit: Page is not available for address-translation
182 * P Page-Protection Bit: Store access not possible for page
Christian Borntraeger6a985c62009-12-07 12:52:11 +0100183 * C Change-bit override: HW is not required to set change bit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 *
185 * A 64 bit segmenttable entry of S390 has following format:
186 * | P-table origin | TT
187 * 0000000000111111111122222222223333333333444444444455555555556666
188 * 0123456789012345678901234567890123456789012345678901234567890123
189 *
190 * I Segment-Invalid Bit: Segment is not available for address-translation
191 * C Common-Segment Bit: Segment is not private (PoP 3-30)
192 * P Page-Protection Bit: Store access not possible for page
193 * TT Type 00
194 *
195 * A 64 bit region table entry of S390 has following format:
196 * | S-table origin | TF TTTL
197 * 0000000000111111111122222222223333333333444444444455555555556666
198 * 0123456789012345678901234567890123456789012345678901234567890123
199 *
200 * I Segment-Invalid Bit: Segment is not available for address-translation
201 * TT Type 01
202 * TF
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200203 * TL Table length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 *
205 * The 64 bit regiontable origin of S390 has following format:
206 * | region table origon | DTTL
207 * 0000000000111111111122222222223333333333444444444455555555556666
208 * 0123456789012345678901234567890123456789012345678901234567890123
209 *
210 * X Space-Switch event:
211 * G Segment-Invalid Bit:
212 * P Private-Space Bit:
213 * S Storage-Alteration:
214 * R Real space
215 * TL Table-Length:
216 *
217 * A storage key has the following format:
218 * | ACC |F|R|C|0|
219 * 0 3 4 5 6 7
220 * ACC: access key
221 * F : fetch protection bit
222 * R : referenced bit
223 * C : changed bit
224 */
225
226/* Hardware bits in the page table entry */
Christian Borntraeger6a985c62009-12-07 12:52:11 +0100227#define _PAGE_CO 0x100 /* HW Change-bit override */
Martin Schwidefsky83377482006-10-18 18:30:51 +0200228#define _PAGE_RO 0x200 /* HW read-only bit */
229#define _PAGE_INVALID 0x400 /* HW invalid bit */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200230
231/* Software bits in the page table entry */
Martin Schwidefsky83377482006-10-18 18:30:51 +0200232#define _PAGE_SWT 0x001 /* SW pte type bit t */
233#define _PAGE_SWX 0x002 /* SW pte type bit x */
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200234#define _PAGE_SWC 0x004 /* SW pte changed bit (for KVM) */
235#define _PAGE_SWR 0x008 /* SW pte referenced bit (for KVM) */
236#define _PAGE_SPECIAL 0x010 /* SW associated with special page */
Nick Piggina08cb622008-04-28 02:13:03 -0700237#define __HAVE_ARCH_PTE_SPECIAL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Nick Piggin138c9022008-07-08 11:31:06 +0200239/* Set of bits not changed in pte_modify */
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200240#define _PAGE_CHG_MASK (PAGE_MASK | _PAGE_SPECIAL | _PAGE_SWC | _PAGE_SWR)
Nick Piggin138c9022008-07-08 11:31:06 +0200241
Martin Schwidefsky83377482006-10-18 18:30:51 +0200242/* Six different types of pages. */
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200243#define _PAGE_TYPE_EMPTY 0x400
244#define _PAGE_TYPE_NONE 0x401
Martin Schwidefsky83377482006-10-18 18:30:51 +0200245#define _PAGE_TYPE_SWAP 0x403
246#define _PAGE_TYPE_FILE 0x601 /* bit 0x002 is used for offset !! */
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200247#define _PAGE_TYPE_RO 0x200
248#define _PAGE_TYPE_RW 0x000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Martin Schwidefsky83377482006-10-18 18:30:51 +0200250/*
Gerald Schaefer53492b12008-04-30 13:38:46 +0200251 * Only four types for huge pages, using the invalid bit and protection bit
252 * of a segment table entry.
253 */
254#define _HPAGE_TYPE_EMPTY 0x020 /* _SEGMENT_ENTRY_INV */
255#define _HPAGE_TYPE_NONE 0x220
256#define _HPAGE_TYPE_RO 0x200 /* _SEGMENT_ENTRY_RO */
257#define _HPAGE_TYPE_RW 0x000
258
259/*
Martin Schwidefsky83377482006-10-18 18:30:51 +0200260 * PTE type bits are rather complicated. handle_pte_fault uses pte_present,
261 * pte_none and pte_file to find out the pte type WITHOUT holding the page
262 * table lock. ptep_clear_flush on the other hand uses ptep_clear_flush to
263 * invalidate a given pte. ipte sets the hw invalid bit and clears all tlbs
264 * for the page. The page table entry is set to _PAGE_TYPE_EMPTY afterwards.
265 * This change is done while holding the lock, but the intermediate step
266 * of a previously valid pte with the hw invalid bit set can be observed by
267 * handle_pte_fault. That makes it necessary that all valid pte types with
268 * the hw invalid bit set must be distinguishable from the four pte types
269 * empty, none, swap and file.
270 *
271 * irxt ipte irxt
272 * _PAGE_TYPE_EMPTY 1000 -> 1000
273 * _PAGE_TYPE_NONE 1001 -> 1001
274 * _PAGE_TYPE_SWAP 1011 -> 1011
275 * _PAGE_TYPE_FILE 11?1 -> 11?1
276 * _PAGE_TYPE_RO 0100 -> 1100
277 * _PAGE_TYPE_RW 0000 -> 1000
278 *
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100279 * pte_none is true for bits combinations 1000, 1010, 1100, 1110
Martin Schwidefsky83377482006-10-18 18:30:51 +0200280 * pte_present is true for bits combinations 0000, 0010, 0100, 0110, 1001
281 * pte_file is true for bits combinations 1101, 1111
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100282 * swap pte is 1011 and 0001, 0011, 0101, 0111 are invalid.
Martin Schwidefsky83377482006-10-18 18:30:51 +0200283 */
284
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200285#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200287/* Bits in the segment table address-space-control-element */
288#define _ASCE_SPACE_SWITCH 0x80000000UL /* space switch event */
289#define _ASCE_ORIGIN_MASK 0x7ffff000UL /* segment table origin */
290#define _ASCE_PRIVATE_SPACE 0x100 /* private space control */
291#define _ASCE_ALT_EVENT 0x80 /* storage alteration event control */
292#define _ASCE_TABLE_LENGTH 0x7f /* 128 x 64 entries = 8k */
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/* Bits in the segment table entry */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200295#define _SEGMENT_ENTRY_ORIGIN 0x7fffffc0UL /* page table origin */
Martin Schwidefsky80217142010-10-25 16:10:11 +0200296#define _SEGMENT_ENTRY_RO 0x200 /* page protection bit */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200297#define _SEGMENT_ENTRY_INV 0x20 /* invalid segment table entry */
298#define _SEGMENT_ENTRY_COMMON 0x10 /* common segment bit */
299#define _SEGMENT_ENTRY_PTL 0x0f /* page table length */
300
301#define _SEGMENT_ENTRY (_SEGMENT_ENTRY_PTL)
302#define _SEGMENT_ENTRY_EMPTY (_SEGMENT_ENTRY_INV)
303
Martin Schwidefsky6c61cfe2011-06-06 14:14:42 +0200304/* Page status table bits for virtualization */
305#define RCP_ACC_BITS 0xf0000000UL
306#define RCP_FP_BIT 0x08000000UL
307#define RCP_PCL_BIT 0x00800000UL
308#define RCP_HR_BIT 0x00400000UL
309#define RCP_HC_BIT 0x00200000UL
310#define RCP_GR_BIT 0x00040000UL
311#define RCP_GC_BIT 0x00020000UL
312
313/* User dirty / referenced bit for KVM's migration feature */
314#define KVM_UR_BIT 0x00008000UL
315#define KVM_UC_BIT 0x00004000UL
316
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200317#else /* CONFIG_64BIT */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200318
319/* Bits in the segment/region table address-space-control-element */
320#define _ASCE_ORIGIN ~0xfffUL/* segment table origin */
321#define _ASCE_PRIVATE_SPACE 0x100 /* private space control */
322#define _ASCE_ALT_EVENT 0x80 /* storage alteration event control */
323#define _ASCE_SPACE_SWITCH 0x40 /* space switch event */
324#define _ASCE_REAL_SPACE 0x20 /* real space control */
325#define _ASCE_TYPE_MASK 0x0c /* asce table type mask */
326#define _ASCE_TYPE_REGION1 0x0c /* region first table type */
327#define _ASCE_TYPE_REGION2 0x08 /* region second table type */
328#define _ASCE_TYPE_REGION3 0x04 /* region third table type */
329#define _ASCE_TYPE_SEGMENT 0x00 /* segment table type */
330#define _ASCE_TABLE_LENGTH 0x03 /* region table length */
331
332/* Bits in the region table entry */
333#define _REGION_ENTRY_ORIGIN ~0xfffUL/* region/segment table origin */
334#define _REGION_ENTRY_INV 0x20 /* invalid region table entry */
335#define _REGION_ENTRY_TYPE_MASK 0x0c /* region/segment table type mask */
336#define _REGION_ENTRY_TYPE_R1 0x0c /* region first table type */
337#define _REGION_ENTRY_TYPE_R2 0x08 /* region second table type */
338#define _REGION_ENTRY_TYPE_R3 0x04 /* region third table type */
339#define _REGION_ENTRY_LENGTH 0x03 /* region third length */
340
341#define _REGION1_ENTRY (_REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_LENGTH)
342#define _REGION1_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INV)
343#define _REGION2_ENTRY (_REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_LENGTH)
344#define _REGION2_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INV)
345#define _REGION3_ENTRY (_REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_LENGTH)
346#define _REGION3_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INV)
347
Heiko Carstens18da2362012-10-08 09:18:26 +0200348#define _REGION3_ENTRY_LARGE 0x400 /* RTTE-format control, large page */
349
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200350/* Bits in the segment table entry */
351#define _SEGMENT_ENTRY_ORIGIN ~0x7ffUL/* segment table origin */
352#define _SEGMENT_ENTRY_RO 0x200 /* page protection bit */
353#define _SEGMENT_ENTRY_INV 0x20 /* invalid segment table entry */
354
355#define _SEGMENT_ENTRY (0)
356#define _SEGMENT_ENTRY_EMPTY (_SEGMENT_ENTRY_INV)
357
Gerald Schaefer53492b12008-04-30 13:38:46 +0200358#define _SEGMENT_ENTRY_LARGE 0x400 /* STE-format control, large page */
359#define _SEGMENT_ENTRY_CO 0x100 /* change-recording override */
Gerald Schaefer75077af2012-10-08 16:30:15 -0700360#define _SEGMENT_ENTRY_SPLIT_BIT 0 /* THP splitting bit number */
361#define _SEGMENT_ENTRY_SPLIT (1UL << _SEGMENT_ENTRY_SPLIT_BIT)
Gerald Schaefer53492b12008-04-30 13:38:46 +0200362
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -0700363/* Set of bits not changed in pmd_modify */
364#define _SEGMENT_CHG_MASK (_SEGMENT_ENTRY_ORIGIN | _SEGMENT_ENTRY_LARGE \
365 | _SEGMENT_ENTRY_SPLIT | _SEGMENT_ENTRY_CO)
366
Martin Schwidefsky6c61cfe2011-06-06 14:14:42 +0200367/* Page status table bits for virtualization */
368#define RCP_ACC_BITS 0xf000000000000000UL
369#define RCP_FP_BIT 0x0800000000000000UL
370#define RCP_PCL_BIT 0x0080000000000000UL
371#define RCP_HR_BIT 0x0040000000000000UL
372#define RCP_HC_BIT 0x0020000000000000UL
373#define RCP_GR_BIT 0x0004000000000000UL
374#define RCP_GC_BIT 0x0002000000000000UL
375
376/* User dirty / referenced bit for KVM's migration feature */
377#define KVM_UR_BIT 0x0000800000000000UL
378#define KVM_UC_BIT 0x0000400000000000UL
379
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200380#endif /* CONFIG_64BIT */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200381
382/*
383 * A user page table pointer has the space-switch-event bit, the
384 * private-space-control bit and the storage-alteration-event-control
385 * bit set. A kernel page table pointer doesn't need them.
386 */
387#define _ASCE_USER_BITS (_ASCE_SPACE_SWITCH | _ASCE_PRIVATE_SPACE | \
388 _ASCE_ALT_EVENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/*
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200391 * Page protection definitions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 */
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200393#define PAGE_NONE __pgprot(_PAGE_TYPE_NONE)
394#define PAGE_RO __pgprot(_PAGE_TYPE_RO)
395#define PAGE_RW __pgprot(_PAGE_TYPE_RW)
396
397#define PAGE_KERNEL PAGE_RW
398#define PAGE_COPY PAGE_RO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400/*
Martin Schwidefsky043d0702011-05-23 10:24:23 +0200401 * On s390 the page table entry has an invalid bit and a read-only bit.
402 * Read permission implies execute permission and write permission
403 * implies read permission.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 */
405 /*xwr*/
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200406#define __P000 PAGE_NONE
407#define __P001 PAGE_RO
408#define __P010 PAGE_RO
409#define __P011 PAGE_RO
Martin Schwidefsky043d0702011-05-23 10:24:23 +0200410#define __P100 PAGE_RO
411#define __P101 PAGE_RO
412#define __P110 PAGE_RO
413#define __P111 PAGE_RO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200415#define __S000 PAGE_NONE
416#define __S001 PAGE_RO
417#define __S010 PAGE_RW
418#define __S011 PAGE_RW
Martin Schwidefsky043d0702011-05-23 10:24:23 +0200419#define __S100 PAGE_RO
420#define __S101 PAGE_RO
421#define __S110 PAGE_RW
422#define __S111 PAGE_RW
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200424static inline int mm_exclusive(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200426 return likely(mm == current->active_mm &&
427 atomic_read(&mm->context.attach_count) <= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200430static inline int mm_has_pgste(struct mm_struct *mm)
431{
432#ifdef CONFIG_PGSTE
433 if (unlikely(mm->context.has_pgste))
434 return 1;
435#endif
436 return 0;
437}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438/*
439 * pgd/pmd/pte query functions
440 */
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200441#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800443static inline int pgd_present(pgd_t pgd) { return 1; }
444static inline int pgd_none(pgd_t pgd) { return 0; }
445static inline int pgd_bad(pgd_t pgd) { return 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200447static inline int pud_present(pud_t pud) { return 1; }
448static inline int pud_none(pud_t pud) { return 0; }
Heiko Carstens18da2362012-10-08 09:18:26 +0200449static inline int pud_large(pud_t pud) { return 0; }
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200450static inline int pud_bad(pud_t pud) { return 0; }
451
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200452#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100454static inline int pgd_present(pgd_t pgd)
455{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100456 if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R2)
457 return 1;
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100458 return (pgd_val(pgd) & _REGION_ENTRY_ORIGIN) != 0UL;
459}
460
461static inline int pgd_none(pgd_t pgd)
462{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100463 if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R2)
464 return 0;
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100465 return (pgd_val(pgd) & _REGION_ENTRY_INV) != 0UL;
466}
467
468static inline int pgd_bad(pgd_t pgd)
469{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100470 /*
471 * With dynamic page table levels the pgd can be a region table
472 * entry or a segment table entry. Check for the bit that are
473 * invalid for either table entry.
474 */
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100475 unsigned long mask =
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100476 ~_SEGMENT_ENTRY_ORIGIN & ~_REGION_ENTRY_INV &
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100477 ~_REGION_ENTRY_TYPE_MASK & ~_REGION_ENTRY_LENGTH;
478 return (pgd_val(pgd) & mask) != 0;
479}
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200480
481static inline int pud_present(pud_t pud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100483 if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R3)
484 return 1;
Martin Schwidefsky0d017922007-12-17 16:25:48 +0100485 return (pud_val(pud) & _REGION_ENTRY_ORIGIN) != 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200488static inline int pud_none(pud_t pud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100490 if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R3)
491 return 0;
Martin Schwidefsky0d017922007-12-17 16:25:48 +0100492 return (pud_val(pud) & _REGION_ENTRY_INV) != 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Heiko Carstens18da2362012-10-08 09:18:26 +0200495static inline int pud_large(pud_t pud)
496{
497 if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) != _REGION_ENTRY_TYPE_R3)
498 return 0;
499 return !!(pud_val(pud) & _REGION3_ENTRY_LARGE);
500}
501
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200502static inline int pud_bad(pud_t pud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100504 /*
505 * With dynamic page table levels the pud can be a region table
506 * entry or a segment table entry. Check for the bit that are
507 * invalid for either table entry.
508 */
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100509 unsigned long mask =
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100510 ~_SEGMENT_ENTRY_ORIGIN & ~_REGION_ENTRY_INV &
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100511 ~_REGION_ENTRY_TYPE_MASK & ~_REGION_ENTRY_LENGTH;
512 return (pud_val(pud) & mask) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200515#endif /* CONFIG_64BIT */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200516
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800517static inline int pmd_present(pmd_t pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Gerald Schaeferd8e7a332012-10-25 17:42:50 +0200519 unsigned long mask = _SEGMENT_ENTRY_INV | _SEGMENT_ENTRY_RO;
520 return (pmd_val(pmd) & mask) == _HPAGE_TYPE_NONE ||
521 !(pmd_val(pmd) & _SEGMENT_ENTRY_INV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800524static inline int pmd_none(pmd_t pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Gerald Schaeferd8e7a332012-10-25 17:42:50 +0200526 return (pmd_val(pmd) & _SEGMENT_ENTRY_INV) &&
527 !(pmd_val(pmd) & _SEGMENT_ENTRY_RO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Heiko Carstens378b1e72012-10-01 12:58:34 +0200530static inline int pmd_large(pmd_t pmd)
531{
532#ifdef CONFIG_64BIT
533 return !!(pmd_val(pmd) & _SEGMENT_ENTRY_LARGE);
534#else
535 return 0;
536#endif
537}
538
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800539static inline int pmd_bad(pmd_t pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200541 unsigned long mask = ~_SEGMENT_ENTRY_ORIGIN & ~_SEGMENT_ENTRY_INV;
542 return (pmd_val(pmd) & mask) != _SEGMENT_ENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Gerald Schaefer75077af2012-10-08 16:30:15 -0700545#define __HAVE_ARCH_PMDP_SPLITTING_FLUSH
546extern void pmdp_splitting_flush(struct vm_area_struct *vma,
547 unsigned long addr, pmd_t *pmdp);
548
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -0700549#define __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
550extern int pmdp_set_access_flags(struct vm_area_struct *vma,
551 unsigned long address, pmd_t *pmdp,
552 pmd_t entry, int dirty);
553
554#define __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
555extern int pmdp_clear_flush_young(struct vm_area_struct *vma,
556 unsigned long address, pmd_t *pmdp);
557
558#define __HAVE_ARCH_PMD_WRITE
559static inline int pmd_write(pmd_t pmd)
560{
561 return (pmd_val(pmd) & _SEGMENT_ENTRY_RO) == 0;
562}
563
564static inline int pmd_young(pmd_t pmd)
565{
566 return 0;
567}
568
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800569static inline int pte_none(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Martin Schwidefsky83377482006-10-18 18:30:51 +0200571 return (pte_val(pte) & _PAGE_INVALID) && !(pte_val(pte) & _PAGE_SWT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800574static inline int pte_present(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Martin Schwidefsky83377482006-10-18 18:30:51 +0200576 unsigned long mask = _PAGE_RO | _PAGE_INVALID | _PAGE_SWT | _PAGE_SWX;
577 return (pte_val(pte) & mask) == _PAGE_TYPE_NONE ||
578 (!(pte_val(pte) & _PAGE_INVALID) &&
579 !(pte_val(pte) & _PAGE_SWT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800582static inline int pte_file(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Martin Schwidefsky83377482006-10-18 18:30:51 +0200584 unsigned long mask = _PAGE_RO | _PAGE_INVALID | _PAGE_SWT;
585 return (pte_val(pte) & mask) == _PAGE_TYPE_FILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Nick Piggin7e675132008-04-28 02:13:00 -0700588static inline int pte_special(pte_t pte)
589{
Nick Piggina08cb622008-04-28 02:13:03 -0700590 return (pte_val(pte) & _PAGE_SPECIAL);
Nick Piggin7e675132008-04-28 02:13:00 -0700591}
592
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200593#define __HAVE_ARCH_PTE_SAME
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200594static inline int pte_same(pte_t a, pte_t b)
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100595{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200596 return pte_val(a) == pte_val(b);
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100597}
598
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200599static inline pgste_t pgste_get_lock(pte_t *ptep)
600{
601 unsigned long new = 0;
602#ifdef CONFIG_PGSTE
603 unsigned long old;
604
605 preempt_disable();
606 asm(
607 " lg %0,%2\n"
608 "0: lgr %1,%0\n"
609 " nihh %0,0xff7f\n" /* clear RCP_PCL_BIT in old */
610 " oihh %1,0x0080\n" /* set RCP_PCL_BIT in new */
611 " csg %0,%1,%2\n"
612 " jl 0b\n"
613 : "=&d" (old), "=&d" (new), "=Q" (ptep[PTRS_PER_PTE])
614 : "Q" (ptep[PTRS_PER_PTE]) : "cc");
615#endif
616 return __pgste(new);
617}
618
619static inline void pgste_set_unlock(pte_t *ptep, pgste_t pgste)
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100620{
621#ifdef CONFIG_PGSTE
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200622 asm(
623 " nihh %1,0xff7f\n" /* clear RCP_PCL_BIT */
624 " stg %1,%0\n"
625 : "=Q" (ptep[PTRS_PER_PTE])
626 : "d" (pgste_val(pgste)), "Q" (ptep[PTRS_PER_PTE]) : "cc");
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100627 preempt_enable();
628#endif
629}
630
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200631static inline pgste_t pgste_update_all(pte_t *ptep, pgste_t pgste)
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100632{
633#ifdef CONFIG_PGSTE
Heiko Carstensa43a9d92011-05-29 12:40:50 +0200634 unsigned long address, bits;
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200635 unsigned char skey;
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100636
Martin Schwidefsky09b53882011-11-14 11:19:00 +0100637 if (!pte_present(*ptep))
638 return pgste;
Heiko Carstensa43a9d92011-05-29 12:40:50 +0200639 address = pte_val(*ptep) & PAGE_MASK;
640 skey = page_get_storage_key(address);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200641 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
642 /* Clear page changed & referenced bit in the storage key */
Carsten Otte7c818782011-12-01 13:32:16 +0100643 if (bits & _PAGE_CHANGED)
644 page_set_storage_key(address, skey ^ bits, 1);
645 else if (bits)
646 page_reset_referenced(address);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200647 /* Transfer page changed & referenced bit to guest bits in pgste */
648 pgste_val(pgste) |= bits << 48; /* RCP_GR_BIT & RCP_GC_BIT */
649 /* Get host changed & referenced bits from pgste */
650 bits |= (pgste_val(pgste) & (RCP_HR_BIT | RCP_HC_BIT)) >> 52;
651 /* Clear host bits in pgste. */
652 pgste_val(pgste) &= ~(RCP_HR_BIT | RCP_HC_BIT);
653 pgste_val(pgste) &= ~(RCP_ACC_BITS | RCP_FP_BIT);
654 /* Copy page access key and fetch protection bit to pgste */
655 pgste_val(pgste) |=
656 (unsigned long) (skey & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
657 /* Transfer changed and referenced to kvm user bits */
658 pgste_val(pgste) |= bits << 45; /* KVM_UR_BIT & KVM_UC_BIT */
659 /* Transfer changed & referenced to pte sofware bits */
660 pte_val(*ptep) |= bits << 1; /* _PAGE_SWR & _PAGE_SWC */
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100661#endif
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200662 return pgste;
663
664}
665
666static inline pgste_t pgste_update_young(pte_t *ptep, pgste_t pgste)
667{
668#ifdef CONFIG_PGSTE
669 int young;
670
Martin Schwidefsky09b53882011-11-14 11:19:00 +0100671 if (!pte_present(*ptep))
672 return pgste;
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200673 young = page_reset_referenced(pte_val(*ptep) & PAGE_MASK);
674 /* Transfer page referenced bit to pte software bit (host view) */
675 if (young || (pgste_val(pgste) & RCP_HR_BIT))
676 pte_val(*ptep) |= _PAGE_SWR;
677 /* Clear host referenced bit in pgste. */
678 pgste_val(pgste) &= ~RCP_HR_BIT;
679 /* Transfer page referenced bit to guest bit in pgste */
680 pgste_val(pgste) |= (unsigned long) young << 50; /* set RCP_GR_BIT */
681#endif
682 return pgste;
683
684}
685
Martin Schwidefsky09b53882011-11-14 11:19:00 +0100686static inline void pgste_set_pte(pte_t *ptep, pgste_t pgste, pte_t entry)
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200687{
688#ifdef CONFIG_PGSTE
Heiko Carstensa43a9d92011-05-29 12:40:50 +0200689 unsigned long address;
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200690 unsigned long okey, nkey;
691
Martin Schwidefsky09b53882011-11-14 11:19:00 +0100692 if (!pte_present(entry))
693 return;
694 address = pte_val(entry) & PAGE_MASK;
Heiko Carstensa43a9d92011-05-29 12:40:50 +0200695 okey = nkey = page_get_storage_key(address);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200696 nkey &= ~(_PAGE_ACC_BITS | _PAGE_FP_BIT);
697 /* Set page access key and fetch protection bit from pgste */
698 nkey |= (pgste_val(pgste) & (RCP_ACC_BITS | RCP_FP_BIT)) >> 56;
699 if (okey != nkey)
Heiko Carstensa43a9d92011-05-29 12:40:50 +0200700 page_set_storage_key(address, nkey, 1);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200701#endif
702}
703
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200704/**
705 * struct gmap_struct - guest address space
706 * @mm: pointer to the parent mm_struct
707 * @table: pointer to the page directory
Christian Borntraeger480e5922011-09-20 17:07:28 +0200708 * @asce: address space control element for gmap page table
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200709 * @crst_list: list of all crst tables used in the guest address space
710 */
711struct gmap {
712 struct list_head list;
713 struct mm_struct *mm;
714 unsigned long *table;
Christian Borntraeger480e5922011-09-20 17:07:28 +0200715 unsigned long asce;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200716 struct list_head crst_list;
717};
718
719/**
720 * struct gmap_rmap - reverse mapping for segment table entries
721 * @next: pointer to the next gmap_rmap structure in the list
722 * @entry: pointer to a segment table entry
723 */
724struct gmap_rmap {
725 struct list_head list;
726 unsigned long *entry;
727};
728
729/**
730 * struct gmap_pgtable - gmap information attached to a page table
731 * @vmaddr: address of the 1MB segment in the process virtual memory
732 * @mapper: list of segment table entries maping a page table
733 */
734struct gmap_pgtable {
735 unsigned long vmaddr;
736 struct list_head mapper;
737};
738
739struct gmap *gmap_alloc(struct mm_struct *mm);
740void gmap_free(struct gmap *gmap);
741void gmap_enable(struct gmap *gmap);
742void gmap_disable(struct gmap *gmap);
743int gmap_map_segment(struct gmap *gmap, unsigned long from,
744 unsigned long to, unsigned long length);
745int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len);
Carsten Otte499069e2011-10-30 15:17:02 +0100746unsigned long __gmap_fault(unsigned long address, struct gmap *);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200747unsigned long gmap_fault(unsigned long address, struct gmap *);
Christian Borntraeger388186b2011-10-30 15:17:03 +0100748void gmap_discard(unsigned long from, unsigned long to, struct gmap *);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200749
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200750/*
751 * Certain architectures need to do special things when PTEs
752 * within a page table are directly modified. Thus, the following
753 * hook is made available.
754 */
755static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
756 pte_t *ptep, pte_t entry)
757{
758 pgste_t pgste;
759
760 if (mm_has_pgste(mm)) {
761 pgste = pgste_get_lock(ptep);
Martin Schwidefsky09b53882011-11-14 11:19:00 +0100762 pgste_set_pte(ptep, pgste, entry);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200763 *ptep = entry;
764 pgste_set_unlock(ptep, pgste);
765 } else
766 *ptep = entry;
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769/*
770 * query functions pte_write/pte_dirty/pte_young only work if
771 * pte_present() is true. Undefined behaviour if not..
772 */
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800773static inline int pte_write(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 return (pte_val(pte) & _PAGE_RO) == 0;
776}
777
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800778static inline int pte_dirty(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200780#ifdef CONFIG_PGSTE
781 if (pte_val(pte) & _PAGE_SWC)
782 return 1;
783#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return 0;
785}
786
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800787static inline int pte_young(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200789#ifdef CONFIG_PGSTE
790 if (pte_val(pte) & _PAGE_SWR)
791 return 1;
792#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return 0;
794}
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796/*
797 * pgd/pmd/pte modification functions
798 */
799
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200800static inline void pgd_clear(pgd_t *pgd)
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100801{
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200802#ifdef CONFIG_64BIT
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100803 if ((pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
804 pgd_val(*pgd) = _REGION2_ENTRY_EMPTY;
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200805#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100808static inline void pud_clear(pud_t *pud)
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100809{
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200810#ifdef CONFIG_64BIT
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200811 if ((pud_val(*pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
812 pud_val(*pud) = _REGION3_ENTRY_EMPTY;
813#endif
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100814}
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100815
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200816static inline void pmd_clear(pmd_t *pmdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200818 pmd_val(*pmdp) = _SEGMENT_ENTRY_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800821static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200823 pte_val(*ptep) = _PAGE_TYPE_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
826/*
827 * The following pte modification functions only work if
828 * pte_present() is true. Undefined behaviour if not..
829 */
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800830static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Nick Piggin138c9022008-07-08 11:31:06 +0200832 pte_val(pte) &= _PAGE_CHG_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 pte_val(pte) |= pgprot_val(newprot);
834 return pte;
835}
836
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800837static inline pte_t pte_wrprotect(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200839 /* Do not clobber _PAGE_TYPE_NONE pages! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (!(pte_val(pte) & _PAGE_INVALID))
841 pte_val(pte) |= _PAGE_RO;
842 return pte;
843}
844
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800845static inline pte_t pte_mkwrite(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 pte_val(pte) &= ~_PAGE_RO;
848 return pte;
849}
850
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800851static inline pte_t pte_mkclean(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200853#ifdef CONFIG_PGSTE
854 pte_val(pte) &= ~_PAGE_SWC;
855#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return pte;
857}
858
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800859static inline pte_t pte_mkdirty(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return pte;
862}
863
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800864static inline pte_t pte_mkold(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200866#ifdef CONFIG_PGSTE
867 pte_val(pte) &= ~_PAGE_SWR;
868#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return pte;
870}
871
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800872static inline pte_t pte_mkyoung(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return pte;
875}
876
Nick Piggin7e675132008-04-28 02:13:00 -0700877static inline pte_t pte_mkspecial(pte_t pte)
878{
Nick Piggina08cb622008-04-28 02:13:03 -0700879 pte_val(pte) |= _PAGE_SPECIAL;
Nick Piggin7e675132008-04-28 02:13:00 -0700880 return pte;
881}
882
Heiko Carstens84afdce2010-10-25 16:10:36 +0200883#ifdef CONFIG_HUGETLB_PAGE
884static inline pte_t pte_mkhuge(pte_t pte)
885{
886 /*
887 * PROT_NONE needs to be remapped from the pte type to the ste type.
888 * The HW invalid bit is also different for pte and ste. The pte
889 * invalid bit happens to be the same as the ste _SEGMENT_ENTRY_LARGE
890 * bit, so we don't have to clear it.
891 */
892 if (pte_val(pte) & _PAGE_INVALID) {
893 if (pte_val(pte) & _PAGE_SWT)
894 pte_val(pte) |= _HPAGE_TYPE_NONE;
895 pte_val(pte) |= _SEGMENT_ENTRY_INV;
896 }
897 /*
898 * Clear SW pte bits SWT and SWX, there are no SW bits in a segment
899 * table entry.
900 */
901 pte_val(pte) &= ~(_PAGE_SWT | _PAGE_SWX);
902 /*
903 * Also set the change-override bit because we don't need dirty bit
904 * tracking for hugetlbfs pages.
905 */
906 pte_val(pte) |= (_SEGMENT_ENTRY_LARGE | _SEGMENT_ENTRY_CO);
907 return pte;
908}
909#endif
910
Florian Funke15e86b02008-10-10 21:33:26 +0200911/*
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200912 * Get (and clear) the user dirty bit for a pte.
Florian Funke15e86b02008-10-10 21:33:26 +0200913 */
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200914static inline int ptep_test_and_clear_user_dirty(struct mm_struct *mm,
915 pte_t *ptep)
Florian Funke15e86b02008-10-10 21:33:26 +0200916{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200917 pgste_t pgste;
918 int dirty = 0;
Florian Funke15e86b02008-10-10 21:33:26 +0200919
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200920 if (mm_has_pgste(mm)) {
921 pgste = pgste_get_lock(ptep);
922 pgste = pgste_update_all(ptep, pgste);
923 dirty = !!(pgste_val(pgste) & KVM_UC_BIT);
924 pgste_val(pgste) &= ~KVM_UC_BIT;
925 pgste_set_unlock(ptep, pgste);
926 return dirty;
Florian Funke15e86b02008-10-10 21:33:26 +0200927 }
Florian Funke15e86b02008-10-10 21:33:26 +0200928 return dirty;
929}
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200930
931/*
932 * Get (and clear) the user referenced bit for a pte.
933 */
934static inline int ptep_test_and_clear_user_young(struct mm_struct *mm,
935 pte_t *ptep)
936{
937 pgste_t pgste;
938 int young = 0;
939
940 if (mm_has_pgste(mm)) {
941 pgste = pgste_get_lock(ptep);
942 pgste = pgste_update_young(ptep, pgste);
943 young = !!(pgste_val(pgste) & KVM_UR_BIT);
944 pgste_val(pgste) &= ~KVM_UR_BIT;
945 pgste_set_unlock(ptep, pgste);
946 }
947 return young;
948}
Florian Funke15e86b02008-10-10 21:33:26 +0200949
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200950#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
951static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
952 unsigned long addr, pte_t *ptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200954 pgste_t pgste;
955 pte_t pte;
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100956
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200957 if (mm_has_pgste(vma->vm_mm)) {
958 pgste = pgste_get_lock(ptep);
959 pgste = pgste_update_young(ptep, pgste);
960 pte = *ptep;
961 *ptep = pte_mkold(pte);
962 pgste_set_unlock(ptep, pgste);
963 return pte_young(pte);
964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return 0;
966}
967
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200968#define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
969static inline int ptep_clear_flush_young(struct vm_area_struct *vma,
970 unsigned long address, pte_t *ptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100972 /* No need to flush TLB
973 * On s390 reference bits are in storage key and never in TLB
974 * With virtualization we handle the reference bit, without we
975 * we can simply return */
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100976 return ptep_test_and_clear_young(vma, address, ptep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200979static inline void __ptep_ipte(unsigned long address, pte_t *ptep)
980{
981 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200982#ifndef CONFIG_64BIT
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100983 /* pto must point to the start of the segment table */
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200984 pte_t *pto = (pte_t *) (((unsigned long) ptep) & 0x7ffffc00);
985#else
986 /* ipte in zarch mode can do the math */
987 pte_t *pto = ptep;
988#endif
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200989 asm volatile(
990 " ipte %2,%3"
991 : "=m" (*ptep) : "m" (*ptep),
992 "a" (pto), "a" (address));
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200993 }
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200994}
995
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200996/*
997 * This is hard to understand. ptep_get_and_clear and ptep_clear_flush
998 * both clear the TLB for the unmapped pte. The reason is that
999 * ptep_get_and_clear is used in common code (e.g. change_pte_range)
1000 * to modify an active pte. The sequence is
1001 * 1) ptep_get_and_clear
1002 * 2) set_pte_at
1003 * 3) flush_tlb_range
1004 * On s390 the tlb needs to get flushed with the modification of the pte
1005 * if the pte is active. The only way how this can be implemented is to
1006 * have ptep_get_and_clear do the tlb flush. In exchange flush_tlb_range
1007 * is a nop.
1008 */
1009#define __HAVE_ARCH_PTEP_GET_AND_CLEAR
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001010static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
1011 unsigned long address, pte_t *ptep)
1012{
1013 pgste_t pgste;
1014 pte_t pte;
1015
1016 mm->context.flush_mm = 1;
1017 if (mm_has_pgste(mm))
1018 pgste = pgste_get_lock(ptep);
1019
1020 pte = *ptep;
1021 if (!mm_exclusive(mm))
1022 __ptep_ipte(address, ptep);
1023 pte_val(*ptep) = _PAGE_TYPE_EMPTY;
1024
1025 if (mm_has_pgste(mm)) {
1026 pgste = pgste_update_all(&pte, pgste);
1027 pgste_set_unlock(ptep, pgste);
1028 }
1029 return pte;
1030}
1031
1032#define __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
1033static inline pte_t ptep_modify_prot_start(struct mm_struct *mm,
1034 unsigned long address,
1035 pte_t *ptep)
1036{
1037 pte_t pte;
1038
1039 mm->context.flush_mm = 1;
1040 if (mm_has_pgste(mm))
1041 pgste_get_lock(ptep);
1042
1043 pte = *ptep;
1044 if (!mm_exclusive(mm))
1045 __ptep_ipte(address, ptep);
1046 return pte;
1047}
1048
1049static inline void ptep_modify_prot_commit(struct mm_struct *mm,
1050 unsigned long address,
1051 pte_t *ptep, pte_t pte)
1052{
1053 *ptep = pte;
1054 if (mm_has_pgste(mm))
1055 pgste_set_unlock(ptep, *(pgste_t *)(ptep + PTRS_PER_PTE));
1056}
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001057
1058#define __HAVE_ARCH_PTEP_CLEAR_FLUSH
Martin Schwidefskyf0e47c22007-07-17 04:03:03 -07001059static inline pte_t ptep_clear_flush(struct vm_area_struct *vma,
1060 unsigned long address, pte_t *ptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001062 pgste_t pgste;
1063 pte_t pte;
1064
1065 if (mm_has_pgste(vma->vm_mm))
1066 pgste = pgste_get_lock(ptep);
1067
1068 pte = *ptep;
1069 __ptep_ipte(address, ptep);
1070 pte_val(*ptep) = _PAGE_TYPE_EMPTY;
1071
1072 if (mm_has_pgste(vma->vm_mm)) {
1073 pgste = pgste_update_all(&pte, pgste);
1074 pgste_set_unlock(ptep, pgste);
1075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return pte;
1077}
1078
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001079/*
1080 * The batched pte unmap code uses ptep_get_and_clear_full to clear the
1081 * ptes. Here an optimization is possible. tlb_gather_mmu flushes all
1082 * tlbs of an mm if it can guarantee that the ptes of the mm_struct
1083 * cannot be accessed while the batched unmap is running. In this case
1084 * full==1 and a simple pte_clear is enough. See tlb.h.
1085 */
1086#define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
1087static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001088 unsigned long address,
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001089 pte_t *ptep, int full)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001091 pgste_t pgste;
1092 pte_t pte;
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001093
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001094 if (mm_has_pgste(mm))
1095 pgste = pgste_get_lock(ptep);
1096
1097 pte = *ptep;
1098 if (!full)
1099 __ptep_ipte(address, ptep);
1100 pte_val(*ptep) = _PAGE_TYPE_EMPTY;
1101
1102 if (mm_has_pgste(mm)) {
1103 pgste = pgste_update_all(&pte, pgste);
1104 pgste_set_unlock(ptep, pgste);
1105 }
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001106 return pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001109#define __HAVE_ARCH_PTEP_SET_WRPROTECT
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001110static inline pte_t ptep_set_wrprotect(struct mm_struct *mm,
1111 unsigned long address, pte_t *ptep)
1112{
1113 pgste_t pgste;
1114 pte_t pte = *ptep;
1115
1116 if (pte_write(pte)) {
1117 mm->context.flush_mm = 1;
1118 if (mm_has_pgste(mm))
1119 pgste = pgste_get_lock(ptep);
1120
1121 if (!mm_exclusive(mm))
1122 __ptep_ipte(address, ptep);
1123 *ptep = pte_wrprotect(pte);
1124
1125 if (mm_has_pgste(mm))
1126 pgste_set_unlock(ptep, pgste);
1127 }
1128 return pte;
1129}
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001130
1131#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001132static inline int ptep_set_access_flags(struct vm_area_struct *vma,
1133 unsigned long address, pte_t *ptep,
1134 pte_t entry, int dirty)
1135{
1136 pgste_t pgste;
1137
1138 if (pte_same(*ptep, entry))
1139 return 0;
1140 if (mm_has_pgste(vma->vm_mm))
1141 pgste = pgste_get_lock(ptep);
1142
1143 __ptep_ipte(address, ptep);
1144 *ptep = entry;
1145
1146 if (mm_has_pgste(vma->vm_mm))
1147 pgste_set_unlock(ptep, pgste);
1148 return 1;
1149}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 * Conversion functions: convert a page and protection to a page entry,
1153 * and a page entry and page directory to the page they refer to.
1154 */
1155static inline pte_t mk_pte_phys(unsigned long physpage, pgprot_t pgprot)
1156{
1157 pte_t __pte;
1158 pte_val(__pte) = physpage + pgprot_val(pgprot);
1159 return __pte;
1160}
1161
Heiko Carstens2dcea572006-09-29 01:58:41 -07001162static inline pte_t mk_pte(struct page *page, pgprot_t pgprot)
1163{
Heiko Carstens0b2b6e12006-10-04 20:02:23 +02001164 unsigned long physpage = page_to_phys(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Heiko Carstens2dcea572006-09-29 01:58:41 -07001166 return mk_pte_phys(physpage, pgprot);
1167}
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169#define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001170#define pud_index(address) (((address) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
1171#define pmd_index(address) (((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
1172#define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001174#define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175#define pgd_offset_k(address) pgd_offset(&init_mm, address)
1176
Heiko Carstensf4815ac2012-05-23 16:24:51 +02001177#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001179#define pmd_deref(pmd) (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN)
1180#define pud_deref(pmd) ({ BUG(); 0UL; })
1181#define pgd_deref(pmd) ({ BUG(); 0UL; })
1182
1183#define pud_offset(pgd, address) ((pud_t *) pgd)
1184#define pmd_offset(pud, address) ((pmd_t *) pud + pmd_index(address))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Heiko Carstensf4815ac2012-05-23 16:24:51 +02001186#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001188#define pmd_deref(pmd) (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN)
1189#define pud_deref(pud) (pud_val(pud) & _REGION_ENTRY_ORIGIN)
Martin Schwidefsky5a216a22008-02-09 18:24:36 +01001190#define pgd_deref(pgd) (pgd_val(pgd) & _REGION_ENTRY_ORIGIN)
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001191
Martin Schwidefsky5a216a22008-02-09 18:24:36 +01001192static inline pud_t *pud_offset(pgd_t *pgd, unsigned long address)
1193{
Martin Schwidefsky6252d702008-02-09 18:24:37 +01001194 pud_t *pud = (pud_t *) pgd;
1195 if ((pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
1196 pud = (pud_t *) pgd_deref(*pgd);
Martin Schwidefsky5a216a22008-02-09 18:24:36 +01001197 return pud + pud_index(address);
1198}
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001199
1200static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address)
1201{
Martin Schwidefsky6252d702008-02-09 18:24:37 +01001202 pmd_t *pmd = (pmd_t *) pud;
1203 if ((pud_val(*pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
1204 pmd = (pmd_t *) pud_deref(*pud);
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001205 return pmd + pmd_index(address);
1206}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Heiko Carstensf4815ac2012-05-23 16:24:51 +02001208#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001210#define pfn_pte(pfn,pgprot) mk_pte_phys(__pa((pfn) << PAGE_SHIFT),(pgprot))
1211#define pte_pfn(x) (pte_val(x) >> PAGE_SHIFT)
1212#define pte_page(x) pfn_to_page(pte_pfn(x))
1213
1214#define pmd_page(pmd) pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT)
1215
1216/* Find an entry in the lowest level page table.. */
1217#define pte_offset(pmd, addr) ((pte_t *) pmd_deref(*(pmd)) + pte_index(addr))
1218#define pte_offset_kernel(pmd, address) pte_offset(pmd,address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219#define pte_offset_map(pmd, address) pte_offset_kernel(pmd, address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220#define pte_unmap(pte) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001222static inline void __pmd_idte(unsigned long address, pmd_t *pmdp)
1223{
1224 unsigned long sto = (unsigned long) pmdp -
1225 pmd_index(address) * sizeof(pmd_t);
1226
1227 if (!(pmd_val(*pmdp) & _SEGMENT_ENTRY_INV)) {
1228 asm volatile(
1229 " .insn rrf,0xb98e0000,%2,%3,0,0"
1230 : "=m" (*pmdp)
1231 : "m" (*pmdp), "a" (sto),
1232 "a" ((address & HPAGE_MASK))
1233 : "cc"
1234 );
1235 }
1236}
1237
Gerald Schaefer75077af2012-10-08 16:30:15 -07001238#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gerald Schaeferd8e7a332012-10-25 17:42:50 +02001239
1240#define SEGMENT_NONE __pgprot(_HPAGE_TYPE_NONE)
1241#define SEGMENT_RO __pgprot(_HPAGE_TYPE_RO)
1242#define SEGMENT_RW __pgprot(_HPAGE_TYPE_RW)
1243
Gerald Schaefer9501d092012-10-08 16:30:18 -07001244#define __HAVE_ARCH_PGTABLE_DEPOSIT
1245extern void pgtable_trans_huge_deposit(struct mm_struct *mm, pgtable_t pgtable);
1246
1247#define __HAVE_ARCH_PGTABLE_WITHDRAW
1248extern pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm);
1249
Gerald Schaefer75077af2012-10-08 16:30:15 -07001250static inline int pmd_trans_splitting(pmd_t pmd)
1251{
1252 return pmd_val(pmd) & _SEGMENT_ENTRY_SPLIT;
1253}
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001254
1255static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
1256 pmd_t *pmdp, pmd_t entry)
1257{
1258 *pmdp = entry;
1259}
1260
1261static inline unsigned long massage_pgprot_pmd(pgprot_t pgprot)
1262{
Gerald Schaeferd8e7a332012-10-25 17:42:50 +02001263 /*
1264 * pgprot is PAGE_NONE, PAGE_RO, or PAGE_RW (see __Pxxx / __Sxxx)
1265 * Convert to segment table entry format.
1266 */
1267 if (pgprot_val(pgprot) == pgprot_val(PAGE_NONE))
1268 return pgprot_val(SEGMENT_NONE);
1269 if (pgprot_val(pgprot) == pgprot_val(PAGE_RO))
1270 return pgprot_val(SEGMENT_RO);
1271 return pgprot_val(SEGMENT_RW);
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001272}
1273
1274static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
1275{
1276 pmd_val(pmd) &= _SEGMENT_CHG_MASK;
1277 pmd_val(pmd) |= massage_pgprot_pmd(newprot);
1278 return pmd;
1279}
1280
1281static inline pmd_t pmd_mkhuge(pmd_t pmd)
1282{
1283 pmd_val(pmd) |= _SEGMENT_ENTRY_LARGE;
1284 return pmd;
1285}
1286
1287static inline pmd_t pmd_mkwrite(pmd_t pmd)
1288{
Gerald Schaeferd8e7a332012-10-25 17:42:50 +02001289 /* Do not clobber _HPAGE_TYPE_NONE pages! */
1290 if (!(pmd_val(pmd) & _SEGMENT_ENTRY_INV))
1291 pmd_val(pmd) &= ~_SEGMENT_ENTRY_RO;
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001292 return pmd;
1293}
1294
1295static inline pmd_t pmd_wrprotect(pmd_t pmd)
1296{
1297 pmd_val(pmd) |= _SEGMENT_ENTRY_RO;
1298 return pmd;
1299}
1300
1301static inline pmd_t pmd_mkdirty(pmd_t pmd)
1302{
1303 /* No dirty bit in the segment table entry. */
1304 return pmd;
1305}
1306
1307static inline pmd_t pmd_mkold(pmd_t pmd)
1308{
1309 /* No referenced bit in the segment table entry. */
1310 return pmd;
1311}
1312
1313static inline pmd_t pmd_mkyoung(pmd_t pmd)
1314{
1315 /* No referenced bit in the segment table entry. */
1316 return pmd;
1317}
1318
1319#define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
1320static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
1321 unsigned long address, pmd_t *pmdp)
1322{
1323 unsigned long pmd_addr = pmd_val(*pmdp) & HPAGE_MASK;
1324 long tmp, rc;
1325 int counter;
1326
1327 rc = 0;
1328 if (MACHINE_HAS_RRBM) {
1329 counter = PTRS_PER_PTE >> 6;
1330 asm volatile(
1331 "0: .insn rre,0xb9ae0000,%0,%3\n" /* rrbm */
1332 " ogr %1,%0\n"
1333 " la %3,0(%4,%3)\n"
1334 " brct %2,0b\n"
1335 : "=&d" (tmp), "+&d" (rc), "+d" (counter),
1336 "+a" (pmd_addr)
1337 : "a" (64 * 4096UL) : "cc");
1338 rc = !!rc;
1339 } else {
1340 counter = PTRS_PER_PTE;
1341 asm volatile(
1342 "0: rrbe 0,%2\n"
1343 " la %2,0(%3,%2)\n"
1344 " brc 12,1f\n"
1345 " lhi %0,1\n"
1346 "1: brct %1,0b\n"
1347 : "+d" (rc), "+d" (counter), "+a" (pmd_addr)
1348 : "a" (4096UL) : "cc");
1349 }
1350 return rc;
1351}
1352
1353#define __HAVE_ARCH_PMDP_GET_AND_CLEAR
1354static inline pmd_t pmdp_get_and_clear(struct mm_struct *mm,
1355 unsigned long address, pmd_t *pmdp)
1356{
1357 pmd_t pmd = *pmdp;
1358
1359 __pmd_idte(address, pmdp);
1360 pmd_clear(pmdp);
1361 return pmd;
1362}
1363
1364#define __HAVE_ARCH_PMDP_CLEAR_FLUSH
1365static inline pmd_t pmdp_clear_flush(struct vm_area_struct *vma,
1366 unsigned long address, pmd_t *pmdp)
1367{
1368 return pmdp_get_and_clear(vma->vm_mm, address, pmdp);
1369}
1370
1371#define __HAVE_ARCH_PMDP_INVALIDATE
1372static inline void pmdp_invalidate(struct vm_area_struct *vma,
1373 unsigned long address, pmd_t *pmdp)
1374{
1375 __pmd_idte(address, pmdp);
1376}
1377
1378static inline pmd_t mk_pmd_phys(unsigned long physpage, pgprot_t pgprot)
1379{
1380 pmd_t __pmd;
1381 pmd_val(__pmd) = physpage + massage_pgprot_pmd(pgprot);
1382 return __pmd;
1383}
1384
1385#define pfn_pmd(pfn, pgprot) mk_pmd_phys(__pa((pfn) << PAGE_SHIFT), (pgprot))
1386#define mk_pmd(page, pgprot) pfn_pmd(page_to_pfn(page), (pgprot))
1387
1388static inline int pmd_trans_huge(pmd_t pmd)
1389{
1390 return pmd_val(pmd) & _SEGMENT_ENTRY_LARGE;
1391}
1392
1393static inline int has_transparent_hugepage(void)
1394{
1395 return MACHINE_HAS_HPAGE ? 1 : 0;
1396}
1397
1398static inline unsigned long pmd_pfn(pmd_t pmd)
1399{
1400 if (pmd_trans_huge(pmd))
1401 return pmd_val(pmd) >> HPAGE_SHIFT;
1402 else
1403 return pmd_val(pmd) >> PAGE_SHIFT;
1404}
Gerald Schaefer75077af2012-10-08 16:30:15 -07001405#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407/*
1408 * 31 bit swap entry format:
1409 * A page-table entry has some bits we have to treat in a special way.
1410 * Bits 0, 20 and bit 23 have to be zero, otherwise an specification
1411 * exception will occur instead of a page translation exception. The
1412 * specifiation exception has the bad habit not to store necessary
1413 * information in the lowcore.
1414 * Bit 21 and bit 22 are the page invalid bit and the page protection
1415 * bit. We set both to indicate a swapped page.
1416 * Bit 30 and 31 are used to distinguish the different page types. For
1417 * a swapped page these bits need to be zero.
1418 * This leaves the bits 1-19 and bits 24-29 to store type and offset.
1419 * We use the 5 bits from 25-29 for the type and the 20 bits from 1-19
1420 * plus 24 for the offset.
1421 * 0| offset |0110|o|type |00|
1422 * 0 0000000001111111111 2222 2 22222 33
1423 * 0 1234567890123456789 0123 4 56789 01
1424 *
1425 * 64 bit swap entry format:
1426 * A page-table entry has some bits we have to treat in a special way.
1427 * Bits 52 and bit 55 have to be zero, otherwise an specification
1428 * exception will occur instead of a page translation exception. The
1429 * specifiation exception has the bad habit not to store necessary
1430 * information in the lowcore.
1431 * Bit 53 and bit 54 are the page invalid bit and the page protection
1432 * bit. We set both to indicate a swapped page.
1433 * Bit 62 and 63 are used to distinguish the different page types. For
1434 * a swapped page these bits need to be zero.
1435 * This leaves the bits 0-51 and bits 56-61 to store type and offset.
1436 * We use the 5 bits from 57-61 for the type and the 53 bits from 0-51
1437 * plus 56 for the offset.
1438 * | offset |0110|o|type |00|
1439 * 0000000000111111111122222222223333333333444444444455 5555 5 55566 66
1440 * 0123456789012345678901234567890123456789012345678901 2345 6 78901 23
1441 */
Heiko Carstensf4815ac2012-05-23 16:24:51 +02001442#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443#define __SWP_OFFSET_MASK (~0UL >> 12)
1444#else
1445#define __SWP_OFFSET_MASK (~0UL >> 11)
1446#endif
Adrian Bunk4448aaf2005-11-08 21:34:42 -08001447static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 pte_t pte;
1450 offset &= __SWP_OFFSET_MASK;
Gerald Schaefer9282ed92006-09-20 15:59:37 +02001451 pte_val(pte) = _PAGE_TYPE_SWAP | ((type & 0x1f) << 2) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 ((offset & 1UL) << 7) | ((offset & ~1UL) << 11);
1453 return pte;
1454}
1455
1456#define __swp_type(entry) (((entry).val >> 2) & 0x1f)
1457#define __swp_offset(entry) (((entry).val >> 11) | (((entry).val >> 7) & 1))
1458#define __swp_entry(type,offset) ((swp_entry_t) { pte_val(mk_swap_pte((type),(offset))) })
1459
1460#define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
1461#define __swp_entry_to_pte(x) ((pte_t) { (x).val })
1462
Heiko Carstensf4815ac2012-05-23 16:24:51 +02001463#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464# define PTE_FILE_MAX_BITS 26
Heiko Carstensf4815ac2012-05-23 16:24:51 +02001465#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466# define PTE_FILE_MAX_BITS 59
Heiko Carstensf4815ac2012-05-23 16:24:51 +02001467#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469#define pte_to_pgoff(__pte) \
1470 ((((__pte).pte >> 12) << 7) + (((__pte).pte >> 1) & 0x7f))
1471
1472#define pgoff_to_pte(__off) \
1473 ((pte_t) { ((((__off) & 0x7f) << 1) + (((__off) >> 7) << 12)) \
Gerald Schaefer9282ed92006-09-20 15:59:37 +02001474 | _PAGE_TYPE_FILE })
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476#endif /* !__ASSEMBLY__ */
1477
1478#define kern_addr_valid(addr) (1)
1479
Heiko Carstens17f34582008-04-30 13:38:47 +02001480extern int vmem_add_mapping(unsigned long start, unsigned long size);
1481extern int vmem_remove_mapping(unsigned long start, unsigned long size);
Carsten Otte402b0862008-03-25 18:47:10 +01001482extern int s390_enable_sie(void);
Heiko Carstensf4eb07c2006-12-08 15:56:07 +01001483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484/*
1485 * No page table caches to initialise
1486 */
1487#define pgtable_cache_init() do { } while (0)
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489#include <asm-generic/pgtable.h>
1490
1491#endif /* _S390_PAGE_H */