blob: 2981ebea3d1f49d70674e19f8218feba7728b10c [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 Blanchardbbf45ba2008-04-16 23:28:09 -050030
31#include "44x_tlb.h"
32
Hollis Blanchard89168612008-12-02 15:51:53 -060033#ifndef PPC44x_TLBE_SIZE
34#define PPC44x_TLBE_SIZE PPC44x_TLB_4K
35#endif
36
37#define PAGE_SIZE_4K (1<<12)
38#define PAGE_MASK_4K (~(PAGE_SIZE_4K - 1))
39
Hollis Blancharddf9b8562008-11-10 14:57:35 -060040#define PPC44x_TLB_UATTR_MASK \
41 (PPC44x_TLB_U0|PPC44x_TLB_U1|PPC44x_TLB_U2|PPC44x_TLB_U3)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050042#define PPC44x_TLB_USER_PERM_MASK (PPC44x_TLB_UX|PPC44x_TLB_UR|PPC44x_TLB_UW)
43#define PPC44x_TLB_SUPER_PERM_MASK (PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW)
44
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -060045#ifdef DEBUG
46void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu)
47{
48 struct kvmppc_44x_tlbe *tlbe;
49 int i;
50
51 printk("vcpu %d TLB dump:\n", vcpu->vcpu_id);
52 printk("| %2s | %3s | %8s | %8s | %8s |\n",
53 "nr", "tid", "word0", "word1", "word2");
54
Hollis Blanchard7924bd42008-12-02 15:51:55 -060055 for (i = 0; i < ARRAY_SIZE(vcpu_44x->guest_tlb); i++) {
Hollis Blancharddb93f572008-11-05 09:36:18 -060056 tlbe = &vcpu_44x->guest_tlb[i];
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -060057 if (tlbe->word0 & PPC44x_TLB_VALID)
58 printk(" G%2d | %02X | %08X | %08X | %08X |\n",
59 i, tlbe->tid, tlbe->word0, tlbe->word1,
60 tlbe->word2);
61 }
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -060062}
63#endif
64
Hollis Blanchard7924bd42008-12-02 15:51:55 -060065static inline void kvmppc_44x_tlbie(unsigned int index)
66{
67 /* 0 <= index < 64, so the V bit is clear and we can use the index as
68 * word0. */
69 asm volatile(
70 "tlbwe %[index], %[index], 0\n"
71 :
72 : [index] "r"(index)
73 );
74}
75
76static inline void kvmppc_44x_tlbwe(unsigned int index,
77 struct kvmppc_44x_tlbe *stlbe)
78{
79 unsigned long tmp;
80
81 asm volatile(
82 "mfspr %[tmp], %[sprn_mmucr]\n"
83 "rlwimi %[tmp], %[tid], 0, 0xff\n"
84 "mtspr %[sprn_mmucr], %[tmp]\n"
85 "tlbwe %[word0], %[index], 0\n"
86 "tlbwe %[word1], %[index], 1\n"
87 "tlbwe %[word2], %[index], 2\n"
88 : [tmp] "=&r"(tmp)
89 : [word0] "r"(stlbe->word0),
90 [word1] "r"(stlbe->word1),
91 [word2] "r"(stlbe->word2),
92 [tid] "r"(stlbe->tid),
93 [index] "r"(index),
94 [sprn_mmucr] "i"(SPRN_MMUCR)
95 );
96}
97
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050098static u32 kvmppc_44x_tlb_shadow_attrib(u32 attrib, int usermode)
99{
Hollis Blancharddf9b8562008-11-10 14:57:35 -0600100 /* We only care about the guest's permission and user bits. */
101 attrib &= PPC44x_TLB_PERM_MASK|PPC44x_TLB_UATTR_MASK;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500102
103 if (!usermode) {
104 /* Guest is in supervisor mode, so we need to translate guest
105 * supervisor permissions into user permissions. */
106 attrib &= ~PPC44x_TLB_USER_PERM_MASK;
107 attrib |= (attrib & PPC44x_TLB_SUPER_PERM_MASK) << 3;
108 }
109
110 /* Make sure host can always access this memory. */
111 attrib |= PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW;
112
Hollis Blancharddf9b8562008-11-10 14:57:35 -0600113 /* WIMGE = 0b00100 */
114 attrib |= PPC44x_TLB_M;
115
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500116 return attrib;
117}
118
119/* Search the guest TLB for a matching entry. */
120int kvmppc_44x_tlb_index(struct kvm_vcpu *vcpu, gva_t eaddr, unsigned int pid,
121 unsigned int as)
122{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600123 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500124 int i;
125
126 /* XXX Replace loop with fancy data structures. */
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600127 for (i = 0; i < ARRAY_SIZE(vcpu_44x->guest_tlb); i++) {
Hollis Blancharddb93f572008-11-05 09:36:18 -0600128 struct kvmppc_44x_tlbe *tlbe = &vcpu_44x->guest_tlb[i];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500129 unsigned int tid;
130
131 if (eaddr < get_tlb_eaddr(tlbe))
132 continue;
133
134 if (eaddr > get_tlb_end(tlbe))
135 continue;
136
137 tid = get_tlb_tid(tlbe);
138 if (tid && (tid != pid))
139 continue;
140
141 if (!get_tlb_v(tlbe))
142 continue;
143
144 if (get_tlb_ts(tlbe) != as)
145 continue;
146
147 return i;
148 }
149
150 return -1;
151}
152
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600153int kvmppc_44x_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500154{
155 unsigned int as = !!(vcpu->arch.msr & MSR_IS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500156
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600157 return kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500158}
159
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600160int kvmppc_44x_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500161{
162 unsigned int as = !!(vcpu->arch.msr & MSR_DS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500163
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600164 return kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500165}
166
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600167static void kvmppc_44x_shadow_release(struct kvmppc_vcpu_44x *vcpu_44x,
168 unsigned int stlb_index)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500169{
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600170 struct kvmppc_44x_shadow_ref *ref = &vcpu_44x->shadow_refs[stlb_index];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500171
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600172 if (!ref->page)
173 return;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500174
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600175 /* Discard from the TLB. */
176 /* Note: we could actually invalidate a host mapping, if the host overwrote
177 * this TLB entry since we inserted a guest mapping. */
178 kvmppc_44x_tlbie(stlb_index);
179
180 /* Now release the page. */
181 if (ref->writeable)
182 kvm_release_page_dirty(ref->page);
183 else
184 kvm_release_page_clean(ref->page);
185
186 ref->page = NULL;
187
188 /* XXX set tlb_44x_index to stlb_index? */
189
190 KVMTRACE_1D(STLB_INVAL, &vcpu_44x->vcpu, stlb_index, handler);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500191}
192
Hollis Blanchardc30f8a62008-11-24 11:37:38 -0600193void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu)
194{
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600195 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchardc30f8a62008-11-24 11:37:38 -0600196 int i;
197
198 for (i = 0; i <= tlb_44x_hwater; i++)
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600199 kvmppc_44x_shadow_release(vcpu_44x, i);
Hollis Blanchard83aae4a2008-07-25 13:54:52 -0500200}
201
Hollis Blanchard89168612008-12-02 15:51:53 -0600202/**
203 * kvmppc_mmu_map -- create a host mapping for guest memory
204 *
205 * If the guest wanted a larger page than the host supports, only the first
206 * host page is mapped here and the rest are demand faulted.
207 *
208 * If the guest wanted a smaller page than the host page size, we map only the
209 * guest-size page (i.e. not a full host page mapping).
210 *
211 * Caller must ensure that the specified guest TLB entry is safe to insert into
212 * the shadow TLB.
213 */
214void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr, u64 asid,
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600215 u32 flags, u32 max_bytes, unsigned int gtlb_index)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500216{
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600217 struct kvmppc_44x_tlbe stlbe;
Hollis Blancharddb93f572008-11-05 09:36:18 -0600218 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600219 struct kvmppc_44x_shadow_ref *ref;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500220 struct page *new_page;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500221 hpa_t hpaddr;
Hollis Blanchard89168612008-12-02 15:51:53 -0600222 gfn_t gfn;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500223 unsigned int victim;
224
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600225 /* Select TLB entry to clobber. Indirectly guard against races with the TLB
226 * miss handler by disabling interrupts. */
227 local_irq_disable();
228 victim = ++tlb_44x_index;
229 if (victim > tlb_44x_hwater)
230 victim = 0;
231 tlb_44x_index = victim;
232 local_irq_enable();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500233
234 /* Get reference to new page. */
Hollis Blanchard89168612008-12-02 15:51:53 -0600235 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500236 new_page = gfn_to_page(vcpu->kvm, gfn);
237 if (is_error_page(new_page)) {
Hollis Blanchard9dcb40e2008-05-21 18:22:55 -0500238 printk(KERN_ERR "Couldn't get guest page for gfn %lx!\n", gfn);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500239 kvm_release_page_clean(new_page);
240 return;
241 }
242 hpaddr = page_to_phys(new_page);
243
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600244 /* Invalidate any previous shadow mappings. */
245 kvmppc_44x_shadow_release(vcpu_44x, victim);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500246
247 /* XXX Make sure (va, size) doesn't overlap any other
248 * entries. 440x6 user manual says the result would be
249 * "undefined." */
250
251 /* XXX what about AS? */
252
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500253 /* Force TS=1 for all guest mappings. */
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600254 stlbe.word0 = PPC44x_TLB_VALID | PPC44x_TLB_TS;
Hollis Blanchard89168612008-12-02 15:51:53 -0600255
256 if (max_bytes >= PAGE_SIZE) {
257 /* Guest mapping is larger than or equal to host page size. We can use
258 * a "native" host mapping. */
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600259 stlbe.word0 |= (gvaddr & PAGE_MASK) | PPC44x_TLBE_SIZE;
Hollis Blanchard89168612008-12-02 15:51:53 -0600260 } else {
261 /* Guest mapping is smaller than host page size. We must restrict the
262 * size of the mapping to be at most the smaller of the two, but for
263 * simplicity we fall back to a 4K mapping (this is probably what the
264 * guest is using anyways). */
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600265 stlbe.word0 |= (gvaddr & PAGE_MASK_4K) | PPC44x_TLB_4K;
Hollis Blanchard89168612008-12-02 15:51:53 -0600266
267 /* 'hpaddr' is a host page, which is larger than the mapping we're
268 * inserting here. To compensate, we must add the in-page offset to the
269 * sub-page. */
270 hpaddr |= gpaddr & (PAGE_MASK ^ PAGE_MASK_4K);
271 }
272
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600273 stlbe.word1 = (hpaddr & 0xfffffc00) | ((hpaddr >> 32) & 0xf);
274 stlbe.word2 = kvmppc_44x_tlb_shadow_attrib(flags,
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500275 vcpu->arch.msr & MSR_PR);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600276 stlbe.tid = !(asid & 0xff);
Jerone Young31711f22008-07-14 14:00:03 +0200277
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600278 /* Keep track of the reference so we can properly release it later. */
279 ref = &vcpu_44x->shadow_refs[victim];
280 ref->page = new_page;
281 ref->gtlb_index = gtlb_index;
282 ref->writeable = !!(stlbe.word2 & PPC44x_TLB_UW);
283 ref->tid = stlbe.tid;
284
285 /* Insert shadow mapping into hardware TLB. */
286 kvmppc_44x_tlbwe(victim, &stlbe);
287 KVMTRACE_5D(STLB_WRITE, vcpu, victim, stlbe.tid, stlbe.word0, stlbe.word1,
288 stlbe.word2, handler);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500289}
290
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600291/* For a particular guest TLB entry, invalidate the corresponding host TLB
292 * mappings and release the host pages. */
293static void kvmppc_44x_invalidate(struct kvm_vcpu *vcpu,
294 unsigned int gtlb_index)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500295{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600296 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500297 int i;
298
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600299 for (i = 0; i < ARRAY_SIZE(vcpu_44x->shadow_refs); i++) {
300 struct kvmppc_44x_shadow_ref *ref = &vcpu_44x->shadow_refs[i];
301 if (ref->gtlb_index == gtlb_index)
302 kvmppc_44x_shadow_release(vcpu_44x, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500303 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500304}
305
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500306void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
307{
Hollis Blanchardfe4e7712008-11-10 14:57:36 -0600308 vcpu->arch.shadow_pid = !usermode;
309}
310
311void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 new_pid)
312{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600313 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500314 int i;
315
Hollis Blanchardfe4e7712008-11-10 14:57:36 -0600316 if (unlikely(vcpu->arch.pid == new_pid))
317 return;
Jerone Young31711f22008-07-14 14:00:03 +0200318
Hollis Blanchardfe4e7712008-11-10 14:57:36 -0600319 vcpu->arch.pid = new_pid;
320
321 /* Guest userspace runs with TID=0 mappings and PID=0, to make sure it
322 * can't access guest kernel mappings (TID=1). When we switch to a new
323 * guest PID, which will also use host PID=0, we must discard the old guest
324 * userspace mappings. */
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600325 for (i = 0; i < ARRAY_SIZE(vcpu_44x->shadow_refs); i++) {
326 struct kvmppc_44x_shadow_ref *ref = &vcpu_44x->shadow_refs[i];
Hollis Blanchardfe4e7712008-11-10 14:57:36 -0600327
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600328 if (ref->tid == 0)
329 kvmppc_44x_shadow_release(vcpu_44x, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500330 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500331}
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600332
333static int tlbe_is_host_safe(const struct kvm_vcpu *vcpu,
Hollis Blanchard0f55dc42008-11-05 09:36:12 -0600334 const struct kvmppc_44x_tlbe *tlbe)
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600335{
336 gpa_t gpa;
337
338 if (!get_tlb_v(tlbe))
339 return 0;
340
341 /* Does it match current guest AS? */
342 /* XXX what about IS != DS? */
343 if (get_tlb_ts(tlbe) != !!(vcpu->arch.msr & MSR_IS))
344 return 0;
345
346 gpa = get_tlb_raddr(tlbe);
347 if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT))
348 /* Mapping is not for RAM. */
349 return 0;
350
351 return 1;
352}
353
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600354int kvmppc_44x_emul_tlbwe(struct kvm_vcpu *vcpu, u8 ra, u8 rs, u8 ws)
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600355{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600356 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchard0f55dc42008-11-05 09:36:12 -0600357 struct kvmppc_44x_tlbe *tlbe;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600358 unsigned int gtlb_index;
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600359
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600360 gtlb_index = vcpu->arch.gpr[ra];
361 if (gtlb_index > KVM44x_GUEST_TLB_SIZE) {
362 printk("%s: index %d\n", __func__, gtlb_index);
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600363 kvmppc_dump_vcpu(vcpu);
364 return EMULATE_FAIL;
365 }
366
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600367 tlbe = &vcpu_44x->guest_tlb[gtlb_index];
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600368
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600369 /* Invalidate shadow mappings for the about-to-be-clobbered TLB entry. */
370 if (tlbe->word0 & PPC44x_TLB_VALID)
371 kvmppc_44x_invalidate(vcpu, gtlb_index);
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600372
373 switch (ws) {
374 case PPC44x_TLB_PAGEID:
Hollis Blanchardbf5d4022008-11-10 14:57:34 -0600375 tlbe->tid = get_mmucr_stid(vcpu);
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600376 tlbe->word0 = vcpu->arch.gpr[rs];
377 break;
378
379 case PPC44x_TLB_XLAT:
380 tlbe->word1 = vcpu->arch.gpr[rs];
381 break;
382
383 case PPC44x_TLB_ATTRIB:
384 tlbe->word2 = vcpu->arch.gpr[rs];
385 break;
386
387 default:
388 return EMULATE_FAIL;
389 }
390
391 if (tlbe_is_host_safe(vcpu, tlbe)) {
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600392 u64 asid;
393 gva_t eaddr;
Hollis Blanchard89168612008-12-02 15:51:53 -0600394 gpa_t gpaddr;
395 u32 flags;
396 u32 bytes;
397
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600398 eaddr = get_tlb_eaddr(tlbe);
Hollis Blanchard89168612008-12-02 15:51:53 -0600399 gpaddr = get_tlb_raddr(tlbe);
400
401 /* Use the advertised page size to mask effective and real addrs. */
402 bytes = get_tlb_bytes(tlbe);
403 eaddr &= ~(bytes - 1);
404 gpaddr &= ~(bytes - 1);
405
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600406 asid = (tlbe->word0 & PPC44x_TLB_TS) | tlbe->tid;
407 flags = tlbe->word2 & 0xffff;
408
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600409 kvmppc_mmu_map(vcpu, eaddr, gpaddr, asid, flags, bytes, gtlb_index);
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600410 }
411
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600412 KVMTRACE_5D(GTLB_WRITE, vcpu, gtlb_index, tlbe->tid, tlbe->word0,
413 tlbe->word1, tlbe->word2, handler);
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600414
415 return EMULATE_DONE;
416}
417
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600418int kvmppc_44x_emul_tlbsx(struct kvm_vcpu *vcpu, u8 rt, u8 ra, u8 rb, u8 rc)
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600419{
420 u32 ea;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600421 int gtlb_index;
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600422 unsigned int as = get_mmucr_sts(vcpu);
423 unsigned int pid = get_mmucr_stid(vcpu);
424
425 ea = vcpu->arch.gpr[rb];
426 if (ra)
427 ea += vcpu->arch.gpr[ra];
428
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600429 gtlb_index = kvmppc_44x_tlb_index(vcpu, ea, pid, as);
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600430 if (rc) {
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600431 if (gtlb_index < 0)
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600432 vcpu->arch.cr &= ~0x20000000;
433 else
434 vcpu->arch.cr |= 0x20000000;
435 }
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600436 vcpu->arch.gpr[rt] = gtlb_index;
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600437
438 return EMULATE_DONE;
439}