OSDN Git Service

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