blob: 8b65fbd6c57d0ae8985dec80ca59898595ffefa8 [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
31#define PPC44x_TLB_USER_PERM_MASK (PPC44x_TLB_UX|PPC44x_TLB_UR|PPC44x_TLB_UW)
32#define PPC44x_TLB_SUPER_PERM_MASK (PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW)
33
34static unsigned int kvmppc_tlb_44x_pos;
35
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -060036#ifdef DEBUG
37void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu)
38{
39 struct kvmppc_44x_tlbe *tlbe;
40 int i;
41
42 printk("vcpu %d TLB dump:\n", vcpu->vcpu_id);
43 printk("| %2s | %3s | %8s | %8s | %8s |\n",
44 "nr", "tid", "word0", "word1", "word2");
45
46 for (i = 0; i < PPC44x_TLB_SIZE; i++) {
Hollis Blancharddb93f572008-11-05 09:36:18 -060047 tlbe = &vcpu_44x->guest_tlb[i];
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -060048 if (tlbe->word0 & PPC44x_TLB_VALID)
49 printk(" G%2d | %02X | %08X | %08X | %08X |\n",
50 i, tlbe->tid, tlbe->word0, tlbe->word1,
51 tlbe->word2);
52 }
53
54 for (i = 0; i < PPC44x_TLB_SIZE; i++) {
Hollis Blancharddb93f572008-11-05 09:36:18 -060055 tlbe = &vcpu_44x->shadow_tlb[i];
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -060056 if (tlbe->word0 & PPC44x_TLB_VALID)
57 printk(" S%2d | %02X | %08X | %08X | %08X |\n",
58 i, tlbe->tid, tlbe->word0, tlbe->word1,
59 tlbe->word2);
60 }
61}
62#endif
63
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050064static u32 kvmppc_44x_tlb_shadow_attrib(u32 attrib, int usermode)
65{
66 /* Mask off reserved bits. */
67 attrib &= PPC44x_TLB_PERM_MASK|PPC44x_TLB_ATTR_MASK;
68
69 if (!usermode) {
70 /* Guest is in supervisor mode, so we need to translate guest
71 * supervisor permissions into user permissions. */
72 attrib &= ~PPC44x_TLB_USER_PERM_MASK;
73 attrib |= (attrib & PPC44x_TLB_SUPER_PERM_MASK) << 3;
74 }
75
76 /* Make sure host can always access this memory. */
77 attrib |= PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW;
78
79 return attrib;
80}
81
82/* Search the guest TLB for a matching entry. */
83int kvmppc_44x_tlb_index(struct kvm_vcpu *vcpu, gva_t eaddr, unsigned int pid,
84 unsigned int as)
85{
Hollis Blancharddb93f572008-11-05 09:36:18 -060086 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050087 int i;
88
89 /* XXX Replace loop with fancy data structures. */
90 for (i = 0; i < PPC44x_TLB_SIZE; i++) {
Hollis Blancharddb93f572008-11-05 09:36:18 -060091 struct kvmppc_44x_tlbe *tlbe = &vcpu_44x->guest_tlb[i];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050092 unsigned int tid;
93
94 if (eaddr < get_tlb_eaddr(tlbe))
95 continue;
96
97 if (eaddr > get_tlb_end(tlbe))
98 continue;
99
100 tid = get_tlb_tid(tlbe);
101 if (tid && (tid != pid))
102 continue;
103
104 if (!get_tlb_v(tlbe))
105 continue;
106
107 if (get_tlb_ts(tlbe) != as)
108 continue;
109
110 return i;
111 }
112
113 return -1;
114}
115
Hollis Blanchard0f55dc42008-11-05 09:36:12 -0600116struct kvmppc_44x_tlbe *kvmppc_44x_itlb_search(struct kvm_vcpu *vcpu,
117 gva_t eaddr)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500118{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600119 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500120 unsigned int as = !!(vcpu->arch.msr & MSR_IS);
121 unsigned int index;
122
123 index = kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
124 if (index == -1)
125 return NULL;
Hollis Blancharddb93f572008-11-05 09:36:18 -0600126 return &vcpu_44x->guest_tlb[index];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500127}
128
Hollis Blanchard0f55dc42008-11-05 09:36:12 -0600129struct kvmppc_44x_tlbe *kvmppc_44x_dtlb_search(struct kvm_vcpu *vcpu,
130 gva_t eaddr)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500131{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600132 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500133 unsigned int as = !!(vcpu->arch.msr & MSR_DS);
134 unsigned int index;
135
136 index = kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
137 if (index == -1)
138 return NULL;
Hollis Blancharddb93f572008-11-05 09:36:18 -0600139 return &vcpu_44x->guest_tlb[index];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500140}
141
Hollis Blanchard0f55dc42008-11-05 09:36:12 -0600142static int kvmppc_44x_tlbe_is_writable(struct kvmppc_44x_tlbe *tlbe)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500143{
144 return tlbe->word2 & (PPC44x_TLB_SW|PPC44x_TLB_UW);
145}
146
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500147static void kvmppc_44x_shadow_release(struct kvm_vcpu *vcpu,
148 unsigned int index)
149{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600150 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
151 struct kvmppc_44x_tlbe *stlbe = &vcpu_44x->shadow_tlb[index];
152 struct page *page = vcpu_44x->shadow_pages[index];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500153
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500154 if (get_tlb_v(stlbe)) {
155 if (kvmppc_44x_tlbe_is_writable(stlbe))
156 kvm_release_page_dirty(page);
157 else
158 kvm_release_page_clean(page);
159 }
160}
161
Hollis Blanchardc30f8a62008-11-24 11:37:38 -0600162void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu)
163{
164 int i;
165
166 for (i = 0; i <= tlb_44x_hwater; i++)
167 kvmppc_44x_shadow_release(vcpu, i);
168}
169
Hollis Blanchard83aae4a2008-07-25 13:54:52 -0500170void kvmppc_tlbe_set_modified(struct kvm_vcpu *vcpu, unsigned int i)
171{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600172 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
173
174 vcpu_44x->shadow_tlb_mod[i] = 1;
Hollis Blanchard83aae4a2008-07-25 13:54:52 -0500175}
176
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500177/* Caller must ensure that the specified guest TLB entry is safe to insert into
178 * the shadow TLB. */
179void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gfn_t gfn, u64 asid,
180 u32 flags)
181{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600182 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500183 struct page *new_page;
Hollis Blanchard0f55dc42008-11-05 09:36:12 -0600184 struct kvmppc_44x_tlbe *stlbe;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500185 hpa_t hpaddr;
186 unsigned int victim;
187
188 /* Future optimization: don't overwrite the TLB entry containing the
189 * current PC (or stack?). */
190 victim = kvmppc_tlb_44x_pos++;
191 if (kvmppc_tlb_44x_pos > tlb_44x_hwater)
192 kvmppc_tlb_44x_pos = 0;
Hollis Blancharddb93f572008-11-05 09:36:18 -0600193 stlbe = &vcpu_44x->shadow_tlb[victim];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500194
195 /* Get reference to new page. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500196 new_page = gfn_to_page(vcpu->kvm, gfn);
197 if (is_error_page(new_page)) {
Hollis Blanchard9dcb40e2008-05-21 18:22:55 -0500198 printk(KERN_ERR "Couldn't get guest page for gfn %lx!\n", gfn);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500199 kvm_release_page_clean(new_page);
200 return;
201 }
202 hpaddr = page_to_phys(new_page);
203
204 /* Drop reference to old page. */
205 kvmppc_44x_shadow_release(vcpu, victim);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500206
Hollis Blancharddb93f572008-11-05 09:36:18 -0600207 vcpu_44x->shadow_pages[victim] = new_page;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500208
209 /* XXX Make sure (va, size) doesn't overlap any other
210 * entries. 440x6 user manual says the result would be
211 * "undefined." */
212
213 /* XXX what about AS? */
214
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500215 stlbe->tid = !(asid & 0xff);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500216
217 /* Force TS=1 for all guest mappings. */
218 /* For now we hardcode 4KB mappings, but it will be important to
219 * use host large pages in the future. */
220 stlbe->word0 = (gvaddr & PAGE_MASK) | PPC44x_TLB_VALID | PPC44x_TLB_TS
221 | PPC44x_TLB_4K;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500222 stlbe->word1 = (hpaddr & 0xfffffc00) | ((hpaddr >> 32) & 0xf);
223 stlbe->word2 = kvmppc_44x_tlb_shadow_attrib(flags,
224 vcpu->arch.msr & MSR_PR);
Hollis Blanchard83aae4a2008-07-25 13:54:52 -0500225 kvmppc_tlbe_set_modified(vcpu, victim);
Jerone Young31711f22008-07-14 14:00:03 +0200226
227 KVMTRACE_5D(STLB_WRITE, vcpu, victim,
228 stlbe->tid, stlbe->word0, stlbe->word1, stlbe->word2,
229 handler);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500230}
231
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600232static void kvmppc_mmu_invalidate(struct kvm_vcpu *vcpu, gva_t eaddr,
233 gva_t eend, u32 asid)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500234{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600235 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500236 unsigned int pid = !(asid & 0xff);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500237 int i;
238
239 /* XXX Replace loop with fancy data structures. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500240 for (i = 0; i <= tlb_44x_hwater; i++) {
Hollis Blancharddb93f572008-11-05 09:36:18 -0600241 struct kvmppc_44x_tlbe *stlbe = &vcpu_44x->shadow_tlb[i];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500242 unsigned int tid;
243
244 if (!get_tlb_v(stlbe))
245 continue;
246
Hollis Blanchardcc044542008-07-25 13:54:50 -0500247 if (eend < get_tlb_eaddr(stlbe))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500248 continue;
249
250 if (eaddr > get_tlb_end(stlbe))
251 continue;
252
253 tid = get_tlb_tid(stlbe);
254 if (tid && (tid != pid))
255 continue;
256
257 kvmppc_44x_shadow_release(vcpu, i);
258 stlbe->word0 = 0;
Hollis Blanchard83aae4a2008-07-25 13:54:52 -0500259 kvmppc_tlbe_set_modified(vcpu, i);
Jerone Young31711f22008-07-14 14:00:03 +0200260 KVMTRACE_5D(STLB_INVAL, vcpu, i,
261 stlbe->tid, stlbe->word0, stlbe->word1,
262 stlbe->word2, handler);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500263 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500264}
265
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500266/* Invalidate all mappings on the privilege switch after PID has been changed.
267 * The guest always runs with PID=1, so we must clear the entire TLB when
268 * switching address spaces. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500269void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
270{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600271 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500272 int i;
273
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500274 if (vcpu->arch.swap_pid) {
275 /* XXX Replace loop with fancy data structures. */
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500276 for (i = 0; i <= tlb_44x_hwater; i++) {
Hollis Blancharddb93f572008-11-05 09:36:18 -0600277 struct kvmppc_44x_tlbe *stlbe = &vcpu_44x->shadow_tlb[i];
Jerone Young31711f22008-07-14 14:00:03 +0200278
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500279 /* Future optimization: clear only userspace mappings. */
280 kvmppc_44x_shadow_release(vcpu, i);
281 stlbe->word0 = 0;
282 kvmppc_tlbe_set_modified(vcpu, i);
283 KVMTRACE_5D(STLB_INVAL, vcpu, i,
284 stlbe->tid, stlbe->word0, stlbe->word1,
285 stlbe->word2, handler);
286 }
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500287 vcpu->arch.swap_pid = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500288 }
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500289
290 vcpu->arch.shadow_pid = !usermode;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500291}
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600292
293static int tlbe_is_host_safe(const struct kvm_vcpu *vcpu,
Hollis Blanchard0f55dc42008-11-05 09:36:12 -0600294 const struct kvmppc_44x_tlbe *tlbe)
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600295{
296 gpa_t gpa;
297
298 if (!get_tlb_v(tlbe))
299 return 0;
300
301 /* Does it match current guest AS? */
302 /* XXX what about IS != DS? */
303 if (get_tlb_ts(tlbe) != !!(vcpu->arch.msr & MSR_IS))
304 return 0;
305
306 gpa = get_tlb_raddr(tlbe);
307 if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT))
308 /* Mapping is not for RAM. */
309 return 0;
310
311 return 1;
312}
313
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600314int kvmppc_44x_emul_tlbwe(struct kvm_vcpu *vcpu, u8 ra, u8 rs, u8 ws)
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600315{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600316 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600317 u64 eaddr;
318 u64 raddr;
319 u64 asid;
320 u32 flags;
Hollis Blanchard0f55dc42008-11-05 09:36:12 -0600321 struct kvmppc_44x_tlbe *tlbe;
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600322 unsigned int index;
323
324 index = vcpu->arch.gpr[ra];
325 if (index > PPC44x_TLB_SIZE) {
326 printk("%s: index %d\n", __func__, index);
327 kvmppc_dump_vcpu(vcpu);
328 return EMULATE_FAIL;
329 }
330
Hollis Blancharddb93f572008-11-05 09:36:18 -0600331 tlbe = &vcpu_44x->guest_tlb[index];
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600332
333 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
334 if (tlbe->word0 & PPC44x_TLB_VALID) {
335 eaddr = get_tlb_eaddr(tlbe);
336 asid = (tlbe->word0 & PPC44x_TLB_TS) | tlbe->tid;
337 kvmppc_mmu_invalidate(vcpu, eaddr, get_tlb_end(tlbe), asid);
338 }
339
340 switch (ws) {
341 case PPC44x_TLB_PAGEID:
342 tlbe->tid = vcpu->arch.mmucr & 0xff;
343 tlbe->word0 = vcpu->arch.gpr[rs];
344 break;
345
346 case PPC44x_TLB_XLAT:
347 tlbe->word1 = vcpu->arch.gpr[rs];
348 break;
349
350 case PPC44x_TLB_ATTRIB:
351 tlbe->word2 = vcpu->arch.gpr[rs];
352 break;
353
354 default:
355 return EMULATE_FAIL;
356 }
357
358 if (tlbe_is_host_safe(vcpu, tlbe)) {
359 eaddr = get_tlb_eaddr(tlbe);
360 raddr = get_tlb_raddr(tlbe);
361 asid = (tlbe->word0 & PPC44x_TLB_TS) | tlbe->tid;
362 flags = tlbe->word2 & 0xffff;
363
364 /* Create a 4KB mapping on the host. If the guest wanted a
365 * large page, only the first 4KB is mapped here and the rest
366 * are mapped on the fly. */
367 kvmppc_mmu_map(vcpu, eaddr, raddr >> PAGE_SHIFT, asid, flags);
368 }
369
370 KVMTRACE_5D(GTLB_WRITE, vcpu, index,
371 tlbe->tid, tlbe->word0, tlbe->word1, tlbe->word2,
372 handler);
373
374 return EMULATE_DONE;
375}
376
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600377int kvmppc_44x_emul_tlbsx(struct kvm_vcpu *vcpu, u8 rt, u8 ra, u8 rb, u8 rc)
Hollis Blancharda0d7b9f2008-11-05 09:36:11 -0600378{
379 u32 ea;
380 int index;
381 unsigned int as = get_mmucr_sts(vcpu);
382 unsigned int pid = get_mmucr_stid(vcpu);
383
384 ea = vcpu->arch.gpr[rb];
385 if (ra)
386 ea += vcpu->arch.gpr[ra];
387
388 index = kvmppc_44x_tlb_index(vcpu, ea, pid, as);
389 if (rc) {
390 if (index < 0)
391 vcpu->arch.cr &= ~0x20000000;
392 else
393 vcpu->arch.cr |= 0x20000000;
394 }
395 vcpu->arch.gpr[rt] = index;
396
397 return EMULATE_DONE;
398}