book3s_32_mmu.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 SUSE Linux Products GmbH 2009
  16. *
  17. * Authors: Alexander Graf <agraf@suse.de>
  18. */
  19. #include <linux/types.h>
  20. #include <linux/string.h>
  21. #include <linux/kvm.h>
  22. #include <linux/kvm_host.h>
  23. #include <linux/highmem.h>
  24. #include <asm/tlbflush.h>
  25. #include <asm/kvm_ppc.h>
  26. #include <asm/kvm_book3s.h>
  27. /* #define DEBUG_MMU */
  28. /* #define DEBUG_MMU_PTE */
  29. /* #define DEBUG_MMU_PTE_IP 0xfff14c40 */
  30. #ifdef DEBUG_MMU
  31. #define dprintk(X...) printk(KERN_INFO X)
  32. #else
  33. #define dprintk(X...) do { } while(0)
  34. #endif
  35. #ifdef DEBUG_MMU_PTE
  36. #define dprintk_pte(X...) printk(KERN_INFO X)
  37. #else
  38. #define dprintk_pte(X...) do { } while(0)
  39. #endif
  40. #define PTEG_FLAG_ACCESSED 0x00000100
  41. #define PTEG_FLAG_DIRTY 0x00000080
  42. static inline bool check_debug_ip(struct kvm_vcpu *vcpu)
  43. {
  44. #ifdef DEBUG_MMU_PTE_IP
  45. return vcpu->arch.pc == DEBUG_MMU_PTE_IP;
  46. #else
  47. return true;
  48. #endif
  49. }
  50. static int kvmppc_mmu_book3s_32_xlate_bat(struct kvm_vcpu *vcpu, gva_t eaddr,
  51. struct kvmppc_pte *pte, bool data);
  52. static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu *vcpu, u64 esid,
  53. u64 *vsid);
  54. static struct kvmppc_sr *find_sr(struct kvmppc_vcpu_book3s *vcpu_book3s, gva_t eaddr)
  55. {
  56. return &vcpu_book3s->sr[(eaddr >> 28) & 0xf];
  57. }
  58. static u64 kvmppc_mmu_book3s_32_ea_to_vp(struct kvm_vcpu *vcpu, gva_t eaddr,
  59. bool data)
  60. {
  61. u64 vsid;
  62. struct kvmppc_pte pte;
  63. if (!kvmppc_mmu_book3s_32_xlate_bat(vcpu, eaddr, &pte, data))
  64. return pte.vpage;
  65. kvmppc_mmu_book3s_32_esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
  66. return (((u64)eaddr >> 12) & 0xffff) | (vsid << 16);
  67. }
  68. static void kvmppc_mmu_book3s_32_reset_msr(struct kvm_vcpu *vcpu)
  69. {
  70. kvmppc_set_msr(vcpu, 0);
  71. }
  72. static hva_t kvmppc_mmu_book3s_32_get_pteg(struct kvmppc_vcpu_book3s *vcpu_book3s,
  73. struct kvmppc_sr *sre, gva_t eaddr,
  74. bool primary)
  75. {
  76. u32 page, hash, pteg, htabmask;
  77. hva_t r;
  78. page = (eaddr & 0x0FFFFFFF) >> 12;
  79. htabmask = ((vcpu_book3s->sdr1 & 0x1FF) << 16) | 0xFFC0;
  80. hash = ((sre->vsid ^ page) << 6);
  81. if (!primary)
  82. hash = ~hash;
  83. hash &= htabmask;
  84. pteg = (vcpu_book3s->sdr1 & 0xffff0000) | hash;
  85. dprintk("MMU: pc=0x%lx eaddr=0x%lx sdr1=0x%llx pteg=0x%x vsid=0x%x\n",
  86. vcpu_book3s->vcpu.arch.pc, eaddr, vcpu_book3s->sdr1, pteg,
  87. sre->vsid);
  88. r = gfn_to_hva(vcpu_book3s->vcpu.kvm, pteg >> PAGE_SHIFT);
  89. if (kvm_is_error_hva(r))
  90. return r;
  91. return r | (pteg & ~PAGE_MASK);
  92. }
  93. static u32 kvmppc_mmu_book3s_32_get_ptem(struct kvmppc_sr *sre, gva_t eaddr,
  94. bool primary)
  95. {
  96. return ((eaddr & 0x0fffffff) >> 22) | (sre->vsid << 7) |
  97. (primary ? 0 : 0x40) | 0x80000000;
  98. }
  99. static int kvmppc_mmu_book3s_32_xlate_bat(struct kvm_vcpu *vcpu, gva_t eaddr,
  100. struct kvmppc_pte *pte, bool data)
  101. {
  102. struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
  103. struct kvmppc_bat *bat;
  104. int i;
  105. for (i = 0; i < 8; i++) {
  106. if (data)
  107. bat = &vcpu_book3s->dbat[i];
  108. else
  109. bat = &vcpu_book3s->ibat[i];
  110. if (vcpu->arch.msr & MSR_PR) {
  111. if (!bat->vp)
  112. continue;
  113. } else {
  114. if (!bat->vs)
  115. continue;
  116. }
  117. if (check_debug_ip(vcpu))
  118. {
  119. dprintk_pte("%cBAT %02d: 0x%lx - 0x%x (0x%x)\n",
  120. data ? 'd' : 'i', i, eaddr, bat->bepi,
  121. bat->bepi_mask);
  122. }
  123. if ((eaddr & bat->bepi_mask) == bat->bepi) {
  124. u64 vsid;
  125. kvmppc_mmu_book3s_32_esid_to_vsid(vcpu,
  126. eaddr >> SID_SHIFT, &vsid);
  127. vsid <<= 16;
  128. pte->vpage = (((u64)eaddr >> 12) & 0xffff) | vsid;
  129. pte->raddr = bat->brpn | (eaddr & ~bat->bepi_mask);
  130. pte->may_read = bat->pp;
  131. pte->may_write = bat->pp > 1;
  132. pte->may_execute = true;
  133. if (!pte->may_read) {
  134. printk(KERN_INFO "BAT is not readable!\n");
  135. continue;
  136. }
  137. if (!pte->may_write) {
  138. /* let's treat r/o BATs as not-readable for now */
  139. dprintk_pte("BAT is read-only!\n");
  140. continue;
  141. }
  142. return 0;
  143. }
  144. }
  145. return -ENOENT;
  146. }
  147. static int kvmppc_mmu_book3s_32_xlate_pte(struct kvm_vcpu *vcpu, gva_t eaddr,
  148. struct kvmppc_pte *pte, bool data,
  149. bool primary)
  150. {
  151. struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
  152. struct kvmppc_sr *sre;
  153. hva_t ptegp;
  154. u32 pteg[16];
  155. u64 ptem = 0;
  156. int i;
  157. int found = 0;
  158. sre = find_sr(vcpu_book3s, eaddr);
  159. dprintk_pte("SR 0x%lx: vsid=0x%x, raw=0x%x\n", eaddr >> 28,
  160. sre->vsid, sre->raw);
  161. pte->vpage = kvmppc_mmu_book3s_32_ea_to_vp(vcpu, eaddr, data);
  162. ptegp = kvmppc_mmu_book3s_32_get_pteg(vcpu_book3s, sre, eaddr, primary);
  163. if (kvm_is_error_hva(ptegp)) {
  164. printk(KERN_INFO "KVM: Invalid PTEG!\n");
  165. goto no_page_found;
  166. }
  167. ptem = kvmppc_mmu_book3s_32_get_ptem(sre, eaddr, primary);
  168. if(copy_from_user(pteg, (void __user *)ptegp, sizeof(pteg))) {
  169. printk(KERN_ERR "KVM: Can't copy data from 0x%lx!\n", ptegp);
  170. goto no_page_found;
  171. }
  172. for (i=0; i<16; i+=2) {
  173. if (ptem == pteg[i]) {
  174. u8 pp;
  175. pte->raddr = (pteg[i+1] & ~(0xFFFULL)) | (eaddr & 0xFFF);
  176. pp = pteg[i+1] & 3;
  177. if ((sre->Kp && (vcpu->arch.msr & MSR_PR)) ||
  178. (sre->Ks && !(vcpu->arch.msr & MSR_PR)))
  179. pp |= 4;
  180. pte->may_write = false;
  181. pte->may_read = false;
  182. pte->may_execute = true;
  183. switch (pp) {
  184. case 0:
  185. case 1:
  186. case 2:
  187. case 6:
  188. pte->may_write = true;
  189. case 3:
  190. case 5:
  191. case 7:
  192. pte->may_read = true;
  193. break;
  194. }
  195. if ( !pte->may_read )
  196. continue;
  197. dprintk_pte("MMU: Found PTE -> %x %x - %x\n",
  198. pteg[i], pteg[i+1], pp);
  199. found = 1;
  200. break;
  201. }
  202. }
  203. /* Update PTE C and A bits, so the guest's swapper knows we used the
  204. page */
  205. if (found) {
  206. u32 oldpte = pteg[i+1];
  207. if (pte->may_read)
  208. pteg[i+1] |= PTEG_FLAG_ACCESSED;
  209. if (pte->may_write)
  210. pteg[i+1] |= PTEG_FLAG_DIRTY;
  211. else
  212. dprintk_pte("KVM: Mapping read-only page!\n");
  213. /* Write back into the PTEG */
  214. if (pteg[i+1] != oldpte)
  215. copy_to_user((void __user *)ptegp, pteg, sizeof(pteg));
  216. return 0;
  217. }
  218. no_page_found:
  219. if (check_debug_ip(vcpu)) {
  220. dprintk_pte("KVM MMU: No PTE found (sdr1=0x%llx ptegp=0x%lx)\n",
  221. to_book3s(vcpu)->sdr1, ptegp);
  222. for (i=0; i<16; i+=2) {
  223. dprintk_pte(" %02d: 0x%x - 0x%x (0x%llx)\n",
  224. i, pteg[i], pteg[i+1], ptem);
  225. }
  226. }
  227. return -ENOENT;
  228. }
  229. static int kvmppc_mmu_book3s_32_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
  230. struct kvmppc_pte *pte, bool data)
  231. {
  232. int r;
  233. pte->eaddr = eaddr;
  234. r = kvmppc_mmu_book3s_32_xlate_bat(vcpu, eaddr, pte, data);
  235. if (r < 0)
  236. r = kvmppc_mmu_book3s_32_xlate_pte(vcpu, eaddr, pte, data, true);
  237. if (r < 0)
  238. r = kvmppc_mmu_book3s_32_xlate_pte(vcpu, eaddr, pte, data, false);
  239. return r;
  240. }
  241. static u32 kvmppc_mmu_book3s_32_mfsrin(struct kvm_vcpu *vcpu, u32 srnum)
  242. {
  243. return to_book3s(vcpu)->sr[srnum].raw;
  244. }
  245. static void kvmppc_mmu_book3s_32_mtsrin(struct kvm_vcpu *vcpu, u32 srnum,
  246. ulong value)
  247. {
  248. struct kvmppc_sr *sre;
  249. sre = &to_book3s(vcpu)->sr[srnum];
  250. /* Flush any left-over shadows from the previous SR */
  251. /* XXX Not necessary? */
  252. /* kvmppc_mmu_pte_flush(vcpu, ((u64)sre->vsid) << 28, 0xf0000000ULL); */
  253. /* And then put in the new SR */
  254. sre->raw = value;
  255. sre->vsid = (value & 0x0fffffff);
  256. sre->valid = (value & 0x80000000) ? false : true;
  257. sre->Ks = (value & 0x40000000) ? true : false;
  258. sre->Kp = (value & 0x20000000) ? true : false;
  259. sre->nx = (value & 0x10000000) ? true : false;
  260. /* Map the new segment */
  261. kvmppc_mmu_map_segment(vcpu, srnum << SID_SHIFT);
  262. }
  263. static void kvmppc_mmu_book3s_32_tlbie(struct kvm_vcpu *vcpu, ulong ea, bool large)
  264. {
  265. kvmppc_mmu_pte_flush(vcpu, ea, 0x0FFFF000);
  266. }
  267. static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu *vcpu, u64 esid,
  268. u64 *vsid)
  269. {
  270. /* In case we only have one of MSR_IR or MSR_DR set, let's put
  271. that in the real-mode context (and hope RM doesn't access
  272. high memory) */
  273. switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
  274. case 0:
  275. *vsid = (VSID_REAL >> 16) | esid;
  276. break;
  277. case MSR_IR:
  278. *vsid = (VSID_REAL_IR >> 16) | esid;
  279. break;
  280. case MSR_DR:
  281. *vsid = (VSID_REAL_DR >> 16) | esid;
  282. break;
  283. case MSR_DR|MSR_IR:
  284. {
  285. ulong ea = esid << SID_SHIFT;
  286. struct kvmppc_sr *sr = find_sr(to_book3s(vcpu), ea);
  287. if (!sr->valid)
  288. return -1;
  289. *vsid = sr->vsid;
  290. break;
  291. }
  292. default:
  293. BUG();
  294. }
  295. if (vcpu->arch.msr & MSR_PR)
  296. *vsid |= VSID_PR;
  297. return 0;
  298. }
  299. static bool kvmppc_mmu_book3s_32_is_dcbz32(struct kvm_vcpu *vcpu)
  300. {
  301. return true;
  302. }
  303. void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu)
  304. {
  305. struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
  306. mmu->mtsrin = kvmppc_mmu_book3s_32_mtsrin;
  307. mmu->mfsrin = kvmppc_mmu_book3s_32_mfsrin;
  308. mmu->xlate = kvmppc_mmu_book3s_32_xlate;
  309. mmu->reset_msr = kvmppc_mmu_book3s_32_reset_msr;
  310. mmu->tlbie = kvmppc_mmu_book3s_32_tlbie;
  311. mmu->esid_to_vsid = kvmppc_mmu_book3s_32_esid_to_vsid;
  312. mmu->ea_to_vp = kvmppc_mmu_book3s_32_ea_to_vp;
  313. mmu->is_dcbz32 = kvmppc_mmu_book3s_32_is_dcbz32;
  314. mmu->slbmte = NULL;
  315. mmu->slbmfee = NULL;
  316. mmu->slbmfev = NULL;
  317. mmu->slbie = NULL;
  318. mmu->slbia = NULL;
  319. }