blob: a2eb6d354a57d093c3e46c3f038771176261c38c [file] [log] [blame]
Alexander Graf01235182009-10-30 05:47:13 +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/* #define DEBUG_MMU_PTE */
32/* #define DEBUG_MMU_PTE_IP 0xfff14c40 */
33
34#ifdef DEBUG_MMU
35#define dprintk(X...) printk(KERN_INFO X)
36#else
37#define dprintk(X...) do { } while(0)
38#endif
39
Alexander Grafe425a6d2010-02-19 11:00:36 +010040#ifdef DEBUG_MMU_PTE
Alexander Graf01235182009-10-30 05:47:13 +000041#define dprintk_pte(X...) printk(KERN_INFO X)
42#else
43#define dprintk_pte(X...) do { } while(0)
44#endif
45
46#define PTEG_FLAG_ACCESSED 0x00000100
47#define PTEG_FLAG_DIRTY 0x00000080
Alexander Graf07b09072010-04-16 00:11:53 +020048#ifndef SID_SHIFT
49#define SID_SHIFT 28
50#endif
Alexander Graf01235182009-10-30 05:47:13 +000051
52static inline bool check_debug_ip(struct kvm_vcpu *vcpu)
53{
54#ifdef DEBUG_MMU_PTE_IP
55 return vcpu->arch.pc == DEBUG_MMU_PTE_IP;
56#else
57 return true;
58#endif
59}
60
Alexander Graf8e865172010-08-03 01:06:11 +020061static inline u32 sr_vsid(u32 sr_raw)
62{
63 return sr_raw & 0x0fffffff;
64}
65
66static inline bool sr_valid(u32 sr_raw)
67{
68 return (sr_raw & 0x80000000) ? false : true;
69}
70
71static inline bool sr_ks(u32 sr_raw)
72{
73 return (sr_raw & 0x40000000) ? true: false;
74}
75
76static inline bool sr_kp(u32 sr_raw)
77{
78 return (sr_raw & 0x20000000) ? true: false;
79}
80
Alexander Graf01235182009-10-30 05:47:13 +000081static int kvmppc_mmu_book3s_32_xlate_bat(struct kvm_vcpu *vcpu, gva_t eaddr,
Paul Mackerras93b159b2013-09-20 14:52:51 +100082 struct kvmppc_pte *pte, bool data,
83 bool iswrite);
Alexander Grafaf7b4d12010-04-20 02:49:46 +020084static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,
Alexander Graf4b389ca2010-03-24 21:48:20 +010085 u64 *vsid);
Alexander Graf01235182009-10-30 05:47:13 +000086
Alexander Grafdf1bfa22010-08-03 02:29:27 +020087static u32 find_sr(struct kvm_vcpu *vcpu, gva_t eaddr)
Alexander Graf01235182009-10-30 05:47:13 +000088{
Alexander Graf5deb8e72014-04-24 13:46:24 +020089 return kvmppc_get_sr(vcpu, (eaddr >> 28) & 0xf);
Alexander Graf01235182009-10-30 05:47:13 +000090}
91
92static u64 kvmppc_mmu_book3s_32_ea_to_vp(struct kvm_vcpu *vcpu, gva_t eaddr,
93 bool data)
94{
Alexander Graf4b389ca2010-03-24 21:48:20 +010095 u64 vsid;
Alexander Graf01235182009-10-30 05:47:13 +000096 struct kvmppc_pte pte;
97
Paul Mackerras93b159b2013-09-20 14:52:51 +100098 if (!kvmppc_mmu_book3s_32_xlate_bat(vcpu, eaddr, &pte, data, false))
Alexander Graf01235182009-10-30 05:47:13 +000099 return pte.vpage;
100
Alexander Graf4b389ca2010-03-24 21:48:20 +0100101 kvmppc_mmu_book3s_32_esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
102 return (((u64)eaddr >> 12) & 0xffff) | (vsid << 16);
Alexander Graf01235182009-10-30 05:47:13 +0000103}
104
105static void kvmppc_mmu_book3s_32_reset_msr(struct kvm_vcpu *vcpu)
106{
107 kvmppc_set_msr(vcpu, 0);
108}
109
Paul Mackerras3ff95502013-09-20 14:52:49 +1000110static hva_t kvmppc_mmu_book3s_32_get_pteg(struct kvm_vcpu *vcpu,
Alexander Graf8e865172010-08-03 01:06:11 +0200111 u32 sre, gva_t eaddr,
Alexander Graf01235182009-10-30 05:47:13 +0000112 bool primary)
113{
Paul Mackerras3ff95502013-09-20 14:52:49 +1000114 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
Alexander Graf01235182009-10-30 05:47:13 +0000115 u32 page, hash, pteg, htabmask;
116 hva_t r;
117
118 page = (eaddr & 0x0FFFFFFF) >> 12;
119 htabmask = ((vcpu_book3s->sdr1 & 0x1FF) << 16) | 0xFFC0;
120
Alexander Graf8e865172010-08-03 01:06:11 +0200121 hash = ((sr_vsid(sre) ^ page) << 6);
Alexander Graf01235182009-10-30 05:47:13 +0000122 if (!primary)
123 hash = ~hash;
124 hash &= htabmask;
125
126 pteg = (vcpu_book3s->sdr1 & 0xffff0000) | hash;
127
128 dprintk("MMU: pc=0x%lx eaddr=0x%lx sdr1=0x%llx pteg=0x%x vsid=0x%x\n",
Alexander Grafcd087ee2014-04-24 13:52:01 +0200129 kvmppc_get_pc(vcpu), eaddr, vcpu_book3s->sdr1, pteg,
Alexander Graf8e865172010-08-03 01:06:11 +0200130 sr_vsid(sre));
Alexander Graf01235182009-10-30 05:47:13 +0000131
Paul Mackerras3ff95502013-09-20 14:52:49 +1000132 r = gfn_to_hva(vcpu->kvm, pteg >> PAGE_SHIFT);
Alexander Graf01235182009-10-30 05:47:13 +0000133 if (kvm_is_error_hva(r))
134 return r;
135 return r | (pteg & ~PAGE_MASK);
136}
137
Alexander Graf8e865172010-08-03 01:06:11 +0200138static u32 kvmppc_mmu_book3s_32_get_ptem(u32 sre, gva_t eaddr, bool primary)
Alexander Graf01235182009-10-30 05:47:13 +0000139{
Alexander Graf8e865172010-08-03 01:06:11 +0200140 return ((eaddr & 0x0fffffff) >> 22) | (sr_vsid(sre) << 7) |
Alexander Graf01235182009-10-30 05:47:13 +0000141 (primary ? 0 : 0x40) | 0x80000000;
142}
143
144static int kvmppc_mmu_book3s_32_xlate_bat(struct kvm_vcpu *vcpu, gva_t eaddr,
Paul Mackerras93b159b2013-09-20 14:52:51 +1000145 struct kvmppc_pte *pte, bool data,
146 bool iswrite)
Alexander Graf01235182009-10-30 05:47:13 +0000147{
148 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
149 struct kvmppc_bat *bat;
150 int i;
151
152 for (i = 0; i < 8; i++) {
153 if (data)
154 bat = &vcpu_book3s->dbat[i];
155 else
156 bat = &vcpu_book3s->ibat[i];
157
Alexander Graf5deb8e72014-04-24 13:46:24 +0200158 if (kvmppc_get_msr(vcpu) & MSR_PR) {
Alexander Graf01235182009-10-30 05:47:13 +0000159 if (!bat->vp)
160 continue;
161 } else {
162 if (!bat->vs)
163 continue;
164 }
165
166 if (check_debug_ip(vcpu))
167 {
168 dprintk_pte("%cBAT %02d: 0x%lx - 0x%x (0x%x)\n",
169 data ? 'd' : 'i', i, eaddr, bat->bepi,
170 bat->bepi_mask);
171 }
172 if ((eaddr & bat->bepi_mask) == bat->bepi) {
Alexander Graf4b389ca2010-03-24 21:48:20 +0100173 u64 vsid;
174 kvmppc_mmu_book3s_32_esid_to_vsid(vcpu,
175 eaddr >> SID_SHIFT, &vsid);
176 vsid <<= 16;
177 pte->vpage = (((u64)eaddr >> 12) & 0xffff) | vsid;
178
Alexander Graf01235182009-10-30 05:47:13 +0000179 pte->raddr = bat->brpn | (eaddr & ~bat->bepi_mask);
Alexander Graf01235182009-10-30 05:47:13 +0000180 pte->may_read = bat->pp;
181 pte->may_write = bat->pp > 1;
182 pte->may_execute = true;
183 if (!pte->may_read) {
184 printk(KERN_INFO "BAT is not readable!\n");
185 continue;
186 }
Paul Mackerras93b159b2013-09-20 14:52:51 +1000187 if (iswrite && !pte->may_write) {
Alexander Graf01235182009-10-30 05:47:13 +0000188 dprintk_pte("BAT is read-only!\n");
189 continue;
190 }
191
192 return 0;
193 }
194 }
195
196 return -ENOENT;
197}
198
199static int kvmppc_mmu_book3s_32_xlate_pte(struct kvm_vcpu *vcpu, gva_t eaddr,
200 struct kvmppc_pte *pte, bool data,
Paul Mackerras93b159b2013-09-20 14:52:51 +1000201 bool iswrite, bool primary)
Alexander Graf01235182009-10-30 05:47:13 +0000202{
Alexander Graf8e865172010-08-03 01:06:11 +0200203 u32 sre;
Alexander Graf01235182009-10-30 05:47:13 +0000204 hva_t ptegp;
205 u32 pteg[16];
Alexander Graf860540b2014-04-24 12:51:44 +0200206 u32 pte0, pte1;
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200207 u32 ptem = 0;
Alexander Graf01235182009-10-30 05:47:13 +0000208 int i;
209 int found = 0;
210
Alexander Grafdf1bfa22010-08-03 02:29:27 +0200211 sre = find_sr(vcpu, eaddr);
Alexander Graf01235182009-10-30 05:47:13 +0000212
213 dprintk_pte("SR 0x%lx: vsid=0x%x, raw=0x%x\n", eaddr >> 28,
Alexander Graf8e865172010-08-03 01:06:11 +0200214 sr_vsid(sre), sre);
Alexander Graf01235182009-10-30 05:47:13 +0000215
216 pte->vpage = kvmppc_mmu_book3s_32_ea_to_vp(vcpu, eaddr, data);
217
Paul Mackerras3ff95502013-09-20 14:52:49 +1000218 ptegp = kvmppc_mmu_book3s_32_get_pteg(vcpu, sre, eaddr, primary);
Alexander Graf01235182009-10-30 05:47:13 +0000219 if (kvm_is_error_hva(ptegp)) {
220 printk(KERN_INFO "KVM: Invalid PTEG!\n");
221 goto no_page_found;
222 }
223
224 ptem = kvmppc_mmu_book3s_32_get_ptem(sre, eaddr, primary);
225
226 if(copy_from_user(pteg, (void __user *)ptegp, sizeof(pteg))) {
227 printk(KERN_ERR "KVM: Can't copy data from 0x%lx!\n", ptegp);
228 goto no_page_found;
229 }
230
231 for (i=0; i<16; i+=2) {
Alexander Graf860540b2014-04-24 12:51:44 +0200232 pte0 = be32_to_cpu(pteg[i]);
233 pte1 = be32_to_cpu(pteg[i + 1]);
234 if (ptem == pte0) {
Alexander Graf01235182009-10-30 05:47:13 +0000235 u8 pp;
236
Alexander Graf860540b2014-04-24 12:51:44 +0200237 pte->raddr = (pte1 & ~(0xFFFULL)) | (eaddr & 0xFFF);
238 pp = pte1 & 3;
Alexander Graf01235182009-10-30 05:47:13 +0000239
Alexander Graf5deb8e72014-04-24 13:46:24 +0200240 if ((sr_kp(sre) && (kvmppc_get_msr(vcpu) & MSR_PR)) ||
241 (sr_ks(sre) && !(kvmppc_get_msr(vcpu) & MSR_PR)))
Alexander Graf01235182009-10-30 05:47:13 +0000242 pp |= 4;
243
244 pte->may_write = false;
245 pte->may_read = false;
246 pte->may_execute = true;
247 switch (pp) {
248 case 0:
249 case 1:
250 case 2:
251 case 6:
252 pte->may_write = true;
253 case 3:
254 case 5:
255 case 7:
256 pte->may_read = true;
257 break;
258 }
259
Alexander Graf01235182009-10-30 05:47:13 +0000260 dprintk_pte("MMU: Found PTE -> %x %x - %x\n",
Alexander Graf860540b2014-04-24 12:51:44 +0200261 pte0, pte1, pp);
Alexander Graf01235182009-10-30 05:47:13 +0000262 found = 1;
263 break;
264 }
265 }
266
267 /* Update PTE C and A bits, so the guest's swapper knows we used the
268 page */
269 if (found) {
Alexander Graf860540b2014-04-24 12:51:44 +0200270 u32 pte_r = pte1;
Alexander Graf740f8342014-04-24 12:48:19 +0200271 char __user *addr = (char __user *) (ptegp + (i+1) * sizeof(u32));
Alexander Graf01235182009-10-30 05:47:13 +0000272
Paul Mackerras9308ab82013-09-20 14:52:48 +1000273 /*
274 * Use single-byte writes to update the HPTE, to
275 * conform to what real hardware does.
276 */
277 if (pte->may_read && !(pte_r & PTEG_FLAG_ACCESSED)) {
278 pte_r |= PTEG_FLAG_ACCESSED;
279 put_user(pte_r >> 8, addr + 2);
280 }
Paul Mackerras93b159b2013-09-20 14:52:51 +1000281 if (iswrite && pte->may_write && !(pte_r & PTEG_FLAG_DIRTY)) {
Paul Mackerras9308ab82013-09-20 14:52:48 +1000282 pte_r |= PTEG_FLAG_DIRTY;
283 put_user(pte_r, addr + 3);
284 }
Paul Mackerras93b159b2013-09-20 14:52:51 +1000285 if (!pte->may_read || (iswrite && !pte->may_write))
286 return -EPERM;
Alexander Graf01235182009-10-30 05:47:13 +0000287 return 0;
288 }
289
290no_page_found:
291
292 if (check_debug_ip(vcpu)) {
293 dprintk_pte("KVM MMU: No PTE found (sdr1=0x%llx ptegp=0x%lx)\n",
294 to_book3s(vcpu)->sdr1, ptegp);
295 for (i=0; i<16; i+=2) {
Alexander Graf53021042010-07-29 15:04:16 +0200296 dprintk_pte(" %02d: 0x%x - 0x%x (0x%x)\n",
Alexander Graf860540b2014-04-24 12:51:44 +0200297 i, be32_to_cpu(pteg[i]),
298 be32_to_cpu(pteg[i+1]), ptem);
Alexander Graf01235182009-10-30 05:47:13 +0000299 }
300 }
301
302 return -ENOENT;
303}
304
305static int kvmppc_mmu_book3s_32_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
Paul Mackerras93b159b2013-09-20 14:52:51 +1000306 struct kvmppc_pte *pte, bool data,
307 bool iswrite)
Alexander Graf01235182009-10-30 05:47:13 +0000308{
309 int r;
Alexander Grafe8508942010-07-29 14:47:54 +0200310 ulong mp_ea = vcpu->arch.magic_page_ea;
Alexander Graf01235182009-10-30 05:47:13 +0000311
312 pte->eaddr = eaddr;
Paul Mackerrasc9029c32013-09-20 14:52:45 +1000313 pte->page_size = MMU_PAGE_4K;
Alexander Grafe8508942010-07-29 14:47:54 +0200314
315 /* Magic page override */
316 if (unlikely(mp_ea) &&
317 unlikely((eaddr & ~0xfffULL) == (mp_ea & ~0xfffULL)) &&
Alexander Graf5deb8e72014-04-24 13:46:24 +0200318 !(kvmppc_get_msr(vcpu) & MSR_PR)) {
Alexander Grafe8508942010-07-29 14:47:54 +0200319 pte->vpage = kvmppc_mmu_book3s_32_ea_to_vp(vcpu, eaddr, data);
320 pte->raddr = vcpu->arch.magic_page_pa | (pte->raddr & 0xfff);
321 pte->raddr &= KVM_PAM;
322 pte->may_execute = true;
323 pte->may_read = true;
324 pte->may_write = true;
325
326 return 0;
327 }
328
Paul Mackerras93b159b2013-09-20 14:52:51 +1000329 r = kvmppc_mmu_book3s_32_xlate_bat(vcpu, eaddr, pte, data, iswrite);
Alexander Graf01235182009-10-30 05:47:13 +0000330 if (r < 0)
Paul Mackerras93b159b2013-09-20 14:52:51 +1000331 r = kvmppc_mmu_book3s_32_xlate_pte(vcpu, eaddr, pte,
332 data, iswrite, true);
Alexander Graf2e27ecc2014-07-10 19:22:03 +0200333 if (r == -ENOENT)
Paul Mackerras93b159b2013-09-20 14:52:51 +1000334 r = kvmppc_mmu_book3s_32_xlate_pte(vcpu, eaddr, pte,
335 data, iswrite, false);
Alexander Graf01235182009-10-30 05:47:13 +0000336
337 return r;
338}
339
340
341static u32 kvmppc_mmu_book3s_32_mfsrin(struct kvm_vcpu *vcpu, u32 srnum)
342{
Alexander Graf5deb8e72014-04-24 13:46:24 +0200343 return kvmppc_get_sr(vcpu, srnum);
Alexander Graf01235182009-10-30 05:47:13 +0000344}
345
346static void kvmppc_mmu_book3s_32_mtsrin(struct kvm_vcpu *vcpu, u32 srnum,
347 ulong value)
348{
Alexander Graf5deb8e72014-04-24 13:46:24 +0200349 kvmppc_set_sr(vcpu, srnum, value);
Alexander Graf01235182009-10-30 05:47:13 +0000350 kvmppc_mmu_map_segment(vcpu, srnum << SID_SHIFT);
351}
352
353static void kvmppc_mmu_book3s_32_tlbie(struct kvm_vcpu *vcpu, ulong ea, bool large)
354{
Paul Mackerras9308ab82013-09-20 14:52:48 +1000355 int i;
356 struct kvm_vcpu *v;
357
358 /* flush this VA on all cpus */
359 kvm_for_each_vcpu(i, v, vcpu->kvm)
360 kvmppc_mmu_pte_flush(v, ea, 0x0FFFF000);
Alexander Graf01235182009-10-30 05:47:13 +0000361}
362
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200363static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,
Alexander Graf01235182009-10-30 05:47:13 +0000364 u64 *vsid)
365{
Alexander Graff7bc74e2010-04-20 02:49:48 +0200366 ulong ea = esid << SID_SHIFT;
Alexander Graf8e865172010-08-03 01:06:11 +0200367 u32 sr;
Alexander Graff7bc74e2010-04-20 02:49:48 +0200368 u64 gvsid = esid;
Alexander Graf5deb8e72014-04-24 13:46:24 +0200369 u64 msr = kvmppc_get_msr(vcpu);
Alexander Graff7bc74e2010-04-20 02:49:48 +0200370
Alexander Graf5deb8e72014-04-24 13:46:24 +0200371 if (msr & (MSR_DR|MSR_IR)) {
Alexander Grafdf1bfa22010-08-03 02:29:27 +0200372 sr = find_sr(vcpu, ea);
Alexander Graf8e865172010-08-03 01:06:11 +0200373 if (sr_valid(sr))
374 gvsid = sr_vsid(sr);
Alexander Graff7bc74e2010-04-20 02:49:48 +0200375 }
376
Alexander Graf01235182009-10-30 05:47:13 +0000377 /* In case we only have one of MSR_IR or MSR_DR set, let's put
378 that in the real-mode context (and hope RM doesn't access
379 high memory) */
Alexander Graf5deb8e72014-04-24 13:46:24 +0200380 switch (msr & (MSR_DR|MSR_IR)) {
Alexander Graf01235182009-10-30 05:47:13 +0000381 case 0:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200382 *vsid = VSID_REAL | esid;
Alexander Graf01235182009-10-30 05:47:13 +0000383 break;
384 case MSR_IR:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200385 *vsid = VSID_REAL_IR | gvsid;
Alexander Graf01235182009-10-30 05:47:13 +0000386 break;
387 case MSR_DR:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200388 *vsid = VSID_REAL_DR | gvsid;
Alexander Graf01235182009-10-30 05:47:13 +0000389 break;
390 case MSR_DR|MSR_IR:
Alexander Graf8e865172010-08-03 01:06:11 +0200391 if (sr_valid(sr))
392 *vsid = sr_vsid(sr);
Alexander Graf4d29bdb2010-06-21 15:24:55 +0200393 else
394 *vsid = VSID_BAT | gvsid;
Alexander Graf01235182009-10-30 05:47:13 +0000395 break;
Alexander Graf01235182009-10-30 05:47:13 +0000396 default:
397 BUG();
398 }
399
Alexander Graf5deb8e72014-04-24 13:46:24 +0200400 if (msr & MSR_PR)
Alexander Graf4b389ca2010-03-24 21:48:20 +0100401 *vsid |= VSID_PR;
402
Alexander Graf01235182009-10-30 05:47:13 +0000403 return 0;
404}
405
406static bool kvmppc_mmu_book3s_32_is_dcbz32(struct kvm_vcpu *vcpu)
407{
408 return true;
409}
410
411
412void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu)
413{
414 struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
415
416 mmu->mtsrin = kvmppc_mmu_book3s_32_mtsrin;
417 mmu->mfsrin = kvmppc_mmu_book3s_32_mfsrin;
418 mmu->xlate = kvmppc_mmu_book3s_32_xlate;
419 mmu->reset_msr = kvmppc_mmu_book3s_32_reset_msr;
420 mmu->tlbie = kvmppc_mmu_book3s_32_tlbie;
421 mmu->esid_to_vsid = kvmppc_mmu_book3s_32_esid_to_vsid;
422 mmu->ea_to_vp = kvmppc_mmu_book3s_32_ea_to_vp;
423 mmu->is_dcbz32 = kvmppc_mmu_book3s_32_is_dcbz32;
424
425 mmu->slbmte = NULL;
426 mmu->slbmfee = NULL;
427 mmu->slbmfev = NULL;
428 mmu->slbie = NULL;
429 mmu->slbia = NULL;
430}