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.
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.
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.
15 * Copyright IBM Corp. 2007
17 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
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>
27 #include <asm/cputable.h>
28 #include <asm/uaccess.h>
29 #include <asm/kvm_ppc.h>
30 #include <asm/tlbflush.h>
33 gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
38 int kvm_cpu_has_interrupt(struct kvm_vcpu
*v
)
40 return !!(v
->arch
.pending_exceptions
);
43 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*v
)
45 return !(v
->arch
.msr
& MSR_WE
);
49 int kvmppc_emulate_mmio(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
)
51 enum emulation_result er
;
54 er
= kvmppc_emulate_instruction(run
, vcpu
);
57 /* Future optimization: only reload non-volatiles if they were
58 * actually modified. */
62 run
->exit_reason
= KVM_EXIT_MMIO
;
63 /* We must reload nonvolatiles because "update" load/store
64 * instructions modify register state. */
65 /* Future optimization: only reload non-volatiles if they were
66 * actually modified. */
70 /* XXX Deliver Program interrupt to guest. */
71 printk(KERN_EMERG
"%s: emulation failed (%08x)\n", __func__
,
72 vcpu
->arch
.last_inst
);
82 void kvm_arch_hardware_enable(void *garbage
)
86 void kvm_arch_hardware_disable(void *garbage
)
90 int kvm_arch_hardware_setup(void)
95 void kvm_arch_hardware_unsetup(void)
99 void kvm_arch_check_processor_compat(void *rtn
)
103 if (strcmp(cur_cpu_spec
->platform
, "ppc440") == 0)
111 struct kvm
*kvm_arch_create_vm(void)
115 kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
117 return ERR_PTR(-ENOMEM
);
122 static void kvmppc_free_vcpus(struct kvm
*kvm
)
126 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
128 kvm_arch_vcpu_free(kvm
->vcpus
[i
]);
129 kvm
->vcpus
[i
] = NULL
;
134 void kvm_arch_destroy_vm(struct kvm
*kvm
)
136 kvmppc_free_vcpus(kvm
);
137 kvm_free_physmem(kvm
);
141 int kvm_dev_ioctl_check_extension(long ext
)
146 case KVM_CAP_USER_MEMORY
:
149 case KVM_CAP_COALESCED_MMIO
:
150 r
= KVM_COALESCED_MMIO_PAGE_OFFSET
;
160 long kvm_arch_dev_ioctl(struct file
*filp
,
161 unsigned int ioctl
, unsigned long arg
)
166 int kvm_arch_set_memory_region(struct kvm
*kvm
,
167 struct kvm_userspace_memory_region
*mem
,
168 struct kvm_memory_slot old
,
174 void kvm_arch_flush_shadow(struct kvm
*kvm
)
178 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
, unsigned int id
)
180 struct kvm_vcpu
*vcpu
;
183 vcpu
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
189 err
= kvm_vcpu_init(vcpu
, kvm
, id
);
196 kmem_cache_free(kvm_vcpu_cache
, vcpu
);
201 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
203 kvm_vcpu_uninit(vcpu
);
204 kmem_cache_free(kvm_vcpu_cache
, vcpu
);
207 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
209 kvm_arch_vcpu_free(vcpu
);
212 int kvm_cpu_has_pending_timer(struct kvm_vcpu
*vcpu
)
214 unsigned int priority
= exception_priority
[BOOKE_INTERRUPT_DECREMENTER
];
216 return test_bit(priority
, &vcpu
->arch
.pending_exceptions
);
219 static void kvmppc_decrementer_func(unsigned long data
)
221 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*)data
;
223 kvmppc_queue_exception(vcpu
, BOOKE_INTERRUPT_DECREMENTER
);
225 if (waitqueue_active(&vcpu
->wq
)) {
226 wake_up_interruptible(&vcpu
->wq
);
227 vcpu
->stat
.halt_wakeup
++;
231 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
233 setup_timer(&vcpu
->arch
.dec_timer
, kvmppc_decrementer_func
,
234 (unsigned long)vcpu
);
239 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
241 kvmppc_core_destroy_mmu(vcpu
);
244 /* Note: clearing MSR[DE] just means that the debug interrupt will not be
245 * delivered *immediately*. Instead, it simply sets the appropriate DBSR bits.
246 * If those DBSR bits are still set when MSR[DE] is re-enabled, the interrupt
247 * will be delivered as an "imprecise debug event" (which is indicated by
250 static void kvmppc_disable_debug_interrupts(void)
252 mtmsr(mfmsr() & ~MSR_DE
);
255 static void kvmppc_restore_host_debug_state(struct kvm_vcpu
*vcpu
)
257 kvmppc_disable_debug_interrupts();
259 mtspr(SPRN_IAC1
, vcpu
->arch
.host_iac
[0]);
260 mtspr(SPRN_IAC2
, vcpu
->arch
.host_iac
[1]);
261 mtspr(SPRN_IAC3
, vcpu
->arch
.host_iac
[2]);
262 mtspr(SPRN_IAC4
, vcpu
->arch
.host_iac
[3]);
263 mtspr(SPRN_DBCR1
, vcpu
->arch
.host_dbcr1
);
264 mtspr(SPRN_DBCR2
, vcpu
->arch
.host_dbcr2
);
265 mtspr(SPRN_DBCR0
, vcpu
->arch
.host_dbcr0
);
266 mtmsr(vcpu
->arch
.host_msr
);
269 static void kvmppc_load_guest_debug_registers(struct kvm_vcpu
*vcpu
)
271 struct kvm_guest_debug
*dbg
= &vcpu
->guest_debug
;
274 vcpu
->arch
.host_msr
= mfmsr();
275 kvmppc_disable_debug_interrupts();
277 /* Save host debug register state. */
278 vcpu
->arch
.host_iac
[0] = mfspr(SPRN_IAC1
);
279 vcpu
->arch
.host_iac
[1] = mfspr(SPRN_IAC2
);
280 vcpu
->arch
.host_iac
[2] = mfspr(SPRN_IAC3
);
281 vcpu
->arch
.host_iac
[3] = mfspr(SPRN_IAC4
);
282 vcpu
->arch
.host_dbcr0
= mfspr(SPRN_DBCR0
);
283 vcpu
->arch
.host_dbcr1
= mfspr(SPRN_DBCR1
);
284 vcpu
->arch
.host_dbcr2
= mfspr(SPRN_DBCR2
);
286 /* set registers up for guest */
289 mtspr(SPRN_IAC1
, dbg
->bp
[0]);
290 dbcr0
|= DBCR0_IAC1
| DBCR0_IDM
;
293 mtspr(SPRN_IAC2
, dbg
->bp
[1]);
294 dbcr0
|= DBCR0_IAC2
| DBCR0_IDM
;
297 mtspr(SPRN_IAC3
, dbg
->bp
[2]);
298 dbcr0
|= DBCR0_IAC3
| DBCR0_IDM
;
301 mtspr(SPRN_IAC4
, dbg
->bp
[3]);
302 dbcr0
|= DBCR0_IAC4
| DBCR0_IDM
;
305 mtspr(SPRN_DBCR0
, dbcr0
);
306 mtspr(SPRN_DBCR1
, 0);
307 mtspr(SPRN_DBCR2
, 0);
310 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
314 if (vcpu
->guest_debug
.enabled
)
315 kvmppc_load_guest_debug_registers(vcpu
);
317 /* Mark every guest entry in the shadow TLB entry modified, so that they
318 * will all be reloaded on the next vcpu run (instead of being
319 * demand-faulted). */
320 for (i
= 0; i
<= tlb_44x_hwater
; i
++)
321 kvmppc_tlbe_set_modified(vcpu
, i
);
324 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
326 if (vcpu
->guest_debug
.enabled
)
327 kvmppc_restore_host_debug_state(vcpu
);
329 /* Don't leave guest TLB entries resident when being de-scheduled. */
330 /* XXX It would be nice to differentiate between heavyweight exit and
331 * sched_out here, since we could avoid the TLB flush for heavyweight
336 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu
*vcpu
,
337 struct kvm_debug_guest
*dbg
)
341 vcpu
->guest_debug
.enabled
= dbg
->enabled
;
342 if (vcpu
->guest_debug
.enabled
) {
343 for (i
=0; i
< ARRAY_SIZE(vcpu
->guest_debug
.bp
); i
++) {
344 if (dbg
->breakpoints
[i
].enabled
)
345 vcpu
->guest_debug
.bp
[i
] = dbg
->breakpoints
[i
].address
;
347 vcpu
->guest_debug
.bp
[i
] = 0;
354 static void kvmppc_complete_dcr_load(struct kvm_vcpu
*vcpu
,
357 u32
*gpr
= &vcpu
->arch
.gpr
[vcpu
->arch
.io_gpr
];
358 *gpr
= run
->dcr
.data
;
361 static void kvmppc_complete_mmio_load(struct kvm_vcpu
*vcpu
,
364 u32
*gpr
= &vcpu
->arch
.gpr
[vcpu
->arch
.io_gpr
];
366 if (run
->mmio
.len
> sizeof(*gpr
)) {
367 printk(KERN_ERR
"bad MMIO length: %d\n", run
->mmio
.len
);
371 if (vcpu
->arch
.mmio_is_bigendian
) {
372 switch (run
->mmio
.len
) {
373 case 4: *gpr
= *(u32
*)run
->mmio
.data
; break;
374 case 2: *gpr
= *(u16
*)run
->mmio
.data
; break;
375 case 1: *gpr
= *(u8
*)run
->mmio
.data
; break;
378 /* Convert BE data from userland back to LE. */
379 switch (run
->mmio
.len
) {
380 case 4: *gpr
= ld_le32((u32
*)run
->mmio
.data
); break;
381 case 2: *gpr
= ld_le16((u16
*)run
->mmio
.data
); break;
382 case 1: *gpr
= *(u8
*)run
->mmio
.data
; break;
387 int kvmppc_handle_load(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
388 unsigned int rt
, unsigned int bytes
, int is_bigendian
)
390 if (bytes
> sizeof(run
->mmio
.data
)) {
391 printk(KERN_ERR
"%s: bad MMIO length: %d\n", __func__
,
395 run
->mmio
.phys_addr
= vcpu
->arch
.paddr_accessed
;
396 run
->mmio
.len
= bytes
;
397 run
->mmio
.is_write
= 0;
399 vcpu
->arch
.io_gpr
= rt
;
400 vcpu
->arch
.mmio_is_bigendian
= is_bigendian
;
401 vcpu
->mmio_needed
= 1;
402 vcpu
->mmio_is_write
= 0;
404 return EMULATE_DO_MMIO
;
407 int kvmppc_handle_store(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
408 u32 val
, unsigned int bytes
, int is_bigendian
)
410 void *data
= run
->mmio
.data
;
412 if (bytes
> sizeof(run
->mmio
.data
)) {
413 printk(KERN_ERR
"%s: bad MMIO length: %d\n", __func__
,
417 run
->mmio
.phys_addr
= vcpu
->arch
.paddr_accessed
;
418 run
->mmio
.len
= bytes
;
419 run
->mmio
.is_write
= 1;
420 vcpu
->mmio_needed
= 1;
421 vcpu
->mmio_is_write
= 1;
423 /* Store the value at the lowest bytes in 'data'. */
426 case 4: *(u32
*)data
= val
; break;
427 case 2: *(u16
*)data
= val
; break;
428 case 1: *(u8
*)data
= val
; break;
431 /* Store LE value into 'data'. */
433 case 4: st_le32(data
, val
); break;
434 case 2: st_le16(data
, val
); break;
435 case 1: *(u8
*)data
= val
; break;
439 return EMULATE_DO_MMIO
;
442 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
449 if (vcpu
->sigset_active
)
450 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
452 if (vcpu
->mmio_needed
) {
453 if (!vcpu
->mmio_is_write
)
454 kvmppc_complete_mmio_load(vcpu
, run
);
455 vcpu
->mmio_needed
= 0;
456 } else if (vcpu
->arch
.dcr_needed
) {
457 if (!vcpu
->arch
.dcr_is_write
)
458 kvmppc_complete_dcr_load(vcpu
, run
);
459 vcpu
->arch
.dcr_needed
= 0;
462 kvmppc_check_and_deliver_interrupts(vcpu
);
466 r
= __kvmppc_vcpu_run(run
, vcpu
);
470 if (vcpu
->sigset_active
)
471 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
478 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
, struct kvm_interrupt
*irq
)
480 kvmppc_queue_exception(vcpu
, BOOKE_INTERRUPT_EXTERNAL
);
482 if (waitqueue_active(&vcpu
->wq
)) {
483 wake_up_interruptible(&vcpu
->wq
);
484 vcpu
->stat
.halt_wakeup
++;
490 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu
*vcpu
,
491 struct kvm_mp_state
*mp_state
)
496 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu
*vcpu
,
497 struct kvm_mp_state
*mp_state
)
502 long kvm_arch_vcpu_ioctl(struct file
*filp
,
503 unsigned int ioctl
, unsigned long arg
)
505 struct kvm_vcpu
*vcpu
= filp
->private_data
;
506 void __user
*argp
= (void __user
*)arg
;
510 case KVM_INTERRUPT
: {
511 struct kvm_interrupt irq
;
513 if (copy_from_user(&irq
, argp
, sizeof(irq
)))
515 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
526 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
, struct kvm_dirty_log
*log
)
531 long kvm_arch_vm_ioctl(struct file
*filp
,
532 unsigned int ioctl
, unsigned long arg
)
544 int kvm_arch_init(void *opaque
)
549 void kvm_arch_exit(void)