book3s_64_mmu_hv.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. *
  15. * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  16. */
  17. #include <linux/types.h>
  18. #include <linux/string.h>
  19. #include <linux/kvm.h>
  20. #include <linux/kvm_host.h>
  21. #include <linux/highmem.h>
  22. #include <linux/gfp.h>
  23. #include <linux/slab.h>
  24. #include <linux/hugetlb.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/srcu.h>
  27. #include <linux/anon_inodes.h>
  28. #include <linux/file.h>
  29. #include <linux/debugfs.h>
  30. #include <asm/tlbflush.h>
  31. #include <asm/kvm_ppc.h>
  32. #include <asm/kvm_book3s.h>
  33. #include <asm/book3s/64/mmu-hash.h>
  34. #include <asm/hvcall.h>
  35. #include <asm/synch.h>
  36. #include <asm/ppc-opcode.h>
  37. #include <asm/cputable.h>
  38. #include <asm/pte-walk.h>
  39. #include "trace_hv.h"
  40. //#define DEBUG_RESIZE_HPT 1
  41. #ifdef DEBUG_RESIZE_HPT
  42. #define resize_hpt_debug(resize, ...) \
  43. do { \
  44. printk(KERN_DEBUG "RESIZE HPT %p: ", resize); \
  45. printk(__VA_ARGS__); \
  46. } while (0)
  47. #else
  48. #define resize_hpt_debug(resize, ...) \
  49. do { } while (0)
  50. #endif
  51. static long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
  52. long pte_index, unsigned long pteh,
  53. unsigned long ptel, unsigned long *pte_idx_ret);
  54. struct kvm_resize_hpt {
  55. /* These fields read-only after init */
  56. struct kvm *kvm;
  57. struct work_struct work;
  58. u32 order;
  59. /* These fields protected by kvm->lock */
  60. /* Possible values and their usage:
  61. * <0 an error occurred during allocation,
  62. * -EBUSY allocation is in the progress,
  63. * 0 allocation made successfuly.
  64. */
  65. int error;
  66. /* Private to the work thread, until error != -EBUSY,
  67. * then protected by kvm->lock.
  68. */
  69. struct kvm_hpt_info hpt;
  70. };
  71. int kvmppc_allocate_hpt(struct kvm_hpt_info *info, u32 order)
  72. {
  73. unsigned long hpt = 0;
  74. int cma = 0;
  75. struct page *page = NULL;
  76. struct revmap_entry *rev;
  77. unsigned long npte;
  78. if ((order < PPC_MIN_HPT_ORDER) || (order > PPC_MAX_HPT_ORDER))
  79. return -EINVAL;
  80. page = kvm_alloc_hpt_cma(1ul << (order - PAGE_SHIFT));
  81. if (page) {
  82. hpt = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
  83. memset((void *)hpt, 0, (1ul << order));
  84. cma = 1;
  85. }
  86. if (!hpt)
  87. hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_RETRY_MAYFAIL
  88. |__GFP_NOWARN, order - PAGE_SHIFT);
  89. if (!hpt)
  90. return -ENOMEM;
  91. /* HPTEs are 2**4 bytes long */
  92. npte = 1ul << (order - 4);
  93. /* Allocate reverse map array */
  94. rev = vmalloc(sizeof(struct revmap_entry) * npte);
  95. if (!rev) {
  96. if (cma)
  97. kvm_free_hpt_cma(page, 1 << (order - PAGE_SHIFT));
  98. else
  99. free_pages(hpt, order - PAGE_SHIFT);
  100. return -ENOMEM;
  101. }
  102. info->order = order;
  103. info->virt = hpt;
  104. info->cma = cma;
  105. info->rev = rev;
  106. return 0;
  107. }
  108. void kvmppc_set_hpt(struct kvm *kvm, struct kvm_hpt_info *info)
  109. {
  110. atomic64_set(&kvm->arch.mmio_update, 0);
  111. kvm->arch.hpt = *info;
  112. kvm->arch.sdr1 = __pa(info->virt) | (info->order - 18);
  113. pr_debug("KVM guest htab at %lx (order %ld), LPID %x\n",
  114. info->virt, (long)info->order, kvm->arch.lpid);
  115. }
  116. long kvmppc_alloc_reset_hpt(struct kvm *kvm, int order)
  117. {
  118. long err = -EBUSY;
  119. struct kvm_hpt_info info;
  120. mutex_lock(&kvm->lock);
  121. if (kvm->arch.mmu_ready) {
  122. kvm->arch.mmu_ready = 0;
  123. /* order mmu_ready vs. vcpus_running */
  124. smp_mb();
  125. if (atomic_read(&kvm->arch.vcpus_running)) {
  126. kvm->arch.mmu_ready = 1;
  127. goto out;
  128. }
  129. }
  130. if (kvm_is_radix(kvm)) {
  131. err = kvmppc_switch_mmu_to_hpt(kvm);
  132. if (err)
  133. goto out;
  134. }
  135. if (kvm->arch.hpt.order == order) {
  136. /* We already have a suitable HPT */
  137. /* Set the entire HPT to 0, i.e. invalid HPTEs */
  138. memset((void *)kvm->arch.hpt.virt, 0, 1ul << order);
  139. /*
  140. * Reset all the reverse-mapping chains for all memslots
  141. */
  142. kvmppc_rmap_reset(kvm);
  143. err = 0;
  144. goto out;
  145. }
  146. if (kvm->arch.hpt.virt) {
  147. kvmppc_free_hpt(&kvm->arch.hpt);
  148. kvmppc_rmap_reset(kvm);
  149. }
  150. err = kvmppc_allocate_hpt(&info, order);
  151. if (err < 0)
  152. goto out;
  153. kvmppc_set_hpt(kvm, &info);
  154. out:
  155. if (err == 0)
  156. /* Ensure that each vcpu will flush its TLB on next entry. */
  157. cpumask_setall(&kvm->arch.need_tlb_flush);
  158. mutex_unlock(&kvm->lock);
  159. return err;
  160. }
  161. void kvmppc_free_hpt(struct kvm_hpt_info *info)
  162. {
  163. vfree(info->rev);
  164. info->rev = NULL;
  165. if (info->cma)
  166. kvm_free_hpt_cma(virt_to_page(info->virt),
  167. 1 << (info->order - PAGE_SHIFT));
  168. else if (info->virt)
  169. free_pages(info->virt, info->order - PAGE_SHIFT);
  170. info->virt = 0;
  171. info->order = 0;
  172. }
  173. /* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
  174. static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize)
  175. {
  176. return (pgsize > 0x1000) ? HPTE_V_LARGE : 0;
  177. }
  178. /* Bits in second HPTE dword for pagesize 4k, 64k or 16M */
  179. static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize)
  180. {
  181. return (pgsize == 0x10000) ? 0x1000 : 0;
  182. }
  183. void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
  184. unsigned long porder)
  185. {
  186. unsigned long i;
  187. unsigned long npages;
  188. unsigned long hp_v, hp_r;
  189. unsigned long addr, hash;
  190. unsigned long psize;
  191. unsigned long hp0, hp1;
  192. unsigned long idx_ret;
  193. long ret;
  194. struct kvm *kvm = vcpu->kvm;
  195. psize = 1ul << porder;
  196. npages = memslot->npages >> (porder - PAGE_SHIFT);
  197. /* VRMA can't be > 1TB */
  198. if (npages > 1ul << (40 - porder))
  199. npages = 1ul << (40 - porder);
  200. /* Can't use more than 1 HPTE per HPTEG */
  201. if (npages > kvmppc_hpt_mask(&kvm->arch.hpt) + 1)
  202. npages = kvmppc_hpt_mask(&kvm->arch.hpt) + 1;
  203. hp0 = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
  204. HPTE_V_BOLTED | hpte0_pgsize_encoding(psize);
  205. hp1 = hpte1_pgsize_encoding(psize) |
  206. HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX;
  207. for (i = 0; i < npages; ++i) {
  208. addr = i << porder;
  209. /* can't use hpt_hash since va > 64 bits */
  210. hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25)))
  211. & kvmppc_hpt_mask(&kvm->arch.hpt);
  212. /*
  213. * We assume that the hash table is empty and no
  214. * vcpus are using it at this stage. Since we create
  215. * at most one HPTE per HPTEG, we just assume entry 7
  216. * is available and use it.
  217. */
  218. hash = (hash << 3) + 7;
  219. hp_v = hp0 | ((addr >> 16) & ~0x7fUL);
  220. hp_r = hp1 | addr;
  221. ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, hash, hp_v, hp_r,
  222. &idx_ret);
  223. if (ret != H_SUCCESS) {
  224. pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
  225. addr, ret);
  226. break;
  227. }
  228. }
  229. }
  230. int kvmppc_mmu_hv_init(void)
  231. {
  232. unsigned long host_lpid, rsvd_lpid;
  233. if (!cpu_has_feature(CPU_FTR_HVMODE))
  234. return -EINVAL;
  235. /* POWER7 has 10-bit LPIDs (12-bit in POWER8) */
  236. host_lpid = mfspr(SPRN_LPID);
  237. rsvd_lpid = LPID_RSVD;
  238. kvmppc_init_lpid(rsvd_lpid + 1);
  239. kvmppc_claim_lpid(host_lpid);
  240. /* rsvd_lpid is reserved for use in partition switching */
  241. kvmppc_claim_lpid(rsvd_lpid);
  242. return 0;
  243. }
  244. static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
  245. {
  246. unsigned long msr = vcpu->arch.intr_msr;
  247. /* If transactional, change to suspend mode on IRQ delivery */
  248. if (MSR_TM_TRANSACTIONAL(vcpu->arch.shregs.msr))
  249. msr |= MSR_TS_S;
  250. else
  251. msr |= vcpu->arch.shregs.msr & MSR_TS_MASK;
  252. kvmppc_set_msr(vcpu, msr);
  253. }
  254. static long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
  255. long pte_index, unsigned long pteh,
  256. unsigned long ptel, unsigned long *pte_idx_ret)
  257. {
  258. long ret;
  259. /* Protect linux PTE lookup from page table destruction */
  260. rcu_read_lock_sched(); /* this disables preemption too */
  261. ret = kvmppc_do_h_enter(kvm, flags, pte_index, pteh, ptel,
  262. current->mm->pgd, false, pte_idx_ret);
  263. rcu_read_unlock_sched();
  264. if (ret == H_TOO_HARD) {
  265. /* this can't happen */
  266. pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
  267. ret = H_RESOURCE; /* or something */
  268. }
  269. return ret;
  270. }
  271. static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu,
  272. gva_t eaddr)
  273. {
  274. u64 mask;
  275. int i;
  276. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  277. if (!(vcpu->arch.slb[i].orige & SLB_ESID_V))
  278. continue;
  279. if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T)
  280. mask = ESID_MASK_1T;
  281. else
  282. mask = ESID_MASK;
  283. if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0)
  284. return &vcpu->arch.slb[i];
  285. }
  286. return NULL;
  287. }
  288. static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r,
  289. unsigned long ea)
  290. {
  291. unsigned long ra_mask;
  292. ra_mask = kvmppc_actual_pgsz(v, r) - 1;
  293. return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask);
  294. }
  295. static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
  296. struct kvmppc_pte *gpte, bool data, bool iswrite)
  297. {
  298. struct kvm *kvm = vcpu->kvm;
  299. struct kvmppc_slb *slbe;
  300. unsigned long slb_v;
  301. unsigned long pp, key;
  302. unsigned long v, orig_v, gr;
  303. __be64 *hptep;
  304. int index;
  305. int virtmode = vcpu->arch.shregs.msr & (data ? MSR_DR : MSR_IR);
  306. if (kvm_is_radix(vcpu->kvm))
  307. return kvmppc_mmu_radix_xlate(vcpu, eaddr, gpte, data, iswrite);
  308. /* Get SLB entry */
  309. if (virtmode) {
  310. slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr);
  311. if (!slbe)
  312. return -EINVAL;
  313. slb_v = slbe->origv;
  314. } else {
  315. /* real mode access */
  316. slb_v = vcpu->kvm->arch.vrma_slb_v;
  317. }
  318. preempt_disable();
  319. /* Find the HPTE in the hash table */
  320. index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v,
  321. HPTE_V_VALID | HPTE_V_ABSENT);
  322. if (index < 0) {
  323. preempt_enable();
  324. return -ENOENT;
  325. }
  326. hptep = (__be64 *)(kvm->arch.hpt.virt + (index << 4));
  327. v = orig_v = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK;
  328. if (cpu_has_feature(CPU_FTR_ARCH_300))
  329. v = hpte_new_to_old_v(v, be64_to_cpu(hptep[1]));
  330. gr = kvm->arch.hpt.rev[index].guest_rpte;
  331. unlock_hpte(hptep, orig_v);
  332. preempt_enable();
  333. gpte->eaddr = eaddr;
  334. gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff);
  335. /* Get PP bits and key for permission check */
  336. pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
  337. key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
  338. key &= slb_v;
  339. /* Calculate permissions */
  340. gpte->may_read = hpte_read_permission(pp, key);
  341. gpte->may_write = hpte_write_permission(pp, key);
  342. gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G));
  343. /* Storage key permission check for POWER7 */
  344. if (data && virtmode) {
  345. int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr);
  346. if (amrfield & 1)
  347. gpte->may_read = 0;
  348. if (amrfield & 2)
  349. gpte->may_write = 0;
  350. }
  351. /* Get the guest physical address */
  352. gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr);
  353. return 0;
  354. }
  355. /*
  356. * Quick test for whether an instruction is a load or a store.
  357. * If the instruction is a load or a store, then this will indicate
  358. * which it is, at least on server processors. (Embedded processors
  359. * have some external PID instructions that don't follow the rule
  360. * embodied here.) If the instruction isn't a load or store, then
  361. * this doesn't return anything useful.
  362. */
  363. static int instruction_is_store(unsigned int instr)
  364. {
  365. unsigned int mask;
  366. mask = 0x10000000;
  367. if ((instr & 0xfc000000) == 0x7c000000)
  368. mask = 0x100; /* major opcode 31 */
  369. return (instr & mask) != 0;
  370. }
  371. int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu,
  372. unsigned long gpa, gva_t ea, int is_store)
  373. {
  374. u32 last_inst;
  375. /*
  376. * If we fail, we just return to the guest and try executing it again.
  377. */
  378. if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
  379. EMULATE_DONE)
  380. return RESUME_GUEST;
  381. /*
  382. * WARNING: We do not know for sure whether the instruction we just
  383. * read from memory is the same that caused the fault in the first
  384. * place. If the instruction we read is neither an load or a store,
  385. * then it can't access memory, so we don't need to worry about
  386. * enforcing access permissions. So, assuming it is a load or
  387. * store, we just check that its direction (load or store) is
  388. * consistent with the original fault, since that's what we
  389. * checked the access permissions against. If there is a mismatch
  390. * we just return and retry the instruction.
  391. */
  392. if (instruction_is_store(last_inst) != !!is_store)
  393. return RESUME_GUEST;
  394. /*
  395. * Emulated accesses are emulated by looking at the hash for
  396. * translation once, then performing the access later. The
  397. * translation could be invalidated in the meantime in which
  398. * point performing the subsequent memory access on the old
  399. * physical address could possibly be a security hole for the
  400. * guest (but not the host).
  401. *
  402. * This is less of an issue for MMIO stores since they aren't
  403. * globally visible. It could be an issue for MMIO loads to
  404. * a certain extent but we'll ignore it for now.
  405. */
  406. vcpu->arch.paddr_accessed = gpa;
  407. vcpu->arch.vaddr_accessed = ea;
  408. return kvmppc_emulate_mmio(run, vcpu);
  409. }
  410. int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
  411. unsigned long ea, unsigned long dsisr)
  412. {
  413. struct kvm *kvm = vcpu->kvm;
  414. unsigned long hpte[3], r;
  415. unsigned long hnow_v, hnow_r;
  416. __be64 *hptep;
  417. unsigned long mmu_seq, psize, pte_size;
  418. unsigned long gpa_base, gfn_base;
  419. unsigned long gpa, gfn, hva, pfn;
  420. struct kvm_memory_slot *memslot;
  421. unsigned long *rmap;
  422. struct revmap_entry *rev;
  423. struct page *page, *pages[1];
  424. long index, ret, npages;
  425. bool is_ci;
  426. unsigned int writing, write_ok;
  427. struct vm_area_struct *vma;
  428. unsigned long rcbits;
  429. long mmio_update;
  430. if (kvm_is_radix(kvm))
  431. return kvmppc_book3s_radix_page_fault(run, vcpu, ea, dsisr);
  432. /*
  433. * Real-mode code has already searched the HPT and found the
  434. * entry we're interested in. Lock the entry and check that
  435. * it hasn't changed. If it has, just return and re-execute the
  436. * instruction.
  437. */
  438. if (ea != vcpu->arch.pgfault_addr)
  439. return RESUME_GUEST;
  440. if (vcpu->arch.pgfault_cache) {
  441. mmio_update = atomic64_read(&kvm->arch.mmio_update);
  442. if (mmio_update == vcpu->arch.pgfault_cache->mmio_update) {
  443. r = vcpu->arch.pgfault_cache->rpte;
  444. psize = kvmppc_actual_pgsz(vcpu->arch.pgfault_hpte[0],
  445. r);
  446. gpa_base = r & HPTE_R_RPN & ~(psize - 1);
  447. gfn_base = gpa_base >> PAGE_SHIFT;
  448. gpa = gpa_base | (ea & (psize - 1));
  449. return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea,
  450. dsisr & DSISR_ISSTORE);
  451. }
  452. }
  453. index = vcpu->arch.pgfault_index;
  454. hptep = (__be64 *)(kvm->arch.hpt.virt + (index << 4));
  455. rev = &kvm->arch.hpt.rev[index];
  456. preempt_disable();
  457. while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
  458. cpu_relax();
  459. hpte[0] = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK;
  460. hpte[1] = be64_to_cpu(hptep[1]);
  461. hpte[2] = r = rev->guest_rpte;
  462. unlock_hpte(hptep, hpte[0]);
  463. preempt_enable();
  464. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  465. hpte[0] = hpte_new_to_old_v(hpte[0], hpte[1]);
  466. hpte[1] = hpte_new_to_old_r(hpte[1]);
  467. }
  468. if (hpte[0] != vcpu->arch.pgfault_hpte[0] ||
  469. hpte[1] != vcpu->arch.pgfault_hpte[1])
  470. return RESUME_GUEST;
  471. /* Translate the logical address and get the page */
  472. psize = kvmppc_actual_pgsz(hpte[0], r);
  473. gpa_base = r & HPTE_R_RPN & ~(psize - 1);
  474. gfn_base = gpa_base >> PAGE_SHIFT;
  475. gpa = gpa_base | (ea & (psize - 1));
  476. gfn = gpa >> PAGE_SHIFT;
  477. memslot = gfn_to_memslot(kvm, gfn);
  478. trace_kvm_page_fault_enter(vcpu, hpte, memslot, ea, dsisr);
  479. /* No memslot means it's an emulated MMIO region */
  480. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  481. return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea,
  482. dsisr & DSISR_ISSTORE);
  483. /*
  484. * This should never happen, because of the slot_is_aligned()
  485. * check in kvmppc_do_h_enter().
  486. */
  487. if (gfn_base < memslot->base_gfn)
  488. return -EFAULT;
  489. /* used to check for invalidations in progress */
  490. mmu_seq = kvm->mmu_notifier_seq;
  491. smp_rmb();
  492. ret = -EFAULT;
  493. is_ci = false;
  494. pfn = 0;
  495. page = NULL;
  496. pte_size = PAGE_SIZE;
  497. writing = (dsisr & DSISR_ISSTORE) != 0;
  498. /* If writing != 0, then the HPTE must allow writing, if we get here */
  499. write_ok = writing;
  500. hva = gfn_to_hva_memslot(memslot, gfn);
  501. npages = get_user_pages_fast(hva, 1, writing, pages);
  502. if (npages < 1) {
  503. /* Check if it's an I/O mapping */
  504. down_read(&current->mm->mmap_sem);
  505. vma = find_vma(current->mm, hva);
  506. if (vma && vma->vm_start <= hva && hva + psize <= vma->vm_end &&
  507. (vma->vm_flags & VM_PFNMAP)) {
  508. pfn = vma->vm_pgoff +
  509. ((hva - vma->vm_start) >> PAGE_SHIFT);
  510. pte_size = psize;
  511. is_ci = pte_ci(__pte((pgprot_val(vma->vm_page_prot))));
  512. write_ok = vma->vm_flags & VM_WRITE;
  513. }
  514. up_read(&current->mm->mmap_sem);
  515. if (!pfn)
  516. goto out_put;
  517. } else {
  518. page = pages[0];
  519. pfn = page_to_pfn(page);
  520. if (PageHuge(page)) {
  521. page = compound_head(page);
  522. pte_size <<= compound_order(page);
  523. }
  524. /* if the guest wants write access, see if that is OK */
  525. if (!writing && hpte_is_writable(r)) {
  526. pte_t *ptep, pte;
  527. unsigned long flags;
  528. /*
  529. * We need to protect against page table destruction
  530. * hugepage split and collapse.
  531. */
  532. local_irq_save(flags);
  533. ptep = find_current_mm_pte(current->mm->pgd,
  534. hva, NULL, NULL);
  535. if (ptep) {
  536. pte = kvmppc_read_update_linux_pte(ptep, 1);
  537. if (__pte_write(pte))
  538. write_ok = 1;
  539. }
  540. local_irq_restore(flags);
  541. }
  542. }
  543. if (psize > pte_size)
  544. goto out_put;
  545. /* Check WIMG vs. the actual page we're accessing */
  546. if (!hpte_cache_flags_ok(r, is_ci)) {
  547. if (is_ci)
  548. goto out_put;
  549. /*
  550. * Allow guest to map emulated device memory as
  551. * uncacheable, but actually make it cacheable.
  552. */
  553. r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M;
  554. }
  555. /*
  556. * Set the HPTE to point to pfn.
  557. * Since the pfn is at PAGE_SIZE granularity, make sure we
  558. * don't mask out lower-order bits if psize < PAGE_SIZE.
  559. */
  560. if (psize < PAGE_SIZE)
  561. psize = PAGE_SIZE;
  562. r = (r & HPTE_R_KEY_HI) | (r & ~(HPTE_R_PP0 - psize)) |
  563. ((pfn << PAGE_SHIFT) & ~(psize - 1));
  564. if (hpte_is_writable(r) && !write_ok)
  565. r = hpte_make_readonly(r);
  566. ret = RESUME_GUEST;
  567. preempt_disable();
  568. while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
  569. cpu_relax();
  570. hnow_v = be64_to_cpu(hptep[0]);
  571. hnow_r = be64_to_cpu(hptep[1]);
  572. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  573. hnow_v = hpte_new_to_old_v(hnow_v, hnow_r);
  574. hnow_r = hpte_new_to_old_r(hnow_r);
  575. }
  576. /*
  577. * If the HPT is being resized, don't update the HPTE,
  578. * instead let the guest retry after the resize operation is complete.
  579. * The synchronization for mmu_ready test vs. set is provided
  580. * by the HPTE lock.
  581. */
  582. if (!kvm->arch.mmu_ready)
  583. goto out_unlock;
  584. if ((hnow_v & ~HPTE_V_HVLOCK) != hpte[0] || hnow_r != hpte[1] ||
  585. rev->guest_rpte != hpte[2])
  586. /* HPTE has been changed under us; let the guest retry */
  587. goto out_unlock;
  588. hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
  589. /* Always put the HPTE in the rmap chain for the page base address */
  590. rmap = &memslot->arch.rmap[gfn_base - memslot->base_gfn];
  591. lock_rmap(rmap);
  592. /* Check if we might have been invalidated; let the guest retry if so */
  593. ret = RESUME_GUEST;
  594. if (mmu_notifier_retry(vcpu->kvm, mmu_seq)) {
  595. unlock_rmap(rmap);
  596. goto out_unlock;
  597. }
  598. /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
  599. rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
  600. r &= rcbits | ~(HPTE_R_R | HPTE_R_C);
  601. if (be64_to_cpu(hptep[0]) & HPTE_V_VALID) {
  602. /* HPTE was previously valid, so we need to invalidate it */
  603. unlock_rmap(rmap);
  604. hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
  605. kvmppc_invalidate_hpte(kvm, hptep, index);
  606. /* don't lose previous R and C bits */
  607. r |= be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C);
  608. } else {
  609. kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
  610. }
  611. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  612. r = hpte_old_to_new_r(hpte[0], r);
  613. hpte[0] = hpte_old_to_new_v(hpte[0]);
  614. }
  615. hptep[1] = cpu_to_be64(r);
  616. eieio();
  617. __unlock_hpte(hptep, hpte[0]);
  618. asm volatile("ptesync" : : : "memory");
  619. preempt_enable();
  620. if (page && hpte_is_writable(r))
  621. SetPageDirty(page);
  622. out_put:
  623. trace_kvm_page_fault_exit(vcpu, hpte, ret);
  624. if (page) {
  625. /*
  626. * We drop pages[0] here, not page because page might
  627. * have been set to the head page of a compound, but
  628. * we have to drop the reference on the correct tail
  629. * page to match the get inside gup()
  630. */
  631. put_page(pages[0]);
  632. }
  633. return ret;
  634. out_unlock:
  635. __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
  636. preempt_enable();
  637. goto out_put;
  638. }
  639. void kvmppc_rmap_reset(struct kvm *kvm)
  640. {
  641. struct kvm_memslots *slots;
  642. struct kvm_memory_slot *memslot;
  643. int srcu_idx;
  644. srcu_idx = srcu_read_lock(&kvm->srcu);
  645. slots = kvm_memslots(kvm);
  646. kvm_for_each_memslot(memslot, slots) {
  647. /*
  648. * This assumes it is acceptable to lose reference and
  649. * change bits across a reset.
  650. */
  651. memset(memslot->arch.rmap, 0,
  652. memslot->npages * sizeof(*memslot->arch.rmap));
  653. }
  654. srcu_read_unlock(&kvm->srcu, srcu_idx);
  655. }
  656. typedef int (*hva_handler_fn)(struct kvm *kvm, struct kvm_memory_slot *memslot,
  657. unsigned long gfn);
  658. static int kvm_handle_hva_range(struct kvm *kvm,
  659. unsigned long start,
  660. unsigned long end,
  661. hva_handler_fn handler)
  662. {
  663. int ret;
  664. int retval = 0;
  665. struct kvm_memslots *slots;
  666. struct kvm_memory_slot *memslot;
  667. slots = kvm_memslots(kvm);
  668. kvm_for_each_memslot(memslot, slots) {
  669. unsigned long hva_start, hva_end;
  670. gfn_t gfn, gfn_end;
  671. hva_start = max(start, memslot->userspace_addr);
  672. hva_end = min(end, memslot->userspace_addr +
  673. (memslot->npages << PAGE_SHIFT));
  674. if (hva_start >= hva_end)
  675. continue;
  676. /*
  677. * {gfn(page) | page intersects with [hva_start, hva_end)} =
  678. * {gfn, gfn+1, ..., gfn_end-1}.
  679. */
  680. gfn = hva_to_gfn_memslot(hva_start, memslot);
  681. gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
  682. for (; gfn < gfn_end; ++gfn) {
  683. ret = handler(kvm, memslot, gfn);
  684. retval |= ret;
  685. }
  686. }
  687. return retval;
  688. }
  689. static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
  690. hva_handler_fn handler)
  691. {
  692. return kvm_handle_hva_range(kvm, hva, hva + 1, handler);
  693. }
  694. /* Must be called with both HPTE and rmap locked */
  695. static void kvmppc_unmap_hpte(struct kvm *kvm, unsigned long i,
  696. struct kvm_memory_slot *memslot,
  697. unsigned long *rmapp, unsigned long gfn)
  698. {
  699. __be64 *hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
  700. struct revmap_entry *rev = kvm->arch.hpt.rev;
  701. unsigned long j, h;
  702. unsigned long ptel, psize, rcbits;
  703. j = rev[i].forw;
  704. if (j == i) {
  705. /* chain is now empty */
  706. *rmapp &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
  707. } else {
  708. /* remove i from chain */
  709. h = rev[i].back;
  710. rev[h].forw = j;
  711. rev[j].back = h;
  712. rev[i].forw = rev[i].back = i;
  713. *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j;
  714. }
  715. /* Now check and modify the HPTE */
  716. ptel = rev[i].guest_rpte;
  717. psize = kvmppc_actual_pgsz(be64_to_cpu(hptep[0]), ptel);
  718. if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) &&
  719. hpte_rpn(ptel, psize) == gfn) {
  720. hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
  721. kvmppc_invalidate_hpte(kvm, hptep, i);
  722. hptep[1] &= ~cpu_to_be64(HPTE_R_KEY_HI | HPTE_R_KEY_LO);
  723. /* Harvest R and C */
  724. rcbits = be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C);
  725. *rmapp |= rcbits << KVMPPC_RMAP_RC_SHIFT;
  726. if ((rcbits & HPTE_R_C) && memslot->dirty_bitmap)
  727. kvmppc_update_dirty_map(memslot, gfn, psize);
  728. if (rcbits & ~rev[i].guest_rpte) {
  729. rev[i].guest_rpte = ptel | rcbits;
  730. note_hpte_modification(kvm, &rev[i]);
  731. }
  732. }
  733. }
  734. static int kvm_unmap_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot,
  735. unsigned long gfn)
  736. {
  737. unsigned long i;
  738. __be64 *hptep;
  739. unsigned long *rmapp;
  740. rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
  741. for (;;) {
  742. lock_rmap(rmapp);
  743. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  744. unlock_rmap(rmapp);
  745. break;
  746. }
  747. /*
  748. * To avoid an ABBA deadlock with the HPTE lock bit,
  749. * we can't spin on the HPTE lock while holding the
  750. * rmap chain lock.
  751. */
  752. i = *rmapp & KVMPPC_RMAP_INDEX;
  753. hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
  754. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  755. /* unlock rmap before spinning on the HPTE lock */
  756. unlock_rmap(rmapp);
  757. while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK)
  758. cpu_relax();
  759. continue;
  760. }
  761. kvmppc_unmap_hpte(kvm, i, memslot, rmapp, gfn);
  762. unlock_rmap(rmapp);
  763. __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
  764. }
  765. return 0;
  766. }
  767. int kvm_unmap_hva_hv(struct kvm *kvm, unsigned long hva)
  768. {
  769. hva_handler_fn handler;
  770. handler = kvm_is_radix(kvm) ? kvm_unmap_radix : kvm_unmap_rmapp;
  771. kvm_handle_hva(kvm, hva, handler);
  772. return 0;
  773. }
  774. int kvm_unmap_hva_range_hv(struct kvm *kvm, unsigned long start, unsigned long end)
  775. {
  776. hva_handler_fn handler;
  777. handler = kvm_is_radix(kvm) ? kvm_unmap_radix : kvm_unmap_rmapp;
  778. kvm_handle_hva_range(kvm, start, end, handler);
  779. return 0;
  780. }
  781. void kvmppc_core_flush_memslot_hv(struct kvm *kvm,
  782. struct kvm_memory_slot *memslot)
  783. {
  784. unsigned long gfn;
  785. unsigned long n;
  786. unsigned long *rmapp;
  787. gfn = memslot->base_gfn;
  788. rmapp = memslot->arch.rmap;
  789. for (n = memslot->npages; n; --n, ++gfn) {
  790. if (kvm_is_radix(kvm)) {
  791. kvm_unmap_radix(kvm, memslot, gfn);
  792. continue;
  793. }
  794. /*
  795. * Testing the present bit without locking is OK because
  796. * the memslot has been marked invalid already, and hence
  797. * no new HPTEs referencing this page can be created,
  798. * thus the present bit can't go from 0 to 1.
  799. */
  800. if (*rmapp & KVMPPC_RMAP_PRESENT)
  801. kvm_unmap_rmapp(kvm, memslot, gfn);
  802. ++rmapp;
  803. }
  804. }
  805. static int kvm_age_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot,
  806. unsigned long gfn)
  807. {
  808. struct revmap_entry *rev = kvm->arch.hpt.rev;
  809. unsigned long head, i, j;
  810. __be64 *hptep;
  811. int ret = 0;
  812. unsigned long *rmapp;
  813. rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
  814. retry:
  815. lock_rmap(rmapp);
  816. if (*rmapp & KVMPPC_RMAP_REFERENCED) {
  817. *rmapp &= ~KVMPPC_RMAP_REFERENCED;
  818. ret = 1;
  819. }
  820. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  821. unlock_rmap(rmapp);
  822. return ret;
  823. }
  824. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  825. do {
  826. hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
  827. j = rev[i].forw;
  828. /* If this HPTE isn't referenced, ignore it */
  829. if (!(be64_to_cpu(hptep[1]) & HPTE_R_R))
  830. continue;
  831. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  832. /* unlock rmap before spinning on the HPTE lock */
  833. unlock_rmap(rmapp);
  834. while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK)
  835. cpu_relax();
  836. goto retry;
  837. }
  838. /* Now check and modify the HPTE */
  839. if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) &&
  840. (be64_to_cpu(hptep[1]) & HPTE_R_R)) {
  841. kvmppc_clear_ref_hpte(kvm, hptep, i);
  842. if (!(rev[i].guest_rpte & HPTE_R_R)) {
  843. rev[i].guest_rpte |= HPTE_R_R;
  844. note_hpte_modification(kvm, &rev[i]);
  845. }
  846. ret = 1;
  847. }
  848. __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
  849. } while ((i = j) != head);
  850. unlock_rmap(rmapp);
  851. return ret;
  852. }
  853. int kvm_age_hva_hv(struct kvm *kvm, unsigned long start, unsigned long end)
  854. {
  855. hva_handler_fn handler;
  856. handler = kvm_is_radix(kvm) ? kvm_age_radix : kvm_age_rmapp;
  857. return kvm_handle_hva_range(kvm, start, end, handler);
  858. }
  859. static int kvm_test_age_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot,
  860. unsigned long gfn)
  861. {
  862. struct revmap_entry *rev = kvm->arch.hpt.rev;
  863. unsigned long head, i, j;
  864. unsigned long *hp;
  865. int ret = 1;
  866. unsigned long *rmapp;
  867. rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
  868. if (*rmapp & KVMPPC_RMAP_REFERENCED)
  869. return 1;
  870. lock_rmap(rmapp);
  871. if (*rmapp & KVMPPC_RMAP_REFERENCED)
  872. goto out;
  873. if (*rmapp & KVMPPC_RMAP_PRESENT) {
  874. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  875. do {
  876. hp = (unsigned long *)(kvm->arch.hpt.virt + (i << 4));
  877. j = rev[i].forw;
  878. if (be64_to_cpu(hp[1]) & HPTE_R_R)
  879. goto out;
  880. } while ((i = j) != head);
  881. }
  882. ret = 0;
  883. out:
  884. unlock_rmap(rmapp);
  885. return ret;
  886. }
  887. int kvm_test_age_hva_hv(struct kvm *kvm, unsigned long hva)
  888. {
  889. hva_handler_fn handler;
  890. handler = kvm_is_radix(kvm) ? kvm_test_age_radix : kvm_test_age_rmapp;
  891. return kvm_handle_hva(kvm, hva, handler);
  892. }
  893. void kvm_set_spte_hva_hv(struct kvm *kvm, unsigned long hva, pte_t pte)
  894. {
  895. hva_handler_fn handler;
  896. handler = kvm_is_radix(kvm) ? kvm_unmap_radix : kvm_unmap_rmapp;
  897. kvm_handle_hva(kvm, hva, handler);
  898. }
  899. static int vcpus_running(struct kvm *kvm)
  900. {
  901. return atomic_read(&kvm->arch.vcpus_running) != 0;
  902. }
  903. /*
  904. * Returns the number of system pages that are dirty.
  905. * This can be more than 1 if we find a huge-page HPTE.
  906. */
  907. static int kvm_test_clear_dirty_npages(struct kvm *kvm, unsigned long *rmapp)
  908. {
  909. struct revmap_entry *rev = kvm->arch.hpt.rev;
  910. unsigned long head, i, j;
  911. unsigned long n;
  912. unsigned long v, r;
  913. __be64 *hptep;
  914. int npages_dirty = 0;
  915. retry:
  916. lock_rmap(rmapp);
  917. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  918. unlock_rmap(rmapp);
  919. return npages_dirty;
  920. }
  921. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  922. do {
  923. unsigned long hptep1;
  924. hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
  925. j = rev[i].forw;
  926. /*
  927. * Checking the C (changed) bit here is racy since there
  928. * is no guarantee about when the hardware writes it back.
  929. * If the HPTE is not writable then it is stable since the
  930. * page can't be written to, and we would have done a tlbie
  931. * (which forces the hardware to complete any writeback)
  932. * when making the HPTE read-only.
  933. * If vcpus are running then this call is racy anyway
  934. * since the page could get dirtied subsequently, so we
  935. * expect there to be a further call which would pick up
  936. * any delayed C bit writeback.
  937. * Otherwise we need to do the tlbie even if C==0 in
  938. * order to pick up any delayed writeback of C.
  939. */
  940. hptep1 = be64_to_cpu(hptep[1]);
  941. if (!(hptep1 & HPTE_R_C) &&
  942. (!hpte_is_writable(hptep1) || vcpus_running(kvm)))
  943. continue;
  944. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  945. /* unlock rmap before spinning on the HPTE lock */
  946. unlock_rmap(rmapp);
  947. while (hptep[0] & cpu_to_be64(HPTE_V_HVLOCK))
  948. cpu_relax();
  949. goto retry;
  950. }
  951. /* Now check and modify the HPTE */
  952. if (!(hptep[0] & cpu_to_be64(HPTE_V_VALID))) {
  953. __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
  954. continue;
  955. }
  956. /* need to make it temporarily absent so C is stable */
  957. hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
  958. kvmppc_invalidate_hpte(kvm, hptep, i);
  959. v = be64_to_cpu(hptep[0]);
  960. r = be64_to_cpu(hptep[1]);
  961. if (r & HPTE_R_C) {
  962. hptep[1] = cpu_to_be64(r & ~HPTE_R_C);
  963. if (!(rev[i].guest_rpte & HPTE_R_C)) {
  964. rev[i].guest_rpte |= HPTE_R_C;
  965. note_hpte_modification(kvm, &rev[i]);
  966. }
  967. n = kvmppc_actual_pgsz(v, r);
  968. n = (n + PAGE_SIZE - 1) >> PAGE_SHIFT;
  969. if (n > npages_dirty)
  970. npages_dirty = n;
  971. eieio();
  972. }
  973. v &= ~HPTE_V_ABSENT;
  974. v |= HPTE_V_VALID;
  975. __unlock_hpte(hptep, v);
  976. } while ((i = j) != head);
  977. unlock_rmap(rmapp);
  978. return npages_dirty;
  979. }
  980. void kvmppc_harvest_vpa_dirty(struct kvmppc_vpa *vpa,
  981. struct kvm_memory_slot *memslot,
  982. unsigned long *map)
  983. {
  984. unsigned long gfn;
  985. if (!vpa->dirty || !vpa->pinned_addr)
  986. return;
  987. gfn = vpa->gpa >> PAGE_SHIFT;
  988. if (gfn < memslot->base_gfn ||
  989. gfn >= memslot->base_gfn + memslot->npages)
  990. return;
  991. vpa->dirty = false;
  992. if (map)
  993. __set_bit_le(gfn - memslot->base_gfn, map);
  994. }
  995. long kvmppc_hv_get_dirty_log_hpt(struct kvm *kvm,
  996. struct kvm_memory_slot *memslot, unsigned long *map)
  997. {
  998. unsigned long i;
  999. unsigned long *rmapp;
  1000. preempt_disable();
  1001. rmapp = memslot->arch.rmap;
  1002. for (i = 0; i < memslot->npages; ++i) {
  1003. int npages = kvm_test_clear_dirty_npages(kvm, rmapp);
  1004. /*
  1005. * Note that if npages > 0 then i must be a multiple of npages,
  1006. * since we always put huge-page HPTEs in the rmap chain
  1007. * corresponding to their page base address.
  1008. */
  1009. if (npages)
  1010. set_dirty_bits(map, i, npages);
  1011. ++rmapp;
  1012. }
  1013. preempt_enable();
  1014. return 0;
  1015. }
  1016. void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
  1017. unsigned long *nb_ret)
  1018. {
  1019. struct kvm_memory_slot *memslot;
  1020. unsigned long gfn = gpa >> PAGE_SHIFT;
  1021. struct page *page, *pages[1];
  1022. int npages;
  1023. unsigned long hva, offset;
  1024. int srcu_idx;
  1025. srcu_idx = srcu_read_lock(&kvm->srcu);
  1026. memslot = gfn_to_memslot(kvm, gfn);
  1027. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  1028. goto err;
  1029. hva = gfn_to_hva_memslot(memslot, gfn);
  1030. npages = get_user_pages_fast(hva, 1, 1, pages);
  1031. if (npages < 1)
  1032. goto err;
  1033. page = pages[0];
  1034. srcu_read_unlock(&kvm->srcu, srcu_idx);
  1035. offset = gpa & (PAGE_SIZE - 1);
  1036. if (nb_ret)
  1037. *nb_ret = PAGE_SIZE - offset;
  1038. return page_address(page) + offset;
  1039. err:
  1040. srcu_read_unlock(&kvm->srcu, srcu_idx);
  1041. return NULL;
  1042. }
  1043. void kvmppc_unpin_guest_page(struct kvm *kvm, void *va, unsigned long gpa,
  1044. bool dirty)
  1045. {
  1046. struct page *page = virt_to_page(va);
  1047. struct kvm_memory_slot *memslot;
  1048. unsigned long gfn;
  1049. int srcu_idx;
  1050. put_page(page);
  1051. if (!dirty)
  1052. return;
  1053. /* We need to mark this page dirty in the memslot dirty_bitmap, if any */
  1054. gfn = gpa >> PAGE_SHIFT;
  1055. srcu_idx = srcu_read_lock(&kvm->srcu);
  1056. memslot = gfn_to_memslot(kvm, gfn);
  1057. if (memslot && memslot->dirty_bitmap)
  1058. set_bit_le(gfn - memslot->base_gfn, memslot->dirty_bitmap);
  1059. srcu_read_unlock(&kvm->srcu, srcu_idx);
  1060. }
  1061. /*
  1062. * HPT resizing
  1063. */
  1064. static int resize_hpt_allocate(struct kvm_resize_hpt *resize)
  1065. {
  1066. int rc;
  1067. rc = kvmppc_allocate_hpt(&resize->hpt, resize->order);
  1068. if (rc < 0)
  1069. return rc;
  1070. resize_hpt_debug(resize, "resize_hpt_allocate(): HPT @ 0x%lx\n",
  1071. resize->hpt.virt);
  1072. return 0;
  1073. }
  1074. static unsigned long resize_hpt_rehash_hpte(struct kvm_resize_hpt *resize,
  1075. unsigned long idx)
  1076. {
  1077. struct kvm *kvm = resize->kvm;
  1078. struct kvm_hpt_info *old = &kvm->arch.hpt;
  1079. struct kvm_hpt_info *new = &resize->hpt;
  1080. unsigned long old_hash_mask = (1ULL << (old->order - 7)) - 1;
  1081. unsigned long new_hash_mask = (1ULL << (new->order - 7)) - 1;
  1082. __be64 *hptep, *new_hptep;
  1083. unsigned long vpte, rpte, guest_rpte;
  1084. int ret;
  1085. struct revmap_entry *rev;
  1086. unsigned long apsize, avpn, pteg, hash;
  1087. unsigned long new_idx, new_pteg, replace_vpte;
  1088. int pshift;
  1089. hptep = (__be64 *)(old->virt + (idx << 4));
  1090. /* Guest is stopped, so new HPTEs can't be added or faulted
  1091. * in, only unmapped or altered by host actions. So, it's
  1092. * safe to check this before we take the HPTE lock */
  1093. vpte = be64_to_cpu(hptep[0]);
  1094. if (!(vpte & HPTE_V_VALID) && !(vpte & HPTE_V_ABSENT))
  1095. return 0; /* nothing to do */
  1096. while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
  1097. cpu_relax();
  1098. vpte = be64_to_cpu(hptep[0]);
  1099. ret = 0;
  1100. if (!(vpte & HPTE_V_VALID) && !(vpte & HPTE_V_ABSENT))
  1101. /* Nothing to do */
  1102. goto out;
  1103. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  1104. rpte = be64_to_cpu(hptep[1]);
  1105. vpte = hpte_new_to_old_v(vpte, rpte);
  1106. }
  1107. /* Unmap */
  1108. rev = &old->rev[idx];
  1109. guest_rpte = rev->guest_rpte;
  1110. ret = -EIO;
  1111. apsize = kvmppc_actual_pgsz(vpte, guest_rpte);
  1112. if (!apsize)
  1113. goto out;
  1114. if (vpte & HPTE_V_VALID) {
  1115. unsigned long gfn = hpte_rpn(guest_rpte, apsize);
  1116. int srcu_idx = srcu_read_lock(&kvm->srcu);
  1117. struct kvm_memory_slot *memslot =
  1118. __gfn_to_memslot(kvm_memslots(kvm), gfn);
  1119. if (memslot) {
  1120. unsigned long *rmapp;
  1121. rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
  1122. lock_rmap(rmapp);
  1123. kvmppc_unmap_hpte(kvm, idx, memslot, rmapp, gfn);
  1124. unlock_rmap(rmapp);
  1125. }
  1126. srcu_read_unlock(&kvm->srcu, srcu_idx);
  1127. }
  1128. /* Reload PTE after unmap */
  1129. vpte = be64_to_cpu(hptep[0]);
  1130. BUG_ON(vpte & HPTE_V_VALID);
  1131. BUG_ON(!(vpte & HPTE_V_ABSENT));
  1132. ret = 0;
  1133. if (!(vpte & HPTE_V_BOLTED))
  1134. goto out;
  1135. rpte = be64_to_cpu(hptep[1]);
  1136. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  1137. vpte = hpte_new_to_old_v(vpte, rpte);
  1138. rpte = hpte_new_to_old_r(rpte);
  1139. }
  1140. pshift = kvmppc_hpte_base_page_shift(vpte, rpte);
  1141. avpn = HPTE_V_AVPN_VAL(vpte) & ~(((1ul << pshift) - 1) >> 23);
  1142. pteg = idx / HPTES_PER_GROUP;
  1143. if (vpte & HPTE_V_SECONDARY)
  1144. pteg = ~pteg;
  1145. if (!(vpte & HPTE_V_1TB_SEG)) {
  1146. unsigned long offset, vsid;
  1147. /* We only have 28 - 23 bits of offset in avpn */
  1148. offset = (avpn & 0x1f) << 23;
  1149. vsid = avpn >> 5;
  1150. /* We can find more bits from the pteg value */
  1151. if (pshift < 23)
  1152. offset |= ((vsid ^ pteg) & old_hash_mask) << pshift;
  1153. hash = vsid ^ (offset >> pshift);
  1154. } else {
  1155. unsigned long offset, vsid;
  1156. /* We only have 40 - 23 bits of seg_off in avpn */
  1157. offset = (avpn & 0x1ffff) << 23;
  1158. vsid = avpn >> 17;
  1159. if (pshift < 23)
  1160. offset |= ((vsid ^ (vsid << 25) ^ pteg) & old_hash_mask) << pshift;
  1161. hash = vsid ^ (vsid << 25) ^ (offset >> pshift);
  1162. }
  1163. new_pteg = hash & new_hash_mask;
  1164. if (vpte & HPTE_V_SECONDARY)
  1165. new_pteg = ~hash & new_hash_mask;
  1166. new_idx = new_pteg * HPTES_PER_GROUP + (idx % HPTES_PER_GROUP);
  1167. new_hptep = (__be64 *)(new->virt + (new_idx << 4));
  1168. replace_vpte = be64_to_cpu(new_hptep[0]);
  1169. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  1170. unsigned long replace_rpte = be64_to_cpu(new_hptep[1]);
  1171. replace_vpte = hpte_new_to_old_v(replace_vpte, replace_rpte);
  1172. }
  1173. if (replace_vpte & (HPTE_V_VALID | HPTE_V_ABSENT)) {
  1174. BUG_ON(new->order >= old->order);
  1175. if (replace_vpte & HPTE_V_BOLTED) {
  1176. if (vpte & HPTE_V_BOLTED)
  1177. /* Bolted collision, nothing we can do */
  1178. ret = -ENOSPC;
  1179. /* Discard the new HPTE */
  1180. goto out;
  1181. }
  1182. /* Discard the previous HPTE */
  1183. }
  1184. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  1185. rpte = hpte_old_to_new_r(vpte, rpte);
  1186. vpte = hpte_old_to_new_v(vpte);
  1187. }
  1188. new_hptep[1] = cpu_to_be64(rpte);
  1189. new->rev[new_idx].guest_rpte = guest_rpte;
  1190. /* No need for a barrier, since new HPT isn't active */
  1191. new_hptep[0] = cpu_to_be64(vpte);
  1192. unlock_hpte(new_hptep, vpte);
  1193. out:
  1194. unlock_hpte(hptep, vpte);
  1195. return ret;
  1196. }
  1197. static int resize_hpt_rehash(struct kvm_resize_hpt *resize)
  1198. {
  1199. struct kvm *kvm = resize->kvm;
  1200. unsigned long i;
  1201. int rc;
  1202. for (i = 0; i < kvmppc_hpt_npte(&kvm->arch.hpt); i++) {
  1203. rc = resize_hpt_rehash_hpte(resize, i);
  1204. if (rc != 0)
  1205. return rc;
  1206. }
  1207. return 0;
  1208. }
  1209. static void resize_hpt_pivot(struct kvm_resize_hpt *resize)
  1210. {
  1211. struct kvm *kvm = resize->kvm;
  1212. struct kvm_hpt_info hpt_tmp;
  1213. /* Exchange the pending tables in the resize structure with
  1214. * the active tables */
  1215. resize_hpt_debug(resize, "resize_hpt_pivot()\n");
  1216. spin_lock(&kvm->mmu_lock);
  1217. asm volatile("ptesync" : : : "memory");
  1218. hpt_tmp = kvm->arch.hpt;
  1219. kvmppc_set_hpt(kvm, &resize->hpt);
  1220. resize->hpt = hpt_tmp;
  1221. spin_unlock(&kvm->mmu_lock);
  1222. synchronize_srcu_expedited(&kvm->srcu);
  1223. if (cpu_has_feature(CPU_FTR_ARCH_300))
  1224. kvmppc_setup_partition_table(kvm);
  1225. resize_hpt_debug(resize, "resize_hpt_pivot() done\n");
  1226. }
  1227. static void resize_hpt_release(struct kvm *kvm, struct kvm_resize_hpt *resize)
  1228. {
  1229. if (WARN_ON(!mutex_is_locked(&kvm->lock)))
  1230. return;
  1231. if (!resize)
  1232. return;
  1233. if (resize->error != -EBUSY) {
  1234. if (resize->hpt.virt)
  1235. kvmppc_free_hpt(&resize->hpt);
  1236. kfree(resize);
  1237. }
  1238. if (kvm->arch.resize_hpt == resize)
  1239. kvm->arch.resize_hpt = NULL;
  1240. }
  1241. static void resize_hpt_prepare_work(struct work_struct *work)
  1242. {
  1243. struct kvm_resize_hpt *resize = container_of(work,
  1244. struct kvm_resize_hpt,
  1245. work);
  1246. struct kvm *kvm = resize->kvm;
  1247. int err = 0;
  1248. if (WARN_ON(resize->error != -EBUSY))
  1249. return;
  1250. mutex_lock(&kvm->lock);
  1251. /* Request is still current? */
  1252. if (kvm->arch.resize_hpt == resize) {
  1253. /* We may request large allocations here:
  1254. * do not sleep with kvm->lock held for a while.
  1255. */
  1256. mutex_unlock(&kvm->lock);
  1257. resize_hpt_debug(resize, "resize_hpt_prepare_work(): order = %d\n",
  1258. resize->order);
  1259. err = resize_hpt_allocate(resize);
  1260. /* We have strict assumption about -EBUSY
  1261. * when preparing for HPT resize.
  1262. */
  1263. if (WARN_ON(err == -EBUSY))
  1264. err = -EINPROGRESS;
  1265. mutex_lock(&kvm->lock);
  1266. /* It is possible that kvm->arch.resize_hpt != resize
  1267. * after we grab kvm->lock again.
  1268. */
  1269. }
  1270. resize->error = err;
  1271. if (kvm->arch.resize_hpt != resize)
  1272. resize_hpt_release(kvm, resize);
  1273. mutex_unlock(&kvm->lock);
  1274. }
  1275. long kvm_vm_ioctl_resize_hpt_prepare(struct kvm *kvm,
  1276. struct kvm_ppc_resize_hpt *rhpt)
  1277. {
  1278. unsigned long flags = rhpt->flags;
  1279. unsigned long shift = rhpt->shift;
  1280. struct kvm_resize_hpt *resize;
  1281. int ret;
  1282. if (flags != 0 || kvm_is_radix(kvm))
  1283. return -EINVAL;
  1284. if (shift && ((shift < 18) || (shift > 46)))
  1285. return -EINVAL;
  1286. mutex_lock(&kvm->lock);
  1287. resize = kvm->arch.resize_hpt;
  1288. if (resize) {
  1289. if (resize->order == shift) {
  1290. /* Suitable resize in progress? */
  1291. ret = resize->error;
  1292. if (ret == -EBUSY)
  1293. ret = 100; /* estimated time in ms */
  1294. else if (ret)
  1295. resize_hpt_release(kvm, resize);
  1296. goto out;
  1297. }
  1298. /* not suitable, cancel it */
  1299. resize_hpt_release(kvm, resize);
  1300. }
  1301. ret = 0;
  1302. if (!shift)
  1303. goto out; /* nothing to do */
  1304. /* start new resize */
  1305. resize = kzalloc(sizeof(*resize), GFP_KERNEL);
  1306. if (!resize) {
  1307. ret = -ENOMEM;
  1308. goto out;
  1309. }
  1310. resize->error = -EBUSY;
  1311. resize->order = shift;
  1312. resize->kvm = kvm;
  1313. INIT_WORK(&resize->work, resize_hpt_prepare_work);
  1314. kvm->arch.resize_hpt = resize;
  1315. schedule_work(&resize->work);
  1316. ret = 100; /* estimated time in ms */
  1317. out:
  1318. mutex_unlock(&kvm->lock);
  1319. return ret;
  1320. }
  1321. static void resize_hpt_boot_vcpu(void *opaque)
  1322. {
  1323. /* Nothing to do, just force a KVM exit */
  1324. }
  1325. long kvm_vm_ioctl_resize_hpt_commit(struct kvm *kvm,
  1326. struct kvm_ppc_resize_hpt *rhpt)
  1327. {
  1328. unsigned long flags = rhpt->flags;
  1329. unsigned long shift = rhpt->shift;
  1330. struct kvm_resize_hpt *resize;
  1331. long ret;
  1332. if (flags != 0 || kvm_is_radix(kvm))
  1333. return -EINVAL;
  1334. if (shift && ((shift < 18) || (shift > 46)))
  1335. return -EINVAL;
  1336. mutex_lock(&kvm->lock);
  1337. resize = kvm->arch.resize_hpt;
  1338. /* This shouldn't be possible */
  1339. ret = -EIO;
  1340. if (WARN_ON(!kvm->arch.mmu_ready))
  1341. goto out_no_hpt;
  1342. /* Stop VCPUs from running while we mess with the HPT */
  1343. kvm->arch.mmu_ready = 0;
  1344. smp_mb();
  1345. /* Boot all CPUs out of the guest so they re-read
  1346. * mmu_ready */
  1347. on_each_cpu(resize_hpt_boot_vcpu, NULL, 1);
  1348. ret = -ENXIO;
  1349. if (!resize || (resize->order != shift))
  1350. goto out;
  1351. ret = resize->error;
  1352. if (ret)
  1353. goto out;
  1354. ret = resize_hpt_rehash(resize);
  1355. if (ret)
  1356. goto out;
  1357. resize_hpt_pivot(resize);
  1358. out:
  1359. /* Let VCPUs run again */
  1360. kvm->arch.mmu_ready = 1;
  1361. smp_mb();
  1362. out_no_hpt:
  1363. resize_hpt_release(kvm, resize);
  1364. mutex_unlock(&kvm->lock);
  1365. return ret;
  1366. }
  1367. /*
  1368. * Functions for reading and writing the hash table via reads and
  1369. * writes on a file descriptor.
  1370. *
  1371. * Reads return the guest view of the hash table, which has to be
  1372. * pieced together from the real hash table and the guest_rpte
  1373. * values in the revmap array.
  1374. *
  1375. * On writes, each HPTE written is considered in turn, and if it
  1376. * is valid, it is written to the HPT as if an H_ENTER with the
  1377. * exact flag set was done. When the invalid count is non-zero
  1378. * in the header written to the stream, the kernel will make
  1379. * sure that that many HPTEs are invalid, and invalidate them
  1380. * if not.
  1381. */
  1382. struct kvm_htab_ctx {
  1383. unsigned long index;
  1384. unsigned long flags;
  1385. struct kvm *kvm;
  1386. int first_pass;
  1387. };
  1388. #define HPTE_SIZE (2 * sizeof(unsigned long))
  1389. /*
  1390. * Returns 1 if this HPT entry has been modified or has pending
  1391. * R/C bit changes.
  1392. */
  1393. static int hpte_dirty(struct revmap_entry *revp, __be64 *hptp)
  1394. {
  1395. unsigned long rcbits_unset;
  1396. if (revp->guest_rpte & HPTE_GR_MODIFIED)
  1397. return 1;
  1398. /* Also need to consider changes in reference and changed bits */
  1399. rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
  1400. if ((be64_to_cpu(hptp[0]) & HPTE_V_VALID) &&
  1401. (be64_to_cpu(hptp[1]) & rcbits_unset))
  1402. return 1;
  1403. return 0;
  1404. }
  1405. static long record_hpte(unsigned long flags, __be64 *hptp,
  1406. unsigned long *hpte, struct revmap_entry *revp,
  1407. int want_valid, int first_pass)
  1408. {
  1409. unsigned long v, r, hr;
  1410. unsigned long rcbits_unset;
  1411. int ok = 1;
  1412. int valid, dirty;
  1413. /* Unmodified entries are uninteresting except on the first pass */
  1414. dirty = hpte_dirty(revp, hptp);
  1415. if (!first_pass && !dirty)
  1416. return 0;
  1417. valid = 0;
  1418. if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)) {
  1419. valid = 1;
  1420. if ((flags & KVM_GET_HTAB_BOLTED_ONLY) &&
  1421. !(be64_to_cpu(hptp[0]) & HPTE_V_BOLTED))
  1422. valid = 0;
  1423. }
  1424. if (valid != want_valid)
  1425. return 0;
  1426. v = r = 0;
  1427. if (valid || dirty) {
  1428. /* lock the HPTE so it's stable and read it */
  1429. preempt_disable();
  1430. while (!try_lock_hpte(hptp, HPTE_V_HVLOCK))
  1431. cpu_relax();
  1432. v = be64_to_cpu(hptp[0]);
  1433. hr = be64_to_cpu(hptp[1]);
  1434. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  1435. v = hpte_new_to_old_v(v, hr);
  1436. hr = hpte_new_to_old_r(hr);
  1437. }
  1438. /* re-evaluate valid and dirty from synchronized HPTE value */
  1439. valid = !!(v & HPTE_V_VALID);
  1440. dirty = !!(revp->guest_rpte & HPTE_GR_MODIFIED);
  1441. /* Harvest R and C into guest view if necessary */
  1442. rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
  1443. if (valid && (rcbits_unset & hr)) {
  1444. revp->guest_rpte |= (hr &
  1445. (HPTE_R_R | HPTE_R_C)) | HPTE_GR_MODIFIED;
  1446. dirty = 1;
  1447. }
  1448. if (v & HPTE_V_ABSENT) {
  1449. v &= ~HPTE_V_ABSENT;
  1450. v |= HPTE_V_VALID;
  1451. valid = 1;
  1452. }
  1453. if ((flags & KVM_GET_HTAB_BOLTED_ONLY) && !(v & HPTE_V_BOLTED))
  1454. valid = 0;
  1455. r = revp->guest_rpte;
  1456. /* only clear modified if this is the right sort of entry */
  1457. if (valid == want_valid && dirty) {
  1458. r &= ~HPTE_GR_MODIFIED;
  1459. revp->guest_rpte = r;
  1460. }
  1461. unlock_hpte(hptp, be64_to_cpu(hptp[0]));
  1462. preempt_enable();
  1463. if (!(valid == want_valid && (first_pass || dirty)))
  1464. ok = 0;
  1465. }
  1466. hpte[0] = cpu_to_be64(v);
  1467. hpte[1] = cpu_to_be64(r);
  1468. return ok;
  1469. }
  1470. static ssize_t kvm_htab_read(struct file *file, char __user *buf,
  1471. size_t count, loff_t *ppos)
  1472. {
  1473. struct kvm_htab_ctx *ctx = file->private_data;
  1474. struct kvm *kvm = ctx->kvm;
  1475. struct kvm_get_htab_header hdr;
  1476. __be64 *hptp;
  1477. struct revmap_entry *revp;
  1478. unsigned long i, nb, nw;
  1479. unsigned long __user *lbuf;
  1480. struct kvm_get_htab_header __user *hptr;
  1481. unsigned long flags;
  1482. int first_pass;
  1483. unsigned long hpte[2];
  1484. if (!access_ok(VERIFY_WRITE, buf, count))
  1485. return -EFAULT;
  1486. if (kvm_is_radix(kvm))
  1487. return 0;
  1488. first_pass = ctx->first_pass;
  1489. flags = ctx->flags;
  1490. i = ctx->index;
  1491. hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE));
  1492. revp = kvm->arch.hpt.rev + i;
  1493. lbuf = (unsigned long __user *)buf;
  1494. nb = 0;
  1495. while (nb + sizeof(hdr) + HPTE_SIZE < count) {
  1496. /* Initialize header */
  1497. hptr = (struct kvm_get_htab_header __user *)buf;
  1498. hdr.n_valid = 0;
  1499. hdr.n_invalid = 0;
  1500. nw = nb;
  1501. nb += sizeof(hdr);
  1502. lbuf = (unsigned long __user *)(buf + sizeof(hdr));
  1503. /* Skip uninteresting entries, i.e. clean on not-first pass */
  1504. if (!first_pass) {
  1505. while (i < kvmppc_hpt_npte(&kvm->arch.hpt) &&
  1506. !hpte_dirty(revp, hptp)) {
  1507. ++i;
  1508. hptp += 2;
  1509. ++revp;
  1510. }
  1511. }
  1512. hdr.index = i;
  1513. /* Grab a series of valid entries */
  1514. while (i < kvmppc_hpt_npte(&kvm->arch.hpt) &&
  1515. hdr.n_valid < 0xffff &&
  1516. nb + HPTE_SIZE < count &&
  1517. record_hpte(flags, hptp, hpte, revp, 1, first_pass)) {
  1518. /* valid entry, write it out */
  1519. ++hdr.n_valid;
  1520. if (__put_user(hpte[0], lbuf) ||
  1521. __put_user(hpte[1], lbuf + 1))
  1522. return -EFAULT;
  1523. nb += HPTE_SIZE;
  1524. lbuf += 2;
  1525. ++i;
  1526. hptp += 2;
  1527. ++revp;
  1528. }
  1529. /* Now skip invalid entries while we can */
  1530. while (i < kvmppc_hpt_npte(&kvm->arch.hpt) &&
  1531. hdr.n_invalid < 0xffff &&
  1532. record_hpte(flags, hptp, hpte, revp, 0, first_pass)) {
  1533. /* found an invalid entry */
  1534. ++hdr.n_invalid;
  1535. ++i;
  1536. hptp += 2;
  1537. ++revp;
  1538. }
  1539. if (hdr.n_valid || hdr.n_invalid) {
  1540. /* write back the header */
  1541. if (__copy_to_user(hptr, &hdr, sizeof(hdr)))
  1542. return -EFAULT;
  1543. nw = nb;
  1544. buf = (char __user *)lbuf;
  1545. } else {
  1546. nb = nw;
  1547. }
  1548. /* Check if we've wrapped around the hash table */
  1549. if (i >= kvmppc_hpt_npte(&kvm->arch.hpt)) {
  1550. i = 0;
  1551. ctx->first_pass = 0;
  1552. break;
  1553. }
  1554. }
  1555. ctx->index = i;
  1556. return nb;
  1557. }
  1558. static ssize_t kvm_htab_write(struct file *file, const char __user *buf,
  1559. size_t count, loff_t *ppos)
  1560. {
  1561. struct kvm_htab_ctx *ctx = file->private_data;
  1562. struct kvm *kvm = ctx->kvm;
  1563. struct kvm_get_htab_header hdr;
  1564. unsigned long i, j;
  1565. unsigned long v, r;
  1566. unsigned long __user *lbuf;
  1567. __be64 *hptp;
  1568. unsigned long tmp[2];
  1569. ssize_t nb;
  1570. long int err, ret;
  1571. int mmu_ready;
  1572. int pshift;
  1573. if (!access_ok(VERIFY_READ, buf, count))
  1574. return -EFAULT;
  1575. if (kvm_is_radix(kvm))
  1576. return -EINVAL;
  1577. /* lock out vcpus from running while we're doing this */
  1578. mutex_lock(&kvm->lock);
  1579. mmu_ready = kvm->arch.mmu_ready;
  1580. if (mmu_ready) {
  1581. kvm->arch.mmu_ready = 0; /* temporarily */
  1582. /* order mmu_ready vs. vcpus_running */
  1583. smp_mb();
  1584. if (atomic_read(&kvm->arch.vcpus_running)) {
  1585. kvm->arch.mmu_ready = 1;
  1586. mutex_unlock(&kvm->lock);
  1587. return -EBUSY;
  1588. }
  1589. }
  1590. err = 0;
  1591. for (nb = 0; nb + sizeof(hdr) <= count; ) {
  1592. err = -EFAULT;
  1593. if (__copy_from_user(&hdr, buf, sizeof(hdr)))
  1594. break;
  1595. err = 0;
  1596. if (nb + hdr.n_valid * HPTE_SIZE > count)
  1597. break;
  1598. nb += sizeof(hdr);
  1599. buf += sizeof(hdr);
  1600. err = -EINVAL;
  1601. i = hdr.index;
  1602. if (i >= kvmppc_hpt_npte(&kvm->arch.hpt) ||
  1603. i + hdr.n_valid + hdr.n_invalid > kvmppc_hpt_npte(&kvm->arch.hpt))
  1604. break;
  1605. hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE));
  1606. lbuf = (unsigned long __user *)buf;
  1607. for (j = 0; j < hdr.n_valid; ++j) {
  1608. __be64 hpte_v;
  1609. __be64 hpte_r;
  1610. err = -EFAULT;
  1611. if (__get_user(hpte_v, lbuf) ||
  1612. __get_user(hpte_r, lbuf + 1))
  1613. goto out;
  1614. v = be64_to_cpu(hpte_v);
  1615. r = be64_to_cpu(hpte_r);
  1616. err = -EINVAL;
  1617. if (!(v & HPTE_V_VALID))
  1618. goto out;
  1619. pshift = kvmppc_hpte_base_page_shift(v, r);
  1620. if (pshift <= 0)
  1621. goto out;
  1622. lbuf += 2;
  1623. nb += HPTE_SIZE;
  1624. if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))
  1625. kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
  1626. err = -EIO;
  1627. ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, i, v, r,
  1628. tmp);
  1629. if (ret != H_SUCCESS) {
  1630. pr_err("kvm_htab_write ret %ld i=%ld v=%lx "
  1631. "r=%lx\n", ret, i, v, r);
  1632. goto out;
  1633. }
  1634. if (!mmu_ready && is_vrma_hpte(v)) {
  1635. unsigned long senc, lpcr;
  1636. senc = slb_pgsize_encoding(1ul << pshift);
  1637. kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
  1638. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  1639. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  1640. lpcr = senc << (LPCR_VRMASD_SH - 4);
  1641. kvmppc_update_lpcr(kvm, lpcr,
  1642. LPCR_VRMASD);
  1643. } else {
  1644. kvmppc_setup_partition_table(kvm);
  1645. }
  1646. mmu_ready = 1;
  1647. }
  1648. ++i;
  1649. hptp += 2;
  1650. }
  1651. for (j = 0; j < hdr.n_invalid; ++j) {
  1652. if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))
  1653. kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
  1654. ++i;
  1655. hptp += 2;
  1656. }
  1657. err = 0;
  1658. }
  1659. out:
  1660. /* Order HPTE updates vs. mmu_ready */
  1661. smp_wmb();
  1662. kvm->arch.mmu_ready = mmu_ready;
  1663. mutex_unlock(&kvm->lock);
  1664. if (err)
  1665. return err;
  1666. return nb;
  1667. }
  1668. static int kvm_htab_release(struct inode *inode, struct file *filp)
  1669. {
  1670. struct kvm_htab_ctx *ctx = filp->private_data;
  1671. filp->private_data = NULL;
  1672. if (!(ctx->flags & KVM_GET_HTAB_WRITE))
  1673. atomic_dec(&ctx->kvm->arch.hpte_mod_interest);
  1674. kvm_put_kvm(ctx->kvm);
  1675. kfree(ctx);
  1676. return 0;
  1677. }
  1678. static const struct file_operations kvm_htab_fops = {
  1679. .read = kvm_htab_read,
  1680. .write = kvm_htab_write,
  1681. .llseek = default_llseek,
  1682. .release = kvm_htab_release,
  1683. };
  1684. int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *ghf)
  1685. {
  1686. int ret;
  1687. struct kvm_htab_ctx *ctx;
  1688. int rwflag;
  1689. /* reject flags we don't recognize */
  1690. if (ghf->flags & ~(KVM_GET_HTAB_BOLTED_ONLY | KVM_GET_HTAB_WRITE))
  1691. return -EINVAL;
  1692. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1693. if (!ctx)
  1694. return -ENOMEM;
  1695. kvm_get_kvm(kvm);
  1696. ctx->kvm = kvm;
  1697. ctx->index = ghf->start_index;
  1698. ctx->flags = ghf->flags;
  1699. ctx->first_pass = 1;
  1700. rwflag = (ghf->flags & KVM_GET_HTAB_WRITE) ? O_WRONLY : O_RDONLY;
  1701. ret = anon_inode_getfd("kvm-htab", &kvm_htab_fops, ctx, rwflag | O_CLOEXEC);
  1702. if (ret < 0) {
  1703. kfree(ctx);
  1704. kvm_put_kvm(kvm);
  1705. return ret;
  1706. }
  1707. if (rwflag == O_RDONLY) {
  1708. mutex_lock(&kvm->slots_lock);
  1709. atomic_inc(&kvm->arch.hpte_mod_interest);
  1710. /* make sure kvmppc_do_h_enter etc. see the increment */
  1711. synchronize_srcu_expedited(&kvm->srcu);
  1712. mutex_unlock(&kvm->slots_lock);
  1713. }
  1714. return ret;
  1715. }
  1716. struct debugfs_htab_state {
  1717. struct kvm *kvm;
  1718. struct mutex mutex;
  1719. unsigned long hpt_index;
  1720. int chars_left;
  1721. int buf_index;
  1722. char buf[64];
  1723. };
  1724. static int debugfs_htab_open(struct inode *inode, struct file *file)
  1725. {
  1726. struct kvm *kvm = inode->i_private;
  1727. struct debugfs_htab_state *p;
  1728. p = kzalloc(sizeof(*p), GFP_KERNEL);
  1729. if (!p)
  1730. return -ENOMEM;
  1731. kvm_get_kvm(kvm);
  1732. p->kvm = kvm;
  1733. mutex_init(&p->mutex);
  1734. file->private_data = p;
  1735. return nonseekable_open(inode, file);
  1736. }
  1737. static int debugfs_htab_release(struct inode *inode, struct file *file)
  1738. {
  1739. struct debugfs_htab_state *p = file->private_data;
  1740. kvm_put_kvm(p->kvm);
  1741. kfree(p);
  1742. return 0;
  1743. }
  1744. static ssize_t debugfs_htab_read(struct file *file, char __user *buf,
  1745. size_t len, loff_t *ppos)
  1746. {
  1747. struct debugfs_htab_state *p = file->private_data;
  1748. ssize_t ret, r;
  1749. unsigned long i, n;
  1750. unsigned long v, hr, gr;
  1751. struct kvm *kvm;
  1752. __be64 *hptp;
  1753. kvm = p->kvm;
  1754. if (kvm_is_radix(kvm))
  1755. return 0;
  1756. ret = mutex_lock_interruptible(&p->mutex);
  1757. if (ret)
  1758. return ret;
  1759. if (p->chars_left) {
  1760. n = p->chars_left;
  1761. if (n > len)
  1762. n = len;
  1763. r = copy_to_user(buf, p->buf + p->buf_index, n);
  1764. n -= r;
  1765. p->chars_left -= n;
  1766. p->buf_index += n;
  1767. buf += n;
  1768. len -= n;
  1769. ret = n;
  1770. if (r) {
  1771. if (!n)
  1772. ret = -EFAULT;
  1773. goto out;
  1774. }
  1775. }
  1776. i = p->hpt_index;
  1777. hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE));
  1778. for (; len != 0 && i < kvmppc_hpt_npte(&kvm->arch.hpt);
  1779. ++i, hptp += 2) {
  1780. if (!(be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)))
  1781. continue;
  1782. /* lock the HPTE so it's stable and read it */
  1783. preempt_disable();
  1784. while (!try_lock_hpte(hptp, HPTE_V_HVLOCK))
  1785. cpu_relax();
  1786. v = be64_to_cpu(hptp[0]) & ~HPTE_V_HVLOCK;
  1787. hr = be64_to_cpu(hptp[1]);
  1788. gr = kvm->arch.hpt.rev[i].guest_rpte;
  1789. unlock_hpte(hptp, v);
  1790. preempt_enable();
  1791. if (!(v & (HPTE_V_VALID | HPTE_V_ABSENT)))
  1792. continue;
  1793. n = scnprintf(p->buf, sizeof(p->buf),
  1794. "%6lx %.16lx %.16lx %.16lx\n",
  1795. i, v, hr, gr);
  1796. p->chars_left = n;
  1797. if (n > len)
  1798. n = len;
  1799. r = copy_to_user(buf, p->buf, n);
  1800. n -= r;
  1801. p->chars_left -= n;
  1802. p->buf_index = n;
  1803. buf += n;
  1804. len -= n;
  1805. ret += n;
  1806. if (r) {
  1807. if (!ret)
  1808. ret = -EFAULT;
  1809. goto out;
  1810. }
  1811. }
  1812. p->hpt_index = i;
  1813. out:
  1814. mutex_unlock(&p->mutex);
  1815. return ret;
  1816. }
  1817. static ssize_t debugfs_htab_write(struct file *file, const char __user *buf,
  1818. size_t len, loff_t *ppos)
  1819. {
  1820. return -EACCES;
  1821. }
  1822. static const struct file_operations debugfs_htab_fops = {
  1823. .owner = THIS_MODULE,
  1824. .open = debugfs_htab_open,
  1825. .release = debugfs_htab_release,
  1826. .read = debugfs_htab_read,
  1827. .write = debugfs_htab_write,
  1828. .llseek = generic_file_llseek,
  1829. };
  1830. void kvmppc_mmu_debugfs_init(struct kvm *kvm)
  1831. {
  1832. kvm->arch.htab_dentry = debugfs_create_file("htab", 0400,
  1833. kvm->arch.debugfs_dir, kvm,
  1834. &debugfs_htab_fops);
  1835. }
  1836. void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
  1837. {
  1838. struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
  1839. vcpu->arch.slb_nr = 32; /* POWER7/POWER8 */
  1840. mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
  1841. mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
  1842. vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
  1843. }