Greg Kroah-Hartman | ac41aae | 2017-11-24 15:00:35 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 2 | /* |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 3 | * Copyright IBM Corp. 2007, 2011 |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/gfp.h> |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 11 | #include <linux/mm.h> |
| 12 | #include <linux/swap.h> |
| 13 | #include <linux/smp.h> |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 14 | #include <linux/spinlock.h> |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 15 | #include <linux/rcupdate.h> |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 16 | #include <linux/slab.h> |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 17 | #include <linux/swapops.h> |
Martin Schwidefsky | 0b46e0a | 2015-04-15 13:23:26 +0200 | [diff] [blame] | 18 | #include <linux/sysctl.h> |
Dominik Dingel | 3ac8e38 | 2014-10-23 12:09:17 +0200 | [diff] [blame] | 19 | #include <linux/ksm.h> |
| 20 | #include <linux/mman.h> |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 21 | |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 22 | #include <asm/tlb.h> |
| 23 | #include <asm/tlbflush.h> |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 24 | #include <asm/mmu_context.h> |
Claudio Imbrenda | 2d42f94 | 2017-04-20 10:03:45 +0200 | [diff] [blame] | 25 | #include <asm/page-states.h> |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 26 | |
Niklas Schnelle | b02002c | 2020-07-13 14:12:49 +0200 | [diff] [blame] | 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 | |
| 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 | |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 47 | static inline void ptep_ipte_local(struct mm_struct *mm, unsigned long addr, |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 48 | pte_t *ptep, int nodat) |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 49 | { |
| 50 | unsigned long opt, asce; |
| 51 | |
| 52 | if (MACHINE_HAS_TLB_GUEST) { |
| 53 | opt = 0; |
| 54 | asce = READ_ONCE(mm->context.gmap_asce); |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 55 | if (asce == 0UL || nodat) |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 56 | opt |= IPTE_NODAT; |
Martin Schwidefsky | 28c807e | 2016-07-26 16:00:22 +0200 | [diff] [blame] | 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); |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 62 | } else { |
Martin Schwidefsky | 28c807e | 2016-07-26 16:00:22 +0200 | [diff] [blame] | 63 | __ptep_ipte(addr, ptep, 0, 0, IPTE_LOCAL); |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | static inline void ptep_ipte_global(struct mm_struct *mm, unsigned long addr, |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 68 | pte_t *ptep, int nodat) |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 69 | { |
| 70 | unsigned long opt, asce; |
| 71 | |
| 72 | if (MACHINE_HAS_TLB_GUEST) { |
| 73 | opt = 0; |
| 74 | asce = READ_ONCE(mm->context.gmap_asce); |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 75 | if (asce == 0UL || nodat) |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 76 | opt |= IPTE_NODAT; |
Martin Schwidefsky | 28c807e | 2016-07-26 16:00:22 +0200 | [diff] [blame] | 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); |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 82 | } else { |
Martin Schwidefsky | 28c807e | 2016-07-26 16:00:22 +0200 | [diff] [blame] | 83 | __ptep_ipte(addr, ptep, 0, 0, IPTE_GLOBAL); |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 87 | static inline pte_t ptep_flush_direct(struct mm_struct *mm, |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 88 | unsigned long addr, pte_t *ptep, |
| 89 | int nodat) |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 90 | { |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 91 | pte_t old; |
| 92 | |
| 93 | old = *ptep; |
| 94 | if (unlikely(pte_val(old) & _PAGE_INVALID)) |
| 95 | return old; |
Martin Schwidefsky | 64f31d5 | 2016-05-25 09:45:26 +0200 | [diff] [blame] | 96 | atomic_inc(&mm->context.flush_count); |
| 97 | if (MACHINE_HAS_TLB_LC && |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 98 | cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id()))) |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 99 | ptep_ipte_local(mm, addr, ptep, nodat); |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 100 | else |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 101 | ptep_ipte_global(mm, addr, ptep, nodat); |
Martin Schwidefsky | 64f31d5 | 2016-05-25 09:45:26 +0200 | [diff] [blame] | 102 | atomic_dec(&mm->context.flush_count); |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 103 | return old; |
| 104 | } |
| 105 | |
| 106 | static inline pte_t ptep_flush_lazy(struct mm_struct *mm, |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 107 | unsigned long addr, pte_t *ptep, |
| 108 | int nodat) |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 109 | { |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 110 | pte_t old; |
| 111 | |
| 112 | old = *ptep; |
| 113 | if (unlikely(pte_val(old) & _PAGE_INVALID)) |
| 114 | return old; |
Martin Schwidefsky | 64f31d5 | 2016-05-25 09:45:26 +0200 | [diff] [blame] | 115 | atomic_inc(&mm->context.flush_count); |
| 116 | if (cpumask_equal(&mm->context.cpu_attach_mask, |
| 117 | cpumask_of(smp_processor_id()))) { |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 118 | pte_val(*ptep) |= _PAGE_INVALID; |
| 119 | mm->context.flush_mm = 1; |
| 120 | } else |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 121 | ptep_ipte_global(mm, addr, ptep, nodat); |
Martin Schwidefsky | 64f31d5 | 2016-05-25 09:45:26 +0200 | [diff] [blame] | 122 | atomic_dec(&mm->context.flush_count); |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 123 | return old; |
| 124 | } |
| 125 | |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 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 | |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 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 | |
| 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"); |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 154 | #endif |
| 155 | } |
| 156 | |
| 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 | |
| 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 | |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 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 | |
Janosch Frank | 55531b7 | 2018-02-15 16:33:47 +0100 | [diff] [blame] | 179 | if (!mm_uses_skeys(mm) || pte_val(pte) & _PAGE_INVALID) |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 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 | |
| 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 | |
Janosch Frank | 55531b7 | 2018-02-15 16:33:47 +0100 | [diff] [blame] | 201 | if (!mm_uses_skeys(mm) || pte_val(entry) & _PAGE_INVALID) |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 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 | |
| 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 | |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 239 | static inline pgste_t pgste_pte_notify(struct mm_struct *mm, |
| 240 | unsigned long addr, |
| 241 | pte_t *ptep, pgste_t pgste) |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 242 | { |
| 243 | #ifdef CONFIG_PGSTE |
Martin Schwidefsky | 4be130a | 2016-03-08 12:12:18 +0100 | [diff] [blame] | 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); |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 250 | } |
| 251 | #endif |
| 252 | return pgste; |
| 253 | } |
| 254 | |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 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); |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 262 | pgste = pgste_pte_notify(mm, addr, ptep, pgste); |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 263 | } |
| 264 | return pgste; |
| 265 | } |
| 266 | |
Christian Borntraeger | 0d6da87 | 2017-01-23 22:59:44 +0100 | [diff] [blame] | 267 | static inline pte_t ptep_xchg_commit(struct mm_struct *mm, |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 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 | } |
Christian Borntraeger | 0d6da87 | 2017-01-23 22:59:44 +0100 | [diff] [blame] | 285 | return old; |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 286 | } |
| 287 | |
| 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; |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 293 | int nodat; |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 294 | |
Martin Schwidefsky | a980940 | 2016-06-06 10:30:45 +0200 | [diff] [blame] | 295 | preempt_disable(); |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 296 | pgste = ptep_xchg_start(mm, addr, ptep); |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 297 | nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT); |
| 298 | old = ptep_flush_direct(mm, addr, ptep, nodat); |
Christian Borntraeger | 0d6da87 | 2017-01-23 22:59:44 +0100 | [diff] [blame] | 299 | old = ptep_xchg_commit(mm, addr, ptep, pgste, old, new); |
Martin Schwidefsky | a980940 | 2016-06-06 10:30:45 +0200 | [diff] [blame] | 300 | preempt_enable(); |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 301 | return old; |
| 302 | } |
| 303 | EXPORT_SYMBOL(ptep_xchg_direct); |
| 304 | |
| 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; |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 310 | int nodat; |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 311 | |
Martin Schwidefsky | a980940 | 2016-06-06 10:30:45 +0200 | [diff] [blame] | 312 | preempt_disable(); |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 313 | pgste = ptep_xchg_start(mm, addr, ptep); |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 314 | nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT); |
| 315 | old = ptep_flush_lazy(mm, addr, ptep, nodat); |
Christian Borntraeger | 0d6da87 | 2017-01-23 22:59:44 +0100 | [diff] [blame] | 316 | old = ptep_xchg_commit(mm, addr, ptep, pgste, old, new); |
Martin Schwidefsky | a980940 | 2016-06-06 10:30:45 +0200 | [diff] [blame] | 317 | preempt_enable(); |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 318 | return old; |
| 319 | } |
| 320 | EXPORT_SYMBOL(ptep_xchg_lazy); |
| 321 | |
Aneesh Kumar K.V | 0cbe3e2 | 2019-03-05 15:46:26 -0800 | [diff] [blame] | 322 | pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr, |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 323 | pte_t *ptep) |
| 324 | { |
| 325 | pgste_t pgste; |
| 326 | pte_t old; |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 327 | int nodat; |
Aneesh Kumar K.V | 0cbe3e2 | 2019-03-05 15:46:26 -0800 | [diff] [blame] | 328 | struct mm_struct *mm = vma->vm_mm; |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 329 | |
Martin Schwidefsky | a980940 | 2016-06-06 10:30:45 +0200 | [diff] [blame] | 330 | preempt_disable(); |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 331 | pgste = ptep_xchg_start(mm, addr, ptep); |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 332 | nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT); |
| 333 | old = ptep_flush_lazy(mm, addr, ptep, nodat); |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 334 | if (mm_has_pgste(mm)) { |
| 335 | pgste = pgste_update_all(old, pgste, mm); |
| 336 | pgste_set(ptep, pgste); |
| 337 | } |
| 338 | return old; |
| 339 | } |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 340 | |
Aneesh Kumar K.V | 0cbe3e2 | 2019-03-05 15:46:26 -0800 | [diff] [blame] | 341 | void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, |
Aneesh Kumar K.V | 04a8645 | 2019-03-05 15:46:29 -0800 | [diff] [blame] | 342 | pte_t *ptep, pte_t old_pte, pte_t pte) |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 343 | { |
| 344 | pgste_t pgste; |
Aneesh Kumar K.V | 0cbe3e2 | 2019-03-05 15:46:26 -0800 | [diff] [blame] | 345 | struct mm_struct *mm = vma->vm_mm; |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 346 | |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 347 | if (!MACHINE_HAS_NX) |
| 348 | pte_val(pte) &= ~_PAGE_NOEXEC; |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 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 | } |
Martin Schwidefsky | a980940 | 2016-06-06 10:30:45 +0200 | [diff] [blame] | 357 | preempt_enable(); |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 358 | } |
Martin Schwidefsky | ebde765 | 2016-03-08 11:08:09 +0100 | [diff] [blame] | 359 | |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 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) |
Martin Schwidefsky | 28c807e | 2016-07-26 16:00:22 +0200 | [diff] [blame] | 364 | __pmdp_idte(addr, pmdp, IDTE_NODAT | IDTE_GUEST_ASCE, |
| 365 | mm->context.asce, IDTE_LOCAL); |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 366 | else |
Martin Schwidefsky | 28c807e | 2016-07-26 16:00:22 +0200 | [diff] [blame] | 367 | __pmdp_idte(addr, pmdp, 0, 0, IDTE_LOCAL); |
Janosch Frank | a9e00d8 | 2018-07-13 11:28:37 +0100 | [diff] [blame] | 368 | if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m) |
Janosch Frank | 6a37627 | 2018-07-13 11:28:22 +0100 | [diff] [blame] | 369 | gmap_pmdp_idte_local(mm, addr); |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static inline void pmdp_idte_global(struct mm_struct *mm, |
| 373 | unsigned long addr, pmd_t *pmdp) |
| 374 | { |
Janosch Frank | 6a37627 | 2018-07-13 11:28:22 +0100 | [diff] [blame] | 375 | if (MACHINE_HAS_TLB_GUEST) { |
Martin Schwidefsky | 28c807e | 2016-07-26 16:00:22 +0200 | [diff] [blame] | 376 | __pmdp_idte(addr, pmdp, IDTE_NODAT | IDTE_GUEST_ASCE, |
| 377 | mm->context.asce, IDTE_GLOBAL); |
Janosch Frank | a9e00d8 | 2018-07-13 11:28:37 +0100 | [diff] [blame] | 378 | if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m) |
Janosch Frank | 6a37627 | 2018-07-13 11:28:22 +0100 | [diff] [blame] | 379 | gmap_pmdp_idte_global(mm, addr); |
| 380 | } else if (MACHINE_HAS_IDTE) { |
Martin Schwidefsky | 28c807e | 2016-07-26 16:00:22 +0200 | [diff] [blame] | 381 | __pmdp_idte(addr, pmdp, 0, 0, IDTE_GLOBAL); |
Janosch Frank | a9e00d8 | 2018-07-13 11:28:37 +0100 | [diff] [blame] | 382 | if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m) |
Janosch Frank | 6a37627 | 2018-07-13 11:28:22 +0100 | [diff] [blame] | 383 | gmap_pmdp_idte_global(mm, addr); |
| 384 | } else { |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 385 | __pmdp_csp(pmdp); |
Janosch Frank | a9e00d8 | 2018-07-13 11:28:37 +0100 | [diff] [blame] | 386 | if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m) |
Janosch Frank | 6a37627 | 2018-07-13 11:28:22 +0100 | [diff] [blame] | 387 | gmap_pmdp_csp(mm, addr); |
| 388 | } |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 389 | } |
| 390 | |
Martin Schwidefsky | 227be79 | 2016-03-08 11:09:25 +0100 | [diff] [blame] | 391 | static inline pmd_t pmdp_flush_direct(struct mm_struct *mm, |
| 392 | unsigned long addr, pmd_t *pmdp) |
| 393 | { |
Martin Schwidefsky | 227be79 | 2016-03-08 11:09:25 +0100 | [diff] [blame] | 394 | pmd_t old; |
| 395 | |
| 396 | old = *pmdp; |
| 397 | if (pmd_val(old) & _SEGMENT_ENTRY_INVALID) |
| 398 | return old; |
Martin Schwidefsky | 64f31d5 | 2016-05-25 09:45:26 +0200 | [diff] [blame] | 399 | atomic_inc(&mm->context.flush_count); |
| 400 | if (MACHINE_HAS_TLB_LC && |
Martin Schwidefsky | 227be79 | 2016-03-08 11:09:25 +0100 | [diff] [blame] | 401 | cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id()))) |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 402 | pmdp_idte_local(mm, addr, pmdp); |
Martin Schwidefsky | 227be79 | 2016-03-08 11:09:25 +0100 | [diff] [blame] | 403 | else |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 404 | pmdp_idte_global(mm, addr, pmdp); |
Martin Schwidefsky | 64f31d5 | 2016-05-25 09:45:26 +0200 | [diff] [blame] | 405 | atomic_dec(&mm->context.flush_count); |
Martin Schwidefsky | 227be79 | 2016-03-08 11:09:25 +0100 | [diff] [blame] | 406 | return old; |
| 407 | } |
| 408 | |
| 409 | static inline pmd_t pmdp_flush_lazy(struct mm_struct *mm, |
| 410 | unsigned long addr, pmd_t *pmdp) |
| 411 | { |
Martin Schwidefsky | 227be79 | 2016-03-08 11:09:25 +0100 | [diff] [blame] | 412 | pmd_t old; |
| 413 | |
| 414 | old = *pmdp; |
| 415 | if (pmd_val(old) & _SEGMENT_ENTRY_INVALID) |
| 416 | return old; |
Martin Schwidefsky | 64f31d5 | 2016-05-25 09:45:26 +0200 | [diff] [blame] | 417 | atomic_inc(&mm->context.flush_count); |
| 418 | if (cpumask_equal(&mm->context.cpu_attach_mask, |
| 419 | cpumask_of(smp_processor_id()))) { |
Martin Schwidefsky | 227be79 | 2016-03-08 11:09:25 +0100 | [diff] [blame] | 420 | pmd_val(*pmdp) |= _SEGMENT_ENTRY_INVALID; |
| 421 | mm->context.flush_mm = 1; |
Janosch Frank | 6a37627 | 2018-07-13 11:28:22 +0100 | [diff] [blame] | 422 | if (mm_has_pgste(mm)) |
| 423 | gmap_pmdp_invalidate(mm, addr); |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 424 | } else { |
| 425 | pmdp_idte_global(mm, addr, pmdp); |
| 426 | } |
Martin Schwidefsky | 64f31d5 | 2016-05-25 09:45:26 +0200 | [diff] [blame] | 427 | atomic_dec(&mm->context.flush_count); |
Martin Schwidefsky | 227be79 | 2016-03-08 11:09:25 +0100 | [diff] [blame] | 428 | return old; |
| 429 | } |
| 430 | |
Thomas Huth | 81a8f2b | 2019-04-07 14:55:09 +0200 | [diff] [blame] | 431 | #ifdef CONFIG_PGSTE |
Janosch Frank | 637ff9e | 2018-07-13 11:28:28 +0100 | [diff] [blame] | 432 | static 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 Huth | 81a8f2b | 2019-04-07 14:55:09 +0200 | [diff] [blame] | 449 | #endif |
Janosch Frank | 637ff9e | 2018-07-13 11:28:28 +0100 | [diff] [blame] | 450 | |
Martin Schwidefsky | 227be79 | 2016-03-08 11:09:25 +0100 | [diff] [blame] | 451 | pmd_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 Schwidefsky | a980940 | 2016-06-06 10:30:45 +0200 | [diff] [blame] | 456 | preempt_disable(); |
Martin Schwidefsky | 227be79 | 2016-03-08 11:09:25 +0100 | [diff] [blame] | 457 | old = pmdp_flush_direct(mm, addr, pmdp); |
| 458 | *pmdp = new; |
Martin Schwidefsky | a980940 | 2016-06-06 10:30:45 +0200 | [diff] [blame] | 459 | preempt_enable(); |
Martin Schwidefsky | 227be79 | 2016-03-08 11:09:25 +0100 | [diff] [blame] | 460 | return old; |
| 461 | } |
| 462 | EXPORT_SYMBOL(pmdp_xchg_direct); |
| 463 | |
| 464 | pmd_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 Schwidefsky | a980940 | 2016-06-06 10:30:45 +0200 | [diff] [blame] | 469 | preempt_disable(); |
Martin Schwidefsky | 227be79 | 2016-03-08 11:09:25 +0100 | [diff] [blame] | 470 | old = pmdp_flush_lazy(mm, addr, pmdp); |
| 471 | *pmdp = new; |
Martin Schwidefsky | a980940 | 2016-06-06 10:30:45 +0200 | [diff] [blame] | 472 | preempt_enable(); |
Martin Schwidefsky | 227be79 | 2016-03-08 11:09:25 +0100 | [diff] [blame] | 473 | return old; |
| 474 | } |
| 475 | EXPORT_SYMBOL(pmdp_xchg_lazy); |
| 476 | |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 477 | static inline void pudp_idte_local(struct mm_struct *mm, |
| 478 | unsigned long addr, pud_t *pudp) |
| 479 | { |
| 480 | if (MACHINE_HAS_TLB_GUEST) |
Martin Schwidefsky | 28c807e | 2016-07-26 16:00:22 +0200 | [diff] [blame] | 481 | __pudp_idte(addr, pudp, IDTE_NODAT | IDTE_GUEST_ASCE, |
| 482 | mm->context.asce, IDTE_LOCAL); |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 483 | else |
Martin Schwidefsky | 28c807e | 2016-07-26 16:00:22 +0200 | [diff] [blame] | 484 | __pudp_idte(addr, pudp, 0, 0, IDTE_LOCAL); |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | static inline void pudp_idte_global(struct mm_struct *mm, |
| 488 | unsigned long addr, pud_t *pudp) |
| 489 | { |
| 490 | if (MACHINE_HAS_TLB_GUEST) |
Martin Schwidefsky | 28c807e | 2016-07-26 16:00:22 +0200 | [diff] [blame] | 491 | __pudp_idte(addr, pudp, IDTE_NODAT | IDTE_GUEST_ASCE, |
| 492 | mm->context.asce, IDTE_GLOBAL); |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 493 | else if (MACHINE_HAS_IDTE) |
Martin Schwidefsky | 28c807e | 2016-07-26 16:00:22 +0200 | [diff] [blame] | 494 | __pudp_idte(addr, pudp, 0, 0, IDTE_GLOBAL); |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 495 | 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 Schaefer | d08de8e | 2016-07-04 14:47:01 +0200 | [diff] [blame] | 503 | static 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 Schaefer | d08de8e | 2016-07-04 14:47:01 +0200 | [diff] [blame] | 511 | atomic_inc(&mm->context.flush_count); |
| 512 | if (MACHINE_HAS_TLB_LC && |
| 513 | cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id()))) |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 514 | pudp_idte_local(mm, addr, pudp); |
Gerald Schaefer | d08de8e | 2016-07-04 14:47:01 +0200 | [diff] [blame] | 515 | else |
Martin Schwidefsky | 118bd31 | 2016-07-26 16:53:09 +0200 | [diff] [blame] | 516 | pudp_idte_global(mm, addr, pudp); |
Gerald Schaefer | d08de8e | 2016-07-04 14:47:01 +0200 | [diff] [blame] | 517 | atomic_dec(&mm->context.flush_count); |
| 518 | return old; |
| 519 | } |
| 520 | |
| 521 | pud_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 | } |
| 532 | EXPORT_SYMBOL(pudp_xchg_direct); |
| 533 | |
Gerald Schaefer | 75077af | 2012-10-08 16:30:15 -0700 | [diff] [blame] | 534 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 535 | void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp, |
| 536 | pgtable_t pgtable) |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 537 | { |
| 538 | struct list_head *lh = (struct list_head *) pgtable; |
| 539 | |
Martin Schwidefsky | ec66ad6 | 2014-02-12 14:16:18 +0100 | [diff] [blame] | 540 | assert_spin_locked(pmd_lockptr(mm, pmdp)); |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 541 | |
| 542 | /* FIFO */ |
Kirill A. Shutemov | c389a25 | 2013-11-14 14:30:59 -0800 | [diff] [blame] | 543 | if (!pmd_huge_pte(mm, pmdp)) |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 544 | INIT_LIST_HEAD(lh); |
| 545 | else |
Kirill A. Shutemov | c389a25 | 2013-11-14 14:30:59 -0800 | [diff] [blame] | 546 | list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp)); |
| 547 | pmd_huge_pte(mm, pmdp) = pgtable; |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 550 | pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp) |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 551 | { |
| 552 | struct list_head *lh; |
| 553 | pgtable_t pgtable; |
| 554 | pte_t *ptep; |
| 555 | |
Martin Schwidefsky | ec66ad6 | 2014-02-12 14:16:18 +0100 | [diff] [blame] | 556 | assert_spin_locked(pmd_lockptr(mm, pmdp)); |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 557 | |
| 558 | /* FIFO */ |
Kirill A. Shutemov | c389a25 | 2013-11-14 14:30:59 -0800 | [diff] [blame] | 559 | pgtable = pmd_huge_pte(mm, pmdp); |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 560 | lh = (struct list_head *) pgtable; |
| 561 | if (list_empty(lh)) |
Kirill A. Shutemov | c389a25 | 2013-11-14 14:30:59 -0800 | [diff] [blame] | 562 | pmd_huge_pte(mm, pmdp) = NULL; |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 563 | else { |
Kirill A. Shutemov | c389a25 | 2013-11-14 14:30:59 -0800 | [diff] [blame] | 564 | pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next; |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 565 | list_del(lh); |
| 566 | } |
| 567 | ptep = (pte_t *) pgtable; |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 568 | pte_val(*ptep) = _PAGE_INVALID; |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 569 | ptep++; |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 570 | pte_val(*ptep) = _PAGE_INVALID; |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 571 | return pgtable; |
| 572 | } |
Gerald Schaefer | 75077af | 2012-10-08 16:30:15 -0700 | [diff] [blame] | 573 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 574 | |
| 575 | #ifdef CONFIG_PGSTE |
| 576 | void 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 Schwidefsky | a980940 | 2016-06-06 10:30:45 +0200 | [diff] [blame] | 582 | preempt_disable(); |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 583 | 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 Schwidefsky | a980940 | 2016-06-06 10:30:45 +0200 | [diff] [blame] | 588 | preempt_enable(); |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | void ptep_set_notify(struct mm_struct *mm, unsigned long addr, pte_t *ptep) |
| 592 | { |
| 593 | pgste_t pgste; |
| 594 | |
Martin Schwidefsky | a980940 | 2016-06-06 10:30:45 +0200 | [diff] [blame] | 595 | preempt_disable(); |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 596 | pgste = pgste_get_lock(ptep); |
| 597 | pgste_val(pgste) |= PGSTE_IN_BIT; |
| 598 | pgste_set_unlock(ptep, pgste); |
Martin Schwidefsky | a980940 | 2016-06-06 10:30:45 +0200 | [diff] [blame] | 599 | preempt_enable(); |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 600 | } |
| 601 | |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 602 | /** |
| 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 Schwidefsky | 4be130a | 2016-03-08 12:12:18 +0100 | [diff] [blame] | 608 | * @bit: pgste bit to set (e.g. for notification) |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 609 | * |
| 610 | * Returns 0 if the access rights were changed and -EAGAIN if the current |
| 611 | * and requested access rights are incompatible. |
| 612 | */ |
| 613 | int ptep_force_prot(struct mm_struct *mm, unsigned long addr, |
Martin Schwidefsky | 4be130a | 2016-03-08 12:12:18 +0100 | [diff] [blame] | 614 | pte_t *ptep, int prot, unsigned long bit) |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 615 | { |
| 616 | pte_t entry; |
| 617 | pgste_t pgste; |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 618 | int pte_i, pte_p, nodat; |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 619 | |
| 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 Schwidefsky | 4be130a | 2016-03-08 12:12:18 +0100 | [diff] [blame] | 630 | /* Change access rights and set pgste bit */ |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 631 | nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT); |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 632 | if (prot == PROT_NONE && !pte_i) { |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 633 | ptep_flush_direct(mm, addr, ptep, nodat); |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 634 | pgste = pgste_update_all(entry, pgste, mm); |
| 635 | pte_val(entry) |= _PAGE_INVALID; |
| 636 | } |
| 637 | if (prot == PROT_READ && !pte_p) { |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 638 | ptep_flush_direct(mm, addr, ptep, nodat); |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 639 | pte_val(entry) &= ~_PAGE_INVALID; |
| 640 | pte_val(entry) |= _PAGE_PROTECT; |
| 641 | } |
Martin Schwidefsky | 4be130a | 2016-03-08 12:12:18 +0100 | [diff] [blame] | 642 | pgste_val(pgste) |= bit; |
Martin Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 643 | pgste = pgste_set_pte(ptep, pgste, entry); |
| 644 | pgste_set_unlock(ptep, pgste); |
| 645 | return 0; |
| 646 | } |
| 647 | |
Martin Schwidefsky | 4be130a | 2016-03-08 12:12:18 +0100 | [diff] [blame] | 648 | int ptep_shadow_pte(struct mm_struct *mm, unsigned long saddr, |
David Hildenbrand | a9d23e7 | 2016-03-08 12:21:41 +0100 | [diff] [blame] | 649 | pte_t *sptep, pte_t *tptep, pte_t pte) |
Martin Schwidefsky | 4be130a | 2016-03-08 12:12:18 +0100 | [diff] [blame] | 650 | { |
| 651 | pgste_t spgste, tpgste; |
| 652 | pte_t spte, tpte; |
| 653 | int rc = -EAGAIN; |
| 654 | |
David Hildenbrand | a9d23e7 | 2016-03-08 12:21:41 +0100 | [diff] [blame] | 655 | if (!(pte_val(*tptep) & _PAGE_INVALID)) |
| 656 | return 0; /* already shadowed */ |
Martin Schwidefsky | 4be130a | 2016-03-08 12:12:18 +0100 | [diff] [blame] | 657 | spgste = pgste_get_lock(sptep); |
| 658 | spte = *sptep; |
| 659 | if (!(pte_val(spte) & _PAGE_INVALID) && |
David Hildenbrand | a9d23e7 | 2016-03-08 12:21:41 +0100 | [diff] [blame] | 660 | !((pte_val(spte) & _PAGE_PROTECT) && |
| 661 | !(pte_val(pte) & _PAGE_PROTECT))) { |
Martin Schwidefsky | 4be130a | 2016-03-08 12:12:18 +0100 | [diff] [blame] | 662 | pgste_val(spgste) |= PGSTE_VSIE_BIT; |
| 663 | tpgste = pgste_get_lock(tptep); |
| 664 | pte_val(tpte) = (pte_val(spte) & PAGE_MASK) | |
David Hildenbrand | a9d23e7 | 2016-03-08 12:21:41 +0100 | [diff] [blame] | 665 | (pte_val(pte) & _PAGE_PROTECT); |
Martin Schwidefsky | 4be130a | 2016-03-08 12:12:18 +0100 | [diff] [blame] | 666 | /* 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 Hildenbrand | a9d23e7 | 2016-03-08 12:21:41 +0100 | [diff] [blame] | 669 | rc = 1; |
Martin Schwidefsky | 4be130a | 2016-03-08 12:12:18 +0100 | [diff] [blame] | 670 | } |
| 671 | pgste_set_unlock(sptep, spgste); |
| 672 | return rc; |
| 673 | } |
| 674 | |
| 675 | void ptep_unshadow_pte(struct mm_struct *mm, unsigned long saddr, pte_t *ptep) |
| 676 | { |
| 677 | pgste_t pgste; |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 678 | int nodat; |
Martin Schwidefsky | 4be130a | 2016-03-08 12:12:18 +0100 | [diff] [blame] | 679 | |
| 680 | pgste = pgste_get_lock(ptep); |
| 681 | /* notifier is called by the caller */ |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 682 | nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT); |
| 683 | ptep_flush_direct(mm, saddr, ptep, nodat); |
Martin Schwidefsky | 4be130a | 2016-03-08 12:12:18 +0100 | [diff] [blame] | 684 | /* 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 Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 689 | static 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 | |
| 701 | void 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 Schwidefsky | a980940 | 2016-06-06 10:30:45 +0200 | [diff] [blame] | 709 | preempt_disable(); |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 710 | pgste = pgste_get_lock(ptep); |
| 711 | pgstev = pgste_val(pgste); |
| 712 | pte = *ptep; |
Christian Borntraeger | 1c343f7 | 2016-06-13 13:14:56 +0200 | [diff] [blame] | 713 | if (!reset && pte_swap(pte) && |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 714 | ((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 Schwidefsky | a980940 | 2016-06-06 10:30:45 +0200 | [diff] [blame] | 722 | preempt_enable(); |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | void ptep_zap_key(struct mm_struct *mm, unsigned long addr, pte_t *ptep) |
| 726 | { |
| 727 | unsigned long ptev; |
| 728 | pgste_t pgste; |
| 729 | |
Christian Borntraeger | 97ca7bf | 2017-07-06 10:12:58 +0200 | [diff] [blame] | 730 | /* Clear storage key ACC and F, but set R/C */ |
Martin Schwidefsky | a980940 | 2016-06-06 10:30:45 +0200 | [diff] [blame] | 731 | preempt_disable(); |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 732 | pgste = pgste_get_lock(ptep); |
Christian Borntraeger | 97ca7bf | 2017-07-06 10:12:58 +0200 | [diff] [blame] | 733 | pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT); |
| 734 | pgste_val(pgste) |= PGSTE_GR_BIT | PGSTE_GC_BIT; |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 735 | 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 Schwidefsky | a980940 | 2016-06-06 10:30:45 +0200 | [diff] [blame] | 739 | preempt_enable(); |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | /* |
| 743 | * Test and reset if a guest page is dirty |
| 744 | */ |
Janosch Frank | 0959e16 | 2018-07-17 13:21:22 +0100 | [diff] [blame] | 745 | bool ptep_test_and_clear_uc(struct mm_struct *mm, unsigned long addr, |
| 746 | pte_t *ptep) |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 747 | { |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 748 | pgste_t pgste; |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 749 | pte_t pte; |
| 750 | bool dirty; |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 751 | int nodat; |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 752 | |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 753 | 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 Schwidefsky | b2d73b2 | 2016-03-08 11:54:42 +0100 | [diff] [blame] | 758 | pgste = pgste_pte_notify(mm, addr, ptep, pgste); |
Martin Schwidefsky | cd774b9 | 2016-07-26 17:02:31 +0200 | [diff] [blame] | 759 | nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT); |
| 760 | ptep_ipte_global(mm, addr, ptep, nodat); |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 761 | 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 Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 768 | return dirty; |
| 769 | } |
Janosch Frank | 0959e16 | 2018-07-17 13:21:22 +0100 | [diff] [blame] | 770 | EXPORT_SYMBOL_GPL(ptep_test_and_clear_uc); |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 771 | |
| 772 | int set_guest_storage_key(struct mm_struct *mm, unsigned long addr, |
| 773 | unsigned char key, bool nq) |
| 774 | { |
Janosch Frank | 637ff9e | 2018-07-13 11:28:28 +0100 | [diff] [blame] | 775 | unsigned long keyul, paddr; |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 776 | spinlock_t *ptl; |
| 777 | pgste_t old, new; |
Janosch Frank | 637ff9e | 2018-07-13 11:28:28 +0100 | [diff] [blame] | 778 | pmd_t *pmdp; |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 779 | pte_t *ptep; |
| 780 | |
Janosch Frank | 637ff9e | 2018-07-13 11:28:28 +0100 | [diff] [blame] | 781 | 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 Schwidefsky | d3ed1ce | 2016-03-08 11:53:35 +0100 | [diff] [blame] | 805 | if (unlikely(!ptep)) |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 806 | return -EFAULT; |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 807 | |
| 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 Frank | 637ff9e | 2018-07-13 11:28:28 +0100 | [diff] [blame] | 815 | unsigned long bits, skey; |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 816 | |
Janosch Frank | 637ff9e | 2018-07-13 11:28:28 +0100 | [diff] [blame] | 817 | paddr = pte_val(*ptep) & PAGE_MASK; |
| 818 | skey = (unsigned long) page_get_storage_key(paddr); |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 819 | bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED); |
| 820 | skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT); |
| 821 | /* Set storage key ACC and FP */ |
Janosch Frank | 637ff9e | 2018-07-13 11:28:28 +0100 | [diff] [blame] | 822 | page_set_storage_key(paddr, skey, !nq); |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 823 | /* 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 Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 833 | return 0; |
| 834 | } |
| 835 | EXPORT_SYMBOL(set_guest_storage_key); |
| 836 | |
David Hildenbrand | 1824c72 | 2016-05-10 09:43:11 +0200 | [diff] [blame] | 837 | /** |
| 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 | */ |
| 844 | int 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 Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 847 | { |
David Hildenbrand | 1824c72 | 2016-05-10 09:43:11 +0200 | [diff] [blame] | 848 | 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 | } |
| 868 | EXPORT_SYMBOL(cond_set_guest_storage_key); |
| 869 | |
David Hildenbrand | a7e19ab | 2016-05-10 09:50:21 +0200 | [diff] [blame] | 870 | /** |
| 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 | */ |
| 875 | int reset_guest_reference_bit(struct mm_struct *mm, unsigned long addr) |
| 876 | { |
| 877 | spinlock_t *ptl; |
Janosch Frank | 637ff9e | 2018-07-13 11:28:28 +0100 | [diff] [blame] | 878 | unsigned long paddr; |
David Hildenbrand | a7e19ab | 2016-05-10 09:50:21 +0200 | [diff] [blame] | 879 | pgste_t old, new; |
Janosch Frank | 637ff9e | 2018-07-13 11:28:28 +0100 | [diff] [blame] | 880 | pmd_t *pmdp; |
David Hildenbrand | a7e19ab | 2016-05-10 09:50:21 +0200 | [diff] [blame] | 881 | pte_t *ptep; |
| 882 | int cc = 0; |
| 883 | |
Janosch Frank | 637ff9e | 2018-07-13 11:28:28 +0100 | [diff] [blame] | 884 | 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 Hildenbrand | a7e19ab | 2016-05-10 09:50:21 +0200 | [diff] [blame] | 904 | 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 Frank | 637ff9e | 2018-07-13 11:28:28 +0100 | [diff] [blame] | 912 | paddr = pte_val(*ptep) & PAGE_MASK; |
| 913 | cc = page_reset_referenced(paddr); |
David Hildenbrand | a7e19ab | 2016-05-10 09:50:21 +0200 | [diff] [blame] | 914 | /* 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 Frank | 4bead2a | 2017-01-27 10:23:59 +0100 | [diff] [blame] | 925 | return cc; |
David Hildenbrand | a7e19ab | 2016-05-10 09:50:21 +0200 | [diff] [blame] | 926 | } |
| 927 | EXPORT_SYMBOL(reset_guest_reference_bit); |
| 928 | |
David Hildenbrand | 154c8c1 | 2016-05-09 11:22:34 +0200 | [diff] [blame] | 929 | int get_guest_storage_key(struct mm_struct *mm, unsigned long addr, |
| 930 | unsigned char *key) |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 931 | { |
Janosch Frank | 637ff9e | 2018-07-13 11:28:28 +0100 | [diff] [blame] | 932 | unsigned long paddr; |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 933 | spinlock_t *ptl; |
| 934 | pgste_t pgste; |
Janosch Frank | 637ff9e | 2018-07-13 11:28:28 +0100 | [diff] [blame] | 935 | pmd_t *pmdp; |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 936 | pte_t *ptep; |
| 937 | |
Janosch Frank | 637ff9e | 2018-07-13 11:28:28 +0100 | [diff] [blame] | 938 | 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 Schwidefsky | d3ed1ce | 2016-03-08 11:53:35 +0100 | [diff] [blame] | 960 | if (unlikely(!ptep)) |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 961 | return -EFAULT; |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 962 | |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 963 | pgste = pgste_get_lock(ptep); |
David Hildenbrand | 154c8c1 | 2016-05-09 11:22:34 +0200 | [diff] [blame] | 964 | *key = (pgste_val(pgste) & (PGSTE_ACC_BITS | PGSTE_FP_BIT)) >> 56; |
Janosch Frank | 637ff9e | 2018-07-13 11:28:28 +0100 | [diff] [blame] | 965 | paddr = pte_val(*ptep) & PAGE_MASK; |
David Hildenbrand | 8d6037a | 2016-05-09 11:15:32 +0200 | [diff] [blame] | 966 | if (!(pte_val(*ptep) & _PAGE_INVALID)) |
Janosch Frank | 637ff9e | 2018-07-13 11:28:28 +0100 | [diff] [blame] | 967 | *key = page_get_storage_key(paddr); |
David Hildenbrand | 8d6037a | 2016-05-09 11:15:32 +0200 | [diff] [blame] | 968 | /* Reflect guest's logical view, not physical */ |
David Hildenbrand | 154c8c1 | 2016-05-09 11:22:34 +0200 | [diff] [blame] | 969 | *key |= (pgste_val(pgste) & (PGSTE_GR_BIT | PGSTE_GC_BIT)) >> 48; |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 970 | pgste_set_unlock(ptep, pgste); |
| 971 | pte_unmap_unlock(ptep, ptl); |
David Hildenbrand | 154c8c1 | 2016-05-09 11:22:34 +0200 | [diff] [blame] | 972 | return 0; |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 973 | } |
| 974 | EXPORT_SYMBOL(get_guest_storage_key); |
Claudio Imbrenda | 2d42f94 | 2017-04-20 10:03:45 +0200 | [diff] [blame] | 975 | |
| 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 | */ |
| 988 | int 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 Imbrenda | 1bab1c0 | 2016-08-29 15:56:55 +0200 | [diff] [blame] | 1014 | pgstev &= ~(_PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT); |
Claudio Imbrenda | 2d42f94 | 2017-04-20 10:03:45 +0200 | [diff] [blame] | 1015 | 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 Imbrenda | 1bab1c0 | 2016-08-29 15:56:55 +0200 | [diff] [blame] | 1060 | case ESSA_SET_STABLE_NODAT: |
| 1061 | pgstev &= ~_PGSTE_GPS_USAGE_MASK; |
| 1062 | pgstev |= _PGSTE_GPS_USAGE_STABLE | _PGSTE_GPS_NODAT; |
| 1063 | break; |
Claudio Imbrenda | 2d42f94 | 2017-04-20 10:03:45 +0200 | [diff] [blame] | 1064 | 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 | } |
| 1077 | EXPORT_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 | */ |
| 1089 | int 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 | } |
| 1108 | EXPORT_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 | */ |
| 1118 | int 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 | } |
| 1130 | EXPORT_SYMBOL(get_pgste); |
Martin Schwidefsky | 1e133ab | 2016-03-08 11:49:57 +0100 | [diff] [blame] | 1131 | #endif |