blob: 61d293465e814eb66cd13b1cf4b9976097af8912 [file] [log] [blame]
Paul Mackerrasde56a942011-06-29 00:21:34 +00001/*
2 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
3 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
4 *
5 * Authors:
6 * Paul Mackerras <paulus@au1.ibm.com>
7 * Alexander Graf <agraf@suse.de>
8 * Kevin Wolf <mail@kevin-wolf.de>
9 *
10 * Description: KVM functions specific to running on Book 3S
11 * processors in hypervisor mode (specifically POWER7 and later).
12 *
13 * This file is derived from arch/powerpc/kvm/book3s.c,
14 * by Alexander Graf <agraf@suse.de>.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License, version 2, as
18 * published by the Free Software Foundation.
19 */
20
21#include <linux/kvm_host.h>
22#include <linux/err.h>
23#include <linux/slab.h>
24#include <linux/preempt.h>
25#include <linux/sched.h>
26#include <linux/delay.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040027#include <linux/export.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000028#include <linux/fs.h>
29#include <linux/anon_inodes.h>
30#include <linux/cpumask.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000031#include <linux/spinlock.h>
32#include <linux/page-flags.h>
Paul Mackerras2c9097e2012-09-11 13:27:01 +000033#include <linux/srcu.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000034
35#include <asm/reg.h>
36#include <asm/cputable.h>
37#include <asm/cacheflush.h>
38#include <asm/tlbflush.h>
39#include <asm/uaccess.h>
40#include <asm/io.h>
41#include <asm/kvm_ppc.h>
42#include <asm/kvm_book3s.h>
43#include <asm/mmu_context.h>
44#include <asm/lppaca.h>
45#include <asm/processor.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000046#include <asm/cputhreads.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000047#include <asm/page.h>
Michael Neulingde1d9242011-11-09 20:39:49 +000048#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010049#include <asm/switch_to.h>
Paul Mackerras512691d2012-10-15 01:15:41 +000050#include <asm/smp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000051#include <linux/gfp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000052#include <linux/vmalloc.h>
53#include <linux/highmem.h>
Paul Mackerrasc77162d2011-12-12 12:31:00 +000054#include <linux/hugetlb.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000055
56/* #define EXIT_DEBUG */
57/* #define EXIT_DEBUG_SIMPLE */
58/* #define EXIT_DEBUG_INT */
59
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000060/* Used to indicate that a guest page fault needs to be handled */
61#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
62
Paul Mackerras19ccb762011-07-23 17:42:46 +100063static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +000064static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +100065
Paul Mackerrasde56a942011-06-29 00:21:34 +000066void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
67{
Paul Mackerras0456ec42012-02-03 00:56:21 +000068 struct kvmppc_vcore *vc = vcpu->arch.vcore;
69
Paul Mackerras0456ec42012-02-03 00:56:21 +000070 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
71 vc->stolen_tb += mftb() - vc->preempt_tb;
Paul Mackerrasde56a942011-06-29 00:21:34 +000072}
73
74void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
75{
Paul Mackerras0456ec42012-02-03 00:56:21 +000076 struct kvmppc_vcore *vc = vcpu->arch.vcore;
77
78 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
79 vc->preempt_tb = mftb();
Paul Mackerrasde56a942011-06-29 00:21:34 +000080}
81
Paul Mackerrasde56a942011-06-29 00:21:34 +000082void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
83{
84 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +100085 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +000086}
87
88void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
89{
90 vcpu->arch.pvr = pvr;
91}
92
93void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
94{
95 int r;
96
97 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
98 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
99 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
100 for (r = 0; r < 16; ++r)
101 pr_err("r%2d = %.16lx r%d = %.16lx\n",
102 r, kvmppc_get_gpr(vcpu, r),
103 r+16, kvmppc_get_gpr(vcpu, r+16));
104 pr_err("ctr = %.16lx lr = %.16lx\n",
105 vcpu->arch.ctr, vcpu->arch.lr);
106 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
107 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
108 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
109 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
110 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
111 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
112 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
113 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
114 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
115 pr_err("fault dar = %.16lx dsisr = %.8x\n",
116 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
117 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
118 for (r = 0; r < vcpu->arch.slb_max; ++r)
119 pr_err(" ESID = %.16llx VSID = %.16llx\n",
120 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
121 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000122 vcpu->kvm->arch.lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000123 vcpu->arch.last_inst);
124}
125
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000126struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
127{
128 int r;
129 struct kvm_vcpu *v, *ret = NULL;
130
131 mutex_lock(&kvm->lock);
132 kvm_for_each_vcpu(r, v, kvm) {
133 if (v->vcpu_id == id) {
134 ret = v;
135 break;
136 }
137 }
138 mutex_unlock(&kvm->lock);
139 return ret;
140}
141
142static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
143{
144 vpa->shared_proc = 1;
145 vpa->yield_count = 1;
146}
147
Paul Mackerras55b665b2012-09-25 20:33:06 +0000148static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
149 unsigned long addr, unsigned long len)
150{
151 /* check address is cacheline aligned */
152 if (addr & (L1_CACHE_BYTES - 1))
153 return -EINVAL;
154 spin_lock(&vcpu->arch.vpa_update_lock);
155 if (v->next_gpa != addr || v->len != len) {
156 v->next_gpa = addr;
157 v->len = addr ? len : 0;
158 v->update_pending = 1;
159 }
160 spin_unlock(&vcpu->arch.vpa_update_lock);
161 return 0;
162}
163
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000164/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
165struct reg_vpa {
166 u32 dummy;
167 union {
168 u16 hword;
169 u32 word;
170 } length;
171};
172
173static int vpa_is_registered(struct kvmppc_vpa *vpap)
174{
175 if (vpap->update_pending)
176 return vpap->next_gpa != 0;
177 return vpap->pinned_addr != NULL;
178}
179
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000180static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
181 unsigned long flags,
182 unsigned long vcpuid, unsigned long vpa)
183{
184 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000185 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000186 void *va;
187 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000188 int err;
189 int subfunc;
190 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000191
192 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
193 if (!tvcpu)
194 return H_PARAMETER;
195
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000196 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
197 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
198 subfunc == H_VPA_REG_SLB) {
199 /* Registering new area - address must be cache-line aligned */
200 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000201 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000202
203 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000204 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
205 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000206 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000207 if (subfunc == H_VPA_REG_VPA)
208 len = ((struct reg_vpa *)va)->length.hword;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000209 else
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000210 len = ((struct reg_vpa *)va)->length.word;
211 kvmppc_unpin_guest_page(kvm, va);
212
213 /* Check length */
214 if (len > nb || len < sizeof(struct reg_vpa))
215 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000216 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000217 vpa = 0;
218 len = 0;
219 }
220
221 err = H_PARAMETER;
222 vpap = NULL;
223 spin_lock(&tvcpu->arch.vpa_update_lock);
224
225 switch (subfunc) {
226 case H_VPA_REG_VPA: /* register VPA */
227 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000228 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000229 vpap = &tvcpu->arch.vpa;
230 err = 0;
231 break;
232
233 case H_VPA_REG_DTL: /* register DTL */
234 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000235 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000236 len -= len % sizeof(struct dtl_entry);
237
238 /* Check that they have previously registered a VPA */
239 err = H_RESOURCE;
240 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000241 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000242
243 vpap = &tvcpu->arch.dtl;
244 err = 0;
245 break;
246
247 case H_VPA_REG_SLB: /* register SLB shadow buffer */
248 /* Check that they have previously registered a VPA */
249 err = H_RESOURCE;
250 if (!vpa_is_registered(&tvcpu->arch.vpa))
251 break;
252
253 vpap = &tvcpu->arch.slb_shadow;
254 err = 0;
255 break;
256
257 case H_VPA_DEREG_VPA: /* deregister VPA */
258 /* Check they don't still have a DTL or SLB buf registered */
259 err = H_RESOURCE;
260 if (vpa_is_registered(&tvcpu->arch.dtl) ||
261 vpa_is_registered(&tvcpu->arch.slb_shadow))
262 break;
263
264 vpap = &tvcpu->arch.vpa;
265 err = 0;
266 break;
267
268 case H_VPA_DEREG_DTL: /* deregister DTL */
269 vpap = &tvcpu->arch.dtl;
270 err = 0;
271 break;
272
273 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
274 vpap = &tvcpu->arch.slb_shadow;
275 err = 0;
276 break;
277 }
278
279 if (vpap) {
280 vpap->next_gpa = vpa;
281 vpap->len = len;
282 vpap->update_pending = 1;
283 }
284
285 spin_unlock(&tvcpu->arch.vpa_update_lock);
286
287 return err;
288}
289
Paul Mackerras081f3232012-06-01 20:20:24 +1000290static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000291{
Paul Mackerras081f3232012-06-01 20:20:24 +1000292 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000293 void *va;
294 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000295 unsigned long gpa;
296
297 /*
298 * We need to pin the page pointed to by vpap->next_gpa,
299 * but we can't call kvmppc_pin_guest_page under the lock
300 * as it does get_user_pages() and down_read(). So we
301 * have to drop the lock, pin the page, then get the lock
302 * again and check that a new area didn't get registered
303 * in the meantime.
304 */
305 for (;;) {
306 gpa = vpap->next_gpa;
307 spin_unlock(&vcpu->arch.vpa_update_lock);
308 va = NULL;
309 nb = 0;
310 if (gpa)
311 va = kvmppc_pin_guest_page(kvm, vpap->next_gpa, &nb);
312 spin_lock(&vcpu->arch.vpa_update_lock);
313 if (gpa == vpap->next_gpa)
314 break;
315 /* sigh... unpin that one and try again */
316 if (va)
317 kvmppc_unpin_guest_page(kvm, va);
318 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000319
320 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000321 if (va && nb < vpap->len) {
322 /*
323 * If it's now too short, it must be that userspace
324 * has changed the mappings underlying guest memory,
325 * so unregister the region.
326 */
327 kvmppc_unpin_guest_page(kvm, va);
328 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000329 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000330 if (vpap->pinned_addr)
331 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr);
332 vpap->pinned_addr = va;
333 if (va)
334 vpap->pinned_end = va + vpap->len;
335}
Paul Mackerras93e60242011-12-12 12:28:55 +0000336
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000337static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
338{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000339 if (!(vcpu->arch.vpa.update_pending ||
340 vcpu->arch.slb_shadow.update_pending ||
341 vcpu->arch.dtl.update_pending))
342 return;
343
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000344 spin_lock(&vcpu->arch.vpa_update_lock);
345 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000346 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000347 if (vcpu->arch.vpa.pinned_addr)
348 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000349 }
350 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000351 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000352 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
353 vcpu->arch.dtl_index = 0;
354 }
355 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000356 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000357 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000358}
359
Paul Mackerras0456ec42012-02-03 00:56:21 +0000360static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
361 struct kvmppc_vcore *vc)
362{
363 struct dtl_entry *dt;
364 struct lppaca *vpa;
365 unsigned long old_stolen;
366
367 dt = vcpu->arch.dtl_ptr;
368 vpa = vcpu->arch.vpa.pinned_addr;
369 old_stolen = vcpu->arch.stolen_logged;
370 vcpu->arch.stolen_logged = vc->stolen_tb;
371 if (!dt || !vpa)
372 return;
373 memset(dt, 0, sizeof(struct dtl_entry));
374 dt->dispatch_reason = 7;
375 dt->processor_id = vc->pcpu + vcpu->arch.ptid;
376 dt->timebase = mftb();
377 dt->enqueue_to_dispatch_time = vc->stolen_tb - old_stolen;
378 dt->srr0 = kvmppc_get_pc(vcpu);
379 dt->srr1 = vcpu->arch.shregs.msr;
380 ++dt;
381 if (dt == vcpu->arch.dtl.pinned_end)
382 dt = vcpu->arch.dtl.pinned_addr;
383 vcpu->arch.dtl_ptr = dt;
384 /* order writing *dt vs. writing vpa->dtl_idx */
385 smp_wmb();
386 vpa->dtl_idx = ++vcpu->arch.dtl_index;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000387}
388
389int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
390{
391 unsigned long req = kvmppc_get_gpr(vcpu, 3);
392 unsigned long target, ret = H_SUCCESS;
393 struct kvm_vcpu *tvcpu;
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000394 int idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000395
396 switch (req) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000397 case H_ENTER:
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000398 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000399 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
400 kvmppc_get_gpr(vcpu, 5),
401 kvmppc_get_gpr(vcpu, 6),
402 kvmppc_get_gpr(vcpu, 7));
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000403 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000404 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000405 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000406 break;
407 case H_PROD:
408 target = kvmppc_get_gpr(vcpu, 4);
409 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
410 if (!tvcpu) {
411 ret = H_PARAMETER;
412 break;
413 }
414 tvcpu->arch.prodded = 1;
415 smp_mb();
416 if (vcpu->arch.ceded) {
417 if (waitqueue_active(&vcpu->wq)) {
418 wake_up_interruptible(&vcpu->wq);
419 vcpu->stat.halt_wakeup++;
420 }
421 }
422 break;
423 case H_CONFER:
424 break;
425 case H_REGISTER_VPA:
426 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
427 kvmppc_get_gpr(vcpu, 5),
428 kvmppc_get_gpr(vcpu, 6));
429 break;
430 default:
431 return RESUME_HOST;
432 }
433 kvmppc_set_gpr(vcpu, 3, ret);
434 vcpu->arch.hcall_needed = 0;
435 return RESUME_GUEST;
436}
437
Paul Mackerrasde56a942011-06-29 00:21:34 +0000438static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
439 struct task_struct *tsk)
440{
441 int r = RESUME_HOST;
442
443 vcpu->stat.sum_exits++;
444
445 run->exit_reason = KVM_EXIT_UNKNOWN;
446 run->ready_for_interrupt_injection = 1;
447 switch (vcpu->arch.trap) {
448 /* We're good on these - the host merely wanted to get our attention */
449 case BOOK3S_INTERRUPT_HV_DECREMENTER:
450 vcpu->stat.dec_exits++;
451 r = RESUME_GUEST;
452 break;
453 case BOOK3S_INTERRUPT_EXTERNAL:
454 vcpu->stat.ext_intr_exits++;
455 r = RESUME_GUEST;
456 break;
457 case BOOK3S_INTERRUPT_PERFMON:
458 r = RESUME_GUEST;
459 break;
460 case BOOK3S_INTERRUPT_PROGRAM:
461 {
462 ulong flags;
463 /*
464 * Normally program interrupts are delivered directly
465 * to the guest by the hardware, but we can get here
466 * as a result of a hypervisor emulation interrupt
467 * (e40) getting turned into a 700 by BML RTAS.
468 */
469 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
470 kvmppc_core_queue_program(vcpu, flags);
471 r = RESUME_GUEST;
472 break;
473 }
474 case BOOK3S_INTERRUPT_SYSCALL:
475 {
476 /* hcall - punt to userspace */
477 int i;
478
479 if (vcpu->arch.shregs.msr & MSR_PR) {
480 /* sc 1 from userspace - reflect to guest syscall */
481 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
482 r = RESUME_GUEST;
483 break;
484 }
485 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
486 for (i = 0; i < 9; ++i)
487 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
488 run->exit_reason = KVM_EXIT_PAPR_HCALL;
489 vcpu->arch.hcall_needed = 1;
490 r = RESUME_HOST;
491 break;
492 }
493 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000494 * We get these next two if the guest accesses a page which it thinks
495 * it has mapped but which is not actually present, either because
496 * it is for an emulated I/O device or because the corresonding
497 * host page has been paged out. Any other HDSI/HISI interrupts
498 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000499 */
500 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000501 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000502 break;
503 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000504 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
505 vcpu->arch.fault_dsisr = 0;
506 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000507 break;
508 /*
509 * This occurs if the guest executes an illegal instruction.
510 * We just generate a program interrupt to the guest, since
511 * we don't emulate any guest instructions at this stage.
512 */
513 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
514 kvmppc_core_queue_program(vcpu, 0x80000);
515 r = RESUME_GUEST;
516 break;
517 default:
518 kvmppc_dump_regs(vcpu);
519 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
520 vcpu->arch.trap, kvmppc_get_pc(vcpu),
521 vcpu->arch.shregs.msr);
522 r = RESUME_HOST;
523 BUG();
524 break;
525 }
526
Paul Mackerrasde56a942011-06-29 00:21:34 +0000527 return r;
528}
529
530int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
531 struct kvm_sregs *sregs)
532{
533 int i;
534
535 sregs->pvr = vcpu->arch.pvr;
536
537 memset(sregs, 0, sizeof(struct kvm_sregs));
538 for (i = 0; i < vcpu->arch.slb_max; i++) {
539 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
540 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
541 }
542
543 return 0;
544}
545
546int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
547 struct kvm_sregs *sregs)
548{
549 int i, j;
550
551 kvmppc_set_pvr(vcpu, sregs->pvr);
552
553 j = 0;
554 for (i = 0; i < vcpu->arch.slb_nr; i++) {
555 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
556 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
557 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
558 ++j;
559 }
560 }
561 vcpu->arch.slb_max = j;
562
563 return 0;
564}
565
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000566int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000567{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000568 int r = 0;
569 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +0000570
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000571 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000572 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000573 *val = get_reg_val(id, 0);
574 break;
575 case KVM_REG_PPC_DABR:
576 *val = get_reg_val(id, vcpu->arch.dabr);
577 break;
578 case KVM_REG_PPC_DSCR:
579 *val = get_reg_val(id, vcpu->arch.dscr);
580 break;
581 case KVM_REG_PPC_PURR:
582 *val = get_reg_val(id, vcpu->arch.purr);
583 break;
584 case KVM_REG_PPC_SPURR:
585 *val = get_reg_val(id, vcpu->arch.spurr);
586 break;
587 case KVM_REG_PPC_AMR:
588 *val = get_reg_val(id, vcpu->arch.amr);
589 break;
590 case KVM_REG_PPC_UAMOR:
591 *val = get_reg_val(id, vcpu->arch.uamor);
592 break;
593 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
594 i = id - KVM_REG_PPC_MMCR0;
595 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
596 break;
597 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
598 i = id - KVM_REG_PPC_PMC1;
599 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +0000600 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000601#ifdef CONFIG_VSX
602 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
603 if (cpu_has_feature(CPU_FTR_VSX)) {
604 /* VSX => FP reg i is stored in arch.vsr[2*i] */
605 long int i = id - KVM_REG_PPC_FPR0;
606 *val = get_reg_val(id, vcpu->arch.vsr[2 * i]);
607 } else {
608 /* let generic code handle it */
609 r = -EINVAL;
610 }
611 break;
612 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
613 if (cpu_has_feature(CPU_FTR_VSX)) {
614 long int i = id - KVM_REG_PPC_VSR0;
615 val->vsxval[0] = vcpu->arch.vsr[2 * i];
616 val->vsxval[1] = vcpu->arch.vsr[2 * i + 1];
617 } else {
618 r = -ENXIO;
619 }
620 break;
621#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000622 case KVM_REG_PPC_VPA_ADDR:
623 spin_lock(&vcpu->arch.vpa_update_lock);
624 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
625 spin_unlock(&vcpu->arch.vpa_update_lock);
626 break;
627 case KVM_REG_PPC_VPA_SLB:
628 spin_lock(&vcpu->arch.vpa_update_lock);
629 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
630 val->vpaval.length = vcpu->arch.slb_shadow.len;
631 spin_unlock(&vcpu->arch.vpa_update_lock);
632 break;
633 case KVM_REG_PPC_VPA_DTL:
634 spin_lock(&vcpu->arch.vpa_update_lock);
635 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
636 val->vpaval.length = vcpu->arch.dtl.len;
637 spin_unlock(&vcpu->arch.vpa_update_lock);
638 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000639 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000640 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000641 break;
642 }
643
644 return r;
645}
646
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000647int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000648{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000649 int r = 0;
650 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +0000651 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +0000652
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000653 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000654 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +0000655 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000656 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +0000657 r = -EINVAL;
658 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000659 case KVM_REG_PPC_DABR:
660 vcpu->arch.dabr = set_reg_val(id, *val);
661 break;
662 case KVM_REG_PPC_DSCR:
663 vcpu->arch.dscr = set_reg_val(id, *val);
664 break;
665 case KVM_REG_PPC_PURR:
666 vcpu->arch.purr = set_reg_val(id, *val);
667 break;
668 case KVM_REG_PPC_SPURR:
669 vcpu->arch.spurr = set_reg_val(id, *val);
670 break;
671 case KVM_REG_PPC_AMR:
672 vcpu->arch.amr = set_reg_val(id, *val);
673 break;
674 case KVM_REG_PPC_UAMOR:
675 vcpu->arch.uamor = set_reg_val(id, *val);
676 break;
677 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
678 i = id - KVM_REG_PPC_MMCR0;
679 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
680 break;
681 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
682 i = id - KVM_REG_PPC_PMC1;
683 vcpu->arch.pmc[i] = set_reg_val(id, *val);
684 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000685#ifdef CONFIG_VSX
686 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
687 if (cpu_has_feature(CPU_FTR_VSX)) {
688 /* VSX => FP reg i is stored in arch.vsr[2*i] */
689 long int i = id - KVM_REG_PPC_FPR0;
690 vcpu->arch.vsr[2 * i] = set_reg_val(id, *val);
691 } else {
692 /* let generic code handle it */
693 r = -EINVAL;
694 }
695 break;
696 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
697 if (cpu_has_feature(CPU_FTR_VSX)) {
698 long int i = id - KVM_REG_PPC_VSR0;
699 vcpu->arch.vsr[2 * i] = val->vsxval[0];
700 vcpu->arch.vsr[2 * i + 1] = val->vsxval[1];
701 } else {
702 r = -ENXIO;
703 }
704 break;
705#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000706 case KVM_REG_PPC_VPA_ADDR:
707 addr = set_reg_val(id, *val);
708 r = -EINVAL;
709 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
710 vcpu->arch.dtl.next_gpa))
711 break;
712 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
713 break;
714 case KVM_REG_PPC_VPA_SLB:
715 addr = val->vpaval.addr;
716 len = val->vpaval.length;
717 r = -EINVAL;
718 if (addr && !vcpu->arch.vpa.next_gpa)
719 break;
720 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
721 break;
722 case KVM_REG_PPC_VPA_DTL:
723 addr = val->vpaval.addr;
724 len = val->vpaval.length;
725 r = -EINVAL;
726 if (len < sizeof(struct dtl_entry))
727 break;
728 if (addr && !vcpu->arch.vpa.next_gpa)
729 break;
730 len -= len % sizeof(struct dtl_entry);
731 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
732 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000733 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000734 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000735 break;
736 }
737
738 return r;
739}
740
Paul Mackerrasde56a942011-06-29 00:21:34 +0000741int kvmppc_core_check_processor_compat(void)
742{
Paul Mackerras9e368f22011-06-29 00:40:08 +0000743 if (cpu_has_feature(CPU_FTR_HVMODE))
Paul Mackerrasde56a942011-06-29 00:21:34 +0000744 return 0;
745 return -EIO;
746}
747
748struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
749{
750 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000751 int err = -EINVAL;
752 int core;
753 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000754
Paul Mackerras371fefd2011-06-29 00:23:08 +0000755 core = id / threads_per_core;
756 if (core >= KVM_MAX_VCORES)
757 goto out;
758
759 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200760 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000761 if (!vcpu)
762 goto out;
763
764 err = kvm_vcpu_init(vcpu, kvm, id);
765 if (err)
766 goto free_vcpu;
767
768 vcpu->arch.shared = &vcpu->arch.shregs;
769 vcpu->arch.last_cpu = -1;
770 vcpu->arch.mmcr[0] = MMCR0_FC;
771 vcpu->arch.ctrl = CTRL_RUNLATCH;
772 /* default to host PVR, since we can't spoof it */
773 vcpu->arch.pvr = mfspr(SPRN_PVR);
774 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000775 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000776
Paul Mackerrasde56a942011-06-29 00:21:34 +0000777 kvmppc_mmu_book3s_hv_init(vcpu);
778
Paul Mackerras8455d792012-10-15 01:17:42 +0000779 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000780
781 init_waitqueue_head(&vcpu->arch.cpu_run);
782
783 mutex_lock(&kvm->lock);
784 vcore = kvm->arch.vcores[core];
785 if (!vcore) {
786 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
787 if (vcore) {
788 INIT_LIST_HEAD(&vcore->runnable_threads);
789 spin_lock_init(&vcore->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000790 init_waitqueue_head(&vcore->wq);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000791 vcore->preempt_tb = mftb();
Paul Mackerras371fefd2011-06-29 00:23:08 +0000792 }
793 kvm->arch.vcores[core] = vcore;
794 }
795 mutex_unlock(&kvm->lock);
796
797 if (!vcore)
798 goto free_vcpu;
799
800 spin_lock(&vcore->lock);
801 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000802 spin_unlock(&vcore->lock);
803 vcpu->arch.vcore = vcore;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000804 vcpu->arch.stolen_logged = vcore->stolen_tb;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000805
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200806 vcpu->arch.cpu_type = KVM_CPU_3S_64;
807 kvmppc_sanity_check(vcpu);
808
Paul Mackerrasde56a942011-06-29 00:21:34 +0000809 return vcpu;
810
811free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200812 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000813out:
814 return ERR_PTR(err);
815}
816
817void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
818{
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000819 spin_lock(&vcpu->arch.vpa_update_lock);
820 if (vcpu->arch.dtl.pinned_addr)
821 kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.dtl.pinned_addr);
822 if (vcpu->arch.slb_shadow.pinned_addr)
823 kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.slb_shadow.pinned_addr);
824 if (vcpu->arch.vpa.pinned_addr)
825 kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.vpa.pinned_addr);
826 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000827 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200828 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000829}
830
Paul Mackerras19ccb762011-07-23 17:42:46 +1000831static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000832{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000833 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000834
Paul Mackerras19ccb762011-07-23 17:42:46 +1000835 now = get_tb();
836 if (now > vcpu->arch.dec_expires) {
837 /* decrementer has already gone negative */
838 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -0600839 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000840 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000841 }
Paul Mackerras19ccb762011-07-23 17:42:46 +1000842 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
843 / tb_ticks_per_sec;
844 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
845 HRTIMER_MODE_REL);
846 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000847}
848
Paul Mackerras19ccb762011-07-23 17:42:46 +1000849static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000850{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000851 vcpu->arch.ceded = 0;
852 if (vcpu->arch.timer_running) {
853 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
854 vcpu->arch.timer_running = 0;
855 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000856}
857
858extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
859extern void xics_wake_cpu(int cpu);
860
861static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
862 struct kvm_vcpu *vcpu)
863{
Paul Mackerras371fefd2011-06-29 00:23:08 +0000864 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
865 return;
Paul Mackerras8455d792012-10-15 01:17:42 +0000866 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000867 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000868 list_del(&vcpu->arch.run_list);
869}
870
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000871static int kvmppc_grab_hwthread(int cpu)
872{
873 struct paca_struct *tpaca;
874 long timeout = 1000;
875
876 tpaca = &paca[cpu];
877
878 /* Ensure the thread won't go into the kernel if it wakes */
879 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerras7b444c62012-10-15 01:16:14 +0000880 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000881
882 /*
883 * If the thread is already executing in the kernel (e.g. handling
884 * a stray interrupt), wait for it to get back to nap mode.
885 * The smp_mb() is to ensure that our setting of hwthread_req
886 * is visible before we look at hwthread_state, so if this
887 * races with the code at system_reset_pSeries and the thread
888 * misses our setting of hwthread_req, we are sure to see its
889 * setting of hwthread_state, and vice versa.
890 */
891 smp_mb();
892 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
893 if (--timeout <= 0) {
894 pr_err("KVM: couldn't grab cpu %d\n", cpu);
895 return -EBUSY;
896 }
897 udelay(1);
898 }
899 return 0;
900}
901
902static void kvmppc_release_hwthread(int cpu)
903{
904 struct paca_struct *tpaca;
905
906 tpaca = &paca[cpu];
907 tpaca->kvm_hstate.hwthread_req = 0;
908 tpaca->kvm_hstate.kvm_vcpu = NULL;
909}
910
Paul Mackerras371fefd2011-06-29 00:23:08 +0000911static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
912{
913 int cpu;
914 struct paca_struct *tpaca;
915 struct kvmppc_vcore *vc = vcpu->arch.vcore;
916
Paul Mackerras19ccb762011-07-23 17:42:46 +1000917 if (vcpu->arch.timer_running) {
918 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
919 vcpu->arch.timer_running = 0;
920 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000921 cpu = vc->pcpu + vcpu->arch.ptid;
922 tpaca = &paca[cpu];
923 tpaca->kvm_hstate.kvm_vcpu = vcpu;
924 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000925 tpaca->kvm_hstate.napping = 0;
926 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000927 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +0000928#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000929 if (vcpu->arch.ptid) {
Paul Mackerras371fefd2011-06-29 00:23:08 +0000930 xics_wake_cpu(cpu);
931 ++vc->n_woken;
932 }
933#endif
934}
935
936static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
937{
938 int i;
939
940 HMT_low();
941 i = 0;
942 while (vc->nap_count < vc->n_woken) {
943 if (++i >= 1000000) {
944 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
945 vc->nap_count, vc->n_woken);
946 break;
947 }
948 cpu_relax();
949 }
950 HMT_medium();
951}
952
953/*
954 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +0000955 * this core are off-line. Then grab the threads so they can't
956 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000957 */
958static int on_primary_thread(void)
959{
960 int cpu = smp_processor_id();
961 int thr = cpu_thread_in_core(cpu);
962
963 if (thr)
964 return 0;
965 while (++thr < threads_per_core)
966 if (cpu_online(cpu + thr))
967 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +0000968
969 /* Grab all hw threads so they can't go into the kernel */
970 for (thr = 1; thr < threads_per_core; ++thr) {
971 if (kvmppc_grab_hwthread(cpu + thr)) {
972 /* Couldn't grab one; let the others go */
973 do {
974 kvmppc_release_hwthread(cpu + thr);
975 } while (--thr > 0);
976 return 0;
977 }
978 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000979 return 1;
980}
981
982/*
983 * Run a set of guest threads on a physical core.
984 * Called with vc->lock held.
985 */
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000986static void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000987{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000988 struct kvm_vcpu *vcpu, *vcpu0, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000989 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000990 u64 now;
Paul Mackerras081f3232012-06-01 20:20:24 +1000991 int ptid, i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000992 int srcu_idx;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000993 struct kvm_vcpu *vcpus_to_update[threads_per_core];
Paul Mackerrasde56a942011-06-29 00:21:34 +0000994
Paul Mackerras371fefd2011-06-29 00:23:08 +0000995 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +1000996 need_vpa_update = 0;
997 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +0000998 if (signal_pending(vcpu->arch.run_task))
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000999 return;
1000 if (vcpu->arch.vpa.update_pending ||
1001 vcpu->arch.slb_shadow.update_pending ||
1002 vcpu->arch.dtl.update_pending)
1003 vcpus_to_update[need_vpa_update++] = vcpu;
Paul Mackerras081f3232012-06-01 20:20:24 +10001004 }
1005
1006 /*
1007 * Initialize *vc, in particular vc->vcore_state, so we can
1008 * drop the vcore lock if necessary.
1009 */
1010 vc->n_woken = 0;
1011 vc->nap_count = 0;
1012 vc->entry_exit_count = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001013 vc->vcore_state = VCORE_STARTING;
Paul Mackerras081f3232012-06-01 20:20:24 +10001014 vc->in_guest = 0;
1015 vc->napping_threads = 0;
1016
1017 /*
1018 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1019 * which can't be called with any spinlocks held.
1020 */
1021 if (need_vpa_update) {
1022 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001023 for (i = 0; i < need_vpa_update; ++i)
1024 kvmppc_update_vpas(vcpus_to_update[i]);
Paul Mackerras081f3232012-06-01 20:20:24 +10001025 spin_lock(&vc->lock);
1026 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001027
1028 /*
Paul Mackerras19ccb762011-07-23 17:42:46 +10001029 * Assign physical thread IDs, first to non-ceded vcpus
1030 * and then to ceded ones.
1031 */
1032 ptid = 0;
1033 vcpu0 = NULL;
1034 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1035 if (!vcpu->arch.ceded) {
1036 if (!ptid)
1037 vcpu0 = vcpu;
1038 vcpu->arch.ptid = ptid++;
1039 }
1040 }
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001041 if (!vcpu0) {
1042 vc->vcore_state = VCORE_INACTIVE;
1043 return; /* nothing to run; should never happen */
1044 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001045 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1046 if (vcpu->arch.ceded)
1047 vcpu->arch.ptid = ptid++;
1048
Paul Mackerras7b444c62012-10-15 01:16:14 +00001049 /*
1050 * Make sure we are running on thread 0, and that
1051 * secondary threads are offline.
1052 */
1053 if (threads_per_core > 1 && !on_primary_thread()) {
1054 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1055 vcpu->arch.ret = -EBUSY;
1056 goto out;
1057 }
1058
Paul Mackerras0456ec42012-02-03 00:56:21 +00001059 vc->stolen_tb += mftb() - vc->preempt_tb;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001060 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001061 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001062 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001063 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001064 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001065
Paul Mackerras2f12f032012-10-15 01:17:17 +00001066 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001067 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001068 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001069
Paul Mackerras19ccb762011-07-23 17:42:46 +10001070 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001071
1072 srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
1073
Paul Mackerras19ccb762011-07-23 17:42:46 +10001074 __kvmppc_vcore_entry(NULL, vcpu0);
1075
Paul Mackerras371fefd2011-06-29 00:23:08 +00001076 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001077 /* disable sending of IPIs on virtual external irqs */
1078 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1079 vcpu->cpu = -1;
1080 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001081 if (vc->nap_count < vc->n_woken)
1082 kvmppc_wait_for_nap(vc);
Paul Mackerras2f12f032012-10-15 01:17:17 +00001083 for (i = 0; i < threads_per_core; ++i)
1084 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001085 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001086 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001087 spin_unlock(&vc->lock);
1088
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001089 srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
1090
Paul Mackerras371fefd2011-06-29 00:23:08 +00001091 /* make sure updates to secondary vcpu structs are visible now */
1092 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001093 kvm_guest_exit();
1094
1095 preempt_enable();
1096 kvm_resched(vcpu);
1097
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001098 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001099 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001100 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1101 /* cancel pending dec exception if dec is positive */
1102 if (now < vcpu->arch.dec_expires &&
1103 kvmppc_core_pending_dec(vcpu))
1104 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001105
1106 ret = RESUME_GUEST;
1107 if (vcpu->arch.trap)
1108 ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
1109 vcpu->arch.run_task);
1110
Paul Mackerras371fefd2011-06-29 00:23:08 +00001111 vcpu->arch.ret = ret;
1112 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001113
1114 if (vcpu->arch.ceded) {
1115 if (ret != RESUME_GUEST)
1116 kvmppc_end_cede(vcpu);
1117 else
1118 kvmppc_set_timer(vcpu);
1119 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001120 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001121
Paul Mackerrasde56a942011-06-29 00:21:34 +00001122 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001123 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras0456ec42012-02-03 00:56:21 +00001124 vc->preempt_tb = mftb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001125 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1126 arch.run_list) {
1127 if (vcpu->arch.ret != RESUME_GUEST) {
1128 kvmppc_remove_runnable(vc, vcpu);
1129 wake_up(&vcpu->arch.cpu_run);
1130 }
1131 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001132}
1133
Paul Mackerras19ccb762011-07-23 17:42:46 +10001134/*
1135 * Wait for some other vcpu thread to execute us, and
1136 * wake us up when we need to handle something in the host.
1137 */
1138static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001139{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001140 DEFINE_WAIT(wait);
1141
Paul Mackerras19ccb762011-07-23 17:42:46 +10001142 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1143 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1144 schedule();
1145 finish_wait(&vcpu->arch.cpu_run, &wait);
1146}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001147
Paul Mackerras19ccb762011-07-23 17:42:46 +10001148/*
1149 * All the vcpus in this vcore are idle, so wait for a decrementer
1150 * or external interrupt to one of the vcpus. vc->lock is held.
1151 */
1152static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1153{
1154 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001155
1156 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1157 vc->vcore_state = VCORE_SLEEPING;
1158 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001159 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001160 finish_wait(&vc->wq, &wait);
1161 spin_lock(&vc->lock);
1162 vc->vcore_state = VCORE_INACTIVE;
1163}
1164
1165static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1166{
1167 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001168 struct kvmppc_vcore *vc;
1169 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001170
Paul Mackerras371fefd2011-06-29 00:23:08 +00001171 kvm_run->exit_reason = 0;
1172 vcpu->arch.ret = RESUME_GUEST;
1173 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001174 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001175
Paul Mackerras371fefd2011-06-29 00:23:08 +00001176 /*
1177 * Synchronize with other threads in this virtual core
1178 */
1179 vc = vcpu->arch.vcore;
1180 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001181 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001182 vcpu->arch.run_task = current;
1183 vcpu->arch.kvm_run = kvm_run;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001184 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001185 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1186 ++vc->n_runnable;
1187
Paul Mackerras19ccb762011-07-23 17:42:46 +10001188 /*
1189 * This happens the first time this is called for a vcpu.
1190 * If the vcore is already running, we may be able to start
1191 * this thread straight away and have it join in.
1192 */
Paul Mackerras8455d792012-10-15 01:17:42 +00001193 if (!signal_pending(current)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001194 if (vc->vcore_state == VCORE_RUNNING &&
1195 VCORE_EXIT_COUNT(vc) == 0) {
1196 vcpu->arch.ptid = vc->n_runnable - 1;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001197 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001198 kvmppc_start_thread(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00001199 } else if (vc->vcore_state == VCORE_SLEEPING) {
1200 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001201 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001202
Paul Mackerras8455d792012-10-15 01:17:42 +00001203 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001204
1205 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1206 !signal_pending(current)) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001207 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001208 spin_unlock(&vc->lock);
1209 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1210 spin_lock(&vc->lock);
1211 continue;
1212 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001213 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1214 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001215 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001216 if (signal_pending(v->arch.run_task)) {
1217 kvmppc_remove_runnable(vc, v);
1218 v->stat.signal_exits++;
1219 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1220 v->arch.ret = -EINTR;
1221 wake_up(&v->arch.cpu_run);
1222 }
1223 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001224 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1225 break;
1226 vc->runner = vcpu;
1227 n_ceded = 0;
1228 list_for_each_entry(v, &vc->runnable_threads, arch.run_list)
1229 if (!v->arch.pending_exceptions)
1230 n_ceded += v->arch.ceded;
1231 if (n_ceded == vc->n_runnable)
1232 kvmppc_vcore_blocked(vc);
1233 else
1234 kvmppc_run_core(vc);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001235 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001236 }
1237
Paul Mackerras8455d792012-10-15 01:17:42 +00001238 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1239 (vc->vcore_state == VCORE_RUNNING ||
1240 vc->vcore_state == VCORE_EXITING)) {
1241 spin_unlock(&vc->lock);
1242 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1243 spin_lock(&vc->lock);
1244 }
1245
1246 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1247 kvmppc_remove_runnable(vc, vcpu);
1248 vcpu->stat.signal_exits++;
1249 kvm_run->exit_reason = KVM_EXIT_INTR;
1250 vcpu->arch.ret = -EINTR;
1251 }
1252
1253 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1254 /* Wake up some vcpu to run the core */
1255 v = list_first_entry(&vc->runnable_threads,
1256 struct kvm_vcpu, arch.run_list);
1257 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001258 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001259
Paul Mackerras19ccb762011-07-23 17:42:46 +10001260 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001261 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001262}
1263
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001264int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
1265{
1266 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001267 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001268
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001269 if (!vcpu->arch.sane) {
1270 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1271 return -EINVAL;
1272 }
1273
Scott Wood25051b52011-11-08 18:23:23 -06001274 kvmppc_core_prepare_to_enter(vcpu);
1275
Paul Mackerras19ccb762011-07-23 17:42:46 +10001276 /* No need to go into the guest when all we'll do is come back out */
1277 if (signal_pending(current)) {
1278 run->exit_reason = KVM_EXIT_INTR;
1279 return -EINTR;
1280 }
1281
Paul Mackerras32fad282012-05-04 02:32:53 +00001282 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1283 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1284 smp_mb();
1285
1286 /* On the first time here, set up HTAB and VRMA or RMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001287 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001288 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001289 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001290 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001291 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001292
1293 flush_fp_to_thread(current);
1294 flush_altivec_to_thread(current);
1295 flush_vsx_to_thread(current);
1296 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001297 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001298
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001299 do {
1300 r = kvmppc_run_vcpu(run, vcpu);
1301
1302 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1303 !(vcpu->arch.shregs.msr & MSR_PR)) {
1304 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001305 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001306 } else if (r == RESUME_PAGE_FAULT) {
1307 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1308 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1309 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1310 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001311 }
1312 } while (r == RESUME_GUEST);
Paul Mackerras32fad282012-05-04 02:32:53 +00001313
1314 out:
1315 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001316 return r;
1317}
1318
David Gibson54738c02011-06-29 00:22:41 +00001319
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001320/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +00001321 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001322static inline int lpcr_rmls(unsigned long rma_size)
1323{
1324 switch (rma_size) {
1325 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001326 if (cpu_has_feature(CPU_FTR_ARCH_206))
1327 return 8; /* only supported on POWER7 */
1328 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001329 case 64ul << 20: /* 64 MB */
1330 return 3;
1331 case 128ul << 20: /* 128 MB */
1332 return 7;
1333 case 256ul << 20: /* 256 MB */
1334 return 4;
1335 case 1ul << 30: /* 1 GB */
1336 return 2;
1337 case 16ul << 30: /* 16 GB */
1338 return 1;
1339 case 256ul << 30: /* 256 GB */
1340 return 0;
1341 default:
1342 return -1;
1343 }
1344}
1345
1346static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1347{
Alexander Grafb4e70612012-01-16 16:50:10 +01001348 struct kvmppc_linear_info *ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001349 struct page *page;
1350
1351 if (vmf->pgoff >= ri->npages)
1352 return VM_FAULT_SIGBUS;
1353
1354 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1355 get_page(page);
1356 vmf->page = page;
1357 return 0;
1358}
1359
1360static const struct vm_operations_struct kvm_rma_vm_ops = {
1361 .fault = kvm_rma_fault,
1362};
1363
1364static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1365{
1366 vma->vm_flags |= VM_RESERVED;
1367 vma->vm_ops = &kvm_rma_vm_ops;
1368 return 0;
1369}
1370
1371static int kvm_rma_release(struct inode *inode, struct file *filp)
1372{
Alexander Grafb4e70612012-01-16 16:50:10 +01001373 struct kvmppc_linear_info *ri = filp->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001374
1375 kvm_release_rma(ri);
1376 return 0;
1377}
1378
1379static struct file_operations kvm_rma_fops = {
1380 .mmap = kvm_rma_mmap,
1381 .release = kvm_rma_release,
1382};
1383
1384long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
1385{
Alexander Grafb4e70612012-01-16 16:50:10 +01001386 struct kvmppc_linear_info *ri;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001387 long fd;
1388
1389 ri = kvm_alloc_rma();
1390 if (!ri)
1391 return -ENOMEM;
1392
1393 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR);
1394 if (fd < 0)
1395 kvm_release_rma(ri);
1396
1397 ret->rma_size = ri->npages << PAGE_SHIFT;
1398 return fd;
1399}
1400
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001401static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1402 int linux_psize)
1403{
1404 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1405
1406 if (!def->shift)
1407 return;
1408 (*sps)->page_shift = def->shift;
1409 (*sps)->slb_enc = def->sllp;
1410 (*sps)->enc[0].page_shift = def->shift;
1411 (*sps)->enc[0].pte_enc = def->penc;
1412 (*sps)++;
1413}
1414
1415int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
1416{
1417 struct kvm_ppc_one_seg_page_size *sps;
1418
1419 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1420 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1421 info->flags |= KVM_PPC_1T_SEGMENTS;
1422 info->slb_size = mmu_slb_size;
1423
1424 /* We only support these sizes for now, and no muti-size segments */
1425 sps = &info->sps[0];
1426 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1427 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1428 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1429
1430 return 0;
1431}
1432
Paul Mackerras82ed3612011-12-15 02:03:22 +00001433/*
1434 * Get (and clear) the dirty memory log for a memory slot.
1435 */
1436int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1437{
1438 struct kvm_memory_slot *memslot;
1439 int r;
1440 unsigned long n;
1441
1442 mutex_lock(&kvm->slots_lock);
1443
1444 r = -EINVAL;
1445 if (log->slot >= KVM_MEMORY_SLOTS)
1446 goto out;
1447
1448 memslot = id_to_memslot(kvm->memslots, log->slot);
1449 r = -ENOENT;
1450 if (!memslot->dirty_bitmap)
1451 goto out;
1452
1453 n = kvm_dirty_bitmap_bytes(memslot);
1454 memset(memslot->dirty_bitmap, 0, n);
1455
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001456 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00001457 if (r)
1458 goto out;
1459
1460 r = -EFAULT;
1461 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1462 goto out;
1463
1464 r = 0;
1465out:
1466 mutex_unlock(&kvm->slots_lock);
1467 return r;
1468}
1469
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001470static unsigned long slb_pgsize_encoding(unsigned long psize)
1471{
1472 unsigned long senc = 0;
1473
1474 if (psize > 0x1000) {
1475 senc = SLB_VSID_L;
1476 if (psize == 0x10000)
1477 senc |= SLB_VSID_LP_01;
1478 }
1479 return senc;
1480}
1481
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001482static void unpin_slot(struct kvm_memory_slot *memslot)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001483{
1484 unsigned long *physp;
1485 unsigned long j, npages, pfn;
1486 struct page *page;
1487
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001488 physp = memslot->arch.slot_phys;
1489 npages = memslot->npages;
1490 if (!physp)
1491 return;
1492 for (j = 0; j < npages; j++) {
1493 if (!(physp[j] & KVMPPC_GOT_PAGE))
1494 continue;
1495 pfn = physp[j] >> PAGE_SHIFT;
1496 page = pfn_to_page(pfn);
1497 SetPageDirty(page);
1498 put_page(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001499 }
1500}
1501
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001502void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1503 struct kvm_memory_slot *dont)
1504{
1505 if (!dont || free->arch.rmap != dont->arch.rmap) {
1506 vfree(free->arch.rmap);
1507 free->arch.rmap = NULL;
1508 }
1509 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1510 unpin_slot(free);
1511 vfree(free->arch.slot_phys);
1512 free->arch.slot_phys = NULL;
1513 }
1514}
1515
1516int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1517 unsigned long npages)
1518{
1519 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1520 if (!slot->arch.rmap)
1521 return -ENOMEM;
1522 slot->arch.slot_phys = NULL;
1523
1524 return 0;
1525}
1526
1527int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1528 struct kvm_memory_slot *memslot,
1529 struct kvm_userspace_memory_region *mem)
1530{
1531 unsigned long *phys;
1532
1533 /* Allocate a slot_phys array if needed */
1534 phys = memslot->arch.slot_phys;
1535 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1536 phys = vzalloc(memslot->npages * sizeof(unsigned long));
1537 if (!phys)
1538 return -ENOMEM;
1539 memslot->arch.slot_phys = phys;
1540 }
1541
1542 return 0;
1543}
1544
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001545void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001546 struct kvm_userspace_memory_region *mem,
1547 struct kvm_memory_slot old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001548{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001549 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1550 struct kvm_memory_slot *memslot;
1551
1552 if (npages && old.npages) {
1553 /*
1554 * If modifying a memslot, reset all the rmap dirty bits.
1555 * If this is a new memslot, we don't need to do anything
1556 * since the rmap array starts out as all zeroes,
1557 * i.e. no pages are dirty.
1558 */
1559 memslot = id_to_memslot(kvm->memslots, mem->slot);
1560 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1561 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001562}
1563
Paul Mackerras32fad282012-05-04 02:32:53 +00001564static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001565{
1566 int err = 0;
1567 struct kvm *kvm = vcpu->kvm;
Alexander Grafb4e70612012-01-16 16:50:10 +01001568 struct kvmppc_linear_info *ri = NULL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001569 unsigned long hva;
1570 struct kvm_memory_slot *memslot;
1571 struct vm_area_struct *vma;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001572 unsigned long lpcr, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001573 unsigned long psize, porder;
1574 unsigned long rma_size;
1575 unsigned long rmls;
1576 unsigned long *physp;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001577 unsigned long i, npages;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001578 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001579
1580 mutex_lock(&kvm->lock);
1581 if (kvm->arch.rma_setup_done)
1582 goto out; /* another vcpu beat us to it */
1583
Paul Mackerras32fad282012-05-04 02:32:53 +00001584 /* Allocate hashed page table (if not done already) and reset it */
1585 if (!kvm->arch.hpt_virt) {
1586 err = kvmppc_alloc_hpt(kvm, NULL);
1587 if (err) {
1588 pr_err("KVM: Couldn't alloc HPT\n");
1589 goto out;
1590 }
1591 }
1592
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001593 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001594 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001595 memslot = gfn_to_memslot(kvm, 0);
1596
1597 /* We must have some memory at 0 by now */
1598 err = -EINVAL;
1599 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001600 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001601
1602 /* Look up the VMA for the start of this memory slot */
1603 hva = memslot->userspace_addr;
1604 down_read(&current->mm->mmap_sem);
1605 vma = find_vma(current->mm, hva);
1606 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
1607 goto up_out;
1608
1609 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001610 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001611
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001612 /* Is this one of our preallocated RMAs? */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001613 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
1614 hva == vma->vm_start)
1615 ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001616
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001617 up_read(&current->mm->mmap_sem);
1618
1619 if (!ri) {
1620 /* On POWER7, use VRMA; on PPC970, give up */
1621 err = -EPERM;
1622 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1623 pr_err("KVM: CPU requires an RMO\n");
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001624 goto out_srcu;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001625 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001626
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001627 /* We can handle 4k, 64k or 16M pages in the VRMA */
1628 err = -EINVAL;
1629 if (!(psize == 0x1000 || psize == 0x10000 ||
1630 psize == 0x1000000))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001631 goto out_srcu;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001632
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001633 /* Update VRMASD field in the LPCR */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001634 senc = slb_pgsize_encoding(psize);
Paul Mackerras697d3892011-12-12 12:36:37 +00001635 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1636 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001637 lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
1638 lpcr |= senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001639 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001640
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001641 /* Create HPTEs in the hash page table for the VRMA */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001642 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001643
1644 } else {
1645 /* Set up to use an RMO region */
1646 rma_size = ri->npages;
1647 if (rma_size > memslot->npages)
1648 rma_size = memslot->npages;
1649 rma_size <<= PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001650 rmls = lpcr_rmls(rma_size);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001651 err = -EINVAL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001652 if (rmls < 0) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001653 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001654 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001655 }
1656 atomic_inc(&ri->use_count);
1657 kvm->arch.rma = ri;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001658
1659 /* Update LPCR and RMOR */
1660 lpcr = kvm->arch.lpcr;
1661 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1662 /* PPC970; insert RMLS value (split field) in HID4 */
1663 lpcr &= ~((1ul << HID4_RMLS0_SH) |
1664 (3ul << HID4_RMLS2_SH));
1665 lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
1666 ((rmls & 3) << HID4_RMLS2_SH);
1667 /* RMOR is also in HID4 */
1668 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
1669 << HID4_RMOR_SH;
1670 } else {
1671 /* POWER7 */
1672 lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
1673 lpcr |= rmls << LPCR_RMLS_SH;
1674 kvm->arch.rmor = kvm->arch.rma->base_pfn << PAGE_SHIFT;
1675 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001676 kvm->arch.lpcr = lpcr;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001677 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001678 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001679
1680 /* Initialize phys addrs of pages in RMO */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001681 npages = ri->npages;
1682 porder = __ilog2(npages);
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001683 physp = memslot->arch.slot_phys;
1684 if (physp) {
1685 if (npages > memslot->npages)
1686 npages = memslot->npages;
1687 spin_lock(&kvm->arch.slot_phys_lock);
1688 for (i = 0; i < npages; ++i)
1689 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
1690 porder;
1691 spin_unlock(&kvm->arch.slot_phys_lock);
1692 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001693 }
1694
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001695 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
1696 smp_wmb();
1697 kvm->arch.rma_setup_done = 1;
1698 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001699 out_srcu:
1700 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001701 out:
1702 mutex_unlock(&kvm->lock);
1703 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001704
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001705 up_out:
1706 up_read(&current->mm->mmap_sem);
1707 goto out;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001708}
1709
1710int kvmppc_core_init_vm(struct kvm *kvm)
1711{
Paul Mackerras32fad282012-05-04 02:32:53 +00001712 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001713
Paul Mackerras32fad282012-05-04 02:32:53 +00001714 /* Allocate the guest's logical partition ID */
1715
1716 lpid = kvmppc_alloc_lpid();
1717 if (lpid < 0)
1718 return -ENOMEM;
1719 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001720
David Gibson54738c02011-06-29 00:22:41 +00001721 INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001722
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001723 kvm->arch.rma = NULL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001724
Paul Mackerras9e368f22011-06-29 00:40:08 +00001725 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001726
Paul Mackerras9e368f22011-06-29 00:40:08 +00001727 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1728 /* PPC970; HID4 is effectively the LPCR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001729 kvm->arch.host_lpid = 0;
1730 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
1731 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
1732 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
1733 ((lpid & 0xf) << HID4_LPID5_SH);
1734 } else {
1735 /* POWER7; init LPCR for virtual RMA mode */
1736 kvm->arch.host_lpid = mfspr(SPRN_LPID);
1737 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
1738 lpcr &= LPCR_PECE | LPCR_LPES;
1739 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
Paul Mackerras697d3892011-12-12 12:36:37 +00001740 LPCR_VPM0 | LPCR_VPM1;
1741 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
1742 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerras9e368f22011-06-29 00:40:08 +00001743 }
1744 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001745
Paul Mackerras342d3db2011-12-12 12:38:05 +00001746 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001747 spin_lock_init(&kvm->arch.slot_phys_lock);
Paul Mackerras512691d2012-10-15 01:15:41 +00001748
1749 /*
1750 * Don't allow secondary CPU threads to come online
1751 * while any KVM VMs exist.
1752 */
1753 inhibit_secondary_onlining();
1754
David Gibson54738c02011-06-29 00:22:41 +00001755 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001756}
1757
1758void kvmppc_core_destroy_vm(struct kvm *kvm)
1759{
Paul Mackerras512691d2012-10-15 01:15:41 +00001760 uninhibit_secondary_onlining();
1761
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001762 if (kvm->arch.rma) {
1763 kvm_release_rma(kvm->arch.rma);
1764 kvm->arch.rma = NULL;
1765 }
1766
Paul Mackerrasde56a942011-06-29 00:21:34 +00001767 kvmppc_free_hpt(kvm);
David Gibson54738c02011-06-29 00:22:41 +00001768 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
Paul Mackerrasde56a942011-06-29 00:21:34 +00001769}
1770
1771/* These are stubs for now */
1772void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
1773{
1774}
1775
1776/* We don't need to emulate any privileged instructions or dcbz */
1777int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
1778 unsigned int inst, int *advance)
1779{
1780 return EMULATE_FAIL;
1781}
1782
Alexander Graf54771e62012-05-04 14:55:12 +02001783int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001784{
1785 return EMULATE_FAIL;
1786}
1787
Alexander Graf54771e62012-05-04 14:55:12 +02001788int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001789{
1790 return EMULATE_FAIL;
1791}
1792
1793static int kvmppc_book3s_hv_init(void)
1794{
1795 int r;
1796
1797 r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1798
1799 if (r)
1800 return r;
1801
1802 r = kvmppc_mmu_hv_init();
1803
1804 return r;
1805}
1806
1807static void kvmppc_book3s_hv_exit(void)
1808{
1809 kvm_exit();
1810}
1811
1812module_init(kvmppc_book3s_hv_init);
1813module_exit(kvmppc_book3s_hv_exit);