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