blob: 1fba63997d5092a6e84dcb417d145543b7114fd7 [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>
Martin Schwidefskyabf09be2012-11-07 13:17:37 +010032#include <linux/page-flags.h>
Martin Schwidefsky527e30b2014-04-30 16:04:25 +020033#include <linux/radix-tree.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/bug.h>
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +020035#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037extern pgd_t swapper_pg_dir[] __attribute__ ((aligned (4096)));
38extern void paging_init(void);
Heiko Carstens2b67fc42007-02-05 21:16:47 +010039extern void vmem_map_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/*
42 * The S390 doesn't have any external MMU info: the kernel page
43 * tables contain all the necessary information.
44 */
Russell King4b3073e2009-12-18 16:40:18 +000045#define update_mmu_cache(vma, address, ptep) do { } while (0)
David Millerb113da62012-10-08 16:34:25 -070046#define update_mmu_cache_pmd(vma, address, ptep) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/*
Martin Schwidefsky238ec4e2010-10-25 16:10:07 +020049 * ZERO_PAGE is a global shared page that is always zero; used
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * for zero-mapped memory areas etc..
51 */
Martin Schwidefsky238ec4e2010-10-25 16:10:07 +020052
53extern unsigned long empty_zero_page;
54extern unsigned long zero_page_mask;
55
56#define ZERO_PAGE(vaddr) \
57 (virt_to_page((void *)(empty_zero_page + \
58 (((unsigned long)(vaddr)) &zero_page_mask))))
Kirill A. Shutemov816422a2012-12-12 13:52:36 -080059#define __HAVE_COLOR_ZERO_PAGE
Martin Schwidefsky238ec4e2010-10-25 16:10:07 +020060
Linus Torvalds4f2e2902013-04-17 08:46:19 -070061/* TODO: s390 cannot support io_remap_pfn_range... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#endif /* !__ASSEMBLY__ */
63
64/*
65 * PMD_SHIFT determines the size of the area a second-level page
66 * table can map
67 * PGDIR_SHIFT determines what a third-level page table entry can map
68 */
Heiko Carstens5a79859a2015-02-12 13:08:27 +010069#define PMD_SHIFT 20
70#define PUD_SHIFT 31
71#define PGDIR_SHIFT 42
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73#define PMD_SIZE (1UL << PMD_SHIFT)
74#define PMD_MASK (~(PMD_SIZE-1))
Martin Schwidefsky190a1d72007-10-22 12:52:48 +020075#define PUD_SIZE (1UL << PUD_SHIFT)
76#define PUD_MASK (~(PUD_SIZE-1))
Martin Schwidefsky5a216a22008-02-09 18:24:36 +010077#define PGDIR_SIZE (1UL << PGDIR_SHIFT)
78#define PGDIR_MASK (~(PGDIR_SIZE-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/*
81 * entries per page directory level: the S390 is two-level, so
82 * we don't really have any PMD directory physically.
83 * for S390 segment-table entries are combined to one PGD
84 * that leads to 1024 pte per pgd
85 */
Martin Schwidefsky146e4b32008-02-09 18:24:35 +010086#define PTRS_PER_PTE 256
Martin Schwidefsky146e4b32008-02-09 18:24:35 +010087#define PTRS_PER_PMD 2048
Martin Schwidefsky5a216a22008-02-09 18:24:36 +010088#define PTRS_PER_PUD 2048
Martin Schwidefsky146e4b32008-02-09 18:24:35 +010089#define PTRS_PER_PGD 2048
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Kirill A. Shutemovd016bf72015-02-11 15:26:41 -080091#define FIRST_USER_ADDRESS 0UL
Hugh Dickinsd455a362005-04-19 13:29:23 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#define pte_ERROR(e) \
94 printk("%s:%d: bad pte %p.\n", __FILE__, __LINE__, (void *) pte_val(e))
95#define pmd_ERROR(e) \
96 printk("%s:%d: bad pmd %p.\n", __FILE__, __LINE__, (void *) pmd_val(e))
Martin Schwidefsky190a1d72007-10-22 12:52:48 +020097#define pud_ERROR(e) \
98 printk("%s:%d: bad pud %p.\n", __FILE__, __LINE__, (void *) pud_val(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#define pgd_ERROR(e) \
100 printk("%s:%d: bad pgd %p.\n", __FILE__, __LINE__, (void *) pgd_val(e))
101
102#ifndef __ASSEMBLY__
103/*
Heiko Carstensc972cc62012-10-05 16:52:18 +0200104 * The vmalloc and module area will always be on the topmost area of the kernel
105 * mapping. We reserve 96MB (31bit) / 128GB (64bit) for vmalloc and modules.
106 * On 64 bit kernels we have a 2GB area at the top of the vmalloc area where
107 * modules will reside. That makes sure that inter module branches always
108 * happen without trampolines and in addition the placement within a 2GB frame
109 * is branch prediction unit friendly.
Heiko Carstens8b62bc92006-12-04 15:40:56 +0100110 */
Heiko Carstens239a64252009-06-12 10:26:33 +0200111extern unsigned long VMALLOC_START;
Martin Schwidefsky14045eb2011-12-27 11:27:07 +0100112extern unsigned long VMALLOC_END;
113extern struct page *vmemmap;
Heiko Carstens239a64252009-06-12 10:26:33 +0200114
Martin Schwidefsky14045eb2011-12-27 11:27:07 +0100115#define VMEM_MAX_PHYS ((unsigned long) vmemmap)
Christian Borntraeger5fd9c6e2008-01-26 14:11:00 +0100116
Heiko Carstensc972cc62012-10-05 16:52:18 +0200117extern unsigned long MODULES_VADDR;
118extern unsigned long MODULES_END;
119#define MODULES_VADDR MODULES_VADDR
120#define MODULES_END MODULES_END
121#define MODULES_LEN (1UL << 31)
Heiko Carstensc972cc62012-10-05 16:52:18 +0200122
Heiko Carstensc9331462014-10-15 12:17:38 +0200123static inline int is_module_addr(void *addr)
124{
Heiko Carstensc9331462014-10-15 12:17:38 +0200125 BUILD_BUG_ON(MODULES_LEN > (1UL << 31));
126 if (addr < (void *)MODULES_VADDR)
127 return 0;
128 if (addr > (void *)MODULES_END)
129 return 0;
Heiko Carstensc9331462014-10-15 12:17:38 +0200130 return 1;
131}
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/*
134 * A 31 bit pagetable entry of S390 has following format:
135 * | PFRA | | OS |
136 * 0 0IP0
137 * 00000000001111111111222222222233
138 * 01234567890123456789012345678901
139 *
140 * I Page-Invalid Bit: Page is not available for address-translation
141 * P Page-Protection Bit: Store access not possible for page
142 *
143 * A 31 bit segmenttable entry of S390 has following format:
144 * | P-table origin | |PTL
145 * 0 IC
146 * 00000000001111111111222222222233
147 * 01234567890123456789012345678901
148 *
149 * I Segment-Invalid Bit: Segment is not available for address-translation
150 * C Common-Segment Bit: Segment is not private (PoP 3-30)
151 * PTL Page-Table-Length: Page-table length (PTL+1*16 entries -> up to 256)
152 *
153 * The 31 bit segmenttable origin of S390 has following format:
154 *
155 * |S-table origin | | STL |
156 * X **GPS
157 * 00000000001111111111222222222233
158 * 01234567890123456789012345678901
159 *
160 * X Space-Switch event:
161 * G Segment-Invalid Bit: *
162 * P Private-Space Bit: Segment is not private (PoP 3-30)
163 * S Storage-Alteration:
164 * STL Segment-Table-Length: Segment-table length (STL+1*16 entries -> up to 2048)
165 *
166 * A 64 bit pagetable entry of S390 has following format:
Christian Borntraeger6a985c62009-12-07 12:52:11 +0100167 * | PFRA |0IPC| OS |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * 0000000000111111111122222222223333333333444444444455555555556666
169 * 0123456789012345678901234567890123456789012345678901234567890123
170 *
171 * I Page-Invalid Bit: Page is not available for address-translation
172 * P Page-Protection Bit: Store access not possible for page
Christian Borntraeger6a985c62009-12-07 12:52:11 +0100173 * C Change-bit override: HW is not required to set change bit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *
175 * A 64 bit segmenttable entry of S390 has following format:
176 * | P-table origin | TT
177 * 0000000000111111111122222222223333333333444444444455555555556666
178 * 0123456789012345678901234567890123456789012345678901234567890123
179 *
180 * I Segment-Invalid Bit: Segment is not available for address-translation
181 * C Common-Segment Bit: Segment is not private (PoP 3-30)
182 * P Page-Protection Bit: Store access not possible for page
183 * TT Type 00
184 *
185 * A 64 bit region table entry of S390 has following format:
186 * | S-table origin | TF TTTL
187 * 0000000000111111111122222222223333333333444444444455555555556666
188 * 0123456789012345678901234567890123456789012345678901234567890123
189 *
190 * I Segment-Invalid Bit: Segment is not available for address-translation
191 * TT Type 01
192 * TF
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200193 * TL Table length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
195 * The 64 bit regiontable origin of S390 has following format:
196 * | region table origon | DTTL
197 * 0000000000111111111122222222223333333333444444444455555555556666
198 * 0123456789012345678901234567890123456789012345678901234567890123
199 *
200 * X Space-Switch event:
201 * G Segment-Invalid Bit:
202 * P Private-Space Bit:
203 * S Storage-Alteration:
204 * R Real space
205 * TL Table-Length:
206 *
207 * A storage key has the following format:
208 * | ACC |F|R|C|0|
209 * 0 3 4 5 6 7
210 * ACC: access key
211 * F : fetch protection bit
212 * R : referenced bit
213 * C : changed bit
214 */
215
216/* Hardware bits in the page table entry */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200217#define _PAGE_PROTECT 0x200 /* HW read-only bit */
Martin Schwidefsky83377482006-10-18 18:30:51 +0200218#define _PAGE_INVALID 0x400 /* HW invalid bit */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200219#define _PAGE_LARGE 0x800 /* Bit to mark a large pte */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200220
221/* Software bits in the page table entry */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200222#define _PAGE_PRESENT 0x001 /* SW pte present bit */
223#define _PAGE_TYPE 0x002 /* SW pte type bit */
224#define _PAGE_YOUNG 0x004 /* SW pte young bit */
225#define _PAGE_DIRTY 0x008 /* SW pte dirty bit */
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200226#define _PAGE_READ 0x010 /* SW pte read bit */
227#define _PAGE_WRITE 0x020 /* SW pte write bit */
228#define _PAGE_SPECIAL 0x040 /* SW associated with special page */
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200229#define _PAGE_UNUSED 0x080 /* SW bit for pgste usage state */
Nick Piggina08cb622008-04-28 02:13:03 -0700230#define __HAVE_ARCH_PTE_SPECIAL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Nick Piggin138c9022008-07-08 11:31:06 +0200232/* Set of bits not changed in pte_modify */
Heiko Carstens6a5c1482014-09-22 08:50:51 +0200233#define _PAGE_CHG_MASK (PAGE_MASK | _PAGE_SPECIAL | _PAGE_DIRTY | \
234 _PAGE_YOUNG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Martin Schwidefsky83377482006-10-18 18:30:51 +0200236/*
Kirill A. Shutemov6e76d4b2015-02-10 14:11:04 -0800237 * handle_pte_fault uses pte_present and pte_none to find out the pte type
238 * WITHOUT holding the page table lock. The _PAGE_PRESENT bit is used to
239 * distinguish present from not-present ptes. It is changed only with the page
240 * table lock held.
Martin Schwidefsky83377482006-10-18 18:30:51 +0200241 *
Martin Schwidefskye5098612013-07-23 20:57:57 +0200242 * The following table gives the different possible bit combinations for
243 * the pte hardware and software bits in the last 12 bits of a pte:
Martin Schwidefsky83377482006-10-18 18:30:51 +0200244 *
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200245 * 842100000000
246 * 000084210000
247 * 000000008421
248 * .IR...wrdytp
249 * empty .10...000000
250 * swap .10...xxxx10
251 * file .11...xxxxx0
252 * prot-none, clean, old .11...000001
253 * prot-none, clean, young .11...000101
254 * prot-none, dirty, old .10...001001
255 * prot-none, dirty, young .10...001101
256 * read-only, clean, old .11...010001
257 * read-only, clean, young .01...010101
258 * read-only, dirty, old .11...011001
259 * read-only, dirty, young .01...011101
260 * read-write, clean, old .11...110001
261 * read-write, clean, young .01...110101
262 * read-write, dirty, old .10...111001
263 * read-write, dirty, young .00...111101
Martin Schwidefskye5098612013-07-23 20:57:57 +0200264 *
265 * pte_present is true for the bit pattern .xx...xxxxx1, (pte & 0x001) == 0x001
266 * pte_none is true for the bit pattern .10...xxxx00, (pte & 0x603) == 0x400
Martin Schwidefskye5098612013-07-23 20:57:57 +0200267 * pte_swap is true for the bit pattern .10...xxxx10, (pte & 0x603) == 0x402
Martin Schwidefsky83377482006-10-18 18:30:51 +0200268 */
269
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200270/* Bits in the segment/region table address-space-control-element */
271#define _ASCE_ORIGIN ~0xfffUL/* segment table origin */
272#define _ASCE_PRIVATE_SPACE 0x100 /* private space control */
273#define _ASCE_ALT_EVENT 0x80 /* storage alteration event control */
274#define _ASCE_SPACE_SWITCH 0x40 /* space switch event */
275#define _ASCE_REAL_SPACE 0x20 /* real space control */
276#define _ASCE_TYPE_MASK 0x0c /* asce table type mask */
277#define _ASCE_TYPE_REGION1 0x0c /* region first table type */
278#define _ASCE_TYPE_REGION2 0x08 /* region second table type */
279#define _ASCE_TYPE_REGION3 0x04 /* region third table type */
280#define _ASCE_TYPE_SEGMENT 0x00 /* segment table type */
281#define _ASCE_TABLE_LENGTH 0x03 /* region table length */
282
283/* Bits in the region table entry */
284#define _REGION_ENTRY_ORIGIN ~0xfffUL/* region/segment table origin */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200285#define _REGION_ENTRY_PROTECT 0x200 /* region protection bit */
286#define _REGION_ENTRY_INVALID 0x20 /* invalid region table entry */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200287#define _REGION_ENTRY_TYPE_MASK 0x0c /* region/segment table type mask */
288#define _REGION_ENTRY_TYPE_R1 0x0c /* region first table type */
289#define _REGION_ENTRY_TYPE_R2 0x08 /* region second table type */
290#define _REGION_ENTRY_TYPE_R3 0x04 /* region third table type */
291#define _REGION_ENTRY_LENGTH 0x03 /* region third length */
292
293#define _REGION1_ENTRY (_REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_LENGTH)
Martin Schwidefskye5098612013-07-23 20:57:57 +0200294#define _REGION1_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INVALID)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200295#define _REGION2_ENTRY (_REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_LENGTH)
Martin Schwidefskye5098612013-07-23 20:57:57 +0200296#define _REGION2_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INVALID)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200297#define _REGION3_ENTRY (_REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_LENGTH)
Martin Schwidefskye5098612013-07-23 20:57:57 +0200298#define _REGION3_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INVALID)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200299
Heiko Carstens18da2362012-10-08 09:18:26 +0200300#define _REGION3_ENTRY_LARGE 0x400 /* RTTE-format control, large page */
Heiko Carstens1819ed12013-02-16 11:47:27 +0100301#define _REGION3_ENTRY_RO 0x200 /* page protection bit */
Heiko Carstens18da2362012-10-08 09:18:26 +0200302
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200303/* Bits in the segment table entry */
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200304#define _SEGMENT_ENTRY_BITS 0xfffffffffffffe33UL
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200305#define _SEGMENT_ENTRY_BITS_LARGE 0xfffffffffff0ff33UL
Heiko Carstensea815312013-03-21 12:50:39 +0100306#define _SEGMENT_ENTRY_ORIGIN_LARGE ~0xfffffUL /* large page address */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200307#define _SEGMENT_ENTRY_ORIGIN ~0x7ffUL/* segment table origin */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200308#define _SEGMENT_ENTRY_PROTECT 0x200 /* page protection bit */
309#define _SEGMENT_ENTRY_INVALID 0x20 /* invalid segment table entry */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200310
311#define _SEGMENT_ENTRY (0)
Martin Schwidefskye5098612013-07-23 20:57:57 +0200312#define _SEGMENT_ENTRY_EMPTY (_SEGMENT_ENTRY_INVALID)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200313
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200314#define _SEGMENT_ENTRY_DIRTY 0x2000 /* SW segment dirty bit */
315#define _SEGMENT_ENTRY_YOUNG 0x1000 /* SW segment young bit */
316#define _SEGMENT_ENTRY_SPLIT 0x0800 /* THP splitting bit */
317#define _SEGMENT_ENTRY_LARGE 0x0400 /* STE-format control, large page */
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200318#define _SEGMENT_ENTRY_READ 0x0002 /* SW segment read bit */
319#define _SEGMENT_ENTRY_WRITE 0x0001 /* SW segment write bit */
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200320
321/*
322 * Segment table entry encoding (R = read-only, I = invalid, y = young bit):
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200323 * dy..R...I...wr
324 * prot-none, clean, old 00..1...1...00
325 * prot-none, clean, young 01..1...1...00
326 * prot-none, dirty, old 10..1...1...00
327 * prot-none, dirty, young 11..1...1...00
328 * read-only, clean, old 00..1...1...01
329 * read-only, clean, young 01..1...0...01
330 * read-only, dirty, old 10..1...1...01
331 * read-only, dirty, young 11..1...0...01
332 * read-write, clean, old 00..1...1...11
333 * read-write, clean, young 01..1...0...11
334 * read-write, dirty, old 10..0...1...11
335 * read-write, dirty, young 11..0...0...11
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200336 * The segment table origin is used to distinguish empty (origin==0) from
337 * read-write, old segment table entries (origin!=0)
338 */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200339
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200340#define _SEGMENT_ENTRY_SPLIT_BIT 11 /* THP splitting bit number */
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -0700341
Martin Schwidefsky6c61cfe2011-06-06 14:14:42 +0200342/* Page status table bits for virtualization */
Martin Schwidefsky0d0dafc2013-05-17 14:41:33 +0200343#define PGSTE_ACC_BITS 0xf000000000000000UL
344#define PGSTE_FP_BIT 0x0800000000000000UL
345#define PGSTE_PCL_BIT 0x0080000000000000UL
346#define PGSTE_HR_BIT 0x0040000000000000UL
347#define PGSTE_HC_BIT 0x0020000000000000UL
348#define PGSTE_GR_BIT 0x0004000000000000UL
349#define PGSTE_GC_BIT 0x0002000000000000UL
Martin Schwidefsky0a61b222013-10-18 12:03:41 +0200350#define PGSTE_UC_BIT 0x0000800000000000UL /* user dirty (migration) */
351#define PGSTE_IN_BIT 0x0000400000000000UL /* IPTE notify bit */
Martin Schwidefsky6c61cfe2011-06-06 14:14:42 +0200352
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200353/* Guest Page State used for virtualization */
354#define _PGSTE_GPS_ZERO 0x0000000080000000UL
355#define _PGSTE_GPS_USAGE_MASK 0x0000000003000000UL
356#define _PGSTE_GPS_USAGE_STABLE 0x0000000000000000UL
357#define _PGSTE_GPS_USAGE_UNUSED 0x0000000001000000UL
358
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200359/*
360 * A user page table pointer has the space-switch-event bit, the
361 * private-space-control bit and the storage-alteration-event-control
362 * bit set. A kernel page table pointer doesn't need them.
363 */
364#define _ASCE_USER_BITS (_ASCE_SPACE_SWITCH | _ASCE_PRIVATE_SPACE | \
365 _ASCE_ALT_EVENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/*
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200368 * Page protection definitions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200370#define PAGE_NONE __pgprot(_PAGE_PRESENT | _PAGE_INVALID)
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200371#define PAGE_READ __pgprot(_PAGE_PRESENT | _PAGE_READ | \
372 _PAGE_INVALID | _PAGE_PROTECT)
373#define PAGE_WRITE __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE | \
374 _PAGE_INVALID | _PAGE_PROTECT)
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200375
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200376#define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE | \
377 _PAGE_YOUNG | _PAGE_DIRTY)
378#define PAGE_KERNEL __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE | \
379 _PAGE_YOUNG | _PAGE_DIRTY)
380#define PAGE_KERNEL_RO __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_YOUNG | \
381 _PAGE_PROTECT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383/*
Martin Schwidefsky043d0702011-05-23 10:24:23 +0200384 * On s390 the page table entry has an invalid bit and a read-only bit.
385 * Read permission implies execute permission and write permission
386 * implies read permission.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 */
388 /*xwr*/
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200389#define __P000 PAGE_NONE
Martin Schwidefskye5098612013-07-23 20:57:57 +0200390#define __P001 PAGE_READ
391#define __P010 PAGE_READ
392#define __P011 PAGE_READ
393#define __P100 PAGE_READ
394#define __P101 PAGE_READ
395#define __P110 PAGE_READ
396#define __P111 PAGE_READ
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200398#define __S000 PAGE_NONE
Martin Schwidefskye5098612013-07-23 20:57:57 +0200399#define __S001 PAGE_READ
400#define __S010 PAGE_WRITE
401#define __S011 PAGE_WRITE
402#define __S100 PAGE_READ
403#define __S101 PAGE_READ
404#define __S110 PAGE_WRITE
405#define __S111 PAGE_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Gerald Schaefer106c9922013-04-29 15:07:23 -0700407/*
408 * Segment entry (large page) protection definitions.
409 */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200410#define SEGMENT_NONE __pgprot(_SEGMENT_ENTRY_INVALID | \
411 _SEGMENT_ENTRY_PROTECT)
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200412#define SEGMENT_READ __pgprot(_SEGMENT_ENTRY_PROTECT | \
413 _SEGMENT_ENTRY_READ)
414#define SEGMENT_WRITE __pgprot(_SEGMENT_ENTRY_READ | \
415 _SEGMENT_ENTRY_WRITE)
Gerald Schaefer106c9922013-04-29 15:07:23 -0700416
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200417static inline int mm_has_pgste(struct mm_struct *mm)
418{
419#ifdef CONFIG_PGSTE
420 if (unlikely(mm->context.has_pgste))
421 return 1;
422#endif
423 return 0;
424}
Dominik Dingel65eef3352014-01-14 15:02:11 +0100425
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +0200426static inline int mm_alloc_pgste(struct mm_struct *mm)
427{
428#ifdef CONFIG_PGSTE
429 if (unlikely(mm->context.alloc_pgste))
430 return 1;
431#endif
432 return 0;
433}
434
Dominik Dingel2faee8f2014-10-23 12:08:38 +0200435/*
436 * In the case that a guest uses storage keys
437 * faults should no longer be backed by zero pages
438 */
439#define mm_forbids_zeropage mm_use_skey
Dominik Dingel65eef3352014-01-14 15:02:11 +0100440static inline int mm_use_skey(struct mm_struct *mm)
441{
442#ifdef CONFIG_PGSTE
443 if (mm->context.use_skey)
444 return 1;
445#endif
446 return 0;
447}
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/*
450 * pgd/pmd/pte query functions
451 */
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100452static inline int pgd_present(pgd_t pgd)
453{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100454 if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R2)
455 return 1;
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100456 return (pgd_val(pgd) & _REGION_ENTRY_ORIGIN) != 0UL;
457}
458
459static inline int pgd_none(pgd_t pgd)
460{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100461 if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R2)
462 return 0;
Martin Schwidefskye5098612013-07-23 20:57:57 +0200463 return (pgd_val(pgd) & _REGION_ENTRY_INVALID) != 0UL;
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100464}
465
466static inline int pgd_bad(pgd_t pgd)
467{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100468 /*
469 * With dynamic page table levels the pgd can be a region table
470 * entry or a segment table entry. Check for the bit that are
471 * invalid for either table entry.
472 */
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100473 unsigned long mask =
Martin Schwidefskye5098612013-07-23 20:57:57 +0200474 ~_SEGMENT_ENTRY_ORIGIN & ~_REGION_ENTRY_INVALID &
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100475 ~_REGION_ENTRY_TYPE_MASK & ~_REGION_ENTRY_LENGTH;
476 return (pgd_val(pgd) & mask) != 0;
477}
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200478
479static inline int pud_present(pud_t pud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100481 if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R3)
482 return 1;
Martin Schwidefsky0d017922007-12-17 16:25:48 +0100483 return (pud_val(pud) & _REGION_ENTRY_ORIGIN) != 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200486static inline int pud_none(pud_t pud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100488 if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R3)
489 return 0;
Martin Schwidefskye5098612013-07-23 20:57:57 +0200490 return (pud_val(pud) & _REGION_ENTRY_INVALID) != 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Heiko Carstens18da2362012-10-08 09:18:26 +0200493static inline int pud_large(pud_t pud)
494{
495 if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) != _REGION_ENTRY_TYPE_R3)
496 return 0;
497 return !!(pud_val(pud) & _REGION3_ENTRY_LARGE);
498}
499
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200500static inline int pud_bad(pud_t pud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100502 /*
503 * With dynamic page table levels the pud can be a region table
504 * entry or a segment table entry. Check for the bit that are
505 * invalid for either table entry.
506 */
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100507 unsigned long mask =
Martin Schwidefskye5098612013-07-23 20:57:57 +0200508 ~_SEGMENT_ENTRY_ORIGIN & ~_REGION_ENTRY_INVALID &
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100509 ~_REGION_ENTRY_TYPE_MASK & ~_REGION_ENTRY_LENGTH;
510 return (pud_val(pud) & mask) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800513static inline int pmd_present(pmd_t pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200515 return pmd_val(pmd) != _SEGMENT_ENTRY_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800518static inline int pmd_none(pmd_t pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200520 return pmd_val(pmd) == _SEGMENT_ENTRY_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Heiko Carstens378b1e72012-10-01 12:58:34 +0200523static inline int pmd_large(pmd_t pmd)
524{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200525 return (pmd_val(pmd) & _SEGMENT_ENTRY_LARGE) != 0;
Heiko Carstens378b1e72012-10-01 12:58:34 +0200526}
527
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200528static inline int pmd_pfn(pmd_t pmd)
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200529{
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200530 unsigned long origin_mask;
531
532 origin_mask = _SEGMENT_ENTRY_ORIGIN;
533 if (pmd_large(pmd))
534 origin_mask = _SEGMENT_ENTRY_ORIGIN_LARGE;
535 return (pmd_val(pmd) & origin_mask) >> PAGE_SHIFT;
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200536}
537
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800538static inline int pmd_bad(pmd_t pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200540 if (pmd_large(pmd))
541 return (pmd_val(pmd) & ~_SEGMENT_ENTRY_BITS_LARGE) != 0;
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200542 return (pmd_val(pmd) & ~_SEGMENT_ENTRY_BITS) != 0;
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{
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200561 return (pmd_val(pmd) & _SEGMENT_ENTRY_WRITE) != 0;
562}
563
564static inline int pmd_dirty(pmd_t pmd)
565{
566 int dirty = 1;
567 if (pmd_large(pmd))
568 dirty = (pmd_val(pmd) & _SEGMENT_ENTRY_DIRTY) != 0;
569 return dirty;
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -0700570}
571
572static inline int pmd_young(pmd_t pmd)
573{
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200574 int young = 1;
575 if (pmd_large(pmd))
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200576 young = (pmd_val(pmd) & _SEGMENT_ENTRY_YOUNG) != 0;
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200577 return young;
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -0700578}
579
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800580static inline int pte_present(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200582 /* Bit pattern: (pte & 0x001) == 0x001 */
583 return (pte_val(pte) & _PAGE_PRESENT) != 0;
584}
585
586static inline int pte_none(pte_t pte)
587{
588 /* Bit pattern: pte == 0x400 */
589 return pte_val(pte) == _PAGE_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200592static inline int pte_swap(pte_t pte)
593{
594 /* Bit pattern: (pte & 0x603) == 0x402 */
595 return (pte_val(pte) & (_PAGE_INVALID | _PAGE_PROTECT |
596 _PAGE_TYPE | _PAGE_PRESENT))
597 == (_PAGE_INVALID | _PAGE_TYPE);
598}
599
Nick Piggin7e675132008-04-28 02:13:00 -0700600static inline int pte_special(pte_t pte)
601{
Nick Piggina08cb622008-04-28 02:13:03 -0700602 return (pte_val(pte) & _PAGE_SPECIAL);
Nick Piggin7e675132008-04-28 02:13:00 -0700603}
604
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200605#define __HAVE_ARCH_PTE_SAME
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200606static inline int pte_same(pte_t a, pte_t b)
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100607{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200608 return pte_val(a) == pte_val(b);
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100609}
610
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200611static inline pgste_t pgste_get_lock(pte_t *ptep)
612{
613 unsigned long new = 0;
614#ifdef CONFIG_PGSTE
615 unsigned long old;
616
617 preempt_disable();
618 asm(
619 " lg %0,%2\n"
620 "0: lgr %1,%0\n"
Martin Schwidefsky0d0dafc2013-05-17 14:41:33 +0200621 " nihh %0,0xff7f\n" /* clear PCL bit in old */
622 " oihh %1,0x0080\n" /* set PCL bit in new */
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200623 " csg %0,%1,%2\n"
624 " jl 0b\n"
625 : "=&d" (old), "=&d" (new), "=Q" (ptep[PTRS_PER_PTE])
Christian Borntraegera8f6e7f2013-06-05 09:25:34 +0200626 : "Q" (ptep[PTRS_PER_PTE]) : "cc", "memory");
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200627#endif
628 return __pgste(new);
629}
630
631static inline void pgste_set_unlock(pte_t *ptep, pgste_t pgste)
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100632{
633#ifdef CONFIG_PGSTE
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200634 asm(
Martin Schwidefsky0d0dafc2013-05-17 14:41:33 +0200635 " nihh %1,0xff7f\n" /* clear PCL bit */
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200636 " stg %1,%0\n"
637 : "=Q" (ptep[PTRS_PER_PTE])
Christian Borntraegera8f6e7f2013-06-05 09:25:34 +0200638 : "d" (pgste_val(pgste)), "Q" (ptep[PTRS_PER_PTE])
639 : "cc", "memory");
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100640 preempt_enable();
641#endif
642}
643
Martin Schwidefskyd56c8932013-07-19 11:15:54 +0200644static inline pgste_t pgste_get(pte_t *ptep)
645{
646 unsigned long pgste = 0;
647#ifdef CONFIG_PGSTE
648 pgste = *(unsigned long *)(ptep + PTRS_PER_PTE);
649#endif
650 return __pgste(pgste);
651}
652
Christian Borntraeger3a826032013-06-05 09:22:33 +0200653static inline void pgste_set(pte_t *ptep, pgste_t pgste)
654{
655#ifdef CONFIG_PGSTE
656 *(pgste_t *)(ptep + PTRS_PER_PTE) = pgste;
657#endif
658}
659
Dominik Dingel65eef3352014-01-14 15:02:11 +0100660static inline pgste_t pgste_update_all(pte_t *ptep, pgste_t pgste,
661 struct mm_struct *mm)
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100662{
663#ifdef CONFIG_PGSTE
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200664 unsigned long address, bits, skey;
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100665
Dominik Dingel65eef3352014-01-14 15:02:11 +0100666 if (!mm_use_skey(mm) || pte_val(*ptep) & _PAGE_INVALID)
Martin Schwidefsky09b53882011-11-14 11:19:00 +0100667 return pgste;
Heiko Carstensa43a9d92011-05-29 12:40:50 +0200668 address = pte_val(*ptep) & PAGE_MASK;
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200669 skey = (unsigned long) page_get_storage_key(address);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200670 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200671 /* Transfer page changed & referenced bit to guest bits in pgste */
Martin Schwidefsky0d0dafc2013-05-17 14:41:33 +0200672 pgste_val(pgste) |= bits << 48; /* GR bit & GC bit */
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200673 /* Copy page access key and fetch protection bit to pgste */
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200674 pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT);
675 pgste_val(pgste) |= (skey & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100676#endif
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200677 return pgste;
678
679}
680
Dominik Dingel65eef3352014-01-14 15:02:11 +0100681static inline void pgste_set_key(pte_t *ptep, pgste_t pgste, pte_t entry,
682 struct mm_struct *mm)
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200683{
684#ifdef CONFIG_PGSTE
Heiko Carstensa43a9d92011-05-29 12:40:50 +0200685 unsigned long address;
Christian Borntraeger338679f2013-06-04 09:53:59 +0200686 unsigned long nkey;
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200687
Dominik Dingel65eef3352014-01-14 15:02:11 +0100688 if (!mm_use_skey(mm) || pte_val(entry) & _PAGE_INVALID)
Martin Schwidefsky09b53882011-11-14 11:19:00 +0100689 return;
Christian Borntraeger338679f2013-06-04 09:53:59 +0200690 VM_BUG_ON(!(pte_val(*ptep) & _PAGE_INVALID));
Martin Schwidefsky09b53882011-11-14 11:19:00 +0100691 address = pte_val(entry) & PAGE_MASK;
Christian Borntraeger338679f2013-06-04 09:53:59 +0200692 /*
693 * Set page access key and fetch protection bit from pgste.
694 * The guest C/R information is still in the PGSTE, set real
695 * key C/R to 0.
696 */
Linus Torvaldsfe489bf2013-07-03 13:21:40 -0700697 nkey = (pgste_val(pgste) & (PGSTE_ACC_BITS | PGSTE_FP_BIT)) >> 56;
Martin Schwidefsky0a61b222013-10-18 12:03:41 +0200698 nkey |= (pgste_val(pgste) & (PGSTE_GR_BIT | PGSTE_GC_BIT)) >> 48;
Christian Borntraeger338679f2013-06-04 09:53:59 +0200699 page_set_storage_key(address, nkey, 0);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200700#endif
701}
702
Martin Schwidefsky0a61b222013-10-18 12:03:41 +0200703static inline pgste_t pgste_set_pte(pte_t *ptep, pgste_t pgste, pte_t entry)
Martin Schwidefskyabf09be2012-11-07 13:17:37 +0100704{
Martin Schwidefsky0a61b222013-10-18 12:03:41 +0200705 if ((pte_val(entry) & _PAGE_PRESENT) &&
706 (pte_val(entry) & _PAGE_WRITE) &&
707 !(pte_val(entry) & _PAGE_INVALID)) {
708 if (!MACHINE_HAS_ESOP) {
709 /*
710 * Without enhanced suppression-on-protection force
711 * the dirty bit on for all writable ptes.
712 */
713 pte_val(entry) |= _PAGE_DIRTY;
714 pte_val(entry) &= ~_PAGE_PROTECT;
715 }
716 if (!(pte_val(entry) & _PAGE_PROTECT))
717 /* This pte allows write access, set user-dirty */
718 pgste_val(pgste) |= PGSTE_UC_BIT;
Martin Schwidefskyabf09be2012-11-07 13:17:37 +0100719 }
720 *ptep = entry;
Martin Schwidefsky0a61b222013-10-18 12:03:41 +0200721 return pgste;
Martin Schwidefskyabf09be2012-11-07 13:17:37 +0100722}
723
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200724/**
725 * struct gmap_struct - guest address space
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200726 * @crst_list: list of all crst tables used in the guest address space
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200727 * @mm: pointer to the parent mm_struct
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200728 * @guest_to_host: radix tree with guest to host address translation
729 * @host_to_guest: radix tree with pointer to segment table entries
730 * @guest_table_lock: spinlock to protect all entries in the guest page table
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200731 * @table: pointer to the page directory
Christian Borntraeger480e5922011-09-20 17:07:28 +0200732 * @asce: address space control element for gmap page table
Dominik Dingel24eb3a82013-06-17 16:25:18 +0200733 * @pfault_enabled: defines if pfaults are applicable for the guest
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200734 */
735struct gmap {
736 struct list_head list;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200737 struct list_head crst_list;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200738 struct mm_struct *mm;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200739 struct radix_tree_root guest_to_host;
740 struct radix_tree_root host_to_guest;
741 spinlock_t guest_table_lock;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200742 unsigned long *table;
Christian Borntraeger480e5922011-09-20 17:07:28 +0200743 unsigned long asce;
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200744 unsigned long asce_end;
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200745 void *private;
Dominik Dingel24eb3a82013-06-17 16:25:18 +0200746 bool pfault_enabled;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200747};
748
749/**
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200750 * struct gmap_notifier - notify function block for page invalidation
751 * @notifier_call: address of callback function
752 */
753struct gmap_notifier {
754 struct list_head list;
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200755 void (*notifier_call)(struct gmap *gmap, unsigned long gaddr);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200756};
757
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200758struct gmap *gmap_alloc(struct mm_struct *mm, unsigned long limit);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200759void gmap_free(struct gmap *gmap);
760void gmap_enable(struct gmap *gmap);
761void gmap_disable(struct gmap *gmap);
762int gmap_map_segment(struct gmap *gmap, unsigned long from,
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200763 unsigned long to, unsigned long len);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200764int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len);
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200765unsigned long __gmap_translate(struct gmap *, unsigned long gaddr);
766unsigned long gmap_translate(struct gmap *, unsigned long gaddr);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200767int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr);
768int gmap_fault(struct gmap *, unsigned long gaddr, unsigned int fault_flags);
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200769void gmap_discard(struct gmap *, unsigned long from, unsigned long to);
770void __gmap_zap(struct gmap *, unsigned long gaddr);
Dominik Dingela0bf4f12014-03-24 14:27:58 +0100771bool gmap_test_and_clear_dirty(unsigned long address, struct gmap *);
772
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200773
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200774void gmap_register_ipte_notifier(struct gmap_notifier *);
775void gmap_unregister_ipte_notifier(struct gmap_notifier *);
776int gmap_ipte_notify(struct gmap *, unsigned long start, unsigned long len);
Martin Schwidefsky9da4e382014-04-30 14:46:26 +0200777void gmap_do_ipte_notify(struct mm_struct *, unsigned long addr, pte_t *);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200778
779static inline pgste_t pgste_ipte_notify(struct mm_struct *mm,
Martin Schwidefsky55dbbdd2014-04-30 14:44:44 +0200780 unsigned long addr,
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200781 pte_t *ptep, pgste_t pgste)
782{
783#ifdef CONFIG_PGSTE
Martin Schwidefsky0d0dafc2013-05-17 14:41:33 +0200784 if (pgste_val(pgste) & PGSTE_IN_BIT) {
785 pgste_val(pgste) &= ~PGSTE_IN_BIT;
Martin Schwidefsky9da4e382014-04-30 14:46:26 +0200786 gmap_do_ipte_notify(mm, addr, ptep);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200787 }
788#endif
789 return pgste;
790}
791
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200792/*
793 * Certain architectures need to do special things when PTEs
794 * within a page table are directly modified. Thus, the following
795 * hook is made available.
796 */
797static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
798 pte_t *ptep, pte_t entry)
799{
800 pgste_t pgste;
801
802 if (mm_has_pgste(mm)) {
803 pgste = pgste_get_lock(ptep);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200804 pgste_val(pgste) &= ~_PGSTE_GPS_ZERO;
Dominik Dingel65eef3352014-01-14 15:02:11 +0100805 pgste_set_key(ptep, pgste, entry, mm);
Martin Schwidefsky0a61b222013-10-18 12:03:41 +0200806 pgste = pgste_set_pte(ptep, pgste, entry);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200807 pgste_set_unlock(ptep, pgste);
Martin Schwidefskyabf09be2012-11-07 13:17:37 +0100808 } else {
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200809 *ptep = entry;
Martin Schwidefskyabf09be2012-11-07 13:17:37 +0100810 }
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100811}
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813/*
814 * query functions pte_write/pte_dirty/pte_young only work if
815 * pte_present() is true. Undefined behaviour if not..
816 */
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800817static inline int pte_write(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200819 return (pte_val(pte) & _PAGE_WRITE) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800822static inline int pte_dirty(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200824 return (pte_val(pte) & _PAGE_DIRTY) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800827static inline int pte_young(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200829 return (pte_val(pte) & _PAGE_YOUNG) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200832#define __HAVE_ARCH_PTE_UNUSED
833static inline int pte_unused(pte_t pte)
834{
835 return pte_val(pte) & _PAGE_UNUSED;
836}
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838/*
839 * pgd/pmd/pte modification functions
840 */
841
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200842static inline void pgd_clear(pgd_t *pgd)
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100843{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100844 if ((pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
845 pgd_val(*pgd) = _REGION2_ENTRY_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100848static inline void pud_clear(pud_t *pud)
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100849{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200850 if ((pud_val(*pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
851 pud_val(*pud) = _REGION3_ENTRY_EMPTY;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100852}
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100853
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200854static inline void pmd_clear(pmd_t *pmdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200856 pmd_val(*pmdp) = _SEGMENT_ENTRY_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800859static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200861 pte_val(*ptep) = _PAGE_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
864/*
865 * The following pte modification functions only work if
866 * pte_present() is true. Undefined behaviour if not..
867 */
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800868static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Nick Piggin138c9022008-07-08 11:31:06 +0200870 pte_val(pte) &= _PAGE_CHG_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 pte_val(pte) |= pgprot_val(newprot);
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200872 /*
873 * newprot for PAGE_NONE, PAGE_READ and PAGE_WRITE has the
874 * invalid bit set, clear it again for readable, young pages
875 */
876 if ((pte_val(pte) & _PAGE_YOUNG) && (pte_val(pte) & _PAGE_READ))
877 pte_val(pte) &= ~_PAGE_INVALID;
878 /*
879 * newprot for PAGE_READ and PAGE_WRITE has the page protection
880 * bit set, clear it again for writable, dirty pages
881 */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200882 if ((pte_val(pte) & _PAGE_DIRTY) && (pte_val(pte) & _PAGE_WRITE))
883 pte_val(pte) &= ~_PAGE_PROTECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return pte;
885}
886
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800887static inline pte_t pte_wrprotect(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200889 pte_val(pte) &= ~_PAGE_WRITE;
890 pte_val(pte) |= _PAGE_PROTECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return pte;
892}
893
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800894static inline pte_t pte_mkwrite(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200896 pte_val(pte) |= _PAGE_WRITE;
897 if (pte_val(pte) & _PAGE_DIRTY)
898 pte_val(pte) &= ~_PAGE_PROTECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return pte;
900}
901
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800902static inline pte_t pte_mkclean(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200904 pte_val(pte) &= ~_PAGE_DIRTY;
905 pte_val(pte) |= _PAGE_PROTECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return pte;
907}
908
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800909static inline pte_t pte_mkdirty(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200911 pte_val(pte) |= _PAGE_DIRTY;
912 if (pte_val(pte) & _PAGE_WRITE)
913 pte_val(pte) &= ~_PAGE_PROTECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return pte;
915}
916
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800917static inline pte_t pte_mkold(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200919 pte_val(pte) &= ~_PAGE_YOUNG;
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200920 pte_val(pte) |= _PAGE_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return pte;
922}
923
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800924static inline pte_t pte_mkyoung(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200926 pte_val(pte) |= _PAGE_YOUNG;
927 if (pte_val(pte) & _PAGE_READ)
928 pte_val(pte) &= ~_PAGE_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return pte;
930}
931
Nick Piggin7e675132008-04-28 02:13:00 -0700932static inline pte_t pte_mkspecial(pte_t pte)
933{
Nick Piggina08cb622008-04-28 02:13:03 -0700934 pte_val(pte) |= _PAGE_SPECIAL;
Nick Piggin7e675132008-04-28 02:13:00 -0700935 return pte;
936}
937
Heiko Carstens84afdce2010-10-25 16:10:36 +0200938#ifdef CONFIG_HUGETLB_PAGE
939static inline pte_t pte_mkhuge(pte_t pte)
940{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200941 pte_val(pte) |= _PAGE_LARGE;
Heiko Carstens84afdce2010-10-25 16:10:36 +0200942 return pte;
943}
944#endif
945
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200946static inline void __ptep_ipte(unsigned long address, pte_t *ptep)
947{
Martin Schwidefsky53e857f2012-09-10 13:00:09 +0200948 unsigned long pto = (unsigned long) ptep;
949
Martin Schwidefsky53e857f2012-09-10 13:00:09 +0200950 /* Invalidation + global TLB flush for the pte */
951 asm volatile(
952 " ipte %2,%3"
953 : "=m" (*ptep) : "m" (*ptep), "a" (pto), "a" (address));
954}
955
Martin Schwidefsky1b948d62014-04-03 13:55:01 +0200956static inline void __ptep_ipte_local(unsigned long address, pte_t *ptep)
957{
958 unsigned long pto = (unsigned long) ptep;
959
Martin Schwidefsky1b948d62014-04-03 13:55:01 +0200960 /* Invalidation + local TLB flush for the pte */
961 asm volatile(
962 " .insn rrf,0xb2210000,%2,%3,0,1"
963 : "=m" (*ptep) : "m" (*ptep), "a" (pto), "a" (address));
964}
965
Heiko Carstenscfb0b242014-09-23 21:29:20 +0200966static inline void __ptep_ipte_range(unsigned long address, int nr, pte_t *ptep)
967{
968 unsigned long pto = (unsigned long) ptep;
969
Heiko Carstenscfb0b242014-09-23 21:29:20 +0200970 /* Invalidate a range of ptes + global TLB flush of the ptes */
971 do {
972 asm volatile(
973 " .insn rrf,0xb2210000,%2,%0,%1,0"
974 : "+a" (address), "+a" (nr) : "a" (pto) : "memory");
975 } while (nr != 255);
976}
977
Martin Schwidefsky53e857f2012-09-10 13:00:09 +0200978static inline void ptep_flush_direct(struct mm_struct *mm,
979 unsigned long address, pte_t *ptep)
980{
Martin Schwidefsky1b948d62014-04-03 13:55:01 +0200981 int active, count;
982
Martin Schwidefsky53e857f2012-09-10 13:00:09 +0200983 if (pte_val(*ptep) & _PAGE_INVALID)
984 return;
Martin Schwidefsky1b948d62014-04-03 13:55:01 +0200985 active = (mm == current->active_mm) ? 1 : 0;
986 count = atomic_add_return(0x10000, &mm->context.attach_count);
987 if (MACHINE_HAS_TLB_LC && (count & 0xffff) <= active &&
988 cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
989 __ptep_ipte_local(address, ptep);
990 else
991 __ptep_ipte(address, ptep);
992 atomic_sub(0x10000, &mm->context.attach_count);
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200993}
994
Martin Schwidefsky5c474a12013-08-16 13:31:40 +0200995static inline void ptep_flush_lazy(struct mm_struct *mm,
996 unsigned long address, pte_t *ptep)
997{
Martin Schwidefsky53e857f2012-09-10 13:00:09 +0200998 int active, count;
Martin Schwidefsky5c474a12013-08-16 13:31:40 +0200999
Martin Schwidefsky53e857f2012-09-10 13:00:09 +02001000 if (pte_val(*ptep) & _PAGE_INVALID)
1001 return;
1002 active = (mm == current->active_mm) ? 1 : 0;
1003 count = atomic_add_return(0x10000, &mm->context.attach_count);
1004 if ((count & 0xffff) <= active) {
1005 pte_val(*ptep) |= _PAGE_INVALID;
Martin Schwidefsky5c474a12013-08-16 13:31:40 +02001006 mm->context.flush_mm = 1;
Martin Schwidefsky53e857f2012-09-10 13:00:09 +02001007 } else
1008 __ptep_ipte(address, ptep);
1009 atomic_sub(0x10000, &mm->context.attach_count);
Martin Schwidefsky5c474a12013-08-16 13:31:40 +02001010}
1011
Martin Schwidefsky0a61b222013-10-18 12:03:41 +02001012/*
1013 * Get (and clear) the user dirty bit for a pte.
1014 */
1015static inline int ptep_test_and_clear_user_dirty(struct mm_struct *mm,
1016 unsigned long addr,
1017 pte_t *ptep)
1018{
1019 pgste_t pgste;
1020 pte_t pte;
1021 int dirty;
1022
1023 if (!mm_has_pgste(mm))
1024 return 0;
1025 pgste = pgste_get_lock(ptep);
1026 dirty = !!(pgste_val(pgste) & PGSTE_UC_BIT);
1027 pgste_val(pgste) &= ~PGSTE_UC_BIT;
1028 pte = *ptep;
1029 if (dirty && (pte_val(pte) & _PAGE_PRESENT)) {
Martin Schwidefsky55dbbdd2014-04-30 14:44:44 +02001030 pgste = pgste_ipte_notify(mm, addr, ptep, pgste);
Martin Schwidefsky0a61b222013-10-18 12:03:41 +02001031 __ptep_ipte(addr, ptep);
1032 if (MACHINE_HAS_ESOP || !(pte_val(pte) & _PAGE_WRITE))
1033 pte_val(pte) |= _PAGE_PROTECT;
1034 else
1035 pte_val(pte) |= _PAGE_INVALID;
1036 *ptep = pte;
1037 }
1038 pgste_set_unlock(ptep, pgste);
1039 return dirty;
1040}
1041
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001042#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
1043static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
1044 unsigned long addr, pte_t *ptep)
1045{
1046 pgste_t pgste;
Christian Borntraeger3e03d4c2014-08-28 21:21:41 +02001047 pte_t pte, oldpte;
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001048 int young;
1049
1050 if (mm_has_pgste(vma->vm_mm)) {
1051 pgste = pgste_get_lock(ptep);
Martin Schwidefsky55dbbdd2014-04-30 14:44:44 +02001052 pgste = pgste_ipte_notify(vma->vm_mm, addr, ptep, pgste);
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001053 }
1054
Christian Borntraeger3e03d4c2014-08-28 21:21:41 +02001055 oldpte = pte = *ptep;
Martin Schwidefsky53e857f2012-09-10 13:00:09 +02001056 ptep_flush_direct(vma->vm_mm, addr, ptep);
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001057 young = pte_young(pte);
1058 pte = pte_mkold(pte);
1059
1060 if (mm_has_pgste(vma->vm_mm)) {
Christian Borntraeger3e03d4c2014-08-28 21:21:41 +02001061 pgste = pgste_update_all(&oldpte, pgste, vma->vm_mm);
Martin Schwidefsky0a61b222013-10-18 12:03:41 +02001062 pgste = pgste_set_pte(ptep, pgste, pte);
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001063 pgste_set_unlock(ptep, pgste);
1064 } else
1065 *ptep = pte;
1066
1067 return young;
1068}
1069
1070#define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
1071static inline int ptep_clear_flush_young(struct vm_area_struct *vma,
1072 unsigned long address, pte_t *ptep)
1073{
1074 return ptep_test_and_clear_young(vma, address, ptep);
1075}
1076
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001077/*
1078 * This is hard to understand. ptep_get_and_clear and ptep_clear_flush
1079 * both clear the TLB for the unmapped pte. The reason is that
1080 * ptep_get_and_clear is used in common code (e.g. change_pte_range)
1081 * to modify an active pte. The sequence is
1082 * 1) ptep_get_and_clear
1083 * 2) set_pte_at
1084 * 3) flush_tlb_range
1085 * On s390 the tlb needs to get flushed with the modification of the pte
1086 * if the pte is active. The only way how this can be implemented is to
1087 * have ptep_get_and_clear do the tlb flush. In exchange flush_tlb_range
1088 * is a nop.
1089 */
1090#define __HAVE_ARCH_PTEP_GET_AND_CLEAR
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001091static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
1092 unsigned long address, pte_t *ptep)
1093{
1094 pgste_t pgste;
1095 pte_t pte;
1096
Martin Schwidefskyd3383632013-04-17 10:53:39 +02001097 if (mm_has_pgste(mm)) {
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001098 pgste = pgste_get_lock(ptep);
Martin Schwidefsky55dbbdd2014-04-30 14:44:44 +02001099 pgste = pgste_ipte_notify(mm, address, ptep, pgste);
Martin Schwidefskyd3383632013-04-17 10:53:39 +02001100 }
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001101
1102 pte = *ptep;
Martin Schwidefsky5c474a12013-08-16 13:31:40 +02001103 ptep_flush_lazy(mm, address, ptep);
Martin Schwidefskye5098612013-07-23 20:57:57 +02001104 pte_val(*ptep) = _PAGE_INVALID;
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001105
1106 if (mm_has_pgste(mm)) {
Dominik Dingel65eef3352014-01-14 15:02:11 +01001107 pgste = pgste_update_all(&pte, pgste, mm);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001108 pgste_set_unlock(ptep, pgste);
1109 }
1110 return pte;
1111}
1112
1113#define __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
1114static inline pte_t ptep_modify_prot_start(struct mm_struct *mm,
1115 unsigned long address,
1116 pte_t *ptep)
1117{
Martin Schwidefskyd3383632013-04-17 10:53:39 +02001118 pgste_t pgste;
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001119 pte_t pte;
1120
Martin Schwidefskyd3383632013-04-17 10:53:39 +02001121 if (mm_has_pgste(mm)) {
1122 pgste = pgste_get_lock(ptep);
Martin Schwidefsky55dbbdd2014-04-30 14:44:44 +02001123 pgste_ipte_notify(mm, address, ptep, pgste);
Martin Schwidefskyd3383632013-04-17 10:53:39 +02001124 }
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001125
1126 pte = *ptep;
Martin Schwidefsky5c474a12013-08-16 13:31:40 +02001127 ptep_flush_lazy(mm, address, ptep);
Christian Borntraegerb56433c2013-05-27 16:19:55 +02001128
Christian Borntraeger3a826032013-06-05 09:22:33 +02001129 if (mm_has_pgste(mm)) {
Dominik Dingel65eef3352014-01-14 15:02:11 +01001130 pgste = pgste_update_all(&pte, pgste, mm);
Christian Borntraeger3a826032013-06-05 09:22:33 +02001131 pgste_set(ptep, pgste);
1132 }
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001133 return pte;
1134}
1135
1136static inline void ptep_modify_prot_commit(struct mm_struct *mm,
1137 unsigned long address,
1138 pte_t *ptep, pte_t pte)
1139{
Christian Borntraegerb56433c2013-05-27 16:19:55 +02001140 pgste_t pgste;
1141
Martin Schwidefskyabf09be2012-11-07 13:17:37 +01001142 if (mm_has_pgste(mm)) {
Martin Schwidefskyd56c8932013-07-19 11:15:54 +02001143 pgste = pgste_get(ptep);
Dominik Dingel65eef3352014-01-14 15:02:11 +01001144 pgste_set_key(ptep, pgste, pte, mm);
Martin Schwidefsky0a61b222013-10-18 12:03:41 +02001145 pgste = pgste_set_pte(ptep, pgste, pte);
Christian Borntraegerb56433c2013-05-27 16:19:55 +02001146 pgste_set_unlock(ptep, pgste);
Martin Schwidefskyabf09be2012-11-07 13:17:37 +01001147 } else
1148 *ptep = pte;
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001149}
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001150
1151#define __HAVE_ARCH_PTEP_CLEAR_FLUSH
Martin Schwidefskyf0e47c22007-07-17 04:03:03 -07001152static inline pte_t ptep_clear_flush(struct vm_area_struct *vma,
1153 unsigned long address, pte_t *ptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001155 pgste_t pgste;
1156 pte_t pte;
1157
Martin Schwidefskyd3383632013-04-17 10:53:39 +02001158 if (mm_has_pgste(vma->vm_mm)) {
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001159 pgste = pgste_get_lock(ptep);
Martin Schwidefsky55dbbdd2014-04-30 14:44:44 +02001160 pgste = pgste_ipte_notify(vma->vm_mm, address, ptep, pgste);
Martin Schwidefskyd3383632013-04-17 10:53:39 +02001161 }
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001162
1163 pte = *ptep;
Martin Schwidefsky53e857f2012-09-10 13:00:09 +02001164 ptep_flush_direct(vma->vm_mm, address, ptep);
Martin Schwidefskye5098612013-07-23 20:57:57 +02001165 pte_val(*ptep) = _PAGE_INVALID;
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001166
1167 if (mm_has_pgste(vma->vm_mm)) {
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001168 if ((pgste_val(pgste) & _PGSTE_GPS_USAGE_MASK) ==
1169 _PGSTE_GPS_USAGE_UNUSED)
1170 pte_val(pte) |= _PAGE_UNUSED;
Dominik Dingel65eef3352014-01-14 15:02:11 +01001171 pgste = pgste_update_all(&pte, pgste, vma->vm_mm);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001172 pgste_set_unlock(ptep, pgste);
1173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return pte;
1175}
1176
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001177/*
1178 * The batched pte unmap code uses ptep_get_and_clear_full to clear the
1179 * ptes. Here an optimization is possible. tlb_gather_mmu flushes all
1180 * tlbs of an mm if it can guarantee that the ptes of the mm_struct
1181 * cannot be accessed while the batched unmap is running. In this case
1182 * full==1 and a simple pte_clear is enough. See tlb.h.
1183 */
1184#define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
1185static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001186 unsigned long address,
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001187 pte_t *ptep, int full)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001189 pgste_t pgste;
1190 pte_t pte;
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001191
Martin Schwidefskya055f662013-07-19 10:31:55 +02001192 if (!full && mm_has_pgste(mm)) {
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001193 pgste = pgste_get_lock(ptep);
Martin Schwidefsky55dbbdd2014-04-30 14:44:44 +02001194 pgste = pgste_ipte_notify(mm, address, ptep, pgste);
Martin Schwidefskyd3383632013-04-17 10:53:39 +02001195 }
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001196
1197 pte = *ptep;
1198 if (!full)
Martin Schwidefsky5c474a12013-08-16 13:31:40 +02001199 ptep_flush_lazy(mm, address, ptep);
Martin Schwidefskye5098612013-07-23 20:57:57 +02001200 pte_val(*ptep) = _PAGE_INVALID;
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001201
Martin Schwidefskya055f662013-07-19 10:31:55 +02001202 if (!full && mm_has_pgste(mm)) {
Dominik Dingel65eef3352014-01-14 15:02:11 +01001203 pgste = pgste_update_all(&pte, pgste, mm);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001204 pgste_set_unlock(ptep, pgste);
1205 }
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001206 return pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001209#define __HAVE_ARCH_PTEP_SET_WRPROTECT
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001210static inline pte_t ptep_set_wrprotect(struct mm_struct *mm,
1211 unsigned long address, pte_t *ptep)
1212{
1213 pgste_t pgste;
1214 pte_t pte = *ptep;
1215
1216 if (pte_write(pte)) {
Martin Schwidefskyd3383632013-04-17 10:53:39 +02001217 if (mm_has_pgste(mm)) {
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001218 pgste = pgste_get_lock(ptep);
Martin Schwidefsky55dbbdd2014-04-30 14:44:44 +02001219 pgste = pgste_ipte_notify(mm, address, ptep, pgste);
Martin Schwidefskyd3383632013-04-17 10:53:39 +02001220 }
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001221
Martin Schwidefsky5c474a12013-08-16 13:31:40 +02001222 ptep_flush_lazy(mm, address, ptep);
Martin Schwidefskyabf09be2012-11-07 13:17:37 +01001223 pte = pte_wrprotect(pte);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001224
Martin Schwidefskyabf09be2012-11-07 13:17:37 +01001225 if (mm_has_pgste(mm)) {
Martin Schwidefsky0a61b222013-10-18 12:03:41 +02001226 pgste = pgste_set_pte(ptep, pgste, pte);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001227 pgste_set_unlock(ptep, pgste);
Martin Schwidefskyabf09be2012-11-07 13:17:37 +01001228 } else
1229 *ptep = pte;
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001230 }
1231 return pte;
1232}
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001233
1234#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001235static inline int ptep_set_access_flags(struct vm_area_struct *vma,
1236 unsigned long address, pte_t *ptep,
1237 pte_t entry, int dirty)
1238{
1239 pgste_t pgste;
1240
1241 if (pte_same(*ptep, entry))
1242 return 0;
Martin Schwidefskyd3383632013-04-17 10:53:39 +02001243 if (mm_has_pgste(vma->vm_mm)) {
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001244 pgste = pgste_get_lock(ptep);
Martin Schwidefsky55dbbdd2014-04-30 14:44:44 +02001245 pgste = pgste_ipte_notify(vma->vm_mm, address, ptep, pgste);
Martin Schwidefskyd3383632013-04-17 10:53:39 +02001246 }
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001247
Martin Schwidefsky53e857f2012-09-10 13:00:09 +02001248 ptep_flush_direct(vma->vm_mm, address, ptep);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001249
Martin Schwidefskyabf09be2012-11-07 13:17:37 +01001250 if (mm_has_pgste(vma->vm_mm)) {
Christian Borntraeger19514972014-08-28 23:44:57 +02001251 pgste_set_key(ptep, pgste, entry, vma->vm_mm);
Martin Schwidefsky0a61b222013-10-18 12:03:41 +02001252 pgste = pgste_set_pte(ptep, pgste, entry);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001253 pgste_set_unlock(ptep, pgste);
Martin Schwidefskyabf09be2012-11-07 13:17:37 +01001254 } else
1255 *ptep = entry;
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001256 return 1;
1257}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 * Conversion functions: convert a page and protection to a page entry,
1261 * and a page entry and page directory to the page they refer to.
1262 */
1263static inline pte_t mk_pte_phys(unsigned long physpage, pgprot_t pgprot)
1264{
1265 pte_t __pte;
1266 pte_val(__pte) = physpage + pgprot_val(pgprot);
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001267 return pte_mkyoung(__pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Heiko Carstens2dcea572006-09-29 01:58:41 -07001270static inline pte_t mk_pte(struct page *page, pgprot_t pgprot)
1271{
Heiko Carstens0b2b6e1d2006-10-04 20:02:23 +02001272 unsigned long physpage = page_to_phys(page);
Martin Schwidefskyabf09be2012-11-07 13:17:37 +01001273 pte_t __pte = mk_pte_phys(physpage, pgprot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Martin Schwidefskye5098612013-07-23 20:57:57 +02001275 if (pte_write(__pte) && PageDirty(page))
1276 __pte = pte_mkdirty(__pte);
Martin Schwidefskyabf09be2012-11-07 13:17:37 +01001277 return __pte;
Heiko Carstens2dcea572006-09-29 01:58:41 -07001278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280#define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001281#define pud_index(address) (((address) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
1282#define pmd_index(address) (((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
1283#define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001285#define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286#define pgd_offset_k(address) pgd_offset(&init_mm, address)
1287
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001288#define pmd_deref(pmd) (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN)
1289#define pud_deref(pud) (pud_val(pud) & _REGION_ENTRY_ORIGIN)
Martin Schwidefsky5a216a22008-02-09 18:24:36 +01001290#define pgd_deref(pgd) (pgd_val(pgd) & _REGION_ENTRY_ORIGIN)
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001291
Martin Schwidefsky5a216a22008-02-09 18:24:36 +01001292static inline pud_t *pud_offset(pgd_t *pgd, unsigned long address)
1293{
Martin Schwidefsky6252d702008-02-09 18:24:37 +01001294 pud_t *pud = (pud_t *) pgd;
1295 if ((pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
1296 pud = (pud_t *) pgd_deref(*pgd);
Martin Schwidefsky5a216a22008-02-09 18:24:36 +01001297 return pud + pud_index(address);
1298}
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001299
1300static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address)
1301{
Martin Schwidefsky6252d702008-02-09 18:24:37 +01001302 pmd_t *pmd = (pmd_t *) pud;
1303 if ((pud_val(*pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
1304 pmd = (pmd_t *) pud_deref(*pud);
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001305 return pmd + pmd_index(address);
1306}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001308#define pfn_pte(pfn,pgprot) mk_pte_phys(__pa((pfn) << PAGE_SHIFT),(pgprot))
1309#define pte_pfn(x) (pte_val(x) >> PAGE_SHIFT)
1310#define pte_page(x) pfn_to_page(pte_pfn(x))
1311
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001312#define pmd_page(pmd) pfn_to_page(pmd_pfn(pmd))
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001313
1314/* Find an entry in the lowest level page table.. */
1315#define pte_offset(pmd, addr) ((pte_t *) pmd_deref(*(pmd)) + pte_index(addr))
1316#define pte_offset_kernel(pmd, address) pte_offset(pmd,address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317#define pte_offset_map(pmd, address) pte_offset_kernel(pmd, address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318#define pte_unmap(pte) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Gerald Schaefer106c9922013-04-29 15:07:23 -07001320#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLB_PAGE)
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001321static inline unsigned long massage_pgprot_pmd(pgprot_t pgprot)
1322{
Gerald Schaeferd8e7a332012-10-25 17:42:50 +02001323 /*
Martin Schwidefskye5098612013-07-23 20:57:57 +02001324 * pgprot is PAGE_NONE, PAGE_READ, or PAGE_WRITE (see __Pxxx / __Sxxx)
Gerald Schaeferd8e7a332012-10-25 17:42:50 +02001325 * Convert to segment table entry format.
1326 */
1327 if (pgprot_val(pgprot) == pgprot_val(PAGE_NONE))
1328 return pgprot_val(SEGMENT_NONE);
Martin Schwidefskye5098612013-07-23 20:57:57 +02001329 if (pgprot_val(pgprot) == pgprot_val(PAGE_READ))
1330 return pgprot_val(SEGMENT_READ);
1331 return pgprot_val(SEGMENT_WRITE);
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001332}
1333
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001334static inline pmd_t pmd_wrprotect(pmd_t pmd)
1335{
1336 pmd_val(pmd) &= ~_SEGMENT_ENTRY_WRITE;
1337 pmd_val(pmd) |= _SEGMENT_ENTRY_PROTECT;
1338 return pmd;
1339}
1340
1341static inline pmd_t pmd_mkwrite(pmd_t pmd)
1342{
1343 pmd_val(pmd) |= _SEGMENT_ENTRY_WRITE;
1344 if (pmd_large(pmd) && !(pmd_val(pmd) & _SEGMENT_ENTRY_DIRTY))
1345 return pmd;
1346 pmd_val(pmd) &= ~_SEGMENT_ENTRY_PROTECT;
1347 return pmd;
1348}
1349
1350static inline pmd_t pmd_mkclean(pmd_t pmd)
1351{
1352 if (pmd_large(pmd)) {
1353 pmd_val(pmd) &= ~_SEGMENT_ENTRY_DIRTY;
1354 pmd_val(pmd) |= _SEGMENT_ENTRY_PROTECT;
1355 }
1356 return pmd;
1357}
1358
1359static inline pmd_t pmd_mkdirty(pmd_t pmd)
1360{
1361 if (pmd_large(pmd)) {
1362 pmd_val(pmd) |= _SEGMENT_ENTRY_DIRTY;
1363 if (pmd_val(pmd) & _SEGMENT_ENTRY_WRITE)
1364 pmd_val(pmd) &= ~_SEGMENT_ENTRY_PROTECT;
1365 }
1366 return pmd;
1367}
1368
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001369static inline pmd_t pmd_mkyoung(pmd_t pmd)
1370{
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001371 if (pmd_large(pmd)) {
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001372 pmd_val(pmd) |= _SEGMENT_ENTRY_YOUNG;
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001373 if (pmd_val(pmd) & _SEGMENT_ENTRY_READ)
1374 pmd_val(pmd) &= ~_SEGMENT_ENTRY_INVALID;
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001375 }
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001376 return pmd;
1377}
1378
1379static inline pmd_t pmd_mkold(pmd_t pmd)
1380{
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001381 if (pmd_large(pmd)) {
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001382 pmd_val(pmd) &= ~_SEGMENT_ENTRY_YOUNG;
1383 pmd_val(pmd) |= _SEGMENT_ENTRY_INVALID;
1384 }
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001385 return pmd;
1386}
1387
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001388static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
1389{
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001390 if (pmd_large(pmd)) {
1391 pmd_val(pmd) &= _SEGMENT_ENTRY_ORIGIN_LARGE |
1392 _SEGMENT_ENTRY_DIRTY | _SEGMENT_ENTRY_YOUNG |
1393 _SEGMENT_ENTRY_LARGE | _SEGMENT_ENTRY_SPLIT;
1394 pmd_val(pmd) |= massage_pgprot_pmd(newprot);
1395 if (!(pmd_val(pmd) & _SEGMENT_ENTRY_DIRTY))
1396 pmd_val(pmd) |= _SEGMENT_ENTRY_PROTECT;
1397 if (!(pmd_val(pmd) & _SEGMENT_ENTRY_YOUNG))
1398 pmd_val(pmd) |= _SEGMENT_ENTRY_INVALID;
1399 return pmd;
1400 }
1401 pmd_val(pmd) &= _SEGMENT_ENTRY_ORIGIN;
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001402 pmd_val(pmd) |= massage_pgprot_pmd(newprot);
1403 return pmd;
1404}
1405
Gerald Schaefer106c9922013-04-29 15:07:23 -07001406static inline pmd_t mk_pmd_phys(unsigned long physpage, pgprot_t pgprot)
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001407{
Gerald Schaefer106c9922013-04-29 15:07:23 -07001408 pmd_t __pmd;
1409 pmd_val(__pmd) = physpage + massage_pgprot_pmd(pgprot);
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001410 return __pmd;
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001411}
1412
Gerald Schaefer106c9922013-04-29 15:07:23 -07001413#endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLB_PAGE */
1414
Martin Schwidefsky1b948d62014-04-03 13:55:01 +02001415static inline void __pmdp_csp(pmd_t *pmdp)
1416{
1417 register unsigned long reg2 asm("2") = pmd_val(*pmdp);
1418 register unsigned long reg3 asm("3") = pmd_val(*pmdp) |
1419 _SEGMENT_ENTRY_INVALID;
1420 register unsigned long reg4 asm("4") = ((unsigned long) pmdp) + 5;
1421
1422 asm volatile(
1423 " csp %1,%3"
1424 : "=m" (*pmdp)
1425 : "d" (reg2), "d" (reg3), "d" (reg4), "m" (*pmdp) : "cc");
1426}
1427
1428static inline void __pmdp_idte(unsigned long address, pmd_t *pmdp)
1429{
1430 unsigned long sto;
1431
1432 sto = (unsigned long) pmdp - pmd_index(address) * sizeof(pmd_t);
1433 asm volatile(
1434 " .insn rrf,0xb98e0000,%2,%3,0,0"
1435 : "=m" (*pmdp)
1436 : "m" (*pmdp), "a" (sto), "a" ((address & HPAGE_MASK))
1437 : "cc" );
1438}
1439
1440static inline void __pmdp_idte_local(unsigned long address, pmd_t *pmdp)
1441{
1442 unsigned long sto;
1443
1444 sto = (unsigned long) pmdp - pmd_index(address) * sizeof(pmd_t);
1445 asm volatile(
1446 " .insn rrf,0xb98e0000,%2,%3,0,1"
1447 : "=m" (*pmdp)
1448 : "m" (*pmdp), "a" (sto), "a" ((address & HPAGE_MASK))
1449 : "cc" );
1450}
1451
1452static inline void pmdp_flush_direct(struct mm_struct *mm,
1453 unsigned long address, pmd_t *pmdp)
1454{
1455 int active, count;
1456
1457 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
1458 return;
1459 if (!MACHINE_HAS_IDTE) {
1460 __pmdp_csp(pmdp);
1461 return;
1462 }
1463 active = (mm == current->active_mm) ? 1 : 0;
1464 count = atomic_add_return(0x10000, &mm->context.attach_count);
1465 if (MACHINE_HAS_TLB_LC && (count & 0xffff) <= active &&
1466 cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
1467 __pmdp_idte_local(address, pmdp);
1468 else
1469 __pmdp_idte(address, pmdp);
1470 atomic_sub(0x10000, &mm->context.attach_count);
1471}
1472
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001473static inline void pmdp_flush_lazy(struct mm_struct *mm,
1474 unsigned long address, pmd_t *pmdp)
1475{
Martin Schwidefsky53e857f2012-09-10 13:00:09 +02001476 int active, count;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001477
Martin Schwidefsky1b948d62014-04-03 13:55:01 +02001478 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
1479 return;
Martin Schwidefsky53e857f2012-09-10 13:00:09 +02001480 active = (mm == current->active_mm) ? 1 : 0;
1481 count = atomic_add_return(0x10000, &mm->context.attach_count);
1482 if ((count & 0xffff) <= active) {
1483 pmd_val(*pmdp) |= _SEGMENT_ENTRY_INVALID;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001484 mm->context.flush_mm = 1;
Martin Schwidefsky1b948d62014-04-03 13:55:01 +02001485 } else if (MACHINE_HAS_IDTE)
1486 __pmdp_idte(address, pmdp);
1487 else
1488 __pmdp_csp(pmdp);
Martin Schwidefsky53e857f2012-09-10 13:00:09 +02001489 atomic_sub(0x10000, &mm->context.attach_count);
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001490}
1491
Gerald Schaefer106c9922013-04-29 15:07:23 -07001492#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1493
1494#define __HAVE_ARCH_PGTABLE_DEPOSIT
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001495extern void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
1496 pgtable_t pgtable);
Gerald Schaefer106c9922013-04-29 15:07:23 -07001497
1498#define __HAVE_ARCH_PGTABLE_WITHDRAW
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001499extern pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp);
Gerald Schaefer106c9922013-04-29 15:07:23 -07001500
1501static inline int pmd_trans_splitting(pmd_t pmd)
1502{
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001503 return (pmd_val(pmd) & _SEGMENT_ENTRY_LARGE) &&
1504 (pmd_val(pmd) & _SEGMENT_ENTRY_SPLIT);
Gerald Schaefer106c9922013-04-29 15:07:23 -07001505}
1506
1507static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
1508 pmd_t *pmdp, pmd_t entry)
1509{
Gerald Schaefer106c9922013-04-29 15:07:23 -07001510 *pmdp = entry;
1511}
1512
1513static inline pmd_t pmd_mkhuge(pmd_t pmd)
1514{
1515 pmd_val(pmd) |= _SEGMENT_ENTRY_LARGE;
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001516 pmd_val(pmd) |= _SEGMENT_ENTRY_YOUNG;
1517 pmd_val(pmd) |= _SEGMENT_ENTRY_PROTECT;
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001518 return pmd;
1519}
1520
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001521#define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
1522static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
1523 unsigned long address, pmd_t *pmdp)
1524{
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001525 pmd_t pmd;
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001526
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001527 pmd = *pmdp;
Martin Schwidefsky1b948d62014-04-03 13:55:01 +02001528 pmdp_flush_direct(vma->vm_mm, address, pmdp);
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001529 *pmdp = pmd_mkold(pmd);
1530 return pmd_young(pmd);
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001531}
1532
1533#define __HAVE_ARCH_PMDP_GET_AND_CLEAR
1534static inline pmd_t pmdp_get_and_clear(struct mm_struct *mm,
1535 unsigned long address, pmd_t *pmdp)
1536{
1537 pmd_t pmd = *pmdp;
1538
Martin Schwidefsky1b948d62014-04-03 13:55:01 +02001539 pmdp_flush_direct(mm, address, pmdp);
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001540 pmd_clear(pmdp);
1541 return pmd;
1542}
1543
Martin Schwidefskyfcbe08d62014-10-24 10:52:29 +02001544#define __HAVE_ARCH_PMDP_GET_AND_CLEAR_FULL
1545static inline pmd_t pmdp_get_and_clear_full(struct mm_struct *mm,
1546 unsigned long address,
1547 pmd_t *pmdp, int full)
1548{
1549 pmd_t pmd = *pmdp;
1550
1551 if (!full)
1552 pmdp_flush_lazy(mm, address, pmdp);
1553 pmd_clear(pmdp);
1554 return pmd;
1555}
1556
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001557#define __HAVE_ARCH_PMDP_CLEAR_FLUSH
1558static inline pmd_t pmdp_clear_flush(struct vm_area_struct *vma,
1559 unsigned long address, pmd_t *pmdp)
1560{
1561 return pmdp_get_and_clear(vma->vm_mm, address, pmdp);
1562}
1563
1564#define __HAVE_ARCH_PMDP_INVALIDATE
1565static inline void pmdp_invalidate(struct vm_area_struct *vma,
1566 unsigned long address, pmd_t *pmdp)
1567{
Martin Schwidefsky1b948d62014-04-03 13:55:01 +02001568 pmdp_flush_direct(vma->vm_mm, address, pmdp);
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001569}
1570
Gerald Schaeferbe328652013-01-21 16:48:07 +01001571#define __HAVE_ARCH_PMDP_SET_WRPROTECT
1572static inline void pmdp_set_wrprotect(struct mm_struct *mm,
1573 unsigned long address, pmd_t *pmdp)
1574{
1575 pmd_t pmd = *pmdp;
1576
1577 if (pmd_write(pmd)) {
Martin Schwidefsky1b948d62014-04-03 13:55:01 +02001578 pmdp_flush_direct(mm, address, pmdp);
Gerald Schaeferbe328652013-01-21 16:48:07 +01001579 set_pmd_at(mm, address, pmdp, pmd_wrprotect(pmd));
1580 }
1581}
1582
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001583#define pfn_pmd(pfn, pgprot) mk_pmd_phys(__pa((pfn) << PAGE_SHIFT), (pgprot))
1584#define mk_pmd(page, pgprot) pfn_pmd(page_to_pfn(page), (pgprot))
1585
1586static inline int pmd_trans_huge(pmd_t pmd)
1587{
1588 return pmd_val(pmd) & _SEGMENT_ENTRY_LARGE;
1589}
1590
1591static inline int has_transparent_hugepage(void)
1592{
1593 return MACHINE_HAS_HPAGE ? 1 : 0;
1594}
Gerald Schaefer75077af2012-10-08 16:30:15 -07001595#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597/*
1598 * 31 bit swap entry format:
1599 * A page-table entry has some bits we have to treat in a special way.
1600 * Bits 0, 20 and bit 23 have to be zero, otherwise an specification
1601 * exception will occur instead of a page translation exception. The
1602 * specifiation exception has the bad habit not to store necessary
1603 * information in the lowcore.
Martin Schwidefskye5098612013-07-23 20:57:57 +02001604 * Bits 21, 22, 30 and 31 are used to indicate the page type.
1605 * A swap pte is indicated by bit pattern (pte & 0x603) == 0x402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 * This leaves the bits 1-19 and bits 24-29 to store type and offset.
1607 * We use the 5 bits from 25-29 for the type and the 20 bits from 1-19
1608 * plus 24 for the offset.
1609 * 0| offset |0110|o|type |00|
1610 * 0 0000000001111111111 2222 2 22222 33
1611 * 0 1234567890123456789 0123 4 56789 01
1612 *
1613 * 64 bit swap entry format:
1614 * A page-table entry has some bits we have to treat in a special way.
1615 * Bits 52 and bit 55 have to be zero, otherwise an specification
1616 * exception will occur instead of a page translation exception. The
1617 * specifiation exception has the bad habit not to store necessary
1618 * information in the lowcore.
Martin Schwidefskye5098612013-07-23 20:57:57 +02001619 * Bits 53, 54, 62 and 63 are used to indicate the page type.
1620 * A swap pte is indicated by bit pattern (pte & 0x603) == 0x402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 * This leaves the bits 0-51 and bits 56-61 to store type and offset.
1622 * We use the 5 bits from 57-61 for the type and the 53 bits from 0-51
1623 * plus 56 for the offset.
1624 * | offset |0110|o|type |00|
1625 * 0000000000111111111122222222223333333333444444444455 5555 5 55566 66
1626 * 0123456789012345678901234567890123456789012345678901 2345 6 78901 23
1627 */
Heiko Carstens5a79859a2015-02-12 13:08:27 +01001628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629#define __SWP_OFFSET_MASK (~0UL >> 11)
Heiko Carstens5a79859a2015-02-12 13:08:27 +01001630
Adrian Bunk4448aaf2005-11-08 21:34:42 -08001631static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
1633 pte_t pte;
1634 offset &= __SWP_OFFSET_MASK;
Martin Schwidefskye5098612013-07-23 20:57:57 +02001635 pte_val(pte) = _PAGE_INVALID | _PAGE_TYPE | ((type & 0x1f) << 2) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 ((offset & 1UL) << 7) | ((offset & ~1UL) << 11);
1637 return pte;
1638}
1639
1640#define __swp_type(entry) (((entry).val >> 2) & 0x1f)
1641#define __swp_offset(entry) (((entry).val >> 11) | (((entry).val >> 7) & 1))
1642#define __swp_entry(type,offset) ((swp_entry_t) { pte_val(mk_swap_pte((type),(offset))) })
1643
1644#define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
1645#define __swp_entry_to_pte(x) ((pte_t) { (x).val })
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647#endif /* !__ASSEMBLY__ */
1648
1649#define kern_addr_valid(addr) (1)
1650
Heiko Carstens17f34582008-04-30 13:38:47 +02001651extern int vmem_add_mapping(unsigned long start, unsigned long size);
1652extern int vmem_remove_mapping(unsigned long start, unsigned long size);
Carsten Otte402b0862008-03-25 18:47:10 +01001653extern int s390_enable_sie(void);
Dominik Dingel3ac8e382014-10-23 12:09:17 +02001654extern int s390_enable_skey(void);
Dominik Dingela13cff32014-10-23 12:07:14 +02001655extern void s390_reset_cmma(struct mm_struct *mm);
Heiko Carstensf4eb07c2006-12-08 15:56:07 +01001656
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +01001657/* s390 has a private copy of get unmapped area to deal with cache synonyms */
1658#define HAVE_ARCH_UNMAPPED_AREA
1659#define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661/*
1662 * No page table caches to initialise
1663 */
Heiko Carstens765a0ca2013-03-23 10:29:01 +01001664static inline void pgtable_cache_init(void) { }
1665static inline void check_pgt_cache(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667#include <asm-generic/pgtable.h>
1668
1669#endif /* _S390_PAGE_H */