blob: 18205f851c247aaf2e20e79a5cc564fe122c38b7 [file] [log] [blame]
Greg Kroah-Hartmanac41aae2017-11-24 15:00:35 +01001// SPDX-License-Identifier: GPL-2.0
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02002/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02003 * Copyright IBM Corp. 2007, 2011
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02004 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
5 */
6
7#include <linux/sched.h>
8#include <linux/kernel.h>
9#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/gfp.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020011#include <linux/mm.h>
12#include <linux/swap.h>
13#include <linux/smp.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020014#include <linux/spinlock.h>
Martin Schwidefsky80217142010-10-25 16:10:11 +020015#include <linux/rcupdate.h>
Martin Schwidefskye5992f22011-07-24 10:48:20 +020016#include <linux/slab.h>
Konstantin Weitzb31288f2013-04-17 17:36:29 +020017#include <linux/swapops.h>
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +020018#include <linux/sysctl.h>
Dominik Dingel3ac8e382014-10-23 12:09:17 +020019#include <linux/ksm.h>
20#include <linux/mman.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020021
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020022#include <asm/tlb.h>
23#include <asm/tlbflush.h>
Martin Schwidefsky6252d702008-02-09 18:24:37 +010024#include <asm/mmu_context.h>
Claudio Imbrenda2d42f942017-04-20 10:03:45 +020025#include <asm/page-states.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020026
Niklas Schnelleb02002c2020-07-13 14:12:49 +020027pgprot_t pgprot_writecombine(pgprot_t prot)
28{
29 /*
30 * mio_wb_bit_mask may be set on a different CPU, but it is only set
31 * once at init and only read afterwards.
32 */
33 return __pgprot(pgprot_val(prot) | mio_wb_bit_mask);
34}
35EXPORT_SYMBOL_GPL(pgprot_writecombine);
36
37pgprot_t pgprot_writethrough(pgprot_t prot)
38{
39 /*
40 * mio_wb_bit_mask may be set on a different CPU, but it is only set
41 * once at init and only read afterwards.
42 */
43 return __pgprot(pgprot_val(prot) & ~mio_wb_bit_mask);
44}
45EXPORT_SYMBOL_GPL(pgprot_writethrough);
46
Martin Schwidefsky118bd312016-07-26 16:53:09 +020047static inline void ptep_ipte_local(struct mm_struct *mm, unsigned long addr,
Martin Schwidefskycd774b92016-07-26 17:02:31 +020048 pte_t *ptep, int nodat)
Martin Schwidefsky118bd312016-07-26 16:53:09 +020049{
50 unsigned long opt, asce;
51
52 if (MACHINE_HAS_TLB_GUEST) {
53 opt = 0;
54 asce = READ_ONCE(mm->context.gmap_asce);
Martin Schwidefskycd774b92016-07-26 17:02:31 +020055 if (asce == 0UL || nodat)
Martin Schwidefsky118bd312016-07-26 16:53:09 +020056 opt |= IPTE_NODAT;
Martin Schwidefsky28c807e2016-07-26 16:00:22 +020057 if (asce != -1UL) {
58 asce = asce ? : mm->context.asce;
59 opt |= IPTE_GUEST_ASCE;
60 }
61 __ptep_ipte(addr, ptep, opt, asce, IPTE_LOCAL);
Martin Schwidefsky118bd312016-07-26 16:53:09 +020062 } else {
Martin Schwidefsky28c807e2016-07-26 16:00:22 +020063 __ptep_ipte(addr, ptep, 0, 0, IPTE_LOCAL);
Martin Schwidefsky118bd312016-07-26 16:53:09 +020064 }
65}
66
67static inline void ptep_ipte_global(struct mm_struct *mm, unsigned long addr,
Martin Schwidefskycd774b92016-07-26 17:02:31 +020068 pte_t *ptep, int nodat)
Martin Schwidefsky118bd312016-07-26 16:53:09 +020069{
70 unsigned long opt, asce;
71
72 if (MACHINE_HAS_TLB_GUEST) {
73 opt = 0;
74 asce = READ_ONCE(mm->context.gmap_asce);
Martin Schwidefskycd774b92016-07-26 17:02:31 +020075 if (asce == 0UL || nodat)
Martin Schwidefsky118bd312016-07-26 16:53:09 +020076 opt |= IPTE_NODAT;
Martin Schwidefsky28c807e2016-07-26 16:00:22 +020077 if (asce != -1UL) {
78 asce = asce ? : mm->context.asce;
79 opt |= IPTE_GUEST_ASCE;
80 }
81 __ptep_ipte(addr, ptep, opt, asce, IPTE_GLOBAL);
Martin Schwidefsky118bd312016-07-26 16:53:09 +020082 } else {
Martin Schwidefsky28c807e2016-07-26 16:00:22 +020083 __ptep_ipte(addr, ptep, 0, 0, IPTE_GLOBAL);
Martin Schwidefsky118bd312016-07-26 16:53:09 +020084 }
85}
86
Martin Schwidefskyebde7652016-03-08 11:08:09 +010087static inline pte_t ptep_flush_direct(struct mm_struct *mm,
Martin Schwidefskycd774b92016-07-26 17:02:31 +020088 unsigned long addr, pte_t *ptep,
89 int nodat)
Martin Schwidefskyebde7652016-03-08 11:08:09 +010090{
Martin Schwidefskyebde7652016-03-08 11:08:09 +010091 pte_t old;
92
93 old = *ptep;
94 if (unlikely(pte_val(old) & _PAGE_INVALID))
95 return old;
Martin Schwidefsky64f31d52016-05-25 09:45:26 +020096 atomic_inc(&mm->context.flush_count);
97 if (MACHINE_HAS_TLB_LC &&
Martin Schwidefskyebde7652016-03-08 11:08:09 +010098 cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
Martin Schwidefskycd774b92016-07-26 17:02:31 +020099 ptep_ipte_local(mm, addr, ptep, nodat);
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100100 else
Martin Schwidefskycd774b92016-07-26 17:02:31 +0200101 ptep_ipte_global(mm, addr, ptep, nodat);
Martin Schwidefsky64f31d52016-05-25 09:45:26 +0200102 atomic_dec(&mm->context.flush_count);
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100103 return old;
104}
105
106static inline pte_t ptep_flush_lazy(struct mm_struct *mm,
Martin Schwidefskycd774b92016-07-26 17:02:31 +0200107 unsigned long addr, pte_t *ptep,
108 int nodat)
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100109{
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100110 pte_t old;
111
112 old = *ptep;
113 if (unlikely(pte_val(old) & _PAGE_INVALID))
114 return old;
Martin Schwidefsky64f31d52016-05-25 09:45:26 +0200115 atomic_inc(&mm->context.flush_count);
116 if (cpumask_equal(&mm->context.cpu_attach_mask,
117 cpumask_of(smp_processor_id()))) {
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100118 pte_val(*ptep) |= _PAGE_INVALID;
119 mm->context.flush_mm = 1;
120 } else
Martin Schwidefskycd774b92016-07-26 17:02:31 +0200121 ptep_ipte_global(mm, addr, ptep, nodat);
Martin Schwidefsky64f31d52016-05-25 09:45:26 +0200122 atomic_dec(&mm->context.flush_count);
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100123 return old;
124}
125
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100126static inline pgste_t pgste_get_lock(pte_t *ptep)
127{
128 unsigned long new = 0;
129#ifdef CONFIG_PGSTE
130 unsigned long old;
131
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100132 asm(
133 " lg %0,%2\n"
134 "0: lgr %1,%0\n"
135 " nihh %0,0xff7f\n" /* clear PCL bit in old */
136 " oihh %1,0x0080\n" /* set PCL bit in new */
137 " csg %0,%1,%2\n"
138 " jl 0b\n"
139 : "=&d" (old), "=&d" (new), "=Q" (ptep[PTRS_PER_PTE])
140 : "Q" (ptep[PTRS_PER_PTE]) : "cc", "memory");
141#endif
142 return __pgste(new);
143}
144
145static inline void pgste_set_unlock(pte_t *ptep, pgste_t pgste)
146{
147#ifdef CONFIG_PGSTE
148 asm(
149 " nihh %1,0xff7f\n" /* clear PCL bit */
150 " stg %1,%0\n"
151 : "=Q" (ptep[PTRS_PER_PTE])
152 : "d" (pgste_val(pgste)), "Q" (ptep[PTRS_PER_PTE])
153 : "cc", "memory");
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100154#endif
155}
156
157static inline pgste_t pgste_get(pte_t *ptep)
158{
159 unsigned long pgste = 0;
160#ifdef CONFIG_PGSTE
161 pgste = *(unsigned long *)(ptep + PTRS_PER_PTE);
162#endif
163 return __pgste(pgste);
164}
165
166static inline void pgste_set(pte_t *ptep, pgste_t pgste)
167{
168#ifdef CONFIG_PGSTE
169 *(pgste_t *)(ptep + PTRS_PER_PTE) = pgste;
170#endif
171}
172
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100173static inline pgste_t pgste_update_all(pte_t pte, pgste_t pgste,
174 struct mm_struct *mm)
175{
176#ifdef CONFIG_PGSTE
177 unsigned long address, bits, skey;
178
Janosch Frank55531b72018-02-15 16:33:47 +0100179 if (!mm_uses_skeys(mm) || pte_val(pte) & _PAGE_INVALID)
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100180 return pgste;
181 address = pte_val(pte) & PAGE_MASK;
182 skey = (unsigned long) page_get_storage_key(address);
183 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
184 /* Transfer page changed & referenced bit to guest bits in pgste */
185 pgste_val(pgste) |= bits << 48; /* GR bit & GC bit */
186 /* Copy page access key and fetch protection bit to pgste */
187 pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT);
188 pgste_val(pgste) |= (skey & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
189#endif
190 return pgste;
191
192}
193
194static inline void pgste_set_key(pte_t *ptep, pgste_t pgste, pte_t entry,
195 struct mm_struct *mm)
196{
197#ifdef CONFIG_PGSTE
198 unsigned long address;
199 unsigned long nkey;
200
Janosch Frank55531b72018-02-15 16:33:47 +0100201 if (!mm_uses_skeys(mm) || pte_val(entry) & _PAGE_INVALID)
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100202 return;
203 VM_BUG_ON(!(pte_val(*ptep) & _PAGE_INVALID));
204 address = pte_val(entry) & PAGE_MASK;
205 /*
206 * Set page access key and fetch protection bit from pgste.
207 * The guest C/R information is still in the PGSTE, set real
208 * key C/R to 0.
209 */
210 nkey = (pgste_val(pgste) & (PGSTE_ACC_BITS | PGSTE_FP_BIT)) >> 56;
211 nkey |= (pgste_val(pgste) & (PGSTE_GR_BIT | PGSTE_GC_BIT)) >> 48;
212 page_set_storage_key(address, nkey, 0);
213#endif
214}
215
216static inline pgste_t pgste_set_pte(pte_t *ptep, pgste_t pgste, pte_t entry)
217{
218#ifdef CONFIG_PGSTE
219 if ((pte_val(entry) & _PAGE_PRESENT) &&
220 (pte_val(entry) & _PAGE_WRITE) &&
221 !(pte_val(entry) & _PAGE_INVALID)) {
222 if (!MACHINE_HAS_ESOP) {
223 /*
224 * Without enhanced suppression-on-protection force
225 * the dirty bit on for all writable ptes.
226 */
227 pte_val(entry) |= _PAGE_DIRTY;
228 pte_val(entry) &= ~_PAGE_PROTECT;
229 }
230 if (!(pte_val(entry) & _PAGE_PROTECT))
231 /* This pte allows write access, set user-dirty */
232 pgste_val(pgste) |= PGSTE_UC_BIT;
233 }
234#endif
235 *ptep = entry;
236 return pgste;
237}
238
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100239static inline pgste_t pgste_pte_notify(struct mm_struct *mm,
240 unsigned long addr,
241 pte_t *ptep, pgste_t pgste)
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100242{
243#ifdef CONFIG_PGSTE
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100244 unsigned long bits;
245
246 bits = pgste_val(pgste) & (PGSTE_IN_BIT | PGSTE_VSIE_BIT);
247 if (bits) {
248 pgste_val(pgste) ^= bits;
249 ptep_notify(mm, addr, ptep, bits);
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100250 }
251#endif
252 return pgste;
253}
254
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100255static inline pgste_t ptep_xchg_start(struct mm_struct *mm,
256 unsigned long addr, pte_t *ptep)
257{
258 pgste_t pgste = __pgste(0);
259
260 if (mm_has_pgste(mm)) {
261 pgste = pgste_get_lock(ptep);
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100262 pgste = pgste_pte_notify(mm, addr, ptep, pgste);
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100263 }
264 return pgste;
265}
266
Christian Borntraeger0d6da872017-01-23 22:59:44 +0100267static inline pte_t ptep_xchg_commit(struct mm_struct *mm,
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100268 unsigned long addr, pte_t *ptep,
269 pgste_t pgste, pte_t old, pte_t new)
270{
271 if (mm_has_pgste(mm)) {
272 if (pte_val(old) & _PAGE_INVALID)
273 pgste_set_key(ptep, pgste, new, mm);
274 if (pte_val(new) & _PAGE_INVALID) {
275 pgste = pgste_update_all(old, pgste, mm);
276 if ((pgste_val(pgste) & _PGSTE_GPS_USAGE_MASK) ==
277 _PGSTE_GPS_USAGE_UNUSED)
278 pte_val(old) |= _PAGE_UNUSED;
279 }
280 pgste = pgste_set_pte(ptep, pgste, new);
281 pgste_set_unlock(ptep, pgste);
282 } else {
283 *ptep = new;
284 }
Christian Borntraeger0d6da872017-01-23 22:59:44 +0100285 return old;
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100286}
287
288pte_t ptep_xchg_direct(struct mm_struct *mm, unsigned long addr,
289 pte_t *ptep, pte_t new)
290{
291 pgste_t pgste;
292 pte_t old;
Martin Schwidefskycd774b92016-07-26 17:02:31 +0200293 int nodat;
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100294
Martin Schwidefskya9809402016-06-06 10:30:45 +0200295 preempt_disable();
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100296 pgste = ptep_xchg_start(mm, addr, ptep);
Martin Schwidefskycd774b92016-07-26 17:02:31 +0200297 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
298 old = ptep_flush_direct(mm, addr, ptep, nodat);
Christian Borntraeger0d6da872017-01-23 22:59:44 +0100299 old = ptep_xchg_commit(mm, addr, ptep, pgste, old, new);
Martin Schwidefskya9809402016-06-06 10:30:45 +0200300 preempt_enable();
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100301 return old;
302}
303EXPORT_SYMBOL(ptep_xchg_direct);
304
305pte_t ptep_xchg_lazy(struct mm_struct *mm, unsigned long addr,
306 pte_t *ptep, pte_t new)
307{
308 pgste_t pgste;
309 pte_t old;
Martin Schwidefskycd774b92016-07-26 17:02:31 +0200310 int nodat;
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100311
Martin Schwidefskya9809402016-06-06 10:30:45 +0200312 preempt_disable();
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100313 pgste = ptep_xchg_start(mm, addr, ptep);
Martin Schwidefskycd774b92016-07-26 17:02:31 +0200314 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
315 old = ptep_flush_lazy(mm, addr, ptep, nodat);
Christian Borntraeger0d6da872017-01-23 22:59:44 +0100316 old = ptep_xchg_commit(mm, addr, ptep, pgste, old, new);
Martin Schwidefskya9809402016-06-06 10:30:45 +0200317 preempt_enable();
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100318 return old;
319}
320EXPORT_SYMBOL(ptep_xchg_lazy);
321
Aneesh Kumar K.V0cbe3e22019-03-05 15:46:26 -0800322pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr,
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100323 pte_t *ptep)
324{
325 pgste_t pgste;
326 pte_t old;
Martin Schwidefskycd774b92016-07-26 17:02:31 +0200327 int nodat;
Aneesh Kumar K.V0cbe3e22019-03-05 15:46:26 -0800328 struct mm_struct *mm = vma->vm_mm;
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100329
Martin Schwidefskya9809402016-06-06 10:30:45 +0200330 preempt_disable();
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100331 pgste = ptep_xchg_start(mm, addr, ptep);
Martin Schwidefskycd774b92016-07-26 17:02:31 +0200332 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
333 old = ptep_flush_lazy(mm, addr, ptep, nodat);
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100334 if (mm_has_pgste(mm)) {
335 pgste = pgste_update_all(old, pgste, mm);
336 pgste_set(ptep, pgste);
337 }
338 return old;
339}
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100340
Aneesh Kumar K.V0cbe3e22019-03-05 15:46:26 -0800341void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr,
Aneesh Kumar K.V04a86452019-03-05 15:46:29 -0800342 pte_t *ptep, pte_t old_pte, pte_t pte)
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100343{
344 pgste_t pgste;
Aneesh Kumar K.V0cbe3e22019-03-05 15:46:26 -0800345 struct mm_struct *mm = vma->vm_mm;
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100346
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100347 if (!MACHINE_HAS_NX)
348 pte_val(pte) &= ~_PAGE_NOEXEC;
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100349 if (mm_has_pgste(mm)) {
350 pgste = pgste_get(ptep);
351 pgste_set_key(ptep, pgste, pte, mm);
352 pgste = pgste_set_pte(ptep, pgste, pte);
353 pgste_set_unlock(ptep, pgste);
354 } else {
355 *ptep = pte;
356 }
Martin Schwidefskya9809402016-06-06 10:30:45 +0200357 preempt_enable();
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100358}
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100359
Martin Schwidefsky118bd312016-07-26 16:53:09 +0200360static inline void pmdp_idte_local(struct mm_struct *mm,
361 unsigned long addr, pmd_t *pmdp)
362{
363 if (MACHINE_HAS_TLB_GUEST)
Martin Schwidefsky28c807e2016-07-26 16:00:22 +0200364 __pmdp_idte(addr, pmdp, IDTE_NODAT | IDTE_GUEST_ASCE,
365 mm->context.asce, IDTE_LOCAL);
Martin Schwidefsky118bd312016-07-26 16:53:09 +0200366 else
Martin Schwidefsky28c807e2016-07-26 16:00:22 +0200367 __pmdp_idte(addr, pmdp, 0, 0, IDTE_LOCAL);
Janosch Franka9e00d82018-07-13 11:28:37 +0100368 if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m)
Janosch Frank6a376272018-07-13 11:28:22 +0100369 gmap_pmdp_idte_local(mm, addr);
Martin Schwidefsky118bd312016-07-26 16:53:09 +0200370}
371
372static inline void pmdp_idte_global(struct mm_struct *mm,
373 unsigned long addr, pmd_t *pmdp)
374{
Janosch Frank6a376272018-07-13 11:28:22 +0100375 if (MACHINE_HAS_TLB_GUEST) {
Martin Schwidefsky28c807e2016-07-26 16:00:22 +0200376 __pmdp_idte(addr, pmdp, IDTE_NODAT | IDTE_GUEST_ASCE,
377 mm->context.asce, IDTE_GLOBAL);
Janosch Franka9e00d82018-07-13 11:28:37 +0100378 if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m)
Janosch Frank6a376272018-07-13 11:28:22 +0100379 gmap_pmdp_idte_global(mm, addr);
380 } else if (MACHINE_HAS_IDTE) {
Martin Schwidefsky28c807e2016-07-26 16:00:22 +0200381 __pmdp_idte(addr, pmdp, 0, 0, IDTE_GLOBAL);
Janosch Franka9e00d82018-07-13 11:28:37 +0100382 if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m)
Janosch Frank6a376272018-07-13 11:28:22 +0100383 gmap_pmdp_idte_global(mm, addr);
384 } else {
Martin Schwidefsky118bd312016-07-26 16:53:09 +0200385 __pmdp_csp(pmdp);
Janosch Franka9e00d82018-07-13 11:28:37 +0100386 if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m)
Janosch Frank6a376272018-07-13 11:28:22 +0100387 gmap_pmdp_csp(mm, addr);
388 }
Martin Schwidefsky118bd312016-07-26 16:53:09 +0200389}
390
Martin Schwidefsky227be792016-03-08 11:09:25 +0100391static inline pmd_t pmdp_flush_direct(struct mm_struct *mm,
392 unsigned long addr, pmd_t *pmdp)
393{
Martin Schwidefsky227be792016-03-08 11:09:25 +0100394 pmd_t old;
395
396 old = *pmdp;
397 if (pmd_val(old) & _SEGMENT_ENTRY_INVALID)
398 return old;
Martin Schwidefsky64f31d52016-05-25 09:45:26 +0200399 atomic_inc(&mm->context.flush_count);
400 if (MACHINE_HAS_TLB_LC &&
Martin Schwidefsky227be792016-03-08 11:09:25 +0100401 cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
Martin Schwidefsky118bd312016-07-26 16:53:09 +0200402 pmdp_idte_local(mm, addr, pmdp);
Martin Schwidefsky227be792016-03-08 11:09:25 +0100403 else
Martin Schwidefsky118bd312016-07-26 16:53:09 +0200404 pmdp_idte_global(mm, addr, pmdp);
Martin Schwidefsky64f31d52016-05-25 09:45:26 +0200405 atomic_dec(&mm->context.flush_count);
Martin Schwidefsky227be792016-03-08 11:09:25 +0100406 return old;
407}
408
409static inline pmd_t pmdp_flush_lazy(struct mm_struct *mm,
410 unsigned long addr, pmd_t *pmdp)
411{
Martin Schwidefsky227be792016-03-08 11:09:25 +0100412 pmd_t old;
413
414 old = *pmdp;
415 if (pmd_val(old) & _SEGMENT_ENTRY_INVALID)
416 return old;
Martin Schwidefsky64f31d52016-05-25 09:45:26 +0200417 atomic_inc(&mm->context.flush_count);
418 if (cpumask_equal(&mm->context.cpu_attach_mask,
419 cpumask_of(smp_processor_id()))) {
Martin Schwidefsky227be792016-03-08 11:09:25 +0100420 pmd_val(*pmdp) |= _SEGMENT_ENTRY_INVALID;
421 mm->context.flush_mm = 1;
Janosch Frank6a376272018-07-13 11:28:22 +0100422 if (mm_has_pgste(mm))
423 gmap_pmdp_invalidate(mm, addr);
Martin Schwidefsky118bd312016-07-26 16:53:09 +0200424 } else {
425 pmdp_idte_global(mm, addr, pmdp);
426 }
Martin Schwidefsky64f31d52016-05-25 09:45:26 +0200427 atomic_dec(&mm->context.flush_count);
Martin Schwidefsky227be792016-03-08 11:09:25 +0100428 return old;
429}
430
Thomas Huth81a8f2b2019-04-07 14:55:09 +0200431#ifdef CONFIG_PGSTE
Janosch Frank637ff9e2018-07-13 11:28:28 +0100432static pmd_t *pmd_alloc_map(struct mm_struct *mm, unsigned long addr)
433{
434 pgd_t *pgd;
435 p4d_t *p4d;
436 pud_t *pud;
437 pmd_t *pmd;
438
439 pgd = pgd_offset(mm, addr);
440 p4d = p4d_alloc(mm, pgd, addr);
441 if (!p4d)
442 return NULL;
443 pud = pud_alloc(mm, p4d, addr);
444 if (!pud)
445 return NULL;
446 pmd = pmd_alloc(mm, pud, addr);
447 return pmd;
448}
Thomas Huth81a8f2b2019-04-07 14:55:09 +0200449#endif
Janosch Frank637ff9e2018-07-13 11:28:28 +0100450
Martin Schwidefsky227be792016-03-08 11:09:25 +0100451pmd_t pmdp_xchg_direct(struct mm_struct *mm, unsigned long addr,
452 pmd_t *pmdp, pmd_t new)
453{
454 pmd_t old;
455
Martin Schwidefskya9809402016-06-06 10:30:45 +0200456 preempt_disable();
Martin Schwidefsky227be792016-03-08 11:09:25 +0100457 old = pmdp_flush_direct(mm, addr, pmdp);
458 *pmdp = new;
Martin Schwidefskya9809402016-06-06 10:30:45 +0200459 preempt_enable();
Martin Schwidefsky227be792016-03-08 11:09:25 +0100460 return old;
461}
462EXPORT_SYMBOL(pmdp_xchg_direct);
463
464pmd_t pmdp_xchg_lazy(struct mm_struct *mm, unsigned long addr,
465 pmd_t *pmdp, pmd_t new)
466{
467 pmd_t old;
468
Martin Schwidefskya9809402016-06-06 10:30:45 +0200469 preempt_disable();
Martin Schwidefsky227be792016-03-08 11:09:25 +0100470 old = pmdp_flush_lazy(mm, addr, pmdp);
471 *pmdp = new;
Martin Schwidefskya9809402016-06-06 10:30:45 +0200472 preempt_enable();
Martin Schwidefsky227be792016-03-08 11:09:25 +0100473 return old;
474}
475EXPORT_SYMBOL(pmdp_xchg_lazy);
476
Martin Schwidefsky118bd312016-07-26 16:53:09 +0200477static inline void pudp_idte_local(struct mm_struct *mm,
478 unsigned long addr, pud_t *pudp)
479{
480 if (MACHINE_HAS_TLB_GUEST)
Martin Schwidefsky28c807e2016-07-26 16:00:22 +0200481 __pudp_idte(addr, pudp, IDTE_NODAT | IDTE_GUEST_ASCE,
482 mm->context.asce, IDTE_LOCAL);
Martin Schwidefsky118bd312016-07-26 16:53:09 +0200483 else
Martin Schwidefsky28c807e2016-07-26 16:00:22 +0200484 __pudp_idte(addr, pudp, 0, 0, IDTE_LOCAL);
Martin Schwidefsky118bd312016-07-26 16:53:09 +0200485}
486
487static inline void pudp_idte_global(struct mm_struct *mm,
488 unsigned long addr, pud_t *pudp)
489{
490 if (MACHINE_HAS_TLB_GUEST)
Martin Schwidefsky28c807e2016-07-26 16:00:22 +0200491 __pudp_idte(addr, pudp, IDTE_NODAT | IDTE_GUEST_ASCE,
492 mm->context.asce, IDTE_GLOBAL);
Martin Schwidefsky118bd312016-07-26 16:53:09 +0200493 else if (MACHINE_HAS_IDTE)
Martin Schwidefsky28c807e2016-07-26 16:00:22 +0200494 __pudp_idte(addr, pudp, 0, 0, IDTE_GLOBAL);
Martin Schwidefsky118bd312016-07-26 16:53:09 +0200495 else
496 /*
497 * Invalid bit position is the same for pmd and pud, so we can
498 * re-use _pmd_csp() here
499 */
500 __pmdp_csp((pmd_t *) pudp);
501}
502
Gerald Schaeferd08de8e2016-07-04 14:47:01 +0200503static inline pud_t pudp_flush_direct(struct mm_struct *mm,
504 unsigned long addr, pud_t *pudp)
505{
506 pud_t old;
507
508 old = *pudp;
509 if (pud_val(old) & _REGION_ENTRY_INVALID)
510 return old;
Gerald Schaeferd08de8e2016-07-04 14:47:01 +0200511 atomic_inc(&mm->context.flush_count);
512 if (MACHINE_HAS_TLB_LC &&
513 cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
Martin Schwidefsky118bd312016-07-26 16:53:09 +0200514 pudp_idte_local(mm, addr, pudp);
Gerald Schaeferd08de8e2016-07-04 14:47:01 +0200515 else
Martin Schwidefsky118bd312016-07-26 16:53:09 +0200516 pudp_idte_global(mm, addr, pudp);
Gerald Schaeferd08de8e2016-07-04 14:47:01 +0200517 atomic_dec(&mm->context.flush_count);
518 return old;
519}
520
521pud_t pudp_xchg_direct(struct mm_struct *mm, unsigned long addr,
522 pud_t *pudp, pud_t new)
523{
524 pud_t old;
525
526 preempt_disable();
527 old = pudp_flush_direct(mm, addr, pudp);
528 *pudp = new;
529 preempt_enable();
530 return old;
531}
532EXPORT_SYMBOL(pudp_xchg_direct);
533
Gerald Schaefer75077af2012-10-08 16:30:15 -0700534#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700535void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
536 pgtable_t pgtable)
Gerald Schaefer9501d092012-10-08 16:30:18 -0700537{
538 struct list_head *lh = (struct list_head *) pgtable;
539
Martin Schwidefskyec66ad62014-02-12 14:16:18 +0100540 assert_spin_locked(pmd_lockptr(mm, pmdp));
Gerald Schaefer9501d092012-10-08 16:30:18 -0700541
542 /* FIFO */
Kirill A. Shutemovc389a252013-11-14 14:30:59 -0800543 if (!pmd_huge_pte(mm, pmdp))
Gerald Schaefer9501d092012-10-08 16:30:18 -0700544 INIT_LIST_HEAD(lh);
545 else
Kirill A. Shutemovc389a252013-11-14 14:30:59 -0800546 list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
547 pmd_huge_pte(mm, pmdp) = pgtable;
Gerald Schaefer9501d092012-10-08 16:30:18 -0700548}
549
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700550pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
Gerald Schaefer9501d092012-10-08 16:30:18 -0700551{
552 struct list_head *lh;
553 pgtable_t pgtable;
554 pte_t *ptep;
555
Martin Schwidefskyec66ad62014-02-12 14:16:18 +0100556 assert_spin_locked(pmd_lockptr(mm, pmdp));
Gerald Schaefer9501d092012-10-08 16:30:18 -0700557
558 /* FIFO */
Kirill A. Shutemovc389a252013-11-14 14:30:59 -0800559 pgtable = pmd_huge_pte(mm, pmdp);
Gerald Schaefer9501d092012-10-08 16:30:18 -0700560 lh = (struct list_head *) pgtable;
561 if (list_empty(lh))
Kirill A. Shutemovc389a252013-11-14 14:30:59 -0800562 pmd_huge_pte(mm, pmdp) = NULL;
Gerald Schaefer9501d092012-10-08 16:30:18 -0700563 else {
Kirill A. Shutemovc389a252013-11-14 14:30:59 -0800564 pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
Gerald Schaefer9501d092012-10-08 16:30:18 -0700565 list_del(lh);
566 }
567 ptep = (pte_t *) pgtable;
Martin Schwidefskye5098612013-07-23 20:57:57 +0200568 pte_val(*ptep) = _PAGE_INVALID;
Gerald Schaefer9501d092012-10-08 16:30:18 -0700569 ptep++;
Martin Schwidefskye5098612013-07-23 20:57:57 +0200570 pte_val(*ptep) = _PAGE_INVALID;
Gerald Schaefer9501d092012-10-08 16:30:18 -0700571 return pgtable;
572}
Gerald Schaefer75077af2012-10-08 16:30:15 -0700573#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100574
575#ifdef CONFIG_PGSTE
576void ptep_set_pte_at(struct mm_struct *mm, unsigned long addr,
577 pte_t *ptep, pte_t entry)
578{
579 pgste_t pgste;
580
581 /* the mm_has_pgste() check is done in set_pte_at() */
Martin Schwidefskya9809402016-06-06 10:30:45 +0200582 preempt_disable();
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100583 pgste = pgste_get_lock(ptep);
584 pgste_val(pgste) &= ~_PGSTE_GPS_ZERO;
585 pgste_set_key(ptep, pgste, entry, mm);
586 pgste = pgste_set_pte(ptep, pgste, entry);
587 pgste_set_unlock(ptep, pgste);
Martin Schwidefskya9809402016-06-06 10:30:45 +0200588 preempt_enable();
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100589}
590
591void ptep_set_notify(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
592{
593 pgste_t pgste;
594
Martin Schwidefskya9809402016-06-06 10:30:45 +0200595 preempt_disable();
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100596 pgste = pgste_get_lock(ptep);
597 pgste_val(pgste) |= PGSTE_IN_BIT;
598 pgste_set_unlock(ptep, pgste);
Martin Schwidefskya9809402016-06-06 10:30:45 +0200599 preempt_enable();
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100600}
601
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100602/**
603 * ptep_force_prot - change access rights of a locked pte
604 * @mm: pointer to the process mm_struct
605 * @addr: virtual address in the guest address space
606 * @ptep: pointer to the page table entry
607 * @prot: indicates guest access rights: PROT_NONE, PROT_READ or PROT_WRITE
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100608 * @bit: pgste bit to set (e.g. for notification)
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100609 *
610 * Returns 0 if the access rights were changed and -EAGAIN if the current
611 * and requested access rights are incompatible.
612 */
613int ptep_force_prot(struct mm_struct *mm, unsigned long addr,
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100614 pte_t *ptep, int prot, unsigned long bit)
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100615{
616 pte_t entry;
617 pgste_t pgste;
Martin Schwidefskycd774b92016-07-26 17:02:31 +0200618 int pte_i, pte_p, nodat;
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100619
620 pgste = pgste_get_lock(ptep);
621 entry = *ptep;
622 /* Check pte entry after all locks have been acquired */
623 pte_i = pte_val(entry) & _PAGE_INVALID;
624 pte_p = pte_val(entry) & _PAGE_PROTECT;
625 if ((pte_i && (prot != PROT_NONE)) ||
626 (pte_p && (prot & PROT_WRITE))) {
627 pgste_set_unlock(ptep, pgste);
628 return -EAGAIN;
629 }
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100630 /* Change access rights and set pgste bit */
Martin Schwidefskycd774b92016-07-26 17:02:31 +0200631 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100632 if (prot == PROT_NONE && !pte_i) {
Martin Schwidefskycd774b92016-07-26 17:02:31 +0200633 ptep_flush_direct(mm, addr, ptep, nodat);
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100634 pgste = pgste_update_all(entry, pgste, mm);
635 pte_val(entry) |= _PAGE_INVALID;
636 }
637 if (prot == PROT_READ && !pte_p) {
Martin Schwidefskycd774b92016-07-26 17:02:31 +0200638 ptep_flush_direct(mm, addr, ptep, nodat);
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100639 pte_val(entry) &= ~_PAGE_INVALID;
640 pte_val(entry) |= _PAGE_PROTECT;
641 }
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100642 pgste_val(pgste) |= bit;
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100643 pgste = pgste_set_pte(ptep, pgste, entry);
644 pgste_set_unlock(ptep, pgste);
645 return 0;
646}
647
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100648int ptep_shadow_pte(struct mm_struct *mm, unsigned long saddr,
David Hildenbranda9d23e72016-03-08 12:21:41 +0100649 pte_t *sptep, pte_t *tptep, pte_t pte)
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100650{
651 pgste_t spgste, tpgste;
652 pte_t spte, tpte;
653 int rc = -EAGAIN;
654
David Hildenbranda9d23e72016-03-08 12:21:41 +0100655 if (!(pte_val(*tptep) & _PAGE_INVALID))
656 return 0; /* already shadowed */
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100657 spgste = pgste_get_lock(sptep);
658 spte = *sptep;
659 if (!(pte_val(spte) & _PAGE_INVALID) &&
David Hildenbranda9d23e72016-03-08 12:21:41 +0100660 !((pte_val(spte) & _PAGE_PROTECT) &&
661 !(pte_val(pte) & _PAGE_PROTECT))) {
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100662 pgste_val(spgste) |= PGSTE_VSIE_BIT;
663 tpgste = pgste_get_lock(tptep);
664 pte_val(tpte) = (pte_val(spte) & PAGE_MASK) |
David Hildenbranda9d23e72016-03-08 12:21:41 +0100665 (pte_val(pte) & _PAGE_PROTECT);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100666 /* don't touch the storage key - it belongs to parent pgste */
667 tpgste = pgste_set_pte(tptep, tpgste, tpte);
668 pgste_set_unlock(tptep, tpgste);
David Hildenbranda9d23e72016-03-08 12:21:41 +0100669 rc = 1;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100670 }
671 pgste_set_unlock(sptep, spgste);
672 return rc;
673}
674
675void ptep_unshadow_pte(struct mm_struct *mm, unsigned long saddr, pte_t *ptep)
676{
677 pgste_t pgste;
Martin Schwidefskycd774b92016-07-26 17:02:31 +0200678 int nodat;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100679
680 pgste = pgste_get_lock(ptep);
681 /* notifier is called by the caller */
Martin Schwidefskycd774b92016-07-26 17:02:31 +0200682 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
683 ptep_flush_direct(mm, saddr, ptep, nodat);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100684 /* don't touch the storage key - it belongs to parent pgste */
685 pgste = pgste_set_pte(ptep, pgste, __pte(_PAGE_INVALID));
686 pgste_set_unlock(ptep, pgste);
687}
688
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100689static void ptep_zap_swap_entry(struct mm_struct *mm, swp_entry_t entry)
690{
691 if (!non_swap_entry(entry))
692 dec_mm_counter(mm, MM_SWAPENTS);
693 else if (is_migration_entry(entry)) {
694 struct page *page = migration_entry_to_page(entry);
695
696 dec_mm_counter(mm, mm_counter(page));
697 }
698 free_swap_and_cache(entry);
699}
700
701void ptep_zap_unused(struct mm_struct *mm, unsigned long addr,
702 pte_t *ptep, int reset)
703{
704 unsigned long pgstev;
705 pgste_t pgste;
706 pte_t pte;
707
708 /* Zap unused and logically-zero pages */
Martin Schwidefskya9809402016-06-06 10:30:45 +0200709 preempt_disable();
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100710 pgste = pgste_get_lock(ptep);
711 pgstev = pgste_val(pgste);
712 pte = *ptep;
Christian Borntraeger1c343f72016-06-13 13:14:56 +0200713 if (!reset && pte_swap(pte) &&
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100714 ((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED ||
715 (pgstev & _PGSTE_GPS_ZERO))) {
716 ptep_zap_swap_entry(mm, pte_to_swp_entry(pte));
717 pte_clear(mm, addr, ptep);
718 }
719 if (reset)
720 pgste_val(pgste) &= ~_PGSTE_GPS_USAGE_MASK;
721 pgste_set_unlock(ptep, pgste);
Martin Schwidefskya9809402016-06-06 10:30:45 +0200722 preempt_enable();
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100723}
724
725void ptep_zap_key(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
726{
727 unsigned long ptev;
728 pgste_t pgste;
729
Christian Borntraeger97ca7bf2017-07-06 10:12:58 +0200730 /* Clear storage key ACC and F, but set R/C */
Martin Schwidefskya9809402016-06-06 10:30:45 +0200731 preempt_disable();
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100732 pgste = pgste_get_lock(ptep);
Christian Borntraeger97ca7bf2017-07-06 10:12:58 +0200733 pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT);
734 pgste_val(pgste) |= PGSTE_GR_BIT | PGSTE_GC_BIT;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100735 ptev = pte_val(*ptep);
736 if (!(ptev & _PAGE_INVALID) && (ptev & _PAGE_WRITE))
737 page_set_storage_key(ptev & PAGE_MASK, PAGE_DEFAULT_KEY, 1);
738 pgste_set_unlock(ptep, pgste);
Martin Schwidefskya9809402016-06-06 10:30:45 +0200739 preempt_enable();
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100740}
741
742/*
743 * Test and reset if a guest page is dirty
744 */
Janosch Frank0959e162018-07-17 13:21:22 +0100745bool ptep_test_and_clear_uc(struct mm_struct *mm, unsigned long addr,
746 pte_t *ptep)
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100747{
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100748 pgste_t pgste;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100749 pte_t pte;
750 bool dirty;
Martin Schwidefskycd774b92016-07-26 17:02:31 +0200751 int nodat;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100752
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100753 pgste = pgste_get_lock(ptep);
754 dirty = !!(pgste_val(pgste) & PGSTE_UC_BIT);
755 pgste_val(pgste) &= ~PGSTE_UC_BIT;
756 pte = *ptep;
757 if (dirty && (pte_val(pte) & _PAGE_PRESENT)) {
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100758 pgste = pgste_pte_notify(mm, addr, ptep, pgste);
Martin Schwidefskycd774b92016-07-26 17:02:31 +0200759 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
760 ptep_ipte_global(mm, addr, ptep, nodat);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100761 if (MACHINE_HAS_ESOP || !(pte_val(pte) & _PAGE_WRITE))
762 pte_val(pte) |= _PAGE_PROTECT;
763 else
764 pte_val(pte) |= _PAGE_INVALID;
765 *ptep = pte;
766 }
767 pgste_set_unlock(ptep, pgste);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100768 return dirty;
769}
Janosch Frank0959e162018-07-17 13:21:22 +0100770EXPORT_SYMBOL_GPL(ptep_test_and_clear_uc);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100771
772int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
773 unsigned char key, bool nq)
774{
Janosch Frank637ff9e2018-07-13 11:28:28 +0100775 unsigned long keyul, paddr;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100776 spinlock_t *ptl;
777 pgste_t old, new;
Janosch Frank637ff9e2018-07-13 11:28:28 +0100778 pmd_t *pmdp;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100779 pte_t *ptep;
780
Janosch Frank637ff9e2018-07-13 11:28:28 +0100781 pmdp = pmd_alloc_map(mm, addr);
782 if (unlikely(!pmdp))
783 return -EFAULT;
784
785 ptl = pmd_lock(mm, pmdp);
786 if (!pmd_present(*pmdp)) {
787 spin_unlock(ptl);
788 return -EFAULT;
789 }
790
791 if (pmd_large(*pmdp)) {
792 paddr = pmd_val(*pmdp) & HPAGE_MASK;
793 paddr |= addr & ~HPAGE_MASK;
794 /*
795 * Huge pmds need quiescing operations, they are
796 * always mapped.
797 */
798 page_set_storage_key(paddr, key, 1);
799 spin_unlock(ptl);
800 return 0;
801 }
802 spin_unlock(ptl);
803
804 ptep = pte_alloc_map_lock(mm, pmdp, addr, &ptl);
Martin Schwidefskyd3ed1ce2016-03-08 11:53:35 +0100805 if (unlikely(!ptep))
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100806 return -EFAULT;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100807
808 new = old = pgste_get_lock(ptep);
809 pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
810 PGSTE_ACC_BITS | PGSTE_FP_BIT);
811 keyul = (unsigned long) key;
812 pgste_val(new) |= (keyul & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
813 pgste_val(new) |= (keyul & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
814 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
Janosch Frank637ff9e2018-07-13 11:28:28 +0100815 unsigned long bits, skey;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100816
Janosch Frank637ff9e2018-07-13 11:28:28 +0100817 paddr = pte_val(*ptep) & PAGE_MASK;
818 skey = (unsigned long) page_get_storage_key(paddr);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100819 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
820 skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
821 /* Set storage key ACC and FP */
Janosch Frank637ff9e2018-07-13 11:28:28 +0100822 page_set_storage_key(paddr, skey, !nq);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100823 /* Merge host changed & referenced into pgste */
824 pgste_val(new) |= bits << 52;
825 }
826 /* changing the guest storage key is considered a change of the page */
827 if ((pgste_val(new) ^ pgste_val(old)) &
828 (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
829 pgste_val(new) |= PGSTE_UC_BIT;
830
831 pgste_set_unlock(ptep, new);
832 pte_unmap_unlock(ptep, ptl);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100833 return 0;
834}
835EXPORT_SYMBOL(set_guest_storage_key);
836
David Hildenbrand1824c722016-05-10 09:43:11 +0200837/**
838 * Conditionally set a guest storage key (handling csske).
839 * oldkey will be updated when either mr or mc is set and a pointer is given.
840 *
841 * Returns 0 if a guests storage key update wasn't necessary, 1 if the guest
842 * storage key was updated and -EFAULT on access errors.
843 */
844int cond_set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
845 unsigned char key, unsigned char *oldkey,
846 bool nq, bool mr, bool mc)
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100847{
David Hildenbrand1824c722016-05-10 09:43:11 +0200848 unsigned char tmp, mask = _PAGE_ACC_BITS | _PAGE_FP_BIT;
849 int rc;
850
851 /* we can drop the pgste lock between getting and setting the key */
852 if (mr | mc) {
853 rc = get_guest_storage_key(current->mm, addr, &tmp);
854 if (rc)
855 return rc;
856 if (oldkey)
857 *oldkey = tmp;
858 if (!mr)
859 mask |= _PAGE_REFERENCED;
860 if (!mc)
861 mask |= _PAGE_CHANGED;
862 if (!((tmp ^ key) & mask))
863 return 0;
864 }
865 rc = set_guest_storage_key(current->mm, addr, key, nq);
866 return rc < 0 ? rc : 1;
867}
868EXPORT_SYMBOL(cond_set_guest_storage_key);
869
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200870/**
871 * Reset a guest reference bit (rrbe), returning the reference and changed bit.
872 *
873 * Returns < 0 in case of error, otherwise the cc to be reported to the guest.
874 */
875int reset_guest_reference_bit(struct mm_struct *mm, unsigned long addr)
876{
877 spinlock_t *ptl;
Janosch Frank637ff9e2018-07-13 11:28:28 +0100878 unsigned long paddr;
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200879 pgste_t old, new;
Janosch Frank637ff9e2018-07-13 11:28:28 +0100880 pmd_t *pmdp;
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200881 pte_t *ptep;
882 int cc = 0;
883
Janosch Frank637ff9e2018-07-13 11:28:28 +0100884 pmdp = pmd_alloc_map(mm, addr);
885 if (unlikely(!pmdp))
886 return -EFAULT;
887
888 ptl = pmd_lock(mm, pmdp);
889 if (!pmd_present(*pmdp)) {
890 spin_unlock(ptl);
891 return -EFAULT;
892 }
893
894 if (pmd_large(*pmdp)) {
895 paddr = pmd_val(*pmdp) & HPAGE_MASK;
896 paddr |= addr & ~HPAGE_MASK;
897 cc = page_reset_referenced(paddr);
898 spin_unlock(ptl);
899 return cc;
900 }
901 spin_unlock(ptl);
902
903 ptep = pte_alloc_map_lock(mm, pmdp, addr, &ptl);
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200904 if (unlikely(!ptep))
905 return -EFAULT;
906
907 new = old = pgste_get_lock(ptep);
908 /* Reset guest reference bit only */
909 pgste_val(new) &= ~PGSTE_GR_BIT;
910
911 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
Janosch Frank637ff9e2018-07-13 11:28:28 +0100912 paddr = pte_val(*ptep) & PAGE_MASK;
913 cc = page_reset_referenced(paddr);
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200914 /* Merge real referenced bit into host-set */
915 pgste_val(new) |= ((unsigned long) cc << 53) & PGSTE_HR_BIT;
916 }
917 /* Reflect guest's logical view, not physical */
918 cc |= (pgste_val(old) & (PGSTE_GR_BIT | PGSTE_GC_BIT)) >> 49;
919 /* Changing the guest storage key is considered a change of the page */
920 if ((pgste_val(new) ^ pgste_val(old)) & PGSTE_GR_BIT)
921 pgste_val(new) |= PGSTE_UC_BIT;
922
923 pgste_set_unlock(ptep, new);
924 pte_unmap_unlock(ptep, ptl);
Janosch Frank4bead2a2017-01-27 10:23:59 +0100925 return cc;
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200926}
927EXPORT_SYMBOL(reset_guest_reference_bit);
928
David Hildenbrand154c8c12016-05-09 11:22:34 +0200929int get_guest_storage_key(struct mm_struct *mm, unsigned long addr,
930 unsigned char *key)
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100931{
Janosch Frank637ff9e2018-07-13 11:28:28 +0100932 unsigned long paddr;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100933 spinlock_t *ptl;
934 pgste_t pgste;
Janosch Frank637ff9e2018-07-13 11:28:28 +0100935 pmd_t *pmdp;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100936 pte_t *ptep;
937
Janosch Frank637ff9e2018-07-13 11:28:28 +0100938 pmdp = pmd_alloc_map(mm, addr);
939 if (unlikely(!pmdp))
940 return -EFAULT;
941
942 ptl = pmd_lock(mm, pmdp);
943 if (!pmd_present(*pmdp)) {
944 /* Not yet mapped memory has a zero key */
945 spin_unlock(ptl);
946 *key = 0;
947 return 0;
948 }
949
950 if (pmd_large(*pmdp)) {
951 paddr = pmd_val(*pmdp) & HPAGE_MASK;
952 paddr |= addr & ~HPAGE_MASK;
953 *key = page_get_storage_key(paddr);
954 spin_unlock(ptl);
955 return 0;
956 }
957 spin_unlock(ptl);
958
959 ptep = pte_alloc_map_lock(mm, pmdp, addr, &ptl);
Martin Schwidefskyd3ed1ce2016-03-08 11:53:35 +0100960 if (unlikely(!ptep))
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100961 return -EFAULT;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100962
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100963 pgste = pgste_get_lock(ptep);
David Hildenbrand154c8c12016-05-09 11:22:34 +0200964 *key = (pgste_val(pgste) & (PGSTE_ACC_BITS | PGSTE_FP_BIT)) >> 56;
Janosch Frank637ff9e2018-07-13 11:28:28 +0100965 paddr = pte_val(*ptep) & PAGE_MASK;
David Hildenbrand8d6037a2016-05-09 11:15:32 +0200966 if (!(pte_val(*ptep) & _PAGE_INVALID))
Janosch Frank637ff9e2018-07-13 11:28:28 +0100967 *key = page_get_storage_key(paddr);
David Hildenbrand8d6037a2016-05-09 11:15:32 +0200968 /* Reflect guest's logical view, not physical */
David Hildenbrand154c8c12016-05-09 11:22:34 +0200969 *key |= (pgste_val(pgste) & (PGSTE_GR_BIT | PGSTE_GC_BIT)) >> 48;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100970 pgste_set_unlock(ptep, pgste);
971 pte_unmap_unlock(ptep, ptl);
David Hildenbrand154c8c12016-05-09 11:22:34 +0200972 return 0;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100973}
974EXPORT_SYMBOL(get_guest_storage_key);
Claudio Imbrenda2d42f942017-04-20 10:03:45 +0200975
976/**
977 * pgste_perform_essa - perform ESSA actions on the PGSTE.
978 * @mm: the memory context. It must have PGSTEs, no check is performed here!
979 * @hva: the host virtual address of the page whose PGSTE is to be processed
980 * @orc: the specific action to perform, see the ESSA_SET_* macros.
981 * @oldpte: the PTE will be saved there if the pointer is not NULL.
982 * @oldpgste: the old PGSTE will be saved there if the pointer is not NULL.
983 *
984 * Return: 1 if the page is to be added to the CBRL, otherwise 0,
985 * or < 0 in case of error. -EINVAL is returned for invalid values
986 * of orc, -EFAULT for invalid addresses.
987 */
988int pgste_perform_essa(struct mm_struct *mm, unsigned long hva, int orc,
989 unsigned long *oldpte, unsigned long *oldpgste)
990{
991 unsigned long pgstev;
992 spinlock_t *ptl;
993 pgste_t pgste;
994 pte_t *ptep;
995 int res = 0;
996
997 WARN_ON_ONCE(orc > ESSA_MAX);
998 if (unlikely(orc > ESSA_MAX))
999 return -EINVAL;
1000 ptep = get_locked_pte(mm, hva, &ptl);
1001 if (unlikely(!ptep))
1002 return -EFAULT;
1003 pgste = pgste_get_lock(ptep);
1004 pgstev = pgste_val(pgste);
1005 if (oldpte)
1006 *oldpte = pte_val(*ptep);
1007 if (oldpgste)
1008 *oldpgste = pgstev;
1009
1010 switch (orc) {
1011 case ESSA_GET_STATE:
1012 break;
1013 case ESSA_SET_STABLE:
Claudio Imbrenda1bab1c02016-08-29 15:56:55 +02001014 pgstev &= ~(_PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT);
Claudio Imbrenda2d42f942017-04-20 10:03:45 +02001015 pgstev |= _PGSTE_GPS_USAGE_STABLE;
1016 break;
1017 case ESSA_SET_UNUSED:
1018 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1019 pgstev |= _PGSTE_GPS_USAGE_UNUSED;
1020 if (pte_val(*ptep) & _PAGE_INVALID)
1021 res = 1;
1022 break;
1023 case ESSA_SET_VOLATILE:
1024 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1025 pgstev |= _PGSTE_GPS_USAGE_VOLATILE;
1026 if (pte_val(*ptep) & _PAGE_INVALID)
1027 res = 1;
1028 break;
1029 case ESSA_SET_POT_VOLATILE:
1030 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1031 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
1032 pgstev |= _PGSTE_GPS_USAGE_POT_VOLATILE;
1033 break;
1034 }
1035 if (pgstev & _PGSTE_GPS_ZERO) {
1036 pgstev |= _PGSTE_GPS_USAGE_VOLATILE;
1037 break;
1038 }
1039 if (!(pgstev & PGSTE_GC_BIT)) {
1040 pgstev |= _PGSTE_GPS_USAGE_VOLATILE;
1041 res = 1;
1042 break;
1043 }
1044 break;
1045 case ESSA_SET_STABLE_RESIDENT:
1046 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1047 pgstev |= _PGSTE_GPS_USAGE_STABLE;
1048 /*
1049 * Since the resident state can go away any time after this
1050 * call, we will not make this page resident. We can revisit
1051 * this decision if a guest will ever start using this.
1052 */
1053 break;
1054 case ESSA_SET_STABLE_IF_RESIDENT:
1055 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
1056 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1057 pgstev |= _PGSTE_GPS_USAGE_STABLE;
1058 }
1059 break;
Claudio Imbrenda1bab1c02016-08-29 15:56:55 +02001060 case ESSA_SET_STABLE_NODAT:
1061 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1062 pgstev |= _PGSTE_GPS_USAGE_STABLE | _PGSTE_GPS_NODAT;
1063 break;
Claudio Imbrenda2d42f942017-04-20 10:03:45 +02001064 default:
1065 /* we should never get here! */
1066 break;
1067 }
1068 /* If we are discarding a page, set it to logical zero */
1069 if (res)
1070 pgstev |= _PGSTE_GPS_ZERO;
1071
1072 pgste_val(pgste) = pgstev;
1073 pgste_set_unlock(ptep, pgste);
1074 pte_unmap_unlock(ptep, ptl);
1075 return res;
1076}
1077EXPORT_SYMBOL(pgste_perform_essa);
1078
1079/**
1080 * set_pgste_bits - set specific PGSTE bits.
1081 * @mm: the memory context. It must have PGSTEs, no check is performed here!
1082 * @hva: the host virtual address of the page whose PGSTE is to be processed
1083 * @bits: a bitmask representing the bits that will be touched
1084 * @value: the values of the bits to be written. Only the bits in the mask
1085 * will be written.
1086 *
1087 * Return: 0 on success, < 0 in case of error.
1088 */
1089int set_pgste_bits(struct mm_struct *mm, unsigned long hva,
1090 unsigned long bits, unsigned long value)
1091{
1092 spinlock_t *ptl;
1093 pgste_t new;
1094 pte_t *ptep;
1095
1096 ptep = get_locked_pte(mm, hva, &ptl);
1097 if (unlikely(!ptep))
1098 return -EFAULT;
1099 new = pgste_get_lock(ptep);
1100
1101 pgste_val(new) &= ~bits;
1102 pgste_val(new) |= value & bits;
1103
1104 pgste_set_unlock(ptep, new);
1105 pte_unmap_unlock(ptep, ptl);
1106 return 0;
1107}
1108EXPORT_SYMBOL(set_pgste_bits);
1109
1110/**
1111 * get_pgste - get the current PGSTE for the given address.
1112 * @mm: the memory context. It must have PGSTEs, no check is performed here!
1113 * @hva: the host virtual address of the page whose PGSTE is to be processed
1114 * @pgstep: will be written with the current PGSTE for the given address.
1115 *
1116 * Return: 0 on success, < 0 in case of error.
1117 */
1118int get_pgste(struct mm_struct *mm, unsigned long hva, unsigned long *pgstep)
1119{
1120 spinlock_t *ptl;
1121 pte_t *ptep;
1122
1123 ptep = get_locked_pte(mm, hva, &ptl);
1124 if (unlikely(!ptep))
1125 return -EFAULT;
1126 *pgstep = pgste_val(pgste_get(ptep));
1127 pte_unmap_unlock(ptep, ptl);
1128 return 0;
1129}
1130EXPORT_SYMBOL(get_pgste);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001131#endif