blob: b75607180ddb5a42363319f3391158f00333dd67 [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>
30
31
32gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
33{
34 return gfn;
35}
36
37int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
38{
Hollis Blanchard45c5eb62008-04-25 17:55:49 -050039 return !!(v->arch.pending_exceptions);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050040}
41
42int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
43{
Hollis Blanchard45c5eb62008-04-25 17:55:49 -050044 return !(v->arch.msr & MSR_WE);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050045}
46
47
48int 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
81void kvm_arch_hardware_enable(void *garbage)
82{
83}
84
85void kvm_arch_hardware_disable(void *garbage)
86{
87}
88
89int kvm_arch_hardware_setup(void)
90{
91 return 0;
92}
93
94void kvm_arch_hardware_unsetup(void)
95{
96}
97
98void 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
110struct 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
121static 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
133void kvm_arch_destroy_vm(struct kvm *kvm)
134{
135 kvmppc_free_vcpus(kvm);
136 kvm_free_physmem(kvm);
137 kfree(kvm);
138}
139
140int 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;
Laurent Vivier588968b2008-05-30 16:05:56 +0200148 case KVM_CAP_COALESCED_MMIO:
149 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
150 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500151 default:
152 r = 0;
153 break;
154 }
155 return r;
156
157}
158
159long kvm_arch_dev_ioctl(struct file *filp,
160 unsigned int ioctl, unsigned long arg)
161{
162 return -EINVAL;
163}
164
165int 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
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300173void kvm_arch_flush_shadow(struct kvm *kvm)
174{
175}
176
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500177struct 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
194free_vcpu:
195 kmem_cache_free(kvm_vcpu_cache, vcpu);
196out:
197 return ERR_PTR(err);
198}
199
200void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
201{
202 kvm_vcpu_uninit(vcpu);
203 kmem_cache_free(kvm_vcpu_cache, vcpu);
204}
205
206void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
207{
208 kvm_arch_vcpu_free(vcpu);
209}
210
211int 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
218static 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);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500223
224 if (waitqueue_active(&vcpu->wq)) {
225 wake_up_interruptible(&vcpu->wq);
226 vcpu->stat.halt_wakeup++;
227 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500228}
229
230int 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
238void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
239{
240}
241
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500242/* 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 */
248static void kvmppc_disable_debug_interrupts(void)
249{
250 mtmsr(mfmsr() & ~MSR_DE);
251}
252
253static 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
267static 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
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500308void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
309{
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500310 if (vcpu->guest_debug.enabled)
311 kvmppc_load_guest_debug_registers(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500312}
313
314void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
315{
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500316 if (vcpu->guest_debug.enabled)
317 kvmppc_restore_host_debug_state(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500318}
319
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500320int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
321 struct kvm_debug_guest *dbg)
322{
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500323 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;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500336}
337
338static 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
345static 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
371int 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
391int 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
426int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
427{
428 int r;
429 sigset_t sigsaved;
430
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500431 vcpu_load(vcpu);
432
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500433 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
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500457 vcpu_put(vcpu);
458
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500459 return r;
460}
461
462int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
463{
464 kvmppc_queue_exception(vcpu, BOOKE_INTERRUPT_EXTERNAL);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500465
466 if (waitqueue_active(&vcpu->wq)) {
467 wake_up_interruptible(&vcpu->wq);
468 vcpu->stat.halt_wakeup++;
469 }
470
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500471 return 0;
472}
473
474int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
475 struct kvm_mp_state *mp_state)
476{
477 return -EINVAL;
478}
479
480int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
481 struct kvm_mp_state *mp_state)
482{
483 return -EINVAL;
484}
485
486long 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
506out:
507 return r;
508}
509
510int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
511{
512 return -ENOTSUPP;
513}
514
515long 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
528int kvm_arch_init(void *opaque)
529{
530 return 0;
531}
532
533void kvm_arch_exit(void)
534{
535}