blob: 82ba00f68b074392a886a9de191fd227297e97cb [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
Paul Mackerrasc7b67672012-10-15 01:18:07 +000069/*
70 * We use the vcpu_load/put functions to measure stolen time.
71 * Stolen time is counted as time when either the vcpu is able to
72 * run as part of a virtual core, but the task running the vcore
73 * is preempted or sleeping, or when the vcpu needs something done
74 * in the kernel by the task running the vcpu, but that task is
75 * preempted or sleeping. Those two things have to be counted
76 * separately, since one of the vcpu tasks will take on the job
77 * of running the core, and the other vcpu tasks in the vcore will
78 * sleep waiting for it to do that, but that sleep shouldn't count
79 * as stolen time.
80 *
81 * Hence we accumulate stolen time when the vcpu can run as part of
82 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
83 * needs its task to do other things in the kernel (for example,
84 * service a page fault) in busy_stolen. We don't accumulate
85 * stolen time for a vcore when it is inactive, or for a vcpu
86 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
87 * a misnomer; it means that the vcpu task is not executing in
88 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
89 * the kernel. We don't have any way of dividing up that time
90 * between time that the vcpu is genuinely stopped, time that
91 * the task is actively working on behalf of the vcpu, and time
92 * that the task is preempted, so we don't count any of it as
93 * stolen.
94 *
95 * Updates to busy_stolen are protected by arch.tbacct_lock;
96 * updates to vc->stolen_tb are protected by the arch.tbacct_lock
97 * of the vcpu that has taken responsibility for running the vcore
98 * (i.e. vc->runner). The stolen times are measured in units of
99 * timebase ticks. (Note that the != TB_NIL checks below are
100 * purely defensive; they should never fail.)
101 */
102
Paul Mackerrasde56a942011-06-29 00:21:34 +0000103void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
104{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000105 struct kvmppc_vcore *vc = vcpu->arch.vcore;
106
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000107 spin_lock(&vcpu->arch.tbacct_lock);
108 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
109 vc->preempt_tb != TB_NIL) {
Paul Mackerras0456ec42012-02-03 00:56:21 +0000110 vc->stolen_tb += mftb() - vc->preempt_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000111 vc->preempt_tb = TB_NIL;
112 }
113 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
114 vcpu->arch.busy_preempt != TB_NIL) {
115 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
116 vcpu->arch.busy_preempt = TB_NIL;
117 }
118 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000119}
120
121void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
122{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000123 struct kvmppc_vcore *vc = vcpu->arch.vcore;
124
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000125 spin_lock(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000126 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
127 vc->preempt_tb = mftb();
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000128 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
129 vcpu->arch.busy_preempt = mftb();
130 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000131}
132
Paul Mackerrasde56a942011-06-29 00:21:34 +0000133void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
134{
135 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000136 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000137}
138
139void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
140{
141 vcpu->arch.pvr = pvr;
142}
143
144void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
145{
146 int r;
147
148 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
149 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
150 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
151 for (r = 0; r < 16; ++r)
152 pr_err("r%2d = %.16lx r%d = %.16lx\n",
153 r, kvmppc_get_gpr(vcpu, r),
154 r+16, kvmppc_get_gpr(vcpu, r+16));
155 pr_err("ctr = %.16lx lr = %.16lx\n",
156 vcpu->arch.ctr, vcpu->arch.lr);
157 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
158 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
159 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
160 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
161 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
162 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
163 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
164 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
165 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
166 pr_err("fault dar = %.16lx dsisr = %.8x\n",
167 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
168 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
169 for (r = 0; r < vcpu->arch.slb_max; ++r)
170 pr_err(" ESID = %.16llx VSID = %.16llx\n",
171 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
172 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000173 vcpu->kvm->arch.lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000174 vcpu->arch.last_inst);
175}
176
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000177struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
178{
179 int r;
180 struct kvm_vcpu *v, *ret = NULL;
181
182 mutex_lock(&kvm->lock);
183 kvm_for_each_vcpu(r, v, kvm) {
184 if (v->vcpu_id == id) {
185 ret = v;
186 break;
187 }
188 }
189 mutex_unlock(&kvm->lock);
190 return ret;
191}
192
193static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
194{
195 vpa->shared_proc = 1;
196 vpa->yield_count = 1;
197}
198
Paul Mackerras55b665b2012-09-25 20:33:06 +0000199static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
200 unsigned long addr, unsigned long len)
201{
202 /* check address is cacheline aligned */
203 if (addr & (L1_CACHE_BYTES - 1))
204 return -EINVAL;
205 spin_lock(&vcpu->arch.vpa_update_lock);
206 if (v->next_gpa != addr || v->len != len) {
207 v->next_gpa = addr;
208 v->len = addr ? len : 0;
209 v->update_pending = 1;
210 }
211 spin_unlock(&vcpu->arch.vpa_update_lock);
212 return 0;
213}
214
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000215/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
216struct reg_vpa {
217 u32 dummy;
218 union {
219 u16 hword;
220 u32 word;
221 } length;
222};
223
224static int vpa_is_registered(struct kvmppc_vpa *vpap)
225{
226 if (vpap->update_pending)
227 return vpap->next_gpa != 0;
228 return vpap->pinned_addr != NULL;
229}
230
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000231static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
232 unsigned long flags,
233 unsigned long vcpuid, unsigned long vpa)
234{
235 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000236 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000237 void *va;
238 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000239 int err;
240 int subfunc;
241 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000242
243 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
244 if (!tvcpu)
245 return H_PARAMETER;
246
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000247 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
248 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
249 subfunc == H_VPA_REG_SLB) {
250 /* Registering new area - address must be cache-line aligned */
251 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000252 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000253
254 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000255 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
256 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000257 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000258 if (subfunc == H_VPA_REG_VPA)
259 len = ((struct reg_vpa *)va)->length.hword;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000260 else
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000261 len = ((struct reg_vpa *)va)->length.word;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000262 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000263
264 /* Check length */
265 if (len > nb || len < sizeof(struct reg_vpa))
266 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000267 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000268 vpa = 0;
269 len = 0;
270 }
271
272 err = H_PARAMETER;
273 vpap = NULL;
274 spin_lock(&tvcpu->arch.vpa_update_lock);
275
276 switch (subfunc) {
277 case H_VPA_REG_VPA: /* register VPA */
278 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000279 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000280 vpap = &tvcpu->arch.vpa;
281 err = 0;
282 break;
283
284 case H_VPA_REG_DTL: /* register DTL */
285 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000286 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000287 len -= len % sizeof(struct dtl_entry);
288
289 /* Check that they have previously registered a VPA */
290 err = H_RESOURCE;
291 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000292 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000293
294 vpap = &tvcpu->arch.dtl;
295 err = 0;
296 break;
297
298 case H_VPA_REG_SLB: /* register SLB shadow buffer */
299 /* Check that they have previously registered a VPA */
300 err = H_RESOURCE;
301 if (!vpa_is_registered(&tvcpu->arch.vpa))
302 break;
303
304 vpap = &tvcpu->arch.slb_shadow;
305 err = 0;
306 break;
307
308 case H_VPA_DEREG_VPA: /* deregister VPA */
309 /* Check they don't still have a DTL or SLB buf registered */
310 err = H_RESOURCE;
311 if (vpa_is_registered(&tvcpu->arch.dtl) ||
312 vpa_is_registered(&tvcpu->arch.slb_shadow))
313 break;
314
315 vpap = &tvcpu->arch.vpa;
316 err = 0;
317 break;
318
319 case H_VPA_DEREG_DTL: /* deregister DTL */
320 vpap = &tvcpu->arch.dtl;
321 err = 0;
322 break;
323
324 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
325 vpap = &tvcpu->arch.slb_shadow;
326 err = 0;
327 break;
328 }
329
330 if (vpap) {
331 vpap->next_gpa = vpa;
332 vpap->len = len;
333 vpap->update_pending = 1;
334 }
335
336 spin_unlock(&tvcpu->arch.vpa_update_lock);
337
338 return err;
339}
340
Paul Mackerras081f3232012-06-01 20:20:24 +1000341static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000342{
Paul Mackerras081f3232012-06-01 20:20:24 +1000343 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000344 void *va;
345 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000346 unsigned long gpa;
347
348 /*
349 * We need to pin the page pointed to by vpap->next_gpa,
350 * but we can't call kvmppc_pin_guest_page under the lock
351 * as it does get_user_pages() and down_read(). So we
352 * have to drop the lock, pin the page, then get the lock
353 * again and check that a new area didn't get registered
354 * in the meantime.
355 */
356 for (;;) {
357 gpa = vpap->next_gpa;
358 spin_unlock(&vcpu->arch.vpa_update_lock);
359 va = NULL;
360 nb = 0;
361 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000362 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000363 spin_lock(&vcpu->arch.vpa_update_lock);
364 if (gpa == vpap->next_gpa)
365 break;
366 /* sigh... unpin that one and try again */
367 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000368 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000369 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000370
371 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000372 if (va && nb < vpap->len) {
373 /*
374 * If it's now too short, it must be that userspace
375 * has changed the mappings underlying guest memory,
376 * so unregister the region.
377 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000378 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000379 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000380 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000381 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000382 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
383 vpap->dirty);
384 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000385 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000386 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000387 if (va)
388 vpap->pinned_end = va + vpap->len;
389}
Paul Mackerras93e60242011-12-12 12:28:55 +0000390
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000391static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
392{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000393 if (!(vcpu->arch.vpa.update_pending ||
394 vcpu->arch.slb_shadow.update_pending ||
395 vcpu->arch.dtl.update_pending))
396 return;
397
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000398 spin_lock(&vcpu->arch.vpa_update_lock);
399 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000400 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000401 if (vcpu->arch.vpa.pinned_addr)
402 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000403 }
404 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000405 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000406 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
407 vcpu->arch.dtl_index = 0;
408 }
409 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000410 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000411 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000412}
413
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000414/*
415 * Return the accumulated stolen time for the vcore up until `now'.
416 * The caller should hold the vcore lock.
417 */
418static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
419{
420 u64 p;
421
422 /*
423 * If we are the task running the vcore, then since we hold
424 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
425 * can't be updated, so we don't need the tbacct_lock.
426 * If the vcore is inactive, it can't become active (since we
427 * hold the vcore lock), so the vcpu load/put functions won't
428 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
429 */
430 if (vc->vcore_state != VCORE_INACTIVE &&
431 vc->runner->arch.run_task != current) {
432 spin_lock(&vc->runner->arch.tbacct_lock);
433 p = vc->stolen_tb;
434 if (vc->preempt_tb != TB_NIL)
435 p += now - vc->preempt_tb;
436 spin_unlock(&vc->runner->arch.tbacct_lock);
437 } else {
438 p = vc->stolen_tb;
439 }
440 return p;
441}
442
Paul Mackerras0456ec42012-02-03 00:56:21 +0000443static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
444 struct kvmppc_vcore *vc)
445{
446 struct dtl_entry *dt;
447 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000448 unsigned long stolen;
449 unsigned long core_stolen;
450 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000451
452 dt = vcpu->arch.dtl_ptr;
453 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000454 now = mftb();
455 core_stolen = vcore_stolen_time(vc, now);
456 stolen = core_stolen - vcpu->arch.stolen_logged;
457 vcpu->arch.stolen_logged = core_stolen;
458 spin_lock(&vcpu->arch.tbacct_lock);
459 stolen += vcpu->arch.busy_stolen;
460 vcpu->arch.busy_stolen = 0;
461 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000462 if (!dt || !vpa)
463 return;
464 memset(dt, 0, sizeof(struct dtl_entry));
465 dt->dispatch_reason = 7;
466 dt->processor_id = vc->pcpu + vcpu->arch.ptid;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000467 dt->timebase = now;
468 dt->enqueue_to_dispatch_time = stolen;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000469 dt->srr0 = kvmppc_get_pc(vcpu);
470 dt->srr1 = vcpu->arch.shregs.msr;
471 ++dt;
472 if (dt == vcpu->arch.dtl.pinned_end)
473 dt = vcpu->arch.dtl.pinned_addr;
474 vcpu->arch.dtl_ptr = dt;
475 /* order writing *dt vs. writing vpa->dtl_idx */
476 smp_wmb();
477 vpa->dtl_idx = ++vcpu->arch.dtl_index;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000478 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000479}
480
481int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
482{
483 unsigned long req = kvmppc_get_gpr(vcpu, 3);
484 unsigned long target, ret = H_SUCCESS;
485 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000486 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000487
488 switch (req) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000489 case H_ENTER:
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000490 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000491 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
492 kvmppc_get_gpr(vcpu, 5),
493 kvmppc_get_gpr(vcpu, 6),
494 kvmppc_get_gpr(vcpu, 7));
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000495 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000496 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000497 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000498 break;
499 case H_PROD:
500 target = kvmppc_get_gpr(vcpu, 4);
501 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
502 if (!tvcpu) {
503 ret = H_PARAMETER;
504 break;
505 }
506 tvcpu->arch.prodded = 1;
507 smp_mb();
508 if (vcpu->arch.ceded) {
509 if (waitqueue_active(&vcpu->wq)) {
510 wake_up_interruptible(&vcpu->wq);
511 vcpu->stat.halt_wakeup++;
512 }
513 }
514 break;
515 case H_CONFER:
516 break;
517 case H_REGISTER_VPA:
518 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
519 kvmppc_get_gpr(vcpu, 5),
520 kvmppc_get_gpr(vcpu, 6));
521 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000522 case H_RTAS:
523 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
524 return RESUME_HOST;
525
526 rc = kvmppc_rtas_hcall(vcpu);
527
528 if (rc == -ENOENT)
529 return RESUME_HOST;
530 else if (rc == 0)
531 break;
532
533 /* Send the error out to userspace via KVM_RUN */
534 return rc;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000535
536 case H_XIRR:
537 case H_CPPR:
538 case H_EOI:
539 case H_IPI:
540 if (kvmppc_xics_enabled(vcpu)) {
541 ret = kvmppc_xics_hcall(vcpu, req);
542 break;
543 } /* fallthrough */
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000544 default:
545 return RESUME_HOST;
546 }
547 kvmppc_set_gpr(vcpu, 3, ret);
548 vcpu->arch.hcall_needed = 0;
549 return RESUME_GUEST;
550}
551
Paul Mackerrasde56a942011-06-29 00:21:34 +0000552static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
553 struct task_struct *tsk)
554{
555 int r = RESUME_HOST;
556
557 vcpu->stat.sum_exits++;
558
559 run->exit_reason = KVM_EXIT_UNKNOWN;
560 run->ready_for_interrupt_injection = 1;
561 switch (vcpu->arch.trap) {
562 /* We're good on these - the host merely wanted to get our attention */
563 case BOOK3S_INTERRUPT_HV_DECREMENTER:
564 vcpu->stat.dec_exits++;
565 r = RESUME_GUEST;
566 break;
567 case BOOK3S_INTERRUPT_EXTERNAL:
568 vcpu->stat.ext_intr_exits++;
569 r = RESUME_GUEST;
570 break;
571 case BOOK3S_INTERRUPT_PERFMON:
572 r = RESUME_GUEST;
573 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000574 case BOOK3S_INTERRUPT_MACHINE_CHECK:
575 /*
576 * Deliver a machine check interrupt to the guest.
577 * We have to do this, even if the host has handled the
578 * machine check, because machine checks use SRR0/1 and
579 * the interrupt might have trashed guest state in them.
580 */
581 kvmppc_book3s_queue_irqprio(vcpu,
582 BOOK3S_INTERRUPT_MACHINE_CHECK);
583 r = RESUME_GUEST;
584 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000585 case BOOK3S_INTERRUPT_PROGRAM:
586 {
587 ulong flags;
588 /*
589 * Normally program interrupts are delivered directly
590 * to the guest by the hardware, but we can get here
591 * as a result of a hypervisor emulation interrupt
592 * (e40) getting turned into a 700 by BML RTAS.
593 */
594 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
595 kvmppc_core_queue_program(vcpu, flags);
596 r = RESUME_GUEST;
597 break;
598 }
599 case BOOK3S_INTERRUPT_SYSCALL:
600 {
601 /* hcall - punt to userspace */
602 int i;
603
604 if (vcpu->arch.shregs.msr & MSR_PR) {
605 /* sc 1 from userspace - reflect to guest syscall */
606 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
607 r = RESUME_GUEST;
608 break;
609 }
610 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
611 for (i = 0; i < 9; ++i)
612 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
613 run->exit_reason = KVM_EXIT_PAPR_HCALL;
614 vcpu->arch.hcall_needed = 1;
615 r = RESUME_HOST;
616 break;
617 }
618 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000619 * We get these next two if the guest accesses a page which it thinks
620 * it has mapped but which is not actually present, either because
621 * it is for an emulated I/O device or because the corresonding
622 * host page has been paged out. Any other HDSI/HISI interrupts
623 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000624 */
625 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000626 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000627 break;
628 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000629 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
630 vcpu->arch.fault_dsisr = 0;
631 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000632 break;
633 /*
634 * This occurs if the guest executes an illegal instruction.
635 * We just generate a program interrupt to the guest, since
636 * we don't emulate any guest instructions at this stage.
637 */
638 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
639 kvmppc_core_queue_program(vcpu, 0x80000);
640 r = RESUME_GUEST;
641 break;
642 default:
643 kvmppc_dump_regs(vcpu);
644 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
645 vcpu->arch.trap, kvmppc_get_pc(vcpu),
646 vcpu->arch.shregs.msr);
647 r = RESUME_HOST;
648 BUG();
649 break;
650 }
651
Paul Mackerrasde56a942011-06-29 00:21:34 +0000652 return r;
653}
654
655int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
656 struct kvm_sregs *sregs)
657{
658 int i;
659
660 sregs->pvr = vcpu->arch.pvr;
661
662 memset(sregs, 0, sizeof(struct kvm_sregs));
663 for (i = 0; i < vcpu->arch.slb_max; i++) {
664 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
665 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
666 }
667
668 return 0;
669}
670
671int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
672 struct kvm_sregs *sregs)
673{
674 int i, j;
675
676 kvmppc_set_pvr(vcpu, sregs->pvr);
677
678 j = 0;
679 for (i = 0; i < vcpu->arch.slb_nr; i++) {
680 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
681 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
682 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
683 ++j;
684 }
685 }
686 vcpu->arch.slb_max = j;
687
688 return 0;
689}
690
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000691int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000692{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000693 int r = 0;
694 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +0000695
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000696 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000697 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000698 *val = get_reg_val(id, 0);
699 break;
700 case KVM_REG_PPC_DABR:
701 *val = get_reg_val(id, vcpu->arch.dabr);
702 break;
703 case KVM_REG_PPC_DSCR:
704 *val = get_reg_val(id, vcpu->arch.dscr);
705 break;
706 case KVM_REG_PPC_PURR:
707 *val = get_reg_val(id, vcpu->arch.purr);
708 break;
709 case KVM_REG_PPC_SPURR:
710 *val = get_reg_val(id, vcpu->arch.spurr);
711 break;
712 case KVM_REG_PPC_AMR:
713 *val = get_reg_val(id, vcpu->arch.amr);
714 break;
715 case KVM_REG_PPC_UAMOR:
716 *val = get_reg_val(id, vcpu->arch.uamor);
717 break;
718 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
719 i = id - KVM_REG_PPC_MMCR0;
720 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
721 break;
722 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
723 i = id - KVM_REG_PPC_PMC1;
724 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +0000725 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000726#ifdef CONFIG_VSX
727 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
728 if (cpu_has_feature(CPU_FTR_VSX)) {
729 /* VSX => FP reg i is stored in arch.vsr[2*i] */
730 long int i = id - KVM_REG_PPC_FPR0;
731 *val = get_reg_val(id, vcpu->arch.vsr[2 * i]);
732 } else {
733 /* let generic code handle it */
734 r = -EINVAL;
735 }
736 break;
737 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
738 if (cpu_has_feature(CPU_FTR_VSX)) {
739 long int i = id - KVM_REG_PPC_VSR0;
740 val->vsxval[0] = vcpu->arch.vsr[2 * i];
741 val->vsxval[1] = vcpu->arch.vsr[2 * i + 1];
742 } else {
743 r = -ENXIO;
744 }
745 break;
746#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000747 case KVM_REG_PPC_VPA_ADDR:
748 spin_lock(&vcpu->arch.vpa_update_lock);
749 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
750 spin_unlock(&vcpu->arch.vpa_update_lock);
751 break;
752 case KVM_REG_PPC_VPA_SLB:
753 spin_lock(&vcpu->arch.vpa_update_lock);
754 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
755 val->vpaval.length = vcpu->arch.slb_shadow.len;
756 spin_unlock(&vcpu->arch.vpa_update_lock);
757 break;
758 case KVM_REG_PPC_VPA_DTL:
759 spin_lock(&vcpu->arch.vpa_update_lock);
760 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
761 val->vpaval.length = vcpu->arch.dtl.len;
762 spin_unlock(&vcpu->arch.vpa_update_lock);
763 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000764 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000765 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000766 break;
767 }
768
769 return r;
770}
771
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000772int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000773{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000774 int r = 0;
775 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +0000776 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +0000777
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000778 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000779 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +0000780 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000781 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +0000782 r = -EINVAL;
783 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000784 case KVM_REG_PPC_DABR:
785 vcpu->arch.dabr = set_reg_val(id, *val);
786 break;
787 case KVM_REG_PPC_DSCR:
788 vcpu->arch.dscr = set_reg_val(id, *val);
789 break;
790 case KVM_REG_PPC_PURR:
791 vcpu->arch.purr = set_reg_val(id, *val);
792 break;
793 case KVM_REG_PPC_SPURR:
794 vcpu->arch.spurr = set_reg_val(id, *val);
795 break;
796 case KVM_REG_PPC_AMR:
797 vcpu->arch.amr = set_reg_val(id, *val);
798 break;
799 case KVM_REG_PPC_UAMOR:
800 vcpu->arch.uamor = set_reg_val(id, *val);
801 break;
802 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
803 i = id - KVM_REG_PPC_MMCR0;
804 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
805 break;
806 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
807 i = id - KVM_REG_PPC_PMC1;
808 vcpu->arch.pmc[i] = set_reg_val(id, *val);
809 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000810#ifdef CONFIG_VSX
811 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
812 if (cpu_has_feature(CPU_FTR_VSX)) {
813 /* VSX => FP reg i is stored in arch.vsr[2*i] */
814 long int i = id - KVM_REG_PPC_FPR0;
815 vcpu->arch.vsr[2 * i] = set_reg_val(id, *val);
816 } else {
817 /* let generic code handle it */
818 r = -EINVAL;
819 }
820 break;
821 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
822 if (cpu_has_feature(CPU_FTR_VSX)) {
823 long int i = id - KVM_REG_PPC_VSR0;
824 vcpu->arch.vsr[2 * i] = val->vsxval[0];
825 vcpu->arch.vsr[2 * i + 1] = val->vsxval[1];
826 } else {
827 r = -ENXIO;
828 }
829 break;
830#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000831 case KVM_REG_PPC_VPA_ADDR:
832 addr = set_reg_val(id, *val);
833 r = -EINVAL;
834 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
835 vcpu->arch.dtl.next_gpa))
836 break;
837 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
838 break;
839 case KVM_REG_PPC_VPA_SLB:
840 addr = val->vpaval.addr;
841 len = val->vpaval.length;
842 r = -EINVAL;
843 if (addr && !vcpu->arch.vpa.next_gpa)
844 break;
845 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
846 break;
847 case KVM_REG_PPC_VPA_DTL:
848 addr = val->vpaval.addr;
849 len = val->vpaval.length;
850 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +0000851 if (addr && (len < sizeof(struct dtl_entry) ||
852 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +0000853 break;
854 len -= len % sizeof(struct dtl_entry);
855 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
856 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000857 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000858 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000859 break;
860 }
861
862 return r;
863}
864
Paul Mackerrasde56a942011-06-29 00:21:34 +0000865int kvmppc_core_check_processor_compat(void)
866{
Paul Mackerras9e368f22011-06-29 00:40:08 +0000867 if (cpu_has_feature(CPU_FTR_HVMODE))
Paul Mackerrasde56a942011-06-29 00:21:34 +0000868 return 0;
869 return -EIO;
870}
871
872struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
873{
874 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000875 int err = -EINVAL;
876 int core;
877 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000878
Paul Mackerras371fefd2011-06-29 00:23:08 +0000879 core = id / threads_per_core;
880 if (core >= KVM_MAX_VCORES)
881 goto out;
882
883 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200884 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000885 if (!vcpu)
886 goto out;
887
888 err = kvm_vcpu_init(vcpu, kvm, id);
889 if (err)
890 goto free_vcpu;
891
892 vcpu->arch.shared = &vcpu->arch.shregs;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000893 vcpu->arch.mmcr[0] = MMCR0_FC;
894 vcpu->arch.ctrl = CTRL_RUNLATCH;
895 /* default to host PVR, since we can't spoof it */
896 vcpu->arch.pvr = mfspr(SPRN_PVR);
897 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000898 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000899 spin_lock_init(&vcpu->arch.tbacct_lock);
900 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000901
Paul Mackerrasde56a942011-06-29 00:21:34 +0000902 kvmppc_mmu_book3s_hv_init(vcpu);
903
Paul Mackerras8455d792012-10-15 01:17:42 +0000904 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000905
906 init_waitqueue_head(&vcpu->arch.cpu_run);
907
908 mutex_lock(&kvm->lock);
909 vcore = kvm->arch.vcores[core];
910 if (!vcore) {
911 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
912 if (vcore) {
913 INIT_LIST_HEAD(&vcore->runnable_threads);
914 spin_lock_init(&vcore->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000915 init_waitqueue_head(&vcore->wq);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000916 vcore->preempt_tb = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000917 }
918 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000919 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000920 }
921 mutex_unlock(&kvm->lock);
922
923 if (!vcore)
924 goto free_vcpu;
925
926 spin_lock(&vcore->lock);
927 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000928 spin_unlock(&vcore->lock);
929 vcpu->arch.vcore = vcore;
930
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200931 vcpu->arch.cpu_type = KVM_CPU_3S_64;
932 kvmppc_sanity_check(vcpu);
933
Paul Mackerrasde56a942011-06-29 00:21:34 +0000934 return vcpu;
935
936free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200937 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000938out:
939 return ERR_PTR(err);
940}
941
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000942static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
943{
944 if (vpa->pinned_addr)
945 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
946 vpa->dirty);
947}
948
Paul Mackerrasde56a942011-06-29 00:21:34 +0000949void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
950{
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000951 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000952 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
953 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
954 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000955 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000956 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200957 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000958}
959
Paul Mackerras19ccb762011-07-23 17:42:46 +1000960static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000961{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000962 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000963
Paul Mackerras19ccb762011-07-23 17:42:46 +1000964 now = get_tb();
965 if (now > vcpu->arch.dec_expires) {
966 /* decrementer has already gone negative */
967 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -0600968 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000969 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000970 }
Paul Mackerras19ccb762011-07-23 17:42:46 +1000971 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
972 / tb_ticks_per_sec;
973 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
974 HRTIMER_MODE_REL);
975 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000976}
977
Paul Mackerras19ccb762011-07-23 17:42:46 +1000978static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000979{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000980 vcpu->arch.ceded = 0;
981 if (vcpu->arch.timer_running) {
982 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
983 vcpu->arch.timer_running = 0;
984 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000985}
986
987extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
988extern void xics_wake_cpu(int cpu);
989
990static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
991 struct kvm_vcpu *vcpu)
992{
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000993 u64 now;
994
Paul Mackerras371fefd2011-06-29 00:23:08 +0000995 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
996 return;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000997 spin_lock(&vcpu->arch.tbacct_lock);
998 now = mftb();
999 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1000 vcpu->arch.stolen_logged;
1001 vcpu->arch.busy_preempt = now;
1002 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1003 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001004 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001005 list_del(&vcpu->arch.run_list);
1006}
1007
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001008static int kvmppc_grab_hwthread(int cpu)
1009{
1010 struct paca_struct *tpaca;
1011 long timeout = 1000;
1012
1013 tpaca = &paca[cpu];
1014
1015 /* Ensure the thread won't go into the kernel if it wakes */
1016 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001017 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001018
1019 /*
1020 * If the thread is already executing in the kernel (e.g. handling
1021 * a stray interrupt), wait for it to get back to nap mode.
1022 * The smp_mb() is to ensure that our setting of hwthread_req
1023 * is visible before we look at hwthread_state, so if this
1024 * races with the code at system_reset_pSeries and the thread
1025 * misses our setting of hwthread_req, we are sure to see its
1026 * setting of hwthread_state, and vice versa.
1027 */
1028 smp_mb();
1029 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1030 if (--timeout <= 0) {
1031 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1032 return -EBUSY;
1033 }
1034 udelay(1);
1035 }
1036 return 0;
1037}
1038
1039static void kvmppc_release_hwthread(int cpu)
1040{
1041 struct paca_struct *tpaca;
1042
1043 tpaca = &paca[cpu];
1044 tpaca->kvm_hstate.hwthread_req = 0;
1045 tpaca->kvm_hstate.kvm_vcpu = NULL;
1046}
1047
Paul Mackerras371fefd2011-06-29 00:23:08 +00001048static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1049{
1050 int cpu;
1051 struct paca_struct *tpaca;
1052 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1053
Paul Mackerras19ccb762011-07-23 17:42:46 +10001054 if (vcpu->arch.timer_running) {
1055 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1056 vcpu->arch.timer_running = 0;
1057 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001058 cpu = vc->pcpu + vcpu->arch.ptid;
1059 tpaca = &paca[cpu];
1060 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1061 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001062 tpaca->kvm_hstate.napping = 0;
1063 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001064 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +00001065#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001066 if (vcpu->arch.ptid) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001067 xics_wake_cpu(cpu);
1068 ++vc->n_woken;
1069 }
1070#endif
1071}
1072
1073static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1074{
1075 int i;
1076
1077 HMT_low();
1078 i = 0;
1079 while (vc->nap_count < vc->n_woken) {
1080 if (++i >= 1000000) {
1081 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1082 vc->nap_count, vc->n_woken);
1083 break;
1084 }
1085 cpu_relax();
1086 }
1087 HMT_medium();
1088}
1089
1090/*
1091 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001092 * this core are off-line. Then grab the threads so they can't
1093 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001094 */
1095static int on_primary_thread(void)
1096{
1097 int cpu = smp_processor_id();
1098 int thr = cpu_thread_in_core(cpu);
1099
1100 if (thr)
1101 return 0;
1102 while (++thr < threads_per_core)
1103 if (cpu_online(cpu + thr))
1104 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001105
1106 /* Grab all hw threads so they can't go into the kernel */
1107 for (thr = 1; thr < threads_per_core; ++thr) {
1108 if (kvmppc_grab_hwthread(cpu + thr)) {
1109 /* Couldn't grab one; let the others go */
1110 do {
1111 kvmppc_release_hwthread(cpu + thr);
1112 } while (--thr > 0);
1113 return 0;
1114 }
1115 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001116 return 1;
1117}
1118
1119/*
1120 * Run a set of guest threads on a physical core.
1121 * Called with vc->lock held.
1122 */
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001123static void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001124{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001125 struct kvm_vcpu *vcpu, *vcpu0, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001126 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001127 u64 now;
Paul Mackerras081f3232012-06-01 20:20:24 +10001128 int ptid, i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001129 int srcu_idx;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001130 struct kvm_vcpu *vcpus_to_update[threads_per_core];
Paul Mackerrasde56a942011-06-29 00:21:34 +00001131
Paul Mackerras371fefd2011-06-29 00:23:08 +00001132 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +10001133 need_vpa_update = 0;
1134 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001135 if (signal_pending(vcpu->arch.run_task))
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001136 return;
1137 if (vcpu->arch.vpa.update_pending ||
1138 vcpu->arch.slb_shadow.update_pending ||
1139 vcpu->arch.dtl.update_pending)
1140 vcpus_to_update[need_vpa_update++] = vcpu;
Paul Mackerras081f3232012-06-01 20:20:24 +10001141 }
1142
1143 /*
1144 * Initialize *vc, in particular vc->vcore_state, so we can
1145 * drop the vcore lock if necessary.
1146 */
1147 vc->n_woken = 0;
1148 vc->nap_count = 0;
1149 vc->entry_exit_count = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001150 vc->vcore_state = VCORE_STARTING;
Paul Mackerras081f3232012-06-01 20:20:24 +10001151 vc->in_guest = 0;
1152 vc->napping_threads = 0;
1153
1154 /*
1155 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1156 * which can't be called with any spinlocks held.
1157 */
1158 if (need_vpa_update) {
1159 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001160 for (i = 0; i < need_vpa_update; ++i)
1161 kvmppc_update_vpas(vcpus_to_update[i]);
Paul Mackerras081f3232012-06-01 20:20:24 +10001162 spin_lock(&vc->lock);
1163 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001164
1165 /*
Paul Mackerras19ccb762011-07-23 17:42:46 +10001166 * Assign physical thread IDs, first to non-ceded vcpus
1167 * and then to ceded ones.
1168 */
1169 ptid = 0;
1170 vcpu0 = NULL;
1171 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1172 if (!vcpu->arch.ceded) {
1173 if (!ptid)
1174 vcpu0 = vcpu;
1175 vcpu->arch.ptid = ptid++;
1176 }
1177 }
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001178 if (!vcpu0)
1179 goto out; /* nothing to run; should never happen */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001180 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1181 if (vcpu->arch.ceded)
1182 vcpu->arch.ptid = ptid++;
1183
Paul Mackerras7b444c62012-10-15 01:16:14 +00001184 /*
1185 * Make sure we are running on thread 0, and that
1186 * secondary threads are offline.
1187 */
1188 if (threads_per_core > 1 && !on_primary_thread()) {
1189 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1190 vcpu->arch.ret = -EBUSY;
1191 goto out;
1192 }
1193
Paul Mackerras371fefd2011-06-29 00:23:08 +00001194 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001195 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001196 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001197 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001198 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001199
Paul Mackerras2f12f032012-10-15 01:17:17 +00001200 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001201 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001202 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001203
Paul Mackerras19ccb762011-07-23 17:42:46 +10001204 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001205
1206 srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
1207
Paul Mackerras19ccb762011-07-23 17:42:46 +10001208 __kvmppc_vcore_entry(NULL, vcpu0);
1209
Paul Mackerras371fefd2011-06-29 00:23:08 +00001210 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001211 /* disable sending of IPIs on virtual external irqs */
1212 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1213 vcpu->cpu = -1;
1214 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001215 if (vc->nap_count < vc->n_woken)
1216 kvmppc_wait_for_nap(vc);
Paul Mackerras2f12f032012-10-15 01:17:17 +00001217 for (i = 0; i < threads_per_core; ++i)
1218 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001219 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001220 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001221 spin_unlock(&vc->lock);
1222
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001223 srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
1224
Paul Mackerras371fefd2011-06-29 00:23:08 +00001225 /* make sure updates to secondary vcpu structs are visible now */
1226 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001227 kvm_guest_exit();
1228
1229 preempt_enable();
1230 kvm_resched(vcpu);
1231
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001232 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001233 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001234 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1235 /* cancel pending dec exception if dec is positive */
1236 if (now < vcpu->arch.dec_expires &&
1237 kvmppc_core_pending_dec(vcpu))
1238 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001239
1240 ret = RESUME_GUEST;
1241 if (vcpu->arch.trap)
1242 ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
1243 vcpu->arch.run_task);
1244
Paul Mackerras371fefd2011-06-29 00:23:08 +00001245 vcpu->arch.ret = ret;
1246 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001247
1248 if (vcpu->arch.ceded) {
1249 if (ret != RESUME_GUEST)
1250 kvmppc_end_cede(vcpu);
1251 else
1252 kvmppc_set_timer(vcpu);
1253 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001254 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001255
Paul Mackerrasde56a942011-06-29 00:21:34 +00001256 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001257 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001258 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1259 arch.run_list) {
1260 if (vcpu->arch.ret != RESUME_GUEST) {
1261 kvmppc_remove_runnable(vc, vcpu);
1262 wake_up(&vcpu->arch.cpu_run);
1263 }
1264 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001265}
1266
Paul Mackerras19ccb762011-07-23 17:42:46 +10001267/*
1268 * Wait for some other vcpu thread to execute us, and
1269 * wake us up when we need to handle something in the host.
1270 */
1271static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001272{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001273 DEFINE_WAIT(wait);
1274
Paul Mackerras19ccb762011-07-23 17:42:46 +10001275 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1276 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1277 schedule();
1278 finish_wait(&vcpu->arch.cpu_run, &wait);
1279}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001280
Paul Mackerras19ccb762011-07-23 17:42:46 +10001281/*
1282 * All the vcpus in this vcore are idle, so wait for a decrementer
1283 * or external interrupt to one of the vcpus. vc->lock is held.
1284 */
1285static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1286{
1287 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001288
1289 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1290 vc->vcore_state = VCORE_SLEEPING;
1291 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001292 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001293 finish_wait(&vc->wq, &wait);
1294 spin_lock(&vc->lock);
1295 vc->vcore_state = VCORE_INACTIVE;
1296}
1297
1298static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1299{
1300 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001301 struct kvmppc_vcore *vc;
1302 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001303
Paul Mackerras371fefd2011-06-29 00:23:08 +00001304 kvm_run->exit_reason = 0;
1305 vcpu->arch.ret = RESUME_GUEST;
1306 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001307 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001308
Paul Mackerras371fefd2011-06-29 00:23:08 +00001309 /*
1310 * Synchronize with other threads in this virtual core
1311 */
1312 vc = vcpu->arch.vcore;
1313 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001314 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001315 vcpu->arch.run_task = current;
1316 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001317 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10001318 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001319 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001320 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1321 ++vc->n_runnable;
1322
Paul Mackerras19ccb762011-07-23 17:42:46 +10001323 /*
1324 * This happens the first time this is called for a vcpu.
1325 * If the vcore is already running, we may be able to start
1326 * this thread straight away and have it join in.
1327 */
Paul Mackerras8455d792012-10-15 01:17:42 +00001328 if (!signal_pending(current)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001329 if (vc->vcore_state == VCORE_RUNNING &&
1330 VCORE_EXIT_COUNT(vc) == 0) {
1331 vcpu->arch.ptid = vc->n_runnable - 1;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001332 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001333 kvmppc_start_thread(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00001334 } else if (vc->vcore_state == VCORE_SLEEPING) {
1335 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001336 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001337
Paul Mackerras8455d792012-10-15 01:17:42 +00001338 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001339
1340 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1341 !signal_pending(current)) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001342 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001343 spin_unlock(&vc->lock);
1344 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1345 spin_lock(&vc->lock);
1346 continue;
1347 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001348 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1349 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001350 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001351 if (signal_pending(v->arch.run_task)) {
1352 kvmppc_remove_runnable(vc, v);
1353 v->stat.signal_exits++;
1354 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1355 v->arch.ret = -EINTR;
1356 wake_up(&v->arch.cpu_run);
1357 }
1358 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001359 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1360 break;
1361 vc->runner = vcpu;
1362 n_ceded = 0;
1363 list_for_each_entry(v, &vc->runnable_threads, arch.run_list)
1364 if (!v->arch.pending_exceptions)
1365 n_ceded += v->arch.ceded;
1366 if (n_ceded == vc->n_runnable)
1367 kvmppc_vcore_blocked(vc);
1368 else
1369 kvmppc_run_core(vc);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001370 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001371 }
1372
Paul Mackerras8455d792012-10-15 01:17:42 +00001373 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1374 (vc->vcore_state == VCORE_RUNNING ||
1375 vc->vcore_state == VCORE_EXITING)) {
1376 spin_unlock(&vc->lock);
1377 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1378 spin_lock(&vc->lock);
1379 }
1380
1381 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1382 kvmppc_remove_runnable(vc, vcpu);
1383 vcpu->stat.signal_exits++;
1384 kvm_run->exit_reason = KVM_EXIT_INTR;
1385 vcpu->arch.ret = -EINTR;
1386 }
1387
1388 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1389 /* Wake up some vcpu to run the core */
1390 v = list_first_entry(&vc->runnable_threads,
1391 struct kvm_vcpu, arch.run_list);
1392 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001393 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001394
Paul Mackerras19ccb762011-07-23 17:42:46 +10001395 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001396 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001397}
1398
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001399int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
1400{
1401 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001402 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001403
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001404 if (!vcpu->arch.sane) {
1405 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1406 return -EINVAL;
1407 }
1408
Scott Wood25051b52011-11-08 18:23:23 -06001409 kvmppc_core_prepare_to_enter(vcpu);
1410
Paul Mackerras19ccb762011-07-23 17:42:46 +10001411 /* No need to go into the guest when all we'll do is come back out */
1412 if (signal_pending(current)) {
1413 run->exit_reason = KVM_EXIT_INTR;
1414 return -EINTR;
1415 }
1416
Paul Mackerras32fad282012-05-04 02:32:53 +00001417 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1418 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1419 smp_mb();
1420
1421 /* On the first time here, set up HTAB and VRMA or RMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001422 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001423 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001424 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001425 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001426 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001427
1428 flush_fp_to_thread(current);
1429 flush_altivec_to_thread(current);
1430 flush_vsx_to_thread(current);
1431 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001432 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001433 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001434
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001435 do {
1436 r = kvmppc_run_vcpu(run, vcpu);
1437
1438 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1439 !(vcpu->arch.shregs.msr & MSR_PR)) {
1440 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001441 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001442 } else if (r == RESUME_PAGE_FAULT) {
1443 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1444 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1445 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1446 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001447 }
1448 } while (r == RESUME_GUEST);
Paul Mackerras32fad282012-05-04 02:32:53 +00001449
1450 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001451 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00001452 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001453 return r;
1454}
1455
David Gibson54738c02011-06-29 00:22:41 +00001456
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001457/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +00001458 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001459static inline int lpcr_rmls(unsigned long rma_size)
1460{
1461 switch (rma_size) {
1462 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001463 if (cpu_has_feature(CPU_FTR_ARCH_206))
1464 return 8; /* only supported on POWER7 */
1465 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001466 case 64ul << 20: /* 64 MB */
1467 return 3;
1468 case 128ul << 20: /* 128 MB */
1469 return 7;
1470 case 256ul << 20: /* 256 MB */
1471 return 4;
1472 case 1ul << 30: /* 1 GB */
1473 return 2;
1474 case 16ul << 30: /* 16 GB */
1475 return 1;
1476 case 256ul << 30: /* 256 GB */
1477 return 0;
1478 default:
1479 return -1;
1480 }
1481}
1482
1483static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1484{
Alexander Grafb4e70612012-01-16 16:50:10 +01001485 struct kvmppc_linear_info *ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001486 struct page *page;
1487
1488 if (vmf->pgoff >= ri->npages)
1489 return VM_FAULT_SIGBUS;
1490
1491 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1492 get_page(page);
1493 vmf->page = page;
1494 return 0;
1495}
1496
1497static const struct vm_operations_struct kvm_rma_vm_ops = {
1498 .fault = kvm_rma_fault,
1499};
1500
1501static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1502{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001503 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001504 vma->vm_ops = &kvm_rma_vm_ops;
1505 return 0;
1506}
1507
1508static int kvm_rma_release(struct inode *inode, struct file *filp)
1509{
Alexander Grafb4e70612012-01-16 16:50:10 +01001510 struct kvmppc_linear_info *ri = filp->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001511
1512 kvm_release_rma(ri);
1513 return 0;
1514}
1515
1516static struct file_operations kvm_rma_fops = {
1517 .mmap = kvm_rma_mmap,
1518 .release = kvm_rma_release,
1519};
1520
1521long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
1522{
Alexander Grafb4e70612012-01-16 16:50:10 +01001523 struct kvmppc_linear_info *ri;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001524 long fd;
1525
1526 ri = kvm_alloc_rma();
1527 if (!ri)
1528 return -ENOMEM;
1529
1530 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR);
1531 if (fd < 0)
1532 kvm_release_rma(ri);
1533
1534 ret->rma_size = ri->npages << PAGE_SHIFT;
1535 return fd;
1536}
1537
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001538static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1539 int linux_psize)
1540{
1541 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1542
1543 if (!def->shift)
1544 return;
1545 (*sps)->page_shift = def->shift;
1546 (*sps)->slb_enc = def->sllp;
1547 (*sps)->enc[0].page_shift = def->shift;
1548 (*sps)->enc[0].pte_enc = def->penc;
1549 (*sps)++;
1550}
1551
1552int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
1553{
1554 struct kvm_ppc_one_seg_page_size *sps;
1555
1556 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1557 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1558 info->flags |= KVM_PPC_1T_SEGMENTS;
1559 info->slb_size = mmu_slb_size;
1560
1561 /* We only support these sizes for now, and no muti-size segments */
1562 sps = &info->sps[0];
1563 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1564 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1565 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1566
1567 return 0;
1568}
1569
Paul Mackerras82ed3612011-12-15 02:03:22 +00001570/*
1571 * Get (and clear) the dirty memory log for a memory slot.
1572 */
1573int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1574{
1575 struct kvm_memory_slot *memslot;
1576 int r;
1577 unsigned long n;
1578
1579 mutex_lock(&kvm->slots_lock);
1580
1581 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07001582 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00001583 goto out;
1584
1585 memslot = id_to_memslot(kvm->memslots, log->slot);
1586 r = -ENOENT;
1587 if (!memslot->dirty_bitmap)
1588 goto out;
1589
1590 n = kvm_dirty_bitmap_bytes(memslot);
1591 memset(memslot->dirty_bitmap, 0, n);
1592
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001593 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00001594 if (r)
1595 goto out;
1596
1597 r = -EFAULT;
1598 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1599 goto out;
1600
1601 r = 0;
1602out:
1603 mutex_unlock(&kvm->slots_lock);
1604 return r;
1605}
1606
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001607static void unpin_slot(struct kvm_memory_slot *memslot)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001608{
1609 unsigned long *physp;
1610 unsigned long j, npages, pfn;
1611 struct page *page;
1612
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001613 physp = memslot->arch.slot_phys;
1614 npages = memslot->npages;
1615 if (!physp)
1616 return;
1617 for (j = 0; j < npages; j++) {
1618 if (!(physp[j] & KVMPPC_GOT_PAGE))
1619 continue;
1620 pfn = physp[j] >> PAGE_SHIFT;
1621 page = pfn_to_page(pfn);
1622 SetPageDirty(page);
1623 put_page(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001624 }
1625}
1626
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001627void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1628 struct kvm_memory_slot *dont)
1629{
1630 if (!dont || free->arch.rmap != dont->arch.rmap) {
1631 vfree(free->arch.rmap);
1632 free->arch.rmap = NULL;
1633 }
1634 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1635 unpin_slot(free);
1636 vfree(free->arch.slot_phys);
1637 free->arch.slot_phys = NULL;
1638 }
1639}
1640
1641int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1642 unsigned long npages)
1643{
1644 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1645 if (!slot->arch.rmap)
1646 return -ENOMEM;
1647 slot->arch.slot_phys = NULL;
1648
1649 return 0;
1650}
1651
1652int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1653 struct kvm_memory_slot *memslot,
1654 struct kvm_userspace_memory_region *mem)
1655{
1656 unsigned long *phys;
1657
1658 /* Allocate a slot_phys array if needed */
1659 phys = memslot->arch.slot_phys;
1660 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1661 phys = vzalloc(memslot->npages * sizeof(unsigned long));
1662 if (!phys)
1663 return -ENOMEM;
1664 memslot->arch.slot_phys = phys;
1665 }
1666
1667 return 0;
1668}
1669
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001670void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001671 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001672 const struct kvm_memory_slot *old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001673{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001674 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1675 struct kvm_memory_slot *memslot;
1676
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001677 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001678 /*
1679 * If modifying a memslot, reset all the rmap dirty bits.
1680 * If this is a new memslot, we don't need to do anything
1681 * since the rmap array starts out as all zeroes,
1682 * i.e. no pages are dirty.
1683 */
1684 memslot = id_to_memslot(kvm->memslots, mem->slot);
1685 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1686 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001687}
1688
Paul Mackerras32fad282012-05-04 02:32:53 +00001689static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001690{
1691 int err = 0;
1692 struct kvm *kvm = vcpu->kvm;
Alexander Grafb4e70612012-01-16 16:50:10 +01001693 struct kvmppc_linear_info *ri = NULL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001694 unsigned long hva;
1695 struct kvm_memory_slot *memslot;
1696 struct vm_area_struct *vma;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001697 unsigned long lpcr, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001698 unsigned long psize, porder;
1699 unsigned long rma_size;
1700 unsigned long rmls;
1701 unsigned long *physp;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001702 unsigned long i, npages;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001703 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001704
1705 mutex_lock(&kvm->lock);
1706 if (kvm->arch.rma_setup_done)
1707 goto out; /* another vcpu beat us to it */
1708
Paul Mackerras32fad282012-05-04 02:32:53 +00001709 /* Allocate hashed page table (if not done already) and reset it */
1710 if (!kvm->arch.hpt_virt) {
1711 err = kvmppc_alloc_hpt(kvm, NULL);
1712 if (err) {
1713 pr_err("KVM: Couldn't alloc HPT\n");
1714 goto out;
1715 }
1716 }
1717
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001718 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001719 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001720 memslot = gfn_to_memslot(kvm, 0);
1721
1722 /* We must have some memory at 0 by now */
1723 err = -EINVAL;
1724 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001725 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001726
1727 /* Look up the VMA for the start of this memory slot */
1728 hva = memslot->userspace_addr;
1729 down_read(&current->mm->mmap_sem);
1730 vma = find_vma(current->mm, hva);
1731 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
1732 goto up_out;
1733
1734 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001735 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001736
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001737 /* Is this one of our preallocated RMAs? */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001738 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
1739 hva == vma->vm_start)
1740 ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001741
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001742 up_read(&current->mm->mmap_sem);
1743
1744 if (!ri) {
1745 /* On POWER7, use VRMA; on PPC970, give up */
1746 err = -EPERM;
1747 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1748 pr_err("KVM: CPU requires an RMO\n");
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001749 goto out_srcu;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001750 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001751
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001752 /* We can handle 4k, 64k or 16M pages in the VRMA */
1753 err = -EINVAL;
1754 if (!(psize == 0x1000 || psize == 0x10000 ||
1755 psize == 0x1000000))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001756 goto out_srcu;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001757
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001758 /* Update VRMASD field in the LPCR */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001759 senc = slb_pgsize_encoding(psize);
Paul Mackerras697d3892011-12-12 12:36:37 +00001760 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1761 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001762 lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
1763 lpcr |= senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001764 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001765
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001766 /* Create HPTEs in the hash page table for the VRMA */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001767 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001768
1769 } else {
1770 /* Set up to use an RMO region */
1771 rma_size = ri->npages;
1772 if (rma_size > memslot->npages)
1773 rma_size = memslot->npages;
1774 rma_size <<= PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001775 rmls = lpcr_rmls(rma_size);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001776 err = -EINVAL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001777 if (rmls < 0) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001778 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001779 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001780 }
1781 atomic_inc(&ri->use_count);
1782 kvm->arch.rma = ri;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001783
1784 /* Update LPCR and RMOR */
1785 lpcr = kvm->arch.lpcr;
1786 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1787 /* PPC970; insert RMLS value (split field) in HID4 */
1788 lpcr &= ~((1ul << HID4_RMLS0_SH) |
1789 (3ul << HID4_RMLS2_SH));
1790 lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
1791 ((rmls & 3) << HID4_RMLS2_SH);
1792 /* RMOR is also in HID4 */
1793 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
1794 << HID4_RMOR_SH;
1795 } else {
1796 /* POWER7 */
1797 lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
1798 lpcr |= rmls << LPCR_RMLS_SH;
1799 kvm->arch.rmor = kvm->arch.rma->base_pfn << PAGE_SHIFT;
1800 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001801 kvm->arch.lpcr = lpcr;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001802 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001803 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001804
1805 /* Initialize phys addrs of pages in RMO */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001806 npages = ri->npages;
1807 porder = __ilog2(npages);
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001808 physp = memslot->arch.slot_phys;
1809 if (physp) {
1810 if (npages > memslot->npages)
1811 npages = memslot->npages;
1812 spin_lock(&kvm->arch.slot_phys_lock);
1813 for (i = 0; i < npages; ++i)
1814 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
1815 porder;
1816 spin_unlock(&kvm->arch.slot_phys_lock);
1817 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001818 }
1819
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001820 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
1821 smp_wmb();
1822 kvm->arch.rma_setup_done = 1;
1823 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001824 out_srcu:
1825 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001826 out:
1827 mutex_unlock(&kvm->lock);
1828 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001829
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001830 up_out:
1831 up_read(&current->mm->mmap_sem);
1832 goto out;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001833}
1834
1835int kvmppc_core_init_vm(struct kvm *kvm)
1836{
Paul Mackerras32fad282012-05-04 02:32:53 +00001837 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001838
Paul Mackerras32fad282012-05-04 02:32:53 +00001839 /* Allocate the guest's logical partition ID */
1840
1841 lpid = kvmppc_alloc_lpid();
1842 if (lpid < 0)
1843 return -ENOMEM;
1844 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001845
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001846 /*
1847 * Since we don't flush the TLB when tearing down a VM,
1848 * and this lpid might have previously been used,
1849 * make sure we flush on each core before running the new VM.
1850 */
1851 cpumask_setall(&kvm->arch.need_tlb_flush);
1852
David Gibson54738c02011-06-29 00:22:41 +00001853 INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001854 INIT_LIST_HEAD(&kvm->arch.rtas_tokens);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001855
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001856 kvm->arch.rma = NULL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001857
Paul Mackerras9e368f22011-06-29 00:40:08 +00001858 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001859
Paul Mackerras9e368f22011-06-29 00:40:08 +00001860 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1861 /* PPC970; HID4 is effectively the LPCR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001862 kvm->arch.host_lpid = 0;
1863 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
1864 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
1865 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
1866 ((lpid & 0xf) << HID4_LPID5_SH);
1867 } else {
1868 /* POWER7; init LPCR for virtual RMA mode */
1869 kvm->arch.host_lpid = mfspr(SPRN_LPID);
1870 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
1871 lpcr &= LPCR_PECE | LPCR_LPES;
1872 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
Paul Mackerras697d3892011-12-12 12:36:37 +00001873 LPCR_VPM0 | LPCR_VPM1;
1874 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
1875 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerras9e368f22011-06-29 00:40:08 +00001876 }
1877 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001878
Paul Mackerras342d3db2011-12-12 12:38:05 +00001879 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001880 spin_lock_init(&kvm->arch.slot_phys_lock);
Paul Mackerras512691d2012-10-15 01:15:41 +00001881
1882 /*
1883 * Don't allow secondary CPU threads to come online
1884 * while any KVM VMs exist.
1885 */
1886 inhibit_secondary_onlining();
1887
David Gibson54738c02011-06-29 00:22:41 +00001888 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001889}
1890
1891void kvmppc_core_destroy_vm(struct kvm *kvm)
1892{
Paul Mackerras512691d2012-10-15 01:15:41 +00001893 uninhibit_secondary_onlining();
1894
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001895 if (kvm->arch.rma) {
1896 kvm_release_rma(kvm->arch.rma);
1897 kvm->arch.rma = NULL;
1898 }
1899
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001900 kvmppc_rtas_tokens_free(kvm);
1901
Paul Mackerrasde56a942011-06-29 00:21:34 +00001902 kvmppc_free_hpt(kvm);
David Gibson54738c02011-06-29 00:22:41 +00001903 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
Paul Mackerrasde56a942011-06-29 00:21:34 +00001904}
1905
1906/* These are stubs for now */
1907void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
1908{
1909}
1910
1911/* We don't need to emulate any privileged instructions or dcbz */
1912int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
1913 unsigned int inst, int *advance)
1914{
1915 return EMULATE_FAIL;
1916}
1917
Alexander Graf54771e62012-05-04 14:55:12 +02001918int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001919{
1920 return EMULATE_FAIL;
1921}
1922
Alexander Graf54771e62012-05-04 14:55:12 +02001923int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001924{
1925 return EMULATE_FAIL;
1926}
1927
1928static int kvmppc_book3s_hv_init(void)
1929{
1930 int r;
1931
1932 r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1933
1934 if (r)
1935 return r;
1936
1937 r = kvmppc_mmu_hv_init();
1938
1939 return r;
1940}
1941
1942static void kvmppc_book3s_hv_exit(void)
1943{
1944 kvm_exit();
1945}
1946
1947module_init(kvmppc_book3s_hv_init);
1948module_exit(kvmppc_book3s_hv_exit);