blob: 56f57af7e73877703fdc8daff09f125568da7b05 [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>
Paul Mackerrasde56a942011-06-29 00:21:34 +000055
56/* #define EXIT_DEBUG */
57/* #define EXIT_DEBUG_SIMPLE */
58/* #define EXIT_DEBUG_INT */
59
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000060/* Used to indicate that a guest page fault needs to be handled */
61#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
62
Paul Mackerrasc7b67672012-10-15 01:18:07 +000063/* Used as a "null" value for timebase values */
64#define TB_NIL (~(u64)0)
65
Paul Mackerras19ccb762011-07-23 17:42:46 +100066static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +000067static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +100068
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +000069void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
70{
71 int me;
72 int cpu = vcpu->cpu;
73 wait_queue_head_t *wqp;
74
75 wqp = kvm_arch_vcpu_wq(vcpu);
76 if (waitqueue_active(wqp)) {
77 wake_up_interruptible(wqp);
78 ++vcpu->stat.halt_wakeup;
79 }
80
81 me = get_cpu();
82
83 /* CPU points to the first thread of the core */
84 if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
85 int real_cpu = cpu + vcpu->arch.ptid;
86 if (paca[real_cpu].kvm_hstate.xics_phys)
87 xics_wake_cpu(real_cpu);
88 else if (cpu_online(cpu))
89 smp_send_reschedule(cpu);
90 }
91 put_cpu();
92}
93
Paul Mackerrasc7b67672012-10-15 01:18:07 +000094/*
95 * We use the vcpu_load/put functions to measure stolen time.
96 * Stolen time is counted as time when either the vcpu is able to
97 * run as part of a virtual core, but the task running the vcore
98 * is preempted or sleeping, or when the vcpu needs something done
99 * in the kernel by the task running the vcpu, but that task is
100 * preempted or sleeping. Those two things have to be counted
101 * separately, since one of the vcpu tasks will take on the job
102 * of running the core, and the other vcpu tasks in the vcore will
103 * sleep waiting for it to do that, but that sleep shouldn't count
104 * as stolen time.
105 *
106 * Hence we accumulate stolen time when the vcpu can run as part of
107 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
108 * needs its task to do other things in the kernel (for example,
109 * service a page fault) in busy_stolen. We don't accumulate
110 * stolen time for a vcore when it is inactive, or for a vcpu
111 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
112 * a misnomer; it means that the vcpu task is not executing in
113 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
114 * the kernel. We don't have any way of dividing up that time
115 * between time that the vcpu is genuinely stopped, time that
116 * the task is actively working on behalf of the vcpu, and time
117 * that the task is preempted, so we don't count any of it as
118 * stolen.
119 *
120 * Updates to busy_stolen are protected by arch.tbacct_lock;
121 * updates to vc->stolen_tb are protected by the arch.tbacct_lock
122 * of the vcpu that has taken responsibility for running the vcore
123 * (i.e. vc->runner). The stolen times are measured in units of
124 * timebase ticks. (Note that the != TB_NIL checks below are
125 * purely defensive; they should never fail.)
126 */
127
Paul Mackerrasde56a942011-06-29 00:21:34 +0000128void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
129{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000130 struct kvmppc_vcore *vc = vcpu->arch.vcore;
131
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000132 spin_lock(&vcpu->arch.tbacct_lock);
133 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
134 vc->preempt_tb != TB_NIL) {
Paul Mackerras0456ec42012-02-03 00:56:21 +0000135 vc->stolen_tb += mftb() - vc->preempt_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000136 vc->preempt_tb = TB_NIL;
137 }
138 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
139 vcpu->arch.busy_preempt != TB_NIL) {
140 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
141 vcpu->arch.busy_preempt = TB_NIL;
142 }
143 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000144}
145
146void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
147{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000148 struct kvmppc_vcore *vc = vcpu->arch.vcore;
149
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000150 spin_lock(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000151 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
152 vc->preempt_tb = mftb();
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000153 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
154 vcpu->arch.busy_preempt = mftb();
155 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000156}
157
Paul Mackerrasde56a942011-06-29 00:21:34 +0000158void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
159{
160 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000161 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000162}
163
164void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
165{
166 vcpu->arch.pvr = pvr;
167}
168
169void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
170{
171 int r;
172
173 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
174 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
175 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
176 for (r = 0; r < 16; ++r)
177 pr_err("r%2d = %.16lx r%d = %.16lx\n",
178 r, kvmppc_get_gpr(vcpu, r),
179 r+16, kvmppc_get_gpr(vcpu, r+16));
180 pr_err("ctr = %.16lx lr = %.16lx\n",
181 vcpu->arch.ctr, vcpu->arch.lr);
182 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
183 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
184 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
185 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
186 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
187 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
188 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
189 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
190 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
191 pr_err("fault dar = %.16lx dsisr = %.8x\n",
192 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
193 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
194 for (r = 0; r < vcpu->arch.slb_max; ++r)
195 pr_err(" ESID = %.16llx VSID = %.16llx\n",
196 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
197 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000198 vcpu->kvm->arch.lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000199 vcpu->arch.last_inst);
200}
201
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000202struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
203{
204 int r;
205 struct kvm_vcpu *v, *ret = NULL;
206
207 mutex_lock(&kvm->lock);
208 kvm_for_each_vcpu(r, v, kvm) {
209 if (v->vcpu_id == id) {
210 ret = v;
211 break;
212 }
213 }
214 mutex_unlock(&kvm->lock);
215 return ret;
216}
217
218static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
219{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000220 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000221 vpa->yield_count = 1;
222}
223
Paul Mackerras55b665b2012-09-25 20:33:06 +0000224static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
225 unsigned long addr, unsigned long len)
226{
227 /* check address is cacheline aligned */
228 if (addr & (L1_CACHE_BYTES - 1))
229 return -EINVAL;
230 spin_lock(&vcpu->arch.vpa_update_lock);
231 if (v->next_gpa != addr || v->len != len) {
232 v->next_gpa = addr;
233 v->len = addr ? len : 0;
234 v->update_pending = 1;
235 }
236 spin_unlock(&vcpu->arch.vpa_update_lock);
237 return 0;
238}
239
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000240/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
241struct reg_vpa {
242 u32 dummy;
243 union {
244 u16 hword;
245 u32 word;
246 } length;
247};
248
249static int vpa_is_registered(struct kvmppc_vpa *vpap)
250{
251 if (vpap->update_pending)
252 return vpap->next_gpa != 0;
253 return vpap->pinned_addr != NULL;
254}
255
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000256static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
257 unsigned long flags,
258 unsigned long vcpuid, unsigned long vpa)
259{
260 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000261 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000262 void *va;
263 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000264 int err;
265 int subfunc;
266 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000267
268 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
269 if (!tvcpu)
270 return H_PARAMETER;
271
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000272 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
273 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
274 subfunc == H_VPA_REG_SLB) {
275 /* Registering new area - address must be cache-line aligned */
276 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000277 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000278
279 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000280 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
281 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000282 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000283 if (subfunc == H_VPA_REG_VPA)
284 len = ((struct reg_vpa *)va)->length.hword;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000285 else
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000286 len = ((struct reg_vpa *)va)->length.word;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000287 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000288
289 /* Check length */
290 if (len > nb || len < sizeof(struct reg_vpa))
291 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000292 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000293 vpa = 0;
294 len = 0;
295 }
296
297 err = H_PARAMETER;
298 vpap = NULL;
299 spin_lock(&tvcpu->arch.vpa_update_lock);
300
301 switch (subfunc) {
302 case H_VPA_REG_VPA: /* register VPA */
303 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000304 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000305 vpap = &tvcpu->arch.vpa;
306 err = 0;
307 break;
308
309 case H_VPA_REG_DTL: /* register DTL */
310 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000311 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000312 len -= len % sizeof(struct dtl_entry);
313
314 /* Check that they have previously registered a VPA */
315 err = H_RESOURCE;
316 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000317 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000318
319 vpap = &tvcpu->arch.dtl;
320 err = 0;
321 break;
322
323 case H_VPA_REG_SLB: /* register SLB shadow buffer */
324 /* Check that they have previously registered a VPA */
325 err = H_RESOURCE;
326 if (!vpa_is_registered(&tvcpu->arch.vpa))
327 break;
328
329 vpap = &tvcpu->arch.slb_shadow;
330 err = 0;
331 break;
332
333 case H_VPA_DEREG_VPA: /* deregister VPA */
334 /* Check they don't still have a DTL or SLB buf registered */
335 err = H_RESOURCE;
336 if (vpa_is_registered(&tvcpu->arch.dtl) ||
337 vpa_is_registered(&tvcpu->arch.slb_shadow))
338 break;
339
340 vpap = &tvcpu->arch.vpa;
341 err = 0;
342 break;
343
344 case H_VPA_DEREG_DTL: /* deregister DTL */
345 vpap = &tvcpu->arch.dtl;
346 err = 0;
347 break;
348
349 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
350 vpap = &tvcpu->arch.slb_shadow;
351 err = 0;
352 break;
353 }
354
355 if (vpap) {
356 vpap->next_gpa = vpa;
357 vpap->len = len;
358 vpap->update_pending = 1;
359 }
360
361 spin_unlock(&tvcpu->arch.vpa_update_lock);
362
363 return err;
364}
365
Paul Mackerras081f3232012-06-01 20:20:24 +1000366static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000367{
Paul Mackerras081f3232012-06-01 20:20:24 +1000368 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000369 void *va;
370 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000371 unsigned long gpa;
372
373 /*
374 * We need to pin the page pointed to by vpap->next_gpa,
375 * but we can't call kvmppc_pin_guest_page under the lock
376 * as it does get_user_pages() and down_read(). So we
377 * have to drop the lock, pin the page, then get the lock
378 * again and check that a new area didn't get registered
379 * in the meantime.
380 */
381 for (;;) {
382 gpa = vpap->next_gpa;
383 spin_unlock(&vcpu->arch.vpa_update_lock);
384 va = NULL;
385 nb = 0;
386 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000387 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000388 spin_lock(&vcpu->arch.vpa_update_lock);
389 if (gpa == vpap->next_gpa)
390 break;
391 /* sigh... unpin that one and try again */
392 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000393 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000394 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000395
396 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000397 if (va && nb < vpap->len) {
398 /*
399 * If it's now too short, it must be that userspace
400 * has changed the mappings underlying guest memory,
401 * so unregister the region.
402 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000403 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000404 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000405 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000406 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000407 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
408 vpap->dirty);
409 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000410 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000411 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000412 if (va)
413 vpap->pinned_end = va + vpap->len;
414}
Paul Mackerras93e60242011-12-12 12:28:55 +0000415
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000416static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
417{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000418 if (!(vcpu->arch.vpa.update_pending ||
419 vcpu->arch.slb_shadow.update_pending ||
420 vcpu->arch.dtl.update_pending))
421 return;
422
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000423 spin_lock(&vcpu->arch.vpa_update_lock);
424 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000425 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000426 if (vcpu->arch.vpa.pinned_addr)
427 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000428 }
429 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000430 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000431 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
432 vcpu->arch.dtl_index = 0;
433 }
434 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000435 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000436 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000437}
438
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000439/*
440 * Return the accumulated stolen time for the vcore up until `now'.
441 * The caller should hold the vcore lock.
442 */
443static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
444{
445 u64 p;
446
447 /*
448 * If we are the task running the vcore, then since we hold
449 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
450 * can't be updated, so we don't need the tbacct_lock.
451 * If the vcore is inactive, it can't become active (since we
452 * hold the vcore lock), so the vcpu load/put functions won't
453 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
454 */
455 if (vc->vcore_state != VCORE_INACTIVE &&
456 vc->runner->arch.run_task != current) {
457 spin_lock(&vc->runner->arch.tbacct_lock);
458 p = vc->stolen_tb;
459 if (vc->preempt_tb != TB_NIL)
460 p += now - vc->preempt_tb;
461 spin_unlock(&vc->runner->arch.tbacct_lock);
462 } else {
463 p = vc->stolen_tb;
464 }
465 return p;
466}
467
Paul Mackerras0456ec42012-02-03 00:56:21 +0000468static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
469 struct kvmppc_vcore *vc)
470{
471 struct dtl_entry *dt;
472 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000473 unsigned long stolen;
474 unsigned long core_stolen;
475 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000476
477 dt = vcpu->arch.dtl_ptr;
478 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000479 now = mftb();
480 core_stolen = vcore_stolen_time(vc, now);
481 stolen = core_stolen - vcpu->arch.stolen_logged;
482 vcpu->arch.stolen_logged = core_stolen;
483 spin_lock(&vcpu->arch.tbacct_lock);
484 stolen += vcpu->arch.busy_stolen;
485 vcpu->arch.busy_stolen = 0;
486 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000487 if (!dt || !vpa)
488 return;
489 memset(dt, 0, sizeof(struct dtl_entry));
490 dt->dispatch_reason = 7;
491 dt->processor_id = vc->pcpu + vcpu->arch.ptid;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000492 dt->timebase = now + vc->tb_offset;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000493 dt->enqueue_to_dispatch_time = stolen;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000494 dt->srr0 = kvmppc_get_pc(vcpu);
495 dt->srr1 = vcpu->arch.shregs.msr;
496 ++dt;
497 if (dt == vcpu->arch.dtl.pinned_end)
498 dt = vcpu->arch.dtl.pinned_addr;
499 vcpu->arch.dtl_ptr = dt;
500 /* order writing *dt vs. writing vpa->dtl_idx */
501 smp_wmb();
502 vpa->dtl_idx = ++vcpu->arch.dtl_index;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000503 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000504}
505
506int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
507{
508 unsigned long req = kvmppc_get_gpr(vcpu, 3);
509 unsigned long target, ret = H_SUCCESS;
510 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000511 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000512
513 switch (req) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000514 case H_ENTER:
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000515 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000516 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
517 kvmppc_get_gpr(vcpu, 5),
518 kvmppc_get_gpr(vcpu, 6),
519 kvmppc_get_gpr(vcpu, 7));
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000520 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000521 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000522 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000523 break;
524 case H_PROD:
525 target = kvmppc_get_gpr(vcpu, 4);
526 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
527 if (!tvcpu) {
528 ret = H_PARAMETER;
529 break;
530 }
531 tvcpu->arch.prodded = 1;
532 smp_mb();
533 if (vcpu->arch.ceded) {
534 if (waitqueue_active(&vcpu->wq)) {
535 wake_up_interruptible(&vcpu->wq);
536 vcpu->stat.halt_wakeup++;
537 }
538 }
539 break;
540 case H_CONFER:
541 break;
542 case H_REGISTER_VPA:
543 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
544 kvmppc_get_gpr(vcpu, 5),
545 kvmppc_get_gpr(vcpu, 6));
546 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000547 case H_RTAS:
548 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
549 return RESUME_HOST;
550
551 rc = kvmppc_rtas_hcall(vcpu);
552
553 if (rc == -ENOENT)
554 return RESUME_HOST;
555 else if (rc == 0)
556 break;
557
558 /* Send the error out to userspace via KVM_RUN */
559 return rc;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000560
561 case H_XIRR:
562 case H_CPPR:
563 case H_EOI:
564 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000565 case H_IPOLL:
566 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000567 if (kvmppc_xics_enabled(vcpu)) {
568 ret = kvmppc_xics_hcall(vcpu, req);
569 break;
570 } /* fallthrough */
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000571 default:
572 return RESUME_HOST;
573 }
574 kvmppc_set_gpr(vcpu, 3, ret);
575 vcpu->arch.hcall_needed = 0;
576 return RESUME_GUEST;
577}
578
Paul Mackerrasde56a942011-06-29 00:21:34 +0000579static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
580 struct task_struct *tsk)
581{
582 int r = RESUME_HOST;
583
584 vcpu->stat.sum_exits++;
585
586 run->exit_reason = KVM_EXIT_UNKNOWN;
587 run->ready_for_interrupt_injection = 1;
588 switch (vcpu->arch.trap) {
589 /* We're good on these - the host merely wanted to get our attention */
590 case BOOK3S_INTERRUPT_HV_DECREMENTER:
591 vcpu->stat.dec_exits++;
592 r = RESUME_GUEST;
593 break;
594 case BOOK3S_INTERRUPT_EXTERNAL:
595 vcpu->stat.ext_intr_exits++;
596 r = RESUME_GUEST;
597 break;
598 case BOOK3S_INTERRUPT_PERFMON:
599 r = RESUME_GUEST;
600 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000601 case BOOK3S_INTERRUPT_MACHINE_CHECK:
602 /*
603 * Deliver a machine check interrupt to the guest.
604 * We have to do this, even if the host has handled the
605 * machine check, because machine checks use SRR0/1 and
606 * the interrupt might have trashed guest state in them.
607 */
608 kvmppc_book3s_queue_irqprio(vcpu,
609 BOOK3S_INTERRUPT_MACHINE_CHECK);
610 r = RESUME_GUEST;
611 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000612 case BOOK3S_INTERRUPT_PROGRAM:
613 {
614 ulong flags;
615 /*
616 * Normally program interrupts are delivered directly
617 * to the guest by the hardware, but we can get here
618 * as a result of a hypervisor emulation interrupt
619 * (e40) getting turned into a 700 by BML RTAS.
620 */
621 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
622 kvmppc_core_queue_program(vcpu, flags);
623 r = RESUME_GUEST;
624 break;
625 }
626 case BOOK3S_INTERRUPT_SYSCALL:
627 {
628 /* hcall - punt to userspace */
629 int i;
630
631 if (vcpu->arch.shregs.msr & MSR_PR) {
632 /* sc 1 from userspace - reflect to guest syscall */
633 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
634 r = RESUME_GUEST;
635 break;
636 }
637 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
638 for (i = 0; i < 9; ++i)
639 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
640 run->exit_reason = KVM_EXIT_PAPR_HCALL;
641 vcpu->arch.hcall_needed = 1;
642 r = RESUME_HOST;
643 break;
644 }
645 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000646 * We get these next two if the guest accesses a page which it thinks
647 * it has mapped but which is not actually present, either because
648 * it is for an emulated I/O device or because the corresonding
649 * host page has been paged out. Any other HDSI/HISI interrupts
650 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000651 */
652 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000653 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000654 break;
655 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000656 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
657 vcpu->arch.fault_dsisr = 0;
658 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000659 break;
660 /*
661 * This occurs if the guest executes an illegal instruction.
662 * We just generate a program interrupt to the guest, since
663 * we don't emulate any guest instructions at this stage.
664 */
665 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
666 kvmppc_core_queue_program(vcpu, 0x80000);
667 r = RESUME_GUEST;
668 break;
669 default:
670 kvmppc_dump_regs(vcpu);
671 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
672 vcpu->arch.trap, kvmppc_get_pc(vcpu),
673 vcpu->arch.shregs.msr);
674 r = RESUME_HOST;
675 BUG();
676 break;
677 }
678
Paul Mackerrasde56a942011-06-29 00:21:34 +0000679 return r;
680}
681
682int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
Aneesh Kumar K.V87916442013-08-22 17:08:39 +0530683 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000684{
685 int i;
686
Paul Mackerrasde56a942011-06-29 00:21:34 +0000687 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +0530688 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000689 for (i = 0; i < vcpu->arch.slb_max; i++) {
690 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
691 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
692 }
693
694 return 0;
695}
696
697int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
Aneesh Kumar K.V87916442013-08-22 17:08:39 +0530698 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000699{
700 int i, j;
701
702 kvmppc_set_pvr(vcpu, sregs->pvr);
703
704 j = 0;
705 for (i = 0; i < vcpu->arch.slb_nr; i++) {
706 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
707 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
708 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
709 ++j;
710 }
711 }
712 vcpu->arch.slb_max = j;
713
714 return 0;
715}
716
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000717int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000718{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000719 int r = 0;
720 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +0000721
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000722 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000723 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000724 *val = get_reg_val(id, 0);
725 break;
726 case KVM_REG_PPC_DABR:
727 *val = get_reg_val(id, vcpu->arch.dabr);
728 break;
729 case KVM_REG_PPC_DSCR:
730 *val = get_reg_val(id, vcpu->arch.dscr);
731 break;
732 case KVM_REG_PPC_PURR:
733 *val = get_reg_val(id, vcpu->arch.purr);
734 break;
735 case KVM_REG_PPC_SPURR:
736 *val = get_reg_val(id, vcpu->arch.spurr);
737 break;
738 case KVM_REG_PPC_AMR:
739 *val = get_reg_val(id, vcpu->arch.amr);
740 break;
741 case KVM_REG_PPC_UAMOR:
742 *val = get_reg_val(id, vcpu->arch.uamor);
743 break;
744 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
745 i = id - KVM_REG_PPC_MMCR0;
746 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
747 break;
748 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
749 i = id - KVM_REG_PPC_PMC1;
750 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +0000751 break;
Paul Mackerras14941782013-09-06 13:11:18 +1000752 case KVM_REG_PPC_SIAR:
753 *val = get_reg_val(id, vcpu->arch.siar);
754 break;
755 case KVM_REG_PPC_SDAR:
756 *val = get_reg_val(id, vcpu->arch.sdar);
757 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000758#ifdef CONFIG_VSX
759 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
760 if (cpu_has_feature(CPU_FTR_VSX)) {
761 /* VSX => FP reg i is stored in arch.vsr[2*i] */
762 long int i = id - KVM_REG_PPC_FPR0;
763 *val = get_reg_val(id, vcpu->arch.vsr[2 * i]);
764 } else {
765 /* let generic code handle it */
766 r = -EINVAL;
767 }
768 break;
769 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
770 if (cpu_has_feature(CPU_FTR_VSX)) {
771 long int i = id - KVM_REG_PPC_VSR0;
772 val->vsxval[0] = vcpu->arch.vsr[2 * i];
773 val->vsxval[1] = vcpu->arch.vsr[2 * i + 1];
774 } else {
775 r = -ENXIO;
776 }
777 break;
778#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000779 case KVM_REG_PPC_VPA_ADDR:
780 spin_lock(&vcpu->arch.vpa_update_lock);
781 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
782 spin_unlock(&vcpu->arch.vpa_update_lock);
783 break;
784 case KVM_REG_PPC_VPA_SLB:
785 spin_lock(&vcpu->arch.vpa_update_lock);
786 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
787 val->vpaval.length = vcpu->arch.slb_shadow.len;
788 spin_unlock(&vcpu->arch.vpa_update_lock);
789 break;
790 case KVM_REG_PPC_VPA_DTL:
791 spin_lock(&vcpu->arch.vpa_update_lock);
792 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
793 val->vpaval.length = vcpu->arch.dtl.len;
794 spin_unlock(&vcpu->arch.vpa_update_lock);
795 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000796 case KVM_REG_PPC_TB_OFFSET:
797 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
798 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000799 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000800 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000801 break;
802 }
803
804 return r;
805}
806
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000807int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000808{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000809 int r = 0;
810 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +0000811 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +0000812
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000813 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000814 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +0000815 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000816 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +0000817 r = -EINVAL;
818 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000819 case KVM_REG_PPC_DABR:
820 vcpu->arch.dabr = set_reg_val(id, *val);
821 break;
822 case KVM_REG_PPC_DSCR:
823 vcpu->arch.dscr = set_reg_val(id, *val);
824 break;
825 case KVM_REG_PPC_PURR:
826 vcpu->arch.purr = set_reg_val(id, *val);
827 break;
828 case KVM_REG_PPC_SPURR:
829 vcpu->arch.spurr = set_reg_val(id, *val);
830 break;
831 case KVM_REG_PPC_AMR:
832 vcpu->arch.amr = set_reg_val(id, *val);
833 break;
834 case KVM_REG_PPC_UAMOR:
835 vcpu->arch.uamor = set_reg_val(id, *val);
836 break;
837 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
838 i = id - KVM_REG_PPC_MMCR0;
839 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
840 break;
841 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
842 i = id - KVM_REG_PPC_PMC1;
843 vcpu->arch.pmc[i] = set_reg_val(id, *val);
844 break;
Paul Mackerras14941782013-09-06 13:11:18 +1000845 case KVM_REG_PPC_SIAR:
846 vcpu->arch.siar = set_reg_val(id, *val);
847 break;
848 case KVM_REG_PPC_SDAR:
849 vcpu->arch.sdar = set_reg_val(id, *val);
850 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000851#ifdef CONFIG_VSX
852 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
853 if (cpu_has_feature(CPU_FTR_VSX)) {
854 /* VSX => FP reg i is stored in arch.vsr[2*i] */
855 long int i = id - KVM_REG_PPC_FPR0;
856 vcpu->arch.vsr[2 * i] = set_reg_val(id, *val);
857 } else {
858 /* let generic code handle it */
859 r = -EINVAL;
860 }
861 break;
862 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
863 if (cpu_has_feature(CPU_FTR_VSX)) {
864 long int i = id - KVM_REG_PPC_VSR0;
865 vcpu->arch.vsr[2 * i] = val->vsxval[0];
866 vcpu->arch.vsr[2 * i + 1] = val->vsxval[1];
867 } else {
868 r = -ENXIO;
869 }
870 break;
871#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000872 case KVM_REG_PPC_VPA_ADDR:
873 addr = set_reg_val(id, *val);
874 r = -EINVAL;
875 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
876 vcpu->arch.dtl.next_gpa))
877 break;
878 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
879 break;
880 case KVM_REG_PPC_VPA_SLB:
881 addr = val->vpaval.addr;
882 len = val->vpaval.length;
883 r = -EINVAL;
884 if (addr && !vcpu->arch.vpa.next_gpa)
885 break;
886 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
887 break;
888 case KVM_REG_PPC_VPA_DTL:
889 addr = val->vpaval.addr;
890 len = val->vpaval.length;
891 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +0000892 if (addr && (len < sizeof(struct dtl_entry) ||
893 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +0000894 break;
895 len -= len % sizeof(struct dtl_entry);
896 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
897 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000898 case KVM_REG_PPC_TB_OFFSET:
899 /* round up to multiple of 2^24 */
900 vcpu->arch.vcore->tb_offset =
901 ALIGN(set_reg_val(id, *val), 1UL << 24);
902 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000903 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000904 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000905 break;
906 }
907
908 return r;
909}
910
Paul Mackerrasde56a942011-06-29 00:21:34 +0000911int kvmppc_core_check_processor_compat(void)
912{
Paul Mackerras9e368f22011-06-29 00:40:08 +0000913 if (cpu_has_feature(CPU_FTR_HVMODE))
Paul Mackerrasde56a942011-06-29 00:21:34 +0000914 return 0;
915 return -EIO;
916}
917
918struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
919{
920 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000921 int err = -EINVAL;
922 int core;
923 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000924
Paul Mackerras371fefd2011-06-29 00:23:08 +0000925 core = id / threads_per_core;
926 if (core >= KVM_MAX_VCORES)
927 goto out;
928
929 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200930 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000931 if (!vcpu)
932 goto out;
933
934 err = kvm_vcpu_init(vcpu, kvm, id);
935 if (err)
936 goto free_vcpu;
937
938 vcpu->arch.shared = &vcpu->arch.shregs;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000939 vcpu->arch.mmcr[0] = MMCR0_FC;
940 vcpu->arch.ctrl = CTRL_RUNLATCH;
941 /* default to host PVR, since we can't spoof it */
942 vcpu->arch.pvr = mfspr(SPRN_PVR);
943 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000944 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000945 spin_lock_init(&vcpu->arch.tbacct_lock);
946 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000947
Paul Mackerrasde56a942011-06-29 00:21:34 +0000948 kvmppc_mmu_book3s_hv_init(vcpu);
949
Paul Mackerras8455d792012-10-15 01:17:42 +0000950 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000951
952 init_waitqueue_head(&vcpu->arch.cpu_run);
953
954 mutex_lock(&kvm->lock);
955 vcore = kvm->arch.vcores[core];
956 if (!vcore) {
957 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
958 if (vcore) {
959 INIT_LIST_HEAD(&vcore->runnable_threads);
960 spin_lock_init(&vcore->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000961 init_waitqueue_head(&vcore->wq);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000962 vcore->preempt_tb = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000963 }
964 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000965 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000966 }
967 mutex_unlock(&kvm->lock);
968
969 if (!vcore)
970 goto free_vcpu;
971
972 spin_lock(&vcore->lock);
973 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000974 spin_unlock(&vcore->lock);
975 vcpu->arch.vcore = vcore;
976
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200977 vcpu->arch.cpu_type = KVM_CPU_3S_64;
978 kvmppc_sanity_check(vcpu);
979
Paul Mackerrasde56a942011-06-29 00:21:34 +0000980 return vcpu;
981
982free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200983 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000984out:
985 return ERR_PTR(err);
986}
987
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000988static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
989{
990 if (vpa->pinned_addr)
991 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
992 vpa->dirty);
993}
994
Paul Mackerrasde56a942011-06-29 00:21:34 +0000995void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
996{
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000997 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000998 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
999 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1000 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001001 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001002 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001003 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001004}
1005
Paul Mackerras19ccb762011-07-23 17:42:46 +10001006static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001007{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001008 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001009
Paul Mackerras19ccb762011-07-23 17:42:46 +10001010 now = get_tb();
1011 if (now > vcpu->arch.dec_expires) {
1012 /* decrementer has already gone negative */
1013 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001014 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001015 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001016 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001017 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1018 / tb_ticks_per_sec;
1019 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1020 HRTIMER_MODE_REL);
1021 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001022}
1023
Paul Mackerras19ccb762011-07-23 17:42:46 +10001024static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001025{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001026 vcpu->arch.ceded = 0;
1027 if (vcpu->arch.timer_running) {
1028 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1029 vcpu->arch.timer_running = 0;
1030 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001031}
1032
1033extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001034
1035static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1036 struct kvm_vcpu *vcpu)
1037{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001038 u64 now;
1039
Paul Mackerras371fefd2011-06-29 00:23:08 +00001040 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1041 return;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001042 spin_lock(&vcpu->arch.tbacct_lock);
1043 now = mftb();
1044 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1045 vcpu->arch.stolen_logged;
1046 vcpu->arch.busy_preempt = now;
1047 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1048 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001049 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001050 list_del(&vcpu->arch.run_list);
1051}
1052
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001053static int kvmppc_grab_hwthread(int cpu)
1054{
1055 struct paca_struct *tpaca;
1056 long timeout = 1000;
1057
1058 tpaca = &paca[cpu];
1059
1060 /* Ensure the thread won't go into the kernel if it wakes */
1061 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001062 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001063
1064 /*
1065 * If the thread is already executing in the kernel (e.g. handling
1066 * a stray interrupt), wait for it to get back to nap mode.
1067 * The smp_mb() is to ensure that our setting of hwthread_req
1068 * is visible before we look at hwthread_state, so if this
1069 * races with the code at system_reset_pSeries and the thread
1070 * misses our setting of hwthread_req, we are sure to see its
1071 * setting of hwthread_state, and vice versa.
1072 */
1073 smp_mb();
1074 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1075 if (--timeout <= 0) {
1076 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1077 return -EBUSY;
1078 }
1079 udelay(1);
1080 }
1081 return 0;
1082}
1083
1084static void kvmppc_release_hwthread(int cpu)
1085{
1086 struct paca_struct *tpaca;
1087
1088 tpaca = &paca[cpu];
1089 tpaca->kvm_hstate.hwthread_req = 0;
1090 tpaca->kvm_hstate.kvm_vcpu = NULL;
1091}
1092
Paul Mackerras371fefd2011-06-29 00:23:08 +00001093static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1094{
1095 int cpu;
1096 struct paca_struct *tpaca;
1097 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1098
Paul Mackerras19ccb762011-07-23 17:42:46 +10001099 if (vcpu->arch.timer_running) {
1100 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1101 vcpu->arch.timer_running = 0;
1102 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001103 cpu = vc->pcpu + vcpu->arch.ptid;
1104 tpaca = &paca[cpu];
1105 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1106 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001107 tpaca->kvm_hstate.napping = 0;
1108 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001109 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +00001110#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001111 if (vcpu->arch.ptid) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001112 xics_wake_cpu(cpu);
1113 ++vc->n_woken;
1114 }
1115#endif
1116}
1117
1118static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1119{
1120 int i;
1121
1122 HMT_low();
1123 i = 0;
1124 while (vc->nap_count < vc->n_woken) {
1125 if (++i >= 1000000) {
1126 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1127 vc->nap_count, vc->n_woken);
1128 break;
1129 }
1130 cpu_relax();
1131 }
1132 HMT_medium();
1133}
1134
1135/*
1136 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001137 * this core are off-line. Then grab the threads so they can't
1138 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001139 */
1140static int on_primary_thread(void)
1141{
1142 int cpu = smp_processor_id();
1143 int thr = cpu_thread_in_core(cpu);
1144
1145 if (thr)
1146 return 0;
1147 while (++thr < threads_per_core)
1148 if (cpu_online(cpu + thr))
1149 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001150
1151 /* Grab all hw threads so they can't go into the kernel */
1152 for (thr = 1; thr < threads_per_core; ++thr) {
1153 if (kvmppc_grab_hwthread(cpu + thr)) {
1154 /* Couldn't grab one; let the others go */
1155 do {
1156 kvmppc_release_hwthread(cpu + thr);
1157 } while (--thr > 0);
1158 return 0;
1159 }
1160 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001161 return 1;
1162}
1163
1164/*
1165 * Run a set of guest threads on a physical core.
1166 * Called with vc->lock held.
1167 */
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001168static void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001169{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001170 struct kvm_vcpu *vcpu, *vcpu0, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001171 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001172 u64 now;
Paul Mackerras081f3232012-06-01 20:20:24 +10001173 int ptid, i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001174 int srcu_idx;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001175 struct kvm_vcpu *vcpus_to_update[threads_per_core];
Paul Mackerrasde56a942011-06-29 00:21:34 +00001176
Paul Mackerras371fefd2011-06-29 00:23:08 +00001177 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +10001178 need_vpa_update = 0;
1179 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001180 if (signal_pending(vcpu->arch.run_task))
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001181 return;
1182 if (vcpu->arch.vpa.update_pending ||
1183 vcpu->arch.slb_shadow.update_pending ||
1184 vcpu->arch.dtl.update_pending)
1185 vcpus_to_update[need_vpa_update++] = vcpu;
Paul Mackerras081f3232012-06-01 20:20:24 +10001186 }
1187
1188 /*
1189 * Initialize *vc, in particular vc->vcore_state, so we can
1190 * drop the vcore lock if necessary.
1191 */
1192 vc->n_woken = 0;
1193 vc->nap_count = 0;
1194 vc->entry_exit_count = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001195 vc->vcore_state = VCORE_STARTING;
Paul Mackerras081f3232012-06-01 20:20:24 +10001196 vc->in_guest = 0;
1197 vc->napping_threads = 0;
1198
1199 /*
1200 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1201 * which can't be called with any spinlocks held.
1202 */
1203 if (need_vpa_update) {
1204 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001205 for (i = 0; i < need_vpa_update; ++i)
1206 kvmppc_update_vpas(vcpus_to_update[i]);
Paul Mackerras081f3232012-06-01 20:20:24 +10001207 spin_lock(&vc->lock);
1208 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001209
1210 /*
Paul Mackerras19ccb762011-07-23 17:42:46 +10001211 * Assign physical thread IDs, first to non-ceded vcpus
1212 * and then to ceded ones.
1213 */
1214 ptid = 0;
1215 vcpu0 = NULL;
1216 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1217 if (!vcpu->arch.ceded) {
1218 if (!ptid)
1219 vcpu0 = vcpu;
1220 vcpu->arch.ptid = ptid++;
1221 }
1222 }
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001223 if (!vcpu0)
1224 goto out; /* nothing to run; should never happen */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001225 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1226 if (vcpu->arch.ceded)
1227 vcpu->arch.ptid = ptid++;
1228
Paul Mackerras7b444c62012-10-15 01:16:14 +00001229 /*
1230 * Make sure we are running on thread 0, and that
1231 * secondary threads are offline.
1232 */
1233 if (threads_per_core > 1 && !on_primary_thread()) {
1234 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1235 vcpu->arch.ret = -EBUSY;
1236 goto out;
1237 }
1238
Paul Mackerras371fefd2011-06-29 00:23:08 +00001239 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001240 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001241 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001242 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001243 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001244
Paul Mackerras2f12f032012-10-15 01:17:17 +00001245 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001246 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001247 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001248
Paul Mackerras19ccb762011-07-23 17:42:46 +10001249 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001250
1251 srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
1252
Paul Mackerras19ccb762011-07-23 17:42:46 +10001253 __kvmppc_vcore_entry(NULL, vcpu0);
1254
Paul Mackerras371fefd2011-06-29 00:23:08 +00001255 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001256 /* disable sending of IPIs on virtual external irqs */
1257 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1258 vcpu->cpu = -1;
1259 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001260 if (vc->nap_count < vc->n_woken)
1261 kvmppc_wait_for_nap(vc);
Paul Mackerras2f12f032012-10-15 01:17:17 +00001262 for (i = 0; i < threads_per_core; ++i)
1263 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001264 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001265 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001266 spin_unlock(&vc->lock);
1267
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001268 srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
1269
Paul Mackerras371fefd2011-06-29 00:23:08 +00001270 /* make sure updates to secondary vcpu structs are visible now */
1271 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001272 kvm_guest_exit();
1273
1274 preempt_enable();
1275 kvm_resched(vcpu);
1276
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001277 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001278 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001279 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1280 /* cancel pending dec exception if dec is positive */
1281 if (now < vcpu->arch.dec_expires &&
1282 kvmppc_core_pending_dec(vcpu))
1283 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001284
1285 ret = RESUME_GUEST;
1286 if (vcpu->arch.trap)
1287 ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
1288 vcpu->arch.run_task);
1289
Paul Mackerras371fefd2011-06-29 00:23:08 +00001290 vcpu->arch.ret = ret;
1291 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001292
1293 if (vcpu->arch.ceded) {
1294 if (ret != RESUME_GUEST)
1295 kvmppc_end_cede(vcpu);
1296 else
1297 kvmppc_set_timer(vcpu);
1298 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001299 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001300
Paul Mackerrasde56a942011-06-29 00:21:34 +00001301 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001302 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001303 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1304 arch.run_list) {
1305 if (vcpu->arch.ret != RESUME_GUEST) {
1306 kvmppc_remove_runnable(vc, vcpu);
1307 wake_up(&vcpu->arch.cpu_run);
1308 }
1309 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001310}
1311
Paul Mackerras19ccb762011-07-23 17:42:46 +10001312/*
1313 * Wait for some other vcpu thread to execute us, and
1314 * wake us up when we need to handle something in the host.
1315 */
1316static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001317{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001318 DEFINE_WAIT(wait);
1319
Paul Mackerras19ccb762011-07-23 17:42:46 +10001320 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1321 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1322 schedule();
1323 finish_wait(&vcpu->arch.cpu_run, &wait);
1324}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001325
Paul Mackerras19ccb762011-07-23 17:42:46 +10001326/*
1327 * All the vcpus in this vcore are idle, so wait for a decrementer
1328 * or external interrupt to one of the vcpus. vc->lock is held.
1329 */
1330static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1331{
1332 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001333
1334 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1335 vc->vcore_state = VCORE_SLEEPING;
1336 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001337 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001338 finish_wait(&vc->wq, &wait);
1339 spin_lock(&vc->lock);
1340 vc->vcore_state = VCORE_INACTIVE;
1341}
1342
1343static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1344{
1345 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001346 struct kvmppc_vcore *vc;
1347 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001348
Paul Mackerras371fefd2011-06-29 00:23:08 +00001349 kvm_run->exit_reason = 0;
1350 vcpu->arch.ret = RESUME_GUEST;
1351 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001352 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001353
Paul Mackerras371fefd2011-06-29 00:23:08 +00001354 /*
1355 * Synchronize with other threads in this virtual core
1356 */
1357 vc = vcpu->arch.vcore;
1358 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001359 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001360 vcpu->arch.run_task = current;
1361 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001362 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10001363 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001364 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001365 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1366 ++vc->n_runnable;
1367
Paul Mackerras19ccb762011-07-23 17:42:46 +10001368 /*
1369 * This happens the first time this is called for a vcpu.
1370 * If the vcore is already running, we may be able to start
1371 * this thread straight away and have it join in.
1372 */
Paul Mackerras8455d792012-10-15 01:17:42 +00001373 if (!signal_pending(current)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001374 if (vc->vcore_state == VCORE_RUNNING &&
1375 VCORE_EXIT_COUNT(vc) == 0) {
1376 vcpu->arch.ptid = vc->n_runnable - 1;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001377 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001378 kvmppc_start_thread(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00001379 } else if (vc->vcore_state == VCORE_SLEEPING) {
1380 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001381 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001382
Paul Mackerras8455d792012-10-15 01:17:42 +00001383 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001384
1385 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1386 !signal_pending(current)) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001387 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001388 spin_unlock(&vc->lock);
1389 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1390 spin_lock(&vc->lock);
1391 continue;
1392 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001393 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1394 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001395 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001396 if (signal_pending(v->arch.run_task)) {
1397 kvmppc_remove_runnable(vc, v);
1398 v->stat.signal_exits++;
1399 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1400 v->arch.ret = -EINTR;
1401 wake_up(&v->arch.cpu_run);
1402 }
1403 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001404 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1405 break;
1406 vc->runner = vcpu;
1407 n_ceded = 0;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001408 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001409 if (!v->arch.pending_exceptions)
1410 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001411 else
1412 v->arch.ceded = 0;
1413 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001414 if (n_ceded == vc->n_runnable)
1415 kvmppc_vcore_blocked(vc);
1416 else
1417 kvmppc_run_core(vc);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001418 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001419 }
1420
Paul Mackerras8455d792012-10-15 01:17:42 +00001421 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1422 (vc->vcore_state == VCORE_RUNNING ||
1423 vc->vcore_state == VCORE_EXITING)) {
1424 spin_unlock(&vc->lock);
1425 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1426 spin_lock(&vc->lock);
1427 }
1428
1429 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1430 kvmppc_remove_runnable(vc, vcpu);
1431 vcpu->stat.signal_exits++;
1432 kvm_run->exit_reason = KVM_EXIT_INTR;
1433 vcpu->arch.ret = -EINTR;
1434 }
1435
1436 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1437 /* Wake up some vcpu to run the core */
1438 v = list_first_entry(&vc->runnable_threads,
1439 struct kvm_vcpu, arch.run_list);
1440 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001441 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001442
Paul Mackerras19ccb762011-07-23 17:42:46 +10001443 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001444 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001445}
1446
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001447int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
1448{
1449 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001450 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001451
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001452 if (!vcpu->arch.sane) {
1453 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1454 return -EINVAL;
1455 }
1456
Scott Wood25051b52011-11-08 18:23:23 -06001457 kvmppc_core_prepare_to_enter(vcpu);
1458
Paul Mackerras19ccb762011-07-23 17:42:46 +10001459 /* No need to go into the guest when all we'll do is come back out */
1460 if (signal_pending(current)) {
1461 run->exit_reason = KVM_EXIT_INTR;
1462 return -EINTR;
1463 }
1464
Paul Mackerras32fad282012-05-04 02:32:53 +00001465 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1466 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1467 smp_mb();
1468
1469 /* On the first time here, set up HTAB and VRMA or RMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001470 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001471 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001472 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001473 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001474 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001475
1476 flush_fp_to_thread(current);
1477 flush_altivec_to_thread(current);
1478 flush_vsx_to_thread(current);
1479 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001480 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001481 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001482
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001483 do {
1484 r = kvmppc_run_vcpu(run, vcpu);
1485
1486 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1487 !(vcpu->arch.shregs.msr & MSR_PR)) {
1488 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001489 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001490 } else if (r == RESUME_PAGE_FAULT) {
1491 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1492 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1493 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1494 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001495 }
1496 } while (r == RESUME_GUEST);
Paul Mackerras32fad282012-05-04 02:32:53 +00001497
1498 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001499 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00001500 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001501 return r;
1502}
1503
David Gibson54738c02011-06-29 00:22:41 +00001504
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001505/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +00001506 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001507static inline int lpcr_rmls(unsigned long rma_size)
1508{
1509 switch (rma_size) {
1510 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001511 if (cpu_has_feature(CPU_FTR_ARCH_206))
1512 return 8; /* only supported on POWER7 */
1513 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001514 case 64ul << 20: /* 64 MB */
1515 return 3;
1516 case 128ul << 20: /* 128 MB */
1517 return 7;
1518 case 256ul << 20: /* 256 MB */
1519 return 4;
1520 case 1ul << 30: /* 1 GB */
1521 return 2;
1522 case 16ul << 30: /* 16 GB */
1523 return 1;
1524 case 256ul << 30: /* 256 GB */
1525 return 0;
1526 default:
1527 return -1;
1528 }
1529}
1530
1531static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1532{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001533 struct page *page;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301534 struct kvm_rma_info *ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001535
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301536 if (vmf->pgoff >= kvm_rma_pages)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001537 return VM_FAULT_SIGBUS;
1538
1539 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1540 get_page(page);
1541 vmf->page = page;
1542 return 0;
1543}
1544
1545static const struct vm_operations_struct kvm_rma_vm_ops = {
1546 .fault = kvm_rma_fault,
1547};
1548
1549static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1550{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001551 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001552 vma->vm_ops = &kvm_rma_vm_ops;
1553 return 0;
1554}
1555
1556static int kvm_rma_release(struct inode *inode, struct file *filp)
1557{
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301558 struct kvm_rma_info *ri = filp->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001559
1560 kvm_release_rma(ri);
1561 return 0;
1562}
1563
Al Viro75ef9de2013-04-04 19:09:41 -04001564static const struct file_operations kvm_rma_fops = {
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001565 .mmap = kvm_rma_mmap,
1566 .release = kvm_rma_release,
1567};
1568
1569long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
1570{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001571 long fd;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301572 struct kvm_rma_info *ri;
1573 /*
1574 * Only do this on PPC970 in HV mode
1575 */
1576 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
1577 !cpu_has_feature(CPU_FTR_ARCH_201))
1578 return -EINVAL;
1579
1580 if (!kvm_rma_pages)
1581 return -EINVAL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001582
1583 ri = kvm_alloc_rma();
1584 if (!ri)
1585 return -ENOMEM;
1586
Yann Droneaud2f84d5e2013-08-24 22:14:08 +02001587 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001588 if (fd < 0)
1589 kvm_release_rma(ri);
1590
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301591 ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001592 return fd;
1593}
1594
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001595static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1596 int linux_psize)
1597{
1598 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1599
1600 if (!def->shift)
1601 return;
1602 (*sps)->page_shift = def->shift;
1603 (*sps)->slb_enc = def->sllp;
1604 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00001605 /*
1606 * Only return base page encoding. We don't want to return
1607 * all the supporting pte_enc, because our H_ENTER doesn't
1608 * support MPSS yet. Once they do, we can start passing all
1609 * support pte_enc here
1610 */
1611 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001612 (*sps)++;
1613}
1614
1615int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
1616{
1617 struct kvm_ppc_one_seg_page_size *sps;
1618
1619 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1620 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1621 info->flags |= KVM_PPC_1T_SEGMENTS;
1622 info->slb_size = mmu_slb_size;
1623
1624 /* We only support these sizes for now, and no muti-size segments */
1625 sps = &info->sps[0];
1626 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1627 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1628 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1629
1630 return 0;
1631}
1632
Paul Mackerras82ed3612011-12-15 02:03:22 +00001633/*
1634 * Get (and clear) the dirty memory log for a memory slot.
1635 */
1636int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1637{
1638 struct kvm_memory_slot *memslot;
1639 int r;
1640 unsigned long n;
1641
1642 mutex_lock(&kvm->slots_lock);
1643
1644 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07001645 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00001646 goto out;
1647
1648 memslot = id_to_memslot(kvm->memslots, log->slot);
1649 r = -ENOENT;
1650 if (!memslot->dirty_bitmap)
1651 goto out;
1652
1653 n = kvm_dirty_bitmap_bytes(memslot);
1654 memset(memslot->dirty_bitmap, 0, n);
1655
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001656 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00001657 if (r)
1658 goto out;
1659
1660 r = -EFAULT;
1661 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1662 goto out;
1663
1664 r = 0;
1665out:
1666 mutex_unlock(&kvm->slots_lock);
1667 return r;
1668}
1669
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001670static void unpin_slot(struct kvm_memory_slot *memslot)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001671{
1672 unsigned long *physp;
1673 unsigned long j, npages, pfn;
1674 struct page *page;
1675
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001676 physp = memslot->arch.slot_phys;
1677 npages = memslot->npages;
1678 if (!physp)
1679 return;
1680 for (j = 0; j < npages; j++) {
1681 if (!(physp[j] & KVMPPC_GOT_PAGE))
1682 continue;
1683 pfn = physp[j] >> PAGE_SHIFT;
1684 page = pfn_to_page(pfn);
1685 SetPageDirty(page);
1686 put_page(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001687 }
1688}
1689
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001690void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1691 struct kvm_memory_slot *dont)
1692{
1693 if (!dont || free->arch.rmap != dont->arch.rmap) {
1694 vfree(free->arch.rmap);
1695 free->arch.rmap = NULL;
1696 }
1697 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1698 unpin_slot(free);
1699 vfree(free->arch.slot_phys);
1700 free->arch.slot_phys = NULL;
1701 }
1702}
1703
1704int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1705 unsigned long npages)
1706{
1707 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1708 if (!slot->arch.rmap)
1709 return -ENOMEM;
1710 slot->arch.slot_phys = NULL;
1711
1712 return 0;
1713}
1714
1715int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1716 struct kvm_memory_slot *memslot,
1717 struct kvm_userspace_memory_region *mem)
1718{
1719 unsigned long *phys;
1720
1721 /* Allocate a slot_phys array if needed */
1722 phys = memslot->arch.slot_phys;
1723 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1724 phys = vzalloc(memslot->npages * sizeof(unsigned long));
1725 if (!phys)
1726 return -ENOMEM;
1727 memslot->arch.slot_phys = phys;
1728 }
1729
1730 return 0;
1731}
1732
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001733void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001734 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001735 const struct kvm_memory_slot *old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001736{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001737 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1738 struct kvm_memory_slot *memslot;
1739
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001740 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001741 /*
1742 * If modifying a memslot, reset all the rmap dirty bits.
1743 * If this is a new memslot, we don't need to do anything
1744 * since the rmap array starts out as all zeroes,
1745 * i.e. no pages are dirty.
1746 */
1747 memslot = id_to_memslot(kvm->memslots, mem->slot);
1748 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1749 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001750}
1751
Paul Mackerras32fad282012-05-04 02:32:53 +00001752static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001753{
1754 int err = 0;
1755 struct kvm *kvm = vcpu->kvm;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301756 struct kvm_rma_info *ri = NULL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001757 unsigned long hva;
1758 struct kvm_memory_slot *memslot;
1759 struct vm_area_struct *vma;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001760 unsigned long lpcr, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001761 unsigned long psize, porder;
1762 unsigned long rma_size;
1763 unsigned long rmls;
1764 unsigned long *physp;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001765 unsigned long i, npages;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001766 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001767
1768 mutex_lock(&kvm->lock);
1769 if (kvm->arch.rma_setup_done)
1770 goto out; /* another vcpu beat us to it */
1771
Paul Mackerras32fad282012-05-04 02:32:53 +00001772 /* Allocate hashed page table (if not done already) and reset it */
1773 if (!kvm->arch.hpt_virt) {
1774 err = kvmppc_alloc_hpt(kvm, NULL);
1775 if (err) {
1776 pr_err("KVM: Couldn't alloc HPT\n");
1777 goto out;
1778 }
1779 }
1780
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001781 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001782 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001783 memslot = gfn_to_memslot(kvm, 0);
1784
1785 /* We must have some memory at 0 by now */
1786 err = -EINVAL;
1787 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001788 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001789
1790 /* Look up the VMA for the start of this memory slot */
1791 hva = memslot->userspace_addr;
1792 down_read(&current->mm->mmap_sem);
1793 vma = find_vma(current->mm, hva);
1794 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
1795 goto up_out;
1796
1797 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001798 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001799
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001800 /* Is this one of our preallocated RMAs? */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001801 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
1802 hva == vma->vm_start)
1803 ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001804
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001805 up_read(&current->mm->mmap_sem);
1806
1807 if (!ri) {
1808 /* On POWER7, use VRMA; on PPC970, give up */
1809 err = -EPERM;
1810 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1811 pr_err("KVM: CPU requires an RMO\n");
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001812 goto out_srcu;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001813 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001814
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001815 /* We can handle 4k, 64k or 16M pages in the VRMA */
1816 err = -EINVAL;
1817 if (!(psize == 0x1000 || psize == 0x10000 ||
1818 psize == 0x1000000))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001819 goto out_srcu;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001820
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001821 /* Update VRMASD field in the LPCR */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001822 senc = slb_pgsize_encoding(psize);
Paul Mackerras697d3892011-12-12 12:36:37 +00001823 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1824 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001825 lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
1826 lpcr |= senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001827 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001828
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001829 /* Create HPTEs in the hash page table for the VRMA */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001830 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001831
1832 } else {
1833 /* Set up to use an RMO region */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301834 rma_size = kvm_rma_pages;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001835 if (rma_size > memslot->npages)
1836 rma_size = memslot->npages;
1837 rma_size <<= PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001838 rmls = lpcr_rmls(rma_size);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001839 err = -EINVAL;
Chen Gang5d226ae2013-07-22 14:32:35 +08001840 if ((long)rmls < 0) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001841 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001842 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001843 }
1844 atomic_inc(&ri->use_count);
1845 kvm->arch.rma = ri;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001846
1847 /* Update LPCR and RMOR */
1848 lpcr = kvm->arch.lpcr;
1849 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1850 /* PPC970; insert RMLS value (split field) in HID4 */
1851 lpcr &= ~((1ul << HID4_RMLS0_SH) |
1852 (3ul << HID4_RMLS2_SH));
1853 lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
1854 ((rmls & 3) << HID4_RMLS2_SH);
1855 /* RMOR is also in HID4 */
1856 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
1857 << HID4_RMOR_SH;
1858 } else {
1859 /* POWER7 */
1860 lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
1861 lpcr |= rmls << LPCR_RMLS_SH;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301862 kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001863 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001864 kvm->arch.lpcr = lpcr;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001865 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001866 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001867
1868 /* Initialize phys addrs of pages in RMO */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301869 npages = kvm_rma_pages;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001870 porder = __ilog2(npages);
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001871 physp = memslot->arch.slot_phys;
1872 if (physp) {
1873 if (npages > memslot->npages)
1874 npages = memslot->npages;
1875 spin_lock(&kvm->arch.slot_phys_lock);
1876 for (i = 0; i < npages; ++i)
1877 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
1878 porder;
1879 spin_unlock(&kvm->arch.slot_phys_lock);
1880 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001881 }
1882
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001883 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
1884 smp_wmb();
1885 kvm->arch.rma_setup_done = 1;
1886 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001887 out_srcu:
1888 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001889 out:
1890 mutex_unlock(&kvm->lock);
1891 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001892
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001893 up_out:
1894 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08001895 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001896}
1897
1898int kvmppc_core_init_vm(struct kvm *kvm)
1899{
Paul Mackerras32fad282012-05-04 02:32:53 +00001900 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001901
Paul Mackerras32fad282012-05-04 02:32:53 +00001902 /* Allocate the guest's logical partition ID */
1903
1904 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08001905 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00001906 return -ENOMEM;
1907 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001908
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001909 /*
1910 * Since we don't flush the TLB when tearing down a VM,
1911 * and this lpid might have previously been used,
1912 * make sure we flush on each core before running the new VM.
1913 */
1914 cpumask_setall(&kvm->arch.need_tlb_flush);
1915
David Gibson54738c02011-06-29 00:22:41 +00001916 INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001917 INIT_LIST_HEAD(&kvm->arch.rtas_tokens);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001918
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001919 kvm->arch.rma = NULL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001920
Paul Mackerras9e368f22011-06-29 00:40:08 +00001921 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001922
Paul Mackerras9e368f22011-06-29 00:40:08 +00001923 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1924 /* PPC970; HID4 is effectively the LPCR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001925 kvm->arch.host_lpid = 0;
1926 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
1927 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
1928 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
1929 ((lpid & 0xf) << HID4_LPID5_SH);
1930 } else {
1931 /* POWER7; init LPCR for virtual RMA mode */
1932 kvm->arch.host_lpid = mfspr(SPRN_LPID);
1933 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
1934 lpcr &= LPCR_PECE | LPCR_LPES;
1935 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
Paul Mackerras697d3892011-12-12 12:36:37 +00001936 LPCR_VPM0 | LPCR_VPM1;
1937 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
1938 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerras9e368f22011-06-29 00:40:08 +00001939 }
1940 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001941
Paul Mackerras342d3db2011-12-12 12:38:05 +00001942 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001943 spin_lock_init(&kvm->arch.slot_phys_lock);
Paul Mackerras512691d2012-10-15 01:15:41 +00001944
1945 /*
1946 * Don't allow secondary CPU threads to come online
1947 * while any KVM VMs exist.
1948 */
1949 inhibit_secondary_onlining();
1950
David Gibson54738c02011-06-29 00:22:41 +00001951 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001952}
1953
1954void kvmppc_core_destroy_vm(struct kvm *kvm)
1955{
Paul Mackerras512691d2012-10-15 01:15:41 +00001956 uninhibit_secondary_onlining();
1957
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001958 if (kvm->arch.rma) {
1959 kvm_release_rma(kvm->arch.rma);
1960 kvm->arch.rma = NULL;
1961 }
1962
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001963 kvmppc_rtas_tokens_free(kvm);
1964
Paul Mackerrasde56a942011-06-29 00:21:34 +00001965 kvmppc_free_hpt(kvm);
David Gibson54738c02011-06-29 00:22:41 +00001966 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
Paul Mackerrasde56a942011-06-29 00:21:34 +00001967}
1968
1969/* These are stubs for now */
1970void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
1971{
1972}
1973
1974/* We don't need to emulate any privileged instructions or dcbz */
1975int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
1976 unsigned int inst, int *advance)
1977{
1978 return EMULATE_FAIL;
1979}
1980
Alexander Graf54771e62012-05-04 14:55:12 +02001981int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001982{
1983 return EMULATE_FAIL;
1984}
1985
Alexander Graf54771e62012-05-04 14:55:12 +02001986int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001987{
1988 return EMULATE_FAIL;
1989}
1990
1991static int kvmppc_book3s_hv_init(void)
1992{
1993 int r;
1994
1995 r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1996
1997 if (r)
1998 return r;
1999
2000 r = kvmppc_mmu_hv_init();
2001
2002 return r;
2003}
2004
2005static void kvmppc_book3s_hv_exit(void)
2006{
2007 kvm_exit();
2008}
2009
2010module_init(kvmppc_book3s_hv_init);
2011module_exit(kvmppc_book3s_hv_exit);