blob: 1ee4e9e786ccca32d19f8f5b8a2413f31ffe2718 [file] [log] [blame]
Paul Mackerrasde56a942011-06-29 00:21:34 +00001/*
2 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
3 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
4 *
5 * Authors:
6 * Paul Mackerras <paulus@au1.ibm.com>
7 * Alexander Graf <agraf@suse.de>
8 * Kevin Wolf <mail@kevin-wolf.de>
9 *
10 * Description: KVM functions specific to running on Book 3S
11 * processors in hypervisor mode (specifically POWER7 and later).
12 *
13 * This file is derived from arch/powerpc/kvm/book3s.c,
14 * by Alexander Graf <agraf@suse.de>.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License, version 2, as
18 * published by the Free Software Foundation.
19 */
20
21#include <linux/kvm_host.h>
22#include <linux/err.h>
23#include <linux/slab.h>
24#include <linux/preempt.h>
25#include <linux/sched.h>
26#include <linux/delay.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040027#include <linux/export.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000028#include <linux/fs.h>
29#include <linux/anon_inodes.h>
30#include <linux/cpumask.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000031#include <linux/spinlock.h>
32#include <linux/page-flags.h>
Paul Mackerras2c9097e2012-09-11 13:27:01 +000033#include <linux/srcu.h>
Alexander Graf398a76c2013-12-09 13:53:42 +010034#include <linux/miscdevice.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000035
36#include <asm/reg.h>
37#include <asm/cputable.h>
Stewart Smith9678cda2014-07-18 14:18:43 +100038#include <asm/cache.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000039#include <asm/cacheflush.h>
40#include <asm/tlbflush.h>
41#include <asm/uaccess.h>
42#include <asm/io.h>
43#include <asm/kvm_ppc.h>
44#include <asm/kvm_book3s.h>
45#include <asm/mmu_context.h>
46#include <asm/lppaca.h>
47#include <asm/processor.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000048#include <asm/cputhreads.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000049#include <asm/page.h>
Michael Neulingde1d9242011-11-09 20:39:49 +000050#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010051#include <asm/switch_to.h>
Paul Mackerras512691d2012-10-15 01:15:41 +000052#include <asm/smp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000053#include <linux/gfp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000054#include <linux/vmalloc.h>
55#include <linux/highmem.h>
Paul Mackerrasc77162d2011-12-12 12:31:00 +000056#include <linux/hugetlb.h>
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +053057#include <linux/module.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000058
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053059#include "book3s.h"
60
Suresh E. Warrier3c78f782014-12-03 18:48:10 -060061#define CREATE_TRACE_POINTS
62#include "trace_hv.h"
63
Paul Mackerrasde56a942011-06-29 00:21:34 +000064/* #define EXIT_DEBUG */
65/* #define EXIT_DEBUG_SIMPLE */
66/* #define EXIT_DEBUG_INT */
67
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000068/* Used to indicate that a guest page fault needs to be handled */
69#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
70
Paul Mackerrasc7b67672012-10-15 01:18:07 +000071/* Used as a "null" value for timebase values */
72#define TB_NIL (~(u64)0)
73
Paul Mackerras699a0ea2014-06-02 11:02:59 +100074static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
75
Stewart Smith9678cda2014-07-18 14:18:43 +100076#if defined(CONFIG_PPC_64K_PAGES)
77#define MPP_BUFFER_ORDER 0
78#elif defined(CONFIG_PPC_4K_PAGES)
79#define MPP_BUFFER_ORDER 3
80#endif
81
82
Paul Mackerras19ccb762011-07-23 17:42:46 +100083static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +000084static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +100085
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053086static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +000087{
88 int me;
89 int cpu = vcpu->cpu;
90 wait_queue_head_t *wqp;
91
92 wqp = kvm_arch_vcpu_wq(vcpu);
93 if (waitqueue_active(wqp)) {
94 wake_up_interruptible(wqp);
95 ++vcpu->stat.halt_wakeup;
96 }
97
98 me = get_cpu();
99
100 /* CPU points to the first thread of the core */
101 if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
Anton Blanchard75052582014-03-25 10:47:01 +1100102#ifdef CONFIG_PPC_ICP_NATIVE
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000103 int real_cpu = cpu + vcpu->arch.ptid;
104 if (paca[real_cpu].kvm_hstate.xics_phys)
105 xics_wake_cpu(real_cpu);
Andreas Schwab48eaef02013-12-30 15:36:56 +0100106 else
107#endif
108 if (cpu_online(cpu))
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000109 smp_send_reschedule(cpu);
110 }
111 put_cpu();
112}
113
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000114/*
115 * We use the vcpu_load/put functions to measure stolen time.
116 * Stolen time is counted as time when either the vcpu is able to
117 * run as part of a virtual core, but the task running the vcore
118 * is preempted or sleeping, or when the vcpu needs something done
119 * in the kernel by the task running the vcpu, but that task is
120 * preempted or sleeping. Those two things have to be counted
121 * separately, since one of the vcpu tasks will take on the job
122 * of running the core, and the other vcpu tasks in the vcore will
123 * sleep waiting for it to do that, but that sleep shouldn't count
124 * as stolen time.
125 *
126 * Hence we accumulate stolen time when the vcpu can run as part of
127 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
128 * needs its task to do other things in the kernel (for example,
129 * service a page fault) in busy_stolen. We don't accumulate
130 * stolen time for a vcore when it is inactive, or for a vcpu
131 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
132 * a misnomer; it means that the vcpu task is not executing in
133 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
134 * the kernel. We don't have any way of dividing up that time
135 * between time that the vcpu is genuinely stopped, time that
136 * the task is actively working on behalf of the vcpu, and time
137 * that the task is preempted, so we don't count any of it as
138 * stolen.
139 *
140 * Updates to busy_stolen are protected by arch.tbacct_lock;
Paul Mackerras2711e242014-12-04 16:43:28 +1100141 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
142 * lock. The stolen times are measured in units of timebase ticks.
143 * (Note that the != TB_NIL checks below are purely defensive;
144 * they should never fail.)
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000145 */
146
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530147static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000148{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000149 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100150 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000151
Paul Mackerras2711e242014-12-04 16:43:28 +1100152 /*
153 * We can test vc->runner without taking the vcore lock,
154 * because only this task ever sets vc->runner to this
155 * vcpu, and once it is set to this vcpu, only this task
156 * ever sets it to NULL.
157 */
158 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE) {
159 spin_lock_irqsave(&vc->stoltb_lock, flags);
160 if (vc->preempt_tb != TB_NIL) {
161 vc->stolen_tb += mftb() - vc->preempt_tb;
162 vc->preempt_tb = TB_NIL;
163 }
164 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000165 }
Paul Mackerras2711e242014-12-04 16:43:28 +1100166 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000167 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
168 vcpu->arch.busy_preempt != TB_NIL) {
169 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
170 vcpu->arch.busy_preempt = TB_NIL;
171 }
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100172 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000173}
174
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530175static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000176{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000177 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100178 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000179
Paul Mackerras2711e242014-12-04 16:43:28 +1100180 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE) {
181 spin_lock_irqsave(&vc->stoltb_lock, flags);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000182 vc->preempt_tb = mftb();
Paul Mackerras2711e242014-12-04 16:43:28 +1100183 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
184 }
185 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000186 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
187 vcpu->arch.busy_preempt = mftb();
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100188 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000189}
190
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530191static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000192{
193 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000194 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000195}
196
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530197void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000198{
199 vcpu->arch.pvr = pvr;
200}
201
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000202int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
203{
204 unsigned long pcr = 0;
205 struct kvmppc_vcore *vc = vcpu->arch.vcore;
206
207 if (arch_compat) {
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000208 switch (arch_compat) {
209 case PVR_ARCH_205:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100210 /*
211 * If an arch bit is set in PCR, all the defined
212 * higher-order arch bits also have to be set.
213 */
214 pcr = PCR_ARCH_206 | PCR_ARCH_205;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000215 break;
216 case PVR_ARCH_206:
217 case PVR_ARCH_206p:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100218 pcr = PCR_ARCH_206;
219 break;
220 case PVR_ARCH_207:
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000221 break;
222 default:
223 return -EINVAL;
224 }
Paul Mackerras5557ae02014-01-08 21:25:24 +1100225
226 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
227 /* POWER7 can't emulate POWER8 */
228 if (!(pcr & PCR_ARCH_206))
229 return -EINVAL;
230 pcr &= ~PCR_ARCH_206;
231 }
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000232 }
233
234 spin_lock(&vc->lock);
235 vc->arch_compat = arch_compat;
236 vc->pcr = pcr;
237 spin_unlock(&vc->lock);
238
239 return 0;
240}
241
Paul Mackerrasde56a942011-06-29 00:21:34 +0000242void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
243{
244 int r;
245
246 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
247 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
248 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
249 for (r = 0; r < 16; ++r)
250 pr_err("r%2d = %.16lx r%d = %.16lx\n",
251 r, kvmppc_get_gpr(vcpu, r),
252 r+16, kvmppc_get_gpr(vcpu, r+16));
253 pr_err("ctr = %.16lx lr = %.16lx\n",
254 vcpu->arch.ctr, vcpu->arch.lr);
255 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
256 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
257 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
258 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
259 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
260 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
261 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
262 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
263 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
264 pr_err("fault dar = %.16lx dsisr = %.8x\n",
265 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
266 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
267 for (r = 0; r < vcpu->arch.slb_max; ++r)
268 pr_err(" ESID = %.16llx VSID = %.16llx\n",
269 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
270 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000271 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000272 vcpu->arch.last_inst);
273}
274
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000275struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
276{
277 int r;
278 struct kvm_vcpu *v, *ret = NULL;
279
280 mutex_lock(&kvm->lock);
281 kvm_for_each_vcpu(r, v, kvm) {
282 if (v->vcpu_id == id) {
283 ret = v;
284 break;
285 }
286 }
287 mutex_unlock(&kvm->lock);
288 return ret;
289}
290
291static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
292{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000293 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Alexander Graf02407552014-06-11 10:34:19 +0200294 vpa->yield_count = cpu_to_be32(1);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000295}
296
Paul Mackerras55b665b2012-09-25 20:33:06 +0000297static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
298 unsigned long addr, unsigned long len)
299{
300 /* check address is cacheline aligned */
301 if (addr & (L1_CACHE_BYTES - 1))
302 return -EINVAL;
303 spin_lock(&vcpu->arch.vpa_update_lock);
304 if (v->next_gpa != addr || v->len != len) {
305 v->next_gpa = addr;
306 v->len = addr ? len : 0;
307 v->update_pending = 1;
308 }
309 spin_unlock(&vcpu->arch.vpa_update_lock);
310 return 0;
311}
312
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000313/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
314struct reg_vpa {
315 u32 dummy;
316 union {
Alexander Graf02407552014-06-11 10:34:19 +0200317 __be16 hword;
318 __be32 word;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000319 } length;
320};
321
322static int vpa_is_registered(struct kvmppc_vpa *vpap)
323{
324 if (vpap->update_pending)
325 return vpap->next_gpa != 0;
326 return vpap->pinned_addr != NULL;
327}
328
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000329static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
330 unsigned long flags,
331 unsigned long vcpuid, unsigned long vpa)
332{
333 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000334 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000335 void *va;
336 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000337 int err;
338 int subfunc;
339 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000340
341 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
342 if (!tvcpu)
343 return H_PARAMETER;
344
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000345 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
346 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
347 subfunc == H_VPA_REG_SLB) {
348 /* Registering new area - address must be cache-line aligned */
349 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000350 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000351
352 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000353 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
354 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000355 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000356 if (subfunc == H_VPA_REG_VPA)
Alexander Graf02407552014-06-11 10:34:19 +0200357 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000358 else
Alexander Graf02407552014-06-11 10:34:19 +0200359 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000360 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000361
362 /* Check length */
363 if (len > nb || len < sizeof(struct reg_vpa))
364 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000365 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000366 vpa = 0;
367 len = 0;
368 }
369
370 err = H_PARAMETER;
371 vpap = NULL;
372 spin_lock(&tvcpu->arch.vpa_update_lock);
373
374 switch (subfunc) {
375 case H_VPA_REG_VPA: /* register VPA */
376 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000377 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000378 vpap = &tvcpu->arch.vpa;
379 err = 0;
380 break;
381
382 case H_VPA_REG_DTL: /* register DTL */
383 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000384 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000385 len -= len % sizeof(struct dtl_entry);
386
387 /* Check that they have previously registered a VPA */
388 err = H_RESOURCE;
389 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000390 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000391
392 vpap = &tvcpu->arch.dtl;
393 err = 0;
394 break;
395
396 case H_VPA_REG_SLB: /* register SLB shadow buffer */
397 /* Check that they have previously registered a VPA */
398 err = H_RESOURCE;
399 if (!vpa_is_registered(&tvcpu->arch.vpa))
400 break;
401
402 vpap = &tvcpu->arch.slb_shadow;
403 err = 0;
404 break;
405
406 case H_VPA_DEREG_VPA: /* deregister VPA */
407 /* Check they don't still have a DTL or SLB buf registered */
408 err = H_RESOURCE;
409 if (vpa_is_registered(&tvcpu->arch.dtl) ||
410 vpa_is_registered(&tvcpu->arch.slb_shadow))
411 break;
412
413 vpap = &tvcpu->arch.vpa;
414 err = 0;
415 break;
416
417 case H_VPA_DEREG_DTL: /* deregister DTL */
418 vpap = &tvcpu->arch.dtl;
419 err = 0;
420 break;
421
422 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
423 vpap = &tvcpu->arch.slb_shadow;
424 err = 0;
425 break;
426 }
427
428 if (vpap) {
429 vpap->next_gpa = vpa;
430 vpap->len = len;
431 vpap->update_pending = 1;
432 }
433
434 spin_unlock(&tvcpu->arch.vpa_update_lock);
435
436 return err;
437}
438
Paul Mackerras081f3232012-06-01 20:20:24 +1000439static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000440{
Paul Mackerras081f3232012-06-01 20:20:24 +1000441 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000442 void *va;
443 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000444 unsigned long gpa;
445
446 /*
447 * We need to pin the page pointed to by vpap->next_gpa,
448 * but we can't call kvmppc_pin_guest_page under the lock
449 * as it does get_user_pages() and down_read(). So we
450 * have to drop the lock, pin the page, then get the lock
451 * again and check that a new area didn't get registered
452 * in the meantime.
453 */
454 for (;;) {
455 gpa = vpap->next_gpa;
456 spin_unlock(&vcpu->arch.vpa_update_lock);
457 va = NULL;
458 nb = 0;
459 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000460 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000461 spin_lock(&vcpu->arch.vpa_update_lock);
462 if (gpa == vpap->next_gpa)
463 break;
464 /* sigh... unpin that one and try again */
465 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000466 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000467 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000468
469 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000470 if (va && nb < vpap->len) {
471 /*
472 * If it's now too short, it must be that userspace
473 * has changed the mappings underlying guest memory,
474 * so unregister the region.
475 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000476 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000477 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000478 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000479 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000480 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
481 vpap->dirty);
482 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000483 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000484 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000485 if (va)
486 vpap->pinned_end = va + vpap->len;
487}
Paul Mackerras93e60242011-12-12 12:28:55 +0000488
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000489static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
490{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000491 if (!(vcpu->arch.vpa.update_pending ||
492 vcpu->arch.slb_shadow.update_pending ||
493 vcpu->arch.dtl.update_pending))
494 return;
495
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000496 spin_lock(&vcpu->arch.vpa_update_lock);
497 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000498 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000499 if (vcpu->arch.vpa.pinned_addr)
500 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000501 }
502 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000503 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000504 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
505 vcpu->arch.dtl_index = 0;
506 }
507 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000508 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000509 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000510}
511
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000512/*
513 * Return the accumulated stolen time for the vcore up until `now'.
514 * The caller should hold the vcore lock.
515 */
516static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
517{
518 u64 p;
Paul Mackerras2711e242014-12-04 16:43:28 +1100519 unsigned long flags;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000520
Paul Mackerras2711e242014-12-04 16:43:28 +1100521 spin_lock_irqsave(&vc->stoltb_lock, flags);
522 p = vc->stolen_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000523 if (vc->vcore_state != VCORE_INACTIVE &&
Paul Mackerras2711e242014-12-04 16:43:28 +1100524 vc->preempt_tb != TB_NIL)
525 p += now - vc->preempt_tb;
526 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000527 return p;
528}
529
Paul Mackerras0456ec42012-02-03 00:56:21 +0000530static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
531 struct kvmppc_vcore *vc)
532{
533 struct dtl_entry *dt;
534 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000535 unsigned long stolen;
536 unsigned long core_stolen;
537 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000538
539 dt = vcpu->arch.dtl_ptr;
540 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000541 now = mftb();
542 core_stolen = vcore_stolen_time(vc, now);
543 stolen = core_stolen - vcpu->arch.stolen_logged;
544 vcpu->arch.stolen_logged = core_stolen;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100545 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000546 stolen += vcpu->arch.busy_stolen;
547 vcpu->arch.busy_stolen = 0;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100548 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000549 if (!dt || !vpa)
550 return;
551 memset(dt, 0, sizeof(struct dtl_entry));
552 dt->dispatch_reason = 7;
Alexander Graf02407552014-06-11 10:34:19 +0200553 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
554 dt->timebase = cpu_to_be64(now + vc->tb_offset);
555 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
556 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
557 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000558 ++dt;
559 if (dt == vcpu->arch.dtl.pinned_end)
560 dt = vcpu->arch.dtl.pinned_addr;
561 vcpu->arch.dtl_ptr = dt;
562 /* order writing *dt vs. writing vpa->dtl_idx */
563 smp_wmb();
Alexander Graf02407552014-06-11 10:34:19 +0200564 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000565 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000566}
567
Michael Neuling96423822014-06-02 11:03:01 +1000568static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
569{
570 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
571 return true;
572 if ((!vcpu->arch.vcore->arch_compat) &&
573 cpu_has_feature(CPU_FTR_ARCH_207S))
574 return true;
575 return false;
576}
577
578static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
579 unsigned long resource, unsigned long value1,
580 unsigned long value2)
581{
582 switch (resource) {
583 case H_SET_MODE_RESOURCE_SET_CIABR:
584 if (!kvmppc_power8_compatible(vcpu))
585 return H_P2;
586 if (value2)
587 return H_P4;
588 if (mflags)
589 return H_UNSUPPORTED_FLAG_START;
590 /* Guests can't breakpoint the hypervisor */
591 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
592 return H_P3;
593 vcpu->arch.ciabr = value1;
594 return H_SUCCESS;
595 case H_SET_MODE_RESOURCE_SET_DAWR:
596 if (!kvmppc_power8_compatible(vcpu))
597 return H_P2;
598 if (mflags)
599 return H_UNSUPPORTED_FLAG_START;
600 if (value2 & DABRX_HYP)
601 return H_P4;
602 vcpu->arch.dawr = value1;
603 vcpu->arch.dawrx = value2;
604 return H_SUCCESS;
605 default:
606 return H_TOO_HARD;
607 }
608}
609
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000610int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
611{
612 unsigned long req = kvmppc_get_gpr(vcpu, 3);
613 unsigned long target, ret = H_SUCCESS;
614 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000615 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000616
Paul Mackerras699a0ea2014-06-02 11:02:59 +1000617 if (req <= MAX_HCALL_OPCODE &&
618 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
619 return RESUME_HOST;
620
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000621 switch (req) {
622 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000623 break;
624 case H_PROD:
625 target = kvmppc_get_gpr(vcpu, 4);
626 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
627 if (!tvcpu) {
628 ret = H_PARAMETER;
629 break;
630 }
631 tvcpu->arch.prodded = 1;
632 smp_mb();
633 if (vcpu->arch.ceded) {
634 if (waitqueue_active(&vcpu->wq)) {
635 wake_up_interruptible(&vcpu->wq);
636 vcpu->stat.halt_wakeup++;
637 }
638 }
639 break;
640 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +1000641 target = kvmppc_get_gpr(vcpu, 4);
642 if (target == -1)
643 break;
644 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
645 if (!tvcpu) {
646 ret = H_PARAMETER;
647 break;
648 }
649 kvm_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000650 break;
651 case H_REGISTER_VPA:
652 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
653 kvmppc_get_gpr(vcpu, 5),
654 kvmppc_get_gpr(vcpu, 6));
655 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000656 case H_RTAS:
657 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
658 return RESUME_HOST;
659
Paul Mackerrasc9438092013-11-16 17:46:05 +1100660 idx = srcu_read_lock(&vcpu->kvm->srcu);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000661 rc = kvmppc_rtas_hcall(vcpu);
Paul Mackerrasc9438092013-11-16 17:46:05 +1100662 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000663
664 if (rc == -ENOENT)
665 return RESUME_HOST;
666 else if (rc == 0)
667 break;
668
669 /* Send the error out to userspace via KVM_RUN */
670 return rc;
Michael Neuling96423822014-06-02 11:03:01 +1000671 case H_SET_MODE:
672 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
673 kvmppc_get_gpr(vcpu, 5),
674 kvmppc_get_gpr(vcpu, 6),
675 kvmppc_get_gpr(vcpu, 7));
676 if (ret == H_TOO_HARD)
677 return RESUME_HOST;
678 break;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000679 case H_XIRR:
680 case H_CPPR:
681 case H_EOI:
682 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000683 case H_IPOLL:
684 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000685 if (kvmppc_xics_enabled(vcpu)) {
686 ret = kvmppc_xics_hcall(vcpu, req);
687 break;
688 } /* fallthrough */
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000689 default:
690 return RESUME_HOST;
691 }
692 kvmppc_set_gpr(vcpu, 3, ret);
693 vcpu->arch.hcall_needed = 0;
694 return RESUME_GUEST;
695}
696
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000697static int kvmppc_hcall_impl_hv(unsigned long cmd)
698{
699 switch (cmd) {
700 case H_CEDE:
701 case H_PROD:
702 case H_CONFER:
703 case H_REGISTER_VPA:
Michael Neuling96423822014-06-02 11:03:01 +1000704 case H_SET_MODE:
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000705#ifdef CONFIG_KVM_XICS
706 case H_XIRR:
707 case H_CPPR:
708 case H_EOI:
709 case H_IPI:
710 case H_IPOLL:
711 case H_XIRR_X:
712#endif
713 return 1;
714 }
715
716 /* See if it's in the real-mode table */
717 return kvmppc_hcall_impl_hv_realmode(cmd);
718}
719
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530720static int kvmppc_emulate_debug_inst(struct kvm_run *run,
721 struct kvm_vcpu *vcpu)
722{
723 u32 last_inst;
724
725 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
726 EMULATE_DONE) {
727 /*
728 * Fetch failed, so return to guest and
729 * try executing it again.
730 */
731 return RESUME_GUEST;
732 }
733
734 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
735 run->exit_reason = KVM_EXIT_DEBUG;
736 run->debug.arch.address = kvmppc_get_pc(vcpu);
737 return RESUME_HOST;
738 } else {
739 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
740 return RESUME_GUEST;
741 }
742}
743
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530744static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
745 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000746{
747 int r = RESUME_HOST;
748
749 vcpu->stat.sum_exits++;
750
751 run->exit_reason = KVM_EXIT_UNKNOWN;
752 run->ready_for_interrupt_injection = 1;
753 switch (vcpu->arch.trap) {
754 /* We're good on these - the host merely wanted to get our attention */
755 case BOOK3S_INTERRUPT_HV_DECREMENTER:
756 vcpu->stat.dec_exits++;
757 r = RESUME_GUEST;
758 break;
759 case BOOK3S_INTERRUPT_EXTERNAL:
Paul Mackerras5d00f662014-01-08 21:25:28 +1100760 case BOOK3S_INTERRUPT_H_DOORBELL:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000761 vcpu->stat.ext_intr_exits++;
762 r = RESUME_GUEST;
763 break;
Mahesh Salgaonkardee6f242014-11-03 15:51:57 +1100764 /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
765 case BOOK3S_INTERRUPT_HMI:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000766 case BOOK3S_INTERRUPT_PERFMON:
767 r = RESUME_GUEST;
768 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000769 case BOOK3S_INTERRUPT_MACHINE_CHECK:
770 /*
771 * Deliver a machine check interrupt to the guest.
772 * We have to do this, even if the host has handled the
773 * machine check, because machine checks use SRR0/1 and
774 * the interrupt might have trashed guest state in them.
775 */
776 kvmppc_book3s_queue_irqprio(vcpu,
777 BOOK3S_INTERRUPT_MACHINE_CHECK);
778 r = RESUME_GUEST;
779 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000780 case BOOK3S_INTERRUPT_PROGRAM:
781 {
782 ulong flags;
783 /*
784 * Normally program interrupts are delivered directly
785 * to the guest by the hardware, but we can get here
786 * as a result of a hypervisor emulation interrupt
787 * (e40) getting turned into a 700 by BML RTAS.
788 */
789 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
790 kvmppc_core_queue_program(vcpu, flags);
791 r = RESUME_GUEST;
792 break;
793 }
794 case BOOK3S_INTERRUPT_SYSCALL:
795 {
796 /* hcall - punt to userspace */
797 int i;
798
Liu Ping Fan27025a62013-11-19 14:12:48 +0800799 /* hypercall with MSR_PR has already been handled in rmode,
800 * and never reaches here.
801 */
802
Paul Mackerrasde56a942011-06-29 00:21:34 +0000803 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
804 for (i = 0; i < 9; ++i)
805 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
806 run->exit_reason = KVM_EXIT_PAPR_HCALL;
807 vcpu->arch.hcall_needed = 1;
808 r = RESUME_HOST;
809 break;
810 }
811 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000812 * We get these next two if the guest accesses a page which it thinks
813 * it has mapped but which is not actually present, either because
814 * it is for an emulated I/O device or because the corresonding
815 * host page has been paged out. Any other HDSI/HISI interrupts
816 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000817 */
818 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000819 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000820 break;
821 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000822 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
823 vcpu->arch.fault_dsisr = 0;
824 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000825 break;
826 /*
827 * This occurs if the guest executes an illegal instruction.
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530828 * If the guest debug is disabled, generate a program interrupt
829 * to the guest. If guest debug is enabled, we need to check
830 * whether the instruction is a software breakpoint instruction.
831 * Accordingly return to Guest or Host.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000832 */
833 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530834 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
835 r = kvmppc_emulate_debug_inst(run, vcpu);
836 } else {
837 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
838 r = RESUME_GUEST;
839 }
Michael Ellermanbd3048b2014-01-08 21:25:23 +1100840 break;
841 /*
842 * This occurs if the guest (kernel or userspace), does something that
843 * is prohibited by HFSCR. We just generate a program interrupt to
844 * the guest.
845 */
846 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
847 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000848 r = RESUME_GUEST;
849 break;
850 default:
851 kvmppc_dump_regs(vcpu);
852 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
853 vcpu->arch.trap, kvmppc_get_pc(vcpu),
854 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +1000855 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000856 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000857 break;
858 }
859
Paul Mackerrasde56a942011-06-29 00:21:34 +0000860 return r;
861}
862
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530863static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
864 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000865{
866 int i;
867
Paul Mackerrasde56a942011-06-29 00:21:34 +0000868 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +0530869 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000870 for (i = 0; i < vcpu->arch.slb_max; i++) {
871 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
872 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
873 }
874
875 return 0;
876}
877
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530878static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
879 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000880{
881 int i, j;
882
Paul Mackerras9333e6c2014-09-02 16:14:43 +1000883 /* Only accept the same PVR as the host's, since we can't spoof it */
884 if (sregs->pvr != vcpu->arch.pvr)
885 return -EINVAL;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000886
887 j = 0;
888 for (i = 0; i < vcpu->arch.slb_nr; i++) {
889 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
890 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
891 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
892 ++j;
893 }
894 }
895 vcpu->arch.slb_max = j;
896
897 return 0;
898}
899
Alexey Kardashevskiya0840242014-07-19 17:59:34 +1000900static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
901 bool preserve_top32)
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000902{
903 struct kvmppc_vcore *vc = vcpu->arch.vcore;
904 u64 mask;
905
906 spin_lock(&vc->lock);
907 /*
Anton Blanchardd6829162014-01-08 21:25:30 +1100908 * If ILE (interrupt little-endian) has changed, update the
909 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
910 */
911 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
912 struct kvm *kvm = vcpu->kvm;
913 struct kvm_vcpu *vcpu;
914 int i;
915
916 mutex_lock(&kvm->lock);
917 kvm_for_each_vcpu(i, vcpu, kvm) {
918 if (vcpu->arch.vcore != vc)
919 continue;
920 if (new_lpcr & LPCR_ILE)
921 vcpu->arch.intr_msr |= MSR_LE;
922 else
923 vcpu->arch.intr_msr &= ~MSR_LE;
924 }
925 mutex_unlock(&kvm->lock);
926 }
927
928 /*
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000929 * Userspace can only modify DPFD (default prefetch depth),
930 * ILE (interrupt little-endian) and TC (translation control).
Paul Mackerrase0622bd2014-01-08 21:25:27 +1100931 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000932 */
933 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
Paul Mackerrase0622bd2014-01-08 21:25:27 +1100934 if (cpu_has_feature(CPU_FTR_ARCH_207S))
935 mask |= LPCR_AIL;
Alexey Kardashevskiya0840242014-07-19 17:59:34 +1000936
937 /* Broken 32-bit version of LPCR must not clear top bits */
938 if (preserve_top32)
939 mask &= 0xFFFFFFFF;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000940 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
941 spin_unlock(&vc->lock);
942}
943
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530944static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
945 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000946{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000947 int r = 0;
948 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +0000949
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000950 switch (id) {
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530951 case KVM_REG_PPC_DEBUG_INST:
952 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
953 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000954 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000955 *val = get_reg_val(id, 0);
956 break;
957 case KVM_REG_PPC_DABR:
958 *val = get_reg_val(id, vcpu->arch.dabr);
959 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +1100960 case KVM_REG_PPC_DABRX:
961 *val = get_reg_val(id, vcpu->arch.dabrx);
962 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000963 case KVM_REG_PPC_DSCR:
964 *val = get_reg_val(id, vcpu->arch.dscr);
965 break;
966 case KVM_REG_PPC_PURR:
967 *val = get_reg_val(id, vcpu->arch.purr);
968 break;
969 case KVM_REG_PPC_SPURR:
970 *val = get_reg_val(id, vcpu->arch.spurr);
971 break;
972 case KVM_REG_PPC_AMR:
973 *val = get_reg_val(id, vcpu->arch.amr);
974 break;
975 case KVM_REG_PPC_UAMOR:
976 *val = get_reg_val(id, vcpu->arch.uamor);
977 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100978 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000979 i = id - KVM_REG_PPC_MMCR0;
980 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
981 break;
982 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
983 i = id - KVM_REG_PPC_PMC1;
984 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +0000985 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100986 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
987 i = id - KVM_REG_PPC_SPMC1;
988 *val = get_reg_val(id, vcpu->arch.spmc[i]);
989 break;
Paul Mackerras14941782013-09-06 13:11:18 +1000990 case KVM_REG_PPC_SIAR:
991 *val = get_reg_val(id, vcpu->arch.siar);
992 break;
993 case KVM_REG_PPC_SDAR:
994 *val = get_reg_val(id, vcpu->arch.sdar);
995 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100996 case KVM_REG_PPC_SIER:
997 *val = get_reg_val(id, vcpu->arch.sier);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000998 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100999 case KVM_REG_PPC_IAMR:
1000 *val = get_reg_val(id, vcpu->arch.iamr);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001001 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001002 case KVM_REG_PPC_PSPB:
1003 *val = get_reg_val(id, vcpu->arch.pspb);
1004 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001005 case KVM_REG_PPC_DPDES:
1006 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1007 break;
1008 case KVM_REG_PPC_DAWR:
1009 *val = get_reg_val(id, vcpu->arch.dawr);
1010 break;
1011 case KVM_REG_PPC_DAWRX:
1012 *val = get_reg_val(id, vcpu->arch.dawrx);
1013 break;
1014 case KVM_REG_PPC_CIABR:
1015 *val = get_reg_val(id, vcpu->arch.ciabr);
1016 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001017 case KVM_REG_PPC_CSIGR:
1018 *val = get_reg_val(id, vcpu->arch.csigr);
1019 break;
1020 case KVM_REG_PPC_TACR:
1021 *val = get_reg_val(id, vcpu->arch.tacr);
1022 break;
1023 case KVM_REG_PPC_TCSCR:
1024 *val = get_reg_val(id, vcpu->arch.tcscr);
1025 break;
1026 case KVM_REG_PPC_PID:
1027 *val = get_reg_val(id, vcpu->arch.pid);
1028 break;
1029 case KVM_REG_PPC_ACOP:
1030 *val = get_reg_val(id, vcpu->arch.acop);
1031 break;
1032 case KVM_REG_PPC_WORT:
1033 *val = get_reg_val(id, vcpu->arch.wort);
1034 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001035 case KVM_REG_PPC_VPA_ADDR:
1036 spin_lock(&vcpu->arch.vpa_update_lock);
1037 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1038 spin_unlock(&vcpu->arch.vpa_update_lock);
1039 break;
1040 case KVM_REG_PPC_VPA_SLB:
1041 spin_lock(&vcpu->arch.vpa_update_lock);
1042 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1043 val->vpaval.length = vcpu->arch.slb_shadow.len;
1044 spin_unlock(&vcpu->arch.vpa_update_lock);
1045 break;
1046 case KVM_REG_PPC_VPA_DTL:
1047 spin_lock(&vcpu->arch.vpa_update_lock);
1048 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1049 val->vpaval.length = vcpu->arch.dtl.len;
1050 spin_unlock(&vcpu->arch.vpa_update_lock);
1051 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001052 case KVM_REG_PPC_TB_OFFSET:
1053 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1054 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001055 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001056 case KVM_REG_PPC_LPCR_64:
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001057 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1058 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001059 case KVM_REG_PPC_PPR:
1060 *val = get_reg_val(id, vcpu->arch.ppr);
1061 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001062#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1063 case KVM_REG_PPC_TFHAR:
1064 *val = get_reg_val(id, vcpu->arch.tfhar);
1065 break;
1066 case KVM_REG_PPC_TFIAR:
1067 *val = get_reg_val(id, vcpu->arch.tfiar);
1068 break;
1069 case KVM_REG_PPC_TEXASR:
1070 *val = get_reg_val(id, vcpu->arch.texasr);
1071 break;
1072 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1073 i = id - KVM_REG_PPC_TM_GPR0;
1074 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1075 break;
1076 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1077 {
1078 int j;
1079 i = id - KVM_REG_PPC_TM_VSR0;
1080 if (i < 32)
1081 for (j = 0; j < TS_FPRWIDTH; j++)
1082 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1083 else {
1084 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1085 val->vval = vcpu->arch.vr_tm.vr[i-32];
1086 else
1087 r = -ENXIO;
1088 }
1089 break;
1090 }
1091 case KVM_REG_PPC_TM_CR:
1092 *val = get_reg_val(id, vcpu->arch.cr_tm);
1093 break;
1094 case KVM_REG_PPC_TM_LR:
1095 *val = get_reg_val(id, vcpu->arch.lr_tm);
1096 break;
1097 case KVM_REG_PPC_TM_CTR:
1098 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1099 break;
1100 case KVM_REG_PPC_TM_FPSCR:
1101 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1102 break;
1103 case KVM_REG_PPC_TM_AMR:
1104 *val = get_reg_val(id, vcpu->arch.amr_tm);
1105 break;
1106 case KVM_REG_PPC_TM_PPR:
1107 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1108 break;
1109 case KVM_REG_PPC_TM_VRSAVE:
1110 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1111 break;
1112 case KVM_REG_PPC_TM_VSCR:
1113 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1114 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1115 else
1116 r = -ENXIO;
1117 break;
1118 case KVM_REG_PPC_TM_DSCR:
1119 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1120 break;
1121 case KVM_REG_PPC_TM_TAR:
1122 *val = get_reg_val(id, vcpu->arch.tar_tm);
1123 break;
1124#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001125 case KVM_REG_PPC_ARCH_COMPAT:
1126 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1127 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001128 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001129 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001130 break;
1131 }
1132
1133 return r;
1134}
1135
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301136static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1137 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001138{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001139 int r = 0;
1140 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001141 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +00001142
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001143 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00001144 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +00001145 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001146 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +00001147 r = -EINVAL;
1148 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001149 case KVM_REG_PPC_DABR:
1150 vcpu->arch.dabr = set_reg_val(id, *val);
1151 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001152 case KVM_REG_PPC_DABRX:
1153 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1154 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001155 case KVM_REG_PPC_DSCR:
1156 vcpu->arch.dscr = set_reg_val(id, *val);
1157 break;
1158 case KVM_REG_PPC_PURR:
1159 vcpu->arch.purr = set_reg_val(id, *val);
1160 break;
1161 case KVM_REG_PPC_SPURR:
1162 vcpu->arch.spurr = set_reg_val(id, *val);
1163 break;
1164 case KVM_REG_PPC_AMR:
1165 vcpu->arch.amr = set_reg_val(id, *val);
1166 break;
1167 case KVM_REG_PPC_UAMOR:
1168 vcpu->arch.uamor = set_reg_val(id, *val);
1169 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001170 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001171 i = id - KVM_REG_PPC_MMCR0;
1172 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1173 break;
1174 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1175 i = id - KVM_REG_PPC_PMC1;
1176 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1177 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001178 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1179 i = id - KVM_REG_PPC_SPMC1;
1180 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1181 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001182 case KVM_REG_PPC_SIAR:
1183 vcpu->arch.siar = set_reg_val(id, *val);
1184 break;
1185 case KVM_REG_PPC_SDAR:
1186 vcpu->arch.sdar = set_reg_val(id, *val);
1187 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001188 case KVM_REG_PPC_SIER:
1189 vcpu->arch.sier = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001190 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001191 case KVM_REG_PPC_IAMR:
1192 vcpu->arch.iamr = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001193 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001194 case KVM_REG_PPC_PSPB:
1195 vcpu->arch.pspb = set_reg_val(id, *val);
1196 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001197 case KVM_REG_PPC_DPDES:
1198 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1199 break;
1200 case KVM_REG_PPC_DAWR:
1201 vcpu->arch.dawr = set_reg_val(id, *val);
1202 break;
1203 case KVM_REG_PPC_DAWRX:
1204 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1205 break;
1206 case KVM_REG_PPC_CIABR:
1207 vcpu->arch.ciabr = set_reg_val(id, *val);
1208 /* Don't allow setting breakpoints in hypervisor code */
1209 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1210 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1211 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001212 case KVM_REG_PPC_CSIGR:
1213 vcpu->arch.csigr = set_reg_val(id, *val);
1214 break;
1215 case KVM_REG_PPC_TACR:
1216 vcpu->arch.tacr = set_reg_val(id, *val);
1217 break;
1218 case KVM_REG_PPC_TCSCR:
1219 vcpu->arch.tcscr = set_reg_val(id, *val);
1220 break;
1221 case KVM_REG_PPC_PID:
1222 vcpu->arch.pid = set_reg_val(id, *val);
1223 break;
1224 case KVM_REG_PPC_ACOP:
1225 vcpu->arch.acop = set_reg_val(id, *val);
1226 break;
1227 case KVM_REG_PPC_WORT:
1228 vcpu->arch.wort = set_reg_val(id, *val);
1229 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001230 case KVM_REG_PPC_VPA_ADDR:
1231 addr = set_reg_val(id, *val);
1232 r = -EINVAL;
1233 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1234 vcpu->arch.dtl.next_gpa))
1235 break;
1236 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1237 break;
1238 case KVM_REG_PPC_VPA_SLB:
1239 addr = val->vpaval.addr;
1240 len = val->vpaval.length;
1241 r = -EINVAL;
1242 if (addr && !vcpu->arch.vpa.next_gpa)
1243 break;
1244 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1245 break;
1246 case KVM_REG_PPC_VPA_DTL:
1247 addr = val->vpaval.addr;
1248 len = val->vpaval.length;
1249 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +00001250 if (addr && (len < sizeof(struct dtl_entry) ||
1251 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +00001252 break;
1253 len -= len % sizeof(struct dtl_entry);
1254 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1255 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001256 case KVM_REG_PPC_TB_OFFSET:
1257 /* round up to multiple of 2^24 */
1258 vcpu->arch.vcore->tb_offset =
1259 ALIGN(set_reg_val(id, *val), 1UL << 24);
1260 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001261 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001262 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1263 break;
1264 case KVM_REG_PPC_LPCR_64:
1265 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001266 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001267 case KVM_REG_PPC_PPR:
1268 vcpu->arch.ppr = set_reg_val(id, *val);
1269 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001270#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1271 case KVM_REG_PPC_TFHAR:
1272 vcpu->arch.tfhar = set_reg_val(id, *val);
1273 break;
1274 case KVM_REG_PPC_TFIAR:
1275 vcpu->arch.tfiar = set_reg_val(id, *val);
1276 break;
1277 case KVM_REG_PPC_TEXASR:
1278 vcpu->arch.texasr = set_reg_val(id, *val);
1279 break;
1280 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1281 i = id - KVM_REG_PPC_TM_GPR0;
1282 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1283 break;
1284 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1285 {
1286 int j;
1287 i = id - KVM_REG_PPC_TM_VSR0;
1288 if (i < 32)
1289 for (j = 0; j < TS_FPRWIDTH; j++)
1290 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1291 else
1292 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1293 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1294 else
1295 r = -ENXIO;
1296 break;
1297 }
1298 case KVM_REG_PPC_TM_CR:
1299 vcpu->arch.cr_tm = set_reg_val(id, *val);
1300 break;
1301 case KVM_REG_PPC_TM_LR:
1302 vcpu->arch.lr_tm = set_reg_val(id, *val);
1303 break;
1304 case KVM_REG_PPC_TM_CTR:
1305 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1306 break;
1307 case KVM_REG_PPC_TM_FPSCR:
1308 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1309 break;
1310 case KVM_REG_PPC_TM_AMR:
1311 vcpu->arch.amr_tm = set_reg_val(id, *val);
1312 break;
1313 case KVM_REG_PPC_TM_PPR:
1314 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1315 break;
1316 case KVM_REG_PPC_TM_VRSAVE:
1317 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1318 break;
1319 case KVM_REG_PPC_TM_VSCR:
1320 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1321 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1322 else
1323 r = - ENXIO;
1324 break;
1325 case KVM_REG_PPC_TM_DSCR:
1326 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1327 break;
1328 case KVM_REG_PPC_TM_TAR:
1329 vcpu->arch.tar_tm = set_reg_val(id, *val);
1330 break;
1331#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001332 case KVM_REG_PPC_ARCH_COMPAT:
1333 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1334 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001335 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001336 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001337 break;
1338 }
1339
1340 return r;
1341}
1342
Stewart Smithde9bdd12014-07-18 14:18:42 +10001343static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1344{
1345 struct kvmppc_vcore *vcore;
1346
1347 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1348
1349 if (vcore == NULL)
1350 return NULL;
1351
1352 INIT_LIST_HEAD(&vcore->runnable_threads);
1353 spin_lock_init(&vcore->lock);
Paul Mackerras2711e242014-12-04 16:43:28 +11001354 spin_lock_init(&vcore->stoltb_lock);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001355 init_waitqueue_head(&vcore->wq);
1356 vcore->preempt_tb = TB_NIL;
1357 vcore->lpcr = kvm->arch.lpcr;
1358 vcore->first_vcpuid = core * threads_per_subcore;
1359 vcore->kvm = kvm;
1360
Stewart Smith9678cda2014-07-18 14:18:43 +10001361 vcore->mpp_buffer_is_valid = false;
1362
1363 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1364 vcore->mpp_buffer = (void *)__get_free_pages(
1365 GFP_KERNEL|__GFP_ZERO,
1366 MPP_BUFFER_ORDER);
1367
Stewart Smithde9bdd12014-07-18 14:18:42 +10001368 return vcore;
1369}
1370
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301371static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1372 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001373{
1374 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001375 int err = -EINVAL;
1376 int core;
1377 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001378
Michael Ellerman3102f782014-05-23 18:15:29 +10001379 core = id / threads_per_subcore;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001380 if (core >= KVM_MAX_VCORES)
1381 goto out;
1382
1383 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001384 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001385 if (!vcpu)
1386 goto out;
1387
1388 err = kvm_vcpu_init(vcpu, kvm, id);
1389 if (err)
1390 goto free_vcpu;
1391
1392 vcpu->arch.shared = &vcpu->arch.shregs;
Alexander Graf5deb8e72014-04-24 13:46:24 +02001393#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1394 /*
1395 * The shared struct is never shared on HV,
1396 * so we can always use host endianness
1397 */
1398#ifdef __BIG_ENDIAN__
1399 vcpu->arch.shared_big_endian = true;
1400#else
1401 vcpu->arch.shared_big_endian = false;
1402#endif
1403#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001404 vcpu->arch.mmcr[0] = MMCR0_FC;
1405 vcpu->arch.ctrl = CTRL_RUNLATCH;
1406 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301407 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001408 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001409 spin_lock_init(&vcpu->arch.tbacct_lock);
1410 vcpu->arch.busy_preempt = TB_NIL;
Anton Blanchardd6829162014-01-08 21:25:30 +11001411 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001412
Paul Mackerrasde56a942011-06-29 00:21:34 +00001413 kvmppc_mmu_book3s_hv_init(vcpu);
1414
Paul Mackerras8455d792012-10-15 01:17:42 +00001415 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001416
1417 init_waitqueue_head(&vcpu->arch.cpu_run);
1418
1419 mutex_lock(&kvm->lock);
1420 vcore = kvm->arch.vcores[core];
1421 if (!vcore) {
Stewart Smithde9bdd12014-07-18 14:18:42 +10001422 vcore = kvmppc_vcore_create(kvm, core);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001423 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001424 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001425 }
1426 mutex_unlock(&kvm->lock);
1427
1428 if (!vcore)
1429 goto free_vcpu;
1430
1431 spin_lock(&vcore->lock);
1432 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001433 spin_unlock(&vcore->lock);
1434 vcpu->arch.vcore = vcore;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001435 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001436
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001437 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1438 kvmppc_sanity_check(vcpu);
1439
Paul Mackerrasde56a942011-06-29 00:21:34 +00001440 return vcpu;
1441
1442free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001443 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001444out:
1445 return ERR_PTR(err);
1446}
1447
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001448static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1449{
1450 if (vpa->pinned_addr)
1451 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1452 vpa->dirty);
1453}
1454
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301455static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001456{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001457 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001458 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1459 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1460 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001461 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001462 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001463 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001464}
1465
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301466static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1467{
1468 /* Indicate we want to get back into the guest */
1469 return 1;
1470}
1471
Paul Mackerras19ccb762011-07-23 17:42:46 +10001472static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001473{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001474 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001475
Paul Mackerras19ccb762011-07-23 17:42:46 +10001476 now = get_tb();
1477 if (now > vcpu->arch.dec_expires) {
1478 /* decrementer has already gone negative */
1479 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001480 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001481 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001482 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001483 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1484 / tb_ticks_per_sec;
1485 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1486 HRTIMER_MODE_REL);
1487 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001488}
1489
Paul Mackerras19ccb762011-07-23 17:42:46 +10001490static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001491{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001492 vcpu->arch.ceded = 0;
1493 if (vcpu->arch.timer_running) {
1494 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1495 vcpu->arch.timer_running = 0;
1496 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001497}
1498
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001499extern void __kvmppc_vcore_entry(void);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001500
1501static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1502 struct kvm_vcpu *vcpu)
1503{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001504 u64 now;
1505
Paul Mackerras371fefd2011-06-29 00:23:08 +00001506 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1507 return;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001508 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001509 now = mftb();
1510 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1511 vcpu->arch.stolen_logged;
1512 vcpu->arch.busy_preempt = now;
1513 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001514 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001515 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001516 list_del(&vcpu->arch.run_list);
1517}
1518
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001519static int kvmppc_grab_hwthread(int cpu)
1520{
1521 struct paca_struct *tpaca;
Paul Mackerrasb754c732014-09-02 16:14:42 +10001522 long timeout = 10000;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001523
1524 tpaca = &paca[cpu];
1525
1526 /* Ensure the thread won't go into the kernel if it wakes */
1527 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001528 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001529
1530 /*
1531 * If the thread is already executing in the kernel (e.g. handling
1532 * a stray interrupt), wait for it to get back to nap mode.
1533 * The smp_mb() is to ensure that our setting of hwthread_req
1534 * is visible before we look at hwthread_state, so if this
1535 * races with the code at system_reset_pSeries and the thread
1536 * misses our setting of hwthread_req, we are sure to see its
1537 * setting of hwthread_state, and vice versa.
1538 */
1539 smp_mb();
1540 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1541 if (--timeout <= 0) {
1542 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1543 return -EBUSY;
1544 }
1545 udelay(1);
1546 }
1547 return 0;
1548}
1549
1550static void kvmppc_release_hwthread(int cpu)
1551{
1552 struct paca_struct *tpaca;
1553
1554 tpaca = &paca[cpu];
1555 tpaca->kvm_hstate.hwthread_req = 0;
1556 tpaca->kvm_hstate.kvm_vcpu = NULL;
1557}
1558
Paul Mackerras371fefd2011-06-29 00:23:08 +00001559static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1560{
1561 int cpu;
1562 struct paca_struct *tpaca;
1563 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1564
Paul Mackerras19ccb762011-07-23 17:42:46 +10001565 if (vcpu->arch.timer_running) {
1566 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1567 vcpu->arch.timer_running = 0;
1568 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001569 cpu = vc->pcpu + vcpu->arch.ptid;
1570 tpaca = &paca[cpu];
1571 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1572 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001573 tpaca->kvm_hstate.ptid = vcpu->arch.ptid;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001574 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001575 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +00001576#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001577 if (cpu != smp_processor_id()) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001578 xics_wake_cpu(cpu);
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001579 if (vcpu->arch.ptid)
1580 ++vc->n_woken;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001581 }
1582#endif
1583}
1584
1585static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1586{
1587 int i;
1588
1589 HMT_low();
1590 i = 0;
1591 while (vc->nap_count < vc->n_woken) {
1592 if (++i >= 1000000) {
1593 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1594 vc->nap_count, vc->n_woken);
1595 break;
1596 }
1597 cpu_relax();
1598 }
1599 HMT_medium();
1600}
1601
1602/*
1603 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001604 * this core are off-line. Then grab the threads so they can't
1605 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001606 */
1607static int on_primary_thread(void)
1608{
1609 int cpu = smp_processor_id();
Michael Ellerman3102f782014-05-23 18:15:29 +10001610 int thr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001611
Michael Ellerman3102f782014-05-23 18:15:29 +10001612 /* Are we on a primary subcore? */
1613 if (cpu_thread_in_subcore(cpu))
Paul Mackerras371fefd2011-06-29 00:23:08 +00001614 return 0;
Michael Ellerman3102f782014-05-23 18:15:29 +10001615
1616 thr = 0;
1617 while (++thr < threads_per_subcore)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001618 if (cpu_online(cpu + thr))
1619 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001620
1621 /* Grab all hw threads so they can't go into the kernel */
Michael Ellerman3102f782014-05-23 18:15:29 +10001622 for (thr = 1; thr < threads_per_subcore; ++thr) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00001623 if (kvmppc_grab_hwthread(cpu + thr)) {
1624 /* Couldn't grab one; let the others go */
1625 do {
1626 kvmppc_release_hwthread(cpu + thr);
1627 } while (--thr > 0);
1628 return 0;
1629 }
1630 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001631 return 1;
1632}
1633
Stewart Smith9678cda2014-07-18 14:18:43 +10001634static void kvmppc_start_saving_l2_cache(struct kvmppc_vcore *vc)
1635{
1636 phys_addr_t phy_addr, mpp_addr;
1637
1638 phy_addr = (phys_addr_t)virt_to_phys(vc->mpp_buffer);
1639 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1640
1641 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_ABORT);
1642 logmpp(mpp_addr | PPC_LOGMPP_LOG_L2);
1643
1644 vc->mpp_buffer_is_valid = true;
1645}
1646
1647static void kvmppc_start_restoring_l2_cache(const struct kvmppc_vcore *vc)
1648{
1649 phys_addr_t phy_addr, mpp_addr;
1650
1651 phy_addr = virt_to_phys(vc->mpp_buffer);
1652 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1653
1654 /* We must abort any in-progress save operations to ensure
1655 * the table is valid so that prefetch engine knows when to
1656 * stop prefetching. */
1657 logmpp(mpp_addr | PPC_LOGMPP_LOG_ABORT);
1658 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_WHOLE_TABLE);
1659}
1660
Paul Mackerras371fefd2011-06-29 00:23:08 +00001661/*
1662 * Run a set of guest threads on a physical core.
1663 * Called with vc->lock held.
1664 */
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001665static void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001666{
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001667 struct kvm_vcpu *vcpu, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001668 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001669 u64 now;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001670 int i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001671 int srcu_idx;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001672 struct kvm_vcpu *vcpus_to_update[threads_per_core];
Paul Mackerrasde56a942011-06-29 00:21:34 +00001673
Paul Mackerras371fefd2011-06-29 00:23:08 +00001674 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +10001675 need_vpa_update = 0;
1676 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001677 if (signal_pending(vcpu->arch.run_task))
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001678 return;
1679 if (vcpu->arch.vpa.update_pending ||
1680 vcpu->arch.slb_shadow.update_pending ||
1681 vcpu->arch.dtl.update_pending)
1682 vcpus_to_update[need_vpa_update++] = vcpu;
Paul Mackerras081f3232012-06-01 20:20:24 +10001683 }
1684
1685 /*
1686 * Initialize *vc, in particular vc->vcore_state, so we can
1687 * drop the vcore lock if necessary.
1688 */
1689 vc->n_woken = 0;
1690 vc->nap_count = 0;
1691 vc->entry_exit_count = 0;
Paul Mackerras2711e242014-12-04 16:43:28 +11001692 vc->preempt_tb = TB_NIL;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001693 vc->vcore_state = VCORE_STARTING;
Paul Mackerras081f3232012-06-01 20:20:24 +10001694 vc->in_guest = 0;
1695 vc->napping_threads = 0;
1696
1697 /*
1698 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1699 * which can't be called with any spinlocks held.
1700 */
1701 if (need_vpa_update) {
1702 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001703 for (i = 0; i < need_vpa_update; ++i)
1704 kvmppc_update_vpas(vcpus_to_update[i]);
Paul Mackerras081f3232012-06-01 20:20:24 +10001705 spin_lock(&vc->lock);
1706 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001707
1708 /*
Michael Ellerman3102f782014-05-23 18:15:29 +10001709 * Make sure we are running on primary threads, and that secondary
1710 * threads are offline. Also check if the number of threads in this
1711 * guest are greater than the current system threads per guest.
Paul Mackerras7b444c62012-10-15 01:16:14 +00001712 */
Michael Ellerman3102f782014-05-23 18:15:29 +10001713 if ((threads_per_core > 1) &&
1714 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00001715 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1716 vcpu->arch.ret = -EBUSY;
1717 goto out;
1718 }
1719
Michael Ellerman3102f782014-05-23 18:15:29 +10001720
Paul Mackerras371fefd2011-06-29 00:23:08 +00001721 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001722 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001723 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001724 kvmppc_create_dtl_entry(vcpu, vc);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001725 trace_kvm_guest_enter(vcpu);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001726 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001727
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001728 /* Set this explicitly in case thread 0 doesn't have a vcpu */
1729 get_paca()->kvm_hstate.kvm_vcore = vc;
1730 get_paca()->kvm_hstate.ptid = 0;
1731
Paul Mackerras2f12f032012-10-15 01:17:17 +00001732 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001733 preempt_disable();
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001734
1735 trace_kvmppc_run_core(vc, 0);
1736
Paul Mackerras19ccb762011-07-23 17:42:46 +10001737 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001738
Paul Mackerras19ccb762011-07-23 17:42:46 +10001739 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001740
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001741 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001742
Stewart Smith9678cda2014-07-18 14:18:43 +10001743 if (vc->mpp_buffer_is_valid)
1744 kvmppc_start_restoring_l2_cache(vc);
1745
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001746 __kvmppc_vcore_entry();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001747
Paul Mackerras371fefd2011-06-29 00:23:08 +00001748 spin_lock(&vc->lock);
Stewart Smith9678cda2014-07-18 14:18:43 +10001749
1750 if (vc->mpp_buffer)
1751 kvmppc_start_saving_l2_cache(vc);
1752
Paul Mackerras19ccb762011-07-23 17:42:46 +10001753 /* disable sending of IPIs on virtual external irqs */
1754 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1755 vcpu->cpu = -1;
1756 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001757 if (vc->nap_count < vc->n_woken)
1758 kvmppc_wait_for_nap(vc);
Michael Ellerman3102f782014-05-23 18:15:29 +10001759 for (i = 0; i < threads_per_subcore; ++i)
Paul Mackerras2f12f032012-10-15 01:17:17 +00001760 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001761 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001762 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001763 spin_unlock(&vc->lock);
1764
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001765 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001766
Paul Mackerras371fefd2011-06-29 00:23:08 +00001767 /* make sure updates to secondary vcpu structs are visible now */
1768 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001769 kvm_guest_exit();
1770
1771 preempt_enable();
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09001772 cond_resched();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001773
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001774 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001775 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001776 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1777 /* cancel pending dec exception if dec is positive */
1778 if (now < vcpu->arch.dec_expires &&
1779 kvmppc_core_pending_dec(vcpu))
1780 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001781
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001782 trace_kvm_guest_exit(vcpu);
1783
Paul Mackerras19ccb762011-07-23 17:42:46 +10001784 ret = RESUME_GUEST;
1785 if (vcpu->arch.trap)
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301786 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1787 vcpu->arch.run_task);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001788
Paul Mackerras371fefd2011-06-29 00:23:08 +00001789 vcpu->arch.ret = ret;
1790 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001791
1792 if (vcpu->arch.ceded) {
Greg Kurze59d24e2014-02-06 17:36:56 +01001793 if (!is_kvmppc_resume_guest(ret))
Paul Mackerras19ccb762011-07-23 17:42:46 +10001794 kvmppc_end_cede(vcpu);
1795 else
1796 kvmppc_set_timer(vcpu);
1797 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001798 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001799
Paul Mackerrasde56a942011-06-29 00:21:34 +00001800 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001801 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001802 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1803 arch.run_list) {
Greg Kurze59d24e2014-02-06 17:36:56 +01001804 if (!is_kvmppc_resume_guest(vcpu->arch.ret)) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001805 kvmppc_remove_runnable(vc, vcpu);
1806 wake_up(&vcpu->arch.cpu_run);
1807 }
1808 }
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001809
1810 trace_kvmppc_run_core(vc, 1);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001811}
1812
Paul Mackerras19ccb762011-07-23 17:42:46 +10001813/*
1814 * Wait for some other vcpu thread to execute us, and
1815 * wake us up when we need to handle something in the host.
1816 */
1817static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001818{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001819 DEFINE_WAIT(wait);
1820
Paul Mackerras19ccb762011-07-23 17:42:46 +10001821 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1822 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1823 schedule();
1824 finish_wait(&vcpu->arch.cpu_run, &wait);
1825}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001826
Paul Mackerras19ccb762011-07-23 17:42:46 +10001827/*
1828 * All the vcpus in this vcore are idle, so wait for a decrementer
1829 * or external interrupt to one of the vcpus. vc->lock is held.
1830 */
1831static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1832{
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11001833 struct kvm_vcpu *vcpu;
1834 int do_sleep = 1;
1835
Paul Mackerras19ccb762011-07-23 17:42:46 +10001836 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001837
1838 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11001839
1840 /*
1841 * Check one last time for pending exceptions and ceded state after
1842 * we put ourselves on the wait queue
1843 */
1844 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1845 if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded) {
1846 do_sleep = 0;
1847 break;
1848 }
1849 }
1850
1851 if (!do_sleep) {
1852 finish_wait(&vc->wq, &wait);
1853 return;
1854 }
1855
Paul Mackerras19ccb762011-07-23 17:42:46 +10001856 vc->vcore_state = VCORE_SLEEPING;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001857 trace_kvmppc_vcore_blocked(vc, 0);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001858 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001859 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001860 finish_wait(&vc->wq, &wait);
1861 spin_lock(&vc->lock);
1862 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001863 trace_kvmppc_vcore_blocked(vc, 1);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001864}
1865
1866static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1867{
1868 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001869 struct kvmppc_vcore *vc;
1870 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001871
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001872 trace_kvmppc_run_vcpu_enter(vcpu);
1873
Paul Mackerras371fefd2011-06-29 00:23:08 +00001874 kvm_run->exit_reason = 0;
1875 vcpu->arch.ret = RESUME_GUEST;
1876 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001877 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001878
Paul Mackerras371fefd2011-06-29 00:23:08 +00001879 /*
1880 * Synchronize with other threads in this virtual core
1881 */
1882 vc = vcpu->arch.vcore;
1883 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001884 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001885 vcpu->arch.run_task = current;
1886 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001887 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10001888 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001889 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001890 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1891 ++vc->n_runnable;
1892
Paul Mackerras19ccb762011-07-23 17:42:46 +10001893 /*
1894 * This happens the first time this is called for a vcpu.
1895 * If the vcore is already running, we may be able to start
1896 * this thread straight away and have it join in.
1897 */
Paul Mackerras8455d792012-10-15 01:17:42 +00001898 if (!signal_pending(current)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001899 if (vc->vcore_state == VCORE_RUNNING &&
1900 VCORE_EXIT_COUNT(vc) == 0) {
Paul Mackerras2f12f032012-10-15 01:17:17 +00001901 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001902 kvmppc_start_thread(vcpu);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001903 trace_kvm_guest_enter(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00001904 } else if (vc->vcore_state == VCORE_SLEEPING) {
1905 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001906 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001907
Paul Mackerras8455d792012-10-15 01:17:42 +00001908 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001909
1910 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1911 !signal_pending(current)) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001912 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001913 spin_unlock(&vc->lock);
1914 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1915 spin_lock(&vc->lock);
1916 continue;
1917 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001918 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1919 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001920 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001921 if (signal_pending(v->arch.run_task)) {
1922 kvmppc_remove_runnable(vc, v);
1923 v->stat.signal_exits++;
1924 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1925 v->arch.ret = -EINTR;
1926 wake_up(&v->arch.cpu_run);
1927 }
1928 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001929 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1930 break;
1931 vc->runner = vcpu;
1932 n_ceded = 0;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001933 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001934 if (!v->arch.pending_exceptions)
1935 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001936 else
1937 v->arch.ceded = 0;
1938 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001939 if (n_ceded == vc->n_runnable)
1940 kvmppc_vcore_blocked(vc);
1941 else
1942 kvmppc_run_core(vc);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001943 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001944 }
1945
Paul Mackerras8455d792012-10-15 01:17:42 +00001946 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1947 (vc->vcore_state == VCORE_RUNNING ||
1948 vc->vcore_state == VCORE_EXITING)) {
1949 spin_unlock(&vc->lock);
1950 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1951 spin_lock(&vc->lock);
1952 }
1953
1954 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1955 kvmppc_remove_runnable(vc, vcpu);
1956 vcpu->stat.signal_exits++;
1957 kvm_run->exit_reason = KVM_EXIT_INTR;
1958 vcpu->arch.ret = -EINTR;
1959 }
1960
1961 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1962 /* Wake up some vcpu to run the core */
1963 v = list_first_entry(&vc->runnable_threads,
1964 struct kvm_vcpu, arch.run_list);
1965 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001966 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001967
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06001968 trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001969 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001970 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001971}
1972
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301973static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001974{
1975 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001976 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001977
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001978 if (!vcpu->arch.sane) {
1979 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1980 return -EINVAL;
1981 }
1982
Scott Wood25051b5a2011-11-08 18:23:23 -06001983 kvmppc_core_prepare_to_enter(vcpu);
1984
Paul Mackerras19ccb762011-07-23 17:42:46 +10001985 /* No need to go into the guest when all we'll do is come back out */
1986 if (signal_pending(current)) {
1987 run->exit_reason = KVM_EXIT_INTR;
1988 return -EINTR;
1989 }
1990
Paul Mackerras32fad282012-05-04 02:32:53 +00001991 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1992 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1993 smp_mb();
1994
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001995 /* On the first time here, set up HTAB and VRMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001996 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001997 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001998 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001999 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002000 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002001
2002 flush_fp_to_thread(current);
2003 flush_altivec_to_thread(current);
2004 flush_vsx_to_thread(current);
2005 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00002006 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002007 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10002008
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002009 do {
2010 r = kvmppc_run_vcpu(run, vcpu);
2011
2012 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
2013 !(vcpu->arch.shregs.msr & MSR_PR)) {
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002014 trace_kvm_hcall_enter(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002015 r = kvmppc_pseries_do_hcall(vcpu);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002016 trace_kvm_hcall_exit(vcpu, r);
Scott Wood7e28e60e2011-11-08 18:23:20 -06002017 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002018 } else if (r == RESUME_PAGE_FAULT) {
2019 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2020 r = kvmppc_book3s_hv_page_fault(run, vcpu,
2021 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
2022 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002023 }
Greg Kurze59d24e2014-02-06 17:36:56 +01002024 } while (is_kvmppc_resume_guest(r));
Paul Mackerras32fad282012-05-04 02:32:53 +00002025
2026 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002027 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00002028 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002029 return r;
2030}
2031
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002032static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2033 int linux_psize)
2034{
2035 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2036
2037 if (!def->shift)
2038 return;
2039 (*sps)->page_shift = def->shift;
2040 (*sps)->slb_enc = def->sllp;
2041 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00002042 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05302043 /*
2044 * Add 16MB MPSS support if host supports it
2045 */
2046 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2047 (*sps)->enc[1].page_shift = 24;
2048 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2049 }
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002050 (*sps)++;
2051}
2052
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302053static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2054 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002055{
2056 struct kvm_ppc_one_seg_page_size *sps;
2057
2058 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2059 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2060 info->flags |= KVM_PPC_1T_SEGMENTS;
2061 info->slb_size = mmu_slb_size;
2062
2063 /* We only support these sizes for now, and no muti-size segments */
2064 sps = &info->sps[0];
2065 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2066 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2067 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2068
2069 return 0;
2070}
2071
Paul Mackerras82ed3612011-12-15 02:03:22 +00002072/*
2073 * Get (and clear) the dirty memory log for a memory slot.
2074 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302075static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2076 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002077{
2078 struct kvm_memory_slot *memslot;
2079 int r;
2080 unsigned long n;
2081
2082 mutex_lock(&kvm->slots_lock);
2083
2084 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07002085 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002086 goto out;
2087
2088 memslot = id_to_memslot(kvm->memslots, log->slot);
2089 r = -ENOENT;
2090 if (!memslot->dirty_bitmap)
2091 goto out;
2092
2093 n = kvm_dirty_bitmap_bytes(memslot);
2094 memset(memslot->dirty_bitmap, 0, n);
2095
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002096 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00002097 if (r)
2098 goto out;
2099
2100 r = -EFAULT;
2101 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2102 goto out;
2103
2104 r = 0;
2105out:
2106 mutex_unlock(&kvm->slots_lock);
2107 return r;
2108}
2109
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302110static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2111 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002112{
2113 if (!dont || free->arch.rmap != dont->arch.rmap) {
2114 vfree(free->arch.rmap);
2115 free->arch.rmap = NULL;
2116 }
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002117}
2118
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302119static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
2120 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002121{
2122 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
2123 if (!slot->arch.rmap)
2124 return -ENOMEM;
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002125
2126 return 0;
2127}
2128
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302129static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
2130 struct kvm_memory_slot *memslot,
2131 struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002132{
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002133 return 0;
2134}
2135
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302136static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
2137 struct kvm_userspace_memory_region *mem,
2138 const struct kvm_memory_slot *old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002139{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002140 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
2141 struct kvm_memory_slot *memslot;
2142
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002143 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002144 /*
2145 * If modifying a memslot, reset all the rmap dirty bits.
2146 * If this is a new memslot, we don't need to do anything
2147 * since the rmap array starts out as all zeroes,
2148 * i.e. no pages are dirty.
2149 */
2150 memslot = id_to_memslot(kvm->memslots, mem->slot);
2151 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
2152 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002153}
2154
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002155/*
2156 * Update LPCR values in kvm->arch and in vcores.
2157 * Caller must hold kvm->lock.
2158 */
2159void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
2160{
2161 long int i;
2162 u32 cores_done = 0;
2163
2164 if ((kvm->arch.lpcr & mask) == lpcr)
2165 return;
2166
2167 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2168
2169 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2170 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2171 if (!vc)
2172 continue;
2173 spin_lock(&vc->lock);
2174 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2175 spin_unlock(&vc->lock);
2176 if (++cores_done >= kvm->arch.online_vcores)
2177 break;
2178 }
2179}
2180
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302181static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2182{
2183 return;
2184}
2185
Paul Mackerras32fad282012-05-04 02:32:53 +00002186static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002187{
2188 int err = 0;
2189 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002190 unsigned long hva;
2191 struct kvm_memory_slot *memslot;
2192 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002193 unsigned long lpcr = 0, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002194 unsigned long psize, porder;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002195 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002196
2197 mutex_lock(&kvm->lock);
2198 if (kvm->arch.rma_setup_done)
2199 goto out; /* another vcpu beat us to it */
2200
Paul Mackerras32fad282012-05-04 02:32:53 +00002201 /* Allocate hashed page table (if not done already) and reset it */
2202 if (!kvm->arch.hpt_virt) {
2203 err = kvmppc_alloc_hpt(kvm, NULL);
2204 if (err) {
2205 pr_err("KVM: Couldn't alloc HPT\n");
2206 goto out;
2207 }
2208 }
2209
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002210 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002211 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002212 memslot = gfn_to_memslot(kvm, 0);
2213
2214 /* We must have some memory at 0 by now */
2215 err = -EINVAL;
2216 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002217 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002218
2219 /* Look up the VMA for the start of this memory slot */
2220 hva = memslot->userspace_addr;
2221 down_read(&current->mm->mmap_sem);
2222 vma = find_vma(current->mm, hva);
2223 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2224 goto up_out;
2225
2226 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002227 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002228
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002229 up_read(&current->mm->mmap_sem);
2230
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002231 /* We can handle 4k, 64k or 16M pages in the VRMA */
2232 err = -EINVAL;
2233 if (!(psize == 0x1000 || psize == 0x10000 ||
2234 psize == 0x1000000))
2235 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002236
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002237 /* Update VRMASD field in the LPCR */
2238 senc = slb_pgsize_encoding(psize);
2239 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2240 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2241 /* the -4 is to account for senc values starting at 0x10 */
2242 lpcr = senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002243
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002244 /* Create HPTEs in the hash page table for the VRMA */
2245 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002246
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002247 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002248
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002249 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
2250 smp_wmb();
2251 kvm->arch.rma_setup_done = 1;
2252 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002253 out_srcu:
2254 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002255 out:
2256 mutex_unlock(&kvm->lock);
2257 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002258
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002259 up_out:
2260 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08002261 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002262}
2263
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302264static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002265{
Paul Mackerras32fad282012-05-04 02:32:53 +00002266 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002267
Paul Mackerras32fad282012-05-04 02:32:53 +00002268 /* Allocate the guest's logical partition ID */
2269
2270 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08002271 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00002272 return -ENOMEM;
2273 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002274
Paul Mackerras1b400ba2012-11-21 23:28:08 +00002275 /*
2276 * Since we don't flush the TLB when tearing down a VM,
2277 * and this lpid might have previously been used,
2278 * make sure we flush on each core before running the new VM.
2279 */
2280 cpumask_setall(&kvm->arch.need_tlb_flush);
2281
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002282 /* Start out with the default set of hcalls enabled */
2283 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
2284 sizeof(kvm->arch.enabled_hcalls));
2285
Paul Mackerras9e368f22011-06-29 00:40:08 +00002286 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002287
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002288 /* Init LPCR for virtual RMA mode */
2289 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2290 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2291 lpcr &= LPCR_PECE | LPCR_LPES;
2292 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
2293 LPCR_VPM0 | LPCR_VPM1;
2294 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2295 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2296 /* On POWER8 turn on online bit to enable PURR/SPURR */
2297 if (cpu_has_feature(CPU_FTR_ARCH_207S))
2298 lpcr |= LPCR_ONL;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002299 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002300
Paul Mackerras512691d2012-10-15 01:15:41 +00002301 /*
Michael Ellerman441c19c2014-05-23 18:15:25 +10002302 * Track that we now have a HV mode VM active. This blocks secondary
2303 * CPU threads from coming online.
Paul Mackerras512691d2012-10-15 01:15:41 +00002304 */
Michael Ellerman441c19c2014-05-23 18:15:25 +10002305 kvm_hv_vm_activated();
Paul Mackerras512691d2012-10-15 01:15:41 +00002306
David Gibson54738c02011-06-29 00:22:41 +00002307 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002308}
2309
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302310static void kvmppc_free_vcores(struct kvm *kvm)
2311{
2312 long int i;
2313
Stewart Smith9678cda2014-07-18 14:18:43 +10002314 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2315 if (kvm->arch.vcores[i] && kvm->arch.vcores[i]->mpp_buffer) {
2316 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2317 free_pages((unsigned long)vc->mpp_buffer,
2318 MPP_BUFFER_ORDER);
2319 }
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302320 kfree(kvm->arch.vcores[i]);
Stewart Smith9678cda2014-07-18 14:18:43 +10002321 }
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302322 kvm->arch.online_vcores = 0;
2323}
2324
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302325static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002326{
Michael Ellerman441c19c2014-05-23 18:15:25 +10002327 kvm_hv_vm_deactivated();
Paul Mackerras512691d2012-10-15 01:15:41 +00002328
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302329 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002330
Paul Mackerrasde56a942011-06-29 00:21:34 +00002331 kvmppc_free_hpt(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002332}
2333
2334/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302335static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2336 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002337{
2338 return EMULATE_FAIL;
2339}
2340
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302341static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2342 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002343{
2344 return EMULATE_FAIL;
2345}
2346
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302347static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2348 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002349{
2350 return EMULATE_FAIL;
2351}
2352
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302353static int kvmppc_core_check_processor_compat_hv(void)
2354{
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002355 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
2356 !cpu_has_feature(CPU_FTR_ARCH_206))
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302357 return -EIO;
2358 return 0;
2359}
2360
2361static long kvm_arch_vm_ioctl_hv(struct file *filp,
2362 unsigned int ioctl, unsigned long arg)
2363{
2364 struct kvm *kvm __maybe_unused = filp->private_data;
2365 void __user *argp = (void __user *)arg;
2366 long r;
2367
2368 switch (ioctl) {
2369
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302370 case KVM_PPC_ALLOCATE_HTAB: {
2371 u32 htab_order;
2372
2373 r = -EFAULT;
2374 if (get_user(htab_order, (u32 __user *)argp))
2375 break;
2376 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2377 if (r)
2378 break;
2379 r = -EFAULT;
2380 if (put_user(htab_order, (u32 __user *)argp))
2381 break;
2382 r = 0;
2383 break;
2384 }
2385
2386 case KVM_PPC_GET_HTAB_FD: {
2387 struct kvm_get_htab_fd ghf;
2388
2389 r = -EFAULT;
2390 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2391 break;
2392 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2393 break;
2394 }
2395
2396 default:
2397 r = -ENOTTY;
2398 }
2399
2400 return r;
2401}
2402
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002403/*
2404 * List of hcall numbers to enable by default.
2405 * For compatibility with old userspace, we enable by default
2406 * all hcalls that were implemented before the hcall-enabling
2407 * facility was added. Note this list should not include H_RTAS.
2408 */
2409static unsigned int default_hcall_list[] = {
2410 H_REMOVE,
2411 H_ENTER,
2412 H_READ,
2413 H_PROTECT,
2414 H_BULK_REMOVE,
2415 H_GET_TCE,
2416 H_PUT_TCE,
2417 H_SET_DABR,
2418 H_SET_XDABR,
2419 H_CEDE,
2420 H_PROD,
2421 H_CONFER,
2422 H_REGISTER_VPA,
2423#ifdef CONFIG_KVM_XICS
2424 H_EOI,
2425 H_CPPR,
2426 H_IPI,
2427 H_IPOLL,
2428 H_XIRR,
2429 H_XIRR_X,
2430#endif
2431 0
2432};
2433
2434static void init_default_hcalls(void)
2435{
2436 int i;
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002437 unsigned int hcall;
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002438
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002439 for (i = 0; default_hcall_list[i]; ++i) {
2440 hcall = default_hcall_list[i];
2441 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
2442 __set_bit(hcall / 4, default_enabled_hcalls);
2443 }
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002444}
2445
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302446static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302447 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2448 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2449 .get_one_reg = kvmppc_get_one_reg_hv,
2450 .set_one_reg = kvmppc_set_one_reg_hv,
2451 .vcpu_load = kvmppc_core_vcpu_load_hv,
2452 .vcpu_put = kvmppc_core_vcpu_put_hv,
2453 .set_msr = kvmppc_set_msr_hv,
2454 .vcpu_run = kvmppc_vcpu_run_hv,
2455 .vcpu_create = kvmppc_core_vcpu_create_hv,
2456 .vcpu_free = kvmppc_core_vcpu_free_hv,
2457 .check_requests = kvmppc_core_check_requests_hv,
2458 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2459 .flush_memslot = kvmppc_core_flush_memslot_hv,
2460 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2461 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2462 .unmap_hva = kvm_unmap_hva_hv,
2463 .unmap_hva_range = kvm_unmap_hva_range_hv,
2464 .age_hva = kvm_age_hva_hv,
2465 .test_age_hva = kvm_test_age_hva_hv,
2466 .set_spte_hva = kvm_set_spte_hva_hv,
2467 .mmu_destroy = kvmppc_mmu_destroy_hv,
2468 .free_memslot = kvmppc_core_free_memslot_hv,
2469 .create_memslot = kvmppc_core_create_memslot_hv,
2470 .init_vm = kvmppc_core_init_vm_hv,
2471 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302472 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2473 .emulate_op = kvmppc_core_emulate_op_hv,
2474 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2475 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2476 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2477 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002478 .hcall_implemented = kvmppc_hcall_impl_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302479};
2480
2481static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002482{
2483 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302484 /*
2485 * FIXME!! Do we need to check on all cpus ?
2486 */
2487 r = kvmppc_core_check_processor_compat_hv();
2488 if (r < 0)
Paul Mackerras739e2422014-03-25 10:47:05 +11002489 return -ENODEV;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002490
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302491 kvm_ops_hv.owner = THIS_MODULE;
2492 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002493
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002494 init_default_hcalls();
2495
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302496 r = kvmppc_mmu_hv_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002497 return r;
2498}
2499
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302500static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002501{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302502 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002503}
2504
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302505module_init(kvmppc_book3s_init_hv);
2506module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05302507MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01002508MODULE_ALIAS_MISCDEV(KVM_MINOR);
2509MODULE_ALIAS("devname:kvm");