blob: 0e42aa8a279f323d7728c993251c65b788ef27ae [file] [log] [blame]
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14 *
15 * Copyright IBM Corp. 2007
16 *
17 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
19 */
20
21#include <linux/errno.h>
22#include <linux/err.h>
23#include <linux/kvm_host.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050024#include <linux/vmalloc.h>
Alexander Graf544c6762009-11-02 12:02:31 +000025#include <linux/hrtimer.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010026#include <linux/sched/signal.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050027#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Scott Woodeb1e4f42013-04-12 14:08:47 +000029#include <linux/file.h>
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +053030#include <linux/module.h>
Suresh Warrier95767302016-08-19 15:35:47 +100031#include <linux/irqbypass.h>
32#include <linux/kvm_irqfd.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050033#include <asm/cputable.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080034#include <linux/uaccess.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050035#include <asm/kvm_ppc.h>
Hollis Blanchard83aae4a2008-07-25 13:54:52 -050036#include <asm/tlbflush.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000037#include <asm/cputhreads.h>
Alexander Grafbd2be682012-08-13 01:04:19 +020038#include <asm/irqflags.h>
Alexey Kardashevskiy58ded422016-03-01 17:54:40 +110039#include <asm/iommu.h>
Hollis Blanchard73e75b42008-12-02 15:51:57 -060040#include "timing.h"
Alexander Graf5efdb4b2013-04-17 00:37:57 +020041#include "irq.h"
Paul Mackerrasfad7b9b2008-12-23 14:57:26 +110042#include "../mm/mmu_decl.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050043
Marcelo Tosatti46f43c62009-06-18 11:47:27 -030044#define CREATE_TRACE_POINTS
45#include "trace.h"
46
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +053047struct kvmppc_ops *kvmppc_hv_ops;
48EXPORT_SYMBOL_GPL(kvmppc_hv_ops);
49struct kvmppc_ops *kvmppc_pr_ops;
50EXPORT_SYMBOL_GPL(kvmppc_pr_ops);
51
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053052
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050053int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
54{
Liu Yu-B132019202e072012-07-03 05:48:52 +000055 return !!(v->arch.pending_exceptions) ||
Scott Wooddfd4d472011-11-17 12:39:59 +000056 v->requests;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050057}
58
Christoffer Dallb6d33832012-03-08 16:44:24 -050059int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
60{
61 return 1;
62}
63
Alexander Graf03d25c52012-08-10 12:28:50 +020064/*
65 * Common checks before entering the guest world. Call with interrupts
66 * disabled.
67 *
Alexander Graf7ee78852012-08-13 12:44:41 +020068 * returns:
69 *
70 * == 1 if we're ready to go into guest state
71 * <= 0 if we need to go back to the host with return value
Alexander Graf03d25c52012-08-10 12:28:50 +020072 */
73int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
74{
Scott Wood6c85f522014-01-09 19:18:40 -060075 int r;
Alexander Graf03d25c52012-08-10 12:28:50 +020076
Scott Wood6c85f522014-01-09 19:18:40 -060077 WARN_ON(irqs_disabled());
78 hard_irq_disable();
79
Alexander Graf03d25c52012-08-10 12:28:50 +020080 while (true) {
81 if (need_resched()) {
82 local_irq_enable();
83 cond_resched();
Scott Wood6c85f522014-01-09 19:18:40 -060084 hard_irq_disable();
Alexander Graf03d25c52012-08-10 12:28:50 +020085 continue;
86 }
87
88 if (signal_pending(current)) {
Alexander Graf7ee78852012-08-13 12:44:41 +020089 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
90 vcpu->run->exit_reason = KVM_EXIT_INTR;
91 r = -EINTR;
Alexander Graf03d25c52012-08-10 12:28:50 +020092 break;
93 }
94
Scott Wood5bd1cf12012-08-22 15:03:50 +000095 vcpu->mode = IN_GUEST_MODE;
96
97 /*
98 * Reading vcpu->requests must happen after setting vcpu->mode,
99 * so we don't miss a request because the requester sees
100 * OUTSIDE_GUEST_MODE and assumes we'll be checking requests
101 * before next entering the guest (and thus doesn't IPI).
Lan Tianyu489153c2016-03-13 11:10:30 +0800102 * This also orders the write to mode from any reads
103 * to the page tables done while the VCPU is running.
104 * Please see the comment in kvm_flush_remote_tlbs.
Scott Wood5bd1cf12012-08-22 15:03:50 +0000105 */
Alexander Graf03d25c52012-08-10 12:28:50 +0200106 smp_mb();
Scott Wood5bd1cf12012-08-22 15:03:50 +0000107
Alexander Graf03d25c52012-08-10 12:28:50 +0200108 if (vcpu->requests) {
109 /* Make sure we process requests preemptable */
110 local_irq_enable();
111 trace_kvm_check_requests(vcpu);
Alexander Graf7c973a22012-08-13 12:50:35 +0200112 r = kvmppc_core_check_requests(vcpu);
Scott Wood6c85f522014-01-09 19:18:40 -0600113 hard_irq_disable();
Alexander Graf7c973a22012-08-13 12:50:35 +0200114 if (r > 0)
115 continue;
116 break;
Alexander Graf03d25c52012-08-10 12:28:50 +0200117 }
118
119 if (kvmppc_core_prepare_to_enter(vcpu)) {
120 /* interrupts got enabled in between, so we
121 are back at square 1 */
122 continue;
123 }
124
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200125 guest_enter_irqoff();
Scott Wood6c85f522014-01-09 19:18:40 -0600126 return 1;
Alexander Graf03d25c52012-08-10 12:28:50 +0200127 }
128
Scott Wood6c85f522014-01-09 19:18:40 -0600129 /* return to host */
130 local_irq_enable();
Alexander Graf03d25c52012-08-10 12:28:50 +0200131 return r;
132}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +0530133EXPORT_SYMBOL_GPL(kvmppc_prepare_to_enter);
Alexander Graf03d25c52012-08-10 12:28:50 +0200134
Alexander Graf5deb8e72014-04-24 13:46:24 +0200135#if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
136static void kvmppc_swab_shared(struct kvm_vcpu *vcpu)
137{
138 struct kvm_vcpu_arch_shared *shared = vcpu->arch.shared;
139 int i;
140
141 shared->sprg0 = swab64(shared->sprg0);
142 shared->sprg1 = swab64(shared->sprg1);
143 shared->sprg2 = swab64(shared->sprg2);
144 shared->sprg3 = swab64(shared->sprg3);
145 shared->srr0 = swab64(shared->srr0);
146 shared->srr1 = swab64(shared->srr1);
147 shared->dar = swab64(shared->dar);
148 shared->msr = swab64(shared->msr);
149 shared->dsisr = swab32(shared->dsisr);
150 shared->int_pending = swab32(shared->int_pending);
151 for (i = 0; i < ARRAY_SIZE(shared->sr); i++)
152 shared->sr[i] = swab32(shared->sr[i]);
153}
154#endif
155
Alexander Graf2a342ed2010-07-29 14:47:48 +0200156int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
157{
158 int nr = kvmppc_get_gpr(vcpu, 11);
159 int r;
160 unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
161 unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
162 unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
163 unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
164 unsigned long r2 = 0;
165
Alexander Graf5deb8e72014-04-24 13:46:24 +0200166 if (!(kvmppc_get_msr(vcpu) & MSR_SF)) {
Alexander Graf2a342ed2010-07-29 14:47:48 +0200167 /* 32 bit mode */
168 param1 &= 0xffffffff;
169 param2 &= 0xffffffff;
170 param3 &= 0xffffffff;
171 param4 &= 0xffffffff;
172 }
173
174 switch (nr) {
Stuart Yoderfdcf8bd2012-07-03 05:48:50 +0000175 case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE):
Alexander Graf5fc87402010-07-29 14:47:55 +0200176 {
Alexander Graf5deb8e72014-04-24 13:46:24 +0200177#if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
178 /* Book3S can be little endian, find it out here */
179 int shared_big_endian = true;
180 if (vcpu->arch.intr_msr & MSR_LE)
181 shared_big_endian = false;
182 if (shared_big_endian != vcpu->arch.shared_big_endian)
183 kvmppc_swab_shared(vcpu);
184 vcpu->arch.shared_big_endian = shared_big_endian;
185#endif
186
Alexander Graff3383cf2014-05-12 01:08:32 +0200187 if (!(param2 & MAGIC_PAGE_FLAG_NOT_MAPPED_NX)) {
188 /*
189 * Older versions of the Linux magic page code had
190 * a bug where they would map their trampoline code
191 * NX. If that's the case, remove !PR NX capability.
192 */
193 vcpu->arch.disable_kernel_nx = true;
194 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
195 }
196
197 vcpu->arch.magic_page_pa = param1 & ~0xfffULL;
198 vcpu->arch.magic_page_ea = param2 & ~0xfffULL;
Alexander Graf5fc87402010-07-29 14:47:55 +0200199
Alexander Graf89b68c92014-07-13 16:37:12 +0200200#ifdef CONFIG_PPC_64K_PAGES
201 /*
202 * Make sure our 4k magic page is in the same window of a 64k
203 * page within the guest and within the host's page.
204 */
205 if ((vcpu->arch.magic_page_pa & 0xf000) !=
206 ((ulong)vcpu->arch.shared & 0xf000)) {
207 void *old_shared = vcpu->arch.shared;
208 ulong shared = (ulong)vcpu->arch.shared;
209 void *new_shared;
210
211 shared &= PAGE_MASK;
212 shared |= vcpu->arch.magic_page_pa & 0xf000;
213 new_shared = (void*)shared;
214 memcpy(new_shared, old_shared, 0x1000);
215 vcpu->arch.shared = new_shared;
216 }
217#endif
218
Scott Woodb5904972011-11-08 18:23:30 -0600219 r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
Alexander Graf7508e162010-08-03 11:32:56 +0200220
Stuart Yoderfdcf8bd2012-07-03 05:48:50 +0000221 r = EV_SUCCESS;
Alexander Graf5fc87402010-07-29 14:47:55 +0200222 break;
223 }
Stuart Yoderfdcf8bd2012-07-03 05:48:50 +0000224 case KVM_HCALL_TOKEN(KVM_HC_FEATURES):
225 r = EV_SUCCESS;
Alexander Grafbf7ca4b2012-02-15 23:40:00 +0000226#if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2)
Alexander Graf5fc87402010-07-29 14:47:55 +0200227 r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
228#endif
Alexander Graf2a342ed2010-07-29 14:47:48 +0200229
230 /* Second return value is in r4 */
Alexander Graf2a342ed2010-07-29 14:47:48 +0200231 break;
Liu Yu-B132019202e072012-07-03 05:48:52 +0000232 case EV_HCALL_TOKEN(EV_IDLE):
233 r = EV_SUCCESS;
234 kvm_vcpu_block(vcpu);
235 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
236 break;
Alexander Graf2a342ed2010-07-29 14:47:48 +0200237 default:
Stuart Yoderfdcf8bd2012-07-03 05:48:50 +0000238 r = EV_UNIMPLEMENTED;
Alexander Graf2a342ed2010-07-29 14:47:48 +0200239 break;
240 }
241
Alexander Graf7508e162010-08-03 11:32:56 +0200242 kvmppc_set_gpr(vcpu, 4, r2);
243
Alexander Graf2a342ed2010-07-29 14:47:48 +0200244 return r;
245}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +0530246EXPORT_SYMBOL_GPL(kvmppc_kvm_pv);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500247
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200248int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
249{
250 int r = false;
251
252 /* We have to know what CPU to virtualize */
253 if (!vcpu->arch.pvr)
254 goto out;
255
256 /* PAPR only works with book3s_64 */
257 if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
258 goto out;
259
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200260 /* HV KVM can only do PAPR mode for now */
Aneesh Kumar K.Va78b55d2013-10-07 22:18:02 +0530261 if (!vcpu->arch.papr_enabled && is_kvmppc_hv_enabled(vcpu->kvm))
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200262 goto out;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200263
Scott Woodd30f6e42011-12-20 15:34:43 +0000264#ifdef CONFIG_KVM_BOOKE_HV
265 if (!cpu_has_feature(CPU_FTR_EMB_HV))
266 goto out;
267#endif
268
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200269 r = true;
270
271out:
272 vcpu->arch.sane = r;
273 return r ? 0 : -EINVAL;
274}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +0530275EXPORT_SYMBOL_GPL(kvmppc_sanity_check);
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200276
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500277int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
278{
279 enum emulation_result er;
280 int r;
281
Alexander Grafd69614a2014-06-18 14:53:49 +0200282 er = kvmppc_emulate_loadstore(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500283 switch (er) {
284 case EMULATE_DONE:
285 /* Future optimization: only reload non-volatiles if they were
286 * actually modified. */
287 r = RESUME_GUEST_NV;
288 break;
Mihai Caraman51f04722014-07-23 19:06:21 +0300289 case EMULATE_AGAIN:
290 r = RESUME_GUEST;
291 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500292 case EMULATE_DO_MMIO:
293 run->exit_reason = KVM_EXIT_MMIO;
294 /* We must reload nonvolatiles because "update" load/store
295 * instructions modify register state. */
296 /* Future optimization: only reload non-volatiles if they were
297 * actually modified. */
298 r = RESUME_HOST_NV;
299 break;
300 case EMULATE_FAIL:
Mihai Caraman51f04722014-07-23 19:06:21 +0300301 {
302 u32 last_inst;
303
Alexander Graf8d0eff62014-09-10 14:37:29 +0200304 kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500305 /* XXX Deliver Program interrupt to guest. */
Mihai Caraman51f04722014-07-23 19:06:21 +0300306 pr_emerg("%s: emulation failed (%08x)\n", __func__, last_inst);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500307 r = RESUME_HOST;
308 break;
Mihai Caraman51f04722014-07-23 19:06:21 +0300309 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500310 default:
Alexander Graf5a331692012-12-14 23:46:03 +0100311 WARN_ON(1);
312 r = RESUME_GUEST;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500313 }
314
315 return r;
316}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +0530317EXPORT_SYMBOL_GPL(kvmppc_emulate_mmio);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500318
Alexander Graf35c4a732014-06-20 13:58:16 +0200319int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
320 bool data)
321{
Alexander Grafc12fb432014-06-20 14:43:36 +0200322 ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
Alexander Graf35c4a732014-06-20 13:58:16 +0200323 struct kvmppc_pte pte;
324 int r;
325
326 vcpu->stat.st++;
327
328 r = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
329 XLATE_WRITE, &pte);
330 if (r < 0)
331 return r;
332
333 *eaddr = pte.raddr;
334
335 if (!pte.may_write)
336 return -EPERM;
337
Alexander Grafc12fb432014-06-20 14:43:36 +0200338 /* Magic page override */
339 if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
340 ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
341 !(kvmppc_get_msr(vcpu) & MSR_PR)) {
342 void *magic = vcpu->arch.shared;
343 magic += pte.eaddr & 0xfff;
344 memcpy(magic, ptr, size);
345 return EMULATE_DONE;
346 }
347
Alexander Graf35c4a732014-06-20 13:58:16 +0200348 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
349 return EMULATE_DO_MMIO;
350
351 return EMULATE_DONE;
352}
353EXPORT_SYMBOL_GPL(kvmppc_st);
354
355int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
356 bool data)
357{
Alexander Grafc12fb432014-06-20 14:43:36 +0200358 ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
Alexander Graf35c4a732014-06-20 13:58:16 +0200359 struct kvmppc_pte pte;
Alexander Graf35c4a732014-06-20 13:58:16 +0200360 int rc;
361
362 vcpu->stat.ld++;
363
364 rc = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
365 XLATE_READ, &pte);
366 if (rc)
367 return rc;
368
369 *eaddr = pte.raddr;
370
371 if (!pte.may_read)
372 return -EPERM;
373
374 if (!data && !pte.may_execute)
375 return -ENOEXEC;
376
Alexander Grafc12fb432014-06-20 14:43:36 +0200377 /* Magic page override */
378 if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
379 ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
380 !(kvmppc_get_msr(vcpu) & MSR_PR)) {
381 void *magic = vcpu->arch.shared;
382 magic += pte.eaddr & 0xfff;
383 memcpy(ptr, magic, size);
384 return EMULATE_DONE;
385 }
386
Alexander Grafc45c5512014-06-20 14:17:30 +0200387 if (kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size))
388 return EMULATE_DO_MMIO;
Alexander Graf35c4a732014-06-20 13:58:16 +0200389
390 return EMULATE_DONE;
Alexander Graf35c4a732014-06-20 13:58:16 +0200391}
392EXPORT_SYMBOL_GPL(kvmppc_ld);
393
Radim Krčmář13a34e02014-08-28 15:13:03 +0200394int kvm_arch_hardware_enable(void)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500395{
Alexander Graf10474ae2009-09-15 11:37:46 +0200396 return 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500397}
398
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500399int kvm_arch_hardware_setup(void)
400{
401 return 0;
402}
403
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500404void kvm_arch_check_processor_compat(void *rtn)
405{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600406 *(int *)rtn = kvmppc_core_check_processor_compat();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500407}
408
Carsten Ottee08b9632012-01-04 10:25:20 +0100409int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500410{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530411 struct kvmppc_ops *kvm_ops = NULL;
412 /*
413 * if we have both HV and PR enabled, default is HV
414 */
415 if (type == 0) {
416 if (kvmppc_hv_ops)
417 kvm_ops = kvmppc_hv_ops;
418 else
419 kvm_ops = kvmppc_pr_ops;
420 if (!kvm_ops)
421 goto err_out;
422 } else if (type == KVM_VM_PPC_HV) {
423 if (!kvmppc_hv_ops)
424 goto err_out;
425 kvm_ops = kvmppc_hv_ops;
426 } else if (type == KVM_VM_PPC_PR) {
427 if (!kvmppc_pr_ops)
428 goto err_out;
429 kvm_ops = kvmppc_pr_ops;
430 } else
431 goto err_out;
Carsten Ottee08b9632012-01-04 10:25:20 +0100432
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530433 if (kvm_ops->owner && !try_module_get(kvm_ops->owner))
434 return -ENOENT;
435
436 kvm->arch.kvm_ops = kvm_ops;
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000437 return kvmppc_core_init_vm(kvm);
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530438err_out:
439 return -EINVAL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500440}
441
Luiz Capitulino235539b2016-09-07 14:47:23 -0400442bool kvm_arch_has_vcpu_debugfs(void)
443{
444 return false;
445}
446
447int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
448{
449 return 0;
450}
451
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100452void kvm_arch_destroy_vm(struct kvm *kvm)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500453{
454 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300455 struct kvm_vcpu *vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500456
Suresh E. Warriere17769e2015-12-21 16:22:51 -0600457#ifdef CONFIG_KVM_XICS
458 /*
459 * We call kick_all_cpus_sync() to ensure that all
460 * CPUs have executed any pending IPIs before we
461 * continue and free VCPUs structures below.
462 */
463 if (is_kvmppc_hv_enabled(kvm))
464 kick_all_cpus_sync();
465#endif
466
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300467 kvm_for_each_vcpu(i, vcpu, kvm)
468 kvm_arch_vcpu_free(vcpu);
469
470 mutex_lock(&kvm->lock);
471 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
472 kvm->vcpus[i] = NULL;
473
474 atomic_set(&kvm->online_vcpus, 0);
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000475
476 kvmppc_core_destroy_vm(kvm);
477
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300478 mutex_unlock(&kvm->lock);
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530479
480 /* drop the module reference */
481 module_put(kvm->arch.kvm_ops->owner);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500482}
483
Alexander Graf784aa3d2014-07-14 18:27:35 +0200484int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500485{
486 int r;
Alexander Graf7a587772014-07-14 18:55:19 +0200487 /* Assume we're using HV mode when the HV module is loaded */
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530488 int hv_enabled = kvmppc_hv_ops ? 1 : 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500489
Alexander Graf7a587772014-07-14 18:55:19 +0200490 if (kvm) {
491 /*
492 * Hooray - we know which VM type we're running on. Depend on
493 * that rather than the guess above.
494 */
495 hv_enabled = is_kvmppc_hv_enabled(kvm);
496 }
497
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500498 switch (ext) {
Scott Wood5ce941e2011-04-27 17:24:21 -0500499#ifdef CONFIG_BOOKE
500 case KVM_CAP_PPC_BOOKE_SREGS:
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000501 case KVM_CAP_PPC_BOOKE_WATCHDOG:
Alexander Graf1c810632013-01-04 18:12:48 +0100502 case KVM_CAP_PPC_EPR:
Scott Wood5ce941e2011-04-27 17:24:21 -0500503#else
Alexander Grafe15a1132009-11-30 03:02:02 +0000504 case KVM_CAP_PPC_SEGSTATE:
Alexander Graf1022fc32011-09-14 21:45:23 +0200505 case KVM_CAP_PPC_HIOR:
Alexander Graf930b4122011-08-08 17:29:42 +0200506 case KVM_CAP_PPC_PAPR:
Scott Wood5ce941e2011-04-27 17:24:21 -0500507#endif
Alexander Graf18978762010-03-24 21:48:18 +0100508 case KVM_CAP_PPC_UNSET_IRQ:
Alexander Graf7b4203e2010-08-30 13:50:45 +0200509 case KVM_CAP_PPC_IRQ_LEVEL:
Alexander Graf71fbfd52010-03-24 21:48:29 +0100510 case KVM_CAP_ENABLE_CAP:
Paul Mackerras699a0ea2014-06-02 11:02:59 +1000511 case KVM_CAP_ENABLE_CAP_VM:
Alexander Grafe24ed812011-09-14 10:02:41 +0200512 case KVM_CAP_ONE_REG:
Alexander Graf0e673fb2012-10-09 00:06:20 +0200513 case KVM_CAP_IOEVENTFD:
Scott Wood5df554ad2013-04-12 14:08:46 +0000514 case KVM_CAP_DEVICE_CTRL:
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100515 case KVM_CAP_IMMEDIATE_EXIT:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000516 r = 1;
517 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000518 case KVM_CAP_PPC_PAIRED_SINGLES:
Alexander Grafad0a0482010-03-24 21:48:30 +0100519 case KVM_CAP_PPC_OSI:
Alexander Graf15711e92010-07-29 14:48:08 +0200520 case KVM_CAP_PPC_GET_PVINFO:
Alexander Grafbf7ca4b2012-02-15 23:40:00 +0000521#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500522 case KVM_CAP_SW_TLB:
523#endif
Aneesh Kumar K.V699cc872013-10-07 22:17:56 +0530524 /* We support this only for PR */
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530525 r = !hv_enabled;
Alexander Grafe15a1132009-11-30 03:02:02 +0000526 break;
Aneesh Kumar K.V699cc872013-10-07 22:17:56 +0530527#ifdef CONFIG_KVM_MPIC
528 case KVM_CAP_IRQ_MPIC:
529 r = 1;
530 break;
531#endif
532
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000533#ifdef CONFIG_PPC_BOOK3S_64
David Gibson54738c02011-06-29 00:22:41 +0000534 case KVM_CAP_SPAPR_TCE:
Alexey Kardashevskiy58ded422016-03-01 17:54:40 +1100535 case KVM_CAP_SPAPR_TCE_64:
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000536 case KVM_CAP_PPC_RTAS:
Alexander Graff2e91042014-05-22 17:40:15 +0200537 case KVM_CAP_PPC_FIXUP_HCALL:
Paul Mackerras699a0ea2014-06-02 11:02:59 +1000538 case KVM_CAP_PPC_ENABLE_HCALL:
Paul Mackerras5975a2e2013-04-27 00:28:37 +0000539#ifdef CONFIG_KVM_XICS
540 case KVM_CAP_IRQ_XICS:
541#endif
David Gibson54738c02011-06-29 00:22:41 +0000542 r = 1;
543 break;
David Gibsona8acaec2016-11-23 16:14:07 +1100544
545 case KVM_CAP_PPC_ALLOC_HTAB:
546 r = hv_enabled;
547 break;
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000548#endif /* CONFIG_PPC_BOOK3S_64 */
Aneesh Kumar K.V699cc872013-10-07 22:17:56 +0530549#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
Paul Mackerras371fefd2011-06-29 00:23:08 +0000550 case KVM_CAP_PPC_SMT:
Paul Mackerras45c940b2016-11-18 17:43:30 +1100551 r = 0;
552 if (hv_enabled) {
553 if (cpu_has_feature(CPU_FTR_ARCH_300))
554 r = 1;
555 else
556 r = threads_per_subcore;
557 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000558 break;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000559 case KVM_CAP_PPC_RMA:
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100560 r = 0;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000561 break;
Michael Ellermane928e9c2015-03-20 20:39:41 +1100562 case KVM_CAP_PPC_HWRNG:
563 r = kvmppc_hwrng_present();
564 break;
Paul Mackerrasc9270132017-01-30 21:21:41 +1100565 case KVM_CAP_PPC_MMU_RADIX:
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +1100566 r = !!(hv_enabled && radix_enabled());
Paul Mackerrasc9270132017-01-30 21:21:41 +1100567 break;
568 case KVM_CAP_PPC_MMU_HASH_V3:
Paul Mackerras468808b2017-01-30 21:21:42 +1100569 r = !!(hv_enabled && !radix_enabled() &&
Paul Mackerrasc9270132017-01-30 21:21:41 +1100570 cpu_has_feature(CPU_FTR_ARCH_300));
571 break;
David Gibson54738c02011-06-29 00:22:41 +0000572#endif
Alexander Graff4800b12012-08-07 10:24:14 +0200573 case KVM_CAP_SYNC_MMU:
Aneesh Kumar K.V699cc872013-10-07 22:17:56 +0530574#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100575 r = hv_enabled;
Alexander Graff4800b12012-08-07 10:24:14 +0200576#elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
577 r = 1;
578#else
579 r = 0;
Paul Mackerrasa2932922012-11-19 22:57:20 +0000580#endif
Aneesh Kumar K.V699cc872013-10-07 22:17:56 +0530581 break;
582#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
Paul Mackerrasa2932922012-11-19 22:57:20 +0000583 case KVM_CAP_PPC_HTAB_FD:
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530584 r = hv_enabled;
Paul Mackerrasa2932922012-11-19 22:57:20 +0000585 break;
Alexander Graff4800b12012-08-07 10:24:14 +0200586#endif
Matt Evansb5434032011-12-07 16:55:57 +0000587 case KVM_CAP_NR_VCPUS:
588 /*
589 * Recommending a number of CPUs is somewhat arbitrary; we
590 * return the number of present CPUs for -HV (since a host
591 * will have secondary threads "offline"), and for other KVM
592 * implementations just count online CPUs.
593 */
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530594 if (hv_enabled)
Aneesh Kumar K.V699cc872013-10-07 22:17:56 +0530595 r = num_present_cpus();
596 else
597 r = num_online_cpus();
Matt Evansb5434032011-12-07 16:55:57 +0000598 break;
Nikunj A Dadhaniabfec5c2c2015-10-16 10:27:53 +0530599 case KVM_CAP_NR_MEMSLOTS:
600 r = KVM_USER_MEM_SLOTS;
601 break;
Matt Evansb5434032011-12-07 16:55:57 +0000602 case KVM_CAP_MAX_VCPUS:
603 r = KVM_MAX_VCPUS;
604 break;
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +0000605#ifdef CONFIG_PPC_BOOK3S_64
606 case KVM_CAP_PPC_GET_SMMU_INFO:
607 r = 1;
608 break;
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +1100609 case KVM_CAP_SPAPR_MULTITCE:
610 r = 1;
611 break;
David Gibson050f2332016-12-20 16:49:07 +1100612 case KVM_CAP_SPAPR_RESIZE_HPT:
Paul Mackerrasbcd3bb62017-02-18 08:30:44 +1100613 /* Disable this on POWER9 until code handles new HPTE format */
614 r = !!hv_enabled && !cpu_has_feature(CPU_FTR_ARCH_300);
David Gibson050f2332016-12-20 16:49:07 +1100615 break;
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +0000616#endif
Sam Bobroff23528bb2016-07-20 13:41:36 +1000617 case KVM_CAP_PPC_HTM:
618 r = cpu_has_feature(CPU_FTR_TM_COMP) &&
619 is_kvmppc_hv_enabled(kvm);
620 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500621 default:
622 r = 0;
623 break;
624 }
625 return r;
626
627}
628
629long kvm_arch_dev_ioctl(struct file *filp,
630 unsigned int ioctl, unsigned long arg)
631{
632 return -EINVAL;
633}
634
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530635void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900636 struct kvm_memory_slot *dont)
637{
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530638 kvmppc_core_free_memslot(kvm, free, dont);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900639}
640
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530641int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
642 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900643{
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530644 return kvmppc_core_create_memslot(kvm, slot, npages);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900645}
646
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200647int kvm_arch_prepare_memory_region(struct kvm *kvm,
Takuya Yoshikawa462fce42013-02-27 19:41:56 +0900648 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200649 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +0900650 enum kvm_mr_change change)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500651{
Paul Mackerrasa66b48c2012-09-11 13:27:46 +0000652 return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500653}
654
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200655void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200656 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +0900657 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +0200658 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +0900659 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200660{
Paolo Bonzinif36f3f22015-05-18 13:20:23 +0200661 kvmppc_core_commit_memory_region(kvm, mem, old, new);
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200662}
663
Marcelo Tosatti2df72e92012-08-24 15:54:57 -0300664void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
665 struct kvm_memory_slot *slot)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300666{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +0000667 kvmppc_core_flush_memslot(kvm, slot);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300668}
669
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500670struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
671{
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600672 struct kvm_vcpu *vcpu;
673 vcpu = kvmppc_core_vcpu_create(kvm, id);
Matt Evans03cdab52011-12-06 21:19:42 +0000674 if (!IS_ERR(vcpu)) {
675 vcpu->arch.wqp = &vcpu->wq;
Wei Yongjun06056bf2010-03-09 14:13:43 +0800676 kvmppc_create_vcpu_debugfs(vcpu, id);
Matt Evans03cdab52011-12-06 21:19:42 +0000677 }
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600678 return vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500679}
680
Dominik Dingel31928aa2014-12-04 15:47:07 +0100681void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -0200682{
Marcelo Tosatti42897d82012-11-27 23:29:02 -0200683}
684
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500685void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
686{
Alexander Grafa5954052010-02-22 16:52:14 +0100687 /* Make sure we're not using the vcpu anymore */
688 hrtimer_cancel(&vcpu->arch.dec_timer);
Alexander Grafa5954052010-02-22 16:52:14 +0100689
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600690 kvmppc_remove_vcpu_debugfs(vcpu);
Scott Woodeb1e4f42013-04-12 14:08:47 +0000691
692 switch (vcpu->arch.irq_type) {
693 case KVMPPC_IRQ_MPIC:
694 kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
695 break;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000696 case KVMPPC_IRQ_XICS:
697 kvmppc_xics_free_icp(vcpu);
698 break;
Scott Woodeb1e4f42013-04-12 14:08:47 +0000699 }
700
Hollis Blancharddb93f572008-11-05 09:36:18 -0600701 kvmppc_core_vcpu_free(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500702}
703
704void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
705{
706 kvm_arch_vcpu_free(vcpu);
707}
708
709int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
710{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600711 return kvmppc_core_pending_dec(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500712}
713
Thomas Huth5358a962015-05-22 09:25:02 +0200714static enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
Alexander Graf544c6762009-11-02 12:02:31 +0000715{
716 struct kvm_vcpu *vcpu;
717
718 vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
Mihai Caramand02d4d12014-09-01 17:19:56 +0300719 kvmppc_decrementer_func(vcpu);
Alexander Graf544c6762009-11-02 12:02:31 +0000720
721 return HRTIMER_NORESTART;
722}
723
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500724int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
725{
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000726 int ret;
727
Alexander Graf544c6762009-11-02 12:02:31 +0000728 hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
Alexander Graf544c6762009-11-02 12:02:31 +0000729 vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000730 vcpu->arch.dec_expires = ~(u64)0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500731
Bharat Bhushan09000ad2011-03-25 10:32:13 +0530732#ifdef CONFIG_KVM_EXIT_TIMING
733 mutex_init(&vcpu->arch.exit_timing_lock);
734#endif
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000735 ret = kvmppc_subarch_vcpu_init(vcpu);
736 return ret;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500737}
738
739void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
740{
Hollis Blanchardecc09812009-01-03 16:22:59 -0600741 kvmppc_mmu_destroy(vcpu);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000742 kvmppc_subarch_vcpu_uninit(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500743}
744
745void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
746{
Scott Woodeab17672011-04-27 17:24:10 -0500747#ifdef CONFIG_BOOKE
748 /*
749 * vrsave (formerly usprg0) isn't used by Linux, but may
750 * be used by the guest.
751 *
752 * On non-booke this is associated with Altivec and
753 * is handled by code in book3s.c.
754 */
755 mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
756#endif
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600757 kvmppc_core_vcpu_load(vcpu, cpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500758}
759
760void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
761{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600762 kvmppc_core_vcpu_put(vcpu);
Scott Woodeab17672011-04-27 17:24:10 -0500763#ifdef CONFIG_BOOKE
764 vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
765#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500766}
767
Suresh Warrier95767302016-08-19 15:35:47 +1000768/*
769 * irq_bypass_add_producer and irq_bypass_del_producer are only
770 * useful if the architecture supports PCI passthrough.
771 * irq_bypass_stop and irq_bypass_start are not needed and so
772 * kvm_ops are not defined for them.
773 */
774bool kvm_arch_has_irq_bypass(void)
775{
776 return ((kvmppc_hv_ops && kvmppc_hv_ops->irq_bypass_add_producer) ||
777 (kvmppc_pr_ops && kvmppc_pr_ops->irq_bypass_add_producer));
778}
779
780int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
781 struct irq_bypass_producer *prod)
782{
783 struct kvm_kernel_irqfd *irqfd =
784 container_of(cons, struct kvm_kernel_irqfd, consumer);
785 struct kvm *kvm = irqfd->kvm;
786
787 if (kvm->arch.kvm_ops->irq_bypass_add_producer)
788 return kvm->arch.kvm_ops->irq_bypass_add_producer(cons, prod);
789
790 return 0;
791}
792
793void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
794 struct irq_bypass_producer *prod)
795{
796 struct kvm_kernel_irqfd *irqfd =
797 container_of(cons, struct kvm_kernel_irqfd, consumer);
798 struct kvm *kvm = irqfd->kvm;
799
800 if (kvm->arch.kvm_ops->irq_bypass_del_producer)
801 kvm->arch.kvm_ops->irq_bypass_del_producer(cons, prod);
802}
803
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500804static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
805 struct kvm_run *run)
806{
Denis Kirjanov69b61832010-06-11 11:23:26 +0000807 u64 uninitialized_var(gpr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500808
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100809 if (run->mmio.len > sizeof(gpr)) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500810 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
811 return;
812 }
813
David Gibsond078eed2015-02-03 16:36:24 +1100814 if (!vcpu->arch.mmio_host_swabbed) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500815 switch (run->mmio.len) {
Alexander Grafb104d062010-02-19 11:00:29 +0100816 case 8: gpr = *(u64 *)run->mmio.data; break;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100817 case 4: gpr = *(u32 *)run->mmio.data; break;
818 case 2: gpr = *(u16 *)run->mmio.data; break;
819 case 1: gpr = *(u8 *)run->mmio.data; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500820 }
821 } else {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500822 switch (run->mmio.len) {
David Gibsond078eed2015-02-03 16:36:24 +1100823 case 8: gpr = swab64(*(u64 *)run->mmio.data); break;
824 case 4: gpr = swab32(*(u32 *)run->mmio.data); break;
825 case 2: gpr = swab16(*(u16 *)run->mmio.data); break;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100826 case 1: gpr = *(u8 *)run->mmio.data; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500827 }
828 }
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100829
Alexander Graf3587d532010-02-19 11:00:30 +0100830 if (vcpu->arch.mmio_sign_extend) {
831 switch (run->mmio.len) {
832#ifdef CONFIG_PPC64
833 case 4:
834 gpr = (s64)(s32)gpr;
835 break;
836#endif
837 case 2:
838 gpr = (s64)(s16)gpr;
839 break;
840 case 1:
841 gpr = (s64)(s8)gpr;
842 break;
843 }
844 }
845
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100846 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
Alexander Grafb104d062010-02-19 11:00:29 +0100847
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100848 switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
849 case KVM_MMIO_REG_GPR:
Alexander Grafb104d062010-02-19 11:00:29 +0100850 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
851 break;
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100852 case KVM_MMIO_REG_FPR:
Paul Mackerrasefff1912013-10-15 20:43:02 +1100853 VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
Alexander Grafb104d062010-02-19 11:00:29 +0100854 break;
Alexander Graf287d5612010-04-01 15:33:21 +0200855#ifdef CONFIG_PPC_BOOK3S
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100856 case KVM_MMIO_REG_QPR:
857 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
Alexander Grafb104d062010-02-19 11:00:29 +0100858 break;
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100859 case KVM_MMIO_REG_FQPR:
Paul Mackerrasefff1912013-10-15 20:43:02 +1100860 VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100861 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
Alexander Grafb104d062010-02-19 11:00:29 +0100862 break;
Alexander Graf287d5612010-04-01 15:33:21 +0200863#endif
Alexander Grafb104d062010-02-19 11:00:29 +0100864 default:
865 BUG();
866 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500867}
868
Paul Mackerraseb8b0562016-05-05 16:17:10 +1000869static int __kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
870 unsigned int rt, unsigned int bytes,
871 int is_default_endian, int sign_extend)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500872{
Scott Wooded840ee2013-04-26 14:53:39 +0000873 int idx, ret;
David Gibsond078eed2015-02-03 16:36:24 +1100874 bool host_swabbed;
Cédric Le Goater73601772014-01-09 11:51:16 +0100875
David Gibsond078eed2015-02-03 16:36:24 +1100876 /* Pity C doesn't have a logical XOR operator */
Cédric Le Goater73601772014-01-09 11:51:16 +0100877 if (kvmppc_need_byteswap(vcpu)) {
David Gibsond078eed2015-02-03 16:36:24 +1100878 host_swabbed = is_default_endian;
Cédric Le Goater73601772014-01-09 11:51:16 +0100879 } else {
David Gibsond078eed2015-02-03 16:36:24 +1100880 host_swabbed = !is_default_endian;
Cédric Le Goater73601772014-01-09 11:51:16 +0100881 }
Scott Wooded840ee2013-04-26 14:53:39 +0000882
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500883 if (bytes > sizeof(run->mmio.data)) {
884 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
885 run->mmio.len);
886 }
887
888 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
889 run->mmio.len = bytes;
890 run->mmio.is_write = 0;
891
892 vcpu->arch.io_gpr = rt;
David Gibsond078eed2015-02-03 16:36:24 +1100893 vcpu->arch.mmio_host_swabbed = host_swabbed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500894 vcpu->mmio_needed = 1;
895 vcpu->mmio_is_write = 0;
Paul Mackerraseb8b0562016-05-05 16:17:10 +1000896 vcpu->arch.mmio_sign_extend = sign_extend;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500897
Scott Wooded840ee2013-04-26 14:53:39 +0000898 idx = srcu_read_lock(&vcpu->kvm->srcu);
899
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000900 ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
Scott Wooded840ee2013-04-26 14:53:39 +0000901 bytes, &run->mmio.data);
902
903 srcu_read_unlock(&vcpu->kvm->srcu, idx);
904
905 if (!ret) {
Alexander Graf0e673fb2012-10-09 00:06:20 +0200906 kvmppc_complete_mmio_load(vcpu, run);
907 vcpu->mmio_needed = 0;
908 return EMULATE_DONE;
909 }
910
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500911 return EMULATE_DO_MMIO;
912}
Paul Mackerraseb8b0562016-05-05 16:17:10 +1000913
914int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
915 unsigned int rt, unsigned int bytes,
916 int is_default_endian)
917{
918 return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 0);
919}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +0530920EXPORT_SYMBOL_GPL(kvmppc_handle_load);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500921
Alexander Graf3587d532010-02-19 11:00:30 +0100922/* Same as above, but sign extends */
923int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
Cédric Le Goater73601772014-01-09 11:51:16 +0100924 unsigned int rt, unsigned int bytes,
925 int is_default_endian)
Alexander Graf3587d532010-02-19 11:00:30 +0100926{
Paul Mackerraseb8b0562016-05-05 16:17:10 +1000927 return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 1);
Alexander Graf3587d532010-02-19 11:00:30 +0100928}
929
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500930int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
Cédric Le Goater73601772014-01-09 11:51:16 +0100931 u64 val, unsigned int bytes, int is_default_endian)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500932{
933 void *data = run->mmio.data;
Scott Wooded840ee2013-04-26 14:53:39 +0000934 int idx, ret;
David Gibsond078eed2015-02-03 16:36:24 +1100935 bool host_swabbed;
Cédric Le Goater73601772014-01-09 11:51:16 +0100936
David Gibsond078eed2015-02-03 16:36:24 +1100937 /* Pity C doesn't have a logical XOR operator */
Cédric Le Goater73601772014-01-09 11:51:16 +0100938 if (kvmppc_need_byteswap(vcpu)) {
David Gibsond078eed2015-02-03 16:36:24 +1100939 host_swabbed = is_default_endian;
Cédric Le Goater73601772014-01-09 11:51:16 +0100940 } else {
David Gibsond078eed2015-02-03 16:36:24 +1100941 host_swabbed = !is_default_endian;
Cédric Le Goater73601772014-01-09 11:51:16 +0100942 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500943
944 if (bytes > sizeof(run->mmio.data)) {
945 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
946 run->mmio.len);
947 }
948
949 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
950 run->mmio.len = bytes;
951 run->mmio.is_write = 1;
952 vcpu->mmio_needed = 1;
953 vcpu->mmio_is_write = 1;
954
955 /* Store the value at the lowest bytes in 'data'. */
David Gibsond078eed2015-02-03 16:36:24 +1100956 if (!host_swabbed) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500957 switch (bytes) {
Alexander Grafb104d062010-02-19 11:00:29 +0100958 case 8: *(u64 *)data = val; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500959 case 4: *(u32 *)data = val; break;
960 case 2: *(u16 *)data = val; break;
961 case 1: *(u8 *)data = val; break;
962 }
963 } else {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500964 switch (bytes) {
David Gibsond078eed2015-02-03 16:36:24 +1100965 case 8: *(u64 *)data = swab64(val); break;
966 case 4: *(u32 *)data = swab32(val); break;
967 case 2: *(u16 *)data = swab16(val); break;
968 case 1: *(u8 *)data = val; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500969 }
970 }
971
Scott Wooded840ee2013-04-26 14:53:39 +0000972 idx = srcu_read_lock(&vcpu->kvm->srcu);
973
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000974 ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
Scott Wooded840ee2013-04-26 14:53:39 +0000975 bytes, &run->mmio.data);
976
977 srcu_read_unlock(&vcpu->kvm->srcu, idx);
978
979 if (!ret) {
Alexander Graf0e673fb2012-10-09 00:06:20 +0200980 vcpu->mmio_needed = 0;
981 return EMULATE_DONE;
982 }
983
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500984 return EMULATE_DO_MMIO;
985}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +0530986EXPORT_SYMBOL_GPL(kvmppc_handle_store);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500987
Mihai Caraman8a41ea52014-08-20 16:36:24 +0300988int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
989{
990 int r = 0;
991 union kvmppc_one_reg val;
992 int size;
993
994 size = one_reg_size(reg->id);
995 if (size > sizeof(val))
996 return -EINVAL;
997
998 r = kvmppc_get_one_reg(vcpu, reg->id, &val);
999 if (r == -EINVAL) {
1000 r = 0;
1001 switch (reg->id) {
Mihai Caraman3840edc2014-08-20 16:36:25 +03001002#ifdef CONFIG_ALTIVEC
1003 case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
1004 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1005 r = -ENXIO;
1006 break;
1007 }
Greg Kurzb4d7f162016-01-13 18:28:17 +01001008 val.vval = vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0];
Mihai Caraman3840edc2014-08-20 16:36:25 +03001009 break;
1010 case KVM_REG_PPC_VSCR:
1011 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1012 r = -ENXIO;
1013 break;
1014 }
Greg Kurzb4d7f162016-01-13 18:28:17 +01001015 val = get_reg_val(reg->id, vcpu->arch.vr.vscr.u[3]);
Mihai Caraman3840edc2014-08-20 16:36:25 +03001016 break;
1017 case KVM_REG_PPC_VRSAVE:
Greg Kurzb4d7f162016-01-13 18:28:17 +01001018 val = get_reg_val(reg->id, vcpu->arch.vrsave);
Mihai Caraman3840edc2014-08-20 16:36:25 +03001019 break;
1020#endif /* CONFIG_ALTIVEC */
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001021 default:
1022 r = -EINVAL;
1023 break;
1024 }
1025 }
1026
1027 if (r)
1028 return r;
1029
1030 if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
1031 r = -EFAULT;
1032
1033 return r;
1034}
1035
1036int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1037{
1038 int r;
1039 union kvmppc_one_reg val;
1040 int size;
1041
1042 size = one_reg_size(reg->id);
1043 if (size > sizeof(val))
1044 return -EINVAL;
1045
1046 if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
1047 return -EFAULT;
1048
1049 r = kvmppc_set_one_reg(vcpu, reg->id, &val);
1050 if (r == -EINVAL) {
1051 r = 0;
1052 switch (reg->id) {
Mihai Caraman3840edc2014-08-20 16:36:25 +03001053#ifdef CONFIG_ALTIVEC
1054 case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
1055 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1056 r = -ENXIO;
1057 break;
1058 }
Greg Kurzb4d7f162016-01-13 18:28:17 +01001059 vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0] = val.vval;
Mihai Caraman3840edc2014-08-20 16:36:25 +03001060 break;
1061 case KVM_REG_PPC_VSCR:
1062 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1063 r = -ENXIO;
1064 break;
1065 }
Greg Kurzb4d7f162016-01-13 18:28:17 +01001066 vcpu->arch.vr.vscr.u[3] = set_reg_val(reg->id, val);
Mihai Caraman3840edc2014-08-20 16:36:25 +03001067 break;
1068 case KVM_REG_PPC_VRSAVE:
Greg Kurzb4d7f162016-01-13 18:28:17 +01001069 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1070 r = -ENXIO;
1071 break;
1072 }
1073 vcpu->arch.vrsave = set_reg_val(reg->id, val);
Mihai Caraman3840edc2014-08-20 16:36:25 +03001074 break;
1075#endif /* CONFIG_ALTIVEC */
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001076 default:
1077 r = -EINVAL;
1078 break;
1079 }
1080 }
1081
1082 return r;
1083}
1084
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001085int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
1086{
1087 int r;
1088 sigset_t sigsaved;
1089
1090 if (vcpu->sigset_active)
1091 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1092
1093 if (vcpu->mmio_needed) {
1094 if (!vcpu->mmio_is_write)
1095 kvmppc_complete_mmio_load(vcpu, run);
1096 vcpu->mmio_needed = 0;
Alexander Grafad0a0482010-03-24 21:48:30 +01001097 } else if (vcpu->arch.osi_needed) {
1098 u64 *gprs = run->osi.gprs;
1099 int i;
1100
1101 for (i = 0; i < 32; i++)
1102 kvmppc_set_gpr(vcpu, i, gprs[i]);
1103 vcpu->arch.osi_needed = 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001104 } else if (vcpu->arch.hcall_needed) {
1105 int i;
1106
1107 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
1108 for (i = 0; i < 9; ++i)
1109 kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
1110 vcpu->arch.hcall_needed = 0;
Alexander Graf1c810632013-01-04 18:12:48 +01001111#ifdef CONFIG_BOOKE
1112 } else if (vcpu->arch.epr_needed) {
1113 kvmppc_set_epr(vcpu, run->epr.epr);
1114 vcpu->arch.epr_needed = 0;
1115#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001116 }
1117
Paolo Bonzini460df4c2017-02-08 11:50:15 +01001118 if (run->immediate_exit)
1119 r = -EINTR;
1120 else
1121 r = kvmppc_vcpu_run(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001122
1123 if (vcpu->sigset_active)
1124 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1125
1126 return r;
1127}
1128
1129int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
1130{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001131 if (irq->irq == KVM_INTERRUPT_UNSET) {
Paul Mackerras4fe27d22013-02-14 14:00:25 +00001132 kvmppc_core_dequeue_external(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001133 return 0;
1134 }
Hollis Blanchard45c5eb62008-04-25 17:55:49 -05001135
Paul Mackerras19ccb762011-07-23 17:42:46 +10001136 kvmppc_core_queue_external(vcpu, irq);
Christoffer Dallb6d33832012-03-08 16:44:24 -05001137
Scott Wooddfd4d472011-11-17 12:39:59 +00001138 kvm_vcpu_kick(vcpu);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -05001139
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001140 return 0;
1141}
1142
Alexander Graf71fbfd52010-03-24 21:48:29 +01001143static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1144 struct kvm_enable_cap *cap)
1145{
1146 int r;
1147
1148 if (cap->flags)
1149 return -EINVAL;
1150
1151 switch (cap->cap) {
Alexander Grafad0a0482010-03-24 21:48:30 +01001152 case KVM_CAP_PPC_OSI:
1153 r = 0;
1154 vcpu->arch.osi_enabled = true;
1155 break;
Alexander Graf930b4122011-08-08 17:29:42 +02001156 case KVM_CAP_PPC_PAPR:
1157 r = 0;
1158 vcpu->arch.papr_enabled = true;
1159 break;
Alexander Graf1c810632013-01-04 18:12:48 +01001160 case KVM_CAP_PPC_EPR:
1161 r = 0;
Scott Wood5df554ad2013-04-12 14:08:46 +00001162 if (cap->args[0])
1163 vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
1164 else
1165 vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
Alexander Graf1c810632013-01-04 18:12:48 +01001166 break;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001167#ifdef CONFIG_BOOKE
1168 case KVM_CAP_PPC_BOOKE_WATCHDOG:
1169 r = 0;
1170 vcpu->arch.watchdog_enabled = true;
1171 break;
1172#endif
Alexander Grafbf7ca4b2012-02-15 23:40:00 +00001173#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Scott Wooddc83b8b2011-08-18 15:25:21 -05001174 case KVM_CAP_SW_TLB: {
1175 struct kvm_config_tlb cfg;
1176 void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
1177
1178 r = -EFAULT;
1179 if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
1180 break;
1181
1182 r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
1183 break;
1184 }
1185#endif
Scott Woodeb1e4f42013-04-12 14:08:47 +00001186#ifdef CONFIG_KVM_MPIC
1187 case KVM_CAP_IRQ_MPIC: {
Al Viro70abade2013-08-30 15:04:22 -04001188 struct fd f;
Scott Woodeb1e4f42013-04-12 14:08:47 +00001189 struct kvm_device *dev;
1190
1191 r = -EBADF;
Al Viro70abade2013-08-30 15:04:22 -04001192 f = fdget(cap->args[0]);
1193 if (!f.file)
Scott Woodeb1e4f42013-04-12 14:08:47 +00001194 break;
1195
1196 r = -EPERM;
Al Viro70abade2013-08-30 15:04:22 -04001197 dev = kvm_device_from_filp(f.file);
Scott Woodeb1e4f42013-04-12 14:08:47 +00001198 if (dev)
1199 r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
1200
Al Viro70abade2013-08-30 15:04:22 -04001201 fdput(f);
Scott Woodeb1e4f42013-04-12 14:08:47 +00001202 break;
1203 }
1204#endif
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001205#ifdef CONFIG_KVM_XICS
1206 case KVM_CAP_IRQ_XICS: {
Al Viro70abade2013-08-30 15:04:22 -04001207 struct fd f;
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001208 struct kvm_device *dev;
1209
1210 r = -EBADF;
Al Viro70abade2013-08-30 15:04:22 -04001211 f = fdget(cap->args[0]);
1212 if (!f.file)
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001213 break;
1214
1215 r = -EPERM;
Al Viro70abade2013-08-30 15:04:22 -04001216 dev = kvm_device_from_filp(f.file);
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001217 if (dev)
1218 r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
1219
Al Viro70abade2013-08-30 15:04:22 -04001220 fdput(f);
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001221 break;
1222 }
1223#endif /* CONFIG_KVM_XICS */
Alexander Graf71fbfd52010-03-24 21:48:29 +01001224 default:
1225 r = -EINVAL;
1226 break;
1227 }
1228
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001229 if (!r)
1230 r = kvmppc_sanity_check(vcpu);
1231
Alexander Graf71fbfd52010-03-24 21:48:29 +01001232 return r;
1233}
1234
Paul Mackerras34a75b02016-08-10 11:27:27 +10001235bool kvm_arch_intc_initialized(struct kvm *kvm)
1236{
1237#ifdef CONFIG_KVM_MPIC
1238 if (kvm->arch.mpic)
1239 return true;
1240#endif
1241#ifdef CONFIG_KVM_XICS
1242 if (kvm->arch.xics)
1243 return true;
1244#endif
1245 return false;
1246}
1247
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001248int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1249 struct kvm_mp_state *mp_state)
1250{
1251 return -EINVAL;
1252}
1253
1254int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1255 struct kvm_mp_state *mp_state)
1256{
1257 return -EINVAL;
1258}
1259
1260long kvm_arch_vcpu_ioctl(struct file *filp,
1261 unsigned int ioctl, unsigned long arg)
1262{
1263 struct kvm_vcpu *vcpu = filp->private_data;
1264 void __user *argp = (void __user *)arg;
1265 long r;
1266
Avi Kivity93736622010-05-13 12:35:17 +03001267 switch (ioctl) {
1268 case KVM_INTERRUPT: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001269 struct kvm_interrupt irq;
1270 r = -EFAULT;
1271 if (copy_from_user(&irq, argp, sizeof(irq)))
Avi Kivity93736622010-05-13 12:35:17 +03001272 goto out;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001273 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity93736622010-05-13 12:35:17 +03001274 goto out;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001275 }
Avi Kivity19483d12010-05-13 12:30:43 +03001276
Alexander Graf71fbfd52010-03-24 21:48:29 +01001277 case KVM_ENABLE_CAP:
1278 {
1279 struct kvm_enable_cap cap;
1280 r = -EFAULT;
1281 if (copy_from_user(&cap, argp, sizeof(cap)))
1282 goto out;
1283 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1284 break;
1285 }
Scott Wooddc83b8b2011-08-18 15:25:21 -05001286
Alexander Grafe24ed812011-09-14 10:02:41 +02001287 case KVM_SET_ONE_REG:
1288 case KVM_GET_ONE_REG:
1289 {
1290 struct kvm_one_reg reg;
1291 r = -EFAULT;
1292 if (copy_from_user(&reg, argp, sizeof(reg)))
1293 goto out;
1294 if (ioctl == KVM_SET_ONE_REG)
1295 r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
1296 else
1297 r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
1298 break;
1299 }
1300
Alexander Grafbf7ca4b2012-02-15 23:40:00 +00001301#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Scott Wooddc83b8b2011-08-18 15:25:21 -05001302 case KVM_DIRTY_TLB: {
1303 struct kvm_dirty_tlb dirty;
1304 r = -EFAULT;
1305 if (copy_from_user(&dirty, argp, sizeof(dirty)))
1306 goto out;
1307 r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
1308 break;
1309 }
1310#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001311 default:
1312 r = -EINVAL;
1313 }
1314
1315out:
1316 return r;
1317}
1318
Carsten Otte5b1c1492012-01-04 10:25:23 +01001319int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1320{
1321 return VM_FAULT_SIGBUS;
1322}
1323
Alexander Graf15711e92010-07-29 14:48:08 +02001324static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
1325{
Stuart Yoder784bafa2012-07-03 05:48:51 +00001326 u32 inst_nop = 0x60000000;
1327#ifdef CONFIG_KVM_BOOKE_HV
1328 u32 inst_sc1 = 0x44000022;
Alexander Graf27431032014-04-24 13:39:16 +02001329 pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
1330 pvinfo->hcall[1] = cpu_to_be32(inst_nop);
1331 pvinfo->hcall[2] = cpu_to_be32(inst_nop);
1332 pvinfo->hcall[3] = cpu_to_be32(inst_nop);
Stuart Yoder784bafa2012-07-03 05:48:51 +00001333#else
Alexander Graf15711e92010-07-29 14:48:08 +02001334 u32 inst_lis = 0x3c000000;
1335 u32 inst_ori = 0x60000000;
Alexander Graf15711e92010-07-29 14:48:08 +02001336 u32 inst_sc = 0x44000002;
1337 u32 inst_imm_mask = 0xffff;
1338
1339 /*
1340 * The hypercall to get into KVM from within guest context is as
1341 * follows:
1342 *
1343 * lis r0, r0, KVM_SC_MAGIC_R0@h
1344 * ori r0, KVM_SC_MAGIC_R0@l
1345 * sc
1346 * nop
1347 */
Alexander Graf27431032014-04-24 13:39:16 +02001348 pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
1349 pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
1350 pvinfo->hcall[2] = cpu_to_be32(inst_sc);
1351 pvinfo->hcall[3] = cpu_to_be32(inst_nop);
Stuart Yoder784bafa2012-07-03 05:48:51 +00001352#endif
Alexander Graf15711e92010-07-29 14:48:08 +02001353
Liu Yu-B132019202e072012-07-03 05:48:52 +00001354 pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
1355
Alexander Graf15711e92010-07-29 14:48:08 +02001356 return 0;
1357}
1358
Alexander Graf5efdb4b2013-04-17 00:37:57 +02001359int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
1360 bool line_status)
1361{
1362 if (!irqchip_in_kernel(kvm))
1363 return -ENXIO;
1364
1365 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
1366 irq_event->irq, irq_event->level,
1367 line_status);
1368 return 0;
1369}
1370
Paul Mackerras699a0ea2014-06-02 11:02:59 +10001371
1372static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
1373 struct kvm_enable_cap *cap)
1374{
1375 int r;
1376
1377 if (cap->flags)
1378 return -EINVAL;
1379
1380 switch (cap->cap) {
1381#ifdef CONFIG_KVM_BOOK3S_64_HANDLER
1382 case KVM_CAP_PPC_ENABLE_HCALL: {
1383 unsigned long hcall = cap->args[0];
1384
1385 r = -EINVAL;
1386 if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
1387 cap->args[1] > 1)
1388 break;
Paul Mackerrasae2113a2014-06-02 11:03:00 +10001389 if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
1390 break;
Paul Mackerras699a0ea2014-06-02 11:02:59 +10001391 if (cap->args[1])
1392 set_bit(hcall / 4, kvm->arch.enabled_hcalls);
1393 else
1394 clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
1395 r = 0;
1396 break;
1397 }
1398#endif
1399 default:
1400 r = -EINVAL;
1401 break;
1402 }
1403
1404 return r;
1405}
1406
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001407long kvm_arch_vm_ioctl(struct file *filp,
1408 unsigned int ioctl, unsigned long arg)
1409{
Scott Wood5df554ad2013-04-12 14:08:46 +00001410 struct kvm *kvm __maybe_unused = filp->private_data;
Alexander Graf15711e92010-07-29 14:48:08 +02001411 void __user *argp = (void __user *)arg;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001412 long r;
1413
1414 switch (ioctl) {
Alexander Graf15711e92010-07-29 14:48:08 +02001415 case KVM_PPC_GET_PVINFO: {
1416 struct kvm_ppc_pvinfo pvinfo;
Vasiliy Kulikovd8cdddc2010-10-30 13:04:24 +04001417 memset(&pvinfo, 0, sizeof(pvinfo));
Alexander Graf15711e92010-07-29 14:48:08 +02001418 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
1419 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
1420 r = -EFAULT;
1421 goto out;
1422 }
1423
1424 break;
1425 }
Paul Mackerras699a0ea2014-06-02 11:02:59 +10001426 case KVM_ENABLE_CAP:
1427 {
1428 struct kvm_enable_cap cap;
1429 r = -EFAULT;
1430 if (copy_from_user(&cap, argp, sizeof(cap)))
1431 goto out;
1432 r = kvm_vm_ioctl_enable_cap(kvm, &cap);
1433 break;
1434 }
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +00001435#ifdef CONFIG_PPC_BOOK3S_64
Alexey Kardashevskiy58ded422016-03-01 17:54:40 +11001436 case KVM_CREATE_SPAPR_TCE_64: {
1437 struct kvm_create_spapr_tce_64 create_tce_64;
1438
1439 r = -EFAULT;
1440 if (copy_from_user(&create_tce_64, argp, sizeof(create_tce_64)))
1441 goto out;
1442 if (create_tce_64.flags) {
1443 r = -EINVAL;
1444 goto out;
1445 }
1446 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
1447 goto out;
1448 }
David Gibson54738c02011-06-29 00:22:41 +00001449 case KVM_CREATE_SPAPR_TCE: {
1450 struct kvm_create_spapr_tce create_tce;
Alexey Kardashevskiy58ded422016-03-01 17:54:40 +11001451 struct kvm_create_spapr_tce_64 create_tce_64;
David Gibson54738c02011-06-29 00:22:41 +00001452
1453 r = -EFAULT;
1454 if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
1455 goto out;
Alexey Kardashevskiy58ded422016-03-01 17:54:40 +11001456
1457 create_tce_64.liobn = create_tce.liobn;
1458 create_tce_64.page_shift = IOMMU_PAGE_SHIFT_4K;
1459 create_tce_64.offset = 0;
1460 create_tce_64.size = create_tce.window_size >>
1461 IOMMU_PAGE_SHIFT_4K;
1462 create_tce_64.flags = 0;
1463 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
David Gibson54738c02011-06-29 00:22:41 +00001464 goto out;
1465 }
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001466 case KVM_PPC_GET_SMMU_INFO: {
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001467 struct kvm_ppc_smmu_info info;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301468 struct kvm *kvm = filp->private_data;
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001469
1470 memset(&info, 0, sizeof(info));
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301471 r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001472 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
1473 r = -EFAULT;
1474 break;
1475 }
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001476 case KVM_PPC_RTAS_DEFINE_TOKEN: {
1477 struct kvm *kvm = filp->private_data;
1478
1479 r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
1480 break;
1481 }
Paul Mackerrasc9270132017-01-30 21:21:41 +11001482 case KVM_PPC_CONFIGURE_V3_MMU: {
1483 struct kvm *kvm = filp->private_data;
1484 struct kvm_ppc_mmuv3_cfg cfg;
1485
1486 r = -EINVAL;
1487 if (!kvm->arch.kvm_ops->configure_mmu)
1488 goto out;
1489 r = -EFAULT;
1490 if (copy_from_user(&cfg, argp, sizeof(cfg)))
1491 goto out;
1492 r = kvm->arch.kvm_ops->configure_mmu(kvm, &cfg);
1493 break;
1494 }
1495 case KVM_PPC_GET_RMMU_INFO: {
1496 struct kvm *kvm = filp->private_data;
1497 struct kvm_ppc_rmmu_info info;
1498
1499 r = -EINVAL;
1500 if (!kvm->arch.kvm_ops->get_rmmu_info)
1501 goto out;
1502 r = kvm->arch.kvm_ops->get_rmmu_info(kvm, &info);
1503 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
1504 r = -EFAULT;
1505 break;
1506 }
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301507 default: {
1508 struct kvm *kvm = filp->private_data;
1509 r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
1510 }
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301511#else /* CONFIG_PPC_BOOK3S_64 */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001512 default:
Avi Kivity367e1312009-08-26 14:57:07 +03001513 r = -ENOTTY;
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301514#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001515 }
Alexander Graf15711e92010-07-29 14:48:08 +02001516out:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001517 return r;
1518}
1519
Scott Wood043cc4d2011-12-20 15:34:20 +00001520static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
1521static unsigned long nr_lpids;
1522
1523long kvmppc_alloc_lpid(void)
1524{
1525 long lpid;
1526
1527 do {
1528 lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
1529 if (lpid >= nr_lpids) {
1530 pr_err("%s: No LPIDs free\n", __func__);
1531 return -ENOMEM;
1532 }
1533 } while (test_and_set_bit(lpid, lpid_inuse));
1534
1535 return lpid;
1536}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05301537EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
Scott Wood043cc4d2011-12-20 15:34:20 +00001538
1539void kvmppc_claim_lpid(long lpid)
1540{
1541 set_bit(lpid, lpid_inuse);
1542}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05301543EXPORT_SYMBOL_GPL(kvmppc_claim_lpid);
Scott Wood043cc4d2011-12-20 15:34:20 +00001544
1545void kvmppc_free_lpid(long lpid)
1546{
1547 clear_bit(lpid, lpid_inuse);
1548}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05301549EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
Scott Wood043cc4d2011-12-20 15:34:20 +00001550
1551void kvmppc_init_lpid(unsigned long nr_lpids_param)
1552{
1553 nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
1554 memset(lpid_inuse, 0, sizeof(lpid_inuse));
1555}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05301556EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
Scott Wood043cc4d2011-12-20 15:34:20 +00001557
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001558int kvm_arch_init(void *opaque)
1559{
1560 return 0;
1561}
1562
Paolo Bonzini478d66862014-08-05 11:29:07 +02001563EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);