book3s_64_mmu.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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. #include <asm/mmu-hash64.h>
  28. /* #define DEBUG_MMU */
  29. #ifdef DEBUG_MMU
  30. #define dprintk(X...) printk(KERN_INFO X)
  31. #else
  32. #define dprintk(X...) do { } while(0)
  33. #endif
  34. static void kvmppc_mmu_book3s_64_reset_msr(struct kvm_vcpu *vcpu)
  35. {
  36. kvmppc_set_msr(vcpu, vcpu->arch.intr_msr);
  37. }
  38. static struct kvmppc_slb *kvmppc_mmu_book3s_64_find_slbe(
  39. struct kvm_vcpu *vcpu,
  40. gva_t eaddr)
  41. {
  42. int i;
  43. u64 esid = GET_ESID(eaddr);
  44. u64 esid_1t = GET_ESID_1T(eaddr);
  45. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  46. u64 cmp_esid = esid;
  47. if (!vcpu->arch.slb[i].valid)
  48. continue;
  49. if (vcpu->arch.slb[i].tb)
  50. cmp_esid = esid_1t;
  51. if (vcpu->arch.slb[i].esid == cmp_esid)
  52. return &vcpu->arch.slb[i];
  53. }
  54. dprintk("KVM: No SLB entry found for 0x%lx [%llx | %llx]\n",
  55. eaddr, esid, esid_1t);
  56. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  57. if (vcpu->arch.slb[i].vsid)
  58. dprintk(" %d: %c%c%c %llx %llx\n", i,
  59. vcpu->arch.slb[i].valid ? 'v' : ' ',
  60. vcpu->arch.slb[i].large ? 'l' : ' ',
  61. vcpu->arch.slb[i].tb ? 't' : ' ',
  62. vcpu->arch.slb[i].esid,
  63. vcpu->arch.slb[i].vsid);
  64. }
  65. return NULL;
  66. }
  67. static int kvmppc_slb_sid_shift(struct kvmppc_slb *slbe)
  68. {
  69. return slbe->tb ? SID_SHIFT_1T : SID_SHIFT;
  70. }
  71. static u64 kvmppc_slb_offset_mask(struct kvmppc_slb *slbe)
  72. {
  73. return (1ul << kvmppc_slb_sid_shift(slbe)) - 1;
  74. }
  75. static u64 kvmppc_slb_calc_vpn(struct kvmppc_slb *slb, gva_t eaddr)
  76. {
  77. eaddr &= kvmppc_slb_offset_mask(slb);
  78. return (eaddr >> VPN_SHIFT) |
  79. ((slb->vsid) << (kvmppc_slb_sid_shift(slb) - VPN_SHIFT));
  80. }
  81. static u64 kvmppc_mmu_book3s_64_ea_to_vp(struct kvm_vcpu *vcpu, gva_t eaddr,
  82. bool data)
  83. {
  84. struct kvmppc_slb *slb;
  85. slb = kvmppc_mmu_book3s_64_find_slbe(vcpu, eaddr);
  86. if (!slb)
  87. return 0;
  88. return kvmppc_slb_calc_vpn(slb, eaddr);
  89. }
  90. static int mmu_pagesize(int mmu_pg)
  91. {
  92. switch (mmu_pg) {
  93. case MMU_PAGE_64K:
  94. return 16;
  95. case MMU_PAGE_16M:
  96. return 24;
  97. }
  98. return 12;
  99. }
  100. static int kvmppc_mmu_book3s_64_get_pagesize(struct kvmppc_slb *slbe)
  101. {
  102. return mmu_pagesize(slbe->base_page_size);
  103. }
  104. static u32 kvmppc_mmu_book3s_64_get_page(struct kvmppc_slb *slbe, gva_t eaddr)
  105. {
  106. int p = kvmppc_mmu_book3s_64_get_pagesize(slbe);
  107. return ((eaddr & kvmppc_slb_offset_mask(slbe)) >> p);
  108. }
  109. static hva_t kvmppc_mmu_book3s_64_get_pteg(struct kvm_vcpu *vcpu,
  110. struct kvmppc_slb *slbe, gva_t eaddr,
  111. bool second)
  112. {
  113. struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
  114. u64 hash, pteg, htabsize;
  115. u32 ssize;
  116. hva_t r;
  117. u64 vpn;
  118. htabsize = ((1 << ((vcpu_book3s->sdr1 & 0x1f) + 11)) - 1);
  119. vpn = kvmppc_slb_calc_vpn(slbe, eaddr);
  120. ssize = slbe->tb ? MMU_SEGSIZE_1T : MMU_SEGSIZE_256M;
  121. hash = hpt_hash(vpn, kvmppc_mmu_book3s_64_get_pagesize(slbe), ssize);
  122. if (second)
  123. hash = ~hash;
  124. hash &= ((1ULL << 39ULL) - 1ULL);
  125. hash &= htabsize;
  126. hash <<= 7ULL;
  127. pteg = vcpu_book3s->sdr1 & 0xfffffffffffc0000ULL;
  128. pteg |= hash;
  129. dprintk("MMU: page=0x%x sdr1=0x%llx pteg=0x%llx vsid=0x%llx\n",
  130. page, vcpu_book3s->sdr1, pteg, slbe->vsid);
  131. /* When running a PAPR guest, SDR1 contains a HVA address instead
  132. of a GPA */
  133. if (vcpu->arch.papr_enabled)
  134. r = pteg;
  135. else
  136. r = gfn_to_hva(vcpu->kvm, pteg >> PAGE_SHIFT);
  137. if (kvm_is_error_hva(r))
  138. return r;
  139. return r | (pteg & ~PAGE_MASK);
  140. }
  141. static u64 kvmppc_mmu_book3s_64_get_avpn(struct kvmppc_slb *slbe, gva_t eaddr)
  142. {
  143. int p = kvmppc_mmu_book3s_64_get_pagesize(slbe);
  144. u64 avpn;
  145. avpn = kvmppc_mmu_book3s_64_get_page(slbe, eaddr);
  146. avpn |= slbe->vsid << (kvmppc_slb_sid_shift(slbe) - p);
  147. if (p < 16)
  148. avpn >>= ((80 - p) - 56) - 8; /* 16 - p */
  149. else
  150. avpn <<= p - 16;
  151. return avpn;
  152. }
  153. /*
  154. * Return page size encoded in the second word of a HPTE, or
  155. * -1 for an invalid encoding for the base page size indicated by
  156. * the SLB entry. This doesn't handle mixed pagesize segments yet.
  157. */
  158. static int decode_pagesize(struct kvmppc_slb *slbe, u64 r)
  159. {
  160. switch (slbe->base_page_size) {
  161. case MMU_PAGE_64K:
  162. if ((r & 0xf000) == 0x1000)
  163. return MMU_PAGE_64K;
  164. break;
  165. case MMU_PAGE_16M:
  166. if ((r & 0xff000) == 0)
  167. return MMU_PAGE_16M;
  168. break;
  169. }
  170. return -1;
  171. }
  172. static int kvmppc_mmu_book3s_64_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
  173. struct kvmppc_pte *gpte, bool data,
  174. bool iswrite)
  175. {
  176. struct kvmppc_slb *slbe;
  177. hva_t ptegp;
  178. u64 pteg[16];
  179. u64 avpn = 0;
  180. u64 v, r;
  181. u64 v_val, v_mask;
  182. u64 eaddr_mask;
  183. int i;
  184. u8 pp, key = 0;
  185. bool found = false;
  186. bool second = false;
  187. int pgsize;
  188. ulong mp_ea = vcpu->arch.magic_page_ea;
  189. /* Magic page override */
  190. if (unlikely(mp_ea) &&
  191. unlikely((eaddr & ~0xfffULL) == (mp_ea & ~0xfffULL)) &&
  192. !(kvmppc_get_msr(vcpu) & MSR_PR)) {
  193. gpte->eaddr = eaddr;
  194. gpte->vpage = kvmppc_mmu_book3s_64_ea_to_vp(vcpu, eaddr, data);
  195. gpte->raddr = vcpu->arch.magic_page_pa | (gpte->raddr & 0xfff);
  196. gpte->raddr &= KVM_PAM;
  197. gpte->may_execute = true;
  198. gpte->may_read = true;
  199. gpte->may_write = true;
  200. gpte->page_size = MMU_PAGE_4K;
  201. return 0;
  202. }
  203. slbe = kvmppc_mmu_book3s_64_find_slbe(vcpu, eaddr);
  204. if (!slbe)
  205. goto no_seg_found;
  206. avpn = kvmppc_mmu_book3s_64_get_avpn(slbe, eaddr);
  207. v_val = avpn & HPTE_V_AVPN;
  208. if (slbe->tb)
  209. v_val |= SLB_VSID_B_1T;
  210. if (slbe->large)
  211. v_val |= HPTE_V_LARGE;
  212. v_val |= HPTE_V_VALID;
  213. v_mask = SLB_VSID_B | HPTE_V_AVPN | HPTE_V_LARGE | HPTE_V_VALID |
  214. HPTE_V_SECONDARY;
  215. pgsize = slbe->large ? MMU_PAGE_16M : MMU_PAGE_4K;
  216. mutex_lock(&vcpu->kvm->arch.hpt_mutex);
  217. do_second:
  218. ptegp = kvmppc_mmu_book3s_64_get_pteg(vcpu, slbe, eaddr, second);
  219. if (kvm_is_error_hva(ptegp))
  220. goto no_page_found;
  221. if(copy_from_user(pteg, (void __user *)ptegp, sizeof(pteg))) {
  222. printk(KERN_ERR "KVM can't copy data from 0x%lx!\n", ptegp);
  223. goto no_page_found;
  224. }
  225. if ((kvmppc_get_msr(vcpu) & MSR_PR) && slbe->Kp)
  226. key = 4;
  227. else if (!(kvmppc_get_msr(vcpu) & MSR_PR) && slbe->Ks)
  228. key = 4;
  229. for (i=0; i<16; i+=2) {
  230. u64 pte0 = be64_to_cpu(pteg[i]);
  231. u64 pte1 = be64_to_cpu(pteg[i + 1]);
  232. /* Check all relevant fields of 1st dword */
  233. if ((pte0 & v_mask) == v_val) {
  234. /* If large page bit is set, check pgsize encoding */
  235. if (slbe->large &&
  236. (vcpu->arch.hflags & BOOK3S_HFLAG_MULTI_PGSIZE)) {
  237. pgsize = decode_pagesize(slbe, pte1);
  238. if (pgsize < 0)
  239. continue;
  240. }
  241. found = true;
  242. break;
  243. }
  244. }
  245. if (!found) {
  246. if (second)
  247. goto no_page_found;
  248. v_val |= HPTE_V_SECONDARY;
  249. second = true;
  250. goto do_second;
  251. }
  252. v = be64_to_cpu(pteg[i]);
  253. r = be64_to_cpu(pteg[i+1]);
  254. pp = (r & HPTE_R_PP) | key;
  255. if (r & HPTE_R_PP0)
  256. pp |= 8;
  257. gpte->eaddr = eaddr;
  258. gpte->vpage = kvmppc_mmu_book3s_64_ea_to_vp(vcpu, eaddr, data);
  259. eaddr_mask = (1ull << mmu_pagesize(pgsize)) - 1;
  260. gpte->raddr = (r & HPTE_R_RPN & ~eaddr_mask) | (eaddr & eaddr_mask);
  261. gpte->page_size = pgsize;
  262. gpte->may_execute = ((r & HPTE_R_N) ? false : true);
  263. if (unlikely(vcpu->arch.disable_kernel_nx) &&
  264. !(kvmppc_get_msr(vcpu) & MSR_PR))
  265. gpte->may_execute = true;
  266. gpte->may_read = false;
  267. gpte->may_write = false;
  268. switch (pp) {
  269. case 0:
  270. case 1:
  271. case 2:
  272. case 6:
  273. gpte->may_write = true;
  274. /* fall through */
  275. case 3:
  276. case 5:
  277. case 7:
  278. case 10:
  279. gpte->may_read = true;
  280. break;
  281. }
  282. dprintk("KVM MMU: Translated 0x%lx [0x%llx] -> 0x%llx "
  283. "-> 0x%lx\n",
  284. eaddr, avpn, gpte->vpage, gpte->raddr);
  285. /* Update PTE R and C bits, so the guest's swapper knows we used the
  286. * page */
  287. if (gpte->may_read && !(r & HPTE_R_R)) {
  288. /*
  289. * Set the accessed flag.
  290. * We have to write this back with a single byte write
  291. * because another vcpu may be accessing this on
  292. * non-PAPR platforms such as mac99, and this is
  293. * what real hardware does.
  294. */
  295. char __user *addr = (char __user *) (ptegp + (i + 1) * sizeof(u64));
  296. r |= HPTE_R_R;
  297. put_user(r >> 8, addr + 6);
  298. }
  299. if (iswrite && gpte->may_write && !(r & HPTE_R_C)) {
  300. /* Set the dirty flag */
  301. /* Use a single byte write */
  302. char __user *addr = (char __user *) (ptegp + (i + 1) * sizeof(u64));
  303. r |= HPTE_R_C;
  304. put_user(r, addr + 7);
  305. }
  306. mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
  307. if (!gpte->may_read || (iswrite && !gpte->may_write))
  308. return -EPERM;
  309. return 0;
  310. no_page_found:
  311. mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
  312. return -ENOENT;
  313. no_seg_found:
  314. dprintk("KVM MMU: Trigger segment fault\n");
  315. return -EINVAL;
  316. }
  317. static void kvmppc_mmu_book3s_64_slbmte(struct kvm_vcpu *vcpu, u64 rs, u64 rb)
  318. {
  319. struct kvmppc_vcpu_book3s *vcpu_book3s;
  320. u64 esid, esid_1t;
  321. int slb_nr;
  322. struct kvmppc_slb *slbe;
  323. dprintk("KVM MMU: slbmte(0x%llx, 0x%llx)\n", rs, rb);
  324. vcpu_book3s = to_book3s(vcpu);
  325. esid = GET_ESID(rb);
  326. esid_1t = GET_ESID_1T(rb);
  327. slb_nr = rb & 0xfff;
  328. if (slb_nr > vcpu->arch.slb_nr)
  329. return;
  330. slbe = &vcpu->arch.slb[slb_nr];
  331. slbe->large = (rs & SLB_VSID_L) ? 1 : 0;
  332. slbe->tb = (rs & SLB_VSID_B_1T) ? 1 : 0;
  333. slbe->esid = slbe->tb ? esid_1t : esid;
  334. slbe->vsid = (rs & ~SLB_VSID_B) >> (kvmppc_slb_sid_shift(slbe) - 16);
  335. slbe->valid = (rb & SLB_ESID_V) ? 1 : 0;
  336. slbe->Ks = (rs & SLB_VSID_KS) ? 1 : 0;
  337. slbe->Kp = (rs & SLB_VSID_KP) ? 1 : 0;
  338. slbe->nx = (rs & SLB_VSID_N) ? 1 : 0;
  339. slbe->class = (rs & SLB_VSID_C) ? 1 : 0;
  340. slbe->base_page_size = MMU_PAGE_4K;
  341. if (slbe->large) {
  342. if (vcpu->arch.hflags & BOOK3S_HFLAG_MULTI_PGSIZE) {
  343. switch (rs & SLB_VSID_LP) {
  344. case SLB_VSID_LP_00:
  345. slbe->base_page_size = MMU_PAGE_16M;
  346. break;
  347. case SLB_VSID_LP_01:
  348. slbe->base_page_size = MMU_PAGE_64K;
  349. break;
  350. }
  351. } else
  352. slbe->base_page_size = MMU_PAGE_16M;
  353. }
  354. slbe->orige = rb & (ESID_MASK | SLB_ESID_V);
  355. slbe->origv = rs;
  356. /* Map the new segment */
  357. kvmppc_mmu_map_segment(vcpu, esid << SID_SHIFT);
  358. }
  359. static u64 kvmppc_mmu_book3s_64_slbmfee(struct kvm_vcpu *vcpu, u64 slb_nr)
  360. {
  361. struct kvmppc_slb *slbe;
  362. if (slb_nr > vcpu->arch.slb_nr)
  363. return 0;
  364. slbe = &vcpu->arch.slb[slb_nr];
  365. return slbe->orige;
  366. }
  367. static u64 kvmppc_mmu_book3s_64_slbmfev(struct kvm_vcpu *vcpu, u64 slb_nr)
  368. {
  369. struct kvmppc_slb *slbe;
  370. if (slb_nr > vcpu->arch.slb_nr)
  371. return 0;
  372. slbe = &vcpu->arch.slb[slb_nr];
  373. return slbe->origv;
  374. }
  375. static void kvmppc_mmu_book3s_64_slbie(struct kvm_vcpu *vcpu, u64 ea)
  376. {
  377. struct kvmppc_slb *slbe;
  378. u64 seg_size;
  379. dprintk("KVM MMU: slbie(0x%llx)\n", ea);
  380. slbe = kvmppc_mmu_book3s_64_find_slbe(vcpu, ea);
  381. if (!slbe)
  382. return;
  383. dprintk("KVM MMU: slbie(0x%llx, 0x%llx)\n", ea, slbe->esid);
  384. slbe->valid = false;
  385. slbe->orige = 0;
  386. slbe->origv = 0;
  387. seg_size = 1ull << kvmppc_slb_sid_shift(slbe);
  388. kvmppc_mmu_flush_segment(vcpu, ea & ~(seg_size - 1), seg_size);
  389. }
  390. static void kvmppc_mmu_book3s_64_slbia(struct kvm_vcpu *vcpu)
  391. {
  392. int i;
  393. dprintk("KVM MMU: slbia()\n");
  394. for (i = 1; i < vcpu->arch.slb_nr; i++) {
  395. vcpu->arch.slb[i].valid = false;
  396. vcpu->arch.slb[i].orige = 0;
  397. vcpu->arch.slb[i].origv = 0;
  398. }
  399. if (kvmppc_get_msr(vcpu) & MSR_IR) {
  400. kvmppc_mmu_flush_segments(vcpu);
  401. kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
  402. }
  403. }
  404. static void kvmppc_mmu_book3s_64_mtsrin(struct kvm_vcpu *vcpu, u32 srnum,
  405. ulong value)
  406. {
  407. u64 rb = 0, rs = 0;
  408. /*
  409. * According to Book3 2.01 mtsrin is implemented as:
  410. *
  411. * The SLB entry specified by (RB)32:35 is loaded from register
  412. * RS, as follows.
  413. *
  414. * SLBE Bit Source SLB Field
  415. *
  416. * 0:31 0x0000_0000 ESID-0:31
  417. * 32:35 (RB)32:35 ESID-32:35
  418. * 36 0b1 V
  419. * 37:61 0x00_0000|| 0b0 VSID-0:24
  420. * 62:88 (RS)37:63 VSID-25:51
  421. * 89:91 (RS)33:35 Ks Kp N
  422. * 92 (RS)36 L ((RS)36 must be 0b0)
  423. * 93 0b0 C
  424. */
  425. dprintk("KVM MMU: mtsrin(0x%x, 0x%lx)\n", srnum, value);
  426. /* ESID = srnum */
  427. rb |= (srnum & 0xf) << 28;
  428. /* Set the valid bit */
  429. rb |= 1 << 27;
  430. /* Index = ESID */
  431. rb |= srnum;
  432. /* VSID = VSID */
  433. rs |= (value & 0xfffffff) << 12;
  434. /* flags = flags */
  435. rs |= ((value >> 28) & 0x7) << 9;
  436. kvmppc_mmu_book3s_64_slbmte(vcpu, rs, rb);
  437. }
  438. static void kvmppc_mmu_book3s_64_tlbie(struct kvm_vcpu *vcpu, ulong va,
  439. bool large)
  440. {
  441. u64 mask = 0xFFFFFFFFFULL;
  442. long i;
  443. struct kvm_vcpu *v;
  444. dprintk("KVM MMU: tlbie(0x%lx)\n", va);
  445. /*
  446. * The tlbie instruction changed behaviour starting with
  447. * POWER6. POWER6 and later don't have the large page flag
  448. * in the instruction but in the RB value, along with bits
  449. * indicating page and segment sizes.
  450. */
  451. if (vcpu->arch.hflags & BOOK3S_HFLAG_NEW_TLBIE) {
  452. /* POWER6 or later */
  453. if (va & 1) { /* L bit */
  454. if ((va & 0xf000) == 0x1000)
  455. mask = 0xFFFFFFFF0ULL; /* 64k page */
  456. else
  457. mask = 0xFFFFFF000ULL; /* 16M page */
  458. }
  459. } else {
  460. /* older processors, e.g. PPC970 */
  461. if (large)
  462. mask = 0xFFFFFF000ULL;
  463. }
  464. /* flush this VA on all vcpus */
  465. kvm_for_each_vcpu(i, v, vcpu->kvm)
  466. kvmppc_mmu_pte_vflush(v, va >> 12, mask);
  467. }
  468. #ifdef CONFIG_PPC_64K_PAGES
  469. static int segment_contains_magic_page(struct kvm_vcpu *vcpu, ulong esid)
  470. {
  471. ulong mp_ea = vcpu->arch.magic_page_ea;
  472. return mp_ea && !(kvmppc_get_msr(vcpu) & MSR_PR) &&
  473. (mp_ea >> SID_SHIFT) == esid;
  474. }
  475. #endif
  476. static int kvmppc_mmu_book3s_64_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,
  477. u64 *vsid)
  478. {
  479. ulong ea = esid << SID_SHIFT;
  480. struct kvmppc_slb *slb;
  481. u64 gvsid = esid;
  482. ulong mp_ea = vcpu->arch.magic_page_ea;
  483. int pagesize = MMU_PAGE_64K;
  484. u64 msr = kvmppc_get_msr(vcpu);
  485. if (msr & (MSR_DR|MSR_IR)) {
  486. slb = kvmppc_mmu_book3s_64_find_slbe(vcpu, ea);
  487. if (slb) {
  488. gvsid = slb->vsid;
  489. pagesize = slb->base_page_size;
  490. if (slb->tb) {
  491. gvsid <<= SID_SHIFT_1T - SID_SHIFT;
  492. gvsid |= esid & ((1ul << (SID_SHIFT_1T - SID_SHIFT)) - 1);
  493. gvsid |= VSID_1T;
  494. }
  495. }
  496. }
  497. switch (msr & (MSR_DR|MSR_IR)) {
  498. case 0:
  499. gvsid = VSID_REAL | esid;
  500. break;
  501. case MSR_IR:
  502. gvsid |= VSID_REAL_IR;
  503. break;
  504. case MSR_DR:
  505. gvsid |= VSID_REAL_DR;
  506. break;
  507. case MSR_DR|MSR_IR:
  508. if (!slb)
  509. goto no_slb;
  510. break;
  511. default:
  512. BUG();
  513. break;
  514. }
  515. #ifdef CONFIG_PPC_64K_PAGES
  516. /*
  517. * Mark this as a 64k segment if the host is using
  518. * 64k pages, the host MMU supports 64k pages and
  519. * the guest segment page size is >= 64k,
  520. * but not if this segment contains the magic page.
  521. */
  522. if (pagesize >= MMU_PAGE_64K &&
  523. mmu_psize_defs[MMU_PAGE_64K].shift &&
  524. !segment_contains_magic_page(vcpu, esid))
  525. gvsid |= VSID_64K;
  526. #endif
  527. if (kvmppc_get_msr(vcpu) & MSR_PR)
  528. gvsid |= VSID_PR;
  529. *vsid = gvsid;
  530. return 0;
  531. no_slb:
  532. /* Catch magic page case */
  533. if (unlikely(mp_ea) &&
  534. unlikely(esid == (mp_ea >> SID_SHIFT)) &&
  535. !(kvmppc_get_msr(vcpu) & MSR_PR)) {
  536. *vsid = VSID_REAL | esid;
  537. return 0;
  538. }
  539. return -EINVAL;
  540. }
  541. static bool kvmppc_mmu_book3s_64_is_dcbz32(struct kvm_vcpu *vcpu)
  542. {
  543. return (to_book3s(vcpu)->hid[5] & 0x80);
  544. }
  545. void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu)
  546. {
  547. struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
  548. mmu->mfsrin = NULL;
  549. mmu->mtsrin = kvmppc_mmu_book3s_64_mtsrin;
  550. mmu->slbmte = kvmppc_mmu_book3s_64_slbmte;
  551. mmu->slbmfee = kvmppc_mmu_book3s_64_slbmfee;
  552. mmu->slbmfev = kvmppc_mmu_book3s_64_slbmfev;
  553. mmu->slbie = kvmppc_mmu_book3s_64_slbie;
  554. mmu->slbia = kvmppc_mmu_book3s_64_slbia;
  555. mmu->xlate = kvmppc_mmu_book3s_64_xlate;
  556. mmu->reset_msr = kvmppc_mmu_book3s_64_reset_msr;
  557. mmu->tlbie = kvmppc_mmu_book3s_64_tlbie;
  558. mmu->esid_to_vsid = kvmppc_mmu_book3s_64_esid_to_vsid;
  559. mmu->ea_to_vp = kvmppc_mmu_book3s_64_ea_to_vp;
  560. mmu->is_dcbz32 = kvmppc_mmu_book3s_64_is_dcbz32;
  561. vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
  562. }