blob: 72c506505fa4a3fe4c53075373f1ac6ec9d20c82 [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>
Alexander Graf544c6762009-11-02 12:02:31 +000026#include <linux/hrtimer.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050027#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050029#include <asm/cputable.h>
30#include <asm/uaccess.h>
31#include <asm/kvm_ppc.h>
Hollis Blanchard83aae4a2008-07-25 13:54:52 -050032#include <asm/tlbflush.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000033#include <asm/cputhreads.h>
Hollis Blanchard73e75b42008-12-02 15:51:57 -060034#include "timing.h"
Paul Mackerrasfad7b9b2008-12-23 14:57:26 +110035#include "../mm/mmu_decl.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050036
Marcelo Tosatti46f43c62009-06-18 11:47:27 -030037#define CREATE_TRACE_POINTS
38#include "trace.h"
39
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050040int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
41{
Paul Mackerrasde56a942011-06-29 00:21:34 +000042#ifndef CONFIG_KVM_BOOK3S_64_HV
Alexander Graf666e7252010-07-29 14:47:43 +020043 return !(v->arch.shared->msr & MSR_WE) ||
44 !!(v->arch.pending_exceptions);
Paul Mackerrasde56a942011-06-29 00:21:34 +000045#else
Paul Mackerrasa8606e22011-06-29 00:22:05 +000046 return !(v->arch.ceded) || !!(v->arch.pending_exceptions);
Paul Mackerrasde56a942011-06-29 00:21:34 +000047#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050048}
49
Alexander Graf2a342ed2010-07-29 14:47:48 +020050int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
51{
52 int nr = kvmppc_get_gpr(vcpu, 11);
53 int r;
54 unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
55 unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
56 unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
57 unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
58 unsigned long r2 = 0;
59
60 if (!(vcpu->arch.shared->msr & MSR_SF)) {
61 /* 32 bit mode */
62 param1 &= 0xffffffff;
63 param2 &= 0xffffffff;
64 param3 &= 0xffffffff;
65 param4 &= 0xffffffff;
66 }
67
68 switch (nr) {
Alexander Graf5fc87402010-07-29 14:47:55 +020069 case HC_VENDOR_KVM | KVM_HC_PPC_MAP_MAGIC_PAGE:
70 {
71 vcpu->arch.magic_page_pa = param1;
72 vcpu->arch.magic_page_ea = param2;
73
Alexander Grafdf1bfa22010-08-03 02:29:27 +020074 r2 = KVM_MAGIC_FEAT_SR;
Alexander Graf7508e162010-08-03 11:32:56 +020075
Alexander Graf5fc87402010-07-29 14:47:55 +020076 r = HC_EV_SUCCESS;
77 break;
78 }
Alexander Graf2a342ed2010-07-29 14:47:48 +020079 case HC_VENDOR_KVM | KVM_HC_FEATURES:
80 r = HC_EV_SUCCESS;
Scott Wooda4cd8b22011-06-14 18:34:41 -050081#if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500)
82 /* XXX Missing magic page on 44x */
Alexander Graf5fc87402010-07-29 14:47:55 +020083 r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
84#endif
Alexander Graf2a342ed2010-07-29 14:47:48 +020085
86 /* Second return value is in r4 */
Alexander Graf2a342ed2010-07-29 14:47:48 +020087 break;
88 default:
89 r = HC_EV_UNIMPLEMENTED;
90 break;
91 }
92
Alexander Graf7508e162010-08-03 11:32:56 +020093 kvmppc_set_gpr(vcpu, 4, r2);
94
Alexander Graf2a342ed2010-07-29 14:47:48 +020095 return r;
96}
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050097
98int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
99{
100 enum emulation_result er;
101 int r;
102
103 er = kvmppc_emulate_instruction(run, vcpu);
104 switch (er) {
105 case EMULATE_DONE:
106 /* Future optimization: only reload non-volatiles if they were
107 * actually modified. */
108 r = RESUME_GUEST_NV;
109 break;
110 case EMULATE_DO_MMIO:
111 run->exit_reason = KVM_EXIT_MMIO;
112 /* We must reload nonvolatiles because "update" load/store
113 * instructions modify register state. */
114 /* Future optimization: only reload non-volatiles if they were
115 * actually modified. */
116 r = RESUME_HOST_NV;
117 break;
118 case EMULATE_FAIL:
119 /* XXX Deliver Program interrupt to guest. */
120 printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__,
Alexander Grafc7f38f42010-04-16 00:11:40 +0200121 kvmppc_get_last_inst(vcpu));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500122 r = RESUME_HOST;
123 break;
124 default:
125 BUG();
126 }
127
128 return r;
129}
130
Alexander Graf10474ae2009-09-15 11:37:46 +0200131int kvm_arch_hardware_enable(void *garbage)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500132{
Alexander Graf10474ae2009-09-15 11:37:46 +0200133 return 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500134}
135
136void kvm_arch_hardware_disable(void *garbage)
137{
138}
139
140int kvm_arch_hardware_setup(void)
141{
142 return 0;
143}
144
145void kvm_arch_hardware_unsetup(void)
146{
147}
148
149void kvm_arch_check_processor_compat(void *rtn)
150{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600151 *(int *)rtn = kvmppc_core_check_processor_compat();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500152}
153
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100154int kvm_arch_init_vm(struct kvm *kvm)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500155{
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000156 return kvmppc_core_init_vm(kvm);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500157}
158
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100159void kvm_arch_destroy_vm(struct kvm *kvm)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500160{
161 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300162 struct kvm_vcpu *vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500163
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300164 kvm_for_each_vcpu(i, vcpu, kvm)
165 kvm_arch_vcpu_free(vcpu);
166
167 mutex_lock(&kvm->lock);
168 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
169 kvm->vcpus[i] = NULL;
170
171 atomic_set(&kvm->online_vcpus, 0);
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000172
173 kvmppc_core_destroy_vm(kvm);
174
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300175 mutex_unlock(&kvm->lock);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500176}
177
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800178void kvm_arch_sync_events(struct kvm *kvm)
179{
180}
181
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500182int kvm_dev_ioctl_check_extension(long ext)
183{
184 int r;
185
186 switch (ext) {
Scott Wood5ce941e2011-04-27 17:24:21 -0500187#ifdef CONFIG_BOOKE
188 case KVM_CAP_PPC_BOOKE_SREGS:
189#else
Alexander Grafe15a1132009-11-30 03:02:02 +0000190 case KVM_CAP_PPC_SEGSTATE:
Scott Wood5ce941e2011-04-27 17:24:21 -0500191#endif
Alexander Graf18978762010-03-24 21:48:18 +0100192 case KVM_CAP_PPC_UNSET_IRQ:
Alexander Graf7b4203e2010-08-30 13:50:45 +0200193 case KVM_CAP_PPC_IRQ_LEVEL:
Alexander Graf71fbfd52010-03-24 21:48:29 +0100194 case KVM_CAP_ENABLE_CAP:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000195 r = 1;
196 break;
197#ifndef CONFIG_KVM_BOOK3S_64_HV
198 case KVM_CAP_PPC_PAIRED_SINGLES:
Alexander Grafad0a0482010-03-24 21:48:30 +0100199 case KVM_CAP_PPC_OSI:
Alexander Graf15711e92010-07-29 14:48:08 +0200200 case KVM_CAP_PPC_GET_PVINFO:
Alexander Grafe15a1132009-11-30 03:02:02 +0000201 r = 1;
202 break;
Laurent Vivier588968b2008-05-30 16:05:56 +0200203 case KVM_CAP_COALESCED_MMIO:
204 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
205 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000206#endif
David Gibson54738c02011-06-29 00:22:41 +0000207#ifdef CONFIG_KVM_BOOK3S_64_HV
208 case KVM_CAP_SPAPR_TCE:
209 r = 1;
210 break;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000211 case KVM_CAP_PPC_SMT:
212 r = threads_per_core;
213 break;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000214 case KVM_CAP_PPC_RMA:
215 r = 1;
216 break;
David Gibson54738c02011-06-29 00:22:41 +0000217#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500218 default:
219 r = 0;
220 break;
221 }
222 return r;
223
224}
225
226long kvm_arch_dev_ioctl(struct file *filp,
227 unsigned int ioctl, unsigned long arg)
228{
229 return -EINVAL;
230}
231
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200232int kvm_arch_prepare_memory_region(struct kvm *kvm,
233 struct kvm_memory_slot *memslot,
234 struct kvm_memory_slot old,
235 struct kvm_userspace_memory_region *mem,
236 int user_alloc)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500237{
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000238 return kvmppc_core_prepare_memory_region(kvm, mem);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500239}
240
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200241void kvm_arch_commit_memory_region(struct kvm *kvm,
242 struct kvm_userspace_memory_region *mem,
243 struct kvm_memory_slot old,
244 int user_alloc)
245{
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000246 kvmppc_core_commit_memory_region(kvm, mem);
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200247}
248
249
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300250void kvm_arch_flush_shadow(struct kvm *kvm)
251{
252}
253
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500254struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
255{
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600256 struct kvm_vcpu *vcpu;
257 vcpu = kvmppc_core_vcpu_create(kvm, id);
Wei Yongjun06056bf2010-03-09 14:13:43 +0800258 if (!IS_ERR(vcpu))
259 kvmppc_create_vcpu_debugfs(vcpu, id);
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600260 return vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500261}
262
263void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
264{
Alexander Grafa5954052010-02-22 16:52:14 +0100265 /* Make sure we're not using the vcpu anymore */
266 hrtimer_cancel(&vcpu->arch.dec_timer);
267 tasklet_kill(&vcpu->arch.tasklet);
268
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600269 kvmppc_remove_vcpu_debugfs(vcpu);
Hollis Blancharddb93f572008-11-05 09:36:18 -0600270 kvmppc_core_vcpu_free(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500271}
272
273void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
274{
275 kvm_arch_vcpu_free(vcpu);
276}
277
278int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
279{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600280 return kvmppc_core_pending_dec(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500281}
282
283static void kvmppc_decrementer_func(unsigned long data)
284{
285 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
286
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600287 kvmppc_core_queue_dec(vcpu);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500288
289 if (waitqueue_active(&vcpu->wq)) {
290 wake_up_interruptible(&vcpu->wq);
291 vcpu->stat.halt_wakeup++;
292 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500293}
294
Alexander Graf544c6762009-11-02 12:02:31 +0000295/*
296 * low level hrtimer wake routine. Because this runs in hardirq context
297 * we schedule a tasklet to do the real work.
298 */
299enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
300{
301 struct kvm_vcpu *vcpu;
302
303 vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
304 tasklet_schedule(&vcpu->arch.tasklet);
305
306 return HRTIMER_NORESTART;
307}
308
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500309int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
310{
Alexander Graf544c6762009-11-02 12:02:31 +0000311 hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
312 tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
313 vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000314 vcpu->arch.dec_expires = ~(u64)0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500315
Bharat Bhushan09000ad2011-03-25 10:32:13 +0530316#ifdef CONFIG_KVM_EXIT_TIMING
317 mutex_init(&vcpu->arch.exit_timing_lock);
318#endif
319
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500320 return 0;
321}
322
323void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
324{
Hollis Blanchardecc09812009-01-03 16:22:59 -0600325 kvmppc_mmu_destroy(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500326}
327
328void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
329{
Scott Woodeab17672011-04-27 17:24:10 -0500330#ifdef CONFIG_BOOKE
331 /*
332 * vrsave (formerly usprg0) isn't used by Linux, but may
333 * be used by the guest.
334 *
335 * On non-booke this is associated with Altivec and
336 * is handled by code in book3s.c.
337 */
338 mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
339#endif
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600340 kvmppc_core_vcpu_load(vcpu, cpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000341 vcpu->cpu = smp_processor_id();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500342}
343
344void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
345{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600346 kvmppc_core_vcpu_put(vcpu);
Scott Woodeab17672011-04-27 17:24:10 -0500347#ifdef CONFIG_BOOKE
348 vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
349#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +0000350 vcpu->cpu = -1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500351}
352
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100353int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
Hollis Blanchardf5d09062009-01-04 13:51:09 -0600354 struct kvm_guest_debug *dbg)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500355{
Hollis Blanchardf5d09062009-01-04 13:51:09 -0600356 return -EINVAL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500357}
358
359static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
360 struct kvm_run *run)
361{
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100362 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500363}
364
365static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
366 struct kvm_run *run)
367{
Denis Kirjanov69b61832010-06-11 11:23:26 +0000368 u64 uninitialized_var(gpr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500369
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100370 if (run->mmio.len > sizeof(gpr)) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500371 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
372 return;
373 }
374
375 if (vcpu->arch.mmio_is_bigendian) {
376 switch (run->mmio.len) {
Alexander Grafb104d062010-02-19 11:00:29 +0100377 case 8: gpr = *(u64 *)run->mmio.data; break;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100378 case 4: gpr = *(u32 *)run->mmio.data; break;
379 case 2: gpr = *(u16 *)run->mmio.data; break;
380 case 1: gpr = *(u8 *)run->mmio.data; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500381 }
382 } else {
383 /* Convert BE data from userland back to LE. */
384 switch (run->mmio.len) {
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100385 case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
386 case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
387 case 1: gpr = *(u8 *)run->mmio.data; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500388 }
389 }
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100390
Alexander Graf3587d532010-02-19 11:00:30 +0100391 if (vcpu->arch.mmio_sign_extend) {
392 switch (run->mmio.len) {
393#ifdef CONFIG_PPC64
394 case 4:
395 gpr = (s64)(s32)gpr;
396 break;
397#endif
398 case 2:
399 gpr = (s64)(s16)gpr;
400 break;
401 case 1:
402 gpr = (s64)(s8)gpr;
403 break;
404 }
405 }
406
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100407 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
Alexander Grafb104d062010-02-19 11:00:29 +0100408
409 switch (vcpu->arch.io_gpr & KVM_REG_EXT_MASK) {
410 case KVM_REG_GPR:
411 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
412 break;
413 case KVM_REG_FPR:
414 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
415 break;
Alexander Graf287d5612010-04-01 15:33:21 +0200416#ifdef CONFIG_PPC_BOOK3S
Alexander Grafb104d062010-02-19 11:00:29 +0100417 case KVM_REG_QPR:
418 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
419 break;
420 case KVM_REG_FQPR:
421 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
422 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
423 break;
Alexander Graf287d5612010-04-01 15:33:21 +0200424#endif
Alexander Grafb104d062010-02-19 11:00:29 +0100425 default:
426 BUG();
427 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500428}
429
430int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
431 unsigned int rt, unsigned int bytes, int is_bigendian)
432{
433 if (bytes > sizeof(run->mmio.data)) {
434 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
435 run->mmio.len);
436 }
437
438 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
439 run->mmio.len = bytes;
440 run->mmio.is_write = 0;
441
442 vcpu->arch.io_gpr = rt;
443 vcpu->arch.mmio_is_bigendian = is_bigendian;
444 vcpu->mmio_needed = 1;
445 vcpu->mmio_is_write = 0;
Alexander Graf3587d532010-02-19 11:00:30 +0100446 vcpu->arch.mmio_sign_extend = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500447
448 return EMULATE_DO_MMIO;
449}
450
Alexander Graf3587d532010-02-19 11:00:30 +0100451/* Same as above, but sign extends */
452int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
453 unsigned int rt, unsigned int bytes, int is_bigendian)
454{
455 int r;
456
457 r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
458 vcpu->arch.mmio_sign_extend = 1;
459
460 return r;
461}
462
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500463int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
Alexander Grafb104d062010-02-19 11:00:29 +0100464 u64 val, unsigned int bytes, int is_bigendian)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500465{
466 void *data = run->mmio.data;
467
468 if (bytes > sizeof(run->mmio.data)) {
469 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
470 run->mmio.len);
471 }
472
473 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
474 run->mmio.len = bytes;
475 run->mmio.is_write = 1;
476 vcpu->mmio_needed = 1;
477 vcpu->mmio_is_write = 1;
478
479 /* Store the value at the lowest bytes in 'data'. */
480 if (is_bigendian) {
481 switch (bytes) {
Alexander Grafb104d062010-02-19 11:00:29 +0100482 case 8: *(u64 *)data = val; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500483 case 4: *(u32 *)data = val; break;
484 case 2: *(u16 *)data = val; break;
485 case 1: *(u8 *)data = val; break;
486 }
487 } else {
488 /* Store LE value into 'data'. */
489 switch (bytes) {
490 case 4: st_le32(data, val); break;
491 case 2: st_le16(data, val); break;
492 case 1: *(u8 *)data = val; break;
493 }
494 }
495
496 return EMULATE_DO_MMIO;
497}
498
499int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
500{
501 int r;
502 sigset_t sigsaved;
503
504 if (vcpu->sigset_active)
505 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
506
507 if (vcpu->mmio_needed) {
508 if (!vcpu->mmio_is_write)
509 kvmppc_complete_mmio_load(vcpu, run);
510 vcpu->mmio_needed = 0;
511 } else if (vcpu->arch.dcr_needed) {
512 if (!vcpu->arch.dcr_is_write)
513 kvmppc_complete_dcr_load(vcpu, run);
514 vcpu->arch.dcr_needed = 0;
Alexander Grafad0a0482010-03-24 21:48:30 +0100515 } else if (vcpu->arch.osi_needed) {
516 u64 *gprs = run->osi.gprs;
517 int i;
518
519 for (i = 0; i < 32; i++)
520 kvmppc_set_gpr(vcpu, i, gprs[i]);
521 vcpu->arch.osi_needed = 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000522 } else if (vcpu->arch.hcall_needed) {
523 int i;
524
525 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
526 for (i = 0; i < 9; ++i)
527 kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
528 vcpu->arch.hcall_needed = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500529 }
530
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600531 kvmppc_core_deliver_interrupts(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500532
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000533 r = kvmppc_vcpu_run(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500534
535 if (vcpu->sigset_active)
536 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
537
538 return r;
539}
540
541int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
542{
Alexander Graf18978762010-03-24 21:48:18 +0100543 if (irq->irq == KVM_INTERRUPT_UNSET)
544 kvmppc_core_dequeue_external(vcpu, irq);
545 else
546 kvmppc_core_queue_external(vcpu, irq);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500547
548 if (waitqueue_active(&vcpu->wq)) {
549 wake_up_interruptible(&vcpu->wq);
550 vcpu->stat.halt_wakeup++;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000551 } else if (vcpu->cpu != -1) {
552 smp_send_reschedule(vcpu->cpu);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500553 }
554
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500555 return 0;
556}
557
Alexander Graf71fbfd52010-03-24 21:48:29 +0100558static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
559 struct kvm_enable_cap *cap)
560{
561 int r;
562
563 if (cap->flags)
564 return -EINVAL;
565
566 switch (cap->cap) {
Alexander Grafad0a0482010-03-24 21:48:30 +0100567 case KVM_CAP_PPC_OSI:
568 r = 0;
569 vcpu->arch.osi_enabled = true;
570 break;
Alexander Graf71fbfd52010-03-24 21:48:29 +0100571 default:
572 r = -EINVAL;
573 break;
574 }
575
576 return r;
577}
578
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500579int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
580 struct kvm_mp_state *mp_state)
581{
582 return -EINVAL;
583}
584
585int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
586 struct kvm_mp_state *mp_state)
587{
588 return -EINVAL;
589}
590
591long kvm_arch_vcpu_ioctl(struct file *filp,
592 unsigned int ioctl, unsigned long arg)
593{
594 struct kvm_vcpu *vcpu = filp->private_data;
595 void __user *argp = (void __user *)arg;
596 long r;
597
Avi Kivity93736622010-05-13 12:35:17 +0300598 switch (ioctl) {
599 case KVM_INTERRUPT: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500600 struct kvm_interrupt irq;
601 r = -EFAULT;
602 if (copy_from_user(&irq, argp, sizeof(irq)))
Avi Kivity93736622010-05-13 12:35:17 +0300603 goto out;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500604 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity93736622010-05-13 12:35:17 +0300605 goto out;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500606 }
Avi Kivity19483d12010-05-13 12:30:43 +0300607
Alexander Graf71fbfd52010-03-24 21:48:29 +0100608 case KVM_ENABLE_CAP:
609 {
610 struct kvm_enable_cap cap;
611 r = -EFAULT;
612 if (copy_from_user(&cap, argp, sizeof(cap)))
613 goto out;
614 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
615 break;
616 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500617 default:
618 r = -EINVAL;
619 }
620
621out:
622 return r;
623}
624
Alexander Graf15711e92010-07-29 14:48:08 +0200625static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
626{
627 u32 inst_lis = 0x3c000000;
628 u32 inst_ori = 0x60000000;
629 u32 inst_nop = 0x60000000;
630 u32 inst_sc = 0x44000002;
631 u32 inst_imm_mask = 0xffff;
632
633 /*
634 * The hypercall to get into KVM from within guest context is as
635 * follows:
636 *
637 * lis r0, r0, KVM_SC_MAGIC_R0@h
638 * ori r0, KVM_SC_MAGIC_R0@l
639 * sc
640 * nop
641 */
642 pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask);
643 pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask);
644 pvinfo->hcall[2] = inst_sc;
645 pvinfo->hcall[3] = inst_nop;
646
647 return 0;
648}
649
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500650long kvm_arch_vm_ioctl(struct file *filp,
651 unsigned int ioctl, unsigned long arg)
652{
Alexander Graf15711e92010-07-29 14:48:08 +0200653 void __user *argp = (void __user *)arg;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500654 long r;
655
656 switch (ioctl) {
Alexander Graf15711e92010-07-29 14:48:08 +0200657 case KVM_PPC_GET_PVINFO: {
658 struct kvm_ppc_pvinfo pvinfo;
Vasiliy Kulikovd8cdddc2010-10-30 13:04:24 +0400659 memset(&pvinfo, 0, sizeof(pvinfo));
Alexander Graf15711e92010-07-29 14:48:08 +0200660 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
661 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
662 r = -EFAULT;
663 goto out;
664 }
665
666 break;
667 }
David Gibson54738c02011-06-29 00:22:41 +0000668#ifdef CONFIG_KVM_BOOK3S_64_HV
669 case KVM_CREATE_SPAPR_TCE: {
670 struct kvm_create_spapr_tce create_tce;
671 struct kvm *kvm = filp->private_data;
672
673 r = -EFAULT;
674 if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
675 goto out;
676 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
677 goto out;
678 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000679
680 case KVM_ALLOCATE_RMA: {
681 struct kvm *kvm = filp->private_data;
682 struct kvm_allocate_rma rma;
683
684 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
685 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
686 r = -EFAULT;
687 break;
688 }
David Gibson54738c02011-06-29 00:22:41 +0000689#endif /* CONFIG_KVM_BOOK3S_64_HV */
690
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500691 default:
Avi Kivity367e1312009-08-26 14:57:07 +0300692 r = -ENOTTY;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500693 }
694
Alexander Graf15711e92010-07-29 14:48:08 +0200695out:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500696 return r;
697}
698
699int kvm_arch_init(void *opaque)
700{
701 return 0;
702}
703
704void kvm_arch_exit(void)
705{
706}