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 IBM Corp. 2007
16 *
17 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
19 */
20
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/kvm_host.h>
24 #include <linux/vmalloc.h>
25 #include <linux/hrtimer.h>
26 #include <linux/sched/signal.h>
27 #include <linux/fs.h>
28 #include <linux/slab.h>
29 #include <linux/file.h>
30 #include <linux/module.h>
31 #include <linux/irqbypass.h>
32 #include <linux/kvm_irqfd.h>
33 #include <asm/cputable.h>
34 #include <linux/uaccess.h>
35 #include <asm/kvm_ppc.h>
36 #include <asm/tlbflush.h>
37 #include <asm/cputhreads.h>
38 #include <asm/irqflags.h>
39 #include <asm/iommu.h>
40 #include <asm/switch_to.h>
41 #include <asm/xive.h>
42
43 #include "timing.h"
44 #include "irq.h"
45 #include "../mm/mmu_decl.h"
46
47 #define CREATE_TRACE_POINTS
48 #include "trace.h"
49
50 struct kvmppc_ops *kvmppc_hv_ops;
51 EXPORT_SYMBOL_GPL(kvmppc_hv_ops);
52 struct kvmppc_ops *kvmppc_pr_ops;
53 EXPORT_SYMBOL_GPL(kvmppc_pr_ops);
54
55
kvm_arch_vcpu_runnable(struct kvm_vcpu * v)56 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
57 {
58 return !!(v->arch.pending_exceptions) || kvm_request_pending(v);
59 }
60
kvm_arch_dy_runnable(struct kvm_vcpu * vcpu)61 bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
62 {
63 return kvm_arch_vcpu_runnable(vcpu);
64 }
65
kvm_arch_vcpu_in_kernel(struct kvm_vcpu * vcpu)66 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
67 {
68 return false;
69 }
70
kvm_arch_vcpu_should_kick(struct kvm_vcpu * vcpu)71 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
72 {
73 return 1;
74 }
75
76 /*
77 * Common checks before entering the guest world. Call with interrupts
78 * disabled.
79 *
80 * returns:
81 *
82 * == 1 if we're ready to go into guest state
83 * <= 0 if we need to go back to the host with return value
84 */
kvmppc_prepare_to_enter(struct kvm_vcpu * vcpu)85 int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
86 {
87 int r;
88
89 WARN_ON(irqs_disabled());
90 hard_irq_disable();
91
92 while (true) {
93 if (need_resched()) {
94 local_irq_enable();
95 cond_resched();
96 hard_irq_disable();
97 continue;
98 }
99
100 if (signal_pending(current)) {
101 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
102 vcpu->run->exit_reason = KVM_EXIT_INTR;
103 r = -EINTR;
104 break;
105 }
106
107 vcpu->mode = IN_GUEST_MODE;
108
109 /*
110 * Reading vcpu->requests must happen after setting vcpu->mode,
111 * so we don't miss a request because the requester sees
112 * OUTSIDE_GUEST_MODE and assumes we'll be checking requests
113 * before next entering the guest (and thus doesn't IPI).
114 * This also orders the write to mode from any reads
115 * to the page tables done while the VCPU is running.
116 * Please see the comment in kvm_flush_remote_tlbs.
117 */
118 smp_mb();
119
120 if (kvm_request_pending(vcpu)) {
121 /* Make sure we process requests preemptable */
122 local_irq_enable();
123 trace_kvm_check_requests(vcpu);
124 r = kvmppc_core_check_requests(vcpu);
125 hard_irq_disable();
126 if (r > 0)
127 continue;
128 break;
129 }
130
131 if (kvmppc_core_prepare_to_enter(vcpu)) {
132 /* interrupts got enabled in between, so we
133 are back at square 1 */
134 continue;
135 }
136
137 guest_enter_irqoff();
138 return 1;
139 }
140
141 /* return to host */
142 local_irq_enable();
143 return r;
144 }
145 EXPORT_SYMBOL_GPL(kvmppc_prepare_to_enter);
146
147 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
kvmppc_swab_shared(struct kvm_vcpu * vcpu)148 static void kvmppc_swab_shared(struct kvm_vcpu *vcpu)
149 {
150 struct kvm_vcpu_arch_shared *shared = vcpu->arch.shared;
151 int i;
152
153 shared->sprg0 = swab64(shared->sprg0);
154 shared->sprg1 = swab64(shared->sprg1);
155 shared->sprg2 = swab64(shared->sprg2);
156 shared->sprg3 = swab64(shared->sprg3);
157 shared->srr0 = swab64(shared->srr0);
158 shared->srr1 = swab64(shared->srr1);
159 shared->dar = swab64(shared->dar);
160 shared->msr = swab64(shared->msr);
161 shared->dsisr = swab32(shared->dsisr);
162 shared->int_pending = swab32(shared->int_pending);
163 for (i = 0; i < ARRAY_SIZE(shared->sr); i++)
164 shared->sr[i] = swab32(shared->sr[i]);
165 }
166 #endif
167
kvmppc_kvm_pv(struct kvm_vcpu * vcpu)168 int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
169 {
170 int nr = kvmppc_get_gpr(vcpu, 11);
171 int r;
172 unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
173 unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
174 unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
175 unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
176 unsigned long r2 = 0;
177
178 if (!(kvmppc_get_msr(vcpu) & MSR_SF)) {
179 /* 32 bit mode */
180 param1 &= 0xffffffff;
181 param2 &= 0xffffffff;
182 param3 &= 0xffffffff;
183 param4 &= 0xffffffff;
184 }
185
186 switch (nr) {
187 case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE):
188 {
189 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
190 /* Book3S can be little endian, find it out here */
191 int shared_big_endian = true;
192 if (vcpu->arch.intr_msr & MSR_LE)
193 shared_big_endian = false;
194 if (shared_big_endian != vcpu->arch.shared_big_endian)
195 kvmppc_swab_shared(vcpu);
196 vcpu->arch.shared_big_endian = shared_big_endian;
197 #endif
198
199 if (!(param2 & MAGIC_PAGE_FLAG_NOT_MAPPED_NX)) {
200 /*
201 * Older versions of the Linux magic page code had
202 * a bug where they would map their trampoline code
203 * NX. If that's the case, remove !PR NX capability.
204 */
205 vcpu->arch.disable_kernel_nx = true;
206 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
207 }
208
209 vcpu->arch.magic_page_pa = param1 & ~0xfffULL;
210 vcpu->arch.magic_page_ea = param2 & ~0xfffULL;
211
212 #ifdef CONFIG_PPC_64K_PAGES
213 /*
214 * Make sure our 4k magic page is in the same window of a 64k
215 * page within the guest and within the host's page.
216 */
217 if ((vcpu->arch.magic_page_pa & 0xf000) !=
218 ((ulong)vcpu->arch.shared & 0xf000)) {
219 void *old_shared = vcpu->arch.shared;
220 ulong shared = (ulong)vcpu->arch.shared;
221 void *new_shared;
222
223 shared &= PAGE_MASK;
224 shared |= vcpu->arch.magic_page_pa & 0xf000;
225 new_shared = (void*)shared;
226 memcpy(new_shared, old_shared, 0x1000);
227 vcpu->arch.shared = new_shared;
228 }
229 #endif
230
231 r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
232
233 r = EV_SUCCESS;
234 break;
235 }
236 case KVM_HCALL_TOKEN(KVM_HC_FEATURES):
237 r = EV_SUCCESS;
238 #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2)
239 r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
240 #endif
241
242 /* Second return value is in r4 */
243 break;
244 case EV_HCALL_TOKEN(EV_IDLE):
245 r = EV_SUCCESS;
246 kvm_vcpu_block(vcpu);
247 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
248 break;
249 default:
250 r = EV_UNIMPLEMENTED;
251 break;
252 }
253
254 kvmppc_set_gpr(vcpu, 4, r2);
255
256 return r;
257 }
258 EXPORT_SYMBOL_GPL(kvmppc_kvm_pv);
259
kvmppc_sanity_check(struct kvm_vcpu * vcpu)260 int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
261 {
262 int r = false;
263
264 /* We have to know what CPU to virtualize */
265 if (!vcpu->arch.pvr)
266 goto out;
267
268 /* PAPR only works with book3s_64 */
269 if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
270 goto out;
271
272 /* HV KVM can only do PAPR mode for now */
273 if (!vcpu->arch.papr_enabled && is_kvmppc_hv_enabled(vcpu->kvm))
274 goto out;
275
276 #ifdef CONFIG_KVM_BOOKE_HV
277 if (!cpu_has_feature(CPU_FTR_EMB_HV))
278 goto out;
279 #endif
280
281 r = true;
282
283 out:
284 vcpu->arch.sane = r;
285 return r ? 0 : -EINVAL;
286 }
287 EXPORT_SYMBOL_GPL(kvmppc_sanity_check);
288
kvmppc_emulate_mmio(struct kvm_run * run,struct kvm_vcpu * vcpu)289 int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
290 {
291 enum emulation_result er;
292 int r;
293
294 er = kvmppc_emulate_loadstore(vcpu);
295 switch (er) {
296 case EMULATE_DONE:
297 /* Future optimization: only reload non-volatiles if they were
298 * actually modified. */
299 r = RESUME_GUEST_NV;
300 break;
301 case EMULATE_AGAIN:
302 r = RESUME_GUEST;
303 break;
304 case EMULATE_DO_MMIO:
305 run->exit_reason = KVM_EXIT_MMIO;
306 /* We must reload nonvolatiles because "update" load/store
307 * instructions modify register state. */
308 /* Future optimization: only reload non-volatiles if they were
309 * actually modified. */
310 r = RESUME_HOST_NV;
311 break;
312 case EMULATE_FAIL:
313 {
314 u32 last_inst;
315
316 kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
317 /* XXX Deliver Program interrupt to guest. */
318 pr_emerg("%s: emulation failed (%08x)\n", __func__, last_inst);
319 r = RESUME_HOST;
320 break;
321 }
322 default:
323 WARN_ON(1);
324 r = RESUME_GUEST;
325 }
326
327 return r;
328 }
329 EXPORT_SYMBOL_GPL(kvmppc_emulate_mmio);
330
kvmppc_st(struct kvm_vcpu * vcpu,ulong * eaddr,int size,void * ptr,bool data)331 int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
332 bool data)
333 {
334 ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
335 struct kvmppc_pte pte;
336 int r;
337
338 vcpu->stat.st++;
339
340 r = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
341 XLATE_WRITE, &pte);
342 if (r < 0)
343 return r;
344
345 *eaddr = pte.raddr;
346
347 if (!pte.may_write)
348 return -EPERM;
349
350 /* Magic page override */
351 if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
352 ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
353 !(kvmppc_get_msr(vcpu) & MSR_PR)) {
354 void *magic = vcpu->arch.shared;
355 magic += pte.eaddr & 0xfff;
356 memcpy(magic, ptr, size);
357 return EMULATE_DONE;
358 }
359
360 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
361 return EMULATE_DO_MMIO;
362
363 return EMULATE_DONE;
364 }
365 EXPORT_SYMBOL_GPL(kvmppc_st);
366
kvmppc_ld(struct kvm_vcpu * vcpu,ulong * eaddr,int size,void * ptr,bool data)367 int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
368 bool data)
369 {
370 ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
371 struct kvmppc_pte pte;
372 int rc;
373
374 vcpu->stat.ld++;
375
376 rc = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
377 XLATE_READ, &pte);
378 if (rc)
379 return rc;
380
381 *eaddr = pte.raddr;
382
383 if (!pte.may_read)
384 return -EPERM;
385
386 if (!data && !pte.may_execute)
387 return -ENOEXEC;
388
389 /* Magic page override */
390 if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
391 ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
392 !(kvmppc_get_msr(vcpu) & MSR_PR)) {
393 void *magic = vcpu->arch.shared;
394 magic += pte.eaddr & 0xfff;
395 memcpy(ptr, magic, size);
396 return EMULATE_DONE;
397 }
398
399 if (kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size))
400 return EMULATE_DO_MMIO;
401
402 return EMULATE_DONE;
403 }
404 EXPORT_SYMBOL_GPL(kvmppc_ld);
405
kvm_arch_hardware_enable(void)406 int kvm_arch_hardware_enable(void)
407 {
408 return 0;
409 }
410
kvm_arch_hardware_setup(void)411 int kvm_arch_hardware_setup(void)
412 {
413 return 0;
414 }
415
kvm_arch_check_processor_compat(void * rtn)416 void kvm_arch_check_processor_compat(void *rtn)
417 {
418 *(int *)rtn = kvmppc_core_check_processor_compat();
419 }
420
kvm_arch_init_vm(struct kvm * kvm,unsigned long type)421 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
422 {
423 struct kvmppc_ops *kvm_ops = NULL;
424 /*
425 * if we have both HV and PR enabled, default is HV
426 */
427 if (type == 0) {
428 if (kvmppc_hv_ops)
429 kvm_ops = kvmppc_hv_ops;
430 else
431 kvm_ops = kvmppc_pr_ops;
432 if (!kvm_ops)
433 goto err_out;
434 } else if (type == KVM_VM_PPC_HV) {
435 if (!kvmppc_hv_ops)
436 goto err_out;
437 kvm_ops = kvmppc_hv_ops;
438 } else if (type == KVM_VM_PPC_PR) {
439 if (!kvmppc_pr_ops)
440 goto err_out;
441 kvm_ops = kvmppc_pr_ops;
442 } else
443 goto err_out;
444
445 if (kvm_ops->owner && !try_module_get(kvm_ops->owner))
446 return -ENOENT;
447
448 kvm->arch.kvm_ops = kvm_ops;
449 return kvmppc_core_init_vm(kvm);
450 err_out:
451 return -EINVAL;
452 }
453
kvm_arch_has_vcpu_debugfs(void)454 bool kvm_arch_has_vcpu_debugfs(void)
455 {
456 return false;
457 }
458
kvm_arch_create_vcpu_debugfs(struct kvm_vcpu * vcpu)459 int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
460 {
461 return 0;
462 }
463
kvm_arch_destroy_vm(struct kvm * kvm)464 void kvm_arch_destroy_vm(struct kvm *kvm)
465 {
466 unsigned int i;
467 struct kvm_vcpu *vcpu;
468
469 #ifdef CONFIG_KVM_XICS
470 /*
471 * We call kick_all_cpus_sync() to ensure that all
472 * CPUs have executed any pending IPIs before we
473 * continue and free VCPUs structures below.
474 */
475 if (is_kvmppc_hv_enabled(kvm))
476 kick_all_cpus_sync();
477 #endif
478
479 kvm_for_each_vcpu(i, vcpu, kvm)
480 kvm_arch_vcpu_free(vcpu);
481
482 mutex_lock(&kvm->lock);
483 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
484 kvm->vcpus[i] = NULL;
485
486 atomic_set(&kvm->online_vcpus, 0);
487
488 kvmppc_core_destroy_vm(kvm);
489
490 mutex_unlock(&kvm->lock);
491
492 /* drop the module reference */
493 module_put(kvm->arch.kvm_ops->owner);
494 }
495
kvm_vm_ioctl_check_extension(struct kvm * kvm,long ext)496 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
497 {
498 int r;
499 /* Assume we're using HV mode when the HV module is loaded */
500 int hv_enabled = kvmppc_hv_ops ? 1 : 0;
501
502 if (kvm) {
503 /*
504 * Hooray - we know which VM type we're running on. Depend on
505 * that rather than the guess above.
506 */
507 hv_enabled = is_kvmppc_hv_enabled(kvm);
508 }
509
510 switch (ext) {
511 #ifdef CONFIG_BOOKE
512 case KVM_CAP_PPC_BOOKE_SREGS:
513 case KVM_CAP_PPC_BOOKE_WATCHDOG:
514 case KVM_CAP_PPC_EPR:
515 #else
516 case KVM_CAP_PPC_SEGSTATE:
517 case KVM_CAP_PPC_HIOR:
518 case KVM_CAP_PPC_PAPR:
519 #endif
520 case KVM_CAP_PPC_UNSET_IRQ:
521 case KVM_CAP_PPC_IRQ_LEVEL:
522 case KVM_CAP_ENABLE_CAP:
523 case KVM_CAP_ENABLE_CAP_VM:
524 case KVM_CAP_ONE_REG:
525 case KVM_CAP_IOEVENTFD:
526 case KVM_CAP_DEVICE_CTRL:
527 case KVM_CAP_IMMEDIATE_EXIT:
528 r = 1;
529 break;
530 case KVM_CAP_PPC_PAIRED_SINGLES:
531 case KVM_CAP_PPC_OSI:
532 case KVM_CAP_PPC_GET_PVINFO:
533 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
534 case KVM_CAP_SW_TLB:
535 #endif
536 /* We support this only for PR */
537 r = !hv_enabled;
538 break;
539 #ifdef CONFIG_KVM_MPIC
540 case KVM_CAP_IRQ_MPIC:
541 r = 1;
542 break;
543 #endif
544
545 #ifdef CONFIG_PPC_BOOK3S_64
546 case KVM_CAP_SPAPR_TCE:
547 case KVM_CAP_SPAPR_TCE_64:
548 r = 1;
549 break;
550 case KVM_CAP_SPAPR_TCE_VFIO:
551 r = !!cpu_has_feature(CPU_FTR_HVMODE);
552 break;
553 case KVM_CAP_PPC_RTAS:
554 case KVM_CAP_PPC_FIXUP_HCALL:
555 case KVM_CAP_PPC_ENABLE_HCALL:
556 #ifdef CONFIG_KVM_XICS
557 case KVM_CAP_IRQ_XICS:
558 #endif
559 r = 1;
560 break;
561
562 case KVM_CAP_PPC_ALLOC_HTAB:
563 r = hv_enabled;
564 break;
565 #endif /* CONFIG_PPC_BOOK3S_64 */
566 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
567 case KVM_CAP_PPC_SMT:
568 r = 0;
569 if (kvm) {
570 if (kvm->arch.emul_smt_mode > 1)
571 r = kvm->arch.emul_smt_mode;
572 else
573 r = kvm->arch.smt_mode;
574 } else if (hv_enabled) {
575 if (cpu_has_feature(CPU_FTR_ARCH_300))
576 r = 1;
577 else
578 r = threads_per_subcore;
579 }
580 break;
581 case KVM_CAP_PPC_SMT_POSSIBLE:
582 r = 1;
583 if (hv_enabled) {
584 if (!cpu_has_feature(CPU_FTR_ARCH_300))
585 r = ((threads_per_subcore << 1) - 1);
586 else
587 /* P9 can emulate dbells, so allow any mode */
588 r = 8 | 4 | 2 | 1;
589 }
590 break;
591 case KVM_CAP_PPC_RMA:
592 r = 0;
593 break;
594 case KVM_CAP_PPC_HWRNG:
595 r = kvmppc_hwrng_present();
596 break;
597 case KVM_CAP_PPC_MMU_RADIX:
598 r = !!(hv_enabled && radix_enabled());
599 break;
600 case KVM_CAP_PPC_MMU_HASH_V3:
601 r = !!(hv_enabled && !radix_enabled() &&
602 cpu_has_feature(CPU_FTR_ARCH_300));
603 break;
604 #endif
605 case KVM_CAP_SYNC_MMU:
606 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
607 r = hv_enabled;
608 #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
609 r = 1;
610 #else
611 r = 0;
612 #endif
613 break;
614 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
615 case KVM_CAP_PPC_HTAB_FD:
616 r = hv_enabled;
617 break;
618 #endif
619 case KVM_CAP_NR_VCPUS:
620 /*
621 * Recommending a number of CPUs is somewhat arbitrary; we
622 * return the number of present CPUs for -HV (since a host
623 * will have secondary threads "offline"), and for other KVM
624 * implementations just count online CPUs.
625 */
626 if (hv_enabled)
627 r = num_present_cpus();
628 else
629 r = num_online_cpus();
630 break;
631 case KVM_CAP_NR_MEMSLOTS:
632 r = KVM_USER_MEM_SLOTS;
633 break;
634 case KVM_CAP_MAX_VCPUS:
635 r = KVM_MAX_VCPUS;
636 break;
637 case KVM_CAP_MAX_VCPU_ID:
638 r = KVM_MAX_VCPU_ID;
639 break;
640 #ifdef CONFIG_PPC_BOOK3S_64
641 case KVM_CAP_PPC_GET_SMMU_INFO:
642 r = 1;
643 break;
644 case KVM_CAP_SPAPR_MULTITCE:
645 r = 1;
646 break;
647 case KVM_CAP_SPAPR_RESIZE_HPT:
648 /* Disable this on POWER9 until code handles new HPTE format */
649 r = !!hv_enabled && !cpu_has_feature(CPU_FTR_ARCH_300);
650 break;
651 #endif
652 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
653 case KVM_CAP_PPC_FWNMI:
654 r = hv_enabled;
655 break;
656 #endif
657 case KVM_CAP_PPC_HTM:
658 r = cpu_has_feature(CPU_FTR_TM_COMP) && hv_enabled;
659 break;
660 default:
661 r = 0;
662 break;
663 }
664 return r;
665
666 }
667
kvm_arch_dev_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)668 long kvm_arch_dev_ioctl(struct file *filp,
669 unsigned int ioctl, unsigned long arg)
670 {
671 return -EINVAL;
672 }
673
kvm_arch_free_memslot(struct kvm * kvm,struct kvm_memory_slot * free,struct kvm_memory_slot * dont)674 void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
675 struct kvm_memory_slot *dont)
676 {
677 kvmppc_core_free_memslot(kvm, free, dont);
678 }
679
kvm_arch_create_memslot(struct kvm * kvm,struct kvm_memory_slot * slot,unsigned long npages)680 int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
681 unsigned long npages)
682 {
683 return kvmppc_core_create_memslot(kvm, slot, npages);
684 }
685
kvm_arch_prepare_memory_region(struct kvm * kvm,struct kvm_memory_slot * memslot,const struct kvm_userspace_memory_region * mem,enum kvm_mr_change change)686 int kvm_arch_prepare_memory_region(struct kvm *kvm,
687 struct kvm_memory_slot *memslot,
688 const struct kvm_userspace_memory_region *mem,
689 enum kvm_mr_change change)
690 {
691 return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
692 }
693
kvm_arch_commit_memory_region(struct kvm * kvm,const struct kvm_userspace_memory_region * mem,const struct kvm_memory_slot * old,const struct kvm_memory_slot * new,enum kvm_mr_change change)694 void kvm_arch_commit_memory_region(struct kvm *kvm,
695 const struct kvm_userspace_memory_region *mem,
696 const struct kvm_memory_slot *old,
697 const struct kvm_memory_slot *new,
698 enum kvm_mr_change change)
699 {
700 kvmppc_core_commit_memory_region(kvm, mem, old, new);
701 }
702
kvm_arch_flush_shadow_memslot(struct kvm * kvm,struct kvm_memory_slot * slot)703 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
704 struct kvm_memory_slot *slot)
705 {
706 kvmppc_core_flush_memslot(kvm, slot);
707 }
708
kvm_arch_vcpu_create(struct kvm * kvm,unsigned int id)709 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
710 {
711 struct kvm_vcpu *vcpu;
712 vcpu = kvmppc_core_vcpu_create(kvm, id);
713 if (!IS_ERR(vcpu)) {
714 vcpu->arch.wqp = &vcpu->wq;
715 kvmppc_create_vcpu_debugfs(vcpu, id);
716 }
717 return vcpu;
718 }
719
kvm_arch_vcpu_postcreate(struct kvm_vcpu * vcpu)720 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
721 {
722 }
723
kvm_arch_vcpu_free(struct kvm_vcpu * vcpu)724 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
725 {
726 /* Make sure we're not using the vcpu anymore */
727 hrtimer_cancel(&vcpu->arch.dec_timer);
728
729 kvmppc_remove_vcpu_debugfs(vcpu);
730
731 switch (vcpu->arch.irq_type) {
732 case KVMPPC_IRQ_MPIC:
733 kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
734 break;
735 case KVMPPC_IRQ_XICS:
736 if (xive_enabled())
737 kvmppc_xive_cleanup_vcpu(vcpu);
738 else
739 kvmppc_xics_free_icp(vcpu);
740 break;
741 }
742
743 kvmppc_core_vcpu_free(vcpu);
744 }
745
kvm_arch_vcpu_destroy(struct kvm_vcpu * vcpu)746 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
747 {
748 kvm_arch_vcpu_free(vcpu);
749 }
750
kvm_cpu_has_pending_timer(struct kvm_vcpu * vcpu)751 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
752 {
753 return kvmppc_core_pending_dec(vcpu);
754 }
755
kvmppc_decrementer_wakeup(struct hrtimer * timer)756 static enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
757 {
758 struct kvm_vcpu *vcpu;
759
760 vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
761 kvmppc_decrementer_func(vcpu);
762
763 return HRTIMER_NORESTART;
764 }
765
kvm_arch_vcpu_init(struct kvm_vcpu * vcpu)766 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
767 {
768 int ret;
769
770 hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
771 vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
772 vcpu->arch.dec_expires = get_tb();
773
774 #ifdef CONFIG_KVM_EXIT_TIMING
775 mutex_init(&vcpu->arch.exit_timing_lock);
776 #endif
777 ret = kvmppc_subarch_vcpu_init(vcpu);
778 return ret;
779 }
780
kvm_arch_vcpu_uninit(struct kvm_vcpu * vcpu)781 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
782 {
783 kvmppc_mmu_destroy(vcpu);
784 kvmppc_subarch_vcpu_uninit(vcpu);
785 }
786
kvm_arch_vcpu_load(struct kvm_vcpu * vcpu,int cpu)787 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
788 {
789 #ifdef CONFIG_BOOKE
790 /*
791 * vrsave (formerly usprg0) isn't used by Linux, but may
792 * be used by the guest.
793 *
794 * On non-booke this is associated with Altivec and
795 * is handled by code in book3s.c.
796 */
797 mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
798 #endif
799 kvmppc_core_vcpu_load(vcpu, cpu);
800 }
801
kvm_arch_vcpu_put(struct kvm_vcpu * vcpu)802 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
803 {
804 kvmppc_core_vcpu_put(vcpu);
805 #ifdef CONFIG_BOOKE
806 vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
807 #endif
808 }
809
810 /*
811 * irq_bypass_add_producer and irq_bypass_del_producer are only
812 * useful if the architecture supports PCI passthrough.
813 * irq_bypass_stop and irq_bypass_start are not needed and so
814 * kvm_ops are not defined for them.
815 */
kvm_arch_has_irq_bypass(void)816 bool kvm_arch_has_irq_bypass(void)
817 {
818 return ((kvmppc_hv_ops && kvmppc_hv_ops->irq_bypass_add_producer) ||
819 (kvmppc_pr_ops && kvmppc_pr_ops->irq_bypass_add_producer));
820 }
821
kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer * cons,struct irq_bypass_producer * prod)822 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
823 struct irq_bypass_producer *prod)
824 {
825 struct kvm_kernel_irqfd *irqfd =
826 container_of(cons, struct kvm_kernel_irqfd, consumer);
827 struct kvm *kvm = irqfd->kvm;
828
829 if (kvm->arch.kvm_ops->irq_bypass_add_producer)
830 return kvm->arch.kvm_ops->irq_bypass_add_producer(cons, prod);
831
832 return 0;
833 }
834
kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer * cons,struct irq_bypass_producer * prod)835 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
836 struct irq_bypass_producer *prod)
837 {
838 struct kvm_kernel_irqfd *irqfd =
839 container_of(cons, struct kvm_kernel_irqfd, consumer);
840 struct kvm *kvm = irqfd->kvm;
841
842 if (kvm->arch.kvm_ops->irq_bypass_del_producer)
843 kvm->arch.kvm_ops->irq_bypass_del_producer(cons, prod);
844 }
845
846 #ifdef CONFIG_VSX
kvmppc_get_vsr_dword_offset(int index)847 static inline int kvmppc_get_vsr_dword_offset(int index)
848 {
849 int offset;
850
851 if ((index != 0) && (index != 1))
852 return -1;
853
854 #ifdef __BIG_ENDIAN
855 offset = index;
856 #else
857 offset = 1 - index;
858 #endif
859
860 return offset;
861 }
862
kvmppc_get_vsr_word_offset(int index)863 static inline int kvmppc_get_vsr_word_offset(int index)
864 {
865 int offset;
866
867 if ((index > 3) || (index < 0))
868 return -1;
869
870 #ifdef __BIG_ENDIAN
871 offset = index;
872 #else
873 offset = 3 - index;
874 #endif
875 return offset;
876 }
877
kvmppc_set_vsr_dword(struct kvm_vcpu * vcpu,u64 gpr)878 static inline void kvmppc_set_vsr_dword(struct kvm_vcpu *vcpu,
879 u64 gpr)
880 {
881 union kvmppc_one_reg val;
882 int offset = kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
883 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
884
885 if (offset == -1)
886 return;
887
888 if (vcpu->arch.mmio_vsx_tx_sx_enabled) {
889 val.vval = VCPU_VSX_VR(vcpu, index);
890 val.vsxval[offset] = gpr;
891 VCPU_VSX_VR(vcpu, index) = val.vval;
892 } else {
893 VCPU_VSX_FPR(vcpu, index, offset) = gpr;
894 }
895 }
896
kvmppc_set_vsr_dword_dump(struct kvm_vcpu * vcpu,u64 gpr)897 static inline void kvmppc_set_vsr_dword_dump(struct kvm_vcpu *vcpu,
898 u64 gpr)
899 {
900 union kvmppc_one_reg val;
901 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
902
903 if (vcpu->arch.mmio_vsx_tx_sx_enabled) {
904 val.vval = VCPU_VSX_VR(vcpu, index);
905 val.vsxval[0] = gpr;
906 val.vsxval[1] = gpr;
907 VCPU_VSX_VR(vcpu, index) = val.vval;
908 } else {
909 VCPU_VSX_FPR(vcpu, index, 0) = gpr;
910 VCPU_VSX_FPR(vcpu, index, 1) = gpr;
911 }
912 }
913
kvmppc_set_vsr_word(struct kvm_vcpu * vcpu,u32 gpr32)914 static inline void kvmppc_set_vsr_word(struct kvm_vcpu *vcpu,
915 u32 gpr32)
916 {
917 union kvmppc_one_reg val;
918 int offset = kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
919 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
920 int dword_offset, word_offset;
921
922 if (offset == -1)
923 return;
924
925 if (vcpu->arch.mmio_vsx_tx_sx_enabled) {
926 val.vval = VCPU_VSX_VR(vcpu, index);
927 val.vsx32val[offset] = gpr32;
928 VCPU_VSX_VR(vcpu, index) = val.vval;
929 } else {
930 dword_offset = offset / 2;
931 word_offset = offset % 2;
932 val.vsxval[0] = VCPU_VSX_FPR(vcpu, index, dword_offset);
933 val.vsx32val[word_offset] = gpr32;
934 VCPU_VSX_FPR(vcpu, index, dword_offset) = val.vsxval[0];
935 }
936 }
937 #endif /* CONFIG_VSX */
938
939 #ifdef CONFIG_PPC_FPU
sp_to_dp(u32 fprs)940 static inline u64 sp_to_dp(u32 fprs)
941 {
942 u64 fprd;
943
944 preempt_disable();
945 enable_kernel_fp();
946 asm ("lfs%U1%X1 0,%1; stfd%U0%X0 0,%0" : "=m" (fprd) : "m" (fprs)
947 : "fr0");
948 preempt_enable();
949 return fprd;
950 }
951
dp_to_sp(u64 fprd)952 static inline u32 dp_to_sp(u64 fprd)
953 {
954 u32 fprs;
955
956 preempt_disable();
957 enable_kernel_fp();
958 asm ("lfd%U1%X1 0,%1; stfs%U0%X0 0,%0" : "=m" (fprs) : "m" (fprd)
959 : "fr0");
960 preempt_enable();
961 return fprs;
962 }
963
964 #else
965 #define sp_to_dp(x) (x)
966 #define dp_to_sp(x) (x)
967 #endif /* CONFIG_PPC_FPU */
968
kvmppc_complete_mmio_load(struct kvm_vcpu * vcpu,struct kvm_run * run)969 static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
970 struct kvm_run *run)
971 {
972 u64 uninitialized_var(gpr);
973
974 if (run->mmio.len > sizeof(gpr)) {
975 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
976 return;
977 }
978
979 if (!vcpu->arch.mmio_host_swabbed) {
980 switch (run->mmio.len) {
981 case 8: gpr = *(u64 *)run->mmio.data; break;
982 case 4: gpr = *(u32 *)run->mmio.data; break;
983 case 2: gpr = *(u16 *)run->mmio.data; break;
984 case 1: gpr = *(u8 *)run->mmio.data; break;
985 }
986 } else {
987 switch (run->mmio.len) {
988 case 8: gpr = swab64(*(u64 *)run->mmio.data); break;
989 case 4: gpr = swab32(*(u32 *)run->mmio.data); break;
990 case 2: gpr = swab16(*(u16 *)run->mmio.data); break;
991 case 1: gpr = *(u8 *)run->mmio.data; break;
992 }
993 }
994
995 /* conversion between single and double precision */
996 if ((vcpu->arch.mmio_sp64_extend) && (run->mmio.len == 4))
997 gpr = sp_to_dp(gpr);
998
999 if (vcpu->arch.mmio_sign_extend) {
1000 switch (run->mmio.len) {
1001 #ifdef CONFIG_PPC64
1002 case 4:
1003 gpr = (s64)(s32)gpr;
1004 break;
1005 #endif
1006 case 2:
1007 gpr = (s64)(s16)gpr;
1008 break;
1009 case 1:
1010 gpr = (s64)(s8)gpr;
1011 break;
1012 }
1013 }
1014
1015 switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
1016 case KVM_MMIO_REG_GPR:
1017 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
1018 break;
1019 case KVM_MMIO_REG_FPR:
1020 VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
1021 break;
1022 #ifdef CONFIG_PPC_BOOK3S
1023 case KVM_MMIO_REG_QPR:
1024 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
1025 break;
1026 case KVM_MMIO_REG_FQPR:
1027 VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
1028 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
1029 break;
1030 #endif
1031 #ifdef CONFIG_VSX
1032 case KVM_MMIO_REG_VSX:
1033 if (vcpu->arch.mmio_vsx_copy_type == KVMPPC_VSX_COPY_DWORD)
1034 kvmppc_set_vsr_dword(vcpu, gpr);
1035 else if (vcpu->arch.mmio_vsx_copy_type == KVMPPC_VSX_COPY_WORD)
1036 kvmppc_set_vsr_word(vcpu, gpr);
1037 else if (vcpu->arch.mmio_vsx_copy_type ==
1038 KVMPPC_VSX_COPY_DWORD_LOAD_DUMP)
1039 kvmppc_set_vsr_dword_dump(vcpu, gpr);
1040 break;
1041 #endif
1042 default:
1043 BUG();
1044 }
1045 }
1046
__kvmppc_handle_load(struct kvm_run * run,struct kvm_vcpu * vcpu,unsigned int rt,unsigned int bytes,int is_default_endian,int sign_extend)1047 static int __kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1048 unsigned int rt, unsigned int bytes,
1049 int is_default_endian, int sign_extend)
1050 {
1051 int idx, ret;
1052 bool host_swabbed;
1053
1054 /* Pity C doesn't have a logical XOR operator */
1055 if (kvmppc_need_byteswap(vcpu)) {
1056 host_swabbed = is_default_endian;
1057 } else {
1058 host_swabbed = !is_default_endian;
1059 }
1060
1061 if (bytes > sizeof(run->mmio.data)) {
1062 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
1063 run->mmio.len);
1064 }
1065
1066 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
1067 run->mmio.len = bytes;
1068 run->mmio.is_write = 0;
1069
1070 vcpu->arch.io_gpr = rt;
1071 vcpu->arch.mmio_host_swabbed = host_swabbed;
1072 vcpu->mmio_needed = 1;
1073 vcpu->mmio_is_write = 0;
1074 vcpu->arch.mmio_sign_extend = sign_extend;
1075
1076 idx = srcu_read_lock(&vcpu->kvm->srcu);
1077
1078 ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
1079 bytes, &run->mmio.data);
1080
1081 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1082
1083 if (!ret) {
1084 kvmppc_complete_mmio_load(vcpu, run);
1085 vcpu->mmio_needed = 0;
1086 return EMULATE_DONE;
1087 }
1088
1089 return EMULATE_DO_MMIO;
1090 }
1091
kvmppc_handle_load(struct kvm_run * run,struct kvm_vcpu * vcpu,unsigned int rt,unsigned int bytes,int is_default_endian)1092 int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1093 unsigned int rt, unsigned int bytes,
1094 int is_default_endian)
1095 {
1096 return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 0);
1097 }
1098 EXPORT_SYMBOL_GPL(kvmppc_handle_load);
1099
1100 /* Same as above, but sign extends */
kvmppc_handle_loads(struct kvm_run * run,struct kvm_vcpu * vcpu,unsigned int rt,unsigned int bytes,int is_default_endian)1101 int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
1102 unsigned int rt, unsigned int bytes,
1103 int is_default_endian)
1104 {
1105 return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 1);
1106 }
1107
1108 #ifdef CONFIG_VSX
kvmppc_handle_vsx_load(struct kvm_run * run,struct kvm_vcpu * vcpu,unsigned int rt,unsigned int bytes,int is_default_endian,int mmio_sign_extend)1109 int kvmppc_handle_vsx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1110 unsigned int rt, unsigned int bytes,
1111 int is_default_endian, int mmio_sign_extend)
1112 {
1113 enum emulation_result emulated = EMULATE_DONE;
1114
1115 /* Currently, mmio_vsx_copy_nums only allowed to be less than 4 */
1116 if ( (vcpu->arch.mmio_vsx_copy_nums > 4) ||
1117 (vcpu->arch.mmio_vsx_copy_nums < 0) ) {
1118 return EMULATE_FAIL;
1119 }
1120
1121 while (vcpu->arch.mmio_vsx_copy_nums) {
1122 emulated = __kvmppc_handle_load(run, vcpu, rt, bytes,
1123 is_default_endian, mmio_sign_extend);
1124
1125 if (emulated != EMULATE_DONE)
1126 break;
1127
1128 vcpu->arch.paddr_accessed += run->mmio.len;
1129
1130 vcpu->arch.mmio_vsx_copy_nums--;
1131 vcpu->arch.mmio_vsx_offset++;
1132 }
1133 return emulated;
1134 }
1135 #endif /* CONFIG_VSX */
1136
kvmppc_handle_store(struct kvm_run * run,struct kvm_vcpu * vcpu,u64 val,unsigned int bytes,int is_default_endian)1137 int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
1138 u64 val, unsigned int bytes, int is_default_endian)
1139 {
1140 void *data = run->mmio.data;
1141 int idx, ret;
1142 bool host_swabbed;
1143
1144 /* Pity C doesn't have a logical XOR operator */
1145 if (kvmppc_need_byteswap(vcpu)) {
1146 host_swabbed = is_default_endian;
1147 } else {
1148 host_swabbed = !is_default_endian;
1149 }
1150
1151 if (bytes > sizeof(run->mmio.data)) {
1152 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
1153 run->mmio.len);
1154 }
1155
1156 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
1157 run->mmio.len = bytes;
1158 run->mmio.is_write = 1;
1159 vcpu->mmio_needed = 1;
1160 vcpu->mmio_is_write = 1;
1161
1162 if ((vcpu->arch.mmio_sp64_extend) && (bytes == 4))
1163 val = dp_to_sp(val);
1164
1165 /* Store the value at the lowest bytes in 'data'. */
1166 if (!host_swabbed) {
1167 switch (bytes) {
1168 case 8: *(u64 *)data = val; break;
1169 case 4: *(u32 *)data = val; break;
1170 case 2: *(u16 *)data = val; break;
1171 case 1: *(u8 *)data = val; break;
1172 }
1173 } else {
1174 switch (bytes) {
1175 case 8: *(u64 *)data = swab64(val); break;
1176 case 4: *(u32 *)data = swab32(val); break;
1177 case 2: *(u16 *)data = swab16(val); break;
1178 case 1: *(u8 *)data = val; break;
1179 }
1180 }
1181
1182 idx = srcu_read_lock(&vcpu->kvm->srcu);
1183
1184 ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
1185 bytes, &run->mmio.data);
1186
1187 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1188
1189 if (!ret) {
1190 vcpu->mmio_needed = 0;
1191 return EMULATE_DONE;
1192 }
1193
1194 return EMULATE_DO_MMIO;
1195 }
1196 EXPORT_SYMBOL_GPL(kvmppc_handle_store);
1197
1198 #ifdef CONFIG_VSX
kvmppc_get_vsr_data(struct kvm_vcpu * vcpu,int rs,u64 * val)1199 static inline int kvmppc_get_vsr_data(struct kvm_vcpu *vcpu, int rs, u64 *val)
1200 {
1201 u32 dword_offset, word_offset;
1202 union kvmppc_one_reg reg;
1203 int vsx_offset = 0;
1204 int copy_type = vcpu->arch.mmio_vsx_copy_type;
1205 int result = 0;
1206
1207 switch (copy_type) {
1208 case KVMPPC_VSX_COPY_DWORD:
1209 vsx_offset =
1210 kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
1211
1212 if (vsx_offset == -1) {
1213 result = -1;
1214 break;
1215 }
1216
1217 if (!vcpu->arch.mmio_vsx_tx_sx_enabled) {
1218 *val = VCPU_VSX_FPR(vcpu, rs, vsx_offset);
1219 } else {
1220 reg.vval = VCPU_VSX_VR(vcpu, rs);
1221 *val = reg.vsxval[vsx_offset];
1222 }
1223 break;
1224
1225 case KVMPPC_VSX_COPY_WORD:
1226 vsx_offset =
1227 kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
1228
1229 if (vsx_offset == -1) {
1230 result = -1;
1231 break;
1232 }
1233
1234 if (!vcpu->arch.mmio_vsx_tx_sx_enabled) {
1235 dword_offset = vsx_offset / 2;
1236 word_offset = vsx_offset % 2;
1237 reg.vsxval[0] = VCPU_VSX_FPR(vcpu, rs, dword_offset);
1238 *val = reg.vsx32val[word_offset];
1239 } else {
1240 reg.vval = VCPU_VSX_VR(vcpu, rs);
1241 *val = reg.vsx32val[vsx_offset];
1242 }
1243 break;
1244
1245 default:
1246 result = -1;
1247 break;
1248 }
1249
1250 return result;
1251 }
1252
kvmppc_handle_vsx_store(struct kvm_run * run,struct kvm_vcpu * vcpu,int rs,unsigned int bytes,int is_default_endian)1253 int kvmppc_handle_vsx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
1254 int rs, unsigned int bytes, int is_default_endian)
1255 {
1256 u64 val;
1257 enum emulation_result emulated = EMULATE_DONE;
1258
1259 vcpu->arch.io_gpr = rs;
1260
1261 /* Currently, mmio_vsx_copy_nums only allowed to be less than 4 */
1262 if ( (vcpu->arch.mmio_vsx_copy_nums > 4) ||
1263 (vcpu->arch.mmio_vsx_copy_nums < 0) ) {
1264 return EMULATE_FAIL;
1265 }
1266
1267 while (vcpu->arch.mmio_vsx_copy_nums) {
1268 if (kvmppc_get_vsr_data(vcpu, rs, &val) == -1)
1269 return EMULATE_FAIL;
1270
1271 emulated = kvmppc_handle_store(run, vcpu,
1272 val, bytes, is_default_endian);
1273
1274 if (emulated != EMULATE_DONE)
1275 break;
1276
1277 vcpu->arch.paddr_accessed += run->mmio.len;
1278
1279 vcpu->arch.mmio_vsx_copy_nums--;
1280 vcpu->arch.mmio_vsx_offset++;
1281 }
1282
1283 return emulated;
1284 }
1285
kvmppc_emulate_mmio_vsx_loadstore(struct kvm_vcpu * vcpu,struct kvm_run * run)1286 static int kvmppc_emulate_mmio_vsx_loadstore(struct kvm_vcpu *vcpu,
1287 struct kvm_run *run)
1288 {
1289 enum emulation_result emulated = EMULATE_FAIL;
1290 int r;
1291
1292 vcpu->arch.paddr_accessed += run->mmio.len;
1293
1294 if (!vcpu->mmio_is_write) {
1295 emulated = kvmppc_handle_vsx_load(run, vcpu, vcpu->arch.io_gpr,
1296 run->mmio.len, 1, vcpu->arch.mmio_sign_extend);
1297 } else {
1298 emulated = kvmppc_handle_vsx_store(run, vcpu,
1299 vcpu->arch.io_gpr, run->mmio.len, 1);
1300 }
1301
1302 switch (emulated) {
1303 case EMULATE_DO_MMIO:
1304 run->exit_reason = KVM_EXIT_MMIO;
1305 r = RESUME_HOST;
1306 break;
1307 case EMULATE_FAIL:
1308 pr_info("KVM: MMIO emulation failed (VSX repeat)\n");
1309 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1310 run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
1311 r = RESUME_HOST;
1312 break;
1313 default:
1314 r = RESUME_GUEST;
1315 break;
1316 }
1317 return r;
1318 }
1319 #endif /* CONFIG_VSX */
1320
kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu * vcpu,struct kvm_one_reg * reg)1321 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1322 {
1323 int r = 0;
1324 union kvmppc_one_reg val;
1325 int size;
1326
1327 size = one_reg_size(reg->id);
1328 if (size > sizeof(val))
1329 return -EINVAL;
1330
1331 r = kvmppc_get_one_reg(vcpu, reg->id, &val);
1332 if (r == -EINVAL) {
1333 r = 0;
1334 switch (reg->id) {
1335 #ifdef CONFIG_ALTIVEC
1336 case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
1337 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1338 r = -ENXIO;
1339 break;
1340 }
1341 val.vval = vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0];
1342 break;
1343 case KVM_REG_PPC_VSCR:
1344 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1345 r = -ENXIO;
1346 break;
1347 }
1348 val = get_reg_val(reg->id, vcpu->arch.vr.vscr.u[3]);
1349 break;
1350 case KVM_REG_PPC_VRSAVE:
1351 val = get_reg_val(reg->id, vcpu->arch.vrsave);
1352 break;
1353 #endif /* CONFIG_ALTIVEC */
1354 default:
1355 r = -EINVAL;
1356 break;
1357 }
1358 }
1359
1360 if (r)
1361 return r;
1362
1363 if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
1364 r = -EFAULT;
1365
1366 return r;
1367 }
1368
kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu * vcpu,struct kvm_one_reg * reg)1369 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1370 {
1371 int r;
1372 union kvmppc_one_reg val;
1373 int size;
1374
1375 size = one_reg_size(reg->id);
1376 if (size > sizeof(val))
1377 return -EINVAL;
1378
1379 if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
1380 return -EFAULT;
1381
1382 r = kvmppc_set_one_reg(vcpu, reg->id, &val);
1383 if (r == -EINVAL) {
1384 r = 0;
1385 switch (reg->id) {
1386 #ifdef CONFIG_ALTIVEC
1387 case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
1388 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1389 r = -ENXIO;
1390 break;
1391 }
1392 vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0] = val.vval;
1393 break;
1394 case KVM_REG_PPC_VSCR:
1395 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1396 r = -ENXIO;
1397 break;
1398 }
1399 vcpu->arch.vr.vscr.u[3] = set_reg_val(reg->id, val);
1400 break;
1401 case KVM_REG_PPC_VRSAVE:
1402 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1403 r = -ENXIO;
1404 break;
1405 }
1406 vcpu->arch.vrsave = set_reg_val(reg->id, val);
1407 break;
1408 #endif /* CONFIG_ALTIVEC */
1409 default:
1410 r = -EINVAL;
1411 break;
1412 }
1413 }
1414
1415 return r;
1416 }
1417
kvm_arch_vcpu_ioctl_run(struct kvm_vcpu * vcpu,struct kvm_run * run)1418 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
1419 {
1420 int r;
1421
1422 if (vcpu->mmio_needed) {
1423 vcpu->mmio_needed = 0;
1424 if (!vcpu->mmio_is_write)
1425 kvmppc_complete_mmio_load(vcpu, run);
1426 #ifdef CONFIG_VSX
1427 if (vcpu->arch.mmio_vsx_copy_nums > 0) {
1428 vcpu->arch.mmio_vsx_copy_nums--;
1429 vcpu->arch.mmio_vsx_offset++;
1430 }
1431
1432 if (vcpu->arch.mmio_vsx_copy_nums > 0) {
1433 r = kvmppc_emulate_mmio_vsx_loadstore(vcpu, run);
1434 if (r == RESUME_HOST) {
1435 vcpu->mmio_needed = 1;
1436 return r;
1437 }
1438 }
1439 #endif
1440 } else if (vcpu->arch.osi_needed) {
1441 u64 *gprs = run->osi.gprs;
1442 int i;
1443
1444 for (i = 0; i < 32; i++)
1445 kvmppc_set_gpr(vcpu, i, gprs[i]);
1446 vcpu->arch.osi_needed = 0;
1447 } else if (vcpu->arch.hcall_needed) {
1448 int i;
1449
1450 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
1451 for (i = 0; i < 9; ++i)
1452 kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
1453 vcpu->arch.hcall_needed = 0;
1454 #ifdef CONFIG_BOOKE
1455 } else if (vcpu->arch.epr_needed) {
1456 kvmppc_set_epr(vcpu, run->epr.epr);
1457 vcpu->arch.epr_needed = 0;
1458 #endif
1459 }
1460
1461 kvm_sigset_activate(vcpu);
1462
1463 if (run->immediate_exit)
1464 r = -EINTR;
1465 else
1466 r = kvmppc_vcpu_run(run, vcpu);
1467
1468 kvm_sigset_deactivate(vcpu);
1469
1470 return r;
1471 }
1472
kvm_vcpu_ioctl_interrupt(struct kvm_vcpu * vcpu,struct kvm_interrupt * irq)1473 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
1474 {
1475 if (irq->irq == KVM_INTERRUPT_UNSET) {
1476 kvmppc_core_dequeue_external(vcpu);
1477 return 0;
1478 }
1479
1480 kvmppc_core_queue_external(vcpu, irq);
1481
1482 kvm_vcpu_kick(vcpu);
1483
1484 return 0;
1485 }
1486
kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu * vcpu,struct kvm_enable_cap * cap)1487 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1488 struct kvm_enable_cap *cap)
1489 {
1490 int r;
1491
1492 if (cap->flags)
1493 return -EINVAL;
1494
1495 switch (cap->cap) {
1496 case KVM_CAP_PPC_OSI:
1497 r = 0;
1498 vcpu->arch.osi_enabled = true;
1499 break;
1500 case KVM_CAP_PPC_PAPR:
1501 r = 0;
1502 vcpu->arch.papr_enabled = true;
1503 break;
1504 case KVM_CAP_PPC_EPR:
1505 r = 0;
1506 if (cap->args[0])
1507 vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
1508 else
1509 vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
1510 break;
1511 #ifdef CONFIG_BOOKE
1512 case KVM_CAP_PPC_BOOKE_WATCHDOG:
1513 r = 0;
1514 vcpu->arch.watchdog_enabled = true;
1515 break;
1516 #endif
1517 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
1518 case KVM_CAP_SW_TLB: {
1519 struct kvm_config_tlb cfg;
1520 void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
1521
1522 r = -EFAULT;
1523 if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
1524 break;
1525
1526 r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
1527 break;
1528 }
1529 #endif
1530 #ifdef CONFIG_KVM_MPIC
1531 case KVM_CAP_IRQ_MPIC: {
1532 struct fd f;
1533 struct kvm_device *dev;
1534
1535 r = -EBADF;
1536 f = fdget(cap->args[0]);
1537 if (!f.file)
1538 break;
1539
1540 r = -EPERM;
1541 dev = kvm_device_from_filp(f.file);
1542 if (dev)
1543 r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
1544
1545 fdput(f);
1546 break;
1547 }
1548 #endif
1549 #ifdef CONFIG_KVM_XICS
1550 case KVM_CAP_IRQ_XICS: {
1551 struct fd f;
1552 struct kvm_device *dev;
1553
1554 r = -EBADF;
1555 f = fdget(cap->args[0]);
1556 if (!f.file)
1557 break;
1558
1559 r = -EPERM;
1560 dev = kvm_device_from_filp(f.file);
1561 if (dev) {
1562 if (xive_enabled())
1563 r = kvmppc_xive_connect_vcpu(dev, vcpu, cap->args[1]);
1564 else
1565 r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
1566 }
1567
1568 fdput(f);
1569 break;
1570 }
1571 #endif /* CONFIG_KVM_XICS */
1572 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
1573 case KVM_CAP_PPC_FWNMI:
1574 r = -EINVAL;
1575 if (!is_kvmppc_hv_enabled(vcpu->kvm))
1576 break;
1577 r = 0;
1578 vcpu->kvm->arch.fwnmi_enabled = true;
1579 break;
1580 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
1581 default:
1582 r = -EINVAL;
1583 break;
1584 }
1585
1586 if (!r)
1587 r = kvmppc_sanity_check(vcpu);
1588
1589 return r;
1590 }
1591
kvm_arch_intc_initialized(struct kvm * kvm)1592 bool kvm_arch_intc_initialized(struct kvm *kvm)
1593 {
1594 #ifdef CONFIG_KVM_MPIC
1595 if (kvm->arch.mpic)
1596 return true;
1597 #endif
1598 #ifdef CONFIG_KVM_XICS
1599 if (kvm->arch.xics || kvm->arch.xive)
1600 return true;
1601 #endif
1602 return false;
1603 }
1604
kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu * vcpu,struct kvm_mp_state * mp_state)1605 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1606 struct kvm_mp_state *mp_state)
1607 {
1608 return -EINVAL;
1609 }
1610
kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu * vcpu,struct kvm_mp_state * mp_state)1611 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1612 struct kvm_mp_state *mp_state)
1613 {
1614 return -EINVAL;
1615 }
1616
kvm_arch_vcpu_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)1617 long kvm_arch_vcpu_ioctl(struct file *filp,
1618 unsigned int ioctl, unsigned long arg)
1619 {
1620 struct kvm_vcpu *vcpu = filp->private_data;
1621 void __user *argp = (void __user *)arg;
1622 long r;
1623
1624 switch (ioctl) {
1625 case KVM_INTERRUPT: {
1626 struct kvm_interrupt irq;
1627 r = -EFAULT;
1628 if (copy_from_user(&irq, argp, sizeof(irq)))
1629 goto out;
1630 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1631 goto out;
1632 }
1633
1634 case KVM_ENABLE_CAP:
1635 {
1636 struct kvm_enable_cap cap;
1637 r = -EFAULT;
1638 if (copy_from_user(&cap, argp, sizeof(cap)))
1639 goto out;
1640 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1641 break;
1642 }
1643
1644 case KVM_SET_ONE_REG:
1645 case KVM_GET_ONE_REG:
1646 {
1647 struct kvm_one_reg reg;
1648 r = -EFAULT;
1649 if (copy_from_user(®, argp, sizeof(reg)))
1650 goto out;
1651 if (ioctl == KVM_SET_ONE_REG)
1652 r = kvm_vcpu_ioctl_set_one_reg(vcpu, ®);
1653 else
1654 r = kvm_vcpu_ioctl_get_one_reg(vcpu, ®);
1655 break;
1656 }
1657
1658 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
1659 case KVM_DIRTY_TLB: {
1660 struct kvm_dirty_tlb dirty;
1661 r = -EFAULT;
1662 if (copy_from_user(&dirty, argp, sizeof(dirty)))
1663 goto out;
1664 r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
1665 break;
1666 }
1667 #endif
1668 default:
1669 r = -EINVAL;
1670 }
1671
1672 out:
1673 return r;
1674 }
1675
kvm_arch_vcpu_fault(struct kvm_vcpu * vcpu,struct vm_fault * vmf)1676 int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1677 {
1678 return VM_FAULT_SIGBUS;
1679 }
1680
kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo * pvinfo)1681 static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
1682 {
1683 u32 inst_nop = 0x60000000;
1684 #ifdef CONFIG_KVM_BOOKE_HV
1685 u32 inst_sc1 = 0x44000022;
1686 pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
1687 pvinfo->hcall[1] = cpu_to_be32(inst_nop);
1688 pvinfo->hcall[2] = cpu_to_be32(inst_nop);
1689 pvinfo->hcall[3] = cpu_to_be32(inst_nop);
1690 #else
1691 u32 inst_lis = 0x3c000000;
1692 u32 inst_ori = 0x60000000;
1693 u32 inst_sc = 0x44000002;
1694 u32 inst_imm_mask = 0xffff;
1695
1696 /*
1697 * The hypercall to get into KVM from within guest context is as
1698 * follows:
1699 *
1700 * lis r0, r0, KVM_SC_MAGIC_R0@h
1701 * ori r0, KVM_SC_MAGIC_R0@l
1702 * sc
1703 * nop
1704 */
1705 pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
1706 pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
1707 pvinfo->hcall[2] = cpu_to_be32(inst_sc);
1708 pvinfo->hcall[3] = cpu_to_be32(inst_nop);
1709 #endif
1710
1711 pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
1712
1713 return 0;
1714 }
1715
kvm_vm_ioctl_irq_line(struct kvm * kvm,struct kvm_irq_level * irq_event,bool line_status)1716 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
1717 bool line_status)
1718 {
1719 if (!irqchip_in_kernel(kvm))
1720 return -ENXIO;
1721
1722 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
1723 irq_event->irq, irq_event->level,
1724 line_status);
1725 return 0;
1726 }
1727
1728
kvm_vm_ioctl_enable_cap(struct kvm * kvm,struct kvm_enable_cap * cap)1729 static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
1730 struct kvm_enable_cap *cap)
1731 {
1732 int r;
1733
1734 if (cap->flags)
1735 return -EINVAL;
1736
1737 switch (cap->cap) {
1738 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
1739 case KVM_CAP_PPC_ENABLE_HCALL: {
1740 unsigned long hcall = cap->args[0];
1741
1742 r = -EINVAL;
1743 if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
1744 cap->args[1] > 1)
1745 break;
1746 if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
1747 break;
1748 if (cap->args[1])
1749 set_bit(hcall / 4, kvm->arch.enabled_hcalls);
1750 else
1751 clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
1752 r = 0;
1753 break;
1754 }
1755 case KVM_CAP_PPC_SMT: {
1756 unsigned long mode = cap->args[0];
1757 unsigned long flags = cap->args[1];
1758
1759 r = -EINVAL;
1760 if (kvm->arch.kvm_ops->set_smt_mode)
1761 r = kvm->arch.kvm_ops->set_smt_mode(kvm, mode, flags);
1762 break;
1763 }
1764 #endif
1765 default:
1766 r = -EINVAL;
1767 break;
1768 }
1769
1770 return r;
1771 }
1772
kvm_arch_vm_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)1773 long kvm_arch_vm_ioctl(struct file *filp,
1774 unsigned int ioctl, unsigned long arg)
1775 {
1776 struct kvm *kvm __maybe_unused = filp->private_data;
1777 void __user *argp = (void __user *)arg;
1778 long r;
1779
1780 switch (ioctl) {
1781 case KVM_PPC_GET_PVINFO: {
1782 struct kvm_ppc_pvinfo pvinfo;
1783 memset(&pvinfo, 0, sizeof(pvinfo));
1784 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
1785 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
1786 r = -EFAULT;
1787 goto out;
1788 }
1789
1790 break;
1791 }
1792 case KVM_ENABLE_CAP:
1793 {
1794 struct kvm_enable_cap cap;
1795 r = -EFAULT;
1796 if (copy_from_user(&cap, argp, sizeof(cap)))
1797 goto out;
1798 r = kvm_vm_ioctl_enable_cap(kvm, &cap);
1799 break;
1800 }
1801 #ifdef CONFIG_SPAPR_TCE_IOMMU
1802 case KVM_CREATE_SPAPR_TCE_64: {
1803 struct kvm_create_spapr_tce_64 create_tce_64;
1804
1805 r = -EFAULT;
1806 if (copy_from_user(&create_tce_64, argp, sizeof(create_tce_64)))
1807 goto out;
1808 if (create_tce_64.flags) {
1809 r = -EINVAL;
1810 goto out;
1811 }
1812 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
1813 goto out;
1814 }
1815 case KVM_CREATE_SPAPR_TCE: {
1816 struct kvm_create_spapr_tce create_tce;
1817 struct kvm_create_spapr_tce_64 create_tce_64;
1818
1819 r = -EFAULT;
1820 if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
1821 goto out;
1822
1823 create_tce_64.liobn = create_tce.liobn;
1824 create_tce_64.page_shift = IOMMU_PAGE_SHIFT_4K;
1825 create_tce_64.offset = 0;
1826 create_tce_64.size = create_tce.window_size >>
1827 IOMMU_PAGE_SHIFT_4K;
1828 create_tce_64.flags = 0;
1829 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
1830 goto out;
1831 }
1832 #endif
1833 #ifdef CONFIG_PPC_BOOK3S_64
1834 case KVM_PPC_GET_SMMU_INFO: {
1835 struct kvm_ppc_smmu_info info;
1836 struct kvm *kvm = filp->private_data;
1837
1838 memset(&info, 0, sizeof(info));
1839 r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
1840 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
1841 r = -EFAULT;
1842 break;
1843 }
1844 case KVM_PPC_RTAS_DEFINE_TOKEN: {
1845 struct kvm *kvm = filp->private_data;
1846
1847 r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
1848 break;
1849 }
1850 case KVM_PPC_CONFIGURE_V3_MMU: {
1851 struct kvm *kvm = filp->private_data;
1852 struct kvm_ppc_mmuv3_cfg cfg;
1853
1854 r = -EINVAL;
1855 if (!kvm->arch.kvm_ops->configure_mmu)
1856 goto out;
1857 r = -EFAULT;
1858 if (copy_from_user(&cfg, argp, sizeof(cfg)))
1859 goto out;
1860 r = kvm->arch.kvm_ops->configure_mmu(kvm, &cfg);
1861 break;
1862 }
1863 case KVM_PPC_GET_RMMU_INFO: {
1864 struct kvm *kvm = filp->private_data;
1865 struct kvm_ppc_rmmu_info info;
1866
1867 r = -EINVAL;
1868 if (!kvm->arch.kvm_ops->get_rmmu_info)
1869 goto out;
1870 r = kvm->arch.kvm_ops->get_rmmu_info(kvm, &info);
1871 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
1872 r = -EFAULT;
1873 break;
1874 }
1875 default: {
1876 struct kvm *kvm = filp->private_data;
1877 r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
1878 }
1879 #else /* CONFIG_PPC_BOOK3S_64 */
1880 default:
1881 r = -ENOTTY;
1882 #endif
1883 }
1884 out:
1885 return r;
1886 }
1887
1888 static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
1889 static unsigned long nr_lpids;
1890
kvmppc_alloc_lpid(void)1891 long kvmppc_alloc_lpid(void)
1892 {
1893 long lpid;
1894
1895 do {
1896 lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
1897 if (lpid >= nr_lpids) {
1898 pr_err("%s: No LPIDs free\n", __func__);
1899 return -ENOMEM;
1900 }
1901 } while (test_and_set_bit(lpid, lpid_inuse));
1902
1903 return lpid;
1904 }
1905 EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
1906
kvmppc_claim_lpid(long lpid)1907 void kvmppc_claim_lpid(long lpid)
1908 {
1909 set_bit(lpid, lpid_inuse);
1910 }
1911 EXPORT_SYMBOL_GPL(kvmppc_claim_lpid);
1912
kvmppc_free_lpid(long lpid)1913 void kvmppc_free_lpid(long lpid)
1914 {
1915 clear_bit(lpid, lpid_inuse);
1916 }
1917 EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
1918
kvmppc_init_lpid(unsigned long nr_lpids_param)1919 void kvmppc_init_lpid(unsigned long nr_lpids_param)
1920 {
1921 nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
1922 memset(lpid_inuse, 0, sizeof(lpid_inuse));
1923 }
1924 EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
1925
kvm_arch_init(void * opaque)1926 int kvm_arch_init(void *opaque)
1927 {
1928 return 0;
1929 }
1930
1931 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);
1932