blob: ed14fc2db6e0e98abf5805760eb92602bb5a3547 [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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/*
Martin Schwidefsky238ec4e2010-10-25 16:10:07 +020047 * ZERO_PAGE is a global shared page that is always zero; used
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * for zero-mapped memory areas etc..
49 */
Martin Schwidefsky238ec4e2010-10-25 16:10:07 +020050
51extern unsigned long empty_zero_page;
52extern unsigned long zero_page_mask;
53
54#define ZERO_PAGE(vaddr) \
55 (virt_to_page((void *)(empty_zero_page + \
56 (((unsigned long)(vaddr)) &zero_page_mask))))
57
58#define is_zero_pfn is_zero_pfn
59static inline int is_zero_pfn(unsigned long pfn)
60{
61 extern unsigned long zero_pfn;
62 unsigned long offset_from_zero_pfn = pfn - zero_pfn;
63 return offset_from_zero_pfn <= (zero_page_mask >> PAGE_SHIFT);
64}
65
66#define my_zero_pfn(addr) page_to_pfn(ZERO_PAGE(addr))
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#endif /* !__ASSEMBLY__ */
69
70/*
71 * PMD_SHIFT determines the size of the area a second-level page
72 * table can map
73 * PGDIR_SHIFT determines what a third-level page table entry can map
74 */
Heiko Carstensf4815ac2012-05-23 16:24:51 +020075#ifndef CONFIG_64BIT
Martin Schwidefsky146e4b32008-02-09 18:24:35 +010076# define PMD_SHIFT 20
77# define PUD_SHIFT 20
78# define PGDIR_SHIFT 20
Heiko Carstensf4815ac2012-05-23 16:24:51 +020079#else /* CONFIG_64BIT */
Martin Schwidefsky146e4b32008-02-09 18:24:35 +010080# define PMD_SHIFT 20
Martin Schwidefsky190a1d72007-10-22 12:52:48 +020081# define PUD_SHIFT 31
Martin Schwidefsky5a216a22008-02-09 18:24:36 +010082# define PGDIR_SHIFT 42
Heiko Carstensf4815ac2012-05-23 16:24:51 +020083#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85#define PMD_SIZE (1UL << PMD_SHIFT)
86#define PMD_MASK (~(PMD_SIZE-1))
Martin Schwidefsky190a1d72007-10-22 12:52:48 +020087#define PUD_SIZE (1UL << PUD_SHIFT)
88#define PUD_MASK (~(PUD_SIZE-1))
Martin Schwidefsky5a216a22008-02-09 18:24:36 +010089#define PGDIR_SIZE (1UL << PGDIR_SHIFT)
90#define PGDIR_MASK (~(PGDIR_SIZE-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/*
93 * entries per page directory level: the S390 is two-level, so
94 * we don't really have any PMD directory physically.
95 * for S390 segment-table entries are combined to one PGD
96 * that leads to 1024 pte per pgd
97 */
Martin Schwidefsky146e4b32008-02-09 18:24:35 +010098#define PTRS_PER_PTE 256
Heiko Carstensf4815ac2012-05-23 16:24:51 +020099#ifndef CONFIG_64BIT
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100100#define PTRS_PER_PMD 1
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100101#define PTRS_PER_PUD 1
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200102#else /* CONFIG_64BIT */
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100103#define PTRS_PER_PMD 2048
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100104#define PTRS_PER_PUD 2048
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200105#endif /* CONFIG_64BIT */
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100106#define PTRS_PER_PGD 2048
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Hugh Dickinsd455a362005-04-19 13:29:23 -0700108#define FIRST_USER_ADDRESS 0
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define pte_ERROR(e) \
111 printk("%s:%d: bad pte %p.\n", __FILE__, __LINE__, (void *) pte_val(e))
112#define pmd_ERROR(e) \
113 printk("%s:%d: bad pmd %p.\n", __FILE__, __LINE__, (void *) pmd_val(e))
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200114#define pud_ERROR(e) \
115 printk("%s:%d: bad pud %p.\n", __FILE__, __LINE__, (void *) pud_val(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define pgd_ERROR(e) \
117 printk("%s:%d: bad pgd %p.\n", __FILE__, __LINE__, (void *) pgd_val(e))
118
119#ifndef __ASSEMBLY__
120/*
Christian Borntraeger5fd9c6e2008-01-26 14:11:00 +0100121 * The vmalloc area will always be on the topmost area of the kernel
Martin Schwidefsky7d3f6612010-04-09 13:43:02 +0200122 * mapping. We reserve 96MB (31bit) / 128GB (64bit) for vmalloc,
Christian Borntraeger5fd9c6e2008-01-26 14:11:00 +0100123 * which should be enough for any sane case.
124 * By putting vmalloc at the top, we maximise the gap between physical
125 * memory and vmalloc to catch misplaced memory accesses. As a side
126 * effect, this also makes sure that 64 bit module code cannot be used
127 * as system call address.
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/*
136 * A 31 bit pagetable entry of S390 has following format:
137 * | PFRA | | OS |
138 * 0 0IP0
139 * 00000000001111111111222222222233
140 * 01234567890123456789012345678901
141 *
142 * I Page-Invalid Bit: Page is not available for address-translation
143 * P Page-Protection Bit: Store access not possible for page
144 *
145 * A 31 bit segmenttable entry of S390 has following format:
146 * | P-table origin | |PTL
147 * 0 IC
148 * 00000000001111111111222222222233
149 * 01234567890123456789012345678901
150 *
151 * I Segment-Invalid Bit: Segment is not available for address-translation
152 * C Common-Segment Bit: Segment is not private (PoP 3-30)
153 * PTL Page-Table-Length: Page-table length (PTL+1*16 entries -> up to 256)
154 *
155 * The 31 bit segmenttable origin of S390 has following format:
156 *
157 * |S-table origin | | STL |
158 * X **GPS
159 * 00000000001111111111222222222233
160 * 01234567890123456789012345678901
161 *
162 * X Space-Switch event:
163 * G Segment-Invalid Bit: *
164 * P Private-Space Bit: Segment is not private (PoP 3-30)
165 * S Storage-Alteration:
166 * STL Segment-Table-Length: Segment-table length (STL+1*16 entries -> up to 2048)
167 *
168 * A 64 bit pagetable entry of S390 has following format:
Christian Borntraeger6a985c62009-12-07 12:52:11 +0100169 * | PFRA |0IPC| OS |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * 0000000000111111111122222222223333333333444444444455555555556666
171 * 0123456789012345678901234567890123456789012345678901234567890123
172 *
173 * I Page-Invalid Bit: Page is not available for address-translation
174 * P Page-Protection Bit: Store access not possible for page
Christian Borntraeger6a985c62009-12-07 12:52:11 +0100175 * C Change-bit override: HW is not required to set change bit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 *
177 * A 64 bit segmenttable entry of S390 has following format:
178 * | P-table origin | TT
179 * 0000000000111111111122222222223333333333444444444455555555556666
180 * 0123456789012345678901234567890123456789012345678901234567890123
181 *
182 * I Segment-Invalid Bit: Segment is not available for address-translation
183 * C Common-Segment Bit: Segment is not private (PoP 3-30)
184 * P Page-Protection Bit: Store access not possible for page
185 * TT Type 00
186 *
187 * A 64 bit region table entry of S390 has following format:
188 * | S-table origin | TF TTTL
189 * 0000000000111111111122222222223333333333444444444455555555556666
190 * 0123456789012345678901234567890123456789012345678901234567890123
191 *
192 * I Segment-Invalid Bit: Segment is not available for address-translation
193 * TT Type 01
194 * TF
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200195 * TL Table length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 *
197 * The 64 bit regiontable origin of S390 has following format:
198 * | region table origon | DTTL
199 * 0000000000111111111122222222223333333333444444444455555555556666
200 * 0123456789012345678901234567890123456789012345678901234567890123
201 *
202 * X Space-Switch event:
203 * G Segment-Invalid Bit:
204 * P Private-Space Bit:
205 * S Storage-Alteration:
206 * R Real space
207 * TL Table-Length:
208 *
209 * A storage key has the following format:
210 * | ACC |F|R|C|0|
211 * 0 3 4 5 6 7
212 * ACC: access key
213 * F : fetch protection bit
214 * R : referenced bit
215 * C : changed bit
216 */
217
218/* Hardware bits in the page table entry */
Christian Borntraeger6a985c62009-12-07 12:52:11 +0100219#define _PAGE_CO 0x100 /* HW Change-bit override */
Martin Schwidefsky83377482006-10-18 18:30:51 +0200220#define _PAGE_RO 0x200 /* HW read-only bit */
221#define _PAGE_INVALID 0x400 /* HW invalid bit */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200222
223/* Software bits in the page table entry */
Martin Schwidefsky83377482006-10-18 18:30:51 +0200224#define _PAGE_SWT 0x001 /* SW pte type bit t */
225#define _PAGE_SWX 0x002 /* SW pte type bit x */
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200226#define _PAGE_SWC 0x004 /* SW pte changed bit (for KVM) */
227#define _PAGE_SWR 0x008 /* SW pte referenced bit (for KVM) */
228#define _PAGE_SPECIAL 0x010 /* SW associated with special page */
Nick Piggina08cb622008-04-28 02:13:03 -0700229#define __HAVE_ARCH_PTE_SPECIAL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Nick Piggin138c9022008-07-08 11:31:06 +0200231/* Set of bits not changed in pte_modify */
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200232#define _PAGE_CHG_MASK (PAGE_MASK | _PAGE_SPECIAL | _PAGE_SWC | _PAGE_SWR)
Nick Piggin138c9022008-07-08 11:31:06 +0200233
Martin Schwidefsky83377482006-10-18 18:30:51 +0200234/* Six different types of pages. */
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200235#define _PAGE_TYPE_EMPTY 0x400
236#define _PAGE_TYPE_NONE 0x401
Martin Schwidefsky83377482006-10-18 18:30:51 +0200237#define _PAGE_TYPE_SWAP 0x403
238#define _PAGE_TYPE_FILE 0x601 /* bit 0x002 is used for offset !! */
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200239#define _PAGE_TYPE_RO 0x200
240#define _PAGE_TYPE_RW 0x000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Martin Schwidefsky83377482006-10-18 18:30:51 +0200242/*
Gerald Schaefer53492b12008-04-30 13:38:46 +0200243 * Only four types for huge pages, using the invalid bit and protection bit
244 * of a segment table entry.
245 */
246#define _HPAGE_TYPE_EMPTY 0x020 /* _SEGMENT_ENTRY_INV */
247#define _HPAGE_TYPE_NONE 0x220
248#define _HPAGE_TYPE_RO 0x200 /* _SEGMENT_ENTRY_RO */
249#define _HPAGE_TYPE_RW 0x000
250
251/*
Martin Schwidefsky83377482006-10-18 18:30:51 +0200252 * PTE type bits are rather complicated. handle_pte_fault uses pte_present,
253 * pte_none and pte_file to find out the pte type WITHOUT holding the page
254 * table lock. ptep_clear_flush on the other hand uses ptep_clear_flush to
255 * invalidate a given pte. ipte sets the hw invalid bit and clears all tlbs
256 * for the page. The page table entry is set to _PAGE_TYPE_EMPTY afterwards.
257 * This change is done while holding the lock, but the intermediate step
258 * of a previously valid pte with the hw invalid bit set can be observed by
259 * handle_pte_fault. That makes it necessary that all valid pte types with
260 * the hw invalid bit set must be distinguishable from the four pte types
261 * empty, none, swap and file.
262 *
263 * irxt ipte irxt
264 * _PAGE_TYPE_EMPTY 1000 -> 1000
265 * _PAGE_TYPE_NONE 1001 -> 1001
266 * _PAGE_TYPE_SWAP 1011 -> 1011
267 * _PAGE_TYPE_FILE 11?1 -> 11?1
268 * _PAGE_TYPE_RO 0100 -> 1100
269 * _PAGE_TYPE_RW 0000 -> 1000
270 *
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100271 * pte_none is true for bits combinations 1000, 1010, 1100, 1110
Martin Schwidefsky83377482006-10-18 18:30:51 +0200272 * pte_present is true for bits combinations 0000, 0010, 0100, 0110, 1001
273 * pte_file is true for bits combinations 1101, 1111
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100274 * swap pte is 1011 and 0001, 0011, 0101, 0111 are invalid.
Martin Schwidefsky83377482006-10-18 18:30:51 +0200275 */
276
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200277#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200279/* Bits in the segment table address-space-control-element */
280#define _ASCE_SPACE_SWITCH 0x80000000UL /* space switch event */
281#define _ASCE_ORIGIN_MASK 0x7ffff000UL /* segment table origin */
282#define _ASCE_PRIVATE_SPACE 0x100 /* private space control */
283#define _ASCE_ALT_EVENT 0x80 /* storage alteration event control */
284#define _ASCE_TABLE_LENGTH 0x7f /* 128 x 64 entries = 8k */
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/* Bits in the segment table entry */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200287#define _SEGMENT_ENTRY_ORIGIN 0x7fffffc0UL /* page table origin */
Martin Schwidefsky80217142010-10-25 16:10:11 +0200288#define _SEGMENT_ENTRY_RO 0x200 /* page protection bit */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200289#define _SEGMENT_ENTRY_INV 0x20 /* invalid segment table entry */
290#define _SEGMENT_ENTRY_COMMON 0x10 /* common segment bit */
291#define _SEGMENT_ENTRY_PTL 0x0f /* page table length */
292
293#define _SEGMENT_ENTRY (_SEGMENT_ENTRY_PTL)
294#define _SEGMENT_ENTRY_EMPTY (_SEGMENT_ENTRY_INV)
295
Martin Schwidefsky6c61cfe2011-06-06 14:14:42 +0200296/* Page status table bits for virtualization */
297#define RCP_ACC_BITS 0xf0000000UL
298#define RCP_FP_BIT 0x08000000UL
299#define RCP_PCL_BIT 0x00800000UL
300#define RCP_HR_BIT 0x00400000UL
301#define RCP_HC_BIT 0x00200000UL
302#define RCP_GR_BIT 0x00040000UL
303#define RCP_GC_BIT 0x00020000UL
304
305/* User dirty / referenced bit for KVM's migration feature */
306#define KVM_UR_BIT 0x00008000UL
307#define KVM_UC_BIT 0x00004000UL
308
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200309#else /* CONFIG_64BIT */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200310
311/* Bits in the segment/region table address-space-control-element */
312#define _ASCE_ORIGIN ~0xfffUL/* segment table origin */
313#define _ASCE_PRIVATE_SPACE 0x100 /* private space control */
314#define _ASCE_ALT_EVENT 0x80 /* storage alteration event control */
315#define _ASCE_SPACE_SWITCH 0x40 /* space switch event */
316#define _ASCE_REAL_SPACE 0x20 /* real space control */
317#define _ASCE_TYPE_MASK 0x0c /* asce table type mask */
318#define _ASCE_TYPE_REGION1 0x0c /* region first table type */
319#define _ASCE_TYPE_REGION2 0x08 /* region second table type */
320#define _ASCE_TYPE_REGION3 0x04 /* region third table type */
321#define _ASCE_TYPE_SEGMENT 0x00 /* segment table type */
322#define _ASCE_TABLE_LENGTH 0x03 /* region table length */
323
324/* Bits in the region table entry */
325#define _REGION_ENTRY_ORIGIN ~0xfffUL/* region/segment table origin */
326#define _REGION_ENTRY_INV 0x20 /* invalid region table entry */
327#define _REGION_ENTRY_TYPE_MASK 0x0c /* region/segment table type mask */
328#define _REGION_ENTRY_TYPE_R1 0x0c /* region first table type */
329#define _REGION_ENTRY_TYPE_R2 0x08 /* region second table type */
330#define _REGION_ENTRY_TYPE_R3 0x04 /* region third table type */
331#define _REGION_ENTRY_LENGTH 0x03 /* region third length */
332
333#define _REGION1_ENTRY (_REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_LENGTH)
334#define _REGION1_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INV)
335#define _REGION2_ENTRY (_REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_LENGTH)
336#define _REGION2_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INV)
337#define _REGION3_ENTRY (_REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_LENGTH)
338#define _REGION3_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INV)
339
340/* Bits in the segment table entry */
341#define _SEGMENT_ENTRY_ORIGIN ~0x7ffUL/* segment table origin */
342#define _SEGMENT_ENTRY_RO 0x200 /* page protection bit */
343#define _SEGMENT_ENTRY_INV 0x20 /* invalid segment table entry */
344
345#define _SEGMENT_ENTRY (0)
346#define _SEGMENT_ENTRY_EMPTY (_SEGMENT_ENTRY_INV)
347
Gerald Schaefer53492b12008-04-30 13:38:46 +0200348#define _SEGMENT_ENTRY_LARGE 0x400 /* STE-format control, large page */
349#define _SEGMENT_ENTRY_CO 0x100 /* change-recording override */
Gerald Schaefer75077af2012-10-08 16:30:15 -0700350#define _SEGMENT_ENTRY_SPLIT_BIT 0 /* THP splitting bit number */
351#define _SEGMENT_ENTRY_SPLIT (1UL << _SEGMENT_ENTRY_SPLIT_BIT)
Gerald Schaefer53492b12008-04-30 13:38:46 +0200352
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -0700353/* Set of bits not changed in pmd_modify */
354#define _SEGMENT_CHG_MASK (_SEGMENT_ENTRY_ORIGIN | _SEGMENT_ENTRY_LARGE \
355 | _SEGMENT_ENTRY_SPLIT | _SEGMENT_ENTRY_CO)
356
Martin Schwidefsky6c61cfe2011-06-06 14:14:42 +0200357/* Page status table bits for virtualization */
358#define RCP_ACC_BITS 0xf000000000000000UL
359#define RCP_FP_BIT 0x0800000000000000UL
360#define RCP_PCL_BIT 0x0080000000000000UL
361#define RCP_HR_BIT 0x0040000000000000UL
362#define RCP_HC_BIT 0x0020000000000000UL
363#define RCP_GR_BIT 0x0004000000000000UL
364#define RCP_GC_BIT 0x0002000000000000UL
365
366/* User dirty / referenced bit for KVM's migration feature */
367#define KVM_UR_BIT 0x0000800000000000UL
368#define KVM_UC_BIT 0x0000400000000000UL
369
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200370#endif /* CONFIG_64BIT */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200371
372/*
373 * A user page table pointer has the space-switch-event bit, the
374 * private-space-control bit and the storage-alteration-event-control
375 * bit set. A kernel page table pointer doesn't need them.
376 */
377#define _ASCE_USER_BITS (_ASCE_SPACE_SWITCH | _ASCE_PRIVATE_SPACE | \
378 _ASCE_ALT_EVENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/*
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200381 * Page protection definitions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200383#define PAGE_NONE __pgprot(_PAGE_TYPE_NONE)
384#define PAGE_RO __pgprot(_PAGE_TYPE_RO)
385#define PAGE_RW __pgprot(_PAGE_TYPE_RW)
386
387#define PAGE_KERNEL PAGE_RW
388#define PAGE_COPY PAGE_RO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390/*
Martin Schwidefsky043d0702011-05-23 10:24:23 +0200391 * On s390 the page table entry has an invalid bit and a read-only bit.
392 * Read permission implies execute permission and write permission
393 * implies read permission.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 */
395 /*xwr*/
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200396#define __P000 PAGE_NONE
397#define __P001 PAGE_RO
398#define __P010 PAGE_RO
399#define __P011 PAGE_RO
Martin Schwidefsky043d0702011-05-23 10:24:23 +0200400#define __P100 PAGE_RO
401#define __P101 PAGE_RO
402#define __P110 PAGE_RO
403#define __P111 PAGE_RO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200405#define __S000 PAGE_NONE
406#define __S001 PAGE_RO
407#define __S010 PAGE_RW
408#define __S011 PAGE_RW
Martin Schwidefsky043d0702011-05-23 10:24:23 +0200409#define __S100 PAGE_RO
410#define __S101 PAGE_RO
411#define __S110 PAGE_RW
412#define __S111 PAGE_RW
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200414static inline int mm_exclusive(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200416 return likely(mm == current->active_mm &&
417 atomic_read(&mm->context.attach_count) <= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200420static inline int mm_has_pgste(struct mm_struct *mm)
421{
422#ifdef CONFIG_PGSTE
423 if (unlikely(mm->context.has_pgste))
424 return 1;
425#endif
426 return 0;
427}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/*
429 * pgd/pmd/pte query functions
430 */
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200431#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800433static inline int pgd_present(pgd_t pgd) { return 1; }
434static inline int pgd_none(pgd_t pgd) { return 0; }
435static inline int pgd_bad(pgd_t pgd) { return 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200437static inline int pud_present(pud_t pud) { return 1; }
438static inline int pud_none(pud_t pud) { return 0; }
439static inline int pud_bad(pud_t pud) { return 0; }
440
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200441#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100443static inline int pgd_present(pgd_t pgd)
444{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100445 if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R2)
446 return 1;
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100447 return (pgd_val(pgd) & _REGION_ENTRY_ORIGIN) != 0UL;
448}
449
450static inline int pgd_none(pgd_t pgd)
451{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100452 if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R2)
453 return 0;
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100454 return (pgd_val(pgd) & _REGION_ENTRY_INV) != 0UL;
455}
456
457static inline int pgd_bad(pgd_t pgd)
458{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100459 /*
460 * With dynamic page table levels the pgd can be a region table
461 * entry or a segment table entry. Check for the bit that are
462 * invalid for either table entry.
463 */
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100464 unsigned long mask =
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100465 ~_SEGMENT_ENTRY_ORIGIN & ~_REGION_ENTRY_INV &
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100466 ~_REGION_ENTRY_TYPE_MASK & ~_REGION_ENTRY_LENGTH;
467 return (pgd_val(pgd) & mask) != 0;
468}
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200469
470static inline int pud_present(pud_t pud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100472 if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R3)
473 return 1;
Martin Schwidefsky0d017922007-12-17 16:25:48 +0100474 return (pud_val(pud) & _REGION_ENTRY_ORIGIN) != 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200477static inline int pud_none(pud_t pud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100479 if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R3)
480 return 0;
Martin Schwidefsky0d017922007-12-17 16:25:48 +0100481 return (pud_val(pud) & _REGION_ENTRY_INV) != 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200484static inline int pud_bad(pud_t pud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100486 /*
487 * With dynamic page table levels the pud can be a region table
488 * entry or a segment table entry. Check for the bit that are
489 * invalid for either table entry.
490 */
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100491 unsigned long mask =
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100492 ~_SEGMENT_ENTRY_ORIGIN & ~_REGION_ENTRY_INV &
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100493 ~_REGION_ENTRY_TYPE_MASK & ~_REGION_ENTRY_LENGTH;
494 return (pud_val(pud) & mask) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200497#endif /* CONFIG_64BIT */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200498
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800499static inline int pmd_present(pmd_t pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Martin Schwidefsky0d017922007-12-17 16:25:48 +0100501 return (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN) != 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800504static inline int pmd_none(pmd_t pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Martin Schwidefsky0d017922007-12-17 16:25:48 +0100506 return (pmd_val(pmd) & _SEGMENT_ENTRY_INV) != 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800509static inline int pmd_bad(pmd_t pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200511 unsigned long mask = ~_SEGMENT_ENTRY_ORIGIN & ~_SEGMENT_ENTRY_INV;
512 return (pmd_val(pmd) & mask) != _SEGMENT_ENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Gerald Schaefer75077af2012-10-08 16:30:15 -0700515#define __HAVE_ARCH_PMDP_SPLITTING_FLUSH
516extern void pmdp_splitting_flush(struct vm_area_struct *vma,
517 unsigned long addr, pmd_t *pmdp);
518
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -0700519#define __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
520extern int pmdp_set_access_flags(struct vm_area_struct *vma,
521 unsigned long address, pmd_t *pmdp,
522 pmd_t entry, int dirty);
523
524#define __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
525extern int pmdp_clear_flush_young(struct vm_area_struct *vma,
526 unsigned long address, pmd_t *pmdp);
527
528#define __HAVE_ARCH_PMD_WRITE
529static inline int pmd_write(pmd_t pmd)
530{
531 return (pmd_val(pmd) & _SEGMENT_ENTRY_RO) == 0;
532}
533
534static inline int pmd_young(pmd_t pmd)
535{
536 return 0;
537}
538
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800539static inline int pte_none(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Martin Schwidefsky83377482006-10-18 18:30:51 +0200541 return (pte_val(pte) & _PAGE_INVALID) && !(pte_val(pte) & _PAGE_SWT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800544static inline int pte_present(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Martin Schwidefsky83377482006-10-18 18:30:51 +0200546 unsigned long mask = _PAGE_RO | _PAGE_INVALID | _PAGE_SWT | _PAGE_SWX;
547 return (pte_val(pte) & mask) == _PAGE_TYPE_NONE ||
548 (!(pte_val(pte) & _PAGE_INVALID) &&
549 !(pte_val(pte) & _PAGE_SWT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800552static inline int pte_file(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Martin Schwidefsky83377482006-10-18 18:30:51 +0200554 unsigned long mask = _PAGE_RO | _PAGE_INVALID | _PAGE_SWT;
555 return (pte_val(pte) & mask) == _PAGE_TYPE_FILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Nick Piggin7e675132008-04-28 02:13:00 -0700558static inline int pte_special(pte_t pte)
559{
Nick Piggina08cb622008-04-28 02:13:03 -0700560 return (pte_val(pte) & _PAGE_SPECIAL);
Nick Piggin7e675132008-04-28 02:13:00 -0700561}
562
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200563#define __HAVE_ARCH_PTE_SAME
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200564static inline int pte_same(pte_t a, pte_t b)
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100565{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200566 return pte_val(a) == pte_val(b);
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100567}
568
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200569static inline pgste_t pgste_get_lock(pte_t *ptep)
570{
571 unsigned long new = 0;
572#ifdef CONFIG_PGSTE
573 unsigned long old;
574
575 preempt_disable();
576 asm(
577 " lg %0,%2\n"
578 "0: lgr %1,%0\n"
579 " nihh %0,0xff7f\n" /* clear RCP_PCL_BIT in old */
580 " oihh %1,0x0080\n" /* set RCP_PCL_BIT in new */
581 " csg %0,%1,%2\n"
582 " jl 0b\n"
583 : "=&d" (old), "=&d" (new), "=Q" (ptep[PTRS_PER_PTE])
584 : "Q" (ptep[PTRS_PER_PTE]) : "cc");
585#endif
586 return __pgste(new);
587}
588
589static inline void pgste_set_unlock(pte_t *ptep, pgste_t pgste)
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100590{
591#ifdef CONFIG_PGSTE
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200592 asm(
593 " nihh %1,0xff7f\n" /* clear RCP_PCL_BIT */
594 " stg %1,%0\n"
595 : "=Q" (ptep[PTRS_PER_PTE])
596 : "d" (pgste_val(pgste)), "Q" (ptep[PTRS_PER_PTE]) : "cc");
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100597 preempt_enable();
598#endif
599}
600
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200601static inline pgste_t pgste_update_all(pte_t *ptep, pgste_t pgste)
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100602{
603#ifdef CONFIG_PGSTE
Heiko Carstensa43a9d92011-05-29 12:40:50 +0200604 unsigned long address, bits;
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200605 unsigned char skey;
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100606
Martin Schwidefsky09b53882011-11-14 11:19:00 +0100607 if (!pte_present(*ptep))
608 return pgste;
Heiko Carstensa43a9d92011-05-29 12:40:50 +0200609 address = pte_val(*ptep) & PAGE_MASK;
610 skey = page_get_storage_key(address);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200611 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
612 /* Clear page changed & referenced bit in the storage key */
Carsten Otte7c818782011-12-01 13:32:16 +0100613 if (bits & _PAGE_CHANGED)
614 page_set_storage_key(address, skey ^ bits, 1);
615 else if (bits)
616 page_reset_referenced(address);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200617 /* Transfer page changed & referenced bit to guest bits in pgste */
618 pgste_val(pgste) |= bits << 48; /* RCP_GR_BIT & RCP_GC_BIT */
619 /* Get host changed & referenced bits from pgste */
620 bits |= (pgste_val(pgste) & (RCP_HR_BIT | RCP_HC_BIT)) >> 52;
621 /* Clear host bits in pgste. */
622 pgste_val(pgste) &= ~(RCP_HR_BIT | RCP_HC_BIT);
623 pgste_val(pgste) &= ~(RCP_ACC_BITS | RCP_FP_BIT);
624 /* Copy page access key and fetch protection bit to pgste */
625 pgste_val(pgste) |=
626 (unsigned long) (skey & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
627 /* Transfer changed and referenced to kvm user bits */
628 pgste_val(pgste) |= bits << 45; /* KVM_UR_BIT & KVM_UC_BIT */
629 /* Transfer changed & referenced to pte sofware bits */
630 pte_val(*ptep) |= bits << 1; /* _PAGE_SWR & _PAGE_SWC */
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100631#endif
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200632 return pgste;
633
634}
635
636static inline pgste_t pgste_update_young(pte_t *ptep, pgste_t pgste)
637{
638#ifdef CONFIG_PGSTE
639 int young;
640
Martin Schwidefsky09b53882011-11-14 11:19:00 +0100641 if (!pte_present(*ptep))
642 return pgste;
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200643 young = page_reset_referenced(pte_val(*ptep) & PAGE_MASK);
644 /* Transfer page referenced bit to pte software bit (host view) */
645 if (young || (pgste_val(pgste) & RCP_HR_BIT))
646 pte_val(*ptep) |= _PAGE_SWR;
647 /* Clear host referenced bit in pgste. */
648 pgste_val(pgste) &= ~RCP_HR_BIT;
649 /* Transfer page referenced bit to guest bit in pgste */
650 pgste_val(pgste) |= (unsigned long) young << 50; /* set RCP_GR_BIT */
651#endif
652 return pgste;
653
654}
655
Martin Schwidefsky09b53882011-11-14 11:19:00 +0100656static inline void pgste_set_pte(pte_t *ptep, pgste_t pgste, pte_t entry)
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200657{
658#ifdef CONFIG_PGSTE
Heiko Carstensa43a9d92011-05-29 12:40:50 +0200659 unsigned long address;
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200660 unsigned long okey, nkey;
661
Martin Schwidefsky09b53882011-11-14 11:19:00 +0100662 if (!pte_present(entry))
663 return;
664 address = pte_val(entry) & PAGE_MASK;
Heiko Carstensa43a9d92011-05-29 12:40:50 +0200665 okey = nkey = page_get_storage_key(address);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200666 nkey &= ~(_PAGE_ACC_BITS | _PAGE_FP_BIT);
667 /* Set page access key and fetch protection bit from pgste */
668 nkey |= (pgste_val(pgste) & (RCP_ACC_BITS | RCP_FP_BIT)) >> 56;
669 if (okey != nkey)
Heiko Carstensa43a9d92011-05-29 12:40:50 +0200670 page_set_storage_key(address, nkey, 1);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200671#endif
672}
673
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200674/**
675 * struct gmap_struct - guest address space
676 * @mm: pointer to the parent mm_struct
677 * @table: pointer to the page directory
Christian Borntraeger480e5922011-09-20 17:07:28 +0200678 * @asce: address space control element for gmap page table
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200679 * @crst_list: list of all crst tables used in the guest address space
680 */
681struct gmap {
682 struct list_head list;
683 struct mm_struct *mm;
684 unsigned long *table;
Christian Borntraeger480e5922011-09-20 17:07:28 +0200685 unsigned long asce;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200686 struct list_head crst_list;
687};
688
689/**
690 * struct gmap_rmap - reverse mapping for segment table entries
691 * @next: pointer to the next gmap_rmap structure in the list
692 * @entry: pointer to a segment table entry
693 */
694struct gmap_rmap {
695 struct list_head list;
696 unsigned long *entry;
697};
698
699/**
700 * struct gmap_pgtable - gmap information attached to a page table
701 * @vmaddr: address of the 1MB segment in the process virtual memory
702 * @mapper: list of segment table entries maping a page table
703 */
704struct gmap_pgtable {
705 unsigned long vmaddr;
706 struct list_head mapper;
707};
708
709struct gmap *gmap_alloc(struct mm_struct *mm);
710void gmap_free(struct gmap *gmap);
711void gmap_enable(struct gmap *gmap);
712void gmap_disable(struct gmap *gmap);
713int gmap_map_segment(struct gmap *gmap, unsigned long from,
714 unsigned long to, unsigned long length);
715int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len);
Carsten Otte499069e2011-10-30 15:17:02 +0100716unsigned long __gmap_fault(unsigned long address, struct gmap *);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200717unsigned long gmap_fault(unsigned long address, struct gmap *);
Christian Borntraeger388186b2011-10-30 15:17:03 +0100718void gmap_discard(unsigned long from, unsigned long to, struct gmap *);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200719
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200720/*
721 * Certain architectures need to do special things when PTEs
722 * within a page table are directly modified. Thus, the following
723 * hook is made available.
724 */
725static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
726 pte_t *ptep, pte_t entry)
727{
728 pgste_t pgste;
729
730 if (mm_has_pgste(mm)) {
731 pgste = pgste_get_lock(ptep);
Martin Schwidefsky09b53882011-11-14 11:19:00 +0100732 pgste_set_pte(ptep, pgste, entry);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200733 *ptep = entry;
734 pgste_set_unlock(ptep, pgste);
735 } else
736 *ptep = entry;
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100737}
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739/*
740 * query functions pte_write/pte_dirty/pte_young only work if
741 * pte_present() is true. Undefined behaviour if not..
742 */
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800743static inline int pte_write(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 return (pte_val(pte) & _PAGE_RO) == 0;
746}
747
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800748static inline int pte_dirty(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200750#ifdef CONFIG_PGSTE
751 if (pte_val(pte) & _PAGE_SWC)
752 return 1;
753#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return 0;
755}
756
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800757static inline int pte_young(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200759#ifdef CONFIG_PGSTE
760 if (pte_val(pte) & _PAGE_SWR)
761 return 1;
762#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return 0;
764}
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766/*
767 * pgd/pmd/pte modification functions
768 */
769
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200770static inline void pgd_clear(pgd_t *pgd)
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100771{
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200772#ifdef CONFIG_64BIT
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100773 if ((pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
774 pgd_val(*pgd) = _REGION2_ENTRY_EMPTY;
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200775#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100778static inline void pud_clear(pud_t *pud)
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100779{
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200780#ifdef CONFIG_64BIT
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200781 if ((pud_val(*pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
782 pud_val(*pud) = _REGION3_ENTRY_EMPTY;
783#endif
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100784}
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100785
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200786static inline void pmd_clear(pmd_t *pmdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200788 pmd_val(*pmdp) = _SEGMENT_ENTRY_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800791static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200793 pte_val(*ptep) = _PAGE_TYPE_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
796/*
797 * The following pte modification functions only work if
798 * pte_present() is true. Undefined behaviour if not..
799 */
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800800static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Nick Piggin138c9022008-07-08 11:31:06 +0200802 pte_val(pte) &= _PAGE_CHG_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 pte_val(pte) |= pgprot_val(newprot);
804 return pte;
805}
806
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800807static inline pte_t pte_wrprotect(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200809 /* Do not clobber _PAGE_TYPE_NONE pages! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (!(pte_val(pte) & _PAGE_INVALID))
811 pte_val(pte) |= _PAGE_RO;
812 return pte;
813}
814
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800815static inline pte_t pte_mkwrite(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 pte_val(pte) &= ~_PAGE_RO;
818 return pte;
819}
820
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800821static inline pte_t pte_mkclean(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200823#ifdef CONFIG_PGSTE
824 pte_val(pte) &= ~_PAGE_SWC;
825#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return pte;
827}
828
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800829static inline pte_t pte_mkdirty(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return pte;
832}
833
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800834static inline pte_t pte_mkold(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200836#ifdef CONFIG_PGSTE
837 pte_val(pte) &= ~_PAGE_SWR;
838#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return pte;
840}
841
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800842static inline pte_t pte_mkyoung(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return pte;
845}
846
Nick Piggin7e675132008-04-28 02:13:00 -0700847static inline pte_t pte_mkspecial(pte_t pte)
848{
Nick Piggina08cb622008-04-28 02:13:03 -0700849 pte_val(pte) |= _PAGE_SPECIAL;
Nick Piggin7e675132008-04-28 02:13:00 -0700850 return pte;
851}
852
Heiko Carstens84afdce2010-10-25 16:10:36 +0200853#ifdef CONFIG_HUGETLB_PAGE
854static inline pte_t pte_mkhuge(pte_t pte)
855{
856 /*
857 * PROT_NONE needs to be remapped from the pte type to the ste type.
858 * The HW invalid bit is also different for pte and ste. The pte
859 * invalid bit happens to be the same as the ste _SEGMENT_ENTRY_LARGE
860 * bit, so we don't have to clear it.
861 */
862 if (pte_val(pte) & _PAGE_INVALID) {
863 if (pte_val(pte) & _PAGE_SWT)
864 pte_val(pte) |= _HPAGE_TYPE_NONE;
865 pte_val(pte) |= _SEGMENT_ENTRY_INV;
866 }
867 /*
868 * Clear SW pte bits SWT and SWX, there are no SW bits in a segment
869 * table entry.
870 */
871 pte_val(pte) &= ~(_PAGE_SWT | _PAGE_SWX);
872 /*
873 * Also set the change-override bit because we don't need dirty bit
874 * tracking for hugetlbfs pages.
875 */
876 pte_val(pte) |= (_SEGMENT_ENTRY_LARGE | _SEGMENT_ENTRY_CO);
877 return pte;
878}
879#endif
880
Florian Funke15e86b02008-10-10 21:33:26 +0200881/*
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200882 * Get (and clear) the user dirty bit for a pte.
Florian Funke15e86b02008-10-10 21:33:26 +0200883 */
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200884static inline int ptep_test_and_clear_user_dirty(struct mm_struct *mm,
885 pte_t *ptep)
Florian Funke15e86b02008-10-10 21:33:26 +0200886{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200887 pgste_t pgste;
888 int dirty = 0;
Florian Funke15e86b02008-10-10 21:33:26 +0200889
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200890 if (mm_has_pgste(mm)) {
891 pgste = pgste_get_lock(ptep);
892 pgste = pgste_update_all(ptep, pgste);
893 dirty = !!(pgste_val(pgste) & KVM_UC_BIT);
894 pgste_val(pgste) &= ~KVM_UC_BIT;
895 pgste_set_unlock(ptep, pgste);
896 return dirty;
Florian Funke15e86b02008-10-10 21:33:26 +0200897 }
Florian Funke15e86b02008-10-10 21:33:26 +0200898 return dirty;
899}
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200900
901/*
902 * Get (and clear) the user referenced bit for a pte.
903 */
904static inline int ptep_test_and_clear_user_young(struct mm_struct *mm,
905 pte_t *ptep)
906{
907 pgste_t pgste;
908 int young = 0;
909
910 if (mm_has_pgste(mm)) {
911 pgste = pgste_get_lock(ptep);
912 pgste = pgste_update_young(ptep, pgste);
913 young = !!(pgste_val(pgste) & KVM_UR_BIT);
914 pgste_val(pgste) &= ~KVM_UR_BIT;
915 pgste_set_unlock(ptep, pgste);
916 }
917 return young;
918}
Florian Funke15e86b02008-10-10 21:33:26 +0200919
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200920#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
921static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
922 unsigned long addr, pte_t *ptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200924 pgste_t pgste;
925 pte_t pte;
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100926
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200927 if (mm_has_pgste(vma->vm_mm)) {
928 pgste = pgste_get_lock(ptep);
929 pgste = pgste_update_young(ptep, pgste);
930 pte = *ptep;
931 *ptep = pte_mkold(pte);
932 pgste_set_unlock(ptep, pgste);
933 return pte_young(pte);
934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return 0;
936}
937
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200938#define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
939static inline int ptep_clear_flush_young(struct vm_area_struct *vma,
940 unsigned long address, pte_t *ptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100942 /* No need to flush TLB
943 * On s390 reference bits are in storage key and never in TLB
944 * With virtualization we handle the reference bit, without we
945 * we can simply return */
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100946 return ptep_test_and_clear_young(vma, address, ptep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200949static inline void __ptep_ipte(unsigned long address, pte_t *ptep)
950{
951 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200952#ifndef CONFIG_64BIT
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100953 /* pto must point to the start of the segment table */
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200954 pte_t *pto = (pte_t *) (((unsigned long) ptep) & 0x7ffffc00);
955#else
956 /* ipte in zarch mode can do the math */
957 pte_t *pto = ptep;
958#endif
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200959 asm volatile(
960 " ipte %2,%3"
961 : "=m" (*ptep) : "m" (*ptep),
962 "a" (pto), "a" (address));
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200963 }
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200964}
965
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200966/*
967 * This is hard to understand. ptep_get_and_clear and ptep_clear_flush
968 * both clear the TLB for the unmapped pte. The reason is that
969 * ptep_get_and_clear is used in common code (e.g. change_pte_range)
970 * to modify an active pte. The sequence is
971 * 1) ptep_get_and_clear
972 * 2) set_pte_at
973 * 3) flush_tlb_range
974 * On s390 the tlb needs to get flushed with the modification of the pte
975 * if the pte is active. The only way how this can be implemented is to
976 * have ptep_get_and_clear do the tlb flush. In exchange flush_tlb_range
977 * is a nop.
978 */
979#define __HAVE_ARCH_PTEP_GET_AND_CLEAR
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200980static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
981 unsigned long address, pte_t *ptep)
982{
983 pgste_t pgste;
984 pte_t pte;
985
986 mm->context.flush_mm = 1;
987 if (mm_has_pgste(mm))
988 pgste = pgste_get_lock(ptep);
989
990 pte = *ptep;
991 if (!mm_exclusive(mm))
992 __ptep_ipte(address, ptep);
993 pte_val(*ptep) = _PAGE_TYPE_EMPTY;
994
995 if (mm_has_pgste(mm)) {
996 pgste = pgste_update_all(&pte, pgste);
997 pgste_set_unlock(ptep, pgste);
998 }
999 return pte;
1000}
1001
1002#define __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
1003static inline pte_t ptep_modify_prot_start(struct mm_struct *mm,
1004 unsigned long address,
1005 pte_t *ptep)
1006{
1007 pte_t pte;
1008
1009 mm->context.flush_mm = 1;
1010 if (mm_has_pgste(mm))
1011 pgste_get_lock(ptep);
1012
1013 pte = *ptep;
1014 if (!mm_exclusive(mm))
1015 __ptep_ipte(address, ptep);
1016 return pte;
1017}
1018
1019static inline void ptep_modify_prot_commit(struct mm_struct *mm,
1020 unsigned long address,
1021 pte_t *ptep, pte_t pte)
1022{
1023 *ptep = pte;
1024 if (mm_has_pgste(mm))
1025 pgste_set_unlock(ptep, *(pgste_t *)(ptep + PTRS_PER_PTE));
1026}
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001027
1028#define __HAVE_ARCH_PTEP_CLEAR_FLUSH
Martin Schwidefskyf0e47c22007-07-17 04:03:03 -07001029static inline pte_t ptep_clear_flush(struct vm_area_struct *vma,
1030 unsigned long address, pte_t *ptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001032 pgste_t pgste;
1033 pte_t pte;
1034
1035 if (mm_has_pgste(vma->vm_mm))
1036 pgste = pgste_get_lock(ptep);
1037
1038 pte = *ptep;
1039 __ptep_ipte(address, ptep);
1040 pte_val(*ptep) = _PAGE_TYPE_EMPTY;
1041
1042 if (mm_has_pgste(vma->vm_mm)) {
1043 pgste = pgste_update_all(&pte, pgste);
1044 pgste_set_unlock(ptep, pgste);
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return pte;
1047}
1048
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001049/*
1050 * The batched pte unmap code uses ptep_get_and_clear_full to clear the
1051 * ptes. Here an optimization is possible. tlb_gather_mmu flushes all
1052 * tlbs of an mm if it can guarantee that the ptes of the mm_struct
1053 * cannot be accessed while the batched unmap is running. In this case
1054 * full==1 and a simple pte_clear is enough. See tlb.h.
1055 */
1056#define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
1057static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001058 unsigned long address,
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001059 pte_t *ptep, int full)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001061 pgste_t pgste;
1062 pte_t pte;
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001063
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001064 if (mm_has_pgste(mm))
1065 pgste = pgste_get_lock(ptep);
1066
1067 pte = *ptep;
1068 if (!full)
1069 __ptep_ipte(address, ptep);
1070 pte_val(*ptep) = _PAGE_TYPE_EMPTY;
1071
1072 if (mm_has_pgste(mm)) {
1073 pgste = pgste_update_all(&pte, pgste);
1074 pgste_set_unlock(ptep, pgste);
1075 }
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001076 return pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001079#define __HAVE_ARCH_PTEP_SET_WRPROTECT
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001080static inline pte_t ptep_set_wrprotect(struct mm_struct *mm,
1081 unsigned long address, pte_t *ptep)
1082{
1083 pgste_t pgste;
1084 pte_t pte = *ptep;
1085
1086 if (pte_write(pte)) {
1087 mm->context.flush_mm = 1;
1088 if (mm_has_pgste(mm))
1089 pgste = pgste_get_lock(ptep);
1090
1091 if (!mm_exclusive(mm))
1092 __ptep_ipte(address, ptep);
1093 *ptep = pte_wrprotect(pte);
1094
1095 if (mm_has_pgste(mm))
1096 pgste_set_unlock(ptep, pgste);
1097 }
1098 return pte;
1099}
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001100
1101#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001102static inline int ptep_set_access_flags(struct vm_area_struct *vma,
1103 unsigned long address, pte_t *ptep,
1104 pte_t entry, int dirty)
1105{
1106 pgste_t pgste;
1107
1108 if (pte_same(*ptep, entry))
1109 return 0;
1110 if (mm_has_pgste(vma->vm_mm))
1111 pgste = pgste_get_lock(ptep);
1112
1113 __ptep_ipte(address, ptep);
1114 *ptep = entry;
1115
1116 if (mm_has_pgste(vma->vm_mm))
1117 pgste_set_unlock(ptep, pgste);
1118 return 1;
1119}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 * Conversion functions: convert a page and protection to a page entry,
1123 * and a page entry and page directory to the page they refer to.
1124 */
1125static inline pte_t mk_pte_phys(unsigned long physpage, pgprot_t pgprot)
1126{
1127 pte_t __pte;
1128 pte_val(__pte) = physpage + pgprot_val(pgprot);
1129 return __pte;
1130}
1131
Heiko Carstens2dcea572006-09-29 01:58:41 -07001132static inline pte_t mk_pte(struct page *page, pgprot_t pgprot)
1133{
Heiko Carstens0b2b6e1d2006-10-04 20:02:23 +02001134 unsigned long physpage = page_to_phys(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Heiko Carstens2dcea572006-09-29 01:58:41 -07001136 return mk_pte_phys(physpage, pgprot);
1137}
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139#define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001140#define pud_index(address) (((address) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
1141#define pmd_index(address) (((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
1142#define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001144#define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145#define pgd_offset_k(address) pgd_offset(&init_mm, address)
1146
Heiko Carstensf4815ac2012-05-23 16:24:51 +02001147#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001149#define pmd_deref(pmd) (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN)
1150#define pud_deref(pmd) ({ BUG(); 0UL; })
1151#define pgd_deref(pmd) ({ BUG(); 0UL; })
1152
1153#define pud_offset(pgd, address) ((pud_t *) pgd)
1154#define pmd_offset(pud, address) ((pmd_t *) pud + pmd_index(address))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Heiko Carstensf4815ac2012-05-23 16:24:51 +02001156#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001158#define pmd_deref(pmd) (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN)
1159#define pud_deref(pud) (pud_val(pud) & _REGION_ENTRY_ORIGIN)
Martin Schwidefsky5a216a22008-02-09 18:24:36 +01001160#define pgd_deref(pgd) (pgd_val(pgd) & _REGION_ENTRY_ORIGIN)
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001161
Martin Schwidefsky5a216a22008-02-09 18:24:36 +01001162static inline pud_t *pud_offset(pgd_t *pgd, unsigned long address)
1163{
Martin Schwidefsky6252d702008-02-09 18:24:37 +01001164 pud_t *pud = (pud_t *) pgd;
1165 if ((pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
1166 pud = (pud_t *) pgd_deref(*pgd);
Martin Schwidefsky5a216a22008-02-09 18:24:36 +01001167 return pud + pud_index(address);
1168}
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001169
1170static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address)
1171{
Martin Schwidefsky6252d702008-02-09 18:24:37 +01001172 pmd_t *pmd = (pmd_t *) pud;
1173 if ((pud_val(*pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
1174 pmd = (pmd_t *) pud_deref(*pud);
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001175 return pmd + pmd_index(address);
1176}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Heiko Carstensf4815ac2012-05-23 16:24:51 +02001178#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001180#define pfn_pte(pfn,pgprot) mk_pte_phys(__pa((pfn) << PAGE_SHIFT),(pgprot))
1181#define pte_pfn(x) (pte_val(x) >> PAGE_SHIFT)
1182#define pte_page(x) pfn_to_page(pte_pfn(x))
1183
1184#define pmd_page(pmd) pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT)
1185
1186/* Find an entry in the lowest level page table.. */
1187#define pte_offset(pmd, addr) ((pte_t *) pmd_deref(*(pmd)) + pte_index(addr))
1188#define pte_offset_kernel(pmd, address) pte_offset(pmd,address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189#define pte_offset_map(pmd, address) pte_offset_kernel(pmd, address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190#define pte_unmap(pte) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001192static inline void __pmd_idte(unsigned long address, pmd_t *pmdp)
1193{
1194 unsigned long sto = (unsigned long) pmdp -
1195 pmd_index(address) * sizeof(pmd_t);
1196
1197 if (!(pmd_val(*pmdp) & _SEGMENT_ENTRY_INV)) {
1198 asm volatile(
1199 " .insn rrf,0xb98e0000,%2,%3,0,0"
1200 : "=m" (*pmdp)
1201 : "m" (*pmdp), "a" (sto),
1202 "a" ((address & HPAGE_MASK))
1203 : "cc"
1204 );
1205 }
1206}
1207
Gerald Schaefer75077af2012-10-08 16:30:15 -07001208#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gerald Schaefer9501d092012-10-08 16:30:18 -07001209#define __HAVE_ARCH_PGTABLE_DEPOSIT
1210extern void pgtable_trans_huge_deposit(struct mm_struct *mm, pgtable_t pgtable);
1211
1212#define __HAVE_ARCH_PGTABLE_WITHDRAW
1213extern pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm);
1214
Gerald Schaefer75077af2012-10-08 16:30:15 -07001215static inline int pmd_trans_splitting(pmd_t pmd)
1216{
1217 return pmd_val(pmd) & _SEGMENT_ENTRY_SPLIT;
1218}
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001219
1220static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
1221 pmd_t *pmdp, pmd_t entry)
1222{
1223 *pmdp = entry;
1224}
1225
1226static inline unsigned long massage_pgprot_pmd(pgprot_t pgprot)
1227{
1228 unsigned long pgprot_pmd = 0;
1229
1230 if (pgprot_val(pgprot) & _PAGE_INVALID) {
1231 if (pgprot_val(pgprot) & _PAGE_SWT)
1232 pgprot_pmd |= _HPAGE_TYPE_NONE;
1233 pgprot_pmd |= _SEGMENT_ENTRY_INV;
1234 }
1235 if (pgprot_val(pgprot) & _PAGE_RO)
1236 pgprot_pmd |= _SEGMENT_ENTRY_RO;
1237 return pgprot_pmd;
1238}
1239
1240static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
1241{
1242 pmd_val(pmd) &= _SEGMENT_CHG_MASK;
1243 pmd_val(pmd) |= massage_pgprot_pmd(newprot);
1244 return pmd;
1245}
1246
1247static inline pmd_t pmd_mkhuge(pmd_t pmd)
1248{
1249 pmd_val(pmd) |= _SEGMENT_ENTRY_LARGE;
1250 return pmd;
1251}
1252
1253static inline pmd_t pmd_mkwrite(pmd_t pmd)
1254{
1255 pmd_val(pmd) &= ~_SEGMENT_ENTRY_RO;
1256 return pmd;
1257}
1258
1259static inline pmd_t pmd_wrprotect(pmd_t pmd)
1260{
1261 pmd_val(pmd) |= _SEGMENT_ENTRY_RO;
1262 return pmd;
1263}
1264
1265static inline pmd_t pmd_mkdirty(pmd_t pmd)
1266{
1267 /* No dirty bit in the segment table entry. */
1268 return pmd;
1269}
1270
1271static inline pmd_t pmd_mkold(pmd_t pmd)
1272{
1273 /* No referenced bit in the segment table entry. */
1274 return pmd;
1275}
1276
1277static inline pmd_t pmd_mkyoung(pmd_t pmd)
1278{
1279 /* No referenced bit in the segment table entry. */
1280 return pmd;
1281}
1282
1283#define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
1284static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
1285 unsigned long address, pmd_t *pmdp)
1286{
1287 unsigned long pmd_addr = pmd_val(*pmdp) & HPAGE_MASK;
1288 long tmp, rc;
1289 int counter;
1290
1291 rc = 0;
1292 if (MACHINE_HAS_RRBM) {
1293 counter = PTRS_PER_PTE >> 6;
1294 asm volatile(
1295 "0: .insn rre,0xb9ae0000,%0,%3\n" /* rrbm */
1296 " ogr %1,%0\n"
1297 " la %3,0(%4,%3)\n"
1298 " brct %2,0b\n"
1299 : "=&d" (tmp), "+&d" (rc), "+d" (counter),
1300 "+a" (pmd_addr)
1301 : "a" (64 * 4096UL) : "cc");
1302 rc = !!rc;
1303 } else {
1304 counter = PTRS_PER_PTE;
1305 asm volatile(
1306 "0: rrbe 0,%2\n"
1307 " la %2,0(%3,%2)\n"
1308 " brc 12,1f\n"
1309 " lhi %0,1\n"
1310 "1: brct %1,0b\n"
1311 : "+d" (rc), "+d" (counter), "+a" (pmd_addr)
1312 : "a" (4096UL) : "cc");
1313 }
1314 return rc;
1315}
1316
1317#define __HAVE_ARCH_PMDP_GET_AND_CLEAR
1318static inline pmd_t pmdp_get_and_clear(struct mm_struct *mm,
1319 unsigned long address, pmd_t *pmdp)
1320{
1321 pmd_t pmd = *pmdp;
1322
1323 __pmd_idte(address, pmdp);
1324 pmd_clear(pmdp);
1325 return pmd;
1326}
1327
1328#define __HAVE_ARCH_PMDP_CLEAR_FLUSH
1329static inline pmd_t pmdp_clear_flush(struct vm_area_struct *vma,
1330 unsigned long address, pmd_t *pmdp)
1331{
1332 return pmdp_get_and_clear(vma->vm_mm, address, pmdp);
1333}
1334
1335#define __HAVE_ARCH_PMDP_INVALIDATE
1336static inline void pmdp_invalidate(struct vm_area_struct *vma,
1337 unsigned long address, pmd_t *pmdp)
1338{
1339 __pmd_idte(address, pmdp);
1340}
1341
1342static inline pmd_t mk_pmd_phys(unsigned long physpage, pgprot_t pgprot)
1343{
1344 pmd_t __pmd;
1345 pmd_val(__pmd) = physpage + massage_pgprot_pmd(pgprot);
1346 return __pmd;
1347}
1348
1349#define pfn_pmd(pfn, pgprot) mk_pmd_phys(__pa((pfn) << PAGE_SHIFT), (pgprot))
1350#define mk_pmd(page, pgprot) pfn_pmd(page_to_pfn(page), (pgprot))
1351
1352static inline int pmd_trans_huge(pmd_t pmd)
1353{
1354 return pmd_val(pmd) & _SEGMENT_ENTRY_LARGE;
1355}
1356
1357static inline int has_transparent_hugepage(void)
1358{
1359 return MACHINE_HAS_HPAGE ? 1 : 0;
1360}
1361
1362static inline unsigned long pmd_pfn(pmd_t pmd)
1363{
1364 if (pmd_trans_huge(pmd))
1365 return pmd_val(pmd) >> HPAGE_SHIFT;
1366 else
1367 return pmd_val(pmd) >> PAGE_SHIFT;
1368}
Gerald Schaefer75077af2012-10-08 16:30:15 -07001369#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371/*
1372 * 31 bit swap entry format:
1373 * A page-table entry has some bits we have to treat in a special way.
1374 * Bits 0, 20 and bit 23 have to be zero, otherwise an specification
1375 * exception will occur instead of a page translation exception. The
1376 * specifiation exception has the bad habit not to store necessary
1377 * information in the lowcore.
1378 * Bit 21 and bit 22 are the page invalid bit and the page protection
1379 * bit. We set both to indicate a swapped page.
1380 * Bit 30 and 31 are used to distinguish the different page types. For
1381 * a swapped page these bits need to be zero.
1382 * This leaves the bits 1-19 and bits 24-29 to store type and offset.
1383 * We use the 5 bits from 25-29 for the type and the 20 bits from 1-19
1384 * plus 24 for the offset.
1385 * 0| offset |0110|o|type |00|
1386 * 0 0000000001111111111 2222 2 22222 33
1387 * 0 1234567890123456789 0123 4 56789 01
1388 *
1389 * 64 bit swap entry format:
1390 * A page-table entry has some bits we have to treat in a special way.
1391 * Bits 52 and bit 55 have to be zero, otherwise an specification
1392 * exception will occur instead of a page translation exception. The
1393 * specifiation exception has the bad habit not to store necessary
1394 * information in the lowcore.
1395 * Bit 53 and bit 54 are the page invalid bit and the page protection
1396 * bit. We set both to indicate a swapped page.
1397 * Bit 62 and 63 are used to distinguish the different page types. For
1398 * a swapped page these bits need to be zero.
1399 * This leaves the bits 0-51 and bits 56-61 to store type and offset.
1400 * We use the 5 bits from 57-61 for the type and the 53 bits from 0-51
1401 * plus 56 for the offset.
1402 * | offset |0110|o|type |00|
1403 * 0000000000111111111122222222223333333333444444444455 5555 5 55566 66
1404 * 0123456789012345678901234567890123456789012345678901 2345 6 78901 23
1405 */
Heiko Carstensf4815ac2012-05-23 16:24:51 +02001406#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407#define __SWP_OFFSET_MASK (~0UL >> 12)
1408#else
1409#define __SWP_OFFSET_MASK (~0UL >> 11)
1410#endif
Adrian Bunk4448aaf2005-11-08 21:34:42 -08001411static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 pte_t pte;
1414 offset &= __SWP_OFFSET_MASK;
Gerald Schaefer9282ed92006-09-20 15:59:37 +02001415 pte_val(pte) = _PAGE_TYPE_SWAP | ((type & 0x1f) << 2) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 ((offset & 1UL) << 7) | ((offset & ~1UL) << 11);
1417 return pte;
1418}
1419
1420#define __swp_type(entry) (((entry).val >> 2) & 0x1f)
1421#define __swp_offset(entry) (((entry).val >> 11) | (((entry).val >> 7) & 1))
1422#define __swp_entry(type,offset) ((swp_entry_t) { pte_val(mk_swap_pte((type),(offset))) })
1423
1424#define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
1425#define __swp_entry_to_pte(x) ((pte_t) { (x).val })
1426
Heiko Carstensf4815ac2012-05-23 16:24:51 +02001427#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428# define PTE_FILE_MAX_BITS 26
Heiko Carstensf4815ac2012-05-23 16:24:51 +02001429#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430# define PTE_FILE_MAX_BITS 59
Heiko Carstensf4815ac2012-05-23 16:24:51 +02001431#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433#define pte_to_pgoff(__pte) \
1434 ((((__pte).pte >> 12) << 7) + (((__pte).pte >> 1) & 0x7f))
1435
1436#define pgoff_to_pte(__off) \
1437 ((pte_t) { ((((__off) & 0x7f) << 1) + (((__off) >> 7) << 12)) \
Gerald Schaefer9282ed92006-09-20 15:59:37 +02001438 | _PAGE_TYPE_FILE })
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440#endif /* !__ASSEMBLY__ */
1441
1442#define kern_addr_valid(addr) (1)
1443
Heiko Carstens17f34582008-04-30 13:38:47 +02001444extern int vmem_add_mapping(unsigned long start, unsigned long size);
1445extern int vmem_remove_mapping(unsigned long start, unsigned long size);
Carsten Otte402b0862008-03-25 18:47:10 +01001446extern int s390_enable_sie(void);
Heiko Carstensf4eb07c2006-12-08 15:56:07 +01001447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448/*
1449 * No page table caches to initialise
1450 */
1451#define pgtable_cache_init() do { } while (0)
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453#include <asm-generic/pgtable.h>
1454
1455#endif /* _S390_PAGE_H */