1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 *
4 * Copyright IBM Corp. 2007
5 *
6 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
7 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
8 */
9
10 #include <linux/errno.h>
11 #include <linux/err.h>
12 #include <linux/kvm_host.h>
13 #include <linux/vmalloc.h>
14 #include <linux/hrtimer.h>
15 #include <linux/sched/signal.h>
16 #include <linux/fs.h>
17 #include <linux/slab.h>
18 #include <linux/file.h>
19 #include <linux/module.h>
20 #include <linux/irqbypass.h>
21 #include <linux/kvm_irqfd.h>
22 #include <linux/of.h>
23 #include <asm/cputable.h>
24 #include <linux/uaccess.h>
25 #include <asm/kvm_ppc.h>
26 #include <asm/cputhreads.h>
27 #include <asm/irqflags.h>
28 #include <asm/iommu.h>
29 #include <asm/switch_to.h>
30 #include <asm/xive.h>
31 #ifdef CONFIG_PPC_PSERIES
32 #include <asm/hvcall.h>
33 #include <asm/plpar_wrappers.h>
34 #endif
35 #include <asm/ultravisor.h>
36 #include <asm/setup.h>
37
38 #include "timing.h"
39 #include "../mm/mmu_decl.h"
40
41 #define CREATE_TRACE_POINTS
42 #include "trace.h"
43
44 struct kvmppc_ops *kvmppc_hv_ops;
45 EXPORT_SYMBOL_GPL(kvmppc_hv_ops);
46 struct kvmppc_ops *kvmppc_pr_ops;
47 EXPORT_SYMBOL_GPL(kvmppc_pr_ops);
48
49
kvm_arch_vcpu_runnable(struct kvm_vcpu * v)50 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
51 {
52 return !!(v->arch.pending_exceptions) || kvm_request_pending(v);
53 }
54
kvm_arch_dy_runnable(struct kvm_vcpu * vcpu)55 bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
56 {
57 return kvm_arch_vcpu_runnable(vcpu);
58 }
59
kvm_arch_vcpu_in_kernel(struct kvm_vcpu * vcpu)60 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
61 {
62 return false;
63 }
64
kvm_arch_vcpu_should_kick(struct kvm_vcpu * vcpu)65 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
66 {
67 return 1;
68 }
69
70 /*
71 * Common checks before entering the guest world. Call with interrupts
72 * disabled.
73 *
74 * returns:
75 *
76 * == 1 if we're ready to go into guest state
77 * <= 0 if we need to go back to the host with return value
78 */
kvmppc_prepare_to_enter(struct kvm_vcpu * vcpu)79 int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
80 {
81 int r;
82
83 WARN_ON(irqs_disabled());
84 hard_irq_disable();
85
86 while (true) {
87 if (need_resched()) {
88 local_irq_enable();
89 cond_resched();
90 hard_irq_disable();
91 continue;
92 }
93
94 if (signal_pending(current)) {
95 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
96 vcpu->run->exit_reason = KVM_EXIT_INTR;
97 r = -EINTR;
98 break;
99 }
100
101 vcpu->mode = IN_GUEST_MODE;
102
103 /*
104 * Reading vcpu->requests must happen after setting vcpu->mode,
105 * so we don't miss a request because the requester sees
106 * OUTSIDE_GUEST_MODE and assumes we'll be checking requests
107 * before next entering the guest (and thus doesn't IPI).
108 * This also orders the write to mode from any reads
109 * to the page tables done while the VCPU is running.
110 * Please see the comment in kvm_flush_remote_tlbs.
111 */
112 smp_mb();
113
114 if (kvm_request_pending(vcpu)) {
115 /* Make sure we process requests preemptable */
116 local_irq_enable();
117 trace_kvm_check_requests(vcpu);
118 r = kvmppc_core_check_requests(vcpu);
119 hard_irq_disable();
120 if (r > 0)
121 continue;
122 break;
123 }
124
125 if (kvmppc_core_prepare_to_enter(vcpu)) {
126 /* interrupts got enabled in between, so we
127 are back at square 1 */
128 continue;
129 }
130
131 guest_enter_irqoff();
132 return 1;
133 }
134
135 /* return to host */
136 local_irq_enable();
137 return r;
138 }
139 EXPORT_SYMBOL_GPL(kvmppc_prepare_to_enter);
140
141 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
kvmppc_swab_shared(struct kvm_vcpu * vcpu)142 static void kvmppc_swab_shared(struct kvm_vcpu *vcpu)
143 {
144 struct kvm_vcpu_arch_shared *shared = vcpu->arch.shared;
145 int i;
146
147 shared->sprg0 = swab64(shared->sprg0);
148 shared->sprg1 = swab64(shared->sprg1);
149 shared->sprg2 = swab64(shared->sprg2);
150 shared->sprg3 = swab64(shared->sprg3);
151 shared->srr0 = swab64(shared->srr0);
152 shared->srr1 = swab64(shared->srr1);
153 shared->dar = swab64(shared->dar);
154 shared->msr = swab64(shared->msr);
155 shared->dsisr = swab32(shared->dsisr);
156 shared->int_pending = swab32(shared->int_pending);
157 for (i = 0; i < ARRAY_SIZE(shared->sr); i++)
158 shared->sr[i] = swab32(shared->sr[i]);
159 }
160 #endif
161
kvmppc_kvm_pv(struct kvm_vcpu * vcpu)162 int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
163 {
164 int nr = kvmppc_get_gpr(vcpu, 11);
165 int r;
166 unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
167 unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
168 unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
169 unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
170 unsigned long r2 = 0;
171
172 if (!(kvmppc_get_msr(vcpu) & MSR_SF)) {
173 /* 32 bit mode */
174 param1 &= 0xffffffff;
175 param2 &= 0xffffffff;
176 param3 &= 0xffffffff;
177 param4 &= 0xffffffff;
178 }
179
180 switch (nr) {
181 case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE):
182 {
183 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
184 /* Book3S can be little endian, find it out here */
185 int shared_big_endian = true;
186 if (vcpu->arch.intr_msr & MSR_LE)
187 shared_big_endian = false;
188 if (shared_big_endian != vcpu->arch.shared_big_endian)
189 kvmppc_swab_shared(vcpu);
190 vcpu->arch.shared_big_endian = shared_big_endian;
191 #endif
192
193 if (!(param2 & MAGIC_PAGE_FLAG_NOT_MAPPED_NX)) {
194 /*
195 * Older versions of the Linux magic page code had
196 * a bug where they would map their trampoline code
197 * NX. If that's the case, remove !PR NX capability.
198 */
199 vcpu->arch.disable_kernel_nx = true;
200 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
201 }
202
203 vcpu->arch.magic_page_pa = param1 & ~0xfffULL;
204 vcpu->arch.magic_page_ea = param2 & ~0xfffULL;
205
206 #ifdef CONFIG_PPC_64K_PAGES
207 /*
208 * Make sure our 4k magic page is in the same window of a 64k
209 * page within the guest and within the host's page.
210 */
211 if ((vcpu->arch.magic_page_pa & 0xf000) !=
212 ((ulong)vcpu->arch.shared & 0xf000)) {
213 void *old_shared = vcpu->arch.shared;
214 ulong shared = (ulong)vcpu->arch.shared;
215 void *new_shared;
216
217 shared &= PAGE_MASK;
218 shared |= vcpu->arch.magic_page_pa & 0xf000;
219 new_shared = (void*)shared;
220 memcpy(new_shared, old_shared, 0x1000);
221 vcpu->arch.shared = new_shared;
222 }
223 #endif
224
225 r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
226
227 r = EV_SUCCESS;
228 break;
229 }
230 case KVM_HCALL_TOKEN(KVM_HC_FEATURES):
231 r = EV_SUCCESS;
232 #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2)
233 r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
234 #endif
235
236 /* Second return value is in r4 */
237 break;
238 case EV_HCALL_TOKEN(EV_IDLE):
239 r = EV_SUCCESS;
240 kvm_vcpu_halt(vcpu);
241 break;
242 default:
243 r = EV_UNIMPLEMENTED;
244 break;
245 }
246
247 kvmppc_set_gpr(vcpu, 4, r2);
248
249 return r;
250 }
251 EXPORT_SYMBOL_GPL(kvmppc_kvm_pv);
252
kvmppc_sanity_check(struct kvm_vcpu * vcpu)253 int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
254 {
255 int r = false;
256
257 /* We have to know what CPU to virtualize */
258 if (!vcpu->arch.pvr)
259 goto out;
260
261 /* PAPR only works with book3s_64 */
262 if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
263 goto out;
264
265 /* HV KVM can only do PAPR mode for now */
266 if (!vcpu->arch.papr_enabled && is_kvmppc_hv_enabled(vcpu->kvm))
267 goto out;
268
269 #ifdef CONFIG_KVM_BOOKE_HV
270 if (!cpu_has_feature(CPU_FTR_EMB_HV))
271 goto out;
272 #endif
273
274 r = true;
275
276 out:
277 vcpu->arch.sane = r;
278 return r ? 0 : -EINVAL;
279 }
280 EXPORT_SYMBOL_GPL(kvmppc_sanity_check);
281
kvmppc_emulate_mmio(struct kvm_vcpu * vcpu)282 int kvmppc_emulate_mmio(struct kvm_vcpu *vcpu)
283 {
284 enum emulation_result er;
285 int r;
286
287 er = kvmppc_emulate_loadstore(vcpu);
288 switch (er) {
289 case EMULATE_DONE:
290 /* Future optimization: only reload non-volatiles if they were
291 * actually modified. */
292 r = RESUME_GUEST_NV;
293 break;
294 case EMULATE_AGAIN:
295 r = RESUME_GUEST;
296 break;
297 case EMULATE_DO_MMIO:
298 vcpu->run->exit_reason = KVM_EXIT_MMIO;
299 /* We must reload nonvolatiles because "update" load/store
300 * instructions modify register state. */
301 /* Future optimization: only reload non-volatiles if they were
302 * actually modified. */
303 r = RESUME_HOST_NV;
304 break;
305 case EMULATE_FAIL:
306 {
307 u32 last_inst;
308
309 kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
310 kvm_debug_ratelimited("Guest access to device memory using unsupported instruction (opcode: %#08x)\n",
311 last_inst);
312
313 /*
314 * Injecting a Data Storage here is a bit more
315 * accurate since the instruction that caused the
316 * access could still be a valid one.
317 */
318 if (!IS_ENABLED(CONFIG_BOOKE)) {
319 ulong dsisr = DSISR_BADACCESS;
320
321 if (vcpu->mmio_is_write)
322 dsisr |= DSISR_ISSTORE;
323
324 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.vaddr_accessed, dsisr);
325 } else {
326 /*
327 * BookE does not send a SIGBUS on a bad
328 * fault, so use a Program interrupt instead
329 * to avoid a fault loop.
330 */
331 kvmppc_core_queue_program(vcpu, 0);
332 }
333
334 r = RESUME_GUEST;
335 break;
336 }
337 default:
338 WARN_ON(1);
339 r = RESUME_GUEST;
340 }
341
342 return r;
343 }
344 EXPORT_SYMBOL_GPL(kvmppc_emulate_mmio);
345
kvmppc_st(struct kvm_vcpu * vcpu,ulong * eaddr,int size,void * ptr,bool data)346 int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
347 bool data)
348 {
349 ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
350 struct kvmppc_pte pte;
351 int r = -EINVAL;
352
353 vcpu->stat.st++;
354
355 if (vcpu->kvm->arch.kvm_ops && vcpu->kvm->arch.kvm_ops->store_to_eaddr)
356 r = vcpu->kvm->arch.kvm_ops->store_to_eaddr(vcpu, eaddr, ptr,
357 size);
358
359 if ((!r) || (r == -EAGAIN))
360 return r;
361
362 r = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
363 XLATE_WRITE, &pte);
364 if (r < 0)
365 return r;
366
367 *eaddr = pte.raddr;
368
369 if (!pte.may_write)
370 return -EPERM;
371
372 /* Magic page override */
373 if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
374 ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
375 !(kvmppc_get_msr(vcpu) & MSR_PR)) {
376 void *magic = vcpu->arch.shared;
377 magic += pte.eaddr & 0xfff;
378 memcpy(magic, ptr, size);
379 return EMULATE_DONE;
380 }
381
382 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
383 return EMULATE_DO_MMIO;
384
385 return EMULATE_DONE;
386 }
387 EXPORT_SYMBOL_GPL(kvmppc_st);
388
kvmppc_ld(struct kvm_vcpu * vcpu,ulong * eaddr,int size,void * ptr,bool data)389 int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
390 bool data)
391 {
392 ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
393 struct kvmppc_pte pte;
394 int rc = -EINVAL;
395
396 vcpu->stat.ld++;
397
398 if (vcpu->kvm->arch.kvm_ops && vcpu->kvm->arch.kvm_ops->load_from_eaddr)
399 rc = vcpu->kvm->arch.kvm_ops->load_from_eaddr(vcpu, eaddr, ptr,
400 size);
401
402 if ((!rc) || (rc == -EAGAIN))
403 return rc;
404
405 rc = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
406 XLATE_READ, &pte);
407 if (rc)
408 return rc;
409
410 *eaddr = pte.raddr;
411
412 if (!pte.may_read)
413 return -EPERM;
414
415 if (!data && !pte.may_execute)
416 return -ENOEXEC;
417
418 /* Magic page override */
419 if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
420 ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
421 !(kvmppc_get_msr(vcpu) & MSR_PR)) {
422 void *magic = vcpu->arch.shared;
423 magic += pte.eaddr & 0xfff;
424 memcpy(ptr, magic, size);
425 return EMULATE_DONE;
426 }
427
428 kvm_vcpu_srcu_read_lock(vcpu);
429 rc = kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size);
430 kvm_vcpu_srcu_read_unlock(vcpu);
431 if (rc)
432 return EMULATE_DO_MMIO;
433
434 return EMULATE_DONE;
435 }
436 EXPORT_SYMBOL_GPL(kvmppc_ld);
437
kvm_arch_init_vm(struct kvm * kvm,unsigned long type)438 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
439 {
440 struct kvmppc_ops *kvm_ops = NULL;
441 int r;
442
443 /*
444 * if we have both HV and PR enabled, default is HV
445 */
446 if (type == 0) {
447 if (kvmppc_hv_ops)
448 kvm_ops = kvmppc_hv_ops;
449 else
450 kvm_ops = kvmppc_pr_ops;
451 if (!kvm_ops)
452 goto err_out;
453 } else if (type == KVM_VM_PPC_HV) {
454 if (!kvmppc_hv_ops)
455 goto err_out;
456 kvm_ops = kvmppc_hv_ops;
457 } else if (type == KVM_VM_PPC_PR) {
458 if (!kvmppc_pr_ops)
459 goto err_out;
460 kvm_ops = kvmppc_pr_ops;
461 } else
462 goto err_out;
463
464 if (!try_module_get(kvm_ops->owner))
465 return -ENOENT;
466
467 kvm->arch.kvm_ops = kvm_ops;
468 r = kvmppc_core_init_vm(kvm);
469 if (r)
470 module_put(kvm_ops->owner);
471 return r;
472 err_out:
473 return -EINVAL;
474 }
475
kvm_arch_destroy_vm(struct kvm * kvm)476 void kvm_arch_destroy_vm(struct kvm *kvm)
477 {
478 #ifdef CONFIG_KVM_XICS
479 /*
480 * We call kick_all_cpus_sync() to ensure that all
481 * CPUs have executed any pending IPIs before we
482 * continue and free VCPUs structures below.
483 */
484 if (is_kvmppc_hv_enabled(kvm))
485 kick_all_cpus_sync();
486 #endif
487
488 kvm_destroy_vcpus(kvm);
489
490 mutex_lock(&kvm->lock);
491
492 kvmppc_core_destroy_vm(kvm);
493
494 mutex_unlock(&kvm->lock);
495
496 /* drop the module reference */
497 module_put(kvm->arch.kvm_ops->owner);
498 }
499
kvm_vm_ioctl_check_extension(struct kvm * kvm,long ext)500 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
501 {
502 int r;
503 /* Assume we're using HV mode when the HV module is loaded */
504 int hv_enabled = kvmppc_hv_ops ? 1 : 0;
505
506 if (kvm) {
507 /*
508 * Hooray - we know which VM type we're running on. Depend on
509 * that rather than the guess above.
510 */
511 hv_enabled = is_kvmppc_hv_enabled(kvm);
512 }
513
514 switch (ext) {
515 #ifdef CONFIG_BOOKE
516 case KVM_CAP_PPC_BOOKE_SREGS:
517 case KVM_CAP_PPC_BOOKE_WATCHDOG:
518 case KVM_CAP_PPC_EPR:
519 #else
520 case KVM_CAP_PPC_SEGSTATE:
521 case KVM_CAP_PPC_HIOR:
522 case KVM_CAP_PPC_PAPR:
523 #endif
524 case KVM_CAP_PPC_UNSET_IRQ:
525 case KVM_CAP_PPC_IRQ_LEVEL:
526 case KVM_CAP_ENABLE_CAP:
527 case KVM_CAP_ONE_REG:
528 case KVM_CAP_IOEVENTFD:
529 case KVM_CAP_DEVICE_CTRL:
530 case KVM_CAP_IMMEDIATE_EXIT:
531 case KVM_CAP_SET_GUEST_DEBUG:
532 r = 1;
533 break;
534 case KVM_CAP_PPC_GUEST_DEBUG_SSTEP:
535 case KVM_CAP_PPC_PAIRED_SINGLES:
536 case KVM_CAP_PPC_OSI:
537 case KVM_CAP_PPC_GET_PVINFO:
538 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
539 case KVM_CAP_SW_TLB:
540 #endif
541 /* We support this only for PR */
542 r = !hv_enabled;
543 break;
544 #ifdef CONFIG_KVM_MPIC
545 case KVM_CAP_IRQ_MPIC:
546 r = 1;
547 break;
548 #endif
549
550 #ifdef CONFIG_PPC_BOOK3S_64
551 case KVM_CAP_SPAPR_TCE:
552 case KVM_CAP_SPAPR_TCE_64:
553 r = 1;
554 break;
555 case KVM_CAP_SPAPR_TCE_VFIO:
556 r = !!cpu_has_feature(CPU_FTR_HVMODE);
557 break;
558 case KVM_CAP_PPC_RTAS:
559 case KVM_CAP_PPC_FIXUP_HCALL:
560 case KVM_CAP_PPC_ENABLE_HCALL:
561 #ifdef CONFIG_KVM_XICS
562 case KVM_CAP_IRQ_XICS:
563 #endif
564 case KVM_CAP_PPC_GET_CPU_CHAR:
565 r = 1;
566 break;
567 #ifdef CONFIG_KVM_XIVE
568 case KVM_CAP_PPC_IRQ_XIVE:
569 /*
570 * We need XIVE to be enabled on the platform (implies
571 * a POWER9 processor) and the PowerNV platform, as
572 * nested is not yet supported.
573 */
574 r = xive_enabled() && !!cpu_has_feature(CPU_FTR_HVMODE) &&
575 kvmppc_xive_native_supported();
576 break;
577 #endif
578
579 case KVM_CAP_PPC_ALLOC_HTAB:
580 r = hv_enabled;
581 break;
582 #endif /* CONFIG_PPC_BOOK3S_64 */
583 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
584 case KVM_CAP_PPC_SMT:
585 r = 0;
586 if (kvm) {
587 if (kvm->arch.emul_smt_mode > 1)
588 r = kvm->arch.emul_smt_mode;
589 else
590 r = kvm->arch.smt_mode;
591 } else if (hv_enabled) {
592 if (cpu_has_feature(CPU_FTR_ARCH_300))
593 r = 1;
594 else
595 r = threads_per_subcore;
596 }
597 break;
598 case KVM_CAP_PPC_SMT_POSSIBLE:
599 r = 1;
600 if (hv_enabled) {
601 if (!cpu_has_feature(CPU_FTR_ARCH_300))
602 r = ((threads_per_subcore << 1) - 1);
603 else
604 /* P9 can emulate dbells, so allow any mode */
605 r = 8 | 4 | 2 | 1;
606 }
607 break;
608 case KVM_CAP_PPC_RMA:
609 r = 0;
610 break;
611 case KVM_CAP_PPC_HWRNG:
612 r = kvmppc_hwrng_present();
613 break;
614 case KVM_CAP_PPC_MMU_RADIX:
615 r = !!(hv_enabled && radix_enabled());
616 break;
617 case KVM_CAP_PPC_MMU_HASH_V3:
618 r = !!(hv_enabled && kvmppc_hv_ops->hash_v3_possible &&
619 kvmppc_hv_ops->hash_v3_possible());
620 break;
621 case KVM_CAP_PPC_NESTED_HV:
622 r = !!(hv_enabled && kvmppc_hv_ops->enable_nested &&
623 !kvmppc_hv_ops->enable_nested(NULL));
624 break;
625 #endif
626 case KVM_CAP_SYNC_MMU:
627 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
628 r = hv_enabled;
629 #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
630 r = 1;
631 #else
632 r = 0;
633 #endif
634 break;
635 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
636 case KVM_CAP_PPC_HTAB_FD:
637 r = hv_enabled;
638 break;
639 #endif
640 case KVM_CAP_NR_VCPUS:
641 /*
642 * Recommending a number of CPUs is somewhat arbitrary; we
643 * return the number of present CPUs for -HV (since a host
644 * will have secondary threads "offline"), and for other KVM
645 * implementations just count online CPUs.
646 */
647 if (hv_enabled)
648 r = min_t(unsigned int, num_present_cpus(), KVM_MAX_VCPUS);
649 else
650 r = min_t(unsigned int, num_online_cpus(), KVM_MAX_VCPUS);
651 break;
652 case KVM_CAP_MAX_VCPUS:
653 r = KVM_MAX_VCPUS;
654 break;
655 case KVM_CAP_MAX_VCPU_ID:
656 r = KVM_MAX_VCPU_IDS;
657 break;
658 #ifdef CONFIG_PPC_BOOK3S_64
659 case KVM_CAP_PPC_GET_SMMU_INFO:
660 r = 1;
661 break;
662 case KVM_CAP_SPAPR_MULTITCE:
663 r = 1;
664 break;
665 case KVM_CAP_SPAPR_RESIZE_HPT:
666 r = !!hv_enabled;
667 break;
668 #endif
669 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
670 case KVM_CAP_PPC_FWNMI:
671 r = hv_enabled;
672 break;
673 #endif
674 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
675 case KVM_CAP_PPC_HTM:
676 r = !!(cur_cpu_spec->cpu_user_features2 & PPC_FEATURE2_HTM) ||
677 (hv_enabled && cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST));
678 break;
679 #endif
680 #if defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE)
681 case KVM_CAP_PPC_SECURE_GUEST:
682 r = hv_enabled && kvmppc_hv_ops->enable_svm &&
683 !kvmppc_hv_ops->enable_svm(NULL);
684 break;
685 case KVM_CAP_PPC_DAWR1:
686 r = !!(hv_enabled && kvmppc_hv_ops->enable_dawr1 &&
687 !kvmppc_hv_ops->enable_dawr1(NULL));
688 break;
689 case KVM_CAP_PPC_RPT_INVALIDATE:
690 r = 1;
691 break;
692 #endif
693 case KVM_CAP_PPC_AIL_MODE_3:
694 r = 0;
695 /*
696 * KVM PR, POWER7, and some POWER9s don't support AIL=3 mode.
697 * The POWER9s can support it if the guest runs in hash mode,
698 * but QEMU doesn't necessarily query the capability in time.
699 */
700 if (hv_enabled) {
701 if (kvmhv_on_pseries()) {
702 if (pseries_reloc_on_exception())
703 r = 1;
704 } else if (cpu_has_feature(CPU_FTR_ARCH_207S) &&
705 !cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG)) {
706 r = 1;
707 }
708 }
709 break;
710 default:
711 r = 0;
712 break;
713 }
714 return r;
715
716 }
717
kvm_arch_dev_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)718 long kvm_arch_dev_ioctl(struct file *filp,
719 unsigned int ioctl, unsigned long arg)
720 {
721 return -EINVAL;
722 }
723
kvm_arch_free_memslot(struct kvm * kvm,struct kvm_memory_slot * slot)724 void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
725 {
726 kvmppc_core_free_memslot(kvm, slot);
727 }
728
kvm_arch_prepare_memory_region(struct kvm * kvm,const struct kvm_memory_slot * old,struct kvm_memory_slot * new,enum kvm_mr_change change)729 int kvm_arch_prepare_memory_region(struct kvm *kvm,
730 const struct kvm_memory_slot *old,
731 struct kvm_memory_slot *new,
732 enum kvm_mr_change change)
733 {
734 return kvmppc_core_prepare_memory_region(kvm, old, new, change);
735 }
736
kvm_arch_commit_memory_region(struct kvm * kvm,struct kvm_memory_slot * old,const struct kvm_memory_slot * new,enum kvm_mr_change change)737 void kvm_arch_commit_memory_region(struct kvm *kvm,
738 struct kvm_memory_slot *old,
739 const struct kvm_memory_slot *new,
740 enum kvm_mr_change change)
741 {
742 kvmppc_core_commit_memory_region(kvm, old, new, change);
743 }
744
kvm_arch_flush_shadow_memslot(struct kvm * kvm,struct kvm_memory_slot * slot)745 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
746 struct kvm_memory_slot *slot)
747 {
748 kvmppc_core_flush_memslot(kvm, slot);
749 }
750
kvm_arch_vcpu_precreate(struct kvm * kvm,unsigned int id)751 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
752 {
753 return 0;
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_create(struct kvm_vcpu * vcpu)766 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
767 {
768 int err;
769
770 hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
771 vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
772
773 #ifdef CONFIG_KVM_EXIT_TIMING
774 mutex_init(&vcpu->arch.exit_timing_lock);
775 #endif
776 err = kvmppc_subarch_vcpu_init(vcpu);
777 if (err)
778 return err;
779
780 err = kvmppc_core_vcpu_create(vcpu);
781 if (err)
782 goto out_vcpu_uninit;
783
784 rcuwait_init(&vcpu->arch.wait);
785 vcpu->arch.waitp = &vcpu->arch.wait;
786 return 0;
787
788 out_vcpu_uninit:
789 kvmppc_subarch_vcpu_uninit(vcpu);
790 return err;
791 }
792
kvm_arch_vcpu_postcreate(struct kvm_vcpu * vcpu)793 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
794 {
795 }
796
kvm_arch_vcpu_destroy(struct kvm_vcpu * vcpu)797 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
798 {
799 /* Make sure we're not using the vcpu anymore */
800 hrtimer_cancel(&vcpu->arch.dec_timer);
801
802 switch (vcpu->arch.irq_type) {
803 case KVMPPC_IRQ_MPIC:
804 kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
805 break;
806 case KVMPPC_IRQ_XICS:
807 if (xics_on_xive())
808 kvmppc_xive_cleanup_vcpu(vcpu);
809 else
810 kvmppc_xics_free_icp(vcpu);
811 break;
812 case KVMPPC_IRQ_XIVE:
813 kvmppc_xive_native_cleanup_vcpu(vcpu);
814 break;
815 }
816
817 kvmppc_core_vcpu_free(vcpu);
818
819 kvmppc_subarch_vcpu_uninit(vcpu);
820 }
821
kvm_cpu_has_pending_timer(struct kvm_vcpu * vcpu)822 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
823 {
824 return kvmppc_core_pending_dec(vcpu);
825 }
826
kvm_arch_vcpu_load(struct kvm_vcpu * vcpu,int cpu)827 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
828 {
829 #ifdef CONFIG_BOOKE
830 /*
831 * vrsave (formerly usprg0) isn't used by Linux, but may
832 * be used by the guest.
833 *
834 * On non-booke this is associated with Altivec and
835 * is handled by code in book3s.c.
836 */
837 mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
838 #endif
839 kvmppc_core_vcpu_load(vcpu, cpu);
840 }
841
kvm_arch_vcpu_put(struct kvm_vcpu * vcpu)842 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
843 {
844 kvmppc_core_vcpu_put(vcpu);
845 #ifdef CONFIG_BOOKE
846 vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
847 #endif
848 }
849
850 /*
851 * irq_bypass_add_producer and irq_bypass_del_producer are only
852 * useful if the architecture supports PCI passthrough.
853 * irq_bypass_stop and irq_bypass_start are not needed and so
854 * kvm_ops are not defined for them.
855 */
kvm_arch_has_irq_bypass(void)856 bool kvm_arch_has_irq_bypass(void)
857 {
858 return ((kvmppc_hv_ops && kvmppc_hv_ops->irq_bypass_add_producer) ||
859 (kvmppc_pr_ops && kvmppc_pr_ops->irq_bypass_add_producer));
860 }
861
kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer * cons,struct irq_bypass_producer * prod)862 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
863 struct irq_bypass_producer *prod)
864 {
865 struct kvm_kernel_irqfd *irqfd =
866 container_of(cons, struct kvm_kernel_irqfd, consumer);
867 struct kvm *kvm = irqfd->kvm;
868
869 if (kvm->arch.kvm_ops->irq_bypass_add_producer)
870 return kvm->arch.kvm_ops->irq_bypass_add_producer(cons, prod);
871
872 return 0;
873 }
874
kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer * cons,struct irq_bypass_producer * prod)875 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
876 struct irq_bypass_producer *prod)
877 {
878 struct kvm_kernel_irqfd *irqfd =
879 container_of(cons, struct kvm_kernel_irqfd, consumer);
880 struct kvm *kvm = irqfd->kvm;
881
882 if (kvm->arch.kvm_ops->irq_bypass_del_producer)
883 kvm->arch.kvm_ops->irq_bypass_del_producer(cons, prod);
884 }
885
886 #ifdef CONFIG_VSX
kvmppc_get_vsr_dword_offset(int index)887 static inline int kvmppc_get_vsr_dword_offset(int index)
888 {
889 int offset;
890
891 if ((index != 0) && (index != 1))
892 return -1;
893
894 #ifdef __BIG_ENDIAN
895 offset = index;
896 #else
897 offset = 1 - index;
898 #endif
899
900 return offset;
901 }
902
kvmppc_get_vsr_word_offset(int index)903 static inline int kvmppc_get_vsr_word_offset(int index)
904 {
905 int offset;
906
907 if ((index > 3) || (index < 0))
908 return -1;
909
910 #ifdef __BIG_ENDIAN
911 offset = index;
912 #else
913 offset = 3 - index;
914 #endif
915 return offset;
916 }
917
kvmppc_set_vsr_dword(struct kvm_vcpu * vcpu,u64 gpr)918 static inline void kvmppc_set_vsr_dword(struct kvm_vcpu *vcpu,
919 u64 gpr)
920 {
921 union kvmppc_one_reg val;
922 int offset = kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
923 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
924
925 if (offset == -1)
926 return;
927
928 if (index >= 32) {
929 val.vval = VCPU_VSX_VR(vcpu, index - 32);
930 val.vsxval[offset] = gpr;
931 VCPU_VSX_VR(vcpu, index - 32) = val.vval;
932 } else {
933 VCPU_VSX_FPR(vcpu, index, offset) = gpr;
934 }
935 }
936
kvmppc_set_vsr_dword_dump(struct kvm_vcpu * vcpu,u64 gpr)937 static inline void kvmppc_set_vsr_dword_dump(struct kvm_vcpu *vcpu,
938 u64 gpr)
939 {
940 union kvmppc_one_reg val;
941 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
942
943 if (index >= 32) {
944 val.vval = VCPU_VSX_VR(vcpu, index - 32);
945 val.vsxval[0] = gpr;
946 val.vsxval[1] = gpr;
947 VCPU_VSX_VR(vcpu, index - 32) = val.vval;
948 } else {
949 VCPU_VSX_FPR(vcpu, index, 0) = gpr;
950 VCPU_VSX_FPR(vcpu, index, 1) = gpr;
951 }
952 }
953
kvmppc_set_vsr_word_dump(struct kvm_vcpu * vcpu,u32 gpr)954 static inline void kvmppc_set_vsr_word_dump(struct kvm_vcpu *vcpu,
955 u32 gpr)
956 {
957 union kvmppc_one_reg val;
958 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
959
960 if (index >= 32) {
961 val.vsx32val[0] = gpr;
962 val.vsx32val[1] = gpr;
963 val.vsx32val[2] = gpr;
964 val.vsx32val[3] = gpr;
965 VCPU_VSX_VR(vcpu, index - 32) = val.vval;
966 } else {
967 val.vsx32val[0] = gpr;
968 val.vsx32val[1] = gpr;
969 VCPU_VSX_FPR(vcpu, index, 0) = val.vsxval[0];
970 VCPU_VSX_FPR(vcpu, index, 1) = val.vsxval[0];
971 }
972 }
973
kvmppc_set_vsr_word(struct kvm_vcpu * vcpu,u32 gpr32)974 static inline void kvmppc_set_vsr_word(struct kvm_vcpu *vcpu,
975 u32 gpr32)
976 {
977 union kvmppc_one_reg val;
978 int offset = kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
979 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
980 int dword_offset, word_offset;
981
982 if (offset == -1)
983 return;
984
985 if (index >= 32) {
986 val.vval = VCPU_VSX_VR(vcpu, index - 32);
987 val.vsx32val[offset] = gpr32;
988 VCPU_VSX_VR(vcpu, index - 32) = val.vval;
989 } else {
990 dword_offset = offset / 2;
991 word_offset = offset % 2;
992 val.vsxval[0] = VCPU_VSX_FPR(vcpu, index, dword_offset);
993 val.vsx32val[word_offset] = gpr32;
994 VCPU_VSX_FPR(vcpu, index, dword_offset) = val.vsxval[0];
995 }
996 }
997 #endif /* CONFIG_VSX */
998
999 #ifdef CONFIG_ALTIVEC
kvmppc_get_vmx_offset_generic(struct kvm_vcpu * vcpu,int index,int element_size)1000 static inline int kvmppc_get_vmx_offset_generic(struct kvm_vcpu *vcpu,
1001 int index, int element_size)
1002 {
1003 int offset;
1004 int elts = sizeof(vector128)/element_size;
1005
1006 if ((index < 0) || (index >= elts))
1007 return -1;
1008
1009 if (kvmppc_need_byteswap(vcpu))
1010 offset = elts - index - 1;
1011 else
1012 offset = index;
1013
1014 return offset;
1015 }
1016
kvmppc_get_vmx_dword_offset(struct kvm_vcpu * vcpu,int index)1017 static inline int kvmppc_get_vmx_dword_offset(struct kvm_vcpu *vcpu,
1018 int index)
1019 {
1020 return kvmppc_get_vmx_offset_generic(vcpu, index, 8);
1021 }
1022
kvmppc_get_vmx_word_offset(struct kvm_vcpu * vcpu,int index)1023 static inline int kvmppc_get_vmx_word_offset(struct kvm_vcpu *vcpu,
1024 int index)
1025 {
1026 return kvmppc_get_vmx_offset_generic(vcpu, index, 4);
1027 }
1028
kvmppc_get_vmx_hword_offset(struct kvm_vcpu * vcpu,int index)1029 static inline int kvmppc_get_vmx_hword_offset(struct kvm_vcpu *vcpu,
1030 int index)
1031 {
1032 return kvmppc_get_vmx_offset_generic(vcpu, index, 2);
1033 }
1034
kvmppc_get_vmx_byte_offset(struct kvm_vcpu * vcpu,int index)1035 static inline int kvmppc_get_vmx_byte_offset(struct kvm_vcpu *vcpu,
1036 int index)
1037 {
1038 return kvmppc_get_vmx_offset_generic(vcpu, index, 1);
1039 }
1040
1041
kvmppc_set_vmx_dword(struct kvm_vcpu * vcpu,u64 gpr)1042 static inline void kvmppc_set_vmx_dword(struct kvm_vcpu *vcpu,
1043 u64 gpr)
1044 {
1045 union kvmppc_one_reg val;
1046 int offset = kvmppc_get_vmx_dword_offset(vcpu,
1047 vcpu->arch.mmio_vmx_offset);
1048 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1049
1050 if (offset == -1)
1051 return;
1052
1053 val.vval = VCPU_VSX_VR(vcpu, index);
1054 val.vsxval[offset] = gpr;
1055 VCPU_VSX_VR(vcpu, index) = val.vval;
1056 }
1057
kvmppc_set_vmx_word(struct kvm_vcpu * vcpu,u32 gpr32)1058 static inline void kvmppc_set_vmx_word(struct kvm_vcpu *vcpu,
1059 u32 gpr32)
1060 {
1061 union kvmppc_one_reg val;
1062 int offset = kvmppc_get_vmx_word_offset(vcpu,
1063 vcpu->arch.mmio_vmx_offset);
1064 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1065
1066 if (offset == -1)
1067 return;
1068
1069 val.vval = VCPU_VSX_VR(vcpu, index);
1070 val.vsx32val[offset] = gpr32;
1071 VCPU_VSX_VR(vcpu, index) = val.vval;
1072 }
1073
kvmppc_set_vmx_hword(struct kvm_vcpu * vcpu,u16 gpr16)1074 static inline void kvmppc_set_vmx_hword(struct kvm_vcpu *vcpu,
1075 u16 gpr16)
1076 {
1077 union kvmppc_one_reg val;
1078 int offset = kvmppc_get_vmx_hword_offset(vcpu,
1079 vcpu->arch.mmio_vmx_offset);
1080 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1081
1082 if (offset == -1)
1083 return;
1084
1085 val.vval = VCPU_VSX_VR(vcpu, index);
1086 val.vsx16val[offset] = gpr16;
1087 VCPU_VSX_VR(vcpu, index) = val.vval;
1088 }
1089
kvmppc_set_vmx_byte(struct kvm_vcpu * vcpu,u8 gpr8)1090 static inline void kvmppc_set_vmx_byte(struct kvm_vcpu *vcpu,
1091 u8 gpr8)
1092 {
1093 union kvmppc_one_reg val;
1094 int offset = kvmppc_get_vmx_byte_offset(vcpu,
1095 vcpu->arch.mmio_vmx_offset);
1096 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1097
1098 if (offset == -1)
1099 return;
1100
1101 val.vval = VCPU_VSX_VR(vcpu, index);
1102 val.vsx8val[offset] = gpr8;
1103 VCPU_VSX_VR(vcpu, index) = val.vval;
1104 }
1105 #endif /* CONFIG_ALTIVEC */
1106
1107 #ifdef CONFIG_PPC_FPU
sp_to_dp(u32 fprs)1108 static inline u64 sp_to_dp(u32 fprs)
1109 {
1110 u64 fprd;
1111
1112 preempt_disable();
1113 enable_kernel_fp();
1114 asm ("lfs%U1%X1 0,%1; stfd%U0%X0 0,%0" : "=m<>" (fprd) : "m<>" (fprs)
1115 : "fr0");
1116 preempt_enable();
1117 return fprd;
1118 }
1119
dp_to_sp(u64 fprd)1120 static inline u32 dp_to_sp(u64 fprd)
1121 {
1122 u32 fprs;
1123
1124 preempt_disable();
1125 enable_kernel_fp();
1126 asm ("lfd%U1%X1 0,%1; stfs%U0%X0 0,%0" : "=m<>" (fprs) : "m<>" (fprd)
1127 : "fr0");
1128 preempt_enable();
1129 return fprs;
1130 }
1131
1132 #else
1133 #define sp_to_dp(x) (x)
1134 #define dp_to_sp(x) (x)
1135 #endif /* CONFIG_PPC_FPU */
1136
kvmppc_complete_mmio_load(struct kvm_vcpu * vcpu)1137 static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu)
1138 {
1139 struct kvm_run *run = vcpu->run;
1140 u64 gpr;
1141
1142 if (run->mmio.len > sizeof(gpr))
1143 return;
1144
1145 if (!vcpu->arch.mmio_host_swabbed) {
1146 switch (run->mmio.len) {
1147 case 8: gpr = *(u64 *)run->mmio.data; break;
1148 case 4: gpr = *(u32 *)run->mmio.data; break;
1149 case 2: gpr = *(u16 *)run->mmio.data; break;
1150 case 1: gpr = *(u8 *)run->mmio.data; break;
1151 }
1152 } else {
1153 switch (run->mmio.len) {
1154 case 8: gpr = swab64(*(u64 *)run->mmio.data); break;
1155 case 4: gpr = swab32(*(u32 *)run->mmio.data); break;
1156 case 2: gpr = swab16(*(u16 *)run->mmio.data); break;
1157 case 1: gpr = *(u8 *)run->mmio.data; break;
1158 }
1159 }
1160
1161 /* conversion between single and double precision */
1162 if ((vcpu->arch.mmio_sp64_extend) && (run->mmio.len == 4))
1163 gpr = sp_to_dp(gpr);
1164
1165 if (vcpu->arch.mmio_sign_extend) {
1166 switch (run->mmio.len) {
1167 #ifdef CONFIG_PPC64
1168 case 4:
1169 gpr = (s64)(s32)gpr;
1170 break;
1171 #endif
1172 case 2:
1173 gpr = (s64)(s16)gpr;
1174 break;
1175 case 1:
1176 gpr = (s64)(s8)gpr;
1177 break;
1178 }
1179 }
1180
1181 switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
1182 case KVM_MMIO_REG_GPR:
1183 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
1184 break;
1185 case KVM_MMIO_REG_FPR:
1186 if (vcpu->kvm->arch.kvm_ops->giveup_ext)
1187 vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_FP);
1188
1189 VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
1190 break;
1191 #ifdef CONFIG_PPC_BOOK3S
1192 case KVM_MMIO_REG_QPR:
1193 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
1194 break;
1195 case KVM_MMIO_REG_FQPR:
1196 VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
1197 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
1198 break;
1199 #endif
1200 #ifdef CONFIG_VSX
1201 case KVM_MMIO_REG_VSX:
1202 if (vcpu->kvm->arch.kvm_ops->giveup_ext)
1203 vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_VSX);
1204
1205 if (vcpu->arch.mmio_copy_type == KVMPPC_VSX_COPY_DWORD)
1206 kvmppc_set_vsr_dword(vcpu, gpr);
1207 else if (vcpu->arch.mmio_copy_type == KVMPPC_VSX_COPY_WORD)
1208 kvmppc_set_vsr_word(vcpu, gpr);
1209 else if (vcpu->arch.mmio_copy_type ==
1210 KVMPPC_VSX_COPY_DWORD_LOAD_DUMP)
1211 kvmppc_set_vsr_dword_dump(vcpu, gpr);
1212 else if (vcpu->arch.mmio_copy_type ==
1213 KVMPPC_VSX_COPY_WORD_LOAD_DUMP)
1214 kvmppc_set_vsr_word_dump(vcpu, gpr);
1215 break;
1216 #endif
1217 #ifdef CONFIG_ALTIVEC
1218 case KVM_MMIO_REG_VMX:
1219 if (vcpu->kvm->arch.kvm_ops->giveup_ext)
1220 vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_VEC);
1221
1222 if (vcpu->arch.mmio_copy_type == KVMPPC_VMX_COPY_DWORD)
1223 kvmppc_set_vmx_dword(vcpu, gpr);
1224 else if (vcpu->arch.mmio_copy_type == KVMPPC_VMX_COPY_WORD)
1225 kvmppc_set_vmx_word(vcpu, gpr);
1226 else if (vcpu->arch.mmio_copy_type ==
1227 KVMPPC_VMX_COPY_HWORD)
1228 kvmppc_set_vmx_hword(vcpu, gpr);
1229 else if (vcpu->arch.mmio_copy_type ==
1230 KVMPPC_VMX_COPY_BYTE)
1231 kvmppc_set_vmx_byte(vcpu, gpr);
1232 break;
1233 #endif
1234 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
1235 case KVM_MMIO_REG_NESTED_GPR:
1236 if (kvmppc_need_byteswap(vcpu))
1237 gpr = swab64(gpr);
1238 kvm_vcpu_write_guest(vcpu, vcpu->arch.nested_io_gpr, &gpr,
1239 sizeof(gpr));
1240 break;
1241 #endif
1242 default:
1243 BUG();
1244 }
1245 }
1246
__kvmppc_handle_load(struct kvm_vcpu * vcpu,unsigned int rt,unsigned int bytes,int is_default_endian,int sign_extend)1247 static int __kvmppc_handle_load(struct kvm_vcpu *vcpu,
1248 unsigned int rt, unsigned int bytes,
1249 int is_default_endian, int sign_extend)
1250 {
1251 struct kvm_run *run = vcpu->run;
1252 int idx, ret;
1253 bool host_swabbed;
1254
1255 /* Pity C doesn't have a logical XOR operator */
1256 if (kvmppc_need_byteswap(vcpu)) {
1257 host_swabbed = is_default_endian;
1258 } else {
1259 host_swabbed = !is_default_endian;
1260 }
1261
1262 if (bytes > sizeof(run->mmio.data))
1263 return EMULATE_FAIL;
1264
1265 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
1266 run->mmio.len = bytes;
1267 run->mmio.is_write = 0;
1268
1269 vcpu->arch.io_gpr = rt;
1270 vcpu->arch.mmio_host_swabbed = host_swabbed;
1271 vcpu->mmio_needed = 1;
1272 vcpu->mmio_is_write = 0;
1273 vcpu->arch.mmio_sign_extend = sign_extend;
1274
1275 idx = srcu_read_lock(&vcpu->kvm->srcu);
1276
1277 ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
1278 bytes, &run->mmio.data);
1279
1280 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1281
1282 if (!ret) {
1283 kvmppc_complete_mmio_load(vcpu);
1284 vcpu->mmio_needed = 0;
1285 return EMULATE_DONE;
1286 }
1287
1288 return EMULATE_DO_MMIO;
1289 }
1290
kvmppc_handle_load(struct kvm_vcpu * vcpu,unsigned int rt,unsigned int bytes,int is_default_endian)1291 int kvmppc_handle_load(struct kvm_vcpu *vcpu,
1292 unsigned int rt, unsigned int bytes,
1293 int is_default_endian)
1294 {
1295 return __kvmppc_handle_load(vcpu, rt, bytes, is_default_endian, 0);
1296 }
1297 EXPORT_SYMBOL_GPL(kvmppc_handle_load);
1298
1299 /* Same as above, but sign extends */
kvmppc_handle_loads(struct kvm_vcpu * vcpu,unsigned int rt,unsigned int bytes,int is_default_endian)1300 int kvmppc_handle_loads(struct kvm_vcpu *vcpu,
1301 unsigned int rt, unsigned int bytes,
1302 int is_default_endian)
1303 {
1304 return __kvmppc_handle_load(vcpu, rt, bytes, is_default_endian, 1);
1305 }
1306
1307 #ifdef CONFIG_VSX
kvmppc_handle_vsx_load(struct kvm_vcpu * vcpu,unsigned int rt,unsigned int bytes,int is_default_endian,int mmio_sign_extend)1308 int kvmppc_handle_vsx_load(struct kvm_vcpu *vcpu,
1309 unsigned int rt, unsigned int bytes,
1310 int is_default_endian, int mmio_sign_extend)
1311 {
1312 enum emulation_result emulated = EMULATE_DONE;
1313
1314 /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
1315 if (vcpu->arch.mmio_vsx_copy_nums > 4)
1316 return EMULATE_FAIL;
1317
1318 while (vcpu->arch.mmio_vsx_copy_nums) {
1319 emulated = __kvmppc_handle_load(vcpu, rt, bytes,
1320 is_default_endian, mmio_sign_extend);
1321
1322 if (emulated != EMULATE_DONE)
1323 break;
1324
1325 vcpu->arch.paddr_accessed += vcpu->run->mmio.len;
1326
1327 vcpu->arch.mmio_vsx_copy_nums--;
1328 vcpu->arch.mmio_vsx_offset++;
1329 }
1330 return emulated;
1331 }
1332 #endif /* CONFIG_VSX */
1333
kvmppc_handle_store(struct kvm_vcpu * vcpu,u64 val,unsigned int bytes,int is_default_endian)1334 int kvmppc_handle_store(struct kvm_vcpu *vcpu,
1335 u64 val, unsigned int bytes, int is_default_endian)
1336 {
1337 struct kvm_run *run = vcpu->run;
1338 void *data = run->mmio.data;
1339 int idx, ret;
1340 bool host_swabbed;
1341
1342 /* Pity C doesn't have a logical XOR operator */
1343 if (kvmppc_need_byteswap(vcpu)) {
1344 host_swabbed = is_default_endian;
1345 } else {
1346 host_swabbed = !is_default_endian;
1347 }
1348
1349 if (bytes > sizeof(run->mmio.data))
1350 return EMULATE_FAIL;
1351
1352 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
1353 run->mmio.len = bytes;
1354 run->mmio.is_write = 1;
1355 vcpu->mmio_needed = 1;
1356 vcpu->mmio_is_write = 1;
1357
1358 if ((vcpu->arch.mmio_sp64_extend) && (bytes == 4))
1359 val = dp_to_sp(val);
1360
1361 /* Store the value at the lowest bytes in 'data'. */
1362 if (!host_swabbed) {
1363 switch (bytes) {
1364 case 8: *(u64 *)data = val; break;
1365 case 4: *(u32 *)data = val; break;
1366 case 2: *(u16 *)data = val; break;
1367 case 1: *(u8 *)data = val; break;
1368 }
1369 } else {
1370 switch (bytes) {
1371 case 8: *(u64 *)data = swab64(val); break;
1372 case 4: *(u32 *)data = swab32(val); break;
1373 case 2: *(u16 *)data = swab16(val); break;
1374 case 1: *(u8 *)data = val; break;
1375 }
1376 }
1377
1378 idx = srcu_read_lock(&vcpu->kvm->srcu);
1379
1380 ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
1381 bytes, &run->mmio.data);
1382
1383 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1384
1385 if (!ret) {
1386 vcpu->mmio_needed = 0;
1387 return EMULATE_DONE;
1388 }
1389
1390 return EMULATE_DO_MMIO;
1391 }
1392 EXPORT_SYMBOL_GPL(kvmppc_handle_store);
1393
1394 #ifdef CONFIG_VSX
kvmppc_get_vsr_data(struct kvm_vcpu * vcpu,int rs,u64 * val)1395 static inline int kvmppc_get_vsr_data(struct kvm_vcpu *vcpu, int rs, u64 *val)
1396 {
1397 u32 dword_offset, word_offset;
1398 union kvmppc_one_reg reg;
1399 int vsx_offset = 0;
1400 int copy_type = vcpu->arch.mmio_copy_type;
1401 int result = 0;
1402
1403 switch (copy_type) {
1404 case KVMPPC_VSX_COPY_DWORD:
1405 vsx_offset =
1406 kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
1407
1408 if (vsx_offset == -1) {
1409 result = -1;
1410 break;
1411 }
1412
1413 if (rs < 32) {
1414 *val = VCPU_VSX_FPR(vcpu, rs, vsx_offset);
1415 } else {
1416 reg.vval = VCPU_VSX_VR(vcpu, rs - 32);
1417 *val = reg.vsxval[vsx_offset];
1418 }
1419 break;
1420
1421 case KVMPPC_VSX_COPY_WORD:
1422 vsx_offset =
1423 kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
1424
1425 if (vsx_offset == -1) {
1426 result = -1;
1427 break;
1428 }
1429
1430 if (rs < 32) {
1431 dword_offset = vsx_offset / 2;
1432 word_offset = vsx_offset % 2;
1433 reg.vsxval[0] = VCPU_VSX_FPR(vcpu, rs, dword_offset);
1434 *val = reg.vsx32val[word_offset];
1435 } else {
1436 reg.vval = VCPU_VSX_VR(vcpu, rs - 32);
1437 *val = reg.vsx32val[vsx_offset];
1438 }
1439 break;
1440
1441 default:
1442 result = -1;
1443 break;
1444 }
1445
1446 return result;
1447 }
1448
kvmppc_handle_vsx_store(struct kvm_vcpu * vcpu,int rs,unsigned int bytes,int is_default_endian)1449 int kvmppc_handle_vsx_store(struct kvm_vcpu *vcpu,
1450 int rs, unsigned int bytes, int is_default_endian)
1451 {
1452 u64 val;
1453 enum emulation_result emulated = EMULATE_DONE;
1454
1455 vcpu->arch.io_gpr = rs;
1456
1457 /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
1458 if (vcpu->arch.mmio_vsx_copy_nums > 4)
1459 return EMULATE_FAIL;
1460
1461 while (vcpu->arch.mmio_vsx_copy_nums) {
1462 if (kvmppc_get_vsr_data(vcpu, rs, &val) == -1)
1463 return EMULATE_FAIL;
1464
1465 emulated = kvmppc_handle_store(vcpu,
1466 val, bytes, is_default_endian);
1467
1468 if (emulated != EMULATE_DONE)
1469 break;
1470
1471 vcpu->arch.paddr_accessed += vcpu->run->mmio.len;
1472
1473 vcpu->arch.mmio_vsx_copy_nums--;
1474 vcpu->arch.mmio_vsx_offset++;
1475 }
1476
1477 return emulated;
1478 }
1479
kvmppc_emulate_mmio_vsx_loadstore(struct kvm_vcpu * vcpu)1480 static int kvmppc_emulate_mmio_vsx_loadstore(struct kvm_vcpu *vcpu)
1481 {
1482 struct kvm_run *run = vcpu->run;
1483 enum emulation_result emulated = EMULATE_FAIL;
1484 int r;
1485
1486 vcpu->arch.paddr_accessed += run->mmio.len;
1487
1488 if (!vcpu->mmio_is_write) {
1489 emulated = kvmppc_handle_vsx_load(vcpu, vcpu->arch.io_gpr,
1490 run->mmio.len, 1, vcpu->arch.mmio_sign_extend);
1491 } else {
1492 emulated = kvmppc_handle_vsx_store(vcpu,
1493 vcpu->arch.io_gpr, run->mmio.len, 1);
1494 }
1495
1496 switch (emulated) {
1497 case EMULATE_DO_MMIO:
1498 run->exit_reason = KVM_EXIT_MMIO;
1499 r = RESUME_HOST;
1500 break;
1501 case EMULATE_FAIL:
1502 pr_info("KVM: MMIO emulation failed (VSX repeat)\n");
1503 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1504 run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
1505 r = RESUME_HOST;
1506 break;
1507 default:
1508 r = RESUME_GUEST;
1509 break;
1510 }
1511 return r;
1512 }
1513 #endif /* CONFIG_VSX */
1514
1515 #ifdef CONFIG_ALTIVEC
kvmppc_handle_vmx_load(struct kvm_vcpu * vcpu,unsigned int rt,unsigned int bytes,int is_default_endian)1516 int kvmppc_handle_vmx_load(struct kvm_vcpu *vcpu,
1517 unsigned int rt, unsigned int bytes, int is_default_endian)
1518 {
1519 enum emulation_result emulated = EMULATE_DONE;
1520
1521 if (vcpu->arch.mmio_vmx_copy_nums > 2)
1522 return EMULATE_FAIL;
1523
1524 while (vcpu->arch.mmio_vmx_copy_nums) {
1525 emulated = __kvmppc_handle_load(vcpu, rt, bytes,
1526 is_default_endian, 0);
1527
1528 if (emulated != EMULATE_DONE)
1529 break;
1530
1531 vcpu->arch.paddr_accessed += vcpu->run->mmio.len;
1532 vcpu->arch.mmio_vmx_copy_nums--;
1533 vcpu->arch.mmio_vmx_offset++;
1534 }
1535
1536 return emulated;
1537 }
1538
kvmppc_get_vmx_dword(struct kvm_vcpu * vcpu,int index,u64 * val)1539 static int kvmppc_get_vmx_dword(struct kvm_vcpu *vcpu, int index, u64 *val)
1540 {
1541 union kvmppc_one_reg reg;
1542 int vmx_offset = 0;
1543 int result = 0;
1544
1545 vmx_offset =
1546 kvmppc_get_vmx_dword_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1547
1548 if (vmx_offset == -1)
1549 return -1;
1550
1551 reg.vval = VCPU_VSX_VR(vcpu, index);
1552 *val = reg.vsxval[vmx_offset];
1553
1554 return result;
1555 }
1556
kvmppc_get_vmx_word(struct kvm_vcpu * vcpu,int index,u64 * val)1557 static int kvmppc_get_vmx_word(struct kvm_vcpu *vcpu, int index, u64 *val)
1558 {
1559 union kvmppc_one_reg reg;
1560 int vmx_offset = 0;
1561 int result = 0;
1562
1563 vmx_offset =
1564 kvmppc_get_vmx_word_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1565
1566 if (vmx_offset == -1)
1567 return -1;
1568
1569 reg.vval = VCPU_VSX_VR(vcpu, index);
1570 *val = reg.vsx32val[vmx_offset];
1571
1572 return result;
1573 }
1574
kvmppc_get_vmx_hword(struct kvm_vcpu * vcpu,int index,u64 * val)1575 static int kvmppc_get_vmx_hword(struct kvm_vcpu *vcpu, int index, u64 *val)
1576 {
1577 union kvmppc_one_reg reg;
1578 int vmx_offset = 0;
1579 int result = 0;
1580
1581 vmx_offset =
1582 kvmppc_get_vmx_hword_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1583
1584 if (vmx_offset == -1)
1585 return -1;
1586
1587 reg.vval = VCPU_VSX_VR(vcpu, index);
1588 *val = reg.vsx16val[vmx_offset];
1589
1590 return result;
1591 }
1592
kvmppc_get_vmx_byte(struct kvm_vcpu * vcpu,int index,u64 * val)1593 static int kvmppc_get_vmx_byte(struct kvm_vcpu *vcpu, int index, u64 *val)
1594 {
1595 union kvmppc_one_reg reg;
1596 int vmx_offset = 0;
1597 int result = 0;
1598
1599 vmx_offset =
1600 kvmppc_get_vmx_byte_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1601
1602 if (vmx_offset == -1)
1603 return -1;
1604
1605 reg.vval = VCPU_VSX_VR(vcpu, index);
1606 *val = reg.vsx8val[vmx_offset];
1607
1608 return result;
1609 }
1610
kvmppc_handle_vmx_store(struct kvm_vcpu * vcpu,unsigned int rs,unsigned int bytes,int is_default_endian)1611 int kvmppc_handle_vmx_store(struct kvm_vcpu *vcpu,
1612 unsigned int rs, unsigned int bytes, int is_default_endian)
1613 {
1614 u64 val = 0;
1615 unsigned int index = rs & KVM_MMIO_REG_MASK;
1616 enum emulation_result emulated = EMULATE_DONE;
1617
1618 if (vcpu->arch.mmio_vmx_copy_nums > 2)
1619 return EMULATE_FAIL;
1620
1621 vcpu->arch.io_gpr = rs;
1622
1623 while (vcpu->arch.mmio_vmx_copy_nums) {
1624 switch (vcpu->arch.mmio_copy_type) {
1625 case KVMPPC_VMX_COPY_DWORD:
1626 if (kvmppc_get_vmx_dword(vcpu, index, &val) == -1)
1627 return EMULATE_FAIL;
1628
1629 break;
1630 case KVMPPC_VMX_COPY_WORD:
1631 if (kvmppc_get_vmx_word(vcpu, index, &val) == -1)
1632 return EMULATE_FAIL;
1633 break;
1634 case KVMPPC_VMX_COPY_HWORD:
1635 if (kvmppc_get_vmx_hword(vcpu, index, &val) == -1)
1636 return EMULATE_FAIL;
1637 break;
1638 case KVMPPC_VMX_COPY_BYTE:
1639 if (kvmppc_get_vmx_byte(vcpu, index, &val) == -1)
1640 return EMULATE_FAIL;
1641 break;
1642 default:
1643 return EMULATE_FAIL;
1644 }
1645
1646 emulated = kvmppc_handle_store(vcpu, val, bytes,
1647 is_default_endian);
1648 if (emulated != EMULATE_DONE)
1649 break;
1650
1651 vcpu->arch.paddr_accessed += vcpu->run->mmio.len;
1652 vcpu->arch.mmio_vmx_copy_nums--;
1653 vcpu->arch.mmio_vmx_offset++;
1654 }
1655
1656 return emulated;
1657 }
1658
kvmppc_emulate_mmio_vmx_loadstore(struct kvm_vcpu * vcpu)1659 static int kvmppc_emulate_mmio_vmx_loadstore(struct kvm_vcpu *vcpu)
1660 {
1661 struct kvm_run *run = vcpu->run;
1662 enum emulation_result emulated = EMULATE_FAIL;
1663 int r;
1664
1665 vcpu->arch.paddr_accessed += run->mmio.len;
1666
1667 if (!vcpu->mmio_is_write) {
1668 emulated = kvmppc_handle_vmx_load(vcpu,
1669 vcpu->arch.io_gpr, run->mmio.len, 1);
1670 } else {
1671 emulated = kvmppc_handle_vmx_store(vcpu,
1672 vcpu->arch.io_gpr, run->mmio.len, 1);
1673 }
1674
1675 switch (emulated) {
1676 case EMULATE_DO_MMIO:
1677 run->exit_reason = KVM_EXIT_MMIO;
1678 r = RESUME_HOST;
1679 break;
1680 case EMULATE_FAIL:
1681 pr_info("KVM: MMIO emulation failed (VMX repeat)\n");
1682 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1683 run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
1684 r = RESUME_HOST;
1685 break;
1686 default:
1687 r = RESUME_GUEST;
1688 break;
1689 }
1690 return r;
1691 }
1692 #endif /* CONFIG_ALTIVEC */
1693
kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu * vcpu,struct kvm_one_reg * reg)1694 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1695 {
1696 int r = 0;
1697 union kvmppc_one_reg val;
1698 int size;
1699
1700 size = one_reg_size(reg->id);
1701 if (size > sizeof(val))
1702 return -EINVAL;
1703
1704 r = kvmppc_get_one_reg(vcpu, reg->id, &val);
1705 if (r == -EINVAL) {
1706 r = 0;
1707 switch (reg->id) {
1708 #ifdef CONFIG_ALTIVEC
1709 case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
1710 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1711 r = -ENXIO;
1712 break;
1713 }
1714 val.vval = vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0];
1715 break;
1716 case KVM_REG_PPC_VSCR:
1717 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1718 r = -ENXIO;
1719 break;
1720 }
1721 val = get_reg_val(reg->id, vcpu->arch.vr.vscr.u[3]);
1722 break;
1723 case KVM_REG_PPC_VRSAVE:
1724 val = get_reg_val(reg->id, vcpu->arch.vrsave);
1725 break;
1726 #endif /* CONFIG_ALTIVEC */
1727 default:
1728 r = -EINVAL;
1729 break;
1730 }
1731 }
1732
1733 if (r)
1734 return r;
1735
1736 if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
1737 r = -EFAULT;
1738
1739 return r;
1740 }
1741
kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu * vcpu,struct kvm_one_reg * reg)1742 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1743 {
1744 int r;
1745 union kvmppc_one_reg val;
1746 int size;
1747
1748 size = one_reg_size(reg->id);
1749 if (size > sizeof(val))
1750 return -EINVAL;
1751
1752 if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
1753 return -EFAULT;
1754
1755 r = kvmppc_set_one_reg(vcpu, reg->id, &val);
1756 if (r == -EINVAL) {
1757 r = 0;
1758 switch (reg->id) {
1759 #ifdef CONFIG_ALTIVEC
1760 case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
1761 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1762 r = -ENXIO;
1763 break;
1764 }
1765 vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0] = val.vval;
1766 break;
1767 case KVM_REG_PPC_VSCR:
1768 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1769 r = -ENXIO;
1770 break;
1771 }
1772 vcpu->arch.vr.vscr.u[3] = set_reg_val(reg->id, val);
1773 break;
1774 case KVM_REG_PPC_VRSAVE:
1775 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1776 r = -ENXIO;
1777 break;
1778 }
1779 vcpu->arch.vrsave = set_reg_val(reg->id, val);
1780 break;
1781 #endif /* CONFIG_ALTIVEC */
1782 default:
1783 r = -EINVAL;
1784 break;
1785 }
1786 }
1787
1788 return r;
1789 }
1790
kvm_arch_vcpu_ioctl_run(struct kvm_vcpu * vcpu)1791 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
1792 {
1793 struct kvm_run *run = vcpu->run;
1794 int r;
1795
1796 vcpu_load(vcpu);
1797
1798 if (vcpu->mmio_needed) {
1799 vcpu->mmio_needed = 0;
1800 if (!vcpu->mmio_is_write)
1801 kvmppc_complete_mmio_load(vcpu);
1802 #ifdef CONFIG_VSX
1803 if (vcpu->arch.mmio_vsx_copy_nums > 0) {
1804 vcpu->arch.mmio_vsx_copy_nums--;
1805 vcpu->arch.mmio_vsx_offset++;
1806 }
1807
1808 if (vcpu->arch.mmio_vsx_copy_nums > 0) {
1809 r = kvmppc_emulate_mmio_vsx_loadstore(vcpu);
1810 if (r == RESUME_HOST) {
1811 vcpu->mmio_needed = 1;
1812 goto out;
1813 }
1814 }
1815 #endif
1816 #ifdef CONFIG_ALTIVEC
1817 if (vcpu->arch.mmio_vmx_copy_nums > 0) {
1818 vcpu->arch.mmio_vmx_copy_nums--;
1819 vcpu->arch.mmio_vmx_offset++;
1820 }
1821
1822 if (vcpu->arch.mmio_vmx_copy_nums > 0) {
1823 r = kvmppc_emulate_mmio_vmx_loadstore(vcpu);
1824 if (r == RESUME_HOST) {
1825 vcpu->mmio_needed = 1;
1826 goto out;
1827 }
1828 }
1829 #endif
1830 } else if (vcpu->arch.osi_needed) {
1831 u64 *gprs = run->osi.gprs;
1832 int i;
1833
1834 for (i = 0; i < 32; i++)
1835 kvmppc_set_gpr(vcpu, i, gprs[i]);
1836 vcpu->arch.osi_needed = 0;
1837 } else if (vcpu->arch.hcall_needed) {
1838 int i;
1839
1840 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
1841 for (i = 0; i < 9; ++i)
1842 kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
1843 vcpu->arch.hcall_needed = 0;
1844 #ifdef CONFIG_BOOKE
1845 } else if (vcpu->arch.epr_needed) {
1846 kvmppc_set_epr(vcpu, run->epr.epr);
1847 vcpu->arch.epr_needed = 0;
1848 #endif
1849 }
1850
1851 kvm_sigset_activate(vcpu);
1852
1853 if (run->immediate_exit)
1854 r = -EINTR;
1855 else
1856 r = kvmppc_vcpu_run(vcpu);
1857
1858 kvm_sigset_deactivate(vcpu);
1859
1860 #ifdef CONFIG_ALTIVEC
1861 out:
1862 #endif
1863
1864 /*
1865 * We're already returning to userspace, don't pass the
1866 * RESUME_HOST flags along.
1867 */
1868 if (r > 0)
1869 r = 0;
1870
1871 vcpu_put(vcpu);
1872 return r;
1873 }
1874
kvm_vcpu_ioctl_interrupt(struct kvm_vcpu * vcpu,struct kvm_interrupt * irq)1875 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
1876 {
1877 if (irq->irq == KVM_INTERRUPT_UNSET) {
1878 kvmppc_core_dequeue_external(vcpu);
1879 return 0;
1880 }
1881
1882 kvmppc_core_queue_external(vcpu, irq);
1883
1884 kvm_vcpu_kick(vcpu);
1885
1886 return 0;
1887 }
1888
kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu * vcpu,struct kvm_enable_cap * cap)1889 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1890 struct kvm_enable_cap *cap)
1891 {
1892 int r;
1893
1894 if (cap->flags)
1895 return -EINVAL;
1896
1897 switch (cap->cap) {
1898 case KVM_CAP_PPC_OSI:
1899 r = 0;
1900 vcpu->arch.osi_enabled = true;
1901 break;
1902 case KVM_CAP_PPC_PAPR:
1903 r = 0;
1904 vcpu->arch.papr_enabled = true;
1905 break;
1906 case KVM_CAP_PPC_EPR:
1907 r = 0;
1908 if (cap->args[0])
1909 vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
1910 else
1911 vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
1912 break;
1913 #ifdef CONFIG_BOOKE
1914 case KVM_CAP_PPC_BOOKE_WATCHDOG:
1915 r = 0;
1916 vcpu->arch.watchdog_enabled = true;
1917 break;
1918 #endif
1919 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
1920 case KVM_CAP_SW_TLB: {
1921 struct kvm_config_tlb cfg;
1922 void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
1923
1924 r = -EFAULT;
1925 if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
1926 break;
1927
1928 r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
1929 break;
1930 }
1931 #endif
1932 #ifdef CONFIG_KVM_MPIC
1933 case KVM_CAP_IRQ_MPIC: {
1934 struct fd f;
1935 struct kvm_device *dev;
1936
1937 r = -EBADF;
1938 f = fdget(cap->args[0]);
1939 if (!f.file)
1940 break;
1941
1942 r = -EPERM;
1943 dev = kvm_device_from_filp(f.file);
1944 if (dev)
1945 r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
1946
1947 fdput(f);
1948 break;
1949 }
1950 #endif
1951 #ifdef CONFIG_KVM_XICS
1952 case KVM_CAP_IRQ_XICS: {
1953 struct fd f;
1954 struct kvm_device *dev;
1955
1956 r = -EBADF;
1957 f = fdget(cap->args[0]);
1958 if (!f.file)
1959 break;
1960
1961 r = -EPERM;
1962 dev = kvm_device_from_filp(f.file);
1963 if (dev) {
1964 if (xics_on_xive())
1965 r = kvmppc_xive_connect_vcpu(dev, vcpu, cap->args[1]);
1966 else
1967 r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
1968 }
1969
1970 fdput(f);
1971 break;
1972 }
1973 #endif /* CONFIG_KVM_XICS */
1974 #ifdef CONFIG_KVM_XIVE
1975 case KVM_CAP_PPC_IRQ_XIVE: {
1976 struct fd f;
1977 struct kvm_device *dev;
1978
1979 r = -EBADF;
1980 f = fdget(cap->args[0]);
1981 if (!f.file)
1982 break;
1983
1984 r = -ENXIO;
1985 if (!xive_enabled())
1986 break;
1987
1988 r = -EPERM;
1989 dev = kvm_device_from_filp(f.file);
1990 if (dev)
1991 r = kvmppc_xive_native_connect_vcpu(dev, vcpu,
1992 cap->args[1]);
1993
1994 fdput(f);
1995 break;
1996 }
1997 #endif /* CONFIG_KVM_XIVE */
1998 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
1999 case KVM_CAP_PPC_FWNMI:
2000 r = -EINVAL;
2001 if (!is_kvmppc_hv_enabled(vcpu->kvm))
2002 break;
2003 r = 0;
2004 vcpu->kvm->arch.fwnmi_enabled = true;
2005 break;
2006 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
2007 default:
2008 r = -EINVAL;
2009 break;
2010 }
2011
2012 if (!r)
2013 r = kvmppc_sanity_check(vcpu);
2014
2015 return r;
2016 }
2017
kvm_arch_intc_initialized(struct kvm * kvm)2018 bool kvm_arch_intc_initialized(struct kvm *kvm)
2019 {
2020 #ifdef CONFIG_KVM_MPIC
2021 if (kvm->arch.mpic)
2022 return true;
2023 #endif
2024 #ifdef CONFIG_KVM_XICS
2025 if (kvm->arch.xics || kvm->arch.xive)
2026 return true;
2027 #endif
2028 return false;
2029 }
2030
kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu * vcpu,struct kvm_mp_state * mp_state)2031 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
2032 struct kvm_mp_state *mp_state)
2033 {
2034 return -EINVAL;
2035 }
2036
kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu * vcpu,struct kvm_mp_state * mp_state)2037 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
2038 struct kvm_mp_state *mp_state)
2039 {
2040 return -EINVAL;
2041 }
2042
kvm_arch_vcpu_async_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)2043 long kvm_arch_vcpu_async_ioctl(struct file *filp,
2044 unsigned int ioctl, unsigned long arg)
2045 {
2046 struct kvm_vcpu *vcpu = filp->private_data;
2047 void __user *argp = (void __user *)arg;
2048
2049 if (ioctl == KVM_INTERRUPT) {
2050 struct kvm_interrupt irq;
2051 if (copy_from_user(&irq, argp, sizeof(irq)))
2052 return -EFAULT;
2053 return kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2054 }
2055 return -ENOIOCTLCMD;
2056 }
2057
kvm_arch_vcpu_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)2058 long kvm_arch_vcpu_ioctl(struct file *filp,
2059 unsigned int ioctl, unsigned long arg)
2060 {
2061 struct kvm_vcpu *vcpu = filp->private_data;
2062 void __user *argp = (void __user *)arg;
2063 long r;
2064
2065 switch (ioctl) {
2066 case KVM_ENABLE_CAP:
2067 {
2068 struct kvm_enable_cap cap;
2069 r = -EFAULT;
2070 if (copy_from_user(&cap, argp, sizeof(cap)))
2071 goto out;
2072 vcpu_load(vcpu);
2073 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
2074 vcpu_put(vcpu);
2075 break;
2076 }
2077
2078 case KVM_SET_ONE_REG:
2079 case KVM_GET_ONE_REG:
2080 {
2081 struct kvm_one_reg reg;
2082 r = -EFAULT;
2083 if (copy_from_user(®, argp, sizeof(reg)))
2084 goto out;
2085 if (ioctl == KVM_SET_ONE_REG)
2086 r = kvm_vcpu_ioctl_set_one_reg(vcpu, ®);
2087 else
2088 r = kvm_vcpu_ioctl_get_one_reg(vcpu, ®);
2089 break;
2090 }
2091
2092 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
2093 case KVM_DIRTY_TLB: {
2094 struct kvm_dirty_tlb dirty;
2095 r = -EFAULT;
2096 if (copy_from_user(&dirty, argp, sizeof(dirty)))
2097 goto out;
2098 vcpu_load(vcpu);
2099 r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
2100 vcpu_put(vcpu);
2101 break;
2102 }
2103 #endif
2104 default:
2105 r = -EINVAL;
2106 }
2107
2108 out:
2109 return r;
2110 }
2111
kvm_arch_vcpu_fault(struct kvm_vcpu * vcpu,struct vm_fault * vmf)2112 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
2113 {
2114 return VM_FAULT_SIGBUS;
2115 }
2116
kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo * pvinfo)2117 static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
2118 {
2119 u32 inst_nop = 0x60000000;
2120 #ifdef CONFIG_KVM_BOOKE_HV
2121 u32 inst_sc1 = 0x44000022;
2122 pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
2123 pvinfo->hcall[1] = cpu_to_be32(inst_nop);
2124 pvinfo->hcall[2] = cpu_to_be32(inst_nop);
2125 pvinfo->hcall[3] = cpu_to_be32(inst_nop);
2126 #else
2127 u32 inst_lis = 0x3c000000;
2128 u32 inst_ori = 0x60000000;
2129 u32 inst_sc = 0x44000002;
2130 u32 inst_imm_mask = 0xffff;
2131
2132 /*
2133 * The hypercall to get into KVM from within guest context is as
2134 * follows:
2135 *
2136 * lis r0, r0, KVM_SC_MAGIC_R0@h
2137 * ori r0, KVM_SC_MAGIC_R0@l
2138 * sc
2139 * nop
2140 */
2141 pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
2142 pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
2143 pvinfo->hcall[2] = cpu_to_be32(inst_sc);
2144 pvinfo->hcall[3] = cpu_to_be32(inst_nop);
2145 #endif
2146
2147 pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
2148
2149 return 0;
2150 }
2151
kvm_arch_irqchip_in_kernel(struct kvm * kvm)2152 bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
2153 {
2154 int ret = 0;
2155
2156 #ifdef CONFIG_KVM_MPIC
2157 ret = ret || (kvm->arch.mpic != NULL);
2158 #endif
2159 #ifdef CONFIG_KVM_XICS
2160 ret = ret || (kvm->arch.xics != NULL);
2161 ret = ret || (kvm->arch.xive != NULL);
2162 #endif
2163 smp_rmb();
2164 return ret;
2165 }
2166
kvm_vm_ioctl_irq_line(struct kvm * kvm,struct kvm_irq_level * irq_event,bool line_status)2167 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
2168 bool line_status)
2169 {
2170 if (!kvm_arch_irqchip_in_kernel(kvm))
2171 return -ENXIO;
2172
2173 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
2174 irq_event->irq, irq_event->level,
2175 line_status);
2176 return 0;
2177 }
2178
2179
kvm_vm_ioctl_enable_cap(struct kvm * kvm,struct kvm_enable_cap * cap)2180 int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
2181 struct kvm_enable_cap *cap)
2182 {
2183 int r;
2184
2185 if (cap->flags)
2186 return -EINVAL;
2187
2188 switch (cap->cap) {
2189 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
2190 case KVM_CAP_PPC_ENABLE_HCALL: {
2191 unsigned long hcall = cap->args[0];
2192
2193 r = -EINVAL;
2194 if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
2195 cap->args[1] > 1)
2196 break;
2197 if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
2198 break;
2199 if (cap->args[1])
2200 set_bit(hcall / 4, kvm->arch.enabled_hcalls);
2201 else
2202 clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
2203 r = 0;
2204 break;
2205 }
2206 case KVM_CAP_PPC_SMT: {
2207 unsigned long mode = cap->args[0];
2208 unsigned long flags = cap->args[1];
2209
2210 r = -EINVAL;
2211 if (kvm->arch.kvm_ops->set_smt_mode)
2212 r = kvm->arch.kvm_ops->set_smt_mode(kvm, mode, flags);
2213 break;
2214 }
2215
2216 case KVM_CAP_PPC_NESTED_HV:
2217 r = -EINVAL;
2218 if (!is_kvmppc_hv_enabled(kvm) ||
2219 !kvm->arch.kvm_ops->enable_nested)
2220 break;
2221 r = kvm->arch.kvm_ops->enable_nested(kvm);
2222 break;
2223 #endif
2224 #if defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE)
2225 case KVM_CAP_PPC_SECURE_GUEST:
2226 r = -EINVAL;
2227 if (!is_kvmppc_hv_enabled(kvm) || !kvm->arch.kvm_ops->enable_svm)
2228 break;
2229 r = kvm->arch.kvm_ops->enable_svm(kvm);
2230 break;
2231 case KVM_CAP_PPC_DAWR1:
2232 r = -EINVAL;
2233 if (!is_kvmppc_hv_enabled(kvm) || !kvm->arch.kvm_ops->enable_dawr1)
2234 break;
2235 r = kvm->arch.kvm_ops->enable_dawr1(kvm);
2236 break;
2237 #endif
2238 default:
2239 r = -EINVAL;
2240 break;
2241 }
2242
2243 return r;
2244 }
2245
2246 #ifdef CONFIG_PPC_BOOK3S_64
2247 /*
2248 * These functions check whether the underlying hardware is safe
2249 * against attacks based on observing the effects of speculatively
2250 * executed instructions, and whether it supplies instructions for
2251 * use in workarounds. The information comes from firmware, either
2252 * via the device tree on powernv platforms or from an hcall on
2253 * pseries platforms.
2254 */
2255 #ifdef CONFIG_PPC_PSERIES
pseries_get_cpu_char(struct kvm_ppc_cpu_char * cp)2256 static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp)
2257 {
2258 struct h_cpu_char_result c;
2259 unsigned long rc;
2260
2261 if (!machine_is(pseries))
2262 return -ENOTTY;
2263
2264 rc = plpar_get_cpu_characteristics(&c);
2265 if (rc == H_SUCCESS) {
2266 cp->character = c.character;
2267 cp->behaviour = c.behaviour;
2268 cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 |
2269 KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED |
2270 KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 |
2271 KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 |
2272 KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV |
2273 KVM_PPC_CPU_CHAR_BR_HINT_HONOURED |
2274 KVM_PPC_CPU_CHAR_MTTRIG_THR_RECONF |
2275 KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS |
2276 KVM_PPC_CPU_CHAR_BCCTR_FLUSH_ASSIST;
2277 cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY |
2278 KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR |
2279 KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR |
2280 KVM_PPC_CPU_BEHAV_FLUSH_COUNT_CACHE;
2281 }
2282 return 0;
2283 }
2284 #else
pseries_get_cpu_char(struct kvm_ppc_cpu_char * cp)2285 static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp)
2286 {
2287 return -ENOTTY;
2288 }
2289 #endif
2290
have_fw_feat(struct device_node * fw_features,const char * state,const char * name)2291 static inline bool have_fw_feat(struct device_node *fw_features,
2292 const char *state, const char *name)
2293 {
2294 struct device_node *np;
2295 bool r = false;
2296
2297 np = of_get_child_by_name(fw_features, name);
2298 if (np) {
2299 r = of_property_read_bool(np, state);
2300 of_node_put(np);
2301 }
2302 return r;
2303 }
2304
kvmppc_get_cpu_char(struct kvm_ppc_cpu_char * cp)2305 static int kvmppc_get_cpu_char(struct kvm_ppc_cpu_char *cp)
2306 {
2307 struct device_node *np, *fw_features;
2308 int r;
2309
2310 memset(cp, 0, sizeof(*cp));
2311 r = pseries_get_cpu_char(cp);
2312 if (r != -ENOTTY)
2313 return r;
2314
2315 np = of_find_node_by_name(NULL, "ibm,opal");
2316 if (np) {
2317 fw_features = of_get_child_by_name(np, "fw-features");
2318 of_node_put(np);
2319 if (!fw_features)
2320 return 0;
2321 if (have_fw_feat(fw_features, "enabled",
2322 "inst-spec-barrier-ori31,31,0"))
2323 cp->character |= KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31;
2324 if (have_fw_feat(fw_features, "enabled",
2325 "fw-bcctrl-serialized"))
2326 cp->character |= KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED;
2327 if (have_fw_feat(fw_features, "enabled",
2328 "inst-l1d-flush-ori30,30,0"))
2329 cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30;
2330 if (have_fw_feat(fw_features, "enabled",
2331 "inst-l1d-flush-trig2"))
2332 cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2;
2333 if (have_fw_feat(fw_features, "enabled",
2334 "fw-l1d-thread-split"))
2335 cp->character |= KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV;
2336 if (have_fw_feat(fw_features, "enabled",
2337 "fw-count-cache-disabled"))
2338 cp->character |= KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS;
2339 if (have_fw_feat(fw_features, "enabled",
2340 "fw-count-cache-flush-bcctr2,0,0"))
2341 cp->character |= KVM_PPC_CPU_CHAR_BCCTR_FLUSH_ASSIST;
2342 cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 |
2343 KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED |
2344 KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 |
2345 KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 |
2346 KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV |
2347 KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS |
2348 KVM_PPC_CPU_CHAR_BCCTR_FLUSH_ASSIST;
2349
2350 if (have_fw_feat(fw_features, "enabled",
2351 "speculation-policy-favor-security"))
2352 cp->behaviour |= KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY;
2353 if (!have_fw_feat(fw_features, "disabled",
2354 "needs-l1d-flush-msr-pr-0-to-1"))
2355 cp->behaviour |= KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR;
2356 if (!have_fw_feat(fw_features, "disabled",
2357 "needs-spec-barrier-for-bound-checks"))
2358 cp->behaviour |= KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
2359 if (have_fw_feat(fw_features, "enabled",
2360 "needs-count-cache-flush-on-context-switch"))
2361 cp->behaviour |= KVM_PPC_CPU_BEHAV_FLUSH_COUNT_CACHE;
2362 cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY |
2363 KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR |
2364 KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR |
2365 KVM_PPC_CPU_BEHAV_FLUSH_COUNT_CACHE;
2366
2367 of_node_put(fw_features);
2368 }
2369
2370 return 0;
2371 }
2372 #endif
2373
kvm_arch_vm_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)2374 long kvm_arch_vm_ioctl(struct file *filp,
2375 unsigned int ioctl, unsigned long arg)
2376 {
2377 struct kvm *kvm __maybe_unused = filp->private_data;
2378 void __user *argp = (void __user *)arg;
2379 long r;
2380
2381 switch (ioctl) {
2382 case KVM_PPC_GET_PVINFO: {
2383 struct kvm_ppc_pvinfo pvinfo;
2384 memset(&pvinfo, 0, sizeof(pvinfo));
2385 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
2386 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
2387 r = -EFAULT;
2388 goto out;
2389 }
2390
2391 break;
2392 }
2393 #ifdef CONFIG_SPAPR_TCE_IOMMU
2394 case KVM_CREATE_SPAPR_TCE_64: {
2395 struct kvm_create_spapr_tce_64 create_tce_64;
2396
2397 r = -EFAULT;
2398 if (copy_from_user(&create_tce_64, argp, sizeof(create_tce_64)))
2399 goto out;
2400 if (create_tce_64.flags) {
2401 r = -EINVAL;
2402 goto out;
2403 }
2404 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
2405 goto out;
2406 }
2407 case KVM_CREATE_SPAPR_TCE: {
2408 struct kvm_create_spapr_tce create_tce;
2409 struct kvm_create_spapr_tce_64 create_tce_64;
2410
2411 r = -EFAULT;
2412 if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
2413 goto out;
2414
2415 create_tce_64.liobn = create_tce.liobn;
2416 create_tce_64.page_shift = IOMMU_PAGE_SHIFT_4K;
2417 create_tce_64.offset = 0;
2418 create_tce_64.size = create_tce.window_size >>
2419 IOMMU_PAGE_SHIFT_4K;
2420 create_tce_64.flags = 0;
2421 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
2422 goto out;
2423 }
2424 #endif
2425 #ifdef CONFIG_PPC_BOOK3S_64
2426 case KVM_PPC_GET_SMMU_INFO: {
2427 struct kvm_ppc_smmu_info info;
2428 struct kvm *kvm = filp->private_data;
2429
2430 memset(&info, 0, sizeof(info));
2431 r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
2432 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
2433 r = -EFAULT;
2434 break;
2435 }
2436 case KVM_PPC_RTAS_DEFINE_TOKEN: {
2437 struct kvm *kvm = filp->private_data;
2438
2439 r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
2440 break;
2441 }
2442 case KVM_PPC_CONFIGURE_V3_MMU: {
2443 struct kvm *kvm = filp->private_data;
2444 struct kvm_ppc_mmuv3_cfg cfg;
2445
2446 r = -EINVAL;
2447 if (!kvm->arch.kvm_ops->configure_mmu)
2448 goto out;
2449 r = -EFAULT;
2450 if (copy_from_user(&cfg, argp, sizeof(cfg)))
2451 goto out;
2452 r = kvm->arch.kvm_ops->configure_mmu(kvm, &cfg);
2453 break;
2454 }
2455 case KVM_PPC_GET_RMMU_INFO: {
2456 struct kvm *kvm = filp->private_data;
2457 struct kvm_ppc_rmmu_info info;
2458
2459 r = -EINVAL;
2460 if (!kvm->arch.kvm_ops->get_rmmu_info)
2461 goto out;
2462 r = kvm->arch.kvm_ops->get_rmmu_info(kvm, &info);
2463 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
2464 r = -EFAULT;
2465 break;
2466 }
2467 case KVM_PPC_GET_CPU_CHAR: {
2468 struct kvm_ppc_cpu_char cpuchar;
2469
2470 r = kvmppc_get_cpu_char(&cpuchar);
2471 if (r >= 0 && copy_to_user(argp, &cpuchar, sizeof(cpuchar)))
2472 r = -EFAULT;
2473 break;
2474 }
2475 case KVM_PPC_SVM_OFF: {
2476 struct kvm *kvm = filp->private_data;
2477
2478 r = 0;
2479 if (!kvm->arch.kvm_ops->svm_off)
2480 goto out;
2481
2482 r = kvm->arch.kvm_ops->svm_off(kvm);
2483 break;
2484 }
2485 default: {
2486 struct kvm *kvm = filp->private_data;
2487 r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
2488 }
2489 #else /* CONFIG_PPC_BOOK3S_64 */
2490 default:
2491 r = -ENOTTY;
2492 #endif
2493 }
2494 out:
2495 return r;
2496 }
2497
2498 static DEFINE_IDA(lpid_inuse);
2499 static unsigned long nr_lpids;
2500
kvmppc_alloc_lpid(void)2501 long kvmppc_alloc_lpid(void)
2502 {
2503 int lpid;
2504
2505 /* The host LPID must always be 0 (allocation starts at 1) */
2506 lpid = ida_alloc_range(&lpid_inuse, 1, nr_lpids - 1, GFP_KERNEL);
2507 if (lpid < 0) {
2508 if (lpid == -ENOMEM)
2509 pr_err("%s: Out of memory\n", __func__);
2510 else
2511 pr_err("%s: No LPIDs free\n", __func__);
2512 return -ENOMEM;
2513 }
2514
2515 return lpid;
2516 }
2517 EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
2518
kvmppc_free_lpid(long lpid)2519 void kvmppc_free_lpid(long lpid)
2520 {
2521 ida_free(&lpid_inuse, lpid);
2522 }
2523 EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
2524
2525 /* nr_lpids_param includes the host LPID */
kvmppc_init_lpid(unsigned long nr_lpids_param)2526 void kvmppc_init_lpid(unsigned long nr_lpids_param)
2527 {
2528 nr_lpids = nr_lpids_param;
2529 }
2530 EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
2531
2532 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);
2533
kvm_arch_create_vcpu_debugfs(struct kvm_vcpu * vcpu,struct dentry * debugfs_dentry)2534 void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu, struct dentry *debugfs_dentry)
2535 {
2536 if (vcpu->kvm->arch.kvm_ops->create_vcpu_debugfs)
2537 vcpu->kvm->arch.kvm_ops->create_vcpu_debugfs(vcpu, debugfs_dentry);
2538 }
2539
kvm_arch_create_vm_debugfs(struct kvm * kvm)2540 int kvm_arch_create_vm_debugfs(struct kvm *kvm)
2541 {
2542 if (kvm->arch.kvm_ops->create_vm_debugfs)
2543 kvm->arch.kvm_ops->create_vm_debugfs(kvm);
2544 return 0;
2545 }
2546