7ad30e0a1b9ae570b66264d47e27e8204c6a878c
[pandora-kernel.git] / 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 "timing.h"
32 #include "../mm/mmu_decl.h"
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_interrupt_allowed(struct kvm_vcpu *vcpu)
45 {
46         /* do real check here */
47         return 1;
48 }
49
50 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
51 {
52         return !(v->arch.msr & MSR_WE);
53 }
54
55
56 int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
57 {
58         enum emulation_result er;
59         int r;
60
61         er = kvmppc_emulate_instruction(run, vcpu);
62         switch (er) {
63         case EMULATE_DONE:
64                 /* Future optimization: only reload non-volatiles if they were
65                  * actually modified. */
66                 r = RESUME_GUEST_NV;
67                 break;
68         case EMULATE_DO_MMIO:
69                 run->exit_reason = KVM_EXIT_MMIO;
70                 /* We must reload nonvolatiles because "update" load/store
71                  * instructions modify register state. */
72                 /* Future optimization: only reload non-volatiles if they were
73                  * actually modified. */
74                 r = RESUME_HOST_NV;
75                 break;
76         case EMULATE_FAIL:
77                 /* XXX Deliver Program interrupt to guest. */
78                 printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__,
79                        vcpu->arch.last_inst);
80                 r = RESUME_HOST;
81                 break;
82         default:
83                 BUG();
84         }
85
86         return r;
87 }
88
89 void kvm_arch_hardware_enable(void *garbage)
90 {
91 }
92
93 void kvm_arch_hardware_disable(void *garbage)
94 {
95 }
96
97 int kvm_arch_hardware_setup(void)
98 {
99         return 0;
100 }
101
102 void kvm_arch_hardware_unsetup(void)
103 {
104 }
105
106 void kvm_arch_check_processor_compat(void *rtn)
107 {
108         *(int *)rtn = kvmppc_core_check_processor_compat();
109 }
110
111 struct kvm *kvm_arch_create_vm(void)
112 {
113         struct kvm *kvm;
114
115         kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
116         if (!kvm)
117                 return ERR_PTR(-ENOMEM);
118
119         return kvm;
120 }
121
122 static void kvmppc_free_vcpus(struct kvm *kvm)
123 {
124         unsigned int i;
125         struct kvm_vcpu *vcpu;
126
127         kvm_for_each_vcpu(i, vcpu, kvm)
128                 kvm_arch_vcpu_free(vcpu);
129
130         mutex_lock(&kvm->lock);
131         for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
132                 kvm->vcpus[i] = NULL;
133
134         atomic_set(&kvm->online_vcpus, 0);
135         mutex_unlock(&kvm->lock);
136 }
137
138 void kvm_arch_sync_events(struct kvm *kvm)
139 {
140 }
141
142 void kvm_arch_destroy_vm(struct kvm *kvm)
143 {
144         kvmppc_free_vcpus(kvm);
145         kvm_free_physmem(kvm);
146         kfree(kvm);
147 }
148
149 int kvm_dev_ioctl_check_extension(long ext)
150 {
151         int r;
152
153         switch (ext) {
154         case KVM_CAP_COALESCED_MMIO:
155                 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
156                 break;
157         default:
158                 r = 0;
159                 break;
160         }
161         return r;
162
163 }
164
165 long kvm_arch_dev_ioctl(struct file *filp,
166                         unsigned int ioctl, unsigned long arg)
167 {
168         return -EINVAL;
169 }
170
171 int kvm_arch_set_memory_region(struct kvm *kvm,
172                                struct kvm_userspace_memory_region *mem,
173                                struct kvm_memory_slot old,
174                                int user_alloc)
175 {
176         return 0;
177 }
178
179 void kvm_arch_flush_shadow(struct kvm *kvm)
180 {
181 }
182
183 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
184 {
185         struct kvm_vcpu *vcpu;
186         vcpu = kvmppc_core_vcpu_create(kvm, id);
187         kvmppc_create_vcpu_debugfs(vcpu, id);
188         return vcpu;
189 }
190
191 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
192 {
193         kvmppc_remove_vcpu_debugfs(vcpu);
194         kvmppc_core_vcpu_free(vcpu);
195 }
196
197 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
198 {
199         kvm_arch_vcpu_free(vcpu);
200 }
201
202 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
203 {
204         return kvmppc_core_pending_dec(vcpu);
205 }
206
207 static void kvmppc_decrementer_func(unsigned long data)
208 {
209         struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
210
211         kvmppc_core_queue_dec(vcpu);
212
213         if (waitqueue_active(&vcpu->wq)) {
214                 wake_up_interruptible(&vcpu->wq);
215                 vcpu->stat.halt_wakeup++;
216         }
217 }
218
219 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
220 {
221         setup_timer(&vcpu->arch.dec_timer, kvmppc_decrementer_func,
222                     (unsigned long)vcpu);
223
224         return 0;
225 }
226
227 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
228 {
229         kvmppc_mmu_destroy(vcpu);
230 }
231
232 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
233 {
234         kvmppc_core_vcpu_load(vcpu, cpu);
235 }
236
237 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
238 {
239         kvmppc_core_vcpu_put(vcpu);
240 }
241
242 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
243                                         struct kvm_guest_debug *dbg)
244 {
245         return -EINVAL;
246 }
247
248 static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
249                                      struct kvm_run *run)
250 {
251         ulong *gpr = &vcpu->arch.gpr[vcpu->arch.io_gpr];
252         *gpr = run->dcr.data;
253 }
254
255 static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
256                                       struct kvm_run *run)
257 {
258         ulong *gpr = &vcpu->arch.gpr[vcpu->arch.io_gpr];
259
260         if (run->mmio.len > sizeof(*gpr)) {
261                 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
262                 return;
263         }
264
265         if (vcpu->arch.mmio_is_bigendian) {
266                 switch (run->mmio.len) {
267                 case 4: *gpr = *(u32 *)run->mmio.data; break;
268                 case 2: *gpr = *(u16 *)run->mmio.data; break;
269                 case 1: *gpr = *(u8 *)run->mmio.data; break;
270                 }
271         } else {
272                 /* Convert BE data from userland back to LE. */
273                 switch (run->mmio.len) {
274                 case 4: *gpr = ld_le32((u32 *)run->mmio.data); break;
275                 case 2: *gpr = ld_le16((u16 *)run->mmio.data); break;
276                 case 1: *gpr = *(u8 *)run->mmio.data; break;
277                 }
278         }
279 }
280
281 int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
282                        unsigned int rt, unsigned int bytes, int is_bigendian)
283 {
284         if (bytes > sizeof(run->mmio.data)) {
285                 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
286                        run->mmio.len);
287         }
288
289         run->mmio.phys_addr = vcpu->arch.paddr_accessed;
290         run->mmio.len = bytes;
291         run->mmio.is_write = 0;
292
293         vcpu->arch.io_gpr = rt;
294         vcpu->arch.mmio_is_bigendian = is_bigendian;
295         vcpu->mmio_needed = 1;
296         vcpu->mmio_is_write = 0;
297
298         return EMULATE_DO_MMIO;
299 }
300
301 int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
302                         u32 val, unsigned int bytes, int is_bigendian)
303 {
304         void *data = run->mmio.data;
305
306         if (bytes > sizeof(run->mmio.data)) {
307                 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
308                        run->mmio.len);
309         }
310
311         run->mmio.phys_addr = vcpu->arch.paddr_accessed;
312         run->mmio.len = bytes;
313         run->mmio.is_write = 1;
314         vcpu->mmio_needed = 1;
315         vcpu->mmio_is_write = 1;
316
317         /* Store the value at the lowest bytes in 'data'. */
318         if (is_bigendian) {
319                 switch (bytes) {
320                 case 4: *(u32 *)data = val; break;
321                 case 2: *(u16 *)data = val; break;
322                 case 1: *(u8  *)data = val; break;
323                 }
324         } else {
325                 /* Store LE value into 'data'. */
326                 switch (bytes) {
327                 case 4: st_le32(data, val); break;
328                 case 2: st_le16(data, val); break;
329                 case 1: *(u8 *)data = val; break;
330                 }
331         }
332
333         return EMULATE_DO_MMIO;
334 }
335
336 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
337 {
338         int r;
339         sigset_t sigsaved;
340
341         vcpu_load(vcpu);
342
343         if (vcpu->sigset_active)
344                 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
345
346         if (vcpu->mmio_needed) {
347                 if (!vcpu->mmio_is_write)
348                         kvmppc_complete_mmio_load(vcpu, run);
349                 vcpu->mmio_needed = 0;
350         } else if (vcpu->arch.dcr_needed) {
351                 if (!vcpu->arch.dcr_is_write)
352                         kvmppc_complete_dcr_load(vcpu, run);
353                 vcpu->arch.dcr_needed = 0;
354         }
355
356         kvmppc_core_deliver_interrupts(vcpu);
357
358         local_irq_disable();
359         kvm_guest_enter();
360         r = __kvmppc_vcpu_run(run, vcpu);
361         kvm_guest_exit();
362         local_irq_enable();
363
364         if (vcpu->sigset_active)
365                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
366
367         vcpu_put(vcpu);
368
369         return r;
370 }
371
372 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
373 {
374         kvmppc_core_queue_external(vcpu, irq);
375
376         if (waitqueue_active(&vcpu->wq)) {
377                 wake_up_interruptible(&vcpu->wq);
378                 vcpu->stat.halt_wakeup++;
379         }
380
381         return 0;
382 }
383
384 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
385                                     struct kvm_mp_state *mp_state)
386 {
387         return -EINVAL;
388 }
389
390 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
391                                     struct kvm_mp_state *mp_state)
392 {
393         return -EINVAL;
394 }
395
396 long kvm_arch_vcpu_ioctl(struct file *filp,
397                          unsigned int ioctl, unsigned long arg)
398 {
399         struct kvm_vcpu *vcpu = filp->private_data;
400         void __user *argp = (void __user *)arg;
401         long r;
402
403         switch (ioctl) {
404         case KVM_INTERRUPT: {
405                 struct kvm_interrupt irq;
406                 r = -EFAULT;
407                 if (copy_from_user(&irq, argp, sizeof(irq)))
408                         goto out;
409                 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
410                 break;
411         }
412         default:
413                 r = -EINVAL;
414         }
415
416 out:
417         return r;
418 }
419
420 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
421 {
422         return -ENOTSUPP;
423 }
424
425 long kvm_arch_vm_ioctl(struct file *filp,
426                        unsigned int ioctl, unsigned long arg)
427 {
428         long r;
429
430         switch (ioctl) {
431         default:
432                 r = -EINVAL;
433         }
434
435         return r;
436 }
437
438 int kvm_arch_init(void *opaque)
439 {
440         return 0;
441 }
442
443 void kvm_arch_exit(void)
444 {
445 }