KVM: PPC: Magic Page Book3s support
[deliverable/linux.git] / arch / powerpc / kvm / powerpc.c
CommitLineData
bbf45ba5
HB
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14 *
15 * Copyright IBM Corp. 2007
16 *
17 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
19 */
20
21#include <linux/errno.h>
22#include <linux/err.h>
23#include <linux/kvm_host.h>
24#include <linux/module.h>
25#include <linux/vmalloc.h>
544c6761 26#include <linux/hrtimer.h>
bbf45ba5 27#include <linux/fs.h>
5a0e3ad6 28#include <linux/slab.h>
bbf45ba5
HB
29#include <asm/cputable.h>
30#include <asm/uaccess.h>
31#include <asm/kvm_ppc.h>
83aae4a8 32#include <asm/tlbflush.h>
73e75b41 33#include "timing.h"
fad7b9b5 34#include "../mm/mmu_decl.h"
bbf45ba5 35
46f43c6e
MT
36#define CREATE_TRACE_POINTS
37#include "trace.h"
38
bbf45ba5
HB
39int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
40{
666e7252
AG
41 return !(v->arch.shared->msr & MSR_WE) ||
42 !!(v->arch.pending_exceptions);
bbf45ba5
HB
43}
44
2a342ed5
AG
45int 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) {
64 case HC_VENDOR_KVM | KVM_HC_FEATURES:
65 r = HC_EV_SUCCESS;
66
67 /* Second return value is in r4 */
68 kvmppc_set_gpr(vcpu, 4, r2);
69 break;
70 default:
71 r = HC_EV_UNIMPLEMENTED;
72 break;
73 }
74
75 return r;
76}
bbf45ba5
HB
77
78int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
79{
80 enum emulation_result er;
81 int r;
82
83 er = kvmppc_emulate_instruction(run, vcpu);
84 switch (er) {
85 case EMULATE_DONE:
86 /* Future optimization: only reload non-volatiles if they were
87 * actually modified. */
88 r = RESUME_GUEST_NV;
89 break;
90 case EMULATE_DO_MMIO:
91 run->exit_reason = KVM_EXIT_MMIO;
92 /* We must reload nonvolatiles because "update" load/store
93 * instructions modify register state. */
94 /* Future optimization: only reload non-volatiles if they were
95 * actually modified. */
96 r = RESUME_HOST_NV;
97 break;
98 case EMULATE_FAIL:
99 /* XXX Deliver Program interrupt to guest. */
100 printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__,
c7f38f46 101 kvmppc_get_last_inst(vcpu));
bbf45ba5
HB
102 r = RESUME_HOST;
103 break;
104 default:
105 BUG();
106 }
107
108 return r;
109}
110
10474ae8 111int kvm_arch_hardware_enable(void *garbage)
bbf45ba5 112{
10474ae8 113 return 0;
bbf45ba5
HB
114}
115
116void kvm_arch_hardware_disable(void *garbage)
117{
118}
119
120int kvm_arch_hardware_setup(void)
121{
122 return 0;
123}
124
125void kvm_arch_hardware_unsetup(void)
126{
127}
128
129void kvm_arch_check_processor_compat(void *rtn)
130{
9dd921cf 131 *(int *)rtn = kvmppc_core_check_processor_compat();
bbf45ba5
HB
132}
133
134struct kvm *kvm_arch_create_vm(void)
135{
136 struct kvm *kvm;
137
138 kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
139 if (!kvm)
140 return ERR_PTR(-ENOMEM);
141
142 return kvm;
143}
144
145static void kvmppc_free_vcpus(struct kvm *kvm)
146{
147 unsigned int i;
988a2cae 148 struct kvm_vcpu *vcpu;
bbf45ba5 149
988a2cae
GN
150 kvm_for_each_vcpu(i, vcpu, kvm)
151 kvm_arch_vcpu_free(vcpu);
152
153 mutex_lock(&kvm->lock);
154 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
155 kvm->vcpus[i] = NULL;
156
157 atomic_set(&kvm->online_vcpus, 0);
158 mutex_unlock(&kvm->lock);
bbf45ba5
HB
159}
160
ad8ba2cd
SY
161void kvm_arch_sync_events(struct kvm *kvm)
162{
163}
164
bbf45ba5
HB
165void kvm_arch_destroy_vm(struct kvm *kvm)
166{
167 kvmppc_free_vcpus(kvm);
168 kvm_free_physmem(kvm);
64749204 169 cleanup_srcu_struct(&kvm->srcu);
bbf45ba5
HB
170 kfree(kvm);
171}
172
173int kvm_dev_ioctl_check_extension(long ext)
174{
175 int r;
176
177 switch (ext) {
e15a1137 178 case KVM_CAP_PPC_SEGSTATE:
c10207fe 179 case KVM_CAP_PPC_PAIRED_SINGLES:
18978768 180 case KVM_CAP_PPC_UNSET_IRQ:
71fbfd5f 181 case KVM_CAP_ENABLE_CAP:
ad0a048b 182 case KVM_CAP_PPC_OSI:
e15a1137
AG
183 r = 1;
184 break;
588968b6
LV
185 case KVM_CAP_COALESCED_MMIO:
186 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
187 break;
bbf45ba5
HB
188 default:
189 r = 0;
190 break;
191 }
192 return r;
193
194}
195
196long kvm_arch_dev_ioctl(struct file *filp,
197 unsigned int ioctl, unsigned long arg)
198{
199 return -EINVAL;
200}
201
f7784b8e
MT
202int kvm_arch_prepare_memory_region(struct kvm *kvm,
203 struct kvm_memory_slot *memslot,
204 struct kvm_memory_slot old,
205 struct kvm_userspace_memory_region *mem,
206 int user_alloc)
bbf45ba5
HB
207{
208 return 0;
209}
210
f7784b8e
MT
211void kvm_arch_commit_memory_region(struct kvm *kvm,
212 struct kvm_userspace_memory_region *mem,
213 struct kvm_memory_slot old,
214 int user_alloc)
215{
216 return;
217}
218
219
34d4cb8f
MT
220void kvm_arch_flush_shadow(struct kvm *kvm)
221{
222}
223
bbf45ba5
HB
224struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
225{
73e75b41
HB
226 struct kvm_vcpu *vcpu;
227 vcpu = kvmppc_core_vcpu_create(kvm, id);
06056bfb
WY
228 if (!IS_ERR(vcpu))
229 kvmppc_create_vcpu_debugfs(vcpu, id);
73e75b41 230 return vcpu;
bbf45ba5
HB
231}
232
233void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
234{
a595405d
AG
235 /* Make sure we're not using the vcpu anymore */
236 hrtimer_cancel(&vcpu->arch.dec_timer);
237 tasklet_kill(&vcpu->arch.tasklet);
238
73e75b41 239 kvmppc_remove_vcpu_debugfs(vcpu);
db93f574 240 kvmppc_core_vcpu_free(vcpu);
bbf45ba5
HB
241}
242
243void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
244{
245 kvm_arch_vcpu_free(vcpu);
246}
247
248int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
249{
9dd921cf 250 return kvmppc_core_pending_dec(vcpu);
bbf45ba5
HB
251}
252
253static void kvmppc_decrementer_func(unsigned long data)
254{
255 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
256
9dd921cf 257 kvmppc_core_queue_dec(vcpu);
45c5eb67
HB
258
259 if (waitqueue_active(&vcpu->wq)) {
260 wake_up_interruptible(&vcpu->wq);
261 vcpu->stat.halt_wakeup++;
262 }
bbf45ba5
HB
263}
264
544c6761
AG
265/*
266 * low level hrtimer wake routine. Because this runs in hardirq context
267 * we schedule a tasklet to do the real work.
268 */
269enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
270{
271 struct kvm_vcpu *vcpu;
272
273 vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
274 tasklet_schedule(&vcpu->arch.tasklet);
275
276 return HRTIMER_NORESTART;
277}
278
bbf45ba5
HB
279int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
280{
544c6761
AG
281 hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
282 tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
283 vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
bbf45ba5
HB
284
285 return 0;
286}
287
288void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
289{
ecc0981f 290 kvmppc_mmu_destroy(vcpu);
bbf45ba5
HB
291}
292
293void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
294{
9dd921cf 295 kvmppc_core_vcpu_load(vcpu, cpu);
bbf45ba5
HB
296}
297
298void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
299{
9dd921cf 300 kvmppc_core_vcpu_put(vcpu);
bbf45ba5
HB
301}
302
d0bfb940 303int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
f5d0906b 304 struct kvm_guest_debug *dbg)
bbf45ba5 305{
f5d0906b 306 return -EINVAL;
bbf45ba5
HB
307}
308
309static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
310 struct kvm_run *run)
311{
8e5b26b5 312 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
bbf45ba5
HB
313}
314
315static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
316 struct kvm_run *run)
317{
69b61833 318 u64 uninitialized_var(gpr);
bbf45ba5 319
8e5b26b5 320 if (run->mmio.len > sizeof(gpr)) {
bbf45ba5
HB
321 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
322 return;
323 }
324
325 if (vcpu->arch.mmio_is_bigendian) {
326 switch (run->mmio.len) {
b104d066 327 case 8: gpr = *(u64 *)run->mmio.data; break;
8e5b26b5
AG
328 case 4: gpr = *(u32 *)run->mmio.data; break;
329 case 2: gpr = *(u16 *)run->mmio.data; break;
330 case 1: gpr = *(u8 *)run->mmio.data; break;
bbf45ba5
HB
331 }
332 } else {
333 /* Convert BE data from userland back to LE. */
334 switch (run->mmio.len) {
8e5b26b5
AG
335 case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
336 case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
337 case 1: gpr = *(u8 *)run->mmio.data; break;
bbf45ba5
HB
338 }
339 }
8e5b26b5 340
3587d534
AG
341 if (vcpu->arch.mmio_sign_extend) {
342 switch (run->mmio.len) {
343#ifdef CONFIG_PPC64
344 case 4:
345 gpr = (s64)(s32)gpr;
346 break;
347#endif
348 case 2:
349 gpr = (s64)(s16)gpr;
350 break;
351 case 1:
352 gpr = (s64)(s8)gpr;
353 break;
354 }
355 }
356
8e5b26b5 357 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
b104d066
AG
358
359 switch (vcpu->arch.io_gpr & KVM_REG_EXT_MASK) {
360 case KVM_REG_GPR:
361 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
362 break;
363 case KVM_REG_FPR:
364 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
365 break;
287d5611 366#ifdef CONFIG_PPC_BOOK3S
b104d066
AG
367 case KVM_REG_QPR:
368 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
369 break;
370 case KVM_REG_FQPR:
371 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
372 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
373 break;
287d5611 374#endif
b104d066
AG
375 default:
376 BUG();
377 }
bbf45ba5
HB
378}
379
380int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
381 unsigned int rt, unsigned int bytes, int is_bigendian)
382{
383 if (bytes > sizeof(run->mmio.data)) {
384 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
385 run->mmio.len);
386 }
387
388 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
389 run->mmio.len = bytes;
390 run->mmio.is_write = 0;
391
392 vcpu->arch.io_gpr = rt;
393 vcpu->arch.mmio_is_bigendian = is_bigendian;
394 vcpu->mmio_needed = 1;
395 vcpu->mmio_is_write = 0;
3587d534 396 vcpu->arch.mmio_sign_extend = 0;
bbf45ba5
HB
397
398 return EMULATE_DO_MMIO;
399}
400
3587d534
AG
401/* Same as above, but sign extends */
402int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
403 unsigned int rt, unsigned int bytes, int is_bigendian)
404{
405 int r;
406
407 r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
408 vcpu->arch.mmio_sign_extend = 1;
409
410 return r;
411}
412
bbf45ba5 413int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
b104d066 414 u64 val, unsigned int bytes, int is_bigendian)
bbf45ba5
HB
415{
416 void *data = run->mmio.data;
417
418 if (bytes > sizeof(run->mmio.data)) {
419 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
420 run->mmio.len);
421 }
422
423 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
424 run->mmio.len = bytes;
425 run->mmio.is_write = 1;
426 vcpu->mmio_needed = 1;
427 vcpu->mmio_is_write = 1;
428
429 /* Store the value at the lowest bytes in 'data'. */
430 if (is_bigendian) {
431 switch (bytes) {
b104d066 432 case 8: *(u64 *)data = val; break;
bbf45ba5
HB
433 case 4: *(u32 *)data = val; break;
434 case 2: *(u16 *)data = val; break;
435 case 1: *(u8 *)data = val; break;
436 }
437 } else {
438 /* Store LE value into 'data'. */
439 switch (bytes) {
440 case 4: st_le32(data, val); break;
441 case 2: st_le16(data, val); break;
442 case 1: *(u8 *)data = val; break;
443 }
444 }
445
446 return EMULATE_DO_MMIO;
447}
448
449int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
450{
451 int r;
452 sigset_t sigsaved;
453
454 if (vcpu->sigset_active)
455 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
456
457 if (vcpu->mmio_needed) {
458 if (!vcpu->mmio_is_write)
459 kvmppc_complete_mmio_load(vcpu, run);
460 vcpu->mmio_needed = 0;
461 } else if (vcpu->arch.dcr_needed) {
462 if (!vcpu->arch.dcr_is_write)
463 kvmppc_complete_dcr_load(vcpu, run);
464 vcpu->arch.dcr_needed = 0;
ad0a048b
AG
465 } else if (vcpu->arch.osi_needed) {
466 u64 *gprs = run->osi.gprs;
467 int i;
468
469 for (i = 0; i < 32; i++)
470 kvmppc_set_gpr(vcpu, i, gprs[i]);
471 vcpu->arch.osi_needed = 0;
bbf45ba5
HB
472 }
473
9dd921cf 474 kvmppc_core_deliver_interrupts(vcpu);
bbf45ba5
HB
475
476 local_irq_disable();
477 kvm_guest_enter();
478 r = __kvmppc_vcpu_run(run, vcpu);
479 kvm_guest_exit();
480 local_irq_enable();
481
482 if (vcpu->sigset_active)
483 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
484
485 return r;
486}
487
488int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
489{
18978768
AG
490 if (irq->irq == KVM_INTERRUPT_UNSET)
491 kvmppc_core_dequeue_external(vcpu, irq);
492 else
493 kvmppc_core_queue_external(vcpu, irq);
45c5eb67
HB
494
495 if (waitqueue_active(&vcpu->wq)) {
496 wake_up_interruptible(&vcpu->wq);
497 vcpu->stat.halt_wakeup++;
498 }
499
bbf45ba5
HB
500 return 0;
501}
502
71fbfd5f
AG
503static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
504 struct kvm_enable_cap *cap)
505{
506 int r;
507
508 if (cap->flags)
509 return -EINVAL;
510
511 switch (cap->cap) {
ad0a048b
AG
512 case KVM_CAP_PPC_OSI:
513 r = 0;
514 vcpu->arch.osi_enabled = true;
515 break;
71fbfd5f
AG
516 default:
517 r = -EINVAL;
518 break;
519 }
520
521 return r;
522}
523
bbf45ba5
HB
524int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
525 struct kvm_mp_state *mp_state)
526{
527 return -EINVAL;
528}
529
530int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
531 struct kvm_mp_state *mp_state)
532{
533 return -EINVAL;
534}
535
536long kvm_arch_vcpu_ioctl(struct file *filp,
537 unsigned int ioctl, unsigned long arg)
538{
539 struct kvm_vcpu *vcpu = filp->private_data;
540 void __user *argp = (void __user *)arg;
541 long r;
542
93736624
AK
543 switch (ioctl) {
544 case KVM_INTERRUPT: {
bbf45ba5
HB
545 struct kvm_interrupt irq;
546 r = -EFAULT;
547 if (copy_from_user(&irq, argp, sizeof(irq)))
93736624 548 goto out;
bbf45ba5 549 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
93736624 550 goto out;
bbf45ba5 551 }
19483d14 552
71fbfd5f
AG
553 case KVM_ENABLE_CAP:
554 {
555 struct kvm_enable_cap cap;
556 r = -EFAULT;
557 if (copy_from_user(&cap, argp, sizeof(cap)))
558 goto out;
559 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
560 break;
561 }
bbf45ba5
HB
562 default:
563 r = -EINVAL;
564 }
565
566out:
567 return r;
568}
569
bbf45ba5
HB
570long kvm_arch_vm_ioctl(struct file *filp,
571 unsigned int ioctl, unsigned long arg)
572{
573 long r;
574
575 switch (ioctl) {
576 default:
367e1319 577 r = -ENOTTY;
bbf45ba5
HB
578 }
579
580 return r;
581}
582
583int kvm_arch_init(void *opaque)
584{
585 return 0;
586}
587
588void kvm_arch_exit(void)
589{
590}
This page took 0.363936 seconds and 5 git commands to generate.