blob: b5e9046462fd958c8d4035fd6c014b85e17ede5b [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>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050024#include <linux/vmalloc.h>
Alexander Graf544c6762009-11-02 12:02:31 +000025#include <linux/hrtimer.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050026#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050028#include <asm/cputable.h>
29#include <asm/uaccess.h>
30#include <asm/kvm_ppc.h>
Hollis Blanchard83aae4a2008-07-25 13:54:52 -050031#include <asm/tlbflush.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000032#include <asm/cputhreads.h>
Hollis Blanchard73e75b42008-12-02 15:51:57 -060033#include "timing.h"
Paul Mackerrasfad7b9b2008-12-23 14:57:26 +110034#include "../mm/mmu_decl.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050035
Marcelo Tosatti46f43c62009-06-18 11:47:27 -030036#define CREATE_TRACE_POINTS
37#include "trace.h"
38
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050039int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
40{
Alexander Graf666e7252010-07-29 14:47:43 +020041 return !(v->arch.shared->msr & MSR_WE) ||
Scott Wooddfd4d472011-11-17 12:39:59 +000042 !!(v->arch.pending_exceptions) ||
43 v->requests;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050044}
45
Christoffer Dallb6d33832012-03-08 16:44:24 -050046int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
47{
48 return 1;
49}
50
Alexander Graf2a342ed2010-07-29 14:47:48 +020051int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
52{
53 int nr = kvmppc_get_gpr(vcpu, 11);
54 int r;
55 unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
56 unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
57 unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
58 unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
59 unsigned long r2 = 0;
60
61 if (!(vcpu->arch.shared->msr & MSR_SF)) {
62 /* 32 bit mode */
63 param1 &= 0xffffffff;
64 param2 &= 0xffffffff;
65 param3 &= 0xffffffff;
66 param4 &= 0xffffffff;
67 }
68
69 switch (nr) {
Alexander Graf5fc87402010-07-29 14:47:55 +020070 case HC_VENDOR_KVM | KVM_HC_PPC_MAP_MAGIC_PAGE:
71 {
72 vcpu->arch.magic_page_pa = param1;
73 vcpu->arch.magic_page_ea = param2;
74
Scott Woodb5904972011-11-08 18:23:30 -060075 r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
Alexander Graf7508e162010-08-03 11:32:56 +020076
Alexander Graf5fc87402010-07-29 14:47:55 +020077 r = HC_EV_SUCCESS;
78 break;
79 }
Alexander Graf2a342ed2010-07-29 14:47:48 +020080 case HC_VENDOR_KVM | KVM_HC_FEATURES:
81 r = HC_EV_SUCCESS;
Scott Wooda4cd8b22011-06-14 18:34:41 -050082#if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500)
83 /* XXX Missing magic page on 44x */
Alexander Graf5fc87402010-07-29 14:47:55 +020084 r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
85#endif
Alexander Graf2a342ed2010-07-29 14:47:48 +020086
87 /* Second return value is in r4 */
Alexander Graf2a342ed2010-07-29 14:47:48 +020088 break;
89 default:
90 r = HC_EV_UNIMPLEMENTED;
91 break;
92 }
93
Alexander Graf7508e162010-08-03 11:32:56 +020094 kvmppc_set_gpr(vcpu, 4, r2);
95
Alexander Graf2a342ed2010-07-29 14:47:48 +020096 return r;
97}
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050098
Alexander Grafaf8f38b2011-08-10 13:57:08 +020099int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
100{
101 int r = false;
102
103 /* We have to know what CPU to virtualize */
104 if (!vcpu->arch.pvr)
105 goto out;
106
107 /* PAPR only works with book3s_64 */
108 if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
109 goto out;
110
111#ifdef CONFIG_KVM_BOOK3S_64_HV
112 /* HV KVM can only do PAPR mode for now */
113 if (!vcpu->arch.papr_enabled)
114 goto out;
115#endif
116
117 r = true;
118
119out:
120 vcpu->arch.sane = r;
121 return r ? 0 : -EINVAL;
122}
123
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500124int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
125{
126 enum emulation_result er;
127 int r;
128
129 er = kvmppc_emulate_instruction(run, vcpu);
130 switch (er) {
131 case EMULATE_DONE:
132 /* Future optimization: only reload non-volatiles if they were
133 * actually modified. */
134 r = RESUME_GUEST_NV;
135 break;
136 case EMULATE_DO_MMIO:
137 run->exit_reason = KVM_EXIT_MMIO;
138 /* We must reload nonvolatiles because "update" load/store
139 * instructions modify register state. */
140 /* Future optimization: only reload non-volatiles if they were
141 * actually modified. */
142 r = RESUME_HOST_NV;
143 break;
144 case EMULATE_FAIL:
145 /* XXX Deliver Program interrupt to guest. */
146 printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__,
Alexander Grafc7f38f42010-04-16 00:11:40 +0200147 kvmppc_get_last_inst(vcpu));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500148 r = RESUME_HOST;
149 break;
150 default:
151 BUG();
152 }
153
154 return r;
155}
156
Alexander Graf10474ae2009-09-15 11:37:46 +0200157int kvm_arch_hardware_enable(void *garbage)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500158{
Alexander Graf10474ae2009-09-15 11:37:46 +0200159 return 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500160}
161
162void kvm_arch_hardware_disable(void *garbage)
163{
164}
165
166int kvm_arch_hardware_setup(void)
167{
168 return 0;
169}
170
171void kvm_arch_hardware_unsetup(void)
172{
173}
174
175void kvm_arch_check_processor_compat(void *rtn)
176{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600177 *(int *)rtn = kvmppc_core_check_processor_compat();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500178}
179
Carsten Ottee08b9632012-01-04 10:25:20 +0100180int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500181{
Carsten Ottee08b9632012-01-04 10:25:20 +0100182 if (type)
183 return -EINVAL;
184
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000185 return kvmppc_core_init_vm(kvm);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500186}
187
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100188void kvm_arch_destroy_vm(struct kvm *kvm)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500189{
190 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300191 struct kvm_vcpu *vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500192
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300193 kvm_for_each_vcpu(i, vcpu, kvm)
194 kvm_arch_vcpu_free(vcpu);
195
196 mutex_lock(&kvm->lock);
197 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
198 kvm->vcpus[i] = NULL;
199
200 atomic_set(&kvm->online_vcpus, 0);
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000201
202 kvmppc_core_destroy_vm(kvm);
203
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300204 mutex_unlock(&kvm->lock);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500205}
206
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800207void kvm_arch_sync_events(struct kvm *kvm)
208{
209}
210
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500211int kvm_dev_ioctl_check_extension(long ext)
212{
213 int r;
214
215 switch (ext) {
Scott Wood5ce941e2011-04-27 17:24:21 -0500216#ifdef CONFIG_BOOKE
217 case KVM_CAP_PPC_BOOKE_SREGS:
218#else
Alexander Grafe15a1132009-11-30 03:02:02 +0000219 case KVM_CAP_PPC_SEGSTATE:
Alexander Graf1022fc32011-09-14 21:45:23 +0200220 case KVM_CAP_PPC_HIOR:
Alexander Graf930b4122011-08-08 17:29:42 +0200221 case KVM_CAP_PPC_PAPR:
Scott Wood5ce941e2011-04-27 17:24:21 -0500222#endif
Alexander Graf18978762010-03-24 21:48:18 +0100223 case KVM_CAP_PPC_UNSET_IRQ:
Alexander Graf7b4203e2010-08-30 13:50:45 +0200224 case KVM_CAP_PPC_IRQ_LEVEL:
Alexander Graf71fbfd52010-03-24 21:48:29 +0100225 case KVM_CAP_ENABLE_CAP:
Alexander Grafe24ed812011-09-14 10:02:41 +0200226 case KVM_CAP_ONE_REG:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000227 r = 1;
228 break;
229#ifndef CONFIG_KVM_BOOK3S_64_HV
230 case KVM_CAP_PPC_PAIRED_SINGLES:
Alexander Grafad0a0482010-03-24 21:48:30 +0100231 case KVM_CAP_PPC_OSI:
Alexander Graf15711e92010-07-29 14:48:08 +0200232 case KVM_CAP_PPC_GET_PVINFO:
Scott Wooddc83b8b2011-08-18 15:25:21 -0500233#ifdef CONFIG_KVM_E500
234 case KVM_CAP_SW_TLB:
235#endif
Alexander Grafe15a1132009-11-30 03:02:02 +0000236 r = 1;
237 break;
Laurent Vivier588968b2008-05-30 16:05:56 +0200238 case KVM_CAP_COALESCED_MMIO:
239 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
240 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000241#endif
David Gibson54738c02011-06-29 00:22:41 +0000242#ifdef CONFIG_KVM_BOOK3S_64_HV
243 case KVM_CAP_SPAPR_TCE:
244 r = 1;
245 break;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000246 case KVM_CAP_PPC_SMT:
247 r = threads_per_core;
248 break;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000249 case KVM_CAP_PPC_RMA:
250 r = 1;
Paul Mackerras9e368f22011-06-29 00:40:08 +0000251 /* PPC970 requires an RMA */
252 if (cpu_has_feature(CPU_FTR_ARCH_201))
253 r = 2;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000254 break;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000255 case KVM_CAP_SYNC_MMU:
256 r = cpu_has_feature(CPU_FTR_ARCH_206) ? 1 : 0;
257 break;
David Gibson54738c02011-06-29 00:22:41 +0000258#endif
Matt Evansb5434032011-12-07 16:55:57 +0000259 case KVM_CAP_NR_VCPUS:
260 /*
261 * Recommending a number of CPUs is somewhat arbitrary; we
262 * return the number of present CPUs for -HV (since a host
263 * will have secondary threads "offline"), and for other KVM
264 * implementations just count online CPUs.
265 */
266#ifdef CONFIG_KVM_BOOK3S_64_HV
267 r = num_present_cpus();
268#else
269 r = num_online_cpus();
270#endif
271 break;
272 case KVM_CAP_MAX_VCPUS:
273 r = KVM_MAX_VCPUS;
274 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500275 default:
276 r = 0;
277 break;
278 }
279 return r;
280
281}
282
283long kvm_arch_dev_ioctl(struct file *filp,
284 unsigned int ioctl, unsigned long arg)
285{
286 return -EINVAL;
287}
288
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900289void kvm_arch_free_memslot(struct kvm_memory_slot *free,
290 struct kvm_memory_slot *dont)
291{
292}
293
294int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
295{
296 return 0;
297}
298
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200299int kvm_arch_prepare_memory_region(struct kvm *kvm,
300 struct kvm_memory_slot *memslot,
301 struct kvm_memory_slot old,
302 struct kvm_userspace_memory_region *mem,
303 int user_alloc)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500304{
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000305 return kvmppc_core_prepare_memory_region(kvm, mem);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500306}
307
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200308void kvm_arch_commit_memory_region(struct kvm *kvm,
309 struct kvm_userspace_memory_region *mem,
310 struct kvm_memory_slot old,
311 int user_alloc)
312{
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000313 kvmppc_core_commit_memory_region(kvm, mem);
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200314}
315
316
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300317void kvm_arch_flush_shadow(struct kvm *kvm)
318{
319}
320
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500321struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
322{
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600323 struct kvm_vcpu *vcpu;
324 vcpu = kvmppc_core_vcpu_create(kvm, id);
Matt Evans03cdab52011-12-06 21:19:42 +0000325 if (!IS_ERR(vcpu)) {
326 vcpu->arch.wqp = &vcpu->wq;
Wei Yongjun06056bf2010-03-09 14:13:43 +0800327 kvmppc_create_vcpu_debugfs(vcpu, id);
Matt Evans03cdab52011-12-06 21:19:42 +0000328 }
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600329 return vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500330}
331
332void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
333{
Alexander Grafa5954052010-02-22 16:52:14 +0100334 /* Make sure we're not using the vcpu anymore */
335 hrtimer_cancel(&vcpu->arch.dec_timer);
336 tasklet_kill(&vcpu->arch.tasklet);
337
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600338 kvmppc_remove_vcpu_debugfs(vcpu);
Hollis Blancharddb93f572008-11-05 09:36:18 -0600339 kvmppc_core_vcpu_free(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500340}
341
342void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
343{
344 kvm_arch_vcpu_free(vcpu);
345}
346
347int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
348{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600349 return kvmppc_core_pending_dec(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500350}
351
Alexander Graf544c6762009-11-02 12:02:31 +0000352/*
353 * low level hrtimer wake routine. Because this runs in hardirq context
354 * we schedule a tasklet to do the real work.
355 */
356enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
357{
358 struct kvm_vcpu *vcpu;
359
360 vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
361 tasklet_schedule(&vcpu->arch.tasklet);
362
363 return HRTIMER_NORESTART;
364}
365
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500366int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
367{
Alexander Graf544c6762009-11-02 12:02:31 +0000368 hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
369 tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
370 vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000371 vcpu->arch.dec_expires = ~(u64)0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500372
Bharat Bhushan09000ad2011-03-25 10:32:13 +0530373#ifdef CONFIG_KVM_EXIT_TIMING
374 mutex_init(&vcpu->arch.exit_timing_lock);
375#endif
376
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500377 return 0;
378}
379
380void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
381{
Hollis Blanchardecc09812009-01-03 16:22:59 -0600382 kvmppc_mmu_destroy(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500383}
384
385void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
386{
Scott Woodeab17672011-04-27 17:24:10 -0500387#ifdef CONFIG_BOOKE
388 /*
389 * vrsave (formerly usprg0) isn't used by Linux, but may
390 * be used by the guest.
391 *
392 * On non-booke this is associated with Altivec and
393 * is handled by code in book3s.c.
394 */
395 mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
396#endif
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600397 kvmppc_core_vcpu_load(vcpu, cpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000398 vcpu->cpu = smp_processor_id();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500399}
400
401void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
402{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600403 kvmppc_core_vcpu_put(vcpu);
Scott Woodeab17672011-04-27 17:24:10 -0500404#ifdef CONFIG_BOOKE
405 vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
406#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +0000407 vcpu->cpu = -1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500408}
409
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100410int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
Hollis Blanchardf5d09062009-01-04 13:51:09 -0600411 struct kvm_guest_debug *dbg)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500412{
Hollis Blanchardf5d09062009-01-04 13:51:09 -0600413 return -EINVAL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500414}
415
416static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
417 struct kvm_run *run)
418{
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100419 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500420}
421
422static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
423 struct kvm_run *run)
424{
Denis Kirjanov69b61832010-06-11 11:23:26 +0000425 u64 uninitialized_var(gpr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500426
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100427 if (run->mmio.len > sizeof(gpr)) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500428 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
429 return;
430 }
431
432 if (vcpu->arch.mmio_is_bigendian) {
433 switch (run->mmio.len) {
Alexander Grafb104d062010-02-19 11:00:29 +0100434 case 8: gpr = *(u64 *)run->mmio.data; break;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100435 case 4: gpr = *(u32 *)run->mmio.data; break;
436 case 2: gpr = *(u16 *)run->mmio.data; break;
437 case 1: gpr = *(u8 *)run->mmio.data; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500438 }
439 } else {
440 /* Convert BE data from userland back to LE. */
441 switch (run->mmio.len) {
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100442 case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
443 case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
444 case 1: gpr = *(u8 *)run->mmio.data; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500445 }
446 }
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100447
Alexander Graf3587d532010-02-19 11:00:30 +0100448 if (vcpu->arch.mmio_sign_extend) {
449 switch (run->mmio.len) {
450#ifdef CONFIG_PPC64
451 case 4:
452 gpr = (s64)(s32)gpr;
453 break;
454#endif
455 case 2:
456 gpr = (s64)(s16)gpr;
457 break;
458 case 1:
459 gpr = (s64)(s8)gpr;
460 break;
461 }
462 }
463
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100464 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
Alexander Grafb104d062010-02-19 11:00:29 +0100465
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100466 switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
467 case KVM_MMIO_REG_GPR:
Alexander Grafb104d062010-02-19 11:00:29 +0100468 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
469 break;
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100470 case KVM_MMIO_REG_FPR:
471 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
Alexander Grafb104d062010-02-19 11:00:29 +0100472 break;
Alexander Graf287d5612010-04-01 15:33:21 +0200473#ifdef CONFIG_PPC_BOOK3S
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100474 case KVM_MMIO_REG_QPR:
475 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
Alexander Grafb104d062010-02-19 11:00:29 +0100476 break;
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100477 case KVM_MMIO_REG_FQPR:
478 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
479 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
Alexander Grafb104d062010-02-19 11:00:29 +0100480 break;
Alexander Graf287d5612010-04-01 15:33:21 +0200481#endif
Alexander Grafb104d062010-02-19 11:00:29 +0100482 default:
483 BUG();
484 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500485}
486
487int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
488 unsigned int rt, unsigned int bytes, int is_bigendian)
489{
490 if (bytes > sizeof(run->mmio.data)) {
491 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
492 run->mmio.len);
493 }
494
495 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
496 run->mmio.len = bytes;
497 run->mmio.is_write = 0;
498
499 vcpu->arch.io_gpr = rt;
500 vcpu->arch.mmio_is_bigendian = is_bigendian;
501 vcpu->mmio_needed = 1;
502 vcpu->mmio_is_write = 0;
Alexander Graf3587d532010-02-19 11:00:30 +0100503 vcpu->arch.mmio_sign_extend = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500504
505 return EMULATE_DO_MMIO;
506}
507
Alexander Graf3587d532010-02-19 11:00:30 +0100508/* Same as above, but sign extends */
509int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
510 unsigned int rt, unsigned int bytes, int is_bigendian)
511{
512 int r;
513
514 r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
515 vcpu->arch.mmio_sign_extend = 1;
516
517 return r;
518}
519
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500520int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
Alexander Grafb104d062010-02-19 11:00:29 +0100521 u64 val, unsigned int bytes, int is_bigendian)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500522{
523 void *data = run->mmio.data;
524
525 if (bytes > sizeof(run->mmio.data)) {
526 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
527 run->mmio.len);
528 }
529
530 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
531 run->mmio.len = bytes;
532 run->mmio.is_write = 1;
533 vcpu->mmio_needed = 1;
534 vcpu->mmio_is_write = 1;
535
536 /* Store the value at the lowest bytes in 'data'. */
537 if (is_bigendian) {
538 switch (bytes) {
Alexander Grafb104d062010-02-19 11:00:29 +0100539 case 8: *(u64 *)data = val; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500540 case 4: *(u32 *)data = val; break;
541 case 2: *(u16 *)data = val; break;
542 case 1: *(u8 *)data = val; break;
543 }
544 } else {
545 /* Store LE value into 'data'. */
546 switch (bytes) {
547 case 4: st_le32(data, val); break;
548 case 2: st_le16(data, val); break;
549 case 1: *(u8 *)data = val; break;
550 }
551 }
552
553 return EMULATE_DO_MMIO;
554}
555
556int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
557{
558 int r;
559 sigset_t sigsaved;
560
561 if (vcpu->sigset_active)
562 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
563
564 if (vcpu->mmio_needed) {
565 if (!vcpu->mmio_is_write)
566 kvmppc_complete_mmio_load(vcpu, run);
567 vcpu->mmio_needed = 0;
568 } else if (vcpu->arch.dcr_needed) {
569 if (!vcpu->arch.dcr_is_write)
570 kvmppc_complete_dcr_load(vcpu, run);
571 vcpu->arch.dcr_needed = 0;
Alexander Grafad0a0482010-03-24 21:48:30 +0100572 } else if (vcpu->arch.osi_needed) {
573 u64 *gprs = run->osi.gprs;
574 int i;
575
576 for (i = 0; i < 32; i++)
577 kvmppc_set_gpr(vcpu, i, gprs[i]);
578 vcpu->arch.osi_needed = 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000579 } else if (vcpu->arch.hcall_needed) {
580 int i;
581
582 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
583 for (i = 0; i < 9; ++i)
584 kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
585 vcpu->arch.hcall_needed = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500586 }
587
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000588 r = kvmppc_vcpu_run(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500589
590 if (vcpu->sigset_active)
591 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
592
593 return r;
594}
595
596int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
597{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000598 if (irq->irq == KVM_INTERRUPT_UNSET) {
Alexander Graf18978762010-03-24 21:48:18 +0100599 kvmppc_core_dequeue_external(vcpu, irq);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000600 return 0;
601 }
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500602
Paul Mackerras19ccb762011-07-23 17:42:46 +1000603 kvmppc_core_queue_external(vcpu, irq);
Christoffer Dallb6d33832012-03-08 16:44:24 -0500604
Scott Wooddfd4d472011-11-17 12:39:59 +0000605 kvm_vcpu_kick(vcpu);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500606
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500607 return 0;
608}
609
Alexander Graf71fbfd52010-03-24 21:48:29 +0100610static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
611 struct kvm_enable_cap *cap)
612{
613 int r;
614
615 if (cap->flags)
616 return -EINVAL;
617
618 switch (cap->cap) {
Alexander Grafad0a0482010-03-24 21:48:30 +0100619 case KVM_CAP_PPC_OSI:
620 r = 0;
621 vcpu->arch.osi_enabled = true;
622 break;
Alexander Graf930b4122011-08-08 17:29:42 +0200623 case KVM_CAP_PPC_PAPR:
624 r = 0;
625 vcpu->arch.papr_enabled = true;
626 break;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500627#ifdef CONFIG_KVM_E500
628 case KVM_CAP_SW_TLB: {
629 struct kvm_config_tlb cfg;
630 void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
631
632 r = -EFAULT;
633 if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
634 break;
635
636 r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
637 break;
638 }
639#endif
Alexander Graf71fbfd52010-03-24 21:48:29 +0100640 default:
641 r = -EINVAL;
642 break;
643 }
644
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200645 if (!r)
646 r = kvmppc_sanity_check(vcpu);
647
Alexander Graf71fbfd52010-03-24 21:48:29 +0100648 return r;
649}
650
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500651int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
652 struct kvm_mp_state *mp_state)
653{
654 return -EINVAL;
655}
656
657int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
658 struct kvm_mp_state *mp_state)
659{
660 return -EINVAL;
661}
662
663long kvm_arch_vcpu_ioctl(struct file *filp,
664 unsigned int ioctl, unsigned long arg)
665{
666 struct kvm_vcpu *vcpu = filp->private_data;
667 void __user *argp = (void __user *)arg;
668 long r;
669
Avi Kivity93736622010-05-13 12:35:17 +0300670 switch (ioctl) {
671 case KVM_INTERRUPT: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500672 struct kvm_interrupt irq;
673 r = -EFAULT;
674 if (copy_from_user(&irq, argp, sizeof(irq)))
Avi Kivity93736622010-05-13 12:35:17 +0300675 goto out;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500676 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity93736622010-05-13 12:35:17 +0300677 goto out;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500678 }
Avi Kivity19483d12010-05-13 12:30:43 +0300679
Alexander Graf71fbfd52010-03-24 21:48:29 +0100680 case KVM_ENABLE_CAP:
681 {
682 struct kvm_enable_cap cap;
683 r = -EFAULT;
684 if (copy_from_user(&cap, argp, sizeof(cap)))
685 goto out;
686 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
687 break;
688 }
Scott Wooddc83b8b2011-08-18 15:25:21 -0500689
Alexander Grafe24ed812011-09-14 10:02:41 +0200690 case KVM_SET_ONE_REG:
691 case KVM_GET_ONE_REG:
692 {
693 struct kvm_one_reg reg;
694 r = -EFAULT;
695 if (copy_from_user(&reg, argp, sizeof(reg)))
696 goto out;
697 if (ioctl == KVM_SET_ONE_REG)
698 r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
699 else
700 r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
701 break;
702 }
703
Scott Wooddc83b8b2011-08-18 15:25:21 -0500704#ifdef CONFIG_KVM_E500
705 case KVM_DIRTY_TLB: {
706 struct kvm_dirty_tlb dirty;
707 r = -EFAULT;
708 if (copy_from_user(&dirty, argp, sizeof(dirty)))
709 goto out;
710 r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
711 break;
712 }
713#endif
714
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500715 default:
716 r = -EINVAL;
717 }
718
719out:
720 return r;
721}
722
Carsten Otte5b1c1492012-01-04 10:25:23 +0100723int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
724{
725 return VM_FAULT_SIGBUS;
726}
727
Alexander Graf15711e92010-07-29 14:48:08 +0200728static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
729{
730 u32 inst_lis = 0x3c000000;
731 u32 inst_ori = 0x60000000;
732 u32 inst_nop = 0x60000000;
733 u32 inst_sc = 0x44000002;
734 u32 inst_imm_mask = 0xffff;
735
736 /*
737 * The hypercall to get into KVM from within guest context is as
738 * follows:
739 *
740 * lis r0, r0, KVM_SC_MAGIC_R0@h
741 * ori r0, KVM_SC_MAGIC_R0@l
742 * sc
743 * nop
744 */
745 pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask);
746 pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask);
747 pvinfo->hcall[2] = inst_sc;
748 pvinfo->hcall[3] = inst_nop;
749
750 return 0;
751}
752
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500753long kvm_arch_vm_ioctl(struct file *filp,
754 unsigned int ioctl, unsigned long arg)
755{
Alexander Graf15711e92010-07-29 14:48:08 +0200756 void __user *argp = (void __user *)arg;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500757 long r;
758
759 switch (ioctl) {
Alexander Graf15711e92010-07-29 14:48:08 +0200760 case KVM_PPC_GET_PVINFO: {
761 struct kvm_ppc_pvinfo pvinfo;
Vasiliy Kulikovd8cdddc2010-10-30 13:04:24 +0400762 memset(&pvinfo, 0, sizeof(pvinfo));
Alexander Graf15711e92010-07-29 14:48:08 +0200763 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
764 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
765 r = -EFAULT;
766 goto out;
767 }
768
769 break;
770 }
David Gibson54738c02011-06-29 00:22:41 +0000771#ifdef CONFIG_KVM_BOOK3S_64_HV
772 case KVM_CREATE_SPAPR_TCE: {
773 struct kvm_create_spapr_tce create_tce;
774 struct kvm *kvm = filp->private_data;
775
776 r = -EFAULT;
777 if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
778 goto out;
779 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
780 goto out;
781 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000782
783 case KVM_ALLOCATE_RMA: {
784 struct kvm *kvm = filp->private_data;
785 struct kvm_allocate_rma rma;
786
787 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
788 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
789 r = -EFAULT;
790 break;
791 }
David Gibson54738c02011-06-29 00:22:41 +0000792#endif /* CONFIG_KVM_BOOK3S_64_HV */
793
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500794 default:
Avi Kivity367e1312009-08-26 14:57:07 +0300795 r = -ENOTTY;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500796 }
797
Alexander Graf15711e92010-07-29 14:48:08 +0200798out:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500799 return r;
800}
801
802int kvm_arch_init(void *opaque)
803{
804 return 0;
805}
806
807void kvm_arch_exit(void)
808{
809}