book3s_32_mmu_host.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 void invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
  49. {
  50. volatile u32 *pteg;
  51. dprintk_mmu("KVM: Flushing SPTE: 0x%llx (0x%llx) -> 0x%llx\n",
  52. pte->pte.eaddr, pte->pte.vpage, pte->host_va);
  53. pteg = (u32*)pte->slot;
  54. pteg[0] = 0;
  55. asm volatile ("sync");
  56. asm volatile ("tlbie %0" : : "r" (pte->pte.eaddr) : "memory");
  57. asm volatile ("sync");
  58. asm volatile ("tlbsync");
  59. pte->host_va = 0;
  60. if (pte->pte.may_write)
  61. kvm_release_pfn_dirty(pte->pfn);
  62. else
  63. kvm_release_pfn_clean(pte->pfn);
  64. }
  65. void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, u64 _guest_ea, u64 _ea_mask)
  66. {
  67. int i;
  68. u32 guest_ea = _guest_ea;
  69. u32 ea_mask = _ea_mask;
  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, u64 pa_start, u64 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. extern struct hash_pte *Hash;
  178. extern unsigned long _SDR1;
  179. static u32 *kvmppc_mmu_get_pteg(struct kvm_vcpu *vcpu, u32 vsid, u32 eaddr,
  180. bool primary)
  181. {
  182. u32 page, hash, htabmask;
  183. ulong pteg = (ulong)Hash;
  184. page = (eaddr & ~ESID_MASK) >> 12;
  185. hash = ((vsid ^ page) << 6);
  186. if (!primary)
  187. hash = ~hash;
  188. htabmask = ((_SDR1 & 0x1FF) << 16) | 0xFFC0;
  189. hash &= htabmask;
  190. pteg |= hash;
  191. dprintk_mmu("htab: %p | hash: %x | htabmask: %x | pteg: %lx\n",
  192. Hash, hash, htabmask, pteg);
  193. return (u32*)pteg;
  194. }
  195. extern char etext[];
  196. int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
  197. {
  198. pfn_t hpaddr;
  199. u64 va;
  200. u64 vsid;
  201. struct kvmppc_sid_map *map;
  202. volatile u32 *pteg;
  203. u32 eaddr = orig_pte->eaddr;
  204. u32 pteg0, pteg1;
  205. register int rr = 0;
  206. bool primary = false;
  207. bool evict = false;
  208. int hpte_id;
  209. struct hpte_cache *pte;
  210. /* Get host physical address for gpa */
  211. hpaddr = gfn_to_pfn(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
  212. if (kvm_is_error_hva(hpaddr)) {
  213. printk(KERN_INFO "Couldn't get guest page for gfn %llx!\n",
  214. orig_pte->eaddr);
  215. return -EINVAL;
  216. }
  217. hpaddr <<= PAGE_SHIFT;
  218. /* and write the mapping ea -> hpa into the pt */
  219. vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
  220. map = find_sid_vsid(vcpu, vsid);
  221. if (!map) {
  222. kvmppc_mmu_map_segment(vcpu, eaddr);
  223. map = find_sid_vsid(vcpu, vsid);
  224. }
  225. BUG_ON(!map);
  226. vsid = map->host_vsid;
  227. va = (vsid << SID_SHIFT) | (eaddr & ~ESID_MASK);
  228. next_pteg:
  229. if (rr == 16) {
  230. primary = !primary;
  231. evict = true;
  232. rr = 0;
  233. }
  234. pteg = kvmppc_mmu_get_pteg(vcpu, vsid, eaddr, primary);
  235. /* not evicting yet */
  236. if (!evict && (pteg[rr] & PTE_V)) {
  237. rr += 2;
  238. goto next_pteg;
  239. }
  240. dprintk_mmu("KVM: old PTEG: %p (%d)\n", pteg, rr);
  241. dprintk_mmu("KVM: %08x - %08x\n", pteg[0], pteg[1]);
  242. dprintk_mmu("KVM: %08x - %08x\n", pteg[2], pteg[3]);
  243. dprintk_mmu("KVM: %08x - %08x\n", pteg[4], pteg[5]);
  244. dprintk_mmu("KVM: %08x - %08x\n", pteg[6], pteg[7]);
  245. dprintk_mmu("KVM: %08x - %08x\n", pteg[8], pteg[9]);
  246. dprintk_mmu("KVM: %08x - %08x\n", pteg[10], pteg[11]);
  247. dprintk_mmu("KVM: %08x - %08x\n", pteg[12], pteg[13]);
  248. dprintk_mmu("KVM: %08x - %08x\n", pteg[14], pteg[15]);
  249. pteg0 = ((eaddr & 0x0fffffff) >> 22) | (vsid << 7) | PTE_V |
  250. (primary ? 0 : PTE_SEC);
  251. pteg1 = hpaddr | PTE_M | PTE_R | PTE_C;
  252. if (orig_pte->may_write) {
  253. pteg1 |= PP_RWRW;
  254. mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
  255. } else {
  256. pteg1 |= PP_RWRX;
  257. }
  258. local_irq_disable();
  259. if (pteg[rr]) {
  260. pteg[rr] = 0;
  261. asm volatile ("sync");
  262. }
  263. pteg[rr + 1] = pteg1;
  264. pteg[rr] = pteg0;
  265. asm volatile ("sync");
  266. local_irq_enable();
  267. dprintk_mmu("KVM: new PTEG: %p\n", pteg);
  268. dprintk_mmu("KVM: %08x - %08x\n", pteg[0], pteg[1]);
  269. dprintk_mmu("KVM: %08x - %08x\n", pteg[2], pteg[3]);
  270. dprintk_mmu("KVM: %08x - %08x\n", pteg[4], pteg[5]);
  271. dprintk_mmu("KVM: %08x - %08x\n", pteg[6], pteg[7]);
  272. dprintk_mmu("KVM: %08x - %08x\n", pteg[8], pteg[9]);
  273. dprintk_mmu("KVM: %08x - %08x\n", pteg[10], pteg[11]);
  274. dprintk_mmu("KVM: %08x - %08x\n", pteg[12], pteg[13]);
  275. dprintk_mmu("KVM: %08x - %08x\n", pteg[14], pteg[15]);
  276. /* Now tell our Shadow PTE code about the new page */
  277. hpte_id = kvmppc_mmu_hpte_cache_next(vcpu);
  278. pte = &vcpu->arch.hpte_cache[hpte_id];
  279. dprintk_mmu("KVM: %c%c Map 0x%llx: [%lx] 0x%llx (0x%llx) -> %lx\n",
  280. orig_pte->may_write ? 'w' : '-',
  281. orig_pte->may_execute ? 'x' : '-',
  282. orig_pte->eaddr, (ulong)pteg, va,
  283. orig_pte->vpage, hpaddr);
  284. pte->slot = (ulong)&pteg[rr];
  285. pte->host_va = va;
  286. pte->pte = *orig_pte;
  287. pte->pfn = hpaddr >> PAGE_SHIFT;
  288. return 0;
  289. }
  290. static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
  291. {
  292. struct kvmppc_sid_map *map;
  293. struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
  294. u16 sid_map_mask;
  295. static int backwards_map = 0;
  296. if (vcpu->arch.msr & MSR_PR)
  297. gvsid |= VSID_PR;
  298. /* We might get collisions that trap in preceding order, so let's
  299. map them differently */
  300. sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
  301. if (backwards_map)
  302. sid_map_mask = SID_MAP_MASK - sid_map_mask;
  303. map = &to_book3s(vcpu)->sid_map[sid_map_mask];
  304. /* Make sure we're taking the other map next time */
  305. backwards_map = !backwards_map;
  306. /* Uh-oh ... out of mappings. Let's flush! */
  307. if (vcpu_book3s->vsid_next >= vcpu_book3s->vsid_max) {
  308. vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
  309. memset(vcpu_book3s->sid_map, 0,
  310. sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
  311. kvmppc_mmu_pte_flush(vcpu, 0, 0);
  312. kvmppc_mmu_flush_segments(vcpu);
  313. }
  314. map->host_vsid = vcpu_book3s->vsid_next;
  315. /* Would have to be 111 to be completely aligned with the rest of
  316. Linux, but that is just way too little space! */
  317. vcpu_book3s->vsid_next+=1;
  318. map->guest_vsid = gvsid;
  319. map->valid = true;
  320. return map;
  321. }
  322. int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
  323. {
  324. u32 esid = eaddr >> SID_SHIFT;
  325. u64 gvsid;
  326. u32 sr;
  327. struct kvmppc_sid_map *map;
  328. struct kvmppc_book3s_shadow_vcpu *svcpu = to_svcpu(vcpu);
  329. if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
  330. /* Invalidate an entry */
  331. svcpu->sr[esid] = SR_INVALID;
  332. return -ENOENT;
  333. }
  334. map = find_sid_vsid(vcpu, gvsid);
  335. if (!map)
  336. map = create_sid_map(vcpu, gvsid);
  337. map->guest_esid = esid;
  338. sr = map->host_vsid | SR_KP;
  339. svcpu->sr[esid] = sr;
  340. dprintk_sr("MMU: mtsr %d, 0x%x\n", esid, sr);
  341. return 0;
  342. }
  343. void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
  344. {
  345. int i;
  346. struct kvmppc_book3s_shadow_vcpu *svcpu = to_svcpu(vcpu);
  347. dprintk_sr("MMU: flushing all segments (%d)\n", ARRAY_SIZE(svcpu->sr));
  348. for (i = 0; i < ARRAY_SIZE(svcpu->sr); i++)
  349. svcpu->sr[i] = SR_INVALID;
  350. }
  351. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  352. {
  353. kvmppc_mmu_pte_flush(vcpu, 0, 0);
  354. preempt_disable();
  355. __destroy_context(to_book3s(vcpu)->context_id);
  356. preempt_enable();
  357. }
  358. /* From mm/mmu_context_hash32.c */
  359. #define CTX_TO_VSID(ctx) (((ctx) * (897 * 16)) & 0xffffff)
  360. int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
  361. {
  362. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  363. int err;
  364. err = __init_new_context();
  365. if (err < 0)
  366. return -1;
  367. vcpu3s->context_id = err;
  368. vcpu3s->vsid_max = CTX_TO_VSID(vcpu3s->context_id + 1) - 1;
  369. vcpu3s->vsid_first = CTX_TO_VSID(vcpu3s->context_id);
  370. #if 0 /* XXX still doesn't guarantee uniqueness */
  371. /* We could collide with the Linux vsid space because the vsid
  372. * wraps around at 24 bits. We're safe if we do our own space
  373. * though, so let's always set the highest bit. */
  374. vcpu3s->vsid_max |= 0x00800000;
  375. vcpu3s->vsid_first |= 0x00800000;
  376. #endif
  377. BUG_ON(vcpu3s->vsid_max < vcpu3s->vsid_first);
  378. vcpu3s->vsid_next = vcpu3s->vsid_first;
  379. return 0;
  380. }