blob: a54c59db7bd82c562c450ab733e0174b5d271575 [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) {
Andreas Schwab250be412013-12-30 15:36:56 +010085#ifdef CONFIG_KVM_XICS
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +000086 int real_cpu = cpu + vcpu->arch.ptid;
87 if (paca[real_cpu].kvm_hstate.xics_phys)
88 xics_wake_cpu(real_cpu);
Andreas Schwab250be412013-12-30 15:36:56 +010089 else
90#endif
91 if (cpu_online(cpu))
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +000092 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
Paul Mackerrasde56a942011-06-29 00:21:34 +0000131void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
132{
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
149void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
150{
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
Paul Mackerrasde56a942011-06-29 00:21:34 +0000161void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
162{
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
167void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
168{
169 vcpu->arch.pvr = pvr;
170}
171
172void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
173{
174 int r;
175
176 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
177 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
178 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
179 for (r = 0; r < 16; ++r)
180 pr_err("r%2d = %.16lx r%d = %.16lx\n",
181 r, kvmppc_get_gpr(vcpu, r),
182 r+16, kvmppc_get_gpr(vcpu, r+16));
183 pr_err("ctr = %.16lx lr = %.16lx\n",
184 vcpu->arch.ctr, vcpu->arch.lr);
185 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
186 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
187 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
188 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
189 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
190 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
191 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
192 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
193 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
194 pr_err("fault dar = %.16lx dsisr = %.8x\n",
195 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
196 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
197 for (r = 0; r < vcpu->arch.slb_max; ++r)
198 pr_err(" ESID = %.16llx VSID = %.16llx\n",
199 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
200 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000201 vcpu->kvm->arch.lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000202 vcpu->arch.last_inst);
203}
204
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000205struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
206{
207 int r;
208 struct kvm_vcpu *v, *ret = NULL;
209
210 mutex_lock(&kvm->lock);
211 kvm_for_each_vcpu(r, v, kvm) {
212 if (v->vcpu_id == id) {
213 ret = v;
214 break;
215 }
216 }
217 mutex_unlock(&kvm->lock);
218 return ret;
219}
220
221static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
222{
223 vpa->shared_proc = 1;
224 vpa->yield_count = 1;
225}
226
Paul Mackerras55b665b2012-09-25 20:33:06 +0000227static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
228 unsigned long addr, unsigned long len)
229{
230 /* check address is cacheline aligned */
231 if (addr & (L1_CACHE_BYTES - 1))
232 return -EINVAL;
233 spin_lock(&vcpu->arch.vpa_update_lock);
234 if (v->next_gpa != addr || v->len != len) {
235 v->next_gpa = addr;
236 v->len = addr ? len : 0;
237 v->update_pending = 1;
238 }
239 spin_unlock(&vcpu->arch.vpa_update_lock);
240 return 0;
241}
242
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000243/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
244struct reg_vpa {
245 u32 dummy;
246 union {
247 u16 hword;
248 u32 word;
249 } length;
250};
251
252static int vpa_is_registered(struct kvmppc_vpa *vpap)
253{
254 if (vpap->update_pending)
255 return vpap->next_gpa != 0;
256 return vpap->pinned_addr != NULL;
257}
258
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000259static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
260 unsigned long flags,
261 unsigned long vcpuid, unsigned long vpa)
262{
263 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000264 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000265 void *va;
266 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000267 int err;
268 int subfunc;
269 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000270
271 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
272 if (!tvcpu)
273 return H_PARAMETER;
274
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000275 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
276 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
277 subfunc == H_VPA_REG_SLB) {
278 /* Registering new area - address must be cache-line aligned */
279 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000280 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000281
282 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000283 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
284 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000285 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000286 if (subfunc == H_VPA_REG_VPA)
287 len = ((struct reg_vpa *)va)->length.hword;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000288 else
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000289 len = ((struct reg_vpa *)va)->length.word;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000290 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000291
292 /* Check length */
293 if (len > nb || len < sizeof(struct reg_vpa))
294 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000295 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000296 vpa = 0;
297 len = 0;
298 }
299
300 err = H_PARAMETER;
301 vpap = NULL;
302 spin_lock(&tvcpu->arch.vpa_update_lock);
303
304 switch (subfunc) {
305 case H_VPA_REG_VPA: /* register VPA */
306 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000307 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000308 vpap = &tvcpu->arch.vpa;
309 err = 0;
310 break;
311
312 case H_VPA_REG_DTL: /* register DTL */
313 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000314 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000315 len -= len % sizeof(struct dtl_entry);
316
317 /* Check that they have previously registered a VPA */
318 err = H_RESOURCE;
319 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000320 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000321
322 vpap = &tvcpu->arch.dtl;
323 err = 0;
324 break;
325
326 case H_VPA_REG_SLB: /* register SLB shadow buffer */
327 /* Check that they have previously registered a VPA */
328 err = H_RESOURCE;
329 if (!vpa_is_registered(&tvcpu->arch.vpa))
330 break;
331
332 vpap = &tvcpu->arch.slb_shadow;
333 err = 0;
334 break;
335
336 case H_VPA_DEREG_VPA: /* deregister VPA */
337 /* Check they don't still have a DTL or SLB buf registered */
338 err = H_RESOURCE;
339 if (vpa_is_registered(&tvcpu->arch.dtl) ||
340 vpa_is_registered(&tvcpu->arch.slb_shadow))
341 break;
342
343 vpap = &tvcpu->arch.vpa;
344 err = 0;
345 break;
346
347 case H_VPA_DEREG_DTL: /* deregister DTL */
348 vpap = &tvcpu->arch.dtl;
349 err = 0;
350 break;
351
352 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
353 vpap = &tvcpu->arch.slb_shadow;
354 err = 0;
355 break;
356 }
357
358 if (vpap) {
359 vpap->next_gpa = vpa;
360 vpap->len = len;
361 vpap->update_pending = 1;
362 }
363
364 spin_unlock(&tvcpu->arch.vpa_update_lock);
365
366 return err;
367}
368
Paul Mackerras081f3232012-06-01 20:20:24 +1000369static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000370{
Paul Mackerras081f3232012-06-01 20:20:24 +1000371 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000372 void *va;
373 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000374 unsigned long gpa;
375
376 /*
377 * We need to pin the page pointed to by vpap->next_gpa,
378 * but we can't call kvmppc_pin_guest_page under the lock
379 * as it does get_user_pages() and down_read(). So we
380 * have to drop the lock, pin the page, then get the lock
381 * again and check that a new area didn't get registered
382 * in the meantime.
383 */
384 for (;;) {
385 gpa = vpap->next_gpa;
386 spin_unlock(&vcpu->arch.vpa_update_lock);
387 va = NULL;
388 nb = 0;
389 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000390 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000391 spin_lock(&vcpu->arch.vpa_update_lock);
392 if (gpa == vpap->next_gpa)
393 break;
394 /* sigh... unpin that one and try again */
395 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000396 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000397 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000398
399 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000400 if (va && nb < vpap->len) {
401 /*
402 * If it's now too short, it must be that userspace
403 * has changed the mappings underlying guest memory,
404 * so unregister the region.
405 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000406 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000407 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000408 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000409 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000410 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
411 vpap->dirty);
412 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000413 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000414 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000415 if (va)
416 vpap->pinned_end = va + vpap->len;
417}
Paul Mackerras93e60242011-12-12 12:28:55 +0000418
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000419static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
420{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000421 if (!(vcpu->arch.vpa.update_pending ||
422 vcpu->arch.slb_shadow.update_pending ||
423 vcpu->arch.dtl.update_pending))
424 return;
425
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000426 spin_lock(&vcpu->arch.vpa_update_lock);
427 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000428 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000429 if (vcpu->arch.vpa.pinned_addr)
430 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000431 }
432 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000433 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000434 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
435 vcpu->arch.dtl_index = 0;
436 }
437 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000438 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000439 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000440}
441
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000442/*
443 * Return the accumulated stolen time for the vcore up until `now'.
444 * The caller should hold the vcore lock.
445 */
446static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
447{
448 u64 p;
449
450 /*
451 * If we are the task running the vcore, then since we hold
452 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
453 * can't be updated, so we don't need the tbacct_lock.
454 * If the vcore is inactive, it can't become active (since we
455 * hold the vcore lock), so the vcpu load/put functions won't
456 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
457 */
458 if (vc->vcore_state != VCORE_INACTIVE &&
459 vc->runner->arch.run_task != current) {
460 spin_lock(&vc->runner->arch.tbacct_lock);
461 p = vc->stolen_tb;
462 if (vc->preempt_tb != TB_NIL)
463 p += now - vc->preempt_tb;
464 spin_unlock(&vc->runner->arch.tbacct_lock);
465 } else {
466 p = vc->stolen_tb;
467 }
468 return p;
469}
470
Paul Mackerras0456ec42012-02-03 00:56:21 +0000471static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
472 struct kvmppc_vcore *vc)
473{
474 struct dtl_entry *dt;
475 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000476 unsigned long stolen;
477 unsigned long core_stolen;
478 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000479
480 dt = vcpu->arch.dtl_ptr;
481 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000482 now = mftb();
483 core_stolen = vcore_stolen_time(vc, now);
484 stolen = core_stolen - vcpu->arch.stolen_logged;
485 vcpu->arch.stolen_logged = core_stolen;
486 spin_lock(&vcpu->arch.tbacct_lock);
487 stolen += vcpu->arch.busy_stolen;
488 vcpu->arch.busy_stolen = 0;
489 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000490 if (!dt || !vpa)
491 return;
492 memset(dt, 0, sizeof(struct dtl_entry));
493 dt->dispatch_reason = 7;
494 dt->processor_id = vc->pcpu + vcpu->arch.ptid;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000495 dt->timebase = now;
496 dt->enqueue_to_dispatch_time = stolen;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000497 dt->srr0 = kvmppc_get_pc(vcpu);
498 dt->srr1 = vcpu->arch.shregs.msr;
499 ++dt;
500 if (dt == vcpu->arch.dtl.pinned_end)
501 dt = vcpu->arch.dtl.pinned_addr;
502 vcpu->arch.dtl_ptr = dt;
503 /* order writing *dt vs. writing vpa->dtl_idx */
504 smp_wmb();
505 vpa->dtl_idx = ++vcpu->arch.dtl_index;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000506 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000507}
508
509int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
510{
511 unsigned long req = kvmppc_get_gpr(vcpu, 3);
512 unsigned long target, ret = H_SUCCESS;
513 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000514 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000515
516 switch (req) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000517 case H_ENTER:
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000518 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000519 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
520 kvmppc_get_gpr(vcpu, 5),
521 kvmppc_get_gpr(vcpu, 6),
522 kvmppc_get_gpr(vcpu, 7));
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000523 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000524 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000525 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000526 break;
527 case H_PROD:
528 target = kvmppc_get_gpr(vcpu, 4);
529 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
530 if (!tvcpu) {
531 ret = H_PARAMETER;
532 break;
533 }
534 tvcpu->arch.prodded = 1;
535 smp_mb();
536 if (vcpu->arch.ceded) {
537 if (waitqueue_active(&vcpu->wq)) {
538 wake_up_interruptible(&vcpu->wq);
539 vcpu->stat.halt_wakeup++;
540 }
541 }
542 break;
543 case H_CONFER:
544 break;
545 case H_REGISTER_VPA:
546 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
547 kvmppc_get_gpr(vcpu, 5),
548 kvmppc_get_gpr(vcpu, 6));
549 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000550 case H_RTAS:
551 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
552 return RESUME_HOST;
553
554 rc = kvmppc_rtas_hcall(vcpu);
555
556 if (rc == -ENOENT)
557 return RESUME_HOST;
558 else if (rc == 0)
559 break;
560
561 /* Send the error out to userspace via KVM_RUN */
562 return rc;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000563
564 case H_XIRR:
565 case H_CPPR:
566 case H_EOI:
567 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000568 case H_IPOLL:
569 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000570 if (kvmppc_xics_enabled(vcpu)) {
571 ret = kvmppc_xics_hcall(vcpu, req);
572 break;
573 } /* fallthrough */
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000574 default:
575 return RESUME_HOST;
576 }
577 kvmppc_set_gpr(vcpu, 3, ret);
578 vcpu->arch.hcall_needed = 0;
579 return RESUME_GUEST;
580}
581
Paul Mackerrasde56a942011-06-29 00:21:34 +0000582static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
583 struct task_struct *tsk)
584{
585 int r = RESUME_HOST;
586
587 vcpu->stat.sum_exits++;
588
589 run->exit_reason = KVM_EXIT_UNKNOWN;
590 run->ready_for_interrupt_injection = 1;
591 switch (vcpu->arch.trap) {
592 /* We're good on these - the host merely wanted to get our attention */
593 case BOOK3S_INTERRUPT_HV_DECREMENTER:
594 vcpu->stat.dec_exits++;
595 r = RESUME_GUEST;
596 break;
597 case BOOK3S_INTERRUPT_EXTERNAL:
598 vcpu->stat.ext_intr_exits++;
599 r = RESUME_GUEST;
600 break;
601 case BOOK3S_INTERRUPT_PERFMON:
602 r = RESUME_GUEST;
603 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000604 case BOOK3S_INTERRUPT_MACHINE_CHECK:
605 /*
606 * Deliver a machine check interrupt to the guest.
607 * We have to do this, even if the host has handled the
608 * machine check, because machine checks use SRR0/1 and
609 * the interrupt might have trashed guest state in them.
610 */
611 kvmppc_book3s_queue_irqprio(vcpu,
612 BOOK3S_INTERRUPT_MACHINE_CHECK);
613 r = RESUME_GUEST;
614 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000615 case BOOK3S_INTERRUPT_PROGRAM:
616 {
617 ulong flags;
618 /*
619 * Normally program interrupts are delivered directly
620 * to the guest by the hardware, but we can get here
621 * as a result of a hypervisor emulation interrupt
622 * (e40) getting turned into a 700 by BML RTAS.
623 */
624 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
625 kvmppc_core_queue_program(vcpu, flags);
626 r = RESUME_GUEST;
627 break;
628 }
629 case BOOK3S_INTERRUPT_SYSCALL:
630 {
631 /* hcall - punt to userspace */
632 int i;
633
634 if (vcpu->arch.shregs.msr & MSR_PR) {
635 /* sc 1 from userspace - reflect to guest syscall */
636 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
637 r = RESUME_GUEST;
638 break;
639 }
640 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
641 for (i = 0; i < 9; ++i)
642 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
643 run->exit_reason = KVM_EXIT_PAPR_HCALL;
644 vcpu->arch.hcall_needed = 1;
645 r = RESUME_HOST;
646 break;
647 }
648 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000649 * We get these next two if the guest accesses a page which it thinks
650 * it has mapped but which is not actually present, either because
651 * it is for an emulated I/O device or because the corresonding
652 * host page has been paged out. Any other HDSI/HISI interrupts
653 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000654 */
655 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000656 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000657 break;
658 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000659 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
660 vcpu->arch.fault_dsisr = 0;
661 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000662 break;
663 /*
664 * This occurs if the guest executes an illegal instruction.
665 * We just generate a program interrupt to the guest, since
666 * we don't emulate any guest instructions at this stage.
667 */
668 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
669 kvmppc_core_queue_program(vcpu, 0x80000);
670 r = RESUME_GUEST;
671 break;
672 default:
673 kvmppc_dump_regs(vcpu);
674 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
675 vcpu->arch.trap, kvmppc_get_pc(vcpu),
676 vcpu->arch.shregs.msr);
677 r = RESUME_HOST;
678 BUG();
679 break;
680 }
681
Paul Mackerrasde56a942011-06-29 00:21:34 +0000682 return r;
683}
684
685int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
686 struct kvm_sregs *sregs)
687{
688 int i;
689
690 sregs->pvr = vcpu->arch.pvr;
691
692 memset(sregs, 0, sizeof(struct kvm_sregs));
693 for (i = 0; i < vcpu->arch.slb_max; i++) {
694 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
695 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
696 }
697
698 return 0;
699}
700
701int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
702 struct kvm_sregs *sregs)
703{
704 int i, j;
705
706 kvmppc_set_pvr(vcpu, sregs->pvr);
707
708 j = 0;
709 for (i = 0; i < vcpu->arch.slb_nr; i++) {
710 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
711 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
712 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
713 ++j;
714 }
715 }
716 vcpu->arch.slb_max = j;
717
718 return 0;
719}
720
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000721int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000722{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000723 int r = 0;
724 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +0000725
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000726 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000727 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000728 *val = get_reg_val(id, 0);
729 break;
730 case KVM_REG_PPC_DABR:
731 *val = get_reg_val(id, vcpu->arch.dabr);
732 break;
733 case KVM_REG_PPC_DSCR:
734 *val = get_reg_val(id, vcpu->arch.dscr);
735 break;
736 case KVM_REG_PPC_PURR:
737 *val = get_reg_val(id, vcpu->arch.purr);
738 break;
739 case KVM_REG_PPC_SPURR:
740 *val = get_reg_val(id, vcpu->arch.spurr);
741 break;
742 case KVM_REG_PPC_AMR:
743 *val = get_reg_val(id, vcpu->arch.amr);
744 break;
745 case KVM_REG_PPC_UAMOR:
746 *val = get_reg_val(id, vcpu->arch.uamor);
747 break;
748 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
749 i = id - KVM_REG_PPC_MMCR0;
750 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
751 break;
752 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
753 i = id - KVM_REG_PPC_PMC1;
754 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +0000755 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000756#ifdef CONFIG_VSX
757 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
758 if (cpu_has_feature(CPU_FTR_VSX)) {
759 /* VSX => FP reg i is stored in arch.vsr[2*i] */
760 long int i = id - KVM_REG_PPC_FPR0;
761 *val = get_reg_val(id, vcpu->arch.vsr[2 * i]);
762 } else {
763 /* let generic code handle it */
764 r = -EINVAL;
765 }
766 break;
767 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
768 if (cpu_has_feature(CPU_FTR_VSX)) {
769 long int i = id - KVM_REG_PPC_VSR0;
770 val->vsxval[0] = vcpu->arch.vsr[2 * i];
771 val->vsxval[1] = vcpu->arch.vsr[2 * i + 1];
772 } else {
773 r = -ENXIO;
774 }
775 break;
776#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000777 case KVM_REG_PPC_VPA_ADDR:
778 spin_lock(&vcpu->arch.vpa_update_lock);
779 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
780 spin_unlock(&vcpu->arch.vpa_update_lock);
781 break;
782 case KVM_REG_PPC_VPA_SLB:
783 spin_lock(&vcpu->arch.vpa_update_lock);
784 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
785 val->vpaval.length = vcpu->arch.slb_shadow.len;
786 spin_unlock(&vcpu->arch.vpa_update_lock);
787 break;
788 case KVM_REG_PPC_VPA_DTL:
789 spin_lock(&vcpu->arch.vpa_update_lock);
790 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
791 val->vpaval.length = vcpu->arch.dtl.len;
792 spin_unlock(&vcpu->arch.vpa_update_lock);
793 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000794 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000795 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000796 break;
797 }
798
799 return r;
800}
801
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000802int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000803{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000804 int r = 0;
805 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +0000806 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +0000807
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000808 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000809 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +0000810 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000811 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +0000812 r = -EINVAL;
813 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000814 case KVM_REG_PPC_DABR:
815 vcpu->arch.dabr = set_reg_val(id, *val);
816 break;
817 case KVM_REG_PPC_DSCR:
818 vcpu->arch.dscr = set_reg_val(id, *val);
819 break;
820 case KVM_REG_PPC_PURR:
821 vcpu->arch.purr = set_reg_val(id, *val);
822 break;
823 case KVM_REG_PPC_SPURR:
824 vcpu->arch.spurr = set_reg_val(id, *val);
825 break;
826 case KVM_REG_PPC_AMR:
827 vcpu->arch.amr = set_reg_val(id, *val);
828 break;
829 case KVM_REG_PPC_UAMOR:
830 vcpu->arch.uamor = set_reg_val(id, *val);
831 break;
832 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
833 i = id - KVM_REG_PPC_MMCR0;
834 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
835 break;
836 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
837 i = id - KVM_REG_PPC_PMC1;
838 vcpu->arch.pmc[i] = set_reg_val(id, *val);
839 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000840#ifdef CONFIG_VSX
841 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
842 if (cpu_has_feature(CPU_FTR_VSX)) {
843 /* VSX => FP reg i is stored in arch.vsr[2*i] */
844 long int i = id - KVM_REG_PPC_FPR0;
845 vcpu->arch.vsr[2 * i] = set_reg_val(id, *val);
846 } else {
847 /* let generic code handle it */
848 r = -EINVAL;
849 }
850 break;
851 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
852 if (cpu_has_feature(CPU_FTR_VSX)) {
853 long int i = id - KVM_REG_PPC_VSR0;
854 vcpu->arch.vsr[2 * i] = val->vsxval[0];
855 vcpu->arch.vsr[2 * i + 1] = val->vsxval[1];
856 } else {
857 r = -ENXIO;
858 }
859 break;
860#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000861 case KVM_REG_PPC_VPA_ADDR:
862 addr = set_reg_val(id, *val);
863 r = -EINVAL;
864 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
865 vcpu->arch.dtl.next_gpa))
866 break;
867 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
868 break;
869 case KVM_REG_PPC_VPA_SLB:
870 addr = val->vpaval.addr;
871 len = val->vpaval.length;
872 r = -EINVAL;
873 if (addr && !vcpu->arch.vpa.next_gpa)
874 break;
875 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
876 break;
877 case KVM_REG_PPC_VPA_DTL:
878 addr = val->vpaval.addr;
879 len = val->vpaval.length;
880 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +0000881 if (addr && (len < sizeof(struct dtl_entry) ||
882 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +0000883 break;
884 len -= len % sizeof(struct dtl_entry);
885 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
886 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000887 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000888 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000889 break;
890 }
891
892 return r;
893}
894
Paul Mackerrasde56a942011-06-29 00:21:34 +0000895int kvmppc_core_check_processor_compat(void)
896{
Paul Mackerras9e368f22011-06-29 00:40:08 +0000897 if (cpu_has_feature(CPU_FTR_HVMODE))
Paul Mackerrasde56a942011-06-29 00:21:34 +0000898 return 0;
899 return -EIO;
900}
901
902struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
903{
904 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000905 int err = -EINVAL;
906 int core;
907 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000908
Paul Mackerras371fefd2011-06-29 00:23:08 +0000909 core = id / threads_per_core;
910 if (core >= KVM_MAX_VCORES)
911 goto out;
912
913 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200914 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000915 if (!vcpu)
916 goto out;
917
918 err = kvm_vcpu_init(vcpu, kvm, id);
919 if (err)
920 goto free_vcpu;
921
922 vcpu->arch.shared = &vcpu->arch.shregs;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000923 vcpu->arch.mmcr[0] = MMCR0_FC;
924 vcpu->arch.ctrl = CTRL_RUNLATCH;
925 /* default to host PVR, since we can't spoof it */
926 vcpu->arch.pvr = mfspr(SPRN_PVR);
927 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000928 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000929 spin_lock_init(&vcpu->arch.tbacct_lock);
930 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000931
Paul Mackerrasde56a942011-06-29 00:21:34 +0000932 kvmppc_mmu_book3s_hv_init(vcpu);
933
Paul Mackerras8455d792012-10-15 01:17:42 +0000934 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000935
936 init_waitqueue_head(&vcpu->arch.cpu_run);
937
938 mutex_lock(&kvm->lock);
939 vcore = kvm->arch.vcores[core];
940 if (!vcore) {
941 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
942 if (vcore) {
943 INIT_LIST_HEAD(&vcore->runnable_threads);
944 spin_lock_init(&vcore->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000945 init_waitqueue_head(&vcore->wq);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000946 vcore->preempt_tb = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000947 }
948 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000949 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000950 }
951 mutex_unlock(&kvm->lock);
952
953 if (!vcore)
954 goto free_vcpu;
955
956 spin_lock(&vcore->lock);
957 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000958 spin_unlock(&vcore->lock);
959 vcpu->arch.vcore = vcore;
960
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200961 vcpu->arch.cpu_type = KVM_CPU_3S_64;
962 kvmppc_sanity_check(vcpu);
963
Paul Mackerrasde56a942011-06-29 00:21:34 +0000964 return vcpu;
965
966free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200967 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000968out:
969 return ERR_PTR(err);
970}
971
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000972static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
973{
974 if (vpa->pinned_addr)
975 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
976 vpa->dirty);
977}
978
Paul Mackerrasde56a942011-06-29 00:21:34 +0000979void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
980{
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000981 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000982 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
983 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
984 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000985 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000986 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200987 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000988}
989
Paul Mackerras19ccb762011-07-23 17:42:46 +1000990static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000991{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000992 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000993
Paul Mackerras19ccb762011-07-23 17:42:46 +1000994 now = get_tb();
995 if (now > vcpu->arch.dec_expires) {
996 /* decrementer has already gone negative */
997 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -0600998 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000999 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001000 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001001 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1002 / tb_ticks_per_sec;
1003 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1004 HRTIMER_MODE_REL);
1005 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001006}
1007
Paul Mackerras19ccb762011-07-23 17:42:46 +10001008static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001009{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001010 vcpu->arch.ceded = 0;
1011 if (vcpu->arch.timer_running) {
1012 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1013 vcpu->arch.timer_running = 0;
1014 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001015}
1016
1017extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001018
1019static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1020 struct kvm_vcpu *vcpu)
1021{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001022 u64 now;
1023
Paul Mackerras371fefd2011-06-29 00:23:08 +00001024 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1025 return;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001026 spin_lock(&vcpu->arch.tbacct_lock);
1027 now = mftb();
1028 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1029 vcpu->arch.stolen_logged;
1030 vcpu->arch.busy_preempt = now;
1031 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1032 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001033 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001034 list_del(&vcpu->arch.run_list);
1035}
1036
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001037static int kvmppc_grab_hwthread(int cpu)
1038{
1039 struct paca_struct *tpaca;
1040 long timeout = 1000;
1041
1042 tpaca = &paca[cpu];
1043
1044 /* Ensure the thread won't go into the kernel if it wakes */
1045 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001046 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001047
1048 /*
1049 * If the thread is already executing in the kernel (e.g. handling
1050 * a stray interrupt), wait for it to get back to nap mode.
1051 * The smp_mb() is to ensure that our setting of hwthread_req
1052 * is visible before we look at hwthread_state, so if this
1053 * races with the code at system_reset_pSeries and the thread
1054 * misses our setting of hwthread_req, we are sure to see its
1055 * setting of hwthread_state, and vice versa.
1056 */
1057 smp_mb();
1058 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1059 if (--timeout <= 0) {
1060 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1061 return -EBUSY;
1062 }
1063 udelay(1);
1064 }
1065 return 0;
1066}
1067
1068static void kvmppc_release_hwthread(int cpu)
1069{
1070 struct paca_struct *tpaca;
1071
1072 tpaca = &paca[cpu];
1073 tpaca->kvm_hstate.hwthread_req = 0;
1074 tpaca->kvm_hstate.kvm_vcpu = NULL;
1075}
1076
Paul Mackerras371fefd2011-06-29 00:23:08 +00001077static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1078{
1079 int cpu;
1080 struct paca_struct *tpaca;
1081 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1082
Paul Mackerras19ccb762011-07-23 17:42:46 +10001083 if (vcpu->arch.timer_running) {
1084 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1085 vcpu->arch.timer_running = 0;
1086 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001087 cpu = vc->pcpu + vcpu->arch.ptid;
1088 tpaca = &paca[cpu];
1089 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1090 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001091 tpaca->kvm_hstate.napping = 0;
1092 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001093 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +00001094#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001095 if (vcpu->arch.ptid) {
Andreas Schwab250be412013-12-30 15:36:56 +01001096#ifdef CONFIG_KVM_XICS
Paul Mackerras371fefd2011-06-29 00:23:08 +00001097 xics_wake_cpu(cpu);
Andreas Schwab250be412013-12-30 15:36:56 +01001098#endif
Paul Mackerras371fefd2011-06-29 00:23:08 +00001099 ++vc->n_woken;
1100 }
1101#endif
1102}
1103
1104static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1105{
1106 int i;
1107
1108 HMT_low();
1109 i = 0;
1110 while (vc->nap_count < vc->n_woken) {
1111 if (++i >= 1000000) {
1112 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1113 vc->nap_count, vc->n_woken);
1114 break;
1115 }
1116 cpu_relax();
1117 }
1118 HMT_medium();
1119}
1120
1121/*
1122 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001123 * this core are off-line. Then grab the threads so they can't
1124 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001125 */
1126static int on_primary_thread(void)
1127{
1128 int cpu = smp_processor_id();
1129 int thr = cpu_thread_in_core(cpu);
1130
1131 if (thr)
1132 return 0;
1133 while (++thr < threads_per_core)
1134 if (cpu_online(cpu + thr))
1135 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001136
1137 /* Grab all hw threads so they can't go into the kernel */
1138 for (thr = 1; thr < threads_per_core; ++thr) {
1139 if (kvmppc_grab_hwthread(cpu + thr)) {
1140 /* Couldn't grab one; let the others go */
1141 do {
1142 kvmppc_release_hwthread(cpu + thr);
1143 } while (--thr > 0);
1144 return 0;
1145 }
1146 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001147 return 1;
1148}
1149
1150/*
1151 * Run a set of guest threads on a physical core.
1152 * Called with vc->lock held.
1153 */
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001154static void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001155{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001156 struct kvm_vcpu *vcpu, *vcpu0, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001157 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001158 u64 now;
Paul Mackerras081f3232012-06-01 20:20:24 +10001159 int ptid, i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001160 int srcu_idx;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001161 struct kvm_vcpu *vcpus_to_update[threads_per_core];
Paul Mackerrasde56a942011-06-29 00:21:34 +00001162
Paul Mackerras371fefd2011-06-29 00:23:08 +00001163 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +10001164 need_vpa_update = 0;
1165 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001166 if (signal_pending(vcpu->arch.run_task))
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001167 return;
1168 if (vcpu->arch.vpa.update_pending ||
1169 vcpu->arch.slb_shadow.update_pending ||
1170 vcpu->arch.dtl.update_pending)
1171 vcpus_to_update[need_vpa_update++] = vcpu;
Paul Mackerras081f3232012-06-01 20:20:24 +10001172 }
1173
1174 /*
1175 * Initialize *vc, in particular vc->vcore_state, so we can
1176 * drop the vcore lock if necessary.
1177 */
1178 vc->n_woken = 0;
1179 vc->nap_count = 0;
1180 vc->entry_exit_count = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001181 vc->vcore_state = VCORE_STARTING;
Paul Mackerras081f3232012-06-01 20:20:24 +10001182 vc->in_guest = 0;
1183 vc->napping_threads = 0;
1184
1185 /*
1186 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1187 * which can't be called with any spinlocks held.
1188 */
1189 if (need_vpa_update) {
1190 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001191 for (i = 0; i < need_vpa_update; ++i)
1192 kvmppc_update_vpas(vcpus_to_update[i]);
Paul Mackerras081f3232012-06-01 20:20:24 +10001193 spin_lock(&vc->lock);
1194 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001195
1196 /*
Paul Mackerras19ccb762011-07-23 17:42:46 +10001197 * Assign physical thread IDs, first to non-ceded vcpus
1198 * and then to ceded ones.
1199 */
1200 ptid = 0;
1201 vcpu0 = NULL;
1202 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1203 if (!vcpu->arch.ceded) {
1204 if (!ptid)
1205 vcpu0 = vcpu;
1206 vcpu->arch.ptid = ptid++;
1207 }
1208 }
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001209 if (!vcpu0)
1210 goto out; /* nothing to run; should never happen */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001211 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1212 if (vcpu->arch.ceded)
1213 vcpu->arch.ptid = ptid++;
1214
Paul Mackerras7b444c62012-10-15 01:16:14 +00001215 /*
1216 * Make sure we are running on thread 0, and that
1217 * secondary threads are offline.
1218 */
1219 if (threads_per_core > 1 && !on_primary_thread()) {
1220 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1221 vcpu->arch.ret = -EBUSY;
1222 goto out;
1223 }
1224
Paul Mackerras371fefd2011-06-29 00:23:08 +00001225 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001226 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001227 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001228 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001229 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001230
Paul Mackerras2f12f032012-10-15 01:17:17 +00001231 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001232 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001233 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001234
Paul Mackerras19ccb762011-07-23 17:42:46 +10001235 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001236
1237 srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
1238
Paul Mackerras19ccb762011-07-23 17:42:46 +10001239 __kvmppc_vcore_entry(NULL, vcpu0);
1240
Paul Mackerras371fefd2011-06-29 00:23:08 +00001241 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001242 /* disable sending of IPIs on virtual external irqs */
1243 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1244 vcpu->cpu = -1;
1245 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001246 if (vc->nap_count < vc->n_woken)
1247 kvmppc_wait_for_nap(vc);
Paul Mackerras2f12f032012-10-15 01:17:17 +00001248 for (i = 0; i < threads_per_core; ++i)
1249 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001250 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001251 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001252 spin_unlock(&vc->lock);
1253
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001254 srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
1255
Paul Mackerras371fefd2011-06-29 00:23:08 +00001256 /* make sure updates to secondary vcpu structs are visible now */
1257 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001258 kvm_guest_exit();
1259
1260 preempt_enable();
Takuya Yoshikawa580ab462013-12-13 15:07:21 +09001261 cond_resched();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001262
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001263 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001264 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001265 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1266 /* cancel pending dec exception if dec is positive */
1267 if (now < vcpu->arch.dec_expires &&
1268 kvmppc_core_pending_dec(vcpu))
1269 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001270
1271 ret = RESUME_GUEST;
1272 if (vcpu->arch.trap)
1273 ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
1274 vcpu->arch.run_task);
1275
Paul Mackerras371fefd2011-06-29 00:23:08 +00001276 vcpu->arch.ret = ret;
1277 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001278
1279 if (vcpu->arch.ceded) {
1280 if (ret != RESUME_GUEST)
1281 kvmppc_end_cede(vcpu);
1282 else
1283 kvmppc_set_timer(vcpu);
1284 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001285 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001286
Paul Mackerrasde56a942011-06-29 00:21:34 +00001287 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001288 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001289 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1290 arch.run_list) {
1291 if (vcpu->arch.ret != RESUME_GUEST) {
1292 kvmppc_remove_runnable(vc, vcpu);
1293 wake_up(&vcpu->arch.cpu_run);
1294 }
1295 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001296}
1297
Paul Mackerras19ccb762011-07-23 17:42:46 +10001298/*
1299 * Wait for some other vcpu thread to execute us, and
1300 * wake us up when we need to handle something in the host.
1301 */
1302static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001303{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001304 DEFINE_WAIT(wait);
1305
Paul Mackerras19ccb762011-07-23 17:42:46 +10001306 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1307 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1308 schedule();
1309 finish_wait(&vcpu->arch.cpu_run, &wait);
1310}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001311
Paul Mackerras19ccb762011-07-23 17:42:46 +10001312/*
1313 * All the vcpus in this vcore are idle, so wait for a decrementer
1314 * or external interrupt to one of the vcpus. vc->lock is held.
1315 */
1316static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1317{
1318 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001319
1320 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1321 vc->vcore_state = VCORE_SLEEPING;
1322 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001323 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001324 finish_wait(&vc->wq, &wait);
1325 spin_lock(&vc->lock);
1326 vc->vcore_state = VCORE_INACTIVE;
1327}
1328
1329static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1330{
1331 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001332 struct kvmppc_vcore *vc;
1333 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001334
Paul Mackerras371fefd2011-06-29 00:23:08 +00001335 kvm_run->exit_reason = 0;
1336 vcpu->arch.ret = RESUME_GUEST;
1337 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001338 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001339
Paul Mackerras371fefd2011-06-29 00:23:08 +00001340 /*
1341 * Synchronize with other threads in this virtual core
1342 */
1343 vc = vcpu->arch.vcore;
1344 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001345 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001346 vcpu->arch.run_task = current;
1347 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001348 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10001349 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001350 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001351 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1352 ++vc->n_runnable;
1353
Paul Mackerras19ccb762011-07-23 17:42:46 +10001354 /*
1355 * This happens the first time this is called for a vcpu.
1356 * If the vcore is already running, we may be able to start
1357 * this thread straight away and have it join in.
1358 */
Paul Mackerras8455d792012-10-15 01:17:42 +00001359 if (!signal_pending(current)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001360 if (vc->vcore_state == VCORE_RUNNING &&
1361 VCORE_EXIT_COUNT(vc) == 0) {
1362 vcpu->arch.ptid = vc->n_runnable - 1;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001363 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001364 kvmppc_start_thread(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00001365 } else if (vc->vcore_state == VCORE_SLEEPING) {
1366 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001367 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001368
Paul Mackerras8455d792012-10-15 01:17:42 +00001369 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001370
1371 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1372 !signal_pending(current)) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001373 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001374 spin_unlock(&vc->lock);
1375 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1376 spin_lock(&vc->lock);
1377 continue;
1378 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001379 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1380 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001381 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001382 if (signal_pending(v->arch.run_task)) {
1383 kvmppc_remove_runnable(vc, v);
1384 v->stat.signal_exits++;
1385 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1386 v->arch.ret = -EINTR;
1387 wake_up(&v->arch.cpu_run);
1388 }
1389 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001390 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1391 break;
1392 vc->runner = vcpu;
1393 n_ceded = 0;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001394 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001395 if (!v->arch.pending_exceptions)
1396 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001397 else
1398 v->arch.ceded = 0;
1399 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001400 if (n_ceded == vc->n_runnable)
1401 kvmppc_vcore_blocked(vc);
1402 else
1403 kvmppc_run_core(vc);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001404 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001405 }
1406
Paul Mackerras8455d792012-10-15 01:17:42 +00001407 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1408 (vc->vcore_state == VCORE_RUNNING ||
1409 vc->vcore_state == VCORE_EXITING)) {
1410 spin_unlock(&vc->lock);
1411 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1412 spin_lock(&vc->lock);
1413 }
1414
1415 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1416 kvmppc_remove_runnable(vc, vcpu);
1417 vcpu->stat.signal_exits++;
1418 kvm_run->exit_reason = KVM_EXIT_INTR;
1419 vcpu->arch.ret = -EINTR;
1420 }
1421
1422 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1423 /* Wake up some vcpu to run the core */
1424 v = list_first_entry(&vc->runnable_threads,
1425 struct kvm_vcpu, arch.run_list);
1426 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001427 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001428
Paul Mackerras19ccb762011-07-23 17:42:46 +10001429 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001430 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001431}
1432
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001433int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
1434{
1435 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001436 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001437
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001438 if (!vcpu->arch.sane) {
1439 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1440 return -EINVAL;
1441 }
1442
Scott Wood25051b52011-11-08 18:23:23 -06001443 kvmppc_core_prepare_to_enter(vcpu);
1444
Paul Mackerras19ccb762011-07-23 17:42:46 +10001445 /* No need to go into the guest when all we'll do is come back out */
1446 if (signal_pending(current)) {
1447 run->exit_reason = KVM_EXIT_INTR;
1448 return -EINTR;
1449 }
1450
Paul Mackerras32fad282012-05-04 02:32:53 +00001451 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1452 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1453 smp_mb();
1454
1455 /* On the first time here, set up HTAB and VRMA or RMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001456 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001457 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001458 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001459 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001460 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001461
1462 flush_fp_to_thread(current);
1463 flush_altivec_to_thread(current);
1464 flush_vsx_to_thread(current);
1465 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001466 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001467 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001468
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001469 do {
1470 r = kvmppc_run_vcpu(run, vcpu);
1471
1472 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1473 !(vcpu->arch.shregs.msr & MSR_PR)) {
1474 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001475 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001476 } else if (r == RESUME_PAGE_FAULT) {
1477 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1478 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1479 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1480 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001481 }
1482 } while (r == RESUME_GUEST);
Paul Mackerras32fad282012-05-04 02:32:53 +00001483
1484 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001485 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00001486 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001487 return r;
1488}
1489
David Gibson54738c02011-06-29 00:22:41 +00001490
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001491/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +00001492 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001493static inline int lpcr_rmls(unsigned long rma_size)
1494{
1495 switch (rma_size) {
1496 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001497 if (cpu_has_feature(CPU_FTR_ARCH_206))
1498 return 8; /* only supported on POWER7 */
1499 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001500 case 64ul << 20: /* 64 MB */
1501 return 3;
1502 case 128ul << 20: /* 128 MB */
1503 return 7;
1504 case 256ul << 20: /* 256 MB */
1505 return 4;
1506 case 1ul << 30: /* 1 GB */
1507 return 2;
1508 case 16ul << 30: /* 16 GB */
1509 return 1;
1510 case 256ul << 30: /* 256 GB */
1511 return 0;
1512 default:
1513 return -1;
1514 }
1515}
1516
1517static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1518{
Alexander Grafb4e70612012-01-16 16:50:10 +01001519 struct kvmppc_linear_info *ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001520 struct page *page;
1521
1522 if (vmf->pgoff >= ri->npages)
1523 return VM_FAULT_SIGBUS;
1524
1525 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1526 get_page(page);
1527 vmf->page = page;
1528 return 0;
1529}
1530
1531static const struct vm_operations_struct kvm_rma_vm_ops = {
1532 .fault = kvm_rma_fault,
1533};
1534
1535static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1536{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001537 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001538 vma->vm_ops = &kvm_rma_vm_ops;
1539 return 0;
1540}
1541
1542static int kvm_rma_release(struct inode *inode, struct file *filp)
1543{
Alexander Grafb4e70612012-01-16 16:50:10 +01001544 struct kvmppc_linear_info *ri = filp->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001545
1546 kvm_release_rma(ri);
1547 return 0;
1548}
1549
Al Viro75ef9de2013-04-04 19:09:41 -04001550static const struct file_operations kvm_rma_fops = {
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001551 .mmap = kvm_rma_mmap,
1552 .release = kvm_rma_release,
1553};
1554
1555long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
1556{
Alexander Grafb4e70612012-01-16 16:50:10 +01001557 struct kvmppc_linear_info *ri;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001558 long fd;
1559
1560 ri = kvm_alloc_rma();
1561 if (!ri)
1562 return -ENOMEM;
1563
1564 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR);
1565 if (fd < 0)
1566 kvm_release_rma(ri);
1567
1568 ret->rma_size = ri->npages << PAGE_SHIFT;
1569 return fd;
1570}
1571
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001572static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1573 int linux_psize)
1574{
1575 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1576
1577 if (!def->shift)
1578 return;
1579 (*sps)->page_shift = def->shift;
1580 (*sps)->slb_enc = def->sllp;
1581 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00001582 /*
1583 * Only return base page encoding. We don't want to return
1584 * all the supporting pte_enc, because our H_ENTER doesn't
1585 * support MPSS yet. Once they do, we can start passing all
1586 * support pte_enc here
1587 */
1588 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001589 (*sps)++;
1590}
1591
1592int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
1593{
1594 struct kvm_ppc_one_seg_page_size *sps;
1595
1596 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1597 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1598 info->flags |= KVM_PPC_1T_SEGMENTS;
1599 info->slb_size = mmu_slb_size;
1600
1601 /* We only support these sizes for now, and no muti-size segments */
1602 sps = &info->sps[0];
1603 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1604 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1605 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1606
1607 return 0;
1608}
1609
Paul Mackerras82ed3612011-12-15 02:03:22 +00001610/*
1611 * Get (and clear) the dirty memory log for a memory slot.
1612 */
1613int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1614{
1615 struct kvm_memory_slot *memslot;
1616 int r;
1617 unsigned long n;
1618
1619 mutex_lock(&kvm->slots_lock);
1620
1621 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07001622 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00001623 goto out;
1624
1625 memslot = id_to_memslot(kvm->memslots, log->slot);
1626 r = -ENOENT;
1627 if (!memslot->dirty_bitmap)
1628 goto out;
1629
1630 n = kvm_dirty_bitmap_bytes(memslot);
1631 memset(memslot->dirty_bitmap, 0, n);
1632
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001633 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00001634 if (r)
1635 goto out;
1636
1637 r = -EFAULT;
1638 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1639 goto out;
1640
1641 r = 0;
1642out:
1643 mutex_unlock(&kvm->slots_lock);
1644 return r;
1645}
1646
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001647static void unpin_slot(struct kvm_memory_slot *memslot)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001648{
1649 unsigned long *physp;
1650 unsigned long j, npages, pfn;
1651 struct page *page;
1652
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001653 physp = memslot->arch.slot_phys;
1654 npages = memslot->npages;
1655 if (!physp)
1656 return;
1657 for (j = 0; j < npages; j++) {
1658 if (!(physp[j] & KVMPPC_GOT_PAGE))
1659 continue;
1660 pfn = physp[j] >> PAGE_SHIFT;
1661 page = pfn_to_page(pfn);
1662 SetPageDirty(page);
1663 put_page(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001664 }
1665}
1666
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001667void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1668 struct kvm_memory_slot *dont)
1669{
1670 if (!dont || free->arch.rmap != dont->arch.rmap) {
1671 vfree(free->arch.rmap);
1672 free->arch.rmap = NULL;
1673 }
1674 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1675 unpin_slot(free);
1676 vfree(free->arch.slot_phys);
1677 free->arch.slot_phys = NULL;
1678 }
1679}
1680
1681int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1682 unsigned long npages)
1683{
1684 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1685 if (!slot->arch.rmap)
1686 return -ENOMEM;
1687 slot->arch.slot_phys = NULL;
1688
1689 return 0;
1690}
1691
1692int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1693 struct kvm_memory_slot *memslot,
1694 struct kvm_userspace_memory_region *mem)
1695{
1696 unsigned long *phys;
1697
1698 /* Allocate a slot_phys array if needed */
1699 phys = memslot->arch.slot_phys;
1700 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1701 phys = vzalloc(memslot->npages * sizeof(unsigned long));
1702 if (!phys)
1703 return -ENOMEM;
1704 memslot->arch.slot_phys = phys;
1705 }
1706
1707 return 0;
1708}
1709
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001710void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001711 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001712 const struct kvm_memory_slot *old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001713{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001714 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1715 struct kvm_memory_slot *memslot;
1716
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001717 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001718 /*
1719 * If modifying a memslot, reset all the rmap dirty bits.
1720 * If this is a new memslot, we don't need to do anything
1721 * since the rmap array starts out as all zeroes,
1722 * i.e. no pages are dirty.
1723 */
1724 memslot = id_to_memslot(kvm->memslots, mem->slot);
1725 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1726 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001727}
1728
Paul Mackerras32fad282012-05-04 02:32:53 +00001729static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001730{
1731 int err = 0;
1732 struct kvm *kvm = vcpu->kvm;
Alexander Grafb4e70612012-01-16 16:50:10 +01001733 struct kvmppc_linear_info *ri = NULL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001734 unsigned long hva;
1735 struct kvm_memory_slot *memslot;
1736 struct vm_area_struct *vma;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001737 unsigned long lpcr, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001738 unsigned long psize, porder;
1739 unsigned long rma_size;
1740 unsigned long rmls;
1741 unsigned long *physp;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001742 unsigned long i, npages;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001743 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001744
1745 mutex_lock(&kvm->lock);
1746 if (kvm->arch.rma_setup_done)
1747 goto out; /* another vcpu beat us to it */
1748
Paul Mackerras32fad282012-05-04 02:32:53 +00001749 /* Allocate hashed page table (if not done already) and reset it */
1750 if (!kvm->arch.hpt_virt) {
1751 err = kvmppc_alloc_hpt(kvm, NULL);
1752 if (err) {
1753 pr_err("KVM: Couldn't alloc HPT\n");
1754 goto out;
1755 }
1756 }
1757
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001758 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001759 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001760 memslot = gfn_to_memslot(kvm, 0);
1761
1762 /* We must have some memory at 0 by now */
1763 err = -EINVAL;
1764 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001765 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001766
1767 /* Look up the VMA for the start of this memory slot */
1768 hva = memslot->userspace_addr;
1769 down_read(&current->mm->mmap_sem);
1770 vma = find_vma(current->mm, hva);
1771 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
1772 goto up_out;
1773
1774 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001775 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001776
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001777 /* Is this one of our preallocated RMAs? */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001778 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
1779 hva == vma->vm_start)
1780 ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001781
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001782 up_read(&current->mm->mmap_sem);
1783
1784 if (!ri) {
1785 /* On POWER7, use VRMA; on PPC970, give up */
1786 err = -EPERM;
1787 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1788 pr_err("KVM: CPU requires an RMO\n");
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001789 goto out_srcu;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001790 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001791
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001792 /* We can handle 4k, 64k or 16M pages in the VRMA */
1793 err = -EINVAL;
1794 if (!(psize == 0x1000 || psize == 0x10000 ||
1795 psize == 0x1000000))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001796 goto out_srcu;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001797
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001798 /* Update VRMASD field in the LPCR */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001799 senc = slb_pgsize_encoding(psize);
Paul Mackerras697d3892011-12-12 12:36:37 +00001800 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1801 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001802 lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
1803 lpcr |= senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001804 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001805
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001806 /* Create HPTEs in the hash page table for the VRMA */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001807 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001808
1809 } else {
1810 /* Set up to use an RMO region */
1811 rma_size = ri->npages;
1812 if (rma_size > memslot->npages)
1813 rma_size = memslot->npages;
1814 rma_size <<= PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001815 rmls = lpcr_rmls(rma_size);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001816 err = -EINVAL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001817 if (rmls < 0) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001818 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001819 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001820 }
1821 atomic_inc(&ri->use_count);
1822 kvm->arch.rma = ri;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001823
1824 /* Update LPCR and RMOR */
1825 lpcr = kvm->arch.lpcr;
1826 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1827 /* PPC970; insert RMLS value (split field) in HID4 */
1828 lpcr &= ~((1ul << HID4_RMLS0_SH) |
1829 (3ul << HID4_RMLS2_SH));
1830 lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
1831 ((rmls & 3) << HID4_RMLS2_SH);
1832 /* RMOR is also in HID4 */
1833 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
1834 << HID4_RMOR_SH;
1835 } else {
1836 /* POWER7 */
1837 lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
1838 lpcr |= rmls << LPCR_RMLS_SH;
1839 kvm->arch.rmor = kvm->arch.rma->base_pfn << PAGE_SHIFT;
1840 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001841 kvm->arch.lpcr = lpcr;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001842 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001843 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001844
1845 /* Initialize phys addrs of pages in RMO */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001846 npages = ri->npages;
1847 porder = __ilog2(npages);
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001848 physp = memslot->arch.slot_phys;
1849 if (physp) {
1850 if (npages > memslot->npages)
1851 npages = memslot->npages;
1852 spin_lock(&kvm->arch.slot_phys_lock);
1853 for (i = 0; i < npages; ++i)
1854 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
1855 porder;
1856 spin_unlock(&kvm->arch.slot_phys_lock);
1857 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001858 }
1859
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001860 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
1861 smp_wmb();
1862 kvm->arch.rma_setup_done = 1;
1863 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001864 out_srcu:
1865 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001866 out:
1867 mutex_unlock(&kvm->lock);
1868 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001869
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001870 up_out:
1871 up_read(&current->mm->mmap_sem);
1872 goto out;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001873}
1874
1875int kvmppc_core_init_vm(struct kvm *kvm)
1876{
Paul Mackerras32fad282012-05-04 02:32:53 +00001877 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001878
Paul Mackerras32fad282012-05-04 02:32:53 +00001879 /* Allocate the guest's logical partition ID */
1880
1881 lpid = kvmppc_alloc_lpid();
1882 if (lpid < 0)
1883 return -ENOMEM;
1884 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001885
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001886 /*
1887 * Since we don't flush the TLB when tearing down a VM,
1888 * and this lpid might have previously been used,
1889 * make sure we flush on each core before running the new VM.
1890 */
1891 cpumask_setall(&kvm->arch.need_tlb_flush);
1892
David Gibson54738c02011-06-29 00:22:41 +00001893 INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001894 INIT_LIST_HEAD(&kvm->arch.rtas_tokens);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001895
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001896 kvm->arch.rma = NULL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001897
Paul Mackerras9e368f22011-06-29 00:40:08 +00001898 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001899
Paul Mackerras9e368f22011-06-29 00:40:08 +00001900 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1901 /* PPC970; HID4 is effectively the LPCR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001902 kvm->arch.host_lpid = 0;
1903 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
1904 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
1905 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
1906 ((lpid & 0xf) << HID4_LPID5_SH);
1907 } else {
1908 /* POWER7; init LPCR for virtual RMA mode */
1909 kvm->arch.host_lpid = mfspr(SPRN_LPID);
1910 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
1911 lpcr &= LPCR_PECE | LPCR_LPES;
1912 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
Paul Mackerras697d3892011-12-12 12:36:37 +00001913 LPCR_VPM0 | LPCR_VPM1;
1914 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
1915 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerras9e368f22011-06-29 00:40:08 +00001916 }
1917 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001918
Paul Mackerras342d3db2011-12-12 12:38:05 +00001919 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001920 spin_lock_init(&kvm->arch.slot_phys_lock);
Paul Mackerras512691d2012-10-15 01:15:41 +00001921
1922 /*
1923 * Don't allow secondary CPU threads to come online
1924 * while any KVM VMs exist.
1925 */
1926 inhibit_secondary_onlining();
1927
David Gibson54738c02011-06-29 00:22:41 +00001928 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001929}
1930
1931void kvmppc_core_destroy_vm(struct kvm *kvm)
1932{
Paul Mackerras512691d2012-10-15 01:15:41 +00001933 uninhibit_secondary_onlining();
1934
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001935 if (kvm->arch.rma) {
1936 kvm_release_rma(kvm->arch.rma);
1937 kvm->arch.rma = NULL;
1938 }
1939
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001940 kvmppc_rtas_tokens_free(kvm);
1941
Paul Mackerrasde56a942011-06-29 00:21:34 +00001942 kvmppc_free_hpt(kvm);
David Gibson54738c02011-06-29 00:22:41 +00001943 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
Paul Mackerrasde56a942011-06-29 00:21:34 +00001944}
1945
1946/* These are stubs for now */
1947void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
1948{
1949}
1950
1951/* We don't need to emulate any privileged instructions or dcbz */
1952int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
1953 unsigned int inst, int *advance)
1954{
1955 return EMULATE_FAIL;
1956}
1957
Alexander Graf54771e62012-05-04 14:55:12 +02001958int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001959{
1960 return EMULATE_FAIL;
1961}
1962
Alexander Graf54771e62012-05-04 14:55:12 +02001963int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001964{
1965 return EMULATE_FAIL;
1966}
1967
1968static int kvmppc_book3s_hv_init(void)
1969{
1970 int r;
1971
1972 r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1973
1974 if (r)
1975 return r;
1976
1977 r = kvmppc_mmu_hv_init();
1978
1979 return r;
1980}
1981
1982static void kvmppc_book3s_hv_exit(void)
1983{
1984 kvm_exit();
1985}
1986
1987module_init(kvmppc_book3s_hv_init);
1988module_exit(kvmppc_book3s_hv_exit);