blob: 2035d16a9262ac5e547bacc0cbcb4fc862706aa5 [file] [log] [blame]
Alexander Grafd32154f2010-04-16 00:11:33 +02001/*
2 * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.
3 *
4 * Authors:
5 * Alexander Graf <agraf@suse.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 2, as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21#include <linux/kvm_host.h>
22
23#include <asm/kvm_ppc.h>
24#include <asm/kvm_book3s.h>
25#include <asm/mmu-hash32.h>
26#include <asm/machdep.h>
27#include <asm/mmu_context.h>
28#include <asm/hw_irq.h>
29
30/* #define DEBUG_MMU */
31/* #define DEBUG_SR */
32
33#ifdef DEBUG_MMU
34#define dprintk_mmu(a, ...) printk(KERN_INFO a, __VA_ARGS__)
35#else
36#define dprintk_mmu(a, ...) do { } while(0)
37#endif
38
39#ifdef DEBUG_SR
40#define dprintk_sr(a, ...) printk(KERN_INFO a, __VA_ARGS__)
41#else
42#define dprintk_sr(a, ...) do { } while(0)
43#endif
44
45#if PAGE_SHIFT != 12
46#error Unknown page size
47#endif
48
49#ifdef CONFIG_SMP
50#error XXX need to grab mmu_hash_lock
51#endif
52
53#ifdef CONFIG_PTE_64BIT
54#error Only 32 bit pages are supported for now
55#endif
56
Alexander Graf251585b2010-04-20 02:49:53 +020057static ulong htab;
58static u32 htabmask;
59
Alexander Graffef093be2010-06-30 15:18:46 +020060void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
Alexander Grafd32154f2010-04-16 00:11:33 +020061{
62 volatile u32 *pteg;
63
Alexander Graffef093be2010-06-30 15:18:46 +020064 /* Remove from host HTAB */
Alexander Grafd32154f2010-04-16 00:11:33 +020065 pteg = (u32*)pte->slot;
Alexander Grafd32154f2010-04-16 00:11:33 +020066 pteg[0] = 0;
Alexander Graffef093be2010-06-30 15:18:46 +020067
68 /* And make sure it's gone from the TLB too */
Alexander Grafd32154f2010-04-16 00:11:33 +020069 asm volatile ("sync");
70 asm volatile ("tlbie %0" : : "r" (pte->pte.eaddr) : "memory");
71 asm volatile ("sync");
72 asm volatile ("tlbsync");
Alexander Grafd32154f2010-04-16 00:11:33 +020073}
74
75/* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
76 * a hash, so we don't waste cycles on looping */
77static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)
78{
Alexander Grafb9877ce2010-08-02 21:48:53 +020079 return (u16)(((gvsid >> (SID_MAP_BITS * 7)) & SID_MAP_MASK) ^
80 ((gvsid >> (SID_MAP_BITS * 6)) & SID_MAP_MASK) ^
81 ((gvsid >> (SID_MAP_BITS * 5)) & SID_MAP_MASK) ^
82 ((gvsid >> (SID_MAP_BITS * 4)) & SID_MAP_MASK) ^
83 ((gvsid >> (SID_MAP_BITS * 3)) & SID_MAP_MASK) ^
84 ((gvsid >> (SID_MAP_BITS * 2)) & SID_MAP_MASK) ^
85 ((gvsid >> (SID_MAP_BITS * 1)) & SID_MAP_MASK) ^
86 ((gvsid >> (SID_MAP_BITS * 0)) & SID_MAP_MASK));
Alexander Grafd32154f2010-04-16 00:11:33 +020087}
88
89
90static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
91{
92 struct kvmppc_sid_map *map;
93 u16 sid_map_mask;
94
Alexander Graf5deb8e72014-04-24 13:46:24 +020095 if (kvmppc_get_msr(vcpu) & MSR_PR)
Alexander Grafd32154f2010-04-16 00:11:33 +020096 gvsid |= VSID_PR;
97
98 sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
99 map = &to_book3s(vcpu)->sid_map[sid_map_mask];
100 if (map->guest_vsid == gvsid) {
101 dprintk_sr("SR: Searching 0x%llx -> 0x%llx\n",
102 gvsid, map->host_vsid);
103 return map;
104 }
105
106 map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];
107 if (map->guest_vsid == gvsid) {
108 dprintk_sr("SR: Searching 0x%llx -> 0x%llx\n",
109 gvsid, map->host_vsid);
110 return map;
111 }
112
113 dprintk_sr("SR: Searching 0x%llx -> not found\n", gvsid);
114 return NULL;
115}
116
Alexander Grafd32154f2010-04-16 00:11:33 +0200117static u32 *kvmppc_mmu_get_pteg(struct kvm_vcpu *vcpu, u32 vsid, u32 eaddr,
118 bool primary)
119{
Alexander Graf251585b2010-04-20 02:49:53 +0200120 u32 page, hash;
121 ulong pteg = htab;
Alexander Grafd32154f2010-04-16 00:11:33 +0200122
123 page = (eaddr & ~ESID_MASK) >> 12;
124
125 hash = ((vsid ^ page) << 6);
126 if (!primary)
127 hash = ~hash;
128
Alexander Grafd32154f2010-04-16 00:11:33 +0200129 hash &= htabmask;
130
131 pteg |= hash;
132
Alexander Graf251585b2010-04-20 02:49:53 +0200133 dprintk_mmu("htab: %lx | hash: %x | htabmask: %x | pteg: %lx\n",
134 htab, hash, htabmask, pteg);
Alexander Grafd32154f2010-04-16 00:11:33 +0200135
136 return (u32*)pteg;
137}
138
139extern char etext[];
140
Paul Mackerras93b159b2013-09-20 14:52:51 +1000141int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte,
142 bool iswrite)
Alexander Grafd32154f2010-04-16 00:11:33 +0200143{
144 pfn_t hpaddr;
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000145 u64 vpn;
Alexander Grafd32154f2010-04-16 00:11:33 +0200146 u64 vsid;
147 struct kvmppc_sid_map *map;
148 volatile u32 *pteg;
149 u32 eaddr = orig_pte->eaddr;
150 u32 pteg0, pteg1;
151 register int rr = 0;
152 bool primary = false;
153 bool evict = false;
Alexander Grafd32154f2010-04-16 00:11:33 +0200154 struct hpte_cache *pte;
Alexander Graf468a12c2011-12-09 14:44:13 +0100155 int r = 0;
Paul Mackerras93b159b2013-09-20 14:52:51 +1000156 bool writable;
Alexander Grafd32154f2010-04-16 00:11:33 +0200157
158 /* Get host physical address for gpa */
Alexander Graf89b68c92014-07-13 16:37:12 +0200159 hpaddr = kvmppc_gpa_to_pfn(vcpu, orig_pte->raddr, iswrite, &writable);
Xiao Guangrong81c52c52012-10-16 20:10:59 +0800160 if (is_error_noslot_pfn(hpaddr)) {
Alexander Graf89b68c92014-07-13 16:37:12 +0200161 printk(KERN_INFO "Couldn't get guest page for gpa %lx!\n",
162 orig_pte->raddr);
Alexander Graf468a12c2011-12-09 14:44:13 +0100163 r = -EINVAL;
164 goto out;
Alexander Grafd32154f2010-04-16 00:11:33 +0200165 }
166 hpaddr <<= PAGE_SHIFT;
167
168 /* and write the mapping ea -> hpa into the pt */
169 vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
170 map = find_sid_vsid(vcpu, vsid);
171 if (!map) {
172 kvmppc_mmu_map_segment(vcpu, eaddr);
173 map = find_sid_vsid(vcpu, vsid);
174 }
175 BUG_ON(!map);
176
177 vsid = map->host_vsid;
Aneesh Kumar K.Vce236ab2012-10-16 22:25:45 +0000178 vpn = (vsid << (SID_SHIFT - VPN_SHIFT)) |
179 ((eaddr & ~ESID_MASK) >> VPN_SHIFT);
Alexander Grafd32154f2010-04-16 00:11:33 +0200180next_pteg:
181 if (rr == 16) {
182 primary = !primary;
183 evict = true;
184 rr = 0;
185 }
186
187 pteg = kvmppc_mmu_get_pteg(vcpu, vsid, eaddr, primary);
188
189 /* not evicting yet */
190 if (!evict && (pteg[rr] & PTE_V)) {
191 rr += 2;
192 goto next_pteg;
193 }
194
195 dprintk_mmu("KVM: old PTEG: %p (%d)\n", pteg, rr);
196 dprintk_mmu("KVM: %08x - %08x\n", pteg[0], pteg[1]);
197 dprintk_mmu("KVM: %08x - %08x\n", pteg[2], pteg[3]);
198 dprintk_mmu("KVM: %08x - %08x\n", pteg[4], pteg[5]);
199 dprintk_mmu("KVM: %08x - %08x\n", pteg[6], pteg[7]);
200 dprintk_mmu("KVM: %08x - %08x\n", pteg[8], pteg[9]);
201 dprintk_mmu("KVM: %08x - %08x\n", pteg[10], pteg[11]);
202 dprintk_mmu("KVM: %08x - %08x\n", pteg[12], pteg[13]);
203 dprintk_mmu("KVM: %08x - %08x\n", pteg[14], pteg[15]);
204
205 pteg0 = ((eaddr & 0x0fffffff) >> 22) | (vsid << 7) | PTE_V |
206 (primary ? 0 : PTE_SEC);
207 pteg1 = hpaddr | PTE_M | PTE_R | PTE_C;
208
Paul Mackerras93b159b2013-09-20 14:52:51 +1000209 if (orig_pte->may_write && writable) {
Alexander Grafd32154f2010-04-16 00:11:33 +0200210 pteg1 |= PP_RWRW;
211 mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
212 } else {
213 pteg1 |= PP_RWRX;
214 }
215
Alexander Graf249ba1e2012-08-03 13:56:33 +0200216 if (orig_pte->may_execute)
217 kvmppc_mmu_flush_icache(hpaddr >> PAGE_SHIFT);
218
Alexander Grafd32154f2010-04-16 00:11:33 +0200219 local_irq_disable();
220
221 if (pteg[rr]) {
222 pteg[rr] = 0;
223 asm volatile ("sync");
224 }
225 pteg[rr + 1] = pteg1;
226 pteg[rr] = pteg0;
227 asm volatile ("sync");
228
229 local_irq_enable();
230
231 dprintk_mmu("KVM: new PTEG: %p\n", pteg);
232 dprintk_mmu("KVM: %08x - %08x\n", pteg[0], pteg[1]);
233 dprintk_mmu("KVM: %08x - %08x\n", pteg[2], pteg[3]);
234 dprintk_mmu("KVM: %08x - %08x\n", pteg[4], pteg[5]);
235 dprintk_mmu("KVM: %08x - %08x\n", pteg[6], pteg[7]);
236 dprintk_mmu("KVM: %08x - %08x\n", pteg[8], pteg[9]);
237 dprintk_mmu("KVM: %08x - %08x\n", pteg[10], pteg[11]);
238 dprintk_mmu("KVM: %08x - %08x\n", pteg[12], pteg[13]);
239 dprintk_mmu("KVM: %08x - %08x\n", pteg[14], pteg[15]);
240
241
242 /* Now tell our Shadow PTE code about the new page */
243
Alexander Graffef093be2010-06-30 15:18:46 +0200244 pte = kvmppc_mmu_hpte_cache_next(vcpu);
Zhouyi Zhou47d45d92013-12-02 18:21:58 +0800245 if (!pte) {
246 kvm_release_pfn_clean(hpaddr >> PAGE_SHIFT);
247 r = -EAGAIN;
248 goto out;
249 }
Alexander Grafd32154f2010-04-16 00:11:33 +0200250
251 dprintk_mmu("KVM: %c%c Map 0x%llx: [%lx] 0x%llx (0x%llx) -> %lx\n",
252 orig_pte->may_write ? 'w' : '-',
253 orig_pte->may_execute ? 'x' : '-',
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000254 orig_pte->eaddr, (ulong)pteg, vpn,
Alexander Grafd32154f2010-04-16 00:11:33 +0200255 orig_pte->vpage, hpaddr);
256
257 pte->slot = (ulong)&pteg[rr];
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000258 pte->host_vpn = vpn;
Alexander Grafd32154f2010-04-16 00:11:33 +0200259 pte->pte = *orig_pte;
260 pte->pfn = hpaddr >> PAGE_SHIFT;
261
Alexander Graffef093be2010-06-30 15:18:46 +0200262 kvmppc_mmu_hpte_cache_map(vcpu, pte);
263
Alexander Graf9b0cb3c2012-08-10 13:23:55 +0200264 kvm_release_pfn_clean(hpaddr >> PAGE_SHIFT);
Alexander Graf468a12c2011-12-09 14:44:13 +0100265out:
266 return r;
Alexander Grafd32154f2010-04-16 00:11:33 +0200267}
268
Paul Mackerras93b159b2013-09-20 14:52:51 +1000269void kvmppc_mmu_unmap_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
270{
271 kvmppc_mmu_pte_vflush(vcpu, pte->vpage, 0xfffffffffULL);
272}
273
Alexander Grafd32154f2010-04-16 00:11:33 +0200274static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
275{
276 struct kvmppc_sid_map *map;
277 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
278 u16 sid_map_mask;
279 static int backwards_map = 0;
280
Alexander Graf5deb8e72014-04-24 13:46:24 +0200281 if (kvmppc_get_msr(vcpu) & MSR_PR)
Alexander Grafd32154f2010-04-16 00:11:33 +0200282 gvsid |= VSID_PR;
283
284 /* We might get collisions that trap in preceding order, so let's
285 map them differently */
286
287 sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
288 if (backwards_map)
289 sid_map_mask = SID_MAP_MASK - sid_map_mask;
290
291 map = &to_book3s(vcpu)->sid_map[sid_map_mask];
292
293 /* Make sure we're taking the other map next time */
294 backwards_map = !backwards_map;
295
296 /* Uh-oh ... out of mappings. Let's flush! */
Alexander Graf8b6db3b2010-08-15 08:04:24 +0200297 if (vcpu_book3s->vsid_next >= VSID_POOL_SIZE) {
298 vcpu_book3s->vsid_next = 0;
Alexander Grafd32154f2010-04-16 00:11:33 +0200299 memset(vcpu_book3s->sid_map, 0,
300 sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
301 kvmppc_mmu_pte_flush(vcpu, 0, 0);
302 kvmppc_mmu_flush_segments(vcpu);
303 }
Alexander Graf8b6db3b2010-08-15 08:04:24 +0200304 map->host_vsid = vcpu_book3s->vsid_pool[vcpu_book3s->vsid_next];
305 vcpu_book3s->vsid_next++;
Alexander Grafd32154f2010-04-16 00:11:33 +0200306
307 map->guest_vsid = gvsid;
308 map->valid = true;
309
310 return map;
311}
312
313int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
314{
315 u32 esid = eaddr >> SID_SHIFT;
316 u64 gvsid;
317 u32 sr;
318 struct kvmppc_sid_map *map;
Alexander Graf468a12c2011-12-09 14:44:13 +0100319 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
320 int r = 0;
Alexander Grafd32154f2010-04-16 00:11:33 +0200321
322 if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
323 /* Invalidate an entry */
324 svcpu->sr[esid] = SR_INVALID;
Alexander Graf468a12c2011-12-09 14:44:13 +0100325 r = -ENOENT;
326 goto out;
Alexander Grafd32154f2010-04-16 00:11:33 +0200327 }
328
329 map = find_sid_vsid(vcpu, gvsid);
330 if (!map)
331 map = create_sid_map(vcpu, gvsid);
332
333 map->guest_esid = esid;
334 sr = map->host_vsid | SR_KP;
335 svcpu->sr[esid] = sr;
336
337 dprintk_sr("MMU: mtsr %d, 0x%x\n", esid, sr);
338
Alexander Graf468a12c2011-12-09 14:44:13 +0100339out:
340 svcpu_put(svcpu);
341 return r;
Alexander Grafd32154f2010-04-16 00:11:33 +0200342}
343
344void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
345{
346 int i;
Alexander Graf468a12c2011-12-09 14:44:13 +0100347 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
Alexander Grafd32154f2010-04-16 00:11:33 +0200348
349 dprintk_sr("MMU: flushing all segments (%d)\n", ARRAY_SIZE(svcpu->sr));
350 for (i = 0; i < ARRAY_SIZE(svcpu->sr); i++)
351 svcpu->sr[i] = SR_INVALID;
Alexander Graf468a12c2011-12-09 14:44:13 +0100352
353 svcpu_put(svcpu);
Alexander Grafd32154f2010-04-16 00:11:33 +0200354}
355
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530356void kvmppc_mmu_destroy_pr(struct kvm_vcpu *vcpu)
Alexander Grafd32154f2010-04-16 00:11:33 +0200357{
Alexander Graf8b6db3b2010-08-15 08:04:24 +0200358 int i;
359
Alexander Graffef093be2010-06-30 15:18:46 +0200360 kvmppc_mmu_hpte_destroy(vcpu);
Alexander Grafd32154f2010-04-16 00:11:33 +0200361 preempt_disable();
Alexander Graf8b6db3b2010-08-15 08:04:24 +0200362 for (i = 0; i < SID_CONTEXTS; i++)
363 __destroy_context(to_book3s(vcpu)->context_id[i]);
Alexander Grafd32154f2010-04-16 00:11:33 +0200364 preempt_enable();
365}
366
367/* From mm/mmu_context_hash32.c */
Alexander Graf8b6db3b2010-08-15 08:04:24 +0200368#define CTX_TO_VSID(c, id) ((((c) * (897 * 16)) + (id * 0x111)) & 0xffffff)
Alexander Grafd32154f2010-04-16 00:11:33 +0200369
370int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
371{
372 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
373 int err;
Alexander Graf251585b2010-04-20 02:49:53 +0200374 ulong sdr1;
Alexander Graf8b6db3b2010-08-15 08:04:24 +0200375 int i;
376 int j;
Alexander Grafd32154f2010-04-16 00:11:33 +0200377
Alexander Graf8b6db3b2010-08-15 08:04:24 +0200378 for (i = 0; i < SID_CONTEXTS; i++) {
379 err = __init_new_context();
380 if (err < 0)
381 goto init_fail;
382 vcpu3s->context_id[i] = err;
Alexander Grafd32154f2010-04-16 00:11:33 +0200383
Alexander Graf8b6db3b2010-08-15 08:04:24 +0200384 /* Remember context id for this combination */
385 for (j = 0; j < 16; j++)
386 vcpu3s->vsid_pool[(i * 16) + j] = CTX_TO_VSID(err, j);
387 }
Alexander Grafd32154f2010-04-16 00:11:33 +0200388
Alexander Graf8b6db3b2010-08-15 08:04:24 +0200389 vcpu3s->vsid_next = 0;
Alexander Grafd32154f2010-04-16 00:11:33 +0200390
Alexander Graf251585b2010-04-20 02:49:53 +0200391 /* Remember where the HTAB is */
392 asm ( "mfsdr1 %0" : "=r"(sdr1) );
393 htabmask = ((sdr1 & 0x1FF) << 16) | 0xFFC0;
394 htab = (ulong)__va(sdr1 & 0xffff0000);
395
Alexander Graffef093be2010-06-30 15:18:46 +0200396 kvmppc_mmu_hpte_init(vcpu);
397
Alexander Grafd32154f2010-04-16 00:11:33 +0200398 return 0;
Alexander Graf8b6db3b2010-08-15 08:04:24 +0200399
400init_fail:
401 for (j = 0; j < i; j++) {
402 if (!vcpu3s->context_id[j])
403 continue;
404
405 __destroy_context(to_book3s(vcpu)->context_id[j]);
406 }
407
408 return -1;
Alexander Grafd32154f2010-04-16 00:11:33 +0200409}