Commit | Line | Data |
---|---|---|
54738c09 DG |
1 | /* |
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 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> | |
16 | * Copyright 2011 David Gibson, IBM Corporation <dwg@au1.ibm.com> | |
d3695aa4 | 17 | * Copyright 2016 Alexey Kardashevskiy, IBM Corporation <aik@au1.ibm.com> |
54738c09 DG |
18 | */ |
19 | ||
20 | #include <linux/types.h> | |
21 | #include <linux/string.h> | |
22 | #include <linux/kvm.h> | |
23 | #include <linux/kvm_host.h> | |
24 | #include <linux/highmem.h> | |
25 | #include <linux/gfp.h> | |
26 | #include <linux/slab.h> | |
27 | #include <linux/hugetlb.h> | |
28 | #include <linux/list.h> | |
29 | ||
30 | #include <asm/tlbflush.h> | |
31 | #include <asm/kvm_ppc.h> | |
32 | #include <asm/kvm_book3s.h> | |
f64e8084 | 33 | #include <asm/book3s/64/mmu-hash.h> |
d3695aa4 | 34 | #include <asm/mmu_context.h> |
54738c09 DG |
35 | #include <asm/hvcall.h> |
36 | #include <asm/synch.h> | |
37 | #include <asm/ppc-opcode.h> | |
38 | #include <asm/kvm_host.h> | |
39 | #include <asm/udbg.h> | |
fcbb2ce6 | 40 | #include <asm/iommu.h> |
5ee7af18 | 41 | #include <asm/tce.h> |
54738c09 DG |
42 | |
43 | #define TCES_PER_PAGE (PAGE_SIZE / sizeof(u64)) | |
44 | ||
fcbb2ce6 AK |
45 | /* |
46 | * Finds a TCE table descriptor by LIOBN. | |
47 | * | |
48 | * WARNING: This will be called in real or virtual mode on HV KVM and virtual | |
49 | * mode on PR KVM | |
50 | */ | |
d3695aa4 | 51 | struct kvmppc_spapr_tce_table *kvmppc_find_table(struct kvm_vcpu *vcpu, |
fcbb2ce6 AK |
52 | unsigned long liobn) |
53 | { | |
54 | struct kvm *kvm = vcpu->kvm; | |
55 | struct kvmppc_spapr_tce_table *stt; | |
56 | ||
366baf28 | 57 | list_for_each_entry_lockless(stt, &kvm->arch.spapr_tce_tables, list) |
fcbb2ce6 AK |
58 | if (stt->liobn == liobn) |
59 | return stt; | |
60 | ||
61 | return NULL; | |
62 | } | |
d3695aa4 | 63 | EXPORT_SYMBOL_GPL(kvmppc_find_table); |
fcbb2ce6 AK |
64 | |
65 | /* | |
66 | * Validates IO address. | |
67 | * | |
68 | * WARNING: This will be called in real-mode on HV KVM and virtual | |
69 | * mode on PR KVM | |
70 | */ | |
5ee7af18 | 71 | long kvmppc_ioba_validate(struct kvmppc_spapr_tce_table *stt, |
fcbb2ce6 AK |
72 | unsigned long ioba, unsigned long npages) |
73 | { | |
fe26e527 AK |
74 | unsigned long mask = (1ULL << stt->page_shift) - 1; |
75 | unsigned long idx = ioba >> stt->page_shift; | |
fcbb2ce6 | 76 | |
14f853f1 AK |
77 | if ((ioba & mask) || (idx < stt->offset) || |
78 | (idx - stt->offset + npages > stt->size) || | |
79 | (idx + npages < idx)) | |
fcbb2ce6 AK |
80 | return H_PARAMETER; |
81 | ||
82 | return H_SUCCESS; | |
83 | } | |
5ee7af18 AK |
84 | EXPORT_SYMBOL_GPL(kvmppc_ioba_validate); |
85 | ||
86 | /* | |
87 | * Validates TCE address. | |
88 | * At the moment flags and page mask are validated. | |
89 | * As the host kernel does not access those addresses (just puts them | |
90 | * to the table and user space is supposed to process them), we can skip | |
91 | * checking other things (such as TCE is a guest RAM address or the page | |
92 | * was actually allocated). | |
93 | * | |
94 | * WARNING: This will be called in real-mode on HV KVM and virtual | |
95 | * mode on PR KVM | |
96 | */ | |
97 | long kvmppc_tce_validate(struct kvmppc_spapr_tce_table *stt, unsigned long tce) | |
98 | { | |
fe26e527 AK |
99 | unsigned long page_mask = ~((1ULL << stt->page_shift) - 1); |
100 | unsigned long mask = ~(page_mask | TCE_PCI_WRITE | TCE_PCI_READ); | |
5ee7af18 AK |
101 | |
102 | if (tce & mask) | |
103 | return H_PARAMETER; | |
104 | ||
105 | return H_SUCCESS; | |
106 | } | |
107 | EXPORT_SYMBOL_GPL(kvmppc_tce_validate); | |
108 | ||
109 | /* Note on the use of page_address() in real mode, | |
110 | * | |
111 | * It is safe to use page_address() in real mode on ppc64 because | |
112 | * page_address() is always defined as lowmem_page_address() | |
113 | * which returns __va(PFN_PHYS(page_to_pfn(page))) which is arithmetic | |
114 | * operation and does not access page struct. | |
115 | * | |
116 | * Theoretically page_address() could be defined different | |
117 | * but either WANT_PAGE_VIRTUAL or HASHED_PAGE_VIRTUAL | |
118 | * would have to be enabled. | |
119 | * WANT_PAGE_VIRTUAL is never enabled on ppc32/ppc64, | |
120 | * HASHED_PAGE_VIRTUAL could be enabled for ppc32 only and only | |
121 | * if CONFIG_HIGHMEM is defined. As CONFIG_SPARSEMEM_VMEMMAP | |
122 | * is not expected to be enabled on ppc32, page_address() | |
123 | * is safe for ppc32 as well. | |
124 | * | |
125 | * WARNING: This will be called in real-mode on HV KVM and virtual | |
126 | * mode on PR KVM | |
127 | */ | |
128 | static u64 *kvmppc_page_address(struct page *page) | |
129 | { | |
130 | #if defined(HASHED_PAGE_VIRTUAL) || defined(WANT_PAGE_VIRTUAL) | |
131 | #error TODO: fix to avoid page_address() here | |
132 | #endif | |
133 | return (u64 *) page_address(page); | |
134 | } | |
135 | ||
136 | /* | |
137 | * Handles TCE requests for emulated devices. | |
138 | * Puts guest TCE values to the table and expects user space to convert them. | |
139 | * Called in both real and virtual modes. | |
140 | * Cannot fail so kvmppc_tce_validate must be called before it. | |
141 | * | |
142 | * WARNING: This will be called in real-mode on HV KVM and virtual | |
143 | * mode on PR KVM | |
144 | */ | |
145 | void kvmppc_tce_put(struct kvmppc_spapr_tce_table *stt, | |
146 | unsigned long idx, unsigned long tce) | |
147 | { | |
148 | struct page *page; | |
149 | u64 *tbl; | |
150 | ||
14f853f1 | 151 | idx -= stt->offset; |
5ee7af18 AK |
152 | page = stt->pages[idx / TCES_PER_PAGE]; |
153 | tbl = kvmppc_page_address(page); | |
154 | ||
155 | tbl[idx % TCES_PER_PAGE] = tce; | |
156 | } | |
157 | EXPORT_SYMBOL_GPL(kvmppc_tce_put); | |
fcbb2ce6 | 158 | |
d3695aa4 AK |
159 | long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa, |
160 | unsigned long *ua, unsigned long **prmap) | |
161 | { | |
162 | unsigned long gfn = gpa >> PAGE_SHIFT; | |
163 | struct kvm_memory_slot *memslot; | |
164 | ||
165 | memslot = search_memslots(kvm_memslots(kvm), gfn); | |
166 | if (!memslot) | |
167 | return -EINVAL; | |
168 | ||
169 | *ua = __gfn_to_hva_memslot(memslot, gfn) | | |
170 | (gpa & ~(PAGE_MASK | TCE_PCI_READ | TCE_PCI_WRITE)); | |
171 | ||
172 | #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE | |
173 | if (prmap) | |
174 | *prmap = &memslot->arch.rmap[gfn - memslot->base_gfn]; | |
175 | #endif | |
176 | ||
177 | return 0; | |
178 | } | |
179 | EXPORT_SYMBOL_GPL(kvmppc_gpa_to_ua); | |
180 | ||
181 | #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE | |
31217db7 AK |
182 | long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn, |
183 | unsigned long ioba, unsigned long tce) | |
54738c09 | 184 | { |
fcbb2ce6 AK |
185 | struct kvmppc_spapr_tce_table *stt = kvmppc_find_table(vcpu, liobn); |
186 | long ret; | |
54738c09 DG |
187 | |
188 | /* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */ | |
189 | /* liobn, ioba, tce); */ | |
190 | ||
fcbb2ce6 AK |
191 | if (!stt) |
192 | return H_TOO_HARD; | |
193 | ||
194 | ret = kvmppc_ioba_validate(stt, ioba, 1); | |
195 | if (ret != H_SUCCESS) | |
196 | return ret; | |
197 | ||
5ee7af18 AK |
198 | ret = kvmppc_tce_validate(stt, tce); |
199 | if (ret != H_SUCCESS) | |
200 | return ret; | |
fcbb2ce6 | 201 | |
fe26e527 | 202 | kvmppc_tce_put(stt, ioba >> stt->page_shift, tce); |
fcbb2ce6 AK |
203 | |
204 | return H_SUCCESS; | |
54738c09 | 205 | } |
69e9fbb2 | 206 | |
d3695aa4 AK |
207 | static long kvmppc_rm_ua_to_hpa(struct kvm_vcpu *vcpu, |
208 | unsigned long ua, unsigned long *phpa) | |
209 | { | |
210 | pte_t *ptep, pte; | |
211 | unsigned shift = 0; | |
212 | ||
213 | ptep = __find_linux_pte_or_hugepte(vcpu->arch.pgdir, ua, NULL, &shift); | |
214 | if (!ptep || !pte_present(*ptep)) | |
215 | return -ENXIO; | |
216 | pte = *ptep; | |
217 | ||
218 | if (!shift) | |
219 | shift = PAGE_SHIFT; | |
220 | ||
221 | /* Avoid handling anything potentially complicated in realmode */ | |
222 | if (shift > PAGE_SHIFT) | |
223 | return -EAGAIN; | |
224 | ||
225 | if (!pte_young(pte)) | |
226 | return -EAGAIN; | |
227 | ||
228 | *phpa = (pte_pfn(pte) << PAGE_SHIFT) | (ua & ((1ULL << shift) - 1)) | | |
229 | (ua & ~PAGE_MASK); | |
230 | ||
231 | return 0; | |
232 | } | |
233 | ||
234 | long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, | |
235 | unsigned long liobn, unsigned long ioba, | |
236 | unsigned long tce_list, unsigned long npages) | |
237 | { | |
238 | struct kvmppc_spapr_tce_table *stt; | |
239 | long i, ret = H_SUCCESS; | |
240 | unsigned long tces, entry, ua = 0; | |
241 | unsigned long *rmap = NULL; | |
242 | ||
243 | stt = kvmppc_find_table(vcpu, liobn); | |
244 | if (!stt) | |
245 | return H_TOO_HARD; | |
246 | ||
fe26e527 | 247 | entry = ioba >> stt->page_shift; |
d3695aa4 AK |
248 | /* |
249 | * The spec says that the maximum size of the list is 512 TCEs | |
250 | * so the whole table addressed resides in 4K page | |
251 | */ | |
252 | if (npages > 512) | |
253 | return H_PARAMETER; | |
254 | ||
255 | if (tce_list & (SZ_4K - 1)) | |
256 | return H_PARAMETER; | |
257 | ||
258 | ret = kvmppc_ioba_validate(stt, ioba, npages); | |
259 | if (ret != H_SUCCESS) | |
260 | return ret; | |
261 | ||
262 | if (kvmppc_gpa_to_ua(vcpu->kvm, tce_list, &ua, &rmap)) | |
263 | return H_TOO_HARD; | |
264 | ||
265 | rmap = (void *) vmalloc_to_phys(rmap); | |
266 | ||
267 | /* | |
268 | * Synchronize with the MMU notifier callbacks in | |
269 | * book3s_64_mmu_hv.c (kvm_unmap_hva_hv etc.). | |
270 | * While we have the rmap lock, code running on other CPUs | |
271 | * cannot finish unmapping the host real page that backs | |
272 | * this guest real page, so we are OK to access the host | |
273 | * real page. | |
274 | */ | |
275 | lock_rmap(rmap); | |
276 | if (kvmppc_rm_ua_to_hpa(vcpu, ua, &tces)) { | |
277 | ret = H_TOO_HARD; | |
278 | goto unlock_exit; | |
279 | } | |
280 | ||
281 | for (i = 0; i < npages; ++i) { | |
282 | unsigned long tce = be64_to_cpu(((u64 *)tces)[i]); | |
283 | ||
284 | ret = kvmppc_tce_validate(stt, tce); | |
285 | if (ret != H_SUCCESS) | |
286 | goto unlock_exit; | |
287 | ||
288 | kvmppc_tce_put(stt, entry + i, tce); | |
289 | } | |
290 | ||
291 | unlock_exit: | |
292 | unlock_rmap(rmap); | |
293 | ||
294 | return ret; | |
295 | } | |
296 | ||
31217db7 | 297 | long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu, |
d3695aa4 AK |
298 | unsigned long liobn, unsigned long ioba, |
299 | unsigned long tce_value, unsigned long npages) | |
300 | { | |
301 | struct kvmppc_spapr_tce_table *stt; | |
302 | long i, ret; | |
303 | ||
304 | stt = kvmppc_find_table(vcpu, liobn); | |
305 | if (!stt) | |
306 | return H_TOO_HARD; | |
307 | ||
308 | ret = kvmppc_ioba_validate(stt, ioba, npages); | |
309 | if (ret != H_SUCCESS) | |
310 | return ret; | |
311 | ||
312 | /* Check permission bits only to allow userspace poison TCE for debug */ | |
313 | if (tce_value & (TCE_PCI_WRITE | TCE_PCI_READ)) | |
314 | return H_PARAMETER; | |
315 | ||
fe26e527 AK |
316 | for (i = 0; i < npages; ++i, ioba += (1ULL << stt->page_shift)) |
317 | kvmppc_tce_put(stt, ioba >> stt->page_shift, tce_value); | |
d3695aa4 AK |
318 | |
319 | return H_SUCCESS; | |
320 | } | |
d3695aa4 | 321 | |
69e9fbb2 LD |
322 | long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn, |
323 | unsigned long ioba) | |
324 | { | |
fcbb2ce6 AK |
325 | struct kvmppc_spapr_tce_table *stt = kvmppc_find_table(vcpu, liobn); |
326 | long ret; | |
327 | unsigned long idx; | |
328 | struct page *page; | |
329 | u64 *tbl; | |
69e9fbb2 | 330 | |
fcbb2ce6 AK |
331 | if (!stt) |
332 | return H_TOO_HARD; | |
69e9fbb2 | 333 | |
fcbb2ce6 AK |
334 | ret = kvmppc_ioba_validate(stt, ioba, 1); |
335 | if (ret != H_SUCCESS) | |
336 | return ret; | |
69e9fbb2 | 337 | |
14f853f1 | 338 | idx = (ioba >> stt->page_shift) - stt->offset; |
fcbb2ce6 AK |
339 | page = stt->pages[idx / TCES_PER_PAGE]; |
340 | tbl = (u64 *)page_address(page); | |
69e9fbb2 | 341 | |
fcbb2ce6 | 342 | vcpu->arch.gpr[4] = tbl[idx % TCES_PER_PAGE]; |
69e9fbb2 | 343 | |
fcbb2ce6 | 344 | return H_SUCCESS; |
69e9fbb2 LD |
345 | } |
346 | EXPORT_SYMBOL_GPL(kvmppc_h_get_tce); | |
d3695aa4 AK |
347 | |
348 | #endif /* KVM_BOOK3S_HV_POSSIBLE */ |