pgtable.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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) && mm->context.allow_gmap_hpage_1m)
  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) && mm->context.allow_gmap_hpage_1m)
  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) && mm->context.allow_gmap_hpage_1m)
  329. gmap_pmdp_idte_global(mm, addr);
  330. } else {
  331. __pmdp_csp(pmdp);
  332. if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m)
  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. #ifdef CONFIG_PGSTE
  373. static pmd_t *pmd_alloc_map(struct mm_struct *mm, unsigned long addr)
  374. {
  375. pgd_t *pgd;
  376. p4d_t *p4d;
  377. pud_t *pud;
  378. pmd_t *pmd;
  379. pgd = pgd_offset(mm, addr);
  380. p4d = p4d_alloc(mm, pgd, addr);
  381. if (!p4d)
  382. return NULL;
  383. pud = pud_alloc(mm, p4d, addr);
  384. if (!pud)
  385. return NULL;
  386. pmd = pmd_alloc(mm, pud, addr);
  387. return pmd;
  388. }
  389. #endif
  390. pmd_t pmdp_xchg_direct(struct mm_struct *mm, unsigned long addr,
  391. pmd_t *pmdp, pmd_t new)
  392. {
  393. pmd_t old;
  394. preempt_disable();
  395. old = pmdp_flush_direct(mm, addr, pmdp);
  396. *pmdp = new;
  397. preempt_enable();
  398. return old;
  399. }
  400. EXPORT_SYMBOL(pmdp_xchg_direct);
  401. pmd_t pmdp_xchg_lazy(struct mm_struct *mm, unsigned long addr,
  402. pmd_t *pmdp, pmd_t new)
  403. {
  404. pmd_t old;
  405. preempt_disable();
  406. old = pmdp_flush_lazy(mm, addr, pmdp);
  407. *pmdp = new;
  408. preempt_enable();
  409. return old;
  410. }
  411. EXPORT_SYMBOL(pmdp_xchg_lazy);
  412. static inline void pudp_idte_local(struct mm_struct *mm,
  413. unsigned long addr, pud_t *pudp)
  414. {
  415. if (MACHINE_HAS_TLB_GUEST)
  416. __pudp_idte(addr, pudp, IDTE_NODAT | IDTE_GUEST_ASCE,
  417. mm->context.asce, IDTE_LOCAL);
  418. else
  419. __pudp_idte(addr, pudp, 0, 0, IDTE_LOCAL);
  420. }
  421. static inline void pudp_idte_global(struct mm_struct *mm,
  422. unsigned long addr, pud_t *pudp)
  423. {
  424. if (MACHINE_HAS_TLB_GUEST)
  425. __pudp_idte(addr, pudp, IDTE_NODAT | IDTE_GUEST_ASCE,
  426. mm->context.asce, IDTE_GLOBAL);
  427. else if (MACHINE_HAS_IDTE)
  428. __pudp_idte(addr, pudp, 0, 0, IDTE_GLOBAL);
  429. else
  430. /*
  431. * Invalid bit position is the same for pmd and pud, so we can
  432. * re-use _pmd_csp() here
  433. */
  434. __pmdp_csp((pmd_t *) pudp);
  435. }
  436. static inline pud_t pudp_flush_direct(struct mm_struct *mm,
  437. unsigned long addr, pud_t *pudp)
  438. {
  439. pud_t old;
  440. old = *pudp;
  441. if (pud_val(old) & _REGION_ENTRY_INVALID)
  442. return old;
  443. atomic_inc(&mm->context.flush_count);
  444. if (MACHINE_HAS_TLB_LC &&
  445. cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
  446. pudp_idte_local(mm, addr, pudp);
  447. else
  448. pudp_idte_global(mm, addr, pudp);
  449. atomic_dec(&mm->context.flush_count);
  450. return old;
  451. }
  452. pud_t pudp_xchg_direct(struct mm_struct *mm, unsigned long addr,
  453. pud_t *pudp, pud_t new)
  454. {
  455. pud_t old;
  456. preempt_disable();
  457. old = pudp_flush_direct(mm, addr, pudp);
  458. *pudp = new;
  459. preempt_enable();
  460. return old;
  461. }
  462. EXPORT_SYMBOL(pudp_xchg_direct);
  463. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  464. void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  465. pgtable_t pgtable)
  466. {
  467. struct list_head *lh = (struct list_head *) pgtable;
  468. assert_spin_locked(pmd_lockptr(mm, pmdp));
  469. /* FIFO */
  470. if (!pmd_huge_pte(mm, pmdp))
  471. INIT_LIST_HEAD(lh);
  472. else
  473. list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
  474. pmd_huge_pte(mm, pmdp) = pgtable;
  475. }
  476. pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  477. {
  478. struct list_head *lh;
  479. pgtable_t pgtable;
  480. pte_t *ptep;
  481. assert_spin_locked(pmd_lockptr(mm, pmdp));
  482. /* FIFO */
  483. pgtable = pmd_huge_pte(mm, pmdp);
  484. lh = (struct list_head *) pgtable;
  485. if (list_empty(lh))
  486. pmd_huge_pte(mm, pmdp) = NULL;
  487. else {
  488. pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
  489. list_del(lh);
  490. }
  491. ptep = (pte_t *) pgtable;
  492. pte_val(*ptep) = _PAGE_INVALID;
  493. ptep++;
  494. pte_val(*ptep) = _PAGE_INVALID;
  495. return pgtable;
  496. }
  497. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  498. #ifdef CONFIG_PGSTE
  499. void ptep_set_pte_at(struct mm_struct *mm, unsigned long addr,
  500. pte_t *ptep, pte_t entry)
  501. {
  502. pgste_t pgste;
  503. /* the mm_has_pgste() check is done in set_pte_at() */
  504. preempt_disable();
  505. pgste = pgste_get_lock(ptep);
  506. pgste_val(pgste) &= ~_PGSTE_GPS_ZERO;
  507. pgste_set_key(ptep, pgste, entry, mm);
  508. pgste = pgste_set_pte(ptep, pgste, entry);
  509. pgste_set_unlock(ptep, pgste);
  510. preempt_enable();
  511. }
  512. void ptep_set_notify(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  513. {
  514. pgste_t pgste;
  515. preempt_disable();
  516. pgste = pgste_get_lock(ptep);
  517. pgste_val(pgste) |= PGSTE_IN_BIT;
  518. pgste_set_unlock(ptep, pgste);
  519. preempt_enable();
  520. }
  521. /**
  522. * ptep_force_prot - change access rights of a locked pte
  523. * @mm: pointer to the process mm_struct
  524. * @addr: virtual address in the guest address space
  525. * @ptep: pointer to the page table entry
  526. * @prot: indicates guest access rights: PROT_NONE, PROT_READ or PROT_WRITE
  527. * @bit: pgste bit to set (e.g. for notification)
  528. *
  529. * Returns 0 if the access rights were changed and -EAGAIN if the current
  530. * and requested access rights are incompatible.
  531. */
  532. int ptep_force_prot(struct mm_struct *mm, unsigned long addr,
  533. pte_t *ptep, int prot, unsigned long bit)
  534. {
  535. pte_t entry;
  536. pgste_t pgste;
  537. int pte_i, pte_p, nodat;
  538. pgste = pgste_get_lock(ptep);
  539. entry = *ptep;
  540. /* Check pte entry after all locks have been acquired */
  541. pte_i = pte_val(entry) & _PAGE_INVALID;
  542. pte_p = pte_val(entry) & _PAGE_PROTECT;
  543. if ((pte_i && (prot != PROT_NONE)) ||
  544. (pte_p && (prot & PROT_WRITE))) {
  545. pgste_set_unlock(ptep, pgste);
  546. return -EAGAIN;
  547. }
  548. /* Change access rights and set pgste bit */
  549. nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
  550. if (prot == PROT_NONE && !pte_i) {
  551. ptep_flush_direct(mm, addr, ptep, nodat);
  552. pgste = pgste_update_all(entry, pgste, mm);
  553. pte_val(entry) |= _PAGE_INVALID;
  554. }
  555. if (prot == PROT_READ && !pte_p) {
  556. ptep_flush_direct(mm, addr, ptep, nodat);
  557. pte_val(entry) &= ~_PAGE_INVALID;
  558. pte_val(entry) |= _PAGE_PROTECT;
  559. }
  560. pgste_val(pgste) |= bit;
  561. pgste = pgste_set_pte(ptep, pgste, entry);
  562. pgste_set_unlock(ptep, pgste);
  563. return 0;
  564. }
  565. int ptep_shadow_pte(struct mm_struct *mm, unsigned long saddr,
  566. pte_t *sptep, pte_t *tptep, pte_t pte)
  567. {
  568. pgste_t spgste, tpgste;
  569. pte_t spte, tpte;
  570. int rc = -EAGAIN;
  571. if (!(pte_val(*tptep) & _PAGE_INVALID))
  572. return 0; /* already shadowed */
  573. spgste = pgste_get_lock(sptep);
  574. spte = *sptep;
  575. if (!(pte_val(spte) & _PAGE_INVALID) &&
  576. !((pte_val(spte) & _PAGE_PROTECT) &&
  577. !(pte_val(pte) & _PAGE_PROTECT))) {
  578. pgste_val(spgste) |= PGSTE_VSIE_BIT;
  579. tpgste = pgste_get_lock(tptep);
  580. pte_val(tpte) = (pte_val(spte) & PAGE_MASK) |
  581. (pte_val(pte) & _PAGE_PROTECT);
  582. /* don't touch the storage key - it belongs to parent pgste */
  583. tpgste = pgste_set_pte(tptep, tpgste, tpte);
  584. pgste_set_unlock(tptep, tpgste);
  585. rc = 1;
  586. }
  587. pgste_set_unlock(sptep, spgste);
  588. return rc;
  589. }
  590. void ptep_unshadow_pte(struct mm_struct *mm, unsigned long saddr, pte_t *ptep)
  591. {
  592. pgste_t pgste;
  593. int nodat;
  594. pgste = pgste_get_lock(ptep);
  595. /* notifier is called by the caller */
  596. nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
  597. ptep_flush_direct(mm, saddr, ptep, nodat);
  598. /* don't touch the storage key - it belongs to parent pgste */
  599. pgste = pgste_set_pte(ptep, pgste, __pte(_PAGE_INVALID));
  600. pgste_set_unlock(ptep, pgste);
  601. }
  602. static void ptep_zap_swap_entry(struct mm_struct *mm, swp_entry_t entry)
  603. {
  604. if (!non_swap_entry(entry))
  605. dec_mm_counter(mm, MM_SWAPENTS);
  606. else if (is_migration_entry(entry)) {
  607. struct page *page = migration_entry_to_page(entry);
  608. dec_mm_counter(mm, mm_counter(page));
  609. }
  610. free_swap_and_cache(entry);
  611. }
  612. void ptep_zap_unused(struct mm_struct *mm, unsigned long addr,
  613. pte_t *ptep, int reset)
  614. {
  615. unsigned long pgstev;
  616. pgste_t pgste;
  617. pte_t pte;
  618. /* Zap unused and logically-zero pages */
  619. preempt_disable();
  620. pgste = pgste_get_lock(ptep);
  621. pgstev = pgste_val(pgste);
  622. pte = *ptep;
  623. if (!reset && pte_swap(pte) &&
  624. ((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED ||
  625. (pgstev & _PGSTE_GPS_ZERO))) {
  626. ptep_zap_swap_entry(mm, pte_to_swp_entry(pte));
  627. pte_clear(mm, addr, ptep);
  628. }
  629. if (reset)
  630. pgste_val(pgste) &= ~_PGSTE_GPS_USAGE_MASK;
  631. pgste_set_unlock(ptep, pgste);
  632. preempt_enable();
  633. }
  634. void ptep_zap_key(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  635. {
  636. unsigned long ptev;
  637. pgste_t pgste;
  638. /* Clear storage key ACC and F, but set R/C */
  639. preempt_disable();
  640. pgste = pgste_get_lock(ptep);
  641. pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT);
  642. pgste_val(pgste) |= PGSTE_GR_BIT | PGSTE_GC_BIT;
  643. ptev = pte_val(*ptep);
  644. if (!(ptev & _PAGE_INVALID) && (ptev & _PAGE_WRITE))
  645. page_set_storage_key(ptev & PAGE_MASK, PAGE_DEFAULT_KEY, 1);
  646. pgste_set_unlock(ptep, pgste);
  647. preempt_enable();
  648. }
  649. /*
  650. * Test and reset if a guest page is dirty
  651. */
  652. bool ptep_test_and_clear_uc(struct mm_struct *mm, unsigned long addr,
  653. pte_t *ptep)
  654. {
  655. pgste_t pgste;
  656. pte_t pte;
  657. bool dirty;
  658. int nodat;
  659. pgste = pgste_get_lock(ptep);
  660. dirty = !!(pgste_val(pgste) & PGSTE_UC_BIT);
  661. pgste_val(pgste) &= ~PGSTE_UC_BIT;
  662. pte = *ptep;
  663. if (dirty && (pte_val(pte) & _PAGE_PRESENT)) {
  664. pgste = pgste_pte_notify(mm, addr, ptep, pgste);
  665. nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
  666. ptep_ipte_global(mm, addr, ptep, nodat);
  667. if (MACHINE_HAS_ESOP || !(pte_val(pte) & _PAGE_WRITE))
  668. pte_val(pte) |= _PAGE_PROTECT;
  669. else
  670. pte_val(pte) |= _PAGE_INVALID;
  671. *ptep = pte;
  672. }
  673. pgste_set_unlock(ptep, pgste);
  674. return dirty;
  675. }
  676. EXPORT_SYMBOL_GPL(ptep_test_and_clear_uc);
  677. int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
  678. unsigned char key, bool nq)
  679. {
  680. unsigned long keyul, paddr;
  681. spinlock_t *ptl;
  682. pgste_t old, new;
  683. pmd_t *pmdp;
  684. pte_t *ptep;
  685. pmdp = pmd_alloc_map(mm, addr);
  686. if (unlikely(!pmdp))
  687. return -EFAULT;
  688. ptl = pmd_lock(mm, pmdp);
  689. if (!pmd_present(*pmdp)) {
  690. spin_unlock(ptl);
  691. return -EFAULT;
  692. }
  693. if (pmd_large(*pmdp)) {
  694. paddr = pmd_val(*pmdp) & HPAGE_MASK;
  695. paddr |= addr & ~HPAGE_MASK;
  696. /*
  697. * Huge pmds need quiescing operations, they are
  698. * always mapped.
  699. */
  700. page_set_storage_key(paddr, key, 1);
  701. spin_unlock(ptl);
  702. return 0;
  703. }
  704. spin_unlock(ptl);
  705. ptep = pte_alloc_map_lock(mm, pmdp, addr, &ptl);
  706. if (unlikely(!ptep))
  707. return -EFAULT;
  708. new = old = pgste_get_lock(ptep);
  709. pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
  710. PGSTE_ACC_BITS | PGSTE_FP_BIT);
  711. keyul = (unsigned long) key;
  712. pgste_val(new) |= (keyul & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
  713. pgste_val(new) |= (keyul & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
  714. if (!(pte_val(*ptep) & _PAGE_INVALID)) {
  715. unsigned long bits, skey;
  716. paddr = pte_val(*ptep) & PAGE_MASK;
  717. skey = (unsigned long) page_get_storage_key(paddr);
  718. bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
  719. skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
  720. /* Set storage key ACC and FP */
  721. page_set_storage_key(paddr, skey, !nq);
  722. /* Merge host changed & referenced into pgste */
  723. pgste_val(new) |= bits << 52;
  724. }
  725. /* changing the guest storage key is considered a change of the page */
  726. if ((pgste_val(new) ^ pgste_val(old)) &
  727. (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
  728. pgste_val(new) |= PGSTE_UC_BIT;
  729. pgste_set_unlock(ptep, new);
  730. pte_unmap_unlock(ptep, ptl);
  731. return 0;
  732. }
  733. EXPORT_SYMBOL(set_guest_storage_key);
  734. /**
  735. * Conditionally set a guest storage key (handling csske).
  736. * oldkey will be updated when either mr or mc is set and a pointer is given.
  737. *
  738. * Returns 0 if a guests storage key update wasn't necessary, 1 if the guest
  739. * storage key was updated and -EFAULT on access errors.
  740. */
  741. int cond_set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
  742. unsigned char key, unsigned char *oldkey,
  743. bool nq, bool mr, bool mc)
  744. {
  745. unsigned char tmp, mask = _PAGE_ACC_BITS | _PAGE_FP_BIT;
  746. int rc;
  747. /* we can drop the pgste lock between getting and setting the key */
  748. if (mr | mc) {
  749. rc = get_guest_storage_key(current->mm, addr, &tmp);
  750. if (rc)
  751. return rc;
  752. if (oldkey)
  753. *oldkey = tmp;
  754. if (!mr)
  755. mask |= _PAGE_REFERENCED;
  756. if (!mc)
  757. mask |= _PAGE_CHANGED;
  758. if (!((tmp ^ key) & mask))
  759. return 0;
  760. }
  761. rc = set_guest_storage_key(current->mm, addr, key, nq);
  762. return rc < 0 ? rc : 1;
  763. }
  764. EXPORT_SYMBOL(cond_set_guest_storage_key);
  765. /**
  766. * Reset a guest reference bit (rrbe), returning the reference and changed bit.
  767. *
  768. * Returns < 0 in case of error, otherwise the cc to be reported to the guest.
  769. */
  770. int reset_guest_reference_bit(struct mm_struct *mm, unsigned long addr)
  771. {
  772. spinlock_t *ptl;
  773. unsigned long paddr;
  774. pgste_t old, new;
  775. pmd_t *pmdp;
  776. pte_t *ptep;
  777. int cc = 0;
  778. pmdp = pmd_alloc_map(mm, addr);
  779. if (unlikely(!pmdp))
  780. return -EFAULT;
  781. ptl = pmd_lock(mm, pmdp);
  782. if (!pmd_present(*pmdp)) {
  783. spin_unlock(ptl);
  784. return -EFAULT;
  785. }
  786. if (pmd_large(*pmdp)) {
  787. paddr = pmd_val(*pmdp) & HPAGE_MASK;
  788. paddr |= addr & ~HPAGE_MASK;
  789. cc = page_reset_referenced(paddr);
  790. spin_unlock(ptl);
  791. return cc;
  792. }
  793. spin_unlock(ptl);
  794. ptep = pte_alloc_map_lock(mm, pmdp, addr, &ptl);
  795. if (unlikely(!ptep))
  796. return -EFAULT;
  797. new = old = pgste_get_lock(ptep);
  798. /* Reset guest reference bit only */
  799. pgste_val(new) &= ~PGSTE_GR_BIT;
  800. if (!(pte_val(*ptep) & _PAGE_INVALID)) {
  801. paddr = pte_val(*ptep) & PAGE_MASK;
  802. cc = page_reset_referenced(paddr);
  803. /* Merge real referenced bit into host-set */
  804. pgste_val(new) |= ((unsigned long) cc << 53) & PGSTE_HR_BIT;
  805. }
  806. /* Reflect guest's logical view, not physical */
  807. cc |= (pgste_val(old) & (PGSTE_GR_BIT | PGSTE_GC_BIT)) >> 49;
  808. /* Changing the guest storage key is considered a change of the page */
  809. if ((pgste_val(new) ^ pgste_val(old)) & PGSTE_GR_BIT)
  810. pgste_val(new) |= PGSTE_UC_BIT;
  811. pgste_set_unlock(ptep, new);
  812. pte_unmap_unlock(ptep, ptl);
  813. return cc;
  814. }
  815. EXPORT_SYMBOL(reset_guest_reference_bit);
  816. int get_guest_storage_key(struct mm_struct *mm, unsigned long addr,
  817. unsigned char *key)
  818. {
  819. unsigned long paddr;
  820. spinlock_t *ptl;
  821. pgste_t pgste;
  822. pmd_t *pmdp;
  823. pte_t *ptep;
  824. pmdp = pmd_alloc_map(mm, addr);
  825. if (unlikely(!pmdp))
  826. return -EFAULT;
  827. ptl = pmd_lock(mm, pmdp);
  828. if (!pmd_present(*pmdp)) {
  829. /* Not yet mapped memory has a zero key */
  830. spin_unlock(ptl);
  831. *key = 0;
  832. return 0;
  833. }
  834. if (pmd_large(*pmdp)) {
  835. paddr = pmd_val(*pmdp) & HPAGE_MASK;
  836. paddr |= addr & ~HPAGE_MASK;
  837. *key = page_get_storage_key(paddr);
  838. spin_unlock(ptl);
  839. return 0;
  840. }
  841. spin_unlock(ptl);
  842. ptep = pte_alloc_map_lock(mm, pmdp, addr, &ptl);
  843. if (unlikely(!ptep))
  844. return -EFAULT;
  845. pgste = pgste_get_lock(ptep);
  846. *key = (pgste_val(pgste) & (PGSTE_ACC_BITS | PGSTE_FP_BIT)) >> 56;
  847. paddr = pte_val(*ptep) & PAGE_MASK;
  848. if (!(pte_val(*ptep) & _PAGE_INVALID))
  849. *key = page_get_storage_key(paddr);
  850. /* Reflect guest's logical view, not physical */
  851. *key |= (pgste_val(pgste) & (PGSTE_GR_BIT | PGSTE_GC_BIT)) >> 48;
  852. pgste_set_unlock(ptep, pgste);
  853. pte_unmap_unlock(ptep, ptl);
  854. return 0;
  855. }
  856. EXPORT_SYMBOL(get_guest_storage_key);
  857. /**
  858. * pgste_perform_essa - perform ESSA actions on the PGSTE.
  859. * @mm: the memory context. It must have PGSTEs, no check is performed here!
  860. * @hva: the host virtual address of the page whose PGSTE is to be processed
  861. * @orc: the specific action to perform, see the ESSA_SET_* macros.
  862. * @oldpte: the PTE will be saved there if the pointer is not NULL.
  863. * @oldpgste: the old PGSTE will be saved there if the pointer is not NULL.
  864. *
  865. * Return: 1 if the page is to be added to the CBRL, otherwise 0,
  866. * or < 0 in case of error. -EINVAL is returned for invalid values
  867. * of orc, -EFAULT for invalid addresses.
  868. */
  869. int pgste_perform_essa(struct mm_struct *mm, unsigned long hva, int orc,
  870. unsigned long *oldpte, unsigned long *oldpgste)
  871. {
  872. unsigned long pgstev;
  873. spinlock_t *ptl;
  874. pgste_t pgste;
  875. pte_t *ptep;
  876. int res = 0;
  877. WARN_ON_ONCE(orc > ESSA_MAX);
  878. if (unlikely(orc > ESSA_MAX))
  879. return -EINVAL;
  880. ptep = get_locked_pte(mm, hva, &ptl);
  881. if (unlikely(!ptep))
  882. return -EFAULT;
  883. pgste = pgste_get_lock(ptep);
  884. pgstev = pgste_val(pgste);
  885. if (oldpte)
  886. *oldpte = pte_val(*ptep);
  887. if (oldpgste)
  888. *oldpgste = pgstev;
  889. switch (orc) {
  890. case ESSA_GET_STATE:
  891. break;
  892. case ESSA_SET_STABLE:
  893. pgstev &= ~(_PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT);
  894. pgstev |= _PGSTE_GPS_USAGE_STABLE;
  895. break;
  896. case ESSA_SET_UNUSED:
  897. pgstev &= ~_PGSTE_GPS_USAGE_MASK;
  898. pgstev |= _PGSTE_GPS_USAGE_UNUSED;
  899. if (pte_val(*ptep) & _PAGE_INVALID)
  900. res = 1;
  901. break;
  902. case ESSA_SET_VOLATILE:
  903. pgstev &= ~_PGSTE_GPS_USAGE_MASK;
  904. pgstev |= _PGSTE_GPS_USAGE_VOLATILE;
  905. if (pte_val(*ptep) & _PAGE_INVALID)
  906. res = 1;
  907. break;
  908. case ESSA_SET_POT_VOLATILE:
  909. pgstev &= ~_PGSTE_GPS_USAGE_MASK;
  910. if (!(pte_val(*ptep) & _PAGE_INVALID)) {
  911. pgstev |= _PGSTE_GPS_USAGE_POT_VOLATILE;
  912. break;
  913. }
  914. if (pgstev & _PGSTE_GPS_ZERO) {
  915. pgstev |= _PGSTE_GPS_USAGE_VOLATILE;
  916. break;
  917. }
  918. if (!(pgstev & PGSTE_GC_BIT)) {
  919. pgstev |= _PGSTE_GPS_USAGE_VOLATILE;
  920. res = 1;
  921. break;
  922. }
  923. break;
  924. case ESSA_SET_STABLE_RESIDENT:
  925. pgstev &= ~_PGSTE_GPS_USAGE_MASK;
  926. pgstev |= _PGSTE_GPS_USAGE_STABLE;
  927. /*
  928. * Since the resident state can go away any time after this
  929. * call, we will not make this page resident. We can revisit
  930. * this decision if a guest will ever start using this.
  931. */
  932. break;
  933. case ESSA_SET_STABLE_IF_RESIDENT:
  934. if (!(pte_val(*ptep) & _PAGE_INVALID)) {
  935. pgstev &= ~_PGSTE_GPS_USAGE_MASK;
  936. pgstev |= _PGSTE_GPS_USAGE_STABLE;
  937. }
  938. break;
  939. case ESSA_SET_STABLE_NODAT:
  940. pgstev &= ~_PGSTE_GPS_USAGE_MASK;
  941. pgstev |= _PGSTE_GPS_USAGE_STABLE | _PGSTE_GPS_NODAT;
  942. break;
  943. default:
  944. /* we should never get here! */
  945. break;
  946. }
  947. /* If we are discarding a page, set it to logical zero */
  948. if (res)
  949. pgstev |= _PGSTE_GPS_ZERO;
  950. pgste_val(pgste) = pgstev;
  951. pgste_set_unlock(ptep, pgste);
  952. pte_unmap_unlock(ptep, ptl);
  953. return res;
  954. }
  955. EXPORT_SYMBOL(pgste_perform_essa);
  956. /**
  957. * set_pgste_bits - set specific PGSTE bits.
  958. * @mm: the memory context. It must have PGSTEs, no check is performed here!
  959. * @hva: the host virtual address of the page whose PGSTE is to be processed
  960. * @bits: a bitmask representing the bits that will be touched
  961. * @value: the values of the bits to be written. Only the bits in the mask
  962. * will be written.
  963. *
  964. * Return: 0 on success, < 0 in case of error.
  965. */
  966. int set_pgste_bits(struct mm_struct *mm, unsigned long hva,
  967. unsigned long bits, unsigned long value)
  968. {
  969. spinlock_t *ptl;
  970. pgste_t new;
  971. pte_t *ptep;
  972. ptep = get_locked_pte(mm, hva, &ptl);
  973. if (unlikely(!ptep))
  974. return -EFAULT;
  975. new = pgste_get_lock(ptep);
  976. pgste_val(new) &= ~bits;
  977. pgste_val(new) |= value & bits;
  978. pgste_set_unlock(ptep, new);
  979. pte_unmap_unlock(ptep, ptl);
  980. return 0;
  981. }
  982. EXPORT_SYMBOL(set_pgste_bits);
  983. /**
  984. * get_pgste - get the current PGSTE for the given address.
  985. * @mm: the memory context. It must have PGSTEs, no check is performed here!
  986. * @hva: the host virtual address of the page whose PGSTE is to be processed
  987. * @pgstep: will be written with the current PGSTE for the given address.
  988. *
  989. * Return: 0 on success, < 0 in case of error.
  990. */
  991. int get_pgste(struct mm_struct *mm, unsigned long hva, unsigned long *pgstep)
  992. {
  993. spinlock_t *ptl;
  994. pte_t *ptep;
  995. ptep = get_locked_pte(mm, hva, &ptl);
  996. if (unlikely(!ptep))
  997. return -EFAULT;
  998. *pgstep = pgste_val(pgste_get(ptep));
  999. pte_unmap_unlock(ptep, ptl);
  1000. return 0;
  1001. }
  1002. EXPORT_SYMBOL(get_pgste);
  1003. #endif