blob: d7b74f888ad8fcd3770a1190840e4e68bae11ed8 [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) {
Anton Blanchard75052582014-03-25 10:47:01 +110089#ifdef CONFIG_PPC_ICP_NATIVE
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;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100138 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000139
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100140 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000141 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
142 vc->preempt_tb != TB_NIL) {
Paul Mackerras0456ec42012-02-03 00:56:21 +0000143 vc->stolen_tb += mftb() - vc->preempt_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000144 vc->preempt_tb = TB_NIL;
145 }
146 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
147 vcpu->arch.busy_preempt != TB_NIL) {
148 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
149 vcpu->arch.busy_preempt = TB_NIL;
150 }
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100151 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000152}
153
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530154static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000155{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000156 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100157 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000158
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100159 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000160 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
161 vc->preempt_tb = mftb();
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000162 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
163 vcpu->arch.busy_preempt = mftb();
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100164 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000165}
166
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530167static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000168{
169 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000170 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000171}
172
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530173void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000174{
175 vcpu->arch.pvr = pvr;
176}
177
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000178int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
179{
180 unsigned long pcr = 0;
181 struct kvmppc_vcore *vc = vcpu->arch.vcore;
182
183 if (arch_compat) {
184 if (!cpu_has_feature(CPU_FTR_ARCH_206))
185 return -EINVAL; /* 970 has no compat mode support */
186
187 switch (arch_compat) {
188 case PVR_ARCH_205:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100189 /*
190 * If an arch bit is set in PCR, all the defined
191 * higher-order arch bits also have to be set.
192 */
193 pcr = PCR_ARCH_206 | PCR_ARCH_205;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000194 break;
195 case PVR_ARCH_206:
196 case PVR_ARCH_206p:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100197 pcr = PCR_ARCH_206;
198 break;
199 case PVR_ARCH_207:
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000200 break;
201 default:
202 return -EINVAL;
203 }
Paul Mackerras5557ae02014-01-08 21:25:24 +1100204
205 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
206 /* POWER7 can't emulate POWER8 */
207 if (!(pcr & PCR_ARCH_206))
208 return -EINVAL;
209 pcr &= ~PCR_ARCH_206;
210 }
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000211 }
212
213 spin_lock(&vc->lock);
214 vc->arch_compat = arch_compat;
215 vc->pcr = pcr;
216 spin_unlock(&vc->lock);
217
218 return 0;
219}
220
Paul Mackerrasde56a942011-06-29 00:21:34 +0000221void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
222{
223 int r;
224
225 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
226 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
227 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
228 for (r = 0; r < 16; ++r)
229 pr_err("r%2d = %.16lx r%d = %.16lx\n",
230 r, kvmppc_get_gpr(vcpu, r),
231 r+16, kvmppc_get_gpr(vcpu, r+16));
232 pr_err("ctr = %.16lx lr = %.16lx\n",
233 vcpu->arch.ctr, vcpu->arch.lr);
234 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
235 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
236 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
237 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
238 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
239 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
240 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
241 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
242 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
243 pr_err("fault dar = %.16lx dsisr = %.8x\n",
244 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
245 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
246 for (r = 0; r < vcpu->arch.slb_max; ++r)
247 pr_err(" ESID = %.16llx VSID = %.16llx\n",
248 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
249 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000250 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000251 vcpu->arch.last_inst);
252}
253
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000254struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
255{
256 int r;
257 struct kvm_vcpu *v, *ret = NULL;
258
259 mutex_lock(&kvm->lock);
260 kvm_for_each_vcpu(r, v, kvm) {
261 if (v->vcpu_id == id) {
262 ret = v;
263 break;
264 }
265 }
266 mutex_unlock(&kvm->lock);
267 return ret;
268}
269
270static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
271{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000272 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000273 vpa->yield_count = 1;
274}
275
Paul Mackerras55b665b2012-09-25 20:33:06 +0000276static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
277 unsigned long addr, unsigned long len)
278{
279 /* check address is cacheline aligned */
280 if (addr & (L1_CACHE_BYTES - 1))
281 return -EINVAL;
282 spin_lock(&vcpu->arch.vpa_update_lock);
283 if (v->next_gpa != addr || v->len != len) {
284 v->next_gpa = addr;
285 v->len = addr ? len : 0;
286 v->update_pending = 1;
287 }
288 spin_unlock(&vcpu->arch.vpa_update_lock);
289 return 0;
290}
291
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000292/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
293struct reg_vpa {
294 u32 dummy;
295 union {
296 u16 hword;
297 u32 word;
298 } length;
299};
300
301static int vpa_is_registered(struct kvmppc_vpa *vpap)
302{
303 if (vpap->update_pending)
304 return vpap->next_gpa != 0;
305 return vpap->pinned_addr != NULL;
306}
307
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000308static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
309 unsigned long flags,
310 unsigned long vcpuid, unsigned long vpa)
311{
312 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000313 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000314 void *va;
315 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000316 int err;
317 int subfunc;
318 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000319
320 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
321 if (!tvcpu)
322 return H_PARAMETER;
323
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000324 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
325 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
326 subfunc == H_VPA_REG_SLB) {
327 /* Registering new area - address must be cache-line aligned */
328 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000329 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000330
331 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000332 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
333 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000334 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000335 if (subfunc == H_VPA_REG_VPA)
336 len = ((struct reg_vpa *)va)->length.hword;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000337 else
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000338 len = ((struct reg_vpa *)va)->length.word;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000339 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000340
341 /* Check length */
342 if (len > nb || len < sizeof(struct reg_vpa))
343 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000344 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000345 vpa = 0;
346 len = 0;
347 }
348
349 err = H_PARAMETER;
350 vpap = NULL;
351 spin_lock(&tvcpu->arch.vpa_update_lock);
352
353 switch (subfunc) {
354 case H_VPA_REG_VPA: /* register VPA */
355 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000356 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000357 vpap = &tvcpu->arch.vpa;
358 err = 0;
359 break;
360
361 case H_VPA_REG_DTL: /* register DTL */
362 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000363 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000364 len -= len % sizeof(struct dtl_entry);
365
366 /* Check that they have previously registered a VPA */
367 err = H_RESOURCE;
368 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000369 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000370
371 vpap = &tvcpu->arch.dtl;
372 err = 0;
373 break;
374
375 case H_VPA_REG_SLB: /* register SLB shadow buffer */
376 /* Check that they have previously registered a VPA */
377 err = H_RESOURCE;
378 if (!vpa_is_registered(&tvcpu->arch.vpa))
379 break;
380
381 vpap = &tvcpu->arch.slb_shadow;
382 err = 0;
383 break;
384
385 case H_VPA_DEREG_VPA: /* deregister VPA */
386 /* Check they don't still have a DTL or SLB buf registered */
387 err = H_RESOURCE;
388 if (vpa_is_registered(&tvcpu->arch.dtl) ||
389 vpa_is_registered(&tvcpu->arch.slb_shadow))
390 break;
391
392 vpap = &tvcpu->arch.vpa;
393 err = 0;
394 break;
395
396 case H_VPA_DEREG_DTL: /* deregister DTL */
397 vpap = &tvcpu->arch.dtl;
398 err = 0;
399 break;
400
401 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
402 vpap = &tvcpu->arch.slb_shadow;
403 err = 0;
404 break;
405 }
406
407 if (vpap) {
408 vpap->next_gpa = vpa;
409 vpap->len = len;
410 vpap->update_pending = 1;
411 }
412
413 spin_unlock(&tvcpu->arch.vpa_update_lock);
414
415 return err;
416}
417
Paul Mackerras081f3232012-06-01 20:20:24 +1000418static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000419{
Paul Mackerras081f3232012-06-01 20:20:24 +1000420 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000421 void *va;
422 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000423 unsigned long gpa;
424
425 /*
426 * We need to pin the page pointed to by vpap->next_gpa,
427 * but we can't call kvmppc_pin_guest_page under the lock
428 * as it does get_user_pages() and down_read(). So we
429 * have to drop the lock, pin the page, then get the lock
430 * again and check that a new area didn't get registered
431 * in the meantime.
432 */
433 for (;;) {
434 gpa = vpap->next_gpa;
435 spin_unlock(&vcpu->arch.vpa_update_lock);
436 va = NULL;
437 nb = 0;
438 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000439 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000440 spin_lock(&vcpu->arch.vpa_update_lock);
441 if (gpa == vpap->next_gpa)
442 break;
443 /* sigh... unpin that one and try again */
444 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000445 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000446 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000447
448 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000449 if (va && nb < vpap->len) {
450 /*
451 * If it's now too short, it must be that userspace
452 * has changed the mappings underlying guest memory,
453 * so unregister the region.
454 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000455 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000456 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000457 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000458 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000459 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
460 vpap->dirty);
461 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000462 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000463 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000464 if (va)
465 vpap->pinned_end = va + vpap->len;
466}
Paul Mackerras93e60242011-12-12 12:28:55 +0000467
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000468static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
469{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000470 if (!(vcpu->arch.vpa.update_pending ||
471 vcpu->arch.slb_shadow.update_pending ||
472 vcpu->arch.dtl.update_pending))
473 return;
474
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000475 spin_lock(&vcpu->arch.vpa_update_lock);
476 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000477 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000478 if (vcpu->arch.vpa.pinned_addr)
479 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000480 }
481 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000482 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000483 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
484 vcpu->arch.dtl_index = 0;
485 }
486 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000487 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000488 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000489}
490
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000491/*
492 * Return the accumulated stolen time for the vcore up until `now'.
493 * The caller should hold the vcore lock.
494 */
495static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
496{
497 u64 p;
498
499 /*
500 * If we are the task running the vcore, then since we hold
501 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
502 * can't be updated, so we don't need the tbacct_lock.
503 * If the vcore is inactive, it can't become active (since we
504 * hold the vcore lock), so the vcpu load/put functions won't
505 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
506 */
507 if (vc->vcore_state != VCORE_INACTIVE &&
508 vc->runner->arch.run_task != current) {
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100509 spin_lock_irq(&vc->runner->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000510 p = vc->stolen_tb;
511 if (vc->preempt_tb != TB_NIL)
512 p += now - vc->preempt_tb;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100513 spin_unlock_irq(&vc->runner->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000514 } else {
515 p = vc->stolen_tb;
516 }
517 return p;
518}
519
Paul Mackerras0456ec42012-02-03 00:56:21 +0000520static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
521 struct kvmppc_vcore *vc)
522{
523 struct dtl_entry *dt;
524 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000525 unsigned long stolen;
526 unsigned long core_stolen;
527 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000528
529 dt = vcpu->arch.dtl_ptr;
530 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000531 now = mftb();
532 core_stolen = vcore_stolen_time(vc, now);
533 stolen = core_stolen - vcpu->arch.stolen_logged;
534 vcpu->arch.stolen_logged = core_stolen;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100535 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000536 stolen += vcpu->arch.busy_stolen;
537 vcpu->arch.busy_stolen = 0;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100538 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000539 if (!dt || !vpa)
540 return;
541 memset(dt, 0, sizeof(struct dtl_entry));
542 dt->dispatch_reason = 7;
543 dt->processor_id = vc->pcpu + vcpu->arch.ptid;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000544 dt->timebase = now + vc->tb_offset;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000545 dt->enqueue_to_dispatch_time = stolen;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000546 dt->srr0 = kvmppc_get_pc(vcpu);
547 dt->srr1 = vcpu->arch.shregs.msr;
548 ++dt;
549 if (dt == vcpu->arch.dtl.pinned_end)
550 dt = vcpu->arch.dtl.pinned_addr;
551 vcpu->arch.dtl_ptr = dt;
552 /* order writing *dt vs. writing vpa->dtl_idx */
553 smp_wmb();
554 vpa->dtl_idx = ++vcpu->arch.dtl_index;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000555 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000556}
557
558int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
559{
560 unsigned long req = kvmppc_get_gpr(vcpu, 3);
561 unsigned long target, ret = H_SUCCESS;
562 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000563 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000564
565 switch (req) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000566 case H_ENTER:
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000567 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000568 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
569 kvmppc_get_gpr(vcpu, 5),
570 kvmppc_get_gpr(vcpu, 6),
571 kvmppc_get_gpr(vcpu, 7));
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000572 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000573 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000574 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000575 break;
576 case H_PROD:
577 target = kvmppc_get_gpr(vcpu, 4);
578 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
579 if (!tvcpu) {
580 ret = H_PARAMETER;
581 break;
582 }
583 tvcpu->arch.prodded = 1;
584 smp_mb();
585 if (vcpu->arch.ceded) {
586 if (waitqueue_active(&vcpu->wq)) {
587 wake_up_interruptible(&vcpu->wq);
588 vcpu->stat.halt_wakeup++;
589 }
590 }
591 break;
592 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +1000593 target = kvmppc_get_gpr(vcpu, 4);
594 if (target == -1)
595 break;
596 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
597 if (!tvcpu) {
598 ret = H_PARAMETER;
599 break;
600 }
601 kvm_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000602 break;
603 case H_REGISTER_VPA:
604 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
605 kvmppc_get_gpr(vcpu, 5),
606 kvmppc_get_gpr(vcpu, 6));
607 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000608 case H_RTAS:
609 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
610 return RESUME_HOST;
611
Paul Mackerrasc9438092013-11-16 17:46:05 +1100612 idx = srcu_read_lock(&vcpu->kvm->srcu);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000613 rc = kvmppc_rtas_hcall(vcpu);
Paul Mackerrasc9438092013-11-16 17:46:05 +1100614 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000615
616 if (rc == -ENOENT)
617 return RESUME_HOST;
618 else if (rc == 0)
619 break;
620
621 /* Send the error out to userspace via KVM_RUN */
622 return rc;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000623
624 case H_XIRR:
625 case H_CPPR:
626 case H_EOI:
627 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000628 case H_IPOLL:
629 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000630 if (kvmppc_xics_enabled(vcpu)) {
631 ret = kvmppc_xics_hcall(vcpu, req);
632 break;
633 } /* fallthrough */
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000634 default:
635 return RESUME_HOST;
636 }
637 kvmppc_set_gpr(vcpu, 3, ret);
638 vcpu->arch.hcall_needed = 0;
639 return RESUME_GUEST;
640}
641
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530642static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
643 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000644{
645 int r = RESUME_HOST;
646
647 vcpu->stat.sum_exits++;
648
649 run->exit_reason = KVM_EXIT_UNKNOWN;
650 run->ready_for_interrupt_injection = 1;
651 switch (vcpu->arch.trap) {
652 /* We're good on these - the host merely wanted to get our attention */
653 case BOOK3S_INTERRUPT_HV_DECREMENTER:
654 vcpu->stat.dec_exits++;
655 r = RESUME_GUEST;
656 break;
657 case BOOK3S_INTERRUPT_EXTERNAL:
Paul Mackerras5d00f662014-01-08 21:25:28 +1100658 case BOOK3S_INTERRUPT_H_DOORBELL:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000659 vcpu->stat.ext_intr_exits++;
660 r = RESUME_GUEST;
661 break;
662 case BOOK3S_INTERRUPT_PERFMON:
663 r = RESUME_GUEST;
664 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000665 case BOOK3S_INTERRUPT_MACHINE_CHECK:
666 /*
667 * Deliver a machine check interrupt to the guest.
668 * We have to do this, even if the host has handled the
669 * machine check, because machine checks use SRR0/1 and
670 * the interrupt might have trashed guest state in them.
671 */
672 kvmppc_book3s_queue_irqprio(vcpu,
673 BOOK3S_INTERRUPT_MACHINE_CHECK);
674 r = RESUME_GUEST;
675 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000676 case BOOK3S_INTERRUPT_PROGRAM:
677 {
678 ulong flags;
679 /*
680 * Normally program interrupts are delivered directly
681 * to the guest by the hardware, but we can get here
682 * as a result of a hypervisor emulation interrupt
683 * (e40) getting turned into a 700 by BML RTAS.
684 */
685 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
686 kvmppc_core_queue_program(vcpu, flags);
687 r = RESUME_GUEST;
688 break;
689 }
690 case BOOK3S_INTERRUPT_SYSCALL:
691 {
692 /* hcall - punt to userspace */
693 int i;
694
Liu Ping Fan27025a62013-11-19 14:12:48 +0800695 /* hypercall with MSR_PR has already been handled in rmode,
696 * and never reaches here.
697 */
698
Paul Mackerrasde56a942011-06-29 00:21:34 +0000699 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
700 for (i = 0; i < 9; ++i)
701 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
702 run->exit_reason = KVM_EXIT_PAPR_HCALL;
703 vcpu->arch.hcall_needed = 1;
704 r = RESUME_HOST;
705 break;
706 }
707 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000708 * We get these next two if the guest accesses a page which it thinks
709 * it has mapped but which is not actually present, either because
710 * it is for an emulated I/O device or because the corresonding
711 * host page has been paged out. Any other HDSI/HISI interrupts
712 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000713 */
714 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000715 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000716 break;
717 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000718 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
719 vcpu->arch.fault_dsisr = 0;
720 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000721 break;
722 /*
723 * This occurs if the guest executes an illegal instruction.
724 * We just generate a program interrupt to the guest, since
725 * we don't emulate any guest instructions at this stage.
726 */
727 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Michael Ellermanbd3048b2014-01-08 21:25:23 +1100728 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
729 r = RESUME_GUEST;
730 break;
731 /*
732 * This occurs if the guest (kernel or userspace), does something that
733 * is prohibited by HFSCR. We just generate a program interrupt to
734 * the guest.
735 */
736 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
737 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000738 r = RESUME_GUEST;
739 break;
740 default:
741 kvmppc_dump_regs(vcpu);
742 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
743 vcpu->arch.trap, kvmppc_get_pc(vcpu),
744 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +1000745 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000746 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000747 break;
748 }
749
Paul Mackerrasde56a942011-06-29 00:21:34 +0000750 return r;
751}
752
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530753static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
754 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000755{
756 int i;
757
Paul Mackerrasde56a942011-06-29 00:21:34 +0000758 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +0530759 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000760 for (i = 0; i < vcpu->arch.slb_max; i++) {
761 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
762 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
763 }
764
765 return 0;
766}
767
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530768static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
769 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000770{
771 int i, j;
772
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530773 kvmppc_set_pvr_hv(vcpu, sregs->pvr);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000774
775 j = 0;
776 for (i = 0; i < vcpu->arch.slb_nr; i++) {
777 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
778 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
779 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
780 ++j;
781 }
782 }
783 vcpu->arch.slb_max = j;
784
785 return 0;
786}
787
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000788static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr)
789{
790 struct kvmppc_vcore *vc = vcpu->arch.vcore;
791 u64 mask;
792
793 spin_lock(&vc->lock);
794 /*
Anton Blanchardd6829162014-01-08 21:25:30 +1100795 * If ILE (interrupt little-endian) has changed, update the
796 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
797 */
798 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
799 struct kvm *kvm = vcpu->kvm;
800 struct kvm_vcpu *vcpu;
801 int i;
802
803 mutex_lock(&kvm->lock);
804 kvm_for_each_vcpu(i, vcpu, kvm) {
805 if (vcpu->arch.vcore != vc)
806 continue;
807 if (new_lpcr & LPCR_ILE)
808 vcpu->arch.intr_msr |= MSR_LE;
809 else
810 vcpu->arch.intr_msr &= ~MSR_LE;
811 }
812 mutex_unlock(&kvm->lock);
813 }
814
815 /*
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000816 * Userspace can only modify DPFD (default prefetch depth),
817 * ILE (interrupt little-endian) and TC (translation control).
Paul Mackerrase0622bd2014-01-08 21:25:27 +1100818 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000819 */
820 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
Paul Mackerrase0622bd2014-01-08 21:25:27 +1100821 if (cpu_has_feature(CPU_FTR_ARCH_207S))
822 mask |= LPCR_AIL;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000823 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
824 spin_unlock(&vc->lock);
825}
826
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530827static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
828 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000829{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000830 int r = 0;
831 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +0000832
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000833 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000834 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000835 *val = get_reg_val(id, 0);
836 break;
837 case KVM_REG_PPC_DABR:
838 *val = get_reg_val(id, vcpu->arch.dabr);
839 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +1100840 case KVM_REG_PPC_DABRX:
841 *val = get_reg_val(id, vcpu->arch.dabrx);
842 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000843 case KVM_REG_PPC_DSCR:
844 *val = get_reg_val(id, vcpu->arch.dscr);
845 break;
846 case KVM_REG_PPC_PURR:
847 *val = get_reg_val(id, vcpu->arch.purr);
848 break;
849 case KVM_REG_PPC_SPURR:
850 *val = get_reg_val(id, vcpu->arch.spurr);
851 break;
852 case KVM_REG_PPC_AMR:
853 *val = get_reg_val(id, vcpu->arch.amr);
854 break;
855 case KVM_REG_PPC_UAMOR:
856 *val = get_reg_val(id, vcpu->arch.uamor);
857 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100858 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000859 i = id - KVM_REG_PPC_MMCR0;
860 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
861 break;
862 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
863 i = id - KVM_REG_PPC_PMC1;
864 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +0000865 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100866 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
867 i = id - KVM_REG_PPC_SPMC1;
868 *val = get_reg_val(id, vcpu->arch.spmc[i]);
869 break;
Paul Mackerras14941782013-09-06 13:11:18 +1000870 case KVM_REG_PPC_SIAR:
871 *val = get_reg_val(id, vcpu->arch.siar);
872 break;
873 case KVM_REG_PPC_SDAR:
874 *val = get_reg_val(id, vcpu->arch.sdar);
875 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100876 case KVM_REG_PPC_SIER:
877 *val = get_reg_val(id, vcpu->arch.sier);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000878 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100879 case KVM_REG_PPC_IAMR:
880 *val = get_reg_val(id, vcpu->arch.iamr);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000881 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100882 case KVM_REG_PPC_FSCR:
883 *val = get_reg_val(id, vcpu->arch.fscr);
884 break;
885 case KVM_REG_PPC_PSPB:
886 *val = get_reg_val(id, vcpu->arch.pspb);
887 break;
888 case KVM_REG_PPC_EBBHR:
889 *val = get_reg_val(id, vcpu->arch.ebbhr);
890 break;
891 case KVM_REG_PPC_EBBRR:
892 *val = get_reg_val(id, vcpu->arch.ebbrr);
893 break;
894 case KVM_REG_PPC_BESCR:
895 *val = get_reg_val(id, vcpu->arch.bescr);
896 break;
897 case KVM_REG_PPC_TAR:
898 *val = get_reg_val(id, vcpu->arch.tar);
899 break;
900 case KVM_REG_PPC_DPDES:
901 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
902 break;
903 case KVM_REG_PPC_DAWR:
904 *val = get_reg_val(id, vcpu->arch.dawr);
905 break;
906 case KVM_REG_PPC_DAWRX:
907 *val = get_reg_val(id, vcpu->arch.dawrx);
908 break;
909 case KVM_REG_PPC_CIABR:
910 *val = get_reg_val(id, vcpu->arch.ciabr);
911 break;
912 case KVM_REG_PPC_IC:
913 *val = get_reg_val(id, vcpu->arch.ic);
914 break;
915 case KVM_REG_PPC_VTB:
916 *val = get_reg_val(id, vcpu->arch.vtb);
917 break;
918 case KVM_REG_PPC_CSIGR:
919 *val = get_reg_val(id, vcpu->arch.csigr);
920 break;
921 case KVM_REG_PPC_TACR:
922 *val = get_reg_val(id, vcpu->arch.tacr);
923 break;
924 case KVM_REG_PPC_TCSCR:
925 *val = get_reg_val(id, vcpu->arch.tcscr);
926 break;
927 case KVM_REG_PPC_PID:
928 *val = get_reg_val(id, vcpu->arch.pid);
929 break;
930 case KVM_REG_PPC_ACOP:
931 *val = get_reg_val(id, vcpu->arch.acop);
932 break;
933 case KVM_REG_PPC_WORT:
934 *val = get_reg_val(id, vcpu->arch.wort);
935 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +0000936 case KVM_REG_PPC_VPA_ADDR:
937 spin_lock(&vcpu->arch.vpa_update_lock);
938 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
939 spin_unlock(&vcpu->arch.vpa_update_lock);
940 break;
941 case KVM_REG_PPC_VPA_SLB:
942 spin_lock(&vcpu->arch.vpa_update_lock);
943 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
944 val->vpaval.length = vcpu->arch.slb_shadow.len;
945 spin_unlock(&vcpu->arch.vpa_update_lock);
946 break;
947 case KVM_REG_PPC_VPA_DTL:
948 spin_lock(&vcpu->arch.vpa_update_lock);
949 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
950 val->vpaval.length = vcpu->arch.dtl.len;
951 spin_unlock(&vcpu->arch.vpa_update_lock);
952 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000953 case KVM_REG_PPC_TB_OFFSET:
954 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
955 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000956 case KVM_REG_PPC_LPCR:
957 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
958 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +1000959 case KVM_REG_PPC_PPR:
960 *val = get_reg_val(id, vcpu->arch.ppr);
961 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +1100962#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
963 case KVM_REG_PPC_TFHAR:
964 *val = get_reg_val(id, vcpu->arch.tfhar);
965 break;
966 case KVM_REG_PPC_TFIAR:
967 *val = get_reg_val(id, vcpu->arch.tfiar);
968 break;
969 case KVM_REG_PPC_TEXASR:
970 *val = get_reg_val(id, vcpu->arch.texasr);
971 break;
972 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
973 i = id - KVM_REG_PPC_TM_GPR0;
974 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
975 break;
976 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
977 {
978 int j;
979 i = id - KVM_REG_PPC_TM_VSR0;
980 if (i < 32)
981 for (j = 0; j < TS_FPRWIDTH; j++)
982 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
983 else {
984 if (cpu_has_feature(CPU_FTR_ALTIVEC))
985 val->vval = vcpu->arch.vr_tm.vr[i-32];
986 else
987 r = -ENXIO;
988 }
989 break;
990 }
991 case KVM_REG_PPC_TM_CR:
992 *val = get_reg_val(id, vcpu->arch.cr_tm);
993 break;
994 case KVM_REG_PPC_TM_LR:
995 *val = get_reg_val(id, vcpu->arch.lr_tm);
996 break;
997 case KVM_REG_PPC_TM_CTR:
998 *val = get_reg_val(id, vcpu->arch.ctr_tm);
999 break;
1000 case KVM_REG_PPC_TM_FPSCR:
1001 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1002 break;
1003 case KVM_REG_PPC_TM_AMR:
1004 *val = get_reg_val(id, vcpu->arch.amr_tm);
1005 break;
1006 case KVM_REG_PPC_TM_PPR:
1007 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1008 break;
1009 case KVM_REG_PPC_TM_VRSAVE:
1010 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1011 break;
1012 case KVM_REG_PPC_TM_VSCR:
1013 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1014 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1015 else
1016 r = -ENXIO;
1017 break;
1018 case KVM_REG_PPC_TM_DSCR:
1019 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1020 break;
1021 case KVM_REG_PPC_TM_TAR:
1022 *val = get_reg_val(id, vcpu->arch.tar_tm);
1023 break;
1024#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001025 case KVM_REG_PPC_ARCH_COMPAT:
1026 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1027 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001028 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001029 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001030 break;
1031 }
1032
1033 return r;
1034}
1035
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301036static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1037 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001038{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001039 int r = 0;
1040 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001041 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +00001042
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001043 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00001044 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +00001045 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001046 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +00001047 r = -EINVAL;
1048 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001049 case KVM_REG_PPC_DABR:
1050 vcpu->arch.dabr = set_reg_val(id, *val);
1051 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001052 case KVM_REG_PPC_DABRX:
1053 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1054 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001055 case KVM_REG_PPC_DSCR:
1056 vcpu->arch.dscr = set_reg_val(id, *val);
1057 break;
1058 case KVM_REG_PPC_PURR:
1059 vcpu->arch.purr = set_reg_val(id, *val);
1060 break;
1061 case KVM_REG_PPC_SPURR:
1062 vcpu->arch.spurr = set_reg_val(id, *val);
1063 break;
1064 case KVM_REG_PPC_AMR:
1065 vcpu->arch.amr = set_reg_val(id, *val);
1066 break;
1067 case KVM_REG_PPC_UAMOR:
1068 vcpu->arch.uamor = set_reg_val(id, *val);
1069 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001070 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001071 i = id - KVM_REG_PPC_MMCR0;
1072 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1073 break;
1074 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1075 i = id - KVM_REG_PPC_PMC1;
1076 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1077 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001078 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1079 i = id - KVM_REG_PPC_SPMC1;
1080 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1081 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001082 case KVM_REG_PPC_SIAR:
1083 vcpu->arch.siar = set_reg_val(id, *val);
1084 break;
1085 case KVM_REG_PPC_SDAR:
1086 vcpu->arch.sdar = set_reg_val(id, *val);
1087 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001088 case KVM_REG_PPC_SIER:
1089 vcpu->arch.sier = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001090 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001091 case KVM_REG_PPC_IAMR:
1092 vcpu->arch.iamr = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001093 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001094 case KVM_REG_PPC_FSCR:
1095 vcpu->arch.fscr = set_reg_val(id, *val);
1096 break;
1097 case KVM_REG_PPC_PSPB:
1098 vcpu->arch.pspb = set_reg_val(id, *val);
1099 break;
1100 case KVM_REG_PPC_EBBHR:
1101 vcpu->arch.ebbhr = set_reg_val(id, *val);
1102 break;
1103 case KVM_REG_PPC_EBBRR:
1104 vcpu->arch.ebbrr = set_reg_val(id, *val);
1105 break;
1106 case KVM_REG_PPC_BESCR:
1107 vcpu->arch.bescr = set_reg_val(id, *val);
1108 break;
1109 case KVM_REG_PPC_TAR:
1110 vcpu->arch.tar = set_reg_val(id, *val);
1111 break;
1112 case KVM_REG_PPC_DPDES:
1113 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1114 break;
1115 case KVM_REG_PPC_DAWR:
1116 vcpu->arch.dawr = set_reg_val(id, *val);
1117 break;
1118 case KVM_REG_PPC_DAWRX:
1119 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1120 break;
1121 case KVM_REG_PPC_CIABR:
1122 vcpu->arch.ciabr = set_reg_val(id, *val);
1123 /* Don't allow setting breakpoints in hypervisor code */
1124 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1125 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1126 break;
1127 case KVM_REG_PPC_IC:
1128 vcpu->arch.ic = set_reg_val(id, *val);
1129 break;
1130 case KVM_REG_PPC_VTB:
1131 vcpu->arch.vtb = set_reg_val(id, *val);
1132 break;
1133 case KVM_REG_PPC_CSIGR:
1134 vcpu->arch.csigr = set_reg_val(id, *val);
1135 break;
1136 case KVM_REG_PPC_TACR:
1137 vcpu->arch.tacr = set_reg_val(id, *val);
1138 break;
1139 case KVM_REG_PPC_TCSCR:
1140 vcpu->arch.tcscr = set_reg_val(id, *val);
1141 break;
1142 case KVM_REG_PPC_PID:
1143 vcpu->arch.pid = set_reg_val(id, *val);
1144 break;
1145 case KVM_REG_PPC_ACOP:
1146 vcpu->arch.acop = set_reg_val(id, *val);
1147 break;
1148 case KVM_REG_PPC_WORT:
1149 vcpu->arch.wort = set_reg_val(id, *val);
1150 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001151 case KVM_REG_PPC_VPA_ADDR:
1152 addr = set_reg_val(id, *val);
1153 r = -EINVAL;
1154 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1155 vcpu->arch.dtl.next_gpa))
1156 break;
1157 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1158 break;
1159 case KVM_REG_PPC_VPA_SLB:
1160 addr = val->vpaval.addr;
1161 len = val->vpaval.length;
1162 r = -EINVAL;
1163 if (addr && !vcpu->arch.vpa.next_gpa)
1164 break;
1165 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1166 break;
1167 case KVM_REG_PPC_VPA_DTL:
1168 addr = val->vpaval.addr;
1169 len = val->vpaval.length;
1170 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +00001171 if (addr && (len < sizeof(struct dtl_entry) ||
1172 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +00001173 break;
1174 len -= len % sizeof(struct dtl_entry);
1175 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1176 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001177 case KVM_REG_PPC_TB_OFFSET:
1178 /* round up to multiple of 2^24 */
1179 vcpu->arch.vcore->tb_offset =
1180 ALIGN(set_reg_val(id, *val), 1UL << 24);
1181 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001182 case KVM_REG_PPC_LPCR:
1183 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val));
1184 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001185 case KVM_REG_PPC_PPR:
1186 vcpu->arch.ppr = set_reg_val(id, *val);
1187 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001188#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1189 case KVM_REG_PPC_TFHAR:
1190 vcpu->arch.tfhar = set_reg_val(id, *val);
1191 break;
1192 case KVM_REG_PPC_TFIAR:
1193 vcpu->arch.tfiar = set_reg_val(id, *val);
1194 break;
1195 case KVM_REG_PPC_TEXASR:
1196 vcpu->arch.texasr = set_reg_val(id, *val);
1197 break;
1198 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1199 i = id - KVM_REG_PPC_TM_GPR0;
1200 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1201 break;
1202 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1203 {
1204 int j;
1205 i = id - KVM_REG_PPC_TM_VSR0;
1206 if (i < 32)
1207 for (j = 0; j < TS_FPRWIDTH; j++)
1208 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1209 else
1210 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1211 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1212 else
1213 r = -ENXIO;
1214 break;
1215 }
1216 case KVM_REG_PPC_TM_CR:
1217 vcpu->arch.cr_tm = set_reg_val(id, *val);
1218 break;
1219 case KVM_REG_PPC_TM_LR:
1220 vcpu->arch.lr_tm = set_reg_val(id, *val);
1221 break;
1222 case KVM_REG_PPC_TM_CTR:
1223 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1224 break;
1225 case KVM_REG_PPC_TM_FPSCR:
1226 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1227 break;
1228 case KVM_REG_PPC_TM_AMR:
1229 vcpu->arch.amr_tm = set_reg_val(id, *val);
1230 break;
1231 case KVM_REG_PPC_TM_PPR:
1232 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1233 break;
1234 case KVM_REG_PPC_TM_VRSAVE:
1235 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1236 break;
1237 case KVM_REG_PPC_TM_VSCR:
1238 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1239 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1240 else
1241 r = - ENXIO;
1242 break;
1243 case KVM_REG_PPC_TM_DSCR:
1244 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1245 break;
1246 case KVM_REG_PPC_TM_TAR:
1247 vcpu->arch.tar_tm = set_reg_val(id, *val);
1248 break;
1249#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001250 case KVM_REG_PPC_ARCH_COMPAT:
1251 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1252 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001253 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001254 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001255 break;
1256 }
1257
1258 return r;
1259}
1260
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301261static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1262 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001263{
1264 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001265 int err = -EINVAL;
1266 int core;
1267 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001268
Paul Mackerras371fefd2011-06-29 00:23:08 +00001269 core = id / threads_per_core;
1270 if (core >= KVM_MAX_VCORES)
1271 goto out;
1272
1273 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001274 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001275 if (!vcpu)
1276 goto out;
1277
1278 err = kvm_vcpu_init(vcpu, kvm, id);
1279 if (err)
1280 goto free_vcpu;
1281
1282 vcpu->arch.shared = &vcpu->arch.shregs;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001283 vcpu->arch.mmcr[0] = MMCR0_FC;
1284 vcpu->arch.ctrl = CTRL_RUNLATCH;
1285 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301286 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001287 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001288 spin_lock_init(&vcpu->arch.tbacct_lock);
1289 vcpu->arch.busy_preempt = TB_NIL;
Anton Blanchardd6829162014-01-08 21:25:30 +11001290 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001291
Paul Mackerrasde56a942011-06-29 00:21:34 +00001292 kvmppc_mmu_book3s_hv_init(vcpu);
1293
Paul Mackerras8455d792012-10-15 01:17:42 +00001294 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001295
1296 init_waitqueue_head(&vcpu->arch.cpu_run);
1297
1298 mutex_lock(&kvm->lock);
1299 vcore = kvm->arch.vcores[core];
1300 if (!vcore) {
1301 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1302 if (vcore) {
1303 INIT_LIST_HEAD(&vcore->runnable_threads);
1304 spin_lock_init(&vcore->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001305 init_waitqueue_head(&vcore->wq);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001306 vcore->preempt_tb = TB_NIL;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001307 vcore->lpcr = kvm->arch.lpcr;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001308 vcore->first_vcpuid = core * threads_per_core;
1309 vcore->kvm = kvm;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001310 }
1311 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001312 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001313 }
1314 mutex_unlock(&kvm->lock);
1315
1316 if (!vcore)
1317 goto free_vcpu;
1318
1319 spin_lock(&vcore->lock);
1320 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001321 spin_unlock(&vcore->lock);
1322 vcpu->arch.vcore = vcore;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001323 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001324
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001325 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1326 kvmppc_sanity_check(vcpu);
1327
Paul Mackerrasde56a942011-06-29 00:21:34 +00001328 return vcpu;
1329
1330free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001331 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001332out:
1333 return ERR_PTR(err);
1334}
1335
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001336static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1337{
1338 if (vpa->pinned_addr)
1339 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1340 vpa->dirty);
1341}
1342
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301343static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001344{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001345 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001346 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1347 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1348 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001349 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001350 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001351 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001352}
1353
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301354static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1355{
1356 /* Indicate we want to get back into the guest */
1357 return 1;
1358}
1359
Paul Mackerras19ccb762011-07-23 17:42:46 +10001360static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001361{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001362 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001363
Paul Mackerras19ccb762011-07-23 17:42:46 +10001364 now = get_tb();
1365 if (now > vcpu->arch.dec_expires) {
1366 /* decrementer has already gone negative */
1367 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001368 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001369 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001370 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001371 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1372 / tb_ticks_per_sec;
1373 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1374 HRTIMER_MODE_REL);
1375 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001376}
1377
Paul Mackerras19ccb762011-07-23 17:42:46 +10001378static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001379{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001380 vcpu->arch.ceded = 0;
1381 if (vcpu->arch.timer_running) {
1382 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1383 vcpu->arch.timer_running = 0;
1384 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001385}
1386
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001387extern void __kvmppc_vcore_entry(void);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001388
1389static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1390 struct kvm_vcpu *vcpu)
1391{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001392 u64 now;
1393
Paul Mackerras371fefd2011-06-29 00:23:08 +00001394 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1395 return;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001396 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001397 now = mftb();
1398 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1399 vcpu->arch.stolen_logged;
1400 vcpu->arch.busy_preempt = now;
1401 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001402 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001403 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001404 list_del(&vcpu->arch.run_list);
1405}
1406
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001407static int kvmppc_grab_hwthread(int cpu)
1408{
1409 struct paca_struct *tpaca;
1410 long timeout = 1000;
1411
1412 tpaca = &paca[cpu];
1413
1414 /* Ensure the thread won't go into the kernel if it wakes */
1415 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001416 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001417
1418 /*
1419 * If the thread is already executing in the kernel (e.g. handling
1420 * a stray interrupt), wait for it to get back to nap mode.
1421 * The smp_mb() is to ensure that our setting of hwthread_req
1422 * is visible before we look at hwthread_state, so if this
1423 * races with the code at system_reset_pSeries and the thread
1424 * misses our setting of hwthread_req, we are sure to see its
1425 * setting of hwthread_state, and vice versa.
1426 */
1427 smp_mb();
1428 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1429 if (--timeout <= 0) {
1430 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1431 return -EBUSY;
1432 }
1433 udelay(1);
1434 }
1435 return 0;
1436}
1437
1438static void kvmppc_release_hwthread(int cpu)
1439{
1440 struct paca_struct *tpaca;
1441
1442 tpaca = &paca[cpu];
1443 tpaca->kvm_hstate.hwthread_req = 0;
1444 tpaca->kvm_hstate.kvm_vcpu = NULL;
1445}
1446
Paul Mackerras371fefd2011-06-29 00:23:08 +00001447static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1448{
1449 int cpu;
1450 struct paca_struct *tpaca;
1451 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1452
Paul Mackerras19ccb762011-07-23 17:42:46 +10001453 if (vcpu->arch.timer_running) {
1454 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1455 vcpu->arch.timer_running = 0;
1456 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001457 cpu = vc->pcpu + vcpu->arch.ptid;
1458 tpaca = &paca[cpu];
1459 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1460 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001461 tpaca->kvm_hstate.ptid = vcpu->arch.ptid;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001462 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001463 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +00001464#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001465 if (cpu != smp_processor_id()) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001466 xics_wake_cpu(cpu);
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001467 if (vcpu->arch.ptid)
1468 ++vc->n_woken;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001469 }
1470#endif
1471}
1472
1473static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1474{
1475 int i;
1476
1477 HMT_low();
1478 i = 0;
1479 while (vc->nap_count < vc->n_woken) {
1480 if (++i >= 1000000) {
1481 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1482 vc->nap_count, vc->n_woken);
1483 break;
1484 }
1485 cpu_relax();
1486 }
1487 HMT_medium();
1488}
1489
1490/*
1491 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001492 * this core are off-line. Then grab the threads so they can't
1493 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001494 */
1495static int on_primary_thread(void)
1496{
1497 int cpu = smp_processor_id();
1498 int thr = cpu_thread_in_core(cpu);
1499
1500 if (thr)
1501 return 0;
1502 while (++thr < threads_per_core)
1503 if (cpu_online(cpu + thr))
1504 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001505
1506 /* Grab all hw threads so they can't go into the kernel */
1507 for (thr = 1; thr < threads_per_core; ++thr) {
1508 if (kvmppc_grab_hwthread(cpu + thr)) {
1509 /* Couldn't grab one; let the others go */
1510 do {
1511 kvmppc_release_hwthread(cpu + thr);
1512 } while (--thr > 0);
1513 return 0;
1514 }
1515 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001516 return 1;
1517}
1518
1519/*
1520 * Run a set of guest threads on a physical core.
1521 * Called with vc->lock held.
1522 */
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001523static void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001524{
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001525 struct kvm_vcpu *vcpu, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001526 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001527 u64 now;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001528 int i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001529 int srcu_idx;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001530 struct kvm_vcpu *vcpus_to_update[threads_per_core];
Paul Mackerrasde56a942011-06-29 00:21:34 +00001531
Paul Mackerras371fefd2011-06-29 00:23:08 +00001532 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +10001533 need_vpa_update = 0;
1534 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001535 if (signal_pending(vcpu->arch.run_task))
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001536 return;
1537 if (vcpu->arch.vpa.update_pending ||
1538 vcpu->arch.slb_shadow.update_pending ||
1539 vcpu->arch.dtl.update_pending)
1540 vcpus_to_update[need_vpa_update++] = vcpu;
Paul Mackerras081f3232012-06-01 20:20:24 +10001541 }
1542
1543 /*
1544 * Initialize *vc, in particular vc->vcore_state, so we can
1545 * drop the vcore lock if necessary.
1546 */
1547 vc->n_woken = 0;
1548 vc->nap_count = 0;
1549 vc->entry_exit_count = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001550 vc->vcore_state = VCORE_STARTING;
Paul Mackerras081f3232012-06-01 20:20:24 +10001551 vc->in_guest = 0;
1552 vc->napping_threads = 0;
1553
1554 /*
1555 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1556 * which can't be called with any spinlocks held.
1557 */
1558 if (need_vpa_update) {
1559 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001560 for (i = 0; i < need_vpa_update; ++i)
1561 kvmppc_update_vpas(vcpus_to_update[i]);
Paul Mackerras081f3232012-06-01 20:20:24 +10001562 spin_lock(&vc->lock);
1563 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001564
1565 /*
Paul Mackerras7b444c62012-10-15 01:16:14 +00001566 * Make sure we are running on thread 0, and that
1567 * secondary threads are offline.
1568 */
1569 if (threads_per_core > 1 && !on_primary_thread()) {
1570 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1571 vcpu->arch.ret = -EBUSY;
1572 goto out;
1573 }
1574
Paul Mackerras371fefd2011-06-29 00:23:08 +00001575 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001576 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001577 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001578 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001579 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001580
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001581 /* Set this explicitly in case thread 0 doesn't have a vcpu */
1582 get_paca()->kvm_hstate.kvm_vcore = vc;
1583 get_paca()->kvm_hstate.ptid = 0;
1584
Paul Mackerras2f12f032012-10-15 01:17:17 +00001585 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001586 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001587 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001588
Paul Mackerras19ccb762011-07-23 17:42:46 +10001589 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001590
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001591 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001592
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001593 __kvmppc_vcore_entry();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001594
Paul Mackerras371fefd2011-06-29 00:23:08 +00001595 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001596 /* disable sending of IPIs on virtual external irqs */
1597 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1598 vcpu->cpu = -1;
1599 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001600 if (vc->nap_count < vc->n_woken)
1601 kvmppc_wait_for_nap(vc);
Paul Mackerras2f12f032012-10-15 01:17:17 +00001602 for (i = 0; i < threads_per_core; ++i)
1603 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001604 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001605 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001606 spin_unlock(&vc->lock);
1607
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001608 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001609
Paul Mackerras371fefd2011-06-29 00:23:08 +00001610 /* make sure updates to secondary vcpu structs are visible now */
1611 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001612 kvm_guest_exit();
1613
1614 preempt_enable();
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09001615 cond_resched();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001616
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001617 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001618 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001619 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1620 /* cancel pending dec exception if dec is positive */
1621 if (now < vcpu->arch.dec_expires &&
1622 kvmppc_core_pending_dec(vcpu))
1623 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001624
1625 ret = RESUME_GUEST;
1626 if (vcpu->arch.trap)
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301627 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1628 vcpu->arch.run_task);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001629
Paul Mackerras371fefd2011-06-29 00:23:08 +00001630 vcpu->arch.ret = ret;
1631 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001632
1633 if (vcpu->arch.ceded) {
Greg Kurze59d24e2014-02-06 17:36:56 +01001634 if (!is_kvmppc_resume_guest(ret))
Paul Mackerras19ccb762011-07-23 17:42:46 +10001635 kvmppc_end_cede(vcpu);
1636 else
1637 kvmppc_set_timer(vcpu);
1638 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001639 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001640
Paul Mackerrasde56a942011-06-29 00:21:34 +00001641 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001642 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001643 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1644 arch.run_list) {
Greg Kurze59d24e2014-02-06 17:36:56 +01001645 if (!is_kvmppc_resume_guest(vcpu->arch.ret)) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001646 kvmppc_remove_runnable(vc, vcpu);
1647 wake_up(&vcpu->arch.cpu_run);
1648 }
1649 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001650}
1651
Paul Mackerras19ccb762011-07-23 17:42:46 +10001652/*
1653 * Wait for some other vcpu thread to execute us, and
1654 * wake us up when we need to handle something in the host.
1655 */
1656static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001657{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001658 DEFINE_WAIT(wait);
1659
Paul Mackerras19ccb762011-07-23 17:42:46 +10001660 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1661 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1662 schedule();
1663 finish_wait(&vcpu->arch.cpu_run, &wait);
1664}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001665
Paul Mackerras19ccb762011-07-23 17:42:46 +10001666/*
1667 * All the vcpus in this vcore are idle, so wait for a decrementer
1668 * or external interrupt to one of the vcpus. vc->lock is held.
1669 */
1670static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1671{
1672 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001673
1674 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1675 vc->vcore_state = VCORE_SLEEPING;
1676 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001677 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001678 finish_wait(&vc->wq, &wait);
1679 spin_lock(&vc->lock);
1680 vc->vcore_state = VCORE_INACTIVE;
1681}
1682
1683static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1684{
1685 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001686 struct kvmppc_vcore *vc;
1687 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001688
Paul Mackerras371fefd2011-06-29 00:23:08 +00001689 kvm_run->exit_reason = 0;
1690 vcpu->arch.ret = RESUME_GUEST;
1691 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001692 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001693
Paul Mackerras371fefd2011-06-29 00:23:08 +00001694 /*
1695 * Synchronize with other threads in this virtual core
1696 */
1697 vc = vcpu->arch.vcore;
1698 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001699 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001700 vcpu->arch.run_task = current;
1701 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001702 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10001703 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001704 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001705 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1706 ++vc->n_runnable;
1707
Paul Mackerras19ccb762011-07-23 17:42:46 +10001708 /*
1709 * This happens the first time this is called for a vcpu.
1710 * If the vcore is already running, we may be able to start
1711 * this thread straight away and have it join in.
1712 */
Paul Mackerras8455d792012-10-15 01:17:42 +00001713 if (!signal_pending(current)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001714 if (vc->vcore_state == VCORE_RUNNING &&
1715 VCORE_EXIT_COUNT(vc) == 0) {
Paul Mackerras2f12f032012-10-15 01:17:17 +00001716 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001717 kvmppc_start_thread(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00001718 } else if (vc->vcore_state == VCORE_SLEEPING) {
1719 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001720 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001721
Paul Mackerras8455d792012-10-15 01:17:42 +00001722 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001723
1724 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1725 !signal_pending(current)) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001726 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001727 spin_unlock(&vc->lock);
1728 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1729 spin_lock(&vc->lock);
1730 continue;
1731 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001732 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1733 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001734 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001735 if (signal_pending(v->arch.run_task)) {
1736 kvmppc_remove_runnable(vc, v);
1737 v->stat.signal_exits++;
1738 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1739 v->arch.ret = -EINTR;
1740 wake_up(&v->arch.cpu_run);
1741 }
1742 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001743 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1744 break;
1745 vc->runner = vcpu;
1746 n_ceded = 0;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001747 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001748 if (!v->arch.pending_exceptions)
1749 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001750 else
1751 v->arch.ceded = 0;
1752 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001753 if (n_ceded == vc->n_runnable)
1754 kvmppc_vcore_blocked(vc);
1755 else
1756 kvmppc_run_core(vc);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001757 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001758 }
1759
Paul Mackerras8455d792012-10-15 01:17:42 +00001760 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1761 (vc->vcore_state == VCORE_RUNNING ||
1762 vc->vcore_state == VCORE_EXITING)) {
1763 spin_unlock(&vc->lock);
1764 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1765 spin_lock(&vc->lock);
1766 }
1767
1768 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1769 kvmppc_remove_runnable(vc, vcpu);
1770 vcpu->stat.signal_exits++;
1771 kvm_run->exit_reason = KVM_EXIT_INTR;
1772 vcpu->arch.ret = -EINTR;
1773 }
1774
1775 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1776 /* Wake up some vcpu to run the core */
1777 v = list_first_entry(&vc->runnable_threads,
1778 struct kvm_vcpu, arch.run_list);
1779 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001780 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001781
Paul Mackerras19ccb762011-07-23 17:42:46 +10001782 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001783 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001784}
1785
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301786static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001787{
1788 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001789 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001790
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001791 if (!vcpu->arch.sane) {
1792 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1793 return -EINVAL;
1794 }
1795
Scott Wood25051b5a2011-11-08 18:23:23 -06001796 kvmppc_core_prepare_to_enter(vcpu);
1797
Paul Mackerras19ccb762011-07-23 17:42:46 +10001798 /* No need to go into the guest when all we'll do is come back out */
1799 if (signal_pending(current)) {
1800 run->exit_reason = KVM_EXIT_INTR;
1801 return -EINTR;
1802 }
1803
Paul Mackerras32fad282012-05-04 02:32:53 +00001804 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1805 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1806 smp_mb();
1807
1808 /* On the first time here, set up HTAB and VRMA or RMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001809 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001810 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001811 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001812 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001813 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001814
1815 flush_fp_to_thread(current);
1816 flush_altivec_to_thread(current);
1817 flush_vsx_to_thread(current);
1818 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001819 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001820 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001821
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001822 do {
1823 r = kvmppc_run_vcpu(run, vcpu);
1824
1825 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1826 !(vcpu->arch.shregs.msr & MSR_PR)) {
1827 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001828 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001829 } else if (r == RESUME_PAGE_FAULT) {
1830 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1831 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1832 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1833 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001834 }
Greg Kurze59d24e2014-02-06 17:36:56 +01001835 } while (is_kvmppc_resume_guest(r));
Paul Mackerras32fad282012-05-04 02:32:53 +00001836
1837 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001838 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00001839 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001840 return r;
1841}
1842
David Gibson54738c02011-06-29 00:22:41 +00001843
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001844/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +00001845 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001846static inline int lpcr_rmls(unsigned long rma_size)
1847{
1848 switch (rma_size) {
1849 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001850 if (cpu_has_feature(CPU_FTR_ARCH_206))
1851 return 8; /* only supported on POWER7 */
1852 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001853 case 64ul << 20: /* 64 MB */
1854 return 3;
1855 case 128ul << 20: /* 128 MB */
1856 return 7;
1857 case 256ul << 20: /* 256 MB */
1858 return 4;
1859 case 1ul << 30: /* 1 GB */
1860 return 2;
1861 case 16ul << 30: /* 16 GB */
1862 return 1;
1863 case 256ul << 30: /* 256 GB */
1864 return 0;
1865 default:
1866 return -1;
1867 }
1868}
1869
1870static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1871{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001872 struct page *page;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301873 struct kvm_rma_info *ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001874
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301875 if (vmf->pgoff >= kvm_rma_pages)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001876 return VM_FAULT_SIGBUS;
1877
1878 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1879 get_page(page);
1880 vmf->page = page;
1881 return 0;
1882}
1883
1884static const struct vm_operations_struct kvm_rma_vm_ops = {
1885 .fault = kvm_rma_fault,
1886};
1887
1888static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1889{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001890 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001891 vma->vm_ops = &kvm_rma_vm_ops;
1892 return 0;
1893}
1894
1895static int kvm_rma_release(struct inode *inode, struct file *filp)
1896{
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301897 struct kvm_rma_info *ri = filp->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001898
1899 kvm_release_rma(ri);
1900 return 0;
1901}
1902
Al Viro75ef9de2013-04-04 19:09:41 -04001903static const struct file_operations kvm_rma_fops = {
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001904 .mmap = kvm_rma_mmap,
1905 .release = kvm_rma_release,
1906};
1907
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301908static long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
1909 struct kvm_allocate_rma *ret)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001910{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001911 long fd;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301912 struct kvm_rma_info *ri;
1913 /*
1914 * Only do this on PPC970 in HV mode
1915 */
1916 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
1917 !cpu_has_feature(CPU_FTR_ARCH_201))
1918 return -EINVAL;
1919
1920 if (!kvm_rma_pages)
1921 return -EINVAL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001922
1923 ri = kvm_alloc_rma();
1924 if (!ri)
1925 return -ENOMEM;
1926
Yann Droneaud2f84d5e2013-08-24 22:14:08 +02001927 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001928 if (fd < 0)
1929 kvm_release_rma(ri);
1930
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301931 ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001932 return fd;
1933}
1934
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001935static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1936 int linux_psize)
1937{
1938 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1939
1940 if (!def->shift)
1941 return;
1942 (*sps)->page_shift = def->shift;
1943 (*sps)->slb_enc = def->sllp;
1944 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00001945 /*
1946 * Only return base page encoding. We don't want to return
1947 * all the supporting pte_enc, because our H_ENTER doesn't
1948 * support MPSS yet. Once they do, we can start passing all
1949 * support pte_enc here
1950 */
1951 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001952 (*sps)++;
1953}
1954
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301955static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
1956 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001957{
1958 struct kvm_ppc_one_seg_page_size *sps;
1959
1960 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1961 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1962 info->flags |= KVM_PPC_1T_SEGMENTS;
1963 info->slb_size = mmu_slb_size;
1964
1965 /* We only support these sizes for now, and no muti-size segments */
1966 sps = &info->sps[0];
1967 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1968 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1969 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1970
1971 return 0;
1972}
1973
Paul Mackerras82ed3612011-12-15 02:03:22 +00001974/*
1975 * Get (and clear) the dirty memory log for a memory slot.
1976 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301977static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
1978 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00001979{
1980 struct kvm_memory_slot *memslot;
1981 int r;
1982 unsigned long n;
1983
1984 mutex_lock(&kvm->slots_lock);
1985
1986 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07001987 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00001988 goto out;
1989
1990 memslot = id_to_memslot(kvm->memslots, log->slot);
1991 r = -ENOENT;
1992 if (!memslot->dirty_bitmap)
1993 goto out;
1994
1995 n = kvm_dirty_bitmap_bytes(memslot);
1996 memset(memslot->dirty_bitmap, 0, n);
1997
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001998 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00001999 if (r)
2000 goto out;
2001
2002 r = -EFAULT;
2003 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2004 goto out;
2005
2006 r = 0;
2007out:
2008 mutex_unlock(&kvm->slots_lock);
2009 return r;
2010}
2011
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002012static void unpin_slot(struct kvm_memory_slot *memslot)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002013{
2014 unsigned long *physp;
2015 unsigned long j, npages, pfn;
2016 struct page *page;
2017
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002018 physp = memslot->arch.slot_phys;
2019 npages = memslot->npages;
2020 if (!physp)
2021 return;
2022 for (j = 0; j < npages; j++) {
2023 if (!(physp[j] & KVMPPC_GOT_PAGE))
2024 continue;
2025 pfn = physp[j] >> PAGE_SHIFT;
2026 page = pfn_to_page(pfn);
2027 SetPageDirty(page);
2028 put_page(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002029 }
2030}
2031
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302032static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2033 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002034{
2035 if (!dont || free->arch.rmap != dont->arch.rmap) {
2036 vfree(free->arch.rmap);
2037 free->arch.rmap = NULL;
2038 }
2039 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
2040 unpin_slot(free);
2041 vfree(free->arch.slot_phys);
2042 free->arch.slot_phys = NULL;
2043 }
2044}
2045
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302046static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
2047 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002048{
2049 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
2050 if (!slot->arch.rmap)
2051 return -ENOMEM;
2052 slot->arch.slot_phys = NULL;
2053
2054 return 0;
2055}
2056
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302057static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
2058 struct kvm_memory_slot *memslot,
2059 struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002060{
2061 unsigned long *phys;
2062
2063 /* Allocate a slot_phys array if needed */
2064 phys = memslot->arch.slot_phys;
2065 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
2066 phys = vzalloc(memslot->npages * sizeof(unsigned long));
2067 if (!phys)
2068 return -ENOMEM;
2069 memslot->arch.slot_phys = phys;
2070 }
2071
2072 return 0;
2073}
2074
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302075static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
2076 struct kvm_userspace_memory_region *mem,
2077 const struct kvm_memory_slot *old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002078{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002079 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
2080 struct kvm_memory_slot *memslot;
2081
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002082 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002083 /*
2084 * If modifying a memslot, reset all the rmap dirty bits.
2085 * If this is a new memslot, we don't need to do anything
2086 * since the rmap array starts out as all zeroes,
2087 * i.e. no pages are dirty.
2088 */
2089 memslot = id_to_memslot(kvm->memslots, mem->slot);
2090 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
2091 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002092}
2093
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002094/*
2095 * Update LPCR values in kvm->arch and in vcores.
2096 * Caller must hold kvm->lock.
2097 */
2098void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
2099{
2100 long int i;
2101 u32 cores_done = 0;
2102
2103 if ((kvm->arch.lpcr & mask) == lpcr)
2104 return;
2105
2106 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2107
2108 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2109 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2110 if (!vc)
2111 continue;
2112 spin_lock(&vc->lock);
2113 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2114 spin_unlock(&vc->lock);
2115 if (++cores_done >= kvm->arch.online_vcores)
2116 break;
2117 }
2118}
2119
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302120static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2121{
2122 return;
2123}
2124
Paul Mackerras32fad282012-05-04 02:32:53 +00002125static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002126{
2127 int err = 0;
2128 struct kvm *kvm = vcpu->kvm;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302129 struct kvm_rma_info *ri = NULL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002130 unsigned long hva;
2131 struct kvm_memory_slot *memslot;
2132 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002133 unsigned long lpcr = 0, senc;
2134 unsigned long lpcr_mask = 0;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002135 unsigned long psize, porder;
2136 unsigned long rma_size;
2137 unsigned long rmls;
2138 unsigned long *physp;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002139 unsigned long i, npages;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002140 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002141
2142 mutex_lock(&kvm->lock);
2143 if (kvm->arch.rma_setup_done)
2144 goto out; /* another vcpu beat us to it */
2145
Paul Mackerras32fad282012-05-04 02:32:53 +00002146 /* Allocate hashed page table (if not done already) and reset it */
2147 if (!kvm->arch.hpt_virt) {
2148 err = kvmppc_alloc_hpt(kvm, NULL);
2149 if (err) {
2150 pr_err("KVM: Couldn't alloc HPT\n");
2151 goto out;
2152 }
2153 }
2154
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002155 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002156 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002157 memslot = gfn_to_memslot(kvm, 0);
2158
2159 /* We must have some memory at 0 by now */
2160 err = -EINVAL;
2161 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002162 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002163
2164 /* Look up the VMA for the start of this memory slot */
2165 hva = memslot->userspace_addr;
2166 down_read(&current->mm->mmap_sem);
2167 vma = find_vma(current->mm, hva);
2168 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2169 goto up_out;
2170
2171 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002172 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002173
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002174 /* Is this one of our preallocated RMAs? */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002175 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
2176 hva == vma->vm_start)
2177 ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002178
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002179 up_read(&current->mm->mmap_sem);
2180
2181 if (!ri) {
2182 /* On POWER7, use VRMA; on PPC970, give up */
2183 err = -EPERM;
2184 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2185 pr_err("KVM: CPU requires an RMO\n");
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002186 goto out_srcu;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002187 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002188
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002189 /* We can handle 4k, 64k or 16M pages in the VRMA */
2190 err = -EINVAL;
2191 if (!(psize == 0x1000 || psize == 0x10000 ||
2192 psize == 0x1000000))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002193 goto out_srcu;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002194
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002195 /* Update VRMASD field in the LPCR */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002196 senc = slb_pgsize_encoding(psize);
Paul Mackerras697d3892011-12-12 12:36:37 +00002197 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2198 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002199 lpcr_mask = LPCR_VRMASD;
2200 /* the -4 is to account for senc values starting at 0x10 */
2201 lpcr = senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002202
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002203 /* Create HPTEs in the hash page table for the VRMA */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002204 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002205
2206 } else {
2207 /* Set up to use an RMO region */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302208 rma_size = kvm_rma_pages;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002209 if (rma_size > memslot->npages)
2210 rma_size = memslot->npages;
2211 rma_size <<= PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002212 rmls = lpcr_rmls(rma_size);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002213 err = -EINVAL;
Chen Gang5d226ae2013-07-22 14:32:35 +08002214 if ((long)rmls < 0) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002215 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002216 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002217 }
2218 atomic_inc(&ri->use_count);
2219 kvm->arch.rma = ri;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002220
2221 /* Update LPCR and RMOR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00002222 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2223 /* PPC970; insert RMLS value (split field) in HID4 */
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002224 lpcr_mask = (1ul << HID4_RMLS0_SH) |
2225 (3ul << HID4_RMLS2_SH) | HID4_RMOR;
2226 lpcr = ((rmls >> 2) << HID4_RMLS0_SH) |
Paul Mackerras9e368f22011-06-29 00:40:08 +00002227 ((rmls & 3) << HID4_RMLS2_SH);
2228 /* RMOR is also in HID4 */
2229 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
2230 << HID4_RMOR_SH;
2231 } else {
2232 /* POWER7 */
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002233 lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS;
2234 lpcr = rmls << LPCR_RMLS_SH;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302235 kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002236 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002237 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002238 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002239
2240 /* Initialize phys addrs of pages in RMO */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302241 npages = kvm_rma_pages;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002242 porder = __ilog2(npages);
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002243 physp = memslot->arch.slot_phys;
2244 if (physp) {
2245 if (npages > memslot->npages)
2246 npages = memslot->npages;
2247 spin_lock(&kvm->arch.slot_phys_lock);
2248 for (i = 0; i < npages; ++i)
2249 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
2250 porder;
2251 spin_unlock(&kvm->arch.slot_phys_lock);
2252 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002253 }
2254
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002255 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
2256
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002257 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
2258 smp_wmb();
2259 kvm->arch.rma_setup_done = 1;
2260 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002261 out_srcu:
2262 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002263 out:
2264 mutex_unlock(&kvm->lock);
2265 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002266
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002267 up_out:
2268 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08002269 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002270}
2271
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302272static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002273{
Paul Mackerras32fad282012-05-04 02:32:53 +00002274 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002275
Paul Mackerras32fad282012-05-04 02:32:53 +00002276 /* Allocate the guest's logical partition ID */
2277
2278 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08002279 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00002280 return -ENOMEM;
2281 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002282
Paul Mackerras1b400ba2012-11-21 23:28:08 +00002283 /*
2284 * Since we don't flush the TLB when tearing down a VM,
2285 * and this lpid might have previously been used,
2286 * make sure we flush on each core before running the new VM.
2287 */
2288 cpumask_setall(&kvm->arch.need_tlb_flush);
2289
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002290 kvm->arch.rma = NULL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002291
Paul Mackerras9e368f22011-06-29 00:40:08 +00002292 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002293
Paul Mackerras9e368f22011-06-29 00:40:08 +00002294 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2295 /* PPC970; HID4 is effectively the LPCR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00002296 kvm->arch.host_lpid = 0;
2297 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
2298 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
2299 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
2300 ((lpid & 0xf) << HID4_LPID5_SH);
2301 } else {
2302 /* POWER7; init LPCR for virtual RMA mode */
2303 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2304 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2305 lpcr &= LPCR_PECE | LPCR_LPES;
2306 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
Paul Mackerras697d3892011-12-12 12:36:37 +00002307 LPCR_VPM0 | LPCR_VPM1;
2308 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2309 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrase0622bd2014-01-08 21:25:27 +11002310 /* On POWER8 turn on online bit to enable PURR/SPURR */
2311 if (cpu_has_feature(CPU_FTR_ARCH_207S))
2312 lpcr |= LPCR_ONL;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002313 }
2314 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002315
Paul Mackerras342d3db2011-12-12 12:38:05 +00002316 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002317 spin_lock_init(&kvm->arch.slot_phys_lock);
Paul Mackerras512691d2012-10-15 01:15:41 +00002318
2319 /*
Michael Ellerman441c19c2014-05-23 18:15:25 +10002320 * Track that we now have a HV mode VM active. This blocks secondary
2321 * CPU threads from coming online.
Paul Mackerras512691d2012-10-15 01:15:41 +00002322 */
Michael Ellerman441c19c2014-05-23 18:15:25 +10002323 kvm_hv_vm_activated();
Paul Mackerras512691d2012-10-15 01:15:41 +00002324
David Gibson54738c02011-06-29 00:22:41 +00002325 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002326}
2327
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302328static void kvmppc_free_vcores(struct kvm *kvm)
2329{
2330 long int i;
2331
2332 for (i = 0; i < KVM_MAX_VCORES; ++i)
2333 kfree(kvm->arch.vcores[i]);
2334 kvm->arch.online_vcores = 0;
2335}
2336
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302337static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002338{
Michael Ellerman441c19c2014-05-23 18:15:25 +10002339 kvm_hv_vm_deactivated();
Paul Mackerras512691d2012-10-15 01:15:41 +00002340
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302341 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002342 if (kvm->arch.rma) {
2343 kvm_release_rma(kvm->arch.rma);
2344 kvm->arch.rma = NULL;
2345 }
2346
Paul Mackerrasde56a942011-06-29 00:21:34 +00002347 kvmppc_free_hpt(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002348}
2349
2350/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302351static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2352 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002353{
2354 return EMULATE_FAIL;
2355}
2356
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302357static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2358 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002359{
2360 return EMULATE_FAIL;
2361}
2362
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302363static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2364 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002365{
2366 return EMULATE_FAIL;
2367}
2368
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302369static int kvmppc_core_check_processor_compat_hv(void)
2370{
2371 if (!cpu_has_feature(CPU_FTR_HVMODE))
2372 return -EIO;
2373 return 0;
2374}
2375
2376static long kvm_arch_vm_ioctl_hv(struct file *filp,
2377 unsigned int ioctl, unsigned long arg)
2378{
2379 struct kvm *kvm __maybe_unused = filp->private_data;
2380 void __user *argp = (void __user *)arg;
2381 long r;
2382
2383 switch (ioctl) {
2384
2385 case KVM_ALLOCATE_RMA: {
2386 struct kvm_allocate_rma rma;
2387 struct kvm *kvm = filp->private_data;
2388
2389 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
2390 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
2391 r = -EFAULT;
2392 break;
2393 }
2394
2395 case KVM_PPC_ALLOCATE_HTAB: {
2396 u32 htab_order;
2397
2398 r = -EFAULT;
2399 if (get_user(htab_order, (u32 __user *)argp))
2400 break;
2401 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2402 if (r)
2403 break;
2404 r = -EFAULT;
2405 if (put_user(htab_order, (u32 __user *)argp))
2406 break;
2407 r = 0;
2408 break;
2409 }
2410
2411 case KVM_PPC_GET_HTAB_FD: {
2412 struct kvm_get_htab_fd ghf;
2413
2414 r = -EFAULT;
2415 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2416 break;
2417 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2418 break;
2419 }
2420
2421 default:
2422 r = -ENOTTY;
2423 }
2424
2425 return r;
2426}
2427
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302428static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302429 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2430 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2431 .get_one_reg = kvmppc_get_one_reg_hv,
2432 .set_one_reg = kvmppc_set_one_reg_hv,
2433 .vcpu_load = kvmppc_core_vcpu_load_hv,
2434 .vcpu_put = kvmppc_core_vcpu_put_hv,
2435 .set_msr = kvmppc_set_msr_hv,
2436 .vcpu_run = kvmppc_vcpu_run_hv,
2437 .vcpu_create = kvmppc_core_vcpu_create_hv,
2438 .vcpu_free = kvmppc_core_vcpu_free_hv,
2439 .check_requests = kvmppc_core_check_requests_hv,
2440 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2441 .flush_memslot = kvmppc_core_flush_memslot_hv,
2442 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2443 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2444 .unmap_hva = kvm_unmap_hva_hv,
2445 .unmap_hva_range = kvm_unmap_hva_range_hv,
2446 .age_hva = kvm_age_hva_hv,
2447 .test_age_hva = kvm_test_age_hva_hv,
2448 .set_spte_hva = kvm_set_spte_hva_hv,
2449 .mmu_destroy = kvmppc_mmu_destroy_hv,
2450 .free_memslot = kvmppc_core_free_memslot_hv,
2451 .create_memslot = kvmppc_core_create_memslot_hv,
2452 .init_vm = kvmppc_core_init_vm_hv,
2453 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302454 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2455 .emulate_op = kvmppc_core_emulate_op_hv,
2456 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2457 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2458 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2459 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
2460};
2461
2462static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002463{
2464 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302465 /*
2466 * FIXME!! Do we need to check on all cpus ?
2467 */
2468 r = kvmppc_core_check_processor_compat_hv();
2469 if (r < 0)
Paul Mackerras739e2422014-03-25 10:47:05 +11002470 return -ENODEV;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002471
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302472 kvm_ops_hv.owner = THIS_MODULE;
2473 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002474
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302475 r = kvmppc_mmu_hv_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002476 return r;
2477}
2478
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302479static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002480{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302481 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002482}
2483
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302484module_init(kvmppc_book3s_init_hv);
2485module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05302486MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01002487MODULE_ALIAS_MISCDEV(KVM_MINOR);
2488MODULE_ALIAS("devname:kvm");