blob: 088a6e54c998c96fbc37703f0e408b3309d95b0b [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 Mackerras19ccb762011-07-23 17:42:46 +100070static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +000071static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +100072
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053073static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +000074{
75 int me;
76 int cpu = vcpu->cpu;
77 wait_queue_head_t *wqp;
78
79 wqp = kvm_arch_vcpu_wq(vcpu);
80 if (waitqueue_active(wqp)) {
81 wake_up_interruptible(wqp);
82 ++vcpu->stat.halt_wakeup;
83 }
84
85 me = get_cpu();
86
87 /* CPU points to the first thread of the core */
88 if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
89 int real_cpu = cpu + vcpu->arch.ptid;
90 if (paca[real_cpu].kvm_hstate.xics_phys)
91 xics_wake_cpu(real_cpu);
92 else if (cpu_online(cpu))
93 smp_send_reschedule(cpu);
94 }
95 put_cpu();
96}
97
Paul Mackerrasc7b67672012-10-15 01:18:07 +000098/*
99 * We use the vcpu_load/put functions to measure stolen time.
100 * Stolen time is counted as time when either the vcpu is able to
101 * run as part of a virtual core, but the task running the vcore
102 * is preempted or sleeping, or when the vcpu needs something done
103 * in the kernel by the task running the vcpu, but that task is
104 * preempted or sleeping. Those two things have to be counted
105 * separately, since one of the vcpu tasks will take on the job
106 * of running the core, and the other vcpu tasks in the vcore will
107 * sleep waiting for it to do that, but that sleep shouldn't count
108 * as stolen time.
109 *
110 * Hence we accumulate stolen time when the vcpu can run as part of
111 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
112 * needs its task to do other things in the kernel (for example,
113 * service a page fault) in busy_stolen. We don't accumulate
114 * stolen time for a vcore when it is inactive, or for a vcpu
115 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
116 * a misnomer; it means that the vcpu task is not executing in
117 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
118 * the kernel. We don't have any way of dividing up that time
119 * between time that the vcpu is genuinely stopped, time that
120 * the task is actively working on behalf of the vcpu, and time
121 * that the task is preempted, so we don't count any of it as
122 * stolen.
123 *
124 * Updates to busy_stolen are protected by arch.tbacct_lock;
125 * updates to vc->stolen_tb are protected by the arch.tbacct_lock
126 * of the vcpu that has taken responsibility for running the vcore
127 * (i.e. vc->runner). The stolen times are measured in units of
128 * timebase ticks. (Note that the != TB_NIL checks below are
129 * purely defensive; they should never fail.)
130 */
131
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530132static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000133{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000134 struct kvmppc_vcore *vc = vcpu->arch.vcore;
135
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000136 spin_lock(&vcpu->arch.tbacct_lock);
137 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 }
147 spin_unlock(&vcpu->arch.tbacct_lock);
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;
153
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000154 spin_lock(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000155 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
156 vc->preempt_tb = mftb();
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000157 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
158 vcpu->arch.busy_preempt = mftb();
159 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000160}
161
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530162static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000163{
164 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000165 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000166}
167
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530168void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000169{
170 vcpu->arch.pvr = pvr;
171}
172
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000173int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
174{
175 unsigned long pcr = 0;
176 struct kvmppc_vcore *vc = vcpu->arch.vcore;
177
178 if (arch_compat) {
179 if (!cpu_has_feature(CPU_FTR_ARCH_206))
180 return -EINVAL; /* 970 has no compat mode support */
181
182 switch (arch_compat) {
183 case PVR_ARCH_205:
184 pcr = PCR_ARCH_205;
185 break;
186 case PVR_ARCH_206:
187 case PVR_ARCH_206p:
188 break;
189 default:
190 return -EINVAL;
191 }
192 }
193
194 spin_lock(&vc->lock);
195 vc->arch_compat = arch_compat;
196 vc->pcr = pcr;
197 spin_unlock(&vc->lock);
198
199 return 0;
200}
201
Paul Mackerrasde56a942011-06-29 00:21:34 +0000202void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
203{
204 int r;
205
206 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
207 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
208 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
209 for (r = 0; r < 16; ++r)
210 pr_err("r%2d = %.16lx r%d = %.16lx\n",
211 r, kvmppc_get_gpr(vcpu, r),
212 r+16, kvmppc_get_gpr(vcpu, r+16));
213 pr_err("ctr = %.16lx lr = %.16lx\n",
214 vcpu->arch.ctr, vcpu->arch.lr);
215 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
216 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
217 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
218 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
219 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
220 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
221 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
222 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
223 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
224 pr_err("fault dar = %.16lx dsisr = %.8x\n",
225 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
226 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
227 for (r = 0; r < vcpu->arch.slb_max; ++r)
228 pr_err(" ESID = %.16llx VSID = %.16llx\n",
229 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
230 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000231 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000232 vcpu->arch.last_inst);
233}
234
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000235struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
236{
237 int r;
238 struct kvm_vcpu *v, *ret = NULL;
239
240 mutex_lock(&kvm->lock);
241 kvm_for_each_vcpu(r, v, kvm) {
242 if (v->vcpu_id == id) {
243 ret = v;
244 break;
245 }
246 }
247 mutex_unlock(&kvm->lock);
248 return ret;
249}
250
251static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
252{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000253 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000254 vpa->yield_count = 1;
255}
256
Paul Mackerras55b665b2012-09-25 20:33:06 +0000257static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
258 unsigned long addr, unsigned long len)
259{
260 /* check address is cacheline aligned */
261 if (addr & (L1_CACHE_BYTES - 1))
262 return -EINVAL;
263 spin_lock(&vcpu->arch.vpa_update_lock);
264 if (v->next_gpa != addr || v->len != len) {
265 v->next_gpa = addr;
266 v->len = addr ? len : 0;
267 v->update_pending = 1;
268 }
269 spin_unlock(&vcpu->arch.vpa_update_lock);
270 return 0;
271}
272
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000273/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
274struct reg_vpa {
275 u32 dummy;
276 union {
277 u16 hword;
278 u32 word;
279 } length;
280};
281
282static int vpa_is_registered(struct kvmppc_vpa *vpap)
283{
284 if (vpap->update_pending)
285 return vpap->next_gpa != 0;
286 return vpap->pinned_addr != NULL;
287}
288
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000289static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
290 unsigned long flags,
291 unsigned long vcpuid, unsigned long vpa)
292{
293 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000294 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000295 void *va;
296 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000297 int err;
298 int subfunc;
299 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000300
301 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
302 if (!tvcpu)
303 return H_PARAMETER;
304
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000305 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
306 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
307 subfunc == H_VPA_REG_SLB) {
308 /* Registering new area - address must be cache-line aligned */
309 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000310 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000311
312 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000313 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
314 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000315 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000316 if (subfunc == H_VPA_REG_VPA)
317 len = ((struct reg_vpa *)va)->length.hword;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000318 else
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000319 len = ((struct reg_vpa *)va)->length.word;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000320 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000321
322 /* Check length */
323 if (len > nb || len < sizeof(struct reg_vpa))
324 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000325 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000326 vpa = 0;
327 len = 0;
328 }
329
330 err = H_PARAMETER;
331 vpap = NULL;
332 spin_lock(&tvcpu->arch.vpa_update_lock);
333
334 switch (subfunc) {
335 case H_VPA_REG_VPA: /* register VPA */
336 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000337 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000338 vpap = &tvcpu->arch.vpa;
339 err = 0;
340 break;
341
342 case H_VPA_REG_DTL: /* register DTL */
343 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000344 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000345 len -= len % sizeof(struct dtl_entry);
346
347 /* Check that they have previously registered a VPA */
348 err = H_RESOURCE;
349 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000350 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000351
352 vpap = &tvcpu->arch.dtl;
353 err = 0;
354 break;
355
356 case H_VPA_REG_SLB: /* register SLB shadow buffer */
357 /* Check that they have previously registered a VPA */
358 err = H_RESOURCE;
359 if (!vpa_is_registered(&tvcpu->arch.vpa))
360 break;
361
362 vpap = &tvcpu->arch.slb_shadow;
363 err = 0;
364 break;
365
366 case H_VPA_DEREG_VPA: /* deregister VPA */
367 /* Check they don't still have a DTL or SLB buf registered */
368 err = H_RESOURCE;
369 if (vpa_is_registered(&tvcpu->arch.dtl) ||
370 vpa_is_registered(&tvcpu->arch.slb_shadow))
371 break;
372
373 vpap = &tvcpu->arch.vpa;
374 err = 0;
375 break;
376
377 case H_VPA_DEREG_DTL: /* deregister DTL */
378 vpap = &tvcpu->arch.dtl;
379 err = 0;
380 break;
381
382 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
383 vpap = &tvcpu->arch.slb_shadow;
384 err = 0;
385 break;
386 }
387
388 if (vpap) {
389 vpap->next_gpa = vpa;
390 vpap->len = len;
391 vpap->update_pending = 1;
392 }
393
394 spin_unlock(&tvcpu->arch.vpa_update_lock);
395
396 return err;
397}
398
Paul Mackerras081f3232012-06-01 20:20:24 +1000399static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000400{
Paul Mackerras081f3232012-06-01 20:20:24 +1000401 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000402 void *va;
403 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000404 unsigned long gpa;
405
406 /*
407 * We need to pin the page pointed to by vpap->next_gpa,
408 * but we can't call kvmppc_pin_guest_page under the lock
409 * as it does get_user_pages() and down_read(). So we
410 * have to drop the lock, pin the page, then get the lock
411 * again and check that a new area didn't get registered
412 * in the meantime.
413 */
414 for (;;) {
415 gpa = vpap->next_gpa;
416 spin_unlock(&vcpu->arch.vpa_update_lock);
417 va = NULL;
418 nb = 0;
419 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000420 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000421 spin_lock(&vcpu->arch.vpa_update_lock);
422 if (gpa == vpap->next_gpa)
423 break;
424 /* sigh... unpin that one and try again */
425 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000426 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000427 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000428
429 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000430 if (va && nb < vpap->len) {
431 /*
432 * If it's now too short, it must be that userspace
433 * has changed the mappings underlying guest memory,
434 * so unregister the region.
435 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000436 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000437 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000438 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000439 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000440 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
441 vpap->dirty);
442 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000443 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000444 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000445 if (va)
446 vpap->pinned_end = va + vpap->len;
447}
Paul Mackerras93e60242011-12-12 12:28:55 +0000448
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000449static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
450{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000451 if (!(vcpu->arch.vpa.update_pending ||
452 vcpu->arch.slb_shadow.update_pending ||
453 vcpu->arch.dtl.update_pending))
454 return;
455
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000456 spin_lock(&vcpu->arch.vpa_update_lock);
457 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000458 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000459 if (vcpu->arch.vpa.pinned_addr)
460 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000461 }
462 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000463 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000464 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
465 vcpu->arch.dtl_index = 0;
466 }
467 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000468 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000469 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000470}
471
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000472/*
473 * Return the accumulated stolen time for the vcore up until `now'.
474 * The caller should hold the vcore lock.
475 */
476static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
477{
478 u64 p;
479
480 /*
481 * If we are the task running the vcore, then since we hold
482 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
483 * can't be updated, so we don't need the tbacct_lock.
484 * If the vcore is inactive, it can't become active (since we
485 * hold the vcore lock), so the vcpu load/put functions won't
486 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
487 */
488 if (vc->vcore_state != VCORE_INACTIVE &&
489 vc->runner->arch.run_task != current) {
490 spin_lock(&vc->runner->arch.tbacct_lock);
491 p = vc->stolen_tb;
492 if (vc->preempt_tb != TB_NIL)
493 p += now - vc->preempt_tb;
494 spin_unlock(&vc->runner->arch.tbacct_lock);
495 } else {
496 p = vc->stolen_tb;
497 }
498 return p;
499}
500
Paul Mackerras0456ec42012-02-03 00:56:21 +0000501static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
502 struct kvmppc_vcore *vc)
503{
504 struct dtl_entry *dt;
505 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000506 unsigned long stolen;
507 unsigned long core_stolen;
508 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000509
510 dt = vcpu->arch.dtl_ptr;
511 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000512 now = mftb();
513 core_stolen = vcore_stolen_time(vc, now);
514 stolen = core_stolen - vcpu->arch.stolen_logged;
515 vcpu->arch.stolen_logged = core_stolen;
516 spin_lock(&vcpu->arch.tbacct_lock);
517 stolen += vcpu->arch.busy_stolen;
518 vcpu->arch.busy_stolen = 0;
519 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000520 if (!dt || !vpa)
521 return;
522 memset(dt, 0, sizeof(struct dtl_entry));
523 dt->dispatch_reason = 7;
524 dt->processor_id = vc->pcpu + vcpu->arch.ptid;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000525 dt->timebase = now + vc->tb_offset;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000526 dt->enqueue_to_dispatch_time = stolen;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000527 dt->srr0 = kvmppc_get_pc(vcpu);
528 dt->srr1 = vcpu->arch.shregs.msr;
529 ++dt;
530 if (dt == vcpu->arch.dtl.pinned_end)
531 dt = vcpu->arch.dtl.pinned_addr;
532 vcpu->arch.dtl_ptr = dt;
533 /* order writing *dt vs. writing vpa->dtl_idx */
534 smp_wmb();
535 vpa->dtl_idx = ++vcpu->arch.dtl_index;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000536 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000537}
538
539int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
540{
541 unsigned long req = kvmppc_get_gpr(vcpu, 3);
542 unsigned long target, ret = H_SUCCESS;
543 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000544 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000545
546 switch (req) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000547 case H_ENTER:
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000548 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000549 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
550 kvmppc_get_gpr(vcpu, 5),
551 kvmppc_get_gpr(vcpu, 6),
552 kvmppc_get_gpr(vcpu, 7));
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000553 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000554 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000555 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000556 break;
557 case H_PROD:
558 target = kvmppc_get_gpr(vcpu, 4);
559 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
560 if (!tvcpu) {
561 ret = H_PARAMETER;
562 break;
563 }
564 tvcpu->arch.prodded = 1;
565 smp_mb();
566 if (vcpu->arch.ceded) {
567 if (waitqueue_active(&vcpu->wq)) {
568 wake_up_interruptible(&vcpu->wq);
569 vcpu->stat.halt_wakeup++;
570 }
571 }
572 break;
573 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +1000574 target = kvmppc_get_gpr(vcpu, 4);
575 if (target == -1)
576 break;
577 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
578 if (!tvcpu) {
579 ret = H_PARAMETER;
580 break;
581 }
582 kvm_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000583 break;
584 case H_REGISTER_VPA:
585 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
586 kvmppc_get_gpr(vcpu, 5),
587 kvmppc_get_gpr(vcpu, 6));
588 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000589 case H_RTAS:
590 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
591 return RESUME_HOST;
592
593 rc = kvmppc_rtas_hcall(vcpu);
594
595 if (rc == -ENOENT)
596 return RESUME_HOST;
597 else if (rc == 0)
598 break;
599
600 /* Send the error out to userspace via KVM_RUN */
601 return rc;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000602
603 case H_XIRR:
604 case H_CPPR:
605 case H_EOI:
606 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000607 case H_IPOLL:
608 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000609 if (kvmppc_xics_enabled(vcpu)) {
610 ret = kvmppc_xics_hcall(vcpu, req);
611 break;
612 } /* fallthrough */
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000613 default:
614 return RESUME_HOST;
615 }
616 kvmppc_set_gpr(vcpu, 3, ret);
617 vcpu->arch.hcall_needed = 0;
618 return RESUME_GUEST;
619}
620
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530621static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
622 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000623{
624 int r = RESUME_HOST;
625
626 vcpu->stat.sum_exits++;
627
628 run->exit_reason = KVM_EXIT_UNKNOWN;
629 run->ready_for_interrupt_injection = 1;
630 switch (vcpu->arch.trap) {
631 /* We're good on these - the host merely wanted to get our attention */
632 case BOOK3S_INTERRUPT_HV_DECREMENTER:
633 vcpu->stat.dec_exits++;
634 r = RESUME_GUEST;
635 break;
636 case BOOK3S_INTERRUPT_EXTERNAL:
637 vcpu->stat.ext_intr_exits++;
638 r = RESUME_GUEST;
639 break;
640 case BOOK3S_INTERRUPT_PERFMON:
641 r = RESUME_GUEST;
642 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000643 case BOOK3S_INTERRUPT_MACHINE_CHECK:
644 /*
645 * Deliver a machine check interrupt to the guest.
646 * We have to do this, even if the host has handled the
647 * machine check, because machine checks use SRR0/1 and
648 * the interrupt might have trashed guest state in them.
649 */
650 kvmppc_book3s_queue_irqprio(vcpu,
651 BOOK3S_INTERRUPT_MACHINE_CHECK);
652 r = RESUME_GUEST;
653 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000654 case BOOK3S_INTERRUPT_PROGRAM:
655 {
656 ulong flags;
657 /*
658 * Normally program interrupts are delivered directly
659 * to the guest by the hardware, but we can get here
660 * as a result of a hypervisor emulation interrupt
661 * (e40) getting turned into a 700 by BML RTAS.
662 */
663 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
664 kvmppc_core_queue_program(vcpu, flags);
665 r = RESUME_GUEST;
666 break;
667 }
668 case BOOK3S_INTERRUPT_SYSCALL:
669 {
670 /* hcall - punt to userspace */
671 int i;
672
Liu Ping Fan27025a62013-11-19 14:12:48 +0800673 /* hypercall with MSR_PR has already been handled in rmode,
674 * and never reaches here.
675 */
676
Paul Mackerrasde56a942011-06-29 00:21:34 +0000677 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
678 for (i = 0; i < 9; ++i)
679 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
680 run->exit_reason = KVM_EXIT_PAPR_HCALL;
681 vcpu->arch.hcall_needed = 1;
682 r = RESUME_HOST;
683 break;
684 }
685 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000686 * We get these next two if the guest accesses a page which it thinks
687 * it has mapped but which is not actually present, either because
688 * it is for an emulated I/O device or because the corresonding
689 * host page has been paged out. Any other HDSI/HISI interrupts
690 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000691 */
692 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000693 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000694 break;
695 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000696 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
697 vcpu->arch.fault_dsisr = 0;
698 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000699 break;
700 /*
701 * This occurs if the guest executes an illegal instruction.
702 * We just generate a program interrupt to the guest, since
703 * we don't emulate any guest instructions at this stage.
704 */
705 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
706 kvmppc_core_queue_program(vcpu, 0x80000);
707 r = RESUME_GUEST;
708 break;
709 default:
710 kvmppc_dump_regs(vcpu);
711 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
712 vcpu->arch.trap, kvmppc_get_pc(vcpu),
713 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +1000714 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000715 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000716 break;
717 }
718
Paul Mackerrasde56a942011-06-29 00:21:34 +0000719 return r;
720}
721
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530722static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
723 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000724{
725 int i;
726
Paul Mackerrasde56a942011-06-29 00:21:34 +0000727 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +0530728 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000729 for (i = 0; i < vcpu->arch.slb_max; i++) {
730 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
731 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
732 }
733
734 return 0;
735}
736
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530737static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
738 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000739{
740 int i, j;
741
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530742 kvmppc_set_pvr_hv(vcpu, sregs->pvr);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000743
744 j = 0;
745 for (i = 0; i < vcpu->arch.slb_nr; i++) {
746 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
747 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
748 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
749 ++j;
750 }
751 }
752 vcpu->arch.slb_max = j;
753
754 return 0;
755}
756
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000757static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr)
758{
759 struct kvmppc_vcore *vc = vcpu->arch.vcore;
760 u64 mask;
761
762 spin_lock(&vc->lock);
763 /*
764 * Userspace can only modify DPFD (default prefetch depth),
765 * ILE (interrupt little-endian) and TC (translation control).
766 */
767 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
768 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
769 spin_unlock(&vc->lock);
770}
771
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530772static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
773 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000774{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000775 int r = 0;
776 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +0000777
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000778 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000779 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000780 *val = get_reg_val(id, 0);
781 break;
782 case KVM_REG_PPC_DABR:
783 *val = get_reg_val(id, vcpu->arch.dabr);
784 break;
785 case KVM_REG_PPC_DSCR:
786 *val = get_reg_val(id, vcpu->arch.dscr);
787 break;
788 case KVM_REG_PPC_PURR:
789 *val = get_reg_val(id, vcpu->arch.purr);
790 break;
791 case KVM_REG_PPC_SPURR:
792 *val = get_reg_val(id, vcpu->arch.spurr);
793 break;
794 case KVM_REG_PPC_AMR:
795 *val = get_reg_val(id, vcpu->arch.amr);
796 break;
797 case KVM_REG_PPC_UAMOR:
798 *val = get_reg_val(id, vcpu->arch.uamor);
799 break;
800 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
801 i = id - KVM_REG_PPC_MMCR0;
802 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
803 break;
804 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
805 i = id - KVM_REG_PPC_PMC1;
806 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +0000807 break;
Paul Mackerras14941782013-09-06 13:11:18 +1000808 case KVM_REG_PPC_SIAR:
809 *val = get_reg_val(id, vcpu->arch.siar);
810 break;
811 case KVM_REG_PPC_SDAR:
812 *val = get_reg_val(id, vcpu->arch.sdar);
813 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000814#ifdef CONFIG_VSX
815 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
816 if (cpu_has_feature(CPU_FTR_VSX)) {
817 /* VSX => FP reg i is stored in arch.vsr[2*i] */
818 long int i = id - KVM_REG_PPC_FPR0;
819 *val = get_reg_val(id, vcpu->arch.vsr[2 * i]);
820 } else {
821 /* let generic code handle it */
822 r = -EINVAL;
823 }
824 break;
825 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
826 if (cpu_has_feature(CPU_FTR_VSX)) {
827 long int i = id - KVM_REG_PPC_VSR0;
828 val->vsxval[0] = vcpu->arch.vsr[2 * i];
829 val->vsxval[1] = vcpu->arch.vsr[2 * i + 1];
830 } else {
831 r = -ENXIO;
832 }
833 break;
834#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000835 case KVM_REG_PPC_VPA_ADDR:
836 spin_lock(&vcpu->arch.vpa_update_lock);
837 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
838 spin_unlock(&vcpu->arch.vpa_update_lock);
839 break;
840 case KVM_REG_PPC_VPA_SLB:
841 spin_lock(&vcpu->arch.vpa_update_lock);
842 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
843 val->vpaval.length = vcpu->arch.slb_shadow.len;
844 spin_unlock(&vcpu->arch.vpa_update_lock);
845 break;
846 case KVM_REG_PPC_VPA_DTL:
847 spin_lock(&vcpu->arch.vpa_update_lock);
848 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
849 val->vpaval.length = vcpu->arch.dtl.len;
850 spin_unlock(&vcpu->arch.vpa_update_lock);
851 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000852 case KVM_REG_PPC_TB_OFFSET:
853 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
854 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000855 case KVM_REG_PPC_LPCR:
856 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
857 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +1000858 case KVM_REG_PPC_PPR:
859 *val = get_reg_val(id, vcpu->arch.ppr);
860 break;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000861 case KVM_REG_PPC_ARCH_COMPAT:
862 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
863 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000864 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000865 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000866 break;
867 }
868
869 return r;
870}
871
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530872static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
873 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000874{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000875 int r = 0;
876 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +0000877 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +0000878
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000879 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000880 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +0000881 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000882 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +0000883 r = -EINVAL;
884 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000885 case KVM_REG_PPC_DABR:
886 vcpu->arch.dabr = set_reg_val(id, *val);
887 break;
888 case KVM_REG_PPC_DSCR:
889 vcpu->arch.dscr = set_reg_val(id, *val);
890 break;
891 case KVM_REG_PPC_PURR:
892 vcpu->arch.purr = set_reg_val(id, *val);
893 break;
894 case KVM_REG_PPC_SPURR:
895 vcpu->arch.spurr = set_reg_val(id, *val);
896 break;
897 case KVM_REG_PPC_AMR:
898 vcpu->arch.amr = set_reg_val(id, *val);
899 break;
900 case KVM_REG_PPC_UAMOR:
901 vcpu->arch.uamor = set_reg_val(id, *val);
902 break;
903 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
904 i = id - KVM_REG_PPC_MMCR0;
905 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
906 break;
907 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
908 i = id - KVM_REG_PPC_PMC1;
909 vcpu->arch.pmc[i] = set_reg_val(id, *val);
910 break;
Paul Mackerras14941782013-09-06 13:11:18 +1000911 case KVM_REG_PPC_SIAR:
912 vcpu->arch.siar = set_reg_val(id, *val);
913 break;
914 case KVM_REG_PPC_SDAR:
915 vcpu->arch.sdar = set_reg_val(id, *val);
916 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000917#ifdef CONFIG_VSX
918 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
919 if (cpu_has_feature(CPU_FTR_VSX)) {
920 /* VSX => FP reg i is stored in arch.vsr[2*i] */
921 long int i = id - KVM_REG_PPC_FPR0;
922 vcpu->arch.vsr[2 * i] = set_reg_val(id, *val);
923 } else {
924 /* let generic code handle it */
925 r = -EINVAL;
926 }
927 break;
928 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
929 if (cpu_has_feature(CPU_FTR_VSX)) {
930 long int i = id - KVM_REG_PPC_VSR0;
931 vcpu->arch.vsr[2 * i] = val->vsxval[0];
932 vcpu->arch.vsr[2 * i + 1] = val->vsxval[1];
933 } else {
934 r = -ENXIO;
935 }
936 break;
937#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000938 case KVM_REG_PPC_VPA_ADDR:
939 addr = set_reg_val(id, *val);
940 r = -EINVAL;
941 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
942 vcpu->arch.dtl.next_gpa))
943 break;
944 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
945 break;
946 case KVM_REG_PPC_VPA_SLB:
947 addr = val->vpaval.addr;
948 len = val->vpaval.length;
949 r = -EINVAL;
950 if (addr && !vcpu->arch.vpa.next_gpa)
951 break;
952 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
953 break;
954 case KVM_REG_PPC_VPA_DTL:
955 addr = val->vpaval.addr;
956 len = val->vpaval.length;
957 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +0000958 if (addr && (len < sizeof(struct dtl_entry) ||
959 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +0000960 break;
961 len -= len % sizeof(struct dtl_entry);
962 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
963 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000964 case KVM_REG_PPC_TB_OFFSET:
965 /* round up to multiple of 2^24 */
966 vcpu->arch.vcore->tb_offset =
967 ALIGN(set_reg_val(id, *val), 1UL << 24);
968 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000969 case KVM_REG_PPC_LPCR:
970 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val));
971 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +1000972 case KVM_REG_PPC_PPR:
973 vcpu->arch.ppr = set_reg_val(id, *val);
974 break;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000975 case KVM_REG_PPC_ARCH_COMPAT:
976 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
977 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000978 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000979 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000980 break;
981 }
982
983 return r;
984}
985
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530986static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
987 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000988{
989 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000990 int err = -EINVAL;
991 int core;
992 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000993
Paul Mackerras371fefd2011-06-29 00:23:08 +0000994 core = id / threads_per_core;
995 if (core >= KVM_MAX_VCORES)
996 goto out;
997
998 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200999 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001000 if (!vcpu)
1001 goto out;
1002
1003 err = kvm_vcpu_init(vcpu, kvm, id);
1004 if (err)
1005 goto free_vcpu;
1006
1007 vcpu->arch.shared = &vcpu->arch.shregs;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001008 vcpu->arch.mmcr[0] = MMCR0_FC;
1009 vcpu->arch.ctrl = CTRL_RUNLATCH;
1010 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301011 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001012 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001013 spin_lock_init(&vcpu->arch.tbacct_lock);
1014 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001015
Paul Mackerrasde56a942011-06-29 00:21:34 +00001016 kvmppc_mmu_book3s_hv_init(vcpu);
1017
Paul Mackerras8455d792012-10-15 01:17:42 +00001018 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001019
1020 init_waitqueue_head(&vcpu->arch.cpu_run);
1021
1022 mutex_lock(&kvm->lock);
1023 vcore = kvm->arch.vcores[core];
1024 if (!vcore) {
1025 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1026 if (vcore) {
1027 INIT_LIST_HEAD(&vcore->runnable_threads);
1028 spin_lock_init(&vcore->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001029 init_waitqueue_head(&vcore->wq);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001030 vcore->preempt_tb = TB_NIL;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001031 vcore->lpcr = kvm->arch.lpcr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001032 }
1033 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001034 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001035 }
1036 mutex_unlock(&kvm->lock);
1037
1038 if (!vcore)
1039 goto free_vcpu;
1040
1041 spin_lock(&vcore->lock);
1042 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001043 spin_unlock(&vcore->lock);
1044 vcpu->arch.vcore = vcore;
1045
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001046 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1047 kvmppc_sanity_check(vcpu);
1048
Paul Mackerrasde56a942011-06-29 00:21:34 +00001049 return vcpu;
1050
1051free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001052 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001053out:
1054 return ERR_PTR(err);
1055}
1056
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001057static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1058{
1059 if (vpa->pinned_addr)
1060 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1061 vpa->dirty);
1062}
1063
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301064static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001065{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001066 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001067 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1068 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1069 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001070 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001071 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001072 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001073}
1074
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301075static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1076{
1077 /* Indicate we want to get back into the guest */
1078 return 1;
1079}
1080
Paul Mackerras19ccb762011-07-23 17:42:46 +10001081static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001082{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001083 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001084
Paul Mackerras19ccb762011-07-23 17:42:46 +10001085 now = get_tb();
1086 if (now > vcpu->arch.dec_expires) {
1087 /* decrementer has already gone negative */
1088 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001089 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001090 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001091 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001092 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1093 / tb_ticks_per_sec;
1094 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1095 HRTIMER_MODE_REL);
1096 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001097}
1098
Paul Mackerras19ccb762011-07-23 17:42:46 +10001099static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001100{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001101 vcpu->arch.ceded = 0;
1102 if (vcpu->arch.timer_running) {
1103 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1104 vcpu->arch.timer_running = 0;
1105 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001106}
1107
1108extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001109
1110static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1111 struct kvm_vcpu *vcpu)
1112{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001113 u64 now;
1114
Paul Mackerras371fefd2011-06-29 00:23:08 +00001115 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1116 return;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001117 spin_lock(&vcpu->arch.tbacct_lock);
1118 now = mftb();
1119 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1120 vcpu->arch.stolen_logged;
1121 vcpu->arch.busy_preempt = now;
1122 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1123 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001124 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001125 list_del(&vcpu->arch.run_list);
1126}
1127
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001128static int kvmppc_grab_hwthread(int cpu)
1129{
1130 struct paca_struct *tpaca;
1131 long timeout = 1000;
1132
1133 tpaca = &paca[cpu];
1134
1135 /* Ensure the thread won't go into the kernel if it wakes */
1136 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001137 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001138
1139 /*
1140 * If the thread is already executing in the kernel (e.g. handling
1141 * a stray interrupt), wait for it to get back to nap mode.
1142 * The smp_mb() is to ensure that our setting of hwthread_req
1143 * is visible before we look at hwthread_state, so if this
1144 * races with the code at system_reset_pSeries and the thread
1145 * misses our setting of hwthread_req, we are sure to see its
1146 * setting of hwthread_state, and vice versa.
1147 */
1148 smp_mb();
1149 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1150 if (--timeout <= 0) {
1151 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1152 return -EBUSY;
1153 }
1154 udelay(1);
1155 }
1156 return 0;
1157}
1158
1159static void kvmppc_release_hwthread(int cpu)
1160{
1161 struct paca_struct *tpaca;
1162
1163 tpaca = &paca[cpu];
1164 tpaca->kvm_hstate.hwthread_req = 0;
1165 tpaca->kvm_hstate.kvm_vcpu = NULL;
1166}
1167
Paul Mackerras371fefd2011-06-29 00:23:08 +00001168static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1169{
1170 int cpu;
1171 struct paca_struct *tpaca;
1172 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1173
Paul Mackerras19ccb762011-07-23 17:42:46 +10001174 if (vcpu->arch.timer_running) {
1175 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1176 vcpu->arch.timer_running = 0;
1177 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001178 cpu = vc->pcpu + vcpu->arch.ptid;
1179 tpaca = &paca[cpu];
1180 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1181 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001182 tpaca->kvm_hstate.napping = 0;
1183 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001184 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +00001185#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001186 if (vcpu->arch.ptid) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001187 xics_wake_cpu(cpu);
1188 ++vc->n_woken;
1189 }
1190#endif
1191}
1192
1193static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1194{
1195 int i;
1196
1197 HMT_low();
1198 i = 0;
1199 while (vc->nap_count < vc->n_woken) {
1200 if (++i >= 1000000) {
1201 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1202 vc->nap_count, vc->n_woken);
1203 break;
1204 }
1205 cpu_relax();
1206 }
1207 HMT_medium();
1208}
1209
1210/*
1211 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001212 * this core are off-line. Then grab the threads so they can't
1213 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001214 */
1215static int on_primary_thread(void)
1216{
1217 int cpu = smp_processor_id();
1218 int thr = cpu_thread_in_core(cpu);
1219
1220 if (thr)
1221 return 0;
1222 while (++thr < threads_per_core)
1223 if (cpu_online(cpu + thr))
1224 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001225
1226 /* Grab all hw threads so they can't go into the kernel */
1227 for (thr = 1; thr < threads_per_core; ++thr) {
1228 if (kvmppc_grab_hwthread(cpu + thr)) {
1229 /* Couldn't grab one; let the others go */
1230 do {
1231 kvmppc_release_hwthread(cpu + thr);
1232 } while (--thr > 0);
1233 return 0;
1234 }
1235 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001236 return 1;
1237}
1238
1239/*
1240 * Run a set of guest threads on a physical core.
1241 * Called with vc->lock held.
1242 */
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001243static void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001244{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001245 struct kvm_vcpu *vcpu, *vcpu0, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001246 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001247 u64 now;
Paul Mackerras081f3232012-06-01 20:20:24 +10001248 int ptid, i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001249 int srcu_idx;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001250 struct kvm_vcpu *vcpus_to_update[threads_per_core];
Paul Mackerrasde56a942011-06-29 00:21:34 +00001251
Paul Mackerras371fefd2011-06-29 00:23:08 +00001252 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +10001253 need_vpa_update = 0;
1254 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001255 if (signal_pending(vcpu->arch.run_task))
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001256 return;
1257 if (vcpu->arch.vpa.update_pending ||
1258 vcpu->arch.slb_shadow.update_pending ||
1259 vcpu->arch.dtl.update_pending)
1260 vcpus_to_update[need_vpa_update++] = vcpu;
Paul Mackerras081f3232012-06-01 20:20:24 +10001261 }
1262
1263 /*
1264 * Initialize *vc, in particular vc->vcore_state, so we can
1265 * drop the vcore lock if necessary.
1266 */
1267 vc->n_woken = 0;
1268 vc->nap_count = 0;
1269 vc->entry_exit_count = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001270 vc->vcore_state = VCORE_STARTING;
Paul Mackerras081f3232012-06-01 20:20:24 +10001271 vc->in_guest = 0;
1272 vc->napping_threads = 0;
1273
1274 /*
1275 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1276 * which can't be called with any spinlocks held.
1277 */
1278 if (need_vpa_update) {
1279 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001280 for (i = 0; i < need_vpa_update; ++i)
1281 kvmppc_update_vpas(vcpus_to_update[i]);
Paul Mackerras081f3232012-06-01 20:20:24 +10001282 spin_lock(&vc->lock);
1283 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001284
1285 /*
Paul Mackerras19ccb762011-07-23 17:42:46 +10001286 * Assign physical thread IDs, first to non-ceded vcpus
1287 * and then to ceded ones.
1288 */
1289 ptid = 0;
1290 vcpu0 = NULL;
1291 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1292 if (!vcpu->arch.ceded) {
1293 if (!ptid)
1294 vcpu0 = vcpu;
1295 vcpu->arch.ptid = ptid++;
1296 }
1297 }
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001298 if (!vcpu0)
1299 goto out; /* nothing to run; should never happen */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001300 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1301 if (vcpu->arch.ceded)
1302 vcpu->arch.ptid = ptid++;
1303
Paul Mackerras7b444c62012-10-15 01:16:14 +00001304 /*
1305 * Make sure we are running on thread 0, and that
1306 * secondary threads are offline.
1307 */
1308 if (threads_per_core > 1 && !on_primary_thread()) {
1309 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1310 vcpu->arch.ret = -EBUSY;
1311 goto out;
1312 }
1313
Paul Mackerras371fefd2011-06-29 00:23:08 +00001314 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001315 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001316 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001317 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001318 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001319
Paul Mackerras2f12f032012-10-15 01:17:17 +00001320 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001321 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001322 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001323
Paul Mackerras19ccb762011-07-23 17:42:46 +10001324 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001325
1326 srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
1327
Paul Mackerras19ccb762011-07-23 17:42:46 +10001328 __kvmppc_vcore_entry(NULL, vcpu0);
1329
Paul Mackerras371fefd2011-06-29 00:23:08 +00001330 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001331 /* disable sending of IPIs on virtual external irqs */
1332 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1333 vcpu->cpu = -1;
1334 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001335 if (vc->nap_count < vc->n_woken)
1336 kvmppc_wait_for_nap(vc);
Paul Mackerras2f12f032012-10-15 01:17:17 +00001337 for (i = 0; i < threads_per_core; ++i)
1338 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001339 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001340 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001341 spin_unlock(&vc->lock);
1342
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001343 srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
1344
Paul Mackerras371fefd2011-06-29 00:23:08 +00001345 /* make sure updates to secondary vcpu structs are visible now */
1346 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001347 kvm_guest_exit();
1348
1349 preempt_enable();
1350 kvm_resched(vcpu);
1351
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001352 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001353 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001354 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1355 /* cancel pending dec exception if dec is positive */
1356 if (now < vcpu->arch.dec_expires &&
1357 kvmppc_core_pending_dec(vcpu))
1358 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001359
1360 ret = RESUME_GUEST;
1361 if (vcpu->arch.trap)
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301362 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1363 vcpu->arch.run_task);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001364
Paul Mackerras371fefd2011-06-29 00:23:08 +00001365 vcpu->arch.ret = ret;
1366 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001367
1368 if (vcpu->arch.ceded) {
1369 if (ret != RESUME_GUEST)
1370 kvmppc_end_cede(vcpu);
1371 else
1372 kvmppc_set_timer(vcpu);
1373 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001374 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001375
Paul Mackerrasde56a942011-06-29 00:21:34 +00001376 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001377 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001378 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1379 arch.run_list) {
1380 if (vcpu->arch.ret != RESUME_GUEST) {
1381 kvmppc_remove_runnable(vc, vcpu);
1382 wake_up(&vcpu->arch.cpu_run);
1383 }
1384 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001385}
1386
Paul Mackerras19ccb762011-07-23 17:42:46 +10001387/*
1388 * Wait for some other vcpu thread to execute us, and
1389 * wake us up when we need to handle something in the host.
1390 */
1391static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001392{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001393 DEFINE_WAIT(wait);
1394
Paul Mackerras19ccb762011-07-23 17:42:46 +10001395 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1396 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1397 schedule();
1398 finish_wait(&vcpu->arch.cpu_run, &wait);
1399}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001400
Paul Mackerras19ccb762011-07-23 17:42:46 +10001401/*
1402 * All the vcpus in this vcore are idle, so wait for a decrementer
1403 * or external interrupt to one of the vcpus. vc->lock is held.
1404 */
1405static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1406{
1407 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001408
1409 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1410 vc->vcore_state = VCORE_SLEEPING;
1411 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001412 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001413 finish_wait(&vc->wq, &wait);
1414 spin_lock(&vc->lock);
1415 vc->vcore_state = VCORE_INACTIVE;
1416}
1417
1418static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1419{
1420 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001421 struct kvmppc_vcore *vc;
1422 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001423
Paul Mackerras371fefd2011-06-29 00:23:08 +00001424 kvm_run->exit_reason = 0;
1425 vcpu->arch.ret = RESUME_GUEST;
1426 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001427 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001428
Paul Mackerras371fefd2011-06-29 00:23:08 +00001429 /*
1430 * Synchronize with other threads in this virtual core
1431 */
1432 vc = vcpu->arch.vcore;
1433 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001434 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001435 vcpu->arch.run_task = current;
1436 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001437 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10001438 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001439 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001440 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1441 ++vc->n_runnable;
1442
Paul Mackerras19ccb762011-07-23 17:42:46 +10001443 /*
1444 * This happens the first time this is called for a vcpu.
1445 * If the vcore is already running, we may be able to start
1446 * this thread straight away and have it join in.
1447 */
Paul Mackerras8455d792012-10-15 01:17:42 +00001448 if (!signal_pending(current)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001449 if (vc->vcore_state == VCORE_RUNNING &&
1450 VCORE_EXIT_COUNT(vc) == 0) {
1451 vcpu->arch.ptid = vc->n_runnable - 1;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001452 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001453 kvmppc_start_thread(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00001454 } else if (vc->vcore_state == VCORE_SLEEPING) {
1455 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001456 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001457
Paul Mackerras8455d792012-10-15 01:17:42 +00001458 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001459
1460 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1461 !signal_pending(current)) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001462 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001463 spin_unlock(&vc->lock);
1464 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1465 spin_lock(&vc->lock);
1466 continue;
1467 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001468 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1469 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001470 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001471 if (signal_pending(v->arch.run_task)) {
1472 kvmppc_remove_runnable(vc, v);
1473 v->stat.signal_exits++;
1474 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1475 v->arch.ret = -EINTR;
1476 wake_up(&v->arch.cpu_run);
1477 }
1478 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001479 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1480 break;
1481 vc->runner = vcpu;
1482 n_ceded = 0;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001483 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001484 if (!v->arch.pending_exceptions)
1485 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001486 else
1487 v->arch.ceded = 0;
1488 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001489 if (n_ceded == vc->n_runnable)
1490 kvmppc_vcore_blocked(vc);
1491 else
1492 kvmppc_run_core(vc);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001493 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001494 }
1495
Paul Mackerras8455d792012-10-15 01:17:42 +00001496 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1497 (vc->vcore_state == VCORE_RUNNING ||
1498 vc->vcore_state == VCORE_EXITING)) {
1499 spin_unlock(&vc->lock);
1500 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1501 spin_lock(&vc->lock);
1502 }
1503
1504 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1505 kvmppc_remove_runnable(vc, vcpu);
1506 vcpu->stat.signal_exits++;
1507 kvm_run->exit_reason = KVM_EXIT_INTR;
1508 vcpu->arch.ret = -EINTR;
1509 }
1510
1511 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1512 /* Wake up some vcpu to run the core */
1513 v = list_first_entry(&vc->runnable_threads,
1514 struct kvm_vcpu, arch.run_list);
1515 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001516 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001517
Paul Mackerras19ccb762011-07-23 17:42:46 +10001518 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001519 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001520}
1521
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301522static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001523{
1524 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001525 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001526
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001527 if (!vcpu->arch.sane) {
1528 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1529 return -EINVAL;
1530 }
1531
Scott Wood25051b52011-11-08 18:23:23 -06001532 kvmppc_core_prepare_to_enter(vcpu);
1533
Paul Mackerras19ccb762011-07-23 17:42:46 +10001534 /* No need to go into the guest when all we'll do is come back out */
1535 if (signal_pending(current)) {
1536 run->exit_reason = KVM_EXIT_INTR;
1537 return -EINTR;
1538 }
1539
Paul Mackerras32fad282012-05-04 02:32:53 +00001540 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1541 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1542 smp_mb();
1543
1544 /* On the first time here, set up HTAB and VRMA or RMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001545 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001546 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001547 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001548 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001549 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001550
1551 flush_fp_to_thread(current);
1552 flush_altivec_to_thread(current);
1553 flush_vsx_to_thread(current);
1554 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001555 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001556 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001557
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001558 do {
1559 r = kvmppc_run_vcpu(run, vcpu);
1560
1561 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1562 !(vcpu->arch.shregs.msr & MSR_PR)) {
1563 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001564 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001565 } else if (r == RESUME_PAGE_FAULT) {
1566 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1567 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1568 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1569 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001570 }
1571 } while (r == RESUME_GUEST);
Paul Mackerras32fad282012-05-04 02:32:53 +00001572
1573 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001574 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00001575 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001576 return r;
1577}
1578
David Gibson54738c02011-06-29 00:22:41 +00001579
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001580/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +00001581 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001582static inline int lpcr_rmls(unsigned long rma_size)
1583{
1584 switch (rma_size) {
1585 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001586 if (cpu_has_feature(CPU_FTR_ARCH_206))
1587 return 8; /* only supported on POWER7 */
1588 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001589 case 64ul << 20: /* 64 MB */
1590 return 3;
1591 case 128ul << 20: /* 128 MB */
1592 return 7;
1593 case 256ul << 20: /* 256 MB */
1594 return 4;
1595 case 1ul << 30: /* 1 GB */
1596 return 2;
1597 case 16ul << 30: /* 16 GB */
1598 return 1;
1599 case 256ul << 30: /* 256 GB */
1600 return 0;
1601 default:
1602 return -1;
1603 }
1604}
1605
1606static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1607{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001608 struct page *page;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301609 struct kvm_rma_info *ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001610
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301611 if (vmf->pgoff >= kvm_rma_pages)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001612 return VM_FAULT_SIGBUS;
1613
1614 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1615 get_page(page);
1616 vmf->page = page;
1617 return 0;
1618}
1619
1620static const struct vm_operations_struct kvm_rma_vm_ops = {
1621 .fault = kvm_rma_fault,
1622};
1623
1624static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1625{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001626 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001627 vma->vm_ops = &kvm_rma_vm_ops;
1628 return 0;
1629}
1630
1631static int kvm_rma_release(struct inode *inode, struct file *filp)
1632{
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301633 struct kvm_rma_info *ri = filp->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001634
1635 kvm_release_rma(ri);
1636 return 0;
1637}
1638
Al Viro75ef9de2013-04-04 19:09:41 -04001639static const struct file_operations kvm_rma_fops = {
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001640 .mmap = kvm_rma_mmap,
1641 .release = kvm_rma_release,
1642};
1643
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301644static long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
1645 struct kvm_allocate_rma *ret)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001646{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001647 long fd;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301648 struct kvm_rma_info *ri;
1649 /*
1650 * Only do this on PPC970 in HV mode
1651 */
1652 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
1653 !cpu_has_feature(CPU_FTR_ARCH_201))
1654 return -EINVAL;
1655
1656 if (!kvm_rma_pages)
1657 return -EINVAL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001658
1659 ri = kvm_alloc_rma();
1660 if (!ri)
1661 return -ENOMEM;
1662
Yann Droneaud2f84d5e2013-08-24 22:14:08 +02001663 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001664 if (fd < 0)
1665 kvm_release_rma(ri);
1666
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301667 ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001668 return fd;
1669}
1670
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001671static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1672 int linux_psize)
1673{
1674 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1675
1676 if (!def->shift)
1677 return;
1678 (*sps)->page_shift = def->shift;
1679 (*sps)->slb_enc = def->sllp;
1680 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00001681 /*
1682 * Only return base page encoding. We don't want to return
1683 * all the supporting pte_enc, because our H_ENTER doesn't
1684 * support MPSS yet. Once they do, we can start passing all
1685 * support pte_enc here
1686 */
1687 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001688 (*sps)++;
1689}
1690
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301691static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
1692 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001693{
1694 struct kvm_ppc_one_seg_page_size *sps;
1695
1696 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1697 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1698 info->flags |= KVM_PPC_1T_SEGMENTS;
1699 info->slb_size = mmu_slb_size;
1700
1701 /* We only support these sizes for now, and no muti-size segments */
1702 sps = &info->sps[0];
1703 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1704 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1705 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1706
1707 return 0;
1708}
1709
Paul Mackerras82ed3612011-12-15 02:03:22 +00001710/*
1711 * Get (and clear) the dirty memory log for a memory slot.
1712 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301713static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
1714 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00001715{
1716 struct kvm_memory_slot *memslot;
1717 int r;
1718 unsigned long n;
1719
1720 mutex_lock(&kvm->slots_lock);
1721
1722 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07001723 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00001724 goto out;
1725
1726 memslot = id_to_memslot(kvm->memslots, log->slot);
1727 r = -ENOENT;
1728 if (!memslot->dirty_bitmap)
1729 goto out;
1730
1731 n = kvm_dirty_bitmap_bytes(memslot);
1732 memset(memslot->dirty_bitmap, 0, n);
1733
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001734 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00001735 if (r)
1736 goto out;
1737
1738 r = -EFAULT;
1739 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1740 goto out;
1741
1742 r = 0;
1743out:
1744 mutex_unlock(&kvm->slots_lock);
1745 return r;
1746}
1747
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001748static void unpin_slot(struct kvm_memory_slot *memslot)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001749{
1750 unsigned long *physp;
1751 unsigned long j, npages, pfn;
1752 struct page *page;
1753
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001754 physp = memslot->arch.slot_phys;
1755 npages = memslot->npages;
1756 if (!physp)
1757 return;
1758 for (j = 0; j < npages; j++) {
1759 if (!(physp[j] & KVMPPC_GOT_PAGE))
1760 continue;
1761 pfn = physp[j] >> PAGE_SHIFT;
1762 page = pfn_to_page(pfn);
1763 SetPageDirty(page);
1764 put_page(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001765 }
1766}
1767
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301768static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
1769 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001770{
1771 if (!dont || free->arch.rmap != dont->arch.rmap) {
1772 vfree(free->arch.rmap);
1773 free->arch.rmap = NULL;
1774 }
1775 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1776 unpin_slot(free);
1777 vfree(free->arch.slot_phys);
1778 free->arch.slot_phys = NULL;
1779 }
1780}
1781
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301782static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
1783 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001784{
1785 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1786 if (!slot->arch.rmap)
1787 return -ENOMEM;
1788 slot->arch.slot_phys = NULL;
1789
1790 return 0;
1791}
1792
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301793static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
1794 struct kvm_memory_slot *memslot,
1795 struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001796{
1797 unsigned long *phys;
1798
1799 /* Allocate a slot_phys array if needed */
1800 phys = memslot->arch.slot_phys;
1801 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1802 phys = vzalloc(memslot->npages * sizeof(unsigned long));
1803 if (!phys)
1804 return -ENOMEM;
1805 memslot->arch.slot_phys = phys;
1806 }
1807
1808 return 0;
1809}
1810
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301811static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
1812 struct kvm_userspace_memory_region *mem,
1813 const struct kvm_memory_slot *old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001814{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001815 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1816 struct kvm_memory_slot *memslot;
1817
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001818 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001819 /*
1820 * If modifying a memslot, reset all the rmap dirty bits.
1821 * If this is a new memslot, we don't need to do anything
1822 * since the rmap array starts out as all zeroes,
1823 * i.e. no pages are dirty.
1824 */
1825 memslot = id_to_memslot(kvm->memslots, mem->slot);
1826 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1827 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001828}
1829
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001830/*
1831 * Update LPCR values in kvm->arch and in vcores.
1832 * Caller must hold kvm->lock.
1833 */
1834void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
1835{
1836 long int i;
1837 u32 cores_done = 0;
1838
1839 if ((kvm->arch.lpcr & mask) == lpcr)
1840 return;
1841
1842 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
1843
1844 for (i = 0; i < KVM_MAX_VCORES; ++i) {
1845 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
1846 if (!vc)
1847 continue;
1848 spin_lock(&vc->lock);
1849 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
1850 spin_unlock(&vc->lock);
1851 if (++cores_done >= kvm->arch.online_vcores)
1852 break;
1853 }
1854}
1855
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301856static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
1857{
1858 return;
1859}
1860
Paul Mackerras32fad282012-05-04 02:32:53 +00001861static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001862{
1863 int err = 0;
1864 struct kvm *kvm = vcpu->kvm;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301865 struct kvm_rma_info *ri = NULL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001866 unsigned long hva;
1867 struct kvm_memory_slot *memslot;
1868 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001869 unsigned long lpcr = 0, senc;
1870 unsigned long lpcr_mask = 0;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001871 unsigned long psize, porder;
1872 unsigned long rma_size;
1873 unsigned long rmls;
1874 unsigned long *physp;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001875 unsigned long i, npages;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001876 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001877
1878 mutex_lock(&kvm->lock);
1879 if (kvm->arch.rma_setup_done)
1880 goto out; /* another vcpu beat us to it */
1881
Paul Mackerras32fad282012-05-04 02:32:53 +00001882 /* Allocate hashed page table (if not done already) and reset it */
1883 if (!kvm->arch.hpt_virt) {
1884 err = kvmppc_alloc_hpt(kvm, NULL);
1885 if (err) {
1886 pr_err("KVM: Couldn't alloc HPT\n");
1887 goto out;
1888 }
1889 }
1890
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001891 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001892 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001893 memslot = gfn_to_memslot(kvm, 0);
1894
1895 /* We must have some memory at 0 by now */
1896 err = -EINVAL;
1897 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001898 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001899
1900 /* Look up the VMA for the start of this memory slot */
1901 hva = memslot->userspace_addr;
1902 down_read(&current->mm->mmap_sem);
1903 vma = find_vma(current->mm, hva);
1904 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
1905 goto up_out;
1906
1907 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001908 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001909
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001910 /* Is this one of our preallocated RMAs? */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001911 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
1912 hva == vma->vm_start)
1913 ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001914
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001915 up_read(&current->mm->mmap_sem);
1916
1917 if (!ri) {
1918 /* On POWER7, use VRMA; on PPC970, give up */
1919 err = -EPERM;
1920 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1921 pr_err("KVM: CPU requires an RMO\n");
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001922 goto out_srcu;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001923 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001924
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001925 /* We can handle 4k, 64k or 16M pages in the VRMA */
1926 err = -EINVAL;
1927 if (!(psize == 0x1000 || psize == 0x10000 ||
1928 psize == 0x1000000))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001929 goto out_srcu;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001930
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001931 /* Update VRMASD field in the LPCR */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001932 senc = slb_pgsize_encoding(psize);
Paul Mackerras697d3892011-12-12 12:36:37 +00001933 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1934 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001935 lpcr_mask = LPCR_VRMASD;
1936 /* the -4 is to account for senc values starting at 0x10 */
1937 lpcr = senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001938
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001939 /* Create HPTEs in the hash page table for the VRMA */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001940 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001941
1942 } else {
1943 /* Set up to use an RMO region */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301944 rma_size = kvm_rma_pages;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001945 if (rma_size > memslot->npages)
1946 rma_size = memslot->npages;
1947 rma_size <<= PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001948 rmls = lpcr_rmls(rma_size);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001949 err = -EINVAL;
Chen Gang5d226ae2013-07-22 14:32:35 +08001950 if ((long)rmls < 0) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001951 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001952 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001953 }
1954 atomic_inc(&ri->use_count);
1955 kvm->arch.rma = ri;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001956
1957 /* Update LPCR and RMOR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001958 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1959 /* PPC970; insert RMLS value (split field) in HID4 */
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001960 lpcr_mask = (1ul << HID4_RMLS0_SH) |
1961 (3ul << HID4_RMLS2_SH) | HID4_RMOR;
1962 lpcr = ((rmls >> 2) << HID4_RMLS0_SH) |
Paul Mackerras9e368f22011-06-29 00:40:08 +00001963 ((rmls & 3) << HID4_RMLS2_SH);
1964 /* RMOR is also in HID4 */
1965 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
1966 << HID4_RMOR_SH;
1967 } else {
1968 /* POWER7 */
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001969 lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS;
1970 lpcr = rmls << LPCR_RMLS_SH;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301971 kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001972 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001973 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001974 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001975
1976 /* Initialize phys addrs of pages in RMO */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301977 npages = kvm_rma_pages;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001978 porder = __ilog2(npages);
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001979 physp = memslot->arch.slot_phys;
1980 if (physp) {
1981 if (npages > memslot->npages)
1982 npages = memslot->npages;
1983 spin_lock(&kvm->arch.slot_phys_lock);
1984 for (i = 0; i < npages; ++i)
1985 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
1986 porder;
1987 spin_unlock(&kvm->arch.slot_phys_lock);
1988 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001989 }
1990
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001991 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
1992
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001993 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
1994 smp_wmb();
1995 kvm->arch.rma_setup_done = 1;
1996 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001997 out_srcu:
1998 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001999 out:
2000 mutex_unlock(&kvm->lock);
2001 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002002
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002003 up_out:
2004 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08002005 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002006}
2007
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302008static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002009{
Paul Mackerras32fad282012-05-04 02:32:53 +00002010 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002011
Paul Mackerras32fad282012-05-04 02:32:53 +00002012 /* Allocate the guest's logical partition ID */
2013
2014 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08002015 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00002016 return -ENOMEM;
2017 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002018
Paul Mackerras1b400ba2012-11-21 23:28:08 +00002019 /*
2020 * Since we don't flush the TLB when tearing down a VM,
2021 * and this lpid might have previously been used,
2022 * make sure we flush on each core before running the new VM.
2023 */
2024 cpumask_setall(&kvm->arch.need_tlb_flush);
2025
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002026 kvm->arch.rma = NULL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002027
Paul Mackerras9e368f22011-06-29 00:40:08 +00002028 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002029
Paul Mackerras9e368f22011-06-29 00:40:08 +00002030 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2031 /* PPC970; HID4 is effectively the LPCR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00002032 kvm->arch.host_lpid = 0;
2033 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
2034 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
2035 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
2036 ((lpid & 0xf) << HID4_LPID5_SH);
2037 } else {
2038 /* POWER7; init LPCR for virtual RMA mode */
2039 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2040 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2041 lpcr &= LPCR_PECE | LPCR_LPES;
2042 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
Paul Mackerras697d3892011-12-12 12:36:37 +00002043 LPCR_VPM0 | LPCR_VPM1;
2044 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2045 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerras9e368f22011-06-29 00:40:08 +00002046 }
2047 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002048
Paul Mackerras342d3db2011-12-12 12:38:05 +00002049 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002050 spin_lock_init(&kvm->arch.slot_phys_lock);
Paul Mackerras512691d2012-10-15 01:15:41 +00002051
2052 /*
2053 * Don't allow secondary CPU threads to come online
2054 * while any KVM VMs exist.
2055 */
2056 inhibit_secondary_onlining();
2057
David Gibson54738c02011-06-29 00:22:41 +00002058 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002059}
2060
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302061static void kvmppc_free_vcores(struct kvm *kvm)
2062{
2063 long int i;
2064
2065 for (i = 0; i < KVM_MAX_VCORES; ++i)
2066 kfree(kvm->arch.vcores[i]);
2067 kvm->arch.online_vcores = 0;
2068}
2069
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302070static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002071{
Paul Mackerras512691d2012-10-15 01:15:41 +00002072 uninhibit_secondary_onlining();
2073
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302074 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002075 if (kvm->arch.rma) {
2076 kvm_release_rma(kvm->arch.rma);
2077 kvm->arch.rma = NULL;
2078 }
2079
Paul Mackerrasde56a942011-06-29 00:21:34 +00002080 kvmppc_free_hpt(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002081}
2082
2083/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302084static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2085 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002086{
2087 return EMULATE_FAIL;
2088}
2089
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302090static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2091 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002092{
2093 return EMULATE_FAIL;
2094}
2095
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302096static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2097 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002098{
2099 return EMULATE_FAIL;
2100}
2101
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302102static int kvmppc_core_check_processor_compat_hv(void)
2103{
2104 if (!cpu_has_feature(CPU_FTR_HVMODE))
2105 return -EIO;
2106 return 0;
2107}
2108
2109static long kvm_arch_vm_ioctl_hv(struct file *filp,
2110 unsigned int ioctl, unsigned long arg)
2111{
2112 struct kvm *kvm __maybe_unused = filp->private_data;
2113 void __user *argp = (void __user *)arg;
2114 long r;
2115
2116 switch (ioctl) {
2117
2118 case KVM_ALLOCATE_RMA: {
2119 struct kvm_allocate_rma rma;
2120 struct kvm *kvm = filp->private_data;
2121
2122 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
2123 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
2124 r = -EFAULT;
2125 break;
2126 }
2127
2128 case KVM_PPC_ALLOCATE_HTAB: {
2129 u32 htab_order;
2130
2131 r = -EFAULT;
2132 if (get_user(htab_order, (u32 __user *)argp))
2133 break;
2134 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2135 if (r)
2136 break;
2137 r = -EFAULT;
2138 if (put_user(htab_order, (u32 __user *)argp))
2139 break;
2140 r = 0;
2141 break;
2142 }
2143
2144 case KVM_PPC_GET_HTAB_FD: {
2145 struct kvm_get_htab_fd ghf;
2146
2147 r = -EFAULT;
2148 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2149 break;
2150 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2151 break;
2152 }
2153
2154 default:
2155 r = -ENOTTY;
2156 }
2157
2158 return r;
2159}
2160
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302161static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302162 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2163 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2164 .get_one_reg = kvmppc_get_one_reg_hv,
2165 .set_one_reg = kvmppc_set_one_reg_hv,
2166 .vcpu_load = kvmppc_core_vcpu_load_hv,
2167 .vcpu_put = kvmppc_core_vcpu_put_hv,
2168 .set_msr = kvmppc_set_msr_hv,
2169 .vcpu_run = kvmppc_vcpu_run_hv,
2170 .vcpu_create = kvmppc_core_vcpu_create_hv,
2171 .vcpu_free = kvmppc_core_vcpu_free_hv,
2172 .check_requests = kvmppc_core_check_requests_hv,
2173 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2174 .flush_memslot = kvmppc_core_flush_memslot_hv,
2175 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2176 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2177 .unmap_hva = kvm_unmap_hva_hv,
2178 .unmap_hva_range = kvm_unmap_hva_range_hv,
2179 .age_hva = kvm_age_hva_hv,
2180 .test_age_hva = kvm_test_age_hva_hv,
2181 .set_spte_hva = kvm_set_spte_hva_hv,
2182 .mmu_destroy = kvmppc_mmu_destroy_hv,
2183 .free_memslot = kvmppc_core_free_memslot_hv,
2184 .create_memslot = kvmppc_core_create_memslot_hv,
2185 .init_vm = kvmppc_core_init_vm_hv,
2186 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302187 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2188 .emulate_op = kvmppc_core_emulate_op_hv,
2189 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2190 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2191 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2192 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
2193};
2194
2195static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002196{
2197 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302198 /*
2199 * FIXME!! Do we need to check on all cpus ?
2200 */
2201 r = kvmppc_core_check_processor_compat_hv();
2202 if (r < 0)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002203 return r;
2204
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302205 kvm_ops_hv.owner = THIS_MODULE;
2206 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002207
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302208 r = kvmppc_mmu_hv_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002209 return r;
2210}
2211
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302212static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002213{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302214 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002215}
2216
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302217module_init(kvmppc_book3s_init_hv);
2218module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05302219MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01002220MODULE_ALIAS_MISCDEV(KVM_MINOR);
2221MODULE_ALIAS("devname:kvm");