book3s_64_mmu.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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/book3s/64/mmu-hash.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. u64 esid, esid_1t;
  320. int slb_nr;
  321. struct kvmppc_slb *slbe;
  322. dprintk("KVM MMU: slbmte(0x%llx, 0x%llx)\n", rs, rb);
  323. esid = GET_ESID(rb);
  324. esid_1t = GET_ESID_1T(rb);
  325. slb_nr = rb & 0xfff;
  326. if (slb_nr > vcpu->arch.slb_nr)
  327. return;
  328. slbe = &vcpu->arch.slb[slb_nr];
  329. slbe->large = (rs & SLB_VSID_L) ? 1 : 0;
  330. slbe->tb = (rs & SLB_VSID_B_1T) ? 1 : 0;
  331. slbe->esid = slbe->tb ? esid_1t : esid;
  332. slbe->vsid = (rs & ~SLB_VSID_B) >> (kvmppc_slb_sid_shift(slbe) - 16);
  333. slbe->valid = (rb & SLB_ESID_V) ? 1 : 0;
  334. slbe->Ks = (rs & SLB_VSID_KS) ? 1 : 0;
  335. slbe->Kp = (rs & SLB_VSID_KP) ? 1 : 0;
  336. slbe->nx = (rs & SLB_VSID_N) ? 1 : 0;
  337. slbe->class = (rs & SLB_VSID_C) ? 1 : 0;
  338. slbe->base_page_size = MMU_PAGE_4K;
  339. if (slbe->large) {
  340. if (vcpu->arch.hflags & BOOK3S_HFLAG_MULTI_PGSIZE) {
  341. switch (rs & SLB_VSID_LP) {
  342. case SLB_VSID_LP_00:
  343. slbe->base_page_size = MMU_PAGE_16M;
  344. break;
  345. case SLB_VSID_LP_01:
  346. slbe->base_page_size = MMU_PAGE_64K;
  347. break;
  348. }
  349. } else
  350. slbe->base_page_size = MMU_PAGE_16M;
  351. }
  352. slbe->orige = rb & (ESID_MASK | SLB_ESID_V);
  353. slbe->origv = rs;
  354. /* Map the new segment */
  355. kvmppc_mmu_map_segment(vcpu, esid << SID_SHIFT);
  356. }
  357. static u64 kvmppc_mmu_book3s_64_slbmfee(struct kvm_vcpu *vcpu, u64 slb_nr)
  358. {
  359. struct kvmppc_slb *slbe;
  360. if (slb_nr > vcpu->arch.slb_nr)
  361. return 0;
  362. slbe = &vcpu->arch.slb[slb_nr];
  363. return slbe->orige;
  364. }
  365. static u64 kvmppc_mmu_book3s_64_slbmfev(struct kvm_vcpu *vcpu, u64 slb_nr)
  366. {
  367. struct kvmppc_slb *slbe;
  368. if (slb_nr > vcpu->arch.slb_nr)
  369. return 0;
  370. slbe = &vcpu->arch.slb[slb_nr];
  371. return slbe->origv;
  372. }
  373. static void kvmppc_mmu_book3s_64_slbie(struct kvm_vcpu *vcpu, u64 ea)
  374. {
  375. struct kvmppc_slb *slbe;
  376. u64 seg_size;
  377. dprintk("KVM MMU: slbie(0x%llx)\n", ea);
  378. slbe = kvmppc_mmu_book3s_64_find_slbe(vcpu, ea);
  379. if (!slbe)
  380. return;
  381. dprintk("KVM MMU: slbie(0x%llx, 0x%llx)\n", ea, slbe->esid);
  382. slbe->valid = false;
  383. slbe->orige = 0;
  384. slbe->origv = 0;
  385. seg_size = 1ull << kvmppc_slb_sid_shift(slbe);
  386. kvmppc_mmu_flush_segment(vcpu, ea & ~(seg_size - 1), seg_size);
  387. }
  388. static void kvmppc_mmu_book3s_64_slbia(struct kvm_vcpu *vcpu)
  389. {
  390. int i;
  391. dprintk("KVM MMU: slbia()\n");
  392. for (i = 1; i < vcpu->arch.slb_nr; i++) {
  393. vcpu->arch.slb[i].valid = false;
  394. vcpu->arch.slb[i].orige = 0;
  395. vcpu->arch.slb[i].origv = 0;
  396. }
  397. if (kvmppc_get_msr(vcpu) & MSR_IR) {
  398. kvmppc_mmu_flush_segments(vcpu);
  399. kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
  400. }
  401. }
  402. static void kvmppc_mmu_book3s_64_mtsrin(struct kvm_vcpu *vcpu, u32 srnum,
  403. ulong value)
  404. {
  405. u64 rb = 0, rs = 0;
  406. /*
  407. * According to Book3 2.01 mtsrin is implemented as:
  408. *
  409. * The SLB entry specified by (RB)32:35 is loaded from register
  410. * RS, as follows.
  411. *
  412. * SLBE Bit Source SLB Field
  413. *
  414. * 0:31 0x0000_0000 ESID-0:31
  415. * 32:35 (RB)32:35 ESID-32:35
  416. * 36 0b1 V
  417. * 37:61 0x00_0000|| 0b0 VSID-0:24
  418. * 62:88 (RS)37:63 VSID-25:51
  419. * 89:91 (RS)33:35 Ks Kp N
  420. * 92 (RS)36 L ((RS)36 must be 0b0)
  421. * 93 0b0 C
  422. */
  423. dprintk("KVM MMU: mtsrin(0x%x, 0x%lx)\n", srnum, value);
  424. /* ESID = srnum */
  425. rb |= (srnum & 0xf) << 28;
  426. /* Set the valid bit */
  427. rb |= 1 << 27;
  428. /* Index = ESID */
  429. rb |= srnum;
  430. /* VSID = VSID */
  431. rs |= (value & 0xfffffff) << 12;
  432. /* flags = flags */
  433. rs |= ((value >> 28) & 0x7) << 9;
  434. kvmppc_mmu_book3s_64_slbmte(vcpu, rs, rb);
  435. }
  436. static void kvmppc_mmu_book3s_64_tlbie(struct kvm_vcpu *vcpu, ulong va,
  437. bool large)
  438. {
  439. u64 mask = 0xFFFFFFFFFULL;
  440. long i;
  441. struct kvm_vcpu *v;
  442. dprintk("KVM MMU: tlbie(0x%lx)\n", va);
  443. /*
  444. * The tlbie instruction changed behaviour starting with
  445. * POWER6. POWER6 and later don't have the large page flag
  446. * in the instruction but in the RB value, along with bits
  447. * indicating page and segment sizes.
  448. */
  449. if (vcpu->arch.hflags & BOOK3S_HFLAG_NEW_TLBIE) {
  450. /* POWER6 or later */
  451. if (va & 1) { /* L bit */
  452. if ((va & 0xf000) == 0x1000)
  453. mask = 0xFFFFFFFF0ULL; /* 64k page */
  454. else
  455. mask = 0xFFFFFF000ULL; /* 16M page */
  456. }
  457. } else {
  458. /* older processors, e.g. PPC970 */
  459. if (large)
  460. mask = 0xFFFFFF000ULL;
  461. }
  462. /* flush this VA on all vcpus */
  463. kvm_for_each_vcpu(i, v, vcpu->kvm)
  464. kvmppc_mmu_pte_vflush(v, va >> 12, mask);
  465. }
  466. #ifdef CONFIG_PPC_64K_PAGES
  467. static int segment_contains_magic_page(struct kvm_vcpu *vcpu, ulong esid)
  468. {
  469. ulong mp_ea = vcpu->arch.magic_page_ea;
  470. return mp_ea && !(kvmppc_get_msr(vcpu) & MSR_PR) &&
  471. (mp_ea >> SID_SHIFT) == esid;
  472. }
  473. #endif
  474. static int kvmppc_mmu_book3s_64_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,
  475. u64 *vsid)
  476. {
  477. ulong ea = esid << SID_SHIFT;
  478. struct kvmppc_slb *slb;
  479. u64 gvsid = esid;
  480. ulong mp_ea = vcpu->arch.magic_page_ea;
  481. int pagesize = MMU_PAGE_64K;
  482. u64 msr = kvmppc_get_msr(vcpu);
  483. if (msr & (MSR_DR|MSR_IR)) {
  484. slb = kvmppc_mmu_book3s_64_find_slbe(vcpu, ea);
  485. if (slb) {
  486. gvsid = slb->vsid;
  487. pagesize = slb->base_page_size;
  488. if (slb->tb) {
  489. gvsid <<= SID_SHIFT_1T - SID_SHIFT;
  490. gvsid |= esid & ((1ul << (SID_SHIFT_1T - SID_SHIFT)) - 1);
  491. gvsid |= VSID_1T;
  492. }
  493. }
  494. }
  495. switch (msr & (MSR_DR|MSR_IR)) {
  496. case 0:
  497. gvsid = VSID_REAL | esid;
  498. break;
  499. case MSR_IR:
  500. gvsid |= VSID_REAL_IR;
  501. break;
  502. case MSR_DR:
  503. gvsid |= VSID_REAL_DR;
  504. break;
  505. case MSR_DR|MSR_IR:
  506. if (!slb)
  507. goto no_slb;
  508. break;
  509. default:
  510. BUG();
  511. break;
  512. }
  513. #ifdef CONFIG_PPC_64K_PAGES
  514. /*
  515. * Mark this as a 64k segment if the host is using
  516. * 64k pages, the host MMU supports 64k pages and
  517. * the guest segment page size is >= 64k,
  518. * but not if this segment contains the magic page.
  519. */
  520. if (pagesize >= MMU_PAGE_64K &&
  521. mmu_psize_defs[MMU_PAGE_64K].shift &&
  522. !segment_contains_magic_page(vcpu, esid))
  523. gvsid |= VSID_64K;
  524. #endif
  525. if (kvmppc_get_msr(vcpu) & MSR_PR)
  526. gvsid |= VSID_PR;
  527. *vsid = gvsid;
  528. return 0;
  529. no_slb:
  530. /* Catch magic page case */
  531. if (unlikely(mp_ea) &&
  532. unlikely(esid == (mp_ea >> SID_SHIFT)) &&
  533. !(kvmppc_get_msr(vcpu) & MSR_PR)) {
  534. *vsid = VSID_REAL | esid;
  535. return 0;
  536. }
  537. return -EINVAL;
  538. }
  539. static bool kvmppc_mmu_book3s_64_is_dcbz32(struct kvm_vcpu *vcpu)
  540. {
  541. return (to_book3s(vcpu)->hid[5] & 0x80);
  542. }
  543. void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu)
  544. {
  545. struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
  546. mmu->mfsrin = NULL;
  547. mmu->mtsrin = kvmppc_mmu_book3s_64_mtsrin;
  548. mmu->slbmte = kvmppc_mmu_book3s_64_slbmte;
  549. mmu->slbmfee = kvmppc_mmu_book3s_64_slbmfee;
  550. mmu->slbmfev = kvmppc_mmu_book3s_64_slbmfev;
  551. mmu->slbie = kvmppc_mmu_book3s_64_slbie;
  552. mmu->slbia = kvmppc_mmu_book3s_64_slbia;
  553. mmu->xlate = kvmppc_mmu_book3s_64_xlate;
  554. mmu->reset_msr = kvmppc_mmu_book3s_64_reset_msr;
  555. mmu->tlbie = kvmppc_mmu_book3s_64_tlbie;
  556. mmu->esid_to_vsid = kvmppc_mmu_book3s_64_esid_to_vsid;
  557. mmu->ea_to_vp = kvmppc_mmu_book3s_64_ea_to_vp;
  558. mmu->is_dcbz32 = kvmppc_mmu_book3s_64_is_dcbz32;
  559. vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
  560. }