blob: 08f8617f404653b228f3aad284c0f3571f2ad5ce [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 Mackerrase23a8082015-03-28 14:21:01 +110035#include <linux/debugfs.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000036
37#include <asm/reg.h>
38#include <asm/cputable.h>
Stewart Smith9678cda2014-07-18 14:18:43 +100039#include <asm/cache.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000040#include <asm/cacheflush.h>
41#include <asm/tlbflush.h>
42#include <asm/uaccess.h>
43#include <asm/io.h>
44#include <asm/kvm_ppc.h>
45#include <asm/kvm_book3s.h>
46#include <asm/mmu_context.h>
47#include <asm/lppaca.h>
48#include <asm/processor.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000049#include <asm/cputhreads.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000050#include <asm/page.h>
Michael Neulingde1d9242011-11-09 20:39:49 +000051#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010052#include <asm/switch_to.h>
Paul Mackerras512691d2012-10-15 01:15:41 +000053#include <asm/smp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000054#include <linux/gfp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000055#include <linux/vmalloc.h>
56#include <linux/highmem.h>
Paul Mackerrasc77162d2011-12-12 12:31:00 +000057#include <linux/hugetlb.h>
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +053058#include <linux/module.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000059
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053060#include "book3s.h"
61
Suresh E. Warrier3c78f782014-12-03 18:48:10 -060062#define CREATE_TRACE_POINTS
63#include "trace_hv.h"
64
Paul Mackerrasde56a942011-06-29 00:21:34 +000065/* #define EXIT_DEBUG */
66/* #define EXIT_DEBUG_SIMPLE */
67/* #define EXIT_DEBUG_INT */
68
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000069/* Used to indicate that a guest page fault needs to be handled */
70#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
71
Paul Mackerrasc7b67672012-10-15 01:18:07 +000072/* Used as a "null" value for timebase values */
73#define TB_NIL (~(u64)0)
74
Paul Mackerras699a0ea2014-06-02 11:02:59 +100075static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
76
Stewart Smith9678cda2014-07-18 14:18:43 +100077#if defined(CONFIG_PPC_64K_PAGES)
78#define MPP_BUFFER_ORDER 0
79#elif defined(CONFIG_PPC_4K_PAGES)
80#define MPP_BUFFER_ORDER 3
81#endif
82
83
Paul Mackerras19ccb762011-07-23 17:42:46 +100084static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +000085static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +100086
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053087static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +000088{
89 int me;
90 int cpu = vcpu->cpu;
91 wait_queue_head_t *wqp;
92
93 wqp = kvm_arch_vcpu_wq(vcpu);
94 if (waitqueue_active(wqp)) {
95 wake_up_interruptible(wqp);
96 ++vcpu->stat.halt_wakeup;
97 }
98
99 me = get_cpu();
100
101 /* CPU points to the first thread of the core */
102 if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
Anton Blanchard75052582014-03-25 10:47:01 +1100103#ifdef CONFIG_PPC_ICP_NATIVE
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000104 int real_cpu = cpu + vcpu->arch.ptid;
105 if (paca[real_cpu].kvm_hstate.xics_phys)
106 xics_wake_cpu(real_cpu);
Andreas Schwab48eaef02013-12-30 15:36:56 +0100107 else
108#endif
109 if (cpu_online(cpu))
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000110 smp_send_reschedule(cpu);
111 }
112 put_cpu();
113}
114
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000115/*
116 * We use the vcpu_load/put functions to measure stolen time.
117 * Stolen time is counted as time when either the vcpu is able to
118 * run as part of a virtual core, but the task running the vcore
119 * is preempted or sleeping, or when the vcpu needs something done
120 * in the kernel by the task running the vcpu, but that task is
121 * preempted or sleeping. Those two things have to be counted
122 * separately, since one of the vcpu tasks will take on the job
123 * of running the core, and the other vcpu tasks in the vcore will
124 * sleep waiting for it to do that, but that sleep shouldn't count
125 * as stolen time.
126 *
127 * Hence we accumulate stolen time when the vcpu can run as part of
128 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
129 * needs its task to do other things in the kernel (for example,
130 * service a page fault) in busy_stolen. We don't accumulate
131 * stolen time for a vcore when it is inactive, or for a vcpu
132 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
133 * a misnomer; it means that the vcpu task is not executing in
134 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
135 * the kernel. We don't have any way of dividing up that time
136 * between time that the vcpu is genuinely stopped, time that
137 * the task is actively working on behalf of the vcpu, and time
138 * that the task is preempted, so we don't count any of it as
139 * stolen.
140 *
141 * Updates to busy_stolen are protected by arch.tbacct_lock;
Paul Mackerras2711e242014-12-04 16:43:28 +1100142 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
143 * lock. The stolen times are measured in units of timebase ticks.
144 * (Note that the != TB_NIL checks below are purely defensive;
145 * they should never fail.)
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000146 */
147
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530148static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000149{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000150 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100151 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000152
Paul Mackerras2711e242014-12-04 16:43:28 +1100153 /*
154 * We can test vc->runner without taking the vcore lock,
155 * because only this task ever sets vc->runner to this
156 * vcpu, and once it is set to this vcpu, only this task
157 * ever sets it to NULL.
158 */
159 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE) {
160 spin_lock_irqsave(&vc->stoltb_lock, flags);
161 if (vc->preempt_tb != TB_NIL) {
162 vc->stolen_tb += mftb() - vc->preempt_tb;
163 vc->preempt_tb = TB_NIL;
164 }
165 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000166 }
Paul Mackerras2711e242014-12-04 16:43:28 +1100167 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000168 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
169 vcpu->arch.busy_preempt != TB_NIL) {
170 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
171 vcpu->arch.busy_preempt = TB_NIL;
172 }
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100173 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000174}
175
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530176static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000177{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000178 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100179 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000180
Paul Mackerras2711e242014-12-04 16:43:28 +1100181 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE) {
182 spin_lock_irqsave(&vc->stoltb_lock, flags);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000183 vc->preempt_tb = mftb();
Paul Mackerras2711e242014-12-04 16:43:28 +1100184 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
185 }
186 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000187 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
188 vcpu->arch.busy_preempt = mftb();
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100189 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000190}
191
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530192static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000193{
194 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000195 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000196}
197
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530198void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000199{
200 vcpu->arch.pvr = pvr;
201}
202
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000203int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
204{
205 unsigned long pcr = 0;
206 struct kvmppc_vcore *vc = vcpu->arch.vcore;
207
208 if (arch_compat) {
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000209 switch (arch_compat) {
210 case PVR_ARCH_205:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100211 /*
212 * If an arch bit is set in PCR, all the defined
213 * higher-order arch bits also have to be set.
214 */
215 pcr = PCR_ARCH_206 | PCR_ARCH_205;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000216 break;
217 case PVR_ARCH_206:
218 case PVR_ARCH_206p:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100219 pcr = PCR_ARCH_206;
220 break;
221 case PVR_ARCH_207:
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000222 break;
223 default:
224 return -EINVAL;
225 }
Paul Mackerras5557ae02014-01-08 21:25:24 +1100226
227 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
228 /* POWER7 can't emulate POWER8 */
229 if (!(pcr & PCR_ARCH_206))
230 return -EINVAL;
231 pcr &= ~PCR_ARCH_206;
232 }
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000233 }
234
235 spin_lock(&vc->lock);
236 vc->arch_compat = arch_compat;
237 vc->pcr = pcr;
238 spin_unlock(&vc->lock);
239
240 return 0;
241}
242
Paul Mackerrasde56a942011-06-29 00:21:34 +0000243void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
244{
245 int r;
246
247 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
248 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
249 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
250 for (r = 0; r < 16; ++r)
251 pr_err("r%2d = %.16lx r%d = %.16lx\n",
252 r, kvmppc_get_gpr(vcpu, r),
253 r+16, kvmppc_get_gpr(vcpu, r+16));
254 pr_err("ctr = %.16lx lr = %.16lx\n",
255 vcpu->arch.ctr, vcpu->arch.lr);
256 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
257 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
258 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
259 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
260 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
261 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
262 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
263 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
264 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
265 pr_err("fault dar = %.16lx dsisr = %.8x\n",
266 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
267 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
268 for (r = 0; r < vcpu->arch.slb_max; ++r)
269 pr_err(" ESID = %.16llx VSID = %.16llx\n",
270 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
271 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000272 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000273 vcpu->arch.last_inst);
274}
275
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000276struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
277{
278 int r;
279 struct kvm_vcpu *v, *ret = NULL;
280
281 mutex_lock(&kvm->lock);
282 kvm_for_each_vcpu(r, v, kvm) {
283 if (v->vcpu_id == id) {
284 ret = v;
285 break;
286 }
287 }
288 mutex_unlock(&kvm->lock);
289 return ret;
290}
291
292static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
293{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000294 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Alexander Graf02407552014-06-11 10:34:19 +0200295 vpa->yield_count = cpu_to_be32(1);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000296}
297
Paul Mackerras55b665b2012-09-25 20:33:06 +0000298static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
299 unsigned long addr, unsigned long len)
300{
301 /* check address is cacheline aligned */
302 if (addr & (L1_CACHE_BYTES - 1))
303 return -EINVAL;
304 spin_lock(&vcpu->arch.vpa_update_lock);
305 if (v->next_gpa != addr || v->len != len) {
306 v->next_gpa = addr;
307 v->len = addr ? len : 0;
308 v->update_pending = 1;
309 }
310 spin_unlock(&vcpu->arch.vpa_update_lock);
311 return 0;
312}
313
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000314/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
315struct reg_vpa {
316 u32 dummy;
317 union {
Alexander Graf02407552014-06-11 10:34:19 +0200318 __be16 hword;
319 __be32 word;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000320 } length;
321};
322
323static int vpa_is_registered(struct kvmppc_vpa *vpap)
324{
325 if (vpap->update_pending)
326 return vpap->next_gpa != 0;
327 return vpap->pinned_addr != NULL;
328}
329
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000330static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
331 unsigned long flags,
332 unsigned long vcpuid, unsigned long vpa)
333{
334 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000335 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000336 void *va;
337 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000338 int err;
339 int subfunc;
340 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000341
342 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
343 if (!tvcpu)
344 return H_PARAMETER;
345
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000346 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
347 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
348 subfunc == H_VPA_REG_SLB) {
349 /* Registering new area - address must be cache-line aligned */
350 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000351 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000352
353 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000354 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
355 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000356 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000357 if (subfunc == H_VPA_REG_VPA)
Alexander Graf02407552014-06-11 10:34:19 +0200358 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000359 else
Alexander Graf02407552014-06-11 10:34:19 +0200360 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000361 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000362
363 /* Check length */
364 if (len > nb || len < sizeof(struct reg_vpa))
365 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000366 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000367 vpa = 0;
368 len = 0;
369 }
370
371 err = H_PARAMETER;
372 vpap = NULL;
373 spin_lock(&tvcpu->arch.vpa_update_lock);
374
375 switch (subfunc) {
376 case H_VPA_REG_VPA: /* register VPA */
377 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000378 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000379 vpap = &tvcpu->arch.vpa;
380 err = 0;
381 break;
382
383 case H_VPA_REG_DTL: /* register DTL */
384 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000385 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000386 len -= len % sizeof(struct dtl_entry);
387
388 /* Check that they have previously registered a VPA */
389 err = H_RESOURCE;
390 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000391 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000392
393 vpap = &tvcpu->arch.dtl;
394 err = 0;
395 break;
396
397 case H_VPA_REG_SLB: /* register SLB shadow buffer */
398 /* Check that they have previously registered a VPA */
399 err = H_RESOURCE;
400 if (!vpa_is_registered(&tvcpu->arch.vpa))
401 break;
402
403 vpap = &tvcpu->arch.slb_shadow;
404 err = 0;
405 break;
406
407 case H_VPA_DEREG_VPA: /* deregister VPA */
408 /* Check they don't still have a DTL or SLB buf registered */
409 err = H_RESOURCE;
410 if (vpa_is_registered(&tvcpu->arch.dtl) ||
411 vpa_is_registered(&tvcpu->arch.slb_shadow))
412 break;
413
414 vpap = &tvcpu->arch.vpa;
415 err = 0;
416 break;
417
418 case H_VPA_DEREG_DTL: /* deregister DTL */
419 vpap = &tvcpu->arch.dtl;
420 err = 0;
421 break;
422
423 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
424 vpap = &tvcpu->arch.slb_shadow;
425 err = 0;
426 break;
427 }
428
429 if (vpap) {
430 vpap->next_gpa = vpa;
431 vpap->len = len;
432 vpap->update_pending = 1;
433 }
434
435 spin_unlock(&tvcpu->arch.vpa_update_lock);
436
437 return err;
438}
439
Paul Mackerras081f3232012-06-01 20:20:24 +1000440static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000441{
Paul Mackerras081f3232012-06-01 20:20:24 +1000442 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000443 void *va;
444 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000445 unsigned long gpa;
446
447 /*
448 * We need to pin the page pointed to by vpap->next_gpa,
449 * but we can't call kvmppc_pin_guest_page under the lock
450 * as it does get_user_pages() and down_read(). So we
451 * have to drop the lock, pin the page, then get the lock
452 * again and check that a new area didn't get registered
453 * in the meantime.
454 */
455 for (;;) {
456 gpa = vpap->next_gpa;
457 spin_unlock(&vcpu->arch.vpa_update_lock);
458 va = NULL;
459 nb = 0;
460 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000461 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000462 spin_lock(&vcpu->arch.vpa_update_lock);
463 if (gpa == vpap->next_gpa)
464 break;
465 /* sigh... unpin that one and try again */
466 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000467 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000468 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000469
470 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000471 if (va && nb < vpap->len) {
472 /*
473 * If it's now too short, it must be that userspace
474 * has changed the mappings underlying guest memory,
475 * so unregister the region.
476 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000477 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000478 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000479 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000480 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000481 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
482 vpap->dirty);
483 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000484 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000485 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000486 if (va)
487 vpap->pinned_end = va + vpap->len;
488}
Paul Mackerras93e60242011-12-12 12:28:55 +0000489
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000490static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
491{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000492 if (!(vcpu->arch.vpa.update_pending ||
493 vcpu->arch.slb_shadow.update_pending ||
494 vcpu->arch.dtl.update_pending))
495 return;
496
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000497 spin_lock(&vcpu->arch.vpa_update_lock);
498 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000499 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000500 if (vcpu->arch.vpa.pinned_addr)
501 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000502 }
503 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000504 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000505 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
506 vcpu->arch.dtl_index = 0;
507 }
508 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000509 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000510 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000511}
512
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000513/*
514 * Return the accumulated stolen time for the vcore up until `now'.
515 * The caller should hold the vcore lock.
516 */
517static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
518{
519 u64 p;
Paul Mackerras2711e242014-12-04 16:43:28 +1100520 unsigned long flags;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000521
Paul Mackerras2711e242014-12-04 16:43:28 +1100522 spin_lock_irqsave(&vc->stoltb_lock, flags);
523 p = vc->stolen_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000524 if (vc->vcore_state != VCORE_INACTIVE &&
Paul Mackerras2711e242014-12-04 16:43:28 +1100525 vc->preempt_tb != TB_NIL)
526 p += now - vc->preempt_tb;
527 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000528 return p;
529}
530
Paul Mackerras0456ec42012-02-03 00:56:21 +0000531static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
532 struct kvmppc_vcore *vc)
533{
534 struct dtl_entry *dt;
535 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000536 unsigned long stolen;
537 unsigned long core_stolen;
538 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000539
540 dt = vcpu->arch.dtl_ptr;
541 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000542 now = mftb();
543 core_stolen = vcore_stolen_time(vc, now);
544 stolen = core_stolen - vcpu->arch.stolen_logged;
545 vcpu->arch.stolen_logged = core_stolen;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100546 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000547 stolen += vcpu->arch.busy_stolen;
548 vcpu->arch.busy_stolen = 0;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100549 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000550 if (!dt || !vpa)
551 return;
552 memset(dt, 0, sizeof(struct dtl_entry));
553 dt->dispatch_reason = 7;
Alexander Graf02407552014-06-11 10:34:19 +0200554 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
555 dt->timebase = cpu_to_be64(now + vc->tb_offset);
556 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
557 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
558 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000559 ++dt;
560 if (dt == vcpu->arch.dtl.pinned_end)
561 dt = vcpu->arch.dtl.pinned_addr;
562 vcpu->arch.dtl_ptr = dt;
563 /* order writing *dt vs. writing vpa->dtl_idx */
564 smp_wmb();
Alexander Graf02407552014-06-11 10:34:19 +0200565 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000566 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000567}
568
Michael Neuling96423822014-06-02 11:03:01 +1000569static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
570{
571 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
572 return true;
573 if ((!vcpu->arch.vcore->arch_compat) &&
574 cpu_has_feature(CPU_FTR_ARCH_207S))
575 return true;
576 return false;
577}
578
579static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
580 unsigned long resource, unsigned long value1,
581 unsigned long value2)
582{
583 switch (resource) {
584 case H_SET_MODE_RESOURCE_SET_CIABR:
585 if (!kvmppc_power8_compatible(vcpu))
586 return H_P2;
587 if (value2)
588 return H_P4;
589 if (mflags)
590 return H_UNSUPPORTED_FLAG_START;
591 /* Guests can't breakpoint the hypervisor */
592 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
593 return H_P3;
594 vcpu->arch.ciabr = value1;
595 return H_SUCCESS;
596 case H_SET_MODE_RESOURCE_SET_DAWR:
597 if (!kvmppc_power8_compatible(vcpu))
598 return H_P2;
599 if (mflags)
600 return H_UNSUPPORTED_FLAG_START;
601 if (value2 & DABRX_HYP)
602 return H_P4;
603 vcpu->arch.dawr = value1;
604 vcpu->arch.dawrx = value2;
605 return H_SUCCESS;
606 default:
607 return H_TOO_HARD;
608 }
609}
610
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100611static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
612{
613 struct kvmppc_vcore *vcore = target->arch.vcore;
614
615 /*
616 * We expect to have been called by the real mode handler
617 * (kvmppc_rm_h_confer()) which would have directly returned
618 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
619 * have useful work to do and should not confer) so we don't
620 * recheck that here.
621 */
622
623 spin_lock(&vcore->lock);
624 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
625 vcore->vcore_state != VCORE_INACTIVE)
626 target = vcore->runner;
627 spin_unlock(&vcore->lock);
628
629 return kvm_vcpu_yield_to(target);
630}
631
632static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
633{
634 int yield_count = 0;
635 struct lppaca *lppaca;
636
637 spin_lock(&vcpu->arch.vpa_update_lock);
638 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
639 if (lppaca)
Paul Mackerrasecb6d612015-03-20 20:39:39 +1100640 yield_count = be32_to_cpu(lppaca->yield_count);
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100641 spin_unlock(&vcpu->arch.vpa_update_lock);
642 return yield_count;
643}
644
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000645int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
646{
647 unsigned long req = kvmppc_get_gpr(vcpu, 3);
648 unsigned long target, ret = H_SUCCESS;
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100649 int yield_count;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000650 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000651 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000652
Paul Mackerras699a0ea2014-06-02 11:02:59 +1000653 if (req <= MAX_HCALL_OPCODE &&
654 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
655 return RESUME_HOST;
656
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000657 switch (req) {
658 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000659 break;
660 case H_PROD:
661 target = kvmppc_get_gpr(vcpu, 4);
662 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
663 if (!tvcpu) {
664 ret = H_PARAMETER;
665 break;
666 }
667 tvcpu->arch.prodded = 1;
668 smp_mb();
669 if (vcpu->arch.ceded) {
670 if (waitqueue_active(&vcpu->wq)) {
671 wake_up_interruptible(&vcpu->wq);
672 vcpu->stat.halt_wakeup++;
673 }
674 }
675 break;
676 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +1000677 target = kvmppc_get_gpr(vcpu, 4);
678 if (target == -1)
679 break;
680 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
681 if (!tvcpu) {
682 ret = H_PARAMETER;
683 break;
684 }
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100685 yield_count = kvmppc_get_gpr(vcpu, 5);
686 if (kvmppc_get_yield_count(tvcpu) != yield_count)
687 break;
688 kvm_arch_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000689 break;
690 case H_REGISTER_VPA:
691 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
692 kvmppc_get_gpr(vcpu, 5),
693 kvmppc_get_gpr(vcpu, 6));
694 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000695 case H_RTAS:
696 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
697 return RESUME_HOST;
698
Paul Mackerrasc9438092013-11-16 17:46:05 +1100699 idx = srcu_read_lock(&vcpu->kvm->srcu);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000700 rc = kvmppc_rtas_hcall(vcpu);
Paul Mackerrasc9438092013-11-16 17:46:05 +1100701 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000702
703 if (rc == -ENOENT)
704 return RESUME_HOST;
705 else if (rc == 0)
706 break;
707
708 /* Send the error out to userspace via KVM_RUN */
709 return rc;
David Gibson99342cf82015-02-05 11:53:25 +1100710 case H_LOGICAL_CI_LOAD:
711 ret = kvmppc_h_logical_ci_load(vcpu);
712 if (ret == H_TOO_HARD)
713 return RESUME_HOST;
714 break;
715 case H_LOGICAL_CI_STORE:
716 ret = kvmppc_h_logical_ci_store(vcpu);
717 if (ret == H_TOO_HARD)
718 return RESUME_HOST;
719 break;
Michael Neuling96423822014-06-02 11:03:01 +1000720 case H_SET_MODE:
721 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
722 kvmppc_get_gpr(vcpu, 5),
723 kvmppc_get_gpr(vcpu, 6),
724 kvmppc_get_gpr(vcpu, 7));
725 if (ret == H_TOO_HARD)
726 return RESUME_HOST;
727 break;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000728 case H_XIRR:
729 case H_CPPR:
730 case H_EOI:
731 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000732 case H_IPOLL:
733 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000734 if (kvmppc_xics_enabled(vcpu)) {
735 ret = kvmppc_xics_hcall(vcpu, req);
736 break;
737 } /* fallthrough */
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000738 default:
739 return RESUME_HOST;
740 }
741 kvmppc_set_gpr(vcpu, 3, ret);
742 vcpu->arch.hcall_needed = 0;
743 return RESUME_GUEST;
744}
745
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000746static int kvmppc_hcall_impl_hv(unsigned long cmd)
747{
748 switch (cmd) {
749 case H_CEDE:
750 case H_PROD:
751 case H_CONFER:
752 case H_REGISTER_VPA:
Michael Neuling96423822014-06-02 11:03:01 +1000753 case H_SET_MODE:
David Gibson99342cf82015-02-05 11:53:25 +1100754 case H_LOGICAL_CI_LOAD:
755 case H_LOGICAL_CI_STORE:
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000756#ifdef CONFIG_KVM_XICS
757 case H_XIRR:
758 case H_CPPR:
759 case H_EOI:
760 case H_IPI:
761 case H_IPOLL:
762 case H_XIRR_X:
763#endif
764 return 1;
765 }
766
767 /* See if it's in the real-mode table */
768 return kvmppc_hcall_impl_hv_realmode(cmd);
769}
770
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530771static int kvmppc_emulate_debug_inst(struct kvm_run *run,
772 struct kvm_vcpu *vcpu)
773{
774 u32 last_inst;
775
776 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
777 EMULATE_DONE) {
778 /*
779 * Fetch failed, so return to guest and
780 * try executing it again.
781 */
782 return RESUME_GUEST;
783 }
784
785 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
786 run->exit_reason = KVM_EXIT_DEBUG;
787 run->debug.arch.address = kvmppc_get_pc(vcpu);
788 return RESUME_HOST;
789 } else {
790 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
791 return RESUME_GUEST;
792 }
793}
794
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530795static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
796 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000797{
798 int r = RESUME_HOST;
799
800 vcpu->stat.sum_exits++;
801
802 run->exit_reason = KVM_EXIT_UNKNOWN;
803 run->ready_for_interrupt_injection = 1;
804 switch (vcpu->arch.trap) {
805 /* We're good on these - the host merely wanted to get our attention */
806 case BOOK3S_INTERRUPT_HV_DECREMENTER:
807 vcpu->stat.dec_exits++;
808 r = RESUME_GUEST;
809 break;
810 case BOOK3S_INTERRUPT_EXTERNAL:
Paul Mackerras5d00f662014-01-08 21:25:28 +1100811 case BOOK3S_INTERRUPT_H_DOORBELL:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000812 vcpu->stat.ext_intr_exits++;
813 r = RESUME_GUEST;
814 break;
Mahesh Salgaonkardee6f242014-11-03 15:51:57 +1100815 /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
816 case BOOK3S_INTERRUPT_HMI:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000817 case BOOK3S_INTERRUPT_PERFMON:
818 r = RESUME_GUEST;
819 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000820 case BOOK3S_INTERRUPT_MACHINE_CHECK:
821 /*
822 * Deliver a machine check interrupt to the guest.
823 * We have to do this, even if the host has handled the
824 * machine check, because machine checks use SRR0/1 and
825 * the interrupt might have trashed guest state in them.
826 */
827 kvmppc_book3s_queue_irqprio(vcpu,
828 BOOK3S_INTERRUPT_MACHINE_CHECK);
829 r = RESUME_GUEST;
830 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000831 case BOOK3S_INTERRUPT_PROGRAM:
832 {
833 ulong flags;
834 /*
835 * Normally program interrupts are delivered directly
836 * to the guest by the hardware, but we can get here
837 * as a result of a hypervisor emulation interrupt
838 * (e40) getting turned into a 700 by BML RTAS.
839 */
840 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
841 kvmppc_core_queue_program(vcpu, flags);
842 r = RESUME_GUEST;
843 break;
844 }
845 case BOOK3S_INTERRUPT_SYSCALL:
846 {
847 /* hcall - punt to userspace */
848 int i;
849
Liu Ping Fan27025a62013-11-19 14:12:48 +0800850 /* hypercall with MSR_PR has already been handled in rmode,
851 * and never reaches here.
852 */
853
Paul Mackerrasde56a942011-06-29 00:21:34 +0000854 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
855 for (i = 0; i < 9; ++i)
856 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
857 run->exit_reason = KVM_EXIT_PAPR_HCALL;
858 vcpu->arch.hcall_needed = 1;
859 r = RESUME_HOST;
860 break;
861 }
862 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000863 * We get these next two if the guest accesses a page which it thinks
864 * it has mapped but which is not actually present, either because
865 * it is for an emulated I/O device or because the corresonding
866 * host page has been paged out. Any other HDSI/HISI interrupts
867 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000868 */
869 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000870 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000871 break;
872 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000873 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
874 vcpu->arch.fault_dsisr = 0;
875 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000876 break;
877 /*
878 * This occurs if the guest executes an illegal instruction.
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530879 * If the guest debug is disabled, generate a program interrupt
880 * to the guest. If guest debug is enabled, we need to check
881 * whether the instruction is a software breakpoint instruction.
882 * Accordingly return to Guest or Host.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000883 */
884 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Paul Mackerras4a157d62014-12-03 13:30:39 +1100885 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
886 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
887 swab32(vcpu->arch.emul_inst) :
888 vcpu->arch.emul_inst;
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530889 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
890 r = kvmppc_emulate_debug_inst(run, vcpu);
891 } else {
892 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
893 r = RESUME_GUEST;
894 }
Michael Ellermanbd3048b2014-01-08 21:25:23 +1100895 break;
896 /*
897 * This occurs if the guest (kernel or userspace), does something that
898 * is prohibited by HFSCR. We just generate a program interrupt to
899 * the guest.
900 */
901 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
902 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000903 r = RESUME_GUEST;
904 break;
905 default:
906 kvmppc_dump_regs(vcpu);
907 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
908 vcpu->arch.trap, kvmppc_get_pc(vcpu),
909 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +1000910 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000911 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000912 break;
913 }
914
Paul Mackerrasde56a942011-06-29 00:21:34 +0000915 return r;
916}
917
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530918static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
919 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000920{
921 int i;
922
Paul Mackerrasde56a942011-06-29 00:21:34 +0000923 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +0530924 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000925 for (i = 0; i < vcpu->arch.slb_max; i++) {
926 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
927 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
928 }
929
930 return 0;
931}
932
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530933static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
934 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000935{
936 int i, j;
937
Paul Mackerras9333e6c2014-09-02 16:14:43 +1000938 /* Only accept the same PVR as the host's, since we can't spoof it */
939 if (sregs->pvr != vcpu->arch.pvr)
940 return -EINVAL;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000941
942 j = 0;
943 for (i = 0; i < vcpu->arch.slb_nr; i++) {
944 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
945 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
946 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
947 ++j;
948 }
949 }
950 vcpu->arch.slb_max = j;
951
952 return 0;
953}
954
Alexey Kardashevskiya0840242014-07-19 17:59:34 +1000955static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
956 bool preserve_top32)
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000957{
Paul Mackerras8f902b02015-03-20 20:39:38 +1100958 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000959 struct kvmppc_vcore *vc = vcpu->arch.vcore;
960 u64 mask;
961
Paul Mackerras8f902b02015-03-20 20:39:38 +1100962 mutex_lock(&kvm->lock);
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000963 spin_lock(&vc->lock);
964 /*
Anton Blanchardd6829162014-01-08 21:25:30 +1100965 * If ILE (interrupt little-endian) has changed, update the
966 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
967 */
968 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
Anton Blanchardd6829162014-01-08 21:25:30 +1100969 struct kvm_vcpu *vcpu;
970 int i;
971
Anton Blanchardd6829162014-01-08 21:25:30 +1100972 kvm_for_each_vcpu(i, vcpu, kvm) {
973 if (vcpu->arch.vcore != vc)
974 continue;
975 if (new_lpcr & LPCR_ILE)
976 vcpu->arch.intr_msr |= MSR_LE;
977 else
978 vcpu->arch.intr_msr &= ~MSR_LE;
979 }
Anton Blanchardd6829162014-01-08 21:25:30 +1100980 }
981
982 /*
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000983 * Userspace can only modify DPFD (default prefetch depth),
984 * ILE (interrupt little-endian) and TC (translation control).
Paul Mackerrase0622bd2014-01-08 21:25:27 +1100985 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000986 */
987 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
Paul Mackerrase0622bd2014-01-08 21:25:27 +1100988 if (cpu_has_feature(CPU_FTR_ARCH_207S))
989 mask |= LPCR_AIL;
Alexey Kardashevskiya0840242014-07-19 17:59:34 +1000990
991 /* Broken 32-bit version of LPCR must not clear top bits */
992 if (preserve_top32)
993 mask &= 0xFFFFFFFF;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000994 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
995 spin_unlock(&vc->lock);
Paul Mackerras8f902b02015-03-20 20:39:38 +1100996 mutex_unlock(&kvm->lock);
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000997}
998
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530999static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1000 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001001{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001002 int r = 0;
1003 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +00001004
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001005 switch (id) {
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301006 case KVM_REG_PPC_DEBUG_INST:
1007 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
1008 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001009 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001010 *val = get_reg_val(id, 0);
1011 break;
1012 case KVM_REG_PPC_DABR:
1013 *val = get_reg_val(id, vcpu->arch.dabr);
1014 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001015 case KVM_REG_PPC_DABRX:
1016 *val = get_reg_val(id, vcpu->arch.dabrx);
1017 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001018 case KVM_REG_PPC_DSCR:
1019 *val = get_reg_val(id, vcpu->arch.dscr);
1020 break;
1021 case KVM_REG_PPC_PURR:
1022 *val = get_reg_val(id, vcpu->arch.purr);
1023 break;
1024 case KVM_REG_PPC_SPURR:
1025 *val = get_reg_val(id, vcpu->arch.spurr);
1026 break;
1027 case KVM_REG_PPC_AMR:
1028 *val = get_reg_val(id, vcpu->arch.amr);
1029 break;
1030 case KVM_REG_PPC_UAMOR:
1031 *val = get_reg_val(id, vcpu->arch.uamor);
1032 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001033 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001034 i = id - KVM_REG_PPC_MMCR0;
1035 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
1036 break;
1037 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1038 i = id - KVM_REG_PPC_PMC1;
1039 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +00001040 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001041 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1042 i = id - KVM_REG_PPC_SPMC1;
1043 *val = get_reg_val(id, vcpu->arch.spmc[i]);
1044 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001045 case KVM_REG_PPC_SIAR:
1046 *val = get_reg_val(id, vcpu->arch.siar);
1047 break;
1048 case KVM_REG_PPC_SDAR:
1049 *val = get_reg_val(id, vcpu->arch.sdar);
1050 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001051 case KVM_REG_PPC_SIER:
1052 *val = get_reg_val(id, vcpu->arch.sier);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001053 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001054 case KVM_REG_PPC_IAMR:
1055 *val = get_reg_val(id, vcpu->arch.iamr);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001056 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001057 case KVM_REG_PPC_PSPB:
1058 *val = get_reg_val(id, vcpu->arch.pspb);
1059 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001060 case KVM_REG_PPC_DPDES:
1061 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1062 break;
1063 case KVM_REG_PPC_DAWR:
1064 *val = get_reg_val(id, vcpu->arch.dawr);
1065 break;
1066 case KVM_REG_PPC_DAWRX:
1067 *val = get_reg_val(id, vcpu->arch.dawrx);
1068 break;
1069 case KVM_REG_PPC_CIABR:
1070 *val = get_reg_val(id, vcpu->arch.ciabr);
1071 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001072 case KVM_REG_PPC_CSIGR:
1073 *val = get_reg_val(id, vcpu->arch.csigr);
1074 break;
1075 case KVM_REG_PPC_TACR:
1076 *val = get_reg_val(id, vcpu->arch.tacr);
1077 break;
1078 case KVM_REG_PPC_TCSCR:
1079 *val = get_reg_val(id, vcpu->arch.tcscr);
1080 break;
1081 case KVM_REG_PPC_PID:
1082 *val = get_reg_val(id, vcpu->arch.pid);
1083 break;
1084 case KVM_REG_PPC_ACOP:
1085 *val = get_reg_val(id, vcpu->arch.acop);
1086 break;
1087 case KVM_REG_PPC_WORT:
1088 *val = get_reg_val(id, vcpu->arch.wort);
1089 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001090 case KVM_REG_PPC_VPA_ADDR:
1091 spin_lock(&vcpu->arch.vpa_update_lock);
1092 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1093 spin_unlock(&vcpu->arch.vpa_update_lock);
1094 break;
1095 case KVM_REG_PPC_VPA_SLB:
1096 spin_lock(&vcpu->arch.vpa_update_lock);
1097 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1098 val->vpaval.length = vcpu->arch.slb_shadow.len;
1099 spin_unlock(&vcpu->arch.vpa_update_lock);
1100 break;
1101 case KVM_REG_PPC_VPA_DTL:
1102 spin_lock(&vcpu->arch.vpa_update_lock);
1103 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1104 val->vpaval.length = vcpu->arch.dtl.len;
1105 spin_unlock(&vcpu->arch.vpa_update_lock);
1106 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001107 case KVM_REG_PPC_TB_OFFSET:
1108 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1109 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001110 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001111 case KVM_REG_PPC_LPCR_64:
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001112 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1113 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001114 case KVM_REG_PPC_PPR:
1115 *val = get_reg_val(id, vcpu->arch.ppr);
1116 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001117#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1118 case KVM_REG_PPC_TFHAR:
1119 *val = get_reg_val(id, vcpu->arch.tfhar);
1120 break;
1121 case KVM_REG_PPC_TFIAR:
1122 *val = get_reg_val(id, vcpu->arch.tfiar);
1123 break;
1124 case KVM_REG_PPC_TEXASR:
1125 *val = get_reg_val(id, vcpu->arch.texasr);
1126 break;
1127 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1128 i = id - KVM_REG_PPC_TM_GPR0;
1129 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1130 break;
1131 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1132 {
1133 int j;
1134 i = id - KVM_REG_PPC_TM_VSR0;
1135 if (i < 32)
1136 for (j = 0; j < TS_FPRWIDTH; j++)
1137 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1138 else {
1139 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1140 val->vval = vcpu->arch.vr_tm.vr[i-32];
1141 else
1142 r = -ENXIO;
1143 }
1144 break;
1145 }
1146 case KVM_REG_PPC_TM_CR:
1147 *val = get_reg_val(id, vcpu->arch.cr_tm);
1148 break;
1149 case KVM_REG_PPC_TM_LR:
1150 *val = get_reg_val(id, vcpu->arch.lr_tm);
1151 break;
1152 case KVM_REG_PPC_TM_CTR:
1153 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1154 break;
1155 case KVM_REG_PPC_TM_FPSCR:
1156 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1157 break;
1158 case KVM_REG_PPC_TM_AMR:
1159 *val = get_reg_val(id, vcpu->arch.amr_tm);
1160 break;
1161 case KVM_REG_PPC_TM_PPR:
1162 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1163 break;
1164 case KVM_REG_PPC_TM_VRSAVE:
1165 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1166 break;
1167 case KVM_REG_PPC_TM_VSCR:
1168 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1169 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1170 else
1171 r = -ENXIO;
1172 break;
1173 case KVM_REG_PPC_TM_DSCR:
1174 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1175 break;
1176 case KVM_REG_PPC_TM_TAR:
1177 *val = get_reg_val(id, vcpu->arch.tar_tm);
1178 break;
1179#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001180 case KVM_REG_PPC_ARCH_COMPAT:
1181 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1182 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001183 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001184 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001185 break;
1186 }
1187
1188 return r;
1189}
1190
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301191static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1192 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001193{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001194 int r = 0;
1195 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001196 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +00001197
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001198 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00001199 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +00001200 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001201 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +00001202 r = -EINVAL;
1203 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001204 case KVM_REG_PPC_DABR:
1205 vcpu->arch.dabr = set_reg_val(id, *val);
1206 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001207 case KVM_REG_PPC_DABRX:
1208 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1209 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001210 case KVM_REG_PPC_DSCR:
1211 vcpu->arch.dscr = set_reg_val(id, *val);
1212 break;
1213 case KVM_REG_PPC_PURR:
1214 vcpu->arch.purr = set_reg_val(id, *val);
1215 break;
1216 case KVM_REG_PPC_SPURR:
1217 vcpu->arch.spurr = set_reg_val(id, *val);
1218 break;
1219 case KVM_REG_PPC_AMR:
1220 vcpu->arch.amr = set_reg_val(id, *val);
1221 break;
1222 case KVM_REG_PPC_UAMOR:
1223 vcpu->arch.uamor = set_reg_val(id, *val);
1224 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001225 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001226 i = id - KVM_REG_PPC_MMCR0;
1227 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1228 break;
1229 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1230 i = id - KVM_REG_PPC_PMC1;
1231 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1232 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001233 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1234 i = id - KVM_REG_PPC_SPMC1;
1235 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1236 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001237 case KVM_REG_PPC_SIAR:
1238 vcpu->arch.siar = set_reg_val(id, *val);
1239 break;
1240 case KVM_REG_PPC_SDAR:
1241 vcpu->arch.sdar = set_reg_val(id, *val);
1242 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001243 case KVM_REG_PPC_SIER:
1244 vcpu->arch.sier = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001245 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001246 case KVM_REG_PPC_IAMR:
1247 vcpu->arch.iamr = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001248 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001249 case KVM_REG_PPC_PSPB:
1250 vcpu->arch.pspb = set_reg_val(id, *val);
1251 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001252 case KVM_REG_PPC_DPDES:
1253 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1254 break;
1255 case KVM_REG_PPC_DAWR:
1256 vcpu->arch.dawr = set_reg_val(id, *val);
1257 break;
1258 case KVM_REG_PPC_DAWRX:
1259 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1260 break;
1261 case KVM_REG_PPC_CIABR:
1262 vcpu->arch.ciabr = set_reg_val(id, *val);
1263 /* Don't allow setting breakpoints in hypervisor code */
1264 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1265 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1266 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001267 case KVM_REG_PPC_CSIGR:
1268 vcpu->arch.csigr = set_reg_val(id, *val);
1269 break;
1270 case KVM_REG_PPC_TACR:
1271 vcpu->arch.tacr = set_reg_val(id, *val);
1272 break;
1273 case KVM_REG_PPC_TCSCR:
1274 vcpu->arch.tcscr = set_reg_val(id, *val);
1275 break;
1276 case KVM_REG_PPC_PID:
1277 vcpu->arch.pid = set_reg_val(id, *val);
1278 break;
1279 case KVM_REG_PPC_ACOP:
1280 vcpu->arch.acop = set_reg_val(id, *val);
1281 break;
1282 case KVM_REG_PPC_WORT:
1283 vcpu->arch.wort = set_reg_val(id, *val);
1284 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001285 case KVM_REG_PPC_VPA_ADDR:
1286 addr = set_reg_val(id, *val);
1287 r = -EINVAL;
1288 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1289 vcpu->arch.dtl.next_gpa))
1290 break;
1291 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1292 break;
1293 case KVM_REG_PPC_VPA_SLB:
1294 addr = val->vpaval.addr;
1295 len = val->vpaval.length;
1296 r = -EINVAL;
1297 if (addr && !vcpu->arch.vpa.next_gpa)
1298 break;
1299 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1300 break;
1301 case KVM_REG_PPC_VPA_DTL:
1302 addr = val->vpaval.addr;
1303 len = val->vpaval.length;
1304 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +00001305 if (addr && (len < sizeof(struct dtl_entry) ||
1306 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +00001307 break;
1308 len -= len % sizeof(struct dtl_entry);
1309 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1310 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001311 case KVM_REG_PPC_TB_OFFSET:
1312 /* round up to multiple of 2^24 */
1313 vcpu->arch.vcore->tb_offset =
1314 ALIGN(set_reg_val(id, *val), 1UL << 24);
1315 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001316 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001317 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1318 break;
1319 case KVM_REG_PPC_LPCR_64:
1320 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001321 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001322 case KVM_REG_PPC_PPR:
1323 vcpu->arch.ppr = set_reg_val(id, *val);
1324 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001325#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1326 case KVM_REG_PPC_TFHAR:
1327 vcpu->arch.tfhar = set_reg_val(id, *val);
1328 break;
1329 case KVM_REG_PPC_TFIAR:
1330 vcpu->arch.tfiar = set_reg_val(id, *val);
1331 break;
1332 case KVM_REG_PPC_TEXASR:
1333 vcpu->arch.texasr = set_reg_val(id, *val);
1334 break;
1335 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1336 i = id - KVM_REG_PPC_TM_GPR0;
1337 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1338 break;
1339 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1340 {
1341 int j;
1342 i = id - KVM_REG_PPC_TM_VSR0;
1343 if (i < 32)
1344 for (j = 0; j < TS_FPRWIDTH; j++)
1345 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1346 else
1347 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1348 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1349 else
1350 r = -ENXIO;
1351 break;
1352 }
1353 case KVM_REG_PPC_TM_CR:
1354 vcpu->arch.cr_tm = set_reg_val(id, *val);
1355 break;
1356 case KVM_REG_PPC_TM_LR:
1357 vcpu->arch.lr_tm = set_reg_val(id, *val);
1358 break;
1359 case KVM_REG_PPC_TM_CTR:
1360 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1361 break;
1362 case KVM_REG_PPC_TM_FPSCR:
1363 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1364 break;
1365 case KVM_REG_PPC_TM_AMR:
1366 vcpu->arch.amr_tm = set_reg_val(id, *val);
1367 break;
1368 case KVM_REG_PPC_TM_PPR:
1369 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1370 break;
1371 case KVM_REG_PPC_TM_VRSAVE:
1372 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1373 break;
1374 case KVM_REG_PPC_TM_VSCR:
1375 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1376 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1377 else
1378 r = - ENXIO;
1379 break;
1380 case KVM_REG_PPC_TM_DSCR:
1381 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1382 break;
1383 case KVM_REG_PPC_TM_TAR:
1384 vcpu->arch.tar_tm = set_reg_val(id, *val);
1385 break;
1386#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001387 case KVM_REG_PPC_ARCH_COMPAT:
1388 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1389 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001390 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001391 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001392 break;
1393 }
1394
1395 return r;
1396}
1397
Stewart Smithde9bdd12014-07-18 14:18:42 +10001398static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1399{
1400 struct kvmppc_vcore *vcore;
1401
1402 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1403
1404 if (vcore == NULL)
1405 return NULL;
1406
1407 INIT_LIST_HEAD(&vcore->runnable_threads);
1408 spin_lock_init(&vcore->lock);
Paul Mackerras2711e242014-12-04 16:43:28 +11001409 spin_lock_init(&vcore->stoltb_lock);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001410 init_waitqueue_head(&vcore->wq);
1411 vcore->preempt_tb = TB_NIL;
1412 vcore->lpcr = kvm->arch.lpcr;
1413 vcore->first_vcpuid = core * threads_per_subcore;
1414 vcore->kvm = kvm;
1415
Stewart Smith9678cda2014-07-18 14:18:43 +10001416 vcore->mpp_buffer_is_valid = false;
1417
1418 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1419 vcore->mpp_buffer = (void *)__get_free_pages(
1420 GFP_KERNEL|__GFP_ZERO,
1421 MPP_BUFFER_ORDER);
1422
Stewart Smithde9bdd12014-07-18 14:18:42 +10001423 return vcore;
1424}
1425
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301426static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1427 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001428{
1429 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001430 int err = -EINVAL;
1431 int core;
1432 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001433
Michael Ellerman3102f782014-05-23 18:15:29 +10001434 core = id / threads_per_subcore;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001435 if (core >= KVM_MAX_VCORES)
1436 goto out;
1437
1438 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001439 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001440 if (!vcpu)
1441 goto out;
1442
1443 err = kvm_vcpu_init(vcpu, kvm, id);
1444 if (err)
1445 goto free_vcpu;
1446
1447 vcpu->arch.shared = &vcpu->arch.shregs;
Alexander Graf5deb8e72014-04-24 13:46:24 +02001448#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1449 /*
1450 * The shared struct is never shared on HV,
1451 * so we can always use host endianness
1452 */
1453#ifdef __BIG_ENDIAN__
1454 vcpu->arch.shared_big_endian = true;
1455#else
1456 vcpu->arch.shared_big_endian = false;
1457#endif
1458#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001459 vcpu->arch.mmcr[0] = MMCR0_FC;
1460 vcpu->arch.ctrl = CTRL_RUNLATCH;
1461 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301462 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001463 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001464 spin_lock_init(&vcpu->arch.tbacct_lock);
1465 vcpu->arch.busy_preempt = TB_NIL;
Anton Blanchardd6829162014-01-08 21:25:30 +11001466 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001467
Paul Mackerrasde56a942011-06-29 00:21:34 +00001468 kvmppc_mmu_book3s_hv_init(vcpu);
1469
Paul Mackerras8455d792012-10-15 01:17:42 +00001470 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001471
1472 init_waitqueue_head(&vcpu->arch.cpu_run);
1473
1474 mutex_lock(&kvm->lock);
1475 vcore = kvm->arch.vcores[core];
1476 if (!vcore) {
Stewart Smithde9bdd12014-07-18 14:18:42 +10001477 vcore = kvmppc_vcore_create(kvm, core);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001478 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001479 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001480 }
1481 mutex_unlock(&kvm->lock);
1482
1483 if (!vcore)
1484 goto free_vcpu;
1485
1486 spin_lock(&vcore->lock);
1487 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001488 spin_unlock(&vcore->lock);
1489 vcpu->arch.vcore = vcore;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001490 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001491
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001492 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1493 kvmppc_sanity_check(vcpu);
1494
Paul Mackerrasde56a942011-06-29 00:21:34 +00001495 return vcpu;
1496
1497free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001498 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001499out:
1500 return ERR_PTR(err);
1501}
1502
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001503static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1504{
1505 if (vpa->pinned_addr)
1506 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1507 vpa->dirty);
1508}
1509
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301510static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001511{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001512 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001513 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1514 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1515 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001516 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001517 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001518 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001519}
1520
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301521static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1522{
1523 /* Indicate we want to get back into the guest */
1524 return 1;
1525}
1526
Paul Mackerras19ccb762011-07-23 17:42:46 +10001527static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001528{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001529 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001530
Paul Mackerras19ccb762011-07-23 17:42:46 +10001531 now = get_tb();
1532 if (now > vcpu->arch.dec_expires) {
1533 /* decrementer has already gone negative */
1534 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001535 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001536 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001537 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001538 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1539 / tb_ticks_per_sec;
1540 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1541 HRTIMER_MODE_REL);
1542 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001543}
1544
Paul Mackerras19ccb762011-07-23 17:42:46 +10001545static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001546{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001547 vcpu->arch.ceded = 0;
1548 if (vcpu->arch.timer_running) {
1549 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1550 vcpu->arch.timer_running = 0;
1551 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001552}
1553
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001554extern void __kvmppc_vcore_entry(void);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001555
1556static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1557 struct kvm_vcpu *vcpu)
1558{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001559 u64 now;
1560
Paul Mackerras371fefd2011-06-29 00:23:08 +00001561 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1562 return;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001563 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001564 now = mftb();
1565 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1566 vcpu->arch.stolen_logged;
1567 vcpu->arch.busy_preempt = now;
1568 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001569 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001570 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001571 list_del(&vcpu->arch.run_list);
1572}
1573
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001574static int kvmppc_grab_hwthread(int cpu)
1575{
1576 struct paca_struct *tpaca;
Paul Mackerrasb754c732014-09-02 16:14:42 +10001577 long timeout = 10000;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001578
1579 tpaca = &paca[cpu];
1580
1581 /* Ensure the thread won't go into the kernel if it wakes */
1582 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001583 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001584
1585 /*
1586 * If the thread is already executing in the kernel (e.g. handling
1587 * a stray interrupt), wait for it to get back to nap mode.
1588 * The smp_mb() is to ensure that our setting of hwthread_req
1589 * is visible before we look at hwthread_state, so if this
1590 * races with the code at system_reset_pSeries and the thread
1591 * misses our setting of hwthread_req, we are sure to see its
1592 * setting of hwthread_state, and vice versa.
1593 */
1594 smp_mb();
1595 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1596 if (--timeout <= 0) {
1597 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1598 return -EBUSY;
1599 }
1600 udelay(1);
1601 }
1602 return 0;
1603}
1604
1605static void kvmppc_release_hwthread(int cpu)
1606{
1607 struct paca_struct *tpaca;
1608
1609 tpaca = &paca[cpu];
1610 tpaca->kvm_hstate.hwthread_req = 0;
1611 tpaca->kvm_hstate.kvm_vcpu = NULL;
1612}
1613
Paul Mackerras371fefd2011-06-29 00:23:08 +00001614static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1615{
1616 int cpu;
1617 struct paca_struct *tpaca;
1618 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1619
Paul Mackerras19ccb762011-07-23 17:42:46 +10001620 if (vcpu->arch.timer_running) {
1621 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1622 vcpu->arch.timer_running = 0;
1623 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001624 cpu = vc->pcpu + vcpu->arch.ptid;
1625 tpaca = &paca[cpu];
1626 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1627 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001628 tpaca->kvm_hstate.ptid = vcpu->arch.ptid;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001629 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001630 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +00001631#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001632 if (cpu != smp_processor_id()) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001633 xics_wake_cpu(cpu);
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001634 if (vcpu->arch.ptid)
1635 ++vc->n_woken;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001636 }
1637#endif
1638}
1639
1640static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1641{
1642 int i;
1643
1644 HMT_low();
1645 i = 0;
1646 while (vc->nap_count < vc->n_woken) {
1647 if (++i >= 1000000) {
1648 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1649 vc->nap_count, vc->n_woken);
1650 break;
1651 }
1652 cpu_relax();
1653 }
1654 HMT_medium();
1655}
1656
1657/*
1658 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001659 * this core are off-line. Then grab the threads so they can't
1660 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001661 */
1662static int on_primary_thread(void)
1663{
1664 int cpu = smp_processor_id();
Michael Ellerman3102f782014-05-23 18:15:29 +10001665 int thr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001666
Michael Ellerman3102f782014-05-23 18:15:29 +10001667 /* Are we on a primary subcore? */
1668 if (cpu_thread_in_subcore(cpu))
Paul Mackerras371fefd2011-06-29 00:23:08 +00001669 return 0;
Michael Ellerman3102f782014-05-23 18:15:29 +10001670
1671 thr = 0;
1672 while (++thr < threads_per_subcore)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001673 if (cpu_online(cpu + thr))
1674 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001675
1676 /* Grab all hw threads so they can't go into the kernel */
Michael Ellerman3102f782014-05-23 18:15:29 +10001677 for (thr = 1; thr < threads_per_subcore; ++thr) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00001678 if (kvmppc_grab_hwthread(cpu + thr)) {
1679 /* Couldn't grab one; let the others go */
1680 do {
1681 kvmppc_release_hwthread(cpu + thr);
1682 } while (--thr > 0);
1683 return 0;
1684 }
1685 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001686 return 1;
1687}
1688
Stewart Smith9678cda2014-07-18 14:18:43 +10001689static void kvmppc_start_saving_l2_cache(struct kvmppc_vcore *vc)
1690{
1691 phys_addr_t phy_addr, mpp_addr;
1692
1693 phy_addr = (phys_addr_t)virt_to_phys(vc->mpp_buffer);
1694 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1695
1696 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_ABORT);
1697 logmpp(mpp_addr | PPC_LOGMPP_LOG_L2);
1698
1699 vc->mpp_buffer_is_valid = true;
1700}
1701
1702static void kvmppc_start_restoring_l2_cache(const struct kvmppc_vcore *vc)
1703{
1704 phys_addr_t phy_addr, mpp_addr;
1705
1706 phy_addr = virt_to_phys(vc->mpp_buffer);
1707 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1708
1709 /* We must abort any in-progress save operations to ensure
1710 * the table is valid so that prefetch engine knows when to
1711 * stop prefetching. */
1712 logmpp(mpp_addr | PPC_LOGMPP_LOG_ABORT);
1713 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_WHOLE_TABLE);
1714}
1715
Paul Mackerras371fefd2011-06-29 00:23:08 +00001716/*
1717 * Run a set of guest threads on a physical core.
1718 * Called with vc->lock held.
1719 */
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001720static void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001721{
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001722 struct kvm_vcpu *vcpu, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001723 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001724 u64 now;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001725 int i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001726 int srcu_idx;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001727 struct kvm_vcpu *vcpus_to_update[threads_per_core];
Paul Mackerrasde56a942011-06-29 00:21:34 +00001728
Paul Mackerras371fefd2011-06-29 00:23:08 +00001729 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +10001730 need_vpa_update = 0;
1731 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001732 if (signal_pending(vcpu->arch.run_task))
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001733 return;
1734 if (vcpu->arch.vpa.update_pending ||
1735 vcpu->arch.slb_shadow.update_pending ||
1736 vcpu->arch.dtl.update_pending)
1737 vcpus_to_update[need_vpa_update++] = vcpu;
Paul Mackerras081f3232012-06-01 20:20:24 +10001738 }
1739
1740 /*
1741 * Initialize *vc, in particular vc->vcore_state, so we can
1742 * drop the vcore lock if necessary.
1743 */
1744 vc->n_woken = 0;
1745 vc->nap_count = 0;
1746 vc->entry_exit_count = 0;
Paul Mackerras2711e242014-12-04 16:43:28 +11001747 vc->preempt_tb = TB_NIL;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001748 vc->vcore_state = VCORE_STARTING;
Paul Mackerras081f3232012-06-01 20:20:24 +10001749 vc->in_guest = 0;
1750 vc->napping_threads = 0;
Sam Bobroff90fd09f2014-12-03 13:30:40 +11001751 vc->conferring_threads = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +10001752
1753 /*
1754 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1755 * which can't be called with any spinlocks held.
1756 */
1757 if (need_vpa_update) {
1758 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001759 for (i = 0; i < need_vpa_update; ++i)
1760 kvmppc_update_vpas(vcpus_to_update[i]);
Paul Mackerras081f3232012-06-01 20:20:24 +10001761 spin_lock(&vc->lock);
1762 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001763
1764 /*
Michael Ellerman3102f782014-05-23 18:15:29 +10001765 * Make sure we are running on primary threads, and that secondary
1766 * threads are offline. Also check if the number of threads in this
1767 * guest are greater than the current system threads per guest.
Paul Mackerras7b444c62012-10-15 01:16:14 +00001768 */
Michael Ellerman3102f782014-05-23 18:15:29 +10001769 if ((threads_per_core > 1) &&
1770 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00001771 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1772 vcpu->arch.ret = -EBUSY;
1773 goto out;
1774 }
1775
Michael Ellerman3102f782014-05-23 18:15:29 +10001776
Paul Mackerras371fefd2011-06-29 00:23:08 +00001777 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001778 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001779 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001780 kvmppc_create_dtl_entry(vcpu, vc);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001781 trace_kvm_guest_enter(vcpu);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001782 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001783
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001784 /* Set this explicitly in case thread 0 doesn't have a vcpu */
1785 get_paca()->kvm_hstate.kvm_vcore = vc;
1786 get_paca()->kvm_hstate.ptid = 0;
1787
Paul Mackerras2f12f032012-10-15 01:17:17 +00001788 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001789 preempt_disable();
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001790
1791 trace_kvmppc_run_core(vc, 0);
1792
Paul Mackerras19ccb762011-07-23 17:42:46 +10001793 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001794
Paul Mackerras19ccb762011-07-23 17:42:46 +10001795 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001796
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001797 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001798
Stewart Smith9678cda2014-07-18 14:18:43 +10001799 if (vc->mpp_buffer_is_valid)
1800 kvmppc_start_restoring_l2_cache(vc);
1801
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001802 __kvmppc_vcore_entry();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001803
Paul Mackerras371fefd2011-06-29 00:23:08 +00001804 spin_lock(&vc->lock);
Stewart Smith9678cda2014-07-18 14:18:43 +10001805
1806 if (vc->mpp_buffer)
1807 kvmppc_start_saving_l2_cache(vc);
1808
Paul Mackerras19ccb762011-07-23 17:42:46 +10001809 /* disable sending of IPIs on virtual external irqs */
1810 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1811 vcpu->cpu = -1;
1812 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001813 if (vc->nap_count < vc->n_woken)
1814 kvmppc_wait_for_nap(vc);
Michael Ellerman3102f782014-05-23 18:15:29 +10001815 for (i = 0; i < threads_per_subcore; ++i)
Paul Mackerras2f12f032012-10-15 01:17:17 +00001816 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001817 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001818 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001819 spin_unlock(&vc->lock);
1820
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001821 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001822
Paul Mackerras371fefd2011-06-29 00:23:08 +00001823 /* make sure updates to secondary vcpu structs are visible now */
1824 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001825 kvm_guest_exit();
1826
1827 preempt_enable();
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09001828 cond_resched();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001829
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001830 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001831 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001832 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1833 /* cancel pending dec exception if dec is positive */
1834 if (now < vcpu->arch.dec_expires &&
1835 kvmppc_core_pending_dec(vcpu))
1836 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001837
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001838 trace_kvm_guest_exit(vcpu);
1839
Paul Mackerras19ccb762011-07-23 17:42:46 +10001840 ret = RESUME_GUEST;
1841 if (vcpu->arch.trap)
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301842 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1843 vcpu->arch.run_task);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001844
Paul Mackerras371fefd2011-06-29 00:23:08 +00001845 vcpu->arch.ret = ret;
1846 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001847
1848 if (vcpu->arch.ceded) {
Greg Kurze59d24e2014-02-06 17:36:56 +01001849 if (!is_kvmppc_resume_guest(ret))
Paul Mackerras19ccb762011-07-23 17:42:46 +10001850 kvmppc_end_cede(vcpu);
1851 else
1852 kvmppc_set_timer(vcpu);
1853 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001854 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001855
Paul Mackerrasde56a942011-06-29 00:21:34 +00001856 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001857 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001858 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1859 arch.run_list) {
Greg Kurze59d24e2014-02-06 17:36:56 +01001860 if (!is_kvmppc_resume_guest(vcpu->arch.ret)) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001861 kvmppc_remove_runnable(vc, vcpu);
1862 wake_up(&vcpu->arch.cpu_run);
1863 }
1864 }
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001865
1866 trace_kvmppc_run_core(vc, 1);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001867}
1868
Paul Mackerras19ccb762011-07-23 17:42:46 +10001869/*
1870 * Wait for some other vcpu thread to execute us, and
1871 * wake us up when we need to handle something in the host.
1872 */
1873static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001874{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001875 DEFINE_WAIT(wait);
1876
Paul Mackerras19ccb762011-07-23 17:42:46 +10001877 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1878 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1879 schedule();
1880 finish_wait(&vcpu->arch.cpu_run, &wait);
1881}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001882
Paul Mackerras19ccb762011-07-23 17:42:46 +10001883/*
1884 * All the vcpus in this vcore are idle, so wait for a decrementer
1885 * or external interrupt to one of the vcpus. vc->lock is held.
1886 */
1887static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1888{
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11001889 struct kvm_vcpu *vcpu;
1890 int do_sleep = 1;
1891
Paul Mackerras19ccb762011-07-23 17:42:46 +10001892 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001893
1894 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11001895
1896 /*
1897 * Check one last time for pending exceptions and ceded state after
1898 * we put ourselves on the wait queue
1899 */
1900 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1901 if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded) {
1902 do_sleep = 0;
1903 break;
1904 }
1905 }
1906
1907 if (!do_sleep) {
1908 finish_wait(&vc->wq, &wait);
1909 return;
1910 }
1911
Paul Mackerras19ccb762011-07-23 17:42:46 +10001912 vc->vcore_state = VCORE_SLEEPING;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001913 trace_kvmppc_vcore_blocked(vc, 0);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001914 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001915 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001916 finish_wait(&vc->wq, &wait);
1917 spin_lock(&vc->lock);
1918 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001919 trace_kvmppc_vcore_blocked(vc, 1);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001920}
1921
1922static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1923{
1924 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001925 struct kvmppc_vcore *vc;
1926 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001927
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001928 trace_kvmppc_run_vcpu_enter(vcpu);
1929
Paul Mackerras371fefd2011-06-29 00:23:08 +00001930 kvm_run->exit_reason = 0;
1931 vcpu->arch.ret = RESUME_GUEST;
1932 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001933 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001934
Paul Mackerras371fefd2011-06-29 00:23:08 +00001935 /*
1936 * Synchronize with other threads in this virtual core
1937 */
1938 vc = vcpu->arch.vcore;
1939 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001940 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001941 vcpu->arch.run_task = current;
1942 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001943 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10001944 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001945 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001946 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1947 ++vc->n_runnable;
1948
Paul Mackerras19ccb762011-07-23 17:42:46 +10001949 /*
1950 * This happens the first time this is called for a vcpu.
1951 * If the vcore is already running, we may be able to start
1952 * this thread straight away and have it join in.
1953 */
Paul Mackerras8455d792012-10-15 01:17:42 +00001954 if (!signal_pending(current)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001955 if (vc->vcore_state == VCORE_RUNNING &&
1956 VCORE_EXIT_COUNT(vc) == 0) {
Paul Mackerras2f12f032012-10-15 01:17:17 +00001957 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001958 kvmppc_start_thread(vcpu);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001959 trace_kvm_guest_enter(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00001960 } else if (vc->vcore_state == VCORE_SLEEPING) {
1961 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001962 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001963
Paul Mackerras8455d792012-10-15 01:17:42 +00001964 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001965
1966 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1967 !signal_pending(current)) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001968 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001969 spin_unlock(&vc->lock);
1970 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1971 spin_lock(&vc->lock);
1972 continue;
1973 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001974 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1975 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001976 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001977 if (signal_pending(v->arch.run_task)) {
1978 kvmppc_remove_runnable(vc, v);
1979 v->stat.signal_exits++;
1980 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1981 v->arch.ret = -EINTR;
1982 wake_up(&v->arch.cpu_run);
1983 }
1984 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001985 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1986 break;
1987 vc->runner = vcpu;
1988 n_ceded = 0;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001989 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001990 if (!v->arch.pending_exceptions)
1991 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001992 else
1993 v->arch.ceded = 0;
1994 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001995 if (n_ceded == vc->n_runnable)
1996 kvmppc_vcore_blocked(vc);
1997 else
1998 kvmppc_run_core(vc);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001999 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002000 }
2001
Paul Mackerras8455d792012-10-15 01:17:42 +00002002 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2003 (vc->vcore_state == VCORE_RUNNING ||
2004 vc->vcore_state == VCORE_EXITING)) {
2005 spin_unlock(&vc->lock);
2006 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
2007 spin_lock(&vc->lock);
2008 }
2009
2010 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2011 kvmppc_remove_runnable(vc, vcpu);
2012 vcpu->stat.signal_exits++;
2013 kvm_run->exit_reason = KVM_EXIT_INTR;
2014 vcpu->arch.ret = -EINTR;
2015 }
2016
2017 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
2018 /* Wake up some vcpu to run the core */
2019 v = list_first_entry(&vc->runnable_threads,
2020 struct kvm_vcpu, arch.run_list);
2021 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002022 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002023
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002024 trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002025 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002026 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002027}
2028
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302029static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002030{
2031 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002032 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002033
Alexander Grafaf8f38b2011-08-10 13:57:08 +02002034 if (!vcpu->arch.sane) {
2035 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2036 return -EINVAL;
2037 }
2038
Scott Wood25051b52011-11-08 18:23:23 -06002039 kvmppc_core_prepare_to_enter(vcpu);
2040
Paul Mackerras19ccb762011-07-23 17:42:46 +10002041 /* No need to go into the guest when all we'll do is come back out */
2042 if (signal_pending(current)) {
2043 run->exit_reason = KVM_EXIT_INTR;
2044 return -EINTR;
2045 }
2046
Paul Mackerras32fad282012-05-04 02:32:53 +00002047 atomic_inc(&vcpu->kvm->arch.vcpus_running);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002048 /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */
Paul Mackerras32fad282012-05-04 02:32:53 +00002049 smp_mb();
2050
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002051 /* On the first time here, set up HTAB and VRMA */
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002052 if (!vcpu->kvm->arch.hpte_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00002053 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002054 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00002055 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002056 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002057
2058 flush_fp_to_thread(current);
2059 flush_altivec_to_thread(current);
2060 flush_vsx_to_thread(current);
2061 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00002062 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002063 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10002064
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002065 do {
2066 r = kvmppc_run_vcpu(run, vcpu);
2067
2068 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
2069 !(vcpu->arch.shregs.msr & MSR_PR)) {
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002070 trace_kvm_hcall_enter(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002071 r = kvmppc_pseries_do_hcall(vcpu);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002072 trace_kvm_hcall_exit(vcpu, r);
Scott Wood7e28e60e2011-11-08 18:23:20 -06002073 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002074 } else if (r == RESUME_PAGE_FAULT) {
2075 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2076 r = kvmppc_book3s_hv_page_fault(run, vcpu,
2077 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
2078 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002079 }
Greg Kurze59d24e2014-02-06 17:36:56 +01002080 } while (is_kvmppc_resume_guest(r));
Paul Mackerras32fad282012-05-04 02:32:53 +00002081
2082 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002083 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00002084 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002085 return r;
2086}
2087
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002088static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2089 int linux_psize)
2090{
2091 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2092
2093 if (!def->shift)
2094 return;
2095 (*sps)->page_shift = def->shift;
2096 (*sps)->slb_enc = def->sllp;
2097 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00002098 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05302099 /*
2100 * Add 16MB MPSS support if host supports it
2101 */
2102 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2103 (*sps)->enc[1].page_shift = 24;
2104 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2105 }
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002106 (*sps)++;
2107}
2108
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302109static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2110 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002111{
2112 struct kvm_ppc_one_seg_page_size *sps;
2113
2114 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2115 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2116 info->flags |= KVM_PPC_1T_SEGMENTS;
2117 info->slb_size = mmu_slb_size;
2118
2119 /* We only support these sizes for now, and no muti-size segments */
2120 sps = &info->sps[0];
2121 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2122 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2123 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2124
2125 return 0;
2126}
2127
Paul Mackerras82ed3612011-12-15 02:03:22 +00002128/*
2129 * Get (and clear) the dirty memory log for a memory slot.
2130 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302131static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2132 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002133{
2134 struct kvm_memory_slot *memslot;
2135 int r;
2136 unsigned long n;
2137
2138 mutex_lock(&kvm->slots_lock);
2139
2140 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07002141 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002142 goto out;
2143
2144 memslot = id_to_memslot(kvm->memslots, log->slot);
2145 r = -ENOENT;
2146 if (!memslot->dirty_bitmap)
2147 goto out;
2148
2149 n = kvm_dirty_bitmap_bytes(memslot);
2150 memset(memslot->dirty_bitmap, 0, n);
2151
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002152 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00002153 if (r)
2154 goto out;
2155
2156 r = -EFAULT;
2157 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2158 goto out;
2159
2160 r = 0;
2161out:
2162 mutex_unlock(&kvm->slots_lock);
2163 return r;
2164}
2165
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302166static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2167 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002168{
2169 if (!dont || free->arch.rmap != dont->arch.rmap) {
2170 vfree(free->arch.rmap);
2171 free->arch.rmap = NULL;
2172 }
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002173}
2174
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302175static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
2176 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002177{
2178 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
2179 if (!slot->arch.rmap)
2180 return -ENOMEM;
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002181
2182 return 0;
2183}
2184
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302185static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
2186 struct kvm_memory_slot *memslot,
2187 struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002188{
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002189 return 0;
2190}
2191
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302192static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
2193 struct kvm_userspace_memory_region *mem,
2194 const struct kvm_memory_slot *old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002195{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002196 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
2197 struct kvm_memory_slot *memslot;
2198
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002199 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002200 /*
2201 * If modifying a memslot, reset all the rmap dirty bits.
2202 * If this is a new memslot, we don't need to do anything
2203 * since the rmap array starts out as all zeroes,
2204 * i.e. no pages are dirty.
2205 */
2206 memslot = id_to_memslot(kvm->memslots, mem->slot);
2207 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
2208 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002209}
2210
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002211/*
2212 * Update LPCR values in kvm->arch and in vcores.
2213 * Caller must hold kvm->lock.
2214 */
2215void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
2216{
2217 long int i;
2218 u32 cores_done = 0;
2219
2220 if ((kvm->arch.lpcr & mask) == lpcr)
2221 return;
2222
2223 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2224
2225 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2226 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2227 if (!vc)
2228 continue;
2229 spin_lock(&vc->lock);
2230 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2231 spin_unlock(&vc->lock);
2232 if (++cores_done >= kvm->arch.online_vcores)
2233 break;
2234 }
2235}
2236
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302237static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2238{
2239 return;
2240}
2241
Paul Mackerras32fad282012-05-04 02:32:53 +00002242static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002243{
2244 int err = 0;
2245 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002246 unsigned long hva;
2247 struct kvm_memory_slot *memslot;
2248 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002249 unsigned long lpcr = 0, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002250 unsigned long psize, porder;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002251 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002252
2253 mutex_lock(&kvm->lock);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002254 if (kvm->arch.hpte_setup_done)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002255 goto out; /* another vcpu beat us to it */
2256
Paul Mackerras32fad282012-05-04 02:32:53 +00002257 /* Allocate hashed page table (if not done already) and reset it */
2258 if (!kvm->arch.hpt_virt) {
2259 err = kvmppc_alloc_hpt(kvm, NULL);
2260 if (err) {
2261 pr_err("KVM: Couldn't alloc HPT\n");
2262 goto out;
2263 }
2264 }
2265
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002266 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002267 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002268 memslot = gfn_to_memslot(kvm, 0);
2269
2270 /* We must have some memory at 0 by now */
2271 err = -EINVAL;
2272 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002273 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002274
2275 /* Look up the VMA for the start of this memory slot */
2276 hva = memslot->userspace_addr;
2277 down_read(&current->mm->mmap_sem);
2278 vma = find_vma(current->mm, hva);
2279 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2280 goto up_out;
2281
2282 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002283 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002284
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002285 up_read(&current->mm->mmap_sem);
2286
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002287 /* We can handle 4k, 64k or 16M pages in the VRMA */
2288 err = -EINVAL;
2289 if (!(psize == 0x1000 || psize == 0x10000 ||
2290 psize == 0x1000000))
2291 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002292
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002293 /* Update VRMASD field in the LPCR */
2294 senc = slb_pgsize_encoding(psize);
2295 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2296 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2297 /* the -4 is to account for senc values starting at 0x10 */
2298 lpcr = senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002299
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002300 /* Create HPTEs in the hash page table for the VRMA */
2301 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002302
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002303 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002304
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002305 /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002306 smp_wmb();
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002307 kvm->arch.hpte_setup_done = 1;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002308 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002309 out_srcu:
2310 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002311 out:
2312 mutex_unlock(&kvm->lock);
2313 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002314
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002315 up_out:
2316 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08002317 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002318}
2319
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302320static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002321{
Paul Mackerras32fad282012-05-04 02:32:53 +00002322 unsigned long lpcr, lpid;
Paul Mackerrase23a8082015-03-28 14:21:01 +11002323 char buf[32];
Paul Mackerrasde56a942011-06-29 00:21:34 +00002324
Paul Mackerras32fad282012-05-04 02:32:53 +00002325 /* Allocate the guest's logical partition ID */
2326
2327 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08002328 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00002329 return -ENOMEM;
2330 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002331
Paul Mackerras1b400ba2012-11-21 23:28:08 +00002332 /*
2333 * Since we don't flush the TLB when tearing down a VM,
2334 * and this lpid might have previously been used,
2335 * make sure we flush on each core before running the new VM.
2336 */
2337 cpumask_setall(&kvm->arch.need_tlb_flush);
2338
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002339 /* Start out with the default set of hcalls enabled */
2340 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
2341 sizeof(kvm->arch.enabled_hcalls));
2342
Paul Mackerras9e368f22011-06-29 00:40:08 +00002343 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002344
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002345 /* Init LPCR for virtual RMA mode */
2346 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2347 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2348 lpcr &= LPCR_PECE | LPCR_LPES;
2349 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
2350 LPCR_VPM0 | LPCR_VPM1;
2351 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2352 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2353 /* On POWER8 turn on online bit to enable PURR/SPURR */
2354 if (cpu_has_feature(CPU_FTR_ARCH_207S))
2355 lpcr |= LPCR_ONL;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002356 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002357
Paul Mackerras512691d2012-10-15 01:15:41 +00002358 /*
Michael Ellerman441c19c2014-05-23 18:15:25 +10002359 * Track that we now have a HV mode VM active. This blocks secondary
2360 * CPU threads from coming online.
Paul Mackerras512691d2012-10-15 01:15:41 +00002361 */
Michael Ellerman441c19c2014-05-23 18:15:25 +10002362 kvm_hv_vm_activated();
Paul Mackerras512691d2012-10-15 01:15:41 +00002363
Paul Mackerrase23a8082015-03-28 14:21:01 +11002364 /*
2365 * Create a debugfs directory for the VM
2366 */
2367 snprintf(buf, sizeof(buf), "vm%d", current->pid);
2368 kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
2369 if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
2370 kvmppc_mmu_debugfs_init(kvm);
2371
David Gibson54738c02011-06-29 00:22:41 +00002372 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002373}
2374
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302375static void kvmppc_free_vcores(struct kvm *kvm)
2376{
2377 long int i;
2378
Stewart Smith9678cda2014-07-18 14:18:43 +10002379 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2380 if (kvm->arch.vcores[i] && kvm->arch.vcores[i]->mpp_buffer) {
2381 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2382 free_pages((unsigned long)vc->mpp_buffer,
2383 MPP_BUFFER_ORDER);
2384 }
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302385 kfree(kvm->arch.vcores[i]);
Stewart Smith9678cda2014-07-18 14:18:43 +10002386 }
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302387 kvm->arch.online_vcores = 0;
2388}
2389
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302390static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002391{
Paul Mackerrase23a8082015-03-28 14:21:01 +11002392 debugfs_remove_recursive(kvm->arch.debugfs_dir);
2393
Michael Ellerman441c19c2014-05-23 18:15:25 +10002394 kvm_hv_vm_deactivated();
Paul Mackerras512691d2012-10-15 01:15:41 +00002395
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302396 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002397
Paul Mackerrasde56a942011-06-29 00:21:34 +00002398 kvmppc_free_hpt(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002399}
2400
2401/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302402static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2403 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002404{
2405 return EMULATE_FAIL;
2406}
2407
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302408static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2409 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002410{
2411 return EMULATE_FAIL;
2412}
2413
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302414static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2415 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002416{
2417 return EMULATE_FAIL;
2418}
2419
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302420static int kvmppc_core_check_processor_compat_hv(void)
2421{
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002422 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
2423 !cpu_has_feature(CPU_FTR_ARCH_206))
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302424 return -EIO;
2425 return 0;
2426}
2427
2428static long kvm_arch_vm_ioctl_hv(struct file *filp,
2429 unsigned int ioctl, unsigned long arg)
2430{
2431 struct kvm *kvm __maybe_unused = filp->private_data;
2432 void __user *argp = (void __user *)arg;
2433 long r;
2434
2435 switch (ioctl) {
2436
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302437 case KVM_PPC_ALLOCATE_HTAB: {
2438 u32 htab_order;
2439
2440 r = -EFAULT;
2441 if (get_user(htab_order, (u32 __user *)argp))
2442 break;
2443 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2444 if (r)
2445 break;
2446 r = -EFAULT;
2447 if (put_user(htab_order, (u32 __user *)argp))
2448 break;
2449 r = 0;
2450 break;
2451 }
2452
2453 case KVM_PPC_GET_HTAB_FD: {
2454 struct kvm_get_htab_fd ghf;
2455
2456 r = -EFAULT;
2457 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2458 break;
2459 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2460 break;
2461 }
2462
2463 default:
2464 r = -ENOTTY;
2465 }
2466
2467 return r;
2468}
2469
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002470/*
2471 * List of hcall numbers to enable by default.
2472 * For compatibility with old userspace, we enable by default
2473 * all hcalls that were implemented before the hcall-enabling
2474 * facility was added. Note this list should not include H_RTAS.
2475 */
2476static unsigned int default_hcall_list[] = {
2477 H_REMOVE,
2478 H_ENTER,
2479 H_READ,
2480 H_PROTECT,
2481 H_BULK_REMOVE,
2482 H_GET_TCE,
2483 H_PUT_TCE,
2484 H_SET_DABR,
2485 H_SET_XDABR,
2486 H_CEDE,
2487 H_PROD,
2488 H_CONFER,
2489 H_REGISTER_VPA,
2490#ifdef CONFIG_KVM_XICS
2491 H_EOI,
2492 H_CPPR,
2493 H_IPI,
2494 H_IPOLL,
2495 H_XIRR,
2496 H_XIRR_X,
2497#endif
2498 0
2499};
2500
2501static void init_default_hcalls(void)
2502{
2503 int i;
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002504 unsigned int hcall;
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002505
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002506 for (i = 0; default_hcall_list[i]; ++i) {
2507 hcall = default_hcall_list[i];
2508 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
2509 __set_bit(hcall / 4, default_enabled_hcalls);
2510 }
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002511}
2512
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302513static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302514 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2515 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2516 .get_one_reg = kvmppc_get_one_reg_hv,
2517 .set_one_reg = kvmppc_set_one_reg_hv,
2518 .vcpu_load = kvmppc_core_vcpu_load_hv,
2519 .vcpu_put = kvmppc_core_vcpu_put_hv,
2520 .set_msr = kvmppc_set_msr_hv,
2521 .vcpu_run = kvmppc_vcpu_run_hv,
2522 .vcpu_create = kvmppc_core_vcpu_create_hv,
2523 .vcpu_free = kvmppc_core_vcpu_free_hv,
2524 .check_requests = kvmppc_core_check_requests_hv,
2525 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2526 .flush_memslot = kvmppc_core_flush_memslot_hv,
2527 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2528 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2529 .unmap_hva = kvm_unmap_hva_hv,
2530 .unmap_hva_range = kvm_unmap_hva_range_hv,
2531 .age_hva = kvm_age_hva_hv,
2532 .test_age_hva = kvm_test_age_hva_hv,
2533 .set_spte_hva = kvm_set_spte_hva_hv,
2534 .mmu_destroy = kvmppc_mmu_destroy_hv,
2535 .free_memslot = kvmppc_core_free_memslot_hv,
2536 .create_memslot = kvmppc_core_create_memslot_hv,
2537 .init_vm = kvmppc_core_init_vm_hv,
2538 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302539 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2540 .emulate_op = kvmppc_core_emulate_op_hv,
2541 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2542 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2543 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2544 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002545 .hcall_implemented = kvmppc_hcall_impl_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302546};
2547
2548static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002549{
2550 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302551 /*
2552 * FIXME!! Do we need to check on all cpus ?
2553 */
2554 r = kvmppc_core_check_processor_compat_hv();
2555 if (r < 0)
Paul Mackerras739e2422014-03-25 10:47:05 +11002556 return -ENODEV;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002557
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302558 kvm_ops_hv.owner = THIS_MODULE;
2559 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002560
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002561 init_default_hcalls();
2562
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302563 r = kvmppc_mmu_hv_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002564 return r;
2565}
2566
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302567static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002568{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302569 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002570}
2571
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302572module_init(kvmppc_book3s_init_hv);
2573module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05302574MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01002575MODULE_ALIAS_MISCDEV(KVM_MINOR);
2576MODULE_ALIAS("devname:kvm");