blob: e63587d30b70819b15bb215ca858a7b722795a21 [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;
772 case BOOK3S_INTERRUPT_PERFMON:
773 r = RESUME_GUEST;
774 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000775 case BOOK3S_INTERRUPT_MACHINE_CHECK:
776 /*
777 * Deliver a machine check interrupt to the guest.
778 * We have to do this, even if the host has handled the
779 * machine check, because machine checks use SRR0/1 and
780 * the interrupt might have trashed guest state in them.
781 */
782 kvmppc_book3s_queue_irqprio(vcpu,
783 BOOK3S_INTERRUPT_MACHINE_CHECK);
784 r = RESUME_GUEST;
785 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000786 case BOOK3S_INTERRUPT_PROGRAM:
787 {
788 ulong flags;
789 /*
790 * Normally program interrupts are delivered directly
791 * to the guest by the hardware, but we can get here
792 * as a result of a hypervisor emulation interrupt
793 * (e40) getting turned into a 700 by BML RTAS.
794 */
795 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
796 kvmppc_core_queue_program(vcpu, flags);
797 r = RESUME_GUEST;
798 break;
799 }
800 case BOOK3S_INTERRUPT_SYSCALL:
801 {
802 /* hcall - punt to userspace */
803 int i;
804
Liu Ping Fan27025a62013-11-19 14:12:48 +0800805 /* hypercall with MSR_PR has already been handled in rmode,
806 * and never reaches here.
807 */
808
Paul Mackerrasde56a942011-06-29 00:21:34 +0000809 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
810 for (i = 0; i < 9; ++i)
811 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
812 run->exit_reason = KVM_EXIT_PAPR_HCALL;
813 vcpu->arch.hcall_needed = 1;
814 r = RESUME_HOST;
815 break;
816 }
817 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000818 * We get these next two if the guest accesses a page which it thinks
819 * it has mapped but which is not actually present, either because
820 * it is for an emulated I/O device or because the corresonding
821 * host page has been paged out. Any other HDSI/HISI interrupts
822 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000823 */
824 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000825 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000826 break;
827 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000828 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
829 vcpu->arch.fault_dsisr = 0;
830 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000831 break;
832 /*
833 * This occurs if the guest executes an illegal instruction.
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530834 * If the guest debug is disabled, generate a program interrupt
835 * to the guest. If guest debug is enabled, we need to check
836 * whether the instruction is a software breakpoint instruction.
837 * Accordingly return to Guest or Host.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000838 */
839 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530840 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
841 r = kvmppc_emulate_debug_inst(run, vcpu);
842 } else {
843 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
844 r = RESUME_GUEST;
845 }
Michael Ellermanbd3048b2014-01-08 21:25:23 +1100846 break;
847 /*
848 * This occurs if the guest (kernel or userspace), does something that
849 * is prohibited by HFSCR. We just generate a program interrupt to
850 * the guest.
851 */
852 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
853 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000854 r = RESUME_GUEST;
855 break;
856 default:
857 kvmppc_dump_regs(vcpu);
858 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
859 vcpu->arch.trap, kvmppc_get_pc(vcpu),
860 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +1000861 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000862 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000863 break;
864 }
865
Paul Mackerrasde56a942011-06-29 00:21:34 +0000866 return r;
867}
868
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530869static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
870 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000871{
872 int i;
873
Paul Mackerrasde56a942011-06-29 00:21:34 +0000874 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +0530875 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000876 for (i = 0; i < vcpu->arch.slb_max; i++) {
877 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
878 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
879 }
880
881 return 0;
882}
883
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530884static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
885 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000886{
887 int i, j;
888
Paul Mackerras9333e6c2014-09-02 16:14:43 +1000889 /* Only accept the same PVR as the host's, since we can't spoof it */
890 if (sregs->pvr != vcpu->arch.pvr)
891 return -EINVAL;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000892
893 j = 0;
894 for (i = 0; i < vcpu->arch.slb_nr; i++) {
895 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
896 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
897 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
898 ++j;
899 }
900 }
901 vcpu->arch.slb_max = j;
902
903 return 0;
904}
905
Alexey Kardashevskiya0840242014-07-19 17:59:34 +1000906static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
907 bool preserve_top32)
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000908{
909 struct kvmppc_vcore *vc = vcpu->arch.vcore;
910 u64 mask;
911
912 spin_lock(&vc->lock);
913 /*
Anton Blanchardd6829162014-01-08 21:25:30 +1100914 * If ILE (interrupt little-endian) has changed, update the
915 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
916 */
917 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
918 struct kvm *kvm = vcpu->kvm;
919 struct kvm_vcpu *vcpu;
920 int i;
921
922 mutex_lock(&kvm->lock);
923 kvm_for_each_vcpu(i, vcpu, kvm) {
924 if (vcpu->arch.vcore != vc)
925 continue;
926 if (new_lpcr & LPCR_ILE)
927 vcpu->arch.intr_msr |= MSR_LE;
928 else
929 vcpu->arch.intr_msr &= ~MSR_LE;
930 }
931 mutex_unlock(&kvm->lock);
932 }
933
934 /*
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000935 * Userspace can only modify DPFD (default prefetch depth),
936 * ILE (interrupt little-endian) and TC (translation control).
Paul Mackerrase0622bd2014-01-08 21:25:27 +1100937 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000938 */
939 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
Paul Mackerrase0622bd2014-01-08 21:25:27 +1100940 if (cpu_has_feature(CPU_FTR_ARCH_207S))
941 mask |= LPCR_AIL;
Alexey Kardashevskiya0840242014-07-19 17:59:34 +1000942
943 /* Broken 32-bit version of LPCR must not clear top bits */
944 if (preserve_top32)
945 mask &= 0xFFFFFFFF;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000946 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
947 spin_unlock(&vc->lock);
948}
949
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530950static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
951 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000952{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000953 int r = 0;
954 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +0000955
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000956 switch (id) {
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530957 case KVM_REG_PPC_DEBUG_INST:
958 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
959 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000960 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000961 *val = get_reg_val(id, 0);
962 break;
963 case KVM_REG_PPC_DABR:
964 *val = get_reg_val(id, vcpu->arch.dabr);
965 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +1100966 case KVM_REG_PPC_DABRX:
967 *val = get_reg_val(id, vcpu->arch.dabrx);
968 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000969 case KVM_REG_PPC_DSCR:
970 *val = get_reg_val(id, vcpu->arch.dscr);
971 break;
972 case KVM_REG_PPC_PURR:
973 *val = get_reg_val(id, vcpu->arch.purr);
974 break;
975 case KVM_REG_PPC_SPURR:
976 *val = get_reg_val(id, vcpu->arch.spurr);
977 break;
978 case KVM_REG_PPC_AMR:
979 *val = get_reg_val(id, vcpu->arch.amr);
980 break;
981 case KVM_REG_PPC_UAMOR:
982 *val = get_reg_val(id, vcpu->arch.uamor);
983 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100984 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000985 i = id - KVM_REG_PPC_MMCR0;
986 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
987 break;
988 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
989 i = id - KVM_REG_PPC_PMC1;
990 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +0000991 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100992 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
993 i = id - KVM_REG_PPC_SPMC1;
994 *val = get_reg_val(id, vcpu->arch.spmc[i]);
995 break;
Paul Mackerras14941782013-09-06 13:11:18 +1000996 case KVM_REG_PPC_SIAR:
997 *val = get_reg_val(id, vcpu->arch.siar);
998 break;
999 case KVM_REG_PPC_SDAR:
1000 *val = get_reg_val(id, vcpu->arch.sdar);
1001 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001002 case KVM_REG_PPC_SIER:
1003 *val = get_reg_val(id, vcpu->arch.sier);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001004 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001005 case KVM_REG_PPC_IAMR:
1006 *val = get_reg_val(id, vcpu->arch.iamr);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001007 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001008 case KVM_REG_PPC_PSPB:
1009 *val = get_reg_val(id, vcpu->arch.pspb);
1010 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001011 case KVM_REG_PPC_DPDES:
1012 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1013 break;
1014 case KVM_REG_PPC_DAWR:
1015 *val = get_reg_val(id, vcpu->arch.dawr);
1016 break;
1017 case KVM_REG_PPC_DAWRX:
1018 *val = get_reg_val(id, vcpu->arch.dawrx);
1019 break;
1020 case KVM_REG_PPC_CIABR:
1021 *val = get_reg_val(id, vcpu->arch.ciabr);
1022 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001023 case KVM_REG_PPC_CSIGR:
1024 *val = get_reg_val(id, vcpu->arch.csigr);
1025 break;
1026 case KVM_REG_PPC_TACR:
1027 *val = get_reg_val(id, vcpu->arch.tacr);
1028 break;
1029 case KVM_REG_PPC_TCSCR:
1030 *val = get_reg_val(id, vcpu->arch.tcscr);
1031 break;
1032 case KVM_REG_PPC_PID:
1033 *val = get_reg_val(id, vcpu->arch.pid);
1034 break;
1035 case KVM_REG_PPC_ACOP:
1036 *val = get_reg_val(id, vcpu->arch.acop);
1037 break;
1038 case KVM_REG_PPC_WORT:
1039 *val = get_reg_val(id, vcpu->arch.wort);
1040 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001041 case KVM_REG_PPC_VPA_ADDR:
1042 spin_lock(&vcpu->arch.vpa_update_lock);
1043 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1044 spin_unlock(&vcpu->arch.vpa_update_lock);
1045 break;
1046 case KVM_REG_PPC_VPA_SLB:
1047 spin_lock(&vcpu->arch.vpa_update_lock);
1048 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1049 val->vpaval.length = vcpu->arch.slb_shadow.len;
1050 spin_unlock(&vcpu->arch.vpa_update_lock);
1051 break;
1052 case KVM_REG_PPC_VPA_DTL:
1053 spin_lock(&vcpu->arch.vpa_update_lock);
1054 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1055 val->vpaval.length = vcpu->arch.dtl.len;
1056 spin_unlock(&vcpu->arch.vpa_update_lock);
1057 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001058 case KVM_REG_PPC_TB_OFFSET:
1059 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1060 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001061 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001062 case KVM_REG_PPC_LPCR_64:
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001063 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1064 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001065 case KVM_REG_PPC_PPR:
1066 *val = get_reg_val(id, vcpu->arch.ppr);
1067 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001068#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1069 case KVM_REG_PPC_TFHAR:
1070 *val = get_reg_val(id, vcpu->arch.tfhar);
1071 break;
1072 case KVM_REG_PPC_TFIAR:
1073 *val = get_reg_val(id, vcpu->arch.tfiar);
1074 break;
1075 case KVM_REG_PPC_TEXASR:
1076 *val = get_reg_val(id, vcpu->arch.texasr);
1077 break;
1078 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1079 i = id - KVM_REG_PPC_TM_GPR0;
1080 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1081 break;
1082 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1083 {
1084 int j;
1085 i = id - KVM_REG_PPC_TM_VSR0;
1086 if (i < 32)
1087 for (j = 0; j < TS_FPRWIDTH; j++)
1088 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1089 else {
1090 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1091 val->vval = vcpu->arch.vr_tm.vr[i-32];
1092 else
1093 r = -ENXIO;
1094 }
1095 break;
1096 }
1097 case KVM_REG_PPC_TM_CR:
1098 *val = get_reg_val(id, vcpu->arch.cr_tm);
1099 break;
1100 case KVM_REG_PPC_TM_LR:
1101 *val = get_reg_val(id, vcpu->arch.lr_tm);
1102 break;
1103 case KVM_REG_PPC_TM_CTR:
1104 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1105 break;
1106 case KVM_REG_PPC_TM_FPSCR:
1107 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1108 break;
1109 case KVM_REG_PPC_TM_AMR:
1110 *val = get_reg_val(id, vcpu->arch.amr_tm);
1111 break;
1112 case KVM_REG_PPC_TM_PPR:
1113 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1114 break;
1115 case KVM_REG_PPC_TM_VRSAVE:
1116 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1117 break;
1118 case KVM_REG_PPC_TM_VSCR:
1119 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1120 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1121 else
1122 r = -ENXIO;
1123 break;
1124 case KVM_REG_PPC_TM_DSCR:
1125 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1126 break;
1127 case KVM_REG_PPC_TM_TAR:
1128 *val = get_reg_val(id, vcpu->arch.tar_tm);
1129 break;
1130#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001131 case KVM_REG_PPC_ARCH_COMPAT:
1132 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1133 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001134 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001135 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001136 break;
1137 }
1138
1139 return r;
1140}
1141
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301142static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1143 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001144{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001145 int r = 0;
1146 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001147 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +00001148
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001149 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00001150 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +00001151 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001152 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +00001153 r = -EINVAL;
1154 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001155 case KVM_REG_PPC_DABR:
1156 vcpu->arch.dabr = set_reg_val(id, *val);
1157 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001158 case KVM_REG_PPC_DABRX:
1159 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1160 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001161 case KVM_REG_PPC_DSCR:
1162 vcpu->arch.dscr = set_reg_val(id, *val);
1163 break;
1164 case KVM_REG_PPC_PURR:
1165 vcpu->arch.purr = set_reg_val(id, *val);
1166 break;
1167 case KVM_REG_PPC_SPURR:
1168 vcpu->arch.spurr = set_reg_val(id, *val);
1169 break;
1170 case KVM_REG_PPC_AMR:
1171 vcpu->arch.amr = set_reg_val(id, *val);
1172 break;
1173 case KVM_REG_PPC_UAMOR:
1174 vcpu->arch.uamor = set_reg_val(id, *val);
1175 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001176 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001177 i = id - KVM_REG_PPC_MMCR0;
1178 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1179 break;
1180 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1181 i = id - KVM_REG_PPC_PMC1;
1182 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1183 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001184 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1185 i = id - KVM_REG_PPC_SPMC1;
1186 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1187 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001188 case KVM_REG_PPC_SIAR:
1189 vcpu->arch.siar = set_reg_val(id, *val);
1190 break;
1191 case KVM_REG_PPC_SDAR:
1192 vcpu->arch.sdar = set_reg_val(id, *val);
1193 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001194 case KVM_REG_PPC_SIER:
1195 vcpu->arch.sier = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001196 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001197 case KVM_REG_PPC_IAMR:
1198 vcpu->arch.iamr = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001199 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001200 case KVM_REG_PPC_PSPB:
1201 vcpu->arch.pspb = set_reg_val(id, *val);
1202 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001203 case KVM_REG_PPC_DPDES:
1204 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1205 break;
1206 case KVM_REG_PPC_DAWR:
1207 vcpu->arch.dawr = set_reg_val(id, *val);
1208 break;
1209 case KVM_REG_PPC_DAWRX:
1210 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1211 break;
1212 case KVM_REG_PPC_CIABR:
1213 vcpu->arch.ciabr = set_reg_val(id, *val);
1214 /* Don't allow setting breakpoints in hypervisor code */
1215 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1216 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1217 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001218 case KVM_REG_PPC_CSIGR:
1219 vcpu->arch.csigr = set_reg_val(id, *val);
1220 break;
1221 case KVM_REG_PPC_TACR:
1222 vcpu->arch.tacr = set_reg_val(id, *val);
1223 break;
1224 case KVM_REG_PPC_TCSCR:
1225 vcpu->arch.tcscr = set_reg_val(id, *val);
1226 break;
1227 case KVM_REG_PPC_PID:
1228 vcpu->arch.pid = set_reg_val(id, *val);
1229 break;
1230 case KVM_REG_PPC_ACOP:
1231 vcpu->arch.acop = set_reg_val(id, *val);
1232 break;
1233 case KVM_REG_PPC_WORT:
1234 vcpu->arch.wort = set_reg_val(id, *val);
1235 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001236 case KVM_REG_PPC_VPA_ADDR:
1237 addr = set_reg_val(id, *val);
1238 r = -EINVAL;
1239 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1240 vcpu->arch.dtl.next_gpa))
1241 break;
1242 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1243 break;
1244 case KVM_REG_PPC_VPA_SLB:
1245 addr = val->vpaval.addr;
1246 len = val->vpaval.length;
1247 r = -EINVAL;
1248 if (addr && !vcpu->arch.vpa.next_gpa)
1249 break;
1250 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1251 break;
1252 case KVM_REG_PPC_VPA_DTL:
1253 addr = val->vpaval.addr;
1254 len = val->vpaval.length;
1255 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +00001256 if (addr && (len < sizeof(struct dtl_entry) ||
1257 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +00001258 break;
1259 len -= len % sizeof(struct dtl_entry);
1260 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1261 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001262 case KVM_REG_PPC_TB_OFFSET:
1263 /* round up to multiple of 2^24 */
1264 vcpu->arch.vcore->tb_offset =
1265 ALIGN(set_reg_val(id, *val), 1UL << 24);
1266 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001267 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001268 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1269 break;
1270 case KVM_REG_PPC_LPCR_64:
1271 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001272 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001273 case KVM_REG_PPC_PPR:
1274 vcpu->arch.ppr = set_reg_val(id, *val);
1275 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001276#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1277 case KVM_REG_PPC_TFHAR:
1278 vcpu->arch.tfhar = set_reg_val(id, *val);
1279 break;
1280 case KVM_REG_PPC_TFIAR:
1281 vcpu->arch.tfiar = set_reg_val(id, *val);
1282 break;
1283 case KVM_REG_PPC_TEXASR:
1284 vcpu->arch.texasr = set_reg_val(id, *val);
1285 break;
1286 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1287 i = id - KVM_REG_PPC_TM_GPR0;
1288 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1289 break;
1290 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1291 {
1292 int j;
1293 i = id - KVM_REG_PPC_TM_VSR0;
1294 if (i < 32)
1295 for (j = 0; j < TS_FPRWIDTH; j++)
1296 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1297 else
1298 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1299 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1300 else
1301 r = -ENXIO;
1302 break;
1303 }
1304 case KVM_REG_PPC_TM_CR:
1305 vcpu->arch.cr_tm = set_reg_val(id, *val);
1306 break;
1307 case KVM_REG_PPC_TM_LR:
1308 vcpu->arch.lr_tm = set_reg_val(id, *val);
1309 break;
1310 case KVM_REG_PPC_TM_CTR:
1311 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1312 break;
1313 case KVM_REG_PPC_TM_FPSCR:
1314 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1315 break;
1316 case KVM_REG_PPC_TM_AMR:
1317 vcpu->arch.amr_tm = set_reg_val(id, *val);
1318 break;
1319 case KVM_REG_PPC_TM_PPR:
1320 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1321 break;
1322 case KVM_REG_PPC_TM_VRSAVE:
1323 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1324 break;
1325 case KVM_REG_PPC_TM_VSCR:
1326 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1327 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1328 else
1329 r = - ENXIO;
1330 break;
1331 case KVM_REG_PPC_TM_DSCR:
1332 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1333 break;
1334 case KVM_REG_PPC_TM_TAR:
1335 vcpu->arch.tar_tm = set_reg_val(id, *val);
1336 break;
1337#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001338 case KVM_REG_PPC_ARCH_COMPAT:
1339 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1340 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001341 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001342 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001343 break;
1344 }
1345
1346 return r;
1347}
1348
Stewart Smithde9bdd12014-07-18 14:18:42 +10001349static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1350{
1351 struct kvmppc_vcore *vcore;
1352
1353 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1354
1355 if (vcore == NULL)
1356 return NULL;
1357
1358 INIT_LIST_HEAD(&vcore->runnable_threads);
1359 spin_lock_init(&vcore->lock);
1360 init_waitqueue_head(&vcore->wq);
1361 vcore->preempt_tb = TB_NIL;
1362 vcore->lpcr = kvm->arch.lpcr;
1363 vcore->first_vcpuid = core * threads_per_subcore;
1364 vcore->kvm = kvm;
1365
Stewart Smith9678cda2014-07-18 14:18:43 +10001366 vcore->mpp_buffer_is_valid = false;
1367
1368 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1369 vcore->mpp_buffer = (void *)__get_free_pages(
1370 GFP_KERNEL|__GFP_ZERO,
1371 MPP_BUFFER_ORDER);
1372
Stewart Smithde9bdd12014-07-18 14:18:42 +10001373 return vcore;
1374}
1375
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301376static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1377 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001378{
1379 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001380 int err = -EINVAL;
1381 int core;
1382 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001383
Michael Ellerman3102f782014-05-23 18:15:29 +10001384 core = id / threads_per_subcore;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001385 if (core >= KVM_MAX_VCORES)
1386 goto out;
1387
1388 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001389 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001390 if (!vcpu)
1391 goto out;
1392
1393 err = kvm_vcpu_init(vcpu, kvm, id);
1394 if (err)
1395 goto free_vcpu;
1396
1397 vcpu->arch.shared = &vcpu->arch.shregs;
Alexander Graf5deb8e72014-04-24 13:46:24 +02001398#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1399 /*
1400 * The shared struct is never shared on HV,
1401 * so we can always use host endianness
1402 */
1403#ifdef __BIG_ENDIAN__
1404 vcpu->arch.shared_big_endian = true;
1405#else
1406 vcpu->arch.shared_big_endian = false;
1407#endif
1408#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001409 vcpu->arch.mmcr[0] = MMCR0_FC;
1410 vcpu->arch.ctrl = CTRL_RUNLATCH;
1411 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301412 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001413 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001414 spin_lock_init(&vcpu->arch.tbacct_lock);
1415 vcpu->arch.busy_preempt = TB_NIL;
Anton Blanchardd6829162014-01-08 21:25:30 +11001416 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001417
Paul Mackerrasde56a942011-06-29 00:21:34 +00001418 kvmppc_mmu_book3s_hv_init(vcpu);
1419
Paul Mackerras8455d792012-10-15 01:17:42 +00001420 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001421
1422 init_waitqueue_head(&vcpu->arch.cpu_run);
1423
1424 mutex_lock(&kvm->lock);
1425 vcore = kvm->arch.vcores[core];
1426 if (!vcore) {
Stewart Smithde9bdd12014-07-18 14:18:42 +10001427 vcore = kvmppc_vcore_create(kvm, core);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001428 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001429 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001430 }
1431 mutex_unlock(&kvm->lock);
1432
1433 if (!vcore)
1434 goto free_vcpu;
1435
1436 spin_lock(&vcore->lock);
1437 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001438 spin_unlock(&vcore->lock);
1439 vcpu->arch.vcore = vcore;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001440 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001441
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001442 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1443 kvmppc_sanity_check(vcpu);
1444
Paul Mackerrasde56a942011-06-29 00:21:34 +00001445 return vcpu;
1446
1447free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001448 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001449out:
1450 return ERR_PTR(err);
1451}
1452
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001453static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1454{
1455 if (vpa->pinned_addr)
1456 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1457 vpa->dirty);
1458}
1459
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301460static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001461{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001462 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001463 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1464 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1465 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001466 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001467 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001468 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001469}
1470
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301471static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1472{
1473 /* Indicate we want to get back into the guest */
1474 return 1;
1475}
1476
Paul Mackerras19ccb762011-07-23 17:42:46 +10001477static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001478{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001479 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001480
Paul Mackerras19ccb762011-07-23 17:42:46 +10001481 now = get_tb();
1482 if (now > vcpu->arch.dec_expires) {
1483 /* decrementer has already gone negative */
1484 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001485 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001486 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001487 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001488 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1489 / tb_ticks_per_sec;
1490 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1491 HRTIMER_MODE_REL);
1492 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001493}
1494
Paul Mackerras19ccb762011-07-23 17:42:46 +10001495static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001496{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001497 vcpu->arch.ceded = 0;
1498 if (vcpu->arch.timer_running) {
1499 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1500 vcpu->arch.timer_running = 0;
1501 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001502}
1503
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001504extern void __kvmppc_vcore_entry(void);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001505
1506static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1507 struct kvm_vcpu *vcpu)
1508{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001509 u64 now;
1510
Paul Mackerras371fefd2011-06-29 00:23:08 +00001511 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1512 return;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001513 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001514 now = mftb();
1515 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1516 vcpu->arch.stolen_logged;
1517 vcpu->arch.busy_preempt = now;
1518 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001519 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001520 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001521 list_del(&vcpu->arch.run_list);
1522}
1523
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001524static int kvmppc_grab_hwthread(int cpu)
1525{
1526 struct paca_struct *tpaca;
Paul Mackerrasb754c732014-09-02 16:14:42 +10001527 long timeout = 10000;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001528
1529 tpaca = &paca[cpu];
1530
1531 /* Ensure the thread won't go into the kernel if it wakes */
1532 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001533 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001534
1535 /*
1536 * If the thread is already executing in the kernel (e.g. handling
1537 * a stray interrupt), wait for it to get back to nap mode.
1538 * The smp_mb() is to ensure that our setting of hwthread_req
1539 * is visible before we look at hwthread_state, so if this
1540 * races with the code at system_reset_pSeries and the thread
1541 * misses our setting of hwthread_req, we are sure to see its
1542 * setting of hwthread_state, and vice versa.
1543 */
1544 smp_mb();
1545 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1546 if (--timeout <= 0) {
1547 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1548 return -EBUSY;
1549 }
1550 udelay(1);
1551 }
1552 return 0;
1553}
1554
1555static void kvmppc_release_hwthread(int cpu)
1556{
1557 struct paca_struct *tpaca;
1558
1559 tpaca = &paca[cpu];
1560 tpaca->kvm_hstate.hwthread_req = 0;
1561 tpaca->kvm_hstate.kvm_vcpu = NULL;
1562}
1563
Paul Mackerras371fefd2011-06-29 00:23:08 +00001564static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1565{
1566 int cpu;
1567 struct paca_struct *tpaca;
1568 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1569
Paul Mackerras19ccb762011-07-23 17:42:46 +10001570 if (vcpu->arch.timer_running) {
1571 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1572 vcpu->arch.timer_running = 0;
1573 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001574 cpu = vc->pcpu + vcpu->arch.ptid;
1575 tpaca = &paca[cpu];
1576 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1577 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001578 tpaca->kvm_hstate.ptid = vcpu->arch.ptid;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001579 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001580 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +00001581#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001582 if (cpu != smp_processor_id()) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001583 xics_wake_cpu(cpu);
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001584 if (vcpu->arch.ptid)
1585 ++vc->n_woken;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001586 }
1587#endif
1588}
1589
1590static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1591{
1592 int i;
1593
1594 HMT_low();
1595 i = 0;
1596 while (vc->nap_count < vc->n_woken) {
1597 if (++i >= 1000000) {
1598 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1599 vc->nap_count, vc->n_woken);
1600 break;
1601 }
1602 cpu_relax();
1603 }
1604 HMT_medium();
1605}
1606
1607/*
1608 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001609 * this core are off-line. Then grab the threads so they can't
1610 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001611 */
1612static int on_primary_thread(void)
1613{
1614 int cpu = smp_processor_id();
Michael Ellerman3102f782014-05-23 18:15:29 +10001615 int thr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001616
Michael Ellerman3102f782014-05-23 18:15:29 +10001617 /* Are we on a primary subcore? */
1618 if (cpu_thread_in_subcore(cpu))
Paul Mackerras371fefd2011-06-29 00:23:08 +00001619 return 0;
Michael Ellerman3102f782014-05-23 18:15:29 +10001620
1621 thr = 0;
1622 while (++thr < threads_per_subcore)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001623 if (cpu_online(cpu + thr))
1624 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001625
1626 /* Grab all hw threads so they can't go into the kernel */
Michael Ellerman3102f782014-05-23 18:15:29 +10001627 for (thr = 1; thr < threads_per_subcore; ++thr) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00001628 if (kvmppc_grab_hwthread(cpu + thr)) {
1629 /* Couldn't grab one; let the others go */
1630 do {
1631 kvmppc_release_hwthread(cpu + thr);
1632 } while (--thr > 0);
1633 return 0;
1634 }
1635 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001636 return 1;
1637}
1638
Stewart Smith9678cda2014-07-18 14:18:43 +10001639static void kvmppc_start_saving_l2_cache(struct kvmppc_vcore *vc)
1640{
1641 phys_addr_t phy_addr, mpp_addr;
1642
1643 phy_addr = (phys_addr_t)virt_to_phys(vc->mpp_buffer);
1644 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1645
1646 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_ABORT);
1647 logmpp(mpp_addr | PPC_LOGMPP_LOG_L2);
1648
1649 vc->mpp_buffer_is_valid = true;
1650}
1651
1652static void kvmppc_start_restoring_l2_cache(const struct kvmppc_vcore *vc)
1653{
1654 phys_addr_t phy_addr, mpp_addr;
1655
1656 phy_addr = virt_to_phys(vc->mpp_buffer);
1657 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1658
1659 /* We must abort any in-progress save operations to ensure
1660 * the table is valid so that prefetch engine knows when to
1661 * stop prefetching. */
1662 logmpp(mpp_addr | PPC_LOGMPP_LOG_ABORT);
1663 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_WHOLE_TABLE);
1664}
1665
Paul Mackerras371fefd2011-06-29 00:23:08 +00001666/*
1667 * Run a set of guest threads on a physical core.
1668 * Called with vc->lock held.
1669 */
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001670static void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001671{
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001672 struct kvm_vcpu *vcpu, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001673 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001674 u64 now;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001675 int i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001676 int srcu_idx;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001677 struct kvm_vcpu *vcpus_to_update[threads_per_core];
Paul Mackerrasde56a942011-06-29 00:21:34 +00001678
Paul Mackerras371fefd2011-06-29 00:23:08 +00001679 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +10001680 need_vpa_update = 0;
1681 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001682 if (signal_pending(vcpu->arch.run_task))
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001683 return;
1684 if (vcpu->arch.vpa.update_pending ||
1685 vcpu->arch.slb_shadow.update_pending ||
1686 vcpu->arch.dtl.update_pending)
1687 vcpus_to_update[need_vpa_update++] = vcpu;
Paul Mackerras081f3232012-06-01 20:20:24 +10001688 }
1689
1690 /*
1691 * Initialize *vc, in particular vc->vcore_state, so we can
1692 * drop the vcore lock if necessary.
1693 */
1694 vc->n_woken = 0;
1695 vc->nap_count = 0;
1696 vc->entry_exit_count = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001697 vc->vcore_state = VCORE_STARTING;
Paul Mackerras081f3232012-06-01 20:20:24 +10001698 vc->in_guest = 0;
1699 vc->napping_threads = 0;
1700
1701 /*
1702 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1703 * which can't be called with any spinlocks held.
1704 */
1705 if (need_vpa_update) {
1706 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001707 for (i = 0; i < need_vpa_update; ++i)
1708 kvmppc_update_vpas(vcpus_to_update[i]);
Paul Mackerras081f3232012-06-01 20:20:24 +10001709 spin_lock(&vc->lock);
1710 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001711
1712 /*
Michael Ellerman3102f782014-05-23 18:15:29 +10001713 * Make sure we are running on primary threads, and that secondary
1714 * threads are offline. Also check if the number of threads in this
1715 * guest are greater than the current system threads per guest.
Paul Mackerras7b444c62012-10-15 01:16:14 +00001716 */
Michael Ellerman3102f782014-05-23 18:15:29 +10001717 if ((threads_per_core > 1) &&
1718 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00001719 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1720 vcpu->arch.ret = -EBUSY;
1721 goto out;
1722 }
1723
Michael Ellerman3102f782014-05-23 18:15:29 +10001724
Paul Mackerras371fefd2011-06-29 00:23:08 +00001725 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001726 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001727 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001728 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001729 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001730
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001731 /* Set this explicitly in case thread 0 doesn't have a vcpu */
1732 get_paca()->kvm_hstate.kvm_vcore = vc;
1733 get_paca()->kvm_hstate.ptid = 0;
1734
Paul Mackerras2f12f032012-10-15 01:17:17 +00001735 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001736 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001737 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001738
Paul Mackerras19ccb762011-07-23 17:42:46 +10001739 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001740
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001741 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001742
Stewart Smith9678cda2014-07-18 14:18:43 +10001743 if (vc->mpp_buffer_is_valid)
1744 kvmppc_start_restoring_l2_cache(vc);
1745
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001746 __kvmppc_vcore_entry();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001747
Paul Mackerras371fefd2011-06-29 00:23:08 +00001748 spin_lock(&vc->lock);
Stewart Smith9678cda2014-07-18 14:18:43 +10001749
1750 if (vc->mpp_buffer)
1751 kvmppc_start_saving_l2_cache(vc);
1752
Paul Mackerras19ccb762011-07-23 17:42:46 +10001753 /* disable sending of IPIs on virtual external irqs */
1754 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1755 vcpu->cpu = -1;
1756 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001757 if (vc->nap_count < vc->n_woken)
1758 kvmppc_wait_for_nap(vc);
Michael Ellerman3102f782014-05-23 18:15:29 +10001759 for (i = 0; i < threads_per_subcore; ++i)
Paul Mackerras2f12f032012-10-15 01:17:17 +00001760 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001761 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001762 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001763 spin_unlock(&vc->lock);
1764
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001765 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001766
Paul Mackerras371fefd2011-06-29 00:23:08 +00001767 /* make sure updates to secondary vcpu structs are visible now */
1768 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001769 kvm_guest_exit();
1770
1771 preempt_enable();
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09001772 cond_resched();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001773
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001774 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001775 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001776 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1777 /* cancel pending dec exception if dec is positive */
1778 if (now < vcpu->arch.dec_expires &&
1779 kvmppc_core_pending_dec(vcpu))
1780 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001781
1782 ret = RESUME_GUEST;
1783 if (vcpu->arch.trap)
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301784 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1785 vcpu->arch.run_task);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001786
Paul Mackerras371fefd2011-06-29 00:23:08 +00001787 vcpu->arch.ret = ret;
1788 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001789
1790 if (vcpu->arch.ceded) {
Greg Kurze59d24e2014-02-06 17:36:56 +01001791 if (!is_kvmppc_resume_guest(ret))
Paul Mackerras19ccb762011-07-23 17:42:46 +10001792 kvmppc_end_cede(vcpu);
1793 else
1794 kvmppc_set_timer(vcpu);
1795 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001796 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001797
Paul Mackerrasde56a942011-06-29 00:21:34 +00001798 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001799 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001800 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1801 arch.run_list) {
Greg Kurze59d24e2014-02-06 17:36:56 +01001802 if (!is_kvmppc_resume_guest(vcpu->arch.ret)) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001803 kvmppc_remove_runnable(vc, vcpu);
1804 wake_up(&vcpu->arch.cpu_run);
1805 }
1806 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001807}
1808
Paul Mackerras19ccb762011-07-23 17:42:46 +10001809/*
1810 * Wait for some other vcpu thread to execute us, and
1811 * wake us up when we need to handle something in the host.
1812 */
1813static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001814{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001815 DEFINE_WAIT(wait);
1816
Paul Mackerras19ccb762011-07-23 17:42:46 +10001817 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1818 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1819 schedule();
1820 finish_wait(&vcpu->arch.cpu_run, &wait);
1821}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001822
Paul Mackerras19ccb762011-07-23 17:42:46 +10001823/*
1824 * All the vcpus in this vcore are idle, so wait for a decrementer
1825 * or external interrupt to one of the vcpus. vc->lock is held.
1826 */
1827static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1828{
1829 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001830
1831 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1832 vc->vcore_state = VCORE_SLEEPING;
1833 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001834 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001835 finish_wait(&vc->wq, &wait);
1836 spin_lock(&vc->lock);
1837 vc->vcore_state = VCORE_INACTIVE;
1838}
1839
1840static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1841{
1842 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001843 struct kvmppc_vcore *vc;
1844 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001845
Paul Mackerras371fefd2011-06-29 00:23:08 +00001846 kvm_run->exit_reason = 0;
1847 vcpu->arch.ret = RESUME_GUEST;
1848 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001849 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001850
Paul Mackerras371fefd2011-06-29 00:23:08 +00001851 /*
1852 * Synchronize with other threads in this virtual core
1853 */
1854 vc = vcpu->arch.vcore;
1855 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001856 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001857 vcpu->arch.run_task = current;
1858 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001859 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10001860 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001861 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001862 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1863 ++vc->n_runnable;
1864
Paul Mackerras19ccb762011-07-23 17:42:46 +10001865 /*
1866 * This happens the first time this is called for a vcpu.
1867 * If the vcore is already running, we may be able to start
1868 * this thread straight away and have it join in.
1869 */
Paul Mackerras8455d792012-10-15 01:17:42 +00001870 if (!signal_pending(current)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001871 if (vc->vcore_state == VCORE_RUNNING &&
1872 VCORE_EXIT_COUNT(vc) == 0) {
Paul Mackerras2f12f032012-10-15 01:17:17 +00001873 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001874 kvmppc_start_thread(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00001875 } else if (vc->vcore_state == VCORE_SLEEPING) {
1876 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001877 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001878
Paul Mackerras8455d792012-10-15 01:17:42 +00001879 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001880
1881 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1882 !signal_pending(current)) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001883 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001884 spin_unlock(&vc->lock);
1885 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1886 spin_lock(&vc->lock);
1887 continue;
1888 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001889 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1890 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001891 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001892 if (signal_pending(v->arch.run_task)) {
1893 kvmppc_remove_runnable(vc, v);
1894 v->stat.signal_exits++;
1895 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1896 v->arch.ret = -EINTR;
1897 wake_up(&v->arch.cpu_run);
1898 }
1899 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001900 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1901 break;
1902 vc->runner = vcpu;
1903 n_ceded = 0;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001904 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001905 if (!v->arch.pending_exceptions)
1906 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001907 else
1908 v->arch.ceded = 0;
1909 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001910 if (n_ceded == vc->n_runnable)
1911 kvmppc_vcore_blocked(vc);
1912 else
1913 kvmppc_run_core(vc);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001914 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001915 }
1916
Paul Mackerras8455d792012-10-15 01:17:42 +00001917 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1918 (vc->vcore_state == VCORE_RUNNING ||
1919 vc->vcore_state == VCORE_EXITING)) {
1920 spin_unlock(&vc->lock);
1921 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1922 spin_lock(&vc->lock);
1923 }
1924
1925 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1926 kvmppc_remove_runnable(vc, vcpu);
1927 vcpu->stat.signal_exits++;
1928 kvm_run->exit_reason = KVM_EXIT_INTR;
1929 vcpu->arch.ret = -EINTR;
1930 }
1931
1932 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1933 /* Wake up some vcpu to run the core */
1934 v = list_first_entry(&vc->runnable_threads,
1935 struct kvm_vcpu, arch.run_list);
1936 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001937 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001938
Paul Mackerras19ccb762011-07-23 17:42:46 +10001939 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001940 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001941}
1942
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301943static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001944{
1945 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001946 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001947
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001948 if (!vcpu->arch.sane) {
1949 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1950 return -EINVAL;
1951 }
1952
Scott Wood25051b5a2011-11-08 18:23:23 -06001953 kvmppc_core_prepare_to_enter(vcpu);
1954
Paul Mackerras19ccb762011-07-23 17:42:46 +10001955 /* No need to go into the guest when all we'll do is come back out */
1956 if (signal_pending(current)) {
1957 run->exit_reason = KVM_EXIT_INTR;
1958 return -EINTR;
1959 }
1960
Paul Mackerras32fad282012-05-04 02:32:53 +00001961 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1962 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1963 smp_mb();
1964
1965 /* On the first time here, set up HTAB and VRMA or RMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001966 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001967 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001968 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001969 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001970 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001971
1972 flush_fp_to_thread(current);
1973 flush_altivec_to_thread(current);
1974 flush_vsx_to_thread(current);
1975 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001976 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001977 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001978
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001979 do {
1980 r = kvmppc_run_vcpu(run, vcpu);
1981
1982 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1983 !(vcpu->arch.shregs.msr & MSR_PR)) {
1984 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001985 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001986 } else if (r == RESUME_PAGE_FAULT) {
1987 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1988 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1989 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1990 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001991 }
Greg Kurze59d24e2014-02-06 17:36:56 +01001992 } while (is_kvmppc_resume_guest(r));
Paul Mackerras32fad282012-05-04 02:32:53 +00001993
1994 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001995 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00001996 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001997 return r;
1998}
1999
David Gibson54738c02011-06-29 00:22:41 +00002000
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002001/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +00002002 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002003static inline int lpcr_rmls(unsigned long rma_size)
2004{
2005 switch (rma_size) {
2006 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +00002007 if (cpu_has_feature(CPU_FTR_ARCH_206))
2008 return 8; /* only supported on POWER7 */
2009 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002010 case 64ul << 20: /* 64 MB */
2011 return 3;
2012 case 128ul << 20: /* 128 MB */
2013 return 7;
2014 case 256ul << 20: /* 256 MB */
2015 return 4;
2016 case 1ul << 30: /* 1 GB */
2017 return 2;
2018 case 16ul << 30: /* 16 GB */
2019 return 1;
2020 case 256ul << 30: /* 256 GB */
2021 return 0;
2022 default:
2023 return -1;
2024 }
2025}
2026
2027static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2028{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002029 struct page *page;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302030 struct kvm_rma_info *ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002031
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302032 if (vmf->pgoff >= kvm_rma_pages)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002033 return VM_FAULT_SIGBUS;
2034
2035 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
2036 get_page(page);
2037 vmf->page = page;
2038 return 0;
2039}
2040
2041static const struct vm_operations_struct kvm_rma_vm_ops = {
2042 .fault = kvm_rma_fault,
2043};
2044
2045static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
2046{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07002047 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002048 vma->vm_ops = &kvm_rma_vm_ops;
2049 return 0;
2050}
2051
2052static int kvm_rma_release(struct inode *inode, struct file *filp)
2053{
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302054 struct kvm_rma_info *ri = filp->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002055
2056 kvm_release_rma(ri);
2057 return 0;
2058}
2059
Al Viro75ef9de2013-04-04 19:09:41 -04002060static const struct file_operations kvm_rma_fops = {
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002061 .mmap = kvm_rma_mmap,
2062 .release = kvm_rma_release,
2063};
2064
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302065static long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
2066 struct kvm_allocate_rma *ret)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002067{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002068 long fd;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302069 struct kvm_rma_info *ri;
2070 /*
2071 * Only do this on PPC970 in HV mode
2072 */
2073 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
2074 !cpu_has_feature(CPU_FTR_ARCH_201))
2075 return -EINVAL;
2076
2077 if (!kvm_rma_pages)
2078 return -EINVAL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002079
2080 ri = kvm_alloc_rma();
2081 if (!ri)
2082 return -ENOMEM;
2083
Yann Droneaud2f84d5e2013-08-24 22:14:08 +02002084 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002085 if (fd < 0)
2086 kvm_release_rma(ri);
2087
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302088 ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002089 return fd;
2090}
2091
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002092static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2093 int linux_psize)
2094{
2095 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2096
2097 if (!def->shift)
2098 return;
2099 (*sps)->page_shift = def->shift;
2100 (*sps)->slb_enc = def->sllp;
2101 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00002102 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05302103 /*
2104 * Add 16MB MPSS support if host supports it
2105 */
2106 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2107 (*sps)->enc[1].page_shift = 24;
2108 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2109 }
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002110 (*sps)++;
2111}
2112
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302113static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2114 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002115{
2116 struct kvm_ppc_one_seg_page_size *sps;
2117
2118 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2119 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2120 info->flags |= KVM_PPC_1T_SEGMENTS;
2121 info->slb_size = mmu_slb_size;
2122
2123 /* We only support these sizes for now, and no muti-size segments */
2124 sps = &info->sps[0];
2125 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2126 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2127 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2128
2129 return 0;
2130}
2131
Paul Mackerras82ed3612011-12-15 02:03:22 +00002132/*
2133 * Get (and clear) the dirty memory log for a memory slot.
2134 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302135static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2136 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002137{
2138 struct kvm_memory_slot *memslot;
2139 int r;
2140 unsigned long n;
2141
2142 mutex_lock(&kvm->slots_lock);
2143
2144 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07002145 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002146 goto out;
2147
2148 memslot = id_to_memslot(kvm->memslots, log->slot);
2149 r = -ENOENT;
2150 if (!memslot->dirty_bitmap)
2151 goto out;
2152
2153 n = kvm_dirty_bitmap_bytes(memslot);
2154 memset(memslot->dirty_bitmap, 0, n);
2155
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002156 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00002157 if (r)
2158 goto out;
2159
2160 r = -EFAULT;
2161 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2162 goto out;
2163
2164 r = 0;
2165out:
2166 mutex_unlock(&kvm->slots_lock);
2167 return r;
2168}
2169
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002170static void unpin_slot(struct kvm_memory_slot *memslot)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002171{
2172 unsigned long *physp;
2173 unsigned long j, npages, pfn;
2174 struct page *page;
2175
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002176 physp = memslot->arch.slot_phys;
2177 npages = memslot->npages;
2178 if (!physp)
2179 return;
2180 for (j = 0; j < npages; j++) {
2181 if (!(physp[j] & KVMPPC_GOT_PAGE))
2182 continue;
2183 pfn = physp[j] >> PAGE_SHIFT;
2184 page = pfn_to_page(pfn);
2185 SetPageDirty(page);
2186 put_page(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002187 }
2188}
2189
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302190static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2191 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002192{
2193 if (!dont || free->arch.rmap != dont->arch.rmap) {
2194 vfree(free->arch.rmap);
2195 free->arch.rmap = NULL;
2196 }
2197 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
2198 unpin_slot(free);
2199 vfree(free->arch.slot_phys);
2200 free->arch.slot_phys = NULL;
2201 }
2202}
2203
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302204static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
2205 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002206{
2207 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
2208 if (!slot->arch.rmap)
2209 return -ENOMEM;
2210 slot->arch.slot_phys = NULL;
2211
2212 return 0;
2213}
2214
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302215static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
2216 struct kvm_memory_slot *memslot,
2217 struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002218{
2219 unsigned long *phys;
2220
2221 /* Allocate a slot_phys array if needed */
2222 phys = memslot->arch.slot_phys;
2223 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
2224 phys = vzalloc(memslot->npages * sizeof(unsigned long));
2225 if (!phys)
2226 return -ENOMEM;
2227 memslot->arch.slot_phys = phys;
2228 }
2229
2230 return 0;
2231}
2232
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302233static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
2234 struct kvm_userspace_memory_region *mem,
2235 const struct kvm_memory_slot *old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002236{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002237 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
2238 struct kvm_memory_slot *memslot;
2239
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002240 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002241 /*
2242 * If modifying a memslot, reset all the rmap dirty bits.
2243 * If this is a new memslot, we don't need to do anything
2244 * since the rmap array starts out as all zeroes,
2245 * i.e. no pages are dirty.
2246 */
2247 memslot = id_to_memslot(kvm->memslots, mem->slot);
2248 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
2249 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002250}
2251
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002252/*
2253 * Update LPCR values in kvm->arch and in vcores.
2254 * Caller must hold kvm->lock.
2255 */
2256void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
2257{
2258 long int i;
2259 u32 cores_done = 0;
2260
2261 if ((kvm->arch.lpcr & mask) == lpcr)
2262 return;
2263
2264 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2265
2266 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2267 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2268 if (!vc)
2269 continue;
2270 spin_lock(&vc->lock);
2271 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2272 spin_unlock(&vc->lock);
2273 if (++cores_done >= kvm->arch.online_vcores)
2274 break;
2275 }
2276}
2277
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302278static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2279{
2280 return;
2281}
2282
Paul Mackerras32fad282012-05-04 02:32:53 +00002283static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002284{
2285 int err = 0;
2286 struct kvm *kvm = vcpu->kvm;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302287 struct kvm_rma_info *ri = NULL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002288 unsigned long hva;
2289 struct kvm_memory_slot *memslot;
2290 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002291 unsigned long lpcr = 0, senc;
2292 unsigned long lpcr_mask = 0;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002293 unsigned long psize, porder;
2294 unsigned long rma_size;
2295 unsigned long rmls;
2296 unsigned long *physp;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002297 unsigned long i, npages;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002298 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002299
2300 mutex_lock(&kvm->lock);
2301 if (kvm->arch.rma_setup_done)
2302 goto out; /* another vcpu beat us to it */
2303
Paul Mackerras32fad282012-05-04 02:32:53 +00002304 /* Allocate hashed page table (if not done already) and reset it */
2305 if (!kvm->arch.hpt_virt) {
2306 err = kvmppc_alloc_hpt(kvm, NULL);
2307 if (err) {
2308 pr_err("KVM: Couldn't alloc HPT\n");
2309 goto out;
2310 }
2311 }
2312
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002313 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002314 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002315 memslot = gfn_to_memslot(kvm, 0);
2316
2317 /* We must have some memory at 0 by now */
2318 err = -EINVAL;
2319 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002320 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002321
2322 /* Look up the VMA for the start of this memory slot */
2323 hva = memslot->userspace_addr;
2324 down_read(&current->mm->mmap_sem);
2325 vma = find_vma(current->mm, hva);
2326 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2327 goto up_out;
2328
2329 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002330 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002331
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002332 /* Is this one of our preallocated RMAs? */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002333 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
2334 hva == vma->vm_start)
2335 ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002336
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002337 up_read(&current->mm->mmap_sem);
2338
2339 if (!ri) {
2340 /* On POWER7, use VRMA; on PPC970, give up */
2341 err = -EPERM;
2342 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2343 pr_err("KVM: CPU requires an RMO\n");
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002344 goto out_srcu;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002345 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002346
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002347 /* We can handle 4k, 64k or 16M pages in the VRMA */
2348 err = -EINVAL;
2349 if (!(psize == 0x1000 || psize == 0x10000 ||
2350 psize == 0x1000000))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002351 goto out_srcu;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002352
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002353 /* Update VRMASD field in the LPCR */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002354 senc = slb_pgsize_encoding(psize);
Paul Mackerras697d3892011-12-12 12:36:37 +00002355 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2356 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002357 lpcr_mask = LPCR_VRMASD;
2358 /* the -4 is to account for senc values starting at 0x10 */
2359 lpcr = senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002360
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002361 /* Create HPTEs in the hash page table for the VRMA */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002362 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002363
2364 } else {
2365 /* Set up to use an RMO region */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302366 rma_size = kvm_rma_pages;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002367 if (rma_size > memslot->npages)
2368 rma_size = memslot->npages;
2369 rma_size <<= PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002370 rmls = lpcr_rmls(rma_size);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002371 err = -EINVAL;
Chen Gang5d226ae2013-07-22 14:32:35 +08002372 if ((long)rmls < 0) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002373 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002374 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002375 }
2376 atomic_inc(&ri->use_count);
2377 kvm->arch.rma = ri;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002378
2379 /* Update LPCR and RMOR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00002380 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2381 /* PPC970; insert RMLS value (split field) in HID4 */
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002382 lpcr_mask = (1ul << HID4_RMLS0_SH) |
2383 (3ul << HID4_RMLS2_SH) | HID4_RMOR;
2384 lpcr = ((rmls >> 2) << HID4_RMLS0_SH) |
Paul Mackerras9e368f22011-06-29 00:40:08 +00002385 ((rmls & 3) << HID4_RMLS2_SH);
2386 /* RMOR is also in HID4 */
2387 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
2388 << HID4_RMOR_SH;
2389 } else {
2390 /* POWER7 */
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002391 lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS;
2392 lpcr = rmls << LPCR_RMLS_SH;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302393 kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002394 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002395 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002396 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002397
2398 /* Initialize phys addrs of pages in RMO */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302399 npages = kvm_rma_pages;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002400 porder = __ilog2(npages);
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002401 physp = memslot->arch.slot_phys;
2402 if (physp) {
2403 if (npages > memslot->npages)
2404 npages = memslot->npages;
2405 spin_lock(&kvm->arch.slot_phys_lock);
2406 for (i = 0; i < npages; ++i)
2407 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
2408 porder;
2409 spin_unlock(&kvm->arch.slot_phys_lock);
2410 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002411 }
2412
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002413 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
2414
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002415 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
2416 smp_wmb();
2417 kvm->arch.rma_setup_done = 1;
2418 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002419 out_srcu:
2420 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002421 out:
2422 mutex_unlock(&kvm->lock);
2423 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002424
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002425 up_out:
2426 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08002427 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002428}
2429
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302430static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002431{
Paul Mackerras32fad282012-05-04 02:32:53 +00002432 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002433
Paul Mackerras32fad282012-05-04 02:32:53 +00002434 /* Allocate the guest's logical partition ID */
2435
2436 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08002437 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00002438 return -ENOMEM;
2439 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002440
Paul Mackerras1b400ba2012-11-21 23:28:08 +00002441 /*
2442 * Since we don't flush the TLB when tearing down a VM,
2443 * and this lpid might have previously been used,
2444 * make sure we flush on each core before running the new VM.
2445 */
2446 cpumask_setall(&kvm->arch.need_tlb_flush);
2447
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002448 /* Start out with the default set of hcalls enabled */
2449 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
2450 sizeof(kvm->arch.enabled_hcalls));
2451
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002452 kvm->arch.rma = NULL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002453
Paul Mackerras9e368f22011-06-29 00:40:08 +00002454 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002455
Paul Mackerras9e368f22011-06-29 00:40:08 +00002456 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2457 /* PPC970; HID4 is effectively the LPCR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00002458 kvm->arch.host_lpid = 0;
2459 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
2460 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
2461 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
2462 ((lpid & 0xf) << HID4_LPID5_SH);
2463 } else {
2464 /* POWER7; init LPCR for virtual RMA mode */
2465 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2466 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2467 lpcr &= LPCR_PECE | LPCR_LPES;
2468 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
Paul Mackerras697d3892011-12-12 12:36:37 +00002469 LPCR_VPM0 | LPCR_VPM1;
2470 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2471 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrase0622bd2014-01-08 21:25:27 +11002472 /* On POWER8 turn on online bit to enable PURR/SPURR */
2473 if (cpu_has_feature(CPU_FTR_ARCH_207S))
2474 lpcr |= LPCR_ONL;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002475 }
2476 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002477
Paul Mackerras342d3db2011-12-12 12:38:05 +00002478 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002479 spin_lock_init(&kvm->arch.slot_phys_lock);
Paul Mackerras512691d2012-10-15 01:15:41 +00002480
2481 /*
Michael Ellerman441c19c2014-05-23 18:15:25 +10002482 * Track that we now have a HV mode VM active. This blocks secondary
2483 * CPU threads from coming online.
Paul Mackerras512691d2012-10-15 01:15:41 +00002484 */
Michael Ellerman441c19c2014-05-23 18:15:25 +10002485 kvm_hv_vm_activated();
Paul Mackerras512691d2012-10-15 01:15:41 +00002486
David Gibson54738c02011-06-29 00:22:41 +00002487 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002488}
2489
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302490static void kvmppc_free_vcores(struct kvm *kvm)
2491{
2492 long int i;
2493
Stewart Smith9678cda2014-07-18 14:18:43 +10002494 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2495 if (kvm->arch.vcores[i] && kvm->arch.vcores[i]->mpp_buffer) {
2496 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2497 free_pages((unsigned long)vc->mpp_buffer,
2498 MPP_BUFFER_ORDER);
2499 }
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302500 kfree(kvm->arch.vcores[i]);
Stewart Smith9678cda2014-07-18 14:18:43 +10002501 }
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302502 kvm->arch.online_vcores = 0;
2503}
2504
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302505static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002506{
Michael Ellerman441c19c2014-05-23 18:15:25 +10002507 kvm_hv_vm_deactivated();
Paul Mackerras512691d2012-10-15 01:15:41 +00002508
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302509 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002510 if (kvm->arch.rma) {
2511 kvm_release_rma(kvm->arch.rma);
2512 kvm->arch.rma = NULL;
2513 }
2514
Paul Mackerrasde56a942011-06-29 00:21:34 +00002515 kvmppc_free_hpt(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002516}
2517
2518/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302519static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2520 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002521{
2522 return EMULATE_FAIL;
2523}
2524
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302525static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2526 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002527{
2528 return EMULATE_FAIL;
2529}
2530
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302531static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2532 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002533{
2534 return EMULATE_FAIL;
2535}
2536
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302537static int kvmppc_core_check_processor_compat_hv(void)
2538{
2539 if (!cpu_has_feature(CPU_FTR_HVMODE))
2540 return -EIO;
2541 return 0;
2542}
2543
2544static long kvm_arch_vm_ioctl_hv(struct file *filp,
2545 unsigned int ioctl, unsigned long arg)
2546{
2547 struct kvm *kvm __maybe_unused = filp->private_data;
2548 void __user *argp = (void __user *)arg;
2549 long r;
2550
2551 switch (ioctl) {
2552
2553 case KVM_ALLOCATE_RMA: {
2554 struct kvm_allocate_rma rma;
2555 struct kvm *kvm = filp->private_data;
2556
2557 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
2558 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
2559 r = -EFAULT;
2560 break;
2561 }
2562
2563 case KVM_PPC_ALLOCATE_HTAB: {
2564 u32 htab_order;
2565
2566 r = -EFAULT;
2567 if (get_user(htab_order, (u32 __user *)argp))
2568 break;
2569 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2570 if (r)
2571 break;
2572 r = -EFAULT;
2573 if (put_user(htab_order, (u32 __user *)argp))
2574 break;
2575 r = 0;
2576 break;
2577 }
2578
2579 case KVM_PPC_GET_HTAB_FD: {
2580 struct kvm_get_htab_fd ghf;
2581
2582 r = -EFAULT;
2583 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2584 break;
2585 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2586 break;
2587 }
2588
2589 default:
2590 r = -ENOTTY;
2591 }
2592
2593 return r;
2594}
2595
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002596/*
2597 * List of hcall numbers to enable by default.
2598 * For compatibility with old userspace, we enable by default
2599 * all hcalls that were implemented before the hcall-enabling
2600 * facility was added. Note this list should not include H_RTAS.
2601 */
2602static unsigned int default_hcall_list[] = {
2603 H_REMOVE,
2604 H_ENTER,
2605 H_READ,
2606 H_PROTECT,
2607 H_BULK_REMOVE,
2608 H_GET_TCE,
2609 H_PUT_TCE,
2610 H_SET_DABR,
2611 H_SET_XDABR,
2612 H_CEDE,
2613 H_PROD,
2614 H_CONFER,
2615 H_REGISTER_VPA,
2616#ifdef CONFIG_KVM_XICS
2617 H_EOI,
2618 H_CPPR,
2619 H_IPI,
2620 H_IPOLL,
2621 H_XIRR,
2622 H_XIRR_X,
2623#endif
2624 0
2625};
2626
2627static void init_default_hcalls(void)
2628{
2629 int i;
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002630 unsigned int hcall;
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002631
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002632 for (i = 0; default_hcall_list[i]; ++i) {
2633 hcall = default_hcall_list[i];
2634 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
2635 __set_bit(hcall / 4, default_enabled_hcalls);
2636 }
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002637}
2638
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302639static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302640 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2641 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2642 .get_one_reg = kvmppc_get_one_reg_hv,
2643 .set_one_reg = kvmppc_set_one_reg_hv,
2644 .vcpu_load = kvmppc_core_vcpu_load_hv,
2645 .vcpu_put = kvmppc_core_vcpu_put_hv,
2646 .set_msr = kvmppc_set_msr_hv,
2647 .vcpu_run = kvmppc_vcpu_run_hv,
2648 .vcpu_create = kvmppc_core_vcpu_create_hv,
2649 .vcpu_free = kvmppc_core_vcpu_free_hv,
2650 .check_requests = kvmppc_core_check_requests_hv,
2651 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2652 .flush_memslot = kvmppc_core_flush_memslot_hv,
2653 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2654 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2655 .unmap_hva = kvm_unmap_hva_hv,
2656 .unmap_hva_range = kvm_unmap_hva_range_hv,
2657 .age_hva = kvm_age_hva_hv,
2658 .test_age_hva = kvm_test_age_hva_hv,
2659 .set_spte_hva = kvm_set_spte_hva_hv,
2660 .mmu_destroy = kvmppc_mmu_destroy_hv,
2661 .free_memslot = kvmppc_core_free_memslot_hv,
2662 .create_memslot = kvmppc_core_create_memslot_hv,
2663 .init_vm = kvmppc_core_init_vm_hv,
2664 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302665 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2666 .emulate_op = kvmppc_core_emulate_op_hv,
2667 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2668 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2669 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2670 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002671 .hcall_implemented = kvmppc_hcall_impl_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302672};
2673
2674static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002675{
2676 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302677 /*
2678 * FIXME!! Do we need to check on all cpus ?
2679 */
2680 r = kvmppc_core_check_processor_compat_hv();
2681 if (r < 0)
Paul Mackerras739e2422014-03-25 10:47:05 +11002682 return -ENODEV;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002683
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302684 kvm_ops_hv.owner = THIS_MODULE;
2685 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002686
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002687 init_default_hcalls();
2688
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302689 r = kvmppc_mmu_hv_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002690 return r;
2691}
2692
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302693static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002694{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302695 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002696}
2697
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302698module_init(kvmppc_book3s_init_hv);
2699module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05302700MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01002701MODULE_ALIAS_MISCDEV(KVM_MINOR);
2702MODULE_ALIAS("devname:kvm");