blob: 3fa99b20894f3ee54cdc4b83f6743b31377c16c8 [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;
134
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000135 spin_lock(&vcpu->arch.tbacct_lock);
136 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
137 vc->preempt_tb != TB_NIL) {
Paul Mackerras0456ec42012-02-03 00:56:21 +0000138 vc->stolen_tb += mftb() - vc->preempt_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000139 vc->preempt_tb = TB_NIL;
140 }
141 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
142 vcpu->arch.busy_preempt != TB_NIL) {
143 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
144 vcpu->arch.busy_preempt = TB_NIL;
145 }
146 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000147}
148
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530149static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000150{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000151 struct kvmppc_vcore *vc = vcpu->arch.vcore;
152
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000153 spin_lock(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000154 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
155 vc->preempt_tb = mftb();
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000156 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
157 vcpu->arch.busy_preempt = mftb();
158 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000159}
160
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530161static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000162{
163 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000164 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000165}
166
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530167void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000168{
169 vcpu->arch.pvr = pvr;
170}
171
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000172int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
173{
174 unsigned long pcr = 0;
175 struct kvmppc_vcore *vc = vcpu->arch.vcore;
176
177 if (arch_compat) {
178 if (!cpu_has_feature(CPU_FTR_ARCH_206))
179 return -EINVAL; /* 970 has no compat mode support */
180
181 switch (arch_compat) {
182 case PVR_ARCH_205:
183 pcr = PCR_ARCH_205;
184 break;
185 case PVR_ARCH_206:
186 case PVR_ARCH_206p:
187 break;
188 default:
189 return -EINVAL;
190 }
191 }
192
193 spin_lock(&vc->lock);
194 vc->arch_compat = arch_compat;
195 vc->pcr = pcr;
196 spin_unlock(&vc->lock);
197
198 return 0;
199}
200
Paul Mackerrasde56a942011-06-29 00:21:34 +0000201void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
202{
203 int r;
204
205 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
206 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
207 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
208 for (r = 0; r < 16; ++r)
209 pr_err("r%2d = %.16lx r%d = %.16lx\n",
210 r, kvmppc_get_gpr(vcpu, r),
211 r+16, kvmppc_get_gpr(vcpu, r+16));
212 pr_err("ctr = %.16lx lr = %.16lx\n",
213 vcpu->arch.ctr, vcpu->arch.lr);
214 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
215 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
216 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
217 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
218 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
219 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
220 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
221 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
222 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
223 pr_err("fault dar = %.16lx dsisr = %.8x\n",
224 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
225 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
226 for (r = 0; r < vcpu->arch.slb_max; ++r)
227 pr_err(" ESID = %.16llx VSID = %.16llx\n",
228 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
229 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000230 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000231 vcpu->arch.last_inst);
232}
233
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000234struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
235{
236 int r;
237 struct kvm_vcpu *v, *ret = NULL;
238
239 mutex_lock(&kvm->lock);
240 kvm_for_each_vcpu(r, v, kvm) {
241 if (v->vcpu_id == id) {
242 ret = v;
243 break;
244 }
245 }
246 mutex_unlock(&kvm->lock);
247 return ret;
248}
249
250static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
251{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000252 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000253 vpa->yield_count = 1;
254}
255
Paul Mackerras55b665b2012-09-25 20:33:06 +0000256static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
257 unsigned long addr, unsigned long len)
258{
259 /* check address is cacheline aligned */
260 if (addr & (L1_CACHE_BYTES - 1))
261 return -EINVAL;
262 spin_lock(&vcpu->arch.vpa_update_lock);
263 if (v->next_gpa != addr || v->len != len) {
264 v->next_gpa = addr;
265 v->len = addr ? len : 0;
266 v->update_pending = 1;
267 }
268 spin_unlock(&vcpu->arch.vpa_update_lock);
269 return 0;
270}
271
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000272/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
273struct reg_vpa {
274 u32 dummy;
275 union {
276 u16 hword;
277 u32 word;
278 } length;
279};
280
281static int vpa_is_registered(struct kvmppc_vpa *vpap)
282{
283 if (vpap->update_pending)
284 return vpap->next_gpa != 0;
285 return vpap->pinned_addr != NULL;
286}
287
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000288static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
289 unsigned long flags,
290 unsigned long vcpuid, unsigned long vpa)
291{
292 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000293 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000294 void *va;
295 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000296 int err;
297 int subfunc;
298 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000299
300 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
301 if (!tvcpu)
302 return H_PARAMETER;
303
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000304 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
305 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
306 subfunc == H_VPA_REG_SLB) {
307 /* Registering new area - address must be cache-line aligned */
308 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000309 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000310
311 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000312 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
313 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000314 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000315 if (subfunc == H_VPA_REG_VPA)
316 len = ((struct reg_vpa *)va)->length.hword;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000317 else
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000318 len = ((struct reg_vpa *)va)->length.word;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000319 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000320
321 /* Check length */
322 if (len > nb || len < sizeof(struct reg_vpa))
323 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000324 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000325 vpa = 0;
326 len = 0;
327 }
328
329 err = H_PARAMETER;
330 vpap = NULL;
331 spin_lock(&tvcpu->arch.vpa_update_lock);
332
333 switch (subfunc) {
334 case H_VPA_REG_VPA: /* register VPA */
335 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000336 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000337 vpap = &tvcpu->arch.vpa;
338 err = 0;
339 break;
340
341 case H_VPA_REG_DTL: /* register DTL */
342 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000343 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000344 len -= len % sizeof(struct dtl_entry);
345
346 /* Check that they have previously registered a VPA */
347 err = H_RESOURCE;
348 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000349 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000350
351 vpap = &tvcpu->arch.dtl;
352 err = 0;
353 break;
354
355 case H_VPA_REG_SLB: /* register SLB shadow buffer */
356 /* Check that they have previously registered a VPA */
357 err = H_RESOURCE;
358 if (!vpa_is_registered(&tvcpu->arch.vpa))
359 break;
360
361 vpap = &tvcpu->arch.slb_shadow;
362 err = 0;
363 break;
364
365 case H_VPA_DEREG_VPA: /* deregister VPA */
366 /* Check they don't still have a DTL or SLB buf registered */
367 err = H_RESOURCE;
368 if (vpa_is_registered(&tvcpu->arch.dtl) ||
369 vpa_is_registered(&tvcpu->arch.slb_shadow))
370 break;
371
372 vpap = &tvcpu->arch.vpa;
373 err = 0;
374 break;
375
376 case H_VPA_DEREG_DTL: /* deregister DTL */
377 vpap = &tvcpu->arch.dtl;
378 err = 0;
379 break;
380
381 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
382 vpap = &tvcpu->arch.slb_shadow;
383 err = 0;
384 break;
385 }
386
387 if (vpap) {
388 vpap->next_gpa = vpa;
389 vpap->len = len;
390 vpap->update_pending = 1;
391 }
392
393 spin_unlock(&tvcpu->arch.vpa_update_lock);
394
395 return err;
396}
397
Paul Mackerras081f3232012-06-01 20:20:24 +1000398static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000399{
Paul Mackerras081f3232012-06-01 20:20:24 +1000400 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000401 void *va;
402 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000403 unsigned long gpa;
404
405 /*
406 * We need to pin the page pointed to by vpap->next_gpa,
407 * but we can't call kvmppc_pin_guest_page under the lock
408 * as it does get_user_pages() and down_read(). So we
409 * have to drop the lock, pin the page, then get the lock
410 * again and check that a new area didn't get registered
411 * in the meantime.
412 */
413 for (;;) {
414 gpa = vpap->next_gpa;
415 spin_unlock(&vcpu->arch.vpa_update_lock);
416 va = NULL;
417 nb = 0;
418 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000419 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000420 spin_lock(&vcpu->arch.vpa_update_lock);
421 if (gpa == vpap->next_gpa)
422 break;
423 /* sigh... unpin that one and try again */
424 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000425 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000426 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000427
428 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000429 if (va && nb < vpap->len) {
430 /*
431 * If it's now too short, it must be that userspace
432 * has changed the mappings underlying guest memory,
433 * so unregister the region.
434 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000435 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000436 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000437 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000438 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000439 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
440 vpap->dirty);
441 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000442 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000443 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000444 if (va)
445 vpap->pinned_end = va + vpap->len;
446}
Paul Mackerras93e60242011-12-12 12:28:55 +0000447
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000448static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
449{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000450 if (!(vcpu->arch.vpa.update_pending ||
451 vcpu->arch.slb_shadow.update_pending ||
452 vcpu->arch.dtl.update_pending))
453 return;
454
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000455 spin_lock(&vcpu->arch.vpa_update_lock);
456 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000457 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000458 if (vcpu->arch.vpa.pinned_addr)
459 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000460 }
461 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000462 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000463 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
464 vcpu->arch.dtl_index = 0;
465 }
466 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000467 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000468 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000469}
470
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000471/*
472 * Return the accumulated stolen time for the vcore up until `now'.
473 * The caller should hold the vcore lock.
474 */
475static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
476{
477 u64 p;
478
479 /*
480 * If we are the task running the vcore, then since we hold
481 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
482 * can't be updated, so we don't need the tbacct_lock.
483 * If the vcore is inactive, it can't become active (since we
484 * hold the vcore lock), so the vcpu load/put functions won't
485 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
486 */
487 if (vc->vcore_state != VCORE_INACTIVE &&
488 vc->runner->arch.run_task != current) {
489 spin_lock(&vc->runner->arch.tbacct_lock);
490 p = vc->stolen_tb;
491 if (vc->preempt_tb != TB_NIL)
492 p += now - vc->preempt_tb;
493 spin_unlock(&vc->runner->arch.tbacct_lock);
494 } else {
495 p = vc->stolen_tb;
496 }
497 return p;
498}
499
Paul Mackerras0456ec42012-02-03 00:56:21 +0000500static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
501 struct kvmppc_vcore *vc)
502{
503 struct dtl_entry *dt;
504 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000505 unsigned long stolen;
506 unsigned long core_stolen;
507 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000508
509 dt = vcpu->arch.dtl_ptr;
510 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000511 now = mftb();
512 core_stolen = vcore_stolen_time(vc, now);
513 stolen = core_stolen - vcpu->arch.stolen_logged;
514 vcpu->arch.stolen_logged = core_stolen;
515 spin_lock(&vcpu->arch.tbacct_lock);
516 stolen += vcpu->arch.busy_stolen;
517 vcpu->arch.busy_stolen = 0;
518 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000519 if (!dt || !vpa)
520 return;
521 memset(dt, 0, sizeof(struct dtl_entry));
522 dt->dispatch_reason = 7;
523 dt->processor_id = vc->pcpu + vcpu->arch.ptid;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000524 dt->timebase = now + vc->tb_offset;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000525 dt->enqueue_to_dispatch_time = stolen;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000526 dt->srr0 = kvmppc_get_pc(vcpu);
527 dt->srr1 = vcpu->arch.shregs.msr;
528 ++dt;
529 if (dt == vcpu->arch.dtl.pinned_end)
530 dt = vcpu->arch.dtl.pinned_addr;
531 vcpu->arch.dtl_ptr = dt;
532 /* order writing *dt vs. writing vpa->dtl_idx */
533 smp_wmb();
534 vpa->dtl_idx = ++vcpu->arch.dtl_index;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000535 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000536}
537
538int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
539{
540 unsigned long req = kvmppc_get_gpr(vcpu, 3);
541 unsigned long target, ret = H_SUCCESS;
542 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000543 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000544
545 switch (req) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000546 case H_ENTER:
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000547 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000548 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
549 kvmppc_get_gpr(vcpu, 5),
550 kvmppc_get_gpr(vcpu, 6),
551 kvmppc_get_gpr(vcpu, 7));
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000552 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000553 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000554 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000555 break;
556 case H_PROD:
557 target = kvmppc_get_gpr(vcpu, 4);
558 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
559 if (!tvcpu) {
560 ret = H_PARAMETER;
561 break;
562 }
563 tvcpu->arch.prodded = 1;
564 smp_mb();
565 if (vcpu->arch.ceded) {
566 if (waitqueue_active(&vcpu->wq)) {
567 wake_up_interruptible(&vcpu->wq);
568 vcpu->stat.halt_wakeup++;
569 }
570 }
571 break;
572 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +1000573 target = kvmppc_get_gpr(vcpu, 4);
574 if (target == -1)
575 break;
576 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
577 if (!tvcpu) {
578 ret = H_PARAMETER;
579 break;
580 }
581 kvm_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000582 break;
583 case H_REGISTER_VPA:
584 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
585 kvmppc_get_gpr(vcpu, 5),
586 kvmppc_get_gpr(vcpu, 6));
587 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000588 case H_RTAS:
589 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
590 return RESUME_HOST;
591
592 rc = kvmppc_rtas_hcall(vcpu);
593
594 if (rc == -ENOENT)
595 return RESUME_HOST;
596 else if (rc == 0)
597 break;
598
599 /* Send the error out to userspace via KVM_RUN */
600 return rc;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000601
602 case H_XIRR:
603 case H_CPPR:
604 case H_EOI:
605 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000606 case H_IPOLL:
607 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000608 if (kvmppc_xics_enabled(vcpu)) {
609 ret = kvmppc_xics_hcall(vcpu, req);
610 break;
611 } /* fallthrough */
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000612 default:
613 return RESUME_HOST;
614 }
615 kvmppc_set_gpr(vcpu, 3, ret);
616 vcpu->arch.hcall_needed = 0;
617 return RESUME_GUEST;
618}
619
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530620static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
621 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000622{
623 int r = RESUME_HOST;
624
625 vcpu->stat.sum_exits++;
626
627 run->exit_reason = KVM_EXIT_UNKNOWN;
628 run->ready_for_interrupt_injection = 1;
629 switch (vcpu->arch.trap) {
630 /* We're good on these - the host merely wanted to get our attention */
631 case BOOK3S_INTERRUPT_HV_DECREMENTER:
632 vcpu->stat.dec_exits++;
633 r = RESUME_GUEST;
634 break;
635 case BOOK3S_INTERRUPT_EXTERNAL:
636 vcpu->stat.ext_intr_exits++;
637 r = RESUME_GUEST;
638 break;
639 case BOOK3S_INTERRUPT_PERFMON:
640 r = RESUME_GUEST;
641 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000642 case BOOK3S_INTERRUPT_MACHINE_CHECK:
643 /*
644 * Deliver a machine check interrupt to the guest.
645 * We have to do this, even if the host has handled the
646 * machine check, because machine checks use SRR0/1 and
647 * the interrupt might have trashed guest state in them.
648 */
649 kvmppc_book3s_queue_irqprio(vcpu,
650 BOOK3S_INTERRUPT_MACHINE_CHECK);
651 r = RESUME_GUEST;
652 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000653 case BOOK3S_INTERRUPT_PROGRAM:
654 {
655 ulong flags;
656 /*
657 * Normally program interrupts are delivered directly
658 * to the guest by the hardware, but we can get here
659 * as a result of a hypervisor emulation interrupt
660 * (e40) getting turned into a 700 by BML RTAS.
661 */
662 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
663 kvmppc_core_queue_program(vcpu, flags);
664 r = RESUME_GUEST;
665 break;
666 }
667 case BOOK3S_INTERRUPT_SYSCALL:
668 {
669 /* hcall - punt to userspace */
670 int i;
671
672 if (vcpu->arch.shregs.msr & MSR_PR) {
673 /* sc 1 from userspace - reflect to guest syscall */
674 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
675 r = RESUME_GUEST;
676 break;
677 }
678 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
679 for (i = 0; i < 9; ++i)
680 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
681 run->exit_reason = KVM_EXIT_PAPR_HCALL;
682 vcpu->arch.hcall_needed = 1;
683 r = RESUME_HOST;
684 break;
685 }
686 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000687 * We get these next two if the guest accesses a page which it thinks
688 * it has mapped but which is not actually present, either because
689 * it is for an emulated I/O device or because the corresonding
690 * host page has been paged out. Any other HDSI/HISI interrupts
691 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000692 */
693 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000694 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000695 break;
696 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000697 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
698 vcpu->arch.fault_dsisr = 0;
699 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000700 break;
701 /*
702 * This occurs if the guest executes an illegal instruction.
703 * We just generate a program interrupt to the guest, since
704 * we don't emulate any guest instructions at this stage.
705 */
706 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
707 kvmppc_core_queue_program(vcpu, 0x80000);
708 r = RESUME_GUEST;
709 break;
710 default:
711 kvmppc_dump_regs(vcpu);
712 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
713 vcpu->arch.trap, kvmppc_get_pc(vcpu),
714 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +1000715 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000716 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000717 break;
718 }
719
Paul Mackerrasde56a942011-06-29 00:21:34 +0000720 return r;
721}
722
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530723static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
724 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000725{
726 int i;
727
Paul Mackerrasde56a942011-06-29 00:21:34 +0000728 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +0530729 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000730 for (i = 0; i < vcpu->arch.slb_max; i++) {
731 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
732 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
733 }
734
735 return 0;
736}
737
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530738static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
739 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000740{
741 int i, j;
742
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530743 kvmppc_set_pvr_hv(vcpu, sregs->pvr);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000744
745 j = 0;
746 for (i = 0; i < vcpu->arch.slb_nr; i++) {
747 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
748 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
749 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
750 ++j;
751 }
752 }
753 vcpu->arch.slb_max = j;
754
755 return 0;
756}
757
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000758static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr)
759{
760 struct kvmppc_vcore *vc = vcpu->arch.vcore;
761 u64 mask;
762
763 spin_lock(&vc->lock);
764 /*
765 * Userspace can only modify DPFD (default prefetch depth),
766 * ILE (interrupt little-endian) and TC (translation control).
767 */
768 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
769 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
770 spin_unlock(&vc->lock);
771}
772
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530773static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
774 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000775{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000776 int r = 0;
777 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +0000778
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000779 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000780 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000781 *val = get_reg_val(id, 0);
782 break;
783 case KVM_REG_PPC_DABR:
784 *val = get_reg_val(id, vcpu->arch.dabr);
785 break;
786 case KVM_REG_PPC_DSCR:
787 *val = get_reg_val(id, vcpu->arch.dscr);
788 break;
789 case KVM_REG_PPC_PURR:
790 *val = get_reg_val(id, vcpu->arch.purr);
791 break;
792 case KVM_REG_PPC_SPURR:
793 *val = get_reg_val(id, vcpu->arch.spurr);
794 break;
795 case KVM_REG_PPC_AMR:
796 *val = get_reg_val(id, vcpu->arch.amr);
797 break;
798 case KVM_REG_PPC_UAMOR:
799 *val = get_reg_val(id, vcpu->arch.uamor);
800 break;
801 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
802 i = id - KVM_REG_PPC_MMCR0;
803 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
804 break;
805 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
806 i = id - KVM_REG_PPC_PMC1;
807 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +0000808 break;
Paul Mackerras14941782013-09-06 13:11:18 +1000809 case KVM_REG_PPC_SIAR:
810 *val = get_reg_val(id, vcpu->arch.siar);
811 break;
812 case KVM_REG_PPC_SDAR:
813 *val = get_reg_val(id, vcpu->arch.sdar);
814 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000815#ifdef CONFIG_VSX
816 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
817 if (cpu_has_feature(CPU_FTR_VSX)) {
818 /* VSX => FP reg i is stored in arch.vsr[2*i] */
819 long int i = id - KVM_REG_PPC_FPR0;
820 *val = get_reg_val(id, vcpu->arch.vsr[2 * i]);
821 } else {
822 /* let generic code handle it */
823 r = -EINVAL;
824 }
825 break;
826 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
827 if (cpu_has_feature(CPU_FTR_VSX)) {
828 long int i = id - KVM_REG_PPC_VSR0;
829 val->vsxval[0] = vcpu->arch.vsr[2 * i];
830 val->vsxval[1] = vcpu->arch.vsr[2 * i + 1];
831 } else {
832 r = -ENXIO;
833 }
834 break;
835#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000836 case KVM_REG_PPC_VPA_ADDR:
837 spin_lock(&vcpu->arch.vpa_update_lock);
838 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
839 spin_unlock(&vcpu->arch.vpa_update_lock);
840 break;
841 case KVM_REG_PPC_VPA_SLB:
842 spin_lock(&vcpu->arch.vpa_update_lock);
843 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
844 val->vpaval.length = vcpu->arch.slb_shadow.len;
845 spin_unlock(&vcpu->arch.vpa_update_lock);
846 break;
847 case KVM_REG_PPC_VPA_DTL:
848 spin_lock(&vcpu->arch.vpa_update_lock);
849 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
850 val->vpaval.length = vcpu->arch.dtl.len;
851 spin_unlock(&vcpu->arch.vpa_update_lock);
852 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000853 case KVM_REG_PPC_TB_OFFSET:
854 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
855 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000856 case KVM_REG_PPC_LPCR:
857 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
858 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +1000859 case KVM_REG_PPC_PPR:
860 *val = get_reg_val(id, vcpu->arch.ppr);
861 break;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000862 case KVM_REG_PPC_ARCH_COMPAT:
863 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
864 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000865 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000866 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000867 break;
868 }
869
870 return r;
871}
872
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530873static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
874 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000875{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000876 int r = 0;
877 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +0000878 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +0000879
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000880 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000881 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +0000882 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000883 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +0000884 r = -EINVAL;
885 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000886 case KVM_REG_PPC_DABR:
887 vcpu->arch.dabr = set_reg_val(id, *val);
888 break;
889 case KVM_REG_PPC_DSCR:
890 vcpu->arch.dscr = set_reg_val(id, *val);
891 break;
892 case KVM_REG_PPC_PURR:
893 vcpu->arch.purr = set_reg_val(id, *val);
894 break;
895 case KVM_REG_PPC_SPURR:
896 vcpu->arch.spurr = set_reg_val(id, *val);
897 break;
898 case KVM_REG_PPC_AMR:
899 vcpu->arch.amr = set_reg_val(id, *val);
900 break;
901 case KVM_REG_PPC_UAMOR:
902 vcpu->arch.uamor = set_reg_val(id, *val);
903 break;
904 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
905 i = id - KVM_REG_PPC_MMCR0;
906 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
907 break;
908 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
909 i = id - KVM_REG_PPC_PMC1;
910 vcpu->arch.pmc[i] = set_reg_val(id, *val);
911 break;
Paul Mackerras14941782013-09-06 13:11:18 +1000912 case KVM_REG_PPC_SIAR:
913 vcpu->arch.siar = set_reg_val(id, *val);
914 break;
915 case KVM_REG_PPC_SDAR:
916 vcpu->arch.sdar = set_reg_val(id, *val);
917 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000918#ifdef CONFIG_VSX
919 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
920 if (cpu_has_feature(CPU_FTR_VSX)) {
921 /* VSX => FP reg i is stored in arch.vsr[2*i] */
922 long int i = id - KVM_REG_PPC_FPR0;
923 vcpu->arch.vsr[2 * i] = set_reg_val(id, *val);
924 } else {
925 /* let generic code handle it */
926 r = -EINVAL;
927 }
928 break;
929 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
930 if (cpu_has_feature(CPU_FTR_VSX)) {
931 long int i = id - KVM_REG_PPC_VSR0;
932 vcpu->arch.vsr[2 * i] = val->vsxval[0];
933 vcpu->arch.vsr[2 * i + 1] = val->vsxval[1];
934 } else {
935 r = -ENXIO;
936 }
937 break;
938#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000939 case KVM_REG_PPC_VPA_ADDR:
940 addr = set_reg_val(id, *val);
941 r = -EINVAL;
942 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
943 vcpu->arch.dtl.next_gpa))
944 break;
945 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
946 break;
947 case KVM_REG_PPC_VPA_SLB:
948 addr = val->vpaval.addr;
949 len = val->vpaval.length;
950 r = -EINVAL;
951 if (addr && !vcpu->arch.vpa.next_gpa)
952 break;
953 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
954 break;
955 case KVM_REG_PPC_VPA_DTL:
956 addr = val->vpaval.addr;
957 len = val->vpaval.length;
958 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +0000959 if (addr && (len < sizeof(struct dtl_entry) ||
960 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +0000961 break;
962 len -= len % sizeof(struct dtl_entry);
963 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
964 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000965 case KVM_REG_PPC_TB_OFFSET:
966 /* round up to multiple of 2^24 */
967 vcpu->arch.vcore->tb_offset =
968 ALIGN(set_reg_val(id, *val), 1UL << 24);
969 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000970 case KVM_REG_PPC_LPCR:
971 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val));
972 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +1000973 case KVM_REG_PPC_PPR:
974 vcpu->arch.ppr = set_reg_val(id, *val);
975 break;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000976 case KVM_REG_PPC_ARCH_COMPAT:
977 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
978 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000979 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000980 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000981 break;
982 }
983
984 return r;
985}
986
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530987static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
988 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000989{
990 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000991 int err = -EINVAL;
992 int core;
993 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000994
Paul Mackerras371fefd2011-06-29 00:23:08 +0000995 core = id / threads_per_core;
996 if (core >= KVM_MAX_VCORES)
997 goto out;
998
999 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001000 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001001 if (!vcpu)
1002 goto out;
1003
1004 err = kvm_vcpu_init(vcpu, kvm, id);
1005 if (err)
1006 goto free_vcpu;
1007
1008 vcpu->arch.shared = &vcpu->arch.shregs;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001009 vcpu->arch.mmcr[0] = MMCR0_FC;
1010 vcpu->arch.ctrl = CTRL_RUNLATCH;
1011 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301012 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001013 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001014 spin_lock_init(&vcpu->arch.tbacct_lock);
1015 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001016
Paul Mackerrasde56a942011-06-29 00:21:34 +00001017 kvmppc_mmu_book3s_hv_init(vcpu);
1018
Paul Mackerras8455d792012-10-15 01:17:42 +00001019 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001020
1021 init_waitqueue_head(&vcpu->arch.cpu_run);
1022
1023 mutex_lock(&kvm->lock);
1024 vcore = kvm->arch.vcores[core];
1025 if (!vcore) {
1026 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1027 if (vcore) {
1028 INIT_LIST_HEAD(&vcore->runnable_threads);
1029 spin_lock_init(&vcore->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001030 init_waitqueue_head(&vcore->wq);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001031 vcore->preempt_tb = TB_NIL;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001032 vcore->lpcr = kvm->arch.lpcr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001033 }
1034 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001035 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001036 }
1037 mutex_unlock(&kvm->lock);
1038
1039 if (!vcore)
1040 goto free_vcpu;
1041
1042 spin_lock(&vcore->lock);
1043 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001044 spin_unlock(&vcore->lock);
1045 vcpu->arch.vcore = vcore;
1046
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001047 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1048 kvmppc_sanity_check(vcpu);
1049
Paul Mackerrasde56a942011-06-29 00:21:34 +00001050 return vcpu;
1051
1052free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001053 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001054out:
1055 return ERR_PTR(err);
1056}
1057
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001058static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1059{
1060 if (vpa->pinned_addr)
1061 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1062 vpa->dirty);
1063}
1064
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301065static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001066{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001067 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001068 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1069 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1070 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001071 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001072 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001073 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001074}
1075
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301076static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1077{
1078 /* Indicate we want to get back into the guest */
1079 return 1;
1080}
1081
Paul Mackerras19ccb762011-07-23 17:42:46 +10001082static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001083{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001084 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001085
Paul Mackerras19ccb762011-07-23 17:42:46 +10001086 now = get_tb();
1087 if (now > vcpu->arch.dec_expires) {
1088 /* decrementer has already gone negative */
1089 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001090 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001091 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001092 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001093 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1094 / tb_ticks_per_sec;
1095 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1096 HRTIMER_MODE_REL);
1097 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001098}
1099
Paul Mackerras19ccb762011-07-23 17:42:46 +10001100static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001101{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001102 vcpu->arch.ceded = 0;
1103 if (vcpu->arch.timer_running) {
1104 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1105 vcpu->arch.timer_running = 0;
1106 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001107}
1108
1109extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001110
1111static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1112 struct kvm_vcpu *vcpu)
1113{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001114 u64 now;
1115
Paul Mackerras371fefd2011-06-29 00:23:08 +00001116 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1117 return;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001118 spin_lock(&vcpu->arch.tbacct_lock);
1119 now = mftb();
1120 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1121 vcpu->arch.stolen_logged;
1122 vcpu->arch.busy_preempt = now;
1123 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1124 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001125 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001126 list_del(&vcpu->arch.run_list);
1127}
1128
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001129static int kvmppc_grab_hwthread(int cpu)
1130{
1131 struct paca_struct *tpaca;
1132 long timeout = 1000;
1133
1134 tpaca = &paca[cpu];
1135
1136 /* Ensure the thread won't go into the kernel if it wakes */
1137 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001138 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001139
1140 /*
1141 * If the thread is already executing in the kernel (e.g. handling
1142 * a stray interrupt), wait for it to get back to nap mode.
1143 * The smp_mb() is to ensure that our setting of hwthread_req
1144 * is visible before we look at hwthread_state, so if this
1145 * races with the code at system_reset_pSeries and the thread
1146 * misses our setting of hwthread_req, we are sure to see its
1147 * setting of hwthread_state, and vice versa.
1148 */
1149 smp_mb();
1150 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1151 if (--timeout <= 0) {
1152 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1153 return -EBUSY;
1154 }
1155 udelay(1);
1156 }
1157 return 0;
1158}
1159
1160static void kvmppc_release_hwthread(int cpu)
1161{
1162 struct paca_struct *tpaca;
1163
1164 tpaca = &paca[cpu];
1165 tpaca->kvm_hstate.hwthread_req = 0;
1166 tpaca->kvm_hstate.kvm_vcpu = NULL;
1167}
1168
Paul Mackerras371fefd2011-06-29 00:23:08 +00001169static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1170{
1171 int cpu;
1172 struct paca_struct *tpaca;
1173 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1174
Paul Mackerras19ccb762011-07-23 17:42:46 +10001175 if (vcpu->arch.timer_running) {
1176 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1177 vcpu->arch.timer_running = 0;
1178 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001179 cpu = vc->pcpu + vcpu->arch.ptid;
1180 tpaca = &paca[cpu];
1181 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1182 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001183 tpaca->kvm_hstate.napping = 0;
1184 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001185 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +00001186#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001187 if (vcpu->arch.ptid) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001188 xics_wake_cpu(cpu);
1189 ++vc->n_woken;
1190 }
1191#endif
1192}
1193
1194static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1195{
1196 int i;
1197
1198 HMT_low();
1199 i = 0;
1200 while (vc->nap_count < vc->n_woken) {
1201 if (++i >= 1000000) {
1202 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1203 vc->nap_count, vc->n_woken);
1204 break;
1205 }
1206 cpu_relax();
1207 }
1208 HMT_medium();
1209}
1210
1211/*
1212 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001213 * this core are off-line. Then grab the threads so they can't
1214 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001215 */
1216static int on_primary_thread(void)
1217{
1218 int cpu = smp_processor_id();
1219 int thr = cpu_thread_in_core(cpu);
1220
1221 if (thr)
1222 return 0;
1223 while (++thr < threads_per_core)
1224 if (cpu_online(cpu + thr))
1225 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001226
1227 /* Grab all hw threads so they can't go into the kernel */
1228 for (thr = 1; thr < threads_per_core; ++thr) {
1229 if (kvmppc_grab_hwthread(cpu + thr)) {
1230 /* Couldn't grab one; let the others go */
1231 do {
1232 kvmppc_release_hwthread(cpu + thr);
1233 } while (--thr > 0);
1234 return 0;
1235 }
1236 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001237 return 1;
1238}
1239
1240/*
1241 * Run a set of guest threads on a physical core.
1242 * Called with vc->lock held.
1243 */
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001244static void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001245{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001246 struct kvm_vcpu *vcpu, *vcpu0, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001247 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001248 u64 now;
Paul Mackerras081f3232012-06-01 20:20:24 +10001249 int ptid, i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001250 int srcu_idx;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001251 struct kvm_vcpu *vcpus_to_update[threads_per_core];
Paul Mackerrasde56a942011-06-29 00:21:34 +00001252
Paul Mackerras371fefd2011-06-29 00:23:08 +00001253 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +10001254 need_vpa_update = 0;
1255 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001256 if (signal_pending(vcpu->arch.run_task))
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001257 return;
1258 if (vcpu->arch.vpa.update_pending ||
1259 vcpu->arch.slb_shadow.update_pending ||
1260 vcpu->arch.dtl.update_pending)
1261 vcpus_to_update[need_vpa_update++] = vcpu;
Paul Mackerras081f3232012-06-01 20:20:24 +10001262 }
1263
1264 /*
1265 * Initialize *vc, in particular vc->vcore_state, so we can
1266 * drop the vcore lock if necessary.
1267 */
1268 vc->n_woken = 0;
1269 vc->nap_count = 0;
1270 vc->entry_exit_count = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001271 vc->vcore_state = VCORE_STARTING;
Paul Mackerras081f3232012-06-01 20:20:24 +10001272 vc->in_guest = 0;
1273 vc->napping_threads = 0;
1274
1275 /*
1276 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1277 * which can't be called with any spinlocks held.
1278 */
1279 if (need_vpa_update) {
1280 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001281 for (i = 0; i < need_vpa_update; ++i)
1282 kvmppc_update_vpas(vcpus_to_update[i]);
Paul Mackerras081f3232012-06-01 20:20:24 +10001283 spin_lock(&vc->lock);
1284 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001285
1286 /*
Paul Mackerras19ccb762011-07-23 17:42:46 +10001287 * Assign physical thread IDs, first to non-ceded vcpus
1288 * and then to ceded ones.
1289 */
1290 ptid = 0;
1291 vcpu0 = NULL;
1292 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1293 if (!vcpu->arch.ceded) {
1294 if (!ptid)
1295 vcpu0 = vcpu;
1296 vcpu->arch.ptid = ptid++;
1297 }
1298 }
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001299 if (!vcpu0)
1300 goto out; /* nothing to run; should never happen */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001301 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1302 if (vcpu->arch.ceded)
1303 vcpu->arch.ptid = ptid++;
1304
Paul Mackerras7b444c62012-10-15 01:16:14 +00001305 /*
1306 * Make sure we are running on thread 0, and that
1307 * secondary threads are offline.
1308 */
1309 if (threads_per_core > 1 && !on_primary_thread()) {
1310 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1311 vcpu->arch.ret = -EBUSY;
1312 goto out;
1313 }
1314
Paul Mackerras371fefd2011-06-29 00:23:08 +00001315 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001316 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001317 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001318 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001319 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001320
Paul Mackerras2f12f032012-10-15 01:17:17 +00001321 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001322 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001323 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001324
Paul Mackerras19ccb762011-07-23 17:42:46 +10001325 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001326
1327 srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
1328
Paul Mackerras19ccb762011-07-23 17:42:46 +10001329 __kvmppc_vcore_entry(NULL, vcpu0);
1330
Paul Mackerras371fefd2011-06-29 00:23:08 +00001331 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001332 /* disable sending of IPIs on virtual external irqs */
1333 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1334 vcpu->cpu = -1;
1335 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001336 if (vc->nap_count < vc->n_woken)
1337 kvmppc_wait_for_nap(vc);
Paul Mackerras2f12f032012-10-15 01:17:17 +00001338 for (i = 0; i < threads_per_core; ++i)
1339 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001340 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001341 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001342 spin_unlock(&vc->lock);
1343
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001344 srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
1345
Paul Mackerras371fefd2011-06-29 00:23:08 +00001346 /* make sure updates to secondary vcpu structs are visible now */
1347 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001348 kvm_guest_exit();
1349
1350 preempt_enable();
Takuya Yoshikawac08ac062013-12-13 15:07:21 +09001351 cond_resched();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001352
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001353 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001354 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001355 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1356 /* cancel pending dec exception if dec is positive */
1357 if (now < vcpu->arch.dec_expires &&
1358 kvmppc_core_pending_dec(vcpu))
1359 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001360
1361 ret = RESUME_GUEST;
1362 if (vcpu->arch.trap)
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301363 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1364 vcpu->arch.run_task);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001365
Paul Mackerras371fefd2011-06-29 00:23:08 +00001366 vcpu->arch.ret = ret;
1367 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001368
1369 if (vcpu->arch.ceded) {
1370 if (ret != RESUME_GUEST)
1371 kvmppc_end_cede(vcpu);
1372 else
1373 kvmppc_set_timer(vcpu);
1374 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001375 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001376
Paul Mackerrasde56a942011-06-29 00:21:34 +00001377 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001378 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001379 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1380 arch.run_list) {
1381 if (vcpu->arch.ret != RESUME_GUEST) {
1382 kvmppc_remove_runnable(vc, vcpu);
1383 wake_up(&vcpu->arch.cpu_run);
1384 }
1385 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001386}
1387
Paul Mackerras19ccb762011-07-23 17:42:46 +10001388/*
1389 * Wait for some other vcpu thread to execute us, and
1390 * wake us up when we need to handle something in the host.
1391 */
1392static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001393{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001394 DEFINE_WAIT(wait);
1395
Paul Mackerras19ccb762011-07-23 17:42:46 +10001396 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1397 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1398 schedule();
1399 finish_wait(&vcpu->arch.cpu_run, &wait);
1400}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001401
Paul Mackerras19ccb762011-07-23 17:42:46 +10001402/*
1403 * All the vcpus in this vcore are idle, so wait for a decrementer
1404 * or external interrupt to one of the vcpus. vc->lock is held.
1405 */
1406static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1407{
1408 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001409
1410 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1411 vc->vcore_state = VCORE_SLEEPING;
1412 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001413 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001414 finish_wait(&vc->wq, &wait);
1415 spin_lock(&vc->lock);
1416 vc->vcore_state = VCORE_INACTIVE;
1417}
1418
1419static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1420{
1421 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001422 struct kvmppc_vcore *vc;
1423 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001424
Paul Mackerras371fefd2011-06-29 00:23:08 +00001425 kvm_run->exit_reason = 0;
1426 vcpu->arch.ret = RESUME_GUEST;
1427 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001428 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001429
Paul Mackerras371fefd2011-06-29 00:23:08 +00001430 /*
1431 * Synchronize with other threads in this virtual core
1432 */
1433 vc = vcpu->arch.vcore;
1434 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001435 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001436 vcpu->arch.run_task = current;
1437 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001438 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10001439 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001440 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001441 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1442 ++vc->n_runnable;
1443
Paul Mackerras19ccb762011-07-23 17:42:46 +10001444 /*
1445 * This happens the first time this is called for a vcpu.
1446 * If the vcore is already running, we may be able to start
1447 * this thread straight away and have it join in.
1448 */
Paul Mackerras8455d792012-10-15 01:17:42 +00001449 if (!signal_pending(current)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001450 if (vc->vcore_state == VCORE_RUNNING &&
1451 VCORE_EXIT_COUNT(vc) == 0) {
1452 vcpu->arch.ptid = vc->n_runnable - 1;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001453 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001454 kvmppc_start_thread(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00001455 } else if (vc->vcore_state == VCORE_SLEEPING) {
1456 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001457 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001458
Paul Mackerras8455d792012-10-15 01:17:42 +00001459 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001460
1461 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1462 !signal_pending(current)) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001463 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001464 spin_unlock(&vc->lock);
1465 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1466 spin_lock(&vc->lock);
1467 continue;
1468 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001469 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1470 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001471 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001472 if (signal_pending(v->arch.run_task)) {
1473 kvmppc_remove_runnable(vc, v);
1474 v->stat.signal_exits++;
1475 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1476 v->arch.ret = -EINTR;
1477 wake_up(&v->arch.cpu_run);
1478 }
1479 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001480 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1481 break;
1482 vc->runner = vcpu;
1483 n_ceded = 0;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001484 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001485 if (!v->arch.pending_exceptions)
1486 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001487 else
1488 v->arch.ceded = 0;
1489 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001490 if (n_ceded == vc->n_runnable)
1491 kvmppc_vcore_blocked(vc);
1492 else
1493 kvmppc_run_core(vc);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001494 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001495 }
1496
Paul Mackerras8455d792012-10-15 01:17:42 +00001497 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1498 (vc->vcore_state == VCORE_RUNNING ||
1499 vc->vcore_state == VCORE_EXITING)) {
1500 spin_unlock(&vc->lock);
1501 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1502 spin_lock(&vc->lock);
1503 }
1504
1505 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1506 kvmppc_remove_runnable(vc, vcpu);
1507 vcpu->stat.signal_exits++;
1508 kvm_run->exit_reason = KVM_EXIT_INTR;
1509 vcpu->arch.ret = -EINTR;
1510 }
1511
1512 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1513 /* Wake up some vcpu to run the core */
1514 v = list_first_entry(&vc->runnable_threads,
1515 struct kvm_vcpu, arch.run_list);
1516 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001517 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001518
Paul Mackerras19ccb762011-07-23 17:42:46 +10001519 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001520 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001521}
1522
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301523static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001524{
1525 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001526 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001527
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001528 if (!vcpu->arch.sane) {
1529 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1530 return -EINVAL;
1531 }
1532
Scott Wood25051b52011-11-08 18:23:23 -06001533 kvmppc_core_prepare_to_enter(vcpu);
1534
Paul Mackerras19ccb762011-07-23 17:42:46 +10001535 /* No need to go into the guest when all we'll do is come back out */
1536 if (signal_pending(current)) {
1537 run->exit_reason = KVM_EXIT_INTR;
1538 return -EINTR;
1539 }
1540
Paul Mackerras32fad282012-05-04 02:32:53 +00001541 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1542 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1543 smp_mb();
1544
1545 /* On the first time here, set up HTAB and VRMA or RMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001546 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001547 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001548 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001549 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001550 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001551
1552 flush_fp_to_thread(current);
1553 flush_altivec_to_thread(current);
1554 flush_vsx_to_thread(current);
1555 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001556 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001557 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001558
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001559 do {
1560 r = kvmppc_run_vcpu(run, vcpu);
1561
1562 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1563 !(vcpu->arch.shregs.msr & MSR_PR)) {
1564 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001565 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001566 } else if (r == RESUME_PAGE_FAULT) {
1567 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1568 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1569 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1570 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001571 }
1572 } while (r == RESUME_GUEST);
Paul Mackerras32fad282012-05-04 02:32:53 +00001573
1574 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001575 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00001576 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001577 return r;
1578}
1579
David Gibson54738c02011-06-29 00:22:41 +00001580
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001581/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +00001582 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001583static inline int lpcr_rmls(unsigned long rma_size)
1584{
1585 switch (rma_size) {
1586 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001587 if (cpu_has_feature(CPU_FTR_ARCH_206))
1588 return 8; /* only supported on POWER7 */
1589 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001590 case 64ul << 20: /* 64 MB */
1591 return 3;
1592 case 128ul << 20: /* 128 MB */
1593 return 7;
1594 case 256ul << 20: /* 256 MB */
1595 return 4;
1596 case 1ul << 30: /* 1 GB */
1597 return 2;
1598 case 16ul << 30: /* 16 GB */
1599 return 1;
1600 case 256ul << 30: /* 256 GB */
1601 return 0;
1602 default:
1603 return -1;
1604 }
1605}
1606
1607static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1608{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001609 struct page *page;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301610 struct kvm_rma_info *ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001611
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301612 if (vmf->pgoff >= kvm_rma_pages)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001613 return VM_FAULT_SIGBUS;
1614
1615 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1616 get_page(page);
1617 vmf->page = page;
1618 return 0;
1619}
1620
1621static const struct vm_operations_struct kvm_rma_vm_ops = {
1622 .fault = kvm_rma_fault,
1623};
1624
1625static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1626{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001627 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001628 vma->vm_ops = &kvm_rma_vm_ops;
1629 return 0;
1630}
1631
1632static int kvm_rma_release(struct inode *inode, struct file *filp)
1633{
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301634 struct kvm_rma_info *ri = filp->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001635
1636 kvm_release_rma(ri);
1637 return 0;
1638}
1639
Al Viro75ef9de2013-04-04 19:09:41 -04001640static const struct file_operations kvm_rma_fops = {
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001641 .mmap = kvm_rma_mmap,
1642 .release = kvm_rma_release,
1643};
1644
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301645static long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
1646 struct kvm_allocate_rma *ret)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001647{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001648 long fd;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301649 struct kvm_rma_info *ri;
1650 /*
1651 * Only do this on PPC970 in HV mode
1652 */
1653 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
1654 !cpu_has_feature(CPU_FTR_ARCH_201))
1655 return -EINVAL;
1656
1657 if (!kvm_rma_pages)
1658 return -EINVAL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001659
1660 ri = kvm_alloc_rma();
1661 if (!ri)
1662 return -ENOMEM;
1663
Yann Droneaud2f84d5e2013-08-24 22:14:08 +02001664 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001665 if (fd < 0)
1666 kvm_release_rma(ri);
1667
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301668 ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001669 return fd;
1670}
1671
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001672static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1673 int linux_psize)
1674{
1675 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1676
1677 if (!def->shift)
1678 return;
1679 (*sps)->page_shift = def->shift;
1680 (*sps)->slb_enc = def->sllp;
1681 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00001682 /*
1683 * Only return base page encoding. We don't want to return
1684 * all the supporting pte_enc, because our H_ENTER doesn't
1685 * support MPSS yet. Once they do, we can start passing all
1686 * support pte_enc here
1687 */
1688 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001689 (*sps)++;
1690}
1691
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301692static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
1693 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001694{
1695 struct kvm_ppc_one_seg_page_size *sps;
1696
1697 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1698 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1699 info->flags |= KVM_PPC_1T_SEGMENTS;
1700 info->slb_size = mmu_slb_size;
1701
1702 /* We only support these sizes for now, and no muti-size segments */
1703 sps = &info->sps[0];
1704 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1705 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1706 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1707
1708 return 0;
1709}
1710
Paul Mackerras82ed3612011-12-15 02:03:22 +00001711/*
1712 * Get (and clear) the dirty memory log for a memory slot.
1713 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301714static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
1715 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00001716{
1717 struct kvm_memory_slot *memslot;
1718 int r;
1719 unsigned long n;
1720
1721 mutex_lock(&kvm->slots_lock);
1722
1723 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07001724 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00001725 goto out;
1726
1727 memslot = id_to_memslot(kvm->memslots, log->slot);
1728 r = -ENOENT;
1729 if (!memslot->dirty_bitmap)
1730 goto out;
1731
1732 n = kvm_dirty_bitmap_bytes(memslot);
1733 memset(memslot->dirty_bitmap, 0, n);
1734
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001735 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00001736 if (r)
1737 goto out;
1738
1739 r = -EFAULT;
1740 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1741 goto out;
1742
1743 r = 0;
1744out:
1745 mutex_unlock(&kvm->slots_lock);
1746 return r;
1747}
1748
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001749static void unpin_slot(struct kvm_memory_slot *memslot)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001750{
1751 unsigned long *physp;
1752 unsigned long j, npages, pfn;
1753 struct page *page;
1754
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001755 physp = memslot->arch.slot_phys;
1756 npages = memslot->npages;
1757 if (!physp)
1758 return;
1759 for (j = 0; j < npages; j++) {
1760 if (!(physp[j] & KVMPPC_GOT_PAGE))
1761 continue;
1762 pfn = physp[j] >> PAGE_SHIFT;
1763 page = pfn_to_page(pfn);
1764 SetPageDirty(page);
1765 put_page(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001766 }
1767}
1768
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301769static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
1770 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001771{
1772 if (!dont || free->arch.rmap != dont->arch.rmap) {
1773 vfree(free->arch.rmap);
1774 free->arch.rmap = NULL;
1775 }
1776 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1777 unpin_slot(free);
1778 vfree(free->arch.slot_phys);
1779 free->arch.slot_phys = NULL;
1780 }
1781}
1782
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301783static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
1784 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001785{
1786 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1787 if (!slot->arch.rmap)
1788 return -ENOMEM;
1789 slot->arch.slot_phys = NULL;
1790
1791 return 0;
1792}
1793
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301794static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
1795 struct kvm_memory_slot *memslot,
1796 struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001797{
1798 unsigned long *phys;
1799
1800 /* Allocate a slot_phys array if needed */
1801 phys = memslot->arch.slot_phys;
1802 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1803 phys = vzalloc(memslot->npages * sizeof(unsigned long));
1804 if (!phys)
1805 return -ENOMEM;
1806 memslot->arch.slot_phys = phys;
1807 }
1808
1809 return 0;
1810}
1811
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301812static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
1813 struct kvm_userspace_memory_region *mem,
1814 const struct kvm_memory_slot *old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001815{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001816 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1817 struct kvm_memory_slot *memslot;
1818
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001819 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001820 /*
1821 * If modifying a memslot, reset all the rmap dirty bits.
1822 * If this is a new memslot, we don't need to do anything
1823 * since the rmap array starts out as all zeroes,
1824 * i.e. no pages are dirty.
1825 */
1826 memslot = id_to_memslot(kvm->memslots, mem->slot);
1827 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1828 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001829}
1830
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001831/*
1832 * Update LPCR values in kvm->arch and in vcores.
1833 * Caller must hold kvm->lock.
1834 */
1835void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
1836{
1837 long int i;
1838 u32 cores_done = 0;
1839
1840 if ((kvm->arch.lpcr & mask) == lpcr)
1841 return;
1842
1843 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
1844
1845 for (i = 0; i < KVM_MAX_VCORES; ++i) {
1846 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
1847 if (!vc)
1848 continue;
1849 spin_lock(&vc->lock);
1850 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
1851 spin_unlock(&vc->lock);
1852 if (++cores_done >= kvm->arch.online_vcores)
1853 break;
1854 }
1855}
1856
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301857static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
1858{
1859 return;
1860}
1861
Paul Mackerras32fad282012-05-04 02:32:53 +00001862static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001863{
1864 int err = 0;
1865 struct kvm *kvm = vcpu->kvm;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301866 struct kvm_rma_info *ri = NULL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001867 unsigned long hva;
1868 struct kvm_memory_slot *memslot;
1869 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001870 unsigned long lpcr = 0, senc;
1871 unsigned long lpcr_mask = 0;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001872 unsigned long psize, porder;
1873 unsigned long rma_size;
1874 unsigned long rmls;
1875 unsigned long *physp;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001876 unsigned long i, npages;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001877 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001878
1879 mutex_lock(&kvm->lock);
1880 if (kvm->arch.rma_setup_done)
1881 goto out; /* another vcpu beat us to it */
1882
Paul Mackerras32fad282012-05-04 02:32:53 +00001883 /* Allocate hashed page table (if not done already) and reset it */
1884 if (!kvm->arch.hpt_virt) {
1885 err = kvmppc_alloc_hpt(kvm, NULL);
1886 if (err) {
1887 pr_err("KVM: Couldn't alloc HPT\n");
1888 goto out;
1889 }
1890 }
1891
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001892 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001893 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001894 memslot = gfn_to_memslot(kvm, 0);
1895
1896 /* We must have some memory at 0 by now */
1897 err = -EINVAL;
1898 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001899 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001900
1901 /* Look up the VMA for the start of this memory slot */
1902 hva = memslot->userspace_addr;
1903 down_read(&current->mm->mmap_sem);
1904 vma = find_vma(current->mm, hva);
1905 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
1906 goto up_out;
1907
1908 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001909 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001910
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001911 /* Is this one of our preallocated RMAs? */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001912 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
1913 hva == vma->vm_start)
1914 ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001915
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001916 up_read(&current->mm->mmap_sem);
1917
1918 if (!ri) {
1919 /* On POWER7, use VRMA; on PPC970, give up */
1920 err = -EPERM;
1921 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1922 pr_err("KVM: CPU requires an RMO\n");
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001923 goto out_srcu;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001924 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001925
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001926 /* We can handle 4k, 64k or 16M pages in the VRMA */
1927 err = -EINVAL;
1928 if (!(psize == 0x1000 || psize == 0x10000 ||
1929 psize == 0x1000000))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001930 goto out_srcu;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001931
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001932 /* Update VRMASD field in the LPCR */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001933 senc = slb_pgsize_encoding(psize);
Paul Mackerras697d3892011-12-12 12:36:37 +00001934 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1935 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001936 lpcr_mask = LPCR_VRMASD;
1937 /* the -4 is to account for senc values starting at 0x10 */
1938 lpcr = senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001939
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001940 /* Create HPTEs in the hash page table for the VRMA */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001941 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001942
1943 } else {
1944 /* Set up to use an RMO region */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301945 rma_size = kvm_rma_pages;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001946 if (rma_size > memslot->npages)
1947 rma_size = memslot->npages;
1948 rma_size <<= PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001949 rmls = lpcr_rmls(rma_size);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001950 err = -EINVAL;
Chen Gang5d226ae2013-07-22 14:32:35 +08001951 if ((long)rmls < 0) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001952 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001953 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001954 }
1955 atomic_inc(&ri->use_count);
1956 kvm->arch.rma = ri;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001957
1958 /* Update LPCR and RMOR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001959 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1960 /* PPC970; insert RMLS value (split field) in HID4 */
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001961 lpcr_mask = (1ul << HID4_RMLS0_SH) |
1962 (3ul << HID4_RMLS2_SH) | HID4_RMOR;
1963 lpcr = ((rmls >> 2) << HID4_RMLS0_SH) |
Paul Mackerras9e368f22011-06-29 00:40:08 +00001964 ((rmls & 3) << HID4_RMLS2_SH);
1965 /* RMOR is also in HID4 */
1966 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
1967 << HID4_RMOR_SH;
1968 } else {
1969 /* POWER7 */
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001970 lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS;
1971 lpcr = rmls << LPCR_RMLS_SH;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301972 kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001973 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001974 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001975 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001976
1977 /* Initialize phys addrs of pages in RMO */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301978 npages = kvm_rma_pages;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001979 porder = __ilog2(npages);
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001980 physp = memslot->arch.slot_phys;
1981 if (physp) {
1982 if (npages > memslot->npages)
1983 npages = memslot->npages;
1984 spin_lock(&kvm->arch.slot_phys_lock);
1985 for (i = 0; i < npages; ++i)
1986 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
1987 porder;
1988 spin_unlock(&kvm->arch.slot_phys_lock);
1989 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001990 }
1991
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001992 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
1993
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001994 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
1995 smp_wmb();
1996 kvm->arch.rma_setup_done = 1;
1997 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001998 out_srcu:
1999 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002000 out:
2001 mutex_unlock(&kvm->lock);
2002 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002003
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002004 up_out:
2005 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08002006 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002007}
2008
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302009static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002010{
Paul Mackerras32fad282012-05-04 02:32:53 +00002011 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002012
Paul Mackerras32fad282012-05-04 02:32:53 +00002013 /* Allocate the guest's logical partition ID */
2014
2015 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08002016 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00002017 return -ENOMEM;
2018 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002019
Paul Mackerras1b400ba2012-11-21 23:28:08 +00002020 /*
2021 * Since we don't flush the TLB when tearing down a VM,
2022 * and this lpid might have previously been used,
2023 * make sure we flush on each core before running the new VM.
2024 */
2025 cpumask_setall(&kvm->arch.need_tlb_flush);
2026
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002027 kvm->arch.rma = NULL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002028
Paul Mackerras9e368f22011-06-29 00:40:08 +00002029 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002030
Paul Mackerras9e368f22011-06-29 00:40:08 +00002031 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2032 /* PPC970; HID4 is effectively the LPCR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00002033 kvm->arch.host_lpid = 0;
2034 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
2035 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
2036 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
2037 ((lpid & 0xf) << HID4_LPID5_SH);
2038 } else {
2039 /* POWER7; init LPCR for virtual RMA mode */
2040 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2041 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2042 lpcr &= LPCR_PECE | LPCR_LPES;
2043 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
Paul Mackerras697d3892011-12-12 12:36:37 +00002044 LPCR_VPM0 | LPCR_VPM1;
2045 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2046 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerras9e368f22011-06-29 00:40:08 +00002047 }
2048 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002049
Paul Mackerras342d3db2011-12-12 12:38:05 +00002050 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002051 spin_lock_init(&kvm->arch.slot_phys_lock);
Paul Mackerras512691d2012-10-15 01:15:41 +00002052
2053 /*
2054 * Don't allow secondary CPU threads to come online
2055 * while any KVM VMs exist.
2056 */
2057 inhibit_secondary_onlining();
2058
David Gibson54738c02011-06-29 00:22:41 +00002059 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002060}
2061
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302062static void kvmppc_free_vcores(struct kvm *kvm)
2063{
2064 long int i;
2065
2066 for (i = 0; i < KVM_MAX_VCORES; ++i)
2067 kfree(kvm->arch.vcores[i]);
2068 kvm->arch.online_vcores = 0;
2069}
2070
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302071static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002072{
Paul Mackerras512691d2012-10-15 01:15:41 +00002073 uninhibit_secondary_onlining();
2074
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302075 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002076 if (kvm->arch.rma) {
2077 kvm_release_rma(kvm->arch.rma);
2078 kvm->arch.rma = NULL;
2079 }
2080
Paul Mackerrasde56a942011-06-29 00:21:34 +00002081 kvmppc_free_hpt(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002082}
2083
2084/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302085static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2086 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002087{
2088 return EMULATE_FAIL;
2089}
2090
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302091static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2092 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002093{
2094 return EMULATE_FAIL;
2095}
2096
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302097static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2098 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002099{
2100 return EMULATE_FAIL;
2101}
2102
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302103static int kvmppc_core_check_processor_compat_hv(void)
2104{
2105 if (!cpu_has_feature(CPU_FTR_HVMODE))
2106 return -EIO;
2107 return 0;
2108}
2109
2110static long kvm_arch_vm_ioctl_hv(struct file *filp,
2111 unsigned int ioctl, unsigned long arg)
2112{
2113 struct kvm *kvm __maybe_unused = filp->private_data;
2114 void __user *argp = (void __user *)arg;
2115 long r;
2116
2117 switch (ioctl) {
2118
2119 case KVM_ALLOCATE_RMA: {
2120 struct kvm_allocate_rma rma;
2121 struct kvm *kvm = filp->private_data;
2122
2123 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
2124 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
2125 r = -EFAULT;
2126 break;
2127 }
2128
2129 case KVM_PPC_ALLOCATE_HTAB: {
2130 u32 htab_order;
2131
2132 r = -EFAULT;
2133 if (get_user(htab_order, (u32 __user *)argp))
2134 break;
2135 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2136 if (r)
2137 break;
2138 r = -EFAULT;
2139 if (put_user(htab_order, (u32 __user *)argp))
2140 break;
2141 r = 0;
2142 break;
2143 }
2144
2145 case KVM_PPC_GET_HTAB_FD: {
2146 struct kvm_get_htab_fd ghf;
2147
2148 r = -EFAULT;
2149 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2150 break;
2151 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2152 break;
2153 }
2154
2155 default:
2156 r = -ENOTTY;
2157 }
2158
2159 return r;
2160}
2161
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302162static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302163 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2164 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2165 .get_one_reg = kvmppc_get_one_reg_hv,
2166 .set_one_reg = kvmppc_set_one_reg_hv,
2167 .vcpu_load = kvmppc_core_vcpu_load_hv,
2168 .vcpu_put = kvmppc_core_vcpu_put_hv,
2169 .set_msr = kvmppc_set_msr_hv,
2170 .vcpu_run = kvmppc_vcpu_run_hv,
2171 .vcpu_create = kvmppc_core_vcpu_create_hv,
2172 .vcpu_free = kvmppc_core_vcpu_free_hv,
2173 .check_requests = kvmppc_core_check_requests_hv,
2174 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2175 .flush_memslot = kvmppc_core_flush_memslot_hv,
2176 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2177 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2178 .unmap_hva = kvm_unmap_hva_hv,
2179 .unmap_hva_range = kvm_unmap_hva_range_hv,
2180 .age_hva = kvm_age_hva_hv,
2181 .test_age_hva = kvm_test_age_hva_hv,
2182 .set_spte_hva = kvm_set_spte_hva_hv,
2183 .mmu_destroy = kvmppc_mmu_destroy_hv,
2184 .free_memslot = kvmppc_core_free_memslot_hv,
2185 .create_memslot = kvmppc_core_create_memslot_hv,
2186 .init_vm = kvmppc_core_init_vm_hv,
2187 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302188 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2189 .emulate_op = kvmppc_core_emulate_op_hv,
2190 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2191 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2192 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2193 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
2194};
2195
2196static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002197{
2198 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302199 /*
2200 * FIXME!! Do we need to check on all cpus ?
2201 */
2202 r = kvmppc_core_check_processor_compat_hv();
2203 if (r < 0)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002204 return r;
2205
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302206 kvm_ops_hv.owner = THIS_MODULE;
2207 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002208
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302209 r = kvmppc_mmu_hv_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002210 return r;
2211}
2212
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302213static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002214{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302215 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002216}
2217
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302218module_init(kvmppc_book3s_init_hv);
2219module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05302220MODULE_LICENSE("GPL");