blob: 0341391eff1266012dac9652a62b7833a764bd6f [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
Marcelo Tosatti46f43c62009-06-18 11:47:27 -030034#define CREATE_TRACE_POINTS
35#include "trace.h"
36
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050037gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
38{
39 return gfn;
40}
41
42int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
43{
Hollis Blanchard45c5eb62008-04-25 17:55:49 -050044 return !!(v->arch.pending_exceptions);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050045}
46
Gleb Natapov78646122009-03-23 12:12:11 +020047int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
48{
49 /* do real check here */
50 return 1;
51}
52
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050053int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
54{
Hollis Blanchard45c5eb62008-04-25 17:55:49 -050055 return !(v->arch.msr & MSR_WE);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050056}
57
58
59int 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
92void kvm_arch_hardware_enable(void *garbage)
93{
94}
95
96void kvm_arch_hardware_disable(void *garbage)
97{
98}
99
100int kvm_arch_hardware_setup(void)
101{
102 return 0;
103}
104
105void kvm_arch_hardware_unsetup(void)
106{
107}
108
109void kvm_arch_check_processor_compat(void *rtn)
110{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600111 *(int *)rtn = kvmppc_core_check_processor_compat();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500112}
113
114struct 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
125static void kvmppc_free_vcpus(struct kvm *kvm)
126{
127 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300128 struct kvm_vcpu *vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500129
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300130 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);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500139}
140
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800141void kvm_arch_sync_events(struct kvm *kvm)
142{
143}
144
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500145void kvm_arch_destroy_vm(struct kvm *kvm)
146{
147 kvmppc_free_vcpus(kvm);
148 kvm_free_physmem(kvm);
149 kfree(kvm);
150}
151
152int kvm_dev_ioctl_check_extension(long ext)
153{
154 int r;
155
156 switch (ext) {
Laurent Vivier588968b2008-05-30 16:05:56 +0200157 case KVM_CAP_COALESCED_MMIO:
158 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
159 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500160 default:
161 r = 0;
162 break;
163 }
164 return r;
165
166}
167
168long kvm_arch_dev_ioctl(struct file *filp,
169 unsigned int ioctl, unsigned long arg)
170{
171 return -EINVAL;
172}
173
174int 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
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300182void kvm_arch_flush_shadow(struct kvm *kvm)
183{
184}
185
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500186struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
187{
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600188 struct kvm_vcpu *vcpu;
189 vcpu = kvmppc_core_vcpu_create(kvm, id);
190 kvmppc_create_vcpu_debugfs(vcpu, id);
191 return vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500192}
193
194void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
195{
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600196 kvmppc_remove_vcpu_debugfs(vcpu);
Hollis Blancharddb93f572008-11-05 09:36:18 -0600197 kvmppc_core_vcpu_free(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500198}
199
200void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
201{
202 kvm_arch_vcpu_free(vcpu);
203}
204
205int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
206{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600207 return kvmppc_core_pending_dec(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500208}
209
210static void kvmppc_decrementer_func(unsigned long data)
211{
212 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
213
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600214 kvmppc_core_queue_dec(vcpu);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500215
216 if (waitqueue_active(&vcpu->wq)) {
217 wake_up_interruptible(&vcpu->wq);
218 vcpu->stat.halt_wakeup++;
219 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500220}
221
222int 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
230void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
231{
Hollis Blanchardecc09812009-01-03 16:22:59 -0600232 kvmppc_mmu_destroy(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500233}
234
235void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
236{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600237 kvmppc_core_vcpu_load(vcpu, cpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500238}
239
240void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
241{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600242 kvmppc_core_vcpu_put(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500243}
244
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100245int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
Hollis Blanchardf5d09062009-01-04 13:51:09 -0600246 struct kvm_guest_debug *dbg)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500247{
Hollis Blanchardf5d09062009-01-04 13:51:09 -0600248 return -EINVAL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500249}
250
251static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
252 struct kvm_run *run)
253{
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -0600254 ulong *gpr = &vcpu->arch.gpr[vcpu->arch.io_gpr];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500255 *gpr = run->dcr.data;
256}
257
258static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
259 struct kvm_run *run)
260{
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -0600261 ulong *gpr = &vcpu->arch.gpr[vcpu->arch.io_gpr];
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500262
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
284int 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
304int 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
339int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
340{
341 int r;
342 sigset_t sigsaved;
343
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500344 vcpu_load(vcpu);
345
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500346 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
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600359 kvmppc_core_deliver_interrupts(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500360
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
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500370 vcpu_put(vcpu);
371
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500372 return r;
373}
374
375int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
376{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600377 kvmppc_core_queue_external(vcpu, irq);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500378
379 if (waitqueue_active(&vcpu->wq)) {
380 wake_up_interruptible(&vcpu->wq);
381 vcpu->stat.halt_wakeup++;
382 }
383
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500384 return 0;
385}
386
387int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
388 struct kvm_mp_state *mp_state)
389{
390 return -EINVAL;
391}
392
393int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
394 struct kvm_mp_state *mp_state)
395{
396 return -EINVAL;
397}
398
399long 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
419out:
420 return r;
421}
422
423int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
424{
425 return -ENOTSUPP;
426}
427
428long 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
441int kvm_arch_init(void *opaque)
442{
443 return 0;
444}
445
446void kvm_arch_exit(void)
447{
448}