pgtable.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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. #include <linux/sched.h>
  7. #include <linux/kernel.h>
  8. #include <linux/errno.h>
  9. #include <linux/gfp.h>
  10. #include <linux/mm.h>
  11. #include <linux/swap.h>
  12. #include <linux/smp.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/rcupdate.h>
  15. #include <linux/slab.h>
  16. #include <linux/swapops.h>
  17. #include <linux/sysctl.h>
  18. #include <linux/ksm.h>
  19. #include <linux/mman.h>
  20. #include <asm/pgtable.h>
  21. #include <asm/pgalloc.h>
  22. #include <asm/tlb.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/mmu_context.h>
  25. #include <asm/page-states.h>
  26. static inline void ptep_ipte_local(struct mm_struct *mm, unsigned long addr,
  27. pte_t *ptep, int nodat)
  28. {
  29. unsigned long opt, asce;
  30. if (MACHINE_HAS_TLB_GUEST) {
  31. opt = 0;
  32. asce = READ_ONCE(mm->context.gmap_asce);
  33. if (asce == 0UL || nodat)
  34. opt |= IPTE_NODAT;
  35. if (asce != -1UL) {
  36. asce = asce ? : mm->context.asce;
  37. opt |= IPTE_GUEST_ASCE;
  38. }
  39. __ptep_ipte(addr, ptep, opt, asce, IPTE_LOCAL);
  40. } else {
  41. __ptep_ipte(addr, ptep, 0, 0, IPTE_LOCAL);
  42. }
  43. }
  44. static inline void ptep_ipte_global(struct mm_struct *mm, unsigned long addr,
  45. pte_t *ptep, int nodat)
  46. {
  47. unsigned long opt, asce;
  48. if (MACHINE_HAS_TLB_GUEST) {
  49. opt = 0;
  50. asce = READ_ONCE(mm->context.gmap_asce);
  51. if (asce == 0UL || nodat)
  52. opt |= IPTE_NODAT;
  53. if (asce != -1UL) {
  54. asce = asce ? : mm->context.asce;
  55. opt |= IPTE_GUEST_ASCE;
  56. }
  57. __ptep_ipte(addr, ptep, opt, asce, IPTE_GLOBAL);
  58. } else {
  59. __ptep_ipte(addr, ptep, 0, 0, IPTE_GLOBAL);
  60. }
  61. }
  62. static inline pte_t ptep_flush_direct(struct mm_struct *mm,
  63. unsigned long addr, pte_t *ptep,
  64. int nodat)
  65. {
  66. pte_t old;
  67. old = *ptep;
  68. if (unlikely(pte_val(old) & _PAGE_INVALID))
  69. return old;
  70. atomic_inc(&mm->context.flush_count);
  71. if (MACHINE_HAS_TLB_LC &&
  72. cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
  73. ptep_ipte_local(mm, addr, ptep, nodat);
  74. else
  75. ptep_ipte_global(mm, addr, ptep, nodat);
  76. atomic_dec(&mm->context.flush_count);
  77. return old;
  78. }
  79. static inline pte_t ptep_flush_lazy(struct mm_struct *mm,
  80. unsigned long addr, pte_t *ptep,
  81. int nodat)
  82. {
  83. pte_t old;
  84. old = *ptep;
  85. if (unlikely(pte_val(old) & _PAGE_INVALID))
  86. return old;
  87. atomic_inc(&mm->context.flush_count);
  88. if (cpumask_equal(&mm->context.cpu_attach_mask,
  89. cpumask_of(smp_processor_id()))) {
  90. pte_val(*ptep) |= _PAGE_INVALID;
  91. mm->context.flush_mm = 1;
  92. } else
  93. ptep_ipte_global(mm, addr, ptep, nodat);
  94. atomic_dec(&mm->context.flush_count);
  95. return old;
  96. }
  97. static inline pgste_t pgste_get_lock(pte_t *ptep)
  98. {
  99. unsigned long new = 0;
  100. #ifdef CONFIG_PGSTE
  101. unsigned long old;
  102. asm(
  103. " lg %0,%2\n"
  104. "0: lgr %1,%0\n"
  105. " nihh %0,0xff7f\n" /* clear PCL bit in old */
  106. " oihh %1,0x0080\n" /* set PCL bit in new */
  107. " csg %0,%1,%2\n"
  108. " jl 0b\n"
  109. : "=&d" (old), "=&d" (new), "=Q" (ptep[PTRS_PER_PTE])
  110. : "Q" (ptep[PTRS_PER_PTE]) : "cc", "memory");
  111. #endif
  112. return __pgste(new);
  113. }
  114. static inline void pgste_set_unlock(pte_t *ptep, pgste_t pgste)
  115. {
  116. #ifdef CONFIG_PGSTE
  117. asm(
  118. " nihh %1,0xff7f\n" /* clear PCL bit */
  119. " stg %1,%0\n"
  120. : "=Q" (ptep[PTRS_PER_PTE])
  121. : "d" (pgste_val(pgste)), "Q" (ptep[PTRS_PER_PTE])
  122. : "cc", "memory");
  123. #endif
  124. }
  125. static inline pgste_t pgste_get(pte_t *ptep)
  126. {
  127. unsigned long pgste = 0;
  128. #ifdef CONFIG_PGSTE
  129. pgste = *(unsigned long *)(ptep + PTRS_PER_PTE);
  130. #endif
  131. return __pgste(pgste);
  132. }
  133. static inline void pgste_set(pte_t *ptep, pgste_t pgste)
  134. {
  135. #ifdef CONFIG_PGSTE
  136. *(pgste_t *)(ptep + PTRS_PER_PTE) = pgste;
  137. #endif
  138. }
  139. static inline pgste_t pgste_update_all(pte_t pte, pgste_t pgste,
  140. struct mm_struct *mm)
  141. {
  142. #ifdef CONFIG_PGSTE
  143. unsigned long address, bits, skey;
  144. if (!mm_uses_skeys(mm) || pte_val(pte) & _PAGE_INVALID)
  145. return pgste;
  146. address = pte_val(pte) & PAGE_MASK;
  147. skey = (unsigned long) page_get_storage_key(address);
  148. bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
  149. /* Transfer page changed & referenced bit to guest bits in pgste */
  150. pgste_val(pgste) |= bits << 48; /* GR bit & GC bit */
  151. /* Copy page access key and fetch protection bit to pgste */
  152. pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT);
  153. pgste_val(pgste) |= (skey & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
  154. #endif
  155. return pgste;
  156. }
  157. static inline void pgste_set_key(pte_t *ptep, pgste_t pgste, pte_t entry,
  158. struct mm_struct *mm)
  159. {
  160. #ifdef CONFIG_PGSTE
  161. unsigned long address;
  162. unsigned long nkey;
  163. if (!mm_uses_skeys(mm) || pte_val(entry) & _PAGE_INVALID)
  164. return;
  165. VM_BUG_ON(!(pte_val(*ptep) & _PAGE_INVALID));
  166. address = pte_val(entry) & PAGE_MASK;
  167. /*
  168. * Set page access key and fetch protection bit from pgste.
  169. * The guest C/R information is still in the PGSTE, set real
  170. * key C/R to 0.
  171. */
  172. nkey = (pgste_val(pgste) & (PGSTE_ACC_BITS | PGSTE_FP_BIT)) >> 56;
  173. nkey |= (pgste_val(pgste) & (PGSTE_GR_BIT | PGSTE_GC_BIT)) >> 48;
  174. page_set_storage_key(address, nkey, 0);
  175. #endif
  176. }
  177. static inline pgste_t pgste_set_pte(pte_t *ptep, pgste_t pgste, pte_t entry)
  178. {
  179. #ifdef CONFIG_PGSTE
  180. if ((pte_val(entry) & _PAGE_PRESENT) &&
  181. (pte_val(entry) & _PAGE_WRITE) &&
  182. !(pte_val(entry) & _PAGE_INVALID)) {
  183. if (!MACHINE_HAS_ESOP) {
  184. /*
  185. * Without enhanced suppression-on-protection force
  186. * the dirty bit on for all writable ptes.
  187. */
  188. pte_val(entry) |= _PAGE_DIRTY;
  189. pte_val(entry) &= ~_PAGE_PROTECT;
  190. }
  191. if (!(pte_val(entry) & _PAGE_PROTECT))
  192. /* This pte allows write access, set user-dirty */
  193. pgste_val(pgste) |= PGSTE_UC_BIT;
  194. }
  195. #endif
  196. *ptep = entry;
  197. return pgste;
  198. }
  199. static inline pgste_t pgste_pte_notify(struct mm_struct *mm,
  200. unsigned long addr,
  201. pte_t *ptep, pgste_t pgste)
  202. {
  203. #ifdef CONFIG_PGSTE
  204. unsigned long bits;
  205. bits = pgste_val(pgste) & (PGSTE_IN_BIT | PGSTE_VSIE_BIT);
  206. if (bits) {
  207. pgste_val(pgste) ^= bits;
  208. ptep_notify(mm, addr, ptep, bits);
  209. }
  210. #endif
  211. return pgste;
  212. }
  213. static inline pgste_t ptep_xchg_start(struct mm_struct *mm,
  214. unsigned long addr, pte_t *ptep)
  215. {
  216. pgste_t pgste = __pgste(0);
  217. if (mm_has_pgste(mm)) {
  218. pgste = pgste_get_lock(ptep);
  219. pgste = pgste_pte_notify(mm, addr, ptep, pgste);
  220. }
  221. return pgste;
  222. }
  223. static inline pte_t ptep_xchg_commit(struct mm_struct *mm,
  224. unsigned long addr, pte_t *ptep,
  225. pgste_t pgste, pte_t old, pte_t new)
  226. {
  227. if (mm_has_pgste(mm)) {
  228. if (pte_val(old) & _PAGE_INVALID)
  229. pgste_set_key(ptep, pgste, new, mm);
  230. if (pte_val(new) & _PAGE_INVALID) {
  231. pgste = pgste_update_all(old, pgste, mm);
  232. if ((pgste_val(pgste) & _PGSTE_GPS_USAGE_MASK) ==
  233. _PGSTE_GPS_USAGE_UNUSED)
  234. pte_val(old) |= _PAGE_UNUSED;
  235. }
  236. pgste = pgste_set_pte(ptep, pgste, new);
  237. pgste_set_unlock(ptep, pgste);
  238. } else {
  239. *ptep = new;
  240. }
  241. return old;
  242. }
  243. pte_t ptep_xchg_direct(struct mm_struct *mm, unsigned long addr,
  244. pte_t *ptep, pte_t new)
  245. {
  246. pgste_t pgste;
  247. pte_t old;
  248. int nodat;
  249. preempt_disable();
  250. pgste = ptep_xchg_start(mm, addr, ptep);
  251. nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
  252. old = ptep_flush_direct(mm, addr, ptep, nodat);
  253. old = ptep_xchg_commit(mm, addr, ptep, pgste, old, new);
  254. preempt_enable();
  255. return old;
  256. }
  257. EXPORT_SYMBOL(ptep_xchg_direct);
  258. pte_t ptep_xchg_lazy(struct mm_struct *mm, unsigned long addr,
  259. pte_t *ptep, pte_t new)
  260. {
  261. pgste_t pgste;
  262. pte_t old;
  263. int nodat;
  264. preempt_disable();
  265. pgste = ptep_xchg_start(mm, addr, ptep);
  266. nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
  267. old = ptep_flush_lazy(mm, addr, ptep, nodat);
  268. old = ptep_xchg_commit(mm, addr, ptep, pgste, old, new);
  269. preempt_enable();
  270. return old;
  271. }
  272. EXPORT_SYMBOL(ptep_xchg_lazy);
  273. pte_t ptep_modify_prot_start(struct mm_struct *mm, unsigned long addr,
  274. pte_t *ptep)
  275. {
  276. pgste_t pgste;
  277. pte_t old;
  278. int nodat;
  279. preempt_disable();
  280. pgste = ptep_xchg_start(mm, addr, ptep);
  281. nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
  282. old = ptep_flush_lazy(mm, addr, ptep, nodat);
  283. if (mm_has_pgste(mm)) {
  284. pgste = pgste_update_all(old, pgste, mm);
  285. pgste_set(ptep, pgste);
  286. }
  287. return old;
  288. }
  289. EXPORT_SYMBOL(ptep_modify_prot_start);
  290. void ptep_modify_prot_commit(struct mm_struct *mm, unsigned long addr,
  291. pte_t *ptep, pte_t pte)
  292. {
  293. pgste_t pgste;
  294. if (!MACHINE_HAS_NX)
  295. pte_val(pte) &= ~_PAGE_NOEXEC;
  296. if (mm_has_pgste(mm)) {
  297. pgste = pgste_get(ptep);
  298. pgste_set_key(ptep, pgste, pte, mm);
  299. pgste = pgste_set_pte(ptep, pgste, pte);
  300. pgste_set_unlock(ptep, pgste);
  301. } else {
  302. *ptep = pte;
  303. }
  304. preempt_enable();
  305. }
  306. EXPORT_SYMBOL(ptep_modify_prot_commit);
  307. static inline void pmdp_idte_local(struct mm_struct *mm,
  308. unsigned long addr, pmd_t *pmdp)
  309. {
  310. if (MACHINE_HAS_TLB_GUEST)
  311. __pmdp_idte(addr, pmdp, IDTE_NODAT | IDTE_GUEST_ASCE,
  312. mm->context.asce, IDTE_LOCAL);
  313. else
  314. __pmdp_idte(addr, pmdp, 0, 0, IDTE_LOCAL);
  315. if (mm_has_pgste(mm))
  316. gmap_pmdp_idte_local(mm, addr);
  317. }
  318. static inline void pmdp_idte_global(struct mm_struct *mm,
  319. unsigned long addr, pmd_t *pmdp)
  320. {
  321. if (MACHINE_HAS_TLB_GUEST) {
  322. __pmdp_idte(addr, pmdp, IDTE_NODAT | IDTE_GUEST_ASCE,
  323. mm->context.asce, IDTE_GLOBAL);
  324. if (mm_has_pgste(mm))
  325. gmap_pmdp_idte_global(mm, addr);
  326. } else if (MACHINE_HAS_IDTE) {
  327. __pmdp_idte(addr, pmdp, 0, 0, IDTE_GLOBAL);
  328. if (mm_has_pgste(mm))
  329. gmap_pmdp_idte_global(mm, addr);
  330. } else {
  331. __pmdp_csp(pmdp);
  332. if (mm_has_pgste(mm))
  333. gmap_pmdp_csp(mm, addr);
  334. }
  335. }
  336. static inline pmd_t pmdp_flush_direct(struct mm_struct *mm,
  337. unsigned long addr, pmd_t *pmdp)
  338. {
  339. pmd_t old;
  340. old = *pmdp;
  341. if (pmd_val(old) & _SEGMENT_ENTRY_INVALID)
  342. return old;
  343. atomic_inc(&mm->context.flush_count);
  344. if (MACHINE_HAS_TLB_LC &&
  345. cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
  346. pmdp_idte_local(mm, addr, pmdp);
  347. else
  348. pmdp_idte_global(mm, addr, pmdp);
  349. atomic_dec(&mm->context.flush_count);
  350. return old;
  351. }
  352. static inline pmd_t pmdp_flush_lazy(struct mm_struct *mm,
  353. unsigned long addr, pmd_t *pmdp)
  354. {
  355. pmd_t old;
  356. old = *pmdp;
  357. if (pmd_val(old) & _SEGMENT_ENTRY_INVALID)
  358. return old;
  359. atomic_inc(&mm->context.flush_count);
  360. if (cpumask_equal(&mm->context.cpu_attach_mask,
  361. cpumask_of(smp_processor_id()))) {
  362. pmd_val(*pmdp) |= _SEGMENT_ENTRY_INVALID;
  363. mm->context.flush_mm = 1;
  364. if (mm_has_pgste(mm))
  365. gmap_pmdp_invalidate(mm, addr);
  366. } else {
  367. pmdp_idte_global(mm, addr, pmdp);
  368. }
  369. atomic_dec(&mm->context.flush_count);
  370. return old;
  371. }
  372. pmd_t pmdp_xchg_direct(struct mm_struct *mm, unsigned long addr,
  373. pmd_t *pmdp, pmd_t new)
  374. {
  375. pmd_t old;
  376. preempt_disable();
  377. old = pmdp_flush_direct(mm, addr, pmdp);
  378. *pmdp = new;
  379. preempt_enable();
  380. return old;
  381. }
  382. EXPORT_SYMBOL(pmdp_xchg_direct);
  383. pmd_t pmdp_xchg_lazy(struct mm_struct *mm, unsigned long addr,
  384. pmd_t *pmdp, pmd_t new)
  385. {
  386. pmd_t old;
  387. preempt_disable();
  388. old = pmdp_flush_lazy(mm, addr, pmdp);
  389. *pmdp = new;
  390. preempt_enable();
  391. return old;
  392. }
  393. EXPORT_SYMBOL(pmdp_xchg_lazy);
  394. static inline void pudp_idte_local(struct mm_struct *mm,
  395. unsigned long addr, pud_t *pudp)
  396. {
  397. if (MACHINE_HAS_TLB_GUEST)
  398. __pudp_idte(addr, pudp, IDTE_NODAT | IDTE_GUEST_ASCE,
  399. mm->context.asce, IDTE_LOCAL);
  400. else
  401. __pudp_idte(addr, pudp, 0, 0, IDTE_LOCAL);
  402. }
  403. static inline void pudp_idte_global(struct mm_struct *mm,
  404. unsigned long addr, pud_t *pudp)
  405. {
  406. if (MACHINE_HAS_TLB_GUEST)
  407. __pudp_idte(addr, pudp, IDTE_NODAT | IDTE_GUEST_ASCE,
  408. mm->context.asce, IDTE_GLOBAL);
  409. else if (MACHINE_HAS_IDTE)
  410. __pudp_idte(addr, pudp, 0, 0, IDTE_GLOBAL);
  411. else
  412. /*
  413. * Invalid bit position is the same for pmd and pud, so we can
  414. * re-use _pmd_csp() here
  415. */
  416. __pmdp_csp((pmd_t *) pudp);
  417. }
  418. static inline pud_t pudp_flush_direct(struct mm_struct *mm,
  419. unsigned long addr, pud_t *pudp)
  420. {
  421. pud_t old;
  422. old = *pudp;
  423. if (pud_val(old) & _REGION_ENTRY_INVALID)
  424. return old;
  425. atomic_inc(&mm->context.flush_count);
  426. if (MACHINE_HAS_TLB_LC &&
  427. cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
  428. pudp_idte_local(mm, addr, pudp);
  429. else
  430. pudp_idte_global(mm, addr, pudp);
  431. atomic_dec(&mm->context.flush_count);
  432. return old;
  433. }
  434. pud_t pudp_xchg_direct(struct mm_struct *mm, unsigned long addr,
  435. pud_t *pudp, pud_t new)
  436. {
  437. pud_t old;
  438. preempt_disable();
  439. old = pudp_flush_direct(mm, addr, pudp);
  440. *pudp = new;
  441. preempt_enable();
  442. return old;
  443. }
  444. EXPORT_SYMBOL(pudp_xchg_direct);
  445. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  446. void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  447. pgtable_t pgtable)
  448. {
  449. struct list_head *lh = (struct list_head *) pgtable;
  450. assert_spin_locked(pmd_lockptr(mm, pmdp));
  451. /* FIFO */
  452. if (!pmd_huge_pte(mm, pmdp))
  453. INIT_LIST_HEAD(lh);
  454. else
  455. list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
  456. pmd_huge_pte(mm, pmdp) = pgtable;
  457. }
  458. pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  459. {
  460. struct list_head *lh;
  461. pgtable_t pgtable;
  462. pte_t *ptep;
  463. assert_spin_locked(pmd_lockptr(mm, pmdp));
  464. /* FIFO */
  465. pgtable = pmd_huge_pte(mm, pmdp);
  466. lh = (struct list_head *) pgtable;
  467. if (list_empty(lh))
  468. pmd_huge_pte(mm, pmdp) = NULL;
  469. else {
  470. pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
  471. list_del(lh);
  472. }
  473. ptep = (pte_t *) pgtable;
  474. pte_val(*ptep) = _PAGE_INVALID;
  475. ptep++;
  476. pte_val(*ptep) = _PAGE_INVALID;
  477. return pgtable;
  478. }
  479. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  480. #ifdef CONFIG_PGSTE
  481. void ptep_set_pte_at(struct mm_struct *mm, unsigned long addr,
  482. pte_t *ptep, pte_t entry)
  483. {
  484. pgste_t pgste;
  485. /* the mm_has_pgste() check is done in set_pte_at() */
  486. preempt_disable();
  487. pgste = pgste_get_lock(ptep);
  488. pgste_val(pgste) &= ~_PGSTE_GPS_ZERO;
  489. pgste_set_key(ptep, pgste, entry, mm);
  490. pgste = pgste_set_pte(ptep, pgste, entry);
  491. pgste_set_unlock(ptep, pgste);
  492. preempt_enable();
  493. }
  494. void ptep_set_notify(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  495. {
  496. pgste_t pgste;
  497. preempt_disable();
  498. pgste = pgste_get_lock(ptep);
  499. pgste_val(pgste) |= PGSTE_IN_BIT;
  500. pgste_set_unlock(ptep, pgste);
  501. preempt_enable();
  502. }
  503. /**
  504. * ptep_force_prot - change access rights of a locked pte
  505. * @mm: pointer to the process mm_struct
  506. * @addr: virtual address in the guest address space
  507. * @ptep: pointer to the page table entry
  508. * @prot: indicates guest access rights: PROT_NONE, PROT_READ or PROT_WRITE
  509. * @bit: pgste bit to set (e.g. for notification)
  510. *
  511. * Returns 0 if the access rights were changed and -EAGAIN if the current
  512. * and requested access rights are incompatible.
  513. */
  514. int ptep_force_prot(struct mm_struct *mm, unsigned long addr,
  515. pte_t *ptep, int prot, unsigned long bit)
  516. {
  517. pte_t entry;
  518. pgste_t pgste;
  519. int pte_i, pte_p, nodat;
  520. pgste = pgste_get_lock(ptep);
  521. entry = *ptep;
  522. /* Check pte entry after all locks have been acquired */
  523. pte_i = pte_val(entry) & _PAGE_INVALID;
  524. pte_p = pte_val(entry) & _PAGE_PROTECT;
  525. if ((pte_i && (prot != PROT_NONE)) ||
  526. (pte_p && (prot & PROT_WRITE))) {
  527. pgste_set_unlock(ptep, pgste);
  528. return -EAGAIN;
  529. }
  530. /* Change access rights and set pgste bit */
  531. nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
  532. if (prot == PROT_NONE && !pte_i) {
  533. ptep_flush_direct(mm, addr, ptep, nodat);
  534. pgste = pgste_update_all(entry, pgste, mm);
  535. pte_val(entry) |= _PAGE_INVALID;
  536. }
  537. if (prot == PROT_READ && !pte_p) {
  538. ptep_flush_direct(mm, addr, ptep, nodat);
  539. pte_val(entry) &= ~_PAGE_INVALID;
  540. pte_val(entry) |= _PAGE_PROTECT;
  541. }
  542. pgste_val(pgste) |= bit;
  543. pgste = pgste_set_pte(ptep, pgste, entry);
  544. pgste_set_unlock(ptep, pgste);
  545. return 0;
  546. }
  547. int ptep_shadow_pte(struct mm_struct *mm, unsigned long saddr,
  548. pte_t *sptep, pte_t *tptep, pte_t pte)
  549. {
  550. pgste_t spgste, tpgste;
  551. pte_t spte, tpte;
  552. int rc = -EAGAIN;
  553. if (!(pte_val(*tptep) & _PAGE_INVALID))
  554. return 0; /* already shadowed */
  555. spgste = pgste_get_lock(sptep);
  556. spte = *sptep;
  557. if (!(pte_val(spte) & _PAGE_INVALID) &&
  558. !((pte_val(spte) & _PAGE_PROTECT) &&
  559. !(pte_val(pte) & _PAGE_PROTECT))) {
  560. pgste_val(spgste) |= PGSTE_VSIE_BIT;
  561. tpgste = pgste_get_lock(tptep);
  562. pte_val(tpte) = (pte_val(spte) & PAGE_MASK) |
  563. (pte_val(pte) & _PAGE_PROTECT);
  564. /* don't touch the storage key - it belongs to parent pgste */
  565. tpgste = pgste_set_pte(tptep, tpgste, tpte);
  566. pgste_set_unlock(tptep, tpgste);
  567. rc = 1;
  568. }
  569. pgste_set_unlock(sptep, spgste);
  570. return rc;
  571. }
  572. void ptep_unshadow_pte(struct mm_struct *mm, unsigned long saddr, pte_t *ptep)
  573. {
  574. pgste_t pgste;
  575. int nodat;
  576. pgste = pgste_get_lock(ptep);
  577. /* notifier is called by the caller */
  578. nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
  579. ptep_flush_direct(mm, saddr, ptep, nodat);
  580. /* don't touch the storage key - it belongs to parent pgste */
  581. pgste = pgste_set_pte(ptep, pgste, __pte(_PAGE_INVALID));
  582. pgste_set_unlock(ptep, pgste);
  583. }
  584. static void ptep_zap_swap_entry(struct mm_struct *mm, swp_entry_t entry)
  585. {
  586. if (!non_swap_entry(entry))
  587. dec_mm_counter(mm, MM_SWAPENTS);
  588. else if (is_migration_entry(entry)) {
  589. struct page *page = migration_entry_to_page(entry);
  590. dec_mm_counter(mm, mm_counter(page));
  591. }
  592. free_swap_and_cache(entry);
  593. }
  594. void ptep_zap_unused(struct mm_struct *mm, unsigned long addr,
  595. pte_t *ptep, int reset)
  596. {
  597. unsigned long pgstev;
  598. pgste_t pgste;
  599. pte_t pte;
  600. /* Zap unused and logically-zero pages */
  601. preempt_disable();
  602. pgste = pgste_get_lock(ptep);
  603. pgstev = pgste_val(pgste);
  604. pte = *ptep;
  605. if (!reset && pte_swap(pte) &&
  606. ((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED ||
  607. (pgstev & _PGSTE_GPS_ZERO))) {
  608. ptep_zap_swap_entry(mm, pte_to_swp_entry(pte));
  609. pte_clear(mm, addr, ptep);
  610. }
  611. if (reset)
  612. pgste_val(pgste) &= ~_PGSTE_GPS_USAGE_MASK;
  613. pgste_set_unlock(ptep, pgste);
  614. preempt_enable();
  615. }
  616. void ptep_zap_key(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  617. {
  618. unsigned long ptev;
  619. pgste_t pgste;
  620. /* Clear storage key ACC and F, but set R/C */
  621. preempt_disable();
  622. pgste = pgste_get_lock(ptep);
  623. pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT);
  624. pgste_val(pgste) |= PGSTE_GR_BIT | PGSTE_GC_BIT;
  625. ptev = pte_val(*ptep);
  626. if (!(ptev & _PAGE_INVALID) && (ptev & _PAGE_WRITE))
  627. page_set_storage_key(ptev & PAGE_MASK, PAGE_DEFAULT_KEY, 1);
  628. pgste_set_unlock(ptep, pgste);
  629. preempt_enable();
  630. }
  631. /*
  632. * Test and reset if a guest page is dirty
  633. */
  634. bool test_and_clear_guest_dirty(struct mm_struct *mm, unsigned long addr)
  635. {
  636. spinlock_t *ptl;
  637. pgd_t *pgd;
  638. p4d_t *p4d;
  639. pud_t *pud;
  640. pmd_t *pmd;
  641. pgste_t pgste;
  642. pte_t *ptep;
  643. pte_t pte;
  644. bool dirty;
  645. int nodat;
  646. pgd = pgd_offset(mm, addr);
  647. p4d = p4d_alloc(mm, pgd, addr);
  648. if (!p4d)
  649. return false;
  650. pud = pud_alloc(mm, p4d, addr);
  651. if (!pud)
  652. return false;
  653. pmd = pmd_alloc(mm, pud, addr);
  654. if (!pmd)
  655. return false;
  656. /* We can't run guests backed by huge pages, but userspace can
  657. * still set them up and then try to migrate them without any
  658. * migration support.
  659. */
  660. if (pmd_large(*pmd))
  661. return true;
  662. ptep = pte_alloc_map_lock(mm, pmd, addr, &ptl);
  663. if (unlikely(!ptep))
  664. return false;
  665. pgste = pgste_get_lock(ptep);
  666. dirty = !!(pgste_val(pgste) & PGSTE_UC_BIT);
  667. pgste_val(pgste) &= ~PGSTE_UC_BIT;
  668. pte = *ptep;
  669. if (dirty && (pte_val(pte) & _PAGE_PRESENT)) {
  670. pgste = pgste_pte_notify(mm, addr, ptep, pgste);
  671. nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
  672. ptep_ipte_global(mm, addr, ptep, nodat);
  673. if (MACHINE_HAS_ESOP || !(pte_val(pte) & _PAGE_WRITE))
  674. pte_val(pte) |= _PAGE_PROTECT;
  675. else
  676. pte_val(pte) |= _PAGE_INVALID;
  677. *ptep = pte;
  678. }
  679. pgste_set_unlock(ptep, pgste);
  680. spin_unlock(ptl);
  681. return dirty;
  682. }
  683. EXPORT_SYMBOL_GPL(test_and_clear_guest_dirty);
  684. int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
  685. unsigned char key, bool nq)
  686. {
  687. unsigned long keyul;
  688. spinlock_t *ptl;
  689. pgste_t old, new;
  690. pte_t *ptep;
  691. ptep = get_locked_pte(mm, addr, &ptl);
  692. if (unlikely(!ptep))
  693. return -EFAULT;
  694. new = old = pgste_get_lock(ptep);
  695. pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
  696. PGSTE_ACC_BITS | PGSTE_FP_BIT);
  697. keyul = (unsigned long) key;
  698. pgste_val(new) |= (keyul & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
  699. pgste_val(new) |= (keyul & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
  700. if (!(pte_val(*ptep) & _PAGE_INVALID)) {
  701. unsigned long address, bits, skey;
  702. address = pte_val(*ptep) & PAGE_MASK;
  703. skey = (unsigned long) page_get_storage_key(address);
  704. bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
  705. skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
  706. /* Set storage key ACC and FP */
  707. page_set_storage_key(address, skey, !nq);
  708. /* Merge host changed & referenced into pgste */
  709. pgste_val(new) |= bits << 52;
  710. }
  711. /* changing the guest storage key is considered a change of the page */
  712. if ((pgste_val(new) ^ pgste_val(old)) &
  713. (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
  714. pgste_val(new) |= PGSTE_UC_BIT;
  715. pgste_set_unlock(ptep, new);
  716. pte_unmap_unlock(ptep, ptl);
  717. return 0;
  718. }
  719. EXPORT_SYMBOL(set_guest_storage_key);
  720. /**
  721. * Conditionally set a guest storage key (handling csske).
  722. * oldkey will be updated when either mr or mc is set and a pointer is given.
  723. *
  724. * Returns 0 if a guests storage key update wasn't necessary, 1 if the guest
  725. * storage key was updated and -EFAULT on access errors.
  726. */
  727. int cond_set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
  728. unsigned char key, unsigned char *oldkey,
  729. bool nq, bool mr, bool mc)
  730. {
  731. unsigned char tmp, mask = _PAGE_ACC_BITS | _PAGE_FP_BIT;
  732. int rc;
  733. /* we can drop the pgste lock between getting and setting the key */
  734. if (mr | mc) {
  735. rc = get_guest_storage_key(current->mm, addr, &tmp);
  736. if (rc)
  737. return rc;
  738. if (oldkey)
  739. *oldkey = tmp;
  740. if (!mr)
  741. mask |= _PAGE_REFERENCED;
  742. if (!mc)
  743. mask |= _PAGE_CHANGED;
  744. if (!((tmp ^ key) & mask))
  745. return 0;
  746. }
  747. rc = set_guest_storage_key(current->mm, addr, key, nq);
  748. return rc < 0 ? rc : 1;
  749. }
  750. EXPORT_SYMBOL(cond_set_guest_storage_key);
  751. /**
  752. * Reset a guest reference bit (rrbe), returning the reference and changed bit.
  753. *
  754. * Returns < 0 in case of error, otherwise the cc to be reported to the guest.
  755. */
  756. int reset_guest_reference_bit(struct mm_struct *mm, unsigned long addr)
  757. {
  758. spinlock_t *ptl;
  759. pgste_t old, new;
  760. pte_t *ptep;
  761. int cc = 0;
  762. ptep = get_locked_pte(mm, addr, &ptl);
  763. if (unlikely(!ptep))
  764. return -EFAULT;
  765. new = old = pgste_get_lock(ptep);
  766. /* Reset guest reference bit only */
  767. pgste_val(new) &= ~PGSTE_GR_BIT;
  768. if (!(pte_val(*ptep) & _PAGE_INVALID)) {
  769. cc = page_reset_referenced(pte_val(*ptep) & PAGE_MASK);
  770. /* Merge real referenced bit into host-set */
  771. pgste_val(new) |= ((unsigned long) cc << 53) & PGSTE_HR_BIT;
  772. }
  773. /* Reflect guest's logical view, not physical */
  774. cc |= (pgste_val(old) & (PGSTE_GR_BIT | PGSTE_GC_BIT)) >> 49;
  775. /* Changing the guest storage key is considered a change of the page */
  776. if ((pgste_val(new) ^ pgste_val(old)) & PGSTE_GR_BIT)
  777. pgste_val(new) |= PGSTE_UC_BIT;
  778. pgste_set_unlock(ptep, new);
  779. pte_unmap_unlock(ptep, ptl);
  780. return cc;
  781. }
  782. EXPORT_SYMBOL(reset_guest_reference_bit);
  783. int get_guest_storage_key(struct mm_struct *mm, unsigned long addr,
  784. unsigned char *key)
  785. {
  786. spinlock_t *ptl;
  787. pgste_t pgste;
  788. pte_t *ptep;
  789. ptep = get_locked_pte(mm, addr, &ptl);
  790. if (unlikely(!ptep))
  791. return -EFAULT;
  792. pgste = pgste_get_lock(ptep);
  793. *key = (pgste_val(pgste) & (PGSTE_ACC_BITS | PGSTE_FP_BIT)) >> 56;
  794. if (!(pte_val(*ptep) & _PAGE_INVALID))
  795. *key = page_get_storage_key(pte_val(*ptep) & PAGE_MASK);
  796. /* Reflect guest's logical view, not physical */
  797. *key |= (pgste_val(pgste) & (PGSTE_GR_BIT | PGSTE_GC_BIT)) >> 48;
  798. pgste_set_unlock(ptep, pgste);
  799. pte_unmap_unlock(ptep, ptl);
  800. return 0;
  801. }
  802. EXPORT_SYMBOL(get_guest_storage_key);
  803. /**
  804. * pgste_perform_essa - perform ESSA actions on the PGSTE.
  805. * @mm: the memory context. It must have PGSTEs, no check is performed here!
  806. * @hva: the host virtual address of the page whose PGSTE is to be processed
  807. * @orc: the specific action to perform, see the ESSA_SET_* macros.
  808. * @oldpte: the PTE will be saved there if the pointer is not NULL.
  809. * @oldpgste: the old PGSTE will be saved there if the pointer is not NULL.
  810. *
  811. * Return: 1 if the page is to be added to the CBRL, otherwise 0,
  812. * or < 0 in case of error. -EINVAL is returned for invalid values
  813. * of orc, -EFAULT for invalid addresses.
  814. */
  815. int pgste_perform_essa(struct mm_struct *mm, unsigned long hva, int orc,
  816. unsigned long *oldpte, unsigned long *oldpgste)
  817. {
  818. unsigned long pgstev;
  819. spinlock_t *ptl;
  820. pgste_t pgste;
  821. pte_t *ptep;
  822. int res = 0;
  823. WARN_ON_ONCE(orc > ESSA_MAX);
  824. if (unlikely(orc > ESSA_MAX))
  825. return -EINVAL;
  826. ptep = get_locked_pte(mm, hva, &ptl);
  827. if (unlikely(!ptep))
  828. return -EFAULT;
  829. pgste = pgste_get_lock(ptep);
  830. pgstev = pgste_val(pgste);
  831. if (oldpte)
  832. *oldpte = pte_val(*ptep);
  833. if (oldpgste)
  834. *oldpgste = pgstev;
  835. switch (orc) {
  836. case ESSA_GET_STATE:
  837. break;
  838. case ESSA_SET_STABLE:
  839. pgstev &= ~(_PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT);
  840. pgstev |= _PGSTE_GPS_USAGE_STABLE;
  841. break;
  842. case ESSA_SET_UNUSED:
  843. pgstev &= ~_PGSTE_GPS_USAGE_MASK;
  844. pgstev |= _PGSTE_GPS_USAGE_UNUSED;
  845. if (pte_val(*ptep) & _PAGE_INVALID)
  846. res = 1;
  847. break;
  848. case ESSA_SET_VOLATILE:
  849. pgstev &= ~_PGSTE_GPS_USAGE_MASK;
  850. pgstev |= _PGSTE_GPS_USAGE_VOLATILE;
  851. if (pte_val(*ptep) & _PAGE_INVALID)
  852. res = 1;
  853. break;
  854. case ESSA_SET_POT_VOLATILE:
  855. pgstev &= ~_PGSTE_GPS_USAGE_MASK;
  856. if (!(pte_val(*ptep) & _PAGE_INVALID)) {
  857. pgstev |= _PGSTE_GPS_USAGE_POT_VOLATILE;
  858. break;
  859. }
  860. if (pgstev & _PGSTE_GPS_ZERO) {
  861. pgstev |= _PGSTE_GPS_USAGE_VOLATILE;
  862. break;
  863. }
  864. if (!(pgstev & PGSTE_GC_BIT)) {
  865. pgstev |= _PGSTE_GPS_USAGE_VOLATILE;
  866. res = 1;
  867. break;
  868. }
  869. break;
  870. case ESSA_SET_STABLE_RESIDENT:
  871. pgstev &= ~_PGSTE_GPS_USAGE_MASK;
  872. pgstev |= _PGSTE_GPS_USAGE_STABLE;
  873. /*
  874. * Since the resident state can go away any time after this
  875. * call, we will not make this page resident. We can revisit
  876. * this decision if a guest will ever start using this.
  877. */
  878. break;
  879. case ESSA_SET_STABLE_IF_RESIDENT:
  880. if (!(pte_val(*ptep) & _PAGE_INVALID)) {
  881. pgstev &= ~_PGSTE_GPS_USAGE_MASK;
  882. pgstev |= _PGSTE_GPS_USAGE_STABLE;
  883. }
  884. break;
  885. case ESSA_SET_STABLE_NODAT:
  886. pgstev &= ~_PGSTE_GPS_USAGE_MASK;
  887. pgstev |= _PGSTE_GPS_USAGE_STABLE | _PGSTE_GPS_NODAT;
  888. break;
  889. default:
  890. /* we should never get here! */
  891. break;
  892. }
  893. /* If we are discarding a page, set it to logical zero */
  894. if (res)
  895. pgstev |= _PGSTE_GPS_ZERO;
  896. pgste_val(pgste) = pgstev;
  897. pgste_set_unlock(ptep, pgste);
  898. pte_unmap_unlock(ptep, ptl);
  899. return res;
  900. }
  901. EXPORT_SYMBOL(pgste_perform_essa);
  902. /**
  903. * set_pgste_bits - set specific PGSTE bits.
  904. * @mm: the memory context. It must have PGSTEs, no check is performed here!
  905. * @hva: the host virtual address of the page whose PGSTE is to be processed
  906. * @bits: a bitmask representing the bits that will be touched
  907. * @value: the values of the bits to be written. Only the bits in the mask
  908. * will be written.
  909. *
  910. * Return: 0 on success, < 0 in case of error.
  911. */
  912. int set_pgste_bits(struct mm_struct *mm, unsigned long hva,
  913. unsigned long bits, unsigned long value)
  914. {
  915. spinlock_t *ptl;
  916. pgste_t new;
  917. pte_t *ptep;
  918. ptep = get_locked_pte(mm, hva, &ptl);
  919. if (unlikely(!ptep))
  920. return -EFAULT;
  921. new = pgste_get_lock(ptep);
  922. pgste_val(new) &= ~bits;
  923. pgste_val(new) |= value & bits;
  924. pgste_set_unlock(ptep, new);
  925. pte_unmap_unlock(ptep, ptl);
  926. return 0;
  927. }
  928. EXPORT_SYMBOL(set_pgste_bits);
  929. /**
  930. * get_pgste - get the current PGSTE for the given address.
  931. * @mm: the memory context. It must have PGSTEs, no check is performed here!
  932. * @hva: the host virtual address of the page whose PGSTE is to be processed
  933. * @pgstep: will be written with the current PGSTE for the given address.
  934. *
  935. * Return: 0 on success, < 0 in case of error.
  936. */
  937. int get_pgste(struct mm_struct *mm, unsigned long hva, unsigned long *pgstep)
  938. {
  939. spinlock_t *ptl;
  940. pte_t *ptep;
  941. ptep = get_locked_pte(mm, hva, &ptl);
  942. if (unlikely(!ptep))
  943. return -EFAULT;
  944. *pgstep = pgste_val(pgste_get(ptep));
  945. pte_unmap_unlock(ptep, ptl);
  946. return 0;
  947. }
  948. EXPORT_SYMBOL(get_pgste);
  949. #endif