book3s_32_mmu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. #ifndef SID_SHIFT
  43. #define SID_SHIFT 28
  44. #endif
  45. static inline bool check_debug_ip(struct kvm_vcpu *vcpu)
  46. {
  47. #ifdef DEBUG_MMU_PTE_IP
  48. return vcpu->arch.pc == DEBUG_MMU_PTE_IP;
  49. #else
  50. return true;
  51. #endif
  52. }
  53. static inline u32 sr_vsid(u32 sr_raw)
  54. {
  55. return sr_raw & 0x0fffffff;
  56. }
  57. static inline bool sr_valid(u32 sr_raw)
  58. {
  59. return (sr_raw & 0x80000000) ? false : true;
  60. }
  61. static inline bool sr_ks(u32 sr_raw)
  62. {
  63. return (sr_raw & 0x40000000) ? true: false;
  64. }
  65. static inline bool sr_kp(u32 sr_raw)
  66. {
  67. return (sr_raw & 0x20000000) ? true: false;
  68. }
  69. static int kvmppc_mmu_book3s_32_xlate_bat(struct kvm_vcpu *vcpu, gva_t eaddr,
  70. struct kvmppc_pte *pte, bool data,
  71. bool iswrite);
  72. static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,
  73. u64 *vsid);
  74. static u32 find_sr(struct kvm_vcpu *vcpu, gva_t eaddr)
  75. {
  76. return kvmppc_get_sr(vcpu, (eaddr >> 28) & 0xf);
  77. }
  78. static u64 kvmppc_mmu_book3s_32_ea_to_vp(struct kvm_vcpu *vcpu, gva_t eaddr,
  79. bool data)
  80. {
  81. u64 vsid;
  82. struct kvmppc_pte pte;
  83. if (!kvmppc_mmu_book3s_32_xlate_bat(vcpu, eaddr, &pte, data, false))
  84. return pte.vpage;
  85. kvmppc_mmu_book3s_32_esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
  86. return (((u64)eaddr >> 12) & 0xffff) | (vsid << 16);
  87. }
  88. static void kvmppc_mmu_book3s_32_reset_msr(struct kvm_vcpu *vcpu)
  89. {
  90. kvmppc_set_msr(vcpu, 0);
  91. }
  92. static hva_t kvmppc_mmu_book3s_32_get_pteg(struct kvm_vcpu *vcpu,
  93. u32 sre, gva_t eaddr,
  94. bool primary)
  95. {
  96. struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
  97. u32 page, hash, pteg, htabmask;
  98. hva_t r;
  99. page = (eaddr & 0x0FFFFFFF) >> 12;
  100. htabmask = ((vcpu_book3s->sdr1 & 0x1FF) << 16) | 0xFFC0;
  101. hash = ((sr_vsid(sre) ^ page) << 6);
  102. if (!primary)
  103. hash = ~hash;
  104. hash &= htabmask;
  105. pteg = (vcpu_book3s->sdr1 & 0xffff0000) | hash;
  106. dprintk("MMU: pc=0x%lx eaddr=0x%lx sdr1=0x%llx pteg=0x%x vsid=0x%x\n",
  107. kvmppc_get_pc(vcpu), eaddr, vcpu_book3s->sdr1, pteg,
  108. sr_vsid(sre));
  109. r = gfn_to_hva(vcpu->kvm, pteg >> PAGE_SHIFT);
  110. if (kvm_is_error_hva(r))
  111. return r;
  112. return r | (pteg & ~PAGE_MASK);
  113. }
  114. static u32 kvmppc_mmu_book3s_32_get_ptem(u32 sre, gva_t eaddr, bool primary)
  115. {
  116. return ((eaddr & 0x0fffffff) >> 22) | (sr_vsid(sre) << 7) |
  117. (primary ? 0 : 0x40) | 0x80000000;
  118. }
  119. static int kvmppc_mmu_book3s_32_xlate_bat(struct kvm_vcpu *vcpu, gva_t eaddr,
  120. struct kvmppc_pte *pte, bool data,
  121. bool iswrite)
  122. {
  123. struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
  124. struct kvmppc_bat *bat;
  125. int i;
  126. for (i = 0; i < 8; i++) {
  127. if (data)
  128. bat = &vcpu_book3s->dbat[i];
  129. else
  130. bat = &vcpu_book3s->ibat[i];
  131. if (kvmppc_get_msr(vcpu) & MSR_PR) {
  132. if (!bat->vp)
  133. continue;
  134. } else {
  135. if (!bat->vs)
  136. continue;
  137. }
  138. if (check_debug_ip(vcpu))
  139. {
  140. dprintk_pte("%cBAT %02d: 0x%lx - 0x%x (0x%x)\n",
  141. data ? 'd' : 'i', i, eaddr, bat->bepi,
  142. bat->bepi_mask);
  143. }
  144. if ((eaddr & bat->bepi_mask) == bat->bepi) {
  145. u64 vsid;
  146. kvmppc_mmu_book3s_32_esid_to_vsid(vcpu,
  147. eaddr >> SID_SHIFT, &vsid);
  148. vsid <<= 16;
  149. pte->vpage = (((u64)eaddr >> 12) & 0xffff) | vsid;
  150. pte->raddr = bat->brpn | (eaddr & ~bat->bepi_mask);
  151. pte->may_read = bat->pp;
  152. pte->may_write = bat->pp > 1;
  153. pte->may_execute = true;
  154. if (!pte->may_read) {
  155. printk(KERN_INFO "BAT is not readable!\n");
  156. continue;
  157. }
  158. if (iswrite && !pte->may_write) {
  159. dprintk_pte("BAT is read-only!\n");
  160. continue;
  161. }
  162. return 0;
  163. }
  164. }
  165. return -ENOENT;
  166. }
  167. static int kvmppc_mmu_book3s_32_xlate_pte(struct kvm_vcpu *vcpu, gva_t eaddr,
  168. struct kvmppc_pte *pte, bool data,
  169. bool iswrite, bool primary)
  170. {
  171. u32 sre;
  172. hva_t ptegp;
  173. u32 pteg[16];
  174. u32 pte0, pte1;
  175. u32 ptem = 0;
  176. int i;
  177. int found = 0;
  178. sre = find_sr(vcpu, eaddr);
  179. dprintk_pte("SR 0x%lx: vsid=0x%x, raw=0x%x\n", eaddr >> 28,
  180. sr_vsid(sre), sre);
  181. pte->vpage = kvmppc_mmu_book3s_32_ea_to_vp(vcpu, eaddr, data);
  182. ptegp = kvmppc_mmu_book3s_32_get_pteg(vcpu, sre, eaddr, primary);
  183. if (kvm_is_error_hva(ptegp)) {
  184. printk(KERN_INFO "KVM: Invalid PTEG!\n");
  185. goto no_page_found;
  186. }
  187. ptem = kvmppc_mmu_book3s_32_get_ptem(sre, eaddr, primary);
  188. if(copy_from_user(pteg, (void __user *)ptegp, sizeof(pteg))) {
  189. printk(KERN_ERR "KVM: Can't copy data from 0x%lx!\n", ptegp);
  190. goto no_page_found;
  191. }
  192. for (i=0; i<16; i+=2) {
  193. pte0 = be32_to_cpu(pteg[i]);
  194. pte1 = be32_to_cpu(pteg[i + 1]);
  195. if (ptem == pte0) {
  196. u8 pp;
  197. pte->raddr = (pte1 & ~(0xFFFULL)) | (eaddr & 0xFFF);
  198. pp = pte1 & 3;
  199. if ((sr_kp(sre) && (kvmppc_get_msr(vcpu) & MSR_PR)) ||
  200. (sr_ks(sre) && !(kvmppc_get_msr(vcpu) & MSR_PR)))
  201. pp |= 4;
  202. pte->may_write = false;
  203. pte->may_read = false;
  204. pte->may_execute = true;
  205. switch (pp) {
  206. case 0:
  207. case 1:
  208. case 2:
  209. case 6:
  210. pte->may_write = true;
  211. case 3:
  212. case 5:
  213. case 7:
  214. pte->may_read = true;
  215. break;
  216. }
  217. dprintk_pte("MMU: Found PTE -> %x %x - %x\n",
  218. pte0, pte1, pp);
  219. found = 1;
  220. break;
  221. }
  222. }
  223. /* Update PTE C and A bits, so the guest's swapper knows we used the
  224. page */
  225. if (found) {
  226. u32 pte_r = pte1;
  227. char __user *addr = (char __user *) (ptegp + (i+1) * sizeof(u32));
  228. /*
  229. * Use single-byte writes to update the HPTE, to
  230. * conform to what real hardware does.
  231. */
  232. if (pte->may_read && !(pte_r & PTEG_FLAG_ACCESSED)) {
  233. pte_r |= PTEG_FLAG_ACCESSED;
  234. put_user(pte_r >> 8, addr + 2);
  235. }
  236. if (iswrite && pte->may_write && !(pte_r & PTEG_FLAG_DIRTY)) {
  237. pte_r |= PTEG_FLAG_DIRTY;
  238. put_user(pte_r, addr + 3);
  239. }
  240. if (!pte->may_read || (iswrite && !pte->may_write))
  241. return -EPERM;
  242. return 0;
  243. }
  244. no_page_found:
  245. if (check_debug_ip(vcpu)) {
  246. dprintk_pte("KVM MMU: No PTE found (sdr1=0x%llx ptegp=0x%lx)\n",
  247. to_book3s(vcpu)->sdr1, ptegp);
  248. for (i=0; i<16; i+=2) {
  249. dprintk_pte(" %02d: 0x%x - 0x%x (0x%x)\n",
  250. i, be32_to_cpu(pteg[i]),
  251. be32_to_cpu(pteg[i+1]), ptem);
  252. }
  253. }
  254. return -ENOENT;
  255. }
  256. static int kvmppc_mmu_book3s_32_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
  257. struct kvmppc_pte *pte, bool data,
  258. bool iswrite)
  259. {
  260. int r;
  261. ulong mp_ea = vcpu->arch.magic_page_ea;
  262. pte->eaddr = eaddr;
  263. pte->page_size = MMU_PAGE_4K;
  264. /* Magic page override */
  265. if (unlikely(mp_ea) &&
  266. unlikely((eaddr & ~0xfffULL) == (mp_ea & ~0xfffULL)) &&
  267. !(kvmppc_get_msr(vcpu) & MSR_PR)) {
  268. pte->vpage = kvmppc_mmu_book3s_32_ea_to_vp(vcpu, eaddr, data);
  269. pte->raddr = vcpu->arch.magic_page_pa | (pte->raddr & 0xfff);
  270. pte->raddr &= KVM_PAM;
  271. pte->may_execute = true;
  272. pte->may_read = true;
  273. pte->may_write = true;
  274. return 0;
  275. }
  276. r = kvmppc_mmu_book3s_32_xlate_bat(vcpu, eaddr, pte, data, iswrite);
  277. if (r < 0)
  278. r = kvmppc_mmu_book3s_32_xlate_pte(vcpu, eaddr, pte,
  279. data, iswrite, true);
  280. if (r == -ENOENT)
  281. r = kvmppc_mmu_book3s_32_xlate_pte(vcpu, eaddr, pte,
  282. data, iswrite, false);
  283. return r;
  284. }
  285. static u32 kvmppc_mmu_book3s_32_mfsrin(struct kvm_vcpu *vcpu, u32 srnum)
  286. {
  287. return kvmppc_get_sr(vcpu, srnum);
  288. }
  289. static void kvmppc_mmu_book3s_32_mtsrin(struct kvm_vcpu *vcpu, u32 srnum,
  290. ulong value)
  291. {
  292. kvmppc_set_sr(vcpu, srnum, value);
  293. kvmppc_mmu_map_segment(vcpu, srnum << SID_SHIFT);
  294. }
  295. static void kvmppc_mmu_book3s_32_tlbie(struct kvm_vcpu *vcpu, ulong ea, bool large)
  296. {
  297. int i;
  298. struct kvm_vcpu *v;
  299. /* flush this VA on all cpus */
  300. kvm_for_each_vcpu(i, v, vcpu->kvm)
  301. kvmppc_mmu_pte_flush(v, ea, 0x0FFFF000);
  302. }
  303. static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,
  304. u64 *vsid)
  305. {
  306. ulong ea = esid << SID_SHIFT;
  307. u32 sr;
  308. u64 gvsid = esid;
  309. u64 msr = kvmppc_get_msr(vcpu);
  310. if (msr & (MSR_DR|MSR_IR)) {
  311. sr = find_sr(vcpu, ea);
  312. if (sr_valid(sr))
  313. gvsid = sr_vsid(sr);
  314. }
  315. /* In case we only have one of MSR_IR or MSR_DR set, let's put
  316. that in the real-mode context (and hope RM doesn't access
  317. high memory) */
  318. switch (msr & (MSR_DR|MSR_IR)) {
  319. case 0:
  320. *vsid = VSID_REAL | esid;
  321. break;
  322. case MSR_IR:
  323. *vsid = VSID_REAL_IR | gvsid;
  324. break;
  325. case MSR_DR:
  326. *vsid = VSID_REAL_DR | gvsid;
  327. break;
  328. case MSR_DR|MSR_IR:
  329. if (sr_valid(sr))
  330. *vsid = sr_vsid(sr);
  331. else
  332. *vsid = VSID_BAT | gvsid;
  333. break;
  334. default:
  335. BUG();
  336. }
  337. if (msr & MSR_PR)
  338. *vsid |= VSID_PR;
  339. return 0;
  340. }
  341. static bool kvmppc_mmu_book3s_32_is_dcbz32(struct kvm_vcpu *vcpu)
  342. {
  343. return true;
  344. }
  345. void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu)
  346. {
  347. struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
  348. mmu->mtsrin = kvmppc_mmu_book3s_32_mtsrin;
  349. mmu->mfsrin = kvmppc_mmu_book3s_32_mfsrin;
  350. mmu->xlate = kvmppc_mmu_book3s_32_xlate;
  351. mmu->reset_msr = kvmppc_mmu_book3s_32_reset_msr;
  352. mmu->tlbie = kvmppc_mmu_book3s_32_tlbie;
  353. mmu->esid_to_vsid = kvmppc_mmu_book3s_32_esid_to_vsid;
  354. mmu->ea_to_vp = kvmppc_mmu_book3s_32_ea_to_vp;
  355. mmu->is_dcbz32 = kvmppc_mmu_book3s_32_is_dcbz32;
  356. mmu->slbmte = NULL;
  357. mmu->slbmfee = NULL;
  358. mmu->slbmfev = NULL;
  359. mmu->slbie = NULL;
  360. mmu->slbia = NULL;
  361. }