• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/arch/powerpc/kvm/
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
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
40#ifdef DEBUG_MMU_PTE
41#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
48#ifndef SID_SHIFT
49#define SID_SHIFT		28
50#endif
51
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
61static int kvmppc_mmu_book3s_32_xlate_bat(struct kvm_vcpu *vcpu, gva_t eaddr,
62					  struct kvmppc_pte *pte, bool data);
63static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,
64					     u64 *vsid);
65
66static struct kvmppc_sr *find_sr(struct kvmppc_vcpu_book3s *vcpu_book3s, gva_t eaddr)
67{
68	return &vcpu_book3s->sr[(eaddr >> 28) & 0xf];
69}
70
71static u64 kvmppc_mmu_book3s_32_ea_to_vp(struct kvm_vcpu *vcpu, gva_t eaddr,
72					 bool data)
73{
74	u64 vsid;
75	struct kvmppc_pte pte;
76
77	if (!kvmppc_mmu_book3s_32_xlate_bat(vcpu, eaddr, &pte, data))
78		return pte.vpage;
79
80	kvmppc_mmu_book3s_32_esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
81	return (((u64)eaddr >> 12) & 0xffff) | (vsid << 16);
82}
83
84static void kvmppc_mmu_book3s_32_reset_msr(struct kvm_vcpu *vcpu)
85{
86	kvmppc_set_msr(vcpu, 0);
87}
88
89static hva_t kvmppc_mmu_book3s_32_get_pteg(struct kvmppc_vcpu_book3s *vcpu_book3s,
90				      struct kvmppc_sr *sre, gva_t eaddr,
91				      bool primary)
92{
93	u32 page, hash, pteg, htabmask;
94	hva_t r;
95
96	page = (eaddr & 0x0FFFFFFF) >> 12;
97	htabmask = ((vcpu_book3s->sdr1 & 0x1FF) << 16) | 0xFFC0;
98
99	hash = ((sre->vsid ^ page) << 6);
100	if (!primary)
101		hash = ~hash;
102	hash &= htabmask;
103
104	pteg = (vcpu_book3s->sdr1 & 0xffff0000) | hash;
105
106	dprintk("MMU: pc=0x%lx eaddr=0x%lx sdr1=0x%llx pteg=0x%x vsid=0x%x\n",
107		vcpu_book3s->vcpu.arch.pc, eaddr, vcpu_book3s->sdr1, pteg,
108		sre->vsid);
109
110	r = gfn_to_hva(vcpu_book3s->vcpu.kvm, pteg >> PAGE_SHIFT);
111	if (kvm_is_error_hva(r))
112		return r;
113	return r | (pteg & ~PAGE_MASK);
114}
115
116static u32 kvmppc_mmu_book3s_32_get_ptem(struct kvmppc_sr *sre, gva_t eaddr,
117				    bool primary)
118{
119	return ((eaddr & 0x0fffffff) >> 22) | (sre->vsid << 7) |
120	       (primary ? 0 : 0x40) | 0x80000000;
121}
122
123static int kvmppc_mmu_book3s_32_xlate_bat(struct kvm_vcpu *vcpu, gva_t eaddr,
124					  struct kvmppc_pte *pte, bool data)
125{
126	struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
127	struct kvmppc_bat *bat;
128	int i;
129
130	for (i = 0; i < 8; i++) {
131		if (data)
132			bat = &vcpu_book3s->dbat[i];
133		else
134			bat = &vcpu_book3s->ibat[i];
135
136		if (vcpu->arch.msr & MSR_PR) {
137			if (!bat->vp)
138				continue;
139		} else {
140			if (!bat->vs)
141				continue;
142		}
143
144		if (check_debug_ip(vcpu))
145		{
146			dprintk_pte("%cBAT %02d: 0x%lx - 0x%x (0x%x)\n",
147				    data ? 'd' : 'i', i, eaddr, bat->bepi,
148				    bat->bepi_mask);
149		}
150		if ((eaddr & bat->bepi_mask) == bat->bepi) {
151			u64 vsid;
152			kvmppc_mmu_book3s_32_esid_to_vsid(vcpu,
153				eaddr >> SID_SHIFT, &vsid);
154			vsid <<= 16;
155			pte->vpage = (((u64)eaddr >> 12) & 0xffff) | vsid;
156
157			pte->raddr = bat->brpn | (eaddr & ~bat->bepi_mask);
158			pte->may_read = bat->pp;
159			pte->may_write = bat->pp > 1;
160			pte->may_execute = true;
161			if (!pte->may_read) {
162				printk(KERN_INFO "BAT is not readable!\n");
163				continue;
164			}
165			if (!pte->may_write) {
166				/* let's treat r/o BATs as not-readable for now */
167				dprintk_pte("BAT is read-only!\n");
168				continue;
169			}
170
171			return 0;
172		}
173	}
174
175	return -ENOENT;
176}
177
178static int kvmppc_mmu_book3s_32_xlate_pte(struct kvm_vcpu *vcpu, gva_t eaddr,
179				     struct kvmppc_pte *pte, bool data,
180				     bool primary)
181{
182	struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
183	struct kvmppc_sr *sre;
184	hva_t ptegp;
185	u32 pteg[16];
186	u32 ptem = 0;
187	int i;
188	int found = 0;
189
190	sre = find_sr(vcpu_book3s, eaddr);
191
192	dprintk_pte("SR 0x%lx: vsid=0x%x, raw=0x%x\n", eaddr >> 28,
193		    sre->vsid, sre->raw);
194
195	pte->vpage = kvmppc_mmu_book3s_32_ea_to_vp(vcpu, eaddr, data);
196
197	ptegp = kvmppc_mmu_book3s_32_get_pteg(vcpu_book3s, sre, eaddr, primary);
198	if (kvm_is_error_hva(ptegp)) {
199		printk(KERN_INFO "KVM: Invalid PTEG!\n");
200		goto no_page_found;
201	}
202
203	ptem = kvmppc_mmu_book3s_32_get_ptem(sre, eaddr, primary);
204
205	if(copy_from_user(pteg, (void __user *)ptegp, sizeof(pteg))) {
206		printk(KERN_ERR "KVM: Can't copy data from 0x%lx!\n", ptegp);
207		goto no_page_found;
208	}
209
210	for (i=0; i<16; i+=2) {
211		if (ptem == pteg[i]) {
212			u8 pp;
213
214			pte->raddr = (pteg[i+1] & ~(0xFFFULL)) | (eaddr & 0xFFF);
215			pp = pteg[i+1] & 3;
216
217			if ((sre->Kp &&  (vcpu->arch.msr & MSR_PR)) ||
218			    (sre->Ks && !(vcpu->arch.msr & MSR_PR)))
219				pp |= 4;
220
221			pte->may_write = false;
222			pte->may_read = false;
223			pte->may_execute = true;
224			switch (pp) {
225				case 0:
226				case 1:
227				case 2:
228				case 6:
229					pte->may_write = true;
230				case 3:
231				case 5:
232				case 7:
233					pte->may_read = true;
234					break;
235			}
236
237			if ( !pte->may_read )
238				continue;
239
240			dprintk_pte("MMU: Found PTE -> %x %x - %x\n",
241				    pteg[i], pteg[i+1], pp);
242			found = 1;
243			break;
244		}
245	}
246
247	/* Update PTE C and A bits, so the guest's swapper knows we used the
248	   page */
249	if (found) {
250		u32 oldpte = pteg[i+1];
251
252		if (pte->may_read)
253			pteg[i+1] |= PTEG_FLAG_ACCESSED;
254		if (pte->may_write)
255			pteg[i+1] |= PTEG_FLAG_DIRTY;
256		else
257			dprintk_pte("KVM: Mapping read-only page!\n");
258
259		/* Write back into the PTEG */
260		if (pteg[i+1] != oldpte)
261			copy_to_user((void __user *)ptegp, pteg, sizeof(pteg));
262
263		return 0;
264	}
265
266no_page_found:
267
268	if (check_debug_ip(vcpu)) {
269		dprintk_pte("KVM MMU: No PTE found (sdr1=0x%llx ptegp=0x%lx)\n",
270			    to_book3s(vcpu)->sdr1, ptegp);
271		for (i=0; i<16; i+=2) {
272			dprintk_pte("   %02d: 0x%x - 0x%x (0x%llx)\n",
273				    i, pteg[i], pteg[i+1], ptem);
274		}
275	}
276
277	return -ENOENT;
278}
279
280static int kvmppc_mmu_book3s_32_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
281				      struct kvmppc_pte *pte, bool data)
282{
283	int r;
284
285	pte->eaddr = eaddr;
286	r = kvmppc_mmu_book3s_32_xlate_bat(vcpu, eaddr, pte, data);
287	if (r < 0)
288	       r = kvmppc_mmu_book3s_32_xlate_pte(vcpu, eaddr, pte, data, true);
289	if (r < 0)
290	       r = kvmppc_mmu_book3s_32_xlate_pte(vcpu, eaddr, pte, data, false);
291
292	return r;
293}
294
295
296static u32 kvmppc_mmu_book3s_32_mfsrin(struct kvm_vcpu *vcpu, u32 srnum)
297{
298	return to_book3s(vcpu)->sr[srnum].raw;
299}
300
301static void kvmppc_mmu_book3s_32_mtsrin(struct kvm_vcpu *vcpu, u32 srnum,
302					ulong value)
303{
304	struct kvmppc_sr *sre;
305
306	sre = &to_book3s(vcpu)->sr[srnum];
307
308	/* Flush any left-over shadows from the previous SR */
309
310	/* kvmppc_mmu_pte_flush(vcpu, ((u64)sre->vsid) << 28, 0xf0000000ULL); */
311
312	/* And then put in the new SR */
313	sre->raw = value;
314	sre->vsid = (value & 0x0fffffff);
315	sre->valid = (value & 0x80000000) ? false : true;
316	sre->Ks = (value & 0x40000000) ? true : false;
317	sre->Kp = (value & 0x20000000) ? true : false;
318	sre->nx = (value & 0x10000000) ? true : false;
319
320	/* Map the new segment */
321	kvmppc_mmu_map_segment(vcpu, srnum << SID_SHIFT);
322}
323
324static void kvmppc_mmu_book3s_32_tlbie(struct kvm_vcpu *vcpu, ulong ea, bool large)
325{
326	kvmppc_mmu_pte_flush(vcpu, ea, 0x0FFFF000);
327}
328
329static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,
330					     u64 *vsid)
331{
332	ulong ea = esid << SID_SHIFT;
333	struct kvmppc_sr *sr;
334	u64 gvsid = esid;
335
336	if (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
337		sr = find_sr(to_book3s(vcpu), ea);
338		if (sr->valid)
339			gvsid = sr->vsid;
340	}
341
342	/* In case we only have one of MSR_IR or MSR_DR set, let's put
343	   that in the real-mode context (and hope RM doesn't access
344	   high memory) */
345	switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
346	case 0:
347		*vsid = VSID_REAL | esid;
348		break;
349	case MSR_IR:
350		*vsid = VSID_REAL_IR | gvsid;
351		break;
352	case MSR_DR:
353		*vsid = VSID_REAL_DR | gvsid;
354		break;
355	case MSR_DR|MSR_IR:
356		if (sr->valid)
357			*vsid = sr->vsid;
358		else
359			*vsid = VSID_BAT | gvsid;
360		break;
361	default:
362		BUG();
363	}
364
365	if (vcpu->arch.msr & MSR_PR)
366		*vsid |= VSID_PR;
367
368	return 0;
369}
370
371static bool kvmppc_mmu_book3s_32_is_dcbz32(struct kvm_vcpu *vcpu)
372{
373	return true;
374}
375
376
377void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu)
378{
379	struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
380
381	mmu->mtsrin = kvmppc_mmu_book3s_32_mtsrin;
382	mmu->mfsrin = kvmppc_mmu_book3s_32_mfsrin;
383	mmu->xlate = kvmppc_mmu_book3s_32_xlate;
384	mmu->reset_msr = kvmppc_mmu_book3s_32_reset_msr;
385	mmu->tlbie = kvmppc_mmu_book3s_32_tlbie;
386	mmu->esid_to_vsid = kvmppc_mmu_book3s_32_esid_to_vsid;
387	mmu->ea_to_vp = kvmppc_mmu_book3s_32_ea_to_vp;
388	mmu->is_dcbz32 = kvmppc_mmu_book3s_32_is_dcbz32;
389
390	mmu->slbmte = NULL;
391	mmu->slbmfee = NULL;
392	mmu->slbmfev = NULL;
393	mmu->slbie = NULL;
394	mmu->slbia = NULL;
395}
396