blob: 45fe433316ea8e6e903fc90432702ffe4d726067 [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{
Liu Yu-B132019202e072012-07-03 05:48:52 +000041 return !!(v->arch.pending_exceptions) ||
Scott Wooddfd4d472011-11-17 12:39:59 +000042 v->requests;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050043}
44
Christoffer Dallb6d33832012-03-08 16:44:24 -050045int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
46{
47 return 1;
48}
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) {
Stuart Yoderfdcf8bd2012-07-03 05:48:50 +000069 case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE):
Alexander Graf5fc87402010-07-29 14:47:55 +020070 {
71 vcpu->arch.magic_page_pa = param1;
72 vcpu->arch.magic_page_ea = param2;
73
Scott Woodb5904972011-11-08 18:23:30 -060074 r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
Alexander Graf7508e162010-08-03 11:32:56 +020075
Stuart Yoderfdcf8bd2012-07-03 05:48:50 +000076 r = EV_SUCCESS;
Alexander Graf5fc87402010-07-29 14:47:55 +020077 break;
78 }
Stuart Yoderfdcf8bd2012-07-03 05:48:50 +000079 case KVM_HCALL_TOKEN(KVM_HC_FEATURES):
80 r = EV_SUCCESS;
Alexander Grafbf7ca4b2012-02-15 23:40:00 +000081#if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2)
Scott Wooda4cd8b22011-06-14 18:34:41 -050082 /* 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;
Liu Yu-B132019202e072012-07-03 05:48:52 +000088 case EV_HCALL_TOKEN(EV_IDLE):
89 r = EV_SUCCESS;
90 kvm_vcpu_block(vcpu);
91 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
92 break;
Alexander Graf2a342ed2010-07-29 14:47:48 +020093 default:
Stuart Yoderfdcf8bd2012-07-03 05:48:50 +000094 r = EV_UNIMPLEMENTED;
Alexander Graf2a342ed2010-07-29 14:47:48 +020095 break;
96 }
97
Alexander Graf7508e162010-08-03 11:32:56 +020098 kvmppc_set_gpr(vcpu, 4, r2);
99
Alexander Graf2a342ed2010-07-29 14:47:48 +0200100 return r;
101}
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500102
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200103int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
104{
105 int r = false;
106
107 /* We have to know what CPU to virtualize */
108 if (!vcpu->arch.pvr)
109 goto out;
110
111 /* PAPR only works with book3s_64 */
112 if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
113 goto out;
114
115#ifdef CONFIG_KVM_BOOK3S_64_HV
116 /* HV KVM can only do PAPR mode for now */
117 if (!vcpu->arch.papr_enabled)
118 goto out;
119#endif
120
Scott Woodd30f6e42011-12-20 15:34:43 +0000121#ifdef CONFIG_KVM_BOOKE_HV
122 if (!cpu_has_feature(CPU_FTR_EMB_HV))
123 goto out;
124#endif
125
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200126 r = true;
127
128out:
129 vcpu->arch.sane = r;
130 return r ? 0 : -EINVAL;
131}
132
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500133int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
134{
135 enum emulation_result er;
136 int r;
137
138 er = kvmppc_emulate_instruction(run, vcpu);
139 switch (er) {
140 case EMULATE_DONE:
141 /* Future optimization: only reload non-volatiles if they were
142 * actually modified. */
143 r = RESUME_GUEST_NV;
144 break;
145 case EMULATE_DO_MMIO:
146 run->exit_reason = KVM_EXIT_MMIO;
147 /* We must reload nonvolatiles because "update" load/store
148 * instructions modify register state. */
149 /* Future optimization: only reload non-volatiles if they were
150 * actually modified. */
151 r = RESUME_HOST_NV;
152 break;
153 case EMULATE_FAIL:
154 /* XXX Deliver Program interrupt to guest. */
155 printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__,
Alexander Grafc7f38f42010-04-16 00:11:40 +0200156 kvmppc_get_last_inst(vcpu));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500157 r = RESUME_HOST;
158 break;
159 default:
160 BUG();
161 }
162
163 return r;
164}
165
Alexander Graf10474ae2009-09-15 11:37:46 +0200166int kvm_arch_hardware_enable(void *garbage)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500167{
Alexander Graf10474ae2009-09-15 11:37:46 +0200168 return 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500169}
170
171void kvm_arch_hardware_disable(void *garbage)
172{
173}
174
175int kvm_arch_hardware_setup(void)
176{
177 return 0;
178}
179
180void kvm_arch_hardware_unsetup(void)
181{
182}
183
184void kvm_arch_check_processor_compat(void *rtn)
185{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600186 *(int *)rtn = kvmppc_core_check_processor_compat();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500187}
188
Carsten Ottee08b9632012-01-04 10:25:20 +0100189int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500190{
Carsten Ottee08b9632012-01-04 10:25:20 +0100191 if (type)
192 return -EINVAL;
193
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000194 return kvmppc_core_init_vm(kvm);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500195}
196
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100197void kvm_arch_destroy_vm(struct kvm *kvm)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500198{
199 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300200 struct kvm_vcpu *vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500201
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300202 kvm_for_each_vcpu(i, vcpu, kvm)
203 kvm_arch_vcpu_free(vcpu);
204
205 mutex_lock(&kvm->lock);
206 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
207 kvm->vcpus[i] = NULL;
208
209 atomic_set(&kvm->online_vcpus, 0);
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000210
211 kvmppc_core_destroy_vm(kvm);
212
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300213 mutex_unlock(&kvm->lock);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500214}
215
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800216void kvm_arch_sync_events(struct kvm *kvm)
217{
218}
219
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500220int kvm_dev_ioctl_check_extension(long ext)
221{
222 int r;
223
224 switch (ext) {
Scott Wood5ce941e2011-04-27 17:24:21 -0500225#ifdef CONFIG_BOOKE
226 case KVM_CAP_PPC_BOOKE_SREGS:
227#else
Alexander Grafe15a1132009-11-30 03:02:02 +0000228 case KVM_CAP_PPC_SEGSTATE:
Alexander Graf1022fc32011-09-14 21:45:23 +0200229 case KVM_CAP_PPC_HIOR:
Alexander Graf930b4122011-08-08 17:29:42 +0200230 case KVM_CAP_PPC_PAPR:
Scott Wood5ce941e2011-04-27 17:24:21 -0500231#endif
Alexander Graf18978762010-03-24 21:48:18 +0100232 case KVM_CAP_PPC_UNSET_IRQ:
Alexander Graf7b4203e2010-08-30 13:50:45 +0200233 case KVM_CAP_PPC_IRQ_LEVEL:
Alexander Graf71fbfd52010-03-24 21:48:29 +0100234 case KVM_CAP_ENABLE_CAP:
Alexander Grafe24ed812011-09-14 10:02:41 +0200235 case KVM_CAP_ONE_REG:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000236 r = 1;
237 break;
238#ifndef CONFIG_KVM_BOOK3S_64_HV
239 case KVM_CAP_PPC_PAIRED_SINGLES:
Alexander Grafad0a0482010-03-24 21:48:30 +0100240 case KVM_CAP_PPC_OSI:
Alexander Graf15711e92010-07-29 14:48:08 +0200241 case KVM_CAP_PPC_GET_PVINFO:
Alexander Grafbf7ca4b2012-02-15 23:40:00 +0000242#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500243 case KVM_CAP_SW_TLB:
244#endif
Alexander Grafe15a1132009-11-30 03:02:02 +0000245 r = 1;
246 break;
Laurent Vivier588968b2008-05-30 16:05:56 +0200247 case KVM_CAP_COALESCED_MMIO:
248 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
249 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000250#endif
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000251#ifdef CONFIG_PPC_BOOK3S_64
David Gibson54738c02011-06-29 00:22:41 +0000252 case KVM_CAP_SPAPR_TCE:
Paul Mackerras32fad282012-05-04 02:32:53 +0000253 case KVM_CAP_PPC_ALLOC_HTAB:
David Gibson54738c02011-06-29 00:22:41 +0000254 r = 1;
255 break;
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000256#endif /* CONFIG_PPC_BOOK3S_64 */
257#ifdef CONFIG_KVM_BOOK3S_64_HV
Paul Mackerras371fefd2011-06-29 00:23:08 +0000258 case KVM_CAP_PPC_SMT:
259 r = threads_per_core;
260 break;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000261 case KVM_CAP_PPC_RMA:
262 r = 1;
Paul Mackerras9e368f22011-06-29 00:40:08 +0000263 /* PPC970 requires an RMA */
264 if (cpu_has_feature(CPU_FTR_ARCH_201))
265 r = 2;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000266 break;
David Gibson54738c02011-06-29 00:22:41 +0000267#endif
Alexander Graff4800b12012-08-07 10:24:14 +0200268 case KVM_CAP_SYNC_MMU:
269#ifdef CONFIG_KVM_BOOK3S_64_HV
270 r = cpu_has_feature(CPU_FTR_ARCH_206) ? 1 : 0;
271#elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
272 r = 1;
273#else
274 r = 0;
275#endif
276 break;
Matt Evansb5434032011-12-07 16:55:57 +0000277 case KVM_CAP_NR_VCPUS:
278 /*
279 * Recommending a number of CPUs is somewhat arbitrary; we
280 * return the number of present CPUs for -HV (since a host
281 * will have secondary threads "offline"), and for other KVM
282 * implementations just count online CPUs.
283 */
284#ifdef CONFIG_KVM_BOOK3S_64_HV
285 r = num_present_cpus();
286#else
287 r = num_online_cpus();
288#endif
289 break;
290 case KVM_CAP_MAX_VCPUS:
291 r = KVM_MAX_VCPUS;
292 break;
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +0000293#ifdef CONFIG_PPC_BOOK3S_64
294 case KVM_CAP_PPC_GET_SMMU_INFO:
295 r = 1;
296 break;
297#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500298 default:
299 r = 0;
300 break;
301 }
302 return r;
303
304}
305
306long kvm_arch_dev_ioctl(struct file *filp,
307 unsigned int ioctl, unsigned long arg)
308{
309 return -EINVAL;
310}
311
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900312void kvm_arch_free_memslot(struct kvm_memory_slot *free,
313 struct kvm_memory_slot *dont)
314{
Takuya Yoshikawad89cc612012-08-01 18:03:28 +0900315 if (!dont || free->arch.rmap != dont->arch.rmap) {
316 vfree(free->arch.rmap);
317 free->arch.rmap = NULL;
318 }
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900319}
320
321int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
322{
Takuya Yoshikawad89cc612012-08-01 18:03:28 +0900323 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
324 if (!slot->arch.rmap)
325 return -ENOMEM;
326
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900327 return 0;
328}
329
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200330int kvm_arch_prepare_memory_region(struct kvm *kvm,
331 struct kvm_memory_slot *memslot,
332 struct kvm_memory_slot old,
333 struct kvm_userspace_memory_region *mem,
334 int user_alloc)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500335{
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000336 return kvmppc_core_prepare_memory_region(kvm, mem);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500337}
338
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200339void kvm_arch_commit_memory_region(struct kvm *kvm,
340 struct kvm_userspace_memory_region *mem,
341 struct kvm_memory_slot old,
342 int user_alloc)
343{
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000344 kvmppc_core_commit_memory_region(kvm, mem);
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200345}
346
Marcelo Tosatti2df72e92012-08-24 15:54:57 -0300347void kvm_arch_flush_shadow_all(struct kvm *kvm)
348{
349}
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200350
Marcelo Tosatti2df72e92012-08-24 15:54:57 -0300351void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
352 struct kvm_memory_slot *slot)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300353{
354}
355
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500356struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
357{
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600358 struct kvm_vcpu *vcpu;
359 vcpu = kvmppc_core_vcpu_create(kvm, id);
Matt Evans03cdab52011-12-06 21:19:42 +0000360 if (!IS_ERR(vcpu)) {
361 vcpu->arch.wqp = &vcpu->wq;
Wei Yongjun06056bf2010-03-09 14:13:43 +0800362 kvmppc_create_vcpu_debugfs(vcpu, id);
Matt Evans03cdab52011-12-06 21:19:42 +0000363 }
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600364 return vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500365}
366
367void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
368{
Alexander Grafa5954052010-02-22 16:52:14 +0100369 /* Make sure we're not using the vcpu anymore */
370 hrtimer_cancel(&vcpu->arch.dec_timer);
371 tasklet_kill(&vcpu->arch.tasklet);
372
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600373 kvmppc_remove_vcpu_debugfs(vcpu);
Hollis Blancharddb93f572008-11-05 09:36:18 -0600374 kvmppc_core_vcpu_free(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500375}
376
377void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
378{
379 kvm_arch_vcpu_free(vcpu);
380}
381
382int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
383{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600384 return kvmppc_core_pending_dec(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500385}
386
Alexander Graf544c6762009-11-02 12:02:31 +0000387/*
388 * low level hrtimer wake routine. Because this runs in hardirq context
389 * we schedule a tasklet to do the real work.
390 */
391enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
392{
393 struct kvm_vcpu *vcpu;
394
395 vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
396 tasklet_schedule(&vcpu->arch.tasklet);
397
398 return HRTIMER_NORESTART;
399}
400
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500401int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
402{
Alexander Graf544c6762009-11-02 12:02:31 +0000403 hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
404 tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
405 vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000406 vcpu->arch.dec_expires = ~(u64)0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500407
Bharat Bhushan09000ad2011-03-25 10:32:13 +0530408#ifdef CONFIG_KVM_EXIT_TIMING
409 mutex_init(&vcpu->arch.exit_timing_lock);
410#endif
411
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500412 return 0;
413}
414
415void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
416{
Hollis Blanchardecc09812009-01-03 16:22:59 -0600417 kvmppc_mmu_destroy(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500418}
419
420void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
421{
Scott Woodeab17672011-04-27 17:24:10 -0500422#ifdef CONFIG_BOOKE
423 /*
424 * vrsave (formerly usprg0) isn't used by Linux, but may
425 * be used by the guest.
426 *
427 * On non-booke this is associated with Altivec and
428 * is handled by code in book3s.c.
429 */
430 mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
431#endif
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600432 kvmppc_core_vcpu_load(vcpu, cpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000433 vcpu->cpu = smp_processor_id();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500434}
435
436void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
437{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600438 kvmppc_core_vcpu_put(vcpu);
Scott Woodeab17672011-04-27 17:24:10 -0500439#ifdef CONFIG_BOOKE
440 vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
441#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +0000442 vcpu->cpu = -1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500443}
444
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100445int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
Hollis Blanchardf5d09062009-01-04 13:51:09 -0600446 struct kvm_guest_debug *dbg)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500447{
Hollis Blanchardf5d09062009-01-04 13:51:09 -0600448 return -EINVAL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500449}
450
451static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
452 struct kvm_run *run)
453{
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100454 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500455}
456
457static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
458 struct kvm_run *run)
459{
Denis Kirjanov69b61832010-06-11 11:23:26 +0000460 u64 uninitialized_var(gpr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500461
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100462 if (run->mmio.len > sizeof(gpr)) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500463 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
464 return;
465 }
466
467 if (vcpu->arch.mmio_is_bigendian) {
468 switch (run->mmio.len) {
Alexander Grafb104d062010-02-19 11:00:29 +0100469 case 8: gpr = *(u64 *)run->mmio.data; break;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100470 case 4: gpr = *(u32 *)run->mmio.data; break;
471 case 2: gpr = *(u16 *)run->mmio.data; break;
472 case 1: gpr = *(u8 *)run->mmio.data; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500473 }
474 } else {
475 /* Convert BE data from userland back to LE. */
476 switch (run->mmio.len) {
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100477 case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
478 case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
479 case 1: gpr = *(u8 *)run->mmio.data; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500480 }
481 }
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100482
Alexander Graf3587d532010-02-19 11:00:30 +0100483 if (vcpu->arch.mmio_sign_extend) {
484 switch (run->mmio.len) {
485#ifdef CONFIG_PPC64
486 case 4:
487 gpr = (s64)(s32)gpr;
488 break;
489#endif
490 case 2:
491 gpr = (s64)(s16)gpr;
492 break;
493 case 1:
494 gpr = (s64)(s8)gpr;
495 break;
496 }
497 }
498
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100499 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
Alexander Grafb104d062010-02-19 11:00:29 +0100500
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100501 switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
502 case KVM_MMIO_REG_GPR:
Alexander Grafb104d062010-02-19 11:00:29 +0100503 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
504 break;
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100505 case KVM_MMIO_REG_FPR:
506 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
Alexander Grafb104d062010-02-19 11:00:29 +0100507 break;
Alexander Graf287d5612010-04-01 15:33:21 +0200508#ifdef CONFIG_PPC_BOOK3S
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100509 case KVM_MMIO_REG_QPR:
510 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
Alexander Grafb104d062010-02-19 11:00:29 +0100511 break;
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100512 case KVM_MMIO_REG_FQPR:
513 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
514 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
Alexander Grafb104d062010-02-19 11:00:29 +0100515 break;
Alexander Graf287d5612010-04-01 15:33:21 +0200516#endif
Alexander Grafb104d062010-02-19 11:00:29 +0100517 default:
518 BUG();
519 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500520}
521
522int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
523 unsigned int rt, unsigned int bytes, int is_bigendian)
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 = 0;
533
534 vcpu->arch.io_gpr = rt;
535 vcpu->arch.mmio_is_bigendian = is_bigendian;
536 vcpu->mmio_needed = 1;
537 vcpu->mmio_is_write = 0;
Alexander Graf3587d532010-02-19 11:00:30 +0100538 vcpu->arch.mmio_sign_extend = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500539
540 return EMULATE_DO_MMIO;
541}
542
Alexander Graf3587d532010-02-19 11:00:30 +0100543/* Same as above, but sign extends */
544int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
545 unsigned int rt, unsigned int bytes, int is_bigendian)
546{
547 int r;
548
549 r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
550 vcpu->arch.mmio_sign_extend = 1;
551
552 return r;
553}
554
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500555int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
Alexander Grafb104d062010-02-19 11:00:29 +0100556 u64 val, unsigned int bytes, int is_bigendian)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500557{
558 void *data = run->mmio.data;
559
560 if (bytes > sizeof(run->mmio.data)) {
561 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
562 run->mmio.len);
563 }
564
565 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
566 run->mmio.len = bytes;
567 run->mmio.is_write = 1;
568 vcpu->mmio_needed = 1;
569 vcpu->mmio_is_write = 1;
570
571 /* Store the value at the lowest bytes in 'data'. */
572 if (is_bigendian) {
573 switch (bytes) {
Alexander Grafb104d062010-02-19 11:00:29 +0100574 case 8: *(u64 *)data = val; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500575 case 4: *(u32 *)data = val; break;
576 case 2: *(u16 *)data = val; break;
577 case 1: *(u8 *)data = val; break;
578 }
579 } else {
580 /* Store LE value into 'data'. */
581 switch (bytes) {
582 case 4: st_le32(data, val); break;
583 case 2: st_le16(data, val); break;
584 case 1: *(u8 *)data = val; break;
585 }
586 }
587
588 return EMULATE_DO_MMIO;
589}
590
591int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
592{
593 int r;
594 sigset_t sigsaved;
595
596 if (vcpu->sigset_active)
597 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
598
599 if (vcpu->mmio_needed) {
600 if (!vcpu->mmio_is_write)
601 kvmppc_complete_mmio_load(vcpu, run);
602 vcpu->mmio_needed = 0;
603 } else if (vcpu->arch.dcr_needed) {
604 if (!vcpu->arch.dcr_is_write)
605 kvmppc_complete_dcr_load(vcpu, run);
606 vcpu->arch.dcr_needed = 0;
Alexander Grafad0a0482010-03-24 21:48:30 +0100607 } else if (vcpu->arch.osi_needed) {
608 u64 *gprs = run->osi.gprs;
609 int i;
610
611 for (i = 0; i < 32; i++)
612 kvmppc_set_gpr(vcpu, i, gprs[i]);
613 vcpu->arch.osi_needed = 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000614 } else if (vcpu->arch.hcall_needed) {
615 int i;
616
617 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
618 for (i = 0; i < 9; ++i)
619 kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
620 vcpu->arch.hcall_needed = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500621 }
622
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000623 r = kvmppc_vcpu_run(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500624
625 if (vcpu->sigset_active)
626 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
627
628 return r;
629}
630
631int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
632{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000633 if (irq->irq == KVM_INTERRUPT_UNSET) {
Alexander Graf18978762010-03-24 21:48:18 +0100634 kvmppc_core_dequeue_external(vcpu, irq);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000635 return 0;
636 }
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500637
Paul Mackerras19ccb762011-07-23 17:42:46 +1000638 kvmppc_core_queue_external(vcpu, irq);
Christoffer Dallb6d33832012-03-08 16:44:24 -0500639
Scott Wooddfd4d472011-11-17 12:39:59 +0000640 kvm_vcpu_kick(vcpu);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500641
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500642 return 0;
643}
644
Alexander Graf71fbfd52010-03-24 21:48:29 +0100645static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
646 struct kvm_enable_cap *cap)
647{
648 int r;
649
650 if (cap->flags)
651 return -EINVAL;
652
653 switch (cap->cap) {
Alexander Grafad0a0482010-03-24 21:48:30 +0100654 case KVM_CAP_PPC_OSI:
655 r = 0;
656 vcpu->arch.osi_enabled = true;
657 break;
Alexander Graf930b4122011-08-08 17:29:42 +0200658 case KVM_CAP_PPC_PAPR:
659 r = 0;
660 vcpu->arch.papr_enabled = true;
661 break;
Alexander Grafbf7ca4b2012-02-15 23:40:00 +0000662#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500663 case KVM_CAP_SW_TLB: {
664 struct kvm_config_tlb cfg;
665 void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
666
667 r = -EFAULT;
668 if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
669 break;
670
671 r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
672 break;
673 }
674#endif
Alexander Graf71fbfd52010-03-24 21:48:29 +0100675 default:
676 r = -EINVAL;
677 break;
678 }
679
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200680 if (!r)
681 r = kvmppc_sanity_check(vcpu);
682
Alexander Graf71fbfd52010-03-24 21:48:29 +0100683 return r;
684}
685
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500686int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
687 struct kvm_mp_state *mp_state)
688{
689 return -EINVAL;
690}
691
692int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
693 struct kvm_mp_state *mp_state)
694{
695 return -EINVAL;
696}
697
698long kvm_arch_vcpu_ioctl(struct file *filp,
699 unsigned int ioctl, unsigned long arg)
700{
701 struct kvm_vcpu *vcpu = filp->private_data;
702 void __user *argp = (void __user *)arg;
703 long r;
704
Avi Kivity93736622010-05-13 12:35:17 +0300705 switch (ioctl) {
706 case KVM_INTERRUPT: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500707 struct kvm_interrupt irq;
708 r = -EFAULT;
709 if (copy_from_user(&irq, argp, sizeof(irq)))
Avi Kivity93736622010-05-13 12:35:17 +0300710 goto out;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500711 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity93736622010-05-13 12:35:17 +0300712 goto out;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500713 }
Avi Kivity19483d12010-05-13 12:30:43 +0300714
Alexander Graf71fbfd52010-03-24 21:48:29 +0100715 case KVM_ENABLE_CAP:
716 {
717 struct kvm_enable_cap cap;
718 r = -EFAULT;
719 if (copy_from_user(&cap, argp, sizeof(cap)))
720 goto out;
721 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
722 break;
723 }
Scott Wooddc83b8b2011-08-18 15:25:21 -0500724
Alexander Grafe24ed812011-09-14 10:02:41 +0200725 case KVM_SET_ONE_REG:
726 case KVM_GET_ONE_REG:
727 {
728 struct kvm_one_reg reg;
729 r = -EFAULT;
730 if (copy_from_user(&reg, argp, sizeof(reg)))
731 goto out;
732 if (ioctl == KVM_SET_ONE_REG)
733 r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
734 else
735 r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
736 break;
737 }
738
Alexander Grafbf7ca4b2012-02-15 23:40:00 +0000739#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500740 case KVM_DIRTY_TLB: {
741 struct kvm_dirty_tlb dirty;
742 r = -EFAULT;
743 if (copy_from_user(&dirty, argp, sizeof(dirty)))
744 goto out;
745 r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
746 break;
747 }
748#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500749 default:
750 r = -EINVAL;
751 }
752
753out:
754 return r;
755}
756
Carsten Otte5b1c1492012-01-04 10:25:23 +0100757int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
758{
759 return VM_FAULT_SIGBUS;
760}
761
Alexander Graf15711e92010-07-29 14:48:08 +0200762static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
763{
Stuart Yoder784bafa2012-07-03 05:48:51 +0000764 u32 inst_nop = 0x60000000;
765#ifdef CONFIG_KVM_BOOKE_HV
766 u32 inst_sc1 = 0x44000022;
767 pvinfo->hcall[0] = inst_sc1;
768 pvinfo->hcall[1] = inst_nop;
769 pvinfo->hcall[2] = inst_nop;
770 pvinfo->hcall[3] = inst_nop;
771#else
Alexander Graf15711e92010-07-29 14:48:08 +0200772 u32 inst_lis = 0x3c000000;
773 u32 inst_ori = 0x60000000;
Alexander Graf15711e92010-07-29 14:48:08 +0200774 u32 inst_sc = 0x44000002;
775 u32 inst_imm_mask = 0xffff;
776
777 /*
778 * The hypercall to get into KVM from within guest context is as
779 * follows:
780 *
781 * lis r0, r0, KVM_SC_MAGIC_R0@h
782 * ori r0, KVM_SC_MAGIC_R0@l
783 * sc
784 * nop
785 */
786 pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask);
787 pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask);
788 pvinfo->hcall[2] = inst_sc;
789 pvinfo->hcall[3] = inst_nop;
Stuart Yoder784bafa2012-07-03 05:48:51 +0000790#endif
Alexander Graf15711e92010-07-29 14:48:08 +0200791
Liu Yu-B132019202e072012-07-03 05:48:52 +0000792 pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
793
Alexander Graf15711e92010-07-29 14:48:08 +0200794 return 0;
795}
796
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500797long kvm_arch_vm_ioctl(struct file *filp,
798 unsigned int ioctl, unsigned long arg)
799{
Alexander Graf15711e92010-07-29 14:48:08 +0200800 void __user *argp = (void __user *)arg;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500801 long r;
802
803 switch (ioctl) {
Alexander Graf15711e92010-07-29 14:48:08 +0200804 case KVM_PPC_GET_PVINFO: {
805 struct kvm_ppc_pvinfo pvinfo;
Vasiliy Kulikovd8cdddc2010-10-30 13:04:24 +0400806 memset(&pvinfo, 0, sizeof(pvinfo));
Alexander Graf15711e92010-07-29 14:48:08 +0200807 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
808 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
809 r = -EFAULT;
810 goto out;
811 }
812
813 break;
814 }
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000815#ifdef CONFIG_PPC_BOOK3S_64
David Gibson54738c02011-06-29 00:22:41 +0000816 case KVM_CREATE_SPAPR_TCE: {
817 struct kvm_create_spapr_tce create_tce;
818 struct kvm *kvm = filp->private_data;
819
820 r = -EFAULT;
821 if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
822 goto out;
823 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
824 goto out;
825 }
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000826#endif /* CONFIG_PPC_BOOK3S_64 */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000827
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000828#ifdef CONFIG_KVM_BOOK3S_64_HV
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000829 case KVM_ALLOCATE_RMA: {
830 struct kvm *kvm = filp->private_data;
831 struct kvm_allocate_rma rma;
832
833 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
834 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
835 r = -EFAULT;
836 break;
837 }
Paul Mackerras32fad282012-05-04 02:32:53 +0000838
839 case KVM_PPC_ALLOCATE_HTAB: {
840 struct kvm *kvm = filp->private_data;
841 u32 htab_order;
842
843 r = -EFAULT;
844 if (get_user(htab_order, (u32 __user *)argp))
845 break;
846 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
847 if (r)
848 break;
849 r = -EFAULT;
850 if (put_user(htab_order, (u32 __user *)argp))
851 break;
852 r = 0;
853 break;
854 }
David Gibson54738c02011-06-29 00:22:41 +0000855#endif /* CONFIG_KVM_BOOK3S_64_HV */
856
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +0000857#ifdef CONFIG_PPC_BOOK3S_64
858 case KVM_PPC_GET_SMMU_INFO: {
859 struct kvm *kvm = filp->private_data;
860 struct kvm_ppc_smmu_info info;
861
862 memset(&info, 0, sizeof(info));
863 r = kvm_vm_ioctl_get_smmu_info(kvm, &info);
864 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
865 r = -EFAULT;
866 break;
867 }
868#endif /* CONFIG_PPC_BOOK3S_64 */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500869 default:
Avi Kivity367e1312009-08-26 14:57:07 +0300870 r = -ENOTTY;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500871 }
872
Alexander Graf15711e92010-07-29 14:48:08 +0200873out:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500874 return r;
875}
876
Scott Wood043cc4d2011-12-20 15:34:20 +0000877static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
878static unsigned long nr_lpids;
879
880long kvmppc_alloc_lpid(void)
881{
882 long lpid;
883
884 do {
885 lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
886 if (lpid >= nr_lpids) {
887 pr_err("%s: No LPIDs free\n", __func__);
888 return -ENOMEM;
889 }
890 } while (test_and_set_bit(lpid, lpid_inuse));
891
892 return lpid;
893}
894
895void kvmppc_claim_lpid(long lpid)
896{
897 set_bit(lpid, lpid_inuse);
898}
899
900void kvmppc_free_lpid(long lpid)
901{
902 clear_bit(lpid, lpid_inuse);
903}
904
905void kvmppc_init_lpid(unsigned long nr_lpids_param)
906{
907 nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
908 memset(lpid_inuse, 0, sizeof(lpid_inuse));
909}
910
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500911int kvm_arch_init(void *opaque)
912{
913 return 0;
914}
915
916void kvm_arch_exit(void)
917{
918}