OSDN Git Service

KVM: PPC: Validate TCEs against preregistered memory page sizes
[uclinux-h8/linux.git] / arch / powerpc / kvm / book3s_64_vio_hv.c
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>
17  * Copyright 2016 Alexey Kardashevskiy, IBM Corporation <aik@au1.ibm.com>
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 #include <linux/stringify.h>
30
31 #include <asm/kvm_ppc.h>
32 #include <asm/kvm_book3s.h>
33 #include <asm/book3s/64/mmu-hash.h>
34 #include <asm/mmu_context.h>
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>
40 #include <asm/iommu.h>
41 #include <asm/tce.h>
42 #include <asm/pte-walk.h>
43
44 #ifdef CONFIG_BUG
45
46 #define WARN_ON_ONCE_RM(condition)      ({                      \
47         static bool __section(.data.unlikely) __warned;         \
48         int __ret_warn_once = !!(condition);                    \
49                                                                 \
50         if (unlikely(__ret_warn_once && !__warned)) {           \
51                 __warned = true;                                \
52                 pr_err("WARN_ON_ONCE_RM: (%s) at %s:%u\n",      \
53                                 __stringify(condition),         \
54                                 __func__, __LINE__);            \
55                 dump_stack();                                   \
56         }                                                       \
57         unlikely(__ret_warn_once);                              \
58 })
59
60 #else
61
62 #define WARN_ON_ONCE_RM(condition) ({                           \
63         int __ret_warn_on = !!(condition);                      \
64         unlikely(__ret_warn_on);                                \
65 })
66
67 #endif
68
69 #define TCES_PER_PAGE   (PAGE_SIZE / sizeof(u64))
70
71 /*
72  * Finds a TCE table descriptor by LIOBN.
73  *
74  * WARNING: This will be called in real or virtual mode on HV KVM and virtual
75  *          mode on PR KVM
76  */
77 struct kvmppc_spapr_tce_table *kvmppc_find_table(struct kvm *kvm,
78                 unsigned long liobn)
79 {
80         struct kvmppc_spapr_tce_table *stt;
81
82         list_for_each_entry_lockless(stt, &kvm->arch.spapr_tce_tables, list)
83                 if (stt->liobn == liobn)
84                         return stt;
85
86         return NULL;
87 }
88 EXPORT_SYMBOL_GPL(kvmppc_find_table);
89
90 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
91 /*
92  * Validates TCE address.
93  * At the moment flags and page mask are validated.
94  * As the host kernel does not access those addresses (just puts them
95  * to the table and user space is supposed to process them), we can skip
96  * checking other things (such as TCE is a guest RAM address or the page
97  * was actually allocated).
98  */
99 static long kvmppc_rm_tce_validate(struct kvmppc_spapr_tce_table *stt,
100                 unsigned long tce)
101 {
102         unsigned long gpa = tce & ~(TCE_PCI_READ | TCE_PCI_WRITE);
103         enum dma_data_direction dir = iommu_tce_direction(tce);
104         struct kvmppc_spapr_tce_iommu_table *stit;
105         unsigned long ua = 0;
106
107         /* Allow userspace to poison TCE table */
108         if (dir == DMA_NONE)
109                 return H_SUCCESS;
110
111         if (iommu_tce_check_gpa(stt->page_shift, gpa))
112                 return H_PARAMETER;
113
114         if (kvmppc_gpa_to_ua(stt->kvm, tce & ~(TCE_PCI_READ | TCE_PCI_WRITE),
115                                 &ua, NULL))
116                 return H_TOO_HARD;
117
118         list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
119                 unsigned long hpa = 0;
120                 struct mm_iommu_table_group_mem_t *mem;
121                 long shift = stit->tbl->it_page_shift;
122
123                 mem = mm_iommu_lookup_rm(stt->kvm->mm, ua, 1ULL << shift);
124                 if (!mem)
125                         return H_TOO_HARD;
126
127                 if (mm_iommu_ua_to_hpa_rm(mem, ua, shift, &hpa))
128                         return H_TOO_HARD;
129         }
130
131         return H_SUCCESS;
132 }
133 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
134
135 /* Note on the use of page_address() in real mode,
136  *
137  * It is safe to use page_address() in real mode on ppc64 because
138  * page_address() is always defined as lowmem_page_address()
139  * which returns __va(PFN_PHYS(page_to_pfn(page))) which is arithmetic
140  * operation and does not access page struct.
141  *
142  * Theoretically page_address() could be defined different
143  * but either WANT_PAGE_VIRTUAL or HASHED_PAGE_VIRTUAL
144  * would have to be enabled.
145  * WANT_PAGE_VIRTUAL is never enabled on ppc32/ppc64,
146  * HASHED_PAGE_VIRTUAL could be enabled for ppc32 only and only
147  * if CONFIG_HIGHMEM is defined. As CONFIG_SPARSEMEM_VMEMMAP
148  * is not expected to be enabled on ppc32, page_address()
149  * is safe for ppc32 as well.
150  *
151  * WARNING: This will be called in real-mode on HV KVM and virtual
152  *          mode on PR KVM
153  */
154 static u64 *kvmppc_page_address(struct page *page)
155 {
156 #if defined(HASHED_PAGE_VIRTUAL) || defined(WANT_PAGE_VIRTUAL)
157 #error TODO: fix to avoid page_address() here
158 #endif
159         return (u64 *) page_address(page);
160 }
161
162 /*
163  * Handles TCE requests for emulated devices.
164  * Puts guest TCE values to the table and expects user space to convert them.
165  * Called in both real and virtual modes.
166  * Cannot fail so kvmppc_tce_validate must be called before it.
167  *
168  * WARNING: This will be called in real-mode on HV KVM and virtual
169  *          mode on PR KVM
170  */
171 void kvmppc_tce_put(struct kvmppc_spapr_tce_table *stt,
172                 unsigned long idx, unsigned long tce)
173 {
174         struct page *page;
175         u64 *tbl;
176
177         idx -= stt->offset;
178         page = stt->pages[idx / TCES_PER_PAGE];
179         tbl = kvmppc_page_address(page);
180
181         tbl[idx % TCES_PER_PAGE] = tce;
182 }
183 EXPORT_SYMBOL_GPL(kvmppc_tce_put);
184
185 long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa,
186                 unsigned long *ua, unsigned long **prmap)
187 {
188         unsigned long gfn = gpa >> PAGE_SHIFT;
189         struct kvm_memory_slot *memslot;
190
191         memslot = search_memslots(kvm_memslots(kvm), gfn);
192         if (!memslot)
193                 return -EINVAL;
194
195         *ua = __gfn_to_hva_memslot(memslot, gfn) |
196                 (gpa & ~(PAGE_MASK | TCE_PCI_READ | TCE_PCI_WRITE));
197
198 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
199         if (prmap)
200                 *prmap = &memslot->arch.rmap[gfn - memslot->base_gfn];
201 #endif
202
203         return 0;
204 }
205 EXPORT_SYMBOL_GPL(kvmppc_gpa_to_ua);
206
207 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
208 static long iommu_tce_xchg_rm(struct mm_struct *mm, struct iommu_table *tbl,
209                 unsigned long entry, unsigned long *hpa,
210                 enum dma_data_direction *direction)
211 {
212         long ret;
213
214         ret = tbl->it_ops->exchange_rm(tbl, entry, hpa, direction);
215
216         if (!ret && ((*direction == DMA_FROM_DEVICE) ||
217                                 (*direction == DMA_BIDIRECTIONAL))) {
218                 __be64 *pua = IOMMU_TABLE_USERSPACE_ENTRY_RM(tbl, entry);
219                 /*
220                  * kvmppc_rm_tce_iommu_do_map() updates the UA cache after
221                  * calling this so we still get here a valid UA.
222                  */
223                 if (pua && *pua)
224                         mm_iommu_ua_mark_dirty_rm(mm, be64_to_cpu(*pua));
225         }
226
227         return ret;
228 }
229
230 static void kvmppc_rm_clear_tce(struct kvm *kvm, struct iommu_table *tbl,
231                 unsigned long entry)
232 {
233         unsigned long hpa = 0;
234         enum dma_data_direction dir = DMA_NONE;
235
236         iommu_tce_xchg_rm(kvm->mm, tbl, entry, &hpa, &dir);
237 }
238
239 static long kvmppc_rm_tce_iommu_mapped_dec(struct kvm *kvm,
240                 struct iommu_table *tbl, unsigned long entry)
241 {
242         struct mm_iommu_table_group_mem_t *mem = NULL;
243         const unsigned long pgsize = 1ULL << tbl->it_page_shift;
244         __be64 *pua = IOMMU_TABLE_USERSPACE_ENTRY_RM(tbl, entry);
245
246         if (!pua)
247                 /* it_userspace allocation might be delayed */
248                 return H_TOO_HARD;
249
250         mem = mm_iommu_lookup_rm(kvm->mm, be64_to_cpu(*pua), pgsize);
251         if (!mem)
252                 return H_TOO_HARD;
253
254         mm_iommu_mapped_dec(mem);
255
256         *pua = cpu_to_be64(0);
257
258         return H_SUCCESS;
259 }
260
261 static long kvmppc_rm_tce_iommu_do_unmap(struct kvm *kvm,
262                 struct iommu_table *tbl, unsigned long entry)
263 {
264         enum dma_data_direction dir = DMA_NONE;
265         unsigned long hpa = 0;
266         long ret;
267
268         if (iommu_tce_xchg_rm(kvm->mm, tbl, entry, &hpa, &dir))
269                 /*
270                  * real mode xchg can fail if struct page crosses
271                  * a page boundary
272                  */
273                 return H_TOO_HARD;
274
275         if (dir == DMA_NONE)
276                 return H_SUCCESS;
277
278         ret = kvmppc_rm_tce_iommu_mapped_dec(kvm, tbl, entry);
279         if (ret)
280                 iommu_tce_xchg_rm(kvm->mm, tbl, entry, &hpa, &dir);
281
282         return ret;
283 }
284
285 static long kvmppc_rm_tce_iommu_unmap(struct kvm *kvm,
286                 struct kvmppc_spapr_tce_table *stt, struct iommu_table *tbl,
287                 unsigned long entry)
288 {
289         unsigned long i, ret = H_SUCCESS;
290         unsigned long subpages = 1ULL << (stt->page_shift - tbl->it_page_shift);
291         unsigned long io_entry = entry * subpages;
292
293         for (i = 0; i < subpages; ++i) {
294                 ret = kvmppc_rm_tce_iommu_do_unmap(kvm, tbl, io_entry + i);
295                 if (ret != H_SUCCESS)
296                         break;
297         }
298
299         return ret;
300 }
301
302 static long kvmppc_rm_tce_iommu_do_map(struct kvm *kvm, struct iommu_table *tbl,
303                 unsigned long entry, unsigned long ua,
304                 enum dma_data_direction dir)
305 {
306         long ret;
307         unsigned long hpa = 0;
308         __be64 *pua = IOMMU_TABLE_USERSPACE_ENTRY_RM(tbl, entry);
309         struct mm_iommu_table_group_mem_t *mem;
310
311         if (!pua)
312                 /* it_userspace allocation might be delayed */
313                 return H_TOO_HARD;
314
315         mem = mm_iommu_lookup_rm(kvm->mm, ua, 1ULL << tbl->it_page_shift);
316         if (!mem)
317                 return H_TOO_HARD;
318
319         if (WARN_ON_ONCE_RM(mm_iommu_ua_to_hpa_rm(mem, ua, tbl->it_page_shift,
320                         &hpa)))
321                 return H_TOO_HARD;
322
323         if (WARN_ON_ONCE_RM(mm_iommu_mapped_inc(mem)))
324                 return H_TOO_HARD;
325
326         ret = iommu_tce_xchg_rm(kvm->mm, tbl, entry, &hpa, &dir);
327         if (ret) {
328                 mm_iommu_mapped_dec(mem);
329                 /*
330                  * real mode xchg can fail if struct page crosses
331                  * a page boundary
332                  */
333                 return H_TOO_HARD;
334         }
335
336         if (dir != DMA_NONE)
337                 kvmppc_rm_tce_iommu_mapped_dec(kvm, tbl, entry);
338
339         *pua = cpu_to_be64(ua);
340
341         return 0;
342 }
343
344 static long kvmppc_rm_tce_iommu_map(struct kvm *kvm,
345                 struct kvmppc_spapr_tce_table *stt, struct iommu_table *tbl,
346                 unsigned long entry, unsigned long ua,
347                 enum dma_data_direction dir)
348 {
349         unsigned long i, pgoff, ret = H_SUCCESS;
350         unsigned long subpages = 1ULL << (stt->page_shift - tbl->it_page_shift);
351         unsigned long io_entry = entry * subpages;
352
353         for (i = 0, pgoff = 0; i < subpages;
354                         ++i, pgoff += IOMMU_PAGE_SIZE(tbl)) {
355
356                 ret = kvmppc_rm_tce_iommu_do_map(kvm, tbl,
357                                 io_entry + i, ua + pgoff, dir);
358                 if (ret != H_SUCCESS)
359                         break;
360         }
361
362         return ret;
363 }
364
365 long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
366                 unsigned long ioba, unsigned long tce)
367 {
368         struct kvmppc_spapr_tce_table *stt;
369         long ret;
370         struct kvmppc_spapr_tce_iommu_table *stit;
371         unsigned long entry, ua = 0;
372         enum dma_data_direction dir;
373
374         /* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
375         /*          liobn, ioba, tce); */
376
377         /* For radix, we might be in virtual mode, so punt */
378         if (kvm_is_radix(vcpu->kvm))
379                 return H_TOO_HARD;
380
381         stt = kvmppc_find_table(vcpu->kvm, liobn);
382         if (!stt)
383                 return H_TOO_HARD;
384
385         ret = kvmppc_ioba_validate(stt, ioba, 1);
386         if (ret != H_SUCCESS)
387                 return ret;
388
389         ret = kvmppc_rm_tce_validate(stt, tce);
390         if (ret != H_SUCCESS)
391                 return ret;
392
393         dir = iommu_tce_direction(tce);
394         if ((dir != DMA_NONE) && kvmppc_gpa_to_ua(vcpu->kvm,
395                         tce & ~(TCE_PCI_READ | TCE_PCI_WRITE), &ua, NULL))
396                 return H_PARAMETER;
397
398         entry = ioba >> stt->page_shift;
399
400         list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
401                 if (dir == DMA_NONE)
402                         ret = kvmppc_rm_tce_iommu_unmap(vcpu->kvm, stt,
403                                         stit->tbl, entry);
404                 else
405                         ret = kvmppc_rm_tce_iommu_map(vcpu->kvm, stt,
406                                         stit->tbl, entry, ua, dir);
407
408                 if (ret == H_SUCCESS)
409                         continue;
410
411                 if (ret == H_TOO_HARD)
412                         return ret;
413
414                 WARN_ON_ONCE_RM(1);
415                 kvmppc_rm_clear_tce(vcpu->kvm, stit->tbl, entry);
416         }
417
418         kvmppc_tce_put(stt, entry, tce);
419
420         return H_SUCCESS;
421 }
422
423 static long kvmppc_rm_ua_to_hpa(struct kvm_vcpu *vcpu,
424                 unsigned long ua, unsigned long *phpa)
425 {
426         pte_t *ptep, pte;
427         unsigned shift = 0;
428
429         /*
430          * Called in real mode with MSR_EE = 0. We are safe here.
431          * It is ok to do the lookup with arch.pgdir here, because
432          * we are doing this on secondary cpus and current task there
433          * is not the hypervisor. Also this is safe against THP in the
434          * host, because an IPI to primary thread will wait for the secondary
435          * to exit which will agains result in the below page table walk
436          * to finish.
437          */
438         ptep = __find_linux_pte(vcpu->arch.pgdir, ua, NULL, &shift);
439         if (!ptep || !pte_present(*ptep))
440                 return -ENXIO;
441         pte = *ptep;
442
443         if (!shift)
444                 shift = PAGE_SHIFT;
445
446         /* Avoid handling anything potentially complicated in realmode */
447         if (shift > PAGE_SHIFT)
448                 return -EAGAIN;
449
450         if (!pte_young(pte))
451                 return -EAGAIN;
452
453         *phpa = (pte_pfn(pte) << PAGE_SHIFT) | (ua & ((1ULL << shift) - 1)) |
454                         (ua & ~PAGE_MASK);
455
456         return 0;
457 }
458
459 long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
460                 unsigned long liobn, unsigned long ioba,
461                 unsigned long tce_list, unsigned long npages)
462 {
463         struct kvmppc_spapr_tce_table *stt;
464         long i, ret = H_SUCCESS;
465         unsigned long tces, entry, ua = 0;
466         unsigned long *rmap = NULL;
467         bool prereg = false;
468         struct kvmppc_spapr_tce_iommu_table *stit;
469
470         /* For radix, we might be in virtual mode, so punt */
471         if (kvm_is_radix(vcpu->kvm))
472                 return H_TOO_HARD;
473
474         stt = kvmppc_find_table(vcpu->kvm, liobn);
475         if (!stt)
476                 return H_TOO_HARD;
477
478         entry = ioba >> stt->page_shift;
479         /*
480          * The spec says that the maximum size of the list is 512 TCEs
481          * so the whole table addressed resides in 4K page
482          */
483         if (npages > 512)
484                 return H_PARAMETER;
485
486         if (tce_list & (SZ_4K - 1))
487                 return H_PARAMETER;
488
489         ret = kvmppc_ioba_validate(stt, ioba, npages);
490         if (ret != H_SUCCESS)
491                 return ret;
492
493         if (mm_iommu_preregistered(vcpu->kvm->mm)) {
494                 /*
495                  * We get here if guest memory was pre-registered which
496                  * is normally VFIO case and gpa->hpa translation does not
497                  * depend on hpt.
498                  */
499                 struct mm_iommu_table_group_mem_t *mem;
500
501                 if (kvmppc_gpa_to_ua(vcpu->kvm, tce_list, &ua, NULL))
502                         return H_TOO_HARD;
503
504                 mem = mm_iommu_lookup_rm(vcpu->kvm->mm, ua, IOMMU_PAGE_SIZE_4K);
505                 if (mem)
506                         prereg = mm_iommu_ua_to_hpa_rm(mem, ua,
507                                         IOMMU_PAGE_SHIFT_4K, &tces) == 0;
508         }
509
510         if (!prereg) {
511                 /*
512                  * This is usually a case of a guest with emulated devices only
513                  * when TCE list is not in preregistered memory.
514                  * We do not require memory to be preregistered in this case
515                  * so lock rmap and do __find_linux_pte_or_hugepte().
516                  */
517                 if (kvmppc_gpa_to_ua(vcpu->kvm, tce_list, &ua, &rmap))
518                         return H_TOO_HARD;
519
520                 rmap = (void *) vmalloc_to_phys(rmap);
521                 if (WARN_ON_ONCE_RM(!rmap))
522                         return H_TOO_HARD;
523
524                 /*
525                  * Synchronize with the MMU notifier callbacks in
526                  * book3s_64_mmu_hv.c (kvm_unmap_hva_range_hv etc.).
527                  * While we have the rmap lock, code running on other CPUs
528                  * cannot finish unmapping the host real page that backs
529                  * this guest real page, so we are OK to access the host
530                  * real page.
531                  */
532                 lock_rmap(rmap);
533                 if (kvmppc_rm_ua_to_hpa(vcpu, ua, &tces)) {
534                         ret = H_TOO_HARD;
535                         goto unlock_exit;
536                 }
537         }
538
539         for (i = 0; i < npages; ++i) {
540                 unsigned long tce = be64_to_cpu(((u64 *)tces)[i]);
541
542                 ret = kvmppc_rm_tce_validate(stt, tce);
543                 if (ret != H_SUCCESS)
544                         goto unlock_exit;
545         }
546
547         for (i = 0; i < npages; ++i) {
548                 unsigned long tce = be64_to_cpu(((u64 *)tces)[i]);
549
550                 ua = 0;
551                 if (kvmppc_gpa_to_ua(vcpu->kvm,
552                                 tce & ~(TCE_PCI_READ | TCE_PCI_WRITE),
553                                 &ua, NULL))
554                         return H_PARAMETER;
555
556                 list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
557                         ret = kvmppc_rm_tce_iommu_map(vcpu->kvm, stt,
558                                         stit->tbl, entry + i, ua,
559                                         iommu_tce_direction(tce));
560
561                         if (ret == H_SUCCESS)
562                                 continue;
563
564                         if (ret == H_TOO_HARD)
565                                 goto unlock_exit;
566
567                         WARN_ON_ONCE_RM(1);
568                         kvmppc_rm_clear_tce(vcpu->kvm, stit->tbl, entry);
569                 }
570
571                 kvmppc_tce_put(stt, entry + i, tce);
572         }
573
574 unlock_exit:
575         if (rmap)
576                 unlock_rmap(rmap);
577
578         return ret;
579 }
580
581 long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,
582                 unsigned long liobn, unsigned long ioba,
583                 unsigned long tce_value, unsigned long npages)
584 {
585         struct kvmppc_spapr_tce_table *stt;
586         long i, ret;
587         struct kvmppc_spapr_tce_iommu_table *stit;
588
589         /* For radix, we might be in virtual mode, so punt */
590         if (kvm_is_radix(vcpu->kvm))
591                 return H_TOO_HARD;
592
593         stt = kvmppc_find_table(vcpu->kvm, liobn);
594         if (!stt)
595                 return H_TOO_HARD;
596
597         ret = kvmppc_ioba_validate(stt, ioba, npages);
598         if (ret != H_SUCCESS)
599                 return ret;
600
601         /* Check permission bits only to allow userspace poison TCE for debug */
602         if (tce_value & (TCE_PCI_WRITE | TCE_PCI_READ))
603                 return H_PARAMETER;
604
605         list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
606                 unsigned long entry = ioba >> stt->page_shift;
607
608                 for (i = 0; i < npages; ++i) {
609                         ret = kvmppc_rm_tce_iommu_unmap(vcpu->kvm, stt,
610                                         stit->tbl, entry + i);
611
612                         if (ret == H_SUCCESS)
613                                 continue;
614
615                         if (ret == H_TOO_HARD)
616                                 return ret;
617
618                         WARN_ON_ONCE_RM(1);
619                         kvmppc_rm_clear_tce(vcpu->kvm, stit->tbl, entry);
620                 }
621         }
622
623         for (i = 0; i < npages; ++i, ioba += (1ULL << stt->page_shift))
624                 kvmppc_tce_put(stt, ioba >> stt->page_shift, tce_value);
625
626         return H_SUCCESS;
627 }
628
629 /* This can be called in either virtual mode or real mode */
630 long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
631                       unsigned long ioba)
632 {
633         struct kvmppc_spapr_tce_table *stt;
634         long ret;
635         unsigned long idx;
636         struct page *page;
637         u64 *tbl;
638
639         stt = kvmppc_find_table(vcpu->kvm, liobn);
640         if (!stt)
641                 return H_TOO_HARD;
642
643         ret = kvmppc_ioba_validate(stt, ioba, 1);
644         if (ret != H_SUCCESS)
645                 return ret;
646
647         idx = (ioba >> stt->page_shift) - stt->offset;
648         page = stt->pages[idx / TCES_PER_PAGE];
649         tbl = (u64 *)page_address(page);
650
651         vcpu->arch.regs.gpr[4] = tbl[idx % TCES_PER_PAGE];
652
653         return H_SUCCESS;
654 }
655 EXPORT_SYMBOL_GPL(kvmppc_h_get_tce);
656
657 #endif /* KVM_BOOK3S_HV_POSSIBLE */