Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux...
[linux-2.6] / arch / powerpc / kvm / powerpc.c
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>
26 #include <linux/fs.h>
27 #include <asm/cputable.h>
28 #include <asm/uaccess.h>
29 #include <asm/kvm_ppc.h>
30 #include <asm/tlbflush.h>
31 #include "../mm/mmu_decl.h"
32
33
34 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
35 {
36         return gfn;
37 }
38
39 int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
40 {
41         return !!(v->arch.pending_exceptions);
42 }
43
44 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
45 {
46         return !(v->arch.msr & MSR_WE);
47 }
48
49
50 int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
51 {
52         enum emulation_result er;
53         int r;
54
55         er = kvmppc_emulate_instruction(run, vcpu);
56         switch (er) {
57         case EMULATE_DONE:
58                 /* Future optimization: only reload non-volatiles if they were
59                  * actually modified. */
60                 r = RESUME_GUEST_NV;
61                 break;
62         case EMULATE_DO_MMIO:
63                 run->exit_reason = KVM_EXIT_MMIO;
64                 /* We must reload nonvolatiles because "update" load/store
65                  * instructions modify register state. */
66                 /* Future optimization: only reload non-volatiles if they were
67                  * actually modified. */
68                 r = RESUME_HOST_NV;
69                 break;
70         case EMULATE_FAIL:
71                 /* XXX Deliver Program interrupt to guest. */
72                 printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__,
73                        vcpu->arch.last_inst);
74                 r = RESUME_HOST;
75                 break;
76         default:
77                 BUG();
78         }
79
80         return r;
81 }
82
83 void kvm_arch_hardware_enable(void *garbage)
84 {
85 }
86
87 void kvm_arch_hardware_disable(void *garbage)
88 {
89 }
90
91 int kvm_arch_hardware_setup(void)
92 {
93         return 0;
94 }
95
96 void kvm_arch_hardware_unsetup(void)
97 {
98 }
99
100 void kvm_arch_check_processor_compat(void *rtn)
101 {
102         int r;
103
104         if (strcmp(cur_cpu_spec->platform, "ppc440") == 0)
105                 r = 0;
106         else
107                 r = -ENOTSUPP;
108
109         *(int *)rtn = r;
110 }
111
112 struct kvm *kvm_arch_create_vm(void)
113 {
114         struct kvm *kvm;
115
116         kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
117         if (!kvm)
118                 return ERR_PTR(-ENOMEM);
119
120         return kvm;
121 }
122
123 static void kvmppc_free_vcpus(struct kvm *kvm)
124 {
125         unsigned int i;
126
127         for (i = 0; i < KVM_MAX_VCPUS; ++i) {
128                 if (kvm->vcpus[i]) {
129                         kvm_arch_vcpu_free(kvm->vcpus[i]);
130                         kvm->vcpus[i] = NULL;
131                 }
132         }
133 }
134
135 void kvm_arch_destroy_vm(struct kvm *kvm)
136 {
137         kvmppc_free_vcpus(kvm);
138         kvm_free_physmem(kvm);
139         kfree(kvm);
140 }
141
142 int kvm_dev_ioctl_check_extension(long ext)
143 {
144         int r;
145
146         switch (ext) {
147         case KVM_CAP_USER_MEMORY:
148                 r = 1;
149                 break;
150         case KVM_CAP_COALESCED_MMIO:
151                 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
152                 break;
153         default:
154                 r = 0;
155                 break;
156         }
157         return r;
158
159 }
160
161 long kvm_arch_dev_ioctl(struct file *filp,
162                         unsigned int ioctl, unsigned long arg)
163 {
164         return -EINVAL;
165 }
166
167 int kvm_arch_set_memory_region(struct kvm *kvm,
168                                struct kvm_userspace_memory_region *mem,
169                                struct kvm_memory_slot old,
170                                int user_alloc)
171 {
172         return 0;
173 }
174
175 void kvm_arch_flush_shadow(struct kvm *kvm)
176 {
177 }
178
179 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
180 {
181         struct kvm_vcpu *vcpu;
182         int err;
183
184         vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
185         if (!vcpu) {
186                 err = -ENOMEM;
187                 goto out;
188         }
189
190         err = kvm_vcpu_init(vcpu, kvm, id);
191         if (err)
192                 goto free_vcpu;
193
194         return vcpu;
195
196 free_vcpu:
197         kmem_cache_free(kvm_vcpu_cache, vcpu);
198 out:
199         return ERR_PTR(err);
200 }
201
202 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
203 {
204         kvm_vcpu_uninit(vcpu);
205         kmem_cache_free(kvm_vcpu_cache, vcpu);
206 }
207
208 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
209 {
210         kvm_arch_vcpu_free(vcpu);
211 }
212
213 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
214 {
215         unsigned int priority = exception_priority[BOOKE_INTERRUPT_DECREMENTER];
216
217         return test_bit(priority, &vcpu->arch.pending_exceptions);
218 }
219
220 static void kvmppc_decrementer_func(unsigned long data)
221 {
222         struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
223
224         kvmppc_queue_exception(vcpu, BOOKE_INTERRUPT_DECREMENTER);
225
226         if (waitqueue_active(&vcpu->wq)) {
227                 wake_up_interruptible(&vcpu->wq);
228                 vcpu->stat.halt_wakeup++;
229         }
230 }
231
232 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
233 {
234         setup_timer(&vcpu->arch.dec_timer, kvmppc_decrementer_func,
235                     (unsigned long)vcpu);
236
237         return 0;
238 }
239
240 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
241 {
242         kvmppc_core_destroy_mmu(vcpu);
243 }
244
245 /* Note: clearing MSR[DE] just means that the debug interrupt will not be
246  * delivered *immediately*. Instead, it simply sets the appropriate DBSR bits.
247  * If those DBSR bits are still set when MSR[DE] is re-enabled, the interrupt
248  * will be delivered as an "imprecise debug event" (which is indicated by
249  * DBSR[IDE].
250  */
251 static void kvmppc_disable_debug_interrupts(void)
252 {
253         mtmsr(mfmsr() & ~MSR_DE);
254 }
255
256 static void kvmppc_restore_host_debug_state(struct kvm_vcpu *vcpu)
257 {
258         kvmppc_disable_debug_interrupts();
259
260         mtspr(SPRN_IAC1, vcpu->arch.host_iac[0]);
261         mtspr(SPRN_IAC2, vcpu->arch.host_iac[1]);
262         mtspr(SPRN_IAC3, vcpu->arch.host_iac[2]);
263         mtspr(SPRN_IAC4, vcpu->arch.host_iac[3]);
264         mtspr(SPRN_DBCR1, vcpu->arch.host_dbcr1);
265         mtspr(SPRN_DBCR2, vcpu->arch.host_dbcr2);
266         mtspr(SPRN_DBCR0, vcpu->arch.host_dbcr0);
267         mtmsr(vcpu->arch.host_msr);
268 }
269
270 static void kvmppc_load_guest_debug_registers(struct kvm_vcpu *vcpu)
271 {
272         struct kvm_guest_debug *dbg = &vcpu->guest_debug;
273         u32 dbcr0 = 0;
274
275         vcpu->arch.host_msr = mfmsr();
276         kvmppc_disable_debug_interrupts();
277
278         /* Save host debug register state. */
279         vcpu->arch.host_iac[0] = mfspr(SPRN_IAC1);
280         vcpu->arch.host_iac[1] = mfspr(SPRN_IAC2);
281         vcpu->arch.host_iac[2] = mfspr(SPRN_IAC3);
282         vcpu->arch.host_iac[3] = mfspr(SPRN_IAC4);
283         vcpu->arch.host_dbcr0 = mfspr(SPRN_DBCR0);
284         vcpu->arch.host_dbcr1 = mfspr(SPRN_DBCR1);
285         vcpu->arch.host_dbcr2 = mfspr(SPRN_DBCR2);
286
287         /* set registers up for guest */
288
289         if (dbg->bp[0]) {
290                 mtspr(SPRN_IAC1, dbg->bp[0]);
291                 dbcr0 |= DBCR0_IAC1 | DBCR0_IDM;
292         }
293         if (dbg->bp[1]) {
294                 mtspr(SPRN_IAC2, dbg->bp[1]);
295                 dbcr0 |= DBCR0_IAC2 | DBCR0_IDM;
296         }
297         if (dbg->bp[2]) {
298                 mtspr(SPRN_IAC3, dbg->bp[2]);
299                 dbcr0 |= DBCR0_IAC3 | DBCR0_IDM;
300         }
301         if (dbg->bp[3]) {
302                 mtspr(SPRN_IAC4, dbg->bp[3]);
303                 dbcr0 |= DBCR0_IAC4 | DBCR0_IDM;
304         }
305
306         mtspr(SPRN_DBCR0, dbcr0);
307         mtspr(SPRN_DBCR1, 0);
308         mtspr(SPRN_DBCR2, 0);
309 }
310
311 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
312 {
313         int i;
314
315         if (vcpu->guest_debug.enabled)
316                 kvmppc_load_guest_debug_registers(vcpu);
317
318         /* Mark every guest entry in the shadow TLB entry modified, so that they
319          * will all be reloaded on the next vcpu run (instead of being
320          * demand-faulted). */
321         for (i = 0; i <= tlb_44x_hwater; i++)
322                 kvmppc_tlbe_set_modified(vcpu, i);
323 }
324
325 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
326 {
327         if (vcpu->guest_debug.enabled)
328                 kvmppc_restore_host_debug_state(vcpu);
329
330         /* Don't leave guest TLB entries resident when being de-scheduled. */
331         /* XXX It would be nice to differentiate between heavyweight exit and
332          * sched_out here, since we could avoid the TLB flush for heavyweight
333          * exits. */
334         _tlbil_all();
335 }
336
337 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
338                                     struct kvm_debug_guest *dbg)
339 {
340         int i;
341
342         vcpu->guest_debug.enabled = dbg->enabled;
343         if (vcpu->guest_debug.enabled) {
344                 for (i=0; i < ARRAY_SIZE(vcpu->guest_debug.bp); i++) {
345                         if (dbg->breakpoints[i].enabled)
346                                 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
347                         else
348                                 vcpu->guest_debug.bp[i] = 0;
349                 }
350         }
351
352         return 0;
353 }
354
355 static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
356                                      struct kvm_run *run)
357 {
358         u32 *gpr = &vcpu->arch.gpr[vcpu->arch.io_gpr];
359         *gpr = run->dcr.data;
360 }
361
362 static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
363                                       struct kvm_run *run)
364 {
365         u32 *gpr = &vcpu->arch.gpr[vcpu->arch.io_gpr];
366
367         if (run->mmio.len > sizeof(*gpr)) {
368                 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
369                 return;
370         }
371
372         if (vcpu->arch.mmio_is_bigendian) {
373                 switch (run->mmio.len) {
374                 case 4: *gpr = *(u32 *)run->mmio.data; break;
375                 case 2: *gpr = *(u16 *)run->mmio.data; break;
376                 case 1: *gpr = *(u8 *)run->mmio.data; break;
377                 }
378         } else {
379                 /* Convert BE data from userland back to LE. */
380                 switch (run->mmio.len) {
381                 case 4: *gpr = ld_le32((u32 *)run->mmio.data); break;
382                 case 2: *gpr = ld_le16((u16 *)run->mmio.data); break;
383                 case 1: *gpr = *(u8 *)run->mmio.data; break;
384                 }
385         }
386 }
387
388 int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
389                        unsigned int rt, unsigned int bytes, int is_bigendian)
390 {
391         if (bytes > sizeof(run->mmio.data)) {
392                 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
393                        run->mmio.len);
394         }
395
396         run->mmio.phys_addr = vcpu->arch.paddr_accessed;
397         run->mmio.len = bytes;
398         run->mmio.is_write = 0;
399
400         vcpu->arch.io_gpr = rt;
401         vcpu->arch.mmio_is_bigendian = is_bigendian;
402         vcpu->mmio_needed = 1;
403         vcpu->mmio_is_write = 0;
404
405         return EMULATE_DO_MMIO;
406 }
407
408 int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
409                         u32 val, unsigned int bytes, int is_bigendian)
410 {
411         void *data = run->mmio.data;
412
413         if (bytes > sizeof(run->mmio.data)) {
414                 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
415                        run->mmio.len);
416         }
417
418         run->mmio.phys_addr = vcpu->arch.paddr_accessed;
419         run->mmio.len = bytes;
420         run->mmio.is_write = 1;
421         vcpu->mmio_needed = 1;
422         vcpu->mmio_is_write = 1;
423
424         /* Store the value at the lowest bytes in 'data'. */
425         if (is_bigendian) {
426                 switch (bytes) {
427                 case 4: *(u32 *)data = val; break;
428                 case 2: *(u16 *)data = val; break;
429                 case 1: *(u8  *)data = val; break;
430                 }
431         } else {
432                 /* Store LE value into 'data'. */
433                 switch (bytes) {
434                 case 4: st_le32(data, val); break;
435                 case 2: st_le16(data, val); break;
436                 case 1: *(u8 *)data = val; break;
437                 }
438         }
439
440         return EMULATE_DO_MMIO;
441 }
442
443 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
444 {
445         int r;
446         sigset_t sigsaved;
447
448         vcpu_load(vcpu);
449
450         if (vcpu->sigset_active)
451                 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
452
453         if (vcpu->mmio_needed) {
454                 if (!vcpu->mmio_is_write)
455                         kvmppc_complete_mmio_load(vcpu, run);
456                 vcpu->mmio_needed = 0;
457         } else if (vcpu->arch.dcr_needed) {
458                 if (!vcpu->arch.dcr_is_write)
459                         kvmppc_complete_dcr_load(vcpu, run);
460                 vcpu->arch.dcr_needed = 0;
461         }
462
463         kvmppc_check_and_deliver_interrupts(vcpu);
464
465         local_irq_disable();
466         kvm_guest_enter();
467         r = __kvmppc_vcpu_run(run, vcpu);
468         kvm_guest_exit();
469         local_irq_enable();
470
471         if (vcpu->sigset_active)
472                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
473
474         vcpu_put(vcpu);
475
476         return r;
477 }
478
479 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
480 {
481         kvmppc_queue_exception(vcpu, BOOKE_INTERRUPT_EXTERNAL);
482
483         if (waitqueue_active(&vcpu->wq)) {
484                 wake_up_interruptible(&vcpu->wq);
485                 vcpu->stat.halt_wakeup++;
486         }
487
488         return 0;
489 }
490
491 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
492                                     struct kvm_mp_state *mp_state)
493 {
494         return -EINVAL;
495 }
496
497 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
498                                     struct kvm_mp_state *mp_state)
499 {
500         return -EINVAL;
501 }
502
503 long kvm_arch_vcpu_ioctl(struct file *filp,
504                          unsigned int ioctl, unsigned long arg)
505 {
506         struct kvm_vcpu *vcpu = filp->private_data;
507         void __user *argp = (void __user *)arg;
508         long r;
509
510         switch (ioctl) {
511         case KVM_INTERRUPT: {
512                 struct kvm_interrupt irq;
513                 r = -EFAULT;
514                 if (copy_from_user(&irq, argp, sizeof(irq)))
515                         goto out;
516                 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
517                 break;
518         }
519         default:
520                 r = -EINVAL;
521         }
522
523 out:
524         return r;
525 }
526
527 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
528 {
529         return -ENOTSUPP;
530 }
531
532 long kvm_arch_vm_ioctl(struct file *filp,
533                        unsigned int ioctl, unsigned long arg)
534 {
535         long r;
536
537         switch (ioctl) {
538         default:
539                 r = -EINVAL;
540         }
541
542         return r;
543 }
544
545 int kvm_arch_init(void *opaque)
546 {
547         return 0;
548 }
549
550 void kvm_arch_exit(void)
551 {
552 }