blob: 89995fa6e94510c1dd6eb8dfd326b3918eea8c72 [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 Mackerras371fefd2011-06-29 00:23:08 +0000779 /*
Paul Mackerras19ccb762011-07-23 17:42:46 +1000780 * We consider the vcpu stopped until we see the first run ioctl for it.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000781 */
Paul Mackerras19ccb762011-07-23 17:42:46 +1000782 vcpu->arch.state = KVMPPC_VCPU_STOPPED;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000783
784 init_waitqueue_head(&vcpu->arch.cpu_run);
785
786 mutex_lock(&kvm->lock);
787 vcore = kvm->arch.vcores[core];
788 if (!vcore) {
789 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
790 if (vcore) {
791 INIT_LIST_HEAD(&vcore->runnable_threads);
792 spin_lock_init(&vcore->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000793 init_waitqueue_head(&vcore->wq);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000794 vcore->preempt_tb = mftb();
Paul Mackerras371fefd2011-06-29 00:23:08 +0000795 }
796 kvm->arch.vcores[core] = vcore;
797 }
798 mutex_unlock(&kvm->lock);
799
800 if (!vcore)
801 goto free_vcpu;
802
803 spin_lock(&vcore->lock);
804 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000805 spin_unlock(&vcore->lock);
806 vcpu->arch.vcore = vcore;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000807 vcpu->arch.stolen_logged = vcore->stolen_tb;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000808
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200809 vcpu->arch.cpu_type = KVM_CPU_3S_64;
810 kvmppc_sanity_check(vcpu);
811
Paul Mackerrasde56a942011-06-29 00:21:34 +0000812 return vcpu;
813
814free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200815 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000816out:
817 return ERR_PTR(err);
818}
819
820void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
821{
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000822 spin_lock(&vcpu->arch.vpa_update_lock);
823 if (vcpu->arch.dtl.pinned_addr)
824 kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.dtl.pinned_addr);
825 if (vcpu->arch.slb_shadow.pinned_addr)
826 kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.slb_shadow.pinned_addr);
827 if (vcpu->arch.vpa.pinned_addr)
828 kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.vpa.pinned_addr);
829 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000830 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200831 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000832}
833
Paul Mackerras19ccb762011-07-23 17:42:46 +1000834static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000835{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000836 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000837
Paul Mackerras19ccb762011-07-23 17:42:46 +1000838 now = get_tb();
839 if (now > vcpu->arch.dec_expires) {
840 /* decrementer has already gone negative */
841 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -0600842 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000843 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000844 }
Paul Mackerras19ccb762011-07-23 17:42:46 +1000845 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
846 / tb_ticks_per_sec;
847 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
848 HRTIMER_MODE_REL);
849 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000850}
851
Paul Mackerras19ccb762011-07-23 17:42:46 +1000852static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000853{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000854 vcpu->arch.ceded = 0;
855 if (vcpu->arch.timer_running) {
856 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
857 vcpu->arch.timer_running = 0;
858 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000859}
860
861extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
862extern void xics_wake_cpu(int cpu);
863
864static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
865 struct kvm_vcpu *vcpu)
866{
Paul Mackerras371fefd2011-06-29 00:23:08 +0000867 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
868 return;
869 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
870 --vc->n_runnable;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000871 ++vc->n_busy;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000872 list_del(&vcpu->arch.run_list);
873}
874
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000875static int kvmppc_grab_hwthread(int cpu)
876{
877 struct paca_struct *tpaca;
878 long timeout = 1000;
879
880 tpaca = &paca[cpu];
881
882 /* Ensure the thread won't go into the kernel if it wakes */
883 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerras7b444c62012-10-15 01:16:14 +0000884 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000885
886 /*
887 * If the thread is already executing in the kernel (e.g. handling
888 * a stray interrupt), wait for it to get back to nap mode.
889 * The smp_mb() is to ensure that our setting of hwthread_req
890 * is visible before we look at hwthread_state, so if this
891 * races with the code at system_reset_pSeries and the thread
892 * misses our setting of hwthread_req, we are sure to see its
893 * setting of hwthread_state, and vice versa.
894 */
895 smp_mb();
896 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
897 if (--timeout <= 0) {
898 pr_err("KVM: couldn't grab cpu %d\n", cpu);
899 return -EBUSY;
900 }
901 udelay(1);
902 }
903 return 0;
904}
905
906static void kvmppc_release_hwthread(int cpu)
907{
908 struct paca_struct *tpaca;
909
910 tpaca = &paca[cpu];
911 tpaca->kvm_hstate.hwthread_req = 0;
912 tpaca->kvm_hstate.kvm_vcpu = NULL;
913}
914
Paul Mackerras371fefd2011-06-29 00:23:08 +0000915static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
916{
917 int cpu;
918 struct paca_struct *tpaca;
919 struct kvmppc_vcore *vc = vcpu->arch.vcore;
920
Paul Mackerras19ccb762011-07-23 17:42:46 +1000921 if (vcpu->arch.timer_running) {
922 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
923 vcpu->arch.timer_running = 0;
924 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000925 cpu = vc->pcpu + vcpu->arch.ptid;
926 tpaca = &paca[cpu];
927 tpaca->kvm_hstate.kvm_vcpu = vcpu;
928 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000929 tpaca->kvm_hstate.napping = 0;
930 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000931 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +0000932#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000933 if (vcpu->arch.ptid) {
Paul Mackerras371fefd2011-06-29 00:23:08 +0000934 xics_wake_cpu(cpu);
935 ++vc->n_woken;
936 }
937#endif
938}
939
940static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
941{
942 int i;
943
944 HMT_low();
945 i = 0;
946 while (vc->nap_count < vc->n_woken) {
947 if (++i >= 1000000) {
948 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
949 vc->nap_count, vc->n_woken);
950 break;
951 }
952 cpu_relax();
953 }
954 HMT_medium();
955}
956
957/*
958 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +0000959 * this core are off-line. Then grab the threads so they can't
960 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000961 */
962static int on_primary_thread(void)
963{
964 int cpu = smp_processor_id();
965 int thr = cpu_thread_in_core(cpu);
966
967 if (thr)
968 return 0;
969 while (++thr < threads_per_core)
970 if (cpu_online(cpu + thr))
971 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +0000972
973 /* Grab all hw threads so they can't go into the kernel */
974 for (thr = 1; thr < threads_per_core; ++thr) {
975 if (kvmppc_grab_hwthread(cpu + thr)) {
976 /* Couldn't grab one; let the others go */
977 do {
978 kvmppc_release_hwthread(cpu + thr);
979 } while (--thr > 0);
980 return 0;
981 }
982 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000983 return 1;
984}
985
986/*
987 * Run a set of guest threads on a physical core.
988 * Called with vc->lock held.
989 */
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000990static void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000991{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000992 struct kvm_vcpu *vcpu, *vcpu0, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000993 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000994 u64 now;
Paul Mackerras081f3232012-06-01 20:20:24 +1000995 int ptid, i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000996 int srcu_idx;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000997 struct kvm_vcpu *vcpus_to_update[threads_per_core];
Paul Mackerrasde56a942011-06-29 00:21:34 +0000998
Paul Mackerras371fefd2011-06-29 00:23:08 +0000999 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +10001000 need_vpa_update = 0;
1001 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001002 if (signal_pending(vcpu->arch.run_task))
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001003 return;
1004 if (vcpu->arch.vpa.update_pending ||
1005 vcpu->arch.slb_shadow.update_pending ||
1006 vcpu->arch.dtl.update_pending)
1007 vcpus_to_update[need_vpa_update++] = vcpu;
Paul Mackerras081f3232012-06-01 20:20:24 +10001008 }
1009
1010 /*
1011 * Initialize *vc, in particular vc->vcore_state, so we can
1012 * drop the vcore lock if necessary.
1013 */
1014 vc->n_woken = 0;
1015 vc->nap_count = 0;
1016 vc->entry_exit_count = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001017 vc->vcore_state = VCORE_STARTING;
Paul Mackerras081f3232012-06-01 20:20:24 +10001018 vc->in_guest = 0;
1019 vc->napping_threads = 0;
1020
1021 /*
1022 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1023 * which can't be called with any spinlocks held.
1024 */
1025 if (need_vpa_update) {
1026 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001027 for (i = 0; i < need_vpa_update; ++i)
1028 kvmppc_update_vpas(vcpus_to_update[i]);
Paul Mackerras081f3232012-06-01 20:20:24 +10001029 spin_lock(&vc->lock);
1030 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001031
1032 /*
Paul Mackerras19ccb762011-07-23 17:42:46 +10001033 * Assign physical thread IDs, first to non-ceded vcpus
1034 * and then to ceded ones.
1035 */
1036 ptid = 0;
1037 vcpu0 = NULL;
1038 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1039 if (!vcpu->arch.ceded) {
1040 if (!ptid)
1041 vcpu0 = vcpu;
1042 vcpu->arch.ptid = ptid++;
1043 }
1044 }
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001045 if (!vcpu0) {
1046 vc->vcore_state = VCORE_INACTIVE;
1047 return; /* nothing to run; should never happen */
1048 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001049 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1050 if (vcpu->arch.ceded)
1051 vcpu->arch.ptid = ptid++;
1052
Paul Mackerras7b444c62012-10-15 01:16:14 +00001053 /*
1054 * Make sure we are running on thread 0, and that
1055 * secondary threads are offline.
1056 */
1057 if (threads_per_core > 1 && !on_primary_thread()) {
1058 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1059 vcpu->arch.ret = -EBUSY;
1060 goto out;
1061 }
1062
Paul Mackerras0456ec42012-02-03 00:56:21 +00001063 vc->stolen_tb += mftb() - vc->preempt_tb;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001064 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001065 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001066 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001067 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001068 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001069
Paul Mackerras2f12f032012-10-15 01:17:17 +00001070 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001071 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001072 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001073
Paul Mackerras19ccb762011-07-23 17:42:46 +10001074 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001075
1076 srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
1077
Paul Mackerras19ccb762011-07-23 17:42:46 +10001078 __kvmppc_vcore_entry(NULL, vcpu0);
1079
Paul Mackerras371fefd2011-06-29 00:23:08 +00001080 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001081 /* disable sending of IPIs on virtual external irqs */
1082 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1083 vcpu->cpu = -1;
1084 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001085 if (vc->nap_count < vc->n_woken)
1086 kvmppc_wait_for_nap(vc);
Paul Mackerras2f12f032012-10-15 01:17:17 +00001087 for (i = 0; i < threads_per_core; ++i)
1088 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001089 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001090 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001091 spin_unlock(&vc->lock);
1092
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001093 srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
1094
Paul Mackerras371fefd2011-06-29 00:23:08 +00001095 /* make sure updates to secondary vcpu structs are visible now */
1096 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001097 kvm_guest_exit();
1098
1099 preempt_enable();
1100 kvm_resched(vcpu);
1101
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001102 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001103 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001104 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1105 /* cancel pending dec exception if dec is positive */
1106 if (now < vcpu->arch.dec_expires &&
1107 kvmppc_core_pending_dec(vcpu))
1108 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001109
1110 ret = RESUME_GUEST;
1111 if (vcpu->arch.trap)
1112 ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
1113 vcpu->arch.run_task);
1114
Paul Mackerras371fefd2011-06-29 00:23:08 +00001115 vcpu->arch.ret = ret;
1116 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001117
1118 if (vcpu->arch.ceded) {
1119 if (ret != RESUME_GUEST)
1120 kvmppc_end_cede(vcpu);
1121 else
1122 kvmppc_set_timer(vcpu);
1123 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001124 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001125
Paul Mackerrasde56a942011-06-29 00:21:34 +00001126 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001127 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras0456ec42012-02-03 00:56:21 +00001128 vc->preempt_tb = mftb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001129 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1130 arch.run_list) {
1131 if (vcpu->arch.ret != RESUME_GUEST) {
1132 kvmppc_remove_runnable(vc, vcpu);
1133 wake_up(&vcpu->arch.cpu_run);
1134 }
1135 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001136}
1137
Paul Mackerras19ccb762011-07-23 17:42:46 +10001138/*
1139 * Wait for some other vcpu thread to execute us, and
1140 * wake us up when we need to handle something in the host.
1141 */
1142static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001143{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001144 DEFINE_WAIT(wait);
1145
Paul Mackerras19ccb762011-07-23 17:42:46 +10001146 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1147 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1148 schedule();
1149 finish_wait(&vcpu->arch.cpu_run, &wait);
1150}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001151
Paul Mackerras19ccb762011-07-23 17:42:46 +10001152/*
1153 * All the vcpus in this vcore are idle, so wait for a decrementer
1154 * or external interrupt to one of the vcpus. vc->lock is held.
1155 */
1156static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1157{
1158 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001159
1160 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1161 vc->vcore_state = VCORE_SLEEPING;
1162 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001163 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001164 finish_wait(&vc->wq, &wait);
1165 spin_lock(&vc->lock);
1166 vc->vcore_state = VCORE_INACTIVE;
1167}
1168
1169static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1170{
1171 int n_ceded;
1172 int prev_state;
1173 struct kvmppc_vcore *vc;
1174 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001175
Paul Mackerras371fefd2011-06-29 00:23:08 +00001176 kvm_run->exit_reason = 0;
1177 vcpu->arch.ret = RESUME_GUEST;
1178 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001179 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001180
Paul Mackerras371fefd2011-06-29 00:23:08 +00001181 /*
1182 * Synchronize with other threads in this virtual core
1183 */
1184 vc = vcpu->arch.vcore;
1185 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001186 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001187 vcpu->arch.run_task = current;
1188 vcpu->arch.kvm_run = kvm_run;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001189 prev_state = vcpu->arch.state;
1190 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001191 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1192 ++vc->n_runnable;
1193
Paul Mackerras19ccb762011-07-23 17:42:46 +10001194 /*
1195 * This happens the first time this is called for a vcpu.
1196 * If the vcore is already running, we may be able to start
1197 * this thread straight away and have it join in.
1198 */
1199 if (prev_state == KVMPPC_VCPU_STOPPED) {
1200 if (vc->vcore_state == VCORE_RUNNING &&
1201 VCORE_EXIT_COUNT(vc) == 0) {
1202 vcpu->arch.ptid = vc->n_runnable - 1;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001203 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001204 kvmppc_start_thread(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001205 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001206
Paul Mackerras19ccb762011-07-23 17:42:46 +10001207 } else if (prev_state == KVMPPC_VCPU_BUSY_IN_HOST)
1208 --vc->n_busy;
1209
1210 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1211 !signal_pending(current)) {
1212 if (vc->n_busy || vc->vcore_state != VCORE_INACTIVE) {
1213 spin_unlock(&vc->lock);
1214 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1215 spin_lock(&vc->lock);
1216 continue;
1217 }
Paul Mackerras0456ec42012-02-03 00:56:21 +00001218 vc->runner = vcpu;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001219 n_ceded = 0;
1220 list_for_each_entry(v, &vc->runnable_threads, arch.run_list)
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001221 if (!v->arch.pending_exceptions)
1222 n_ceded += v->arch.ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001223 if (n_ceded == vc->n_runnable)
1224 kvmppc_vcore_blocked(vc);
1225 else
1226 kvmppc_run_core(vc);
1227
1228 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1229 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001230 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001231 if (signal_pending(v->arch.run_task)) {
1232 kvmppc_remove_runnable(vc, v);
1233 v->stat.signal_exits++;
1234 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1235 v->arch.ret = -EINTR;
1236 wake_up(&v->arch.cpu_run);
1237 }
1238 }
Paul Mackerras0456ec42012-02-03 00:56:21 +00001239 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001240 }
1241
Paul Mackerras19ccb762011-07-23 17:42:46 +10001242 if (signal_pending(current)) {
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001243 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1244 (vc->vcore_state == VCORE_RUNNING ||
1245 vc->vcore_state == VCORE_EXITING)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001246 spin_unlock(&vc->lock);
1247 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1248 spin_lock(&vc->lock);
1249 }
1250 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1251 kvmppc_remove_runnable(vc, vcpu);
1252 vcpu->stat.signal_exits++;
1253 kvm_run->exit_reason = KVM_EXIT_INTR;
1254 vcpu->arch.ret = -EINTR;
1255 }
1256 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001257
Paul Mackerras19ccb762011-07-23 17:42:46 +10001258 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001259 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001260}
1261
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001262int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
1263{
1264 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001265 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001266
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001267 if (!vcpu->arch.sane) {
1268 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1269 return -EINVAL;
1270 }
1271
Scott Wood25051b52011-11-08 18:23:23 -06001272 kvmppc_core_prepare_to_enter(vcpu);
1273
Paul Mackerras19ccb762011-07-23 17:42:46 +10001274 /* No need to go into the guest when all we'll do is come back out */
1275 if (signal_pending(current)) {
1276 run->exit_reason = KVM_EXIT_INTR;
1277 return -EINTR;
1278 }
1279
Paul Mackerras32fad282012-05-04 02:32:53 +00001280 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1281 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1282 smp_mb();
1283
1284 /* On the first time here, set up HTAB and VRMA or RMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001285 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001286 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001287 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001288 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001289 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001290
1291 flush_fp_to_thread(current);
1292 flush_altivec_to_thread(current);
1293 flush_vsx_to_thread(current);
1294 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001295 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001296
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001297 do {
1298 r = kvmppc_run_vcpu(run, vcpu);
1299
1300 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1301 !(vcpu->arch.shregs.msr & MSR_PR)) {
1302 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001303 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001304 } else if (r == RESUME_PAGE_FAULT) {
1305 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1306 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1307 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1308 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001309 }
1310 } while (r == RESUME_GUEST);
Paul Mackerras32fad282012-05-04 02:32:53 +00001311
1312 out:
1313 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001314 return r;
1315}
1316
David Gibson54738c02011-06-29 00:22:41 +00001317
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001318/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +00001319 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001320static inline int lpcr_rmls(unsigned long rma_size)
1321{
1322 switch (rma_size) {
1323 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001324 if (cpu_has_feature(CPU_FTR_ARCH_206))
1325 return 8; /* only supported on POWER7 */
1326 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001327 case 64ul << 20: /* 64 MB */
1328 return 3;
1329 case 128ul << 20: /* 128 MB */
1330 return 7;
1331 case 256ul << 20: /* 256 MB */
1332 return 4;
1333 case 1ul << 30: /* 1 GB */
1334 return 2;
1335 case 16ul << 30: /* 16 GB */
1336 return 1;
1337 case 256ul << 30: /* 256 GB */
1338 return 0;
1339 default:
1340 return -1;
1341 }
1342}
1343
1344static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1345{
Alexander Grafb4e70612012-01-16 16:50:10 +01001346 struct kvmppc_linear_info *ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001347 struct page *page;
1348
1349 if (vmf->pgoff >= ri->npages)
1350 return VM_FAULT_SIGBUS;
1351
1352 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1353 get_page(page);
1354 vmf->page = page;
1355 return 0;
1356}
1357
1358static const struct vm_operations_struct kvm_rma_vm_ops = {
1359 .fault = kvm_rma_fault,
1360};
1361
1362static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1363{
1364 vma->vm_flags |= VM_RESERVED;
1365 vma->vm_ops = &kvm_rma_vm_ops;
1366 return 0;
1367}
1368
1369static int kvm_rma_release(struct inode *inode, struct file *filp)
1370{
Alexander Grafb4e70612012-01-16 16:50:10 +01001371 struct kvmppc_linear_info *ri = filp->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001372
1373 kvm_release_rma(ri);
1374 return 0;
1375}
1376
1377static struct file_operations kvm_rma_fops = {
1378 .mmap = kvm_rma_mmap,
1379 .release = kvm_rma_release,
1380};
1381
1382long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
1383{
Alexander Grafb4e70612012-01-16 16:50:10 +01001384 struct kvmppc_linear_info *ri;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001385 long fd;
1386
1387 ri = kvm_alloc_rma();
1388 if (!ri)
1389 return -ENOMEM;
1390
1391 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR);
1392 if (fd < 0)
1393 kvm_release_rma(ri);
1394
1395 ret->rma_size = ri->npages << PAGE_SHIFT;
1396 return fd;
1397}
1398
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001399static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1400 int linux_psize)
1401{
1402 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1403
1404 if (!def->shift)
1405 return;
1406 (*sps)->page_shift = def->shift;
1407 (*sps)->slb_enc = def->sllp;
1408 (*sps)->enc[0].page_shift = def->shift;
1409 (*sps)->enc[0].pte_enc = def->penc;
1410 (*sps)++;
1411}
1412
1413int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
1414{
1415 struct kvm_ppc_one_seg_page_size *sps;
1416
1417 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1418 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1419 info->flags |= KVM_PPC_1T_SEGMENTS;
1420 info->slb_size = mmu_slb_size;
1421
1422 /* We only support these sizes for now, and no muti-size segments */
1423 sps = &info->sps[0];
1424 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1425 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1426 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1427
1428 return 0;
1429}
1430
Paul Mackerras82ed3612011-12-15 02:03:22 +00001431/*
1432 * Get (and clear) the dirty memory log for a memory slot.
1433 */
1434int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1435{
1436 struct kvm_memory_slot *memslot;
1437 int r;
1438 unsigned long n;
1439
1440 mutex_lock(&kvm->slots_lock);
1441
1442 r = -EINVAL;
1443 if (log->slot >= KVM_MEMORY_SLOTS)
1444 goto out;
1445
1446 memslot = id_to_memslot(kvm->memslots, log->slot);
1447 r = -ENOENT;
1448 if (!memslot->dirty_bitmap)
1449 goto out;
1450
1451 n = kvm_dirty_bitmap_bytes(memslot);
1452 memset(memslot->dirty_bitmap, 0, n);
1453
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001454 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00001455 if (r)
1456 goto out;
1457
1458 r = -EFAULT;
1459 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1460 goto out;
1461
1462 r = 0;
1463out:
1464 mutex_unlock(&kvm->slots_lock);
1465 return r;
1466}
1467
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001468static unsigned long slb_pgsize_encoding(unsigned long psize)
1469{
1470 unsigned long senc = 0;
1471
1472 if (psize > 0x1000) {
1473 senc = SLB_VSID_L;
1474 if (psize == 0x10000)
1475 senc |= SLB_VSID_LP_01;
1476 }
1477 return senc;
1478}
1479
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001480static void unpin_slot(struct kvm_memory_slot *memslot)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001481{
1482 unsigned long *physp;
1483 unsigned long j, npages, pfn;
1484 struct page *page;
1485
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001486 physp = memslot->arch.slot_phys;
1487 npages = memslot->npages;
1488 if (!physp)
1489 return;
1490 for (j = 0; j < npages; j++) {
1491 if (!(physp[j] & KVMPPC_GOT_PAGE))
1492 continue;
1493 pfn = physp[j] >> PAGE_SHIFT;
1494 page = pfn_to_page(pfn);
1495 SetPageDirty(page);
1496 put_page(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001497 }
1498}
1499
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001500void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1501 struct kvm_memory_slot *dont)
1502{
1503 if (!dont || free->arch.rmap != dont->arch.rmap) {
1504 vfree(free->arch.rmap);
1505 free->arch.rmap = NULL;
1506 }
1507 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1508 unpin_slot(free);
1509 vfree(free->arch.slot_phys);
1510 free->arch.slot_phys = NULL;
1511 }
1512}
1513
1514int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1515 unsigned long npages)
1516{
1517 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1518 if (!slot->arch.rmap)
1519 return -ENOMEM;
1520 slot->arch.slot_phys = NULL;
1521
1522 return 0;
1523}
1524
1525int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1526 struct kvm_memory_slot *memslot,
1527 struct kvm_userspace_memory_region *mem)
1528{
1529 unsigned long *phys;
1530
1531 /* Allocate a slot_phys array if needed */
1532 phys = memslot->arch.slot_phys;
1533 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1534 phys = vzalloc(memslot->npages * sizeof(unsigned long));
1535 if (!phys)
1536 return -ENOMEM;
1537 memslot->arch.slot_phys = phys;
1538 }
1539
1540 return 0;
1541}
1542
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001543void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001544 struct kvm_userspace_memory_region *mem,
1545 struct kvm_memory_slot old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001546{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001547 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1548 struct kvm_memory_slot *memslot;
1549
1550 if (npages && old.npages) {
1551 /*
1552 * If modifying a memslot, reset all the rmap dirty bits.
1553 * If this is a new memslot, we don't need to do anything
1554 * since the rmap array starts out as all zeroes,
1555 * i.e. no pages are dirty.
1556 */
1557 memslot = id_to_memslot(kvm->memslots, mem->slot);
1558 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1559 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001560}
1561
Paul Mackerras32fad282012-05-04 02:32:53 +00001562static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001563{
1564 int err = 0;
1565 struct kvm *kvm = vcpu->kvm;
Alexander Grafb4e70612012-01-16 16:50:10 +01001566 struct kvmppc_linear_info *ri = NULL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001567 unsigned long hva;
1568 struct kvm_memory_slot *memslot;
1569 struct vm_area_struct *vma;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001570 unsigned long lpcr, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001571 unsigned long psize, porder;
1572 unsigned long rma_size;
1573 unsigned long rmls;
1574 unsigned long *physp;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001575 unsigned long i, npages;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001576 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001577
1578 mutex_lock(&kvm->lock);
1579 if (kvm->arch.rma_setup_done)
1580 goto out; /* another vcpu beat us to it */
1581
Paul Mackerras32fad282012-05-04 02:32:53 +00001582 /* Allocate hashed page table (if not done already) and reset it */
1583 if (!kvm->arch.hpt_virt) {
1584 err = kvmppc_alloc_hpt(kvm, NULL);
1585 if (err) {
1586 pr_err("KVM: Couldn't alloc HPT\n");
1587 goto out;
1588 }
1589 }
1590
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001591 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001592 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001593 memslot = gfn_to_memslot(kvm, 0);
1594
1595 /* We must have some memory at 0 by now */
1596 err = -EINVAL;
1597 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001598 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001599
1600 /* Look up the VMA for the start of this memory slot */
1601 hva = memslot->userspace_addr;
1602 down_read(&current->mm->mmap_sem);
1603 vma = find_vma(current->mm, hva);
1604 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
1605 goto up_out;
1606
1607 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001608 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001609
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001610 /* Is this one of our preallocated RMAs? */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001611 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
1612 hva == vma->vm_start)
1613 ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001614
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001615 up_read(&current->mm->mmap_sem);
1616
1617 if (!ri) {
1618 /* On POWER7, use VRMA; on PPC970, give up */
1619 err = -EPERM;
1620 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1621 pr_err("KVM: CPU requires an RMO\n");
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001622 goto out_srcu;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001623 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001624
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001625 /* We can handle 4k, 64k or 16M pages in the VRMA */
1626 err = -EINVAL;
1627 if (!(psize == 0x1000 || psize == 0x10000 ||
1628 psize == 0x1000000))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001629 goto out_srcu;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001630
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001631 /* Update VRMASD field in the LPCR */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001632 senc = slb_pgsize_encoding(psize);
Paul Mackerras697d3892011-12-12 12:36:37 +00001633 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1634 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001635 lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
1636 lpcr |= senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001637 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001638
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001639 /* Create HPTEs in the hash page table for the VRMA */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001640 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001641
1642 } else {
1643 /* Set up to use an RMO region */
1644 rma_size = ri->npages;
1645 if (rma_size > memslot->npages)
1646 rma_size = memslot->npages;
1647 rma_size <<= PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001648 rmls = lpcr_rmls(rma_size);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001649 err = -EINVAL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001650 if (rmls < 0) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001651 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001652 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001653 }
1654 atomic_inc(&ri->use_count);
1655 kvm->arch.rma = ri;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001656
1657 /* Update LPCR and RMOR */
1658 lpcr = kvm->arch.lpcr;
1659 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1660 /* PPC970; insert RMLS value (split field) in HID4 */
1661 lpcr &= ~((1ul << HID4_RMLS0_SH) |
1662 (3ul << HID4_RMLS2_SH));
1663 lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
1664 ((rmls & 3) << HID4_RMLS2_SH);
1665 /* RMOR is also in HID4 */
1666 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
1667 << HID4_RMOR_SH;
1668 } else {
1669 /* POWER7 */
1670 lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
1671 lpcr |= rmls << LPCR_RMLS_SH;
1672 kvm->arch.rmor = kvm->arch.rma->base_pfn << PAGE_SHIFT;
1673 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001674 kvm->arch.lpcr = lpcr;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001675 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001676 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001677
1678 /* Initialize phys addrs of pages in RMO */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001679 npages = ri->npages;
1680 porder = __ilog2(npages);
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001681 physp = memslot->arch.slot_phys;
1682 if (physp) {
1683 if (npages > memslot->npages)
1684 npages = memslot->npages;
1685 spin_lock(&kvm->arch.slot_phys_lock);
1686 for (i = 0; i < npages; ++i)
1687 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
1688 porder;
1689 spin_unlock(&kvm->arch.slot_phys_lock);
1690 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001691 }
1692
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001693 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
1694 smp_wmb();
1695 kvm->arch.rma_setup_done = 1;
1696 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001697 out_srcu:
1698 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001699 out:
1700 mutex_unlock(&kvm->lock);
1701 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001702
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001703 up_out:
1704 up_read(&current->mm->mmap_sem);
1705 goto out;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001706}
1707
1708int kvmppc_core_init_vm(struct kvm *kvm)
1709{
Paul Mackerras32fad282012-05-04 02:32:53 +00001710 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001711
Paul Mackerras32fad282012-05-04 02:32:53 +00001712 /* Allocate the guest's logical partition ID */
1713
1714 lpid = kvmppc_alloc_lpid();
1715 if (lpid < 0)
1716 return -ENOMEM;
1717 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001718
David Gibson54738c02011-06-29 00:22:41 +00001719 INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001720
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001721 kvm->arch.rma = NULL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001722
Paul Mackerras9e368f22011-06-29 00:40:08 +00001723 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001724
Paul Mackerras9e368f22011-06-29 00:40:08 +00001725 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1726 /* PPC970; HID4 is effectively the LPCR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001727 kvm->arch.host_lpid = 0;
1728 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
1729 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
1730 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
1731 ((lpid & 0xf) << HID4_LPID5_SH);
1732 } else {
1733 /* POWER7; init LPCR for virtual RMA mode */
1734 kvm->arch.host_lpid = mfspr(SPRN_LPID);
1735 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
1736 lpcr &= LPCR_PECE | LPCR_LPES;
1737 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
Paul Mackerras697d3892011-12-12 12:36:37 +00001738 LPCR_VPM0 | LPCR_VPM1;
1739 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
1740 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerras9e368f22011-06-29 00:40:08 +00001741 }
1742 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001743
Paul Mackerras342d3db2011-12-12 12:38:05 +00001744 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001745 spin_lock_init(&kvm->arch.slot_phys_lock);
Paul Mackerras512691d2012-10-15 01:15:41 +00001746
1747 /*
1748 * Don't allow secondary CPU threads to come online
1749 * while any KVM VMs exist.
1750 */
1751 inhibit_secondary_onlining();
1752
David Gibson54738c02011-06-29 00:22:41 +00001753 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001754}
1755
1756void kvmppc_core_destroy_vm(struct kvm *kvm)
1757{
Paul Mackerras512691d2012-10-15 01:15:41 +00001758 uninhibit_secondary_onlining();
1759
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001760 if (kvm->arch.rma) {
1761 kvm_release_rma(kvm->arch.rma);
1762 kvm->arch.rma = NULL;
1763 }
1764
Paul Mackerrasde56a942011-06-29 00:21:34 +00001765 kvmppc_free_hpt(kvm);
David Gibson54738c02011-06-29 00:22:41 +00001766 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
Paul Mackerrasde56a942011-06-29 00:21:34 +00001767}
1768
1769/* These are stubs for now */
1770void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
1771{
1772}
1773
1774/* We don't need to emulate any privileged instructions or dcbz */
1775int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
1776 unsigned int inst, int *advance)
1777{
1778 return EMULATE_FAIL;
1779}
1780
Alexander Graf54771e62012-05-04 14:55:12 +02001781int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001782{
1783 return EMULATE_FAIL;
1784}
1785
Alexander Graf54771e62012-05-04 14:55:12 +02001786int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001787{
1788 return EMULATE_FAIL;
1789}
1790
1791static int kvmppc_book3s_hv_init(void)
1792{
1793 int r;
1794
1795 r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1796
1797 if (r)
1798 return r;
1799
1800 r = kvmppc_mmu_hv_init();
1801
1802 return r;
1803}
1804
1805static void kvmppc_book3s_hv_exit(void)
1806{
1807 kvm_exit();
1808}
1809
1810module_init(kvmppc_book3s_hv_init);
1811module_exit(kvmppc_book3s_hv_exit);