blob: 06a5fcfc4d33363ed51fd81547f58baebbaf510c [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>
27
28#include "44x_tlb.h"
29
30#define PPC44x_TLB_USER_PERM_MASK (PPC44x_TLB_UX|PPC44x_TLB_UR|PPC44x_TLB_UW)
31#define PPC44x_TLB_SUPER_PERM_MASK (PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW)
32
33static unsigned int kvmppc_tlb_44x_pos;
34
35static u32 kvmppc_44x_tlb_shadow_attrib(u32 attrib, int usermode)
36{
37 /* Mask off reserved bits. */
38 attrib &= PPC44x_TLB_PERM_MASK|PPC44x_TLB_ATTR_MASK;
39
40 if (!usermode) {
41 /* Guest is in supervisor mode, so we need to translate guest
42 * supervisor permissions into user permissions. */
43 attrib &= ~PPC44x_TLB_USER_PERM_MASK;
44 attrib |= (attrib & PPC44x_TLB_SUPER_PERM_MASK) << 3;
45 }
46
47 /* Make sure host can always access this memory. */
48 attrib |= PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW;
49
50 return attrib;
51}
52
53/* Search the guest TLB for a matching entry. */
54int kvmppc_44x_tlb_index(struct kvm_vcpu *vcpu, gva_t eaddr, unsigned int pid,
55 unsigned int as)
56{
57 int i;
58
59 /* XXX Replace loop with fancy data structures. */
60 for (i = 0; i < PPC44x_TLB_SIZE; i++) {
61 struct tlbe *tlbe = &vcpu->arch.guest_tlb[i];
62 unsigned int tid;
63
64 if (eaddr < get_tlb_eaddr(tlbe))
65 continue;
66
67 if (eaddr > get_tlb_end(tlbe))
68 continue;
69
70 tid = get_tlb_tid(tlbe);
71 if (tid && (tid != pid))
72 continue;
73
74 if (!get_tlb_v(tlbe))
75 continue;
76
77 if (get_tlb_ts(tlbe) != as)
78 continue;
79
80 return i;
81 }
82
83 return -1;
84}
85
86struct tlbe *kvmppc_44x_itlb_search(struct kvm_vcpu *vcpu, gva_t eaddr)
87{
88 unsigned int as = !!(vcpu->arch.msr & MSR_IS);
89 unsigned int index;
90
91 index = kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
92 if (index == -1)
93 return NULL;
94 return &vcpu->arch.guest_tlb[index];
95}
96
97struct tlbe *kvmppc_44x_dtlb_search(struct kvm_vcpu *vcpu, gva_t eaddr)
98{
99 unsigned int as = !!(vcpu->arch.msr & MSR_DS);
100 unsigned int index;
101
102 index = kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
103 if (index == -1)
104 return NULL;
105 return &vcpu->arch.guest_tlb[index];
106}
107
108static int kvmppc_44x_tlbe_is_writable(struct tlbe *tlbe)
109{
110 return tlbe->word2 & (PPC44x_TLB_SW|PPC44x_TLB_UW);
111}
112
113/* Must be called with mmap_sem locked for writing. */
114static void kvmppc_44x_shadow_release(struct kvm_vcpu *vcpu,
115 unsigned int index)
116{
117 struct tlbe *stlbe = &vcpu->arch.shadow_tlb[index];
118 struct page *page = vcpu->arch.shadow_pages[index];
119
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500120 if (get_tlb_v(stlbe)) {
121 if (kvmppc_44x_tlbe_is_writable(stlbe))
122 kvm_release_page_dirty(page);
123 else
124 kvm_release_page_clean(page);
125 }
126}
127
Hollis Blanchard83aae4a2008-07-25 13:54:52 -0500128void kvmppc_tlbe_set_modified(struct kvm_vcpu *vcpu, unsigned int i)
129{
130 vcpu->arch.shadow_tlb_mod[i] = 1;
131}
132
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500133/* Caller must ensure that the specified guest TLB entry is safe to insert into
134 * the shadow TLB. */
135void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gfn_t gfn, u64 asid,
136 u32 flags)
137{
138 struct page *new_page;
139 struct tlbe *stlbe;
140 hpa_t hpaddr;
141 unsigned int victim;
142
143 /* Future optimization: don't overwrite the TLB entry containing the
144 * current PC (or stack?). */
145 victim = kvmppc_tlb_44x_pos++;
146 if (kvmppc_tlb_44x_pos > tlb_44x_hwater)
147 kvmppc_tlb_44x_pos = 0;
148 stlbe = &vcpu->arch.shadow_tlb[victim];
149
150 /* Get reference to new page. */
Hollis Blanchard905fa4b2008-05-21 18:22:54 -0500151 down_read(&current->mm->mmap_sem);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500152 new_page = gfn_to_page(vcpu->kvm, gfn);
153 if (is_error_page(new_page)) {
Hollis Blanchard9dcb40e2008-05-21 18:22:55 -0500154 printk(KERN_ERR "Couldn't get guest page for gfn %lx!\n", gfn);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500155 kvm_release_page_clean(new_page);
Hollis Blanchard905fa4b2008-05-21 18:22:54 -0500156 up_read(&current->mm->mmap_sem);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500157 return;
158 }
159 hpaddr = page_to_phys(new_page);
160
161 /* Drop reference to old page. */
162 kvmppc_44x_shadow_release(vcpu, victim);
Hollis Blanchard905fa4b2008-05-21 18:22:54 -0500163 up_read(&current->mm->mmap_sem);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500164
165 vcpu->arch.shadow_pages[victim] = new_page;
166
167 /* XXX Make sure (va, size) doesn't overlap any other
168 * entries. 440x6 user manual says the result would be
169 * "undefined." */
170
171 /* XXX what about AS? */
172
173 stlbe->tid = asid & 0xff;
174
175 /* Force TS=1 for all guest mappings. */
176 /* For now we hardcode 4KB mappings, but it will be important to
177 * use host large pages in the future. */
178 stlbe->word0 = (gvaddr & PAGE_MASK) | PPC44x_TLB_VALID | PPC44x_TLB_TS
179 | PPC44x_TLB_4K;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500180 stlbe->word1 = (hpaddr & 0xfffffc00) | ((hpaddr >> 32) & 0xf);
181 stlbe->word2 = kvmppc_44x_tlb_shadow_attrib(flags,
182 vcpu->arch.msr & MSR_PR);
Hollis Blanchard83aae4a2008-07-25 13:54:52 -0500183 kvmppc_tlbe_set_modified(vcpu, victim);
Jerone Young31711f22008-07-14 14:00:03 +0200184
185 KVMTRACE_5D(STLB_WRITE, vcpu, victim,
186 stlbe->tid, stlbe->word0, stlbe->word1, stlbe->word2,
187 handler);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500188}
189
Hollis Blanchardcc044542008-07-25 13:54:50 -0500190void kvmppc_mmu_invalidate(struct kvm_vcpu *vcpu, gva_t eaddr,
191 gva_t eend, u32 asid)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500192{
193 unsigned int pid = asid & 0xff;
194 int i;
195
196 /* XXX Replace loop with fancy data structures. */
197 down_write(&current->mm->mmap_sem);
198 for (i = 0; i <= tlb_44x_hwater; i++) {
199 struct tlbe *stlbe = &vcpu->arch.shadow_tlb[i];
200 unsigned int tid;
201
202 if (!get_tlb_v(stlbe))
203 continue;
204
Hollis Blanchardcc044542008-07-25 13:54:50 -0500205 if (eend < get_tlb_eaddr(stlbe))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500206 continue;
207
208 if (eaddr > get_tlb_end(stlbe))
209 continue;
210
211 tid = get_tlb_tid(stlbe);
212 if (tid && (tid != pid))
213 continue;
214
215 kvmppc_44x_shadow_release(vcpu, i);
216 stlbe->word0 = 0;
Hollis Blanchard83aae4a2008-07-25 13:54:52 -0500217 kvmppc_tlbe_set_modified(vcpu, i);
Jerone Young31711f22008-07-14 14:00:03 +0200218 KVMTRACE_5D(STLB_INVAL, vcpu, i,
219 stlbe->tid, stlbe->word0, stlbe->word1,
220 stlbe->word2, handler);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500221 }
222 up_write(&current->mm->mmap_sem);
223}
224
225/* Invalidate all mappings, so that when they fault back in they will get the
226 * proper permission bits. */
227void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
228{
229 int i;
230
231 /* XXX Replace loop with fancy data structures. */
232 down_write(&current->mm->mmap_sem);
233 for (i = 0; i <= tlb_44x_hwater; i++) {
Jerone Young31711f22008-07-14 14:00:03 +0200234 struct tlbe *stlbe = &vcpu->arch.shadow_tlb[i];
235
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500236 kvmppc_44x_shadow_release(vcpu, i);
Jerone Young31711f22008-07-14 14:00:03 +0200237 stlbe->word0 = 0;
Hollis Blanchard83aae4a2008-07-25 13:54:52 -0500238 kvmppc_tlbe_set_modified(vcpu, i);
Jerone Young31711f22008-07-14 14:00:03 +0200239 KVMTRACE_5D(STLB_INVAL, vcpu, i,
240 stlbe->tid, stlbe->word0, stlbe->word1,
241 stlbe->word2, handler);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500242 }
243 up_write(&current->mm->mmap_sem);
244}