blob: 6fadbd696021d7dc59c4d1343272dd876bb97dce [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>
25#include <asm/mmu-44x.h>
26#include <asm/kvm_ppc.h>
Hollis Blancharddb93f572008-11-05 09:36:18 -060027#include <asm/kvm_44x.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050028
29#include "44x_tlb.h"
30
Hollis Blancharddf9b8562008-11-10 14:57:35 -060031#define PPC44x_TLB_UATTR_MASK \
32 (PPC44x_TLB_U0|PPC44x_TLB_U1|PPC44x_TLB_U2|PPC44x_TLB_U3)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050033#define PPC44x_TLB_USER_PERM_MASK (PPC44x_TLB_UX|PPC44x_TLB_UR|PPC44x_TLB_UW)
34#define PPC44x_TLB_SUPER_PERM_MASK (PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW)
35
36static unsigned int kvmppc_tlb_44x_pos;
37
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -060038#ifdef DEBUG
39void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu)
40{
41 struct kvmppc_44x_tlbe *tlbe;
42 int i;
43
44 printk("vcpu %d TLB dump:\n", vcpu->vcpu_id);
45 printk("| %2s | %3s | %8s | %8s | %8s |\n",
46 "nr", "tid", "word0", "word1", "word2");
47
48 for (i = 0; i < PPC44x_TLB_SIZE; i++) {
Hollis Blancharddb93f572008-11-05 09:36:18 -060049 tlbe = &vcpu_44x->guest_tlb[i];
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -060050 if (tlbe->word0 & PPC44x_TLB_VALID)
51 printk(" G%2d | %02X | %08X | %08X | %08X |\n",
52 i, tlbe->tid, tlbe->word0, tlbe->word1,
53 tlbe->word2);
54 }
55
56 for (i = 0; i < PPC44x_TLB_SIZE; i++) {
Hollis Blancharddb93f572008-11-05 09:36:18 -060057 tlbe = &vcpu_44x->shadow_tlb[i];
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -060058 if (tlbe->word0 & PPC44x_TLB_VALID)
59 printk(" S%2d | %02X | %08X | %08X | %08X |\n",
60 i, tlbe->tid, tlbe->word0, tlbe->word1,
61 tlbe->word2);
62 }
63}
64#endif
65
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050066static u32 kvmppc_44x_tlb_shadow_attrib(u32 attrib, int usermode)
67{
Hollis Blancharddf9b8562008-11-10 14:57:35 -060068 /* We only care about the guest's permission and user bits. */
69 attrib &= PPC44x_TLB_PERM_MASK|PPC44x_TLB_UATTR_MASK;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050070
71 if (!usermode) {
72 /* Guest is in supervisor mode, so we need to translate guest
73 * supervisor permissions into user permissions. */
74 attrib &= ~PPC44x_TLB_USER_PERM_MASK;
75 attrib |= (attrib & PPC44x_TLB_SUPER_PERM_MASK) << 3;
76 }
77
78 /* Make sure host can always access this memory. */
79 attrib |= PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW;
80
Hollis Blancharddf9b8562008-11-10 14:57:35 -060081 /* WIMGE = 0b00100 */
82 attrib |= PPC44x_TLB_M;
83
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050084 return attrib;
85}
86
87/* Search the guest TLB for a matching entry. */
88int kvmppc_44x_tlb_index(struct kvm_vcpu *vcpu, gva_t eaddr, unsigned int pid,
89 unsigned int as)
90{
Hollis Blancharddb93f572008-11-05 09:36:18 -060091 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050092 int i;
93
94 /* XXX Replace loop with fancy data structures. */
95 for (i = 0; i < PPC44x_TLB_SIZE; i++) {
Hollis Blancharddb93f572008-11-05 09:36:18 -060096 struct kvmppc_44x_tlbe *tlbe = &vcpu_44x->guest_tlb[i];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050097 unsigned int tid;
98
99 if (eaddr < get_tlb_eaddr(tlbe))
100 continue;
101
102 if (eaddr > get_tlb_end(tlbe))
103 continue;
104
105 tid = get_tlb_tid(tlbe);
106 if (tid && (tid != pid))
107 continue;
108
109 if (!get_tlb_v(tlbe))
110 continue;
111
112 if (get_tlb_ts(tlbe) != as)
113 continue;
114
115 return i;
116 }
117
118 return -1;
119}
120
Hollis Blanchard0f55dc42008-11-05 09:36:12 -0600121struct kvmppc_44x_tlbe *kvmppc_44x_itlb_search(struct kvm_vcpu *vcpu,
122 gva_t eaddr)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500123{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600124 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500125 unsigned int as = !!(vcpu->arch.msr & MSR_IS);
126 unsigned int index;
127
128 index = kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
129 if (index == -1)
130 return NULL;
Hollis Blancharddb93f572008-11-05 09:36:18 -0600131 return &vcpu_44x->guest_tlb[index];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500132}
133
Hollis Blanchard0f55dc42008-11-05 09:36:12 -0600134struct kvmppc_44x_tlbe *kvmppc_44x_dtlb_search(struct kvm_vcpu *vcpu,
135 gva_t eaddr)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500136{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600137 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500138 unsigned int as = !!(vcpu->arch.msr & MSR_DS);
139 unsigned int index;
140
141 index = kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
142 if (index == -1)
143 return NULL;
Hollis Blancharddb93f572008-11-05 09:36:18 -0600144 return &vcpu_44x->guest_tlb[index];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500145}
146
Hollis Blanchard0f55dc42008-11-05 09:36:12 -0600147static int kvmppc_44x_tlbe_is_writable(struct kvmppc_44x_tlbe *tlbe)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500148{
149 return tlbe->word2 & (PPC44x_TLB_SW|PPC44x_TLB_UW);
150}
151
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500152static void kvmppc_44x_shadow_release(struct kvm_vcpu *vcpu,
153 unsigned int index)
154{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600155 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
156 struct kvmppc_44x_tlbe *stlbe = &vcpu_44x->shadow_tlb[index];
157 struct page *page = vcpu_44x->shadow_pages[index];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500158
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500159 if (get_tlb_v(stlbe)) {
160 if (kvmppc_44x_tlbe_is_writable(stlbe))
161 kvm_release_page_dirty(page);
162 else
163 kvm_release_page_clean(page);
164 }
165}
166
Hollis Blanchardc30f8a62008-11-24 11:37:38 -0600167void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu)
168{
169 int i;
170
171 for (i = 0; i <= tlb_44x_hwater; i++)
172 kvmppc_44x_shadow_release(vcpu, i);
173}
174
Hollis Blanchard83aae4a2008-07-25 13:54:52 -0500175void kvmppc_tlbe_set_modified(struct kvm_vcpu *vcpu, unsigned int i)
176{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600177 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
178
179 vcpu_44x->shadow_tlb_mod[i] = 1;
Hollis Blanchard83aae4a2008-07-25 13:54:52 -0500180}
181
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500182/* Caller must ensure that the specified guest TLB entry is safe to insert into
183 * the shadow TLB. */
184void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gfn_t gfn, u64 asid,
185 u32 flags)
186{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600187 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500188 struct page *new_page;
Hollis Blanchard0f55dc42008-11-05 09:36:12 -0600189 struct kvmppc_44x_tlbe *stlbe;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500190 hpa_t hpaddr;
191 unsigned int victim;
192
193 /* Future optimization: don't overwrite the TLB entry containing the
194 * current PC (or stack?). */
195 victim = kvmppc_tlb_44x_pos++;
196 if (kvmppc_tlb_44x_pos > tlb_44x_hwater)
197 kvmppc_tlb_44x_pos = 0;
Hollis Blancharddb93f572008-11-05 09:36:18 -0600198 stlbe = &vcpu_44x->shadow_tlb[victim];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500199
200 /* Get reference to new page. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500201 new_page = gfn_to_page(vcpu->kvm, gfn);
202 if (is_error_page(new_page)) {
Hollis Blanchard9dcb40e2008-05-21 18:22:55 -0500203 printk(KERN_ERR "Couldn't get guest page for gfn %lx!\n", gfn);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500204 kvm_release_page_clean(new_page);
205 return;
206 }
207 hpaddr = page_to_phys(new_page);
208
209 /* Drop reference to old page. */
210 kvmppc_44x_shadow_release(vcpu, victim);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500211
Hollis Blancharddb93f572008-11-05 09:36:18 -0600212 vcpu_44x->shadow_pages[victim] = new_page;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500213
214 /* XXX Make sure (va, size) doesn't overlap any other
215 * entries. 440x6 user manual says the result would be
216 * "undefined." */
217
218 /* XXX what about AS? */
219
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500220 stlbe->tid = !(asid & 0xff);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500221
222 /* Force TS=1 for all guest mappings. */
223 /* For now we hardcode 4KB mappings, but it will be important to
224 * use host large pages in the future. */
225 stlbe->word0 = (gvaddr & PAGE_MASK) | PPC44x_TLB_VALID | PPC44x_TLB_TS
226 | PPC44x_TLB_4K;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500227 stlbe->word1 = (hpaddr & 0xfffffc00) | ((hpaddr >> 32) & 0xf);
228 stlbe->word2 = kvmppc_44x_tlb_shadow_attrib(flags,
229 vcpu->arch.msr & MSR_PR);
Hollis Blanchard83aae4a2008-07-25 13:54:52 -0500230 kvmppc_tlbe_set_modified(vcpu, victim);
Jerone Young31711f22008-07-14 14:00:03 +0200231
232 KVMTRACE_5D(STLB_WRITE, vcpu, victim,
233 stlbe->tid, stlbe->word0, stlbe->word1, stlbe->word2,
234 handler);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500235}
236
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600237static void kvmppc_mmu_invalidate(struct kvm_vcpu *vcpu, gva_t eaddr,
238 gva_t eend, u32 asid)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500239{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600240 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500241 unsigned int pid = !(asid & 0xff);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500242 int i;
243
244 /* XXX Replace loop with fancy data structures. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500245 for (i = 0; i <= tlb_44x_hwater; i++) {
Hollis Blancharddb93f572008-11-05 09:36:18 -0600246 struct kvmppc_44x_tlbe *stlbe = &vcpu_44x->shadow_tlb[i];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500247 unsigned int tid;
248
249 if (!get_tlb_v(stlbe))
250 continue;
251
Hollis Blanchardcc044542008-07-25 13:54:50 -0500252 if (eend < get_tlb_eaddr(stlbe))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500253 continue;
254
255 if (eaddr > get_tlb_end(stlbe))
256 continue;
257
258 tid = get_tlb_tid(stlbe);
259 if (tid && (tid != pid))
260 continue;
261
262 kvmppc_44x_shadow_release(vcpu, i);
263 stlbe->word0 = 0;
Hollis Blanchard83aae4a2008-07-25 13:54:52 -0500264 kvmppc_tlbe_set_modified(vcpu, i);
Jerone Young31711f22008-07-14 14:00:03 +0200265 KVMTRACE_5D(STLB_INVAL, vcpu, i,
266 stlbe->tid, stlbe->word0, stlbe->word1,
267 stlbe->word2, handler);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500268 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500269}
270
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500271/* Invalidate all mappings on the privilege switch after PID has been changed.
272 * The guest always runs with PID=1, so we must clear the entire TLB when
273 * switching address spaces. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500274void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
275{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600276 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500277 int i;
278
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500279 if (vcpu->arch.swap_pid) {
280 /* XXX Replace loop with fancy data structures. */
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500281 for (i = 0; i <= tlb_44x_hwater; i++) {
Hollis Blancharddb93f572008-11-05 09:36:18 -0600282 struct kvmppc_44x_tlbe *stlbe = &vcpu_44x->shadow_tlb[i];
Jerone Young31711f22008-07-14 14:00:03 +0200283
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500284 /* Future optimization: clear only userspace mappings. */
285 kvmppc_44x_shadow_release(vcpu, i);
286 stlbe->word0 = 0;
287 kvmppc_tlbe_set_modified(vcpu, i);
288 KVMTRACE_5D(STLB_INVAL, vcpu, i,
289 stlbe->tid, stlbe->word0, stlbe->word1,
290 stlbe->word2, handler);
291 }
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500292 vcpu->arch.swap_pid = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500293 }
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500294
295 vcpu->arch.shadow_pid = !usermode;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500296}
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600297
298static int tlbe_is_host_safe(const struct kvm_vcpu *vcpu,
Hollis Blanchard0f55dc42008-11-05 09:36:12 -0600299 const struct kvmppc_44x_tlbe *tlbe)
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600300{
301 gpa_t gpa;
302
303 if (!get_tlb_v(tlbe))
304 return 0;
305
306 /* Does it match current guest AS? */
307 /* XXX what about IS != DS? */
308 if (get_tlb_ts(tlbe) != !!(vcpu->arch.msr & MSR_IS))
309 return 0;
310
311 gpa = get_tlb_raddr(tlbe);
312 if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT))
313 /* Mapping is not for RAM. */
314 return 0;
315
316 return 1;
317}
318
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600319int kvmppc_44x_emul_tlbwe(struct kvm_vcpu *vcpu, u8 ra, u8 rs, u8 ws)
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600320{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600321 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600322 u64 eaddr;
323 u64 raddr;
324 u64 asid;
325 u32 flags;
Hollis Blanchard0f55dc42008-11-05 09:36:12 -0600326 struct kvmppc_44x_tlbe *tlbe;
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600327 unsigned int index;
328
329 index = vcpu->arch.gpr[ra];
330 if (index > PPC44x_TLB_SIZE) {
331 printk("%s: index %d\n", __func__, index);
332 kvmppc_dump_vcpu(vcpu);
333 return EMULATE_FAIL;
334 }
335
Hollis Blancharddb93f572008-11-05 09:36:18 -0600336 tlbe = &vcpu_44x->guest_tlb[index];
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600337
338 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
339 if (tlbe->word0 & PPC44x_TLB_VALID) {
340 eaddr = get_tlb_eaddr(tlbe);
341 asid = (tlbe->word0 & PPC44x_TLB_TS) | tlbe->tid;
342 kvmppc_mmu_invalidate(vcpu, eaddr, get_tlb_end(tlbe), asid);
343 }
344
345 switch (ws) {
346 case PPC44x_TLB_PAGEID:
Hollis Blanchardbf5d4022008-11-10 14:57:34 -0600347 tlbe->tid = get_mmucr_stid(vcpu);
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600348 tlbe->word0 = vcpu->arch.gpr[rs];
349 break;
350
351 case PPC44x_TLB_XLAT:
352 tlbe->word1 = vcpu->arch.gpr[rs];
353 break;
354
355 case PPC44x_TLB_ATTRIB:
356 tlbe->word2 = vcpu->arch.gpr[rs];
357 break;
358
359 default:
360 return EMULATE_FAIL;
361 }
362
363 if (tlbe_is_host_safe(vcpu, tlbe)) {
364 eaddr = get_tlb_eaddr(tlbe);
365 raddr = get_tlb_raddr(tlbe);
366 asid = (tlbe->word0 & PPC44x_TLB_TS) | tlbe->tid;
367 flags = tlbe->word2 & 0xffff;
368
369 /* Create a 4KB mapping on the host. If the guest wanted a
370 * large page, only the first 4KB is mapped here and the rest
371 * are mapped on the fly. */
372 kvmppc_mmu_map(vcpu, eaddr, raddr >> PAGE_SHIFT, asid, flags);
373 }
374
375 KVMTRACE_5D(GTLB_WRITE, vcpu, index,
376 tlbe->tid, tlbe->word0, tlbe->word1, tlbe->word2,
377 handler);
378
379 return EMULATE_DONE;
380}
381
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600382int kvmppc_44x_emul_tlbsx(struct kvm_vcpu *vcpu, u8 rt, u8 ra, u8 rb, u8 rc)
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600383{
384 u32 ea;
385 int index;
386 unsigned int as = get_mmucr_sts(vcpu);
387 unsigned int pid = get_mmucr_stid(vcpu);
388
389 ea = vcpu->arch.gpr[rb];
390 if (ra)
391 ea += vcpu->arch.gpr[ra];
392
393 index = kvmppc_44x_tlb_index(vcpu, ea, pid, as);
394 if (rc) {
395 if (index < 0)
396 vcpu->arch.cr &= ~0x20000000;
397 else
398 vcpu->arch.cr |= 0x20000000;
399 }
400 vcpu->arch.gpr[rt] = index;
401
402 return EMULATE_DONE;
403}