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>
32 #include "../mm/mmu_decl.h"
34 #define CREATE_TRACE_POINTS
37 gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
42 int kvm_cpu_has_interrupt(struct kvm_vcpu
*v
)
44 return !!(v
->arch
.pending_exceptions
);
47 int kvm_arch_interrupt_allowed(struct kvm_vcpu
*vcpu
)
49 /* do real check here */
53 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*v
)
55 return !(v
->arch
.msr
& MSR_WE
);
59 int kvmppc_emulate_mmio(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
)
61 enum emulation_result er
;
64 er
= kvmppc_emulate_instruction(run
, vcpu
);
67 /* Future optimization: only reload non-volatiles if they were
68 * actually modified. */
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. */
80 /* XXX Deliver Program interrupt to guest. */
81 printk(KERN_EMERG
"%s: emulation failed (%08x)\n", __func__
,
82 vcpu
->arch
.last_inst
);
92 void kvm_arch_hardware_enable(void *garbage
)
96 void kvm_arch_hardware_disable(void *garbage
)
100 int kvm_arch_hardware_setup(void)
105 void kvm_arch_hardware_unsetup(void)
109 void kvm_arch_check_processor_compat(void *rtn
)
111 *(int *)rtn
= kvmppc_core_check_processor_compat();
114 struct kvm
*kvm_arch_create_vm(void)
118 kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
120 return ERR_PTR(-ENOMEM
);
125 static void kvmppc_free_vcpus(struct kvm
*kvm
)
128 struct kvm_vcpu
*vcpu
;
130 kvm_for_each_vcpu(i
, vcpu
, kvm
)
131 kvm_arch_vcpu_free(vcpu
);
133 mutex_lock(&kvm
->lock
);
134 for (i
= 0; i
< atomic_read(&kvm
->online_vcpus
); i
++)
135 kvm
->vcpus
[i
] = NULL
;
137 atomic_set(&kvm
->online_vcpus
, 0);
138 mutex_unlock(&kvm
->lock
);
141 void kvm_arch_sync_events(struct kvm
*kvm
)
145 void kvm_arch_destroy_vm(struct kvm
*kvm
)
147 kvmppc_free_vcpus(kvm
);
148 kvm_free_physmem(kvm
);
152 int kvm_dev_ioctl_check_extension(long ext
)
157 case KVM_CAP_COALESCED_MMIO
:
158 r
= KVM_COALESCED_MMIO_PAGE_OFFSET
;
168 long kvm_arch_dev_ioctl(struct file
*filp
,
169 unsigned int ioctl
, unsigned long arg
)
174 int kvm_arch_set_memory_region(struct kvm
*kvm
,
175 struct kvm_userspace_memory_region
*mem
,
176 struct kvm_memory_slot old
,
182 void kvm_arch_flush_shadow(struct kvm
*kvm
)
186 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
, unsigned int id
)
188 struct kvm_vcpu
*vcpu
;
189 vcpu
= kvmppc_core_vcpu_create(kvm
, id
);
190 kvmppc_create_vcpu_debugfs(vcpu
, id
);
194 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
196 kvmppc_remove_vcpu_debugfs(vcpu
);
197 kvmppc_core_vcpu_free(vcpu
);
200 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
202 kvm_arch_vcpu_free(vcpu
);
205 int kvm_cpu_has_pending_timer(struct kvm_vcpu
*vcpu
)
207 return kvmppc_core_pending_dec(vcpu
);
210 static void kvmppc_decrementer_func(unsigned long data
)
212 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*)data
;
214 kvmppc_core_queue_dec(vcpu
);
216 if (waitqueue_active(&vcpu
->wq
)) {
217 wake_up_interruptible(&vcpu
->wq
);
218 vcpu
->stat
.halt_wakeup
++;
222 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
224 setup_timer(&vcpu
->arch
.dec_timer
, kvmppc_decrementer_func
,
225 (unsigned long)vcpu
);
230 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
232 kvmppc_mmu_destroy(vcpu
);
235 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
237 kvmppc_core_vcpu_load(vcpu
, cpu
);
240 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
242 kvmppc_core_vcpu_put(vcpu
);
245 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu
*vcpu
,
246 struct kvm_guest_debug
*dbg
)
251 static void kvmppc_complete_dcr_load(struct kvm_vcpu
*vcpu
,
254 ulong
*gpr
= &vcpu
->arch
.gpr
[vcpu
->arch
.io_gpr
];
255 *gpr
= run
->dcr
.data
;
258 static void kvmppc_complete_mmio_load(struct kvm_vcpu
*vcpu
,
261 ulong
*gpr
= &vcpu
->arch
.gpr
[vcpu
->arch
.io_gpr
];
263 if (run
->mmio
.len
> sizeof(*gpr
)) {
264 printk(KERN_ERR
"bad MMIO length: %d\n", run
->mmio
.len
);
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;
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;
284 int kvmppc_handle_load(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
285 unsigned int rt
, unsigned int bytes
, int is_bigendian
)
287 if (bytes
> sizeof(run
->mmio
.data
)) {
288 printk(KERN_ERR
"%s: bad MMIO length: %d\n", __func__
,
292 run
->mmio
.phys_addr
= vcpu
->arch
.paddr_accessed
;
293 run
->mmio
.len
= bytes
;
294 run
->mmio
.is_write
= 0;
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;
301 return EMULATE_DO_MMIO
;
304 int kvmppc_handle_store(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
305 u32 val
, unsigned int bytes
, int is_bigendian
)
307 void *data
= run
->mmio
.data
;
309 if (bytes
> sizeof(run
->mmio
.data
)) {
310 printk(KERN_ERR
"%s: bad MMIO length: %d\n", __func__
,
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;
320 /* Store the value at the lowest bytes in 'data'. */
323 case 4: *(u32
*)data
= val
; break;
324 case 2: *(u16
*)data
= val
; break;
325 case 1: *(u8
*)data
= val
; break;
328 /* Store LE value into 'data'. */
330 case 4: st_le32(data
, val
); break;
331 case 2: st_le16(data
, val
); break;
332 case 1: *(u8
*)data
= val
; break;
336 return EMULATE_DO_MMIO
;
339 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
346 if (vcpu
->sigset_active
)
347 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
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;
359 kvmppc_core_deliver_interrupts(vcpu
);
363 r
= __kvmppc_vcpu_run(run
, vcpu
);
367 if (vcpu
->sigset_active
)
368 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
375 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
, struct kvm_interrupt
*irq
)
377 kvmppc_core_queue_external(vcpu
, irq
);
379 if (waitqueue_active(&vcpu
->wq
)) {
380 wake_up_interruptible(&vcpu
->wq
);
381 vcpu
->stat
.halt_wakeup
++;
387 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu
*vcpu
,
388 struct kvm_mp_state
*mp_state
)
393 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu
*vcpu
,
394 struct kvm_mp_state
*mp_state
)
399 long kvm_arch_vcpu_ioctl(struct file
*filp
,
400 unsigned int ioctl
, unsigned long arg
)
402 struct kvm_vcpu
*vcpu
= filp
->private_data
;
403 void __user
*argp
= (void __user
*)arg
;
407 case KVM_INTERRUPT
: {
408 struct kvm_interrupt irq
;
410 if (copy_from_user(&irq
, argp
, sizeof(irq
)))
412 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
423 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
, struct kvm_dirty_log
*log
)
428 long kvm_arch_vm_ioctl(struct file
*filp
,
429 unsigned int ioctl
, unsigned long arg
)
441 int kvm_arch_init(void *opaque
)
446 void kvm_arch_exit(void)