blob: 6f8a67a103be8f6d1b011b7602a633028eede47e [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/*
Martin Schwidefskya1c843b2015-04-22 13:55:59 +020015 * The Linux memory management assumes a three-level page table setup.
16 * For s390 64 bit we use up to four of the five levels the hardware
17 * provides (region first tables are not used).
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 *
19 * The "pgd_xxx()" functions are trivial for a folded two-level
20 * setup: the pgd is never bad, and a pmd always exists (as it's folded
21 * into the pgd entry)
22 *
23 * This file contains the functions and defines necessary to modify and use
24 * the S390 page table tree.
25 */
26#ifndef __ASSEMBLY__
Heiko Carstens9789db02008-07-14 09:59:11 +020027#include <linux/sched.h>
Heiko Carstens2dcea572006-09-29 01:58:41 -070028#include <linux/mm_types.h>
Martin Schwidefskyabf09be2012-11-07 13:17:37 +010029#include <linux/page-flags.h>
Martin Schwidefsky527e30b2014-04-30 16:04:25 +020030#include <linux/radix-tree.h>
Heiko Carstens37cd9442016-05-20 08:08:14 +020031#include <linux/atomic.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
Heiko Carstens0ccb32c2016-05-28 10:03:55 +020035extern pgd_t swapper_pg_dir[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070036extern void paging_init(void);
Heiko Carstens2b67fc42007-02-05 21:16:47 +010037extern void vmem_map_init(void);
Heiko Carstense8a97e42016-05-17 10:50:15 +020038pmd_t *vmem_pmd_alloc(void);
39pte_t *vmem_pte_alloc(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Heiko Carstens37cd9442016-05-20 08:08:14 +020041enum {
42 PG_DIRECT_MAP_4K = 0,
43 PG_DIRECT_MAP_1M,
44 PG_DIRECT_MAP_2G,
45 PG_DIRECT_MAP_MAX
46};
47
48extern atomic_long_t direct_pages_count[PG_DIRECT_MAP_MAX];
49
50static inline void update_page_count(int level, long count)
51{
52 if (IS_ENABLED(CONFIG_PROC_FS))
53 atomic_long_add(count, &direct_pages_count[level]);
54}
55
56struct seq_file;
57void arch_report_meminfo(struct seq_file *m);
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
60 * The S390 doesn't have any external MMU info: the kernel page
61 * tables contain all the necessary information.
62 */
Russell King4b3073e2009-12-18 16:40:18 +000063#define update_mmu_cache(vma, address, ptep) do { } while (0)
David Millerb113da62012-10-08 16:34:25 -070064#define update_mmu_cache_pmd(vma, address, ptep) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/*
Martin Schwidefsky238ec4e2010-10-25 16:10:07 +020067 * ZERO_PAGE is a global shared page that is always zero; used
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * for zero-mapped memory areas etc..
69 */
Martin Schwidefsky238ec4e2010-10-25 16:10:07 +020070
71extern unsigned long empty_zero_page;
72extern unsigned long zero_page_mask;
73
74#define ZERO_PAGE(vaddr) \
75 (virt_to_page((void *)(empty_zero_page + \
76 (((unsigned long)(vaddr)) &zero_page_mask))))
Kirill A. Shutemov816422a2012-12-12 13:52:36 -080077#define __HAVE_COLOR_ZERO_PAGE
Martin Schwidefsky238ec4e2010-10-25 16:10:07 +020078
Linus Torvalds4f2e2902013-04-17 08:46:19 -070079/* TODO: s390 cannot support io_remap_pfn_range... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#endif /* !__ASSEMBLY__ */
81
82/*
83 * PMD_SHIFT determines the size of the area a second-level page
84 * table can map
85 * PGDIR_SHIFT determines what a third-level page table entry can map
86 */
Heiko Carstens5a79859a2015-02-12 13:08:27 +010087#define PMD_SHIFT 20
88#define PUD_SHIFT 31
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +020089#define P4D_SHIFT 42
90#define PGDIR_SHIFT 53
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#define PMD_SIZE (1UL << PMD_SHIFT)
93#define PMD_MASK (~(PMD_SIZE-1))
Martin Schwidefsky190a1d72007-10-22 12:52:48 +020094#define PUD_SIZE (1UL << PUD_SHIFT)
95#define PUD_MASK (~(PUD_SIZE-1))
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +020096#define P4D_SIZE (1UL << P4D_SHIFT)
97#define P4D_MASK (~(P4D_SIZE-1))
Martin Schwidefsky5a216a22008-02-09 18:24:36 +010098#define PGDIR_SIZE (1UL << PGDIR_SHIFT)
99#define PGDIR_MASK (~(PGDIR_SIZE-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/*
102 * entries per page directory level: the S390 is two-level, so
103 * we don't really have any PMD directory physically.
104 * for S390 segment-table entries are combined to one PGD
105 * that leads to 1024 pte per pgd
106 */
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100107#define PTRS_PER_PTE 256
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100108#define PTRS_PER_PMD 2048
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100109#define PTRS_PER_PUD 2048
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +0200110#define PTRS_PER_P4D 2048
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100111#define PTRS_PER_PGD 2048
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Kirill A. Shutemovd016bf72015-02-11 15:26:41 -0800113#define FIRST_USER_ADDRESS 0UL
Hugh Dickinsd455a362005-04-19 13:29:23 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define pte_ERROR(e) \
116 printk("%s:%d: bad pte %p.\n", __FILE__, __LINE__, (void *) pte_val(e))
117#define pmd_ERROR(e) \
118 printk("%s:%d: bad pmd %p.\n", __FILE__, __LINE__, (void *) pmd_val(e))
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200119#define pud_ERROR(e) \
120 printk("%s:%d: bad pud %p.\n", __FILE__, __LINE__, (void *) pud_val(e))
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +0200121#define p4d_ERROR(e) \
122 printk("%s:%d: bad p4d %p.\n", __FILE__, __LINE__, (void *) p4d_val(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#define pgd_ERROR(e) \
124 printk("%s:%d: bad pgd %p.\n", __FILE__, __LINE__, (void *) pgd_val(e))
125
126#ifndef __ASSEMBLY__
127/*
Martin Schwidefskya1c843b2015-04-22 13:55:59 +0200128 * The vmalloc and module area will always be on the topmost area of the
129 * kernel mapping. We reserve 128GB (64bit) for vmalloc and modules.
Heiko Carstensc972cc62012-10-05 16:52:18 +0200130 * On 64 bit kernels we have a 2GB area at the top of the vmalloc area where
131 * modules will reside. That makes sure that inter module branches always
132 * happen without trampolines and in addition the placement within a 2GB frame
133 * is branch prediction unit friendly.
Heiko Carstens8b62bc92006-12-04 15:40:56 +0100134 */
Heiko Carstens239a64252009-06-12 10:26:33 +0200135extern unsigned long VMALLOC_START;
Martin Schwidefsky14045eb2011-12-27 11:27:07 +0100136extern unsigned long VMALLOC_END;
137extern struct page *vmemmap;
Heiko Carstens239a64252009-06-12 10:26:33 +0200138
Martin Schwidefsky14045eb2011-12-27 11:27:07 +0100139#define VMEM_MAX_PHYS ((unsigned long) vmemmap)
Christian Borntraeger5fd9c6e2008-01-26 14:11:00 +0100140
Heiko Carstensc972cc62012-10-05 16:52:18 +0200141extern unsigned long MODULES_VADDR;
142extern unsigned long MODULES_END;
143#define MODULES_VADDR MODULES_VADDR
144#define MODULES_END MODULES_END
145#define MODULES_LEN (1UL << 31)
Heiko Carstensc972cc62012-10-05 16:52:18 +0200146
Heiko Carstensc9331462014-10-15 12:17:38 +0200147static inline int is_module_addr(void *addr)
148{
Heiko Carstensc9331462014-10-15 12:17:38 +0200149 BUILD_BUG_ON(MODULES_LEN > (1UL << 31));
150 if (addr < (void *)MODULES_VADDR)
151 return 0;
152 if (addr > (void *)MODULES_END)
153 return 0;
Heiko Carstensc9331462014-10-15 12:17:38 +0200154 return 1;
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * A 64 bit pagetable entry of S390 has following format:
Christian Borntraeger6a985c62009-12-07 12:52:11 +0100159 * | PFRA |0IPC| OS |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * 0000000000111111111122222222223333333333444444444455555555556666
161 * 0123456789012345678901234567890123456789012345678901234567890123
162 *
163 * I Page-Invalid Bit: Page is not available for address-translation
164 * P Page-Protection Bit: Store access not possible for page
Christian Borntraeger6a985c62009-12-07 12:52:11 +0100165 * C Change-bit override: HW is not required to set change bit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 *
167 * A 64 bit segmenttable entry of S390 has following format:
168 * | P-table origin | TT
169 * 0000000000111111111122222222223333333333444444444455555555556666
170 * 0123456789012345678901234567890123456789012345678901234567890123
171 *
172 * I Segment-Invalid Bit: Segment is not available for address-translation
173 * C Common-Segment Bit: Segment is not private (PoP 3-30)
174 * P Page-Protection Bit: Store access not possible for page
175 * TT Type 00
176 *
177 * A 64 bit region table entry of S390 has following format:
178 * | S-table origin | TF TTTL
179 * 0000000000111111111122222222223333333333444444444455555555556666
180 * 0123456789012345678901234567890123456789012345678901234567890123
181 *
182 * I Segment-Invalid Bit: Segment is not available for address-translation
183 * TT Type 01
184 * TF
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200185 * TL Table length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 *
187 * The 64 bit regiontable origin of S390 has following format:
188 * | region table origon | DTTL
189 * 0000000000111111111122222222223333333333444444444455555555556666
190 * 0123456789012345678901234567890123456789012345678901234567890123
191 *
192 * X Space-Switch event:
193 * G Segment-Invalid Bit:
194 * P Private-Space Bit:
195 * S Storage-Alteration:
196 * R Real space
197 * TL Table-Length:
198 *
199 * A storage key has the following format:
200 * | ACC |F|R|C|0|
201 * 0 3 4 5 6 7
202 * ACC: access key
203 * F : fetch protection bit
204 * R : referenced bit
205 * C : changed bit
206 */
207
208/* Hardware bits in the page table entry */
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100209#define _PAGE_NOEXEC 0x100 /* HW no-execute bit */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200210#define _PAGE_PROTECT 0x200 /* HW read-only bit */
Martin Schwidefsky83377482006-10-18 18:30:51 +0200211#define _PAGE_INVALID 0x400 /* HW invalid bit */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200212#define _PAGE_LARGE 0x800 /* Bit to mark a large pte */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200213
214/* Software bits in the page table entry */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200215#define _PAGE_PRESENT 0x001 /* SW pte present bit */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200216#define _PAGE_YOUNG 0x004 /* SW pte young bit */
217#define _PAGE_DIRTY 0x008 /* SW pte dirty bit */
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200218#define _PAGE_READ 0x010 /* SW pte read bit */
219#define _PAGE_WRITE 0x020 /* SW pte write bit */
220#define _PAGE_SPECIAL 0x040 /* SW associated with special page */
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200221#define _PAGE_UNUSED 0x080 /* SW bit for pgste usage state */
Nick Piggina08cb622008-04-28 02:13:03 -0700222#define __HAVE_ARCH_PTE_SPECIAL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Martin Schwidefsky5614dd92015-04-22 14:47:42 +0200224#ifdef CONFIG_MEM_SOFT_DIRTY
225#define _PAGE_SOFT_DIRTY 0x002 /* SW pte soft dirty bit */
226#else
227#define _PAGE_SOFT_DIRTY 0x000
228#endif
229
Nick Piggin138c9022008-07-08 11:31:06 +0200230/* Set of bits not changed in pte_modify */
Heiko Carstens6a5c1482014-09-22 08:50:51 +0200231#define _PAGE_CHG_MASK (PAGE_MASK | _PAGE_SPECIAL | _PAGE_DIRTY | \
Martin Schwidefsky5614dd92015-04-22 14:47:42 +0200232 _PAGE_YOUNG | _PAGE_SOFT_DIRTY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Martin Schwidefsky83377482006-10-18 18:30:51 +0200234/*
Kirill A. Shutemov6e76d4b2015-02-10 14:11:04 -0800235 * handle_pte_fault uses pte_present and pte_none to find out the pte type
236 * WITHOUT holding the page table lock. The _PAGE_PRESENT bit is used to
237 * distinguish present from not-present ptes. It is changed only with the page
238 * table lock held.
Martin Schwidefsky83377482006-10-18 18:30:51 +0200239 *
Martin Schwidefskye5098612013-07-23 20:57:57 +0200240 * The following table gives the different possible bit combinations for
Martin Schwidefskya1c843b2015-04-22 13:55:59 +0200241 * the pte hardware and software bits in the last 12 bits of a pte
242 * (. unassigned bit, x don't care, t swap type):
Martin Schwidefsky83377482006-10-18 18:30:51 +0200243 *
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200244 * 842100000000
245 * 000084210000
246 * 000000008421
Martin Schwidefskya1c843b2015-04-22 13:55:59 +0200247 * .IR.uswrdy.p
248 * empty .10.00000000
249 * swap .11..ttttt.0
250 * prot-none, clean, old .11.xx0000.1
251 * prot-none, clean, young .11.xx0001.1
Gerald Schaeferbc29b7a2016-07-18 14:35:13 +0200252 * prot-none, dirty, old .11.xx0010.1
253 * prot-none, dirty, young .11.xx0011.1
Martin Schwidefskya1c843b2015-04-22 13:55:59 +0200254 * read-only, clean, old .11.xx0100.1
255 * read-only, clean, young .01.xx0101.1
256 * read-only, dirty, old .11.xx0110.1
257 * read-only, dirty, young .01.xx0111.1
258 * read-write, clean, old .11.xx1100.1
259 * read-write, clean, young .01.xx1101.1
260 * read-write, dirty, old .10.xx1110.1
261 * read-write, dirty, young .00.xx1111.1
262 * HW-bits: R read-only, I invalid
263 * SW-bits: p present, y young, d dirty, r read, w write, s special,
264 * u unused, l large
Martin Schwidefskye5098612013-07-23 20:57:57 +0200265 *
Martin Schwidefskya1c843b2015-04-22 13:55:59 +0200266 * pte_none is true for the bit pattern .10.00000000, pte == 0x400
267 * pte_swap is true for the bit pattern .11..ooooo.0, (pte & 0x201) == 0x200
268 * pte_present is true for the bit pattern .xx.xxxxxx.1, (pte & 0x001) == 0x001
Martin Schwidefsky83377482006-10-18 18:30:51 +0200269 */
270
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200271/* Bits in the segment/region table address-space-control-element */
272#define _ASCE_ORIGIN ~0xfffUL/* segment table origin */
273#define _ASCE_PRIVATE_SPACE 0x100 /* private space control */
274#define _ASCE_ALT_EVENT 0x80 /* storage alteration event control */
275#define _ASCE_SPACE_SWITCH 0x40 /* space switch event */
276#define _ASCE_REAL_SPACE 0x20 /* real space control */
277#define _ASCE_TYPE_MASK 0x0c /* asce table type mask */
278#define _ASCE_TYPE_REGION1 0x0c /* region first table type */
279#define _ASCE_TYPE_REGION2 0x08 /* region second table type */
280#define _ASCE_TYPE_REGION3 0x04 /* region third table type */
281#define _ASCE_TYPE_SEGMENT 0x00 /* segment table type */
282#define _ASCE_TABLE_LENGTH 0x03 /* region table length */
283
284/* Bits in the region table entry */
285#define _REGION_ENTRY_ORIGIN ~0xfffUL/* region/segment table origin */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200286#define _REGION_ENTRY_PROTECT 0x200 /* region protection bit */
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100287#define _REGION_ENTRY_NOEXEC 0x100 /* region no-execute bit */
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100288#define _REGION_ENTRY_OFFSET 0xc0 /* region table offset */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200289#define _REGION_ENTRY_INVALID 0x20 /* invalid region table entry */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200290#define _REGION_ENTRY_TYPE_MASK 0x0c /* region/segment table type mask */
291#define _REGION_ENTRY_TYPE_R1 0x0c /* region first table type */
292#define _REGION_ENTRY_TYPE_R2 0x08 /* region second table type */
293#define _REGION_ENTRY_TYPE_R3 0x04 /* region third table type */
294#define _REGION_ENTRY_LENGTH 0x03 /* region third length */
295
296#define _REGION1_ENTRY (_REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_LENGTH)
Martin Schwidefskye5098612013-07-23 20:57:57 +0200297#define _REGION1_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INVALID)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200298#define _REGION2_ENTRY (_REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_LENGTH)
Martin Schwidefskye5098612013-07-23 20:57:57 +0200299#define _REGION2_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INVALID)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200300#define _REGION3_ENTRY (_REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_LENGTH)
Martin Schwidefskye5098612013-07-23 20:57:57 +0200301#define _REGION3_ENTRY_EMPTY (_REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INVALID)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200302
Heiko Carstens9e20b4d2016-05-10 10:34:47 +0200303#define _REGION3_ENTRY_ORIGIN_LARGE ~0x7fffffffUL /* large page address */
Heiko Carstens2dffdcb2016-05-11 10:52:07 +0200304#define _REGION3_ENTRY_DIRTY 0x2000 /* SW region dirty bit */
305#define _REGION3_ENTRY_YOUNG 0x1000 /* SW region young bit */
306#define _REGION3_ENTRY_LARGE 0x0400 /* RTTE-format control, large page */
307#define _REGION3_ENTRY_READ 0x0002 /* SW region read bit */
308#define _REGION3_ENTRY_WRITE 0x0001 /* SW region write bit */
309
310#ifdef CONFIG_MEM_SOFT_DIRTY
311#define _REGION3_ENTRY_SOFT_DIRTY 0x4000 /* SW region soft dirty bit */
312#else
313#define _REGION3_ENTRY_SOFT_DIRTY 0x0000 /* SW region soft dirty bit */
314#endif
315
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +0200316#define _REGION_ENTRY_BITS 0xfffffffffffff22fUL
317#define _REGION_ENTRY_BITS_LARGE 0xffffffff8000fe2fUL
Gerald Schaeferd08de8e2016-07-04 14:47:01 +0200318
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200319/* Bits in the segment table entry */
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200320#define _SEGMENT_ENTRY_BITS 0xfffffffffffffe33UL
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200321#define _SEGMENT_ENTRY_BITS_LARGE 0xfffffffffff0ff33UL
Heiko Carstensea815312013-03-21 12:50:39 +0100322#define _SEGMENT_ENTRY_ORIGIN_LARGE ~0xfffffUL /* large page address */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200323#define _SEGMENT_ENTRY_ORIGIN ~0x7ffUL/* segment table origin */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200324#define _SEGMENT_ENTRY_PROTECT 0x200 /* page protection bit */
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100325#define _SEGMENT_ENTRY_NOEXEC 0x100 /* region no-execute bit */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200326#define _SEGMENT_ENTRY_INVALID 0x20 /* invalid segment table entry */
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200327
328#define _SEGMENT_ENTRY (0)
Martin Schwidefskye5098612013-07-23 20:57:57 +0200329#define _SEGMENT_ENTRY_EMPTY (_SEGMENT_ENTRY_INVALID)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200330
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200331#define _SEGMENT_ENTRY_DIRTY 0x2000 /* SW segment dirty bit */
332#define _SEGMENT_ENTRY_YOUNG 0x1000 /* SW segment young bit */
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200333#define _SEGMENT_ENTRY_LARGE 0x0400 /* STE-format control, large page */
Gerald Schaeferbc29b7a2016-07-18 14:35:13 +0200334#define _SEGMENT_ENTRY_WRITE 0x0002 /* SW segment write bit */
335#define _SEGMENT_ENTRY_READ 0x0001 /* SW segment read bit */
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200336
Martin Schwidefsky5614dd92015-04-22 14:47:42 +0200337#ifdef CONFIG_MEM_SOFT_DIRTY
338#define _SEGMENT_ENTRY_SOFT_DIRTY 0x4000 /* SW segment soft dirty bit */
339#else
340#define _SEGMENT_ENTRY_SOFT_DIRTY 0x0000 /* SW segment soft dirty bit */
341#endif
342
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200343/*
Heiko Carstens2dffdcb2016-05-11 10:52:07 +0200344 * Segment table and region3 table entry encoding
345 * (R = read-only, I = invalid, y = young bit):
Gerald Schaeferbc29b7a2016-07-18 14:35:13 +0200346 * dy..R...I...wr
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200347 * prot-none, clean, old 00..1...1...00
348 * prot-none, clean, young 01..1...1...00
349 * prot-none, dirty, old 10..1...1...00
350 * prot-none, dirty, young 11..1...1...00
Gerald Schaeferbc29b7a2016-07-18 14:35:13 +0200351 * read-only, clean, old 00..1...1...01
352 * read-only, clean, young 01..1...0...01
353 * read-only, dirty, old 10..1...1...01
354 * read-only, dirty, young 11..1...0...01
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200355 * read-write, clean, old 00..1...1...11
356 * read-write, clean, young 01..1...0...11
357 * read-write, dirty, old 10..0...1...11
358 * read-write, dirty, young 11..0...0...11
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200359 * The segment table origin is used to distinguish empty (origin==0) from
360 * read-write, old segment table entries (origin!=0)
Martin Schwidefskya1c843b2015-04-22 13:55:59 +0200361 * HW-bits: R read-only, I invalid
362 * SW-bits: y young, d dirty, r read, w write
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200363 */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200364
Martin Schwidefsky6c61cfe2011-06-06 14:14:42 +0200365/* Page status table bits for virtualization */
Martin Schwidefsky0d0dafc2013-05-17 14:41:33 +0200366#define PGSTE_ACC_BITS 0xf000000000000000UL
367#define PGSTE_FP_BIT 0x0800000000000000UL
368#define PGSTE_PCL_BIT 0x0080000000000000UL
369#define PGSTE_HR_BIT 0x0040000000000000UL
370#define PGSTE_HC_BIT 0x0020000000000000UL
371#define PGSTE_GR_BIT 0x0004000000000000UL
372#define PGSTE_GC_BIT 0x0002000000000000UL
Martin Schwidefsky0a61b222013-10-18 12:03:41 +0200373#define PGSTE_UC_BIT 0x0000800000000000UL /* user dirty (migration) */
374#define PGSTE_IN_BIT 0x0000400000000000UL /* IPTE notify bit */
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100375#define PGSTE_VSIE_BIT 0x0000200000000000UL /* ref'd in a shadow table */
Martin Schwidefsky6c61cfe2011-06-06 14:14:42 +0200376
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200377/* Guest Page State used for virtualization */
Claudio Imbrenda2d42f942017-04-20 10:03:45 +0200378#define _PGSTE_GPS_ZERO 0x0000000080000000UL
379#define _PGSTE_GPS_USAGE_MASK 0x0000000003000000UL
380#define _PGSTE_GPS_USAGE_STABLE 0x0000000000000000UL
381#define _PGSTE_GPS_USAGE_UNUSED 0x0000000001000000UL
382#define _PGSTE_GPS_USAGE_POT_VOLATILE 0x0000000002000000UL
383#define _PGSTE_GPS_USAGE_VOLATILE _PGSTE_GPS_USAGE_MASK
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200384
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200385/*
386 * A user page table pointer has the space-switch-event bit, the
387 * private-space-control bit and the storage-alteration-event-control
388 * bit set. A kernel page table pointer doesn't need them.
389 */
390#define _ASCE_USER_BITS (_ASCE_SPACE_SWITCH | _ASCE_PRIVATE_SPACE | \
391 _ASCE_ALT_EVENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/*
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200394 * Page protection definitions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
Gerald Schaeferbc29b7a2016-07-18 14:35:13 +0200396#define PAGE_NONE __pgprot(_PAGE_PRESENT | _PAGE_INVALID | _PAGE_PROTECT)
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100397#define PAGE_RO __pgprot(_PAGE_PRESENT | _PAGE_READ | \
398 _PAGE_NOEXEC | _PAGE_INVALID | _PAGE_PROTECT)
399#define PAGE_RX __pgprot(_PAGE_PRESENT | _PAGE_READ | \
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200400 _PAGE_INVALID | _PAGE_PROTECT)
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100401#define PAGE_RW __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE | \
402 _PAGE_NOEXEC | _PAGE_INVALID | _PAGE_PROTECT)
403#define PAGE_RWX __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE | \
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200404 _PAGE_INVALID | _PAGE_PROTECT)
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200405
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200406#define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE | \
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100407 _PAGE_YOUNG | _PAGE_DIRTY | _PAGE_NOEXEC)
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200408#define PAGE_KERNEL __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE | \
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100409 _PAGE_YOUNG | _PAGE_DIRTY | _PAGE_NOEXEC)
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200410#define PAGE_KERNEL_RO __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_YOUNG | \
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100411 _PAGE_PROTECT | _PAGE_NOEXEC)
412#define PAGE_KERNEL_EXEC __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE | \
413 _PAGE_YOUNG | _PAGE_DIRTY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415/*
Martin Schwidefsky043d0702011-05-23 10:24:23 +0200416 * On s390 the page table entry has an invalid bit and a read-only bit.
417 * Read permission implies execute permission and write permission
418 * implies read permission.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 */
420 /*xwr*/
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200421#define __P000 PAGE_NONE
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100422#define __P001 PAGE_RO
423#define __P010 PAGE_RO
424#define __P011 PAGE_RO
425#define __P100 PAGE_RX
426#define __P101 PAGE_RX
427#define __P110 PAGE_RX
428#define __P111 PAGE_RX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200430#define __S000 PAGE_NONE
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100431#define __S001 PAGE_RO
432#define __S010 PAGE_RW
433#define __S011 PAGE_RW
434#define __S100 PAGE_RX
435#define __S101 PAGE_RX
436#define __S110 PAGE_RWX
437#define __S111 PAGE_RWX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Gerald Schaefer106c9922013-04-29 15:07:23 -0700439/*
440 * Segment entry (large page) protection definitions.
441 */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200442#define SEGMENT_NONE __pgprot(_SEGMENT_ENTRY_INVALID | \
443 _SEGMENT_ENTRY_PROTECT)
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100444#define SEGMENT_RO __pgprot(_SEGMENT_ENTRY_PROTECT | \
445 _SEGMENT_ENTRY_READ | \
446 _SEGMENT_ENTRY_NOEXEC)
447#define SEGMENT_RX __pgprot(_SEGMENT_ENTRY_PROTECT | \
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200448 _SEGMENT_ENTRY_READ)
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100449#define SEGMENT_RW __pgprot(_SEGMENT_ENTRY_READ | \
450 _SEGMENT_ENTRY_WRITE | \
451 _SEGMENT_ENTRY_NOEXEC)
452#define SEGMENT_RWX __pgprot(_SEGMENT_ENTRY_READ | \
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200453 _SEGMENT_ENTRY_WRITE)
Heiko Carstens2dffdcb2016-05-11 10:52:07 +0200454#define SEGMENT_KERNEL __pgprot(_SEGMENT_ENTRY | \
455 _SEGMENT_ENTRY_LARGE | \
456 _SEGMENT_ENTRY_READ | \
457 _SEGMENT_ENTRY_WRITE | \
458 _SEGMENT_ENTRY_YOUNG | \
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100459 _SEGMENT_ENTRY_DIRTY | \
460 _SEGMENT_ENTRY_NOEXEC)
Heiko Carstens2dffdcb2016-05-11 10:52:07 +0200461#define SEGMENT_KERNEL_RO __pgprot(_SEGMENT_ENTRY | \
462 _SEGMENT_ENTRY_LARGE | \
463 _SEGMENT_ENTRY_READ | \
464 _SEGMENT_ENTRY_YOUNG | \
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100465 _SEGMENT_ENTRY_PROTECT | \
466 _SEGMENT_ENTRY_NOEXEC)
Heiko Carstens2dffdcb2016-05-11 10:52:07 +0200467
468/*
469 * Region3 entry (large page) protection definitions.
470 */
471
472#define REGION3_KERNEL __pgprot(_REGION_ENTRY_TYPE_R3 | \
473 _REGION3_ENTRY_LARGE | \
474 _REGION3_ENTRY_READ | \
475 _REGION3_ENTRY_WRITE | \
476 _REGION3_ENTRY_YOUNG | \
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100477 _REGION3_ENTRY_DIRTY | \
478 _REGION_ENTRY_NOEXEC)
Heiko Carstens2dffdcb2016-05-11 10:52:07 +0200479#define REGION3_KERNEL_RO __pgprot(_REGION_ENTRY_TYPE_R3 | \
480 _REGION3_ENTRY_LARGE | \
481 _REGION3_ENTRY_READ | \
482 _REGION3_ENTRY_YOUNG | \
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100483 _REGION_ENTRY_PROTECT | \
484 _REGION_ENTRY_NOEXEC)
Gerald Schaefer106c9922013-04-29 15:07:23 -0700485
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200486static inline int mm_has_pgste(struct mm_struct *mm)
487{
488#ifdef CONFIG_PGSTE
489 if (unlikely(mm->context.has_pgste))
490 return 1;
491#endif
492 return 0;
493}
Dominik Dingel65eef3352014-01-14 15:02:11 +0100494
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +0200495static inline int mm_alloc_pgste(struct mm_struct *mm)
496{
497#ifdef CONFIG_PGSTE
498 if (unlikely(mm->context.alloc_pgste))
499 return 1;
500#endif
501 return 0;
502}
503
Dominik Dingel2faee8f2014-10-23 12:08:38 +0200504/*
505 * In the case that a guest uses storage keys
506 * faults should no longer be backed by zero pages
507 */
508#define mm_forbids_zeropage mm_use_skey
Dominik Dingel65eef3352014-01-14 15:02:11 +0100509static inline int mm_use_skey(struct mm_struct *mm)
510{
511#ifdef CONFIG_PGSTE
512 if (mm->context.use_skey)
513 return 1;
514#endif
515 return 0;
516}
517
Heiko Carstens4ccccc52016-05-14 10:46:33 +0200518static inline void csp(unsigned int *ptr, unsigned int old, unsigned int new)
519{
520 register unsigned long reg2 asm("2") = old;
521 register unsigned long reg3 asm("3") = new;
522 unsigned long address = (unsigned long)ptr | 1;
523
524 asm volatile(
525 " csp %0,%3"
526 : "+d" (reg2), "+m" (*ptr)
527 : "d" (reg3), "d" (address)
528 : "cc");
529}
530
Heiko Carstense8a97e42016-05-17 10:50:15 +0200531static inline void cspg(unsigned long *ptr, unsigned long old, unsigned long new)
532{
533 register unsigned long reg2 asm("2") = old;
534 register unsigned long reg3 asm("3") = new;
535 unsigned long address = (unsigned long)ptr | 1;
536
537 asm volatile(
538 " .insn rre,0xb98a0000,%0,%3"
539 : "+d" (reg2), "+m" (*ptr)
540 : "d" (reg3), "d" (address)
541 : "cc");
542}
543
544#define CRDTE_DTT_PAGE 0x00UL
545#define CRDTE_DTT_SEGMENT 0x10UL
546#define CRDTE_DTT_REGION3 0x14UL
547#define CRDTE_DTT_REGION2 0x18UL
548#define CRDTE_DTT_REGION1 0x1cUL
549
550static inline void crdte(unsigned long old, unsigned long new,
551 unsigned long table, unsigned long dtt,
552 unsigned long address, unsigned long asce)
553{
554 register unsigned long reg2 asm("2") = old;
555 register unsigned long reg3 asm("3") = new;
556 register unsigned long reg4 asm("4") = table | dtt;
557 register unsigned long reg5 asm("5") = address;
558
559 asm volatile(".insn rrf,0xb98f0000,%0,%2,%4,0"
560 : "+d" (reg2)
561 : "d" (reg3), "d" (reg4), "d" (reg5), "a" (asce)
562 : "memory", "cc");
563}
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565/*
566 * pgd/pmd/pte query functions
567 */
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100568static inline int pgd_present(pgd_t pgd)
569{
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +0200570 if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R1)
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100571 return 1;
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100572 return (pgd_val(pgd) & _REGION_ENTRY_ORIGIN) != 0UL;
573}
574
575static inline int pgd_none(pgd_t pgd)
576{
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +0200577 if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R1)
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100578 return 0;
Martin Schwidefskye5098612013-07-23 20:57:57 +0200579 return (pgd_val(pgd) & _REGION_ENTRY_INVALID) != 0UL;
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100580}
581
582static inline int pgd_bad(pgd_t pgd)
583{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100584 /*
585 * With dynamic page table levels the pgd can be a region table
586 * entry or a segment table entry. Check for the bit that are
587 * invalid for either table entry.
588 */
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100589 unsigned long mask =
Martin Schwidefskye5098612013-07-23 20:57:57 +0200590 ~_SEGMENT_ENTRY_ORIGIN & ~_REGION_ENTRY_INVALID &
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100591 ~_REGION_ENTRY_TYPE_MASK & ~_REGION_ENTRY_LENGTH;
592 return (pgd_val(pgd) & mask) != 0;
593}
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200594
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +0200595static inline int p4d_present(p4d_t p4d)
596{
597 if ((p4d_val(p4d) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R2)
598 return 1;
599 return (p4d_val(p4d) & _REGION_ENTRY_ORIGIN) != 0UL;
600}
601
602static inline int p4d_none(p4d_t p4d)
603{
604 if ((p4d_val(p4d) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R2)
605 return 0;
606 return p4d_val(p4d) == _REGION2_ENTRY_EMPTY;
607}
608
609static inline unsigned long p4d_pfn(p4d_t p4d)
610{
611 unsigned long origin_mask;
612
613 origin_mask = _REGION_ENTRY_ORIGIN;
614 return (p4d_val(p4d) & origin_mask) >> PAGE_SHIFT;
615}
616
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200617static inline int pud_present(pud_t pud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100619 if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R3)
620 return 1;
Martin Schwidefsky0d017922007-12-17 16:25:48 +0100621 return (pud_val(pud) & _REGION_ENTRY_ORIGIN) != 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200624static inline int pud_none(pud_t pud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100626 if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R3)
627 return 0;
Gerald Schaeferd08de8e2016-07-04 14:47:01 +0200628 return pud_val(pud) == _REGION3_ENTRY_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Heiko Carstens18da2362012-10-08 09:18:26 +0200631static inline int pud_large(pud_t pud)
632{
633 if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) != _REGION_ENTRY_TYPE_R3)
634 return 0;
635 return !!(pud_val(pud) & _REGION3_ENTRY_LARGE);
636}
637
Heiko Carstens9e20b4d2016-05-10 10:34:47 +0200638static inline unsigned long pud_pfn(pud_t pud)
639{
640 unsigned long origin_mask;
641
Heiko Carstensf96c6f72017-05-22 13:27:34 +0200642 origin_mask = _REGION_ENTRY_ORIGIN;
Heiko Carstens9e20b4d2016-05-10 10:34:47 +0200643 if (pud_large(pud))
644 origin_mask = _REGION3_ENTRY_ORIGIN_LARGE;
645 return (pud_val(pud) & origin_mask) >> PAGE_SHIFT;
646}
647
Gerald Schaeferd08de8e2016-07-04 14:47:01 +0200648static inline int pmd_large(pmd_t pmd)
649{
650 return (pmd_val(pmd) & _SEGMENT_ENTRY_LARGE) != 0;
651}
652
653static inline int pmd_bad(pmd_t pmd)
654{
655 if (pmd_large(pmd))
656 return (pmd_val(pmd) & ~_SEGMENT_ENTRY_BITS_LARGE) != 0;
657 return (pmd_val(pmd) & ~_SEGMENT_ENTRY_BITS) != 0;
658}
659
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200660static inline int pud_bad(pud_t pud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Gerald Schaeferd08de8e2016-07-04 14:47:01 +0200662 if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R3)
663 return pmd_bad(__pmd(pud_val(pud)));
664 if (pud_large(pud))
665 return (pud_val(pud) & ~_REGION_ENTRY_BITS_LARGE) != 0;
666 return (pud_val(pud) & ~_REGION_ENTRY_BITS) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +0200669static inline int p4d_bad(p4d_t p4d)
670{
671 if ((p4d_val(p4d) & _REGION_ENTRY_TYPE_MASK) < _REGION_ENTRY_TYPE_R2)
672 return pud_bad(__pud(p4d_val(p4d)));
673 return (p4d_val(p4d) & ~_REGION_ENTRY_BITS) != 0;
674}
675
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800676static inline int pmd_present(pmd_t pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Dominik Dingel54397bb2016-04-27 11:43:07 +0200678 return pmd_val(pmd) != _SEGMENT_ENTRY_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800681static inline int pmd_none(pmd_t pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Dominik Dingel54397bb2016-04-27 11:43:07 +0200683 return pmd_val(pmd) == _SEGMENT_ENTRY_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Martin Schwidefsky7cded342015-05-13 14:33:22 +0200686static inline unsigned long pmd_pfn(pmd_t pmd)
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200687{
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200688 unsigned long origin_mask;
689
690 origin_mask = _SEGMENT_ENTRY_ORIGIN;
691 if (pmd_large(pmd))
692 origin_mask = _SEGMENT_ENTRY_ORIGIN_LARGE;
693 return (pmd_val(pmd) & origin_mask) >> PAGE_SHIFT;
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200694}
695
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -0700696#define __HAVE_ARCH_PMD_WRITE
697static inline int pmd_write(pmd_t pmd)
698{
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200699 return (pmd_val(pmd) & _SEGMENT_ENTRY_WRITE) != 0;
700}
701
702static inline int pmd_dirty(pmd_t pmd)
703{
704 int dirty = 1;
705 if (pmd_large(pmd))
706 dirty = (pmd_val(pmd) & _SEGMENT_ENTRY_DIRTY) != 0;
707 return dirty;
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -0700708}
709
710static inline int pmd_young(pmd_t pmd)
711{
Martin Schwidefsky152125b2014-07-24 11:03:41 +0200712 int young = 1;
713 if (pmd_large(pmd))
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200714 young = (pmd_val(pmd) & _SEGMENT_ENTRY_YOUNG) != 0;
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200715 return young;
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -0700716}
717
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800718static inline int pte_present(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200720 /* Bit pattern: (pte & 0x001) == 0x001 */
721 return (pte_val(pte) & _PAGE_PRESENT) != 0;
722}
723
724static inline int pte_none(pte_t pte)
725{
726 /* Bit pattern: pte == 0x400 */
727 return pte_val(pte) == _PAGE_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200730static inline int pte_swap(pte_t pte)
731{
Martin Schwidefskya1c843b2015-04-22 13:55:59 +0200732 /* Bit pattern: (pte & 0x201) == 0x200 */
733 return (pte_val(pte) & (_PAGE_PROTECT | _PAGE_PRESENT))
734 == _PAGE_PROTECT;
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200735}
736
Nick Piggin7e675132008-04-28 02:13:00 -0700737static inline int pte_special(pte_t pte)
738{
Nick Piggina08cb622008-04-28 02:13:03 -0700739 return (pte_val(pte) & _PAGE_SPECIAL);
Nick Piggin7e675132008-04-28 02:13:00 -0700740}
741
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200742#define __HAVE_ARCH_PTE_SAME
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200743static inline int pte_same(pte_t a, pte_t b)
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100744{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200745 return pte_val(a) == pte_val(b);
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100746}
747
Martin Schwidefskyb54565b2014-09-23 14:01:34 +0200748#ifdef CONFIG_NUMA_BALANCING
749static inline int pte_protnone(pte_t pte)
750{
751 return pte_present(pte) && !(pte_val(pte) & _PAGE_READ);
752}
753
754static inline int pmd_protnone(pmd_t pmd)
755{
756 /* pmd_large(pmd) implies pmd_present(pmd) */
757 return pmd_large(pmd) && !(pmd_val(pmd) & _SEGMENT_ENTRY_READ);
758}
759#endif
760
Martin Schwidefsky5614dd92015-04-22 14:47:42 +0200761static inline int pte_soft_dirty(pte_t pte)
762{
763 return pte_val(pte) & _PAGE_SOFT_DIRTY;
764}
765#define pte_swp_soft_dirty pte_soft_dirty
766
767static inline pte_t pte_mksoft_dirty(pte_t pte)
768{
769 pte_val(pte) |= _PAGE_SOFT_DIRTY;
770 return pte;
771}
772#define pte_swp_mksoft_dirty pte_mksoft_dirty
773
774static inline pte_t pte_clear_soft_dirty(pte_t pte)
775{
776 pte_val(pte) &= ~_PAGE_SOFT_DIRTY;
777 return pte;
778}
779#define pte_swp_clear_soft_dirty pte_clear_soft_dirty
780
781static inline int pmd_soft_dirty(pmd_t pmd)
782{
783 return pmd_val(pmd) & _SEGMENT_ENTRY_SOFT_DIRTY;
784}
785
786static inline pmd_t pmd_mksoft_dirty(pmd_t pmd)
787{
788 pmd_val(pmd) |= _SEGMENT_ENTRY_SOFT_DIRTY;
789 return pmd;
790}
791
792static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd)
793{
794 pmd_val(pmd) &= ~_SEGMENT_ENTRY_SOFT_DIRTY;
795 return pmd;
796}
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798/*
799 * query functions pte_write/pte_dirty/pte_young only work if
800 * pte_present() is true. Undefined behaviour if not..
801 */
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800802static inline int pte_write(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200804 return (pte_val(pte) & _PAGE_WRITE) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800807static inline int pte_dirty(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200809 return (pte_val(pte) & _PAGE_DIRTY) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800812static inline int pte_young(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200814 return (pte_val(pte) & _PAGE_YOUNG) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200817#define __HAVE_ARCH_PTE_UNUSED
818static inline int pte_unused(pte_t pte)
819{
820 return pte_val(pte) & _PAGE_UNUSED;
821}
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823/*
824 * pgd/pmd/pte modification functions
825 */
826
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200827static inline void pgd_clear(pgd_t *pgd)
Martin Schwidefsky5a216a22008-02-09 18:24:36 +0100828{
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +0200829 if ((pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R1)
830 pgd_val(*pgd) = _REGION1_ENTRY_EMPTY;
831}
832
833static inline void p4d_clear(p4d_t *p4d)
834{
835 if ((p4d_val(*p4d) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
836 p4d_val(*p4d) = _REGION2_ENTRY_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100839static inline void pud_clear(pud_t *pud)
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100840{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200841 if ((pud_val(*pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
842 pud_val(*pud) = _REGION3_ENTRY_EMPTY;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100843}
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100844
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200845static inline void pmd_clear(pmd_t *pmdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Dominik Dingel54397bb2016-04-27 11:43:07 +0200847 pmd_val(*pmdp) = _SEGMENT_ENTRY_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800850static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200852 pte_val(*ptep) = _PAGE_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
855/*
856 * The following pte modification functions only work if
857 * pte_present() is true. Undefined behaviour if not..
858 */
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800859static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Nick Piggin138c9022008-07-08 11:31:06 +0200861 pte_val(pte) &= _PAGE_CHG_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 pte_val(pte) |= pgprot_val(newprot);
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200863 /*
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100864 * newprot for PAGE_NONE, PAGE_RO, PAGE_RX, PAGE_RW and PAGE_RWX
865 * has the invalid bit set, clear it again for readable, young pages
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200866 */
867 if ((pte_val(pte) & _PAGE_YOUNG) && (pte_val(pte) & _PAGE_READ))
868 pte_val(pte) &= ~_PAGE_INVALID;
869 /*
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100870 * newprot for PAGE_RO, PAGE_RX, PAGE_RW and PAGE_RWX has the page
871 * protection bit set, clear it again for writable, dirty pages
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200872 */
Martin Schwidefskye5098612013-07-23 20:57:57 +0200873 if ((pte_val(pte) & _PAGE_DIRTY) && (pte_val(pte) & _PAGE_WRITE))
874 pte_val(pte) &= ~_PAGE_PROTECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return pte;
876}
877
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800878static inline pte_t pte_wrprotect(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200880 pte_val(pte) &= ~_PAGE_WRITE;
881 pte_val(pte) |= _PAGE_PROTECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return pte;
883}
884
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800885static inline pte_t pte_mkwrite(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200887 pte_val(pte) |= _PAGE_WRITE;
888 if (pte_val(pte) & _PAGE_DIRTY)
889 pte_val(pte) &= ~_PAGE_PROTECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return pte;
891}
892
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800893static inline pte_t pte_mkclean(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200895 pte_val(pte) &= ~_PAGE_DIRTY;
896 pte_val(pte) |= _PAGE_PROTECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return pte;
898}
899
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800900static inline pte_t pte_mkdirty(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Martin Schwidefsky5614dd92015-04-22 14:47:42 +0200902 pte_val(pte) |= _PAGE_DIRTY | _PAGE_SOFT_DIRTY;
Martin Schwidefskye5098612013-07-23 20:57:57 +0200903 if (pte_val(pte) & _PAGE_WRITE)
904 pte_val(pte) &= ~_PAGE_PROTECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return pte;
906}
907
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800908static inline pte_t pte_mkold(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200910 pte_val(pte) &= ~_PAGE_YOUNG;
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200911 pte_val(pte) |= _PAGE_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return pte;
913}
914
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800915static inline pte_t pte_mkyoung(pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200917 pte_val(pte) |= _PAGE_YOUNG;
918 if (pte_val(pte) & _PAGE_READ)
919 pte_val(pte) &= ~_PAGE_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return pte;
921}
922
Nick Piggin7e675132008-04-28 02:13:00 -0700923static inline pte_t pte_mkspecial(pte_t pte)
924{
Nick Piggina08cb622008-04-28 02:13:03 -0700925 pte_val(pte) |= _PAGE_SPECIAL;
Nick Piggin7e675132008-04-28 02:13:00 -0700926 return pte;
927}
928
Heiko Carstens84afdce2010-10-25 16:10:36 +0200929#ifdef CONFIG_HUGETLB_PAGE
930static inline pte_t pte_mkhuge(pte_t pte)
931{
Martin Schwidefskye5098612013-07-23 20:57:57 +0200932 pte_val(pte) |= _PAGE_LARGE;
Heiko Carstens84afdce2010-10-25 16:10:36 +0200933 return pte;
934}
935#endif
936
Martin Schwidefsky34eeaf32016-06-14 12:38:40 +0200937#define IPTE_GLOBAL 0
938#define IPTE_LOCAL 1
939
940static inline void __ptep_ipte(unsigned long address, pte_t *ptep, int local)
Gerald Schaefer9282ed92006-09-20 15:59:37 +0200941{
Martin Schwidefsky53e857f2012-09-10 13:00:09 +0200942 unsigned long pto = (unsigned long) ptep;
943
Martin Schwidefsky34eeaf32016-06-14 12:38:40 +0200944 /* Invalidation + TLB flush for the pte */
Martin Schwidefsky53e857f2012-09-10 13:00:09 +0200945 asm volatile(
Martin Schwidefsky34eeaf32016-06-14 12:38:40 +0200946 " .insn rrf,0xb2210000,%[r1],%[r2],0,%[m4]"
947 : "+m" (*ptep) : [r1] "a" (pto), [r2] "a" (address),
948 [m4] "i" (local));
Martin Schwidefsky53e857f2012-09-10 13:00:09 +0200949}
950
Martin Schwidefsky34eeaf32016-06-14 12:38:40 +0200951static inline void __ptep_ipte_range(unsigned long address, int nr,
952 pte_t *ptep, int local)
Martin Schwidefsky1b948d62014-04-03 13:55:01 +0200953{
954 unsigned long pto = (unsigned long) ptep;
955
Martin Schwidefsky34eeaf32016-06-14 12:38:40 +0200956 /* Invalidate a range of ptes + TLB flush of the ptes */
Heiko Carstenscfb0b242014-09-23 21:29:20 +0200957 do {
958 asm volatile(
Martin Schwidefsky34eeaf32016-06-14 12:38:40 +0200959 " .insn rrf,0xb2210000,%[r1],%[r2],%[r3],%[m4]"
960 : [r2] "+a" (address), [r3] "+a" (nr)
961 : [r1] "a" (pto), [m4] "i" (local) : "memory");
Heiko Carstenscfb0b242014-09-23 21:29:20 +0200962 } while (nr != 255);
963}
964
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200965/*
966 * This is hard to understand. ptep_get_and_clear and ptep_clear_flush
967 * both clear the TLB for the unmapped pte. The reason is that
968 * ptep_get_and_clear is used in common code (e.g. change_pte_range)
969 * to modify an active pte. The sequence is
970 * 1) ptep_get_and_clear
971 * 2) set_pte_at
972 * 3) flush_tlb_range
973 * On s390 the tlb needs to get flushed with the modification of the pte
974 * if the pte is active. The only way how this can be implemented is to
975 * have ptep_get_and_clear do the tlb flush. In exchange flush_tlb_range
976 * is a nop.
977 */
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100978pte_t ptep_xchg_direct(struct mm_struct *, unsigned long, pte_t *, pte_t);
979pte_t ptep_xchg_lazy(struct mm_struct *, unsigned long, pte_t *, pte_t);
980
981#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
982static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
983 unsigned long addr, pte_t *ptep)
984{
985 pte_t pte = *ptep;
986
987 pte = ptep_xchg_direct(vma->vm_mm, addr, ptep, pte_mkold(pte));
988 return pte_young(pte);
989}
990
991#define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
992static inline int ptep_clear_flush_young(struct vm_area_struct *vma,
993 unsigned long address, pte_t *ptep)
994{
995 return ptep_test_and_clear_young(vma, address, ptep);
996}
997
Martin Schwidefskyba8a9222007-10-22 12:52:44 +0200998#define __HAVE_ARCH_PTEP_GET_AND_CLEAR
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +0200999static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001000 unsigned long addr, pte_t *ptep)
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001001{
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001002 return ptep_xchg_lazy(mm, addr, ptep, __pte(_PAGE_INVALID));
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001003}
1004
1005#define __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001006pte_t ptep_modify_prot_start(struct mm_struct *, unsigned long, pte_t *);
1007void ptep_modify_prot_commit(struct mm_struct *, unsigned long, pte_t *, pte_t);
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001008
1009#define __HAVE_ARCH_PTEP_CLEAR_FLUSH
Martin Schwidefskyf0e47c22007-07-17 04:03:03 -07001010static inline pte_t ptep_clear_flush(struct vm_area_struct *vma,
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001011 unsigned long addr, pte_t *ptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001013 return ptep_xchg_direct(vma->vm_mm, addr, ptep, __pte(_PAGE_INVALID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001016/*
1017 * The batched pte unmap code uses ptep_get_and_clear_full to clear the
1018 * ptes. Here an optimization is possible. tlb_gather_mmu flushes all
1019 * tlbs of an mm if it can guarantee that the ptes of the mm_struct
1020 * cannot be accessed while the batched unmap is running. In this case
1021 * full==1 and a simple pte_clear is enough. See tlb.h.
1022 */
1023#define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
1024static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001025 unsigned long addr,
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001026 pte_t *ptep, int full)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001028 if (full) {
1029 pte_t pte = *ptep;
1030 *ptep = __pte(_PAGE_INVALID);
1031 return pte;
Martin Schwidefskyd3383632013-04-17 10:53:39 +02001032 }
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001033 return ptep_xchg_lazy(mm, addr, ptep, __pte(_PAGE_INVALID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001036#define __HAVE_ARCH_PTEP_SET_WRPROTECT
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001037static inline void ptep_set_wrprotect(struct mm_struct *mm,
1038 unsigned long addr, pte_t *ptep)
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001039{
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001040 pte_t pte = *ptep;
1041
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001042 if (pte_write(pte))
1043 ptep_xchg_lazy(mm, addr, ptep, pte_wrprotect(pte));
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001044}
Martin Schwidefskyba8a9222007-10-22 12:52:44 +02001045
1046#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001047static inline int ptep_set_access_flags(struct vm_area_struct *vma,
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001048 unsigned long addr, pte_t *ptep,
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001049 pte_t entry, int dirty)
1050{
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001051 if (pte_same(*ptep, entry))
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001052 return 0;
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001053 ptep_xchg_direct(vma->vm_mm, addr, ptep, entry);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +02001054 return 1;
1055}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001057/*
1058 * Additional functions to handle KVM guest page tables
1059 */
1060void ptep_set_pte_at(struct mm_struct *mm, unsigned long addr,
1061 pte_t *ptep, pte_t entry);
1062void ptep_set_notify(struct mm_struct *mm, unsigned long addr, pte_t *ptep);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001063void ptep_notify(struct mm_struct *mm, unsigned long addr,
1064 pte_t *ptep, unsigned long bits);
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +01001065int ptep_force_prot(struct mm_struct *mm, unsigned long gaddr,
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001066 pte_t *ptep, int prot, unsigned long bit);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001067void ptep_zap_unused(struct mm_struct *mm, unsigned long addr,
1068 pte_t *ptep , int reset);
1069void ptep_zap_key(struct mm_struct *mm, unsigned long addr, pte_t *ptep);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001070int ptep_shadow_pte(struct mm_struct *mm, unsigned long saddr,
David Hildenbranda9d23e72016-03-08 12:21:41 +01001071 pte_t *sptep, pte_t *tptep, pte_t pte);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001072void ptep_unshadow_pte(struct mm_struct *mm, unsigned long saddr, pte_t *ptep);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001073
1074bool test_and_clear_guest_dirty(struct mm_struct *mm, unsigned long address);
1075int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
1076 unsigned char key, bool nq);
David Hildenbrand1824c722016-05-10 09:43:11 +02001077int cond_set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
1078 unsigned char key, unsigned char *oldkey,
1079 bool nq, bool mr, bool mc);
David Hildenbranda7e19ab2016-05-10 09:50:21 +02001080int reset_guest_reference_bit(struct mm_struct *mm, unsigned long addr);
David Hildenbrand154c8c12016-05-09 11:22:34 +02001081int get_guest_storage_key(struct mm_struct *mm, unsigned long addr,
1082 unsigned char *key);
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001083
Claudio Imbrenda2d42f942017-04-20 10:03:45 +02001084int set_pgste_bits(struct mm_struct *mm, unsigned long addr,
1085 unsigned long bits, unsigned long value);
1086int get_pgste(struct mm_struct *mm, unsigned long hva, unsigned long *pgstep);
1087int pgste_perform_essa(struct mm_struct *mm, unsigned long hva, int orc,
1088 unsigned long *oldpte, unsigned long *oldpgste);
1089
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001090/*
1091 * Certain architectures need to do special things when PTEs
1092 * within a page table are directly modified. Thus, the following
1093 * hook is made available.
1094 */
1095static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
1096 pte_t *ptep, pte_t entry)
1097{
Martin Schwidefsky57d7f932016-03-22 10:54:24 +01001098 if (!MACHINE_HAS_NX)
1099 pte_val(entry) &= ~_PAGE_NOEXEC;
Christian Borntraegera8f60d12017-04-09 22:09:38 +02001100 if (pte_present(entry))
1101 pte_val(entry) &= ~_PAGE_UNUSED;
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001102 if (mm_has_pgste(mm))
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001103 ptep_set_pte_at(mm, addr, ptep, entry);
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001104 else
1105 *ptep = entry;
1106}
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 * Conversion functions: convert a page and protection to a page entry,
1110 * and a page entry and page directory to the page they refer to.
1111 */
1112static inline pte_t mk_pte_phys(unsigned long physpage, pgprot_t pgprot)
1113{
1114 pte_t __pte;
1115 pte_val(__pte) = physpage + pgprot_val(pgprot);
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001116 return pte_mkyoung(__pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
Heiko Carstens2dcea572006-09-29 01:58:41 -07001119static inline pte_t mk_pte(struct page *page, pgprot_t pgprot)
1120{
Heiko Carstens0b2b6e1d2006-10-04 20:02:23 +02001121 unsigned long physpage = page_to_phys(page);
Martin Schwidefskyabf09be2012-11-07 13:17:37 +01001122 pte_t __pte = mk_pte_phys(physpage, pgprot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Martin Schwidefskye5098612013-07-23 20:57:57 +02001124 if (pte_write(__pte) && PageDirty(page))
1125 __pte = pte_mkdirty(__pte);
Martin Schwidefskyabf09be2012-11-07 13:17:37 +01001126 return __pte;
Heiko Carstens2dcea572006-09-29 01:58:41 -07001127}
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129#define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +02001130#define p4d_index(address) (((address) >> P4D_SHIFT) & (PTRS_PER_P4D-1))
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001131#define pud_index(address) (((address) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
1132#define pmd_index(address) (((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
1133#define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001135#define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136#define pgd_offset_k(address) pgd_offset(&init_mm, address)
1137
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001138#define pmd_deref(pmd) (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN)
1139#define pud_deref(pud) (pud_val(pud) & _REGION_ENTRY_ORIGIN)
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +02001140#define p4d_deref(pud) (p4d_val(pud) & _REGION_ENTRY_ORIGIN)
Martin Schwidefsky5a216a22008-02-09 18:24:36 +01001141#define pgd_deref(pgd) (pgd_val(pgd) & _REGION_ENTRY_ORIGIN)
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001142
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +02001143static inline p4d_t *p4d_offset(pgd_t *pgd, unsigned long address)
Martin Schwidefsky5a216a22008-02-09 18:24:36 +01001144{
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +02001145 p4d_t *p4d = (p4d_t *) pgd;
1146
1147 if ((pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R1)
1148 p4d = (p4d_t *) pgd_deref(*pgd);
1149 return p4d + p4d_index(address);
1150}
1151
1152static inline pud_t *pud_offset(p4d_t *p4d, unsigned long address)
1153{
1154 pud_t *pud = (pud_t *) p4d;
1155
1156 if ((p4d_val(*p4d) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
1157 pud = (pud_t *) p4d_deref(*p4d);
1158 return pud + pud_index(address);
Martin Schwidefsky5a216a22008-02-09 18:24:36 +01001159}
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001160
1161static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address)
1162{
Martin Schwidefsky6252d702008-02-09 18:24:37 +01001163 pmd_t *pmd = (pmd_t *) pud;
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +02001164
Martin Schwidefsky6252d702008-02-09 18:24:37 +01001165 if ((pud_val(*pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
1166 pmd = (pmd_t *) pud_deref(*pud);
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001167 return pmd + pmd_index(address);
1168}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001170#define pfn_pte(pfn,pgprot) mk_pte_phys(__pa((pfn) << PAGE_SHIFT),(pgprot))
1171#define pte_pfn(x) (pte_val(x) >> PAGE_SHIFT)
1172#define pte_page(x) pfn_to_page(pte_pfn(x))
1173
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001174#define pmd_page(pmd) pfn_to_page(pmd_pfn(pmd))
Gerald Schaeferd08de8e2016-07-04 14:47:01 +02001175#define pud_page(pud) pfn_to_page(pud_pfn(pud))
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +02001176#define p4d_page(pud) pfn_to_page(p4d_pfn(p4d))
Martin Schwidefsky190a1d72007-10-22 12:52:48 +02001177
1178/* Find an entry in the lowest level page table.. */
1179#define pte_offset(pmd, addr) ((pte_t *) pmd_deref(*(pmd)) + pte_index(addr))
1180#define pte_offset_kernel(pmd, address) pte_offset(pmd,address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181#define pte_offset_map(pmd, address) pte_offset_kernel(pmd, address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182#define pte_unmap(pte) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001184static inline pmd_t pmd_wrprotect(pmd_t pmd)
1185{
1186 pmd_val(pmd) &= ~_SEGMENT_ENTRY_WRITE;
1187 pmd_val(pmd) |= _SEGMENT_ENTRY_PROTECT;
1188 return pmd;
1189}
1190
1191static inline pmd_t pmd_mkwrite(pmd_t pmd)
1192{
1193 pmd_val(pmd) |= _SEGMENT_ENTRY_WRITE;
1194 if (pmd_large(pmd) && !(pmd_val(pmd) & _SEGMENT_ENTRY_DIRTY))
1195 return pmd;
1196 pmd_val(pmd) &= ~_SEGMENT_ENTRY_PROTECT;
1197 return pmd;
1198}
1199
1200static inline pmd_t pmd_mkclean(pmd_t pmd)
1201{
1202 if (pmd_large(pmd)) {
1203 pmd_val(pmd) &= ~_SEGMENT_ENTRY_DIRTY;
1204 pmd_val(pmd) |= _SEGMENT_ENTRY_PROTECT;
1205 }
1206 return pmd;
1207}
1208
1209static inline pmd_t pmd_mkdirty(pmd_t pmd)
1210{
1211 if (pmd_large(pmd)) {
Martin Schwidefsky5614dd92015-04-22 14:47:42 +02001212 pmd_val(pmd) |= _SEGMENT_ENTRY_DIRTY |
1213 _SEGMENT_ENTRY_SOFT_DIRTY;
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001214 if (pmd_val(pmd) & _SEGMENT_ENTRY_WRITE)
1215 pmd_val(pmd) &= ~_SEGMENT_ENTRY_PROTECT;
1216 }
1217 return pmd;
1218}
1219
Heiko Carstens9e20b4d2016-05-10 10:34:47 +02001220static inline pud_t pud_wrprotect(pud_t pud)
1221{
1222 pud_val(pud) &= ~_REGION3_ENTRY_WRITE;
1223 pud_val(pud) |= _REGION_ENTRY_PROTECT;
1224 return pud;
1225}
1226
1227static inline pud_t pud_mkwrite(pud_t pud)
1228{
1229 pud_val(pud) |= _REGION3_ENTRY_WRITE;
1230 if (pud_large(pud) && !(pud_val(pud) & _REGION3_ENTRY_DIRTY))
1231 return pud;
1232 pud_val(pud) &= ~_REGION_ENTRY_PROTECT;
1233 return pud;
1234}
1235
1236static inline pud_t pud_mkclean(pud_t pud)
1237{
1238 if (pud_large(pud)) {
1239 pud_val(pud) &= ~_REGION3_ENTRY_DIRTY;
1240 pud_val(pud) |= _REGION_ENTRY_PROTECT;
1241 }
1242 return pud;
1243}
1244
1245static inline pud_t pud_mkdirty(pud_t pud)
1246{
1247 if (pud_large(pud)) {
1248 pud_val(pud) |= _REGION3_ENTRY_DIRTY |
1249 _REGION3_ENTRY_SOFT_DIRTY;
1250 if (pud_val(pud) & _REGION3_ENTRY_WRITE)
1251 pud_val(pud) &= ~_REGION_ENTRY_PROTECT;
1252 }
1253 return pud;
1254}
1255
1256#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLB_PAGE)
1257static inline unsigned long massage_pgprot_pmd(pgprot_t pgprot)
1258{
1259 /*
Martin Schwidefsky57d7f932016-03-22 10:54:24 +01001260 * pgprot is PAGE_NONE, PAGE_RO, PAGE_RX, PAGE_RW or PAGE_RWX
1261 * (see __Pxxx / __Sxxx). Convert to segment table entry format.
Heiko Carstens9e20b4d2016-05-10 10:34:47 +02001262 */
1263 if (pgprot_val(pgprot) == pgprot_val(PAGE_NONE))
1264 return pgprot_val(SEGMENT_NONE);
Martin Schwidefsky57d7f932016-03-22 10:54:24 +01001265 if (pgprot_val(pgprot) == pgprot_val(PAGE_RO))
1266 return pgprot_val(SEGMENT_RO);
1267 if (pgprot_val(pgprot) == pgprot_val(PAGE_RX))
1268 return pgprot_val(SEGMENT_RX);
1269 if (pgprot_val(pgprot) == pgprot_val(PAGE_RW))
1270 return pgprot_val(SEGMENT_RW);
1271 return pgprot_val(SEGMENT_RWX);
Heiko Carstens9e20b4d2016-05-10 10:34:47 +02001272}
1273
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001274static inline pmd_t pmd_mkyoung(pmd_t pmd)
1275{
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001276 if (pmd_large(pmd)) {
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001277 pmd_val(pmd) |= _SEGMENT_ENTRY_YOUNG;
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001278 if (pmd_val(pmd) & _SEGMENT_ENTRY_READ)
1279 pmd_val(pmd) &= ~_SEGMENT_ENTRY_INVALID;
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001280 }
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001281 return pmd;
1282}
1283
1284static inline pmd_t pmd_mkold(pmd_t pmd)
1285{
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001286 if (pmd_large(pmd)) {
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001287 pmd_val(pmd) &= ~_SEGMENT_ENTRY_YOUNG;
1288 pmd_val(pmd) |= _SEGMENT_ENTRY_INVALID;
1289 }
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001290 return pmd;
1291}
1292
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001293static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
1294{
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001295 if (pmd_large(pmd)) {
1296 pmd_val(pmd) &= _SEGMENT_ENTRY_ORIGIN_LARGE |
1297 _SEGMENT_ENTRY_DIRTY | _SEGMENT_ENTRY_YOUNG |
Kirill A. Shutemovfecffad2016-01-15 16:53:24 -08001298 _SEGMENT_ENTRY_LARGE | _SEGMENT_ENTRY_SOFT_DIRTY;
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001299 pmd_val(pmd) |= massage_pgprot_pmd(newprot);
1300 if (!(pmd_val(pmd) & _SEGMENT_ENTRY_DIRTY))
1301 pmd_val(pmd) |= _SEGMENT_ENTRY_PROTECT;
1302 if (!(pmd_val(pmd) & _SEGMENT_ENTRY_YOUNG))
1303 pmd_val(pmd) |= _SEGMENT_ENTRY_INVALID;
1304 return pmd;
1305 }
1306 pmd_val(pmd) &= _SEGMENT_ENTRY_ORIGIN;
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001307 pmd_val(pmd) |= massage_pgprot_pmd(newprot);
1308 return pmd;
1309}
1310
Gerald Schaefer106c9922013-04-29 15:07:23 -07001311static inline pmd_t mk_pmd_phys(unsigned long physpage, pgprot_t pgprot)
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001312{
Gerald Schaefer106c9922013-04-29 15:07:23 -07001313 pmd_t __pmd;
1314 pmd_val(__pmd) = physpage + massage_pgprot_pmd(pgprot);
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001315 return __pmd;
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001316}
1317
Gerald Schaefer106c9922013-04-29 15:07:23 -07001318#endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLB_PAGE */
1319
Martin Schwidefsky1b948d62014-04-03 13:55:01 +02001320static inline void __pmdp_csp(pmd_t *pmdp)
1321{
Heiko Carstens4ccccc52016-05-14 10:46:33 +02001322 csp((unsigned int *)pmdp + 1, pmd_val(*pmdp),
1323 pmd_val(*pmdp) | _SEGMENT_ENTRY_INVALID);
Martin Schwidefsky1b948d62014-04-03 13:55:01 +02001324}
1325
Martin Schwidefsky47e4d852016-06-14 12:41:35 +02001326#define IDTE_GLOBAL 0
1327#define IDTE_LOCAL 1
1328
1329static inline void __pmdp_idte(unsigned long address, pmd_t *pmdp, int local)
Martin Schwidefsky1b948d62014-04-03 13:55:01 +02001330{
1331 unsigned long sto;
1332
1333 sto = (unsigned long) pmdp - pmd_index(address) * sizeof(pmd_t);
1334 asm volatile(
Martin Schwidefsky47e4d852016-06-14 12:41:35 +02001335 " .insn rrf,0xb98e0000,%[r1],%[r2],0,%[m4]"
1336 : "+m" (*pmdp)
1337 : [r1] "a" (sto), [r2] "a" ((address & HPAGE_MASK)),
1338 [m4] "i" (local)
Martin Schwidefsky1b948d62014-04-03 13:55:01 +02001339 : "cc" );
1340}
1341
Martin Schwidefsky47e4d852016-06-14 12:41:35 +02001342static inline void __pudp_idte(unsigned long address, pud_t *pudp, int local)
Gerald Schaeferd08de8e2016-07-04 14:47:01 +02001343{
1344 unsigned long r3o;
1345
1346 r3o = (unsigned long) pudp - pud_index(address) * sizeof(pud_t);
1347 r3o |= _ASCE_TYPE_REGION3;
1348 asm volatile(
Martin Schwidefsky47e4d852016-06-14 12:41:35 +02001349 " .insn rrf,0xb98e0000,%[r1],%[r2],0,%[m4]"
1350 : "+m" (*pudp)
1351 : [r1] "a" (r3o), [r2] "a" ((address & PUD_MASK)),
1352 [m4] "i" (local)
Gerald Schaeferd08de8e2016-07-04 14:47:01 +02001353 : "cc");
1354}
1355
Martin Schwidefsky227be792016-03-08 11:09:25 +01001356pmd_t pmdp_xchg_direct(struct mm_struct *, unsigned long, pmd_t *, pmd_t);
1357pmd_t pmdp_xchg_lazy(struct mm_struct *, unsigned long, pmd_t *, pmd_t);
Gerald Schaeferd08de8e2016-07-04 14:47:01 +02001358pud_t pudp_xchg_direct(struct mm_struct *, unsigned long, pud_t *, pud_t);
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001359
Gerald Schaefer106c9922013-04-29 15:07:23 -07001360#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1361
1362#define __HAVE_ARCH_PGTABLE_DEPOSIT
Martin Schwidefsky227be792016-03-08 11:09:25 +01001363void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
1364 pgtable_t pgtable);
Gerald Schaefer106c9922013-04-29 15:07:23 -07001365
1366#define __HAVE_ARCH_PGTABLE_WITHDRAW
Martin Schwidefsky227be792016-03-08 11:09:25 +01001367pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp);
1368
1369#define __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
1370static inline int pmdp_set_access_flags(struct vm_area_struct *vma,
1371 unsigned long addr, pmd_t *pmdp,
1372 pmd_t entry, int dirty)
1373{
1374 VM_BUG_ON(addr & ~HPAGE_MASK);
1375
1376 entry = pmd_mkyoung(entry);
1377 if (dirty)
1378 entry = pmd_mkdirty(entry);
1379 if (pmd_val(*pmdp) == pmd_val(entry))
1380 return 0;
1381 pmdp_xchg_direct(vma->vm_mm, addr, pmdp, entry);
1382 return 1;
1383}
1384
1385#define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
1386static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
1387 unsigned long addr, pmd_t *pmdp)
1388{
1389 pmd_t pmd = *pmdp;
1390
1391 pmd = pmdp_xchg_direct(vma->vm_mm, addr, pmdp, pmd_mkold(pmd));
1392 return pmd_young(pmd);
1393}
1394
1395#define __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
1396static inline int pmdp_clear_flush_young(struct vm_area_struct *vma,
1397 unsigned long addr, pmd_t *pmdp)
1398{
1399 VM_BUG_ON(addr & ~HPAGE_MASK);
1400 return pmdp_test_and_clear_young(vma, addr, pmdp);
1401}
Gerald Schaefer106c9922013-04-29 15:07:23 -07001402
Gerald Schaefer106c9922013-04-29 15:07:23 -07001403static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
1404 pmd_t *pmdp, pmd_t entry)
1405{
Martin Schwidefsky57d7f932016-03-22 10:54:24 +01001406 if (!MACHINE_HAS_NX)
1407 pmd_val(entry) &= ~_SEGMENT_ENTRY_NOEXEC;
Gerald Schaefer106c9922013-04-29 15:07:23 -07001408 *pmdp = entry;
1409}
1410
1411static inline pmd_t pmd_mkhuge(pmd_t pmd)
1412{
1413 pmd_val(pmd) |= _SEGMENT_ENTRY_LARGE;
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001414 pmd_val(pmd) |= _SEGMENT_ENTRY_YOUNG;
1415 pmd_val(pmd) |= _SEGMENT_ENTRY_PROTECT;
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001416 return pmd;
1417}
1418
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001419#define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR
1420static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
Martin Schwidefsky227be792016-03-08 11:09:25 +01001421 unsigned long addr, pmd_t *pmdp)
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001422{
Dominik Dingel54397bb2016-04-27 11:43:07 +02001423 return pmdp_xchg_direct(mm, addr, pmdp, __pmd(_SEGMENT_ENTRY_EMPTY));
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001424}
1425
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001426#define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR_FULL
1427static inline pmd_t pmdp_huge_get_and_clear_full(struct mm_struct *mm,
Martin Schwidefsky227be792016-03-08 11:09:25 +01001428 unsigned long addr,
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001429 pmd_t *pmdp, int full)
Martin Schwidefskyfcbe08d62014-10-24 10:52:29 +02001430{
Martin Schwidefsky227be792016-03-08 11:09:25 +01001431 if (full) {
1432 pmd_t pmd = *pmdp;
Dominik Dingel54397bb2016-04-27 11:43:07 +02001433 *pmdp = __pmd(_SEGMENT_ENTRY_EMPTY);
Martin Schwidefsky227be792016-03-08 11:09:25 +01001434 return pmd;
1435 }
Dominik Dingel54397bb2016-04-27 11:43:07 +02001436 return pmdp_xchg_lazy(mm, addr, pmdp, __pmd(_SEGMENT_ENTRY_EMPTY));
Martin Schwidefskyfcbe08d62014-10-24 10:52:29 +02001437}
1438
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001439#define __HAVE_ARCH_PMDP_HUGE_CLEAR_FLUSH
1440static inline pmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma,
Martin Schwidefsky227be792016-03-08 11:09:25 +01001441 unsigned long addr, pmd_t *pmdp)
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001442{
Martin Schwidefsky227be792016-03-08 11:09:25 +01001443 return pmdp_huge_get_and_clear(vma->vm_mm, addr, pmdp);
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001444}
1445
1446#define __HAVE_ARCH_PMDP_INVALIDATE
1447static inline void pmdp_invalidate(struct vm_area_struct *vma,
Martin Schwidefsky227be792016-03-08 11:09:25 +01001448 unsigned long addr, pmd_t *pmdp)
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001449{
Dominik Dingel54397bb2016-04-27 11:43:07 +02001450 pmdp_xchg_direct(vma->vm_mm, addr, pmdp, __pmd(_SEGMENT_ENTRY_EMPTY));
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001451}
1452
Gerald Schaeferbe328652013-01-21 16:48:07 +01001453#define __HAVE_ARCH_PMDP_SET_WRPROTECT
1454static inline void pmdp_set_wrprotect(struct mm_struct *mm,
Martin Schwidefsky227be792016-03-08 11:09:25 +01001455 unsigned long addr, pmd_t *pmdp)
Gerald Schaeferbe328652013-01-21 16:48:07 +01001456{
1457 pmd_t pmd = *pmdp;
1458
Martin Schwidefsky227be792016-03-08 11:09:25 +01001459 if (pmd_write(pmd))
1460 pmd = pmdp_xchg_lazy(mm, addr, pmdp, pmd_wrprotect(pmd));
Gerald Schaeferbe328652013-01-21 16:48:07 +01001461}
1462
Aneesh Kumar K.Vf28b6ff2015-06-24 16:57:42 -07001463static inline pmd_t pmdp_collapse_flush(struct vm_area_struct *vma,
1464 unsigned long address,
1465 pmd_t *pmdp)
1466{
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001467 return pmdp_huge_get_and_clear(vma->vm_mm, address, pmdp);
Aneesh Kumar K.Vf28b6ff2015-06-24 16:57:42 -07001468}
1469#define pmdp_collapse_flush pmdp_collapse_flush
1470
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001471#define pfn_pmd(pfn, pgprot) mk_pmd_phys(__pa((pfn) << PAGE_SHIFT), (pgprot))
1472#define mk_pmd(page, pgprot) pfn_pmd(page_to_pfn(page), (pgprot))
1473
1474static inline int pmd_trans_huge(pmd_t pmd)
1475{
1476 return pmd_val(pmd) & _SEGMENT_ENTRY_LARGE;
1477}
1478
Hugh Dickinsfd8cfd32016-05-19 17:13:00 -07001479#define has_transparent_hugepage has_transparent_hugepage
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001480static inline int has_transparent_hugepage(void)
1481{
Heiko Carstens466178f2017-02-13 15:11:15 +01001482 return MACHINE_HAS_EDAT1 ? 1 : 0;
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001483}
Gerald Schaefer75077af2012-10-08 16:30:15 -07001484#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 * 64 bit swap entry format:
1488 * A page-table entry has some bits we have to treat in a special way.
Geert Uytterhoeven4e0a6412015-05-21 14:00:47 +02001489 * Bits 52 and bit 55 have to be zero, otherwise a specification
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 * exception will occur instead of a page translation exception. The
Geert Uytterhoeven4e0a6412015-05-21 14:00:47 +02001491 * specification exception has the bad habit not to store necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 * information in the lowcore.
Martin Schwidefskya1c843b2015-04-22 13:55:59 +02001493 * Bits 54 and 63 are used to indicate the page type.
1494 * A swap pte is indicated by bit pattern (pte & 0x201) == 0x200
1495 * This leaves the bits 0-51 and bits 56-62 to store type and offset.
1496 * We use the 5 bits from 57-61 for the type and the 52 bits from 0-51
1497 * for the offset.
1498 * | offset |01100|type |00|
1499 * |0000000000111111111122222222223333333333444444444455|55555|55566|66|
1500 * |0123456789012345678901234567890123456789012345678901|23456|78901|23|
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 */
Heiko Carstens5a79859a2015-02-12 13:08:27 +01001502
Martin Schwidefskya1c843b2015-04-22 13:55:59 +02001503#define __SWP_OFFSET_MASK ((1UL << 52) - 1)
1504#define __SWP_OFFSET_SHIFT 12
1505#define __SWP_TYPE_MASK ((1UL << 5) - 1)
1506#define __SWP_TYPE_SHIFT 2
Heiko Carstens5a79859a2015-02-12 13:08:27 +01001507
Adrian Bunk4448aaf2005-11-08 21:34:42 -08001508static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 pte_t pte;
Martin Schwidefskya1c843b2015-04-22 13:55:59 +02001511
1512 pte_val(pte) = _PAGE_INVALID | _PAGE_PROTECT;
1513 pte_val(pte) |= (offset & __SWP_OFFSET_MASK) << __SWP_OFFSET_SHIFT;
1514 pte_val(pte) |= (type & __SWP_TYPE_MASK) << __SWP_TYPE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return pte;
1516}
1517
Martin Schwidefskya1c843b2015-04-22 13:55:59 +02001518static inline unsigned long __swp_type(swp_entry_t entry)
1519{
1520 return (entry.val >> __SWP_TYPE_SHIFT) & __SWP_TYPE_MASK;
1521}
1522
1523static inline unsigned long __swp_offset(swp_entry_t entry)
1524{
1525 return (entry.val >> __SWP_OFFSET_SHIFT) & __SWP_OFFSET_MASK;
1526}
1527
1528static inline swp_entry_t __swp_entry(unsigned long type, unsigned long offset)
1529{
1530 return (swp_entry_t) { pte_val(mk_swap_pte(type, offset)) };
1531}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533#define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
1534#define __swp_entry_to_pte(x) ((pte_t) { (x).val })
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536#endif /* !__ASSEMBLY__ */
1537
1538#define kern_addr_valid(addr) (1)
1539
Heiko Carstens17f34582008-04-30 13:38:47 +02001540extern int vmem_add_mapping(unsigned long start, unsigned long size);
1541extern int vmem_remove_mapping(unsigned long start, unsigned long size);
Carsten Otte402b0862008-03-25 18:47:10 +01001542extern int s390_enable_sie(void);
Dominik Dingel3ac8e382014-10-23 12:09:17 +02001543extern int s390_enable_skey(void);
Dominik Dingela13cff32014-10-23 12:07:14 +02001544extern void s390_reset_cmma(struct mm_struct *mm);
Heiko Carstensf4eb07c2006-12-08 15:56:07 +01001545
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +01001546/* s390 has a private copy of get unmapped area to deal with cache synonyms */
1547#define HAVE_ARCH_UNMAPPED_AREA
1548#define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550/*
1551 * No page table caches to initialise
1552 */
Heiko Carstens765a0ca2013-03-23 10:29:01 +01001553static inline void pgtable_cache_init(void) { }
1554static inline void check_pgt_cache(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556#include <asm-generic/pgtable.h>
1557
1558#endif /* _S390_PAGE_H */