blob: 0c5266e75a2eac127c9dbeb0dcf30345d80cc185 [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>
38#include <asm/cacheflush.h>
39#include <asm/tlbflush.h>
40#include <asm/uaccess.h>
41#include <asm/io.h>
42#include <asm/kvm_ppc.h>
43#include <asm/kvm_book3s.h>
44#include <asm/mmu_context.h>
45#include <asm/lppaca.h>
46#include <asm/processor.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000047#include <asm/cputhreads.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000048#include <asm/page.h>
Michael Neulingde1d9242011-11-09 20:39:49 +000049#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010050#include <asm/switch_to.h>
Paul Mackerras512691d2012-10-15 01:15:41 +000051#include <asm/smp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000052#include <linux/gfp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000053#include <linux/vmalloc.h>
54#include <linux/highmem.h>
Paul Mackerrasc77162d2011-12-12 12:31:00 +000055#include <linux/hugetlb.h>
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +053056#include <linux/module.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000057
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053058#include "book3s.h"
59
Paul Mackerrasde56a942011-06-29 00:21:34 +000060/* #define EXIT_DEBUG */
61/* #define EXIT_DEBUG_SIMPLE */
62/* #define EXIT_DEBUG_INT */
63
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000064/* Used to indicate that a guest page fault needs to be handled */
65#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
66
Paul Mackerrasc7b67672012-10-15 01:18:07 +000067/* Used as a "null" value for timebase values */
68#define TB_NIL (~(u64)0)
69
Paul Mackerras699a0ea2014-06-02 11:02:59 +100070static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
71
Paul Mackerras19ccb762011-07-23 17:42:46 +100072static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +000073static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +100074
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053075static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +000076{
77 int me;
78 int cpu = vcpu->cpu;
79 wait_queue_head_t *wqp;
80
81 wqp = kvm_arch_vcpu_wq(vcpu);
82 if (waitqueue_active(wqp)) {
83 wake_up_interruptible(wqp);
84 ++vcpu->stat.halt_wakeup;
85 }
86
87 me = get_cpu();
88
89 /* CPU points to the first thread of the core */
90 if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
Anton Blanchard75052582014-03-25 10:47:01 +110091#ifdef CONFIG_PPC_ICP_NATIVE
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +000092 int real_cpu = cpu + vcpu->arch.ptid;
93 if (paca[real_cpu].kvm_hstate.xics_phys)
94 xics_wake_cpu(real_cpu);
Andreas Schwab48eaef02013-12-30 15:36:56 +010095 else
96#endif
97 if (cpu_online(cpu))
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +000098 smp_send_reschedule(cpu);
99 }
100 put_cpu();
101}
102
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000103/*
104 * We use the vcpu_load/put functions to measure stolen time.
105 * Stolen time is counted as time when either the vcpu is able to
106 * run as part of a virtual core, but the task running the vcore
107 * is preempted or sleeping, or when the vcpu needs something done
108 * in the kernel by the task running the vcpu, but that task is
109 * preempted or sleeping. Those two things have to be counted
110 * separately, since one of the vcpu tasks will take on the job
111 * of running the core, and the other vcpu tasks in the vcore will
112 * sleep waiting for it to do that, but that sleep shouldn't count
113 * as stolen time.
114 *
115 * Hence we accumulate stolen time when the vcpu can run as part of
116 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
117 * needs its task to do other things in the kernel (for example,
118 * service a page fault) in busy_stolen. We don't accumulate
119 * stolen time for a vcore when it is inactive, or for a vcpu
120 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
121 * a misnomer; it means that the vcpu task is not executing in
122 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
123 * the kernel. We don't have any way of dividing up that time
124 * between time that the vcpu is genuinely stopped, time that
125 * the task is actively working on behalf of the vcpu, and time
126 * that the task is preempted, so we don't count any of it as
127 * stolen.
128 *
129 * Updates to busy_stolen are protected by arch.tbacct_lock;
130 * updates to vc->stolen_tb are protected by the arch.tbacct_lock
131 * of the vcpu that has taken responsibility for running the vcore
132 * (i.e. vc->runner). The stolen times are measured in units of
133 * timebase ticks. (Note that the != TB_NIL checks below are
134 * purely defensive; they should never fail.)
135 */
136
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530137static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000138{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000139 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100140 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000141
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100142 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000143 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
144 vc->preempt_tb != TB_NIL) {
Paul Mackerras0456ec42012-02-03 00:56:21 +0000145 vc->stolen_tb += mftb() - vc->preempt_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000146 vc->preempt_tb = TB_NIL;
147 }
148 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
149 vcpu->arch.busy_preempt != TB_NIL) {
150 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
151 vcpu->arch.busy_preempt = TB_NIL;
152 }
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100153 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000154}
155
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530156static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000157{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000158 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100159 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000160
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100161 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000162 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
163 vc->preempt_tb = mftb();
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000164 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
165 vcpu->arch.busy_preempt = mftb();
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100166 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000167}
168
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530169static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000170{
171 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000172 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000173}
174
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530175void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000176{
177 vcpu->arch.pvr = pvr;
178}
179
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000180int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
181{
182 unsigned long pcr = 0;
183 struct kvmppc_vcore *vc = vcpu->arch.vcore;
184
185 if (arch_compat) {
186 if (!cpu_has_feature(CPU_FTR_ARCH_206))
187 return -EINVAL; /* 970 has no compat mode support */
188
189 switch (arch_compat) {
190 case PVR_ARCH_205:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100191 /*
192 * If an arch bit is set in PCR, all the defined
193 * higher-order arch bits also have to be set.
194 */
195 pcr = PCR_ARCH_206 | PCR_ARCH_205;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000196 break;
197 case PVR_ARCH_206:
198 case PVR_ARCH_206p:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100199 pcr = PCR_ARCH_206;
200 break;
201 case PVR_ARCH_207:
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000202 break;
203 default:
204 return -EINVAL;
205 }
Paul Mackerras5557ae02014-01-08 21:25:24 +1100206
207 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
208 /* POWER7 can't emulate POWER8 */
209 if (!(pcr & PCR_ARCH_206))
210 return -EINVAL;
211 pcr &= ~PCR_ARCH_206;
212 }
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000213 }
214
215 spin_lock(&vc->lock);
216 vc->arch_compat = arch_compat;
217 vc->pcr = pcr;
218 spin_unlock(&vc->lock);
219
220 return 0;
221}
222
Paul Mackerrasde56a942011-06-29 00:21:34 +0000223void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
224{
225 int r;
226
227 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
228 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
229 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
230 for (r = 0; r < 16; ++r)
231 pr_err("r%2d = %.16lx r%d = %.16lx\n",
232 r, kvmppc_get_gpr(vcpu, r),
233 r+16, kvmppc_get_gpr(vcpu, r+16));
234 pr_err("ctr = %.16lx lr = %.16lx\n",
235 vcpu->arch.ctr, vcpu->arch.lr);
236 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
237 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
238 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
239 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
240 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
241 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
242 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
243 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
244 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
245 pr_err("fault dar = %.16lx dsisr = %.8x\n",
246 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
247 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
248 for (r = 0; r < vcpu->arch.slb_max; ++r)
249 pr_err(" ESID = %.16llx VSID = %.16llx\n",
250 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
251 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000252 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000253 vcpu->arch.last_inst);
254}
255
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000256struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
257{
258 int r;
259 struct kvm_vcpu *v, *ret = NULL;
260
261 mutex_lock(&kvm->lock);
262 kvm_for_each_vcpu(r, v, kvm) {
263 if (v->vcpu_id == id) {
264 ret = v;
265 break;
266 }
267 }
268 mutex_unlock(&kvm->lock);
269 return ret;
270}
271
272static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
273{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000274 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Alexander Graf02407552014-06-11 10:34:19 +0200275 vpa->yield_count = cpu_to_be32(1);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000276}
277
Paul Mackerras55b665b2012-09-25 20:33:06 +0000278static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
279 unsigned long addr, unsigned long len)
280{
281 /* check address is cacheline aligned */
282 if (addr & (L1_CACHE_BYTES - 1))
283 return -EINVAL;
284 spin_lock(&vcpu->arch.vpa_update_lock);
285 if (v->next_gpa != addr || v->len != len) {
286 v->next_gpa = addr;
287 v->len = addr ? len : 0;
288 v->update_pending = 1;
289 }
290 spin_unlock(&vcpu->arch.vpa_update_lock);
291 return 0;
292}
293
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000294/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
295struct reg_vpa {
296 u32 dummy;
297 union {
Alexander Graf02407552014-06-11 10:34:19 +0200298 __be16 hword;
299 __be32 word;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000300 } length;
301};
302
303static int vpa_is_registered(struct kvmppc_vpa *vpap)
304{
305 if (vpap->update_pending)
306 return vpap->next_gpa != 0;
307 return vpap->pinned_addr != NULL;
308}
309
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000310static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
311 unsigned long flags,
312 unsigned long vcpuid, unsigned long vpa)
313{
314 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000315 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000316 void *va;
317 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000318 int err;
319 int subfunc;
320 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000321
322 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
323 if (!tvcpu)
324 return H_PARAMETER;
325
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000326 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
327 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
328 subfunc == H_VPA_REG_SLB) {
329 /* Registering new area - address must be cache-line aligned */
330 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000331 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000332
333 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000334 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
335 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000336 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000337 if (subfunc == H_VPA_REG_VPA)
Alexander Graf02407552014-06-11 10:34:19 +0200338 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000339 else
Alexander Graf02407552014-06-11 10:34:19 +0200340 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000341 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000342
343 /* Check length */
344 if (len > nb || len < sizeof(struct reg_vpa))
345 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000346 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000347 vpa = 0;
348 len = 0;
349 }
350
351 err = H_PARAMETER;
352 vpap = NULL;
353 spin_lock(&tvcpu->arch.vpa_update_lock);
354
355 switch (subfunc) {
356 case H_VPA_REG_VPA: /* register VPA */
357 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000358 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000359 vpap = &tvcpu->arch.vpa;
360 err = 0;
361 break;
362
363 case H_VPA_REG_DTL: /* register DTL */
364 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000365 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000366 len -= len % sizeof(struct dtl_entry);
367
368 /* Check that they have previously registered a VPA */
369 err = H_RESOURCE;
370 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000371 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000372
373 vpap = &tvcpu->arch.dtl;
374 err = 0;
375 break;
376
377 case H_VPA_REG_SLB: /* register SLB shadow buffer */
378 /* Check that they have previously registered a VPA */
379 err = H_RESOURCE;
380 if (!vpa_is_registered(&tvcpu->arch.vpa))
381 break;
382
383 vpap = &tvcpu->arch.slb_shadow;
384 err = 0;
385 break;
386
387 case H_VPA_DEREG_VPA: /* deregister VPA */
388 /* Check they don't still have a DTL or SLB buf registered */
389 err = H_RESOURCE;
390 if (vpa_is_registered(&tvcpu->arch.dtl) ||
391 vpa_is_registered(&tvcpu->arch.slb_shadow))
392 break;
393
394 vpap = &tvcpu->arch.vpa;
395 err = 0;
396 break;
397
398 case H_VPA_DEREG_DTL: /* deregister DTL */
399 vpap = &tvcpu->arch.dtl;
400 err = 0;
401 break;
402
403 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
404 vpap = &tvcpu->arch.slb_shadow;
405 err = 0;
406 break;
407 }
408
409 if (vpap) {
410 vpap->next_gpa = vpa;
411 vpap->len = len;
412 vpap->update_pending = 1;
413 }
414
415 spin_unlock(&tvcpu->arch.vpa_update_lock);
416
417 return err;
418}
419
Paul Mackerras081f3232012-06-01 20:20:24 +1000420static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000421{
Paul Mackerras081f3232012-06-01 20:20:24 +1000422 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000423 void *va;
424 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000425 unsigned long gpa;
426
427 /*
428 * We need to pin the page pointed to by vpap->next_gpa,
429 * but we can't call kvmppc_pin_guest_page under the lock
430 * as it does get_user_pages() and down_read(). So we
431 * have to drop the lock, pin the page, then get the lock
432 * again and check that a new area didn't get registered
433 * in the meantime.
434 */
435 for (;;) {
436 gpa = vpap->next_gpa;
437 spin_unlock(&vcpu->arch.vpa_update_lock);
438 va = NULL;
439 nb = 0;
440 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000441 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000442 spin_lock(&vcpu->arch.vpa_update_lock);
443 if (gpa == vpap->next_gpa)
444 break;
445 /* sigh... unpin that one and try again */
446 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000447 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000448 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000449
450 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000451 if (va && nb < vpap->len) {
452 /*
453 * If it's now too short, it must be that userspace
454 * has changed the mappings underlying guest memory,
455 * so unregister the region.
456 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000457 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000458 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000459 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000460 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000461 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
462 vpap->dirty);
463 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000464 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000465 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000466 if (va)
467 vpap->pinned_end = va + vpap->len;
468}
Paul Mackerras93e60242011-12-12 12:28:55 +0000469
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000470static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
471{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000472 if (!(vcpu->arch.vpa.update_pending ||
473 vcpu->arch.slb_shadow.update_pending ||
474 vcpu->arch.dtl.update_pending))
475 return;
476
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000477 spin_lock(&vcpu->arch.vpa_update_lock);
478 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000479 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000480 if (vcpu->arch.vpa.pinned_addr)
481 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000482 }
483 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000484 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000485 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
486 vcpu->arch.dtl_index = 0;
487 }
488 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000489 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000490 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000491}
492
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000493/*
494 * Return the accumulated stolen time for the vcore up until `now'.
495 * The caller should hold the vcore lock.
496 */
497static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
498{
499 u64 p;
500
501 /*
502 * If we are the task running the vcore, then since we hold
503 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
504 * can't be updated, so we don't need the tbacct_lock.
505 * If the vcore is inactive, it can't become active (since we
506 * hold the vcore lock), so the vcpu load/put functions won't
507 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
508 */
509 if (vc->vcore_state != VCORE_INACTIVE &&
510 vc->runner->arch.run_task != current) {
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100511 spin_lock_irq(&vc->runner->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000512 p = vc->stolen_tb;
513 if (vc->preempt_tb != TB_NIL)
514 p += now - vc->preempt_tb;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100515 spin_unlock_irq(&vc->runner->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000516 } else {
517 p = vc->stolen_tb;
518 }
519 return p;
520}
521
Paul Mackerras0456ec42012-02-03 00:56:21 +0000522static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
523 struct kvmppc_vcore *vc)
524{
525 struct dtl_entry *dt;
526 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000527 unsigned long stolen;
528 unsigned long core_stolen;
529 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000530
531 dt = vcpu->arch.dtl_ptr;
532 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000533 now = mftb();
534 core_stolen = vcore_stolen_time(vc, now);
535 stolen = core_stolen - vcpu->arch.stolen_logged;
536 vcpu->arch.stolen_logged = core_stolen;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100537 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000538 stolen += vcpu->arch.busy_stolen;
539 vcpu->arch.busy_stolen = 0;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100540 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000541 if (!dt || !vpa)
542 return;
543 memset(dt, 0, sizeof(struct dtl_entry));
544 dt->dispatch_reason = 7;
Alexander Graf02407552014-06-11 10:34:19 +0200545 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
546 dt->timebase = cpu_to_be64(now + vc->tb_offset);
547 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
548 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
549 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000550 ++dt;
551 if (dt == vcpu->arch.dtl.pinned_end)
552 dt = vcpu->arch.dtl.pinned_addr;
553 vcpu->arch.dtl_ptr = dt;
554 /* order writing *dt vs. writing vpa->dtl_idx */
555 smp_wmb();
Alexander Graf02407552014-06-11 10:34:19 +0200556 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000557 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000558}
559
Michael Neuling96423822014-06-02 11:03:01 +1000560static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
561{
562 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
563 return true;
564 if ((!vcpu->arch.vcore->arch_compat) &&
565 cpu_has_feature(CPU_FTR_ARCH_207S))
566 return true;
567 return false;
568}
569
570static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
571 unsigned long resource, unsigned long value1,
572 unsigned long value2)
573{
574 switch (resource) {
575 case H_SET_MODE_RESOURCE_SET_CIABR:
576 if (!kvmppc_power8_compatible(vcpu))
577 return H_P2;
578 if (value2)
579 return H_P4;
580 if (mflags)
581 return H_UNSUPPORTED_FLAG_START;
582 /* Guests can't breakpoint the hypervisor */
583 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
584 return H_P3;
585 vcpu->arch.ciabr = value1;
586 return H_SUCCESS;
587 case H_SET_MODE_RESOURCE_SET_DAWR:
588 if (!kvmppc_power8_compatible(vcpu))
589 return H_P2;
590 if (mflags)
591 return H_UNSUPPORTED_FLAG_START;
592 if (value2 & DABRX_HYP)
593 return H_P4;
594 vcpu->arch.dawr = value1;
595 vcpu->arch.dawrx = value2;
596 return H_SUCCESS;
597 default:
598 return H_TOO_HARD;
599 }
600}
601
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000602int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
603{
604 unsigned long req = kvmppc_get_gpr(vcpu, 3);
605 unsigned long target, ret = H_SUCCESS;
606 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000607 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000608
Paul Mackerras699a0ea2014-06-02 11:02:59 +1000609 if (req <= MAX_HCALL_OPCODE &&
610 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
611 return RESUME_HOST;
612
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000613 switch (req) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000614 case H_ENTER:
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000615 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000616 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
617 kvmppc_get_gpr(vcpu, 5),
618 kvmppc_get_gpr(vcpu, 6),
619 kvmppc_get_gpr(vcpu, 7));
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000620 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000621 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000622 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
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530720static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
721 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000722{
723 int r = RESUME_HOST;
724
725 vcpu->stat.sum_exits++;
726
727 run->exit_reason = KVM_EXIT_UNKNOWN;
728 run->ready_for_interrupt_injection = 1;
729 switch (vcpu->arch.trap) {
730 /* We're good on these - the host merely wanted to get our attention */
731 case BOOK3S_INTERRUPT_HV_DECREMENTER:
732 vcpu->stat.dec_exits++;
733 r = RESUME_GUEST;
734 break;
735 case BOOK3S_INTERRUPT_EXTERNAL:
Paul Mackerras5d00f662014-01-08 21:25:28 +1100736 case BOOK3S_INTERRUPT_H_DOORBELL:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000737 vcpu->stat.ext_intr_exits++;
738 r = RESUME_GUEST;
739 break;
740 case BOOK3S_INTERRUPT_PERFMON:
741 r = RESUME_GUEST;
742 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000743 case BOOK3S_INTERRUPT_MACHINE_CHECK:
744 /*
745 * Deliver a machine check interrupt to the guest.
746 * We have to do this, even if the host has handled the
747 * machine check, because machine checks use SRR0/1 and
748 * the interrupt might have trashed guest state in them.
749 */
750 kvmppc_book3s_queue_irqprio(vcpu,
751 BOOK3S_INTERRUPT_MACHINE_CHECK);
752 r = RESUME_GUEST;
753 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000754 case BOOK3S_INTERRUPT_PROGRAM:
755 {
756 ulong flags;
757 /*
758 * Normally program interrupts are delivered directly
759 * to the guest by the hardware, but we can get here
760 * as a result of a hypervisor emulation interrupt
761 * (e40) getting turned into a 700 by BML RTAS.
762 */
763 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
764 kvmppc_core_queue_program(vcpu, flags);
765 r = RESUME_GUEST;
766 break;
767 }
768 case BOOK3S_INTERRUPT_SYSCALL:
769 {
770 /* hcall - punt to userspace */
771 int i;
772
Liu Ping Fan27025a62013-11-19 14:12:48 +0800773 /* hypercall with MSR_PR has already been handled in rmode,
774 * and never reaches here.
775 */
776
Paul Mackerrasde56a942011-06-29 00:21:34 +0000777 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
778 for (i = 0; i < 9; ++i)
779 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
780 run->exit_reason = KVM_EXIT_PAPR_HCALL;
781 vcpu->arch.hcall_needed = 1;
782 r = RESUME_HOST;
783 break;
784 }
785 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000786 * We get these next two if the guest accesses a page which it thinks
787 * it has mapped but which is not actually present, either because
788 * it is for an emulated I/O device or because the corresonding
789 * host page has been paged out. Any other HDSI/HISI interrupts
790 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000791 */
792 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000793 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000794 break;
795 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000796 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
797 vcpu->arch.fault_dsisr = 0;
798 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000799 break;
800 /*
801 * This occurs if the guest executes an illegal instruction.
802 * We just generate a program interrupt to the guest, since
803 * we don't emulate any guest instructions at this stage.
804 */
805 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Michael Ellermanbd3048b2014-01-08 21:25:23 +1100806 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
807 r = RESUME_GUEST;
808 break;
809 /*
810 * This occurs if the guest (kernel or userspace), does something that
811 * is prohibited by HFSCR. We just generate a program interrupt to
812 * the guest.
813 */
814 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
815 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000816 r = RESUME_GUEST;
817 break;
818 default:
819 kvmppc_dump_regs(vcpu);
820 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
821 vcpu->arch.trap, kvmppc_get_pc(vcpu),
822 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +1000823 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000824 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000825 break;
826 }
827
Paul Mackerrasde56a942011-06-29 00:21:34 +0000828 return r;
829}
830
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530831static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
832 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000833{
834 int i;
835
Paul Mackerrasde56a942011-06-29 00:21:34 +0000836 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +0530837 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000838 for (i = 0; i < vcpu->arch.slb_max; i++) {
839 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
840 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
841 }
842
843 return 0;
844}
845
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530846static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
847 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000848{
849 int i, j;
850
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530851 kvmppc_set_pvr_hv(vcpu, sregs->pvr);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000852
853 j = 0;
854 for (i = 0; i < vcpu->arch.slb_nr; i++) {
855 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
856 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
857 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
858 ++j;
859 }
860 }
861 vcpu->arch.slb_max = j;
862
863 return 0;
864}
865
Alexey Kardashevskiya0840242014-07-19 17:59:34 +1000866static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
867 bool preserve_top32)
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000868{
869 struct kvmppc_vcore *vc = vcpu->arch.vcore;
870 u64 mask;
871
872 spin_lock(&vc->lock);
873 /*
Anton Blanchardd6829162014-01-08 21:25:30 +1100874 * If ILE (interrupt little-endian) has changed, update the
875 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
876 */
877 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
878 struct kvm *kvm = vcpu->kvm;
879 struct kvm_vcpu *vcpu;
880 int i;
881
882 mutex_lock(&kvm->lock);
883 kvm_for_each_vcpu(i, vcpu, kvm) {
884 if (vcpu->arch.vcore != vc)
885 continue;
886 if (new_lpcr & LPCR_ILE)
887 vcpu->arch.intr_msr |= MSR_LE;
888 else
889 vcpu->arch.intr_msr &= ~MSR_LE;
890 }
891 mutex_unlock(&kvm->lock);
892 }
893
894 /*
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000895 * Userspace can only modify DPFD (default prefetch depth),
896 * ILE (interrupt little-endian) and TC (translation control).
Paul Mackerrase0622bd2014-01-08 21:25:27 +1100897 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000898 */
899 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
Paul Mackerrase0622bd2014-01-08 21:25:27 +1100900 if (cpu_has_feature(CPU_FTR_ARCH_207S))
901 mask |= LPCR_AIL;
Alexey Kardashevskiya0840242014-07-19 17:59:34 +1000902
903 /* Broken 32-bit version of LPCR must not clear top bits */
904 if (preserve_top32)
905 mask &= 0xFFFFFFFF;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000906 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
907 spin_unlock(&vc->lock);
908}
909
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530910static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
911 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000912{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000913 int r = 0;
914 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +0000915
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000916 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000917 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000918 *val = get_reg_val(id, 0);
919 break;
920 case KVM_REG_PPC_DABR:
921 *val = get_reg_val(id, vcpu->arch.dabr);
922 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +1100923 case KVM_REG_PPC_DABRX:
924 *val = get_reg_val(id, vcpu->arch.dabrx);
925 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000926 case KVM_REG_PPC_DSCR:
927 *val = get_reg_val(id, vcpu->arch.dscr);
928 break;
929 case KVM_REG_PPC_PURR:
930 *val = get_reg_val(id, vcpu->arch.purr);
931 break;
932 case KVM_REG_PPC_SPURR:
933 *val = get_reg_val(id, vcpu->arch.spurr);
934 break;
935 case KVM_REG_PPC_AMR:
936 *val = get_reg_val(id, vcpu->arch.amr);
937 break;
938 case KVM_REG_PPC_UAMOR:
939 *val = get_reg_val(id, vcpu->arch.uamor);
940 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100941 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000942 i = id - KVM_REG_PPC_MMCR0;
943 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
944 break;
945 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
946 i = id - KVM_REG_PPC_PMC1;
947 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +0000948 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100949 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
950 i = id - KVM_REG_PPC_SPMC1;
951 *val = get_reg_val(id, vcpu->arch.spmc[i]);
952 break;
Paul Mackerras14941782013-09-06 13:11:18 +1000953 case KVM_REG_PPC_SIAR:
954 *val = get_reg_val(id, vcpu->arch.siar);
955 break;
956 case KVM_REG_PPC_SDAR:
957 *val = get_reg_val(id, vcpu->arch.sdar);
958 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100959 case KVM_REG_PPC_SIER:
960 *val = get_reg_val(id, vcpu->arch.sier);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000961 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100962 case KVM_REG_PPC_IAMR:
963 *val = get_reg_val(id, vcpu->arch.iamr);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000964 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100965 case KVM_REG_PPC_PSPB:
966 *val = get_reg_val(id, vcpu->arch.pspb);
967 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100968 case KVM_REG_PPC_DPDES:
969 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
970 break;
971 case KVM_REG_PPC_DAWR:
972 *val = get_reg_val(id, vcpu->arch.dawr);
973 break;
974 case KVM_REG_PPC_DAWRX:
975 *val = get_reg_val(id, vcpu->arch.dawrx);
976 break;
977 case KVM_REG_PPC_CIABR:
978 *val = get_reg_val(id, vcpu->arch.ciabr);
979 break;
Michael Neulingb005255e2014-01-08 21:25:21 +1100980 case KVM_REG_PPC_CSIGR:
981 *val = get_reg_val(id, vcpu->arch.csigr);
982 break;
983 case KVM_REG_PPC_TACR:
984 *val = get_reg_val(id, vcpu->arch.tacr);
985 break;
986 case KVM_REG_PPC_TCSCR:
987 *val = get_reg_val(id, vcpu->arch.tcscr);
988 break;
989 case KVM_REG_PPC_PID:
990 *val = get_reg_val(id, vcpu->arch.pid);
991 break;
992 case KVM_REG_PPC_ACOP:
993 *val = get_reg_val(id, vcpu->arch.acop);
994 break;
995 case KVM_REG_PPC_WORT:
996 *val = get_reg_val(id, vcpu->arch.wort);
997 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +0000998 case KVM_REG_PPC_VPA_ADDR:
999 spin_lock(&vcpu->arch.vpa_update_lock);
1000 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1001 spin_unlock(&vcpu->arch.vpa_update_lock);
1002 break;
1003 case KVM_REG_PPC_VPA_SLB:
1004 spin_lock(&vcpu->arch.vpa_update_lock);
1005 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1006 val->vpaval.length = vcpu->arch.slb_shadow.len;
1007 spin_unlock(&vcpu->arch.vpa_update_lock);
1008 break;
1009 case KVM_REG_PPC_VPA_DTL:
1010 spin_lock(&vcpu->arch.vpa_update_lock);
1011 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1012 val->vpaval.length = vcpu->arch.dtl.len;
1013 spin_unlock(&vcpu->arch.vpa_update_lock);
1014 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001015 case KVM_REG_PPC_TB_OFFSET:
1016 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1017 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001018 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001019 case KVM_REG_PPC_LPCR_64:
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001020 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1021 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001022 case KVM_REG_PPC_PPR:
1023 *val = get_reg_val(id, vcpu->arch.ppr);
1024 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001025#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1026 case KVM_REG_PPC_TFHAR:
1027 *val = get_reg_val(id, vcpu->arch.tfhar);
1028 break;
1029 case KVM_REG_PPC_TFIAR:
1030 *val = get_reg_val(id, vcpu->arch.tfiar);
1031 break;
1032 case KVM_REG_PPC_TEXASR:
1033 *val = get_reg_val(id, vcpu->arch.texasr);
1034 break;
1035 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1036 i = id - KVM_REG_PPC_TM_GPR0;
1037 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1038 break;
1039 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1040 {
1041 int j;
1042 i = id - KVM_REG_PPC_TM_VSR0;
1043 if (i < 32)
1044 for (j = 0; j < TS_FPRWIDTH; j++)
1045 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1046 else {
1047 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1048 val->vval = vcpu->arch.vr_tm.vr[i-32];
1049 else
1050 r = -ENXIO;
1051 }
1052 break;
1053 }
1054 case KVM_REG_PPC_TM_CR:
1055 *val = get_reg_val(id, vcpu->arch.cr_tm);
1056 break;
1057 case KVM_REG_PPC_TM_LR:
1058 *val = get_reg_val(id, vcpu->arch.lr_tm);
1059 break;
1060 case KVM_REG_PPC_TM_CTR:
1061 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1062 break;
1063 case KVM_REG_PPC_TM_FPSCR:
1064 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1065 break;
1066 case KVM_REG_PPC_TM_AMR:
1067 *val = get_reg_val(id, vcpu->arch.amr_tm);
1068 break;
1069 case KVM_REG_PPC_TM_PPR:
1070 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1071 break;
1072 case KVM_REG_PPC_TM_VRSAVE:
1073 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1074 break;
1075 case KVM_REG_PPC_TM_VSCR:
1076 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1077 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1078 else
1079 r = -ENXIO;
1080 break;
1081 case KVM_REG_PPC_TM_DSCR:
1082 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1083 break;
1084 case KVM_REG_PPC_TM_TAR:
1085 *val = get_reg_val(id, vcpu->arch.tar_tm);
1086 break;
1087#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001088 case KVM_REG_PPC_ARCH_COMPAT:
1089 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1090 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001091 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001092 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001093 break;
1094 }
1095
1096 return r;
1097}
1098
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301099static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1100 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001101{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001102 int r = 0;
1103 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001104 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +00001105
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001106 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00001107 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +00001108 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001109 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +00001110 r = -EINVAL;
1111 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001112 case KVM_REG_PPC_DABR:
1113 vcpu->arch.dabr = set_reg_val(id, *val);
1114 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001115 case KVM_REG_PPC_DABRX:
1116 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1117 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001118 case KVM_REG_PPC_DSCR:
1119 vcpu->arch.dscr = set_reg_val(id, *val);
1120 break;
1121 case KVM_REG_PPC_PURR:
1122 vcpu->arch.purr = set_reg_val(id, *val);
1123 break;
1124 case KVM_REG_PPC_SPURR:
1125 vcpu->arch.spurr = set_reg_val(id, *val);
1126 break;
1127 case KVM_REG_PPC_AMR:
1128 vcpu->arch.amr = set_reg_val(id, *val);
1129 break;
1130 case KVM_REG_PPC_UAMOR:
1131 vcpu->arch.uamor = set_reg_val(id, *val);
1132 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001133 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001134 i = id - KVM_REG_PPC_MMCR0;
1135 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1136 break;
1137 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1138 i = id - KVM_REG_PPC_PMC1;
1139 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1140 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001141 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1142 i = id - KVM_REG_PPC_SPMC1;
1143 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1144 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001145 case KVM_REG_PPC_SIAR:
1146 vcpu->arch.siar = set_reg_val(id, *val);
1147 break;
1148 case KVM_REG_PPC_SDAR:
1149 vcpu->arch.sdar = set_reg_val(id, *val);
1150 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001151 case KVM_REG_PPC_SIER:
1152 vcpu->arch.sier = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001153 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001154 case KVM_REG_PPC_IAMR:
1155 vcpu->arch.iamr = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001156 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001157 case KVM_REG_PPC_PSPB:
1158 vcpu->arch.pspb = set_reg_val(id, *val);
1159 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001160 case KVM_REG_PPC_DPDES:
1161 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1162 break;
1163 case KVM_REG_PPC_DAWR:
1164 vcpu->arch.dawr = set_reg_val(id, *val);
1165 break;
1166 case KVM_REG_PPC_DAWRX:
1167 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1168 break;
1169 case KVM_REG_PPC_CIABR:
1170 vcpu->arch.ciabr = set_reg_val(id, *val);
1171 /* Don't allow setting breakpoints in hypervisor code */
1172 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1173 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1174 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001175 case KVM_REG_PPC_CSIGR:
1176 vcpu->arch.csigr = set_reg_val(id, *val);
1177 break;
1178 case KVM_REG_PPC_TACR:
1179 vcpu->arch.tacr = set_reg_val(id, *val);
1180 break;
1181 case KVM_REG_PPC_TCSCR:
1182 vcpu->arch.tcscr = set_reg_val(id, *val);
1183 break;
1184 case KVM_REG_PPC_PID:
1185 vcpu->arch.pid = set_reg_val(id, *val);
1186 break;
1187 case KVM_REG_PPC_ACOP:
1188 vcpu->arch.acop = set_reg_val(id, *val);
1189 break;
1190 case KVM_REG_PPC_WORT:
1191 vcpu->arch.wort = set_reg_val(id, *val);
1192 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001193 case KVM_REG_PPC_VPA_ADDR:
1194 addr = set_reg_val(id, *val);
1195 r = -EINVAL;
1196 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1197 vcpu->arch.dtl.next_gpa))
1198 break;
1199 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1200 break;
1201 case KVM_REG_PPC_VPA_SLB:
1202 addr = val->vpaval.addr;
1203 len = val->vpaval.length;
1204 r = -EINVAL;
1205 if (addr && !vcpu->arch.vpa.next_gpa)
1206 break;
1207 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1208 break;
1209 case KVM_REG_PPC_VPA_DTL:
1210 addr = val->vpaval.addr;
1211 len = val->vpaval.length;
1212 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +00001213 if (addr && (len < sizeof(struct dtl_entry) ||
1214 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +00001215 break;
1216 len -= len % sizeof(struct dtl_entry);
1217 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1218 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001219 case KVM_REG_PPC_TB_OFFSET:
1220 /* round up to multiple of 2^24 */
1221 vcpu->arch.vcore->tb_offset =
1222 ALIGN(set_reg_val(id, *val), 1UL << 24);
1223 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001224 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001225 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1226 break;
1227 case KVM_REG_PPC_LPCR_64:
1228 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001229 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001230 case KVM_REG_PPC_PPR:
1231 vcpu->arch.ppr = set_reg_val(id, *val);
1232 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001233#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1234 case KVM_REG_PPC_TFHAR:
1235 vcpu->arch.tfhar = set_reg_val(id, *val);
1236 break;
1237 case KVM_REG_PPC_TFIAR:
1238 vcpu->arch.tfiar = set_reg_val(id, *val);
1239 break;
1240 case KVM_REG_PPC_TEXASR:
1241 vcpu->arch.texasr = set_reg_val(id, *val);
1242 break;
1243 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1244 i = id - KVM_REG_PPC_TM_GPR0;
1245 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1246 break;
1247 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1248 {
1249 int j;
1250 i = id - KVM_REG_PPC_TM_VSR0;
1251 if (i < 32)
1252 for (j = 0; j < TS_FPRWIDTH; j++)
1253 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1254 else
1255 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1256 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1257 else
1258 r = -ENXIO;
1259 break;
1260 }
1261 case KVM_REG_PPC_TM_CR:
1262 vcpu->arch.cr_tm = set_reg_val(id, *val);
1263 break;
1264 case KVM_REG_PPC_TM_LR:
1265 vcpu->arch.lr_tm = set_reg_val(id, *val);
1266 break;
1267 case KVM_REG_PPC_TM_CTR:
1268 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1269 break;
1270 case KVM_REG_PPC_TM_FPSCR:
1271 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1272 break;
1273 case KVM_REG_PPC_TM_AMR:
1274 vcpu->arch.amr_tm = set_reg_val(id, *val);
1275 break;
1276 case KVM_REG_PPC_TM_PPR:
1277 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1278 break;
1279 case KVM_REG_PPC_TM_VRSAVE:
1280 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1281 break;
1282 case KVM_REG_PPC_TM_VSCR:
1283 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1284 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1285 else
1286 r = - ENXIO;
1287 break;
1288 case KVM_REG_PPC_TM_DSCR:
1289 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1290 break;
1291 case KVM_REG_PPC_TM_TAR:
1292 vcpu->arch.tar_tm = set_reg_val(id, *val);
1293 break;
1294#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001295 case KVM_REG_PPC_ARCH_COMPAT:
1296 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1297 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001298 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001299 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001300 break;
1301 }
1302
1303 return r;
1304}
1305
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301306static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1307 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001308{
1309 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001310 int err = -EINVAL;
1311 int core;
1312 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001313
Michael Ellerman3102f782014-05-23 18:15:29 +10001314 core = id / threads_per_subcore;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001315 if (core >= KVM_MAX_VCORES)
1316 goto out;
1317
1318 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001319 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001320 if (!vcpu)
1321 goto out;
1322
1323 err = kvm_vcpu_init(vcpu, kvm, id);
1324 if (err)
1325 goto free_vcpu;
1326
1327 vcpu->arch.shared = &vcpu->arch.shregs;
Alexander Graf5deb8e72014-04-24 13:46:24 +02001328#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1329 /*
1330 * The shared struct is never shared on HV,
1331 * so we can always use host endianness
1332 */
1333#ifdef __BIG_ENDIAN__
1334 vcpu->arch.shared_big_endian = true;
1335#else
1336 vcpu->arch.shared_big_endian = false;
1337#endif
1338#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001339 vcpu->arch.mmcr[0] = MMCR0_FC;
1340 vcpu->arch.ctrl = CTRL_RUNLATCH;
1341 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301342 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001343 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001344 spin_lock_init(&vcpu->arch.tbacct_lock);
1345 vcpu->arch.busy_preempt = TB_NIL;
Anton Blanchardd6829162014-01-08 21:25:30 +11001346 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001347
Paul Mackerrasde56a942011-06-29 00:21:34 +00001348 kvmppc_mmu_book3s_hv_init(vcpu);
1349
Paul Mackerras8455d792012-10-15 01:17:42 +00001350 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001351
1352 init_waitqueue_head(&vcpu->arch.cpu_run);
1353
1354 mutex_lock(&kvm->lock);
1355 vcore = kvm->arch.vcores[core];
1356 if (!vcore) {
1357 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1358 if (vcore) {
1359 INIT_LIST_HEAD(&vcore->runnable_threads);
1360 spin_lock_init(&vcore->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001361 init_waitqueue_head(&vcore->wq);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001362 vcore->preempt_tb = TB_NIL;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001363 vcore->lpcr = kvm->arch.lpcr;
Michael Ellerman3102f782014-05-23 18:15:29 +10001364 vcore->first_vcpuid = core * threads_per_subcore;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001365 vcore->kvm = kvm;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001366 }
1367 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001368 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001369 }
1370 mutex_unlock(&kvm->lock);
1371
1372 if (!vcore)
1373 goto free_vcpu;
1374
1375 spin_lock(&vcore->lock);
1376 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001377 spin_unlock(&vcore->lock);
1378 vcpu->arch.vcore = vcore;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001379 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001380
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001381 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1382 kvmppc_sanity_check(vcpu);
1383
Paul Mackerrasde56a942011-06-29 00:21:34 +00001384 return vcpu;
1385
1386free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001387 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001388out:
1389 return ERR_PTR(err);
1390}
1391
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001392static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1393{
1394 if (vpa->pinned_addr)
1395 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1396 vpa->dirty);
1397}
1398
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301399static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001400{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001401 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001402 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1403 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1404 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001405 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001406 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001407 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001408}
1409
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301410static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1411{
1412 /* Indicate we want to get back into the guest */
1413 return 1;
1414}
1415
Paul Mackerras19ccb762011-07-23 17:42:46 +10001416static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001417{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001418 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001419
Paul Mackerras19ccb762011-07-23 17:42:46 +10001420 now = get_tb();
1421 if (now > vcpu->arch.dec_expires) {
1422 /* decrementer has already gone negative */
1423 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001424 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001425 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001426 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001427 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1428 / tb_ticks_per_sec;
1429 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1430 HRTIMER_MODE_REL);
1431 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001432}
1433
Paul Mackerras19ccb762011-07-23 17:42:46 +10001434static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001435{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001436 vcpu->arch.ceded = 0;
1437 if (vcpu->arch.timer_running) {
1438 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1439 vcpu->arch.timer_running = 0;
1440 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001441}
1442
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001443extern void __kvmppc_vcore_entry(void);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001444
1445static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1446 struct kvm_vcpu *vcpu)
1447{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001448 u64 now;
1449
Paul Mackerras371fefd2011-06-29 00:23:08 +00001450 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1451 return;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001452 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001453 now = mftb();
1454 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1455 vcpu->arch.stolen_logged;
1456 vcpu->arch.busy_preempt = now;
1457 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001458 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001459 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001460 list_del(&vcpu->arch.run_list);
1461}
1462
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001463static int kvmppc_grab_hwthread(int cpu)
1464{
1465 struct paca_struct *tpaca;
1466 long timeout = 1000;
1467
1468 tpaca = &paca[cpu];
1469
1470 /* Ensure the thread won't go into the kernel if it wakes */
1471 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001472 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001473
1474 /*
1475 * If the thread is already executing in the kernel (e.g. handling
1476 * a stray interrupt), wait for it to get back to nap mode.
1477 * The smp_mb() is to ensure that our setting of hwthread_req
1478 * is visible before we look at hwthread_state, so if this
1479 * races with the code at system_reset_pSeries and the thread
1480 * misses our setting of hwthread_req, we are sure to see its
1481 * setting of hwthread_state, and vice versa.
1482 */
1483 smp_mb();
1484 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1485 if (--timeout <= 0) {
1486 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1487 return -EBUSY;
1488 }
1489 udelay(1);
1490 }
1491 return 0;
1492}
1493
1494static void kvmppc_release_hwthread(int cpu)
1495{
1496 struct paca_struct *tpaca;
1497
1498 tpaca = &paca[cpu];
1499 tpaca->kvm_hstate.hwthread_req = 0;
1500 tpaca->kvm_hstate.kvm_vcpu = NULL;
1501}
1502
Paul Mackerras371fefd2011-06-29 00:23:08 +00001503static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1504{
1505 int cpu;
1506 struct paca_struct *tpaca;
1507 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1508
Paul Mackerras19ccb762011-07-23 17:42:46 +10001509 if (vcpu->arch.timer_running) {
1510 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1511 vcpu->arch.timer_running = 0;
1512 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001513 cpu = vc->pcpu + vcpu->arch.ptid;
1514 tpaca = &paca[cpu];
1515 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1516 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001517 tpaca->kvm_hstate.ptid = vcpu->arch.ptid;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001518 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001519 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +00001520#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001521 if (cpu != smp_processor_id()) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001522 xics_wake_cpu(cpu);
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001523 if (vcpu->arch.ptid)
1524 ++vc->n_woken;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001525 }
1526#endif
1527}
1528
1529static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1530{
1531 int i;
1532
1533 HMT_low();
1534 i = 0;
1535 while (vc->nap_count < vc->n_woken) {
1536 if (++i >= 1000000) {
1537 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1538 vc->nap_count, vc->n_woken);
1539 break;
1540 }
1541 cpu_relax();
1542 }
1543 HMT_medium();
1544}
1545
1546/*
1547 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001548 * this core are off-line. Then grab the threads so they can't
1549 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001550 */
1551static int on_primary_thread(void)
1552{
1553 int cpu = smp_processor_id();
Michael Ellerman3102f782014-05-23 18:15:29 +10001554 int thr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001555
Michael Ellerman3102f782014-05-23 18:15:29 +10001556 /* Are we on a primary subcore? */
1557 if (cpu_thread_in_subcore(cpu))
Paul Mackerras371fefd2011-06-29 00:23:08 +00001558 return 0;
Michael Ellerman3102f782014-05-23 18:15:29 +10001559
1560 thr = 0;
1561 while (++thr < threads_per_subcore)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001562 if (cpu_online(cpu + thr))
1563 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001564
1565 /* Grab all hw threads so they can't go into the kernel */
Michael Ellerman3102f782014-05-23 18:15:29 +10001566 for (thr = 1; thr < threads_per_subcore; ++thr) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00001567 if (kvmppc_grab_hwthread(cpu + thr)) {
1568 /* Couldn't grab one; let the others go */
1569 do {
1570 kvmppc_release_hwthread(cpu + thr);
1571 } while (--thr > 0);
1572 return 0;
1573 }
1574 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001575 return 1;
1576}
1577
1578/*
1579 * Run a set of guest threads on a physical core.
1580 * Called with vc->lock held.
1581 */
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001582static void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001583{
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001584 struct kvm_vcpu *vcpu, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001585 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001586 u64 now;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001587 int i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001588 int srcu_idx;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001589 struct kvm_vcpu *vcpus_to_update[threads_per_core];
Paul Mackerrasde56a942011-06-29 00:21:34 +00001590
Paul Mackerras371fefd2011-06-29 00:23:08 +00001591 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +10001592 need_vpa_update = 0;
1593 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001594 if (signal_pending(vcpu->arch.run_task))
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001595 return;
1596 if (vcpu->arch.vpa.update_pending ||
1597 vcpu->arch.slb_shadow.update_pending ||
1598 vcpu->arch.dtl.update_pending)
1599 vcpus_to_update[need_vpa_update++] = vcpu;
Paul Mackerras081f3232012-06-01 20:20:24 +10001600 }
1601
1602 /*
1603 * Initialize *vc, in particular vc->vcore_state, so we can
1604 * drop the vcore lock if necessary.
1605 */
1606 vc->n_woken = 0;
1607 vc->nap_count = 0;
1608 vc->entry_exit_count = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001609 vc->vcore_state = VCORE_STARTING;
Paul Mackerras081f3232012-06-01 20:20:24 +10001610 vc->in_guest = 0;
1611 vc->napping_threads = 0;
1612
1613 /*
1614 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1615 * which can't be called with any spinlocks held.
1616 */
1617 if (need_vpa_update) {
1618 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001619 for (i = 0; i < need_vpa_update; ++i)
1620 kvmppc_update_vpas(vcpus_to_update[i]);
Paul Mackerras081f3232012-06-01 20:20:24 +10001621 spin_lock(&vc->lock);
1622 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001623
1624 /*
Michael Ellerman3102f782014-05-23 18:15:29 +10001625 * Make sure we are running on primary threads, and that secondary
1626 * threads are offline. Also check if the number of threads in this
1627 * guest are greater than the current system threads per guest.
Paul Mackerras7b444c62012-10-15 01:16:14 +00001628 */
Michael Ellerman3102f782014-05-23 18:15:29 +10001629 if ((threads_per_core > 1) &&
1630 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00001631 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1632 vcpu->arch.ret = -EBUSY;
1633 goto out;
1634 }
1635
Michael Ellerman3102f782014-05-23 18:15:29 +10001636
Paul Mackerras371fefd2011-06-29 00:23:08 +00001637 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001638 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001639 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001640 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001641 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001642
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001643 /* Set this explicitly in case thread 0 doesn't have a vcpu */
1644 get_paca()->kvm_hstate.kvm_vcore = vc;
1645 get_paca()->kvm_hstate.ptid = 0;
1646
Paul Mackerras2f12f032012-10-15 01:17:17 +00001647 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001648 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001649 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001650
Paul Mackerras19ccb762011-07-23 17:42:46 +10001651 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001652
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001653 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001654
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001655 __kvmppc_vcore_entry();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001656
Paul Mackerras371fefd2011-06-29 00:23:08 +00001657 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001658 /* disable sending of IPIs on virtual external irqs */
1659 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1660 vcpu->cpu = -1;
1661 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001662 if (vc->nap_count < vc->n_woken)
1663 kvmppc_wait_for_nap(vc);
Michael Ellerman3102f782014-05-23 18:15:29 +10001664 for (i = 0; i < threads_per_subcore; ++i)
Paul Mackerras2f12f032012-10-15 01:17:17 +00001665 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001666 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001667 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001668 spin_unlock(&vc->lock);
1669
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001670 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001671
Paul Mackerras371fefd2011-06-29 00:23:08 +00001672 /* make sure updates to secondary vcpu structs are visible now */
1673 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001674 kvm_guest_exit();
1675
1676 preempt_enable();
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09001677 cond_resched();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001678
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001679 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001680 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001681 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1682 /* cancel pending dec exception if dec is positive */
1683 if (now < vcpu->arch.dec_expires &&
1684 kvmppc_core_pending_dec(vcpu))
1685 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001686
1687 ret = RESUME_GUEST;
1688 if (vcpu->arch.trap)
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301689 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1690 vcpu->arch.run_task);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001691
Paul Mackerras371fefd2011-06-29 00:23:08 +00001692 vcpu->arch.ret = ret;
1693 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001694
1695 if (vcpu->arch.ceded) {
Greg Kurze59d24e2014-02-06 17:36:56 +01001696 if (!is_kvmppc_resume_guest(ret))
Paul Mackerras19ccb762011-07-23 17:42:46 +10001697 kvmppc_end_cede(vcpu);
1698 else
1699 kvmppc_set_timer(vcpu);
1700 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001701 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001702
Paul Mackerrasde56a942011-06-29 00:21:34 +00001703 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001704 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001705 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1706 arch.run_list) {
Greg Kurze59d24e2014-02-06 17:36:56 +01001707 if (!is_kvmppc_resume_guest(vcpu->arch.ret)) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001708 kvmppc_remove_runnable(vc, vcpu);
1709 wake_up(&vcpu->arch.cpu_run);
1710 }
1711 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001712}
1713
Paul Mackerras19ccb762011-07-23 17:42:46 +10001714/*
1715 * Wait for some other vcpu thread to execute us, and
1716 * wake us up when we need to handle something in the host.
1717 */
1718static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001719{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001720 DEFINE_WAIT(wait);
1721
Paul Mackerras19ccb762011-07-23 17:42:46 +10001722 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1723 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1724 schedule();
1725 finish_wait(&vcpu->arch.cpu_run, &wait);
1726}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001727
Paul Mackerras19ccb762011-07-23 17:42:46 +10001728/*
1729 * All the vcpus in this vcore are idle, so wait for a decrementer
1730 * or external interrupt to one of the vcpus. vc->lock is held.
1731 */
1732static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1733{
1734 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001735
1736 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1737 vc->vcore_state = VCORE_SLEEPING;
1738 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001739 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001740 finish_wait(&vc->wq, &wait);
1741 spin_lock(&vc->lock);
1742 vc->vcore_state = VCORE_INACTIVE;
1743}
1744
1745static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1746{
1747 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001748 struct kvmppc_vcore *vc;
1749 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001750
Paul Mackerras371fefd2011-06-29 00:23:08 +00001751 kvm_run->exit_reason = 0;
1752 vcpu->arch.ret = RESUME_GUEST;
1753 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001754 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001755
Paul Mackerras371fefd2011-06-29 00:23:08 +00001756 /*
1757 * Synchronize with other threads in this virtual core
1758 */
1759 vc = vcpu->arch.vcore;
1760 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001761 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001762 vcpu->arch.run_task = current;
1763 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001764 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10001765 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001766 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001767 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1768 ++vc->n_runnable;
1769
Paul Mackerras19ccb762011-07-23 17:42:46 +10001770 /*
1771 * This happens the first time this is called for a vcpu.
1772 * If the vcore is already running, we may be able to start
1773 * this thread straight away and have it join in.
1774 */
Paul Mackerras8455d792012-10-15 01:17:42 +00001775 if (!signal_pending(current)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001776 if (vc->vcore_state == VCORE_RUNNING &&
1777 VCORE_EXIT_COUNT(vc) == 0) {
Paul Mackerras2f12f032012-10-15 01:17:17 +00001778 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001779 kvmppc_start_thread(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00001780 } else if (vc->vcore_state == VCORE_SLEEPING) {
1781 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001782 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001783
Paul Mackerras8455d792012-10-15 01:17:42 +00001784 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001785
1786 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1787 !signal_pending(current)) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001788 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001789 spin_unlock(&vc->lock);
1790 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1791 spin_lock(&vc->lock);
1792 continue;
1793 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001794 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1795 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001796 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001797 if (signal_pending(v->arch.run_task)) {
1798 kvmppc_remove_runnable(vc, v);
1799 v->stat.signal_exits++;
1800 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1801 v->arch.ret = -EINTR;
1802 wake_up(&v->arch.cpu_run);
1803 }
1804 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001805 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1806 break;
1807 vc->runner = vcpu;
1808 n_ceded = 0;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001809 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001810 if (!v->arch.pending_exceptions)
1811 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001812 else
1813 v->arch.ceded = 0;
1814 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001815 if (n_ceded == vc->n_runnable)
1816 kvmppc_vcore_blocked(vc);
1817 else
1818 kvmppc_run_core(vc);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001819 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001820 }
1821
Paul Mackerras8455d792012-10-15 01:17:42 +00001822 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1823 (vc->vcore_state == VCORE_RUNNING ||
1824 vc->vcore_state == VCORE_EXITING)) {
1825 spin_unlock(&vc->lock);
1826 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1827 spin_lock(&vc->lock);
1828 }
1829
1830 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1831 kvmppc_remove_runnable(vc, vcpu);
1832 vcpu->stat.signal_exits++;
1833 kvm_run->exit_reason = KVM_EXIT_INTR;
1834 vcpu->arch.ret = -EINTR;
1835 }
1836
1837 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1838 /* Wake up some vcpu to run the core */
1839 v = list_first_entry(&vc->runnable_threads,
1840 struct kvm_vcpu, arch.run_list);
1841 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001842 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001843
Paul Mackerras19ccb762011-07-23 17:42:46 +10001844 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001845 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001846}
1847
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301848static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001849{
1850 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001851 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001852
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001853 if (!vcpu->arch.sane) {
1854 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1855 return -EINVAL;
1856 }
1857
Scott Wood25051b52011-11-08 18:23:23 -06001858 kvmppc_core_prepare_to_enter(vcpu);
1859
Paul Mackerras19ccb762011-07-23 17:42:46 +10001860 /* No need to go into the guest when all we'll do is come back out */
1861 if (signal_pending(current)) {
1862 run->exit_reason = KVM_EXIT_INTR;
1863 return -EINTR;
1864 }
1865
Paul Mackerras32fad282012-05-04 02:32:53 +00001866 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1867 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1868 smp_mb();
1869
1870 /* On the first time here, set up HTAB and VRMA or RMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001871 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001872 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001873 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001874 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001875 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001876
1877 flush_fp_to_thread(current);
1878 flush_altivec_to_thread(current);
1879 flush_vsx_to_thread(current);
1880 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001881 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001882 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001883
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001884 do {
1885 r = kvmppc_run_vcpu(run, vcpu);
1886
1887 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1888 !(vcpu->arch.shregs.msr & MSR_PR)) {
1889 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001890 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001891 } else if (r == RESUME_PAGE_FAULT) {
1892 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1893 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1894 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1895 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001896 }
Greg Kurze59d24e2014-02-06 17:36:56 +01001897 } while (is_kvmppc_resume_guest(r));
Paul Mackerras32fad282012-05-04 02:32:53 +00001898
1899 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001900 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00001901 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001902 return r;
1903}
1904
David Gibson54738c02011-06-29 00:22:41 +00001905
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001906/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +00001907 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001908static inline int lpcr_rmls(unsigned long rma_size)
1909{
1910 switch (rma_size) {
1911 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001912 if (cpu_has_feature(CPU_FTR_ARCH_206))
1913 return 8; /* only supported on POWER7 */
1914 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001915 case 64ul << 20: /* 64 MB */
1916 return 3;
1917 case 128ul << 20: /* 128 MB */
1918 return 7;
1919 case 256ul << 20: /* 256 MB */
1920 return 4;
1921 case 1ul << 30: /* 1 GB */
1922 return 2;
1923 case 16ul << 30: /* 16 GB */
1924 return 1;
1925 case 256ul << 30: /* 256 GB */
1926 return 0;
1927 default:
1928 return -1;
1929 }
1930}
1931
1932static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1933{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001934 struct page *page;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301935 struct kvm_rma_info *ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001936
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301937 if (vmf->pgoff >= kvm_rma_pages)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001938 return VM_FAULT_SIGBUS;
1939
1940 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1941 get_page(page);
1942 vmf->page = page;
1943 return 0;
1944}
1945
1946static const struct vm_operations_struct kvm_rma_vm_ops = {
1947 .fault = kvm_rma_fault,
1948};
1949
1950static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1951{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001952 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001953 vma->vm_ops = &kvm_rma_vm_ops;
1954 return 0;
1955}
1956
1957static int kvm_rma_release(struct inode *inode, struct file *filp)
1958{
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301959 struct kvm_rma_info *ri = filp->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001960
1961 kvm_release_rma(ri);
1962 return 0;
1963}
1964
Al Viro75ef9de2013-04-04 19:09:41 -04001965static const struct file_operations kvm_rma_fops = {
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001966 .mmap = kvm_rma_mmap,
1967 .release = kvm_rma_release,
1968};
1969
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301970static long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
1971 struct kvm_allocate_rma *ret)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001972{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001973 long fd;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301974 struct kvm_rma_info *ri;
1975 /*
1976 * Only do this on PPC970 in HV mode
1977 */
1978 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
1979 !cpu_has_feature(CPU_FTR_ARCH_201))
1980 return -EINVAL;
1981
1982 if (!kvm_rma_pages)
1983 return -EINVAL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001984
1985 ri = kvm_alloc_rma();
1986 if (!ri)
1987 return -ENOMEM;
1988
Yann Droneaud2f84d5e2013-08-24 22:14:08 +02001989 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001990 if (fd < 0)
1991 kvm_release_rma(ri);
1992
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301993 ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001994 return fd;
1995}
1996
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001997static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1998 int linux_psize)
1999{
2000 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2001
2002 if (!def->shift)
2003 return;
2004 (*sps)->page_shift = def->shift;
2005 (*sps)->slb_enc = def->sllp;
2006 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00002007 /*
2008 * Only return base page encoding. We don't want to return
2009 * all the supporting pte_enc, because our H_ENTER doesn't
2010 * support MPSS yet. Once they do, we can start passing all
2011 * support pte_enc here
2012 */
2013 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05302014 /*
2015 * Add 16MB MPSS support if host supports it
2016 */
2017 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2018 (*sps)->enc[1].page_shift = 24;
2019 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2020 }
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002021 (*sps)++;
2022}
2023
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302024static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2025 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002026{
2027 struct kvm_ppc_one_seg_page_size *sps;
2028
2029 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2030 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2031 info->flags |= KVM_PPC_1T_SEGMENTS;
2032 info->slb_size = mmu_slb_size;
2033
2034 /* We only support these sizes for now, and no muti-size segments */
2035 sps = &info->sps[0];
2036 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2037 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2038 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2039
2040 return 0;
2041}
2042
Paul Mackerras82ed3612011-12-15 02:03:22 +00002043/*
2044 * Get (and clear) the dirty memory log for a memory slot.
2045 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302046static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2047 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002048{
2049 struct kvm_memory_slot *memslot;
2050 int r;
2051 unsigned long n;
2052
2053 mutex_lock(&kvm->slots_lock);
2054
2055 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07002056 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002057 goto out;
2058
2059 memslot = id_to_memslot(kvm->memslots, log->slot);
2060 r = -ENOENT;
2061 if (!memslot->dirty_bitmap)
2062 goto out;
2063
2064 n = kvm_dirty_bitmap_bytes(memslot);
2065 memset(memslot->dirty_bitmap, 0, n);
2066
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002067 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00002068 if (r)
2069 goto out;
2070
2071 r = -EFAULT;
2072 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2073 goto out;
2074
2075 r = 0;
2076out:
2077 mutex_unlock(&kvm->slots_lock);
2078 return r;
2079}
2080
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002081static void unpin_slot(struct kvm_memory_slot *memslot)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002082{
2083 unsigned long *physp;
2084 unsigned long j, npages, pfn;
2085 struct page *page;
2086
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002087 physp = memslot->arch.slot_phys;
2088 npages = memslot->npages;
2089 if (!physp)
2090 return;
2091 for (j = 0; j < npages; j++) {
2092 if (!(physp[j] & KVMPPC_GOT_PAGE))
2093 continue;
2094 pfn = physp[j] >> PAGE_SHIFT;
2095 page = pfn_to_page(pfn);
2096 SetPageDirty(page);
2097 put_page(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002098 }
2099}
2100
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302101static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2102 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002103{
2104 if (!dont || free->arch.rmap != dont->arch.rmap) {
2105 vfree(free->arch.rmap);
2106 free->arch.rmap = NULL;
2107 }
2108 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
2109 unpin_slot(free);
2110 vfree(free->arch.slot_phys);
2111 free->arch.slot_phys = NULL;
2112 }
2113}
2114
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302115static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
2116 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002117{
2118 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
2119 if (!slot->arch.rmap)
2120 return -ENOMEM;
2121 slot->arch.slot_phys = NULL;
2122
2123 return 0;
2124}
2125
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302126static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
2127 struct kvm_memory_slot *memslot,
2128 struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002129{
2130 unsigned long *phys;
2131
2132 /* Allocate a slot_phys array if needed */
2133 phys = memslot->arch.slot_phys;
2134 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
2135 phys = vzalloc(memslot->npages * sizeof(unsigned long));
2136 if (!phys)
2137 return -ENOMEM;
2138 memslot->arch.slot_phys = phys;
2139 }
2140
2141 return 0;
2142}
2143
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302144static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
2145 struct kvm_userspace_memory_region *mem,
2146 const struct kvm_memory_slot *old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002147{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002148 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
2149 struct kvm_memory_slot *memslot;
2150
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002151 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002152 /*
2153 * If modifying a memslot, reset all the rmap dirty bits.
2154 * If this is a new memslot, we don't need to do anything
2155 * since the rmap array starts out as all zeroes,
2156 * i.e. no pages are dirty.
2157 */
2158 memslot = id_to_memslot(kvm->memslots, mem->slot);
2159 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
2160 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002161}
2162
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002163/*
2164 * Update LPCR values in kvm->arch and in vcores.
2165 * Caller must hold kvm->lock.
2166 */
2167void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
2168{
2169 long int i;
2170 u32 cores_done = 0;
2171
2172 if ((kvm->arch.lpcr & mask) == lpcr)
2173 return;
2174
2175 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2176
2177 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2178 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2179 if (!vc)
2180 continue;
2181 spin_lock(&vc->lock);
2182 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2183 spin_unlock(&vc->lock);
2184 if (++cores_done >= kvm->arch.online_vcores)
2185 break;
2186 }
2187}
2188
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302189static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2190{
2191 return;
2192}
2193
Paul Mackerras32fad282012-05-04 02:32:53 +00002194static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002195{
2196 int err = 0;
2197 struct kvm *kvm = vcpu->kvm;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302198 struct kvm_rma_info *ri = NULL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002199 unsigned long hva;
2200 struct kvm_memory_slot *memslot;
2201 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002202 unsigned long lpcr = 0, senc;
2203 unsigned long lpcr_mask = 0;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002204 unsigned long psize, porder;
2205 unsigned long rma_size;
2206 unsigned long rmls;
2207 unsigned long *physp;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002208 unsigned long i, npages;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002209 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002210
2211 mutex_lock(&kvm->lock);
2212 if (kvm->arch.rma_setup_done)
2213 goto out; /* another vcpu beat us to it */
2214
Paul Mackerras32fad282012-05-04 02:32:53 +00002215 /* Allocate hashed page table (if not done already) and reset it */
2216 if (!kvm->arch.hpt_virt) {
2217 err = kvmppc_alloc_hpt(kvm, NULL);
2218 if (err) {
2219 pr_err("KVM: Couldn't alloc HPT\n");
2220 goto out;
2221 }
2222 }
2223
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002224 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002225 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002226 memslot = gfn_to_memslot(kvm, 0);
2227
2228 /* We must have some memory at 0 by now */
2229 err = -EINVAL;
2230 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002231 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002232
2233 /* Look up the VMA for the start of this memory slot */
2234 hva = memslot->userspace_addr;
2235 down_read(&current->mm->mmap_sem);
2236 vma = find_vma(current->mm, hva);
2237 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2238 goto up_out;
2239
2240 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002241 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002242
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002243 /* Is this one of our preallocated RMAs? */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002244 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
2245 hva == vma->vm_start)
2246 ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002247
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002248 up_read(&current->mm->mmap_sem);
2249
2250 if (!ri) {
2251 /* On POWER7, use VRMA; on PPC970, give up */
2252 err = -EPERM;
2253 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2254 pr_err("KVM: CPU requires an RMO\n");
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002255 goto out_srcu;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002256 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002257
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002258 /* We can handle 4k, 64k or 16M pages in the VRMA */
2259 err = -EINVAL;
2260 if (!(psize == 0x1000 || psize == 0x10000 ||
2261 psize == 0x1000000))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002262 goto out_srcu;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002263
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002264 /* Update VRMASD field in the LPCR */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002265 senc = slb_pgsize_encoding(psize);
Paul Mackerras697d3892011-12-12 12:36:37 +00002266 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2267 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002268 lpcr_mask = LPCR_VRMASD;
2269 /* the -4 is to account for senc values starting at 0x10 */
2270 lpcr = senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002271
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002272 /* Create HPTEs in the hash page table for the VRMA */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002273 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002274
2275 } else {
2276 /* Set up to use an RMO region */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302277 rma_size = kvm_rma_pages;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002278 if (rma_size > memslot->npages)
2279 rma_size = memslot->npages;
2280 rma_size <<= PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002281 rmls = lpcr_rmls(rma_size);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002282 err = -EINVAL;
Chen Gang5d226ae2013-07-22 14:32:35 +08002283 if ((long)rmls < 0) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002284 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002285 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002286 }
2287 atomic_inc(&ri->use_count);
2288 kvm->arch.rma = ri;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002289
2290 /* Update LPCR and RMOR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00002291 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2292 /* PPC970; insert RMLS value (split field) in HID4 */
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002293 lpcr_mask = (1ul << HID4_RMLS0_SH) |
2294 (3ul << HID4_RMLS2_SH) | HID4_RMOR;
2295 lpcr = ((rmls >> 2) << HID4_RMLS0_SH) |
Paul Mackerras9e368f22011-06-29 00:40:08 +00002296 ((rmls & 3) << HID4_RMLS2_SH);
2297 /* RMOR is also in HID4 */
2298 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
2299 << HID4_RMOR_SH;
2300 } else {
2301 /* POWER7 */
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002302 lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS;
2303 lpcr = rmls << LPCR_RMLS_SH;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302304 kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002305 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002306 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002307 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002308
2309 /* Initialize phys addrs of pages in RMO */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05302310 npages = kvm_rma_pages;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002311 porder = __ilog2(npages);
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002312 physp = memslot->arch.slot_phys;
2313 if (physp) {
2314 if (npages > memslot->npages)
2315 npages = memslot->npages;
2316 spin_lock(&kvm->arch.slot_phys_lock);
2317 for (i = 0; i < npages; ++i)
2318 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
2319 porder;
2320 spin_unlock(&kvm->arch.slot_phys_lock);
2321 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002322 }
2323
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002324 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
2325
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002326 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
2327 smp_wmb();
2328 kvm->arch.rma_setup_done = 1;
2329 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002330 out_srcu:
2331 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002332 out:
2333 mutex_unlock(&kvm->lock);
2334 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002335
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002336 up_out:
2337 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08002338 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002339}
2340
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302341static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002342{
Paul Mackerras32fad282012-05-04 02:32:53 +00002343 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002344
Paul Mackerras32fad282012-05-04 02:32:53 +00002345 /* Allocate the guest's logical partition ID */
2346
2347 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08002348 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00002349 return -ENOMEM;
2350 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002351
Paul Mackerras1b400ba2012-11-21 23:28:08 +00002352 /*
2353 * Since we don't flush the TLB when tearing down a VM,
2354 * and this lpid might have previously been used,
2355 * make sure we flush on each core before running the new VM.
2356 */
2357 cpumask_setall(&kvm->arch.need_tlb_flush);
2358
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002359 /* Start out with the default set of hcalls enabled */
2360 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
2361 sizeof(kvm->arch.enabled_hcalls));
2362
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002363 kvm->arch.rma = NULL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002364
Paul Mackerras9e368f22011-06-29 00:40:08 +00002365 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002366
Paul Mackerras9e368f22011-06-29 00:40:08 +00002367 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2368 /* PPC970; HID4 is effectively the LPCR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00002369 kvm->arch.host_lpid = 0;
2370 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
2371 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
2372 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
2373 ((lpid & 0xf) << HID4_LPID5_SH);
2374 } else {
2375 /* POWER7; init LPCR for virtual RMA mode */
2376 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2377 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2378 lpcr &= LPCR_PECE | LPCR_LPES;
2379 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
Paul Mackerras697d3892011-12-12 12:36:37 +00002380 LPCR_VPM0 | LPCR_VPM1;
2381 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2382 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrase0622bd2014-01-08 21:25:27 +11002383 /* On POWER8 turn on online bit to enable PURR/SPURR */
2384 if (cpu_has_feature(CPU_FTR_ARCH_207S))
2385 lpcr |= LPCR_ONL;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002386 }
2387 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002388
Paul Mackerras342d3db2011-12-12 12:38:05 +00002389 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002390 spin_lock_init(&kvm->arch.slot_phys_lock);
Paul Mackerras512691d2012-10-15 01:15:41 +00002391
2392 /*
Michael Ellerman441c19c2014-05-23 18:15:25 +10002393 * Track that we now have a HV mode VM active. This blocks secondary
2394 * CPU threads from coming online.
Paul Mackerras512691d2012-10-15 01:15:41 +00002395 */
Michael Ellerman441c19c2014-05-23 18:15:25 +10002396 kvm_hv_vm_activated();
Paul Mackerras512691d2012-10-15 01:15:41 +00002397
David Gibson54738c02011-06-29 00:22:41 +00002398 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002399}
2400
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302401static void kvmppc_free_vcores(struct kvm *kvm)
2402{
2403 long int i;
2404
2405 for (i = 0; i < KVM_MAX_VCORES; ++i)
2406 kfree(kvm->arch.vcores[i]);
2407 kvm->arch.online_vcores = 0;
2408}
2409
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302410static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002411{
Michael Ellerman441c19c2014-05-23 18:15:25 +10002412 kvm_hv_vm_deactivated();
Paul Mackerras512691d2012-10-15 01:15:41 +00002413
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302414 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002415 if (kvm->arch.rma) {
2416 kvm_release_rma(kvm->arch.rma);
2417 kvm->arch.rma = NULL;
2418 }
2419
Paul Mackerrasde56a942011-06-29 00:21:34 +00002420 kvmppc_free_hpt(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002421}
2422
2423/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302424static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2425 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002426{
2427 return EMULATE_FAIL;
2428}
2429
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302430static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2431 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002432{
2433 return EMULATE_FAIL;
2434}
2435
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302436static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2437 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002438{
2439 return EMULATE_FAIL;
2440}
2441
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302442static int kvmppc_core_check_processor_compat_hv(void)
2443{
2444 if (!cpu_has_feature(CPU_FTR_HVMODE))
2445 return -EIO;
2446 return 0;
2447}
2448
2449static long kvm_arch_vm_ioctl_hv(struct file *filp,
2450 unsigned int ioctl, unsigned long arg)
2451{
2452 struct kvm *kvm __maybe_unused = filp->private_data;
2453 void __user *argp = (void __user *)arg;
2454 long r;
2455
2456 switch (ioctl) {
2457
2458 case KVM_ALLOCATE_RMA: {
2459 struct kvm_allocate_rma rma;
2460 struct kvm *kvm = filp->private_data;
2461
2462 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
2463 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
2464 r = -EFAULT;
2465 break;
2466 }
2467
2468 case KVM_PPC_ALLOCATE_HTAB: {
2469 u32 htab_order;
2470
2471 r = -EFAULT;
2472 if (get_user(htab_order, (u32 __user *)argp))
2473 break;
2474 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2475 if (r)
2476 break;
2477 r = -EFAULT;
2478 if (put_user(htab_order, (u32 __user *)argp))
2479 break;
2480 r = 0;
2481 break;
2482 }
2483
2484 case KVM_PPC_GET_HTAB_FD: {
2485 struct kvm_get_htab_fd ghf;
2486
2487 r = -EFAULT;
2488 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2489 break;
2490 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2491 break;
2492 }
2493
2494 default:
2495 r = -ENOTTY;
2496 }
2497
2498 return r;
2499}
2500
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002501/*
2502 * List of hcall numbers to enable by default.
2503 * For compatibility with old userspace, we enable by default
2504 * all hcalls that were implemented before the hcall-enabling
2505 * facility was added. Note this list should not include H_RTAS.
2506 */
2507static unsigned int default_hcall_list[] = {
2508 H_REMOVE,
2509 H_ENTER,
2510 H_READ,
2511 H_PROTECT,
2512 H_BULK_REMOVE,
2513 H_GET_TCE,
2514 H_PUT_TCE,
2515 H_SET_DABR,
2516 H_SET_XDABR,
2517 H_CEDE,
2518 H_PROD,
2519 H_CONFER,
2520 H_REGISTER_VPA,
2521#ifdef CONFIG_KVM_XICS
2522 H_EOI,
2523 H_CPPR,
2524 H_IPI,
2525 H_IPOLL,
2526 H_XIRR,
2527 H_XIRR_X,
2528#endif
2529 0
2530};
2531
2532static void init_default_hcalls(void)
2533{
2534 int i;
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002535 unsigned int hcall;
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002536
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002537 for (i = 0; default_hcall_list[i]; ++i) {
2538 hcall = default_hcall_list[i];
2539 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
2540 __set_bit(hcall / 4, default_enabled_hcalls);
2541 }
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002542}
2543
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302544static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302545 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2546 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2547 .get_one_reg = kvmppc_get_one_reg_hv,
2548 .set_one_reg = kvmppc_set_one_reg_hv,
2549 .vcpu_load = kvmppc_core_vcpu_load_hv,
2550 .vcpu_put = kvmppc_core_vcpu_put_hv,
2551 .set_msr = kvmppc_set_msr_hv,
2552 .vcpu_run = kvmppc_vcpu_run_hv,
2553 .vcpu_create = kvmppc_core_vcpu_create_hv,
2554 .vcpu_free = kvmppc_core_vcpu_free_hv,
2555 .check_requests = kvmppc_core_check_requests_hv,
2556 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2557 .flush_memslot = kvmppc_core_flush_memslot_hv,
2558 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2559 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2560 .unmap_hva = kvm_unmap_hva_hv,
2561 .unmap_hva_range = kvm_unmap_hva_range_hv,
2562 .age_hva = kvm_age_hva_hv,
2563 .test_age_hva = kvm_test_age_hva_hv,
2564 .set_spte_hva = kvm_set_spte_hva_hv,
2565 .mmu_destroy = kvmppc_mmu_destroy_hv,
2566 .free_memslot = kvmppc_core_free_memslot_hv,
2567 .create_memslot = kvmppc_core_create_memslot_hv,
2568 .init_vm = kvmppc_core_init_vm_hv,
2569 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302570 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2571 .emulate_op = kvmppc_core_emulate_op_hv,
2572 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2573 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2574 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2575 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002576 .hcall_implemented = kvmppc_hcall_impl_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302577};
2578
2579static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002580{
2581 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302582 /*
2583 * FIXME!! Do we need to check on all cpus ?
2584 */
2585 r = kvmppc_core_check_processor_compat_hv();
2586 if (r < 0)
Paul Mackerras739e2422014-03-25 10:47:05 +11002587 return -ENODEV;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002588
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302589 kvm_ops_hv.owner = THIS_MODULE;
2590 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002591
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002592 init_default_hcalls();
2593
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302594 r = kvmppc_mmu_hv_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002595 return r;
2596}
2597
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302598static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002599{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302600 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002601}
2602
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302603module_init(kvmppc_book3s_init_hv);
2604module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05302605MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01002606MODULE_ALIAS_MISCDEV(KVM_MINOR);
2607MODULE_ALIAS("devname:kvm");