blob: 7e1813ceabc1ce2f17ddf0722e42053e181f067d [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>
Alexander Graf398a76c2013-12-09 13:53:42 +010034#include <linux/miscdevice.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000035
36#include <asm/reg.h>
37#include <asm/cputable.h>
38#include <asm/cacheflush.h>
39#include <asm/tlbflush.h>
40#include <asm/uaccess.h>
41#include <asm/io.h>
42#include <asm/kvm_ppc.h>
43#include <asm/kvm_book3s.h>
44#include <asm/mmu_context.h>
45#include <asm/lppaca.h>
46#include <asm/processor.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000047#include <asm/cputhreads.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000048#include <asm/page.h>
Michael Neulingde1d9242011-11-09 20:39:49 +000049#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010050#include <asm/switch_to.h>
Paul Mackerras512691d2012-10-15 01:15:41 +000051#include <asm/smp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000052#include <linux/gfp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000053#include <linux/vmalloc.h>
54#include <linux/highmem.h>
Paul Mackerrasc77162d2011-12-12 12:31:00 +000055#include <linux/hugetlb.h>
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +053056#include <linux/module.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000057
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053058#include "book3s.h"
59
Paul Mackerrasde56a942011-06-29 00:21:34 +000060/* #define EXIT_DEBUG */
61/* #define EXIT_DEBUG_SIMPLE */
62/* #define EXIT_DEBUG_INT */
63
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000064/* Used to indicate that a guest page fault needs to be handled */
65#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
66
Paul Mackerrasc7b67672012-10-15 01:18:07 +000067/* Used as a "null" value for timebase values */
68#define TB_NIL (~(u64)0)
69
Paul Mackerras19ccb762011-07-23 17:42:46 +100070static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +000071static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +100072
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053073static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +000074{
75 int me;
76 int cpu = vcpu->cpu;
77 wait_queue_head_t *wqp;
78
79 wqp = kvm_arch_vcpu_wq(vcpu);
80 if (waitqueue_active(wqp)) {
81 wake_up_interruptible(wqp);
82 ++vcpu->stat.halt_wakeup;
83 }
84
85 me = get_cpu();
86
87 /* CPU points to the first thread of the core */
88 if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
Andreas Schwab48eaef02013-12-30 15:36:56 +010089#ifdef CONFIG_KVM_XICS
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +000090 int real_cpu = cpu + vcpu->arch.ptid;
91 if (paca[real_cpu].kvm_hstate.xics_phys)
92 xics_wake_cpu(real_cpu);
Andreas Schwab48eaef02013-12-30 15:36:56 +010093 else
94#endif
95 if (cpu_online(cpu))
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +000096 smp_send_reschedule(cpu);
97 }
98 put_cpu();
99}
100
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000101/*
102 * We use the vcpu_load/put functions to measure stolen time.
103 * Stolen time is counted as time when either the vcpu is able to
104 * run as part of a virtual core, but the task running the vcore
105 * is preempted or sleeping, or when the vcpu needs something done
106 * in the kernel by the task running the vcpu, but that task is
107 * preempted or sleeping. Those two things have to be counted
108 * separately, since one of the vcpu tasks will take on the job
109 * of running the core, and the other vcpu tasks in the vcore will
110 * sleep waiting for it to do that, but that sleep shouldn't count
111 * as stolen time.
112 *
113 * Hence we accumulate stolen time when the vcpu can run as part of
114 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
115 * needs its task to do other things in the kernel (for example,
116 * service a page fault) in busy_stolen. We don't accumulate
117 * stolen time for a vcore when it is inactive, or for a vcpu
118 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
119 * a misnomer; it means that the vcpu task is not executing in
120 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
121 * the kernel. We don't have any way of dividing up that time
122 * between time that the vcpu is genuinely stopped, time that
123 * the task is actively working on behalf of the vcpu, and time
124 * that the task is preempted, so we don't count any of it as
125 * stolen.
126 *
127 * Updates to busy_stolen are protected by arch.tbacct_lock;
128 * updates to vc->stolen_tb are protected by the arch.tbacct_lock
129 * of the vcpu that has taken responsibility for running the vcore
130 * (i.e. vc->runner). The stolen times are measured in units of
131 * timebase ticks. (Note that the != TB_NIL checks below are
132 * purely defensive; they should never fail.)
133 */
134
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530135static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000136{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000137 struct kvmppc_vcore *vc = vcpu->arch.vcore;
138
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000139 spin_lock(&vcpu->arch.tbacct_lock);
140 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
141 vc->preempt_tb != TB_NIL) {
Paul Mackerras0456ec42012-02-03 00:56:21 +0000142 vc->stolen_tb += mftb() - vc->preempt_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000143 vc->preempt_tb = TB_NIL;
144 }
145 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
146 vcpu->arch.busy_preempt != TB_NIL) {
147 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
148 vcpu->arch.busy_preempt = TB_NIL;
149 }
150 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000151}
152
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530153static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000154{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000155 struct kvmppc_vcore *vc = vcpu->arch.vcore;
156
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000157 spin_lock(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000158 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
159 vc->preempt_tb = mftb();
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000160 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
161 vcpu->arch.busy_preempt = mftb();
162 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000163}
164
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530165static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000166{
167 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000168 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000169}
170
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530171void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000172{
173 vcpu->arch.pvr = pvr;
174}
175
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000176int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
177{
178 unsigned long pcr = 0;
179 struct kvmppc_vcore *vc = vcpu->arch.vcore;
180
181 if (arch_compat) {
182 if (!cpu_has_feature(CPU_FTR_ARCH_206))
183 return -EINVAL; /* 970 has no compat mode support */
184
185 switch (arch_compat) {
186 case PVR_ARCH_205:
187 pcr = PCR_ARCH_205;
188 break;
189 case PVR_ARCH_206:
190 case PVR_ARCH_206p:
191 break;
192 default:
193 return -EINVAL;
194 }
195 }
196
197 spin_lock(&vc->lock);
198 vc->arch_compat = arch_compat;
199 vc->pcr = pcr;
200 spin_unlock(&vc->lock);
201
202 return 0;
203}
204
Paul Mackerrasde56a942011-06-29 00:21:34 +0000205void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
206{
207 int r;
208
209 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
210 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
211 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
212 for (r = 0; r < 16; ++r)
213 pr_err("r%2d = %.16lx r%d = %.16lx\n",
214 r, kvmppc_get_gpr(vcpu, r),
215 r+16, kvmppc_get_gpr(vcpu, r+16));
216 pr_err("ctr = %.16lx lr = %.16lx\n",
217 vcpu->arch.ctr, vcpu->arch.lr);
218 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
219 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
220 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
221 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
222 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
223 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
224 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
225 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
226 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
227 pr_err("fault dar = %.16lx dsisr = %.8x\n",
228 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
229 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
230 for (r = 0; r < vcpu->arch.slb_max; ++r)
231 pr_err(" ESID = %.16llx VSID = %.16llx\n",
232 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
233 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000234 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000235 vcpu->arch.last_inst);
236}
237
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000238struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
239{
240 int r;
241 struct kvm_vcpu *v, *ret = NULL;
242
243 mutex_lock(&kvm->lock);
244 kvm_for_each_vcpu(r, v, kvm) {
245 if (v->vcpu_id == id) {
246 ret = v;
247 break;
248 }
249 }
250 mutex_unlock(&kvm->lock);
251 return ret;
252}
253
254static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
255{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000256 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000257 vpa->yield_count = 1;
258}
259
Paul Mackerras55b665b2012-09-25 20:33:06 +0000260static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
261 unsigned long addr, unsigned long len)
262{
263 /* check address is cacheline aligned */
264 if (addr & (L1_CACHE_BYTES - 1))
265 return -EINVAL;
266 spin_lock(&vcpu->arch.vpa_update_lock);
267 if (v->next_gpa != addr || v->len != len) {
268 v->next_gpa = addr;
269 v->len = addr ? len : 0;
270 v->update_pending = 1;
271 }
272 spin_unlock(&vcpu->arch.vpa_update_lock);
273 return 0;
274}
275
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000276/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
277struct reg_vpa {
278 u32 dummy;
279 union {
280 u16 hword;
281 u32 word;
282 } length;
283};
284
285static int vpa_is_registered(struct kvmppc_vpa *vpap)
286{
287 if (vpap->update_pending)
288 return vpap->next_gpa != 0;
289 return vpap->pinned_addr != NULL;
290}
291
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000292static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
293 unsigned long flags,
294 unsigned long vcpuid, unsigned long vpa)
295{
296 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000297 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000298 void *va;
299 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000300 int err;
301 int subfunc;
302 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000303
304 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
305 if (!tvcpu)
306 return H_PARAMETER;
307
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000308 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
309 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
310 subfunc == H_VPA_REG_SLB) {
311 /* Registering new area - address must be cache-line aligned */
312 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000313 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000314
315 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000316 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
317 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000318 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000319 if (subfunc == H_VPA_REG_VPA)
320 len = ((struct reg_vpa *)va)->length.hword;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000321 else
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000322 len = ((struct reg_vpa *)va)->length.word;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000323 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000324
325 /* Check length */
326 if (len > nb || len < sizeof(struct reg_vpa))
327 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000328 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000329 vpa = 0;
330 len = 0;
331 }
332
333 err = H_PARAMETER;
334 vpap = NULL;
335 spin_lock(&tvcpu->arch.vpa_update_lock);
336
337 switch (subfunc) {
338 case H_VPA_REG_VPA: /* register VPA */
339 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000340 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000341 vpap = &tvcpu->arch.vpa;
342 err = 0;
343 break;
344
345 case H_VPA_REG_DTL: /* register DTL */
346 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000347 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000348 len -= len % sizeof(struct dtl_entry);
349
350 /* Check that they have previously registered a VPA */
351 err = H_RESOURCE;
352 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000353 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000354
355 vpap = &tvcpu->arch.dtl;
356 err = 0;
357 break;
358
359 case H_VPA_REG_SLB: /* register SLB shadow buffer */
360 /* Check that they have previously registered a VPA */
361 err = H_RESOURCE;
362 if (!vpa_is_registered(&tvcpu->arch.vpa))
363 break;
364
365 vpap = &tvcpu->arch.slb_shadow;
366 err = 0;
367 break;
368
369 case H_VPA_DEREG_VPA: /* deregister VPA */
370 /* Check they don't still have a DTL or SLB buf registered */
371 err = H_RESOURCE;
372 if (vpa_is_registered(&tvcpu->arch.dtl) ||
373 vpa_is_registered(&tvcpu->arch.slb_shadow))
374 break;
375
376 vpap = &tvcpu->arch.vpa;
377 err = 0;
378 break;
379
380 case H_VPA_DEREG_DTL: /* deregister DTL */
381 vpap = &tvcpu->arch.dtl;
382 err = 0;
383 break;
384
385 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
386 vpap = &tvcpu->arch.slb_shadow;
387 err = 0;
388 break;
389 }
390
391 if (vpap) {
392 vpap->next_gpa = vpa;
393 vpap->len = len;
394 vpap->update_pending = 1;
395 }
396
397 spin_unlock(&tvcpu->arch.vpa_update_lock);
398
399 return err;
400}
401
Paul Mackerras081f3232012-06-01 20:20:24 +1000402static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000403{
Paul Mackerras081f3232012-06-01 20:20:24 +1000404 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000405 void *va;
406 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000407 unsigned long gpa;
408
409 /*
410 * We need to pin the page pointed to by vpap->next_gpa,
411 * but we can't call kvmppc_pin_guest_page under the lock
412 * as it does get_user_pages() and down_read(). So we
413 * have to drop the lock, pin the page, then get the lock
414 * again and check that a new area didn't get registered
415 * in the meantime.
416 */
417 for (;;) {
418 gpa = vpap->next_gpa;
419 spin_unlock(&vcpu->arch.vpa_update_lock);
420 va = NULL;
421 nb = 0;
422 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000423 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000424 spin_lock(&vcpu->arch.vpa_update_lock);
425 if (gpa == vpap->next_gpa)
426 break;
427 /* sigh... unpin that one and try again */
428 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000429 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000430 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000431
432 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000433 if (va && nb < vpap->len) {
434 /*
435 * If it's now too short, it must be that userspace
436 * has changed the mappings underlying guest memory,
437 * so unregister the region.
438 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000439 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000440 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000441 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000442 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000443 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
444 vpap->dirty);
445 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000446 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000447 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000448 if (va)
449 vpap->pinned_end = va + vpap->len;
450}
Paul Mackerras93e60242011-12-12 12:28:55 +0000451
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000452static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
453{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000454 if (!(vcpu->arch.vpa.update_pending ||
455 vcpu->arch.slb_shadow.update_pending ||
456 vcpu->arch.dtl.update_pending))
457 return;
458
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000459 spin_lock(&vcpu->arch.vpa_update_lock);
460 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000461 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000462 if (vcpu->arch.vpa.pinned_addr)
463 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000464 }
465 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000466 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000467 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
468 vcpu->arch.dtl_index = 0;
469 }
470 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000471 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000472 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000473}
474
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000475/*
476 * Return the accumulated stolen time for the vcore up until `now'.
477 * The caller should hold the vcore lock.
478 */
479static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
480{
481 u64 p;
482
483 /*
484 * If we are the task running the vcore, then since we hold
485 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
486 * can't be updated, so we don't need the tbacct_lock.
487 * If the vcore is inactive, it can't become active (since we
488 * hold the vcore lock), so the vcpu load/put functions won't
489 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
490 */
491 if (vc->vcore_state != VCORE_INACTIVE &&
492 vc->runner->arch.run_task != current) {
493 spin_lock(&vc->runner->arch.tbacct_lock);
494 p = vc->stolen_tb;
495 if (vc->preempt_tb != TB_NIL)
496 p += now - vc->preempt_tb;
497 spin_unlock(&vc->runner->arch.tbacct_lock);
498 } else {
499 p = vc->stolen_tb;
500 }
501 return p;
502}
503
Paul Mackerras0456ec42012-02-03 00:56:21 +0000504static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
505 struct kvmppc_vcore *vc)
506{
507 struct dtl_entry *dt;
508 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000509 unsigned long stolen;
510 unsigned long core_stolen;
511 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000512
513 dt = vcpu->arch.dtl_ptr;
514 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000515 now = mftb();
516 core_stolen = vcore_stolen_time(vc, now);
517 stolen = core_stolen - vcpu->arch.stolen_logged;
518 vcpu->arch.stolen_logged = core_stolen;
519 spin_lock(&vcpu->arch.tbacct_lock);
520 stolen += vcpu->arch.busy_stolen;
521 vcpu->arch.busy_stolen = 0;
522 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000523 if (!dt || !vpa)
524 return;
525 memset(dt, 0, sizeof(struct dtl_entry));
526 dt->dispatch_reason = 7;
527 dt->processor_id = vc->pcpu + vcpu->arch.ptid;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000528 dt->timebase = now + vc->tb_offset;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000529 dt->enqueue_to_dispatch_time = stolen;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000530 dt->srr0 = kvmppc_get_pc(vcpu);
531 dt->srr1 = vcpu->arch.shregs.msr;
532 ++dt;
533 if (dt == vcpu->arch.dtl.pinned_end)
534 dt = vcpu->arch.dtl.pinned_addr;
535 vcpu->arch.dtl_ptr = dt;
536 /* order writing *dt vs. writing vpa->dtl_idx */
537 smp_wmb();
538 vpa->dtl_idx = ++vcpu->arch.dtl_index;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000539 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000540}
541
542int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
543{
544 unsigned long req = kvmppc_get_gpr(vcpu, 3);
545 unsigned long target, ret = H_SUCCESS;
546 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000547 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000548
549 switch (req) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000550 case H_ENTER:
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000551 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000552 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
553 kvmppc_get_gpr(vcpu, 5),
554 kvmppc_get_gpr(vcpu, 6),
555 kvmppc_get_gpr(vcpu, 7));
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000556 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000557 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000558 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000559 break;
560 case H_PROD:
561 target = kvmppc_get_gpr(vcpu, 4);
562 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
563 if (!tvcpu) {
564 ret = H_PARAMETER;
565 break;
566 }
567 tvcpu->arch.prodded = 1;
568 smp_mb();
569 if (vcpu->arch.ceded) {
570 if (waitqueue_active(&vcpu->wq)) {
571 wake_up_interruptible(&vcpu->wq);
572 vcpu->stat.halt_wakeup++;
573 }
574 }
575 break;
576 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +1000577 target = kvmppc_get_gpr(vcpu, 4);
578 if (target == -1)
579 break;
580 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
581 if (!tvcpu) {
582 ret = H_PARAMETER;
583 break;
584 }
585 kvm_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000586 break;
587 case H_REGISTER_VPA:
588 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
589 kvmppc_get_gpr(vcpu, 5),
590 kvmppc_get_gpr(vcpu, 6));
591 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000592 case H_RTAS:
593 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
594 return RESUME_HOST;
595
596 rc = kvmppc_rtas_hcall(vcpu);
597
598 if (rc == -ENOENT)
599 return RESUME_HOST;
600 else if (rc == 0)
601 break;
602
603 /* Send the error out to userspace via KVM_RUN */
604 return rc;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000605
606 case H_XIRR:
607 case H_CPPR:
608 case H_EOI:
609 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000610 case H_IPOLL:
611 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000612 if (kvmppc_xics_enabled(vcpu)) {
613 ret = kvmppc_xics_hcall(vcpu, req);
614 break;
615 } /* fallthrough */
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000616 default:
617 return RESUME_HOST;
618 }
619 kvmppc_set_gpr(vcpu, 3, ret);
620 vcpu->arch.hcall_needed = 0;
621 return RESUME_GUEST;
622}
623
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530624static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
625 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000626{
627 int r = RESUME_HOST;
628
629 vcpu->stat.sum_exits++;
630
631 run->exit_reason = KVM_EXIT_UNKNOWN;
632 run->ready_for_interrupt_injection = 1;
633 switch (vcpu->arch.trap) {
634 /* We're good on these - the host merely wanted to get our attention */
635 case BOOK3S_INTERRUPT_HV_DECREMENTER:
636 vcpu->stat.dec_exits++;
637 r = RESUME_GUEST;
638 break;
639 case BOOK3S_INTERRUPT_EXTERNAL:
640 vcpu->stat.ext_intr_exits++;
641 r = RESUME_GUEST;
642 break;
643 case BOOK3S_INTERRUPT_PERFMON:
644 r = RESUME_GUEST;
645 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000646 case BOOK3S_INTERRUPT_MACHINE_CHECK:
647 /*
648 * Deliver a machine check interrupt to the guest.
649 * We have to do this, even if the host has handled the
650 * machine check, because machine checks use SRR0/1 and
651 * the interrupt might have trashed guest state in them.
652 */
653 kvmppc_book3s_queue_irqprio(vcpu,
654 BOOK3S_INTERRUPT_MACHINE_CHECK);
655 r = RESUME_GUEST;
656 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000657 case BOOK3S_INTERRUPT_PROGRAM:
658 {
659 ulong flags;
660 /*
661 * Normally program interrupts are delivered directly
662 * to the guest by the hardware, but we can get here
663 * as a result of a hypervisor emulation interrupt
664 * (e40) getting turned into a 700 by BML RTAS.
665 */
666 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
667 kvmppc_core_queue_program(vcpu, flags);
668 r = RESUME_GUEST;
669 break;
670 }
671 case BOOK3S_INTERRUPT_SYSCALL:
672 {
673 /* hcall - punt to userspace */
674 int i;
675
Liu Ping Fan27025a62013-11-19 14:12:48 +0800676 /* hypercall with MSR_PR has already been handled in rmode,
677 * and never reaches here.
678 */
679
Paul Mackerrasde56a942011-06-29 00:21:34 +0000680 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
681 for (i = 0; i < 9; ++i)
682 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
683 run->exit_reason = KVM_EXIT_PAPR_HCALL;
684 vcpu->arch.hcall_needed = 1;
685 r = RESUME_HOST;
686 break;
687 }
688 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000689 * We get these next two if the guest accesses a page which it thinks
690 * it has mapped but which is not actually present, either because
691 * it is for an emulated I/O device or because the corresonding
692 * host page has been paged out. Any other HDSI/HISI interrupts
693 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000694 */
695 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000696 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000697 break;
698 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000699 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
700 vcpu->arch.fault_dsisr = 0;
701 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000702 break;
703 /*
704 * This occurs if the guest executes an illegal instruction.
705 * We just generate a program interrupt to the guest, since
706 * we don't emulate any guest instructions at this stage.
707 */
708 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
709 kvmppc_core_queue_program(vcpu, 0x80000);
710 r = RESUME_GUEST;
711 break;
712 default:
713 kvmppc_dump_regs(vcpu);
714 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
715 vcpu->arch.trap, kvmppc_get_pc(vcpu),
716 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +1000717 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000718 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000719 break;
720 }
721
Paul Mackerrasde56a942011-06-29 00:21:34 +0000722 return r;
723}
724
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530725static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
726 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000727{
728 int i;
729
Paul Mackerrasde56a942011-06-29 00:21:34 +0000730 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +0530731 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000732 for (i = 0; i < vcpu->arch.slb_max; i++) {
733 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
734 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
735 }
736
737 return 0;
738}
739
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530740static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
741 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000742{
743 int i, j;
744
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530745 kvmppc_set_pvr_hv(vcpu, sregs->pvr);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000746
747 j = 0;
748 for (i = 0; i < vcpu->arch.slb_nr; i++) {
749 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
750 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
751 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
752 ++j;
753 }
754 }
755 vcpu->arch.slb_max = j;
756
757 return 0;
758}
759
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000760static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr)
761{
762 struct kvmppc_vcore *vc = vcpu->arch.vcore;
763 u64 mask;
764
765 spin_lock(&vc->lock);
766 /*
767 * Userspace can only modify DPFD (default prefetch depth),
768 * ILE (interrupt little-endian) and TC (translation control).
769 */
770 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
771 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
772 spin_unlock(&vc->lock);
773}
774
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530775static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
776 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000777{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000778 int r = 0;
779 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +0000780
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000781 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000782 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000783 *val = get_reg_val(id, 0);
784 break;
785 case KVM_REG_PPC_DABR:
786 *val = get_reg_val(id, vcpu->arch.dabr);
787 break;
788 case KVM_REG_PPC_DSCR:
789 *val = get_reg_val(id, vcpu->arch.dscr);
790 break;
791 case KVM_REG_PPC_PURR:
792 *val = get_reg_val(id, vcpu->arch.purr);
793 break;
794 case KVM_REG_PPC_SPURR:
795 *val = get_reg_val(id, vcpu->arch.spurr);
796 break;
797 case KVM_REG_PPC_AMR:
798 *val = get_reg_val(id, vcpu->arch.amr);
799 break;
800 case KVM_REG_PPC_UAMOR:
801 *val = get_reg_val(id, vcpu->arch.uamor);
802 break;
803 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
804 i = id - KVM_REG_PPC_MMCR0;
805 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
806 break;
807 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
808 i = id - KVM_REG_PPC_PMC1;
809 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +0000810 break;
Paul Mackerras14941782013-09-06 13:11:18 +1000811 case KVM_REG_PPC_SIAR:
812 *val = get_reg_val(id, vcpu->arch.siar);
813 break;
814 case KVM_REG_PPC_SDAR:
815 *val = get_reg_val(id, vcpu->arch.sdar);
816 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +0000817 case KVM_REG_PPC_VPA_ADDR:
818 spin_lock(&vcpu->arch.vpa_update_lock);
819 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
820 spin_unlock(&vcpu->arch.vpa_update_lock);
821 break;
822 case KVM_REG_PPC_VPA_SLB:
823 spin_lock(&vcpu->arch.vpa_update_lock);
824 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
825 val->vpaval.length = vcpu->arch.slb_shadow.len;
826 spin_unlock(&vcpu->arch.vpa_update_lock);
827 break;
828 case KVM_REG_PPC_VPA_DTL:
829 spin_lock(&vcpu->arch.vpa_update_lock);
830 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
831 val->vpaval.length = vcpu->arch.dtl.len;
832 spin_unlock(&vcpu->arch.vpa_update_lock);
833 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000834 case KVM_REG_PPC_TB_OFFSET:
835 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
836 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000837 case KVM_REG_PPC_LPCR:
838 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
839 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +1000840 case KVM_REG_PPC_PPR:
841 *val = get_reg_val(id, vcpu->arch.ppr);
842 break;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000843 case KVM_REG_PPC_ARCH_COMPAT:
844 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
845 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000846 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000847 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000848 break;
849 }
850
851 return r;
852}
853
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530854static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
855 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000856{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000857 int r = 0;
858 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +0000859 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +0000860
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000861 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000862 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +0000863 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000864 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +0000865 r = -EINVAL;
866 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000867 case KVM_REG_PPC_DABR:
868 vcpu->arch.dabr = set_reg_val(id, *val);
869 break;
870 case KVM_REG_PPC_DSCR:
871 vcpu->arch.dscr = set_reg_val(id, *val);
872 break;
873 case KVM_REG_PPC_PURR:
874 vcpu->arch.purr = set_reg_val(id, *val);
875 break;
876 case KVM_REG_PPC_SPURR:
877 vcpu->arch.spurr = set_reg_val(id, *val);
878 break;
879 case KVM_REG_PPC_AMR:
880 vcpu->arch.amr = set_reg_val(id, *val);
881 break;
882 case KVM_REG_PPC_UAMOR:
883 vcpu->arch.uamor = set_reg_val(id, *val);
884 break;
885 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
886 i = id - KVM_REG_PPC_MMCR0;
887 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
888 break;
889 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
890 i = id - KVM_REG_PPC_PMC1;
891 vcpu->arch.pmc[i] = set_reg_val(id, *val);
892 break;
Paul Mackerras14941782013-09-06 13:11:18 +1000893 case KVM_REG_PPC_SIAR:
894 vcpu->arch.siar = set_reg_val(id, *val);
895 break;
896 case KVM_REG_PPC_SDAR:
897 vcpu->arch.sdar = set_reg_val(id, *val);
898 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +0000899 case KVM_REG_PPC_VPA_ADDR:
900 addr = set_reg_val(id, *val);
901 r = -EINVAL;
902 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
903 vcpu->arch.dtl.next_gpa))
904 break;
905 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
906 break;
907 case KVM_REG_PPC_VPA_SLB:
908 addr = val->vpaval.addr;
909 len = val->vpaval.length;
910 r = -EINVAL;
911 if (addr && !vcpu->arch.vpa.next_gpa)
912 break;
913 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
914 break;
915 case KVM_REG_PPC_VPA_DTL:
916 addr = val->vpaval.addr;
917 len = val->vpaval.length;
918 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +0000919 if (addr && (len < sizeof(struct dtl_entry) ||
920 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +0000921 break;
922 len -= len % sizeof(struct dtl_entry);
923 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
924 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000925 case KVM_REG_PPC_TB_OFFSET:
926 /* round up to multiple of 2^24 */
927 vcpu->arch.vcore->tb_offset =
928 ALIGN(set_reg_val(id, *val), 1UL << 24);
929 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000930 case KVM_REG_PPC_LPCR:
931 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val));
932 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +1000933 case KVM_REG_PPC_PPR:
934 vcpu->arch.ppr = set_reg_val(id, *val);
935 break;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000936 case KVM_REG_PPC_ARCH_COMPAT:
937 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
938 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000939 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000940 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000941 break;
942 }
943
944 return r;
945}
946
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530947static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
948 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000949{
950 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000951 int err = -EINVAL;
952 int core;
953 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000954
Paul Mackerras371fefd2011-06-29 00:23:08 +0000955 core = id / threads_per_core;
956 if (core >= KVM_MAX_VCORES)
957 goto out;
958
959 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200960 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000961 if (!vcpu)
962 goto out;
963
964 err = kvm_vcpu_init(vcpu, kvm, id);
965 if (err)
966 goto free_vcpu;
967
968 vcpu->arch.shared = &vcpu->arch.shregs;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000969 vcpu->arch.mmcr[0] = MMCR0_FC;
970 vcpu->arch.ctrl = CTRL_RUNLATCH;
971 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530972 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000973 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000974 spin_lock_init(&vcpu->arch.tbacct_lock);
975 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000976
Paul Mackerrasde56a942011-06-29 00:21:34 +0000977 kvmppc_mmu_book3s_hv_init(vcpu);
978
Paul Mackerras8455d792012-10-15 01:17:42 +0000979 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000980
981 init_waitqueue_head(&vcpu->arch.cpu_run);
982
983 mutex_lock(&kvm->lock);
984 vcore = kvm->arch.vcores[core];
985 if (!vcore) {
986 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
987 if (vcore) {
988 INIT_LIST_HEAD(&vcore->runnable_threads);
989 spin_lock_init(&vcore->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000990 init_waitqueue_head(&vcore->wq);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000991 vcore->preempt_tb = TB_NIL;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000992 vcore->lpcr = kvm->arch.lpcr;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000993 }
994 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000995 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000996 }
997 mutex_unlock(&kvm->lock);
998
999 if (!vcore)
1000 goto free_vcpu;
1001
1002 spin_lock(&vcore->lock);
1003 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001004 spin_unlock(&vcore->lock);
1005 vcpu->arch.vcore = vcore;
1006
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001007 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1008 kvmppc_sanity_check(vcpu);
1009
Paul Mackerrasde56a942011-06-29 00:21:34 +00001010 return vcpu;
1011
1012free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001013 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001014out:
1015 return ERR_PTR(err);
1016}
1017
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001018static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1019{
1020 if (vpa->pinned_addr)
1021 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1022 vpa->dirty);
1023}
1024
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301025static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001026{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001027 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001028 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1029 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1030 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001031 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001032 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001033 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001034}
1035
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301036static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1037{
1038 /* Indicate we want to get back into the guest */
1039 return 1;
1040}
1041
Paul Mackerras19ccb762011-07-23 17:42:46 +10001042static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001043{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001044 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001045
Paul Mackerras19ccb762011-07-23 17:42:46 +10001046 now = get_tb();
1047 if (now > vcpu->arch.dec_expires) {
1048 /* decrementer has already gone negative */
1049 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001050 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001051 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001052 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001053 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1054 / tb_ticks_per_sec;
1055 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1056 HRTIMER_MODE_REL);
1057 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001058}
1059
Paul Mackerras19ccb762011-07-23 17:42:46 +10001060static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001061{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001062 vcpu->arch.ceded = 0;
1063 if (vcpu->arch.timer_running) {
1064 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1065 vcpu->arch.timer_running = 0;
1066 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001067}
1068
1069extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001070
1071static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1072 struct kvm_vcpu *vcpu)
1073{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001074 u64 now;
1075
Paul Mackerras371fefd2011-06-29 00:23:08 +00001076 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1077 return;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001078 spin_lock(&vcpu->arch.tbacct_lock);
1079 now = mftb();
1080 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1081 vcpu->arch.stolen_logged;
1082 vcpu->arch.busy_preempt = now;
1083 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1084 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001085 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001086 list_del(&vcpu->arch.run_list);
1087}
1088
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001089static int kvmppc_grab_hwthread(int cpu)
1090{
1091 struct paca_struct *tpaca;
1092 long timeout = 1000;
1093
1094 tpaca = &paca[cpu];
1095
1096 /* Ensure the thread won't go into the kernel if it wakes */
1097 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001098 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001099
1100 /*
1101 * If the thread is already executing in the kernel (e.g. handling
1102 * a stray interrupt), wait for it to get back to nap mode.
1103 * The smp_mb() is to ensure that our setting of hwthread_req
1104 * is visible before we look at hwthread_state, so if this
1105 * races with the code at system_reset_pSeries and the thread
1106 * misses our setting of hwthread_req, we are sure to see its
1107 * setting of hwthread_state, and vice versa.
1108 */
1109 smp_mb();
1110 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1111 if (--timeout <= 0) {
1112 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1113 return -EBUSY;
1114 }
1115 udelay(1);
1116 }
1117 return 0;
1118}
1119
1120static void kvmppc_release_hwthread(int cpu)
1121{
1122 struct paca_struct *tpaca;
1123
1124 tpaca = &paca[cpu];
1125 tpaca->kvm_hstate.hwthread_req = 0;
1126 tpaca->kvm_hstate.kvm_vcpu = NULL;
1127}
1128
Paul Mackerras371fefd2011-06-29 00:23:08 +00001129static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1130{
1131 int cpu;
1132 struct paca_struct *tpaca;
1133 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1134
Paul Mackerras19ccb762011-07-23 17:42:46 +10001135 if (vcpu->arch.timer_running) {
1136 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1137 vcpu->arch.timer_running = 0;
1138 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001139 cpu = vc->pcpu + vcpu->arch.ptid;
1140 tpaca = &paca[cpu];
1141 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1142 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001143 tpaca->kvm_hstate.napping = 0;
1144 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001145 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +00001146#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001147 if (vcpu->arch.ptid) {
Andreas Schwab48eaef02013-12-30 15:36:56 +01001148#ifdef CONFIG_KVM_XICS
Paul Mackerras371fefd2011-06-29 00:23:08 +00001149 xics_wake_cpu(cpu);
Andreas Schwab48eaef02013-12-30 15:36:56 +01001150#endif
Paul Mackerras371fefd2011-06-29 00:23:08 +00001151 ++vc->n_woken;
1152 }
1153#endif
1154}
1155
1156static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1157{
1158 int i;
1159
1160 HMT_low();
1161 i = 0;
1162 while (vc->nap_count < vc->n_woken) {
1163 if (++i >= 1000000) {
1164 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1165 vc->nap_count, vc->n_woken);
1166 break;
1167 }
1168 cpu_relax();
1169 }
1170 HMT_medium();
1171}
1172
1173/*
1174 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001175 * this core are off-line. Then grab the threads so they can't
1176 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001177 */
1178static int on_primary_thread(void)
1179{
1180 int cpu = smp_processor_id();
1181 int thr = cpu_thread_in_core(cpu);
1182
1183 if (thr)
1184 return 0;
1185 while (++thr < threads_per_core)
1186 if (cpu_online(cpu + thr))
1187 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001188
1189 /* Grab all hw threads so they can't go into the kernel */
1190 for (thr = 1; thr < threads_per_core; ++thr) {
1191 if (kvmppc_grab_hwthread(cpu + thr)) {
1192 /* Couldn't grab one; let the others go */
1193 do {
1194 kvmppc_release_hwthread(cpu + thr);
1195 } while (--thr > 0);
1196 return 0;
1197 }
1198 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001199 return 1;
1200}
1201
1202/*
1203 * Run a set of guest threads on a physical core.
1204 * Called with vc->lock held.
1205 */
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001206static void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001207{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001208 struct kvm_vcpu *vcpu, *vcpu0, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001209 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001210 u64 now;
Paul Mackerras081f3232012-06-01 20:20:24 +10001211 int ptid, i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001212 int srcu_idx;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001213 struct kvm_vcpu *vcpus_to_update[threads_per_core];
Paul Mackerrasde56a942011-06-29 00:21:34 +00001214
Paul Mackerras371fefd2011-06-29 00:23:08 +00001215 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +10001216 need_vpa_update = 0;
1217 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001218 if (signal_pending(vcpu->arch.run_task))
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001219 return;
1220 if (vcpu->arch.vpa.update_pending ||
1221 vcpu->arch.slb_shadow.update_pending ||
1222 vcpu->arch.dtl.update_pending)
1223 vcpus_to_update[need_vpa_update++] = vcpu;
Paul Mackerras081f3232012-06-01 20:20:24 +10001224 }
1225
1226 /*
1227 * Initialize *vc, in particular vc->vcore_state, so we can
1228 * drop the vcore lock if necessary.
1229 */
1230 vc->n_woken = 0;
1231 vc->nap_count = 0;
1232 vc->entry_exit_count = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001233 vc->vcore_state = VCORE_STARTING;
Paul Mackerras081f3232012-06-01 20:20:24 +10001234 vc->in_guest = 0;
1235 vc->napping_threads = 0;
1236
1237 /*
1238 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1239 * which can't be called with any spinlocks held.
1240 */
1241 if (need_vpa_update) {
1242 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001243 for (i = 0; i < need_vpa_update; ++i)
1244 kvmppc_update_vpas(vcpus_to_update[i]);
Paul Mackerras081f3232012-06-01 20:20:24 +10001245 spin_lock(&vc->lock);
1246 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001247
1248 /*
Paul Mackerras19ccb762011-07-23 17:42:46 +10001249 * Assign physical thread IDs, first to non-ceded vcpus
1250 * and then to ceded ones.
1251 */
1252 ptid = 0;
1253 vcpu0 = NULL;
1254 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1255 if (!vcpu->arch.ceded) {
1256 if (!ptid)
1257 vcpu0 = vcpu;
1258 vcpu->arch.ptid = ptid++;
1259 }
1260 }
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001261 if (!vcpu0)
1262 goto out; /* nothing to run; should never happen */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001263 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1264 if (vcpu->arch.ceded)
1265 vcpu->arch.ptid = ptid++;
1266
Paul Mackerras7b444c62012-10-15 01:16:14 +00001267 /*
1268 * Make sure we are running on thread 0, and that
1269 * secondary threads are offline.
1270 */
1271 if (threads_per_core > 1 && !on_primary_thread()) {
1272 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1273 vcpu->arch.ret = -EBUSY;
1274 goto out;
1275 }
1276
Paul Mackerras371fefd2011-06-29 00:23:08 +00001277 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001278 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001279 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001280 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001281 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001282
Paul Mackerras2f12f032012-10-15 01:17:17 +00001283 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001284 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001285 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001286
Paul Mackerras19ccb762011-07-23 17:42:46 +10001287 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001288
1289 srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
1290
Paul Mackerras19ccb762011-07-23 17:42:46 +10001291 __kvmppc_vcore_entry(NULL, vcpu0);
1292
Paul Mackerras371fefd2011-06-29 00:23:08 +00001293 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001294 /* disable sending of IPIs on virtual external irqs */
1295 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1296 vcpu->cpu = -1;
1297 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001298 if (vc->nap_count < vc->n_woken)
1299 kvmppc_wait_for_nap(vc);
Paul Mackerras2f12f032012-10-15 01:17:17 +00001300 for (i = 0; i < threads_per_core; ++i)
1301 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001302 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001303 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001304 spin_unlock(&vc->lock);
1305
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001306 srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
1307
Paul Mackerras371fefd2011-06-29 00:23:08 +00001308 /* make sure updates to secondary vcpu structs are visible now */
1309 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001310 kvm_guest_exit();
1311
1312 preempt_enable();
1313 kvm_resched(vcpu);
1314
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001315 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001316 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001317 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1318 /* cancel pending dec exception if dec is positive */
1319 if (now < vcpu->arch.dec_expires &&
1320 kvmppc_core_pending_dec(vcpu))
1321 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001322
1323 ret = RESUME_GUEST;
1324 if (vcpu->arch.trap)
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301325 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1326 vcpu->arch.run_task);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001327
Paul Mackerras371fefd2011-06-29 00:23:08 +00001328 vcpu->arch.ret = ret;
1329 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001330
1331 if (vcpu->arch.ceded) {
1332 if (ret != RESUME_GUEST)
1333 kvmppc_end_cede(vcpu);
1334 else
1335 kvmppc_set_timer(vcpu);
1336 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001337 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001338
Paul Mackerrasde56a942011-06-29 00:21:34 +00001339 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001340 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001341 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1342 arch.run_list) {
1343 if (vcpu->arch.ret != RESUME_GUEST) {
1344 kvmppc_remove_runnable(vc, vcpu);
1345 wake_up(&vcpu->arch.cpu_run);
1346 }
1347 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001348}
1349
Paul Mackerras19ccb762011-07-23 17:42:46 +10001350/*
1351 * Wait for some other vcpu thread to execute us, and
1352 * wake us up when we need to handle something in the host.
1353 */
1354static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001355{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001356 DEFINE_WAIT(wait);
1357
Paul Mackerras19ccb762011-07-23 17:42:46 +10001358 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1359 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1360 schedule();
1361 finish_wait(&vcpu->arch.cpu_run, &wait);
1362}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001363
Paul Mackerras19ccb762011-07-23 17:42:46 +10001364/*
1365 * All the vcpus in this vcore are idle, so wait for a decrementer
1366 * or external interrupt to one of the vcpus. vc->lock is held.
1367 */
1368static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1369{
1370 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001371
1372 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1373 vc->vcore_state = VCORE_SLEEPING;
1374 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001375 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001376 finish_wait(&vc->wq, &wait);
1377 spin_lock(&vc->lock);
1378 vc->vcore_state = VCORE_INACTIVE;
1379}
1380
1381static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1382{
1383 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001384 struct kvmppc_vcore *vc;
1385 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001386
Paul Mackerras371fefd2011-06-29 00:23:08 +00001387 kvm_run->exit_reason = 0;
1388 vcpu->arch.ret = RESUME_GUEST;
1389 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001390 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001391
Paul Mackerras371fefd2011-06-29 00:23:08 +00001392 /*
1393 * Synchronize with other threads in this virtual core
1394 */
1395 vc = vcpu->arch.vcore;
1396 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001397 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001398 vcpu->arch.run_task = current;
1399 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001400 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10001401 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001402 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001403 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1404 ++vc->n_runnable;
1405
Paul Mackerras19ccb762011-07-23 17:42:46 +10001406 /*
1407 * This happens the first time this is called for a vcpu.
1408 * If the vcore is already running, we may be able to start
1409 * this thread straight away and have it join in.
1410 */
Paul Mackerras8455d792012-10-15 01:17:42 +00001411 if (!signal_pending(current)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001412 if (vc->vcore_state == VCORE_RUNNING &&
1413 VCORE_EXIT_COUNT(vc) == 0) {
1414 vcpu->arch.ptid = vc->n_runnable - 1;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001415 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001416 kvmppc_start_thread(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00001417 } else if (vc->vcore_state == VCORE_SLEEPING) {
1418 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001419 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001420
Paul Mackerras8455d792012-10-15 01:17:42 +00001421 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001422
1423 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1424 !signal_pending(current)) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001425 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001426 spin_unlock(&vc->lock);
1427 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1428 spin_lock(&vc->lock);
1429 continue;
1430 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001431 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1432 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001433 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001434 if (signal_pending(v->arch.run_task)) {
1435 kvmppc_remove_runnable(vc, v);
1436 v->stat.signal_exits++;
1437 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1438 v->arch.ret = -EINTR;
1439 wake_up(&v->arch.cpu_run);
1440 }
1441 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001442 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1443 break;
1444 vc->runner = vcpu;
1445 n_ceded = 0;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001446 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001447 if (!v->arch.pending_exceptions)
1448 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001449 else
1450 v->arch.ceded = 0;
1451 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001452 if (n_ceded == vc->n_runnable)
1453 kvmppc_vcore_blocked(vc);
1454 else
1455 kvmppc_run_core(vc);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001456 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001457 }
1458
Paul Mackerras8455d792012-10-15 01:17:42 +00001459 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1460 (vc->vcore_state == VCORE_RUNNING ||
1461 vc->vcore_state == VCORE_EXITING)) {
1462 spin_unlock(&vc->lock);
1463 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1464 spin_lock(&vc->lock);
1465 }
1466
1467 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1468 kvmppc_remove_runnable(vc, vcpu);
1469 vcpu->stat.signal_exits++;
1470 kvm_run->exit_reason = KVM_EXIT_INTR;
1471 vcpu->arch.ret = -EINTR;
1472 }
1473
1474 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1475 /* Wake up some vcpu to run the core */
1476 v = list_first_entry(&vc->runnable_threads,
1477 struct kvm_vcpu, arch.run_list);
1478 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001479 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001480
Paul Mackerras19ccb762011-07-23 17:42:46 +10001481 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001482 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001483}
1484
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301485static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001486{
1487 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001488 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001489
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001490 if (!vcpu->arch.sane) {
1491 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1492 return -EINVAL;
1493 }
1494
Scott Wood25051b52011-11-08 18:23:23 -06001495 kvmppc_core_prepare_to_enter(vcpu);
1496
Paul Mackerras19ccb762011-07-23 17:42:46 +10001497 /* No need to go into the guest when all we'll do is come back out */
1498 if (signal_pending(current)) {
1499 run->exit_reason = KVM_EXIT_INTR;
1500 return -EINTR;
1501 }
1502
Paul Mackerras32fad282012-05-04 02:32:53 +00001503 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1504 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1505 smp_mb();
1506
1507 /* On the first time here, set up HTAB and VRMA or RMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001508 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001509 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001510 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001511 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001512 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001513
1514 flush_fp_to_thread(current);
1515 flush_altivec_to_thread(current);
1516 flush_vsx_to_thread(current);
1517 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001518 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001519 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001520
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001521 do {
1522 r = kvmppc_run_vcpu(run, vcpu);
1523
1524 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1525 !(vcpu->arch.shregs.msr & MSR_PR)) {
1526 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001527 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001528 } else if (r == RESUME_PAGE_FAULT) {
1529 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1530 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1531 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1532 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001533 }
1534 } while (r == RESUME_GUEST);
Paul Mackerras32fad282012-05-04 02:32:53 +00001535
1536 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001537 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00001538 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001539 return r;
1540}
1541
David Gibson54738c02011-06-29 00:22:41 +00001542
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001543/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +00001544 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001545static inline int lpcr_rmls(unsigned long rma_size)
1546{
1547 switch (rma_size) {
1548 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001549 if (cpu_has_feature(CPU_FTR_ARCH_206))
1550 return 8; /* only supported on POWER7 */
1551 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001552 case 64ul << 20: /* 64 MB */
1553 return 3;
1554 case 128ul << 20: /* 128 MB */
1555 return 7;
1556 case 256ul << 20: /* 256 MB */
1557 return 4;
1558 case 1ul << 30: /* 1 GB */
1559 return 2;
1560 case 16ul << 30: /* 16 GB */
1561 return 1;
1562 case 256ul << 30: /* 256 GB */
1563 return 0;
1564 default:
1565 return -1;
1566 }
1567}
1568
1569static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1570{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001571 struct page *page;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301572 struct kvm_rma_info *ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001573
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301574 if (vmf->pgoff >= kvm_rma_pages)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001575 return VM_FAULT_SIGBUS;
1576
1577 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1578 get_page(page);
1579 vmf->page = page;
1580 return 0;
1581}
1582
1583static const struct vm_operations_struct kvm_rma_vm_ops = {
1584 .fault = kvm_rma_fault,
1585};
1586
1587static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1588{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001589 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001590 vma->vm_ops = &kvm_rma_vm_ops;
1591 return 0;
1592}
1593
1594static int kvm_rma_release(struct inode *inode, struct file *filp)
1595{
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301596 struct kvm_rma_info *ri = filp->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001597
1598 kvm_release_rma(ri);
1599 return 0;
1600}
1601
Al Viro75ef9de2013-04-04 19:09:41 -04001602static const struct file_operations kvm_rma_fops = {
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001603 .mmap = kvm_rma_mmap,
1604 .release = kvm_rma_release,
1605};
1606
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301607static long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
1608 struct kvm_allocate_rma *ret)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001609{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001610 long fd;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301611 struct kvm_rma_info *ri;
1612 /*
1613 * Only do this on PPC970 in HV mode
1614 */
1615 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
1616 !cpu_has_feature(CPU_FTR_ARCH_201))
1617 return -EINVAL;
1618
1619 if (!kvm_rma_pages)
1620 return -EINVAL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001621
1622 ri = kvm_alloc_rma();
1623 if (!ri)
1624 return -ENOMEM;
1625
Yann Droneaud2f84d5e2013-08-24 22:14:08 +02001626 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001627 if (fd < 0)
1628 kvm_release_rma(ri);
1629
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301630 ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001631 return fd;
1632}
1633
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001634static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1635 int linux_psize)
1636{
1637 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1638
1639 if (!def->shift)
1640 return;
1641 (*sps)->page_shift = def->shift;
1642 (*sps)->slb_enc = def->sllp;
1643 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00001644 /*
1645 * Only return base page encoding. We don't want to return
1646 * all the supporting pte_enc, because our H_ENTER doesn't
1647 * support MPSS yet. Once they do, we can start passing all
1648 * support pte_enc here
1649 */
1650 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001651 (*sps)++;
1652}
1653
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301654static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
1655 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001656{
1657 struct kvm_ppc_one_seg_page_size *sps;
1658
1659 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1660 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1661 info->flags |= KVM_PPC_1T_SEGMENTS;
1662 info->slb_size = mmu_slb_size;
1663
1664 /* We only support these sizes for now, and no muti-size segments */
1665 sps = &info->sps[0];
1666 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1667 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1668 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1669
1670 return 0;
1671}
1672
Paul Mackerras82ed3612011-12-15 02:03:22 +00001673/*
1674 * Get (and clear) the dirty memory log for a memory slot.
1675 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301676static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
1677 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00001678{
1679 struct kvm_memory_slot *memslot;
1680 int r;
1681 unsigned long n;
1682
1683 mutex_lock(&kvm->slots_lock);
1684
1685 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07001686 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00001687 goto out;
1688
1689 memslot = id_to_memslot(kvm->memslots, log->slot);
1690 r = -ENOENT;
1691 if (!memslot->dirty_bitmap)
1692 goto out;
1693
1694 n = kvm_dirty_bitmap_bytes(memslot);
1695 memset(memslot->dirty_bitmap, 0, n);
1696
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001697 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00001698 if (r)
1699 goto out;
1700
1701 r = -EFAULT;
1702 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1703 goto out;
1704
1705 r = 0;
1706out:
1707 mutex_unlock(&kvm->slots_lock);
1708 return r;
1709}
1710
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001711static void unpin_slot(struct kvm_memory_slot *memslot)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001712{
1713 unsigned long *physp;
1714 unsigned long j, npages, pfn;
1715 struct page *page;
1716
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001717 physp = memslot->arch.slot_phys;
1718 npages = memslot->npages;
1719 if (!physp)
1720 return;
1721 for (j = 0; j < npages; j++) {
1722 if (!(physp[j] & KVMPPC_GOT_PAGE))
1723 continue;
1724 pfn = physp[j] >> PAGE_SHIFT;
1725 page = pfn_to_page(pfn);
1726 SetPageDirty(page);
1727 put_page(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001728 }
1729}
1730
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301731static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
1732 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001733{
1734 if (!dont || free->arch.rmap != dont->arch.rmap) {
1735 vfree(free->arch.rmap);
1736 free->arch.rmap = NULL;
1737 }
1738 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1739 unpin_slot(free);
1740 vfree(free->arch.slot_phys);
1741 free->arch.slot_phys = NULL;
1742 }
1743}
1744
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301745static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
1746 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001747{
1748 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1749 if (!slot->arch.rmap)
1750 return -ENOMEM;
1751 slot->arch.slot_phys = NULL;
1752
1753 return 0;
1754}
1755
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301756static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
1757 struct kvm_memory_slot *memslot,
1758 struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001759{
1760 unsigned long *phys;
1761
1762 /* Allocate a slot_phys array if needed */
1763 phys = memslot->arch.slot_phys;
1764 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1765 phys = vzalloc(memslot->npages * sizeof(unsigned long));
1766 if (!phys)
1767 return -ENOMEM;
1768 memslot->arch.slot_phys = phys;
1769 }
1770
1771 return 0;
1772}
1773
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301774static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
1775 struct kvm_userspace_memory_region *mem,
1776 const struct kvm_memory_slot *old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001777{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001778 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1779 struct kvm_memory_slot *memslot;
1780
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001781 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001782 /*
1783 * If modifying a memslot, reset all the rmap dirty bits.
1784 * If this is a new memslot, we don't need to do anything
1785 * since the rmap array starts out as all zeroes,
1786 * i.e. no pages are dirty.
1787 */
1788 memslot = id_to_memslot(kvm->memslots, mem->slot);
1789 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1790 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001791}
1792
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001793/*
1794 * Update LPCR values in kvm->arch and in vcores.
1795 * Caller must hold kvm->lock.
1796 */
1797void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
1798{
1799 long int i;
1800 u32 cores_done = 0;
1801
1802 if ((kvm->arch.lpcr & mask) == lpcr)
1803 return;
1804
1805 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
1806
1807 for (i = 0; i < KVM_MAX_VCORES; ++i) {
1808 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
1809 if (!vc)
1810 continue;
1811 spin_lock(&vc->lock);
1812 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
1813 spin_unlock(&vc->lock);
1814 if (++cores_done >= kvm->arch.online_vcores)
1815 break;
1816 }
1817}
1818
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301819static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
1820{
1821 return;
1822}
1823
Paul Mackerras32fad282012-05-04 02:32:53 +00001824static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001825{
1826 int err = 0;
1827 struct kvm *kvm = vcpu->kvm;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301828 struct kvm_rma_info *ri = NULL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001829 unsigned long hva;
1830 struct kvm_memory_slot *memslot;
1831 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001832 unsigned long lpcr = 0, senc;
1833 unsigned long lpcr_mask = 0;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001834 unsigned long psize, porder;
1835 unsigned long rma_size;
1836 unsigned long rmls;
1837 unsigned long *physp;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001838 unsigned long i, npages;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001839 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001840
1841 mutex_lock(&kvm->lock);
1842 if (kvm->arch.rma_setup_done)
1843 goto out; /* another vcpu beat us to it */
1844
Paul Mackerras32fad282012-05-04 02:32:53 +00001845 /* Allocate hashed page table (if not done already) and reset it */
1846 if (!kvm->arch.hpt_virt) {
1847 err = kvmppc_alloc_hpt(kvm, NULL);
1848 if (err) {
1849 pr_err("KVM: Couldn't alloc HPT\n");
1850 goto out;
1851 }
1852 }
1853
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001854 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001855 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001856 memslot = gfn_to_memslot(kvm, 0);
1857
1858 /* We must have some memory at 0 by now */
1859 err = -EINVAL;
1860 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001861 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001862
1863 /* Look up the VMA for the start of this memory slot */
1864 hva = memslot->userspace_addr;
1865 down_read(&current->mm->mmap_sem);
1866 vma = find_vma(current->mm, hva);
1867 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
1868 goto up_out;
1869
1870 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001871 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001872
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001873 /* Is this one of our preallocated RMAs? */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001874 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
1875 hva == vma->vm_start)
1876 ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001877
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001878 up_read(&current->mm->mmap_sem);
1879
1880 if (!ri) {
1881 /* On POWER7, use VRMA; on PPC970, give up */
1882 err = -EPERM;
1883 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1884 pr_err("KVM: CPU requires an RMO\n");
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001885 goto out_srcu;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001886 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001887
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001888 /* We can handle 4k, 64k or 16M pages in the VRMA */
1889 err = -EINVAL;
1890 if (!(psize == 0x1000 || psize == 0x10000 ||
1891 psize == 0x1000000))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001892 goto out_srcu;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001893
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001894 /* Update VRMASD field in the LPCR */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001895 senc = slb_pgsize_encoding(psize);
Paul Mackerras697d3892011-12-12 12:36:37 +00001896 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1897 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001898 lpcr_mask = LPCR_VRMASD;
1899 /* the -4 is to account for senc values starting at 0x10 */
1900 lpcr = senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001901
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001902 /* Create HPTEs in the hash page table for the VRMA */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001903 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001904
1905 } else {
1906 /* Set up to use an RMO region */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301907 rma_size = kvm_rma_pages;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001908 if (rma_size > memslot->npages)
1909 rma_size = memslot->npages;
1910 rma_size <<= PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001911 rmls = lpcr_rmls(rma_size);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001912 err = -EINVAL;
Chen Gang5d226ae2013-07-22 14:32:35 +08001913 if ((long)rmls < 0) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001914 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001915 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001916 }
1917 atomic_inc(&ri->use_count);
1918 kvm->arch.rma = ri;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001919
1920 /* Update LPCR and RMOR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001921 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1922 /* PPC970; insert RMLS value (split field) in HID4 */
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001923 lpcr_mask = (1ul << HID4_RMLS0_SH) |
1924 (3ul << HID4_RMLS2_SH) | HID4_RMOR;
1925 lpcr = ((rmls >> 2) << HID4_RMLS0_SH) |
Paul Mackerras9e368f22011-06-29 00:40:08 +00001926 ((rmls & 3) << HID4_RMLS2_SH);
1927 /* RMOR is also in HID4 */
1928 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
1929 << HID4_RMOR_SH;
1930 } else {
1931 /* POWER7 */
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001932 lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS;
1933 lpcr = rmls << LPCR_RMLS_SH;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301934 kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001935 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001936 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001937 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001938
1939 /* Initialize phys addrs of pages in RMO */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301940 npages = kvm_rma_pages;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001941 porder = __ilog2(npages);
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001942 physp = memslot->arch.slot_phys;
1943 if (physp) {
1944 if (npages > memslot->npages)
1945 npages = memslot->npages;
1946 spin_lock(&kvm->arch.slot_phys_lock);
1947 for (i = 0; i < npages; ++i)
1948 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
1949 porder;
1950 spin_unlock(&kvm->arch.slot_phys_lock);
1951 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001952 }
1953
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001954 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
1955
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001956 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
1957 smp_wmb();
1958 kvm->arch.rma_setup_done = 1;
1959 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001960 out_srcu:
1961 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001962 out:
1963 mutex_unlock(&kvm->lock);
1964 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001965
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001966 up_out:
1967 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08001968 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001969}
1970
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301971static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001972{
Paul Mackerras32fad282012-05-04 02:32:53 +00001973 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001974
Paul Mackerras32fad282012-05-04 02:32:53 +00001975 /* Allocate the guest's logical partition ID */
1976
1977 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08001978 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00001979 return -ENOMEM;
1980 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001981
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001982 /*
1983 * Since we don't flush the TLB when tearing down a VM,
1984 * and this lpid might have previously been used,
1985 * make sure we flush on each core before running the new VM.
1986 */
1987 cpumask_setall(&kvm->arch.need_tlb_flush);
1988
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001989 kvm->arch.rma = NULL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001990
Paul Mackerras9e368f22011-06-29 00:40:08 +00001991 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001992
Paul Mackerras9e368f22011-06-29 00:40:08 +00001993 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1994 /* PPC970; HID4 is effectively the LPCR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001995 kvm->arch.host_lpid = 0;
1996 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
1997 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
1998 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
1999 ((lpid & 0xf) << HID4_LPID5_SH);
2000 } else {
2001 /* POWER7; init LPCR for virtual RMA mode */
2002 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2003 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2004 lpcr &= LPCR_PECE | LPCR_LPES;
2005 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
Paul Mackerras697d3892011-12-12 12:36:37 +00002006 LPCR_VPM0 | LPCR_VPM1;
2007 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2008 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerras9e368f22011-06-29 00:40:08 +00002009 }
2010 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002011
Paul Mackerras342d3db2011-12-12 12:38:05 +00002012 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002013 spin_lock_init(&kvm->arch.slot_phys_lock);
Paul Mackerras512691d2012-10-15 01:15:41 +00002014
2015 /*
2016 * Don't allow secondary CPU threads to come online
2017 * while any KVM VMs exist.
2018 */
2019 inhibit_secondary_onlining();
2020
David Gibson54738c02011-06-29 00:22:41 +00002021 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002022}
2023
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302024static void kvmppc_free_vcores(struct kvm *kvm)
2025{
2026 long int i;
2027
2028 for (i = 0; i < KVM_MAX_VCORES; ++i)
2029 kfree(kvm->arch.vcores[i]);
2030 kvm->arch.online_vcores = 0;
2031}
2032
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302033static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002034{
Paul Mackerras512691d2012-10-15 01:15:41 +00002035 uninhibit_secondary_onlining();
2036
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302037 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002038 if (kvm->arch.rma) {
2039 kvm_release_rma(kvm->arch.rma);
2040 kvm->arch.rma = NULL;
2041 }
2042
Paul Mackerrasde56a942011-06-29 00:21:34 +00002043 kvmppc_free_hpt(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002044}
2045
2046/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302047static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2048 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002049{
2050 return EMULATE_FAIL;
2051}
2052
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302053static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2054 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002055{
2056 return EMULATE_FAIL;
2057}
2058
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302059static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2060 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002061{
2062 return EMULATE_FAIL;
2063}
2064
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302065static int kvmppc_core_check_processor_compat_hv(void)
2066{
2067 if (!cpu_has_feature(CPU_FTR_HVMODE))
2068 return -EIO;
2069 return 0;
2070}
2071
2072static long kvm_arch_vm_ioctl_hv(struct file *filp,
2073 unsigned int ioctl, unsigned long arg)
2074{
2075 struct kvm *kvm __maybe_unused = filp->private_data;
2076 void __user *argp = (void __user *)arg;
2077 long r;
2078
2079 switch (ioctl) {
2080
2081 case KVM_ALLOCATE_RMA: {
2082 struct kvm_allocate_rma rma;
2083 struct kvm *kvm = filp->private_data;
2084
2085 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
2086 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
2087 r = -EFAULT;
2088 break;
2089 }
2090
2091 case KVM_PPC_ALLOCATE_HTAB: {
2092 u32 htab_order;
2093
2094 r = -EFAULT;
2095 if (get_user(htab_order, (u32 __user *)argp))
2096 break;
2097 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2098 if (r)
2099 break;
2100 r = -EFAULT;
2101 if (put_user(htab_order, (u32 __user *)argp))
2102 break;
2103 r = 0;
2104 break;
2105 }
2106
2107 case KVM_PPC_GET_HTAB_FD: {
2108 struct kvm_get_htab_fd ghf;
2109
2110 r = -EFAULT;
2111 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2112 break;
2113 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2114 break;
2115 }
2116
2117 default:
2118 r = -ENOTTY;
2119 }
2120
2121 return r;
2122}
2123
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302124static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302125 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2126 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2127 .get_one_reg = kvmppc_get_one_reg_hv,
2128 .set_one_reg = kvmppc_set_one_reg_hv,
2129 .vcpu_load = kvmppc_core_vcpu_load_hv,
2130 .vcpu_put = kvmppc_core_vcpu_put_hv,
2131 .set_msr = kvmppc_set_msr_hv,
2132 .vcpu_run = kvmppc_vcpu_run_hv,
2133 .vcpu_create = kvmppc_core_vcpu_create_hv,
2134 .vcpu_free = kvmppc_core_vcpu_free_hv,
2135 .check_requests = kvmppc_core_check_requests_hv,
2136 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2137 .flush_memslot = kvmppc_core_flush_memslot_hv,
2138 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2139 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2140 .unmap_hva = kvm_unmap_hva_hv,
2141 .unmap_hva_range = kvm_unmap_hva_range_hv,
2142 .age_hva = kvm_age_hva_hv,
2143 .test_age_hva = kvm_test_age_hva_hv,
2144 .set_spte_hva = kvm_set_spte_hva_hv,
2145 .mmu_destroy = kvmppc_mmu_destroy_hv,
2146 .free_memslot = kvmppc_core_free_memslot_hv,
2147 .create_memslot = kvmppc_core_create_memslot_hv,
2148 .init_vm = kvmppc_core_init_vm_hv,
2149 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302150 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2151 .emulate_op = kvmppc_core_emulate_op_hv,
2152 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2153 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2154 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2155 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
2156};
2157
2158static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002159{
2160 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302161 /*
2162 * FIXME!! Do we need to check on all cpus ?
2163 */
2164 r = kvmppc_core_check_processor_compat_hv();
2165 if (r < 0)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002166 return r;
2167
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302168 kvm_ops_hv.owner = THIS_MODULE;
2169 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002170
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302171 r = kvmppc_mmu_hv_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002172 return r;
2173}
2174
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302175static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002176{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302177 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002178}
2179
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302180module_init(kvmppc_book3s_init_hv);
2181module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05302182MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01002183MODULE_ALIAS_MISCDEV(KVM_MINOR);
2184MODULE_ALIAS("devname:kvm");