book3s_32_mmu_host.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.
  3. *
  4. * Authors:
  5. * Alexander Graf <agraf@suse.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License, version 2, as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include <linux/kvm_host.h>
  21. #include <asm/kvm_ppc.h>
  22. #include <asm/kvm_book3s.h>
  23. #include <asm/mmu-hash32.h>
  24. #include <asm/machdep.h>
  25. #include <asm/mmu_context.h>
  26. #include <asm/hw_irq.h>
  27. /* #define DEBUG_MMU */
  28. /* #define DEBUG_SR */
  29. #ifdef DEBUG_MMU
  30. #define dprintk_mmu(a, ...) printk(KERN_INFO a, __VA_ARGS__)
  31. #else
  32. #define dprintk_mmu(a, ...) do { } while(0)
  33. #endif
  34. #ifdef DEBUG_SR
  35. #define dprintk_sr(a, ...) printk(KERN_INFO a, __VA_ARGS__)
  36. #else
  37. #define dprintk_sr(a, ...) do { } while(0)
  38. #endif
  39. #if PAGE_SHIFT != 12
  40. #error Unknown page size
  41. #endif
  42. #ifdef CONFIG_SMP
  43. #error XXX need to grab mmu_hash_lock
  44. #endif
  45. #ifdef CONFIG_PTE_64BIT
  46. #error Only 32 bit pages are supported for now
  47. #endif
  48. static ulong htab;
  49. static u32 htabmask;
  50. static void invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
  51. {
  52. volatile u32 *pteg;
  53. dprintk_mmu("KVM: Flushing SPTE: 0x%llx (0x%llx) -> 0x%llx\n",
  54. pte->pte.eaddr, pte->pte.vpage, pte->host_va);
  55. pteg = (u32*)pte->slot;
  56. pteg[0] = 0;
  57. asm volatile ("sync");
  58. asm volatile ("tlbie %0" : : "r" (pte->pte.eaddr) : "memory");
  59. asm volatile ("sync");
  60. asm volatile ("tlbsync");
  61. pte->host_va = 0;
  62. if (pte->pte.may_write)
  63. kvm_release_pfn_dirty(pte->pfn);
  64. else
  65. kvm_release_pfn_clean(pte->pfn);
  66. }
  67. void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong guest_ea, ulong ea_mask)
  68. {
  69. int i;
  70. dprintk_mmu("KVM: Flushing %d Shadow PTEs: 0x%x & 0x%x\n",
  71. vcpu->arch.hpte_cache_offset, guest_ea, ea_mask);
  72. BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
  73. guest_ea &= ea_mask;
  74. for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
  75. struct hpte_cache *pte;
  76. pte = &vcpu->arch.hpte_cache[i];
  77. if (!pte->host_va)
  78. continue;
  79. if ((pte->pte.eaddr & ea_mask) == guest_ea) {
  80. invalidate_pte(vcpu, pte);
  81. }
  82. }
  83. /* Doing a complete flush -> start from scratch */
  84. if (!ea_mask)
  85. vcpu->arch.hpte_cache_offset = 0;
  86. }
  87. void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 guest_vp, u64 vp_mask)
  88. {
  89. int i;
  90. dprintk_mmu("KVM: Flushing %d Shadow vPTEs: 0x%llx & 0x%llx\n",
  91. vcpu->arch.hpte_cache_offset, guest_vp, vp_mask);
  92. BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
  93. guest_vp &= vp_mask;
  94. for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
  95. struct hpte_cache *pte;
  96. pte = &vcpu->arch.hpte_cache[i];
  97. if (!pte->host_va)
  98. continue;
  99. if ((pte->pte.vpage & vp_mask) == guest_vp) {
  100. invalidate_pte(vcpu, pte);
  101. }
  102. }
  103. }
  104. void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
  105. {
  106. int i;
  107. dprintk_mmu("KVM: Flushing %d Shadow pPTEs: 0x%llx & 0x%llx\n",
  108. vcpu->arch.hpte_cache_offset, pa_start, pa_end);
  109. BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
  110. for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
  111. struct hpte_cache *pte;
  112. pte = &vcpu->arch.hpte_cache[i];
  113. if (!pte->host_va)
  114. continue;
  115. if ((pte->pte.raddr >= pa_start) &&
  116. (pte->pte.raddr < pa_end)) {
  117. invalidate_pte(vcpu, pte);
  118. }
  119. }
  120. }
  121. struct kvmppc_pte *kvmppc_mmu_find_pte(struct kvm_vcpu *vcpu, u64 ea, bool data)
  122. {
  123. int i;
  124. u64 guest_vp;
  125. guest_vp = vcpu->arch.mmu.ea_to_vp(vcpu, ea, false);
  126. for (i=0; i<vcpu->arch.hpte_cache_offset; i++) {
  127. struct hpte_cache *pte;
  128. pte = &vcpu->arch.hpte_cache[i];
  129. if (!pte->host_va)
  130. continue;
  131. if (pte->pte.vpage == guest_vp)
  132. return &pte->pte;
  133. }
  134. return NULL;
  135. }
  136. static int kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu)
  137. {
  138. if (vcpu->arch.hpte_cache_offset == HPTEG_CACHE_NUM)
  139. kvmppc_mmu_pte_flush(vcpu, 0, 0);
  140. return vcpu->arch.hpte_cache_offset++;
  141. }
  142. /* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
  143. * a hash, so we don't waste cycles on looping */
  144. static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)
  145. {
  146. return (u16)(((gvsid >> (SID_MAP_BITS * 7)) & SID_MAP_MASK) ^
  147. ((gvsid >> (SID_MAP_BITS * 6)) & SID_MAP_MASK) ^
  148. ((gvsid >> (SID_MAP_BITS * 5)) & SID_MAP_MASK) ^
  149. ((gvsid >> (SID_MAP_BITS * 4)) & SID_MAP_MASK) ^
  150. ((gvsid >> (SID_MAP_BITS * 3)) & SID_MAP_MASK) ^
  151. ((gvsid >> (SID_MAP_BITS * 2)) & SID_MAP_MASK) ^
  152. ((gvsid >> (SID_MAP_BITS * 1)) & SID_MAP_MASK) ^
  153. ((gvsid >> (SID_MAP_BITS * 0)) & SID_MAP_MASK));
  154. }
  155. static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
  156. {
  157. struct kvmppc_sid_map *map;
  158. u16 sid_map_mask;
  159. if (vcpu->arch.msr & MSR_PR)
  160. gvsid |= VSID_PR;
  161. sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
  162. map = &to_book3s(vcpu)->sid_map[sid_map_mask];
  163. if (map->guest_vsid == gvsid) {
  164. dprintk_sr("SR: Searching 0x%llx -> 0x%llx\n",
  165. gvsid, map->host_vsid);
  166. return map;
  167. }
  168. map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];
  169. if (map->guest_vsid == gvsid) {
  170. dprintk_sr("SR: Searching 0x%llx -> 0x%llx\n",
  171. gvsid, map->host_vsid);
  172. return map;
  173. }
  174. dprintk_sr("SR: Searching 0x%llx -> not found\n", gvsid);
  175. return NULL;
  176. }
  177. static u32 *kvmppc_mmu_get_pteg(struct kvm_vcpu *vcpu, u32 vsid, u32 eaddr,
  178. bool primary)
  179. {
  180. u32 page, hash;
  181. ulong pteg = htab;
  182. page = (eaddr & ~ESID_MASK) >> 12;
  183. hash = ((vsid ^ page) << 6);
  184. if (!primary)
  185. hash = ~hash;
  186. hash &= htabmask;
  187. pteg |= hash;
  188. dprintk_mmu("htab: %lx | hash: %x | htabmask: %x | pteg: %lx\n",
  189. htab, hash, htabmask, pteg);
  190. return (u32*)pteg;
  191. }
  192. extern char etext[];
  193. int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
  194. {
  195. pfn_t hpaddr;
  196. u64 va;
  197. u64 vsid;
  198. struct kvmppc_sid_map *map;
  199. volatile u32 *pteg;
  200. u32 eaddr = orig_pte->eaddr;
  201. u32 pteg0, pteg1;
  202. register int rr = 0;
  203. bool primary = false;
  204. bool evict = false;
  205. int hpte_id;
  206. struct hpte_cache *pte;
  207. /* Get host physical address for gpa */
  208. hpaddr = gfn_to_pfn(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
  209. if (kvm_is_error_hva(hpaddr)) {
  210. printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n",
  211. orig_pte->eaddr);
  212. return -EINVAL;
  213. }
  214. hpaddr <<= PAGE_SHIFT;
  215. /* and write the mapping ea -> hpa into the pt */
  216. vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
  217. map = find_sid_vsid(vcpu, vsid);
  218. if (!map) {
  219. kvmppc_mmu_map_segment(vcpu, eaddr);
  220. map = find_sid_vsid(vcpu, vsid);
  221. }
  222. BUG_ON(!map);
  223. vsid = map->host_vsid;
  224. va = (vsid << SID_SHIFT) | (eaddr & ~ESID_MASK);
  225. next_pteg:
  226. if (rr == 16) {
  227. primary = !primary;
  228. evict = true;
  229. rr = 0;
  230. }
  231. pteg = kvmppc_mmu_get_pteg(vcpu, vsid, eaddr, primary);
  232. /* not evicting yet */
  233. if (!evict && (pteg[rr] & PTE_V)) {
  234. rr += 2;
  235. goto next_pteg;
  236. }
  237. dprintk_mmu("KVM: old PTEG: %p (%d)\n", pteg, rr);
  238. dprintk_mmu("KVM: %08x - %08x\n", pteg[0], pteg[1]);
  239. dprintk_mmu("KVM: %08x - %08x\n", pteg[2], pteg[3]);
  240. dprintk_mmu("KVM: %08x - %08x\n", pteg[4], pteg[5]);
  241. dprintk_mmu("KVM: %08x - %08x\n", pteg[6], pteg[7]);
  242. dprintk_mmu("KVM: %08x - %08x\n", pteg[8], pteg[9]);
  243. dprintk_mmu("KVM: %08x - %08x\n", pteg[10], pteg[11]);
  244. dprintk_mmu("KVM: %08x - %08x\n", pteg[12], pteg[13]);
  245. dprintk_mmu("KVM: %08x - %08x\n", pteg[14], pteg[15]);
  246. pteg0 = ((eaddr & 0x0fffffff) >> 22) | (vsid << 7) | PTE_V |
  247. (primary ? 0 : PTE_SEC);
  248. pteg1 = hpaddr | PTE_M | PTE_R | PTE_C;
  249. if (orig_pte->may_write) {
  250. pteg1 |= PP_RWRW;
  251. mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
  252. } else {
  253. pteg1 |= PP_RWRX;
  254. }
  255. local_irq_disable();
  256. if (pteg[rr]) {
  257. pteg[rr] = 0;
  258. asm volatile ("sync");
  259. }
  260. pteg[rr + 1] = pteg1;
  261. pteg[rr] = pteg0;
  262. asm volatile ("sync");
  263. local_irq_enable();
  264. dprintk_mmu("KVM: new PTEG: %p\n", pteg);
  265. dprintk_mmu("KVM: %08x - %08x\n", pteg[0], pteg[1]);
  266. dprintk_mmu("KVM: %08x - %08x\n", pteg[2], pteg[3]);
  267. dprintk_mmu("KVM: %08x - %08x\n", pteg[4], pteg[5]);
  268. dprintk_mmu("KVM: %08x - %08x\n", pteg[6], pteg[7]);
  269. dprintk_mmu("KVM: %08x - %08x\n", pteg[8], pteg[9]);
  270. dprintk_mmu("KVM: %08x - %08x\n", pteg[10], pteg[11]);
  271. dprintk_mmu("KVM: %08x - %08x\n", pteg[12], pteg[13]);
  272. dprintk_mmu("KVM: %08x - %08x\n", pteg[14], pteg[15]);
  273. /* Now tell our Shadow PTE code about the new page */
  274. hpte_id = kvmppc_mmu_hpte_cache_next(vcpu);
  275. pte = &vcpu->arch.hpte_cache[hpte_id];
  276. dprintk_mmu("KVM: %c%c Map 0x%llx: [%lx] 0x%llx (0x%llx) -> %lx\n",
  277. orig_pte->may_write ? 'w' : '-',
  278. orig_pte->may_execute ? 'x' : '-',
  279. orig_pte->eaddr, (ulong)pteg, va,
  280. orig_pte->vpage, hpaddr);
  281. pte->slot = (ulong)&pteg[rr];
  282. pte->host_va = va;
  283. pte->pte = *orig_pte;
  284. pte->pfn = hpaddr >> PAGE_SHIFT;
  285. return 0;
  286. }
  287. static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
  288. {
  289. struct kvmppc_sid_map *map;
  290. struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
  291. u16 sid_map_mask;
  292. static int backwards_map = 0;
  293. if (vcpu->arch.msr & MSR_PR)
  294. gvsid |= VSID_PR;
  295. /* We might get collisions that trap in preceding order, so let's
  296. map them differently */
  297. sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
  298. if (backwards_map)
  299. sid_map_mask = SID_MAP_MASK - sid_map_mask;
  300. map = &to_book3s(vcpu)->sid_map[sid_map_mask];
  301. /* Make sure we're taking the other map next time */
  302. backwards_map = !backwards_map;
  303. /* Uh-oh ... out of mappings. Let's flush! */
  304. if (vcpu_book3s->vsid_next >= vcpu_book3s->vsid_max) {
  305. vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
  306. memset(vcpu_book3s->sid_map, 0,
  307. sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
  308. kvmppc_mmu_pte_flush(vcpu, 0, 0);
  309. kvmppc_mmu_flush_segments(vcpu);
  310. }
  311. map->host_vsid = vcpu_book3s->vsid_next;
  312. /* Would have to be 111 to be completely aligned with the rest of
  313. Linux, but that is just way too little space! */
  314. vcpu_book3s->vsid_next+=1;
  315. map->guest_vsid = gvsid;
  316. map->valid = true;
  317. return map;
  318. }
  319. int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
  320. {
  321. u32 esid = eaddr >> SID_SHIFT;
  322. u64 gvsid;
  323. u32 sr;
  324. struct kvmppc_sid_map *map;
  325. struct kvmppc_book3s_shadow_vcpu *svcpu = to_svcpu(vcpu);
  326. if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
  327. /* Invalidate an entry */
  328. svcpu->sr[esid] = SR_INVALID;
  329. return -ENOENT;
  330. }
  331. map = find_sid_vsid(vcpu, gvsid);
  332. if (!map)
  333. map = create_sid_map(vcpu, gvsid);
  334. map->guest_esid = esid;
  335. sr = map->host_vsid | SR_KP;
  336. svcpu->sr[esid] = sr;
  337. dprintk_sr("MMU: mtsr %d, 0x%x\n", esid, sr);
  338. return 0;
  339. }
  340. void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
  341. {
  342. int i;
  343. struct kvmppc_book3s_shadow_vcpu *svcpu = to_svcpu(vcpu);
  344. dprintk_sr("MMU: flushing all segments (%d)\n", ARRAY_SIZE(svcpu->sr));
  345. for (i = 0; i < ARRAY_SIZE(svcpu->sr); i++)
  346. svcpu->sr[i] = SR_INVALID;
  347. }
  348. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  349. {
  350. kvmppc_mmu_pte_flush(vcpu, 0, 0);
  351. preempt_disable();
  352. __destroy_context(to_book3s(vcpu)->context_id);
  353. preempt_enable();
  354. }
  355. /* From mm/mmu_context_hash32.c */
  356. #define CTX_TO_VSID(ctx) (((ctx) * (897 * 16)) & 0xffffff)
  357. int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
  358. {
  359. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  360. int err;
  361. ulong sdr1;
  362. err = __init_new_context();
  363. if (err < 0)
  364. return -1;
  365. vcpu3s->context_id = err;
  366. vcpu3s->vsid_max = CTX_TO_VSID(vcpu3s->context_id + 1) - 1;
  367. vcpu3s->vsid_first = CTX_TO_VSID(vcpu3s->context_id);
  368. #if 0 /* XXX still doesn't guarantee uniqueness */
  369. /* We could collide with the Linux vsid space because the vsid
  370. * wraps around at 24 bits. We're safe if we do our own space
  371. * though, so let's always set the highest bit. */
  372. vcpu3s->vsid_max |= 0x00800000;
  373. vcpu3s->vsid_first |= 0x00800000;
  374. #endif
  375. BUG_ON(vcpu3s->vsid_max < vcpu3s->vsid_first);
  376. vcpu3s->vsid_next = vcpu3s->vsid_first;
  377. /* Remember where the HTAB is */
  378. asm ( "mfsdr1 %0" : "=r"(sdr1) );
  379. htabmask = ((sdr1 & 0x1FF) << 16) | 0xFFC0;
  380. htab = (ulong)__va(sdr1 & 0xffff0000);
  381. return 0;
  382. }