blob: 2f14671e8a1538c1cf0f0b7f792eabe818a47a18 [file] [log] [blame]
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001/*
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 IBM Corp. 2007
16 *
17 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18 */
19
20#include <linux/types.h>
21#include <linux/string.h>
Jerone Young31711f22008-07-14 14:00:03 +020022#include <linux/kvm.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050023#include <linux/kvm_host.h>
24#include <linux/highmem.h>
Hollis Blanchard7924bd42008-12-02 15:51:55 -060025
26#include <asm/tlbflush.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050027#include <asm/mmu-44x.h>
28#include <asm/kvm_ppc.h>
Hollis Blancharddb93f572008-11-05 09:36:18 -060029#include <asm/kvm_44x.h>
Hollis Blanchard73e75b42008-12-02 15:51:57 -060030#include "timing.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050031
32#include "44x_tlb.h"
33
Hollis Blanchard89168612008-12-02 15:51:53 -060034#ifndef PPC44x_TLBE_SIZE
35#define PPC44x_TLBE_SIZE PPC44x_TLB_4K
36#endif
37
38#define PAGE_SIZE_4K (1<<12)
39#define PAGE_MASK_4K (~(PAGE_SIZE_4K - 1))
40
Hollis Blancharddf9b8562008-11-10 14:57:35 -060041#define PPC44x_TLB_UATTR_MASK \
42 (PPC44x_TLB_U0|PPC44x_TLB_U1|PPC44x_TLB_U2|PPC44x_TLB_U3)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050043#define PPC44x_TLB_USER_PERM_MASK (PPC44x_TLB_UX|PPC44x_TLB_UR|PPC44x_TLB_UW)
44#define PPC44x_TLB_SUPER_PERM_MASK (PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW)
45
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -060046#ifdef DEBUG
47void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu)
48{
49 struct kvmppc_44x_tlbe *tlbe;
50 int i;
51
52 printk("vcpu %d TLB dump:\n", vcpu->vcpu_id);
53 printk("| %2s | %3s | %8s | %8s | %8s |\n",
54 "nr", "tid", "word0", "word1", "word2");
55
Hollis Blanchard7924bd42008-12-02 15:51:55 -060056 for (i = 0; i < ARRAY_SIZE(vcpu_44x->guest_tlb); i++) {
Hollis Blancharddb93f572008-11-05 09:36:18 -060057 tlbe = &vcpu_44x->guest_tlb[i];
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -060058 if (tlbe->word0 & PPC44x_TLB_VALID)
59 printk(" G%2d | %02X | %08X | %08X | %08X |\n",
60 i, tlbe->tid, tlbe->word0, tlbe->word1,
61 tlbe->word2);
62 }
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -060063}
64#endif
65
Hollis Blanchard7924bd42008-12-02 15:51:55 -060066static inline void kvmppc_44x_tlbie(unsigned int index)
67{
68 /* 0 <= index < 64, so the V bit is clear and we can use the index as
69 * word0. */
70 asm volatile(
71 "tlbwe %[index], %[index], 0\n"
72 :
73 : [index] "r"(index)
74 );
75}
76
Hollis Blanchardc5fbdff2008-12-02 15:51:56 -060077static inline void kvmppc_44x_tlbre(unsigned int index,
78 struct kvmppc_44x_tlbe *tlbe)
79{
80 asm volatile(
81 "tlbre %[word0], %[index], 0\n"
82 "mfspr %[tid], %[sprn_mmucr]\n"
83 "andi. %[tid], %[tid], 0xff\n"
84 "tlbre %[word1], %[index], 1\n"
85 "tlbre %[word2], %[index], 2\n"
86 : [word0] "=r"(tlbe->word0),
87 [word1] "=r"(tlbe->word1),
88 [word2] "=r"(tlbe->word2),
89 [tid] "=r"(tlbe->tid)
90 : [index] "r"(index),
91 [sprn_mmucr] "i"(SPRN_MMUCR)
92 : "cc"
93 );
94}
95
Hollis Blanchard7924bd42008-12-02 15:51:55 -060096static inline void kvmppc_44x_tlbwe(unsigned int index,
97 struct kvmppc_44x_tlbe *stlbe)
98{
99 unsigned long tmp;
100
101 asm volatile(
102 "mfspr %[tmp], %[sprn_mmucr]\n"
103 "rlwimi %[tmp], %[tid], 0, 0xff\n"
104 "mtspr %[sprn_mmucr], %[tmp]\n"
105 "tlbwe %[word0], %[index], 0\n"
106 "tlbwe %[word1], %[index], 1\n"
107 "tlbwe %[word2], %[index], 2\n"
108 : [tmp] "=&r"(tmp)
109 : [word0] "r"(stlbe->word0),
110 [word1] "r"(stlbe->word1),
111 [word2] "r"(stlbe->word2),
112 [tid] "r"(stlbe->tid),
113 [index] "r"(index),
114 [sprn_mmucr] "i"(SPRN_MMUCR)
115 );
116}
117
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500118static u32 kvmppc_44x_tlb_shadow_attrib(u32 attrib, int usermode)
119{
Hollis Blancharddf9b8562008-11-10 14:57:35 -0600120 /* We only care about the guest's permission and user bits. */
121 attrib &= PPC44x_TLB_PERM_MASK|PPC44x_TLB_UATTR_MASK;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500122
123 if (!usermode) {
124 /* Guest is in supervisor mode, so we need to translate guest
125 * supervisor permissions into user permissions. */
126 attrib &= ~PPC44x_TLB_USER_PERM_MASK;
127 attrib |= (attrib & PPC44x_TLB_SUPER_PERM_MASK) << 3;
128 }
129
130 /* Make sure host can always access this memory. */
131 attrib |= PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW;
132
Hollis Blancharddf9b8562008-11-10 14:57:35 -0600133 /* WIMGE = 0b00100 */
134 attrib |= PPC44x_TLB_M;
135
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500136 return attrib;
137}
138
Hollis Blanchardc5fbdff2008-12-02 15:51:56 -0600139/* Load shadow TLB back into hardware. */
140void kvmppc_44x_tlb_load(struct kvm_vcpu *vcpu)
141{
142 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
143 int i;
144
145 for (i = 0; i <= tlb_44x_hwater; i++) {
146 struct kvmppc_44x_tlbe *stlbe = &vcpu_44x->shadow_tlb[i];
147
148 if (get_tlb_v(stlbe) && get_tlb_ts(stlbe))
149 kvmppc_44x_tlbwe(i, stlbe);
150 }
151}
152
153static void kvmppc_44x_tlbe_set_modified(struct kvmppc_vcpu_44x *vcpu_44x,
154 unsigned int i)
155{
156 vcpu_44x->shadow_tlb_mod[i] = 1;
157}
158
159/* Save hardware TLB to the vcpu, and invalidate all guest mappings. */
160void kvmppc_44x_tlb_put(struct kvm_vcpu *vcpu)
161{
162 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
163 int i;
164
165 for (i = 0; i <= tlb_44x_hwater; i++) {
166 struct kvmppc_44x_tlbe *stlbe = &vcpu_44x->shadow_tlb[i];
167
168 if (vcpu_44x->shadow_tlb_mod[i])
169 kvmppc_44x_tlbre(i, stlbe);
170
171 if (get_tlb_v(stlbe) && get_tlb_ts(stlbe))
172 kvmppc_44x_tlbie(i);
173 }
174}
175
176
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500177/* Search the guest TLB for a matching entry. */
178int kvmppc_44x_tlb_index(struct kvm_vcpu *vcpu, gva_t eaddr, unsigned int pid,
179 unsigned int as)
180{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600181 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500182 int i;
183
184 /* XXX Replace loop with fancy data structures. */
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600185 for (i = 0; i < ARRAY_SIZE(vcpu_44x->guest_tlb); i++) {
Hollis Blancharddb93f572008-11-05 09:36:18 -0600186 struct kvmppc_44x_tlbe *tlbe = &vcpu_44x->guest_tlb[i];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500187 unsigned int tid;
188
189 if (eaddr < get_tlb_eaddr(tlbe))
190 continue;
191
192 if (eaddr > get_tlb_end(tlbe))
193 continue;
194
195 tid = get_tlb_tid(tlbe);
196 if (tid && (tid != pid))
197 continue;
198
199 if (!get_tlb_v(tlbe))
200 continue;
201
202 if (get_tlb_ts(tlbe) != as)
203 continue;
204
205 return i;
206 }
207
208 return -1;
209}
210
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600211gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int gtlb_index,
212 gva_t eaddr)
213{
214 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
215 struct kvmppc_44x_tlbe *gtlbe = &vcpu_44x->guest_tlb[gtlb_index];
216 unsigned int pgmask = get_tlb_bytes(gtlbe) - 1;
217
218 return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
219}
220
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600221int kvmppc_44x_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500222{
223 unsigned int as = !!(vcpu->arch.msr & MSR_IS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500224
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600225 return kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500226}
227
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600228int kvmppc_44x_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500229{
230 unsigned int as = !!(vcpu->arch.msr & MSR_DS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500231
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600232 return kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500233}
234
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600235static void kvmppc_44x_shadow_release(struct kvmppc_vcpu_44x *vcpu_44x,
236 unsigned int stlb_index)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500237{
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600238 struct kvmppc_44x_shadow_ref *ref = &vcpu_44x->shadow_refs[stlb_index];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500239
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600240 if (!ref->page)
241 return;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500242
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600243 /* Discard from the TLB. */
244 /* Note: we could actually invalidate a host mapping, if the host overwrote
245 * this TLB entry since we inserted a guest mapping. */
246 kvmppc_44x_tlbie(stlb_index);
247
248 /* Now release the page. */
249 if (ref->writeable)
250 kvm_release_page_dirty(ref->page);
251 else
252 kvm_release_page_clean(ref->page);
253
254 ref->page = NULL;
255
256 /* XXX set tlb_44x_index to stlb_index? */
257
258 KVMTRACE_1D(STLB_INVAL, &vcpu_44x->vcpu, stlb_index, handler);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500259}
260
Hollis Blanchardecc09812009-01-03 16:22:59 -0600261void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
Hollis Blanchardc30f8a62008-11-24 11:37:38 -0600262{
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600263 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchardc30f8a62008-11-24 11:37:38 -0600264 int i;
265
266 for (i = 0; i <= tlb_44x_hwater; i++)
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600267 kvmppc_44x_shadow_release(vcpu_44x, i);
Hollis Blanchard83aae4a2008-07-25 13:54:52 -0500268}
269
Hollis Blanchard89168612008-12-02 15:51:53 -0600270/**
271 * kvmppc_mmu_map -- create a host mapping for guest memory
272 *
273 * If the guest wanted a larger page than the host supports, only the first
274 * host page is mapped here and the rest are demand faulted.
275 *
276 * If the guest wanted a smaller page than the host page size, we map only the
277 * guest-size page (i.e. not a full host page mapping).
278 *
279 * Caller must ensure that the specified guest TLB entry is safe to insert into
280 * the shadow TLB.
281 */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600282void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr,
283 unsigned int gtlb_index)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500284{
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600285 struct kvmppc_44x_tlbe stlbe;
Hollis Blancharddb93f572008-11-05 09:36:18 -0600286 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchard58a96212009-01-03 16:23:01 -0600287 struct kvmppc_44x_tlbe *gtlbe = &vcpu_44x->guest_tlb[gtlb_index];
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600288 struct kvmppc_44x_shadow_ref *ref;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500289 struct page *new_page;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500290 hpa_t hpaddr;
Hollis Blanchard89168612008-12-02 15:51:53 -0600291 gfn_t gfn;
Hollis Blanchard58a96212009-01-03 16:23:01 -0600292 u32 asid = gtlbe->tid;
293 u32 flags = gtlbe->word2;
294 u32 max_bytes = get_tlb_bytes(gtlbe);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500295 unsigned int victim;
296
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600297 /* Select TLB entry to clobber. Indirectly guard against races with the TLB
298 * miss handler by disabling interrupts. */
299 local_irq_disable();
300 victim = ++tlb_44x_index;
301 if (victim > tlb_44x_hwater)
302 victim = 0;
303 tlb_44x_index = victim;
304 local_irq_enable();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500305
306 /* Get reference to new page. */
Hollis Blanchard89168612008-12-02 15:51:53 -0600307 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500308 new_page = gfn_to_page(vcpu->kvm, gfn);
309 if (is_error_page(new_page)) {
Hollis Blanchard9dcb40e2008-05-21 18:22:55 -0500310 printk(KERN_ERR "Couldn't get guest page for gfn %lx!\n", gfn);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500311 kvm_release_page_clean(new_page);
312 return;
313 }
314 hpaddr = page_to_phys(new_page);
315
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600316 /* Invalidate any previous shadow mappings. */
317 kvmppc_44x_shadow_release(vcpu_44x, victim);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500318
319 /* XXX Make sure (va, size) doesn't overlap any other
320 * entries. 440x6 user manual says the result would be
321 * "undefined." */
322
323 /* XXX what about AS? */
324
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500325 /* Force TS=1 for all guest mappings. */
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600326 stlbe.word0 = PPC44x_TLB_VALID | PPC44x_TLB_TS;
Hollis Blanchard89168612008-12-02 15:51:53 -0600327
328 if (max_bytes >= PAGE_SIZE) {
329 /* Guest mapping is larger than or equal to host page size. We can use
330 * a "native" host mapping. */
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600331 stlbe.word0 |= (gvaddr & PAGE_MASK) | PPC44x_TLBE_SIZE;
Hollis Blanchard89168612008-12-02 15:51:53 -0600332 } else {
333 /* Guest mapping is smaller than host page size. We must restrict the
334 * size of the mapping to be at most the smaller of the two, but for
335 * simplicity we fall back to a 4K mapping (this is probably what the
336 * guest is using anyways). */
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600337 stlbe.word0 |= (gvaddr & PAGE_MASK_4K) | PPC44x_TLB_4K;
Hollis Blanchard89168612008-12-02 15:51:53 -0600338
339 /* 'hpaddr' is a host page, which is larger than the mapping we're
340 * inserting here. To compensate, we must add the in-page offset to the
341 * sub-page. */
342 hpaddr |= gpaddr & (PAGE_MASK ^ PAGE_MASK_4K);
343 }
344
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600345 stlbe.word1 = (hpaddr & 0xfffffc00) | ((hpaddr >> 32) & 0xf);
346 stlbe.word2 = kvmppc_44x_tlb_shadow_attrib(flags,
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500347 vcpu->arch.msr & MSR_PR);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600348 stlbe.tid = !(asid & 0xff);
Jerone Young31711f22008-07-14 14:00:03 +0200349
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600350 /* Keep track of the reference so we can properly release it later. */
351 ref = &vcpu_44x->shadow_refs[victim];
352 ref->page = new_page;
353 ref->gtlb_index = gtlb_index;
354 ref->writeable = !!(stlbe.word2 & PPC44x_TLB_UW);
355 ref->tid = stlbe.tid;
356
357 /* Insert shadow mapping into hardware TLB. */
Hollis Blanchardc5fbdff2008-12-02 15:51:56 -0600358 kvmppc_44x_tlbe_set_modified(vcpu_44x, victim);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600359 kvmppc_44x_tlbwe(victim, &stlbe);
360 KVMTRACE_5D(STLB_WRITE, vcpu, victim, stlbe.tid, stlbe.word0, stlbe.word1,
361 stlbe.word2, handler);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500362}
363
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600364/* For a particular guest TLB entry, invalidate the corresponding host TLB
365 * mappings and release the host pages. */
366static void kvmppc_44x_invalidate(struct kvm_vcpu *vcpu,
367 unsigned int gtlb_index)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500368{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600369 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500370 int i;
371
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600372 for (i = 0; i < ARRAY_SIZE(vcpu_44x->shadow_refs); i++) {
373 struct kvmppc_44x_shadow_ref *ref = &vcpu_44x->shadow_refs[i];
374 if (ref->gtlb_index == gtlb_index)
375 kvmppc_44x_shadow_release(vcpu_44x, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500376 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500377}
378
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500379void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
380{
Hollis Blanchardfe4e7712008-11-10 14:57:36 -0600381 vcpu->arch.shadow_pid = !usermode;
382}
383
384void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 new_pid)
385{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600386 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500387 int i;
388
Hollis Blanchardfe4e7712008-11-10 14:57:36 -0600389 if (unlikely(vcpu->arch.pid == new_pid))
390 return;
Jerone Young31711f22008-07-14 14:00:03 +0200391
Hollis Blanchardfe4e7712008-11-10 14:57:36 -0600392 vcpu->arch.pid = new_pid;
393
394 /* Guest userspace runs with TID=0 mappings and PID=0, to make sure it
395 * can't access guest kernel mappings (TID=1). When we switch to a new
396 * guest PID, which will also use host PID=0, we must discard the old guest
397 * userspace mappings. */
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600398 for (i = 0; i < ARRAY_SIZE(vcpu_44x->shadow_refs); i++) {
399 struct kvmppc_44x_shadow_ref *ref = &vcpu_44x->shadow_refs[i];
Hollis Blanchardfe4e7712008-11-10 14:57:36 -0600400
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600401 if (ref->tid == 0)
402 kvmppc_44x_shadow_release(vcpu_44x, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500403 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500404}
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600405
406static int tlbe_is_host_safe(const struct kvm_vcpu *vcpu,
Hollis Blanchard0f55dc42008-11-05 09:36:12 -0600407 const struct kvmppc_44x_tlbe *tlbe)
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600408{
409 gpa_t gpa;
410
411 if (!get_tlb_v(tlbe))
412 return 0;
413
414 /* Does it match current guest AS? */
415 /* XXX what about IS != DS? */
416 if (get_tlb_ts(tlbe) != !!(vcpu->arch.msr & MSR_IS))
417 return 0;
418
419 gpa = get_tlb_raddr(tlbe);
420 if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT))
421 /* Mapping is not for RAM. */
422 return 0;
423
424 return 1;
425}
426
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600427int kvmppc_44x_emul_tlbwe(struct kvm_vcpu *vcpu, u8 ra, u8 rs, u8 ws)
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600428{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600429 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchard0f55dc42008-11-05 09:36:12 -0600430 struct kvmppc_44x_tlbe *tlbe;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600431 unsigned int gtlb_index;
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600432
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600433 gtlb_index = vcpu->arch.gpr[ra];
434 if (gtlb_index > KVM44x_GUEST_TLB_SIZE) {
435 printk("%s: index %d\n", __func__, gtlb_index);
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600436 kvmppc_dump_vcpu(vcpu);
437 return EMULATE_FAIL;
438 }
439
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600440 tlbe = &vcpu_44x->guest_tlb[gtlb_index];
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600441
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600442 /* Invalidate shadow mappings for the about-to-be-clobbered TLB entry. */
443 if (tlbe->word0 & PPC44x_TLB_VALID)
444 kvmppc_44x_invalidate(vcpu, gtlb_index);
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600445
446 switch (ws) {
447 case PPC44x_TLB_PAGEID:
Hollis Blanchardbf5d4022008-11-10 14:57:34 -0600448 tlbe->tid = get_mmucr_stid(vcpu);
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600449 tlbe->word0 = vcpu->arch.gpr[rs];
450 break;
451
452 case PPC44x_TLB_XLAT:
453 tlbe->word1 = vcpu->arch.gpr[rs];
454 break;
455
456 case PPC44x_TLB_ATTRIB:
457 tlbe->word2 = vcpu->arch.gpr[rs];
458 break;
459
460 default:
461 return EMULATE_FAIL;
462 }
463
464 if (tlbe_is_host_safe(vcpu, tlbe)) {
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600465 gva_t eaddr;
Hollis Blanchard89168612008-12-02 15:51:53 -0600466 gpa_t gpaddr;
Hollis Blanchard89168612008-12-02 15:51:53 -0600467 u32 bytes;
468
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600469 eaddr = get_tlb_eaddr(tlbe);
Hollis Blanchard89168612008-12-02 15:51:53 -0600470 gpaddr = get_tlb_raddr(tlbe);
471
472 /* Use the advertised page size to mask effective and real addrs. */
473 bytes = get_tlb_bytes(tlbe);
474 eaddr &= ~(bytes - 1);
475 gpaddr &= ~(bytes - 1);
476
Hollis Blanchard58a96212009-01-03 16:23:01 -0600477 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600478 }
479
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600480 KVMTRACE_5D(GTLB_WRITE, vcpu, gtlb_index, tlbe->tid, tlbe->word0,
481 tlbe->word1, tlbe->word2, handler);
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600482
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600483 kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600484 return EMULATE_DONE;
485}
486
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600487int kvmppc_44x_emul_tlbsx(struct kvm_vcpu *vcpu, u8 rt, u8 ra, u8 rb, u8 rc)
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600488{
489 u32 ea;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600490 int gtlb_index;
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600491 unsigned int as = get_mmucr_sts(vcpu);
492 unsigned int pid = get_mmucr_stid(vcpu);
493
494 ea = vcpu->arch.gpr[rb];
495 if (ra)
496 ea += vcpu->arch.gpr[ra];
497
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600498 gtlb_index = kvmppc_44x_tlb_index(vcpu, ea, pid, as);
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600499 if (rc) {
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600500 if (gtlb_index < 0)
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600501 vcpu->arch.cr &= ~0x20000000;
502 else
503 vcpu->arch.cr |= 0x20000000;
504 }
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600505 vcpu->arch.gpr[rt] = gtlb_index;
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600506
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600507 kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600508 return EMULATE_DONE;
509}