blob: d7889ef3211ee38b520b17e3010e16d505fa6ab0 [file] [log] [blame]
Alexander Grafe71b2a32009-10-30 05:47:12 +00001/*
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
20#include <linux/types.h>
21#include <linux/string.h>
22#include <linux/kvm.h>
23#include <linux/kvm_host.h>
24#include <linux/highmem.h>
25
26#include <asm/tlbflush.h>
27#include <asm/kvm_ppc.h>
28#include <asm/kvm_book3s.h>
29
30/* #define DEBUG_MMU */
31
32#ifdef DEBUG_MMU
33#define dprintk(X...) printk(KERN_INFO X)
34#else
35#define dprintk(X...) do { } while(0)
36#endif
37
38static void kvmppc_mmu_book3s_64_reset_msr(struct kvm_vcpu *vcpu)
39{
40 kvmppc_set_msr(vcpu, MSR_SF);
41}
42
43static struct kvmppc_slb *kvmppc_mmu_book3s_64_find_slbe(
44 struct kvmppc_vcpu_book3s *vcpu_book3s,
45 gva_t eaddr)
46{
47 int i;
48 u64 esid = GET_ESID(eaddr);
49 u64 esid_1t = GET_ESID_1T(eaddr);
50
51 for (i = 0; i < vcpu_book3s->slb_nr; i++) {
52 u64 cmp_esid = esid;
53
54 if (!vcpu_book3s->slb[i].valid)
55 continue;
56
Alexander Graf4b5c9b72010-01-10 03:27:47 +010057 if (vcpu_book3s->slb[i].tb)
Alexander Grafe71b2a32009-10-30 05:47:12 +000058 cmp_esid = esid_1t;
59
60 if (vcpu_book3s->slb[i].esid == cmp_esid)
61 return &vcpu_book3s->slb[i];
62 }
63
64 dprintk("KVM: No SLB entry found for 0x%lx [%llx | %llx]\n",
65 eaddr, esid, esid_1t);
66 for (i = 0; i < vcpu_book3s->slb_nr; i++) {
67 if (vcpu_book3s->slb[i].vsid)
Alexander Graf4b5c9b72010-01-10 03:27:47 +010068 dprintk(" %d: %c%c%c %llx %llx\n", i,
Alexander Grafe71b2a32009-10-30 05:47:12 +000069 vcpu_book3s->slb[i].valid ? 'v' : ' ',
70 vcpu_book3s->slb[i].large ? 'l' : ' ',
Alexander Graf4b5c9b72010-01-10 03:27:47 +010071 vcpu_book3s->slb[i].tb ? 't' : ' ',
Alexander Grafe71b2a32009-10-30 05:47:12 +000072 vcpu_book3s->slb[i].esid,
73 vcpu_book3s->slb[i].vsid);
74 }
75
76 return NULL;
77}
78
79static u64 kvmppc_mmu_book3s_64_ea_to_vp(struct kvm_vcpu *vcpu, gva_t eaddr,
80 bool data)
81{
82 struct kvmppc_slb *slb;
83
84 slb = kvmppc_mmu_book3s_64_find_slbe(to_book3s(vcpu), eaddr);
85 if (!slb)
86 return 0;
87
Alexander Graf4b5c9b72010-01-10 03:27:47 +010088 if (slb->tb)
Alexander Grafe71b2a32009-10-30 05:47:12 +000089 return (((u64)eaddr >> 12) & 0xfffffff) |
90 (((u64)slb->vsid) << 28);
91
92 return (((u64)eaddr >> 12) & 0xffff) | (((u64)slb->vsid) << 16);
93}
94
95static int kvmppc_mmu_book3s_64_get_pagesize(struct kvmppc_slb *slbe)
96{
97 return slbe->large ? 24 : 12;
98}
99
100static u32 kvmppc_mmu_book3s_64_get_page(struct kvmppc_slb *slbe, gva_t eaddr)
101{
102 int p = kvmppc_mmu_book3s_64_get_pagesize(slbe);
103 return ((eaddr & 0xfffffff) >> p);
104}
105
106static hva_t kvmppc_mmu_book3s_64_get_pteg(
107 struct kvmppc_vcpu_book3s *vcpu_book3s,
108 struct kvmppc_slb *slbe, gva_t eaddr,
109 bool second)
110{
111 u64 hash, pteg, htabsize;
112 u32 page;
113 hva_t r;
114
115 page = kvmppc_mmu_book3s_64_get_page(slbe, eaddr);
116 htabsize = ((1 << ((vcpu_book3s->sdr1 & 0x1f) + 11)) - 1);
117
118 hash = slbe->vsid ^ page;
119 if (second)
120 hash = ~hash;
121 hash &= ((1ULL << 39ULL) - 1ULL);
122 hash &= htabsize;
123 hash <<= 7ULL;
124
125 pteg = vcpu_book3s->sdr1 & 0xfffffffffffc0000ULL;
126 pteg |= hash;
127
128 dprintk("MMU: page=0x%x sdr1=0x%llx pteg=0x%llx vsid=0x%llx\n",
129 page, vcpu_book3s->sdr1, pteg, slbe->vsid);
130
131 r = gfn_to_hva(vcpu_book3s->vcpu.kvm, pteg >> PAGE_SHIFT);
132 if (kvm_is_error_hva(r))
133 return r;
134 return r | (pteg & ~PAGE_MASK);
135}
136
137static u64 kvmppc_mmu_book3s_64_get_avpn(struct kvmppc_slb *slbe, gva_t eaddr)
138{
139 int p = kvmppc_mmu_book3s_64_get_pagesize(slbe);
140 u64 avpn;
141
142 avpn = kvmppc_mmu_book3s_64_get_page(slbe, eaddr);
143 avpn |= slbe->vsid << (28 - p);
144
145 if (p < 24)
146 avpn >>= ((80 - p) - 56) - 8;
147 else
148 avpn <<= 8;
149
150 return avpn;
151}
152
153static int kvmppc_mmu_book3s_64_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
154 struct kvmppc_pte *gpte, bool data)
155{
156 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
157 struct kvmppc_slb *slbe;
158 hva_t ptegp;
159 u64 pteg[16];
160 u64 avpn = 0;
161 int i;
162 u8 key = 0;
163 bool found = false;
164 bool perm_err = false;
165 int second = 0;
Alexander Grafe8508942010-07-29 14:47:54 +0200166 ulong mp_ea = vcpu->arch.magic_page_ea;
167
168 /* Magic page override */
169 if (unlikely(mp_ea) &&
170 unlikely((eaddr & ~0xfffULL) == (mp_ea & ~0xfffULL)) &&
171 !(vcpu->arch.shared->msr & MSR_PR)) {
172 gpte->eaddr = eaddr;
173 gpte->vpage = kvmppc_mmu_book3s_64_ea_to_vp(vcpu, eaddr, data);
174 gpte->raddr = vcpu->arch.magic_page_pa | (gpte->raddr & 0xfff);
175 gpte->raddr &= KVM_PAM;
176 gpte->may_execute = true;
177 gpte->may_read = true;
178 gpte->may_write = true;
179
180 return 0;
181 }
Alexander Grafe71b2a32009-10-30 05:47:12 +0000182
183 slbe = kvmppc_mmu_book3s_64_find_slbe(vcpu_book3s, eaddr);
184 if (!slbe)
185 goto no_seg_found;
186
187do_second:
188 ptegp = kvmppc_mmu_book3s_64_get_pteg(vcpu_book3s, slbe, eaddr, second);
189 if (kvm_is_error_hva(ptegp))
190 goto no_page_found;
191
192 avpn = kvmppc_mmu_book3s_64_get_avpn(slbe, eaddr);
193
194 if(copy_from_user(pteg, (void __user *)ptegp, sizeof(pteg))) {
195 printk(KERN_ERR "KVM can't copy data from 0x%lx!\n", ptegp);
196 goto no_page_found;
197 }
198
Alexander Graf666e7252010-07-29 14:47:43 +0200199 if ((vcpu->arch.shared->msr & MSR_PR) && slbe->Kp)
Alexander Grafe71b2a32009-10-30 05:47:12 +0000200 key = 4;
Alexander Graf666e7252010-07-29 14:47:43 +0200201 else if (!(vcpu->arch.shared->msr & MSR_PR) && slbe->Ks)
Alexander Grafe71b2a32009-10-30 05:47:12 +0000202 key = 4;
203
204 for (i=0; i<16; i+=2) {
205 u64 v = pteg[i];
206 u64 r = pteg[i+1];
207
208 /* Valid check */
209 if (!(v & HPTE_V_VALID))
210 continue;
211 /* Hash check */
212 if ((v & HPTE_V_SECONDARY) != second)
213 continue;
214
215 /* AVPN compare */
216 if (HPTE_V_AVPN_VAL(avpn) == HPTE_V_AVPN_VAL(v)) {
217 u8 pp = (r & HPTE_R_PP) | key;
218 int eaddr_mask = 0xFFF;
219
220 gpte->eaddr = eaddr;
221 gpte->vpage = kvmppc_mmu_book3s_64_ea_to_vp(vcpu,
222 eaddr,
223 data);
224 if (slbe->large)
225 eaddr_mask = 0xFFFFFF;
226 gpte->raddr = (r & HPTE_R_RPN) | (eaddr & eaddr_mask);
227 gpte->may_execute = ((r & HPTE_R_N) ? false : true);
228 gpte->may_read = false;
229 gpte->may_write = false;
230
231 switch (pp) {
232 case 0:
233 case 1:
234 case 2:
235 case 6:
236 gpte->may_write = true;
237 /* fall through */
238 case 3:
239 case 5:
240 case 7:
241 gpte->may_read = true;
242 break;
243 }
244
245 if (!gpte->may_read) {
246 perm_err = true;
247 continue;
248 }
249
250 dprintk("KVM MMU: Translated 0x%lx [0x%llx] -> 0x%llx "
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200251 "-> 0x%lx\n",
Alexander Grafe71b2a32009-10-30 05:47:12 +0000252 eaddr, avpn, gpte->vpage, gpte->raddr);
253 found = true;
254 break;
255 }
256 }
257
258 /* Update PTE R and C bits, so the guest's swapper knows we used the
259 * page */
260 if (found) {
261 u32 oldr = pteg[i+1];
262
263 if (gpte->may_read) {
264 /* Set the accessed flag */
265 pteg[i+1] |= HPTE_R_R;
266 }
267 if (gpte->may_write) {
268 /* Set the dirty flag */
269 pteg[i+1] |= HPTE_R_C;
270 } else {
271 dprintk("KVM: Mapping read-only page!\n");
272 }
273
274 /* Write back into the PTEG */
275 if (pteg[i+1] != oldr)
276 copy_to_user((void __user *)ptegp, pteg, sizeof(pteg));
277
278 return 0;
279 } else {
280 dprintk("KVM MMU: No PTE found (ea=0x%lx sdr1=0x%llx "
281 "ptegp=0x%lx)\n",
282 eaddr, to_book3s(vcpu)->sdr1, ptegp);
283 for (i = 0; i < 16; i += 2)
284 dprintk(" %02d: 0x%llx - 0x%llx (0x%llx)\n",
285 i, pteg[i], pteg[i+1], avpn);
286
287 if (!second) {
288 second = HPTE_V_SECONDARY;
289 goto do_second;
290 }
291 }
292
293
294no_page_found:
295
296
297 if (perm_err)
298 return -EPERM;
299
300 return -ENOENT;
301
302no_seg_found:
303
304 dprintk("KVM MMU: Trigger segment fault\n");
305 return -EINVAL;
306}
307
308static void kvmppc_mmu_book3s_64_slbmte(struct kvm_vcpu *vcpu, u64 rs, u64 rb)
309{
310 struct kvmppc_vcpu_book3s *vcpu_book3s;
311 u64 esid, esid_1t;
312 int slb_nr;
313 struct kvmppc_slb *slbe;
314
315 dprintk("KVM MMU: slbmte(0x%llx, 0x%llx)\n", rs, rb);
316
317 vcpu_book3s = to_book3s(vcpu);
318
319 esid = GET_ESID(rb);
320 esid_1t = GET_ESID_1T(rb);
321 slb_nr = rb & 0xfff;
322
323 if (slb_nr > vcpu_book3s->slb_nr)
324 return;
325
326 slbe = &vcpu_book3s->slb[slb_nr];
327
328 slbe->large = (rs & SLB_VSID_L) ? 1 : 0;
Alexander Graf4b5c9b72010-01-10 03:27:47 +0100329 slbe->tb = (rs & SLB_VSID_B_1T) ? 1 : 0;
330 slbe->esid = slbe->tb ? esid_1t : esid;
Alexander Grafe71b2a32009-10-30 05:47:12 +0000331 slbe->vsid = rs >> 12;
332 slbe->valid = (rb & SLB_ESID_V) ? 1 : 0;
333 slbe->Ks = (rs & SLB_VSID_KS) ? 1 : 0;
334 slbe->Kp = (rs & SLB_VSID_KP) ? 1 : 0;
335 slbe->nx = (rs & SLB_VSID_N) ? 1 : 0;
336 slbe->class = (rs & SLB_VSID_C) ? 1 : 0;
337
338 slbe->orige = rb & (ESID_MASK | SLB_ESID_V);
339 slbe->origv = rs;
340
341 /* Map the new segment */
342 kvmppc_mmu_map_segment(vcpu, esid << SID_SHIFT);
343}
344
345static u64 kvmppc_mmu_book3s_64_slbmfee(struct kvm_vcpu *vcpu, u64 slb_nr)
346{
347 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
348 struct kvmppc_slb *slbe;
349
350 if (slb_nr > vcpu_book3s->slb_nr)
351 return 0;
352
353 slbe = &vcpu_book3s->slb[slb_nr];
354
355 return slbe->orige;
356}
357
358static u64 kvmppc_mmu_book3s_64_slbmfev(struct kvm_vcpu *vcpu, u64 slb_nr)
359{
360 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
361 struct kvmppc_slb *slbe;
362
363 if (slb_nr > vcpu_book3s->slb_nr)
364 return 0;
365
366 slbe = &vcpu_book3s->slb[slb_nr];
367
368 return slbe->origv;
369}
370
371static void kvmppc_mmu_book3s_64_slbie(struct kvm_vcpu *vcpu, u64 ea)
372{
373 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
374 struct kvmppc_slb *slbe;
375
376 dprintk("KVM MMU: slbie(0x%llx)\n", ea);
377
378 slbe = kvmppc_mmu_book3s_64_find_slbe(vcpu_book3s, ea);
379
380 if (!slbe)
381 return;
382
383 dprintk("KVM MMU: slbie(0x%llx, 0x%llx)\n", ea, slbe->esid);
384
385 slbe->valid = false;
386
387 kvmppc_mmu_map_segment(vcpu, ea);
388}
389
390static void kvmppc_mmu_book3s_64_slbia(struct kvm_vcpu *vcpu)
391{
392 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
393 int i;
394
395 dprintk("KVM MMU: slbia()\n");
396
397 for (i = 1; i < vcpu_book3s->slb_nr; i++)
398 vcpu_book3s->slb[i].valid = false;
399
Alexander Graf666e7252010-07-29 14:47:43 +0200400 if (vcpu->arch.shared->msr & MSR_IR) {
Alexander Grafe71b2a32009-10-30 05:47:12 +0000401 kvmppc_mmu_flush_segments(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +0200402 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
Alexander Grafe71b2a32009-10-30 05:47:12 +0000403 }
404}
405
406static void kvmppc_mmu_book3s_64_mtsrin(struct kvm_vcpu *vcpu, u32 srnum,
407 ulong value)
408{
409 u64 rb = 0, rs = 0;
410
Alexander Graf5279aeb2009-12-19 18:07:39 +0100411 /*
412 * According to Book3 2.01 mtsrin is implemented as:
413 *
414 * The SLB entry specified by (RB)32:35 is loaded from register
415 * RS, as follows.
416 *
417 * SLBE Bit Source SLB Field
418 *
419 * 0:31 0x0000_0000 ESID-0:31
420 * 32:35 (RB)32:35 ESID-32:35
421 * 36 0b1 V
422 * 37:61 0x00_0000|| 0b0 VSID-0:24
423 * 62:88 (RS)37:63 VSID-25:51
424 * 89:91 (RS)33:35 Ks Kp N
425 * 92 (RS)36 L ((RS)36 must be 0b0)
426 * 93 0b0 C
427 */
428
429 dprintk("KVM MMU: mtsrin(0x%x, 0x%lx)\n", srnum, value);
430
Alexander Grafe71b2a32009-10-30 05:47:12 +0000431 /* ESID = srnum */
432 rb |= (srnum & 0xf) << 28;
433 /* Set the valid bit */
434 rb |= 1 << 27;
435 /* Index = ESID */
436 rb |= srnum;
437
438 /* VSID = VSID */
439 rs |= (value & 0xfffffff) << 12;
440 /* flags = flags */
Alexander Graf5279aeb2009-12-19 18:07:39 +0100441 rs |= ((value >> 28) & 0x7) << 9;
Alexander Grafe71b2a32009-10-30 05:47:12 +0000442
443 kvmppc_mmu_book3s_64_slbmte(vcpu, rs, rb);
444}
445
446static void kvmppc_mmu_book3s_64_tlbie(struct kvm_vcpu *vcpu, ulong va,
447 bool large)
448{
449 u64 mask = 0xFFFFFFFFFULL;
450
451 dprintk("KVM MMU: tlbie(0x%lx)\n", va);
452
453 if (large)
454 mask = 0xFFFFFF000ULL;
455 kvmppc_mmu_pte_vflush(vcpu, va >> 12, mask);
456}
457
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200458static int kvmppc_mmu_book3s_64_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,
Alexander Grafe71b2a32009-10-30 05:47:12 +0000459 u64 *vsid)
460{
Alexander Graff7bc74e2010-04-20 02:49:48 +0200461 ulong ea = esid << SID_SHIFT;
462 struct kvmppc_slb *slb;
463 u64 gvsid = esid;
Alexander Grafe8508942010-07-29 14:47:54 +0200464 ulong mp_ea = vcpu->arch.magic_page_ea;
Alexander Graff7bc74e2010-04-20 02:49:48 +0200465
Alexander Graf666e7252010-07-29 14:47:43 +0200466 if (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
Alexander Grafe71b2a32009-10-30 05:47:12 +0000467 slb = kvmppc_mmu_book3s_64_find_slbe(to_book3s(vcpu), ea);
468 if (slb)
Alexander Graff7bc74e2010-04-20 02:49:48 +0200469 gvsid = slb->vsid;
470 }
471
Alexander Graf666e7252010-07-29 14:47:43 +0200472 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
Alexander Graff7bc74e2010-04-20 02:49:48 +0200473 case 0:
474 *vsid = VSID_REAL | esid;
475 break;
476 case MSR_IR:
477 *vsid = VSID_REAL_IR | gvsid;
478 break;
479 case MSR_DR:
480 *vsid = VSID_REAL_DR | gvsid;
481 break;
482 case MSR_DR|MSR_IR:
483 if (!slb)
Alexander Grafe8508942010-07-29 14:47:54 +0200484 goto no_slb;
Alexander Grafe71b2a32009-10-30 05:47:12 +0000485
Alexander Graff7bc74e2010-04-20 02:49:48 +0200486 *vsid = gvsid;
Alexander Grafe71b2a32009-10-30 05:47:12 +0000487 break;
Alexander Grafe71b2a32009-10-30 05:47:12 +0000488 default:
489 BUG();
490 break;
491 }
492
Alexander Graf666e7252010-07-29 14:47:43 +0200493 if (vcpu->arch.shared->msr & MSR_PR)
Alexander Graf63556442010-04-20 02:49:51 +0200494 *vsid |= VSID_PR;
495
Alexander Grafe71b2a32009-10-30 05:47:12 +0000496 return 0;
Alexander Grafe8508942010-07-29 14:47:54 +0200497
498no_slb:
499 /* Catch magic page case */
500 if (unlikely(mp_ea) &&
501 unlikely(esid == (mp_ea >> SID_SHIFT)) &&
502 !(vcpu->arch.shared->msr & MSR_PR)) {
503 *vsid = VSID_REAL | esid;
504 return 0;
505 }
506
507 return -EINVAL;
Alexander Grafe71b2a32009-10-30 05:47:12 +0000508}
509
510static bool kvmppc_mmu_book3s_64_is_dcbz32(struct kvm_vcpu *vcpu)
511{
512 return (to_book3s(vcpu)->hid[5] & 0x80);
513}
514
515void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu)
516{
517 struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
518
519 mmu->mfsrin = NULL;
520 mmu->mtsrin = kvmppc_mmu_book3s_64_mtsrin;
521 mmu->slbmte = kvmppc_mmu_book3s_64_slbmte;
522 mmu->slbmfee = kvmppc_mmu_book3s_64_slbmfee;
523 mmu->slbmfev = kvmppc_mmu_book3s_64_slbmfev;
524 mmu->slbie = kvmppc_mmu_book3s_64_slbie;
525 mmu->slbia = kvmppc_mmu_book3s_64_slbia;
526 mmu->xlate = kvmppc_mmu_book3s_64_xlate;
527 mmu->reset_msr = kvmppc_mmu_book3s_64_reset_msr;
528 mmu->tlbie = kvmppc_mmu_book3s_64_tlbie;
529 mmu->esid_to_vsid = kvmppc_mmu_book3s_64_esid_to_vsid;
530 mmu->ea_to_vp = kvmppc_mmu_book3s_64_ea_to_vp;
531 mmu->is_dcbz32 = kvmppc_mmu_book3s_64_is_dcbz32;
Alexander Grafe15a1132009-11-30 03:02:02 +0000532
533 vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
Alexander Grafe71b2a32009-10-30 05:47:12 +0000534}