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