blob: a8000ce562b06c8c2084cf884613bd685112e2d9 [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
Alexander Grafaf8f38b2011-08-10 13:57:08 +020098int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
99{
100 int r = false;
101
102 /* We have to know what CPU to virtualize */
103 if (!vcpu->arch.pvr)
104 goto out;
105
106 /* PAPR only works with book3s_64 */
107 if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
108 goto out;
109
110#ifdef CONFIG_KVM_BOOK3S_64_HV
111 /* HV KVM can only do PAPR mode for now */
112 if (!vcpu->arch.papr_enabled)
113 goto out;
114#endif
115
116 r = true;
117
118out:
119 vcpu->arch.sane = r;
120 return r ? 0 : -EINVAL;
121}
122
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500123int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
124{
125 enum emulation_result er;
126 int r;
127
128 er = kvmppc_emulate_instruction(run, vcpu);
129 switch (er) {
130 case EMULATE_DONE:
131 /* Future optimization: only reload non-volatiles if they were
132 * actually modified. */
133 r = RESUME_GUEST_NV;
134 break;
135 case EMULATE_DO_MMIO:
136 run->exit_reason = KVM_EXIT_MMIO;
137 /* We must reload nonvolatiles because "update" load/store
138 * instructions modify register state. */
139 /* Future optimization: only reload non-volatiles if they were
140 * actually modified. */
141 r = RESUME_HOST_NV;
142 break;
143 case EMULATE_FAIL:
144 /* XXX Deliver Program interrupt to guest. */
145 printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__,
Alexander Grafc7f38f42010-04-16 00:11:40 +0200146 kvmppc_get_last_inst(vcpu));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500147 r = RESUME_HOST;
148 break;
149 default:
150 BUG();
151 }
152
153 return r;
154}
155
Alexander Graf10474ae2009-09-15 11:37:46 +0200156int kvm_arch_hardware_enable(void *garbage)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500157{
Alexander Graf10474ae2009-09-15 11:37:46 +0200158 return 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500159}
160
161void kvm_arch_hardware_disable(void *garbage)
162{
163}
164
165int kvm_arch_hardware_setup(void)
166{
167 return 0;
168}
169
170void kvm_arch_hardware_unsetup(void)
171{
172}
173
174void kvm_arch_check_processor_compat(void *rtn)
175{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600176 *(int *)rtn = kvmppc_core_check_processor_compat();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500177}
178
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100179int kvm_arch_init_vm(struct kvm *kvm)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500180{
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000181 return kvmppc_core_init_vm(kvm);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500182}
183
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100184void kvm_arch_destroy_vm(struct kvm *kvm)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500185{
186 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300187 struct kvm_vcpu *vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500188
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300189 kvm_for_each_vcpu(i, vcpu, kvm)
190 kvm_arch_vcpu_free(vcpu);
191
192 mutex_lock(&kvm->lock);
193 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
194 kvm->vcpus[i] = NULL;
195
196 atomic_set(&kvm->online_vcpus, 0);
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000197
198 kvmppc_core_destroy_vm(kvm);
199
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300200 mutex_unlock(&kvm->lock);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500201}
202
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800203void kvm_arch_sync_events(struct kvm *kvm)
204{
205}
206
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500207int kvm_dev_ioctl_check_extension(long ext)
208{
209 int r;
210
211 switch (ext) {
Scott Wood5ce941e2011-04-27 17:24:21 -0500212#ifdef CONFIG_BOOKE
213 case KVM_CAP_PPC_BOOKE_SREGS:
214#else
Alexander Grafe15a1132009-11-30 03:02:02 +0000215 case KVM_CAP_PPC_SEGSTATE:
Alexander Grafa15bd352011-08-08 17:17:09 +0200216 case KVM_CAP_PPC_HIOR:
Alexander Graf930b4122011-08-08 17:29:42 +0200217 case KVM_CAP_PPC_PAPR:
Scott Wood5ce941e2011-04-27 17:24:21 -0500218#endif
Alexander Graf18978762010-03-24 21:48:18 +0100219 case KVM_CAP_PPC_UNSET_IRQ:
Alexander Graf7b4203e2010-08-30 13:50:45 +0200220 case KVM_CAP_PPC_IRQ_LEVEL:
Alexander Graf71fbfd52010-03-24 21:48:29 +0100221 case KVM_CAP_ENABLE_CAP:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000222 r = 1;
223 break;
224#ifndef CONFIG_KVM_BOOK3S_64_HV
225 case KVM_CAP_PPC_PAIRED_SINGLES:
Alexander Grafad0a0482010-03-24 21:48:30 +0100226 case KVM_CAP_PPC_OSI:
Alexander Graf15711e92010-07-29 14:48:08 +0200227 case KVM_CAP_PPC_GET_PVINFO:
Alexander Grafe15a1132009-11-30 03:02:02 +0000228 r = 1;
229 break;
Laurent Vivier588968b2008-05-30 16:05:56 +0200230 case KVM_CAP_COALESCED_MMIO:
231 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
232 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000233#endif
David Gibson54738c02011-06-29 00:22:41 +0000234#ifdef CONFIG_KVM_BOOK3S_64_HV
235 case KVM_CAP_SPAPR_TCE:
236 r = 1;
237 break;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000238 case KVM_CAP_PPC_SMT:
239 r = threads_per_core;
240 break;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000241 case KVM_CAP_PPC_RMA:
242 r = 1;
Paul Mackerras9e368f22011-06-29 00:40:08 +0000243 /* PPC970 requires an RMA */
244 if (cpu_has_feature(CPU_FTR_ARCH_201))
245 r = 2;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000246 break;
David Gibson54738c02011-06-29 00:22:41 +0000247#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500248 default:
249 r = 0;
250 break;
251 }
252 return r;
253
254}
255
256long kvm_arch_dev_ioctl(struct file *filp,
257 unsigned int ioctl, unsigned long arg)
258{
259 return -EINVAL;
260}
261
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200262int kvm_arch_prepare_memory_region(struct kvm *kvm,
263 struct kvm_memory_slot *memslot,
264 struct kvm_memory_slot old,
265 struct kvm_userspace_memory_region *mem,
266 int user_alloc)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500267{
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000268 return kvmppc_core_prepare_memory_region(kvm, mem);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500269}
270
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200271void kvm_arch_commit_memory_region(struct kvm *kvm,
272 struct kvm_userspace_memory_region *mem,
273 struct kvm_memory_slot old,
274 int user_alloc)
275{
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000276 kvmppc_core_commit_memory_region(kvm, mem);
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200277}
278
279
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300280void kvm_arch_flush_shadow(struct kvm *kvm)
281{
282}
283
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500284struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
285{
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600286 struct kvm_vcpu *vcpu;
287 vcpu = kvmppc_core_vcpu_create(kvm, id);
Wei Yongjun06056bf2010-03-09 14:13:43 +0800288 if (!IS_ERR(vcpu))
289 kvmppc_create_vcpu_debugfs(vcpu, id);
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600290 return vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500291}
292
293void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
294{
Alexander Grafa5954052010-02-22 16:52:14 +0100295 /* Make sure we're not using the vcpu anymore */
296 hrtimer_cancel(&vcpu->arch.dec_timer);
297 tasklet_kill(&vcpu->arch.tasklet);
298
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600299 kvmppc_remove_vcpu_debugfs(vcpu);
Hollis Blancharddb93f572008-11-05 09:36:18 -0600300 kvmppc_core_vcpu_free(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500301}
302
303void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
304{
305 kvm_arch_vcpu_free(vcpu);
306}
307
308int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
309{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600310 return kvmppc_core_pending_dec(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500311}
312
313static void kvmppc_decrementer_func(unsigned long data)
314{
315 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
316
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600317 kvmppc_core_queue_dec(vcpu);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500318
319 if (waitqueue_active(&vcpu->wq)) {
320 wake_up_interruptible(&vcpu->wq);
321 vcpu->stat.halt_wakeup++;
322 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500323}
324
Alexander Graf544c6762009-11-02 12:02:31 +0000325/*
326 * low level hrtimer wake routine. Because this runs in hardirq context
327 * we schedule a tasklet to do the real work.
328 */
329enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
330{
331 struct kvm_vcpu *vcpu;
332
333 vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
334 tasklet_schedule(&vcpu->arch.tasklet);
335
336 return HRTIMER_NORESTART;
337}
338
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500339int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
340{
Alexander Graf544c6762009-11-02 12:02:31 +0000341 hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
342 tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
343 vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000344 vcpu->arch.dec_expires = ~(u64)0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500345
Bharat Bhushan09000ad2011-03-25 10:32:13 +0530346#ifdef CONFIG_KVM_EXIT_TIMING
347 mutex_init(&vcpu->arch.exit_timing_lock);
348#endif
349
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500350 return 0;
351}
352
353void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
354{
Hollis Blanchardecc09812009-01-03 16:22:59 -0600355 kvmppc_mmu_destroy(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500356}
357
358void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
359{
Scott Woodeab17672011-04-27 17:24:10 -0500360#ifdef CONFIG_BOOKE
361 /*
362 * vrsave (formerly usprg0) isn't used by Linux, but may
363 * be used by the guest.
364 *
365 * On non-booke this is associated with Altivec and
366 * is handled by code in book3s.c.
367 */
368 mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
369#endif
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600370 kvmppc_core_vcpu_load(vcpu, cpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000371 vcpu->cpu = smp_processor_id();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500372}
373
374void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
375{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600376 kvmppc_core_vcpu_put(vcpu);
Scott Woodeab17672011-04-27 17:24:10 -0500377#ifdef CONFIG_BOOKE
378 vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
379#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +0000380 vcpu->cpu = -1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500381}
382
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100383int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
Hollis Blanchardf5d09062009-01-04 13:51:09 -0600384 struct kvm_guest_debug *dbg)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500385{
Hollis Blanchardf5d09062009-01-04 13:51:09 -0600386 return -EINVAL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500387}
388
389static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
390 struct kvm_run *run)
391{
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100392 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500393}
394
395static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
396 struct kvm_run *run)
397{
Denis Kirjanov69b61832010-06-11 11:23:26 +0000398 u64 uninitialized_var(gpr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500399
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100400 if (run->mmio.len > sizeof(gpr)) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500401 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
402 return;
403 }
404
405 if (vcpu->arch.mmio_is_bigendian) {
406 switch (run->mmio.len) {
Alexander Grafb104d062010-02-19 11:00:29 +0100407 case 8: gpr = *(u64 *)run->mmio.data; break;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100408 case 4: gpr = *(u32 *)run->mmio.data; break;
409 case 2: gpr = *(u16 *)run->mmio.data; break;
410 case 1: gpr = *(u8 *)run->mmio.data; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500411 }
412 } else {
413 /* Convert BE data from userland back to LE. */
414 switch (run->mmio.len) {
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100415 case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
416 case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
417 case 1: gpr = *(u8 *)run->mmio.data; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500418 }
419 }
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100420
Alexander Graf3587d532010-02-19 11:00:30 +0100421 if (vcpu->arch.mmio_sign_extend) {
422 switch (run->mmio.len) {
423#ifdef CONFIG_PPC64
424 case 4:
425 gpr = (s64)(s32)gpr;
426 break;
427#endif
428 case 2:
429 gpr = (s64)(s16)gpr;
430 break;
431 case 1:
432 gpr = (s64)(s8)gpr;
433 break;
434 }
435 }
436
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100437 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
Alexander Grafb104d062010-02-19 11:00:29 +0100438
439 switch (vcpu->arch.io_gpr & KVM_REG_EXT_MASK) {
440 case KVM_REG_GPR:
441 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
442 break;
443 case KVM_REG_FPR:
444 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
445 break;
Alexander Graf287d5612010-04-01 15:33:21 +0200446#ifdef CONFIG_PPC_BOOK3S
Alexander Grafb104d062010-02-19 11:00:29 +0100447 case KVM_REG_QPR:
448 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
449 break;
450 case KVM_REG_FQPR:
451 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
452 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
453 break;
Alexander Graf287d5612010-04-01 15:33:21 +0200454#endif
Alexander Grafb104d062010-02-19 11:00:29 +0100455 default:
456 BUG();
457 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500458}
459
460int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
461 unsigned int rt, unsigned int bytes, int is_bigendian)
462{
463 if (bytes > sizeof(run->mmio.data)) {
464 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
465 run->mmio.len);
466 }
467
468 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
469 run->mmio.len = bytes;
470 run->mmio.is_write = 0;
471
472 vcpu->arch.io_gpr = rt;
473 vcpu->arch.mmio_is_bigendian = is_bigendian;
474 vcpu->mmio_needed = 1;
475 vcpu->mmio_is_write = 0;
Alexander Graf3587d532010-02-19 11:00:30 +0100476 vcpu->arch.mmio_sign_extend = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500477
478 return EMULATE_DO_MMIO;
479}
480
Alexander Graf3587d532010-02-19 11:00:30 +0100481/* Same as above, but sign extends */
482int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
483 unsigned int rt, unsigned int bytes, int is_bigendian)
484{
485 int r;
486
487 r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
488 vcpu->arch.mmio_sign_extend = 1;
489
490 return r;
491}
492
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500493int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
Alexander Grafb104d062010-02-19 11:00:29 +0100494 u64 val, unsigned int bytes, int is_bigendian)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500495{
496 void *data = run->mmio.data;
497
498 if (bytes > sizeof(run->mmio.data)) {
499 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
500 run->mmio.len);
501 }
502
503 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
504 run->mmio.len = bytes;
505 run->mmio.is_write = 1;
506 vcpu->mmio_needed = 1;
507 vcpu->mmio_is_write = 1;
508
509 /* Store the value at the lowest bytes in 'data'. */
510 if (is_bigendian) {
511 switch (bytes) {
Alexander Grafb104d062010-02-19 11:00:29 +0100512 case 8: *(u64 *)data = val; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500513 case 4: *(u32 *)data = val; break;
514 case 2: *(u16 *)data = val; break;
515 case 1: *(u8 *)data = val; break;
516 }
517 } else {
518 /* Store LE value into 'data'. */
519 switch (bytes) {
520 case 4: st_le32(data, val); break;
521 case 2: st_le16(data, val); break;
522 case 1: *(u8 *)data = val; break;
523 }
524 }
525
526 return EMULATE_DO_MMIO;
527}
528
529int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
530{
531 int r;
532 sigset_t sigsaved;
533
534 if (vcpu->sigset_active)
535 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
536
537 if (vcpu->mmio_needed) {
538 if (!vcpu->mmio_is_write)
539 kvmppc_complete_mmio_load(vcpu, run);
540 vcpu->mmio_needed = 0;
541 } else if (vcpu->arch.dcr_needed) {
542 if (!vcpu->arch.dcr_is_write)
543 kvmppc_complete_dcr_load(vcpu, run);
544 vcpu->arch.dcr_needed = 0;
Alexander Grafad0a0482010-03-24 21:48:30 +0100545 } else if (vcpu->arch.osi_needed) {
546 u64 *gprs = run->osi.gprs;
547 int i;
548
549 for (i = 0; i < 32; i++)
550 kvmppc_set_gpr(vcpu, i, gprs[i]);
551 vcpu->arch.osi_needed = 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000552 } else if (vcpu->arch.hcall_needed) {
553 int i;
554
555 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
556 for (i = 0; i < 9; ++i)
557 kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
558 vcpu->arch.hcall_needed = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500559 }
560
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600561 kvmppc_core_deliver_interrupts(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500562
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000563 r = kvmppc_vcpu_run(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500564
565 if (vcpu->sigset_active)
566 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
567
568 return r;
569}
570
571int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
572{
Alexander Graf18978762010-03-24 21:48:18 +0100573 if (irq->irq == KVM_INTERRUPT_UNSET)
574 kvmppc_core_dequeue_external(vcpu, irq);
575 else
576 kvmppc_core_queue_external(vcpu, irq);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500577
578 if (waitqueue_active(&vcpu->wq)) {
579 wake_up_interruptible(&vcpu->wq);
580 vcpu->stat.halt_wakeup++;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000581 } else if (vcpu->cpu != -1) {
582 smp_send_reschedule(vcpu->cpu);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500583 }
584
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500585 return 0;
586}
587
Alexander Graf71fbfd52010-03-24 21:48:29 +0100588static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
589 struct kvm_enable_cap *cap)
590{
591 int r;
592
593 if (cap->flags)
594 return -EINVAL;
595
596 switch (cap->cap) {
Alexander Grafad0a0482010-03-24 21:48:30 +0100597 case KVM_CAP_PPC_OSI:
598 r = 0;
599 vcpu->arch.osi_enabled = true;
600 break;
Alexander Graf930b4122011-08-08 17:29:42 +0200601 case KVM_CAP_PPC_PAPR:
602 r = 0;
603 vcpu->arch.papr_enabled = true;
604 break;
Alexander Graf71fbfd52010-03-24 21:48:29 +0100605 default:
606 r = -EINVAL;
607 break;
608 }
609
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200610 if (!r)
611 r = kvmppc_sanity_check(vcpu);
612
Alexander Graf71fbfd52010-03-24 21:48:29 +0100613 return r;
614}
615
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500616int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
617 struct kvm_mp_state *mp_state)
618{
619 return -EINVAL;
620}
621
622int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
623 struct kvm_mp_state *mp_state)
624{
625 return -EINVAL;
626}
627
628long kvm_arch_vcpu_ioctl(struct file *filp,
629 unsigned int ioctl, unsigned long arg)
630{
631 struct kvm_vcpu *vcpu = filp->private_data;
632 void __user *argp = (void __user *)arg;
633 long r;
634
Avi Kivity93736622010-05-13 12:35:17 +0300635 switch (ioctl) {
636 case KVM_INTERRUPT: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500637 struct kvm_interrupt irq;
638 r = -EFAULT;
639 if (copy_from_user(&irq, argp, sizeof(irq)))
Avi Kivity93736622010-05-13 12:35:17 +0300640 goto out;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500641 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity93736622010-05-13 12:35:17 +0300642 goto out;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500643 }
Avi Kivity19483d12010-05-13 12:30:43 +0300644
Alexander Graf71fbfd52010-03-24 21:48:29 +0100645 case KVM_ENABLE_CAP:
646 {
647 struct kvm_enable_cap cap;
648 r = -EFAULT;
649 if (copy_from_user(&cap, argp, sizeof(cap)))
650 goto out;
651 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
652 break;
653 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500654 default:
655 r = -EINVAL;
656 }
657
658out:
659 return r;
660}
661
Alexander Graf15711e92010-07-29 14:48:08 +0200662static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
663{
664 u32 inst_lis = 0x3c000000;
665 u32 inst_ori = 0x60000000;
666 u32 inst_nop = 0x60000000;
667 u32 inst_sc = 0x44000002;
668 u32 inst_imm_mask = 0xffff;
669
670 /*
671 * The hypercall to get into KVM from within guest context is as
672 * follows:
673 *
674 * lis r0, r0, KVM_SC_MAGIC_R0@h
675 * ori r0, KVM_SC_MAGIC_R0@l
676 * sc
677 * nop
678 */
679 pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask);
680 pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask);
681 pvinfo->hcall[2] = inst_sc;
682 pvinfo->hcall[3] = inst_nop;
683
684 return 0;
685}
686
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500687long kvm_arch_vm_ioctl(struct file *filp,
688 unsigned int ioctl, unsigned long arg)
689{
Alexander Graf15711e92010-07-29 14:48:08 +0200690 void __user *argp = (void __user *)arg;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500691 long r;
692
693 switch (ioctl) {
Alexander Graf15711e92010-07-29 14:48:08 +0200694 case KVM_PPC_GET_PVINFO: {
695 struct kvm_ppc_pvinfo pvinfo;
Vasiliy Kulikovd8cdddc2010-10-30 13:04:24 +0400696 memset(&pvinfo, 0, sizeof(pvinfo));
Alexander Graf15711e92010-07-29 14:48:08 +0200697 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
698 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
699 r = -EFAULT;
700 goto out;
701 }
702
703 break;
704 }
David Gibson54738c02011-06-29 00:22:41 +0000705#ifdef CONFIG_KVM_BOOK3S_64_HV
706 case KVM_CREATE_SPAPR_TCE: {
707 struct kvm_create_spapr_tce create_tce;
708 struct kvm *kvm = filp->private_data;
709
710 r = -EFAULT;
711 if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
712 goto out;
713 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
714 goto out;
715 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000716
717 case KVM_ALLOCATE_RMA: {
718 struct kvm *kvm = filp->private_data;
719 struct kvm_allocate_rma rma;
720
721 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
722 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
723 r = -EFAULT;
724 break;
725 }
David Gibson54738c02011-06-29 00:22:41 +0000726#endif /* CONFIG_KVM_BOOK3S_64_HV */
727
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500728 default:
Avi Kivity367e1312009-08-26 14:57:07 +0300729 r = -ENOTTY;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500730 }
731
Alexander Graf15711e92010-07-29 14:48:08 +0200732out:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500733 return r;
734}
735
736int kvm_arch_init(void *opaque)
737{
738 return 0;
739}
740
741void kvm_arch_exit(void)
742{
743}