blob: b51d5db780684ea5dcb3b6dae5c5bc70224dca43 [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>
Paul Mackerrasde56a942011-06-29 00:21:34 +000034
35#include <asm/reg.h>
36#include <asm/cputable.h>
37#include <asm/cacheflush.h>
38#include <asm/tlbflush.h>
39#include <asm/uaccess.h>
40#include <asm/io.h>
41#include <asm/kvm_ppc.h>
42#include <asm/kvm_book3s.h>
43#include <asm/mmu_context.h>
44#include <asm/lppaca.h>
45#include <asm/processor.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000046#include <asm/cputhreads.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000047#include <asm/page.h>
Michael Neulingde1d9242011-11-09 20:39:49 +000048#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010049#include <asm/switch_to.h>
Paul Mackerras512691d2012-10-15 01:15:41 +000050#include <asm/smp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000051#include <linux/gfp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000052#include <linux/vmalloc.h>
53#include <linux/highmem.h>
Paul Mackerrasc77162d2011-12-12 12:31:00 +000054#include <linux/hugetlb.h>
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +053055#include <linux/module.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000056
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053057#include "book3s.h"
58
Paul Mackerrasde56a942011-06-29 00:21:34 +000059/* #define EXIT_DEBUG */
60/* #define EXIT_DEBUG_SIMPLE */
61/* #define EXIT_DEBUG_INT */
62
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000063/* Used to indicate that a guest page fault needs to be handled */
64#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
65
Paul Mackerrasc7b67672012-10-15 01:18:07 +000066/* Used as a "null" value for timebase values */
67#define TB_NIL (~(u64)0)
68
Paul Mackerras19ccb762011-07-23 17:42:46 +100069static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +000070static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +100071
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053072static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +000073{
74 int me;
75 int cpu = vcpu->cpu;
76 wait_queue_head_t *wqp;
77
78 wqp = kvm_arch_vcpu_wq(vcpu);
79 if (waitqueue_active(wqp)) {
80 wake_up_interruptible(wqp);
81 ++vcpu->stat.halt_wakeup;
82 }
83
84 me = get_cpu();
85
86 /* CPU points to the first thread of the core */
87 if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
88 int real_cpu = cpu + vcpu->arch.ptid;
89 if (paca[real_cpu].kvm_hstate.xics_phys)
90 xics_wake_cpu(real_cpu);
91 else if (cpu_online(cpu))
92 smp_send_reschedule(cpu);
93 }
94 put_cpu();
95}
96
Paul Mackerrasc7b67672012-10-15 01:18:07 +000097/*
98 * We use the vcpu_load/put functions to measure stolen time.
99 * Stolen time is counted as time when either the vcpu is able to
100 * run as part of a virtual core, but the task running the vcore
101 * is preempted or sleeping, or when the vcpu needs something done
102 * in the kernel by the task running the vcpu, but that task is
103 * preempted or sleeping. Those two things have to be counted
104 * separately, since one of the vcpu tasks will take on the job
105 * of running the core, and the other vcpu tasks in the vcore will
106 * sleep waiting for it to do that, but that sleep shouldn't count
107 * as stolen time.
108 *
109 * Hence we accumulate stolen time when the vcpu can run as part of
110 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
111 * needs its task to do other things in the kernel (for example,
112 * service a page fault) in busy_stolen. We don't accumulate
113 * stolen time for a vcore when it is inactive, or for a vcpu
114 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
115 * a misnomer; it means that the vcpu task is not executing in
116 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
117 * the kernel. We don't have any way of dividing up that time
118 * between time that the vcpu is genuinely stopped, time that
119 * the task is actively working on behalf of the vcpu, and time
120 * that the task is preempted, so we don't count any of it as
121 * stolen.
122 *
123 * Updates to busy_stolen are protected by arch.tbacct_lock;
124 * updates to vc->stolen_tb are protected by the arch.tbacct_lock
125 * of the vcpu that has taken responsibility for running the vcore
126 * (i.e. vc->runner). The stolen times are measured in units of
127 * timebase ticks. (Note that the != TB_NIL checks below are
128 * purely defensive; they should never fail.)
129 */
130
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530131static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000132{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000133 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100134 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000135
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100136 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000137 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
138 vc->preempt_tb != TB_NIL) {
Paul Mackerras0456ec42012-02-03 00:56:21 +0000139 vc->stolen_tb += mftb() - vc->preempt_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000140 vc->preempt_tb = TB_NIL;
141 }
142 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
143 vcpu->arch.busy_preempt != TB_NIL) {
144 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
145 vcpu->arch.busy_preempt = TB_NIL;
146 }
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100147 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000148}
149
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530150static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000151{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000152 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100153 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000154
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100155 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000156 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
157 vc->preempt_tb = mftb();
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000158 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
159 vcpu->arch.busy_preempt = mftb();
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100160 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000161}
162
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530163static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000164{
165 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000166 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000167}
168
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530169void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000170{
171 vcpu->arch.pvr = pvr;
172}
173
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000174int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
175{
176 unsigned long pcr = 0;
177 struct kvmppc_vcore *vc = vcpu->arch.vcore;
178
179 if (arch_compat) {
180 if (!cpu_has_feature(CPU_FTR_ARCH_206))
181 return -EINVAL; /* 970 has no compat mode support */
182
183 switch (arch_compat) {
184 case PVR_ARCH_205:
185 pcr = PCR_ARCH_205;
186 break;
187 case PVR_ARCH_206:
188 case PVR_ARCH_206p:
189 break;
190 default:
191 return -EINVAL;
192 }
193 }
194
195 spin_lock(&vc->lock);
196 vc->arch_compat = arch_compat;
197 vc->pcr = pcr;
198 spin_unlock(&vc->lock);
199
200 return 0;
201}
202
Paul Mackerrasde56a942011-06-29 00:21:34 +0000203void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
204{
205 int r;
206
207 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
208 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
209 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
210 for (r = 0; r < 16; ++r)
211 pr_err("r%2d = %.16lx r%d = %.16lx\n",
212 r, kvmppc_get_gpr(vcpu, r),
213 r+16, kvmppc_get_gpr(vcpu, r+16));
214 pr_err("ctr = %.16lx lr = %.16lx\n",
215 vcpu->arch.ctr, vcpu->arch.lr);
216 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
217 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
218 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
219 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
220 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
221 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
222 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
223 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
224 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
225 pr_err("fault dar = %.16lx dsisr = %.8x\n",
226 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
227 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
228 for (r = 0; r < vcpu->arch.slb_max; ++r)
229 pr_err(" ESID = %.16llx VSID = %.16llx\n",
230 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
231 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000232 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000233 vcpu->arch.last_inst);
234}
235
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000236struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
237{
238 int r;
239 struct kvm_vcpu *v, *ret = NULL;
240
241 mutex_lock(&kvm->lock);
242 kvm_for_each_vcpu(r, v, kvm) {
243 if (v->vcpu_id == id) {
244 ret = v;
245 break;
246 }
247 }
248 mutex_unlock(&kvm->lock);
249 return ret;
250}
251
252static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
253{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000254 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000255 vpa->yield_count = 1;
256}
257
Paul Mackerras55b665b2012-09-25 20:33:06 +0000258static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
259 unsigned long addr, unsigned long len)
260{
261 /* check address is cacheline aligned */
262 if (addr & (L1_CACHE_BYTES - 1))
263 return -EINVAL;
264 spin_lock(&vcpu->arch.vpa_update_lock);
265 if (v->next_gpa != addr || v->len != len) {
266 v->next_gpa = addr;
267 v->len = addr ? len : 0;
268 v->update_pending = 1;
269 }
270 spin_unlock(&vcpu->arch.vpa_update_lock);
271 return 0;
272}
273
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000274/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
275struct reg_vpa {
276 u32 dummy;
277 union {
278 u16 hword;
279 u32 word;
280 } length;
281};
282
283static int vpa_is_registered(struct kvmppc_vpa *vpap)
284{
285 if (vpap->update_pending)
286 return vpap->next_gpa != 0;
287 return vpap->pinned_addr != NULL;
288}
289
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000290static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
291 unsigned long flags,
292 unsigned long vcpuid, unsigned long vpa)
293{
294 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000295 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000296 void *va;
297 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000298 int err;
299 int subfunc;
300 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000301
302 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
303 if (!tvcpu)
304 return H_PARAMETER;
305
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000306 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
307 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
308 subfunc == H_VPA_REG_SLB) {
309 /* Registering new area - address must be cache-line aligned */
310 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000311 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000312
313 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000314 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
315 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000316 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000317 if (subfunc == H_VPA_REG_VPA)
318 len = ((struct reg_vpa *)va)->length.hword;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000319 else
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000320 len = ((struct reg_vpa *)va)->length.word;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000321 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000322
323 /* Check length */
324 if (len > nb || len < sizeof(struct reg_vpa))
325 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000326 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000327 vpa = 0;
328 len = 0;
329 }
330
331 err = H_PARAMETER;
332 vpap = NULL;
333 spin_lock(&tvcpu->arch.vpa_update_lock);
334
335 switch (subfunc) {
336 case H_VPA_REG_VPA: /* register VPA */
337 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000338 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000339 vpap = &tvcpu->arch.vpa;
340 err = 0;
341 break;
342
343 case H_VPA_REG_DTL: /* register DTL */
344 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000345 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000346 len -= len % sizeof(struct dtl_entry);
347
348 /* Check that they have previously registered a VPA */
349 err = H_RESOURCE;
350 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000351 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000352
353 vpap = &tvcpu->arch.dtl;
354 err = 0;
355 break;
356
357 case H_VPA_REG_SLB: /* register SLB shadow buffer */
358 /* Check that they have previously registered a VPA */
359 err = H_RESOURCE;
360 if (!vpa_is_registered(&tvcpu->arch.vpa))
361 break;
362
363 vpap = &tvcpu->arch.slb_shadow;
364 err = 0;
365 break;
366
367 case H_VPA_DEREG_VPA: /* deregister VPA */
368 /* Check they don't still have a DTL or SLB buf registered */
369 err = H_RESOURCE;
370 if (vpa_is_registered(&tvcpu->arch.dtl) ||
371 vpa_is_registered(&tvcpu->arch.slb_shadow))
372 break;
373
374 vpap = &tvcpu->arch.vpa;
375 err = 0;
376 break;
377
378 case H_VPA_DEREG_DTL: /* deregister DTL */
379 vpap = &tvcpu->arch.dtl;
380 err = 0;
381 break;
382
383 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
384 vpap = &tvcpu->arch.slb_shadow;
385 err = 0;
386 break;
387 }
388
389 if (vpap) {
390 vpap->next_gpa = vpa;
391 vpap->len = len;
392 vpap->update_pending = 1;
393 }
394
395 spin_unlock(&tvcpu->arch.vpa_update_lock);
396
397 return err;
398}
399
Paul Mackerras081f3232012-06-01 20:20:24 +1000400static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000401{
Paul Mackerras081f3232012-06-01 20:20:24 +1000402 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000403 void *va;
404 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000405 unsigned long gpa;
406
407 /*
408 * We need to pin the page pointed to by vpap->next_gpa,
409 * but we can't call kvmppc_pin_guest_page under the lock
410 * as it does get_user_pages() and down_read(). So we
411 * have to drop the lock, pin the page, then get the lock
412 * again and check that a new area didn't get registered
413 * in the meantime.
414 */
415 for (;;) {
416 gpa = vpap->next_gpa;
417 spin_unlock(&vcpu->arch.vpa_update_lock);
418 va = NULL;
419 nb = 0;
420 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000421 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000422 spin_lock(&vcpu->arch.vpa_update_lock);
423 if (gpa == vpap->next_gpa)
424 break;
425 /* sigh... unpin that one and try again */
426 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000427 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000428 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000429
430 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000431 if (va && nb < vpap->len) {
432 /*
433 * If it's now too short, it must be that userspace
434 * has changed the mappings underlying guest memory,
435 * so unregister the region.
436 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000437 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000438 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000439 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000440 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000441 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
442 vpap->dirty);
443 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000444 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000445 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000446 if (va)
447 vpap->pinned_end = va + vpap->len;
448}
Paul Mackerras93e60242011-12-12 12:28:55 +0000449
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000450static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
451{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000452 if (!(vcpu->arch.vpa.update_pending ||
453 vcpu->arch.slb_shadow.update_pending ||
454 vcpu->arch.dtl.update_pending))
455 return;
456
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000457 spin_lock(&vcpu->arch.vpa_update_lock);
458 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000459 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000460 if (vcpu->arch.vpa.pinned_addr)
461 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000462 }
463 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000464 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000465 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
466 vcpu->arch.dtl_index = 0;
467 }
468 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000469 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000470 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000471}
472
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000473/*
474 * Return the accumulated stolen time for the vcore up until `now'.
475 * The caller should hold the vcore lock.
476 */
477static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
478{
479 u64 p;
480
481 /*
482 * If we are the task running the vcore, then since we hold
483 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
484 * can't be updated, so we don't need the tbacct_lock.
485 * If the vcore is inactive, it can't become active (since we
486 * hold the vcore lock), so the vcpu load/put functions won't
487 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
488 */
489 if (vc->vcore_state != VCORE_INACTIVE &&
490 vc->runner->arch.run_task != current) {
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100491 spin_lock_irq(&vc->runner->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000492 p = vc->stolen_tb;
493 if (vc->preempt_tb != TB_NIL)
494 p += now - vc->preempt_tb;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100495 spin_unlock_irq(&vc->runner->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000496 } else {
497 p = vc->stolen_tb;
498 }
499 return p;
500}
501
Paul Mackerras0456ec42012-02-03 00:56:21 +0000502static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
503 struct kvmppc_vcore *vc)
504{
505 struct dtl_entry *dt;
506 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000507 unsigned long stolen;
508 unsigned long core_stolen;
509 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000510
511 dt = vcpu->arch.dtl_ptr;
512 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000513 now = mftb();
514 core_stolen = vcore_stolen_time(vc, now);
515 stolen = core_stolen - vcpu->arch.stolen_logged;
516 vcpu->arch.stolen_logged = core_stolen;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100517 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000518 stolen += vcpu->arch.busy_stolen;
519 vcpu->arch.busy_stolen = 0;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100520 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000521 if (!dt || !vpa)
522 return;
523 memset(dt, 0, sizeof(struct dtl_entry));
524 dt->dispatch_reason = 7;
525 dt->processor_id = vc->pcpu + vcpu->arch.ptid;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000526 dt->timebase = now + vc->tb_offset;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000527 dt->enqueue_to_dispatch_time = stolen;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000528 dt->srr0 = kvmppc_get_pc(vcpu);
529 dt->srr1 = vcpu->arch.shregs.msr;
530 ++dt;
531 if (dt == vcpu->arch.dtl.pinned_end)
532 dt = vcpu->arch.dtl.pinned_addr;
533 vcpu->arch.dtl_ptr = dt;
534 /* order writing *dt vs. writing vpa->dtl_idx */
535 smp_wmb();
536 vpa->dtl_idx = ++vcpu->arch.dtl_index;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000537 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000538}
539
540int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
541{
542 unsigned long req = kvmppc_get_gpr(vcpu, 3);
543 unsigned long target, ret = H_SUCCESS;
544 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000545 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000546
547 switch (req) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000548 case H_ENTER:
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000549 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000550 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
551 kvmppc_get_gpr(vcpu, 5),
552 kvmppc_get_gpr(vcpu, 6),
553 kvmppc_get_gpr(vcpu, 7));
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000554 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000555 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000556 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000557 break;
558 case H_PROD:
559 target = kvmppc_get_gpr(vcpu, 4);
560 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
561 if (!tvcpu) {
562 ret = H_PARAMETER;
563 break;
564 }
565 tvcpu->arch.prodded = 1;
566 smp_mb();
567 if (vcpu->arch.ceded) {
568 if (waitqueue_active(&vcpu->wq)) {
569 wake_up_interruptible(&vcpu->wq);
570 vcpu->stat.halt_wakeup++;
571 }
572 }
573 break;
574 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +1000575 target = kvmppc_get_gpr(vcpu, 4);
576 if (target == -1)
577 break;
578 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
579 if (!tvcpu) {
580 ret = H_PARAMETER;
581 break;
582 }
583 kvm_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000584 break;
585 case H_REGISTER_VPA:
586 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
587 kvmppc_get_gpr(vcpu, 5),
588 kvmppc_get_gpr(vcpu, 6));
589 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000590 case H_RTAS:
591 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
592 return RESUME_HOST;
593
Paul Mackerrasc9438092013-11-16 17:46:05 +1100594 idx = srcu_read_lock(&vcpu->kvm->srcu);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000595 rc = kvmppc_rtas_hcall(vcpu);
Paul Mackerrasc9438092013-11-16 17:46:05 +1100596 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000597
598 if (rc == -ENOENT)
599 return RESUME_HOST;
600 else if (rc == 0)
601 break;
602
603 /* Send the error out to userspace via KVM_RUN */
604 return rc;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000605
606 case H_XIRR:
607 case H_CPPR:
608 case H_EOI:
609 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000610 case H_IPOLL:
611 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000612 if (kvmppc_xics_enabled(vcpu)) {
613 ret = kvmppc_xics_hcall(vcpu, req);
614 break;
615 } /* fallthrough */
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000616 default:
617 return RESUME_HOST;
618 }
619 kvmppc_set_gpr(vcpu, 3, ret);
620 vcpu->arch.hcall_needed = 0;
621 return RESUME_GUEST;
622}
623
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530624static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
625 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000626{
627 int r = RESUME_HOST;
628
629 vcpu->stat.sum_exits++;
630
631 run->exit_reason = KVM_EXIT_UNKNOWN;
632 run->ready_for_interrupt_injection = 1;
633 switch (vcpu->arch.trap) {
634 /* We're good on these - the host merely wanted to get our attention */
635 case BOOK3S_INTERRUPT_HV_DECREMENTER:
636 vcpu->stat.dec_exits++;
637 r = RESUME_GUEST;
638 break;
639 case BOOK3S_INTERRUPT_EXTERNAL:
640 vcpu->stat.ext_intr_exits++;
641 r = RESUME_GUEST;
642 break;
643 case BOOK3S_INTERRUPT_PERFMON:
644 r = RESUME_GUEST;
645 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000646 case BOOK3S_INTERRUPT_MACHINE_CHECK:
647 /*
648 * Deliver a machine check interrupt to the guest.
649 * We have to do this, even if the host has handled the
650 * machine check, because machine checks use SRR0/1 and
651 * the interrupt might have trashed guest state in them.
652 */
653 kvmppc_book3s_queue_irqprio(vcpu,
654 BOOK3S_INTERRUPT_MACHINE_CHECK);
655 r = RESUME_GUEST;
656 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000657 case BOOK3S_INTERRUPT_PROGRAM:
658 {
659 ulong flags;
660 /*
661 * Normally program interrupts are delivered directly
662 * to the guest by the hardware, but we can get here
663 * as a result of a hypervisor emulation interrupt
664 * (e40) getting turned into a 700 by BML RTAS.
665 */
666 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
667 kvmppc_core_queue_program(vcpu, flags);
668 r = RESUME_GUEST;
669 break;
670 }
671 case BOOK3S_INTERRUPT_SYSCALL:
672 {
673 /* hcall - punt to userspace */
674 int i;
675
676 if (vcpu->arch.shregs.msr & MSR_PR) {
677 /* sc 1 from userspace - reflect to guest syscall */
678 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
679 r = RESUME_GUEST;
680 break;
681 }
682 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
683 for (i = 0; i < 9; ++i)
684 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
685 run->exit_reason = KVM_EXIT_PAPR_HCALL;
686 vcpu->arch.hcall_needed = 1;
687 r = RESUME_HOST;
688 break;
689 }
690 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000691 * We get these next two if the guest accesses a page which it thinks
692 * it has mapped but which is not actually present, either because
693 * it is for an emulated I/O device or because the corresonding
694 * host page has been paged out. Any other HDSI/HISI interrupts
695 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000696 */
697 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000698 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000699 break;
700 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000701 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
702 vcpu->arch.fault_dsisr = 0;
703 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000704 break;
705 /*
706 * This occurs if the guest executes an illegal instruction.
707 * We just generate a program interrupt to the guest, since
708 * we don't emulate any guest instructions at this stage.
709 */
710 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
711 kvmppc_core_queue_program(vcpu, 0x80000);
712 r = RESUME_GUEST;
713 break;
714 default:
715 kvmppc_dump_regs(vcpu);
716 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
717 vcpu->arch.trap, kvmppc_get_pc(vcpu),
718 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +1000719 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000720 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000721 break;
722 }
723
Paul Mackerrasde56a942011-06-29 00:21:34 +0000724 return r;
725}
726
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530727static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
728 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000729{
730 int i;
731
Paul Mackerrasde56a942011-06-29 00:21:34 +0000732 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +0530733 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000734 for (i = 0; i < vcpu->arch.slb_max; i++) {
735 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
736 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
737 }
738
739 return 0;
740}
741
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530742static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
743 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000744{
745 int i, j;
746
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530747 kvmppc_set_pvr_hv(vcpu, sregs->pvr);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000748
749 j = 0;
750 for (i = 0; i < vcpu->arch.slb_nr; i++) {
751 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
752 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
753 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
754 ++j;
755 }
756 }
757 vcpu->arch.slb_max = j;
758
759 return 0;
760}
761
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000762static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr)
763{
764 struct kvmppc_vcore *vc = vcpu->arch.vcore;
765 u64 mask;
766
767 spin_lock(&vc->lock);
768 /*
769 * Userspace can only modify DPFD (default prefetch depth),
770 * ILE (interrupt little-endian) and TC (translation control).
771 */
772 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
773 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
774 spin_unlock(&vc->lock);
775}
776
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530777static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
778 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000779{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000780 int r = 0;
781 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +0000782
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000783 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000784 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000785 *val = get_reg_val(id, 0);
786 break;
787 case KVM_REG_PPC_DABR:
788 *val = get_reg_val(id, vcpu->arch.dabr);
789 break;
790 case KVM_REG_PPC_DSCR:
791 *val = get_reg_val(id, vcpu->arch.dscr);
792 break;
793 case KVM_REG_PPC_PURR:
794 *val = get_reg_val(id, vcpu->arch.purr);
795 break;
796 case KVM_REG_PPC_SPURR:
797 *val = get_reg_val(id, vcpu->arch.spurr);
798 break;
799 case KVM_REG_PPC_AMR:
800 *val = get_reg_val(id, vcpu->arch.amr);
801 break;
802 case KVM_REG_PPC_UAMOR:
803 *val = get_reg_val(id, vcpu->arch.uamor);
804 break;
805 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
806 i = id - KVM_REG_PPC_MMCR0;
807 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
808 break;
809 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
810 i = id - KVM_REG_PPC_PMC1;
811 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +0000812 break;
Paul Mackerras14941782013-09-06 13:11:18 +1000813 case KVM_REG_PPC_SIAR:
814 *val = get_reg_val(id, vcpu->arch.siar);
815 break;
816 case KVM_REG_PPC_SDAR:
817 *val = get_reg_val(id, vcpu->arch.sdar);
818 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000819#ifdef CONFIG_VSX
820 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
821 if (cpu_has_feature(CPU_FTR_VSX)) {
822 /* VSX => FP reg i is stored in arch.vsr[2*i] */
823 long int i = id - KVM_REG_PPC_FPR0;
824 *val = get_reg_val(id, vcpu->arch.vsr[2 * i]);
825 } else {
826 /* let generic code handle it */
827 r = -EINVAL;
828 }
829 break;
830 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
831 if (cpu_has_feature(CPU_FTR_VSX)) {
832 long int i = id - KVM_REG_PPC_VSR0;
833 val->vsxval[0] = vcpu->arch.vsr[2 * i];
834 val->vsxval[1] = vcpu->arch.vsr[2 * i + 1];
835 } else {
836 r = -ENXIO;
837 }
838 break;
839#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000840 case KVM_REG_PPC_VPA_ADDR:
841 spin_lock(&vcpu->arch.vpa_update_lock);
842 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
843 spin_unlock(&vcpu->arch.vpa_update_lock);
844 break;
845 case KVM_REG_PPC_VPA_SLB:
846 spin_lock(&vcpu->arch.vpa_update_lock);
847 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
848 val->vpaval.length = vcpu->arch.slb_shadow.len;
849 spin_unlock(&vcpu->arch.vpa_update_lock);
850 break;
851 case KVM_REG_PPC_VPA_DTL:
852 spin_lock(&vcpu->arch.vpa_update_lock);
853 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
854 val->vpaval.length = vcpu->arch.dtl.len;
855 spin_unlock(&vcpu->arch.vpa_update_lock);
856 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000857 case KVM_REG_PPC_TB_OFFSET:
858 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
859 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000860 case KVM_REG_PPC_LPCR:
861 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
862 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +1000863 case KVM_REG_PPC_PPR:
864 *val = get_reg_val(id, vcpu->arch.ppr);
865 break;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000866 case KVM_REG_PPC_ARCH_COMPAT:
867 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
868 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000869 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000870 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000871 break;
872 }
873
874 return r;
875}
876
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530877static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
878 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000879{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000880 int r = 0;
881 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +0000882 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +0000883
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000884 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000885 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +0000886 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000887 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +0000888 r = -EINVAL;
889 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000890 case KVM_REG_PPC_DABR:
891 vcpu->arch.dabr = set_reg_val(id, *val);
892 break;
893 case KVM_REG_PPC_DSCR:
894 vcpu->arch.dscr = set_reg_val(id, *val);
895 break;
896 case KVM_REG_PPC_PURR:
897 vcpu->arch.purr = set_reg_val(id, *val);
898 break;
899 case KVM_REG_PPC_SPURR:
900 vcpu->arch.spurr = set_reg_val(id, *val);
901 break;
902 case KVM_REG_PPC_AMR:
903 vcpu->arch.amr = set_reg_val(id, *val);
904 break;
905 case KVM_REG_PPC_UAMOR:
906 vcpu->arch.uamor = set_reg_val(id, *val);
907 break;
908 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
909 i = id - KVM_REG_PPC_MMCR0;
910 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
911 break;
912 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
913 i = id - KVM_REG_PPC_PMC1;
914 vcpu->arch.pmc[i] = set_reg_val(id, *val);
915 break;
Paul Mackerras14941782013-09-06 13:11:18 +1000916 case KVM_REG_PPC_SIAR:
917 vcpu->arch.siar = set_reg_val(id, *val);
918 break;
919 case KVM_REG_PPC_SDAR:
920 vcpu->arch.sdar = set_reg_val(id, *val);
921 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000922#ifdef CONFIG_VSX
923 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
924 if (cpu_has_feature(CPU_FTR_VSX)) {
925 /* VSX => FP reg i is stored in arch.vsr[2*i] */
926 long int i = id - KVM_REG_PPC_FPR0;
927 vcpu->arch.vsr[2 * i] = set_reg_val(id, *val);
928 } else {
929 /* let generic code handle it */
930 r = -EINVAL;
931 }
932 break;
933 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
934 if (cpu_has_feature(CPU_FTR_VSX)) {
935 long int i = id - KVM_REG_PPC_VSR0;
936 vcpu->arch.vsr[2 * i] = val->vsxval[0];
937 vcpu->arch.vsr[2 * i + 1] = val->vsxval[1];
938 } else {
939 r = -ENXIO;
940 }
941 break;
942#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000943 case KVM_REG_PPC_VPA_ADDR:
944 addr = set_reg_val(id, *val);
945 r = -EINVAL;
946 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
947 vcpu->arch.dtl.next_gpa))
948 break;
949 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
950 break;
951 case KVM_REG_PPC_VPA_SLB:
952 addr = val->vpaval.addr;
953 len = val->vpaval.length;
954 r = -EINVAL;
955 if (addr && !vcpu->arch.vpa.next_gpa)
956 break;
957 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
958 break;
959 case KVM_REG_PPC_VPA_DTL:
960 addr = val->vpaval.addr;
961 len = val->vpaval.length;
962 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +0000963 if (addr && (len < sizeof(struct dtl_entry) ||
964 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +0000965 break;
966 len -= len % sizeof(struct dtl_entry);
967 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
968 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000969 case KVM_REG_PPC_TB_OFFSET:
970 /* round up to multiple of 2^24 */
971 vcpu->arch.vcore->tb_offset =
972 ALIGN(set_reg_val(id, *val), 1UL << 24);
973 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000974 case KVM_REG_PPC_LPCR:
975 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val));
976 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +1000977 case KVM_REG_PPC_PPR:
978 vcpu->arch.ppr = set_reg_val(id, *val);
979 break;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000980 case KVM_REG_PPC_ARCH_COMPAT:
981 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
982 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000983 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000984 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000985 break;
986 }
987
988 return r;
989}
990
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530991static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
992 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000993{
994 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000995 int err = -EINVAL;
996 int core;
997 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000998
Paul Mackerras371fefd2011-06-29 00:23:08 +0000999 core = id / threads_per_core;
1000 if (core >= KVM_MAX_VCORES)
1001 goto out;
1002
1003 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001004 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001005 if (!vcpu)
1006 goto out;
1007
1008 err = kvm_vcpu_init(vcpu, kvm, id);
1009 if (err)
1010 goto free_vcpu;
1011
1012 vcpu->arch.shared = &vcpu->arch.shregs;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001013 vcpu->arch.mmcr[0] = MMCR0_FC;
1014 vcpu->arch.ctrl = CTRL_RUNLATCH;
1015 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301016 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001017 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001018 spin_lock_init(&vcpu->arch.tbacct_lock);
1019 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001020
Paul Mackerrasde56a942011-06-29 00:21:34 +00001021 kvmppc_mmu_book3s_hv_init(vcpu);
1022
Paul Mackerras8455d792012-10-15 01:17:42 +00001023 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001024
1025 init_waitqueue_head(&vcpu->arch.cpu_run);
1026
1027 mutex_lock(&kvm->lock);
1028 vcore = kvm->arch.vcores[core];
1029 if (!vcore) {
1030 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1031 if (vcore) {
1032 INIT_LIST_HEAD(&vcore->runnable_threads);
1033 spin_lock_init(&vcore->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001034 init_waitqueue_head(&vcore->wq);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001035 vcore->preempt_tb = TB_NIL;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001036 vcore->lpcr = kvm->arch.lpcr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001037 }
1038 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001039 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001040 }
1041 mutex_unlock(&kvm->lock);
1042
1043 if (!vcore)
1044 goto free_vcpu;
1045
1046 spin_lock(&vcore->lock);
1047 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001048 spin_unlock(&vcore->lock);
1049 vcpu->arch.vcore = vcore;
1050
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001051 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1052 kvmppc_sanity_check(vcpu);
1053
Paul Mackerrasde56a942011-06-29 00:21:34 +00001054 return vcpu;
1055
1056free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001057 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001058out:
1059 return ERR_PTR(err);
1060}
1061
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001062static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1063{
1064 if (vpa->pinned_addr)
1065 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1066 vpa->dirty);
1067}
1068
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301069static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001070{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001071 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001072 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1073 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1074 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001075 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001076 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001077 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001078}
1079
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301080static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1081{
1082 /* Indicate we want to get back into the guest */
1083 return 1;
1084}
1085
Paul Mackerras19ccb762011-07-23 17:42:46 +10001086static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001087{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001088 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001089
Paul Mackerras19ccb762011-07-23 17:42:46 +10001090 now = get_tb();
1091 if (now > vcpu->arch.dec_expires) {
1092 /* decrementer has already gone negative */
1093 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001094 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001095 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001096 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001097 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1098 / tb_ticks_per_sec;
1099 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1100 HRTIMER_MODE_REL);
1101 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001102}
1103
Paul Mackerras19ccb762011-07-23 17:42:46 +10001104static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001105{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001106 vcpu->arch.ceded = 0;
1107 if (vcpu->arch.timer_running) {
1108 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1109 vcpu->arch.timer_running = 0;
1110 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001111}
1112
1113extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001114
1115static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1116 struct kvm_vcpu *vcpu)
1117{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001118 u64 now;
1119
Paul Mackerras371fefd2011-06-29 00:23:08 +00001120 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1121 return;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001122 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001123 now = mftb();
1124 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1125 vcpu->arch.stolen_logged;
1126 vcpu->arch.busy_preempt = now;
1127 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001128 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001129 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001130 list_del(&vcpu->arch.run_list);
1131}
1132
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001133static int kvmppc_grab_hwthread(int cpu)
1134{
1135 struct paca_struct *tpaca;
1136 long timeout = 1000;
1137
1138 tpaca = &paca[cpu];
1139
1140 /* Ensure the thread won't go into the kernel if it wakes */
1141 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001142 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001143
1144 /*
1145 * If the thread is already executing in the kernel (e.g. handling
1146 * a stray interrupt), wait for it to get back to nap mode.
1147 * The smp_mb() is to ensure that our setting of hwthread_req
1148 * is visible before we look at hwthread_state, so if this
1149 * races with the code at system_reset_pSeries and the thread
1150 * misses our setting of hwthread_req, we are sure to see its
1151 * setting of hwthread_state, and vice versa.
1152 */
1153 smp_mb();
1154 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1155 if (--timeout <= 0) {
1156 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1157 return -EBUSY;
1158 }
1159 udelay(1);
1160 }
1161 return 0;
1162}
1163
1164static void kvmppc_release_hwthread(int cpu)
1165{
1166 struct paca_struct *tpaca;
1167
1168 tpaca = &paca[cpu];
1169 tpaca->kvm_hstate.hwthread_req = 0;
1170 tpaca->kvm_hstate.kvm_vcpu = NULL;
1171}
1172
Paul Mackerras371fefd2011-06-29 00:23:08 +00001173static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1174{
1175 int cpu;
1176 struct paca_struct *tpaca;
1177 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1178
Paul Mackerras19ccb762011-07-23 17:42:46 +10001179 if (vcpu->arch.timer_running) {
1180 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1181 vcpu->arch.timer_running = 0;
1182 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001183 cpu = vc->pcpu + vcpu->arch.ptid;
1184 tpaca = &paca[cpu];
1185 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1186 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001187 tpaca->kvm_hstate.napping = 0;
1188 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001189 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +00001190#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001191 if (vcpu->arch.ptid) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001192 xics_wake_cpu(cpu);
1193 ++vc->n_woken;
1194 }
1195#endif
1196}
1197
1198static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1199{
1200 int i;
1201
1202 HMT_low();
1203 i = 0;
1204 while (vc->nap_count < vc->n_woken) {
1205 if (++i >= 1000000) {
1206 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1207 vc->nap_count, vc->n_woken);
1208 break;
1209 }
1210 cpu_relax();
1211 }
1212 HMT_medium();
1213}
1214
1215/*
1216 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001217 * this core are off-line. Then grab the threads so they can't
1218 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001219 */
1220static int on_primary_thread(void)
1221{
1222 int cpu = smp_processor_id();
1223 int thr = cpu_thread_in_core(cpu);
1224
1225 if (thr)
1226 return 0;
1227 while (++thr < threads_per_core)
1228 if (cpu_online(cpu + thr))
1229 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001230
1231 /* Grab all hw threads so they can't go into the kernel */
1232 for (thr = 1; thr < threads_per_core; ++thr) {
1233 if (kvmppc_grab_hwthread(cpu + thr)) {
1234 /* Couldn't grab one; let the others go */
1235 do {
1236 kvmppc_release_hwthread(cpu + thr);
1237 } while (--thr > 0);
1238 return 0;
1239 }
1240 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001241 return 1;
1242}
1243
1244/*
1245 * Run a set of guest threads on a physical core.
1246 * Called with vc->lock held.
1247 */
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001248static void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001249{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001250 struct kvm_vcpu *vcpu, *vcpu0, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001251 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001252 u64 now;
Paul Mackerras081f3232012-06-01 20:20:24 +10001253 int ptid, i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001254 int srcu_idx;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001255 struct kvm_vcpu *vcpus_to_update[threads_per_core];
Paul Mackerrasde56a942011-06-29 00:21:34 +00001256
Paul Mackerras371fefd2011-06-29 00:23:08 +00001257 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +10001258 need_vpa_update = 0;
1259 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001260 if (signal_pending(vcpu->arch.run_task))
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001261 return;
1262 if (vcpu->arch.vpa.update_pending ||
1263 vcpu->arch.slb_shadow.update_pending ||
1264 vcpu->arch.dtl.update_pending)
1265 vcpus_to_update[need_vpa_update++] = vcpu;
Paul Mackerras081f3232012-06-01 20:20:24 +10001266 }
1267
1268 /*
1269 * Initialize *vc, in particular vc->vcore_state, so we can
1270 * drop the vcore lock if necessary.
1271 */
1272 vc->n_woken = 0;
1273 vc->nap_count = 0;
1274 vc->entry_exit_count = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001275 vc->vcore_state = VCORE_STARTING;
Paul Mackerras081f3232012-06-01 20:20:24 +10001276 vc->in_guest = 0;
1277 vc->napping_threads = 0;
1278
1279 /*
1280 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1281 * which can't be called with any spinlocks held.
1282 */
1283 if (need_vpa_update) {
1284 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001285 for (i = 0; i < need_vpa_update; ++i)
1286 kvmppc_update_vpas(vcpus_to_update[i]);
Paul Mackerras081f3232012-06-01 20:20:24 +10001287 spin_lock(&vc->lock);
1288 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001289
1290 /*
Paul Mackerras19ccb762011-07-23 17:42:46 +10001291 * Assign physical thread IDs, first to non-ceded vcpus
1292 * and then to ceded ones.
1293 */
1294 ptid = 0;
1295 vcpu0 = NULL;
1296 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1297 if (!vcpu->arch.ceded) {
1298 if (!ptid)
1299 vcpu0 = vcpu;
1300 vcpu->arch.ptid = ptid++;
1301 }
1302 }
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001303 if (!vcpu0)
1304 goto out; /* nothing to run; should never happen */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001305 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1306 if (vcpu->arch.ceded)
1307 vcpu->arch.ptid = ptid++;
1308
Paul Mackerras7b444c62012-10-15 01:16:14 +00001309 /*
1310 * Make sure we are running on thread 0, and that
1311 * secondary threads are offline.
1312 */
1313 if (threads_per_core > 1 && !on_primary_thread()) {
1314 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1315 vcpu->arch.ret = -EBUSY;
1316 goto out;
1317 }
1318
Paul Mackerras371fefd2011-06-29 00:23:08 +00001319 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001320 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001321 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001322 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001323 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001324
Paul Mackerras2f12f032012-10-15 01:17:17 +00001325 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001326 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001327 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001328
Paul Mackerras19ccb762011-07-23 17:42:46 +10001329 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001330
1331 srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
1332
Paul Mackerras19ccb762011-07-23 17:42:46 +10001333 __kvmppc_vcore_entry(NULL, vcpu0);
1334
Paul Mackerras371fefd2011-06-29 00:23:08 +00001335 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001336 /* disable sending of IPIs on virtual external irqs */
1337 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1338 vcpu->cpu = -1;
1339 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001340 if (vc->nap_count < vc->n_woken)
1341 kvmppc_wait_for_nap(vc);
Paul Mackerras2f12f032012-10-15 01:17:17 +00001342 for (i = 0; i < threads_per_core; ++i)
1343 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001344 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001345 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001346 spin_unlock(&vc->lock);
1347
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001348 srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
1349
Paul Mackerras371fefd2011-06-29 00:23:08 +00001350 /* make sure updates to secondary vcpu structs are visible now */
1351 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001352 kvm_guest_exit();
1353
1354 preempt_enable();
1355 kvm_resched(vcpu);
1356
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001357 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001358 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001359 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1360 /* cancel pending dec exception if dec is positive */
1361 if (now < vcpu->arch.dec_expires &&
1362 kvmppc_core_pending_dec(vcpu))
1363 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001364
1365 ret = RESUME_GUEST;
1366 if (vcpu->arch.trap)
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301367 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1368 vcpu->arch.run_task);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001369
Paul Mackerras371fefd2011-06-29 00:23:08 +00001370 vcpu->arch.ret = ret;
1371 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001372
1373 if (vcpu->arch.ceded) {
1374 if (ret != RESUME_GUEST)
1375 kvmppc_end_cede(vcpu);
1376 else
1377 kvmppc_set_timer(vcpu);
1378 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001379 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001380
Paul Mackerrasde56a942011-06-29 00:21:34 +00001381 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001382 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001383 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1384 arch.run_list) {
1385 if (vcpu->arch.ret != RESUME_GUEST) {
1386 kvmppc_remove_runnable(vc, vcpu);
1387 wake_up(&vcpu->arch.cpu_run);
1388 }
1389 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001390}
1391
Paul Mackerras19ccb762011-07-23 17:42:46 +10001392/*
1393 * Wait for some other vcpu thread to execute us, and
1394 * wake us up when we need to handle something in the host.
1395 */
1396static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001397{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001398 DEFINE_WAIT(wait);
1399
Paul Mackerras19ccb762011-07-23 17:42:46 +10001400 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1401 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1402 schedule();
1403 finish_wait(&vcpu->arch.cpu_run, &wait);
1404}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001405
Paul Mackerras19ccb762011-07-23 17:42:46 +10001406/*
1407 * All the vcpus in this vcore are idle, so wait for a decrementer
1408 * or external interrupt to one of the vcpus. vc->lock is held.
1409 */
1410static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1411{
1412 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001413
1414 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1415 vc->vcore_state = VCORE_SLEEPING;
1416 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001417 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001418 finish_wait(&vc->wq, &wait);
1419 spin_lock(&vc->lock);
1420 vc->vcore_state = VCORE_INACTIVE;
1421}
1422
1423static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1424{
1425 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001426 struct kvmppc_vcore *vc;
1427 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001428
Paul Mackerras371fefd2011-06-29 00:23:08 +00001429 kvm_run->exit_reason = 0;
1430 vcpu->arch.ret = RESUME_GUEST;
1431 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001432 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001433
Paul Mackerras371fefd2011-06-29 00:23:08 +00001434 /*
1435 * Synchronize with other threads in this virtual core
1436 */
1437 vc = vcpu->arch.vcore;
1438 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001439 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001440 vcpu->arch.run_task = current;
1441 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001442 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10001443 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001444 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001445 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1446 ++vc->n_runnable;
1447
Paul Mackerras19ccb762011-07-23 17:42:46 +10001448 /*
1449 * This happens the first time this is called for a vcpu.
1450 * If the vcore is already running, we may be able to start
1451 * this thread straight away and have it join in.
1452 */
Paul Mackerras8455d792012-10-15 01:17:42 +00001453 if (!signal_pending(current)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001454 if (vc->vcore_state == VCORE_RUNNING &&
1455 VCORE_EXIT_COUNT(vc) == 0) {
1456 vcpu->arch.ptid = vc->n_runnable - 1;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001457 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001458 kvmppc_start_thread(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00001459 } else if (vc->vcore_state == VCORE_SLEEPING) {
1460 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001461 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001462
Paul Mackerras8455d792012-10-15 01:17:42 +00001463 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001464
1465 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1466 !signal_pending(current)) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001467 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001468 spin_unlock(&vc->lock);
1469 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1470 spin_lock(&vc->lock);
1471 continue;
1472 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001473 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1474 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001475 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001476 if (signal_pending(v->arch.run_task)) {
1477 kvmppc_remove_runnable(vc, v);
1478 v->stat.signal_exits++;
1479 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1480 v->arch.ret = -EINTR;
1481 wake_up(&v->arch.cpu_run);
1482 }
1483 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001484 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1485 break;
1486 vc->runner = vcpu;
1487 n_ceded = 0;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001488 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001489 if (!v->arch.pending_exceptions)
1490 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001491 else
1492 v->arch.ceded = 0;
1493 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001494 if (n_ceded == vc->n_runnable)
1495 kvmppc_vcore_blocked(vc);
1496 else
1497 kvmppc_run_core(vc);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001498 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001499 }
1500
Paul Mackerras8455d792012-10-15 01:17:42 +00001501 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1502 (vc->vcore_state == VCORE_RUNNING ||
1503 vc->vcore_state == VCORE_EXITING)) {
1504 spin_unlock(&vc->lock);
1505 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1506 spin_lock(&vc->lock);
1507 }
1508
1509 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1510 kvmppc_remove_runnable(vc, vcpu);
1511 vcpu->stat.signal_exits++;
1512 kvm_run->exit_reason = KVM_EXIT_INTR;
1513 vcpu->arch.ret = -EINTR;
1514 }
1515
1516 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1517 /* Wake up some vcpu to run the core */
1518 v = list_first_entry(&vc->runnable_threads,
1519 struct kvm_vcpu, arch.run_list);
1520 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001521 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001522
Paul Mackerras19ccb762011-07-23 17:42:46 +10001523 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001524 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001525}
1526
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301527static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001528{
1529 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001530 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001531
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001532 if (!vcpu->arch.sane) {
1533 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1534 return -EINVAL;
1535 }
1536
Scott Wood25051b52011-11-08 18:23:23 -06001537 kvmppc_core_prepare_to_enter(vcpu);
1538
Paul Mackerras19ccb762011-07-23 17:42:46 +10001539 /* No need to go into the guest when all we'll do is come back out */
1540 if (signal_pending(current)) {
1541 run->exit_reason = KVM_EXIT_INTR;
1542 return -EINTR;
1543 }
1544
Paul Mackerras32fad282012-05-04 02:32:53 +00001545 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1546 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1547 smp_mb();
1548
1549 /* On the first time here, set up HTAB and VRMA or RMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001550 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001551 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001552 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001553 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001554 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001555
1556 flush_fp_to_thread(current);
1557 flush_altivec_to_thread(current);
1558 flush_vsx_to_thread(current);
1559 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001560 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001561 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001562
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001563 do {
1564 r = kvmppc_run_vcpu(run, vcpu);
1565
1566 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1567 !(vcpu->arch.shregs.msr & MSR_PR)) {
1568 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001569 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001570 } else if (r == RESUME_PAGE_FAULT) {
1571 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1572 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1573 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1574 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001575 }
1576 } while (r == RESUME_GUEST);
Paul Mackerras32fad282012-05-04 02:32:53 +00001577
1578 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001579 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00001580 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001581 return r;
1582}
1583
David Gibson54738c02011-06-29 00:22:41 +00001584
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001585/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +00001586 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001587static inline int lpcr_rmls(unsigned long rma_size)
1588{
1589 switch (rma_size) {
1590 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001591 if (cpu_has_feature(CPU_FTR_ARCH_206))
1592 return 8; /* only supported on POWER7 */
1593 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001594 case 64ul << 20: /* 64 MB */
1595 return 3;
1596 case 128ul << 20: /* 128 MB */
1597 return 7;
1598 case 256ul << 20: /* 256 MB */
1599 return 4;
1600 case 1ul << 30: /* 1 GB */
1601 return 2;
1602 case 16ul << 30: /* 16 GB */
1603 return 1;
1604 case 256ul << 30: /* 256 GB */
1605 return 0;
1606 default:
1607 return -1;
1608 }
1609}
1610
1611static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1612{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001613 struct page *page;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301614 struct kvm_rma_info *ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001615
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301616 if (vmf->pgoff >= kvm_rma_pages)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001617 return VM_FAULT_SIGBUS;
1618
1619 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1620 get_page(page);
1621 vmf->page = page;
1622 return 0;
1623}
1624
1625static const struct vm_operations_struct kvm_rma_vm_ops = {
1626 .fault = kvm_rma_fault,
1627};
1628
1629static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1630{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001631 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001632 vma->vm_ops = &kvm_rma_vm_ops;
1633 return 0;
1634}
1635
1636static int kvm_rma_release(struct inode *inode, struct file *filp)
1637{
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301638 struct kvm_rma_info *ri = filp->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001639
1640 kvm_release_rma(ri);
1641 return 0;
1642}
1643
Al Viro75ef9de2013-04-04 19:09:41 -04001644static const struct file_operations kvm_rma_fops = {
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001645 .mmap = kvm_rma_mmap,
1646 .release = kvm_rma_release,
1647};
1648
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301649static long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
1650 struct kvm_allocate_rma *ret)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001651{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001652 long fd;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301653 struct kvm_rma_info *ri;
1654 /*
1655 * Only do this on PPC970 in HV mode
1656 */
1657 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
1658 !cpu_has_feature(CPU_FTR_ARCH_201))
1659 return -EINVAL;
1660
1661 if (!kvm_rma_pages)
1662 return -EINVAL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001663
1664 ri = kvm_alloc_rma();
1665 if (!ri)
1666 return -ENOMEM;
1667
Yann Droneaud2f84d5e2013-08-24 22:14:08 +02001668 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001669 if (fd < 0)
1670 kvm_release_rma(ri);
1671
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301672 ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001673 return fd;
1674}
1675
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001676static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1677 int linux_psize)
1678{
1679 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1680
1681 if (!def->shift)
1682 return;
1683 (*sps)->page_shift = def->shift;
1684 (*sps)->slb_enc = def->sllp;
1685 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00001686 /*
1687 * Only return base page encoding. We don't want to return
1688 * all the supporting pte_enc, because our H_ENTER doesn't
1689 * support MPSS yet. Once they do, we can start passing all
1690 * support pte_enc here
1691 */
1692 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001693 (*sps)++;
1694}
1695
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301696static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
1697 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001698{
1699 struct kvm_ppc_one_seg_page_size *sps;
1700
1701 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1702 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1703 info->flags |= KVM_PPC_1T_SEGMENTS;
1704 info->slb_size = mmu_slb_size;
1705
1706 /* We only support these sizes for now, and no muti-size segments */
1707 sps = &info->sps[0];
1708 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1709 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1710 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1711
1712 return 0;
1713}
1714
Paul Mackerras82ed3612011-12-15 02:03:22 +00001715/*
1716 * Get (and clear) the dirty memory log for a memory slot.
1717 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301718static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
1719 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00001720{
1721 struct kvm_memory_slot *memslot;
1722 int r;
1723 unsigned long n;
1724
1725 mutex_lock(&kvm->slots_lock);
1726
1727 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07001728 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00001729 goto out;
1730
1731 memslot = id_to_memslot(kvm->memslots, log->slot);
1732 r = -ENOENT;
1733 if (!memslot->dirty_bitmap)
1734 goto out;
1735
1736 n = kvm_dirty_bitmap_bytes(memslot);
1737 memset(memslot->dirty_bitmap, 0, n);
1738
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001739 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00001740 if (r)
1741 goto out;
1742
1743 r = -EFAULT;
1744 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1745 goto out;
1746
1747 r = 0;
1748out:
1749 mutex_unlock(&kvm->slots_lock);
1750 return r;
1751}
1752
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001753static void unpin_slot(struct kvm_memory_slot *memslot)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001754{
1755 unsigned long *physp;
1756 unsigned long j, npages, pfn;
1757 struct page *page;
1758
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001759 physp = memslot->arch.slot_phys;
1760 npages = memslot->npages;
1761 if (!physp)
1762 return;
1763 for (j = 0; j < npages; j++) {
1764 if (!(physp[j] & KVMPPC_GOT_PAGE))
1765 continue;
1766 pfn = physp[j] >> PAGE_SHIFT;
1767 page = pfn_to_page(pfn);
1768 SetPageDirty(page);
1769 put_page(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001770 }
1771}
1772
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301773static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
1774 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001775{
1776 if (!dont || free->arch.rmap != dont->arch.rmap) {
1777 vfree(free->arch.rmap);
1778 free->arch.rmap = NULL;
1779 }
1780 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1781 unpin_slot(free);
1782 vfree(free->arch.slot_phys);
1783 free->arch.slot_phys = NULL;
1784 }
1785}
1786
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301787static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
1788 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001789{
1790 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1791 if (!slot->arch.rmap)
1792 return -ENOMEM;
1793 slot->arch.slot_phys = NULL;
1794
1795 return 0;
1796}
1797
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301798static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
1799 struct kvm_memory_slot *memslot,
1800 struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001801{
1802 unsigned long *phys;
1803
1804 /* Allocate a slot_phys array if needed */
1805 phys = memslot->arch.slot_phys;
1806 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1807 phys = vzalloc(memslot->npages * sizeof(unsigned long));
1808 if (!phys)
1809 return -ENOMEM;
1810 memslot->arch.slot_phys = phys;
1811 }
1812
1813 return 0;
1814}
1815
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301816static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
1817 struct kvm_userspace_memory_region *mem,
1818 const struct kvm_memory_slot *old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001819{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001820 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1821 struct kvm_memory_slot *memslot;
1822
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001823 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001824 /*
1825 * If modifying a memslot, reset all the rmap dirty bits.
1826 * If this is a new memslot, we don't need to do anything
1827 * since the rmap array starts out as all zeroes,
1828 * i.e. no pages are dirty.
1829 */
1830 memslot = id_to_memslot(kvm->memslots, mem->slot);
1831 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1832 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001833}
1834
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001835/*
1836 * Update LPCR values in kvm->arch and in vcores.
1837 * Caller must hold kvm->lock.
1838 */
1839void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
1840{
1841 long int i;
1842 u32 cores_done = 0;
1843
1844 if ((kvm->arch.lpcr & mask) == lpcr)
1845 return;
1846
1847 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
1848
1849 for (i = 0; i < KVM_MAX_VCORES; ++i) {
1850 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
1851 if (!vc)
1852 continue;
1853 spin_lock(&vc->lock);
1854 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
1855 spin_unlock(&vc->lock);
1856 if (++cores_done >= kvm->arch.online_vcores)
1857 break;
1858 }
1859}
1860
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301861static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
1862{
1863 return;
1864}
1865
Paul Mackerras32fad282012-05-04 02:32:53 +00001866static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001867{
1868 int err = 0;
1869 struct kvm *kvm = vcpu->kvm;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301870 struct kvm_rma_info *ri = NULL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001871 unsigned long hva;
1872 struct kvm_memory_slot *memslot;
1873 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001874 unsigned long lpcr = 0, senc;
1875 unsigned long lpcr_mask = 0;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001876 unsigned long psize, porder;
1877 unsigned long rma_size;
1878 unsigned long rmls;
1879 unsigned long *physp;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001880 unsigned long i, npages;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001881 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001882
1883 mutex_lock(&kvm->lock);
1884 if (kvm->arch.rma_setup_done)
1885 goto out; /* another vcpu beat us to it */
1886
Paul Mackerras32fad282012-05-04 02:32:53 +00001887 /* Allocate hashed page table (if not done already) and reset it */
1888 if (!kvm->arch.hpt_virt) {
1889 err = kvmppc_alloc_hpt(kvm, NULL);
1890 if (err) {
1891 pr_err("KVM: Couldn't alloc HPT\n");
1892 goto out;
1893 }
1894 }
1895
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001896 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001897 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001898 memslot = gfn_to_memslot(kvm, 0);
1899
1900 /* We must have some memory at 0 by now */
1901 err = -EINVAL;
1902 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001903 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001904
1905 /* Look up the VMA for the start of this memory slot */
1906 hva = memslot->userspace_addr;
1907 down_read(&current->mm->mmap_sem);
1908 vma = find_vma(current->mm, hva);
1909 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
1910 goto up_out;
1911
1912 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001913 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001914
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001915 /* Is this one of our preallocated RMAs? */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001916 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
1917 hva == vma->vm_start)
1918 ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001919
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001920 up_read(&current->mm->mmap_sem);
1921
1922 if (!ri) {
1923 /* On POWER7, use VRMA; on PPC970, give up */
1924 err = -EPERM;
1925 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1926 pr_err("KVM: CPU requires an RMO\n");
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001927 goto out_srcu;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001928 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001929
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001930 /* We can handle 4k, 64k or 16M pages in the VRMA */
1931 err = -EINVAL;
1932 if (!(psize == 0x1000 || psize == 0x10000 ||
1933 psize == 0x1000000))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001934 goto out_srcu;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001935
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001936 /* Update VRMASD field in the LPCR */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001937 senc = slb_pgsize_encoding(psize);
Paul Mackerras697d3892011-12-12 12:36:37 +00001938 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1939 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001940 lpcr_mask = LPCR_VRMASD;
1941 /* the -4 is to account for senc values starting at 0x10 */
1942 lpcr = senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001943
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001944 /* Create HPTEs in the hash page table for the VRMA */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001945 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001946
1947 } else {
1948 /* Set up to use an RMO region */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301949 rma_size = kvm_rma_pages;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001950 if (rma_size > memslot->npages)
1951 rma_size = memslot->npages;
1952 rma_size <<= PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001953 rmls = lpcr_rmls(rma_size);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001954 err = -EINVAL;
Chen Gang5d226ae2013-07-22 14:32:35 +08001955 if ((long)rmls < 0) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001956 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001957 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001958 }
1959 atomic_inc(&ri->use_count);
1960 kvm->arch.rma = ri;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001961
1962 /* Update LPCR and RMOR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001963 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1964 /* PPC970; insert RMLS value (split field) in HID4 */
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001965 lpcr_mask = (1ul << HID4_RMLS0_SH) |
1966 (3ul << HID4_RMLS2_SH) | HID4_RMOR;
1967 lpcr = ((rmls >> 2) << HID4_RMLS0_SH) |
Paul Mackerras9e368f22011-06-29 00:40:08 +00001968 ((rmls & 3) << HID4_RMLS2_SH);
1969 /* RMOR is also in HID4 */
1970 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
1971 << HID4_RMOR_SH;
1972 } else {
1973 /* POWER7 */
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001974 lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS;
1975 lpcr = rmls << LPCR_RMLS_SH;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301976 kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001977 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001978 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001979 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001980
1981 /* Initialize phys addrs of pages in RMO */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301982 npages = kvm_rma_pages;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001983 porder = __ilog2(npages);
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001984 physp = memslot->arch.slot_phys;
1985 if (physp) {
1986 if (npages > memslot->npages)
1987 npages = memslot->npages;
1988 spin_lock(&kvm->arch.slot_phys_lock);
1989 for (i = 0; i < npages; ++i)
1990 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
1991 porder;
1992 spin_unlock(&kvm->arch.slot_phys_lock);
1993 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001994 }
1995
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001996 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
1997
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001998 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
1999 smp_wmb();
2000 kvm->arch.rma_setup_done = 1;
2001 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002002 out_srcu:
2003 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002004 out:
2005 mutex_unlock(&kvm->lock);
2006 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002007
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002008 up_out:
2009 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08002010 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002011}
2012
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302013static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002014{
Paul Mackerras32fad282012-05-04 02:32:53 +00002015 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002016
Paul Mackerras32fad282012-05-04 02:32:53 +00002017 /* Allocate the guest's logical partition ID */
2018
2019 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08002020 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00002021 return -ENOMEM;
2022 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002023
Paul Mackerras1b400ba2012-11-21 23:28:08 +00002024 /*
2025 * Since we don't flush the TLB when tearing down a VM,
2026 * and this lpid might have previously been used,
2027 * make sure we flush on each core before running the new VM.
2028 */
2029 cpumask_setall(&kvm->arch.need_tlb_flush);
2030
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002031 kvm->arch.rma = NULL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002032
Paul Mackerras9e368f22011-06-29 00:40:08 +00002033 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002034
Paul Mackerras9e368f22011-06-29 00:40:08 +00002035 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2036 /* PPC970; HID4 is effectively the LPCR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00002037 kvm->arch.host_lpid = 0;
2038 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
2039 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
2040 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
2041 ((lpid & 0xf) << HID4_LPID5_SH);
2042 } else {
2043 /* POWER7; init LPCR for virtual RMA mode */
2044 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2045 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2046 lpcr &= LPCR_PECE | LPCR_LPES;
2047 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
Paul Mackerras697d3892011-12-12 12:36:37 +00002048 LPCR_VPM0 | LPCR_VPM1;
2049 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2050 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerras9e368f22011-06-29 00:40:08 +00002051 }
2052 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002053
Paul Mackerras342d3db2011-12-12 12:38:05 +00002054 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002055 spin_lock_init(&kvm->arch.slot_phys_lock);
Paul Mackerras512691d2012-10-15 01:15:41 +00002056
2057 /*
2058 * Don't allow secondary CPU threads to come online
2059 * while any KVM VMs exist.
2060 */
2061 inhibit_secondary_onlining();
2062
David Gibson54738c02011-06-29 00:22:41 +00002063 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002064}
2065
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302066static void kvmppc_free_vcores(struct kvm *kvm)
2067{
2068 long int i;
2069
2070 for (i = 0; i < KVM_MAX_VCORES; ++i)
2071 kfree(kvm->arch.vcores[i]);
2072 kvm->arch.online_vcores = 0;
2073}
2074
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302075static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002076{
Paul Mackerras512691d2012-10-15 01:15:41 +00002077 uninhibit_secondary_onlining();
2078
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302079 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002080 if (kvm->arch.rma) {
2081 kvm_release_rma(kvm->arch.rma);
2082 kvm->arch.rma = NULL;
2083 }
2084
Paul Mackerrasde56a942011-06-29 00:21:34 +00002085 kvmppc_free_hpt(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002086}
2087
2088/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302089static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2090 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002091{
2092 return EMULATE_FAIL;
2093}
2094
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302095static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2096 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002097{
2098 return EMULATE_FAIL;
2099}
2100
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302101static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2102 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002103{
2104 return EMULATE_FAIL;
2105}
2106
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302107static int kvmppc_core_check_processor_compat_hv(void)
2108{
2109 if (!cpu_has_feature(CPU_FTR_HVMODE))
2110 return -EIO;
2111 return 0;
2112}
2113
2114static long kvm_arch_vm_ioctl_hv(struct file *filp,
2115 unsigned int ioctl, unsigned long arg)
2116{
2117 struct kvm *kvm __maybe_unused = filp->private_data;
2118 void __user *argp = (void __user *)arg;
2119 long r;
2120
2121 switch (ioctl) {
2122
2123 case KVM_ALLOCATE_RMA: {
2124 struct kvm_allocate_rma rma;
2125 struct kvm *kvm = filp->private_data;
2126
2127 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
2128 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
2129 r = -EFAULT;
2130 break;
2131 }
2132
2133 case KVM_PPC_ALLOCATE_HTAB: {
2134 u32 htab_order;
2135
2136 r = -EFAULT;
2137 if (get_user(htab_order, (u32 __user *)argp))
2138 break;
2139 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2140 if (r)
2141 break;
2142 r = -EFAULT;
2143 if (put_user(htab_order, (u32 __user *)argp))
2144 break;
2145 r = 0;
2146 break;
2147 }
2148
2149 case KVM_PPC_GET_HTAB_FD: {
2150 struct kvm_get_htab_fd ghf;
2151
2152 r = -EFAULT;
2153 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2154 break;
2155 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2156 break;
2157 }
2158
2159 default:
2160 r = -ENOTTY;
2161 }
2162
2163 return r;
2164}
2165
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302166static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302167 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2168 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2169 .get_one_reg = kvmppc_get_one_reg_hv,
2170 .set_one_reg = kvmppc_set_one_reg_hv,
2171 .vcpu_load = kvmppc_core_vcpu_load_hv,
2172 .vcpu_put = kvmppc_core_vcpu_put_hv,
2173 .set_msr = kvmppc_set_msr_hv,
2174 .vcpu_run = kvmppc_vcpu_run_hv,
2175 .vcpu_create = kvmppc_core_vcpu_create_hv,
2176 .vcpu_free = kvmppc_core_vcpu_free_hv,
2177 .check_requests = kvmppc_core_check_requests_hv,
2178 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2179 .flush_memslot = kvmppc_core_flush_memslot_hv,
2180 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2181 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2182 .unmap_hva = kvm_unmap_hva_hv,
2183 .unmap_hva_range = kvm_unmap_hva_range_hv,
2184 .age_hva = kvm_age_hva_hv,
2185 .test_age_hva = kvm_test_age_hva_hv,
2186 .set_spte_hva = kvm_set_spte_hva_hv,
2187 .mmu_destroy = kvmppc_mmu_destroy_hv,
2188 .free_memslot = kvmppc_core_free_memslot_hv,
2189 .create_memslot = kvmppc_core_create_memslot_hv,
2190 .init_vm = kvmppc_core_init_vm_hv,
2191 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302192 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2193 .emulate_op = kvmppc_core_emulate_op_hv,
2194 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2195 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2196 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2197 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
2198};
2199
2200static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002201{
2202 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302203 /*
2204 * FIXME!! Do we need to check on all cpus ?
2205 */
2206 r = kvmppc_core_check_processor_compat_hv();
2207 if (r < 0)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002208 return r;
2209
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302210 kvm_ops_hv.owner = THIS_MODULE;
2211 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002212
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302213 r = kvmppc_mmu_hv_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002214 return r;
2215}
2216
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302217static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002218{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302219 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002220}
2221
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302222module_init(kvmppc_book3s_init_hv);
2223module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05302224MODULE_LICENSE("GPL");