xref: /openbmc/linux/arch/powerpc/kvm/powerpc.c (revision 9ac8d3fb)
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License, version 2, as
4  * published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
14  *
15  * Copyright IBM Corp. 2007
16  *
17  * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18  *          Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
19  */
20 
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/kvm_host.h>
24 #include <linux/module.h>
25 #include <linux/vmalloc.h>
26 #include <linux/fs.h>
27 #include <asm/cputable.h>
28 #include <asm/uaccess.h>
29 #include <asm/kvm_ppc.h>
30 #include <asm/tlbflush.h>
31 
32 
33 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
34 {
35 	return gfn;
36 }
37 
38 int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
39 {
40 	return !!(v->arch.pending_exceptions);
41 }
42 
43 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
44 {
45 	return !(v->arch.msr & MSR_WE);
46 }
47 
48 
49 int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
50 {
51 	enum emulation_result er;
52 	int r;
53 
54 	er = kvmppc_emulate_instruction(run, vcpu);
55 	switch (er) {
56 	case EMULATE_DONE:
57 		/* Future optimization: only reload non-volatiles if they were
58 		 * actually modified. */
59 		r = RESUME_GUEST_NV;
60 		break;
61 	case EMULATE_DO_MMIO:
62 		run->exit_reason = KVM_EXIT_MMIO;
63 		/* We must reload nonvolatiles because "update" load/store
64 		 * instructions modify register state. */
65 		/* Future optimization: only reload non-volatiles if they were
66 		 * actually modified. */
67 		r = RESUME_HOST_NV;
68 		break;
69 	case EMULATE_FAIL:
70 		/* XXX Deliver Program interrupt to guest. */
71 		printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__,
72 		       vcpu->arch.last_inst);
73 		r = RESUME_HOST;
74 		break;
75 	default:
76 		BUG();
77 	}
78 
79 	return r;
80 }
81 
82 void kvm_arch_hardware_enable(void *garbage)
83 {
84 }
85 
86 void kvm_arch_hardware_disable(void *garbage)
87 {
88 }
89 
90 int kvm_arch_hardware_setup(void)
91 {
92 	return 0;
93 }
94 
95 void kvm_arch_hardware_unsetup(void)
96 {
97 }
98 
99 void kvm_arch_check_processor_compat(void *rtn)
100 {
101 	int r;
102 
103 	if (strcmp(cur_cpu_spec->platform, "ppc440") == 0)
104 		r = 0;
105 	else
106 		r = -ENOTSUPP;
107 
108 	*(int *)rtn = r;
109 }
110 
111 struct kvm *kvm_arch_create_vm(void)
112 {
113 	struct kvm *kvm;
114 
115 	kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
116 	if (!kvm)
117 		return ERR_PTR(-ENOMEM);
118 
119 	return kvm;
120 }
121 
122 static void kvmppc_free_vcpus(struct kvm *kvm)
123 {
124 	unsigned int i;
125 
126 	for (i = 0; i < KVM_MAX_VCPUS; ++i) {
127 		if (kvm->vcpus[i]) {
128 			kvm_arch_vcpu_free(kvm->vcpus[i]);
129 			kvm->vcpus[i] = NULL;
130 		}
131 	}
132 }
133 
134 void kvm_arch_destroy_vm(struct kvm *kvm)
135 {
136 	kvmppc_free_vcpus(kvm);
137 	kvm_free_physmem(kvm);
138 	kfree(kvm);
139 }
140 
141 int kvm_dev_ioctl_check_extension(long ext)
142 {
143 	int r;
144 
145 	switch (ext) {
146 	case KVM_CAP_USER_MEMORY:
147 		r = 1;
148 		break;
149 	case KVM_CAP_COALESCED_MMIO:
150 		r = KVM_COALESCED_MMIO_PAGE_OFFSET;
151 		break;
152 	default:
153 		r = 0;
154 		break;
155 	}
156 	return r;
157 
158 }
159 
160 long kvm_arch_dev_ioctl(struct file *filp,
161                         unsigned int ioctl, unsigned long arg)
162 {
163 	return -EINVAL;
164 }
165 
166 int kvm_arch_set_memory_region(struct kvm *kvm,
167                                struct kvm_userspace_memory_region *mem,
168                                struct kvm_memory_slot old,
169                                int user_alloc)
170 {
171 	return 0;
172 }
173 
174 void kvm_arch_flush_shadow(struct kvm *kvm)
175 {
176 }
177 
178 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
179 {
180 	struct kvm_vcpu *vcpu;
181 	int err;
182 
183 	vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
184 	if (!vcpu) {
185 		err = -ENOMEM;
186 		goto out;
187 	}
188 
189 	err = kvm_vcpu_init(vcpu, kvm, id);
190 	if (err)
191 		goto free_vcpu;
192 
193 	return vcpu;
194 
195 free_vcpu:
196 	kmem_cache_free(kvm_vcpu_cache, vcpu);
197 out:
198 	return ERR_PTR(err);
199 }
200 
201 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
202 {
203 	kvm_vcpu_uninit(vcpu);
204 	kmem_cache_free(kvm_vcpu_cache, vcpu);
205 }
206 
207 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
208 {
209 	kvm_arch_vcpu_free(vcpu);
210 }
211 
212 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
213 {
214 	unsigned int priority = exception_priority[BOOKE_INTERRUPT_DECREMENTER];
215 
216 	return test_bit(priority, &vcpu->arch.pending_exceptions);
217 }
218 
219 static void kvmppc_decrementer_func(unsigned long data)
220 {
221 	struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
222 
223 	kvmppc_queue_exception(vcpu, BOOKE_INTERRUPT_DECREMENTER);
224 
225 	if (waitqueue_active(&vcpu->wq)) {
226 		wake_up_interruptible(&vcpu->wq);
227 		vcpu->stat.halt_wakeup++;
228 	}
229 }
230 
231 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
232 {
233 	setup_timer(&vcpu->arch.dec_timer, kvmppc_decrementer_func,
234 	            (unsigned long)vcpu);
235 
236 	return 0;
237 }
238 
239 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
240 {
241 }
242 
243 /* Note: clearing MSR[DE] just means that the debug interrupt will not be
244  * delivered *immediately*. Instead, it simply sets the appropriate DBSR bits.
245  * If those DBSR bits are still set when MSR[DE] is re-enabled, the interrupt
246  * will be delivered as an "imprecise debug event" (which is indicated by
247  * DBSR[IDE].
248  */
249 static void kvmppc_disable_debug_interrupts(void)
250 {
251 	mtmsr(mfmsr() & ~MSR_DE);
252 }
253 
254 static void kvmppc_restore_host_debug_state(struct kvm_vcpu *vcpu)
255 {
256 	kvmppc_disable_debug_interrupts();
257 
258 	mtspr(SPRN_IAC1, vcpu->arch.host_iac[0]);
259 	mtspr(SPRN_IAC2, vcpu->arch.host_iac[1]);
260 	mtspr(SPRN_IAC3, vcpu->arch.host_iac[2]);
261 	mtspr(SPRN_IAC4, vcpu->arch.host_iac[3]);
262 	mtspr(SPRN_DBCR1, vcpu->arch.host_dbcr1);
263 	mtspr(SPRN_DBCR2, vcpu->arch.host_dbcr2);
264 	mtspr(SPRN_DBCR0, vcpu->arch.host_dbcr0);
265 	mtmsr(vcpu->arch.host_msr);
266 }
267 
268 static void kvmppc_load_guest_debug_registers(struct kvm_vcpu *vcpu)
269 {
270 	struct kvm_guest_debug *dbg = &vcpu->guest_debug;
271 	u32 dbcr0 = 0;
272 
273 	vcpu->arch.host_msr = mfmsr();
274 	kvmppc_disable_debug_interrupts();
275 
276 	/* Save host debug register state. */
277 	vcpu->arch.host_iac[0] = mfspr(SPRN_IAC1);
278 	vcpu->arch.host_iac[1] = mfspr(SPRN_IAC2);
279 	vcpu->arch.host_iac[2] = mfspr(SPRN_IAC3);
280 	vcpu->arch.host_iac[3] = mfspr(SPRN_IAC4);
281 	vcpu->arch.host_dbcr0 = mfspr(SPRN_DBCR0);
282 	vcpu->arch.host_dbcr1 = mfspr(SPRN_DBCR1);
283 	vcpu->arch.host_dbcr2 = mfspr(SPRN_DBCR2);
284 
285 	/* set registers up for guest */
286 
287 	if (dbg->bp[0]) {
288 		mtspr(SPRN_IAC1, dbg->bp[0]);
289 		dbcr0 |= DBCR0_IAC1 | DBCR0_IDM;
290 	}
291 	if (dbg->bp[1]) {
292 		mtspr(SPRN_IAC2, dbg->bp[1]);
293 		dbcr0 |= DBCR0_IAC2 | DBCR0_IDM;
294 	}
295 	if (dbg->bp[2]) {
296 		mtspr(SPRN_IAC3, dbg->bp[2]);
297 		dbcr0 |= DBCR0_IAC3 | DBCR0_IDM;
298 	}
299 	if (dbg->bp[3]) {
300 		mtspr(SPRN_IAC4, dbg->bp[3]);
301 		dbcr0 |= DBCR0_IAC4 | DBCR0_IDM;
302 	}
303 
304 	mtspr(SPRN_DBCR0, dbcr0);
305 	mtspr(SPRN_DBCR1, 0);
306 	mtspr(SPRN_DBCR2, 0);
307 }
308 
309 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
310 {
311 	int i;
312 
313 	if (vcpu->guest_debug.enabled)
314 		kvmppc_load_guest_debug_registers(vcpu);
315 
316 	/* Mark every guest entry in the shadow TLB entry modified, so that they
317 	 * will all be reloaded on the next vcpu run (instead of being
318 	 * demand-faulted). */
319 	for (i = 0; i <= tlb_44x_hwater; i++)
320 		kvmppc_tlbe_set_modified(vcpu, i);
321 }
322 
323 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
324 {
325 	if (vcpu->guest_debug.enabled)
326 		kvmppc_restore_host_debug_state(vcpu);
327 
328 	/* Don't leave guest TLB entries resident when being de-scheduled. */
329 	/* XXX It would be nice to differentiate between heavyweight exit and
330 	 * sched_out here, since we could avoid the TLB flush for heavyweight
331 	 * exits. */
332 	_tlbia();
333 }
334 
335 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
336                                     struct kvm_debug_guest *dbg)
337 {
338 	int i;
339 
340 	vcpu->guest_debug.enabled = dbg->enabled;
341 	if (vcpu->guest_debug.enabled) {
342 		for (i=0; i < ARRAY_SIZE(vcpu->guest_debug.bp); i++) {
343 			if (dbg->breakpoints[i].enabled)
344 				vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
345 			else
346 				vcpu->guest_debug.bp[i] = 0;
347 		}
348 	}
349 
350 	return 0;
351 }
352 
353 static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
354                                      struct kvm_run *run)
355 {
356 	u32 *gpr = &vcpu->arch.gpr[vcpu->arch.io_gpr];
357 	*gpr = run->dcr.data;
358 }
359 
360 static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
361                                       struct kvm_run *run)
362 {
363 	u32 *gpr = &vcpu->arch.gpr[vcpu->arch.io_gpr];
364 
365 	if (run->mmio.len > sizeof(*gpr)) {
366 		printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
367 		return;
368 	}
369 
370 	if (vcpu->arch.mmio_is_bigendian) {
371 		switch (run->mmio.len) {
372 		case 4: *gpr = *(u32 *)run->mmio.data; break;
373 		case 2: *gpr = *(u16 *)run->mmio.data; break;
374 		case 1: *gpr = *(u8 *)run->mmio.data; break;
375 		}
376 	} else {
377 		/* Convert BE data from userland back to LE. */
378 		switch (run->mmio.len) {
379 		case 4: *gpr = ld_le32((u32 *)run->mmio.data); break;
380 		case 2: *gpr = ld_le16((u16 *)run->mmio.data); break;
381 		case 1: *gpr = *(u8 *)run->mmio.data; break;
382 		}
383 	}
384 }
385 
386 int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
387                        unsigned int rt, unsigned int bytes, int is_bigendian)
388 {
389 	if (bytes > sizeof(run->mmio.data)) {
390 		printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
391 		       run->mmio.len);
392 	}
393 
394 	run->mmio.phys_addr = vcpu->arch.paddr_accessed;
395 	run->mmio.len = bytes;
396 	run->mmio.is_write = 0;
397 
398 	vcpu->arch.io_gpr = rt;
399 	vcpu->arch.mmio_is_bigendian = is_bigendian;
400 	vcpu->mmio_needed = 1;
401 	vcpu->mmio_is_write = 0;
402 
403 	return EMULATE_DO_MMIO;
404 }
405 
406 int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
407                         u32 val, unsigned int bytes, int is_bigendian)
408 {
409 	void *data = run->mmio.data;
410 
411 	if (bytes > sizeof(run->mmio.data)) {
412 		printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
413 		       run->mmio.len);
414 	}
415 
416 	run->mmio.phys_addr = vcpu->arch.paddr_accessed;
417 	run->mmio.len = bytes;
418 	run->mmio.is_write = 1;
419 	vcpu->mmio_needed = 1;
420 	vcpu->mmio_is_write = 1;
421 
422 	/* Store the value at the lowest bytes in 'data'. */
423 	if (is_bigendian) {
424 		switch (bytes) {
425 		case 4: *(u32 *)data = val; break;
426 		case 2: *(u16 *)data = val; break;
427 		case 1: *(u8  *)data = val; break;
428 		}
429 	} else {
430 		/* Store LE value into 'data'. */
431 		switch (bytes) {
432 		case 4: st_le32(data, val); break;
433 		case 2: st_le16(data, val); break;
434 		case 1: *(u8 *)data = val; break;
435 		}
436 	}
437 
438 	return EMULATE_DO_MMIO;
439 }
440 
441 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
442 {
443 	int r;
444 	sigset_t sigsaved;
445 
446 	vcpu_load(vcpu);
447 
448 	if (vcpu->sigset_active)
449 		sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
450 
451 	if (vcpu->mmio_needed) {
452 		if (!vcpu->mmio_is_write)
453 			kvmppc_complete_mmio_load(vcpu, run);
454 		vcpu->mmio_needed = 0;
455 	} else if (vcpu->arch.dcr_needed) {
456 		if (!vcpu->arch.dcr_is_write)
457 			kvmppc_complete_dcr_load(vcpu, run);
458 		vcpu->arch.dcr_needed = 0;
459 	}
460 
461 	kvmppc_check_and_deliver_interrupts(vcpu);
462 
463 	local_irq_disable();
464 	kvm_guest_enter();
465 	r = __kvmppc_vcpu_run(run, vcpu);
466 	kvm_guest_exit();
467 	local_irq_enable();
468 
469 	if (vcpu->sigset_active)
470 		sigprocmask(SIG_SETMASK, &sigsaved, NULL);
471 
472 	vcpu_put(vcpu);
473 
474 	return r;
475 }
476 
477 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
478 {
479 	kvmppc_queue_exception(vcpu, BOOKE_INTERRUPT_EXTERNAL);
480 
481 	if (waitqueue_active(&vcpu->wq)) {
482 		wake_up_interruptible(&vcpu->wq);
483 		vcpu->stat.halt_wakeup++;
484 	}
485 
486 	return 0;
487 }
488 
489 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
490                                     struct kvm_mp_state *mp_state)
491 {
492 	return -EINVAL;
493 }
494 
495 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
496                                     struct kvm_mp_state *mp_state)
497 {
498 	return -EINVAL;
499 }
500 
501 long kvm_arch_vcpu_ioctl(struct file *filp,
502                          unsigned int ioctl, unsigned long arg)
503 {
504 	struct kvm_vcpu *vcpu = filp->private_data;
505 	void __user *argp = (void __user *)arg;
506 	long r;
507 
508 	switch (ioctl) {
509 	case KVM_INTERRUPT: {
510 		struct kvm_interrupt irq;
511 		r = -EFAULT;
512 		if (copy_from_user(&irq, argp, sizeof(irq)))
513 			goto out;
514 		r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
515 		break;
516 	}
517 	default:
518 		r = -EINVAL;
519 	}
520 
521 out:
522 	return r;
523 }
524 
525 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
526 {
527 	return -ENOTSUPP;
528 }
529 
530 long kvm_arch_vm_ioctl(struct file *filp,
531                        unsigned int ioctl, unsigned long arg)
532 {
533 	long r;
534 
535 	switch (ioctl) {
536 	default:
537 		r = -EINVAL;
538 	}
539 
540 	return r;
541 }
542 
543 int kvm_arch_init(void *opaque)
544 {
545 	return 0;
546 }
547 
548 void kvm_arch_exit(void)
549 {
550 }
551