blob: dbf56e173c25b138af016e865f4c99b369abba24 [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;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000267 case KVM_CAP_SYNC_MMU:
268 r = cpu_has_feature(CPU_FTR_ARCH_206) ? 1 : 0;
269 break;
David Gibson54738c02011-06-29 00:22:41 +0000270#endif
Matt Evansb5434032011-12-07 16:55:57 +0000271 case KVM_CAP_NR_VCPUS:
272 /*
273 * Recommending a number of CPUs is somewhat arbitrary; we
274 * return the number of present CPUs for -HV (since a host
275 * will have secondary threads "offline"), and for other KVM
276 * implementations just count online CPUs.
277 */
278#ifdef CONFIG_KVM_BOOK3S_64_HV
279 r = num_present_cpus();
280#else
281 r = num_online_cpus();
282#endif
283 break;
284 case KVM_CAP_MAX_VCPUS:
285 r = KVM_MAX_VCPUS;
286 break;
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +0000287#ifdef CONFIG_PPC_BOOK3S_64
288 case KVM_CAP_PPC_GET_SMMU_INFO:
289 r = 1;
290 break;
291#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500292 default:
293 r = 0;
294 break;
295 }
296 return r;
297
298}
299
300long kvm_arch_dev_ioctl(struct file *filp,
301 unsigned int ioctl, unsigned long arg)
302{
303 return -EINVAL;
304}
305
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900306void kvm_arch_free_memslot(struct kvm_memory_slot *free,
307 struct kvm_memory_slot *dont)
308{
Takuya Yoshikawad89cc612012-08-01 18:03:28 +0900309 if (!dont || free->arch.rmap != dont->arch.rmap) {
310 vfree(free->arch.rmap);
311 free->arch.rmap = NULL;
312 }
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900313}
314
315int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
316{
Takuya Yoshikawad89cc612012-08-01 18:03:28 +0900317 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
318 if (!slot->arch.rmap)
319 return -ENOMEM;
320
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900321 return 0;
322}
323
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200324int kvm_arch_prepare_memory_region(struct kvm *kvm,
325 struct kvm_memory_slot *memslot,
326 struct kvm_memory_slot old,
327 struct kvm_userspace_memory_region *mem,
328 int user_alloc)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500329{
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000330 return kvmppc_core_prepare_memory_region(kvm, mem);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500331}
332
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200333void kvm_arch_commit_memory_region(struct kvm *kvm,
334 struct kvm_userspace_memory_region *mem,
335 struct kvm_memory_slot old,
336 int user_alloc)
337{
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000338 kvmppc_core_commit_memory_region(kvm, mem);
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200339}
340
Marcelo Tosatti2df72e92012-08-24 15:54:57 -0300341void kvm_arch_flush_shadow_all(struct kvm *kvm)
342{
343}
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200344
Marcelo Tosatti2df72e92012-08-24 15:54:57 -0300345void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
346 struct kvm_memory_slot *slot)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300347{
348}
349
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500350struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
351{
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600352 struct kvm_vcpu *vcpu;
353 vcpu = kvmppc_core_vcpu_create(kvm, id);
Matt Evans03cdab52011-12-06 21:19:42 +0000354 if (!IS_ERR(vcpu)) {
355 vcpu->arch.wqp = &vcpu->wq;
Wei Yongjun06056bf2010-03-09 14:13:43 +0800356 kvmppc_create_vcpu_debugfs(vcpu, id);
Matt Evans03cdab52011-12-06 21:19:42 +0000357 }
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600358 return vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500359}
360
361void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
362{
Alexander Grafa5954052010-02-22 16:52:14 +0100363 /* Make sure we're not using the vcpu anymore */
364 hrtimer_cancel(&vcpu->arch.dec_timer);
365 tasklet_kill(&vcpu->arch.tasklet);
366
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600367 kvmppc_remove_vcpu_debugfs(vcpu);
Hollis Blancharddb93f572008-11-05 09:36:18 -0600368 kvmppc_core_vcpu_free(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500369}
370
371void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
372{
373 kvm_arch_vcpu_free(vcpu);
374}
375
376int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
377{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600378 return kvmppc_core_pending_dec(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500379}
380
Alexander Graf544c6762009-11-02 12:02:31 +0000381/*
382 * low level hrtimer wake routine. Because this runs in hardirq context
383 * we schedule a tasklet to do the real work.
384 */
385enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
386{
387 struct kvm_vcpu *vcpu;
388
389 vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
390 tasklet_schedule(&vcpu->arch.tasklet);
391
392 return HRTIMER_NORESTART;
393}
394
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500395int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
396{
Alexander Graf544c6762009-11-02 12:02:31 +0000397 hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
398 tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
399 vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000400 vcpu->arch.dec_expires = ~(u64)0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500401
Bharat Bhushan09000ad2011-03-25 10:32:13 +0530402#ifdef CONFIG_KVM_EXIT_TIMING
403 mutex_init(&vcpu->arch.exit_timing_lock);
404#endif
405
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500406 return 0;
407}
408
409void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
410{
Hollis Blanchardecc09812009-01-03 16:22:59 -0600411 kvmppc_mmu_destroy(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500412}
413
414void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
415{
Scott Woodeab17672011-04-27 17:24:10 -0500416#ifdef CONFIG_BOOKE
417 /*
418 * vrsave (formerly usprg0) isn't used by Linux, but may
419 * be used by the guest.
420 *
421 * On non-booke this is associated with Altivec and
422 * is handled by code in book3s.c.
423 */
424 mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
425#endif
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600426 kvmppc_core_vcpu_load(vcpu, cpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000427 vcpu->cpu = smp_processor_id();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500428}
429
430void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
431{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600432 kvmppc_core_vcpu_put(vcpu);
Scott Woodeab17672011-04-27 17:24:10 -0500433#ifdef CONFIG_BOOKE
434 vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
435#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +0000436 vcpu->cpu = -1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500437}
438
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100439int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
Hollis Blanchardf5d09062009-01-04 13:51:09 -0600440 struct kvm_guest_debug *dbg)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500441{
Hollis Blanchardf5d09062009-01-04 13:51:09 -0600442 return -EINVAL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500443}
444
445static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
446 struct kvm_run *run)
447{
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100448 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500449}
450
451static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
452 struct kvm_run *run)
453{
Denis Kirjanov69b61832010-06-11 11:23:26 +0000454 u64 uninitialized_var(gpr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500455
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100456 if (run->mmio.len > sizeof(gpr)) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500457 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
458 return;
459 }
460
461 if (vcpu->arch.mmio_is_bigendian) {
462 switch (run->mmio.len) {
Alexander Grafb104d062010-02-19 11:00:29 +0100463 case 8: gpr = *(u64 *)run->mmio.data; break;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100464 case 4: gpr = *(u32 *)run->mmio.data; break;
465 case 2: gpr = *(u16 *)run->mmio.data; break;
466 case 1: gpr = *(u8 *)run->mmio.data; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500467 }
468 } else {
469 /* Convert BE data from userland back to LE. */
470 switch (run->mmio.len) {
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100471 case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
472 case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
473 case 1: gpr = *(u8 *)run->mmio.data; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500474 }
475 }
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100476
Alexander Graf3587d532010-02-19 11:00:30 +0100477 if (vcpu->arch.mmio_sign_extend) {
478 switch (run->mmio.len) {
479#ifdef CONFIG_PPC64
480 case 4:
481 gpr = (s64)(s32)gpr;
482 break;
483#endif
484 case 2:
485 gpr = (s64)(s16)gpr;
486 break;
487 case 1:
488 gpr = (s64)(s8)gpr;
489 break;
490 }
491 }
492
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100493 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
Alexander Grafb104d062010-02-19 11:00:29 +0100494
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100495 switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
496 case KVM_MMIO_REG_GPR:
Alexander Grafb104d062010-02-19 11:00:29 +0100497 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
498 break;
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100499 case KVM_MMIO_REG_FPR:
500 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
Alexander Grafb104d062010-02-19 11:00:29 +0100501 break;
Alexander Graf287d5612010-04-01 15:33:21 +0200502#ifdef CONFIG_PPC_BOOK3S
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100503 case KVM_MMIO_REG_QPR:
504 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
Alexander Grafb104d062010-02-19 11:00:29 +0100505 break;
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100506 case KVM_MMIO_REG_FQPR:
507 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
508 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
Alexander Grafb104d062010-02-19 11:00:29 +0100509 break;
Alexander Graf287d5612010-04-01 15:33:21 +0200510#endif
Alexander Grafb104d062010-02-19 11:00:29 +0100511 default:
512 BUG();
513 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500514}
515
516int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
517 unsigned int rt, unsigned int bytes, int is_bigendian)
518{
519 if (bytes > sizeof(run->mmio.data)) {
520 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
521 run->mmio.len);
522 }
523
524 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
525 run->mmio.len = bytes;
526 run->mmio.is_write = 0;
527
528 vcpu->arch.io_gpr = rt;
529 vcpu->arch.mmio_is_bigendian = is_bigendian;
530 vcpu->mmio_needed = 1;
531 vcpu->mmio_is_write = 0;
Alexander Graf3587d532010-02-19 11:00:30 +0100532 vcpu->arch.mmio_sign_extend = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500533
534 return EMULATE_DO_MMIO;
535}
536
Alexander Graf3587d532010-02-19 11:00:30 +0100537/* Same as above, but sign extends */
538int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
539 unsigned int rt, unsigned int bytes, int is_bigendian)
540{
541 int r;
542
543 r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
544 vcpu->arch.mmio_sign_extend = 1;
545
546 return r;
547}
548
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500549int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
Alexander Grafb104d062010-02-19 11:00:29 +0100550 u64 val, unsigned int bytes, int is_bigendian)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500551{
552 void *data = run->mmio.data;
553
554 if (bytes > sizeof(run->mmio.data)) {
555 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
556 run->mmio.len);
557 }
558
559 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
560 run->mmio.len = bytes;
561 run->mmio.is_write = 1;
562 vcpu->mmio_needed = 1;
563 vcpu->mmio_is_write = 1;
564
565 /* Store the value at the lowest bytes in 'data'. */
566 if (is_bigendian) {
567 switch (bytes) {
Alexander Grafb104d062010-02-19 11:00:29 +0100568 case 8: *(u64 *)data = val; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500569 case 4: *(u32 *)data = val; break;
570 case 2: *(u16 *)data = val; break;
571 case 1: *(u8 *)data = val; break;
572 }
573 } else {
574 /* Store LE value into 'data'. */
575 switch (bytes) {
576 case 4: st_le32(data, val); break;
577 case 2: st_le16(data, val); break;
578 case 1: *(u8 *)data = val; break;
579 }
580 }
581
582 return EMULATE_DO_MMIO;
583}
584
585int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
586{
587 int r;
588 sigset_t sigsaved;
589
590 if (vcpu->sigset_active)
591 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
592
593 if (vcpu->mmio_needed) {
594 if (!vcpu->mmio_is_write)
595 kvmppc_complete_mmio_load(vcpu, run);
596 vcpu->mmio_needed = 0;
597 } else if (vcpu->arch.dcr_needed) {
598 if (!vcpu->arch.dcr_is_write)
599 kvmppc_complete_dcr_load(vcpu, run);
600 vcpu->arch.dcr_needed = 0;
Alexander Grafad0a0482010-03-24 21:48:30 +0100601 } else if (vcpu->arch.osi_needed) {
602 u64 *gprs = run->osi.gprs;
603 int i;
604
605 for (i = 0; i < 32; i++)
606 kvmppc_set_gpr(vcpu, i, gprs[i]);
607 vcpu->arch.osi_needed = 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000608 } else if (vcpu->arch.hcall_needed) {
609 int i;
610
611 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
612 for (i = 0; i < 9; ++i)
613 kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
614 vcpu->arch.hcall_needed = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500615 }
616
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000617 r = kvmppc_vcpu_run(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500618
619 if (vcpu->sigset_active)
620 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
621
622 return r;
623}
624
625int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
626{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000627 if (irq->irq == KVM_INTERRUPT_UNSET) {
Alexander Graf18978762010-03-24 21:48:18 +0100628 kvmppc_core_dequeue_external(vcpu, irq);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000629 return 0;
630 }
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500631
Paul Mackerras19ccb762011-07-23 17:42:46 +1000632 kvmppc_core_queue_external(vcpu, irq);
Christoffer Dallb6d33832012-03-08 16:44:24 -0500633
Scott Wooddfd4d472011-11-17 12:39:59 +0000634 kvm_vcpu_kick(vcpu);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500635
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500636 return 0;
637}
638
Alexander Graf71fbfd52010-03-24 21:48:29 +0100639static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
640 struct kvm_enable_cap *cap)
641{
642 int r;
643
644 if (cap->flags)
645 return -EINVAL;
646
647 switch (cap->cap) {
Alexander Grafad0a0482010-03-24 21:48:30 +0100648 case KVM_CAP_PPC_OSI:
649 r = 0;
650 vcpu->arch.osi_enabled = true;
651 break;
Alexander Graf930b4122011-08-08 17:29:42 +0200652 case KVM_CAP_PPC_PAPR:
653 r = 0;
654 vcpu->arch.papr_enabled = true;
655 break;
Alexander Grafbf7ca4b2012-02-15 23:40:00 +0000656#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500657 case KVM_CAP_SW_TLB: {
658 struct kvm_config_tlb cfg;
659 void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
660
661 r = -EFAULT;
662 if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
663 break;
664
665 r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
666 break;
667 }
668#endif
Alexander Graf71fbfd52010-03-24 21:48:29 +0100669 default:
670 r = -EINVAL;
671 break;
672 }
673
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200674 if (!r)
675 r = kvmppc_sanity_check(vcpu);
676
Alexander Graf71fbfd52010-03-24 21:48:29 +0100677 return r;
678}
679
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500680int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
681 struct kvm_mp_state *mp_state)
682{
683 return -EINVAL;
684}
685
686int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
687 struct kvm_mp_state *mp_state)
688{
689 return -EINVAL;
690}
691
692long kvm_arch_vcpu_ioctl(struct file *filp,
693 unsigned int ioctl, unsigned long arg)
694{
695 struct kvm_vcpu *vcpu = filp->private_data;
696 void __user *argp = (void __user *)arg;
697 long r;
698
Avi Kivity93736622010-05-13 12:35:17 +0300699 switch (ioctl) {
700 case KVM_INTERRUPT: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500701 struct kvm_interrupt irq;
702 r = -EFAULT;
703 if (copy_from_user(&irq, argp, sizeof(irq)))
Avi Kivity93736622010-05-13 12:35:17 +0300704 goto out;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500705 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity93736622010-05-13 12:35:17 +0300706 goto out;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500707 }
Avi Kivity19483d12010-05-13 12:30:43 +0300708
Alexander Graf71fbfd52010-03-24 21:48:29 +0100709 case KVM_ENABLE_CAP:
710 {
711 struct kvm_enable_cap cap;
712 r = -EFAULT;
713 if (copy_from_user(&cap, argp, sizeof(cap)))
714 goto out;
715 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
716 break;
717 }
Scott Wooddc83b8b2011-08-18 15:25:21 -0500718
Alexander Grafe24ed812011-09-14 10:02:41 +0200719 case KVM_SET_ONE_REG:
720 case KVM_GET_ONE_REG:
721 {
722 struct kvm_one_reg reg;
723 r = -EFAULT;
724 if (copy_from_user(&reg, argp, sizeof(reg)))
725 goto out;
726 if (ioctl == KVM_SET_ONE_REG)
727 r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
728 else
729 r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
730 break;
731 }
732
Alexander Grafbf7ca4b2012-02-15 23:40:00 +0000733#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500734 case KVM_DIRTY_TLB: {
735 struct kvm_dirty_tlb dirty;
736 r = -EFAULT;
737 if (copy_from_user(&dirty, argp, sizeof(dirty)))
738 goto out;
739 r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
740 break;
741 }
742#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500743 default:
744 r = -EINVAL;
745 }
746
747out:
748 return r;
749}
750
Carsten Otte5b1c1492012-01-04 10:25:23 +0100751int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
752{
753 return VM_FAULT_SIGBUS;
754}
755
Alexander Graf15711e92010-07-29 14:48:08 +0200756static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
757{
Stuart Yoder784bafa2012-07-03 05:48:51 +0000758 u32 inst_nop = 0x60000000;
759#ifdef CONFIG_KVM_BOOKE_HV
760 u32 inst_sc1 = 0x44000022;
761 pvinfo->hcall[0] = inst_sc1;
762 pvinfo->hcall[1] = inst_nop;
763 pvinfo->hcall[2] = inst_nop;
764 pvinfo->hcall[3] = inst_nop;
765#else
Alexander Graf15711e92010-07-29 14:48:08 +0200766 u32 inst_lis = 0x3c000000;
767 u32 inst_ori = 0x60000000;
Alexander Graf15711e92010-07-29 14:48:08 +0200768 u32 inst_sc = 0x44000002;
769 u32 inst_imm_mask = 0xffff;
770
771 /*
772 * The hypercall to get into KVM from within guest context is as
773 * follows:
774 *
775 * lis r0, r0, KVM_SC_MAGIC_R0@h
776 * ori r0, KVM_SC_MAGIC_R0@l
777 * sc
778 * nop
779 */
780 pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask);
781 pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask);
782 pvinfo->hcall[2] = inst_sc;
783 pvinfo->hcall[3] = inst_nop;
Stuart Yoder784bafa2012-07-03 05:48:51 +0000784#endif
Alexander Graf15711e92010-07-29 14:48:08 +0200785
Liu Yu-B132019202e072012-07-03 05:48:52 +0000786 pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
787
Alexander Graf15711e92010-07-29 14:48:08 +0200788 return 0;
789}
790
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500791long kvm_arch_vm_ioctl(struct file *filp,
792 unsigned int ioctl, unsigned long arg)
793{
Alexander Graf15711e92010-07-29 14:48:08 +0200794 void __user *argp = (void __user *)arg;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500795 long r;
796
797 switch (ioctl) {
Alexander Graf15711e92010-07-29 14:48:08 +0200798 case KVM_PPC_GET_PVINFO: {
799 struct kvm_ppc_pvinfo pvinfo;
Vasiliy Kulikovd8cdddc2010-10-30 13:04:24 +0400800 memset(&pvinfo, 0, sizeof(pvinfo));
Alexander Graf15711e92010-07-29 14:48:08 +0200801 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
802 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
803 r = -EFAULT;
804 goto out;
805 }
806
807 break;
808 }
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000809#ifdef CONFIG_PPC_BOOK3S_64
David Gibson54738c02011-06-29 00:22:41 +0000810 case KVM_CREATE_SPAPR_TCE: {
811 struct kvm_create_spapr_tce create_tce;
812 struct kvm *kvm = filp->private_data;
813
814 r = -EFAULT;
815 if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
816 goto out;
817 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
818 goto out;
819 }
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000820#endif /* CONFIG_PPC_BOOK3S_64 */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000821
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000822#ifdef CONFIG_KVM_BOOK3S_64_HV
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000823 case KVM_ALLOCATE_RMA: {
824 struct kvm *kvm = filp->private_data;
825 struct kvm_allocate_rma rma;
826
827 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
828 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
829 r = -EFAULT;
830 break;
831 }
Paul Mackerras32fad282012-05-04 02:32:53 +0000832
833 case KVM_PPC_ALLOCATE_HTAB: {
834 struct kvm *kvm = filp->private_data;
835 u32 htab_order;
836
837 r = -EFAULT;
838 if (get_user(htab_order, (u32 __user *)argp))
839 break;
840 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
841 if (r)
842 break;
843 r = -EFAULT;
844 if (put_user(htab_order, (u32 __user *)argp))
845 break;
846 r = 0;
847 break;
848 }
David Gibson54738c02011-06-29 00:22:41 +0000849#endif /* CONFIG_KVM_BOOK3S_64_HV */
850
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +0000851#ifdef CONFIG_PPC_BOOK3S_64
852 case KVM_PPC_GET_SMMU_INFO: {
853 struct kvm *kvm = filp->private_data;
854 struct kvm_ppc_smmu_info info;
855
856 memset(&info, 0, sizeof(info));
857 r = kvm_vm_ioctl_get_smmu_info(kvm, &info);
858 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
859 r = -EFAULT;
860 break;
861 }
862#endif /* CONFIG_PPC_BOOK3S_64 */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500863 default:
Avi Kivity367e1312009-08-26 14:57:07 +0300864 r = -ENOTTY;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500865 }
866
Alexander Graf15711e92010-07-29 14:48:08 +0200867out:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500868 return r;
869}
870
Scott Wood043cc4d2011-12-20 15:34:20 +0000871static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
872static unsigned long nr_lpids;
873
874long kvmppc_alloc_lpid(void)
875{
876 long lpid;
877
878 do {
879 lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
880 if (lpid >= nr_lpids) {
881 pr_err("%s: No LPIDs free\n", __func__);
882 return -ENOMEM;
883 }
884 } while (test_and_set_bit(lpid, lpid_inuse));
885
886 return lpid;
887}
888
889void kvmppc_claim_lpid(long lpid)
890{
891 set_bit(lpid, lpid_inuse);
892}
893
894void kvmppc_free_lpid(long lpid)
895{
896 clear_bit(lpid, lpid_inuse);
897}
898
899void kvmppc_init_lpid(unsigned long nr_lpids_param)
900{
901 nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
902 memset(lpid_inuse, 0, sizeof(lpid_inuse));
903}
904
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500905int kvm_arch_init(void *opaque)
906{
907 return 0;
908}
909
910void kvm_arch_exit(void)
911{
912}