blob: 6a53a3f86dae1b65fabb393cbf578397d189bfee [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>
24#include <linux/module.h>
25#include <linux/vmalloc.h>
Alexander Graf544c6762009-11-02 12:02:31 +000026#include <linux/hrtimer.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>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050029#include <asm/cputable.h>
30#include <asm/uaccess.h>
31#include <asm/kvm_ppc.h>
Hollis Blanchard83aae4a2008-07-25 13:54:52 -050032#include <asm/tlbflush.h>
Hollis Blanchard73e75b42008-12-02 15:51:57 -060033#include "timing.h"
Paul Mackerrasfad7b9b2008-12-23 14:57:26 +110034#include "../mm/mmu_decl.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050035
Marcelo Tosatti46f43c62009-06-18 11:47:27 -030036#define CREATE_TRACE_POINTS
37#include "trace.h"
38
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050039int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
40{
Alexander Graf666e7252010-07-29 14:47:43 +020041 return !(v->arch.shared->msr & MSR_WE) ||
42 !!(v->arch.pending_exceptions);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050043}
44
Alexander Graf2a342ed2010-07-29 14:47:48 +020045int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
46{
47 int nr = kvmppc_get_gpr(vcpu, 11);
48 int r;
49 unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
50 unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
51 unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
52 unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
53 unsigned long r2 = 0;
54
55 if (!(vcpu->arch.shared->msr & MSR_SF)) {
56 /* 32 bit mode */
57 param1 &= 0xffffffff;
58 param2 &= 0xffffffff;
59 param3 &= 0xffffffff;
60 param4 &= 0xffffffff;
61 }
62
63 switch (nr) {
Alexander Graf5fc87402010-07-29 14:47:55 +020064 case HC_VENDOR_KVM | KVM_HC_PPC_MAP_MAGIC_PAGE:
65 {
66 vcpu->arch.magic_page_pa = param1;
67 vcpu->arch.magic_page_ea = param2;
68
69 r = HC_EV_SUCCESS;
70 break;
71 }
Alexander Graf2a342ed2010-07-29 14:47:48 +020072 case HC_VENDOR_KVM | KVM_HC_FEATURES:
73 r = HC_EV_SUCCESS;
Alexander Graf5fc87402010-07-29 14:47:55 +020074#if defined(CONFIG_PPC_BOOK3S) /* XXX Missing magic page on BookE */
75 r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
76#endif
Alexander Graf2a342ed2010-07-29 14:47:48 +020077
78 /* Second return value is in r4 */
79 kvmppc_set_gpr(vcpu, 4, r2);
80 break;
81 default:
82 r = HC_EV_UNIMPLEMENTED;
83 break;
84 }
85
86 return r;
87}
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050088
89int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
90{
91 enum emulation_result er;
92 int r;
93
94 er = kvmppc_emulate_instruction(run, vcpu);
95 switch (er) {
96 case EMULATE_DONE:
97 /* Future optimization: only reload non-volatiles if they were
98 * actually modified. */
99 r = RESUME_GUEST_NV;
100 break;
101 case EMULATE_DO_MMIO:
102 run->exit_reason = KVM_EXIT_MMIO;
103 /* We must reload nonvolatiles because "update" load/store
104 * instructions modify register state. */
105 /* Future optimization: only reload non-volatiles if they were
106 * actually modified. */
107 r = RESUME_HOST_NV;
108 break;
109 case EMULATE_FAIL:
110 /* XXX Deliver Program interrupt to guest. */
111 printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__,
Alexander Grafc7f38f42010-04-16 00:11:40 +0200112 kvmppc_get_last_inst(vcpu));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500113 r = RESUME_HOST;
114 break;
115 default:
116 BUG();
117 }
118
119 return r;
120}
121
Alexander Graf10474ae2009-09-15 11:37:46 +0200122int kvm_arch_hardware_enable(void *garbage)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500123{
Alexander Graf10474ae2009-09-15 11:37:46 +0200124 return 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500125}
126
127void kvm_arch_hardware_disable(void *garbage)
128{
129}
130
131int kvm_arch_hardware_setup(void)
132{
133 return 0;
134}
135
136void kvm_arch_hardware_unsetup(void)
137{
138}
139
140void kvm_arch_check_processor_compat(void *rtn)
141{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600142 *(int *)rtn = kvmppc_core_check_processor_compat();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500143}
144
145struct kvm *kvm_arch_create_vm(void)
146{
147 struct kvm *kvm;
148
149 kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
150 if (!kvm)
151 return ERR_PTR(-ENOMEM);
152
153 return kvm;
154}
155
156static void kvmppc_free_vcpus(struct kvm *kvm)
157{
158 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300159 struct kvm_vcpu *vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500160
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300161 kvm_for_each_vcpu(i, vcpu, kvm)
162 kvm_arch_vcpu_free(vcpu);
163
164 mutex_lock(&kvm->lock);
165 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
166 kvm->vcpus[i] = NULL;
167
168 atomic_set(&kvm->online_vcpus, 0);
169 mutex_unlock(&kvm->lock);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500170}
171
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800172void kvm_arch_sync_events(struct kvm *kvm)
173{
174}
175
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500176void kvm_arch_destroy_vm(struct kvm *kvm)
177{
178 kvmppc_free_vcpus(kvm);
179 kvm_free_physmem(kvm);
Marcelo Tosatti64749202010-01-19 12:45:23 -0200180 cleanup_srcu_struct(&kvm->srcu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500181 kfree(kvm);
182}
183
184int kvm_dev_ioctl_check_extension(long ext)
185{
186 int r;
187
188 switch (ext) {
Alexander Grafe15a1132009-11-30 03:02:02 +0000189 case KVM_CAP_PPC_SEGSTATE:
Alexander Grafc10207f2010-02-19 11:00:45 +0100190 case KVM_CAP_PPC_PAIRED_SINGLES:
Alexander Graf18978762010-03-24 21:48:18 +0100191 case KVM_CAP_PPC_UNSET_IRQ:
Alexander Graf71fbfd52010-03-24 21:48:29 +0100192 case KVM_CAP_ENABLE_CAP:
Alexander Grafad0a0482010-03-24 21:48:30 +0100193 case KVM_CAP_PPC_OSI:
Alexander Graf15711e92010-07-29 14:48:08 +0200194 case KVM_CAP_PPC_GET_PVINFO:
Alexander Grafe15a1132009-11-30 03:02:02 +0000195 r = 1;
196 break;
Laurent Vivier588968b2008-05-30 16:05:56 +0200197 case KVM_CAP_COALESCED_MMIO:
198 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
199 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500200 default:
201 r = 0;
202 break;
203 }
204 return r;
205
206}
207
208long kvm_arch_dev_ioctl(struct file *filp,
209 unsigned int ioctl, unsigned long arg)
210{
211 return -EINVAL;
212}
213
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200214int kvm_arch_prepare_memory_region(struct kvm *kvm,
215 struct kvm_memory_slot *memslot,
216 struct kvm_memory_slot old,
217 struct kvm_userspace_memory_region *mem,
218 int user_alloc)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500219{
220 return 0;
221}
222
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200223void kvm_arch_commit_memory_region(struct kvm *kvm,
224 struct kvm_userspace_memory_region *mem,
225 struct kvm_memory_slot old,
226 int user_alloc)
227{
228 return;
229}
230
231
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300232void kvm_arch_flush_shadow(struct kvm *kvm)
233{
234}
235
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500236struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
237{
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600238 struct kvm_vcpu *vcpu;
239 vcpu = kvmppc_core_vcpu_create(kvm, id);
Wei Yongjun06056bf2010-03-09 14:13:43 +0800240 if (!IS_ERR(vcpu))
241 kvmppc_create_vcpu_debugfs(vcpu, id);
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600242 return vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500243}
244
245void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
246{
Alexander Grafa5954052010-02-22 16:52:14 +0100247 /* Make sure we're not using the vcpu anymore */
248 hrtimer_cancel(&vcpu->arch.dec_timer);
249 tasklet_kill(&vcpu->arch.tasklet);
250
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600251 kvmppc_remove_vcpu_debugfs(vcpu);
Hollis Blancharddb93f572008-11-05 09:36:18 -0600252 kvmppc_core_vcpu_free(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500253}
254
255void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
256{
257 kvm_arch_vcpu_free(vcpu);
258}
259
260int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
261{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600262 return kvmppc_core_pending_dec(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500263}
264
265static void kvmppc_decrementer_func(unsigned long data)
266{
267 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
268
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600269 kvmppc_core_queue_dec(vcpu);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500270
271 if (waitqueue_active(&vcpu->wq)) {
272 wake_up_interruptible(&vcpu->wq);
273 vcpu->stat.halt_wakeup++;
274 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500275}
276
Alexander Graf544c6762009-11-02 12:02:31 +0000277/*
278 * low level hrtimer wake routine. Because this runs in hardirq context
279 * we schedule a tasklet to do the real work.
280 */
281enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
282{
283 struct kvm_vcpu *vcpu;
284
285 vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
286 tasklet_schedule(&vcpu->arch.tasklet);
287
288 return HRTIMER_NORESTART;
289}
290
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500291int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
292{
Alexander Graf544c6762009-11-02 12:02:31 +0000293 hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
294 tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
295 vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500296
297 return 0;
298}
299
300void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
301{
Hollis Blanchardecc09812009-01-03 16:22:59 -0600302 kvmppc_mmu_destroy(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500303}
304
305void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
306{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600307 kvmppc_core_vcpu_load(vcpu, cpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500308}
309
310void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
311{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600312 kvmppc_core_vcpu_put(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500313}
314
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100315int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
Hollis Blanchardf5d09062009-01-04 13:51:09 -0600316 struct kvm_guest_debug *dbg)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500317{
Hollis Blanchardf5d09062009-01-04 13:51:09 -0600318 return -EINVAL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500319}
320
321static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
322 struct kvm_run *run)
323{
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100324 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500325}
326
327static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
328 struct kvm_run *run)
329{
Denis Kirjanov69b61832010-06-11 11:23:26 +0000330 u64 uninitialized_var(gpr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500331
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100332 if (run->mmio.len > sizeof(gpr)) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500333 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
334 return;
335 }
336
337 if (vcpu->arch.mmio_is_bigendian) {
338 switch (run->mmio.len) {
Alexander Grafb104d062010-02-19 11:00:29 +0100339 case 8: gpr = *(u64 *)run->mmio.data; break;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100340 case 4: gpr = *(u32 *)run->mmio.data; break;
341 case 2: gpr = *(u16 *)run->mmio.data; break;
342 case 1: gpr = *(u8 *)run->mmio.data; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500343 }
344 } else {
345 /* Convert BE data from userland back to LE. */
346 switch (run->mmio.len) {
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100347 case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
348 case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
349 case 1: gpr = *(u8 *)run->mmio.data; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500350 }
351 }
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100352
Alexander Graf3587d532010-02-19 11:00:30 +0100353 if (vcpu->arch.mmio_sign_extend) {
354 switch (run->mmio.len) {
355#ifdef CONFIG_PPC64
356 case 4:
357 gpr = (s64)(s32)gpr;
358 break;
359#endif
360 case 2:
361 gpr = (s64)(s16)gpr;
362 break;
363 case 1:
364 gpr = (s64)(s8)gpr;
365 break;
366 }
367 }
368
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100369 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
Alexander Grafb104d062010-02-19 11:00:29 +0100370
371 switch (vcpu->arch.io_gpr & KVM_REG_EXT_MASK) {
372 case KVM_REG_GPR:
373 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
374 break;
375 case KVM_REG_FPR:
376 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
377 break;
Alexander Graf287d5612010-04-01 15:33:21 +0200378#ifdef CONFIG_PPC_BOOK3S
Alexander Grafb104d062010-02-19 11:00:29 +0100379 case KVM_REG_QPR:
380 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
381 break;
382 case KVM_REG_FQPR:
383 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
384 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
385 break;
Alexander Graf287d5612010-04-01 15:33:21 +0200386#endif
Alexander Grafb104d062010-02-19 11:00:29 +0100387 default:
388 BUG();
389 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500390}
391
392int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
393 unsigned int rt, unsigned int bytes, int is_bigendian)
394{
395 if (bytes > sizeof(run->mmio.data)) {
396 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
397 run->mmio.len);
398 }
399
400 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
401 run->mmio.len = bytes;
402 run->mmio.is_write = 0;
403
404 vcpu->arch.io_gpr = rt;
405 vcpu->arch.mmio_is_bigendian = is_bigendian;
406 vcpu->mmio_needed = 1;
407 vcpu->mmio_is_write = 0;
Alexander Graf3587d532010-02-19 11:00:30 +0100408 vcpu->arch.mmio_sign_extend = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500409
410 return EMULATE_DO_MMIO;
411}
412
Alexander Graf3587d532010-02-19 11:00:30 +0100413/* Same as above, but sign extends */
414int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
415 unsigned int rt, unsigned int bytes, int is_bigendian)
416{
417 int r;
418
419 r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
420 vcpu->arch.mmio_sign_extend = 1;
421
422 return r;
423}
424
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500425int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
Alexander Grafb104d062010-02-19 11:00:29 +0100426 u64 val, unsigned int bytes, int is_bigendian)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500427{
428 void *data = run->mmio.data;
429
430 if (bytes > sizeof(run->mmio.data)) {
431 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
432 run->mmio.len);
433 }
434
435 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
436 run->mmio.len = bytes;
437 run->mmio.is_write = 1;
438 vcpu->mmio_needed = 1;
439 vcpu->mmio_is_write = 1;
440
441 /* Store the value at the lowest bytes in 'data'. */
442 if (is_bigendian) {
443 switch (bytes) {
Alexander Grafb104d062010-02-19 11:00:29 +0100444 case 8: *(u64 *)data = val; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500445 case 4: *(u32 *)data = val; break;
446 case 2: *(u16 *)data = val; break;
447 case 1: *(u8 *)data = val; break;
448 }
449 } else {
450 /* Store LE value into 'data'. */
451 switch (bytes) {
452 case 4: st_le32(data, val); break;
453 case 2: st_le16(data, val); break;
454 case 1: *(u8 *)data = val; break;
455 }
456 }
457
458 return EMULATE_DO_MMIO;
459}
460
461int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
462{
463 int r;
464 sigset_t sigsaved;
465
466 if (vcpu->sigset_active)
467 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
468
469 if (vcpu->mmio_needed) {
470 if (!vcpu->mmio_is_write)
471 kvmppc_complete_mmio_load(vcpu, run);
472 vcpu->mmio_needed = 0;
473 } else if (vcpu->arch.dcr_needed) {
474 if (!vcpu->arch.dcr_is_write)
475 kvmppc_complete_dcr_load(vcpu, run);
476 vcpu->arch.dcr_needed = 0;
Alexander Grafad0a0482010-03-24 21:48:30 +0100477 } else if (vcpu->arch.osi_needed) {
478 u64 *gprs = run->osi.gprs;
479 int i;
480
481 for (i = 0; i < 32; i++)
482 kvmppc_set_gpr(vcpu, i, gprs[i]);
483 vcpu->arch.osi_needed = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500484 }
485
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600486 kvmppc_core_deliver_interrupts(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500487
488 local_irq_disable();
489 kvm_guest_enter();
490 r = __kvmppc_vcpu_run(run, vcpu);
491 kvm_guest_exit();
492 local_irq_enable();
493
494 if (vcpu->sigset_active)
495 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
496
497 return r;
498}
499
500int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
501{
Alexander Graf18978762010-03-24 21:48:18 +0100502 if (irq->irq == KVM_INTERRUPT_UNSET)
503 kvmppc_core_dequeue_external(vcpu, irq);
504 else
505 kvmppc_core_queue_external(vcpu, irq);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500506
507 if (waitqueue_active(&vcpu->wq)) {
508 wake_up_interruptible(&vcpu->wq);
509 vcpu->stat.halt_wakeup++;
510 }
511
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500512 return 0;
513}
514
Alexander Graf71fbfd52010-03-24 21:48:29 +0100515static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
516 struct kvm_enable_cap *cap)
517{
518 int r;
519
520 if (cap->flags)
521 return -EINVAL;
522
523 switch (cap->cap) {
Alexander Grafad0a0482010-03-24 21:48:30 +0100524 case KVM_CAP_PPC_OSI:
525 r = 0;
526 vcpu->arch.osi_enabled = true;
527 break;
Alexander Graf71fbfd52010-03-24 21:48:29 +0100528 default:
529 r = -EINVAL;
530 break;
531 }
532
533 return r;
534}
535
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500536int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
537 struct kvm_mp_state *mp_state)
538{
539 return -EINVAL;
540}
541
542int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
543 struct kvm_mp_state *mp_state)
544{
545 return -EINVAL;
546}
547
548long kvm_arch_vcpu_ioctl(struct file *filp,
549 unsigned int ioctl, unsigned long arg)
550{
551 struct kvm_vcpu *vcpu = filp->private_data;
552 void __user *argp = (void __user *)arg;
553 long r;
554
Avi Kivity93736622010-05-13 12:35:17 +0300555 switch (ioctl) {
556 case KVM_INTERRUPT: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500557 struct kvm_interrupt irq;
558 r = -EFAULT;
559 if (copy_from_user(&irq, argp, sizeof(irq)))
Avi Kivity93736622010-05-13 12:35:17 +0300560 goto out;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500561 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity93736622010-05-13 12:35:17 +0300562 goto out;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500563 }
Avi Kivity19483d12010-05-13 12:30:43 +0300564
Alexander Graf71fbfd52010-03-24 21:48:29 +0100565 case KVM_ENABLE_CAP:
566 {
567 struct kvm_enable_cap cap;
568 r = -EFAULT;
569 if (copy_from_user(&cap, argp, sizeof(cap)))
570 goto out;
571 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
572 break;
573 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500574 default:
575 r = -EINVAL;
576 }
577
578out:
579 return r;
580}
581
Alexander Graf15711e92010-07-29 14:48:08 +0200582static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
583{
584 u32 inst_lis = 0x3c000000;
585 u32 inst_ori = 0x60000000;
586 u32 inst_nop = 0x60000000;
587 u32 inst_sc = 0x44000002;
588 u32 inst_imm_mask = 0xffff;
589
590 /*
591 * The hypercall to get into KVM from within guest context is as
592 * follows:
593 *
594 * lis r0, r0, KVM_SC_MAGIC_R0@h
595 * ori r0, KVM_SC_MAGIC_R0@l
596 * sc
597 * nop
598 */
599 pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask);
600 pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask);
601 pvinfo->hcall[2] = inst_sc;
602 pvinfo->hcall[3] = inst_nop;
603
604 return 0;
605}
606
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500607long kvm_arch_vm_ioctl(struct file *filp,
608 unsigned int ioctl, unsigned long arg)
609{
Alexander Graf15711e92010-07-29 14:48:08 +0200610 void __user *argp = (void __user *)arg;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500611 long r;
612
613 switch (ioctl) {
Alexander Graf15711e92010-07-29 14:48:08 +0200614 case KVM_PPC_GET_PVINFO: {
615 struct kvm_ppc_pvinfo pvinfo;
616 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
617 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
618 r = -EFAULT;
619 goto out;
620 }
621
622 break;
623 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500624 default:
Avi Kivity367e1312009-08-26 14:57:07 +0300625 r = -ENOTTY;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500626 }
627
Alexander Graf15711e92010-07-29 14:48:08 +0200628out:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500629 return r;
630}
631
632int kvm_arch_init(void *opaque)
633{
634 return 0;
635}
636
637void kvm_arch_exit(void)
638{
639}