blob: 7ad30e0a1b9ae570b66264d47e27e8204c6a878c [file] [log] [blame]
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001/*
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>
Hollis Blanchard83aae4a2008-07-25 13:54:52 -050030#include <asm/tlbflush.h>
Hollis Blanchard73e75b42008-12-02 15:51:57 -060031#include "timing.h"
Paul Mackerrasfad7b9b2008-12-23 14:57:26 +110032#include "../mm/mmu_decl.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050033
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050034gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
35{
36 return gfn;
37}
38
39int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
40{
Hollis Blanchard45c5eb62008-04-25 17:55:49 -050041 return !!(v->arch.pending_exceptions);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050042}
43
Gleb Natapov78646122009-03-23 12:12:11 +020044int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
45{
46 /* do real check here */
47 return 1;
48}
49
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050050int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
51{
Hollis Blanchard45c5eb62008-04-25 17:55:49 -050052 return !(v->arch.msr & MSR_WE);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050053}
54
55
56int 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
89void kvm_arch_hardware_enable(void *garbage)
90{
91}
92
93void kvm_arch_hardware_disable(void *garbage)
94{
95}
96
97int kvm_arch_hardware_setup(void)
98{
99 return 0;
100}
101
102void kvm_arch_hardware_unsetup(void)
103{
104}
105
106void kvm_arch_check_processor_compat(void *rtn)
107{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600108 *(int *)rtn = kvmppc_core_check_processor_compat();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500109}
110
111struct 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
122static void kvmppc_free_vcpus(struct kvm *kvm)
123{
124 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300125 struct kvm_vcpu *vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500126
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300127 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);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500136}
137
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800138void kvm_arch_sync_events(struct kvm *kvm)
139{
140}
141
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500142void kvm_arch_destroy_vm(struct kvm *kvm)
143{
144 kvmppc_free_vcpus(kvm);
145 kvm_free_physmem(kvm);
146 kfree(kvm);
147}
148
149int kvm_dev_ioctl_check_extension(long ext)
150{
151 int r;
152
153 switch (ext) {
Laurent Vivier588968b2008-05-30 16:05:56 +0200154 case KVM_CAP_COALESCED_MMIO:
155 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
156 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500157 default:
158 r = 0;
159 break;
160 }
161 return r;
162
163}
164
165long kvm_arch_dev_ioctl(struct file *filp,
166 unsigned int ioctl, unsigned long arg)
167{
168 return -EINVAL;
169}
170
171int 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
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300179void kvm_arch_flush_shadow(struct kvm *kvm)
180{
181}
182
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500183struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
184{
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600185 struct kvm_vcpu *vcpu;
186 vcpu = kvmppc_core_vcpu_create(kvm, id);
187 kvmppc_create_vcpu_debugfs(vcpu, id);
188 return vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500189}
190
191void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
192{
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600193 kvmppc_remove_vcpu_debugfs(vcpu);
Hollis Blancharddb93f572008-11-05 09:36:18 -0600194 kvmppc_core_vcpu_free(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500195}
196
197void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
198{
199 kvm_arch_vcpu_free(vcpu);
200}
201
202int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
203{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600204 return kvmppc_core_pending_dec(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500205}
206
207static void kvmppc_decrementer_func(unsigned long data)
208{
209 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
210
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600211 kvmppc_core_queue_dec(vcpu);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500212
213 if (waitqueue_active(&vcpu->wq)) {
214 wake_up_interruptible(&vcpu->wq);
215 vcpu->stat.halt_wakeup++;
216 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500217}
218
219int 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
227void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
228{
Hollis Blanchardecc09812009-01-03 16:22:59 -0600229 kvmppc_mmu_destroy(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500230}
231
232void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
233{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600234 kvmppc_core_vcpu_load(vcpu, cpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500235}
236
237void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
238{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600239 kvmppc_core_vcpu_put(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500240}
241
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100242int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
Hollis Blanchardf5d09062009-01-04 13:51:09 -0600243 struct kvm_guest_debug *dbg)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500244{
Hollis Blanchardf5d09062009-01-04 13:51:09 -0600245 return -EINVAL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500246}
247
248static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
249 struct kvm_run *run)
250{
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -0600251 ulong *gpr = &vcpu->arch.gpr[vcpu->arch.io_gpr];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500252 *gpr = run->dcr.data;
253}
254
255static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
256 struct kvm_run *run)
257{
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -0600258 ulong *gpr = &vcpu->arch.gpr[vcpu->arch.io_gpr];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500259
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
281int 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
301int 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
336int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
337{
338 int r;
339 sigset_t sigsaved;
340
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500341 vcpu_load(vcpu);
342
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500343 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
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600356 kvmppc_core_deliver_interrupts(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500357
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
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500367 vcpu_put(vcpu);
368
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500369 return r;
370}
371
372int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
373{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600374 kvmppc_core_queue_external(vcpu, irq);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500375
376 if (waitqueue_active(&vcpu->wq)) {
377 wake_up_interruptible(&vcpu->wq);
378 vcpu->stat.halt_wakeup++;
379 }
380
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500381 return 0;
382}
383
384int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
385 struct kvm_mp_state *mp_state)
386{
387 return -EINVAL;
388}
389
390int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
391 struct kvm_mp_state *mp_state)
392{
393 return -EINVAL;
394}
395
396long 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
416out:
417 return r;
418}
419
420int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
421{
422 return -ENOTSUPP;
423}
424
425long 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
438int kvm_arch_init(void *opaque)
439{
440 return 0;
441}
442
443void kvm_arch_exit(void)
444{
445}