blob: 1a7a281be3bda465df83c3d9c4403db734d20d35 [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>
Stewart Smith9678cda2014-07-18 14:18:43 +100038#include <asm/cache.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000039#include <asm/cacheflush.h>
40#include <asm/tlbflush.h>
41#include <asm/uaccess.h>
42#include <asm/io.h>
43#include <asm/kvm_ppc.h>
44#include <asm/kvm_book3s.h>
45#include <asm/mmu_context.h>
46#include <asm/lppaca.h>
47#include <asm/processor.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000048#include <asm/cputhreads.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000049#include <asm/page.h>
Michael Neulingde1d9242011-11-09 20:39:49 +000050#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010051#include <asm/switch_to.h>
Paul Mackerras512691d2012-10-15 01:15:41 +000052#include <asm/smp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000053#include <linux/gfp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000054#include <linux/vmalloc.h>
55#include <linux/highmem.h>
Paul Mackerrasc77162d2011-12-12 12:31:00 +000056#include <linux/hugetlb.h>
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +053057#include <linux/module.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000058
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053059#include "book3s.h"
60
Paul Mackerrasde56a942011-06-29 00:21:34 +000061/* #define EXIT_DEBUG */
62/* #define EXIT_DEBUG_SIMPLE */
63/* #define EXIT_DEBUG_INT */
64
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000065/* Used to indicate that a guest page fault needs to be handled */
66#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
67
Paul Mackerrasc7b67672012-10-15 01:18:07 +000068/* Used as a "null" value for timebase values */
69#define TB_NIL (~(u64)0)
70
Paul Mackerras699a0ea2014-06-02 11:02:59 +100071static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
72
Stewart Smith9678cda2014-07-18 14:18:43 +100073#if defined(CONFIG_PPC_64K_PAGES)
74#define MPP_BUFFER_ORDER 0
75#elif defined(CONFIG_PPC_4K_PAGES)
76#define MPP_BUFFER_ORDER 3
77#endif
78
79
Paul Mackerras19ccb762011-07-23 17:42:46 +100080static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +000081static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +100082
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053083static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +000084{
85 int me;
86 int cpu = vcpu->cpu;
87 wait_queue_head_t *wqp;
88
89 wqp = kvm_arch_vcpu_wq(vcpu);
90 if (waitqueue_active(wqp)) {
91 wake_up_interruptible(wqp);
92 ++vcpu->stat.halt_wakeup;
93 }
94
95 me = get_cpu();
96
97 /* CPU points to the first thread of the core */
98 if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
Anton Blanchard75052582014-03-25 10:47:01 +110099#ifdef CONFIG_PPC_ICP_NATIVE
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000100 int real_cpu = cpu + vcpu->arch.ptid;
101 if (paca[real_cpu].kvm_hstate.xics_phys)
102 xics_wake_cpu(real_cpu);
Andreas Schwab48eaef02013-12-30 15:36:56 +0100103 else
104#endif
105 if (cpu_online(cpu))
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000106 smp_send_reschedule(cpu);
107 }
108 put_cpu();
109}
110
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000111/*
112 * We use the vcpu_load/put functions to measure stolen time.
113 * Stolen time is counted as time when either the vcpu is able to
114 * run as part of a virtual core, but the task running the vcore
115 * is preempted or sleeping, or when the vcpu needs something done
116 * in the kernel by the task running the vcpu, but that task is
117 * preempted or sleeping. Those two things have to be counted
118 * separately, since one of the vcpu tasks will take on the job
119 * of running the core, and the other vcpu tasks in the vcore will
120 * sleep waiting for it to do that, but that sleep shouldn't count
121 * as stolen time.
122 *
123 * Hence we accumulate stolen time when the vcpu can run as part of
124 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
125 * needs its task to do other things in the kernel (for example,
126 * service a page fault) in busy_stolen. We don't accumulate
127 * stolen time for a vcore when it is inactive, or for a vcpu
128 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
129 * a misnomer; it means that the vcpu task is not executing in
130 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
131 * the kernel. We don't have any way of dividing up that time
132 * between time that the vcpu is genuinely stopped, time that
133 * the task is actively working on behalf of the vcpu, and time
134 * that the task is preempted, so we don't count any of it as
135 * stolen.
136 *
137 * Updates to busy_stolen are protected by arch.tbacct_lock;
138 * updates to vc->stolen_tb are protected by the arch.tbacct_lock
139 * of the vcpu that has taken responsibility for running the vcore
140 * (i.e. vc->runner). The stolen times are measured in units of
141 * timebase ticks. (Note that the != TB_NIL checks below are
142 * purely defensive; they should never fail.)
143 */
144
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530145static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000146{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000147 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100148 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000149
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100150 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000151 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
152 vc->preempt_tb != TB_NIL) {
Paul Mackerras0456ec42012-02-03 00:56:21 +0000153 vc->stolen_tb += mftb() - vc->preempt_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000154 vc->preempt_tb = TB_NIL;
155 }
156 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
157 vcpu->arch.busy_preempt != TB_NIL) {
158 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
159 vcpu->arch.busy_preempt = TB_NIL;
160 }
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100161 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000162}
163
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530164static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000165{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000166 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100167 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000168
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100169 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000170 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
171 vc->preempt_tb = mftb();
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000172 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
173 vcpu->arch.busy_preempt = mftb();
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100174 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000175}
176
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530177static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000178{
179 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000180 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000181}
182
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530183void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000184{
185 vcpu->arch.pvr = pvr;
186}
187
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000188int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
189{
190 unsigned long pcr = 0;
191 struct kvmppc_vcore *vc = vcpu->arch.vcore;
192
193 if (arch_compat) {
194 if (!cpu_has_feature(CPU_FTR_ARCH_206))
195 return -EINVAL; /* 970 has no compat mode support */
196
197 switch (arch_compat) {
198 case PVR_ARCH_205:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100199 /*
200 * If an arch bit is set in PCR, all the defined
201 * higher-order arch bits also have to be set.
202 */
203 pcr = PCR_ARCH_206 | PCR_ARCH_205;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000204 break;
205 case PVR_ARCH_206:
206 case PVR_ARCH_206p:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100207 pcr = PCR_ARCH_206;
208 break;
209 case PVR_ARCH_207:
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000210 break;
211 default:
212 return -EINVAL;
213 }
Paul Mackerras5557ae02014-01-08 21:25:24 +1100214
215 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
216 /* POWER7 can't emulate POWER8 */
217 if (!(pcr & PCR_ARCH_206))
218 return -EINVAL;
219 pcr &= ~PCR_ARCH_206;
220 }
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000221 }
222
223 spin_lock(&vc->lock);
224 vc->arch_compat = arch_compat;
225 vc->pcr = pcr;
226 spin_unlock(&vc->lock);
227
228 return 0;
229}
230
Paul Mackerrasde56a942011-06-29 00:21:34 +0000231void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
232{
233 int r;
234
235 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
236 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
237 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
238 for (r = 0; r < 16; ++r)
239 pr_err("r%2d = %.16lx r%d = %.16lx\n",
240 r, kvmppc_get_gpr(vcpu, r),
241 r+16, kvmppc_get_gpr(vcpu, r+16));
242 pr_err("ctr = %.16lx lr = %.16lx\n",
243 vcpu->arch.ctr, vcpu->arch.lr);
244 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
245 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
246 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
247 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
248 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
249 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
250 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
251 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
252 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
253 pr_err("fault dar = %.16lx dsisr = %.8x\n",
254 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
255 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
256 for (r = 0; r < vcpu->arch.slb_max; ++r)
257 pr_err(" ESID = %.16llx VSID = %.16llx\n",
258 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
259 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000260 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000261 vcpu->arch.last_inst);
262}
263
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000264struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
265{
266 int r;
267 struct kvm_vcpu *v, *ret = NULL;
268
269 mutex_lock(&kvm->lock);
270 kvm_for_each_vcpu(r, v, kvm) {
271 if (v->vcpu_id == id) {
272 ret = v;
273 break;
274 }
275 }
276 mutex_unlock(&kvm->lock);
277 return ret;
278}
279
280static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
281{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000282 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Alexander Graf02407552014-06-11 10:34:19 +0200283 vpa->yield_count = cpu_to_be32(1);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000284}
285
Paul Mackerras55b665b2012-09-25 20:33:06 +0000286static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
287 unsigned long addr, unsigned long len)
288{
289 /* check address is cacheline aligned */
290 if (addr & (L1_CACHE_BYTES - 1))
291 return -EINVAL;
292 spin_lock(&vcpu->arch.vpa_update_lock);
293 if (v->next_gpa != addr || v->len != len) {
294 v->next_gpa = addr;
295 v->len = addr ? len : 0;
296 v->update_pending = 1;
297 }
298 spin_unlock(&vcpu->arch.vpa_update_lock);
299 return 0;
300}
301
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000302/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
303struct reg_vpa {
304 u32 dummy;
305 union {
Alexander Graf02407552014-06-11 10:34:19 +0200306 __be16 hword;
307 __be32 word;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000308 } length;
309};
310
311static int vpa_is_registered(struct kvmppc_vpa *vpap)
312{
313 if (vpap->update_pending)
314 return vpap->next_gpa != 0;
315 return vpap->pinned_addr != NULL;
316}
317
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000318static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
319 unsigned long flags,
320 unsigned long vcpuid, unsigned long vpa)
321{
322 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000323 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000324 void *va;
325 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000326 int err;
327 int subfunc;
328 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000329
330 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
331 if (!tvcpu)
332 return H_PARAMETER;
333
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000334 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
335 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
336 subfunc == H_VPA_REG_SLB) {
337 /* Registering new area - address must be cache-line aligned */
338 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000339 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000340
341 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000342 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
343 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000344 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000345 if (subfunc == H_VPA_REG_VPA)
Alexander Graf02407552014-06-11 10:34:19 +0200346 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000347 else
Alexander Graf02407552014-06-11 10:34:19 +0200348 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000349 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000350
351 /* Check length */
352 if (len > nb || len < sizeof(struct reg_vpa))
353 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000354 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000355 vpa = 0;
356 len = 0;
357 }
358
359 err = H_PARAMETER;
360 vpap = NULL;
361 spin_lock(&tvcpu->arch.vpa_update_lock);
362
363 switch (subfunc) {
364 case H_VPA_REG_VPA: /* register VPA */
365 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000366 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000367 vpap = &tvcpu->arch.vpa;
368 err = 0;
369 break;
370
371 case H_VPA_REG_DTL: /* register DTL */
372 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000373 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000374 len -= len % sizeof(struct dtl_entry);
375
376 /* Check that they have previously registered a VPA */
377 err = H_RESOURCE;
378 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000379 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000380
381 vpap = &tvcpu->arch.dtl;
382 err = 0;
383 break;
384
385 case H_VPA_REG_SLB: /* register SLB shadow buffer */
386 /* Check that they have previously registered a VPA */
387 err = H_RESOURCE;
388 if (!vpa_is_registered(&tvcpu->arch.vpa))
389 break;
390
391 vpap = &tvcpu->arch.slb_shadow;
392 err = 0;
393 break;
394
395 case H_VPA_DEREG_VPA: /* deregister VPA */
396 /* Check they don't still have a DTL or SLB buf registered */
397 err = H_RESOURCE;
398 if (vpa_is_registered(&tvcpu->arch.dtl) ||
399 vpa_is_registered(&tvcpu->arch.slb_shadow))
400 break;
401
402 vpap = &tvcpu->arch.vpa;
403 err = 0;
404 break;
405
406 case H_VPA_DEREG_DTL: /* deregister DTL */
407 vpap = &tvcpu->arch.dtl;
408 err = 0;
409 break;
410
411 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
412 vpap = &tvcpu->arch.slb_shadow;
413 err = 0;
414 break;
415 }
416
417 if (vpap) {
418 vpap->next_gpa = vpa;
419 vpap->len = len;
420 vpap->update_pending = 1;
421 }
422
423 spin_unlock(&tvcpu->arch.vpa_update_lock);
424
425 return err;
426}
427
Paul Mackerras081f3232012-06-01 20:20:24 +1000428static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000429{
Paul Mackerras081f3232012-06-01 20:20:24 +1000430 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000431 void *va;
432 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000433 unsigned long gpa;
434
435 /*
436 * We need to pin the page pointed to by vpap->next_gpa,
437 * but we can't call kvmppc_pin_guest_page under the lock
438 * as it does get_user_pages() and down_read(). So we
439 * have to drop the lock, pin the page, then get the lock
440 * again and check that a new area didn't get registered
441 * in the meantime.
442 */
443 for (;;) {
444 gpa = vpap->next_gpa;
445 spin_unlock(&vcpu->arch.vpa_update_lock);
446 va = NULL;
447 nb = 0;
448 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000449 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000450 spin_lock(&vcpu->arch.vpa_update_lock);
451 if (gpa == vpap->next_gpa)
452 break;
453 /* sigh... unpin that one and try again */
454 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000455 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000456 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000457
458 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000459 if (va && nb < vpap->len) {
460 /*
461 * If it's now too short, it must be that userspace
462 * has changed the mappings underlying guest memory,
463 * so unregister the region.
464 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000465 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000466 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000467 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000468 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000469 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
470 vpap->dirty);
471 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000472 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000473 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000474 if (va)
475 vpap->pinned_end = va + vpap->len;
476}
Paul Mackerras93e60242011-12-12 12:28:55 +0000477
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000478static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
479{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000480 if (!(vcpu->arch.vpa.update_pending ||
481 vcpu->arch.slb_shadow.update_pending ||
482 vcpu->arch.dtl.update_pending))
483 return;
484
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000485 spin_lock(&vcpu->arch.vpa_update_lock);
486 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000487 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000488 if (vcpu->arch.vpa.pinned_addr)
489 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000490 }
491 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000492 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000493 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
494 vcpu->arch.dtl_index = 0;
495 }
496 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000497 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000498 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000499}
500
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000501/*
502 * Return the accumulated stolen time for the vcore up until `now'.
503 * The caller should hold the vcore lock.
504 */
505static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
506{
507 u64 p;
508
509 /*
510 * If we are the task running the vcore, then since we hold
511 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
512 * can't be updated, so we don't need the tbacct_lock.
513 * If the vcore is inactive, it can't become active (since we
514 * hold the vcore lock), so the vcpu load/put functions won't
515 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
516 */
517 if (vc->vcore_state != VCORE_INACTIVE &&
518 vc->runner->arch.run_task != current) {
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100519 spin_lock_irq(&vc->runner->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000520 p = vc->stolen_tb;
521 if (vc->preempt_tb != TB_NIL)
522 p += now - vc->preempt_tb;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100523 spin_unlock_irq(&vc->runner->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000524 } else {
525 p = vc->stolen_tb;
526 }
527 return p;
528}
529
Paul Mackerras0456ec42012-02-03 00:56:21 +0000530static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
531 struct kvmppc_vcore *vc)
532{
533 struct dtl_entry *dt;
534 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000535 unsigned long stolen;
536 unsigned long core_stolen;
537 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000538
539 dt = vcpu->arch.dtl_ptr;
540 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000541 now = mftb();
542 core_stolen = vcore_stolen_time(vc, now);
543 stolen = core_stolen - vcpu->arch.stolen_logged;
544 vcpu->arch.stolen_logged = core_stolen;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100545 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000546 stolen += vcpu->arch.busy_stolen;
547 vcpu->arch.busy_stolen = 0;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100548 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000549 if (!dt || !vpa)
550 return;
551 memset(dt, 0, sizeof(struct dtl_entry));
552 dt->dispatch_reason = 7;
Alexander Graf02407552014-06-11 10:34:19 +0200553 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
554 dt->timebase = cpu_to_be64(now + vc->tb_offset);
555 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
556 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
557 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000558 ++dt;
559 if (dt == vcpu->arch.dtl.pinned_end)
560 dt = vcpu->arch.dtl.pinned_addr;
561 vcpu->arch.dtl_ptr = dt;
562 /* order writing *dt vs. writing vpa->dtl_idx */
563 smp_wmb();
Alexander Graf02407552014-06-11 10:34:19 +0200564 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000565 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000566}
567
Michael Neuling96423822014-06-02 11:03:01 +1000568static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
569{
570 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
571 return true;
572 if ((!vcpu->arch.vcore->arch_compat) &&
573 cpu_has_feature(CPU_FTR_ARCH_207S))
574 return true;
575 return false;
576}
577
578static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
579 unsigned long resource, unsigned long value1,
580 unsigned long value2)
581{
582 switch (resource) {
583 case H_SET_MODE_RESOURCE_SET_CIABR:
584 if (!kvmppc_power8_compatible(vcpu))
585 return H_P2;
586 if (value2)
587 return H_P4;
588 if (mflags)
589 return H_UNSUPPORTED_FLAG_START;
590 /* Guests can't breakpoint the hypervisor */
591 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
592 return H_P3;
593 vcpu->arch.ciabr = value1;
594 return H_SUCCESS;
595 case H_SET_MODE_RESOURCE_SET_DAWR:
596 if (!kvmppc_power8_compatible(vcpu))
597 return H_P2;
598 if (mflags)
599 return H_UNSUPPORTED_FLAG_START;
600 if (value2 & DABRX_HYP)
601 return H_P4;
602 vcpu->arch.dawr = value1;
603 vcpu->arch.dawrx = value2;
604 return H_SUCCESS;
605 default:
606 return H_TOO_HARD;
607 }
608}
609
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000610int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
611{
612 unsigned long req = kvmppc_get_gpr(vcpu, 3);
613 unsigned long target, ret = H_SUCCESS;
614 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000615 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000616
Paul Mackerras699a0ea2014-06-02 11:02:59 +1000617 if (req <= MAX_HCALL_OPCODE &&
618 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
619 return RESUME_HOST;
620
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000621 switch (req) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000622 case H_ENTER:
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000623 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000624 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
625 kvmppc_get_gpr(vcpu, 5),
626 kvmppc_get_gpr(vcpu, 6),
627 kvmppc_get_gpr(vcpu, 7));
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000628 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000629 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000630 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000631 break;
632 case H_PROD:
633 target = kvmppc_get_gpr(vcpu, 4);
634 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
635 if (!tvcpu) {
636 ret = H_PARAMETER;
637 break;
638 }
639 tvcpu->arch.prodded = 1;
640 smp_mb();
641 if (vcpu->arch.ceded) {
642 if (waitqueue_active(&vcpu->wq)) {
643 wake_up_interruptible(&vcpu->wq);
644 vcpu->stat.halt_wakeup++;
645 }
646 }
647 break;
648 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +1000649 target = kvmppc_get_gpr(vcpu, 4);
650 if (target == -1)
651 break;
652 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
653 if (!tvcpu) {
654 ret = H_PARAMETER;
655 break;
656 }
657 kvm_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000658 break;
659 case H_REGISTER_VPA:
660 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
661 kvmppc_get_gpr(vcpu, 5),
662 kvmppc_get_gpr(vcpu, 6));
663 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000664 case H_RTAS:
665 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
666 return RESUME_HOST;
667
Paul Mackerrasc9438092013-11-16 17:46:05 +1100668 idx = srcu_read_lock(&vcpu->kvm->srcu);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000669 rc = kvmppc_rtas_hcall(vcpu);
Paul Mackerrasc9438092013-11-16 17:46:05 +1100670 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000671
672 if (rc == -ENOENT)
673 return RESUME_HOST;
674 else if (rc == 0)
675 break;
676
677 /* Send the error out to userspace via KVM_RUN */
678 return rc;
Michael Neuling96423822014-06-02 11:03:01 +1000679 case H_SET_MODE:
680 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
681 kvmppc_get_gpr(vcpu, 5),
682 kvmppc_get_gpr(vcpu, 6),
683 kvmppc_get_gpr(vcpu, 7));
684 if (ret == H_TOO_HARD)
685 return RESUME_HOST;
686 break;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000687 case H_XIRR:
688 case H_CPPR:
689 case H_EOI:
690 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000691 case H_IPOLL:
692 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000693 if (kvmppc_xics_enabled(vcpu)) {
694 ret = kvmppc_xics_hcall(vcpu, req);
695 break;
696 } /* fallthrough */
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000697 default:
698 return RESUME_HOST;
699 }
700 kvmppc_set_gpr(vcpu, 3, ret);
701 vcpu->arch.hcall_needed = 0;
702 return RESUME_GUEST;
703}
704
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000705static int kvmppc_hcall_impl_hv(unsigned long cmd)
706{
707 switch (cmd) {
708 case H_CEDE:
709 case H_PROD:
710 case H_CONFER:
711 case H_REGISTER_VPA:
Michael Neuling96423822014-06-02 11:03:01 +1000712 case H_SET_MODE:
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000713#ifdef CONFIG_KVM_XICS
714 case H_XIRR:
715 case H_CPPR:
716 case H_EOI:
717 case H_IPI:
718 case H_IPOLL:
719 case H_XIRR_X:
720#endif
721 return 1;
722 }
723
724 /* See if it's in the real-mode table */
725 return kvmppc_hcall_impl_hv_realmode(cmd);
726}
727
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530728static int kvmppc_emulate_debug_inst(struct kvm_run *run,
729 struct kvm_vcpu *vcpu)
730{
731 u32 last_inst;
732
733 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
734 EMULATE_DONE) {
735 /*
736 * Fetch failed, so return to guest and
737 * try executing it again.
738 */
739 return RESUME_GUEST;
740 }
741
742 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
743 run->exit_reason = KVM_EXIT_DEBUG;
744 run->debug.arch.address = kvmppc_get_pc(vcpu);
745 return RESUME_HOST;
746 } else {
747 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
748 return RESUME_GUEST;
749 }
750}
751
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530752static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
753 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000754{
755 int r = RESUME_HOST;
756
757 vcpu->stat.sum_exits++;
758
759 run->exit_reason = KVM_EXIT_UNKNOWN;
760 run->ready_for_interrupt_injection = 1;
761 switch (vcpu->arch.trap) {
762 /* We're good on these - the host merely wanted to get our attention */
763 case BOOK3S_INTERRUPT_HV_DECREMENTER:
764 vcpu->stat.dec_exits++;
765 r = RESUME_GUEST;
766 break;
767 case BOOK3S_INTERRUPT_EXTERNAL:
Paul Mackerras5d00f662014-01-08 21:25:28 +1100768 case BOOK3S_INTERRUPT_H_DOORBELL:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000769 vcpu->stat.ext_intr_exits++;
770 r = RESUME_GUEST;
771 break;
Mahesh Salgaonkardee6f242014-11-03 15:51:57 +1100772 /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
773 case BOOK3S_INTERRUPT_HMI:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000774 case BOOK3S_INTERRUPT_PERFMON:
775 r = RESUME_GUEST;
776 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000777 case BOOK3S_INTERRUPT_MACHINE_CHECK:
778 /*
779 * Deliver a machine check interrupt to the guest.
780 * We have to do this, even if the host has handled the
781 * machine check, because machine checks use SRR0/1 and
782 * the interrupt might have trashed guest state in them.
783 */
784 kvmppc_book3s_queue_irqprio(vcpu,
785 BOOK3S_INTERRUPT_MACHINE_CHECK);
786 r = RESUME_GUEST;
787 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000788 case BOOK3S_INTERRUPT_PROGRAM:
789 {
790 ulong flags;
791 /*
792 * Normally program interrupts are delivered directly
793 * to the guest by the hardware, but we can get here
794 * as a result of a hypervisor emulation interrupt
795 * (e40) getting turned into a 700 by BML RTAS.
796 */
797 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
798 kvmppc_core_queue_program(vcpu, flags);
799 r = RESUME_GUEST;
800 break;
801 }
802 case BOOK3S_INTERRUPT_SYSCALL:
803 {
804 /* hcall - punt to userspace */
805 int i;
806
Liu Ping Fan27025a62013-11-19 14:12:48 +0800807 /* hypercall with MSR_PR has already been handled in rmode,
808 * and never reaches here.
809 */
810
Paul Mackerrasde56a942011-06-29 00:21:34 +0000811 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
812 for (i = 0; i < 9; ++i)
813 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
814 run->exit_reason = KVM_EXIT_PAPR_HCALL;
815 vcpu->arch.hcall_needed = 1;
816 r = RESUME_HOST;
817 break;
818 }
819 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000820 * We get these next two if the guest accesses a page which it thinks
821 * it has mapped but which is not actually present, either because
822 * it is for an emulated I/O device or because the corresonding
823 * host page has been paged out. Any other HDSI/HISI interrupts
824 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000825 */
826 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000827 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000828 break;
829 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000830 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
831 vcpu->arch.fault_dsisr = 0;
832 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000833 break;
834 /*
835 * This occurs if the guest executes an illegal instruction.
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530836 * If the guest debug is disabled, generate a program interrupt
837 * to the guest. If guest debug is enabled, we need to check
838 * whether the instruction is a software breakpoint instruction.
839 * Accordingly return to Guest or Host.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000840 */
841 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530842 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
843 r = kvmppc_emulate_debug_inst(run, vcpu);
844 } else {
845 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
846 r = RESUME_GUEST;
847 }
Michael Ellermanbd3048b2014-01-08 21:25:23 +1100848 break;
849 /*
850 * This occurs if the guest (kernel or userspace), does something that
851 * is prohibited by HFSCR. We just generate a program interrupt to
852 * the guest.
853 */
854 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
855 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000856 r = RESUME_GUEST;
857 break;
858 default:
859 kvmppc_dump_regs(vcpu);
860 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
861 vcpu->arch.trap, kvmppc_get_pc(vcpu),
862 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +1000863 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000864 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000865 break;
866 }
867
Paul Mackerrasde56a942011-06-29 00:21:34 +0000868 return r;
869}
870
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530871static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
872 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000873{
874 int i;
875
Paul Mackerrasde56a942011-06-29 00:21:34 +0000876 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +0530877 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000878 for (i = 0; i < vcpu->arch.slb_max; i++) {
879 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
880 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
881 }
882
883 return 0;
884}
885
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530886static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
887 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000888{
889 int i, j;
890
Paul Mackerras9333e6c2014-09-02 16:14:43 +1000891 /* Only accept the same PVR as the host's, since we can't spoof it */
892 if (sregs->pvr != vcpu->arch.pvr)
893 return -EINVAL;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000894
895 j = 0;
896 for (i = 0; i < vcpu->arch.slb_nr; i++) {
897 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
898 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
899 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
900 ++j;
901 }
902 }
903 vcpu->arch.slb_max = j;
904
905 return 0;
906}
907
Alexey Kardashevskiya0840242014-07-19 17:59:34 +1000908static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
909 bool preserve_top32)
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000910{
911 struct kvmppc_vcore *vc = vcpu->arch.vcore;
912 u64 mask;
913
914 spin_lock(&vc->lock);
915 /*
Anton Blanchardd6829162014-01-08 21:25:30 +1100916 * If ILE (interrupt little-endian) has changed, update the
917 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
918 */
919 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
920 struct kvm *kvm = vcpu->kvm;
921 struct kvm_vcpu *vcpu;
922 int i;
923
924 mutex_lock(&kvm->lock);
925 kvm_for_each_vcpu(i, vcpu, kvm) {
926 if (vcpu->arch.vcore != vc)
927 continue;
928 if (new_lpcr & LPCR_ILE)
929 vcpu->arch.intr_msr |= MSR_LE;
930 else
931 vcpu->arch.intr_msr &= ~MSR_LE;
932 }
933 mutex_unlock(&kvm->lock);
934 }
935
936 /*
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000937 * Userspace can only modify DPFD (default prefetch depth),
938 * ILE (interrupt little-endian) and TC (translation control).
Paul Mackerrase0622bd2014-01-08 21:25:27 +1100939 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000940 */
941 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
Paul Mackerrase0622bd2014-01-08 21:25:27 +1100942 if (cpu_has_feature(CPU_FTR_ARCH_207S))
943 mask |= LPCR_AIL;
Alexey Kardashevskiya0840242014-07-19 17:59:34 +1000944
945 /* Broken 32-bit version of LPCR must not clear top bits */
946 if (preserve_top32)
947 mask &= 0xFFFFFFFF;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000948 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
949 spin_unlock(&vc->lock);
950}
951
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530952static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
953 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000954{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000955 int r = 0;
956 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +0000957
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000958 switch (id) {
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530959 case KVM_REG_PPC_DEBUG_INST:
960 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
961 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000962 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000963 *val = get_reg_val(id, 0);
964 break;
965 case KVM_REG_PPC_DABR:
966 *val = get_reg_val(id, vcpu->arch.dabr);
967 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +1100968 case KVM_REG_PPC_DABRX:
969 *val = get_reg_val(id, vcpu->arch.dabrx);
970 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000971 case KVM_REG_PPC_DSCR:
972 *val = get_reg_val(id, vcpu->arch.dscr);
973 break;
974 case KVM_REG_PPC_PURR:
975 *val = get_reg_val(id, vcpu->arch.purr);
976 break;
977 case KVM_REG_PPC_SPURR:
978 *val = get_reg_val(id, vcpu->arch.spurr);
979 break;
980 case KVM_REG_PPC_AMR:
981 *val = get_reg_val(id, vcpu->arch.amr);
982 break;
983 case KVM_REG_PPC_UAMOR:
984 *val = get_reg_val(id, vcpu->arch.uamor);
985 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100986 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000987 i = id - KVM_REG_PPC_MMCR0;
988 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
989 break;
990 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
991 i = id - KVM_REG_PPC_PMC1;
992 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +0000993 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100994 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
995 i = id - KVM_REG_PPC_SPMC1;
996 *val = get_reg_val(id, vcpu->arch.spmc[i]);
997 break;
Paul Mackerras14941782013-09-06 13:11:18 +1000998 case KVM_REG_PPC_SIAR:
999 *val = get_reg_val(id, vcpu->arch.siar);
1000 break;
1001 case KVM_REG_PPC_SDAR:
1002 *val = get_reg_val(id, vcpu->arch.sdar);
1003 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001004 case KVM_REG_PPC_SIER:
1005 *val = get_reg_val(id, vcpu->arch.sier);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001006 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001007 case KVM_REG_PPC_IAMR:
1008 *val = get_reg_val(id, vcpu->arch.iamr);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001009 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001010 case KVM_REG_PPC_PSPB:
1011 *val = get_reg_val(id, vcpu->arch.pspb);
1012 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001013 case KVM_REG_PPC_DPDES:
1014 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1015 break;
1016 case KVM_REG_PPC_DAWR:
1017 *val = get_reg_val(id, vcpu->arch.dawr);
1018 break;
1019 case KVM_REG_PPC_DAWRX:
1020 *val = get_reg_val(id, vcpu->arch.dawrx);
1021 break;
1022 case KVM_REG_PPC_CIABR:
1023 *val = get_reg_val(id, vcpu->arch.ciabr);
1024 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001025 case KVM_REG_PPC_CSIGR:
1026 *val = get_reg_val(id, vcpu->arch.csigr);
1027 break;
1028 case KVM_REG_PPC_TACR:
1029 *val = get_reg_val(id, vcpu->arch.tacr);
1030 break;
1031 case KVM_REG_PPC_TCSCR:
1032 *val = get_reg_val(id, vcpu->arch.tcscr);
1033 break;
1034 case KVM_REG_PPC_PID:
1035 *val = get_reg_val(id, vcpu->arch.pid);
1036 break;
1037 case KVM_REG_PPC_ACOP:
1038 *val = get_reg_val(id, vcpu->arch.acop);
1039 break;
1040 case KVM_REG_PPC_WORT:
1041 *val = get_reg_val(id, vcpu->arch.wort);
1042 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001043 case KVM_REG_PPC_VPA_ADDR:
1044 spin_lock(&vcpu->arch.vpa_update_lock);
1045 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1046 spin_unlock(&vcpu->arch.vpa_update_lock);
1047 break;
1048 case KVM_REG_PPC_VPA_SLB:
1049 spin_lock(&vcpu->arch.vpa_update_lock);
1050 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1051 val->vpaval.length = vcpu->arch.slb_shadow.len;
1052 spin_unlock(&vcpu->arch.vpa_update_lock);
1053 break;
1054 case KVM_REG_PPC_VPA_DTL:
1055 spin_lock(&vcpu->arch.vpa_update_lock);
1056 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1057 val->vpaval.length = vcpu->arch.dtl.len;
1058 spin_unlock(&vcpu->arch.vpa_update_lock);
1059 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001060 case KVM_REG_PPC_TB_OFFSET:
1061 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1062 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001063 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001064 case KVM_REG_PPC_LPCR_64:
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001065 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1066 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001067 case KVM_REG_PPC_PPR:
1068 *val = get_reg_val(id, vcpu->arch.ppr);
1069 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001070#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1071 case KVM_REG_PPC_TFHAR:
1072 *val = get_reg_val(id, vcpu->arch.tfhar);
1073 break;
1074 case KVM_REG_PPC_TFIAR:
1075 *val = get_reg_val(id, vcpu->arch.tfiar);
1076 break;
1077 case KVM_REG_PPC_TEXASR:
1078 *val = get_reg_val(id, vcpu->arch.texasr);
1079 break;
1080 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1081 i = id - KVM_REG_PPC_TM_GPR0;
1082 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1083 break;
1084 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1085 {
1086 int j;
1087 i = id - KVM_REG_PPC_TM_VSR0;
1088 if (i < 32)
1089 for (j = 0; j < TS_FPRWIDTH; j++)
1090 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1091 else {
1092 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1093 val->vval = vcpu->arch.vr_tm.vr[i-32];
1094 else
1095 r = -ENXIO;
1096 }
1097 break;
1098 }
1099 case KVM_REG_PPC_TM_CR:
1100 *val = get_reg_val(id, vcpu->arch.cr_tm);
1101 break;
1102 case KVM_REG_PPC_TM_LR:
1103 *val = get_reg_val(id, vcpu->arch.lr_tm);
1104 break;
1105 case KVM_REG_PPC_TM_CTR:
1106 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1107 break;
1108 case KVM_REG_PPC_TM_FPSCR:
1109 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1110 break;
1111 case KVM_REG_PPC_TM_AMR:
1112 *val = get_reg_val(id, vcpu->arch.amr_tm);
1113 break;
1114 case KVM_REG_PPC_TM_PPR:
1115 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1116 break;
1117 case KVM_REG_PPC_TM_VRSAVE:
1118 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1119 break;
1120 case KVM_REG_PPC_TM_VSCR:
1121 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1122 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1123 else
1124 r = -ENXIO;
1125 break;
1126 case KVM_REG_PPC_TM_DSCR:
1127 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1128 break;
1129 case KVM_REG_PPC_TM_TAR:
1130 *val = get_reg_val(id, vcpu->arch.tar_tm);
1131 break;
1132#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001133 case KVM_REG_PPC_ARCH_COMPAT:
1134 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1135 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001136 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001137 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001138 break;
1139 }
1140
1141 return r;
1142}
1143
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301144static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1145 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001146{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001147 int r = 0;
1148 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001149 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +00001150
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001151 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00001152 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +00001153 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001154 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +00001155 r = -EINVAL;
1156 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001157 case KVM_REG_PPC_DABR:
1158 vcpu->arch.dabr = set_reg_val(id, *val);
1159 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001160 case KVM_REG_PPC_DABRX:
1161 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1162 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001163 case KVM_REG_PPC_DSCR:
1164 vcpu->arch.dscr = set_reg_val(id, *val);
1165 break;
1166 case KVM_REG_PPC_PURR:
1167 vcpu->arch.purr = set_reg_val(id, *val);
1168 break;
1169 case KVM_REG_PPC_SPURR:
1170 vcpu->arch.spurr = set_reg_val(id, *val);
1171 break;
1172 case KVM_REG_PPC_AMR:
1173 vcpu->arch.amr = set_reg_val(id, *val);
1174 break;
1175 case KVM_REG_PPC_UAMOR:
1176 vcpu->arch.uamor = set_reg_val(id, *val);
1177 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001178 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001179 i = id - KVM_REG_PPC_MMCR0;
1180 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1181 break;
1182 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1183 i = id - KVM_REG_PPC_PMC1;
1184 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1185 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001186 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1187 i = id - KVM_REG_PPC_SPMC1;
1188 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1189 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001190 case KVM_REG_PPC_SIAR:
1191 vcpu->arch.siar = set_reg_val(id, *val);
1192 break;
1193 case KVM_REG_PPC_SDAR:
1194 vcpu->arch.sdar = set_reg_val(id, *val);
1195 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001196 case KVM_REG_PPC_SIER:
1197 vcpu->arch.sier = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001198 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001199 case KVM_REG_PPC_IAMR:
1200 vcpu->arch.iamr = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001201 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001202 case KVM_REG_PPC_PSPB:
1203 vcpu->arch.pspb = set_reg_val(id, *val);
1204 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001205 case KVM_REG_PPC_DPDES:
1206 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1207 break;
1208 case KVM_REG_PPC_DAWR:
1209 vcpu->arch.dawr = set_reg_val(id, *val);
1210 break;
1211 case KVM_REG_PPC_DAWRX:
1212 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1213 break;
1214 case KVM_REG_PPC_CIABR:
1215 vcpu->arch.ciabr = set_reg_val(id, *val);
1216 /* Don't allow setting breakpoints in hypervisor code */
1217 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1218 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1219 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001220 case KVM_REG_PPC_CSIGR:
1221 vcpu->arch.csigr = set_reg_val(id, *val);
1222 break;
1223 case KVM_REG_PPC_TACR:
1224 vcpu->arch.tacr = set_reg_val(id, *val);
1225 break;
1226 case KVM_REG_PPC_TCSCR:
1227 vcpu->arch.tcscr = set_reg_val(id, *val);
1228 break;
1229 case KVM_REG_PPC_PID:
1230 vcpu->arch.pid = set_reg_val(id, *val);
1231 break;
1232 case KVM_REG_PPC_ACOP:
1233 vcpu->arch.acop = set_reg_val(id, *val);
1234 break;
1235 case KVM_REG_PPC_WORT:
1236 vcpu->arch.wort = set_reg_val(id, *val);
1237 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001238 case KVM_REG_PPC_VPA_ADDR:
1239 addr = set_reg_val(id, *val);
1240 r = -EINVAL;
1241 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1242 vcpu->arch.dtl.next_gpa))
1243 break;
1244 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1245 break;
1246 case KVM_REG_PPC_VPA_SLB:
1247 addr = val->vpaval.addr;
1248 len = val->vpaval.length;
1249 r = -EINVAL;
1250 if (addr && !vcpu->arch.vpa.next_gpa)
1251 break;
1252 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1253 break;
1254 case KVM_REG_PPC_VPA_DTL:
1255 addr = val->vpaval.addr;
1256 len = val->vpaval.length;
1257 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +00001258 if (addr && (len < sizeof(struct dtl_entry) ||
1259 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +00001260 break;
1261 len -= len % sizeof(struct dtl_entry);
1262 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1263 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001264 case KVM_REG_PPC_TB_OFFSET:
1265 /* round up to multiple of 2^24 */
1266 vcpu->arch.vcore->tb_offset =
1267 ALIGN(set_reg_val(id, *val), 1UL << 24);
1268 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001269 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001270 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1271 break;
1272 case KVM_REG_PPC_LPCR_64:
1273 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001274 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001275 case KVM_REG_PPC_PPR:
1276 vcpu->arch.ppr = set_reg_val(id, *val);
1277 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001278#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1279 case KVM_REG_PPC_TFHAR:
1280 vcpu->arch.tfhar = set_reg_val(id, *val);
1281 break;
1282 case KVM_REG_PPC_TFIAR:
1283 vcpu->arch.tfiar = set_reg_val(id, *val);
1284 break;
1285 case KVM_REG_PPC_TEXASR:
1286 vcpu->arch.texasr = set_reg_val(id, *val);
1287 break;
1288 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1289 i = id - KVM_REG_PPC_TM_GPR0;
1290 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1291 break;
1292 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1293 {
1294 int j;
1295 i = id - KVM_REG_PPC_TM_VSR0;
1296 if (i < 32)
1297 for (j = 0; j < TS_FPRWIDTH; j++)
1298 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1299 else
1300 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1301 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1302 else
1303 r = -ENXIO;
1304 break;
1305 }
1306 case KVM_REG_PPC_TM_CR:
1307 vcpu->arch.cr_tm = set_reg_val(id, *val);
1308 break;
1309 case KVM_REG_PPC_TM_LR:
1310 vcpu->arch.lr_tm = set_reg_val(id, *val);
1311 break;
1312 case KVM_REG_PPC_TM_CTR:
1313 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1314 break;
1315 case KVM_REG_PPC_TM_FPSCR:
1316 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1317 break;
1318 case KVM_REG_PPC_TM_AMR:
1319 vcpu->arch.amr_tm = set_reg_val(id, *val);
1320 break;
1321 case KVM_REG_PPC_TM_PPR:
1322 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1323 break;
1324 case KVM_REG_PPC_TM_VRSAVE:
1325 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1326 break;
1327 case KVM_REG_PPC_TM_VSCR:
1328 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1329 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1330 else
1331 r = - ENXIO;
1332 break;
1333 case KVM_REG_PPC_TM_DSCR:
1334 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1335 break;
1336 case KVM_REG_PPC_TM_TAR:
1337 vcpu->arch.tar_tm = set_reg_val(id, *val);
1338 break;
1339#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001340 case KVM_REG_PPC_ARCH_COMPAT:
1341 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1342 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001343 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001344 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001345 break;
1346 }
1347
1348 return r;
1349}
1350
Stewart Smithde9bdd12014-07-18 14:18:42 +10001351static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1352{
1353 struct kvmppc_vcore *vcore;
1354
1355 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1356
1357 if (vcore == NULL)
1358 return NULL;
1359
1360 INIT_LIST_HEAD(&vcore->runnable_threads);
1361 spin_lock_init(&vcore->lock);
1362 init_waitqueue_head(&vcore->wq);
1363 vcore->preempt_tb = TB_NIL;
1364 vcore->lpcr = kvm->arch.lpcr;
1365 vcore->first_vcpuid = core * threads_per_subcore;
1366 vcore->kvm = kvm;
1367
Stewart Smith9678cda2014-07-18 14:18:43 +10001368 vcore->mpp_buffer_is_valid = false;
1369
1370 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1371 vcore->mpp_buffer = (void *)__get_free_pages(
1372 GFP_KERNEL|__GFP_ZERO,
1373 MPP_BUFFER_ORDER);
1374
Stewart Smithde9bdd12014-07-18 14:18:42 +10001375 return vcore;
1376}
1377
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301378static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1379 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001380{
1381 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001382 int err = -EINVAL;
1383 int core;
1384 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001385
Michael Ellerman3102f782014-05-23 18:15:29 +10001386 core = id / threads_per_subcore;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001387 if (core >= KVM_MAX_VCORES)
1388 goto out;
1389
1390 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001391 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001392 if (!vcpu)
1393 goto out;
1394
1395 err = kvm_vcpu_init(vcpu, kvm, id);
1396 if (err)
1397 goto free_vcpu;
1398
1399 vcpu->arch.shared = &vcpu->arch.shregs;
Alexander Graf5deb8e72014-04-24 13:46:24 +02001400#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1401 /*
1402 * The shared struct is never shared on HV,
1403 * so we can always use host endianness
1404 */
1405#ifdef __BIG_ENDIAN__
1406 vcpu->arch.shared_big_endian = true;
1407#else
1408 vcpu->arch.shared_big_endian = false;
1409#endif
1410#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001411 vcpu->arch.mmcr[0] = MMCR0_FC;
1412 vcpu->arch.ctrl = CTRL_RUNLATCH;
1413 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301414 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001415 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001416 spin_lock_init(&vcpu->arch.tbacct_lock);
1417 vcpu->arch.busy_preempt = TB_NIL;
Anton Blanchardd6829162014-01-08 21:25:30 +11001418 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001419
Paul Mackerrasde56a942011-06-29 00:21:34 +00001420 kvmppc_mmu_book3s_hv_init(vcpu);
1421
Paul Mackerras8455d792012-10-15 01:17:42 +00001422 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001423
1424 init_waitqueue_head(&vcpu->arch.cpu_run);
1425
1426 mutex_lock(&kvm->lock);
1427 vcore = kvm->arch.vcores[core];
1428 if (!vcore) {
Stewart Smithde9bdd12014-07-18 14:18:42 +10001429 vcore = kvmppc_vcore_create(kvm, core);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001430 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001431 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001432 }
1433 mutex_unlock(&kvm->lock);
1434
1435 if (!vcore)
1436 goto free_vcpu;
1437
1438 spin_lock(&vcore->lock);
1439 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001440 spin_unlock(&vcore->lock);
1441 vcpu->arch.vcore = vcore;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001442 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001443
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001444 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1445 kvmppc_sanity_check(vcpu);
1446
Paul Mackerrasde56a942011-06-29 00:21:34 +00001447 return vcpu;
1448
1449free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001450 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001451out:
1452 return ERR_PTR(err);
1453}
1454
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001455static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1456{
1457 if (vpa->pinned_addr)
1458 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1459 vpa->dirty);
1460}
1461
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301462static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001463{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001464 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001465 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1466 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1467 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001468 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001469 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001470 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001471}
1472
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301473static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1474{
1475 /* Indicate we want to get back into the guest */
1476 return 1;
1477}
1478
Paul Mackerras19ccb762011-07-23 17:42:46 +10001479static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001480{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001481 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001482
Paul Mackerras19ccb762011-07-23 17:42:46 +10001483 now = get_tb();
1484 if (now > vcpu->arch.dec_expires) {
1485 /* decrementer has already gone negative */
1486 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001487 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001488 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001489 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001490 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1491 / tb_ticks_per_sec;
1492 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1493 HRTIMER_MODE_REL);
1494 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001495}
1496
Paul Mackerras19ccb762011-07-23 17:42:46 +10001497static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001498{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001499 vcpu->arch.ceded = 0;
1500 if (vcpu->arch.timer_running) {
1501 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1502 vcpu->arch.timer_running = 0;
1503 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001504}
1505
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001506extern void __kvmppc_vcore_entry(void);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001507
1508static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1509 struct kvm_vcpu *vcpu)
1510{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001511 u64 now;
1512
Paul Mackerras371fefd2011-06-29 00:23:08 +00001513 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1514 return;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001515 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001516 now = mftb();
1517 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1518 vcpu->arch.stolen_logged;
1519 vcpu->arch.busy_preempt = now;
1520 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001521 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001522 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001523 list_del(&vcpu->arch.run_list);
1524}
1525
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001526static int kvmppc_grab_hwthread(int cpu)
1527{
1528 struct paca_struct *tpaca;
Paul Mackerrasb754c732014-09-02 16:14:42 +10001529 long timeout = 10000;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001530
1531 tpaca = &paca[cpu];
1532
1533 /* Ensure the thread won't go into the kernel if it wakes */
1534 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001535 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001536
1537 /*
1538 * If the thread is already executing in the kernel (e.g. handling
1539 * a stray interrupt), wait for it to get back to nap mode.
1540 * The smp_mb() is to ensure that our setting of hwthread_req
1541 * is visible before we look at hwthread_state, so if this
1542 * races with the code at system_reset_pSeries and the thread
1543 * misses our setting of hwthread_req, we are sure to see its
1544 * setting of hwthread_state, and vice versa.
1545 */
1546 smp_mb();
1547 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1548 if (--timeout <= 0) {
1549 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1550 return -EBUSY;
1551 }
1552 udelay(1);
1553 }
1554 return 0;
1555}
1556
1557static void kvmppc_release_hwthread(int cpu)
1558{
1559 struct paca_struct *tpaca;
1560
1561 tpaca = &paca[cpu];
1562 tpaca->kvm_hstate.hwthread_req = 0;
1563 tpaca->kvm_hstate.kvm_vcpu = NULL;
1564}
1565
Paul Mackerras371fefd2011-06-29 00:23:08 +00001566static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1567{
1568 int cpu;
1569 struct paca_struct *tpaca;
1570 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1571
Paul Mackerras19ccb762011-07-23 17:42:46 +10001572 if (vcpu->arch.timer_running) {
1573 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1574 vcpu->arch.timer_running = 0;
1575 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001576 cpu = vc->pcpu + vcpu->arch.ptid;
1577 tpaca = &paca[cpu];
1578 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1579 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001580 tpaca->kvm_hstate.ptid = vcpu->arch.ptid;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001581 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001582 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +00001583#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001584 if (cpu != smp_processor_id()) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001585 xics_wake_cpu(cpu);
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001586 if (vcpu->arch.ptid)
1587 ++vc->n_woken;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001588 }
1589#endif
1590}
1591
1592static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1593{
1594 int i;
1595
1596 HMT_low();
1597 i = 0;
1598 while (vc->nap_count < vc->n_woken) {
1599 if (++i >= 1000000) {
1600 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1601 vc->nap_count, vc->n_woken);
1602 break;
1603 }
1604 cpu_relax();
1605 }
1606 HMT_medium();
1607}
1608
1609/*
1610 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001611 * this core are off-line. Then grab the threads so they can't
1612 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001613 */
1614static int on_primary_thread(void)
1615{
1616 int cpu = smp_processor_id();
Michael Ellerman3102f782014-05-23 18:15:29 +10001617 int thr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001618
Michael Ellerman3102f782014-05-23 18:15:29 +10001619 /* Are we on a primary subcore? */
1620 if (cpu_thread_in_subcore(cpu))
Paul Mackerras371fefd2011-06-29 00:23:08 +00001621 return 0;
Michael Ellerman3102f782014-05-23 18:15:29 +10001622
1623 thr = 0;
1624 while (++thr < threads_per_subcore)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001625 if (cpu_online(cpu + thr))
1626 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001627
1628 /* Grab all hw threads so they can't go into the kernel */
Michael Ellerman3102f782014-05-23 18:15:29 +10001629 for (thr = 1; thr < threads_per_subcore; ++thr) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00001630 if (kvmppc_grab_hwthread(cpu + thr)) {
1631 /* Couldn't grab one; let the others go */
1632 do {
1633 kvmppc_release_hwthread(cpu + thr);
1634 } while (--thr > 0);
1635 return 0;
1636 }
1637 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001638 return 1;
1639}
1640
Stewart Smith9678cda2014-07-18 14:18:43 +10001641static void kvmppc_start_saving_l2_cache(struct kvmppc_vcore *vc)
1642{
1643 phys_addr_t phy_addr, mpp_addr;
1644
1645 phy_addr = (phys_addr_t)virt_to_phys(vc->mpp_buffer);
1646 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1647
1648 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_ABORT);
1649 logmpp(mpp_addr | PPC_LOGMPP_LOG_L2);
1650
1651 vc->mpp_buffer_is_valid = true;
1652}
1653
1654static void kvmppc_start_restoring_l2_cache(const struct kvmppc_vcore *vc)
1655{
1656 phys_addr_t phy_addr, mpp_addr;
1657
1658 phy_addr = virt_to_phys(vc->mpp_buffer);
1659 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1660
1661 /* We must abort any in-progress save operations to ensure
1662 * the table is valid so that prefetch engine knows when to
1663 * stop prefetching. */
1664 logmpp(mpp_addr | PPC_LOGMPP_LOG_ABORT);
1665 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_WHOLE_TABLE);
1666}
1667
Paul Mackerras371fefd2011-06-29 00:23:08 +00001668/*
1669 * Run a set of guest threads on a physical core.
1670 * Called with vc->lock held.
1671 */
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001672static void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001673{
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001674 struct kvm_vcpu *vcpu, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001675 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001676 u64 now;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001677 int i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001678 int srcu_idx;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001679 struct kvm_vcpu *vcpus_to_update[threads_per_core];
Paul Mackerrasde56a942011-06-29 00:21:34 +00001680
Paul Mackerras371fefd2011-06-29 00:23:08 +00001681 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +10001682 need_vpa_update = 0;
1683 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001684 if (signal_pending(vcpu->arch.run_task))
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001685 return;
1686 if (vcpu->arch.vpa.update_pending ||
1687 vcpu->arch.slb_shadow.update_pending ||
1688 vcpu->arch.dtl.update_pending)
1689 vcpus_to_update[need_vpa_update++] = vcpu;
Paul Mackerras081f3232012-06-01 20:20:24 +10001690 }
1691
1692 /*
1693 * Initialize *vc, in particular vc->vcore_state, so we can
1694 * drop the vcore lock if necessary.
1695 */
1696 vc->n_woken = 0;
1697 vc->nap_count = 0;
1698 vc->entry_exit_count = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001699 vc->vcore_state = VCORE_STARTING;
Paul Mackerras081f3232012-06-01 20:20:24 +10001700 vc->in_guest = 0;
1701 vc->napping_threads = 0;
1702
1703 /*
1704 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1705 * which can't be called with any spinlocks held.
1706 */
1707 if (need_vpa_update) {
1708 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001709 for (i = 0; i < need_vpa_update; ++i)
1710 kvmppc_update_vpas(vcpus_to_update[i]);
Paul Mackerras081f3232012-06-01 20:20:24 +10001711 spin_lock(&vc->lock);
1712 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001713
1714 /*
Michael Ellerman3102f782014-05-23 18:15:29 +10001715 * Make sure we are running on primary threads, and that secondary
1716 * threads are offline. Also check if the number of threads in this
1717 * guest are greater than the current system threads per guest.
Paul Mackerras7b444c62012-10-15 01:16:14 +00001718 */
Michael Ellerman3102f782014-05-23 18:15:29 +10001719 if ((threads_per_core > 1) &&
1720 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00001721 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1722 vcpu->arch.ret = -EBUSY;
1723 goto out;
1724 }
1725
Michael Ellerman3102f782014-05-23 18:15:29 +10001726
Paul Mackerras371fefd2011-06-29 00:23:08 +00001727 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001728 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001729 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001730 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001731 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001732
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001733 /* Set this explicitly in case thread 0 doesn't have a vcpu */
1734 get_paca()->kvm_hstate.kvm_vcore = vc;
1735 get_paca()->kvm_hstate.ptid = 0;
1736
Paul Mackerras2f12f032012-10-15 01:17:17 +00001737 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001738 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001739 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001740
Paul Mackerras19ccb762011-07-23 17:42:46 +10001741 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001742
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001743 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001744
Stewart Smith9678cda2014-07-18 14:18:43 +10001745 if (vc->mpp_buffer_is_valid)
1746 kvmppc_start_restoring_l2_cache(vc);
1747
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001748 __kvmppc_vcore_entry();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001749
Paul Mackerras371fefd2011-06-29 00:23:08 +00001750 spin_lock(&vc->lock);
Stewart Smith9678cda2014-07-18 14:18:43 +10001751
1752 if (vc->mpp_buffer)
1753 kvmppc_start_saving_l2_cache(vc);
1754
Paul Mackerras19ccb762011-07-23 17:42:46 +10001755 /* disable sending of IPIs on virtual external irqs */
1756 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1757 vcpu->cpu = -1;
1758 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001759 if (vc->nap_count < vc->n_woken)
1760 kvmppc_wait_for_nap(vc);
Michael Ellerman3102f782014-05-23 18:15:29 +10001761 for (i = 0; i < threads_per_subcore; ++i)
Paul Mackerras2f12f032012-10-15 01:17:17 +00001762 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001763 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001764 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001765 spin_unlock(&vc->lock);
1766
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001767 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001768
Paul Mackerras371fefd2011-06-29 00:23:08 +00001769 /* make sure updates to secondary vcpu structs are visible now */
1770 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001771 kvm_guest_exit();
1772
1773 preempt_enable();
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09001774 cond_resched();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001775
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001776 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001777 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001778 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1779 /* cancel pending dec exception if dec is positive */
1780 if (now < vcpu->arch.dec_expires &&
1781 kvmppc_core_pending_dec(vcpu))
1782 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001783
1784 ret = RESUME_GUEST;
1785 if (vcpu->arch.trap)
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301786 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1787 vcpu->arch.run_task);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001788
Paul Mackerras371fefd2011-06-29 00:23:08 +00001789 vcpu->arch.ret = ret;
1790 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001791
1792 if (vcpu->arch.ceded) {
Greg Kurze59d24e2014-02-06 17:36:56 +01001793 if (!is_kvmppc_resume_guest(ret))
Paul Mackerras19ccb762011-07-23 17:42:46 +10001794 kvmppc_end_cede(vcpu);
1795 else
1796 kvmppc_set_timer(vcpu);
1797 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001798 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001799
Paul Mackerrasde56a942011-06-29 00:21:34 +00001800 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001801 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001802 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1803 arch.run_list) {
Greg Kurze59d24e2014-02-06 17:36:56 +01001804 if (!is_kvmppc_resume_guest(vcpu->arch.ret)) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001805 kvmppc_remove_runnable(vc, vcpu);
1806 wake_up(&vcpu->arch.cpu_run);
1807 }
1808 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001809}
1810
Paul Mackerras19ccb762011-07-23 17:42:46 +10001811/*
1812 * Wait for some other vcpu thread to execute us, and
1813 * wake us up when we need to handle something in the host.
1814 */
1815static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001816{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001817 DEFINE_WAIT(wait);
1818
Paul Mackerras19ccb762011-07-23 17:42:46 +10001819 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1820 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1821 schedule();
1822 finish_wait(&vcpu->arch.cpu_run, &wait);
1823}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001824
Paul Mackerras19ccb762011-07-23 17:42:46 +10001825/*
1826 * All the vcpus in this vcore are idle, so wait for a decrementer
1827 * or external interrupt to one of the vcpus. vc->lock is held.
1828 */
1829static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1830{
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11001831 struct kvm_vcpu *vcpu;
1832 int do_sleep = 1;
1833
Paul Mackerras19ccb762011-07-23 17:42:46 +10001834 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001835
1836 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11001837
1838 /*
1839 * Check one last time for pending exceptions and ceded state after
1840 * we put ourselves on the wait queue
1841 */
1842 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1843 if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded) {
1844 do_sleep = 0;
1845 break;
1846 }
1847 }
1848
1849 if (!do_sleep) {
1850 finish_wait(&vc->wq, &wait);
1851 return;
1852 }
1853
Paul Mackerras19ccb762011-07-23 17:42:46 +10001854 vc->vcore_state = VCORE_SLEEPING;
1855 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001856 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001857 finish_wait(&vc->wq, &wait);
1858 spin_lock(&vc->lock);
1859 vc->vcore_state = VCORE_INACTIVE;
1860}
1861
1862static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1863{
1864 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001865 struct kvmppc_vcore *vc;
1866 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001867
Paul Mackerras371fefd2011-06-29 00:23:08 +00001868 kvm_run->exit_reason = 0;
1869 vcpu->arch.ret = RESUME_GUEST;
1870 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001871 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001872
Paul Mackerras371fefd2011-06-29 00:23:08 +00001873 /*
1874 * Synchronize with other threads in this virtual core
1875 */
1876 vc = vcpu->arch.vcore;
1877 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001878 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001879 vcpu->arch.run_task = current;
1880 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001881 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10001882 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001883 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001884 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1885 ++vc->n_runnable;
1886
Paul Mackerras19ccb762011-07-23 17:42:46 +10001887 /*
1888 * This happens the first time this is called for a vcpu.
1889 * If the vcore is already running, we may be able to start
1890 * this thread straight away and have it join in.
1891 */
Paul Mackerras8455d792012-10-15 01:17:42 +00001892 if (!signal_pending(current)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001893 if (vc->vcore_state == VCORE_RUNNING &&
1894 VCORE_EXIT_COUNT(vc) == 0) {
Paul Mackerras2f12f032012-10-15 01:17:17 +00001895 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001896 kvmppc_start_thread(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00001897 } else if (vc->vcore_state == VCORE_SLEEPING) {
1898 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001899 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001900
Paul Mackerras8455d792012-10-15 01:17:42 +00001901 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001902
1903 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1904 !signal_pending(current)) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001905 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001906 spin_unlock(&vc->lock);
1907 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1908 spin_lock(&vc->lock);
1909 continue;
1910 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001911 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1912 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001913 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001914 if (signal_pending(v->arch.run_task)) {
1915 kvmppc_remove_runnable(vc, v);
1916 v->stat.signal_exits++;
1917 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1918 v->arch.ret = -EINTR;
1919 wake_up(&v->arch.cpu_run);
1920 }
1921 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001922 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1923 break;
1924 vc->runner = vcpu;
1925 n_ceded = 0;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001926 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001927 if (!v->arch.pending_exceptions)
1928 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001929 else
1930 v->arch.ceded = 0;
1931 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001932 if (n_ceded == vc->n_runnable)
1933 kvmppc_vcore_blocked(vc);
1934 else
1935 kvmppc_run_core(vc);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001936 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001937 }
1938
Paul Mackerras8455d792012-10-15 01:17:42 +00001939 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1940 (vc->vcore_state == VCORE_RUNNING ||
1941 vc->vcore_state == VCORE_EXITING)) {
1942 spin_unlock(&vc->lock);
1943 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1944 spin_lock(&vc->lock);
1945 }
1946
1947 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1948 kvmppc_remove_runnable(vc, vcpu);
1949 vcpu->stat.signal_exits++;
1950 kvm_run->exit_reason = KVM_EXIT_INTR;
1951 vcpu->arch.ret = -EINTR;
1952 }
1953
1954 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1955 /* Wake up some vcpu to run the core */
1956 v = list_first_entry(&vc->runnable_threads,
1957 struct kvm_vcpu, arch.run_list);
1958 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001959 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001960
Paul Mackerras19ccb762011-07-23 17:42:46 +10001961 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001962 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001963}
1964
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301965static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001966{
1967 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001968 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001969
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001970 if (!vcpu->arch.sane) {
1971 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1972 return -EINVAL;
1973 }
1974
Scott Wood25051b52011-11-08 18:23:23 -06001975 kvmppc_core_prepare_to_enter(vcpu);
1976
Paul Mackerras19ccb762011-07-23 17:42:46 +10001977 /* No need to go into the guest when all we'll do is come back out */
1978 if (signal_pending(current)) {
1979 run->exit_reason = KVM_EXIT_INTR;
1980 return -EINTR;
1981 }
1982
Paul Mackerras32fad282012-05-04 02:32:53 +00001983 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1984 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1985 smp_mb();
1986
1987 /* On the first time here, set up HTAB and VRMA or RMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001988 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001989 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001990 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001991 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001992 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001993
1994 flush_fp_to_thread(current);
1995 flush_altivec_to_thread(current);
1996 flush_vsx_to_thread(current);
1997 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001998 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001999 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10002000
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002001 do {
2002 r = kvmppc_run_vcpu(run, vcpu);
2003
2004 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
2005 !(vcpu->arch.shregs.msr & MSR_PR)) {
2006 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06002007 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002008 } else if (r == RESUME_PAGE_FAULT) {
2009 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2010 r = kvmppc_book3s_hv_page_fault(run, vcpu,
2011 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
2012 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002013 }
Greg Kurze59d24e2014-02-06 17:36:56 +01002014 } while (is_kvmppc_resume_guest(r));
Paul Mackerras32fad282012-05-04 02:32:53 +00002015
2016 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002017 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00002018 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002019 return r;
2020}
2021
David Gibson54738c02011-06-29 00:22:41 +00002022
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002023/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +00002024 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002025static inline int lpcr_rmls(unsigned long rma_size)
2026{
2027 switch (rma_size) {
2028 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +00002029 if (cpu_has_feature(CPU_FTR_ARCH_206))
2030 return 8; /* only supported on POWER7 */
2031 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002032 case 64ul << 20: /* 64 MB */
2033 return 3;
2034 case 128ul << 20: /* 128 MB */
2035 return 7;
2036 case 256ul << 20: /* 256 MB */
2037 return 4;
2038 case 1ul << 30: /* 1 GB */
2039 return 2;
2040 case 16ul << 30: /* 16 GB */
2041 return 1;
2042 case 256ul << 30: /* 256 GB */
2043 return 0;
2044 default:
2045 return -1;
2046 }
2047}
2048
2049static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2050{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002051 struct page *page;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302052 struct kvm_rma_info *ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002053
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302054 if (vmf->pgoff >= kvm_rma_pages)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002055 return VM_FAULT_SIGBUS;
2056
2057 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
2058 get_page(page);
2059 vmf->page = page;
2060 return 0;
2061}
2062
2063static const struct vm_operations_struct kvm_rma_vm_ops = {
2064 .fault = kvm_rma_fault,
2065};
2066
2067static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
2068{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07002069 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002070 vma->vm_ops = &kvm_rma_vm_ops;
2071 return 0;
2072}
2073
2074static int kvm_rma_release(struct inode *inode, struct file *filp)
2075{
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302076 struct kvm_rma_info *ri = filp->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002077
2078 kvm_release_rma(ri);
2079 return 0;
2080}
2081
Al Viro75ef9de2013-04-04 19:09:41 -04002082static const struct file_operations kvm_rma_fops = {
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002083 .mmap = kvm_rma_mmap,
2084 .release = kvm_rma_release,
2085};
2086
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302087static long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
2088 struct kvm_allocate_rma *ret)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002089{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002090 long fd;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302091 struct kvm_rma_info *ri;
2092 /*
2093 * Only do this on PPC970 in HV mode
2094 */
2095 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
2096 !cpu_has_feature(CPU_FTR_ARCH_201))
2097 return -EINVAL;
2098
2099 if (!kvm_rma_pages)
2100 return -EINVAL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002101
2102 ri = kvm_alloc_rma();
2103 if (!ri)
2104 return -ENOMEM;
2105
Yann Droneaud2f84d5e2013-08-24 22:14:08 +02002106 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002107 if (fd < 0)
2108 kvm_release_rma(ri);
2109
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302110 ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002111 return fd;
2112}
2113
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002114static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2115 int linux_psize)
2116{
2117 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2118
2119 if (!def->shift)
2120 return;
2121 (*sps)->page_shift = def->shift;
2122 (*sps)->slb_enc = def->sllp;
2123 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00002124 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05302125 /*
2126 * Add 16MB MPSS support if host supports it
2127 */
2128 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2129 (*sps)->enc[1].page_shift = 24;
2130 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2131 }
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002132 (*sps)++;
2133}
2134
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302135static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2136 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002137{
2138 struct kvm_ppc_one_seg_page_size *sps;
2139
2140 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2141 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2142 info->flags |= KVM_PPC_1T_SEGMENTS;
2143 info->slb_size = mmu_slb_size;
2144
2145 /* We only support these sizes for now, and no muti-size segments */
2146 sps = &info->sps[0];
2147 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2148 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2149 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2150
2151 return 0;
2152}
2153
Paul Mackerras82ed3612011-12-15 02:03:22 +00002154/*
2155 * Get (and clear) the dirty memory log for a memory slot.
2156 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302157static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2158 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002159{
2160 struct kvm_memory_slot *memslot;
2161 int r;
2162 unsigned long n;
2163
2164 mutex_lock(&kvm->slots_lock);
2165
2166 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07002167 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002168 goto out;
2169
2170 memslot = id_to_memslot(kvm->memslots, log->slot);
2171 r = -ENOENT;
2172 if (!memslot->dirty_bitmap)
2173 goto out;
2174
2175 n = kvm_dirty_bitmap_bytes(memslot);
2176 memset(memslot->dirty_bitmap, 0, n);
2177
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002178 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00002179 if (r)
2180 goto out;
2181
2182 r = -EFAULT;
2183 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2184 goto out;
2185
2186 r = 0;
2187out:
2188 mutex_unlock(&kvm->slots_lock);
2189 return r;
2190}
2191
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002192static void unpin_slot(struct kvm_memory_slot *memslot)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002193{
2194 unsigned long *physp;
2195 unsigned long j, npages, pfn;
2196 struct page *page;
2197
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002198 physp = memslot->arch.slot_phys;
2199 npages = memslot->npages;
2200 if (!physp)
2201 return;
2202 for (j = 0; j < npages; j++) {
2203 if (!(physp[j] & KVMPPC_GOT_PAGE))
2204 continue;
2205 pfn = physp[j] >> PAGE_SHIFT;
2206 page = pfn_to_page(pfn);
2207 SetPageDirty(page);
2208 put_page(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002209 }
2210}
2211
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302212static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2213 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002214{
2215 if (!dont || free->arch.rmap != dont->arch.rmap) {
2216 vfree(free->arch.rmap);
2217 free->arch.rmap = NULL;
2218 }
2219 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
2220 unpin_slot(free);
2221 vfree(free->arch.slot_phys);
2222 free->arch.slot_phys = NULL;
2223 }
2224}
2225
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302226static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
2227 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002228{
2229 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
2230 if (!slot->arch.rmap)
2231 return -ENOMEM;
2232 slot->arch.slot_phys = NULL;
2233
2234 return 0;
2235}
2236
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302237static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
2238 struct kvm_memory_slot *memslot,
2239 struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002240{
2241 unsigned long *phys;
2242
2243 /* Allocate a slot_phys array if needed */
2244 phys = memslot->arch.slot_phys;
2245 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
2246 phys = vzalloc(memslot->npages * sizeof(unsigned long));
2247 if (!phys)
2248 return -ENOMEM;
2249 memslot->arch.slot_phys = phys;
2250 }
2251
2252 return 0;
2253}
2254
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302255static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
2256 struct kvm_userspace_memory_region *mem,
2257 const struct kvm_memory_slot *old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002258{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002259 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
2260 struct kvm_memory_slot *memslot;
2261
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002262 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002263 /*
2264 * If modifying a memslot, reset all the rmap dirty bits.
2265 * If this is a new memslot, we don't need to do anything
2266 * since the rmap array starts out as all zeroes,
2267 * i.e. no pages are dirty.
2268 */
2269 memslot = id_to_memslot(kvm->memslots, mem->slot);
2270 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
2271 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002272}
2273
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002274/*
2275 * Update LPCR values in kvm->arch and in vcores.
2276 * Caller must hold kvm->lock.
2277 */
2278void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
2279{
2280 long int i;
2281 u32 cores_done = 0;
2282
2283 if ((kvm->arch.lpcr & mask) == lpcr)
2284 return;
2285
2286 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2287
2288 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2289 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2290 if (!vc)
2291 continue;
2292 spin_lock(&vc->lock);
2293 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2294 spin_unlock(&vc->lock);
2295 if (++cores_done >= kvm->arch.online_vcores)
2296 break;
2297 }
2298}
2299
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302300static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2301{
2302 return;
2303}
2304
Paul Mackerras32fad282012-05-04 02:32:53 +00002305static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002306{
2307 int err = 0;
2308 struct kvm *kvm = vcpu->kvm;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302309 struct kvm_rma_info *ri = NULL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002310 unsigned long hva;
2311 struct kvm_memory_slot *memslot;
2312 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002313 unsigned long lpcr = 0, senc;
2314 unsigned long lpcr_mask = 0;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002315 unsigned long psize, porder;
2316 unsigned long rma_size;
2317 unsigned long rmls;
2318 unsigned long *physp;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002319 unsigned long i, npages;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002320 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002321
2322 mutex_lock(&kvm->lock);
2323 if (kvm->arch.rma_setup_done)
2324 goto out; /* another vcpu beat us to it */
2325
Paul Mackerras32fad282012-05-04 02:32:53 +00002326 /* Allocate hashed page table (if not done already) and reset it */
2327 if (!kvm->arch.hpt_virt) {
2328 err = kvmppc_alloc_hpt(kvm, NULL);
2329 if (err) {
2330 pr_err("KVM: Couldn't alloc HPT\n");
2331 goto out;
2332 }
2333 }
2334
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002335 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002336 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002337 memslot = gfn_to_memslot(kvm, 0);
2338
2339 /* We must have some memory at 0 by now */
2340 err = -EINVAL;
2341 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002342 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002343
2344 /* Look up the VMA for the start of this memory slot */
2345 hva = memslot->userspace_addr;
2346 down_read(&current->mm->mmap_sem);
2347 vma = find_vma(current->mm, hva);
2348 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2349 goto up_out;
2350
2351 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002352 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002353
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002354 /* Is this one of our preallocated RMAs? */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002355 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
2356 hva == vma->vm_start)
2357 ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002358
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002359 up_read(&current->mm->mmap_sem);
2360
2361 if (!ri) {
2362 /* On POWER7, use VRMA; on PPC970, give up */
2363 err = -EPERM;
2364 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2365 pr_err("KVM: CPU requires an RMO\n");
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002366 goto out_srcu;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002367 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002368
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002369 /* We can handle 4k, 64k or 16M pages in the VRMA */
2370 err = -EINVAL;
2371 if (!(psize == 0x1000 || psize == 0x10000 ||
2372 psize == 0x1000000))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002373 goto out_srcu;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002374
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002375 /* Update VRMASD field in the LPCR */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002376 senc = slb_pgsize_encoding(psize);
Paul Mackerras697d3892011-12-12 12:36:37 +00002377 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2378 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002379 lpcr_mask = LPCR_VRMASD;
2380 /* the -4 is to account for senc values starting at 0x10 */
2381 lpcr = senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002382
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002383 /* Create HPTEs in the hash page table for the VRMA */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002384 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002385
2386 } else {
2387 /* Set up to use an RMO region */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302388 rma_size = kvm_rma_pages;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002389 if (rma_size > memslot->npages)
2390 rma_size = memslot->npages;
2391 rma_size <<= PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002392 rmls = lpcr_rmls(rma_size);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002393 err = -EINVAL;
Chen Gang5d226ae2013-07-22 14:32:35 +08002394 if ((long)rmls < 0) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002395 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002396 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002397 }
2398 atomic_inc(&ri->use_count);
2399 kvm->arch.rma = ri;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002400
2401 /* Update LPCR and RMOR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00002402 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2403 /* PPC970; insert RMLS value (split field) in HID4 */
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002404 lpcr_mask = (1ul << HID4_RMLS0_SH) |
2405 (3ul << HID4_RMLS2_SH) | HID4_RMOR;
2406 lpcr = ((rmls >> 2) << HID4_RMLS0_SH) |
Paul Mackerras9e368f22011-06-29 00:40:08 +00002407 ((rmls & 3) << HID4_RMLS2_SH);
2408 /* RMOR is also in HID4 */
2409 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
2410 << HID4_RMOR_SH;
2411 } else {
2412 /* POWER7 */
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002413 lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS;
2414 lpcr = rmls << LPCR_RMLS_SH;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302415 kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002416 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002417 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002418 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002419
2420 /* Initialize phys addrs of pages in RMO */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302421 npages = kvm_rma_pages;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002422 porder = __ilog2(npages);
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002423 physp = memslot->arch.slot_phys;
2424 if (physp) {
2425 if (npages > memslot->npages)
2426 npages = memslot->npages;
2427 spin_lock(&kvm->arch.slot_phys_lock);
2428 for (i = 0; i < npages; ++i)
2429 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
2430 porder;
2431 spin_unlock(&kvm->arch.slot_phys_lock);
2432 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002433 }
2434
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002435 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
2436
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002437 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
2438 smp_wmb();
2439 kvm->arch.rma_setup_done = 1;
2440 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002441 out_srcu:
2442 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002443 out:
2444 mutex_unlock(&kvm->lock);
2445 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002446
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002447 up_out:
2448 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08002449 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002450}
2451
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302452static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002453{
Paul Mackerras32fad282012-05-04 02:32:53 +00002454 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002455
Paul Mackerras32fad282012-05-04 02:32:53 +00002456 /* Allocate the guest's logical partition ID */
2457
2458 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08002459 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00002460 return -ENOMEM;
2461 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002462
Paul Mackerras1b400ba2012-11-21 23:28:08 +00002463 /*
2464 * Since we don't flush the TLB when tearing down a VM,
2465 * and this lpid might have previously been used,
2466 * make sure we flush on each core before running the new VM.
2467 */
2468 cpumask_setall(&kvm->arch.need_tlb_flush);
2469
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002470 /* Start out with the default set of hcalls enabled */
2471 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
2472 sizeof(kvm->arch.enabled_hcalls));
2473
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002474 kvm->arch.rma = NULL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002475
Paul Mackerras9e368f22011-06-29 00:40:08 +00002476 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002477
Paul Mackerras9e368f22011-06-29 00:40:08 +00002478 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2479 /* PPC970; HID4 is effectively the LPCR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00002480 kvm->arch.host_lpid = 0;
2481 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
2482 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
2483 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
2484 ((lpid & 0xf) << HID4_LPID5_SH);
2485 } else {
2486 /* POWER7; init LPCR for virtual RMA mode */
2487 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2488 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2489 lpcr &= LPCR_PECE | LPCR_LPES;
2490 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
Paul Mackerras697d3892011-12-12 12:36:37 +00002491 LPCR_VPM0 | LPCR_VPM1;
2492 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2493 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrase0622bd2014-01-08 21:25:27 +11002494 /* On POWER8 turn on online bit to enable PURR/SPURR */
2495 if (cpu_has_feature(CPU_FTR_ARCH_207S))
2496 lpcr |= LPCR_ONL;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002497 }
2498 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002499
Paul Mackerras342d3db2011-12-12 12:38:05 +00002500 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002501 spin_lock_init(&kvm->arch.slot_phys_lock);
Paul Mackerras512691d2012-10-15 01:15:41 +00002502
2503 /*
Michael Ellerman441c19c2014-05-23 18:15:25 +10002504 * Track that we now have a HV mode VM active. This blocks secondary
2505 * CPU threads from coming online.
Paul Mackerras512691d2012-10-15 01:15:41 +00002506 */
Michael Ellerman441c19c2014-05-23 18:15:25 +10002507 kvm_hv_vm_activated();
Paul Mackerras512691d2012-10-15 01:15:41 +00002508
David Gibson54738c02011-06-29 00:22:41 +00002509 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002510}
2511
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302512static void kvmppc_free_vcores(struct kvm *kvm)
2513{
2514 long int i;
2515
Stewart Smith9678cda2014-07-18 14:18:43 +10002516 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2517 if (kvm->arch.vcores[i] && kvm->arch.vcores[i]->mpp_buffer) {
2518 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2519 free_pages((unsigned long)vc->mpp_buffer,
2520 MPP_BUFFER_ORDER);
2521 }
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302522 kfree(kvm->arch.vcores[i]);
Stewart Smith9678cda2014-07-18 14:18:43 +10002523 }
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302524 kvm->arch.online_vcores = 0;
2525}
2526
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302527static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002528{
Michael Ellerman441c19c2014-05-23 18:15:25 +10002529 kvm_hv_vm_deactivated();
Paul Mackerras512691d2012-10-15 01:15:41 +00002530
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302531 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002532 if (kvm->arch.rma) {
2533 kvm_release_rma(kvm->arch.rma);
2534 kvm->arch.rma = NULL;
2535 }
2536
Paul Mackerrasde56a942011-06-29 00:21:34 +00002537 kvmppc_free_hpt(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002538}
2539
2540/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302541static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2542 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002543{
2544 return EMULATE_FAIL;
2545}
2546
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302547static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2548 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002549{
2550 return EMULATE_FAIL;
2551}
2552
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302553static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2554 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002555{
2556 return EMULATE_FAIL;
2557}
2558
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302559static int kvmppc_core_check_processor_compat_hv(void)
2560{
2561 if (!cpu_has_feature(CPU_FTR_HVMODE))
2562 return -EIO;
2563 return 0;
2564}
2565
2566static long kvm_arch_vm_ioctl_hv(struct file *filp,
2567 unsigned int ioctl, unsigned long arg)
2568{
2569 struct kvm *kvm __maybe_unused = filp->private_data;
2570 void __user *argp = (void __user *)arg;
2571 long r;
2572
2573 switch (ioctl) {
2574
2575 case KVM_ALLOCATE_RMA: {
2576 struct kvm_allocate_rma rma;
2577 struct kvm *kvm = filp->private_data;
2578
2579 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
2580 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
2581 r = -EFAULT;
2582 break;
2583 }
2584
2585 case KVM_PPC_ALLOCATE_HTAB: {
2586 u32 htab_order;
2587
2588 r = -EFAULT;
2589 if (get_user(htab_order, (u32 __user *)argp))
2590 break;
2591 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2592 if (r)
2593 break;
2594 r = -EFAULT;
2595 if (put_user(htab_order, (u32 __user *)argp))
2596 break;
2597 r = 0;
2598 break;
2599 }
2600
2601 case KVM_PPC_GET_HTAB_FD: {
2602 struct kvm_get_htab_fd ghf;
2603
2604 r = -EFAULT;
2605 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2606 break;
2607 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2608 break;
2609 }
2610
2611 default:
2612 r = -ENOTTY;
2613 }
2614
2615 return r;
2616}
2617
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002618/*
2619 * List of hcall numbers to enable by default.
2620 * For compatibility with old userspace, we enable by default
2621 * all hcalls that were implemented before the hcall-enabling
2622 * facility was added. Note this list should not include H_RTAS.
2623 */
2624static unsigned int default_hcall_list[] = {
2625 H_REMOVE,
2626 H_ENTER,
2627 H_READ,
2628 H_PROTECT,
2629 H_BULK_REMOVE,
2630 H_GET_TCE,
2631 H_PUT_TCE,
2632 H_SET_DABR,
2633 H_SET_XDABR,
2634 H_CEDE,
2635 H_PROD,
2636 H_CONFER,
2637 H_REGISTER_VPA,
2638#ifdef CONFIG_KVM_XICS
2639 H_EOI,
2640 H_CPPR,
2641 H_IPI,
2642 H_IPOLL,
2643 H_XIRR,
2644 H_XIRR_X,
2645#endif
2646 0
2647};
2648
2649static void init_default_hcalls(void)
2650{
2651 int i;
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002652 unsigned int hcall;
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002653
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002654 for (i = 0; default_hcall_list[i]; ++i) {
2655 hcall = default_hcall_list[i];
2656 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
2657 __set_bit(hcall / 4, default_enabled_hcalls);
2658 }
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002659}
2660
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302661static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302662 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2663 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2664 .get_one_reg = kvmppc_get_one_reg_hv,
2665 .set_one_reg = kvmppc_set_one_reg_hv,
2666 .vcpu_load = kvmppc_core_vcpu_load_hv,
2667 .vcpu_put = kvmppc_core_vcpu_put_hv,
2668 .set_msr = kvmppc_set_msr_hv,
2669 .vcpu_run = kvmppc_vcpu_run_hv,
2670 .vcpu_create = kvmppc_core_vcpu_create_hv,
2671 .vcpu_free = kvmppc_core_vcpu_free_hv,
2672 .check_requests = kvmppc_core_check_requests_hv,
2673 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2674 .flush_memslot = kvmppc_core_flush_memslot_hv,
2675 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2676 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2677 .unmap_hva = kvm_unmap_hva_hv,
2678 .unmap_hva_range = kvm_unmap_hva_range_hv,
2679 .age_hva = kvm_age_hva_hv,
2680 .test_age_hva = kvm_test_age_hva_hv,
2681 .set_spte_hva = kvm_set_spte_hva_hv,
2682 .mmu_destroy = kvmppc_mmu_destroy_hv,
2683 .free_memslot = kvmppc_core_free_memslot_hv,
2684 .create_memslot = kvmppc_core_create_memslot_hv,
2685 .init_vm = kvmppc_core_init_vm_hv,
2686 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302687 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2688 .emulate_op = kvmppc_core_emulate_op_hv,
2689 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2690 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2691 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2692 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002693 .hcall_implemented = kvmppc_hcall_impl_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302694};
2695
2696static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002697{
2698 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302699 /*
2700 * FIXME!! Do we need to check on all cpus ?
2701 */
2702 r = kvmppc_core_check_processor_compat_hv();
2703 if (r < 0)
Paul Mackerras739e2422014-03-25 10:47:05 +11002704 return -ENODEV;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002705
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302706 kvm_ops_hv.owner = THIS_MODULE;
2707 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002708
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002709 init_default_hcalls();
2710
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302711 r = kvmppc_mmu_hv_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002712 return r;
2713}
2714
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302715static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002716{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302717 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002718}
2719
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302720module_init(kvmppc_book3s_init_hv);
2721module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05302722MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01002723MODULE_ALIAS_MISCDEV(KVM_MINOR);
2724MODULE_ALIAS("devname:kvm");