book3s_32_mmu_host.c 11 KB

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