blob: de4018a1bc4bd290ecd0e970bd51960b281497c3 [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
Suresh E. Warrier3c78f782014-12-03 18:48:10 -060061#define CREATE_TRACE_POINTS
62#include "trace_hv.h"
63
Paul Mackerrasde56a942011-06-29 00:21:34 +000064/* #define EXIT_DEBUG */
65/* #define EXIT_DEBUG_SIMPLE */
66/* #define EXIT_DEBUG_INT */
67
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000068/* Used to indicate that a guest page fault needs to be handled */
69#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
70
Paul Mackerrasc7b67672012-10-15 01:18:07 +000071/* Used as a "null" value for timebase values */
72#define TB_NIL (~(u64)0)
73
Paul Mackerras699a0ea2014-06-02 11:02:59 +100074static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
75
Stewart Smith9678cda2014-07-18 14:18:43 +100076#if defined(CONFIG_PPC_64K_PAGES)
77#define MPP_BUFFER_ORDER 0
78#elif defined(CONFIG_PPC_4K_PAGES)
79#define MPP_BUFFER_ORDER 3
80#endif
81
82
Paul Mackerras19ccb762011-07-23 17:42:46 +100083static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +000084static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +100085
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053086static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +000087{
88 int me;
89 int cpu = vcpu->cpu;
90 wait_queue_head_t *wqp;
91
92 wqp = kvm_arch_vcpu_wq(vcpu);
93 if (waitqueue_active(wqp)) {
94 wake_up_interruptible(wqp);
95 ++vcpu->stat.halt_wakeup;
96 }
97
98 me = get_cpu();
99
100 /* CPU points to the first thread of the core */
101 if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
Anton Blanchard75052582014-03-25 10:47:01 +1100102#ifdef CONFIG_PPC_ICP_NATIVE
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000103 int real_cpu = cpu + vcpu->arch.ptid;
104 if (paca[real_cpu].kvm_hstate.xics_phys)
105 xics_wake_cpu(real_cpu);
Andreas Schwab48eaef02013-12-30 15:36:56 +0100106 else
107#endif
108 if (cpu_online(cpu))
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000109 smp_send_reschedule(cpu);
110 }
111 put_cpu();
112}
113
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000114/*
115 * We use the vcpu_load/put functions to measure stolen time.
116 * Stolen time is counted as time when either the vcpu is able to
117 * run as part of a virtual core, but the task running the vcore
118 * is preempted or sleeping, or when the vcpu needs something done
119 * in the kernel by the task running the vcpu, but that task is
120 * preempted or sleeping. Those two things have to be counted
121 * separately, since one of the vcpu tasks will take on the job
122 * of running the core, and the other vcpu tasks in the vcore will
123 * sleep waiting for it to do that, but that sleep shouldn't count
124 * as stolen time.
125 *
126 * Hence we accumulate stolen time when the vcpu can run as part of
127 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
128 * needs its task to do other things in the kernel (for example,
129 * service a page fault) in busy_stolen. We don't accumulate
130 * stolen time for a vcore when it is inactive, or for a vcpu
131 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
132 * a misnomer; it means that the vcpu task is not executing in
133 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
134 * the kernel. We don't have any way of dividing up that time
135 * between time that the vcpu is genuinely stopped, time that
136 * the task is actively working on behalf of the vcpu, and time
137 * that the task is preempted, so we don't count any of it as
138 * stolen.
139 *
140 * Updates to busy_stolen are protected by arch.tbacct_lock;
Paul Mackerras2711e242014-12-04 16:43:28 +1100141 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
142 * lock. The stolen times are measured in units of timebase ticks.
143 * (Note that the != TB_NIL checks below are purely defensive;
144 * they should never fail.)
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000145 */
146
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530147static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000148{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000149 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100150 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000151
Paul Mackerras2711e242014-12-04 16:43:28 +1100152 /*
153 * We can test vc->runner without taking the vcore lock,
154 * because only this task ever sets vc->runner to this
155 * vcpu, and once it is set to this vcpu, only this task
156 * ever sets it to NULL.
157 */
158 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE) {
159 spin_lock_irqsave(&vc->stoltb_lock, flags);
160 if (vc->preempt_tb != TB_NIL) {
161 vc->stolen_tb += mftb() - vc->preempt_tb;
162 vc->preempt_tb = TB_NIL;
163 }
164 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000165 }
Paul Mackerras2711e242014-12-04 16:43:28 +1100166 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000167 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
168 vcpu->arch.busy_preempt != TB_NIL) {
169 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
170 vcpu->arch.busy_preempt = TB_NIL;
171 }
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100172 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000173}
174
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530175static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000176{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000177 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100178 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000179
Paul Mackerras2711e242014-12-04 16:43:28 +1100180 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE) {
181 spin_lock_irqsave(&vc->stoltb_lock, flags);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000182 vc->preempt_tb = mftb();
Paul Mackerras2711e242014-12-04 16:43:28 +1100183 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
184 }
185 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000186 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
187 vcpu->arch.busy_preempt = mftb();
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100188 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000189}
190
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530191static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000192{
193 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000194 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000195}
196
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530197void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000198{
199 vcpu->arch.pvr = pvr;
200}
201
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000202int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
203{
204 unsigned long pcr = 0;
205 struct kvmppc_vcore *vc = vcpu->arch.vcore;
206
207 if (arch_compat) {
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000208 switch (arch_compat) {
209 case PVR_ARCH_205:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100210 /*
211 * If an arch bit is set in PCR, all the defined
212 * higher-order arch bits also have to be set.
213 */
214 pcr = PCR_ARCH_206 | PCR_ARCH_205;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000215 break;
216 case PVR_ARCH_206:
217 case PVR_ARCH_206p:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100218 pcr = PCR_ARCH_206;
219 break;
220 case PVR_ARCH_207:
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000221 break;
222 default:
223 return -EINVAL;
224 }
Paul Mackerras5557ae02014-01-08 21:25:24 +1100225
226 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
227 /* POWER7 can't emulate POWER8 */
228 if (!(pcr & PCR_ARCH_206))
229 return -EINVAL;
230 pcr &= ~PCR_ARCH_206;
231 }
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000232 }
233
234 spin_lock(&vc->lock);
235 vc->arch_compat = arch_compat;
236 vc->pcr = pcr;
237 spin_unlock(&vc->lock);
238
239 return 0;
240}
241
Paul Mackerrasde56a942011-06-29 00:21:34 +0000242void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
243{
244 int r;
245
246 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
247 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
248 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
249 for (r = 0; r < 16; ++r)
250 pr_err("r%2d = %.16lx r%d = %.16lx\n",
251 r, kvmppc_get_gpr(vcpu, r),
252 r+16, kvmppc_get_gpr(vcpu, r+16));
253 pr_err("ctr = %.16lx lr = %.16lx\n",
254 vcpu->arch.ctr, vcpu->arch.lr);
255 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
256 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
257 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
258 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
259 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
260 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
261 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
262 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
263 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
264 pr_err("fault dar = %.16lx dsisr = %.8x\n",
265 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
266 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
267 for (r = 0; r < vcpu->arch.slb_max; ++r)
268 pr_err(" ESID = %.16llx VSID = %.16llx\n",
269 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
270 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000271 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000272 vcpu->arch.last_inst);
273}
274
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000275struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
276{
277 int r;
278 struct kvm_vcpu *v, *ret = NULL;
279
280 mutex_lock(&kvm->lock);
281 kvm_for_each_vcpu(r, v, kvm) {
282 if (v->vcpu_id == id) {
283 ret = v;
284 break;
285 }
286 }
287 mutex_unlock(&kvm->lock);
288 return ret;
289}
290
291static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
292{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000293 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Alexander Graf02407552014-06-11 10:34:19 +0200294 vpa->yield_count = cpu_to_be32(1);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000295}
296
Paul Mackerras55b665b2012-09-25 20:33:06 +0000297static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
298 unsigned long addr, unsigned long len)
299{
300 /* check address is cacheline aligned */
301 if (addr & (L1_CACHE_BYTES - 1))
302 return -EINVAL;
303 spin_lock(&vcpu->arch.vpa_update_lock);
304 if (v->next_gpa != addr || v->len != len) {
305 v->next_gpa = addr;
306 v->len = addr ? len : 0;
307 v->update_pending = 1;
308 }
309 spin_unlock(&vcpu->arch.vpa_update_lock);
310 return 0;
311}
312
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000313/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
314struct reg_vpa {
315 u32 dummy;
316 union {
Alexander Graf02407552014-06-11 10:34:19 +0200317 __be16 hword;
318 __be32 word;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000319 } length;
320};
321
322static int vpa_is_registered(struct kvmppc_vpa *vpap)
323{
324 if (vpap->update_pending)
325 return vpap->next_gpa != 0;
326 return vpap->pinned_addr != NULL;
327}
328
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000329static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
330 unsigned long flags,
331 unsigned long vcpuid, unsigned long vpa)
332{
333 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000334 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000335 void *va;
336 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000337 int err;
338 int subfunc;
339 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000340
341 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
342 if (!tvcpu)
343 return H_PARAMETER;
344
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000345 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
346 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
347 subfunc == H_VPA_REG_SLB) {
348 /* Registering new area - address must be cache-line aligned */
349 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000350 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000351
352 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000353 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
354 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000355 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000356 if (subfunc == H_VPA_REG_VPA)
Alexander Graf02407552014-06-11 10:34:19 +0200357 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000358 else
Alexander Graf02407552014-06-11 10:34:19 +0200359 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000360 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000361
362 /* Check length */
363 if (len > nb || len < sizeof(struct reg_vpa))
364 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000365 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000366 vpa = 0;
367 len = 0;
368 }
369
370 err = H_PARAMETER;
371 vpap = NULL;
372 spin_lock(&tvcpu->arch.vpa_update_lock);
373
374 switch (subfunc) {
375 case H_VPA_REG_VPA: /* register VPA */
376 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000377 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000378 vpap = &tvcpu->arch.vpa;
379 err = 0;
380 break;
381
382 case H_VPA_REG_DTL: /* register DTL */
383 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000384 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000385 len -= len % sizeof(struct dtl_entry);
386
387 /* Check that they have previously registered a VPA */
388 err = H_RESOURCE;
389 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000390 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000391
392 vpap = &tvcpu->arch.dtl;
393 err = 0;
394 break;
395
396 case H_VPA_REG_SLB: /* register SLB shadow buffer */
397 /* Check that they have previously registered a VPA */
398 err = H_RESOURCE;
399 if (!vpa_is_registered(&tvcpu->arch.vpa))
400 break;
401
402 vpap = &tvcpu->arch.slb_shadow;
403 err = 0;
404 break;
405
406 case H_VPA_DEREG_VPA: /* deregister VPA */
407 /* Check they don't still have a DTL or SLB buf registered */
408 err = H_RESOURCE;
409 if (vpa_is_registered(&tvcpu->arch.dtl) ||
410 vpa_is_registered(&tvcpu->arch.slb_shadow))
411 break;
412
413 vpap = &tvcpu->arch.vpa;
414 err = 0;
415 break;
416
417 case H_VPA_DEREG_DTL: /* deregister DTL */
418 vpap = &tvcpu->arch.dtl;
419 err = 0;
420 break;
421
422 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
423 vpap = &tvcpu->arch.slb_shadow;
424 err = 0;
425 break;
426 }
427
428 if (vpap) {
429 vpap->next_gpa = vpa;
430 vpap->len = len;
431 vpap->update_pending = 1;
432 }
433
434 spin_unlock(&tvcpu->arch.vpa_update_lock);
435
436 return err;
437}
438
Paul Mackerras081f3232012-06-01 20:20:24 +1000439static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000440{
Paul Mackerras081f3232012-06-01 20:20:24 +1000441 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000442 void *va;
443 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000444 unsigned long gpa;
445
446 /*
447 * We need to pin the page pointed to by vpap->next_gpa,
448 * but we can't call kvmppc_pin_guest_page under the lock
449 * as it does get_user_pages() and down_read(). So we
450 * have to drop the lock, pin the page, then get the lock
451 * again and check that a new area didn't get registered
452 * in the meantime.
453 */
454 for (;;) {
455 gpa = vpap->next_gpa;
456 spin_unlock(&vcpu->arch.vpa_update_lock);
457 va = NULL;
458 nb = 0;
459 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000460 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000461 spin_lock(&vcpu->arch.vpa_update_lock);
462 if (gpa == vpap->next_gpa)
463 break;
464 /* sigh... unpin that one and try again */
465 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000466 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000467 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000468
469 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000470 if (va && nb < vpap->len) {
471 /*
472 * If it's now too short, it must be that userspace
473 * has changed the mappings underlying guest memory,
474 * so unregister the region.
475 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000476 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000477 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000478 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000479 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000480 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
481 vpap->dirty);
482 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000483 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000484 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000485 if (va)
486 vpap->pinned_end = va + vpap->len;
487}
Paul Mackerras93e60242011-12-12 12:28:55 +0000488
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000489static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
490{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000491 if (!(vcpu->arch.vpa.update_pending ||
492 vcpu->arch.slb_shadow.update_pending ||
493 vcpu->arch.dtl.update_pending))
494 return;
495
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000496 spin_lock(&vcpu->arch.vpa_update_lock);
497 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000498 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000499 if (vcpu->arch.vpa.pinned_addr)
500 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000501 }
502 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000503 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000504 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
505 vcpu->arch.dtl_index = 0;
506 }
507 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000508 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000509 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000510}
511
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000512/*
513 * Return the accumulated stolen time for the vcore up until `now'.
514 * The caller should hold the vcore lock.
515 */
516static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
517{
518 u64 p;
Paul Mackerras2711e242014-12-04 16:43:28 +1100519 unsigned long flags;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000520
Paul Mackerras2711e242014-12-04 16:43:28 +1100521 spin_lock_irqsave(&vc->stoltb_lock, flags);
522 p = vc->stolen_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000523 if (vc->vcore_state != VCORE_INACTIVE &&
Paul Mackerras2711e242014-12-04 16:43:28 +1100524 vc->preempt_tb != TB_NIL)
525 p += now - vc->preempt_tb;
526 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000527 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
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100610static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
611{
612 struct kvmppc_vcore *vcore = target->arch.vcore;
613
614 /*
615 * We expect to have been called by the real mode handler
616 * (kvmppc_rm_h_confer()) which would have directly returned
617 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
618 * have useful work to do and should not confer) so we don't
619 * recheck that here.
620 */
621
622 spin_lock(&vcore->lock);
623 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
624 vcore->vcore_state != VCORE_INACTIVE)
625 target = vcore->runner;
626 spin_unlock(&vcore->lock);
627
628 return kvm_vcpu_yield_to(target);
629}
630
631static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
632{
633 int yield_count = 0;
634 struct lppaca *lppaca;
635
636 spin_lock(&vcpu->arch.vpa_update_lock);
637 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
638 if (lppaca)
639 yield_count = lppaca->yield_count;
640 spin_unlock(&vcpu->arch.vpa_update_lock);
641 return yield_count;
642}
643
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000644int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
645{
646 unsigned long req = kvmppc_get_gpr(vcpu, 3);
647 unsigned long target, ret = H_SUCCESS;
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100648 int yield_count;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000649 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000650 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000651
Paul Mackerras699a0ea2014-06-02 11:02:59 +1000652 if (req <= MAX_HCALL_OPCODE &&
653 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
654 return RESUME_HOST;
655
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000656 switch (req) {
657 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000658 break;
659 case H_PROD:
660 target = kvmppc_get_gpr(vcpu, 4);
661 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
662 if (!tvcpu) {
663 ret = H_PARAMETER;
664 break;
665 }
666 tvcpu->arch.prodded = 1;
667 smp_mb();
668 if (vcpu->arch.ceded) {
669 if (waitqueue_active(&vcpu->wq)) {
670 wake_up_interruptible(&vcpu->wq);
671 vcpu->stat.halt_wakeup++;
672 }
673 }
674 break;
675 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +1000676 target = kvmppc_get_gpr(vcpu, 4);
677 if (target == -1)
678 break;
679 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
680 if (!tvcpu) {
681 ret = H_PARAMETER;
682 break;
683 }
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100684 yield_count = kvmppc_get_gpr(vcpu, 5);
685 if (kvmppc_get_yield_count(tvcpu) != yield_count)
686 break;
687 kvm_arch_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000688 break;
689 case H_REGISTER_VPA:
690 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
691 kvmppc_get_gpr(vcpu, 5),
692 kvmppc_get_gpr(vcpu, 6));
693 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000694 case H_RTAS:
695 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
696 return RESUME_HOST;
697
Paul Mackerrasc9438092013-11-16 17:46:05 +1100698 idx = srcu_read_lock(&vcpu->kvm->srcu);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000699 rc = kvmppc_rtas_hcall(vcpu);
Paul Mackerrasc9438092013-11-16 17:46:05 +1100700 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000701
702 if (rc == -ENOENT)
703 return RESUME_HOST;
704 else if (rc == 0)
705 break;
706
707 /* Send the error out to userspace via KVM_RUN */
708 return rc;
Michael Neuling96423822014-06-02 11:03:01 +1000709 case H_SET_MODE:
710 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
711 kvmppc_get_gpr(vcpu, 5),
712 kvmppc_get_gpr(vcpu, 6),
713 kvmppc_get_gpr(vcpu, 7));
714 if (ret == H_TOO_HARD)
715 return RESUME_HOST;
716 break;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000717 case H_XIRR:
718 case H_CPPR:
719 case H_EOI:
720 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000721 case H_IPOLL:
722 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000723 if (kvmppc_xics_enabled(vcpu)) {
724 ret = kvmppc_xics_hcall(vcpu, req);
725 break;
726 } /* fallthrough */
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000727 default:
728 return RESUME_HOST;
729 }
730 kvmppc_set_gpr(vcpu, 3, ret);
731 vcpu->arch.hcall_needed = 0;
732 return RESUME_GUEST;
733}
734
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000735static int kvmppc_hcall_impl_hv(unsigned long cmd)
736{
737 switch (cmd) {
738 case H_CEDE:
739 case H_PROD:
740 case H_CONFER:
741 case H_REGISTER_VPA:
Michael Neuling96423822014-06-02 11:03:01 +1000742 case H_SET_MODE:
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000743#ifdef CONFIG_KVM_XICS
744 case H_XIRR:
745 case H_CPPR:
746 case H_EOI:
747 case H_IPI:
748 case H_IPOLL:
749 case H_XIRR_X:
750#endif
751 return 1;
752 }
753
754 /* See if it's in the real-mode table */
755 return kvmppc_hcall_impl_hv_realmode(cmd);
756}
757
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530758static int kvmppc_emulate_debug_inst(struct kvm_run *run,
759 struct kvm_vcpu *vcpu)
760{
761 u32 last_inst;
762
763 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
764 EMULATE_DONE) {
765 /*
766 * Fetch failed, so return to guest and
767 * try executing it again.
768 */
769 return RESUME_GUEST;
770 }
771
772 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
773 run->exit_reason = KVM_EXIT_DEBUG;
774 run->debug.arch.address = kvmppc_get_pc(vcpu);
775 return RESUME_HOST;
776 } else {
777 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
778 return RESUME_GUEST;
779 }
780}
781
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530782static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
783 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000784{
785 int r = RESUME_HOST;
786
787 vcpu->stat.sum_exits++;
788
789 run->exit_reason = KVM_EXIT_UNKNOWN;
790 run->ready_for_interrupt_injection = 1;
791 switch (vcpu->arch.trap) {
792 /* We're good on these - the host merely wanted to get our attention */
793 case BOOK3S_INTERRUPT_HV_DECREMENTER:
794 vcpu->stat.dec_exits++;
795 r = RESUME_GUEST;
796 break;
797 case BOOK3S_INTERRUPT_EXTERNAL:
Paul Mackerras5d00f662014-01-08 21:25:28 +1100798 case BOOK3S_INTERRUPT_H_DOORBELL:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000799 vcpu->stat.ext_intr_exits++;
800 r = RESUME_GUEST;
801 break;
Mahesh Salgaonkardee6f242014-11-03 15:51:57 +1100802 /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
803 case BOOK3S_INTERRUPT_HMI:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000804 case BOOK3S_INTERRUPT_PERFMON:
805 r = RESUME_GUEST;
806 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000807 case BOOK3S_INTERRUPT_MACHINE_CHECK:
808 /*
809 * Deliver a machine check interrupt to the guest.
810 * We have to do this, even if the host has handled the
811 * machine check, because machine checks use SRR0/1 and
812 * the interrupt might have trashed guest state in them.
813 */
814 kvmppc_book3s_queue_irqprio(vcpu,
815 BOOK3S_INTERRUPT_MACHINE_CHECK);
816 r = RESUME_GUEST;
817 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000818 case BOOK3S_INTERRUPT_PROGRAM:
819 {
820 ulong flags;
821 /*
822 * Normally program interrupts are delivered directly
823 * to the guest by the hardware, but we can get here
824 * as a result of a hypervisor emulation interrupt
825 * (e40) getting turned into a 700 by BML RTAS.
826 */
827 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
828 kvmppc_core_queue_program(vcpu, flags);
829 r = RESUME_GUEST;
830 break;
831 }
832 case BOOK3S_INTERRUPT_SYSCALL:
833 {
834 /* hcall - punt to userspace */
835 int i;
836
Liu Ping Fan27025a62013-11-19 14:12:48 +0800837 /* hypercall with MSR_PR has already been handled in rmode,
838 * and never reaches here.
839 */
840
Paul Mackerrasde56a942011-06-29 00:21:34 +0000841 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
842 for (i = 0; i < 9; ++i)
843 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
844 run->exit_reason = KVM_EXIT_PAPR_HCALL;
845 vcpu->arch.hcall_needed = 1;
846 r = RESUME_HOST;
847 break;
848 }
849 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000850 * We get these next two if the guest accesses a page which it thinks
851 * it has mapped but which is not actually present, either because
852 * it is for an emulated I/O device or because the corresonding
853 * host page has been paged out. Any other HDSI/HISI interrupts
854 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000855 */
856 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000857 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000858 break;
859 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000860 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
861 vcpu->arch.fault_dsisr = 0;
862 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000863 break;
864 /*
865 * This occurs if the guest executes an illegal instruction.
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530866 * If the guest debug is disabled, generate a program interrupt
867 * to the guest. If guest debug is enabled, we need to check
868 * whether the instruction is a software breakpoint instruction.
869 * Accordingly return to Guest or Host.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000870 */
871 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Paul Mackerras4a157d62014-12-03 13:30:39 +1100872 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
873 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
874 swab32(vcpu->arch.emul_inst) :
875 vcpu->arch.emul_inst;
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530876 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
877 r = kvmppc_emulate_debug_inst(run, vcpu);
878 } else {
879 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
880 r = RESUME_GUEST;
881 }
Michael Ellermanbd3048b2014-01-08 21:25:23 +1100882 break;
883 /*
884 * This occurs if the guest (kernel or userspace), does something that
885 * is prohibited by HFSCR. We just generate a program interrupt to
886 * the guest.
887 */
888 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
889 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000890 r = RESUME_GUEST;
891 break;
892 default:
893 kvmppc_dump_regs(vcpu);
894 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
895 vcpu->arch.trap, kvmppc_get_pc(vcpu),
896 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +1000897 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000898 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000899 break;
900 }
901
Paul Mackerrasde56a942011-06-29 00:21:34 +0000902 return r;
903}
904
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530905static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
906 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000907{
908 int i;
909
Paul Mackerrasde56a942011-06-29 00:21:34 +0000910 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +0530911 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000912 for (i = 0; i < vcpu->arch.slb_max; i++) {
913 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
914 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
915 }
916
917 return 0;
918}
919
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530920static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
921 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000922{
923 int i, j;
924
Paul Mackerras9333e6c2014-09-02 16:14:43 +1000925 /* Only accept the same PVR as the host's, since we can't spoof it */
926 if (sregs->pvr != vcpu->arch.pvr)
927 return -EINVAL;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000928
929 j = 0;
930 for (i = 0; i < vcpu->arch.slb_nr; i++) {
931 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
932 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
933 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
934 ++j;
935 }
936 }
937 vcpu->arch.slb_max = j;
938
939 return 0;
940}
941
Alexey Kardashevskiya0840242014-07-19 17:59:34 +1000942static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
943 bool preserve_top32)
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000944{
945 struct kvmppc_vcore *vc = vcpu->arch.vcore;
946 u64 mask;
947
948 spin_lock(&vc->lock);
949 /*
Anton Blanchardd6829162014-01-08 21:25:30 +1100950 * If ILE (interrupt little-endian) has changed, update the
951 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
952 */
953 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
954 struct kvm *kvm = vcpu->kvm;
955 struct kvm_vcpu *vcpu;
956 int i;
957
958 mutex_lock(&kvm->lock);
959 kvm_for_each_vcpu(i, vcpu, kvm) {
960 if (vcpu->arch.vcore != vc)
961 continue;
962 if (new_lpcr & LPCR_ILE)
963 vcpu->arch.intr_msr |= MSR_LE;
964 else
965 vcpu->arch.intr_msr &= ~MSR_LE;
966 }
967 mutex_unlock(&kvm->lock);
968 }
969
970 /*
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000971 * Userspace can only modify DPFD (default prefetch depth),
972 * ILE (interrupt little-endian) and TC (translation control).
Paul Mackerrase0622bd2014-01-08 21:25:27 +1100973 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000974 */
975 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
Paul Mackerrase0622bd2014-01-08 21:25:27 +1100976 if (cpu_has_feature(CPU_FTR_ARCH_207S))
977 mask |= LPCR_AIL;
Alexey Kardashevskiya0840242014-07-19 17:59:34 +1000978
979 /* Broken 32-bit version of LPCR must not clear top bits */
980 if (preserve_top32)
981 mask &= 0xFFFFFFFF;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000982 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
983 spin_unlock(&vc->lock);
984}
985
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530986static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
987 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000988{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000989 int r = 0;
990 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +0000991
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000992 switch (id) {
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530993 case KVM_REG_PPC_DEBUG_INST:
994 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
995 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000996 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000997 *val = get_reg_val(id, 0);
998 break;
999 case KVM_REG_PPC_DABR:
1000 *val = get_reg_val(id, vcpu->arch.dabr);
1001 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001002 case KVM_REG_PPC_DABRX:
1003 *val = get_reg_val(id, vcpu->arch.dabrx);
1004 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001005 case KVM_REG_PPC_DSCR:
1006 *val = get_reg_val(id, vcpu->arch.dscr);
1007 break;
1008 case KVM_REG_PPC_PURR:
1009 *val = get_reg_val(id, vcpu->arch.purr);
1010 break;
1011 case KVM_REG_PPC_SPURR:
1012 *val = get_reg_val(id, vcpu->arch.spurr);
1013 break;
1014 case KVM_REG_PPC_AMR:
1015 *val = get_reg_val(id, vcpu->arch.amr);
1016 break;
1017 case KVM_REG_PPC_UAMOR:
1018 *val = get_reg_val(id, vcpu->arch.uamor);
1019 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001020 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001021 i = id - KVM_REG_PPC_MMCR0;
1022 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
1023 break;
1024 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1025 i = id - KVM_REG_PPC_PMC1;
1026 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +00001027 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001028 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1029 i = id - KVM_REG_PPC_SPMC1;
1030 *val = get_reg_val(id, vcpu->arch.spmc[i]);
1031 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001032 case KVM_REG_PPC_SIAR:
1033 *val = get_reg_val(id, vcpu->arch.siar);
1034 break;
1035 case KVM_REG_PPC_SDAR:
1036 *val = get_reg_val(id, vcpu->arch.sdar);
1037 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001038 case KVM_REG_PPC_SIER:
1039 *val = get_reg_val(id, vcpu->arch.sier);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001040 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001041 case KVM_REG_PPC_IAMR:
1042 *val = get_reg_val(id, vcpu->arch.iamr);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001043 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001044 case KVM_REG_PPC_PSPB:
1045 *val = get_reg_val(id, vcpu->arch.pspb);
1046 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001047 case KVM_REG_PPC_DPDES:
1048 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1049 break;
1050 case KVM_REG_PPC_DAWR:
1051 *val = get_reg_val(id, vcpu->arch.dawr);
1052 break;
1053 case KVM_REG_PPC_DAWRX:
1054 *val = get_reg_val(id, vcpu->arch.dawrx);
1055 break;
1056 case KVM_REG_PPC_CIABR:
1057 *val = get_reg_val(id, vcpu->arch.ciabr);
1058 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001059 case KVM_REG_PPC_CSIGR:
1060 *val = get_reg_val(id, vcpu->arch.csigr);
1061 break;
1062 case KVM_REG_PPC_TACR:
1063 *val = get_reg_val(id, vcpu->arch.tacr);
1064 break;
1065 case KVM_REG_PPC_TCSCR:
1066 *val = get_reg_val(id, vcpu->arch.tcscr);
1067 break;
1068 case KVM_REG_PPC_PID:
1069 *val = get_reg_val(id, vcpu->arch.pid);
1070 break;
1071 case KVM_REG_PPC_ACOP:
1072 *val = get_reg_val(id, vcpu->arch.acop);
1073 break;
1074 case KVM_REG_PPC_WORT:
1075 *val = get_reg_val(id, vcpu->arch.wort);
1076 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001077 case KVM_REG_PPC_VPA_ADDR:
1078 spin_lock(&vcpu->arch.vpa_update_lock);
1079 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1080 spin_unlock(&vcpu->arch.vpa_update_lock);
1081 break;
1082 case KVM_REG_PPC_VPA_SLB:
1083 spin_lock(&vcpu->arch.vpa_update_lock);
1084 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1085 val->vpaval.length = vcpu->arch.slb_shadow.len;
1086 spin_unlock(&vcpu->arch.vpa_update_lock);
1087 break;
1088 case KVM_REG_PPC_VPA_DTL:
1089 spin_lock(&vcpu->arch.vpa_update_lock);
1090 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1091 val->vpaval.length = vcpu->arch.dtl.len;
1092 spin_unlock(&vcpu->arch.vpa_update_lock);
1093 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001094 case KVM_REG_PPC_TB_OFFSET:
1095 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1096 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001097 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001098 case KVM_REG_PPC_LPCR_64:
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001099 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1100 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001101 case KVM_REG_PPC_PPR:
1102 *val = get_reg_val(id, vcpu->arch.ppr);
1103 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001104#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1105 case KVM_REG_PPC_TFHAR:
1106 *val = get_reg_val(id, vcpu->arch.tfhar);
1107 break;
1108 case KVM_REG_PPC_TFIAR:
1109 *val = get_reg_val(id, vcpu->arch.tfiar);
1110 break;
1111 case KVM_REG_PPC_TEXASR:
1112 *val = get_reg_val(id, vcpu->arch.texasr);
1113 break;
1114 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1115 i = id - KVM_REG_PPC_TM_GPR0;
1116 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1117 break;
1118 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1119 {
1120 int j;
1121 i = id - KVM_REG_PPC_TM_VSR0;
1122 if (i < 32)
1123 for (j = 0; j < TS_FPRWIDTH; j++)
1124 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1125 else {
1126 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1127 val->vval = vcpu->arch.vr_tm.vr[i-32];
1128 else
1129 r = -ENXIO;
1130 }
1131 break;
1132 }
1133 case KVM_REG_PPC_TM_CR:
1134 *val = get_reg_val(id, vcpu->arch.cr_tm);
1135 break;
1136 case KVM_REG_PPC_TM_LR:
1137 *val = get_reg_val(id, vcpu->arch.lr_tm);
1138 break;
1139 case KVM_REG_PPC_TM_CTR:
1140 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1141 break;
1142 case KVM_REG_PPC_TM_FPSCR:
1143 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1144 break;
1145 case KVM_REG_PPC_TM_AMR:
1146 *val = get_reg_val(id, vcpu->arch.amr_tm);
1147 break;
1148 case KVM_REG_PPC_TM_PPR:
1149 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1150 break;
1151 case KVM_REG_PPC_TM_VRSAVE:
1152 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1153 break;
1154 case KVM_REG_PPC_TM_VSCR:
1155 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1156 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1157 else
1158 r = -ENXIO;
1159 break;
1160 case KVM_REG_PPC_TM_DSCR:
1161 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1162 break;
1163 case KVM_REG_PPC_TM_TAR:
1164 *val = get_reg_val(id, vcpu->arch.tar_tm);
1165 break;
1166#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001167 case KVM_REG_PPC_ARCH_COMPAT:
1168 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1169 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001170 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001171 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001172 break;
1173 }
1174
1175 return r;
1176}
1177
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301178static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1179 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001180{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001181 int r = 0;
1182 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001183 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +00001184
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001185 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00001186 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +00001187 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001188 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +00001189 r = -EINVAL;
1190 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001191 case KVM_REG_PPC_DABR:
1192 vcpu->arch.dabr = set_reg_val(id, *val);
1193 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001194 case KVM_REG_PPC_DABRX:
1195 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1196 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001197 case KVM_REG_PPC_DSCR:
1198 vcpu->arch.dscr = set_reg_val(id, *val);
1199 break;
1200 case KVM_REG_PPC_PURR:
1201 vcpu->arch.purr = set_reg_val(id, *val);
1202 break;
1203 case KVM_REG_PPC_SPURR:
1204 vcpu->arch.spurr = set_reg_val(id, *val);
1205 break;
1206 case KVM_REG_PPC_AMR:
1207 vcpu->arch.amr = set_reg_val(id, *val);
1208 break;
1209 case KVM_REG_PPC_UAMOR:
1210 vcpu->arch.uamor = set_reg_val(id, *val);
1211 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001212 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001213 i = id - KVM_REG_PPC_MMCR0;
1214 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1215 break;
1216 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1217 i = id - KVM_REG_PPC_PMC1;
1218 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1219 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001220 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1221 i = id - KVM_REG_PPC_SPMC1;
1222 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1223 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001224 case KVM_REG_PPC_SIAR:
1225 vcpu->arch.siar = set_reg_val(id, *val);
1226 break;
1227 case KVM_REG_PPC_SDAR:
1228 vcpu->arch.sdar = set_reg_val(id, *val);
1229 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001230 case KVM_REG_PPC_SIER:
1231 vcpu->arch.sier = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001232 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001233 case KVM_REG_PPC_IAMR:
1234 vcpu->arch.iamr = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001235 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001236 case KVM_REG_PPC_PSPB:
1237 vcpu->arch.pspb = set_reg_val(id, *val);
1238 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001239 case KVM_REG_PPC_DPDES:
1240 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1241 break;
1242 case KVM_REG_PPC_DAWR:
1243 vcpu->arch.dawr = set_reg_val(id, *val);
1244 break;
1245 case KVM_REG_PPC_DAWRX:
1246 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1247 break;
1248 case KVM_REG_PPC_CIABR:
1249 vcpu->arch.ciabr = set_reg_val(id, *val);
1250 /* Don't allow setting breakpoints in hypervisor code */
1251 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1252 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1253 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001254 case KVM_REG_PPC_CSIGR:
1255 vcpu->arch.csigr = set_reg_val(id, *val);
1256 break;
1257 case KVM_REG_PPC_TACR:
1258 vcpu->arch.tacr = set_reg_val(id, *val);
1259 break;
1260 case KVM_REG_PPC_TCSCR:
1261 vcpu->arch.tcscr = set_reg_val(id, *val);
1262 break;
1263 case KVM_REG_PPC_PID:
1264 vcpu->arch.pid = set_reg_val(id, *val);
1265 break;
1266 case KVM_REG_PPC_ACOP:
1267 vcpu->arch.acop = set_reg_val(id, *val);
1268 break;
1269 case KVM_REG_PPC_WORT:
1270 vcpu->arch.wort = set_reg_val(id, *val);
1271 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001272 case KVM_REG_PPC_VPA_ADDR:
1273 addr = set_reg_val(id, *val);
1274 r = -EINVAL;
1275 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1276 vcpu->arch.dtl.next_gpa))
1277 break;
1278 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1279 break;
1280 case KVM_REG_PPC_VPA_SLB:
1281 addr = val->vpaval.addr;
1282 len = val->vpaval.length;
1283 r = -EINVAL;
1284 if (addr && !vcpu->arch.vpa.next_gpa)
1285 break;
1286 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1287 break;
1288 case KVM_REG_PPC_VPA_DTL:
1289 addr = val->vpaval.addr;
1290 len = val->vpaval.length;
1291 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +00001292 if (addr && (len < sizeof(struct dtl_entry) ||
1293 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +00001294 break;
1295 len -= len % sizeof(struct dtl_entry);
1296 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1297 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001298 case KVM_REG_PPC_TB_OFFSET:
1299 /* round up to multiple of 2^24 */
1300 vcpu->arch.vcore->tb_offset =
1301 ALIGN(set_reg_val(id, *val), 1UL << 24);
1302 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001303 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001304 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1305 break;
1306 case KVM_REG_PPC_LPCR_64:
1307 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001308 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001309 case KVM_REG_PPC_PPR:
1310 vcpu->arch.ppr = set_reg_val(id, *val);
1311 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001312#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1313 case KVM_REG_PPC_TFHAR:
1314 vcpu->arch.tfhar = set_reg_val(id, *val);
1315 break;
1316 case KVM_REG_PPC_TFIAR:
1317 vcpu->arch.tfiar = set_reg_val(id, *val);
1318 break;
1319 case KVM_REG_PPC_TEXASR:
1320 vcpu->arch.texasr = set_reg_val(id, *val);
1321 break;
1322 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1323 i = id - KVM_REG_PPC_TM_GPR0;
1324 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1325 break;
1326 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1327 {
1328 int j;
1329 i = id - KVM_REG_PPC_TM_VSR0;
1330 if (i < 32)
1331 for (j = 0; j < TS_FPRWIDTH; j++)
1332 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1333 else
1334 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1335 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1336 else
1337 r = -ENXIO;
1338 break;
1339 }
1340 case KVM_REG_PPC_TM_CR:
1341 vcpu->arch.cr_tm = set_reg_val(id, *val);
1342 break;
1343 case KVM_REG_PPC_TM_LR:
1344 vcpu->arch.lr_tm = set_reg_val(id, *val);
1345 break;
1346 case KVM_REG_PPC_TM_CTR:
1347 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1348 break;
1349 case KVM_REG_PPC_TM_FPSCR:
1350 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1351 break;
1352 case KVM_REG_PPC_TM_AMR:
1353 vcpu->arch.amr_tm = set_reg_val(id, *val);
1354 break;
1355 case KVM_REG_PPC_TM_PPR:
1356 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1357 break;
1358 case KVM_REG_PPC_TM_VRSAVE:
1359 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1360 break;
1361 case KVM_REG_PPC_TM_VSCR:
1362 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1363 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1364 else
1365 r = - ENXIO;
1366 break;
1367 case KVM_REG_PPC_TM_DSCR:
1368 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1369 break;
1370 case KVM_REG_PPC_TM_TAR:
1371 vcpu->arch.tar_tm = set_reg_val(id, *val);
1372 break;
1373#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001374 case KVM_REG_PPC_ARCH_COMPAT:
1375 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1376 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001377 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001378 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001379 break;
1380 }
1381
1382 return r;
1383}
1384
Stewart Smithde9bdd12014-07-18 14:18:42 +10001385static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1386{
1387 struct kvmppc_vcore *vcore;
1388
1389 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1390
1391 if (vcore == NULL)
1392 return NULL;
1393
1394 INIT_LIST_HEAD(&vcore->runnable_threads);
1395 spin_lock_init(&vcore->lock);
Paul Mackerras2711e242014-12-04 16:43:28 +11001396 spin_lock_init(&vcore->stoltb_lock);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001397 init_waitqueue_head(&vcore->wq);
1398 vcore->preempt_tb = TB_NIL;
1399 vcore->lpcr = kvm->arch.lpcr;
1400 vcore->first_vcpuid = core * threads_per_subcore;
1401 vcore->kvm = kvm;
1402
Stewart Smith9678cda2014-07-18 14:18:43 +10001403 vcore->mpp_buffer_is_valid = false;
1404
1405 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1406 vcore->mpp_buffer = (void *)__get_free_pages(
1407 GFP_KERNEL|__GFP_ZERO,
1408 MPP_BUFFER_ORDER);
1409
Stewart Smithde9bdd12014-07-18 14:18:42 +10001410 return vcore;
1411}
1412
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301413static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1414 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001415{
1416 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001417 int err = -EINVAL;
1418 int core;
1419 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001420
Michael Ellerman3102f782014-05-23 18:15:29 +10001421 core = id / threads_per_subcore;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001422 if (core >= KVM_MAX_VCORES)
1423 goto out;
1424
1425 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001426 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001427 if (!vcpu)
1428 goto out;
1429
1430 err = kvm_vcpu_init(vcpu, kvm, id);
1431 if (err)
1432 goto free_vcpu;
1433
1434 vcpu->arch.shared = &vcpu->arch.shregs;
Alexander Graf5deb8e72014-04-24 13:46:24 +02001435#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1436 /*
1437 * The shared struct is never shared on HV,
1438 * so we can always use host endianness
1439 */
1440#ifdef __BIG_ENDIAN__
1441 vcpu->arch.shared_big_endian = true;
1442#else
1443 vcpu->arch.shared_big_endian = false;
1444#endif
1445#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001446 vcpu->arch.mmcr[0] = MMCR0_FC;
1447 vcpu->arch.ctrl = CTRL_RUNLATCH;
1448 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301449 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001450 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001451 spin_lock_init(&vcpu->arch.tbacct_lock);
1452 vcpu->arch.busy_preempt = TB_NIL;
Anton Blanchardd6829162014-01-08 21:25:30 +11001453 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001454
Paul Mackerrasde56a942011-06-29 00:21:34 +00001455 kvmppc_mmu_book3s_hv_init(vcpu);
1456
Paul Mackerras8455d792012-10-15 01:17:42 +00001457 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001458
1459 init_waitqueue_head(&vcpu->arch.cpu_run);
1460
1461 mutex_lock(&kvm->lock);
1462 vcore = kvm->arch.vcores[core];
1463 if (!vcore) {
Stewart Smithde9bdd12014-07-18 14:18:42 +10001464 vcore = kvmppc_vcore_create(kvm, core);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001465 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001466 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001467 }
1468 mutex_unlock(&kvm->lock);
1469
1470 if (!vcore)
1471 goto free_vcpu;
1472
1473 spin_lock(&vcore->lock);
1474 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001475 spin_unlock(&vcore->lock);
1476 vcpu->arch.vcore = vcore;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001477 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001478
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001479 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1480 kvmppc_sanity_check(vcpu);
1481
Paul Mackerrasde56a942011-06-29 00:21:34 +00001482 return vcpu;
1483
1484free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001485 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001486out:
1487 return ERR_PTR(err);
1488}
1489
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001490static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1491{
1492 if (vpa->pinned_addr)
1493 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1494 vpa->dirty);
1495}
1496
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301497static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001498{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001499 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001500 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1501 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1502 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001503 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001504 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001505 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001506}
1507
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301508static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1509{
1510 /* Indicate we want to get back into the guest */
1511 return 1;
1512}
1513
Paul Mackerras19ccb762011-07-23 17:42:46 +10001514static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001515{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001516 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001517
Paul Mackerras19ccb762011-07-23 17:42:46 +10001518 now = get_tb();
1519 if (now > vcpu->arch.dec_expires) {
1520 /* decrementer has already gone negative */
1521 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001522 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001523 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001524 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001525 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1526 / tb_ticks_per_sec;
1527 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1528 HRTIMER_MODE_REL);
1529 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001530}
1531
Paul Mackerras19ccb762011-07-23 17:42:46 +10001532static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001533{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001534 vcpu->arch.ceded = 0;
1535 if (vcpu->arch.timer_running) {
1536 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1537 vcpu->arch.timer_running = 0;
1538 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001539}
1540
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001541extern void __kvmppc_vcore_entry(void);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001542
1543static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1544 struct kvm_vcpu *vcpu)
1545{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001546 u64 now;
1547
Paul Mackerras371fefd2011-06-29 00:23:08 +00001548 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1549 return;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001550 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001551 now = mftb();
1552 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1553 vcpu->arch.stolen_logged;
1554 vcpu->arch.busy_preempt = now;
1555 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001556 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001557 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001558 list_del(&vcpu->arch.run_list);
1559}
1560
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001561static int kvmppc_grab_hwthread(int cpu)
1562{
1563 struct paca_struct *tpaca;
Paul Mackerrasb754c732014-09-02 16:14:42 +10001564 long timeout = 10000;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001565
1566 tpaca = &paca[cpu];
1567
1568 /* Ensure the thread won't go into the kernel if it wakes */
1569 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001570 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001571
1572 /*
1573 * If the thread is already executing in the kernel (e.g. handling
1574 * a stray interrupt), wait for it to get back to nap mode.
1575 * The smp_mb() is to ensure that our setting of hwthread_req
1576 * is visible before we look at hwthread_state, so if this
1577 * races with the code at system_reset_pSeries and the thread
1578 * misses our setting of hwthread_req, we are sure to see its
1579 * setting of hwthread_state, and vice versa.
1580 */
1581 smp_mb();
1582 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1583 if (--timeout <= 0) {
1584 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1585 return -EBUSY;
1586 }
1587 udelay(1);
1588 }
1589 return 0;
1590}
1591
1592static void kvmppc_release_hwthread(int cpu)
1593{
1594 struct paca_struct *tpaca;
1595
1596 tpaca = &paca[cpu];
1597 tpaca->kvm_hstate.hwthread_req = 0;
1598 tpaca->kvm_hstate.kvm_vcpu = NULL;
1599}
1600
Paul Mackerras371fefd2011-06-29 00:23:08 +00001601static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1602{
1603 int cpu;
1604 struct paca_struct *tpaca;
1605 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1606
Paul Mackerras19ccb762011-07-23 17:42:46 +10001607 if (vcpu->arch.timer_running) {
1608 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1609 vcpu->arch.timer_running = 0;
1610 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001611 cpu = vc->pcpu + vcpu->arch.ptid;
1612 tpaca = &paca[cpu];
1613 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1614 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001615 tpaca->kvm_hstate.ptid = vcpu->arch.ptid;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001616 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001617 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +00001618#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001619 if (cpu != smp_processor_id()) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001620 xics_wake_cpu(cpu);
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001621 if (vcpu->arch.ptid)
1622 ++vc->n_woken;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001623 }
1624#endif
1625}
1626
1627static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1628{
1629 int i;
1630
1631 HMT_low();
1632 i = 0;
1633 while (vc->nap_count < vc->n_woken) {
1634 if (++i >= 1000000) {
1635 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1636 vc->nap_count, vc->n_woken);
1637 break;
1638 }
1639 cpu_relax();
1640 }
1641 HMT_medium();
1642}
1643
1644/*
1645 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001646 * this core are off-line. Then grab the threads so they can't
1647 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001648 */
1649static int on_primary_thread(void)
1650{
1651 int cpu = smp_processor_id();
Michael Ellerman3102f782014-05-23 18:15:29 +10001652 int thr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001653
Michael Ellerman3102f782014-05-23 18:15:29 +10001654 /* Are we on a primary subcore? */
1655 if (cpu_thread_in_subcore(cpu))
Paul Mackerras371fefd2011-06-29 00:23:08 +00001656 return 0;
Michael Ellerman3102f782014-05-23 18:15:29 +10001657
1658 thr = 0;
1659 while (++thr < threads_per_subcore)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001660 if (cpu_online(cpu + thr))
1661 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001662
1663 /* Grab all hw threads so they can't go into the kernel */
Michael Ellerman3102f782014-05-23 18:15:29 +10001664 for (thr = 1; thr < threads_per_subcore; ++thr) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00001665 if (kvmppc_grab_hwthread(cpu + thr)) {
1666 /* Couldn't grab one; let the others go */
1667 do {
1668 kvmppc_release_hwthread(cpu + thr);
1669 } while (--thr > 0);
1670 return 0;
1671 }
1672 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001673 return 1;
1674}
1675
Stewart Smith9678cda2014-07-18 14:18:43 +10001676static void kvmppc_start_saving_l2_cache(struct kvmppc_vcore *vc)
1677{
1678 phys_addr_t phy_addr, mpp_addr;
1679
1680 phy_addr = (phys_addr_t)virt_to_phys(vc->mpp_buffer);
1681 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1682
1683 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_ABORT);
1684 logmpp(mpp_addr | PPC_LOGMPP_LOG_L2);
1685
1686 vc->mpp_buffer_is_valid = true;
1687}
1688
1689static void kvmppc_start_restoring_l2_cache(const struct kvmppc_vcore *vc)
1690{
1691 phys_addr_t phy_addr, mpp_addr;
1692
1693 phy_addr = virt_to_phys(vc->mpp_buffer);
1694 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1695
1696 /* We must abort any in-progress save operations to ensure
1697 * the table is valid so that prefetch engine knows when to
1698 * stop prefetching. */
1699 logmpp(mpp_addr | PPC_LOGMPP_LOG_ABORT);
1700 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_WHOLE_TABLE);
1701}
1702
Paul Mackerras371fefd2011-06-29 00:23:08 +00001703/*
1704 * Run a set of guest threads on a physical core.
1705 * Called with vc->lock held.
1706 */
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001707static void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001708{
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001709 struct kvm_vcpu *vcpu, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001710 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001711 u64 now;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001712 int i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001713 int srcu_idx;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001714 struct kvm_vcpu *vcpus_to_update[threads_per_core];
Paul Mackerrasde56a942011-06-29 00:21:34 +00001715
Paul Mackerras371fefd2011-06-29 00:23:08 +00001716 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +10001717 need_vpa_update = 0;
1718 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001719 if (signal_pending(vcpu->arch.run_task))
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001720 return;
1721 if (vcpu->arch.vpa.update_pending ||
1722 vcpu->arch.slb_shadow.update_pending ||
1723 vcpu->arch.dtl.update_pending)
1724 vcpus_to_update[need_vpa_update++] = vcpu;
Paul Mackerras081f3232012-06-01 20:20:24 +10001725 }
1726
1727 /*
1728 * Initialize *vc, in particular vc->vcore_state, so we can
1729 * drop the vcore lock if necessary.
1730 */
1731 vc->n_woken = 0;
1732 vc->nap_count = 0;
1733 vc->entry_exit_count = 0;
Paul Mackerras2711e242014-12-04 16:43:28 +11001734 vc->preempt_tb = TB_NIL;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001735 vc->vcore_state = VCORE_STARTING;
Paul Mackerras081f3232012-06-01 20:20:24 +10001736 vc->in_guest = 0;
1737 vc->napping_threads = 0;
Sam Bobroff90fd09f2014-12-03 13:30:40 +11001738 vc->conferring_threads = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +10001739
1740 /*
1741 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1742 * which can't be called with any spinlocks held.
1743 */
1744 if (need_vpa_update) {
1745 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001746 for (i = 0; i < need_vpa_update; ++i)
1747 kvmppc_update_vpas(vcpus_to_update[i]);
Paul Mackerras081f3232012-06-01 20:20:24 +10001748 spin_lock(&vc->lock);
1749 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001750
1751 /*
Michael Ellerman3102f782014-05-23 18:15:29 +10001752 * Make sure we are running on primary threads, and that secondary
1753 * threads are offline. Also check if the number of threads in this
1754 * guest are greater than the current system threads per guest.
Paul Mackerras7b444c62012-10-15 01:16:14 +00001755 */
Michael Ellerman3102f782014-05-23 18:15:29 +10001756 if ((threads_per_core > 1) &&
1757 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00001758 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1759 vcpu->arch.ret = -EBUSY;
1760 goto out;
1761 }
1762
Michael Ellerman3102f782014-05-23 18:15:29 +10001763
Paul Mackerras371fefd2011-06-29 00:23:08 +00001764 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001765 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001766 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001767 kvmppc_create_dtl_entry(vcpu, vc);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001768 trace_kvm_guest_enter(vcpu);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001769 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001770
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001771 /* Set this explicitly in case thread 0 doesn't have a vcpu */
1772 get_paca()->kvm_hstate.kvm_vcore = vc;
1773 get_paca()->kvm_hstate.ptid = 0;
1774
Paul Mackerras2f12f032012-10-15 01:17:17 +00001775 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001776 preempt_disable();
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001777
1778 trace_kvmppc_run_core(vc, 0);
1779
Paul Mackerras19ccb762011-07-23 17:42:46 +10001780 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001781
Paul Mackerras19ccb762011-07-23 17:42:46 +10001782 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001783
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001784 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001785
Stewart Smith9678cda2014-07-18 14:18:43 +10001786 if (vc->mpp_buffer_is_valid)
1787 kvmppc_start_restoring_l2_cache(vc);
1788
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001789 __kvmppc_vcore_entry();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001790
Paul Mackerras371fefd2011-06-29 00:23:08 +00001791 spin_lock(&vc->lock);
Stewart Smith9678cda2014-07-18 14:18:43 +10001792
1793 if (vc->mpp_buffer)
1794 kvmppc_start_saving_l2_cache(vc);
1795
Paul Mackerras19ccb762011-07-23 17:42:46 +10001796 /* disable sending of IPIs on virtual external irqs */
1797 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1798 vcpu->cpu = -1;
1799 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001800 if (vc->nap_count < vc->n_woken)
1801 kvmppc_wait_for_nap(vc);
Michael Ellerman3102f782014-05-23 18:15:29 +10001802 for (i = 0; i < threads_per_subcore; ++i)
Paul Mackerras2f12f032012-10-15 01:17:17 +00001803 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001804 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001805 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001806 spin_unlock(&vc->lock);
1807
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001808 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001809
Paul Mackerras371fefd2011-06-29 00:23:08 +00001810 /* make sure updates to secondary vcpu structs are visible now */
1811 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001812 kvm_guest_exit();
1813
1814 preempt_enable();
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09001815 cond_resched();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001816
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001817 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001818 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001819 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1820 /* cancel pending dec exception if dec is positive */
1821 if (now < vcpu->arch.dec_expires &&
1822 kvmppc_core_pending_dec(vcpu))
1823 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001824
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001825 trace_kvm_guest_exit(vcpu);
1826
Paul Mackerras19ccb762011-07-23 17:42:46 +10001827 ret = RESUME_GUEST;
1828 if (vcpu->arch.trap)
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301829 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1830 vcpu->arch.run_task);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001831
Paul Mackerras371fefd2011-06-29 00:23:08 +00001832 vcpu->arch.ret = ret;
1833 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001834
1835 if (vcpu->arch.ceded) {
Greg Kurze59d24e2014-02-06 17:36:56 +01001836 if (!is_kvmppc_resume_guest(ret))
Paul Mackerras19ccb762011-07-23 17:42:46 +10001837 kvmppc_end_cede(vcpu);
1838 else
1839 kvmppc_set_timer(vcpu);
1840 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001841 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001842
Paul Mackerrasde56a942011-06-29 00:21:34 +00001843 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001844 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001845 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1846 arch.run_list) {
Greg Kurze59d24e2014-02-06 17:36:56 +01001847 if (!is_kvmppc_resume_guest(vcpu->arch.ret)) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001848 kvmppc_remove_runnable(vc, vcpu);
1849 wake_up(&vcpu->arch.cpu_run);
1850 }
1851 }
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001852
1853 trace_kvmppc_run_core(vc, 1);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001854}
1855
Paul Mackerras19ccb762011-07-23 17:42:46 +10001856/*
1857 * Wait for some other vcpu thread to execute us, and
1858 * wake us up when we need to handle something in the host.
1859 */
1860static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001861{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001862 DEFINE_WAIT(wait);
1863
Paul Mackerras19ccb762011-07-23 17:42:46 +10001864 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1865 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1866 schedule();
1867 finish_wait(&vcpu->arch.cpu_run, &wait);
1868}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001869
Paul Mackerras19ccb762011-07-23 17:42:46 +10001870/*
1871 * All the vcpus in this vcore are idle, so wait for a decrementer
1872 * or external interrupt to one of the vcpus. vc->lock is held.
1873 */
1874static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1875{
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11001876 struct kvm_vcpu *vcpu;
1877 int do_sleep = 1;
1878
Paul Mackerras19ccb762011-07-23 17:42:46 +10001879 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001880
1881 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11001882
1883 /*
1884 * Check one last time for pending exceptions and ceded state after
1885 * we put ourselves on the wait queue
1886 */
1887 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1888 if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded) {
1889 do_sleep = 0;
1890 break;
1891 }
1892 }
1893
1894 if (!do_sleep) {
1895 finish_wait(&vc->wq, &wait);
1896 return;
1897 }
1898
Paul Mackerras19ccb762011-07-23 17:42:46 +10001899 vc->vcore_state = VCORE_SLEEPING;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001900 trace_kvmppc_vcore_blocked(vc, 0);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001901 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001902 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001903 finish_wait(&vc->wq, &wait);
1904 spin_lock(&vc->lock);
1905 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001906 trace_kvmppc_vcore_blocked(vc, 1);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001907}
1908
1909static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1910{
1911 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001912 struct kvmppc_vcore *vc;
1913 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001914
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001915 trace_kvmppc_run_vcpu_enter(vcpu);
1916
Paul Mackerras371fefd2011-06-29 00:23:08 +00001917 kvm_run->exit_reason = 0;
1918 vcpu->arch.ret = RESUME_GUEST;
1919 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001920 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001921
Paul Mackerras371fefd2011-06-29 00:23:08 +00001922 /*
1923 * Synchronize with other threads in this virtual core
1924 */
1925 vc = vcpu->arch.vcore;
1926 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001927 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001928 vcpu->arch.run_task = current;
1929 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001930 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10001931 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001932 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001933 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1934 ++vc->n_runnable;
1935
Paul Mackerras19ccb762011-07-23 17:42:46 +10001936 /*
1937 * This happens the first time this is called for a vcpu.
1938 * If the vcore is already running, we may be able to start
1939 * this thread straight away and have it join in.
1940 */
Paul Mackerras8455d792012-10-15 01:17:42 +00001941 if (!signal_pending(current)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001942 if (vc->vcore_state == VCORE_RUNNING &&
1943 VCORE_EXIT_COUNT(vc) == 0) {
Paul Mackerras2f12f032012-10-15 01:17:17 +00001944 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001945 kvmppc_start_thread(vcpu);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001946 trace_kvm_guest_enter(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00001947 } else if (vc->vcore_state == VCORE_SLEEPING) {
1948 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001949 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001950
Paul Mackerras8455d792012-10-15 01:17:42 +00001951 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001952
1953 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1954 !signal_pending(current)) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001955 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001956 spin_unlock(&vc->lock);
1957 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1958 spin_lock(&vc->lock);
1959 continue;
1960 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001961 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1962 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001963 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001964 if (signal_pending(v->arch.run_task)) {
1965 kvmppc_remove_runnable(vc, v);
1966 v->stat.signal_exits++;
1967 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1968 v->arch.ret = -EINTR;
1969 wake_up(&v->arch.cpu_run);
1970 }
1971 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001972 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1973 break;
1974 vc->runner = vcpu;
1975 n_ceded = 0;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001976 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001977 if (!v->arch.pending_exceptions)
1978 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001979 else
1980 v->arch.ceded = 0;
1981 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001982 if (n_ceded == vc->n_runnable)
1983 kvmppc_vcore_blocked(vc);
1984 else
1985 kvmppc_run_core(vc);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001986 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001987 }
1988
Paul Mackerras8455d792012-10-15 01:17:42 +00001989 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1990 (vc->vcore_state == VCORE_RUNNING ||
1991 vc->vcore_state == VCORE_EXITING)) {
1992 spin_unlock(&vc->lock);
1993 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1994 spin_lock(&vc->lock);
1995 }
1996
1997 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1998 kvmppc_remove_runnable(vc, vcpu);
1999 vcpu->stat.signal_exits++;
2000 kvm_run->exit_reason = KVM_EXIT_INTR;
2001 vcpu->arch.ret = -EINTR;
2002 }
2003
2004 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
2005 /* Wake up some vcpu to run the core */
2006 v = list_first_entry(&vc->runnable_threads,
2007 struct kvm_vcpu, arch.run_list);
2008 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002009 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002010
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002011 trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002012 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002013 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002014}
2015
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302016static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002017{
2018 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002019 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002020
Alexander Grafaf8f38b2011-08-10 13:57:08 +02002021 if (!vcpu->arch.sane) {
2022 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2023 return -EINVAL;
2024 }
2025
Scott Wood25051b5a2011-11-08 18:23:23 -06002026 kvmppc_core_prepare_to_enter(vcpu);
2027
Paul Mackerras19ccb762011-07-23 17:42:46 +10002028 /* No need to go into the guest when all we'll do is come back out */
2029 if (signal_pending(current)) {
2030 run->exit_reason = KVM_EXIT_INTR;
2031 return -EINTR;
2032 }
2033
Paul Mackerras32fad282012-05-04 02:32:53 +00002034 atomic_inc(&vcpu->kvm->arch.vcpus_running);
2035 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
2036 smp_mb();
2037
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002038 /* On the first time here, set up HTAB and VRMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002039 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00002040 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002041 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00002042 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002043 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002044
2045 flush_fp_to_thread(current);
2046 flush_altivec_to_thread(current);
2047 flush_vsx_to_thread(current);
2048 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00002049 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002050 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10002051
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002052 do {
2053 r = kvmppc_run_vcpu(run, vcpu);
2054
2055 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
2056 !(vcpu->arch.shregs.msr & MSR_PR)) {
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002057 trace_kvm_hcall_enter(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002058 r = kvmppc_pseries_do_hcall(vcpu);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002059 trace_kvm_hcall_exit(vcpu, r);
Scott Wood7e28e60e2011-11-08 18:23:20 -06002060 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002061 } else if (r == RESUME_PAGE_FAULT) {
2062 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2063 r = kvmppc_book3s_hv_page_fault(run, vcpu,
2064 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
2065 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002066 }
Greg Kurze59d24e2014-02-06 17:36:56 +01002067 } while (is_kvmppc_resume_guest(r));
Paul Mackerras32fad282012-05-04 02:32:53 +00002068
2069 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002070 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00002071 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002072 return r;
2073}
2074
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002075static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2076 int linux_psize)
2077{
2078 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2079
2080 if (!def->shift)
2081 return;
2082 (*sps)->page_shift = def->shift;
2083 (*sps)->slb_enc = def->sllp;
2084 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00002085 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05302086 /*
2087 * Add 16MB MPSS support if host supports it
2088 */
2089 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2090 (*sps)->enc[1].page_shift = 24;
2091 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2092 }
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002093 (*sps)++;
2094}
2095
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302096static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2097 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002098{
2099 struct kvm_ppc_one_seg_page_size *sps;
2100
2101 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2102 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2103 info->flags |= KVM_PPC_1T_SEGMENTS;
2104 info->slb_size = mmu_slb_size;
2105
2106 /* We only support these sizes for now, and no muti-size segments */
2107 sps = &info->sps[0];
2108 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2109 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2110 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2111
2112 return 0;
2113}
2114
Paul Mackerras82ed3612011-12-15 02:03:22 +00002115/*
2116 * Get (and clear) the dirty memory log for a memory slot.
2117 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302118static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2119 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002120{
2121 struct kvm_memory_slot *memslot;
2122 int r;
2123 unsigned long n;
2124
2125 mutex_lock(&kvm->slots_lock);
2126
2127 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07002128 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002129 goto out;
2130
2131 memslot = id_to_memslot(kvm->memslots, log->slot);
2132 r = -ENOENT;
2133 if (!memslot->dirty_bitmap)
2134 goto out;
2135
2136 n = kvm_dirty_bitmap_bytes(memslot);
2137 memset(memslot->dirty_bitmap, 0, n);
2138
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002139 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00002140 if (r)
2141 goto out;
2142
2143 r = -EFAULT;
2144 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2145 goto out;
2146
2147 r = 0;
2148out:
2149 mutex_unlock(&kvm->slots_lock);
2150 return r;
2151}
2152
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302153static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2154 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002155{
2156 if (!dont || free->arch.rmap != dont->arch.rmap) {
2157 vfree(free->arch.rmap);
2158 free->arch.rmap = NULL;
2159 }
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002160}
2161
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302162static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
2163 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002164{
2165 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
2166 if (!slot->arch.rmap)
2167 return -ENOMEM;
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002168
2169 return 0;
2170}
2171
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302172static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
2173 struct kvm_memory_slot *memslot,
2174 struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002175{
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002176 return 0;
2177}
2178
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302179static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
2180 struct kvm_userspace_memory_region *mem,
2181 const struct kvm_memory_slot *old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002182{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002183 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
2184 struct kvm_memory_slot *memslot;
2185
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002186 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002187 /*
2188 * If modifying a memslot, reset all the rmap dirty bits.
2189 * If this is a new memslot, we don't need to do anything
2190 * since the rmap array starts out as all zeroes,
2191 * i.e. no pages are dirty.
2192 */
2193 memslot = id_to_memslot(kvm->memslots, mem->slot);
2194 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
2195 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002196}
2197
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002198/*
2199 * Update LPCR values in kvm->arch and in vcores.
2200 * Caller must hold kvm->lock.
2201 */
2202void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
2203{
2204 long int i;
2205 u32 cores_done = 0;
2206
2207 if ((kvm->arch.lpcr & mask) == lpcr)
2208 return;
2209
2210 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2211
2212 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2213 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2214 if (!vc)
2215 continue;
2216 spin_lock(&vc->lock);
2217 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2218 spin_unlock(&vc->lock);
2219 if (++cores_done >= kvm->arch.online_vcores)
2220 break;
2221 }
2222}
2223
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302224static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2225{
2226 return;
2227}
2228
Paul Mackerras32fad282012-05-04 02:32:53 +00002229static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002230{
2231 int err = 0;
2232 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002233 unsigned long hva;
2234 struct kvm_memory_slot *memslot;
2235 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002236 unsigned long lpcr = 0, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002237 unsigned long psize, porder;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002238 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002239
2240 mutex_lock(&kvm->lock);
2241 if (kvm->arch.rma_setup_done)
2242 goto out; /* another vcpu beat us to it */
2243
Paul Mackerras32fad282012-05-04 02:32:53 +00002244 /* Allocate hashed page table (if not done already) and reset it */
2245 if (!kvm->arch.hpt_virt) {
2246 err = kvmppc_alloc_hpt(kvm, NULL);
2247 if (err) {
2248 pr_err("KVM: Couldn't alloc HPT\n");
2249 goto out;
2250 }
2251 }
2252
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002253 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002254 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002255 memslot = gfn_to_memslot(kvm, 0);
2256
2257 /* We must have some memory at 0 by now */
2258 err = -EINVAL;
2259 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002260 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002261
2262 /* Look up the VMA for the start of this memory slot */
2263 hva = memslot->userspace_addr;
2264 down_read(&current->mm->mmap_sem);
2265 vma = find_vma(current->mm, hva);
2266 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2267 goto up_out;
2268
2269 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002270 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002271
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002272 up_read(&current->mm->mmap_sem);
2273
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002274 /* We can handle 4k, 64k or 16M pages in the VRMA */
2275 err = -EINVAL;
2276 if (!(psize == 0x1000 || psize == 0x10000 ||
2277 psize == 0x1000000))
2278 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002279
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002280 /* Update VRMASD field in the LPCR */
2281 senc = slb_pgsize_encoding(psize);
2282 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2283 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2284 /* the -4 is to account for senc values starting at 0x10 */
2285 lpcr = senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002286
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002287 /* Create HPTEs in the hash page table for the VRMA */
2288 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002289
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002290 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002291
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002292 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
2293 smp_wmb();
2294 kvm->arch.rma_setup_done = 1;
2295 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002296 out_srcu:
2297 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002298 out:
2299 mutex_unlock(&kvm->lock);
2300 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002301
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002302 up_out:
2303 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08002304 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002305}
2306
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302307static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002308{
Paul Mackerras32fad282012-05-04 02:32:53 +00002309 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002310
Paul Mackerras32fad282012-05-04 02:32:53 +00002311 /* Allocate the guest's logical partition ID */
2312
2313 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08002314 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00002315 return -ENOMEM;
2316 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002317
Paul Mackerras1b400ba2012-11-21 23:28:08 +00002318 /*
2319 * Since we don't flush the TLB when tearing down a VM,
2320 * and this lpid might have previously been used,
2321 * make sure we flush on each core before running the new VM.
2322 */
2323 cpumask_setall(&kvm->arch.need_tlb_flush);
2324
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002325 /* Start out with the default set of hcalls enabled */
2326 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
2327 sizeof(kvm->arch.enabled_hcalls));
2328
Paul Mackerras9e368f22011-06-29 00:40:08 +00002329 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002330
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002331 /* Init LPCR for virtual RMA mode */
2332 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2333 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2334 lpcr &= LPCR_PECE | LPCR_LPES;
2335 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
2336 LPCR_VPM0 | LPCR_VPM1;
2337 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2338 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2339 /* On POWER8 turn on online bit to enable PURR/SPURR */
2340 if (cpu_has_feature(CPU_FTR_ARCH_207S))
2341 lpcr |= LPCR_ONL;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002342 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002343
Paul Mackerras512691d2012-10-15 01:15:41 +00002344 /*
Michael Ellerman441c19c2014-05-23 18:15:25 +10002345 * Track that we now have a HV mode VM active. This blocks secondary
2346 * CPU threads from coming online.
Paul Mackerras512691d2012-10-15 01:15:41 +00002347 */
Michael Ellerman441c19c2014-05-23 18:15:25 +10002348 kvm_hv_vm_activated();
Paul Mackerras512691d2012-10-15 01:15:41 +00002349
David Gibson54738c02011-06-29 00:22:41 +00002350 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002351}
2352
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302353static void kvmppc_free_vcores(struct kvm *kvm)
2354{
2355 long int i;
2356
Stewart Smith9678cda2014-07-18 14:18:43 +10002357 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2358 if (kvm->arch.vcores[i] && kvm->arch.vcores[i]->mpp_buffer) {
2359 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2360 free_pages((unsigned long)vc->mpp_buffer,
2361 MPP_BUFFER_ORDER);
2362 }
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302363 kfree(kvm->arch.vcores[i]);
Stewart Smith9678cda2014-07-18 14:18:43 +10002364 }
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302365 kvm->arch.online_vcores = 0;
2366}
2367
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302368static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002369{
Michael Ellerman441c19c2014-05-23 18:15:25 +10002370 kvm_hv_vm_deactivated();
Paul Mackerras512691d2012-10-15 01:15:41 +00002371
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302372 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002373
Paul Mackerrasde56a942011-06-29 00:21:34 +00002374 kvmppc_free_hpt(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002375}
2376
2377/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302378static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2379 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002380{
2381 return EMULATE_FAIL;
2382}
2383
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302384static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2385 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002386{
2387 return EMULATE_FAIL;
2388}
2389
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302390static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2391 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002392{
2393 return EMULATE_FAIL;
2394}
2395
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302396static int kvmppc_core_check_processor_compat_hv(void)
2397{
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002398 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
2399 !cpu_has_feature(CPU_FTR_ARCH_206))
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302400 return -EIO;
2401 return 0;
2402}
2403
2404static long kvm_arch_vm_ioctl_hv(struct file *filp,
2405 unsigned int ioctl, unsigned long arg)
2406{
2407 struct kvm *kvm __maybe_unused = filp->private_data;
2408 void __user *argp = (void __user *)arg;
2409 long r;
2410
2411 switch (ioctl) {
2412
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302413 case KVM_PPC_ALLOCATE_HTAB: {
2414 u32 htab_order;
2415
2416 r = -EFAULT;
2417 if (get_user(htab_order, (u32 __user *)argp))
2418 break;
2419 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2420 if (r)
2421 break;
2422 r = -EFAULT;
2423 if (put_user(htab_order, (u32 __user *)argp))
2424 break;
2425 r = 0;
2426 break;
2427 }
2428
2429 case KVM_PPC_GET_HTAB_FD: {
2430 struct kvm_get_htab_fd ghf;
2431
2432 r = -EFAULT;
2433 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2434 break;
2435 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2436 break;
2437 }
2438
2439 default:
2440 r = -ENOTTY;
2441 }
2442
2443 return r;
2444}
2445
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002446/*
2447 * List of hcall numbers to enable by default.
2448 * For compatibility with old userspace, we enable by default
2449 * all hcalls that were implemented before the hcall-enabling
2450 * facility was added. Note this list should not include H_RTAS.
2451 */
2452static unsigned int default_hcall_list[] = {
2453 H_REMOVE,
2454 H_ENTER,
2455 H_READ,
2456 H_PROTECT,
2457 H_BULK_REMOVE,
2458 H_GET_TCE,
2459 H_PUT_TCE,
2460 H_SET_DABR,
2461 H_SET_XDABR,
2462 H_CEDE,
2463 H_PROD,
2464 H_CONFER,
2465 H_REGISTER_VPA,
2466#ifdef CONFIG_KVM_XICS
2467 H_EOI,
2468 H_CPPR,
2469 H_IPI,
2470 H_IPOLL,
2471 H_XIRR,
2472 H_XIRR_X,
2473#endif
2474 0
2475};
2476
2477static void init_default_hcalls(void)
2478{
2479 int i;
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002480 unsigned int hcall;
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002481
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002482 for (i = 0; default_hcall_list[i]; ++i) {
2483 hcall = default_hcall_list[i];
2484 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
2485 __set_bit(hcall / 4, default_enabled_hcalls);
2486 }
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002487}
2488
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302489static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302490 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2491 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2492 .get_one_reg = kvmppc_get_one_reg_hv,
2493 .set_one_reg = kvmppc_set_one_reg_hv,
2494 .vcpu_load = kvmppc_core_vcpu_load_hv,
2495 .vcpu_put = kvmppc_core_vcpu_put_hv,
2496 .set_msr = kvmppc_set_msr_hv,
2497 .vcpu_run = kvmppc_vcpu_run_hv,
2498 .vcpu_create = kvmppc_core_vcpu_create_hv,
2499 .vcpu_free = kvmppc_core_vcpu_free_hv,
2500 .check_requests = kvmppc_core_check_requests_hv,
2501 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2502 .flush_memslot = kvmppc_core_flush_memslot_hv,
2503 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2504 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2505 .unmap_hva = kvm_unmap_hva_hv,
2506 .unmap_hva_range = kvm_unmap_hva_range_hv,
2507 .age_hva = kvm_age_hva_hv,
2508 .test_age_hva = kvm_test_age_hva_hv,
2509 .set_spte_hva = kvm_set_spte_hva_hv,
2510 .mmu_destroy = kvmppc_mmu_destroy_hv,
2511 .free_memslot = kvmppc_core_free_memslot_hv,
2512 .create_memslot = kvmppc_core_create_memslot_hv,
2513 .init_vm = kvmppc_core_init_vm_hv,
2514 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302515 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2516 .emulate_op = kvmppc_core_emulate_op_hv,
2517 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2518 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2519 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2520 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002521 .hcall_implemented = kvmppc_hcall_impl_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302522};
2523
2524static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002525{
2526 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302527 /*
2528 * FIXME!! Do we need to check on all cpus ?
2529 */
2530 r = kvmppc_core_check_processor_compat_hv();
2531 if (r < 0)
Paul Mackerras739e2422014-03-25 10:47:05 +11002532 return -ENODEV;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002533
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302534 kvm_ops_hv.owner = THIS_MODULE;
2535 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002536
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002537 init_default_hcalls();
2538
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302539 r = kvmppc_mmu_hv_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002540 return r;
2541}
2542
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302543static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002544{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302545 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002546}
2547
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302548module_init(kvmppc_book3s_init_hv);
2549module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05302550MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01002551MODULE_ALIAS_MISCDEV(KVM_MINOR);
2552MODULE_ALIAS("devname:kvm");