1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright IBM Corp. 2007, 2011
4 * 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>
10 #include <linux/gfp.h>
11 #include <linux/mm.h>
12 #include <linux/swap.h>
13 #include <linux/smp.h>
14 #include <linux/spinlock.h>
15 #include <linux/rcupdate.h>
16 #include <linux/slab.h>
17 #include <linux/swapops.h>
18 #include <linux/sysctl.h>
19 #include <linux/ksm.h>
20 #include <linux/mman.h>
21
22 #include <asm/tlb.h>
23 #include <asm/tlbflush.h>
24 #include <asm/mmu_context.h>
25 #include <asm/page-states.h>
26
pgprot_writecombine(pgprot_t prot)27 pgprot_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 }
35 EXPORT_SYMBOL_GPL(pgprot_writecombine);
36
pgprot_writethrough(pgprot_t prot)37 pgprot_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 }
45 EXPORT_SYMBOL_GPL(pgprot_writethrough);
46
ptep_ipte_local(struct mm_struct * mm,unsigned long addr,pte_t * ptep,int nodat)47 static inline void ptep_ipte_local(struct mm_struct *mm, unsigned long addr,
48 pte_t *ptep, int nodat)
49 {
50 unsigned long opt, asce;
51
52 if (MACHINE_HAS_TLB_GUEST) {
53 opt = 0;
54 asce = READ_ONCE(mm->context.gmap_asce);
55 if (asce == 0UL || nodat)
56 opt |= IPTE_NODAT;
57 if (asce != -1UL) {
58 asce = asce ? : mm->context.asce;
59 opt |= IPTE_GUEST_ASCE;
60 }
61 __ptep_ipte(addr, ptep, opt, asce, IPTE_LOCAL);
62 } else {
63 __ptep_ipte(addr, ptep, 0, 0, IPTE_LOCAL);
64 }
65 }
66
ptep_ipte_global(struct mm_struct * mm,unsigned long addr,pte_t * ptep,int nodat)67 static inline void ptep_ipte_global(struct mm_struct *mm, unsigned long addr,
68 pte_t *ptep, int nodat)
69 {
70 unsigned long opt, asce;
71
72 if (MACHINE_HAS_TLB_GUEST) {
73 opt = 0;
74 asce = READ_ONCE(mm->context.gmap_asce);
75 if (asce == 0UL || nodat)
76 opt |= IPTE_NODAT;
77 if (asce != -1UL) {
78 asce = asce ? : mm->context.asce;
79 opt |= IPTE_GUEST_ASCE;
80 }
81 __ptep_ipte(addr, ptep, opt, asce, IPTE_GLOBAL);
82 } else {
83 __ptep_ipte(addr, ptep, 0, 0, IPTE_GLOBAL);
84 }
85 }
86
ptep_flush_direct(struct mm_struct * mm,unsigned long addr,pte_t * ptep,int nodat)87 static inline pte_t ptep_flush_direct(struct mm_struct *mm,
88 unsigned long addr, pte_t *ptep,
89 int nodat)
90 {
91 pte_t old;
92
93 old = *ptep;
94 if (unlikely(pte_val(old) & _PAGE_INVALID))
95 return old;
96 atomic_inc(&mm->context.flush_count);
97 if (MACHINE_HAS_TLB_LC &&
98 cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
99 ptep_ipte_local(mm, addr, ptep, nodat);
100 else
101 ptep_ipte_global(mm, addr, ptep, nodat);
102 atomic_dec(&mm->context.flush_count);
103 return old;
104 }
105
ptep_flush_lazy(struct mm_struct * mm,unsigned long addr,pte_t * ptep,int nodat)106 static inline pte_t ptep_flush_lazy(struct mm_struct *mm,
107 unsigned long addr, pte_t *ptep,
108 int nodat)
109 {
110 pte_t old;
111
112 old = *ptep;
113 if (unlikely(pte_val(old) & _PAGE_INVALID))
114 return old;
115 atomic_inc(&mm->context.flush_count);
116 if (cpumask_equal(&mm->context.cpu_attach_mask,
117 cpumask_of(smp_processor_id()))) {
118 pte_val(*ptep) |= _PAGE_INVALID;
119 mm->context.flush_mm = 1;
120 } else
121 ptep_ipte_global(mm, addr, ptep, nodat);
122 atomic_dec(&mm->context.flush_count);
123 return old;
124 }
125
pgste_get_lock(pte_t * ptep)126 static inline pgste_t pgste_get_lock(pte_t *ptep)
127 {
128 unsigned long new = 0;
129 #ifdef CONFIG_PGSTE
130 unsigned long old;
131
132 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
pgste_set_unlock(pte_t * ptep,pgste_t pgste)145 static 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");
154 #endif
155 }
156
pgste_get(pte_t * ptep)157 static 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
pgste_set(pte_t * ptep,pgste_t pgste)166 static 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
pgste_update_all(pte_t pte,pgste_t pgste,struct mm_struct * mm)173 static 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
179 if (!mm_uses_skeys(mm) || pte_val(pte) & _PAGE_INVALID)
180 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
pgste_set_key(pte_t * ptep,pgste_t pgste,pte_t entry,struct mm_struct * mm)194 static 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
201 if (!mm_uses_skeys(mm) || pte_val(entry) & _PAGE_INVALID)
202 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
pgste_set_pte(pte_t * ptep,pgste_t pgste,pte_t entry)216 static 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
pgste_pte_notify(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pgste_t pgste)239 static inline pgste_t pgste_pte_notify(struct mm_struct *mm,
240 unsigned long addr,
241 pte_t *ptep, pgste_t pgste)
242 {
243 #ifdef CONFIG_PGSTE
244 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);
250 }
251 #endif
252 return pgste;
253 }
254
ptep_xchg_start(struct mm_struct * mm,unsigned long addr,pte_t * ptep)255 static 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);
262 pgste = pgste_pte_notify(mm, addr, ptep, pgste);
263 }
264 return pgste;
265 }
266
ptep_xchg_commit(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pgste_t pgste,pte_t old,pte_t new)267 static inline pte_t ptep_xchg_commit(struct mm_struct *mm,
268 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 }
285 return old;
286 }
287
ptep_xchg_direct(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t new)288 pte_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;
293 int nodat;
294
295 preempt_disable();
296 pgste = ptep_xchg_start(mm, addr, ptep);
297 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
298 old = ptep_flush_direct(mm, addr, ptep, nodat);
299 old = ptep_xchg_commit(mm, addr, ptep, pgste, old, new);
300 preempt_enable();
301 return old;
302 }
303 EXPORT_SYMBOL(ptep_xchg_direct);
304
ptep_xchg_lazy(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t new)305 pte_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;
310 int nodat;
311
312 preempt_disable();
313 pgste = ptep_xchg_start(mm, addr, ptep);
314 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
315 old = ptep_flush_lazy(mm, addr, ptep, nodat);
316 old = ptep_xchg_commit(mm, addr, ptep, pgste, old, new);
317 preempt_enable();
318 return old;
319 }
320 EXPORT_SYMBOL(ptep_xchg_lazy);
321
ptep_modify_prot_start(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep)322 pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr,
323 pte_t *ptep)
324 {
325 pgste_t pgste;
326 pte_t old;
327 int nodat;
328 struct mm_struct *mm = vma->vm_mm;
329
330 preempt_disable();
331 pgste = ptep_xchg_start(mm, addr, ptep);
332 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
333 old = ptep_flush_lazy(mm, addr, ptep, nodat);
334 if (mm_has_pgste(mm)) {
335 pgste = pgste_update_all(old, pgste, mm);
336 pgste_set(ptep, pgste);
337 }
338 return old;
339 }
340
ptep_modify_prot_commit(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,pte_t old_pte,pte_t pte)341 void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr,
342 pte_t *ptep, pte_t old_pte, pte_t pte)
343 {
344 pgste_t pgste;
345 struct mm_struct *mm = vma->vm_mm;
346
347 if (!MACHINE_HAS_NX)
348 pte_val(pte) &= ~_PAGE_NOEXEC;
349 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 }
357 preempt_enable();
358 }
359
pmdp_idte_local(struct mm_struct * mm,unsigned long addr,pmd_t * pmdp)360 static inline void pmdp_idte_local(struct mm_struct *mm,
361 unsigned long addr, pmd_t *pmdp)
362 {
363 if (MACHINE_HAS_TLB_GUEST)
364 __pmdp_idte(addr, pmdp, IDTE_NODAT | IDTE_GUEST_ASCE,
365 mm->context.asce, IDTE_LOCAL);
366 else
367 __pmdp_idte(addr, pmdp, 0, 0, IDTE_LOCAL);
368 if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m)
369 gmap_pmdp_idte_local(mm, addr);
370 }
371
pmdp_idte_global(struct mm_struct * mm,unsigned long addr,pmd_t * pmdp)372 static inline void pmdp_idte_global(struct mm_struct *mm,
373 unsigned long addr, pmd_t *pmdp)
374 {
375 if (MACHINE_HAS_TLB_GUEST) {
376 __pmdp_idte(addr, pmdp, IDTE_NODAT | IDTE_GUEST_ASCE,
377 mm->context.asce, IDTE_GLOBAL);
378 if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m)
379 gmap_pmdp_idte_global(mm, addr);
380 } else if (MACHINE_HAS_IDTE) {
381 __pmdp_idte(addr, pmdp, 0, 0, IDTE_GLOBAL);
382 if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m)
383 gmap_pmdp_idte_global(mm, addr);
384 } else {
385 __pmdp_csp(pmdp);
386 if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m)
387 gmap_pmdp_csp(mm, addr);
388 }
389 }
390
pmdp_flush_direct(struct mm_struct * mm,unsigned long addr,pmd_t * pmdp)391 static inline pmd_t pmdp_flush_direct(struct mm_struct *mm,
392 unsigned long addr, pmd_t *pmdp)
393 {
394 pmd_t old;
395
396 old = *pmdp;
397 if (pmd_val(old) & _SEGMENT_ENTRY_INVALID)
398 return old;
399 atomic_inc(&mm->context.flush_count);
400 if (MACHINE_HAS_TLB_LC &&
401 cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
402 pmdp_idte_local(mm, addr, pmdp);
403 else
404 pmdp_idte_global(mm, addr, pmdp);
405 atomic_dec(&mm->context.flush_count);
406 return old;
407 }
408
pmdp_flush_lazy(struct mm_struct * mm,unsigned long addr,pmd_t * pmdp)409 static inline pmd_t pmdp_flush_lazy(struct mm_struct *mm,
410 unsigned long addr, pmd_t *pmdp)
411 {
412 pmd_t old;
413
414 old = *pmdp;
415 if (pmd_val(old) & _SEGMENT_ENTRY_INVALID)
416 return old;
417 atomic_inc(&mm->context.flush_count);
418 if (cpumask_equal(&mm->context.cpu_attach_mask,
419 cpumask_of(smp_processor_id()))) {
420 pmd_val(*pmdp) |= _SEGMENT_ENTRY_INVALID;
421 mm->context.flush_mm = 1;
422 if (mm_has_pgste(mm))
423 gmap_pmdp_invalidate(mm, addr);
424 } else {
425 pmdp_idte_global(mm, addr, pmdp);
426 }
427 atomic_dec(&mm->context.flush_count);
428 return old;
429 }
430
431 #ifdef CONFIG_PGSTE
pmd_lookup(struct mm_struct * mm,unsigned long addr,pmd_t ** pmdp)432 static int pmd_lookup(struct mm_struct *mm, unsigned long addr, pmd_t **pmdp)
433 {
434 struct vm_area_struct *vma;
435 pgd_t *pgd;
436 p4d_t *p4d;
437 pud_t *pud;
438
439 /* We need a valid VMA, otherwise this is clearly a fault. */
440 vma = vma_lookup(mm, addr);
441 if (!vma)
442 return -EFAULT;
443
444 pgd = pgd_offset(mm, addr);
445 if (!pgd_present(*pgd))
446 return -ENOENT;
447
448 p4d = p4d_offset(pgd, addr);
449 if (!p4d_present(*p4d))
450 return -ENOENT;
451
452 pud = pud_offset(p4d, addr);
453 if (!pud_present(*pud))
454 return -ENOENT;
455
456 /* Large PUDs are not supported yet. */
457 if (pud_large(*pud))
458 return -EFAULT;
459
460 *pmdp = pmd_offset(pud, addr);
461 return 0;
462 }
463 #endif
464
pmdp_xchg_direct(struct mm_struct * mm,unsigned long addr,pmd_t * pmdp,pmd_t new)465 pmd_t pmdp_xchg_direct(struct mm_struct *mm, unsigned long addr,
466 pmd_t *pmdp, pmd_t new)
467 {
468 pmd_t old;
469
470 preempt_disable();
471 old = pmdp_flush_direct(mm, addr, pmdp);
472 *pmdp = new;
473 preempt_enable();
474 return old;
475 }
476 EXPORT_SYMBOL(pmdp_xchg_direct);
477
pmdp_xchg_lazy(struct mm_struct * mm,unsigned long addr,pmd_t * pmdp,pmd_t new)478 pmd_t pmdp_xchg_lazy(struct mm_struct *mm, unsigned long addr,
479 pmd_t *pmdp, pmd_t new)
480 {
481 pmd_t old;
482
483 preempt_disable();
484 old = pmdp_flush_lazy(mm, addr, pmdp);
485 *pmdp = new;
486 preempt_enable();
487 return old;
488 }
489 EXPORT_SYMBOL(pmdp_xchg_lazy);
490
pudp_idte_local(struct mm_struct * mm,unsigned long addr,pud_t * pudp)491 static inline void pudp_idte_local(struct mm_struct *mm,
492 unsigned long addr, pud_t *pudp)
493 {
494 if (MACHINE_HAS_TLB_GUEST)
495 __pudp_idte(addr, pudp, IDTE_NODAT | IDTE_GUEST_ASCE,
496 mm->context.asce, IDTE_LOCAL);
497 else
498 __pudp_idte(addr, pudp, 0, 0, IDTE_LOCAL);
499 }
500
pudp_idte_global(struct mm_struct * mm,unsigned long addr,pud_t * pudp)501 static inline void pudp_idte_global(struct mm_struct *mm,
502 unsigned long addr, pud_t *pudp)
503 {
504 if (MACHINE_HAS_TLB_GUEST)
505 __pudp_idte(addr, pudp, IDTE_NODAT | IDTE_GUEST_ASCE,
506 mm->context.asce, IDTE_GLOBAL);
507 else if (MACHINE_HAS_IDTE)
508 __pudp_idte(addr, pudp, 0, 0, IDTE_GLOBAL);
509 else
510 /*
511 * Invalid bit position is the same for pmd and pud, so we can
512 * re-use _pmd_csp() here
513 */
514 __pmdp_csp((pmd_t *) pudp);
515 }
516
pudp_flush_direct(struct mm_struct * mm,unsigned long addr,pud_t * pudp)517 static inline pud_t pudp_flush_direct(struct mm_struct *mm,
518 unsigned long addr, pud_t *pudp)
519 {
520 pud_t old;
521
522 old = *pudp;
523 if (pud_val(old) & _REGION_ENTRY_INVALID)
524 return old;
525 atomic_inc(&mm->context.flush_count);
526 if (MACHINE_HAS_TLB_LC &&
527 cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
528 pudp_idte_local(mm, addr, pudp);
529 else
530 pudp_idte_global(mm, addr, pudp);
531 atomic_dec(&mm->context.flush_count);
532 return old;
533 }
534
pudp_xchg_direct(struct mm_struct * mm,unsigned long addr,pud_t * pudp,pud_t new)535 pud_t pudp_xchg_direct(struct mm_struct *mm, unsigned long addr,
536 pud_t *pudp, pud_t new)
537 {
538 pud_t old;
539
540 preempt_disable();
541 old = pudp_flush_direct(mm, addr, pudp);
542 *pudp = new;
543 preempt_enable();
544 return old;
545 }
546 EXPORT_SYMBOL(pudp_xchg_direct);
547
548 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
pgtable_trans_huge_deposit(struct mm_struct * mm,pmd_t * pmdp,pgtable_t pgtable)549 void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
550 pgtable_t pgtable)
551 {
552 struct list_head *lh = (struct list_head *) pgtable;
553
554 assert_spin_locked(pmd_lockptr(mm, pmdp));
555
556 /* FIFO */
557 if (!pmd_huge_pte(mm, pmdp))
558 INIT_LIST_HEAD(lh);
559 else
560 list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
561 pmd_huge_pte(mm, pmdp) = pgtable;
562 }
563
pgtable_trans_huge_withdraw(struct mm_struct * mm,pmd_t * pmdp)564 pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
565 {
566 struct list_head *lh;
567 pgtable_t pgtable;
568 pte_t *ptep;
569
570 assert_spin_locked(pmd_lockptr(mm, pmdp));
571
572 /* FIFO */
573 pgtable = pmd_huge_pte(mm, pmdp);
574 lh = (struct list_head *) pgtable;
575 if (list_empty(lh))
576 pmd_huge_pte(mm, pmdp) = NULL;
577 else {
578 pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
579 list_del(lh);
580 }
581 ptep = (pte_t *) pgtable;
582 pte_val(*ptep) = _PAGE_INVALID;
583 ptep++;
584 pte_val(*ptep) = _PAGE_INVALID;
585 return pgtable;
586 }
587 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
588
589 #ifdef CONFIG_PGSTE
ptep_set_pte_at(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t entry)590 void ptep_set_pte_at(struct mm_struct *mm, unsigned long addr,
591 pte_t *ptep, pte_t entry)
592 {
593 pgste_t pgste;
594
595 /* the mm_has_pgste() check is done in set_pte_at() */
596 preempt_disable();
597 pgste = pgste_get_lock(ptep);
598 pgste_val(pgste) &= ~_PGSTE_GPS_ZERO;
599 pgste_set_key(ptep, pgste, entry, mm);
600 pgste = pgste_set_pte(ptep, pgste, entry);
601 pgste_set_unlock(ptep, pgste);
602 preempt_enable();
603 }
604
ptep_set_notify(struct mm_struct * mm,unsigned long addr,pte_t * ptep)605 void ptep_set_notify(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
606 {
607 pgste_t pgste;
608
609 preempt_disable();
610 pgste = pgste_get_lock(ptep);
611 pgste_val(pgste) |= PGSTE_IN_BIT;
612 pgste_set_unlock(ptep, pgste);
613 preempt_enable();
614 }
615
616 /**
617 * ptep_force_prot - change access rights of a locked pte
618 * @mm: pointer to the process mm_struct
619 * @addr: virtual address in the guest address space
620 * @ptep: pointer to the page table entry
621 * @prot: indicates guest access rights: PROT_NONE, PROT_READ or PROT_WRITE
622 * @bit: pgste bit to set (e.g. for notification)
623 *
624 * Returns 0 if the access rights were changed and -EAGAIN if the current
625 * and requested access rights are incompatible.
626 */
ptep_force_prot(struct mm_struct * mm,unsigned long addr,pte_t * ptep,int prot,unsigned long bit)627 int ptep_force_prot(struct mm_struct *mm, unsigned long addr,
628 pte_t *ptep, int prot, unsigned long bit)
629 {
630 pte_t entry;
631 pgste_t pgste;
632 int pte_i, pte_p, nodat;
633
634 pgste = pgste_get_lock(ptep);
635 entry = *ptep;
636 /* Check pte entry after all locks have been acquired */
637 pte_i = pte_val(entry) & _PAGE_INVALID;
638 pte_p = pte_val(entry) & _PAGE_PROTECT;
639 if ((pte_i && (prot != PROT_NONE)) ||
640 (pte_p && (prot & PROT_WRITE))) {
641 pgste_set_unlock(ptep, pgste);
642 return -EAGAIN;
643 }
644 /* Change access rights and set pgste bit */
645 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
646 if (prot == PROT_NONE && !pte_i) {
647 ptep_flush_direct(mm, addr, ptep, nodat);
648 pgste = pgste_update_all(entry, pgste, mm);
649 pte_val(entry) |= _PAGE_INVALID;
650 }
651 if (prot == PROT_READ && !pte_p) {
652 ptep_flush_direct(mm, addr, ptep, nodat);
653 pte_val(entry) &= ~_PAGE_INVALID;
654 pte_val(entry) |= _PAGE_PROTECT;
655 }
656 pgste_val(pgste) |= bit;
657 pgste = pgste_set_pte(ptep, pgste, entry);
658 pgste_set_unlock(ptep, pgste);
659 return 0;
660 }
661
ptep_shadow_pte(struct mm_struct * mm,unsigned long saddr,pte_t * sptep,pte_t * tptep,pte_t pte)662 int ptep_shadow_pte(struct mm_struct *mm, unsigned long saddr,
663 pte_t *sptep, pte_t *tptep, pte_t pte)
664 {
665 pgste_t spgste, tpgste;
666 pte_t spte, tpte;
667 int rc = -EAGAIN;
668
669 if (!(pte_val(*tptep) & _PAGE_INVALID))
670 return 0; /* already shadowed */
671 spgste = pgste_get_lock(sptep);
672 spte = *sptep;
673 if (!(pte_val(spte) & _PAGE_INVALID) &&
674 !((pte_val(spte) & _PAGE_PROTECT) &&
675 !(pte_val(pte) & _PAGE_PROTECT))) {
676 pgste_val(spgste) |= PGSTE_VSIE_BIT;
677 tpgste = pgste_get_lock(tptep);
678 pte_val(tpte) = (pte_val(spte) & PAGE_MASK) |
679 (pte_val(pte) & _PAGE_PROTECT);
680 /* don't touch the storage key - it belongs to parent pgste */
681 tpgste = pgste_set_pte(tptep, tpgste, tpte);
682 pgste_set_unlock(tptep, tpgste);
683 rc = 1;
684 }
685 pgste_set_unlock(sptep, spgste);
686 return rc;
687 }
688
ptep_unshadow_pte(struct mm_struct * mm,unsigned long saddr,pte_t * ptep)689 void ptep_unshadow_pte(struct mm_struct *mm, unsigned long saddr, pte_t *ptep)
690 {
691 pgste_t pgste;
692 int nodat;
693
694 pgste = pgste_get_lock(ptep);
695 /* notifier is called by the caller */
696 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
697 ptep_flush_direct(mm, saddr, ptep, nodat);
698 /* don't touch the storage key - it belongs to parent pgste */
699 pgste = pgste_set_pte(ptep, pgste, __pte(_PAGE_INVALID));
700 pgste_set_unlock(ptep, pgste);
701 }
702
ptep_zap_swap_entry(struct mm_struct * mm,swp_entry_t entry)703 static void ptep_zap_swap_entry(struct mm_struct *mm, swp_entry_t entry)
704 {
705 if (!non_swap_entry(entry))
706 dec_mm_counter(mm, MM_SWAPENTS);
707 else if (is_migration_entry(entry)) {
708 struct page *page = pfn_swap_entry_to_page(entry);
709
710 dec_mm_counter(mm, mm_counter(page));
711 }
712 free_swap_and_cache(entry);
713 }
714
ptep_zap_unused(struct mm_struct * mm,unsigned long addr,pte_t * ptep,int reset)715 void ptep_zap_unused(struct mm_struct *mm, unsigned long addr,
716 pte_t *ptep, int reset)
717 {
718 unsigned long pgstev;
719 pgste_t pgste;
720 pte_t pte;
721
722 /* Zap unused and logically-zero pages */
723 preempt_disable();
724 pgste = pgste_get_lock(ptep);
725 pgstev = pgste_val(pgste);
726 pte = *ptep;
727 if (!reset && pte_swap(pte) &&
728 ((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED ||
729 (pgstev & _PGSTE_GPS_ZERO))) {
730 ptep_zap_swap_entry(mm, pte_to_swp_entry(pte));
731 pte_clear(mm, addr, ptep);
732 }
733 if (reset)
734 pgste_val(pgste) &= ~(_PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT);
735 pgste_set_unlock(ptep, pgste);
736 preempt_enable();
737 }
738
ptep_zap_key(struct mm_struct * mm,unsigned long addr,pte_t * ptep)739 void ptep_zap_key(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
740 {
741 unsigned long ptev;
742 pgste_t pgste;
743
744 /* Clear storage key ACC and F, but set R/C */
745 preempt_disable();
746 pgste = pgste_get_lock(ptep);
747 pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT);
748 pgste_val(pgste) |= PGSTE_GR_BIT | PGSTE_GC_BIT;
749 ptev = pte_val(*ptep);
750 if (!(ptev & _PAGE_INVALID) && (ptev & _PAGE_WRITE))
751 page_set_storage_key(ptev & PAGE_MASK, PAGE_DEFAULT_KEY, 0);
752 pgste_set_unlock(ptep, pgste);
753 preempt_enable();
754 }
755
756 /*
757 * Test and reset if a guest page is dirty
758 */
ptep_test_and_clear_uc(struct mm_struct * mm,unsigned long addr,pte_t * ptep)759 bool ptep_test_and_clear_uc(struct mm_struct *mm, unsigned long addr,
760 pte_t *ptep)
761 {
762 pgste_t pgste;
763 pte_t pte;
764 bool dirty;
765 int nodat;
766
767 pgste = pgste_get_lock(ptep);
768 dirty = !!(pgste_val(pgste) & PGSTE_UC_BIT);
769 pgste_val(pgste) &= ~PGSTE_UC_BIT;
770 pte = *ptep;
771 if (dirty && (pte_val(pte) & _PAGE_PRESENT)) {
772 pgste = pgste_pte_notify(mm, addr, ptep, pgste);
773 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
774 ptep_ipte_global(mm, addr, ptep, nodat);
775 if (MACHINE_HAS_ESOP || !(pte_val(pte) & _PAGE_WRITE))
776 pte_val(pte) |= _PAGE_PROTECT;
777 else
778 pte_val(pte) |= _PAGE_INVALID;
779 *ptep = pte;
780 }
781 pgste_set_unlock(ptep, pgste);
782 return dirty;
783 }
784 EXPORT_SYMBOL_GPL(ptep_test_and_clear_uc);
785
set_guest_storage_key(struct mm_struct * mm,unsigned long addr,unsigned char key,bool nq)786 int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
787 unsigned char key, bool nq)
788 {
789 unsigned long keyul, paddr;
790 spinlock_t *ptl;
791 pgste_t old, new;
792 pmd_t *pmdp;
793 pte_t *ptep;
794
795 if (pmd_lookup(mm, addr, &pmdp))
796 return -EFAULT;
797
798 ptl = pmd_lock(mm, pmdp);
799 if (!pmd_present(*pmdp)) {
800 spin_unlock(ptl);
801 return -EFAULT;
802 }
803
804 if (pmd_large(*pmdp)) {
805 paddr = pmd_val(*pmdp) & HPAGE_MASK;
806 paddr |= addr & ~HPAGE_MASK;
807 /*
808 * Huge pmds need quiescing operations, they are
809 * always mapped.
810 */
811 page_set_storage_key(paddr, key, 1);
812 spin_unlock(ptl);
813 return 0;
814 }
815 spin_unlock(ptl);
816
817 ptep = pte_alloc_map_lock(mm, pmdp, addr, &ptl);
818 if (unlikely(!ptep))
819 return -EFAULT;
820
821 new = old = pgste_get_lock(ptep);
822 pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
823 PGSTE_ACC_BITS | PGSTE_FP_BIT);
824 keyul = (unsigned long) key;
825 pgste_val(new) |= (keyul & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
826 pgste_val(new) |= (keyul & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
827 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
828 unsigned long bits, skey;
829
830 paddr = pte_val(*ptep) & PAGE_MASK;
831 skey = (unsigned long) page_get_storage_key(paddr);
832 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
833 skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
834 /* Set storage key ACC and FP */
835 page_set_storage_key(paddr, skey, !nq);
836 /* Merge host changed & referenced into pgste */
837 pgste_val(new) |= bits << 52;
838 }
839 /* changing the guest storage key is considered a change of the page */
840 if ((pgste_val(new) ^ pgste_val(old)) &
841 (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
842 pgste_val(new) |= PGSTE_UC_BIT;
843
844 pgste_set_unlock(ptep, new);
845 pte_unmap_unlock(ptep, ptl);
846 return 0;
847 }
848 EXPORT_SYMBOL(set_guest_storage_key);
849
850 /*
851 * Conditionally set a guest storage key (handling csske).
852 * oldkey will be updated when either mr or mc is set and a pointer is given.
853 *
854 * Returns 0 if a guests storage key update wasn't necessary, 1 if the guest
855 * storage key was updated and -EFAULT on access errors.
856 */
cond_set_guest_storage_key(struct mm_struct * mm,unsigned long addr,unsigned char key,unsigned char * oldkey,bool nq,bool mr,bool mc)857 int cond_set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
858 unsigned char key, unsigned char *oldkey,
859 bool nq, bool mr, bool mc)
860 {
861 unsigned char tmp, mask = _PAGE_ACC_BITS | _PAGE_FP_BIT;
862 int rc;
863
864 /* we can drop the pgste lock between getting and setting the key */
865 if (mr | mc) {
866 rc = get_guest_storage_key(current->mm, addr, &tmp);
867 if (rc)
868 return rc;
869 if (oldkey)
870 *oldkey = tmp;
871 if (!mr)
872 mask |= _PAGE_REFERENCED;
873 if (!mc)
874 mask |= _PAGE_CHANGED;
875 if (!((tmp ^ key) & mask))
876 return 0;
877 }
878 rc = set_guest_storage_key(current->mm, addr, key, nq);
879 return rc < 0 ? rc : 1;
880 }
881 EXPORT_SYMBOL(cond_set_guest_storage_key);
882
883 /*
884 * Reset a guest reference bit (rrbe), returning the reference and changed bit.
885 *
886 * Returns < 0 in case of error, otherwise the cc to be reported to the guest.
887 */
reset_guest_reference_bit(struct mm_struct * mm,unsigned long addr)888 int reset_guest_reference_bit(struct mm_struct *mm, unsigned long addr)
889 {
890 spinlock_t *ptl;
891 unsigned long paddr;
892 pgste_t old, new;
893 pmd_t *pmdp;
894 pte_t *ptep;
895 int cc = 0;
896
897 if (pmd_lookup(mm, addr, &pmdp))
898 return -EFAULT;
899
900 ptl = pmd_lock(mm, pmdp);
901 if (!pmd_present(*pmdp)) {
902 spin_unlock(ptl);
903 return -EFAULT;
904 }
905
906 if (pmd_large(*pmdp)) {
907 paddr = pmd_val(*pmdp) & HPAGE_MASK;
908 paddr |= addr & ~HPAGE_MASK;
909 cc = page_reset_referenced(paddr);
910 spin_unlock(ptl);
911 return cc;
912 }
913 spin_unlock(ptl);
914
915 ptep = pte_alloc_map_lock(mm, pmdp, addr, &ptl);
916 if (unlikely(!ptep))
917 return -EFAULT;
918
919 new = old = pgste_get_lock(ptep);
920 /* Reset guest reference bit only */
921 pgste_val(new) &= ~PGSTE_GR_BIT;
922
923 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
924 paddr = pte_val(*ptep) & PAGE_MASK;
925 cc = page_reset_referenced(paddr);
926 /* Merge real referenced bit into host-set */
927 pgste_val(new) |= ((unsigned long) cc << 53) & PGSTE_HR_BIT;
928 }
929 /* Reflect guest's logical view, not physical */
930 cc |= (pgste_val(old) & (PGSTE_GR_BIT | PGSTE_GC_BIT)) >> 49;
931 /* Changing the guest storage key is considered a change of the page */
932 if ((pgste_val(new) ^ pgste_val(old)) & PGSTE_GR_BIT)
933 pgste_val(new) |= PGSTE_UC_BIT;
934
935 pgste_set_unlock(ptep, new);
936 pte_unmap_unlock(ptep, ptl);
937 return cc;
938 }
939 EXPORT_SYMBOL(reset_guest_reference_bit);
940
get_guest_storage_key(struct mm_struct * mm,unsigned long addr,unsigned char * key)941 int get_guest_storage_key(struct mm_struct *mm, unsigned long addr,
942 unsigned char *key)
943 {
944 unsigned long paddr;
945 spinlock_t *ptl;
946 pgste_t pgste;
947 pmd_t *pmdp;
948 pte_t *ptep;
949
950 /*
951 * If we don't have a PTE table and if there is no huge page mapped,
952 * the storage key is 0.
953 */
954 *key = 0;
955
956 switch (pmd_lookup(mm, addr, &pmdp)) {
957 case -ENOENT:
958 return 0;
959 case 0:
960 break;
961 default:
962 return -EFAULT;
963 }
964
965 ptl = pmd_lock(mm, pmdp);
966 if (!pmd_present(*pmdp)) {
967 spin_unlock(ptl);
968 return 0;
969 }
970
971 if (pmd_large(*pmdp)) {
972 paddr = pmd_val(*pmdp) & HPAGE_MASK;
973 paddr |= addr & ~HPAGE_MASK;
974 *key = page_get_storage_key(paddr);
975 spin_unlock(ptl);
976 return 0;
977 }
978 spin_unlock(ptl);
979
980 ptep = pte_alloc_map_lock(mm, pmdp, addr, &ptl);
981 if (unlikely(!ptep))
982 return -EFAULT;
983
984 pgste = pgste_get_lock(ptep);
985 *key = (pgste_val(pgste) & (PGSTE_ACC_BITS | PGSTE_FP_BIT)) >> 56;
986 paddr = pte_val(*ptep) & PAGE_MASK;
987 if (!(pte_val(*ptep) & _PAGE_INVALID))
988 *key = page_get_storage_key(paddr);
989 /* Reflect guest's logical view, not physical */
990 *key |= (pgste_val(pgste) & (PGSTE_GR_BIT | PGSTE_GC_BIT)) >> 48;
991 pgste_set_unlock(ptep, pgste);
992 pte_unmap_unlock(ptep, ptl);
993 return 0;
994 }
995 EXPORT_SYMBOL(get_guest_storage_key);
996
997 /**
998 * pgste_perform_essa - perform ESSA actions on the PGSTE.
999 * @mm: the memory context. It must have PGSTEs, no check is performed here!
1000 * @hva: the host virtual address of the page whose PGSTE is to be processed
1001 * @orc: the specific action to perform, see the ESSA_SET_* macros.
1002 * @oldpte: the PTE will be saved there if the pointer is not NULL.
1003 * @oldpgste: the old PGSTE will be saved there if the pointer is not NULL.
1004 *
1005 * Return: 1 if the page is to be added to the CBRL, otherwise 0,
1006 * or < 0 in case of error. -EINVAL is returned for invalid values
1007 * of orc, -EFAULT for invalid addresses.
1008 */
pgste_perform_essa(struct mm_struct * mm,unsigned long hva,int orc,unsigned long * oldpte,unsigned long * oldpgste)1009 int pgste_perform_essa(struct mm_struct *mm, unsigned long hva, int orc,
1010 unsigned long *oldpte, unsigned long *oldpgste)
1011 {
1012 struct vm_area_struct *vma;
1013 unsigned long pgstev;
1014 spinlock_t *ptl;
1015 pgste_t pgste;
1016 pte_t *ptep;
1017 int res = 0;
1018
1019 WARN_ON_ONCE(orc > ESSA_MAX);
1020 if (unlikely(orc > ESSA_MAX))
1021 return -EINVAL;
1022
1023 vma = vma_lookup(mm, hva);
1024 if (!vma || is_vm_hugetlb_page(vma))
1025 return -EFAULT;
1026 ptep = get_locked_pte(mm, hva, &ptl);
1027 if (unlikely(!ptep))
1028 return -EFAULT;
1029 pgste = pgste_get_lock(ptep);
1030 pgstev = pgste_val(pgste);
1031 if (oldpte)
1032 *oldpte = pte_val(*ptep);
1033 if (oldpgste)
1034 *oldpgste = pgstev;
1035
1036 switch (orc) {
1037 case ESSA_GET_STATE:
1038 break;
1039 case ESSA_SET_STABLE:
1040 pgstev &= ~(_PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT);
1041 pgstev |= _PGSTE_GPS_USAGE_STABLE;
1042 break;
1043 case ESSA_SET_UNUSED:
1044 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1045 pgstev |= _PGSTE_GPS_USAGE_UNUSED;
1046 if (pte_val(*ptep) & _PAGE_INVALID)
1047 res = 1;
1048 break;
1049 case ESSA_SET_VOLATILE:
1050 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1051 pgstev |= _PGSTE_GPS_USAGE_VOLATILE;
1052 if (pte_val(*ptep) & _PAGE_INVALID)
1053 res = 1;
1054 break;
1055 case ESSA_SET_POT_VOLATILE:
1056 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1057 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
1058 pgstev |= _PGSTE_GPS_USAGE_POT_VOLATILE;
1059 break;
1060 }
1061 if (pgstev & _PGSTE_GPS_ZERO) {
1062 pgstev |= _PGSTE_GPS_USAGE_VOLATILE;
1063 break;
1064 }
1065 if (!(pgstev & PGSTE_GC_BIT)) {
1066 pgstev |= _PGSTE_GPS_USAGE_VOLATILE;
1067 res = 1;
1068 break;
1069 }
1070 break;
1071 case ESSA_SET_STABLE_RESIDENT:
1072 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1073 pgstev |= _PGSTE_GPS_USAGE_STABLE;
1074 /*
1075 * Since the resident state can go away any time after this
1076 * call, we will not make this page resident. We can revisit
1077 * this decision if a guest will ever start using this.
1078 */
1079 break;
1080 case ESSA_SET_STABLE_IF_RESIDENT:
1081 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
1082 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1083 pgstev |= _PGSTE_GPS_USAGE_STABLE;
1084 }
1085 break;
1086 case ESSA_SET_STABLE_NODAT:
1087 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1088 pgstev |= _PGSTE_GPS_USAGE_STABLE | _PGSTE_GPS_NODAT;
1089 break;
1090 default:
1091 /* we should never get here! */
1092 break;
1093 }
1094 /* If we are discarding a page, set it to logical zero */
1095 if (res)
1096 pgstev |= _PGSTE_GPS_ZERO;
1097
1098 pgste_val(pgste) = pgstev;
1099 pgste_set_unlock(ptep, pgste);
1100 pte_unmap_unlock(ptep, ptl);
1101 return res;
1102 }
1103 EXPORT_SYMBOL(pgste_perform_essa);
1104
1105 /**
1106 * set_pgste_bits - set specific PGSTE bits.
1107 * @mm: the memory context. It must have PGSTEs, no check is performed here!
1108 * @hva: the host virtual address of the page whose PGSTE is to be processed
1109 * @bits: a bitmask representing the bits that will be touched
1110 * @value: the values of the bits to be written. Only the bits in the mask
1111 * will be written.
1112 *
1113 * Return: 0 on success, < 0 in case of error.
1114 */
set_pgste_bits(struct mm_struct * mm,unsigned long hva,unsigned long bits,unsigned long value)1115 int set_pgste_bits(struct mm_struct *mm, unsigned long hva,
1116 unsigned long bits, unsigned long value)
1117 {
1118 struct vm_area_struct *vma;
1119 spinlock_t *ptl;
1120 pgste_t new;
1121 pte_t *ptep;
1122
1123 vma = vma_lookup(mm, hva);
1124 if (!vma || is_vm_hugetlb_page(vma))
1125 return -EFAULT;
1126 ptep = get_locked_pte(mm, hva, &ptl);
1127 if (unlikely(!ptep))
1128 return -EFAULT;
1129 new = pgste_get_lock(ptep);
1130
1131 pgste_val(new) &= ~bits;
1132 pgste_val(new) |= value & bits;
1133
1134 pgste_set_unlock(ptep, new);
1135 pte_unmap_unlock(ptep, ptl);
1136 return 0;
1137 }
1138 EXPORT_SYMBOL(set_pgste_bits);
1139
1140 /**
1141 * get_pgste - get the current PGSTE for the given address.
1142 * @mm: the memory context. It must have PGSTEs, no check is performed here!
1143 * @hva: the host virtual address of the page whose PGSTE is to be processed
1144 * @pgstep: will be written with the current PGSTE for the given address.
1145 *
1146 * Return: 0 on success, < 0 in case of error.
1147 */
get_pgste(struct mm_struct * mm,unsigned long hva,unsigned long * pgstep)1148 int get_pgste(struct mm_struct *mm, unsigned long hva, unsigned long *pgstep)
1149 {
1150 struct vm_area_struct *vma;
1151 spinlock_t *ptl;
1152 pte_t *ptep;
1153
1154 vma = vma_lookup(mm, hva);
1155 if (!vma || is_vm_hugetlb_page(vma))
1156 return -EFAULT;
1157 ptep = get_locked_pte(mm, hva, &ptl);
1158 if (unlikely(!ptep))
1159 return -EFAULT;
1160 *pgstep = pgste_val(pgste_get(ptep));
1161 pte_unmap_unlock(ptep, ptl);
1162 return 0;
1163 }
1164 EXPORT_SYMBOL(get_pgste);
1165 #endif
1166