1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2007, 2011
4 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
7 #include <linux/sched.h>
8 #include <linux/kernel.h>
9 #include <linux/errno.h>
10 #include <linux/gfp.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>
23 #include <asm/tlbflush.h>
24 #include <asm/mmu_context.h>
25 #include <asm/page-states.h>
27 pgprot_t pgprot_writecombine(pgprot_t prot)
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.
33 return __pgprot(pgprot_val(prot) | mio_wb_bit_mask);
35 EXPORT_SYMBOL_GPL(pgprot_writecombine);
37 pgprot_t pgprot_writethrough(pgprot_t prot)
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.
43 return __pgprot(pgprot_val(prot) & ~mio_wb_bit_mask);
45 EXPORT_SYMBOL_GPL(pgprot_writethrough);
47 static inline void ptep_ipte_local(struct mm_struct *mm, unsigned long addr,
48 pte_t *ptep, int nodat)
50 unsigned long opt, asce;
52 if (MACHINE_HAS_TLB_GUEST) {
54 asce = READ_ONCE(mm->context.gmap_asce);
55 if (asce == 0UL || nodat)
58 asce = asce ? : mm->context.asce;
59 opt |= IPTE_GUEST_ASCE;
61 __ptep_ipte(addr, ptep, opt, asce, IPTE_LOCAL);
63 __ptep_ipte(addr, ptep, 0, 0, IPTE_LOCAL);
67 static inline void ptep_ipte_global(struct mm_struct *mm, unsigned long addr,
68 pte_t *ptep, int nodat)
70 unsigned long opt, asce;
72 if (MACHINE_HAS_TLB_GUEST) {
74 asce = READ_ONCE(mm->context.gmap_asce);
75 if (asce == 0UL || nodat)
78 asce = asce ? : mm->context.asce;
79 opt |= IPTE_GUEST_ASCE;
81 __ptep_ipte(addr, ptep, opt, asce, IPTE_GLOBAL);
83 __ptep_ipte(addr, ptep, 0, 0, IPTE_GLOBAL);
87 static inline pte_t ptep_flush_direct(struct mm_struct *mm,
88 unsigned long addr, pte_t *ptep,
94 if (unlikely(pte_val(old) & _PAGE_INVALID))
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);
101 ptep_ipte_global(mm, addr, ptep, nodat);
102 atomic_dec(&mm->context.flush_count);
106 static inline pte_t ptep_flush_lazy(struct mm_struct *mm,
107 unsigned long addr, pte_t *ptep,
113 if (unlikely(pte_val(old) & _PAGE_INVALID))
115 atomic_inc(&mm->context.flush_count);
116 if (cpumask_equal(&mm->context.cpu_attach_mask,
117 cpumask_of(smp_processor_id()))) {
118 set_pte(ptep, set_pte_bit(*ptep, __pgprot(_PAGE_INVALID)));
119 mm->context.flush_mm = 1;
121 ptep_ipte_global(mm, addr, ptep, nodat);
122 atomic_dec(&mm->context.flush_count);
126 static inline pgste_t pgste_get_lock(pte_t *ptep)
128 unsigned long new = 0;
135 " nihh %0,0xff7f\n" /* clear PCL bit in old */
136 " oihh %1,0x0080\n" /* set PCL bit in new */
139 : "=&d" (old), "=&d" (new), "=Q" (ptep[PTRS_PER_PTE])
140 : "Q" (ptep[PTRS_PER_PTE]) : "cc", "memory");
145 static inline void pgste_set_unlock(pte_t *ptep, pgste_t pgste)
149 " nihh %1,0xff7f\n" /* clear PCL bit */
151 : "=Q" (ptep[PTRS_PER_PTE])
152 : "d" (pgste_val(pgste)), "Q" (ptep[PTRS_PER_PTE])
157 static inline pgste_t pgste_get(pte_t *ptep)
159 unsigned long pgste = 0;
161 pgste = *(unsigned long *)(ptep + PTRS_PER_PTE);
163 return __pgste(pgste);
166 static inline void pgste_set(pte_t *ptep, pgste_t pgste)
169 *(pgste_t *)(ptep + PTRS_PER_PTE) = pgste;
173 static inline pgste_t pgste_update_all(pte_t pte, pgste_t pgste,
174 struct mm_struct *mm)
177 unsigned long address, bits, skey;
179 if (!mm_uses_skeys(mm) || pte_val(pte) & _PAGE_INVALID)
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;
194 static inline void pgste_set_key(pte_t *ptep, pgste_t pgste, pte_t entry,
195 struct mm_struct *mm)
198 unsigned long address;
201 if (!mm_uses_skeys(mm) || pte_val(entry) & _PAGE_INVALID)
203 VM_BUG_ON(!(pte_val(*ptep) & _PAGE_INVALID));
204 address = pte_val(entry) & PAGE_MASK;
206 * Set page access key and fetch protection bit from pgste.
207 * The guest C/R information is still in the PGSTE, set real
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);
216 static inline pgste_t pgste_set_pte(pte_t *ptep, pgste_t pgste, pte_t entry)
219 if ((pte_val(entry) & _PAGE_PRESENT) &&
220 (pte_val(entry) & _PAGE_WRITE) &&
221 !(pte_val(entry) & _PAGE_INVALID)) {
222 if (!MACHINE_HAS_ESOP) {
224 * Without enhanced suppression-on-protection force
225 * the dirty bit on for all writable ptes.
227 entry = set_pte_bit(entry, __pgprot(_PAGE_DIRTY));
228 entry = clear_pte_bit(entry, __pgprot(_PAGE_PROTECT));
230 if (!(pte_val(entry) & _PAGE_PROTECT))
231 /* This pte allows write access, set user-dirty */
232 pgste_val(pgste) |= PGSTE_UC_BIT;
235 set_pte(ptep, entry);
239 static inline pgste_t pgste_pte_notify(struct mm_struct *mm,
241 pte_t *ptep, pgste_t pgste)
246 bits = pgste_val(pgste) & (PGSTE_IN_BIT | PGSTE_VSIE_BIT);
248 pgste_val(pgste) ^= bits;
249 ptep_notify(mm, addr, ptep, bits);
255 static inline pgste_t ptep_xchg_start(struct mm_struct *mm,
256 unsigned long addr, pte_t *ptep)
258 pgste_t pgste = __pgste(0);
260 if (mm_has_pgste(mm)) {
261 pgste = pgste_get_lock(ptep);
262 pgste = pgste_pte_notify(mm, addr, ptep, pgste);
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)
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 old = set_pte_bit(old, __pgprot(_PAGE_UNUSED));
280 pgste = pgste_set_pte(ptep, pgste, new);
281 pgste_set_unlock(ptep, pgste);
288 pte_t ptep_xchg_direct(struct mm_struct *mm, unsigned long addr,
289 pte_t *ptep, pte_t new)
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);
303 EXPORT_SYMBOL(ptep_xchg_direct);
306 * Caller must check that new PTE only differs in _PAGE_PROTECT HW bit, so that
307 * RDP can be used instead of IPTE. See also comments at pte_allow_rdp().
309 void ptep_reset_dat_prot(struct mm_struct *mm, unsigned long addr, pte_t *ptep,
313 atomic_inc(&mm->context.flush_count);
314 if (cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
315 __ptep_rdp(addr, ptep, 0, 0, 1);
317 __ptep_rdp(addr, ptep, 0, 0, 0);
319 * PTE is not invalidated by RDP, only _PAGE_PROTECT is cleared. That
320 * means it is still valid and active, and must not be changed according
321 * to the architecture. But writing a new value that only differs in SW
325 atomic_dec(&mm->context.flush_count);
328 EXPORT_SYMBOL(ptep_reset_dat_prot);
330 pte_t ptep_xchg_lazy(struct mm_struct *mm, unsigned long addr,
331 pte_t *ptep, pte_t new)
338 pgste = ptep_xchg_start(mm, addr, ptep);
339 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
340 old = ptep_flush_lazy(mm, addr, ptep, nodat);
341 old = ptep_xchg_commit(mm, addr, ptep, pgste, old, new);
345 EXPORT_SYMBOL(ptep_xchg_lazy);
347 pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr,
353 struct mm_struct *mm = vma->vm_mm;
356 pgste = ptep_xchg_start(mm, addr, ptep);
357 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
358 old = ptep_flush_lazy(mm, addr, ptep, nodat);
359 if (mm_has_pgste(mm)) {
360 pgste = pgste_update_all(old, pgste, mm);
361 pgste_set(ptep, pgste);
366 void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr,
367 pte_t *ptep, pte_t old_pte, pte_t pte)
370 struct mm_struct *mm = vma->vm_mm;
373 pte = clear_pte_bit(pte, __pgprot(_PAGE_NOEXEC));
374 if (mm_has_pgste(mm)) {
375 pgste = pgste_get(ptep);
376 pgste_set_key(ptep, pgste, pte, mm);
377 pgste = pgste_set_pte(ptep, pgste, pte);
378 pgste_set_unlock(ptep, pgste);
385 static inline void pmdp_idte_local(struct mm_struct *mm,
386 unsigned long addr, pmd_t *pmdp)
388 if (MACHINE_HAS_TLB_GUEST)
389 __pmdp_idte(addr, pmdp, IDTE_NODAT | IDTE_GUEST_ASCE,
390 mm->context.asce, IDTE_LOCAL);
392 __pmdp_idte(addr, pmdp, 0, 0, IDTE_LOCAL);
393 if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m)
394 gmap_pmdp_idte_local(mm, addr);
397 static inline void pmdp_idte_global(struct mm_struct *mm,
398 unsigned long addr, pmd_t *pmdp)
400 if (MACHINE_HAS_TLB_GUEST) {
401 __pmdp_idte(addr, pmdp, IDTE_NODAT | IDTE_GUEST_ASCE,
402 mm->context.asce, IDTE_GLOBAL);
403 if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m)
404 gmap_pmdp_idte_global(mm, addr);
405 } else if (MACHINE_HAS_IDTE) {
406 __pmdp_idte(addr, pmdp, 0, 0, IDTE_GLOBAL);
407 if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m)
408 gmap_pmdp_idte_global(mm, addr);
411 if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m)
412 gmap_pmdp_csp(mm, addr);
416 static inline pmd_t pmdp_flush_direct(struct mm_struct *mm,
417 unsigned long addr, pmd_t *pmdp)
422 if (pmd_val(old) & _SEGMENT_ENTRY_INVALID)
424 atomic_inc(&mm->context.flush_count);
425 if (MACHINE_HAS_TLB_LC &&
426 cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
427 pmdp_idte_local(mm, addr, pmdp);
429 pmdp_idte_global(mm, addr, pmdp);
430 atomic_dec(&mm->context.flush_count);
434 static inline pmd_t pmdp_flush_lazy(struct mm_struct *mm,
435 unsigned long addr, pmd_t *pmdp)
440 if (pmd_val(old) & _SEGMENT_ENTRY_INVALID)
442 atomic_inc(&mm->context.flush_count);
443 if (cpumask_equal(&mm->context.cpu_attach_mask,
444 cpumask_of(smp_processor_id()))) {
445 set_pmd(pmdp, set_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_INVALID)));
446 mm->context.flush_mm = 1;
447 if (mm_has_pgste(mm))
448 gmap_pmdp_invalidate(mm, addr);
450 pmdp_idte_global(mm, addr, pmdp);
452 atomic_dec(&mm->context.flush_count);
457 static int pmd_lookup(struct mm_struct *mm, unsigned long addr, pmd_t **pmdp)
459 struct vm_area_struct *vma;
464 /* We need a valid VMA, otherwise this is clearly a fault. */
465 vma = vma_lookup(mm, addr);
469 pgd = pgd_offset(mm, addr);
470 if (!pgd_present(*pgd))
473 p4d = p4d_offset(pgd, addr);
474 if (!p4d_present(*p4d))
477 pud = pud_offset(p4d, addr);
478 if (!pud_present(*pud))
481 /* Large PUDs are not supported yet. */
485 *pmdp = pmd_offset(pud, addr);
490 pmd_t pmdp_xchg_direct(struct mm_struct *mm, unsigned long addr,
491 pmd_t *pmdp, pmd_t new)
496 old = pmdp_flush_direct(mm, addr, pmdp);
501 EXPORT_SYMBOL(pmdp_xchg_direct);
503 pmd_t pmdp_xchg_lazy(struct mm_struct *mm, unsigned long addr,
504 pmd_t *pmdp, pmd_t new)
509 old = pmdp_flush_lazy(mm, addr, pmdp);
514 EXPORT_SYMBOL(pmdp_xchg_lazy);
516 static inline void pudp_idte_local(struct mm_struct *mm,
517 unsigned long addr, pud_t *pudp)
519 if (MACHINE_HAS_TLB_GUEST)
520 __pudp_idte(addr, pudp, IDTE_NODAT | IDTE_GUEST_ASCE,
521 mm->context.asce, IDTE_LOCAL);
523 __pudp_idte(addr, pudp, 0, 0, IDTE_LOCAL);
526 static inline void pudp_idte_global(struct mm_struct *mm,
527 unsigned long addr, pud_t *pudp)
529 if (MACHINE_HAS_TLB_GUEST)
530 __pudp_idte(addr, pudp, IDTE_NODAT | IDTE_GUEST_ASCE,
531 mm->context.asce, IDTE_GLOBAL);
532 else if (MACHINE_HAS_IDTE)
533 __pudp_idte(addr, pudp, 0, 0, IDTE_GLOBAL);
536 * Invalid bit position is the same for pmd and pud, so we can
537 * re-use _pmd_csp() here
539 __pmdp_csp((pmd_t *) pudp);
542 static inline pud_t pudp_flush_direct(struct mm_struct *mm,
543 unsigned long addr, pud_t *pudp)
548 if (pud_val(old) & _REGION_ENTRY_INVALID)
550 atomic_inc(&mm->context.flush_count);
551 if (MACHINE_HAS_TLB_LC &&
552 cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
553 pudp_idte_local(mm, addr, pudp);
555 pudp_idte_global(mm, addr, pudp);
556 atomic_dec(&mm->context.flush_count);
560 pud_t pudp_xchg_direct(struct mm_struct *mm, unsigned long addr,
561 pud_t *pudp, pud_t new)
566 old = pudp_flush_direct(mm, addr, pudp);
571 EXPORT_SYMBOL(pudp_xchg_direct);
573 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
574 void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
577 struct list_head *lh = (struct list_head *) pgtable;
579 assert_spin_locked(pmd_lockptr(mm, pmdp));
582 if (!pmd_huge_pte(mm, pmdp))
585 list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
586 pmd_huge_pte(mm, pmdp) = pgtable;
589 pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
591 struct list_head *lh;
595 assert_spin_locked(pmd_lockptr(mm, pmdp));
598 pgtable = pmd_huge_pte(mm, pmdp);
599 lh = (struct list_head *) pgtable;
601 pmd_huge_pte(mm, pmdp) = NULL;
603 pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
606 ptep = (pte_t *) pgtable;
607 set_pte(ptep, __pte(_PAGE_INVALID));
609 set_pte(ptep, __pte(_PAGE_INVALID));
612 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
615 void ptep_set_pte_at(struct mm_struct *mm, unsigned long addr,
616 pte_t *ptep, pte_t entry)
620 /* the mm_has_pgste() check is done in set_pte_at() */
622 pgste = pgste_get_lock(ptep);
623 pgste_val(pgste) &= ~_PGSTE_GPS_ZERO;
624 pgste_set_key(ptep, pgste, entry, mm);
625 pgste = pgste_set_pte(ptep, pgste, entry);
626 pgste_set_unlock(ptep, pgste);
630 void ptep_set_notify(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
635 pgste = pgste_get_lock(ptep);
636 pgste_val(pgste) |= PGSTE_IN_BIT;
637 pgste_set_unlock(ptep, pgste);
642 * ptep_force_prot - change access rights of a locked pte
643 * @mm: pointer to the process mm_struct
644 * @addr: virtual address in the guest address space
645 * @ptep: pointer to the page table entry
646 * @prot: indicates guest access rights: PROT_NONE, PROT_READ or PROT_WRITE
647 * @bit: pgste bit to set (e.g. for notification)
649 * Returns 0 if the access rights were changed and -EAGAIN if the current
650 * and requested access rights are incompatible.
652 int ptep_force_prot(struct mm_struct *mm, unsigned long addr,
653 pte_t *ptep, int prot, unsigned long bit)
657 int pte_i, pte_p, nodat;
659 pgste = pgste_get_lock(ptep);
661 /* Check pte entry after all locks have been acquired */
662 pte_i = pte_val(entry) & _PAGE_INVALID;
663 pte_p = pte_val(entry) & _PAGE_PROTECT;
664 if ((pte_i && (prot != PROT_NONE)) ||
665 (pte_p && (prot & PROT_WRITE))) {
666 pgste_set_unlock(ptep, pgste);
669 /* Change access rights and set pgste bit */
670 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
671 if (prot == PROT_NONE && !pte_i) {
672 ptep_flush_direct(mm, addr, ptep, nodat);
673 pgste = pgste_update_all(entry, pgste, mm);
674 entry = set_pte_bit(entry, __pgprot(_PAGE_INVALID));
676 if (prot == PROT_READ && !pte_p) {
677 ptep_flush_direct(mm, addr, ptep, nodat);
678 entry = clear_pte_bit(entry, __pgprot(_PAGE_INVALID));
679 entry = set_pte_bit(entry, __pgprot(_PAGE_PROTECT));
681 pgste_val(pgste) |= bit;
682 pgste = pgste_set_pte(ptep, pgste, entry);
683 pgste_set_unlock(ptep, pgste);
687 int ptep_shadow_pte(struct mm_struct *mm, unsigned long saddr,
688 pte_t *sptep, pte_t *tptep, pte_t pte)
690 pgste_t spgste, tpgste;
694 if (!(pte_val(*tptep) & _PAGE_INVALID))
695 return 0; /* already shadowed */
696 spgste = pgste_get_lock(sptep);
698 if (!(pte_val(spte) & _PAGE_INVALID) &&
699 !((pte_val(spte) & _PAGE_PROTECT) &&
700 !(pte_val(pte) & _PAGE_PROTECT))) {
701 pgste_val(spgste) |= PGSTE_VSIE_BIT;
702 tpgste = pgste_get_lock(tptep);
703 tpte = __pte((pte_val(spte) & PAGE_MASK) |
704 (pte_val(pte) & _PAGE_PROTECT));
705 /* don't touch the storage key - it belongs to parent pgste */
706 tpgste = pgste_set_pte(tptep, tpgste, tpte);
707 pgste_set_unlock(tptep, tpgste);
710 pgste_set_unlock(sptep, spgste);
714 void ptep_unshadow_pte(struct mm_struct *mm, unsigned long saddr, pte_t *ptep)
719 pgste = pgste_get_lock(ptep);
720 /* notifier is called by the caller */
721 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
722 ptep_flush_direct(mm, saddr, ptep, nodat);
723 /* don't touch the storage key - it belongs to parent pgste */
724 pgste = pgste_set_pte(ptep, pgste, __pte(_PAGE_INVALID));
725 pgste_set_unlock(ptep, pgste);
728 static void ptep_zap_swap_entry(struct mm_struct *mm, swp_entry_t entry)
730 if (!non_swap_entry(entry))
731 dec_mm_counter(mm, MM_SWAPENTS);
732 else if (is_migration_entry(entry)) {
733 struct page *page = pfn_swap_entry_to_page(entry);
735 dec_mm_counter(mm, mm_counter(page));
737 free_swap_and_cache(entry);
740 void ptep_zap_unused(struct mm_struct *mm, unsigned long addr,
741 pte_t *ptep, int reset)
743 unsigned long pgstev;
747 /* Zap unused and logically-zero pages */
749 pgste = pgste_get_lock(ptep);
750 pgstev = pgste_val(pgste);
752 if (!reset && pte_swap(pte) &&
753 ((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED ||
754 (pgstev & _PGSTE_GPS_ZERO))) {
755 ptep_zap_swap_entry(mm, pte_to_swp_entry(pte));
756 pte_clear(mm, addr, ptep);
759 pgste_val(pgste) &= ~_PGSTE_GPS_USAGE_MASK;
760 pgste_set_unlock(ptep, pgste);
764 void ptep_zap_key(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
769 /* Clear storage key ACC and F, but set R/C */
771 pgste = pgste_get_lock(ptep);
772 pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT);
773 pgste_val(pgste) |= PGSTE_GR_BIT | PGSTE_GC_BIT;
774 ptev = pte_val(*ptep);
775 if (!(ptev & _PAGE_INVALID) && (ptev & _PAGE_WRITE))
776 page_set_storage_key(ptev & PAGE_MASK, PAGE_DEFAULT_KEY, 0);
777 pgste_set_unlock(ptep, pgste);
782 * Test and reset if a guest page is dirty
784 bool ptep_test_and_clear_uc(struct mm_struct *mm, unsigned long addr,
792 pgste = pgste_get_lock(ptep);
793 dirty = !!(pgste_val(pgste) & PGSTE_UC_BIT);
794 pgste_val(pgste) &= ~PGSTE_UC_BIT;
796 if (dirty && (pte_val(pte) & _PAGE_PRESENT)) {
797 pgste = pgste_pte_notify(mm, addr, ptep, pgste);
798 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
799 ptep_ipte_global(mm, addr, ptep, nodat);
800 if (MACHINE_HAS_ESOP || !(pte_val(pte) & _PAGE_WRITE))
801 pte = set_pte_bit(pte, __pgprot(_PAGE_PROTECT));
803 pte = set_pte_bit(pte, __pgprot(_PAGE_INVALID));
806 pgste_set_unlock(ptep, pgste);
809 EXPORT_SYMBOL_GPL(ptep_test_and_clear_uc);
811 int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
812 unsigned char key, bool nq)
814 unsigned long keyul, paddr;
821 * If we don't have a PTE table and if there is no huge page mapped,
822 * we can ignore attempts to set the key to 0, because it already is 0.
824 switch (pmd_lookup(mm, addr, &pmdp)) {
826 return key ? -EFAULT : 0;
833 ptl = pmd_lock(mm, pmdp);
834 if (!pmd_present(*pmdp)) {
836 return key ? -EFAULT : 0;
839 if (pmd_large(*pmdp)) {
840 paddr = pmd_val(*pmdp) & HPAGE_MASK;
841 paddr |= addr & ~HPAGE_MASK;
843 * Huge pmds need quiescing operations, they are
846 page_set_storage_key(paddr, key, 1);
852 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
855 new = old = pgste_get_lock(ptep);
856 pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
857 PGSTE_ACC_BITS | PGSTE_FP_BIT);
858 keyul = (unsigned long) key;
859 pgste_val(new) |= (keyul & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
860 pgste_val(new) |= (keyul & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
861 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
862 unsigned long bits, skey;
864 paddr = pte_val(*ptep) & PAGE_MASK;
865 skey = (unsigned long) page_get_storage_key(paddr);
866 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
867 skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
868 /* Set storage key ACC and FP */
869 page_set_storage_key(paddr, skey, !nq);
870 /* Merge host changed & referenced into pgste */
871 pgste_val(new) |= bits << 52;
873 /* changing the guest storage key is considered a change of the page */
874 if ((pgste_val(new) ^ pgste_val(old)) &
875 (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
876 pgste_val(new) |= PGSTE_UC_BIT;
878 pgste_set_unlock(ptep, new);
879 pte_unmap_unlock(ptep, ptl);
882 EXPORT_SYMBOL(set_guest_storage_key);
885 * Conditionally set a guest storage key (handling csske).
886 * oldkey will be updated when either mr or mc is set and a pointer is given.
888 * Returns 0 if a guests storage key update wasn't necessary, 1 if the guest
889 * storage key was updated and -EFAULT on access errors.
891 int cond_set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
892 unsigned char key, unsigned char *oldkey,
893 bool nq, bool mr, bool mc)
895 unsigned char tmp, mask = _PAGE_ACC_BITS | _PAGE_FP_BIT;
898 /* we can drop the pgste lock between getting and setting the key */
900 rc = get_guest_storage_key(current->mm, addr, &tmp);
906 mask |= _PAGE_REFERENCED;
908 mask |= _PAGE_CHANGED;
909 if (!((tmp ^ key) & mask))
912 rc = set_guest_storage_key(current->mm, addr, key, nq);
913 return rc < 0 ? rc : 1;
915 EXPORT_SYMBOL(cond_set_guest_storage_key);
918 * Reset a guest reference bit (rrbe), returning the reference and changed bit.
920 * Returns < 0 in case of error, otherwise the cc to be reported to the guest.
922 int reset_guest_reference_bit(struct mm_struct *mm, unsigned long addr)
932 * If we don't have a PTE table and if there is no huge page mapped,
933 * the storage key is 0 and there is nothing for us to do.
935 switch (pmd_lookup(mm, addr, &pmdp)) {
944 ptl = pmd_lock(mm, pmdp);
945 if (!pmd_present(*pmdp)) {
950 if (pmd_large(*pmdp)) {
951 paddr = pmd_val(*pmdp) & HPAGE_MASK;
952 paddr |= addr & ~HPAGE_MASK;
953 cc = page_reset_referenced(paddr);
959 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
962 new = old = pgste_get_lock(ptep);
963 /* Reset guest reference bit only */
964 pgste_val(new) &= ~PGSTE_GR_BIT;
966 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
967 paddr = pte_val(*ptep) & PAGE_MASK;
968 cc = page_reset_referenced(paddr);
969 /* Merge real referenced bit into host-set */
970 pgste_val(new) |= ((unsigned long) cc << 53) & PGSTE_HR_BIT;
972 /* Reflect guest's logical view, not physical */
973 cc |= (pgste_val(old) & (PGSTE_GR_BIT | PGSTE_GC_BIT)) >> 49;
974 /* Changing the guest storage key is considered a change of the page */
975 if ((pgste_val(new) ^ pgste_val(old)) & PGSTE_GR_BIT)
976 pgste_val(new) |= PGSTE_UC_BIT;
978 pgste_set_unlock(ptep, new);
979 pte_unmap_unlock(ptep, ptl);
982 EXPORT_SYMBOL(reset_guest_reference_bit);
984 int get_guest_storage_key(struct mm_struct *mm, unsigned long addr,
994 * If we don't have a PTE table and if there is no huge page mapped,
995 * the storage key is 0.
999 switch (pmd_lookup(mm, addr, &pmdp)) {
1008 ptl = pmd_lock(mm, pmdp);
1009 if (!pmd_present(*pmdp)) {
1014 if (pmd_large(*pmdp)) {
1015 paddr = pmd_val(*pmdp) & HPAGE_MASK;
1016 paddr |= addr & ~HPAGE_MASK;
1017 *key = page_get_storage_key(paddr);
1023 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
1026 pgste = pgste_get_lock(ptep);
1027 *key = (pgste_val(pgste) & (PGSTE_ACC_BITS | PGSTE_FP_BIT)) >> 56;
1028 paddr = pte_val(*ptep) & PAGE_MASK;
1029 if (!(pte_val(*ptep) & _PAGE_INVALID))
1030 *key = page_get_storage_key(paddr);
1031 /* Reflect guest's logical view, not physical */
1032 *key |= (pgste_val(pgste) & (PGSTE_GR_BIT | PGSTE_GC_BIT)) >> 48;
1033 pgste_set_unlock(ptep, pgste);
1034 pte_unmap_unlock(ptep, ptl);
1037 EXPORT_SYMBOL(get_guest_storage_key);
1040 * pgste_perform_essa - perform ESSA actions on the PGSTE.
1041 * @mm: the memory context. It must have PGSTEs, no check is performed here!
1042 * @hva: the host virtual address of the page whose PGSTE is to be processed
1043 * @orc: the specific action to perform, see the ESSA_SET_* macros.
1044 * @oldpte: the PTE will be saved there if the pointer is not NULL.
1045 * @oldpgste: the old PGSTE will be saved there if the pointer is not NULL.
1047 * Return: 1 if the page is to be added to the CBRL, otherwise 0,
1048 * or < 0 in case of error. -EINVAL is returned for invalid values
1049 * of orc, -EFAULT for invalid addresses.
1051 int pgste_perform_essa(struct mm_struct *mm, unsigned long hva, int orc,
1052 unsigned long *oldpte, unsigned long *oldpgste)
1054 struct vm_area_struct *vma;
1055 unsigned long pgstev;
1061 WARN_ON_ONCE(orc > ESSA_MAX);
1062 if (unlikely(orc > ESSA_MAX))
1065 vma = vma_lookup(mm, hva);
1066 if (!vma || is_vm_hugetlb_page(vma))
1068 ptep = get_locked_pte(mm, hva, &ptl);
1069 if (unlikely(!ptep))
1071 pgste = pgste_get_lock(ptep);
1072 pgstev = pgste_val(pgste);
1074 *oldpte = pte_val(*ptep);
1079 case ESSA_GET_STATE:
1081 case ESSA_SET_STABLE:
1082 pgstev &= ~(_PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT);
1083 pgstev |= _PGSTE_GPS_USAGE_STABLE;
1085 case ESSA_SET_UNUSED:
1086 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1087 pgstev |= _PGSTE_GPS_USAGE_UNUSED;
1088 if (pte_val(*ptep) & _PAGE_INVALID)
1091 case ESSA_SET_VOLATILE:
1092 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1093 pgstev |= _PGSTE_GPS_USAGE_VOLATILE;
1094 if (pte_val(*ptep) & _PAGE_INVALID)
1097 case ESSA_SET_POT_VOLATILE:
1098 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1099 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
1100 pgstev |= _PGSTE_GPS_USAGE_POT_VOLATILE;
1103 if (pgstev & _PGSTE_GPS_ZERO) {
1104 pgstev |= _PGSTE_GPS_USAGE_VOLATILE;
1107 if (!(pgstev & PGSTE_GC_BIT)) {
1108 pgstev |= _PGSTE_GPS_USAGE_VOLATILE;
1113 case ESSA_SET_STABLE_RESIDENT:
1114 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1115 pgstev |= _PGSTE_GPS_USAGE_STABLE;
1117 * Since the resident state can go away any time after this
1118 * call, we will not make this page resident. We can revisit
1119 * this decision if a guest will ever start using this.
1122 case ESSA_SET_STABLE_IF_RESIDENT:
1123 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
1124 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1125 pgstev |= _PGSTE_GPS_USAGE_STABLE;
1128 case ESSA_SET_STABLE_NODAT:
1129 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1130 pgstev |= _PGSTE_GPS_USAGE_STABLE | _PGSTE_GPS_NODAT;
1133 /* we should never get here! */
1136 /* If we are discarding a page, set it to logical zero */
1138 pgstev |= _PGSTE_GPS_ZERO;
1140 pgste_val(pgste) = pgstev;
1141 pgste_set_unlock(ptep, pgste);
1142 pte_unmap_unlock(ptep, ptl);
1145 EXPORT_SYMBOL(pgste_perform_essa);
1148 * set_pgste_bits - set specific PGSTE bits.
1149 * @mm: the memory context. It must have PGSTEs, no check is performed here!
1150 * @hva: the host virtual address of the page whose PGSTE is to be processed
1151 * @bits: a bitmask representing the bits that will be touched
1152 * @value: the values of the bits to be written. Only the bits in the mask
1155 * Return: 0 on success, < 0 in case of error.
1157 int set_pgste_bits(struct mm_struct *mm, unsigned long hva,
1158 unsigned long bits, unsigned long value)
1160 struct vm_area_struct *vma;
1165 vma = vma_lookup(mm, hva);
1166 if (!vma || is_vm_hugetlb_page(vma))
1168 ptep = get_locked_pte(mm, hva, &ptl);
1169 if (unlikely(!ptep))
1171 new = pgste_get_lock(ptep);
1173 pgste_val(new) &= ~bits;
1174 pgste_val(new) |= value & bits;
1176 pgste_set_unlock(ptep, new);
1177 pte_unmap_unlock(ptep, ptl);
1180 EXPORT_SYMBOL(set_pgste_bits);
1183 * get_pgste - get the current PGSTE for the given address.
1184 * @mm: the memory context. It must have PGSTEs, no check is performed here!
1185 * @hva: the host virtual address of the page whose PGSTE is to be processed
1186 * @pgstep: will be written with the current PGSTE for the given address.
1188 * Return: 0 on success, < 0 in case of error.
1190 int get_pgste(struct mm_struct *mm, unsigned long hva, unsigned long *pgstep)
1192 struct vm_area_struct *vma;
1196 vma = vma_lookup(mm, hva);
1197 if (!vma || is_vm_hugetlb_page(vma))
1199 ptep = get_locked_pte(mm, hva, &ptl);
1200 if (unlikely(!ptep))
1202 *pgstep = pgste_val(pgste_get(ptep));
1203 pte_unmap_unlock(ptep, ptl);
1206 EXPORT_SYMBOL(get_pgste);