blob: 5af0f29798332f1ca908efd8975e48f138beb5d0 [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;
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000486 int idx;
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;
522 default:
523 return RESUME_HOST;
524 }
525 kvmppc_set_gpr(vcpu, 3, ret);
526 vcpu->arch.hcall_needed = 0;
527 return RESUME_GUEST;
528}
529
Paul Mackerrasde56a942011-06-29 00:21:34 +0000530static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
531 struct task_struct *tsk)
532{
533 int r = RESUME_HOST;
534
535 vcpu->stat.sum_exits++;
536
537 run->exit_reason = KVM_EXIT_UNKNOWN;
538 run->ready_for_interrupt_injection = 1;
539 switch (vcpu->arch.trap) {
540 /* We're good on these - the host merely wanted to get our attention */
541 case BOOK3S_INTERRUPT_HV_DECREMENTER:
542 vcpu->stat.dec_exits++;
543 r = RESUME_GUEST;
544 break;
545 case BOOK3S_INTERRUPT_EXTERNAL:
546 vcpu->stat.ext_intr_exits++;
547 r = RESUME_GUEST;
548 break;
549 case BOOK3S_INTERRUPT_PERFMON:
550 r = RESUME_GUEST;
551 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000552 case BOOK3S_INTERRUPT_MACHINE_CHECK:
553 /*
554 * Deliver a machine check interrupt to the guest.
555 * We have to do this, even if the host has handled the
556 * machine check, because machine checks use SRR0/1 and
557 * the interrupt might have trashed guest state in them.
558 */
559 kvmppc_book3s_queue_irqprio(vcpu,
560 BOOK3S_INTERRUPT_MACHINE_CHECK);
561 r = RESUME_GUEST;
562 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000563 case BOOK3S_INTERRUPT_PROGRAM:
564 {
565 ulong flags;
566 /*
567 * Normally program interrupts are delivered directly
568 * to the guest by the hardware, but we can get here
569 * as a result of a hypervisor emulation interrupt
570 * (e40) getting turned into a 700 by BML RTAS.
571 */
572 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
573 kvmppc_core_queue_program(vcpu, flags);
574 r = RESUME_GUEST;
575 break;
576 }
577 case BOOK3S_INTERRUPT_SYSCALL:
578 {
579 /* hcall - punt to userspace */
580 int i;
581
582 if (vcpu->arch.shregs.msr & MSR_PR) {
583 /* sc 1 from userspace - reflect to guest syscall */
584 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
585 r = RESUME_GUEST;
586 break;
587 }
588 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
589 for (i = 0; i < 9; ++i)
590 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
591 run->exit_reason = KVM_EXIT_PAPR_HCALL;
592 vcpu->arch.hcall_needed = 1;
593 r = RESUME_HOST;
594 break;
595 }
596 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000597 * We get these next two if the guest accesses a page which it thinks
598 * it has mapped but which is not actually present, either because
599 * it is for an emulated I/O device or because the corresonding
600 * host page has been paged out. Any other HDSI/HISI interrupts
601 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000602 */
603 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000604 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000605 break;
606 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000607 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
608 vcpu->arch.fault_dsisr = 0;
609 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000610 break;
611 /*
612 * This occurs if the guest executes an illegal instruction.
613 * We just generate a program interrupt to the guest, since
614 * we don't emulate any guest instructions at this stage.
615 */
616 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
617 kvmppc_core_queue_program(vcpu, 0x80000);
618 r = RESUME_GUEST;
619 break;
620 default:
621 kvmppc_dump_regs(vcpu);
622 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
623 vcpu->arch.trap, kvmppc_get_pc(vcpu),
624 vcpu->arch.shregs.msr);
625 r = RESUME_HOST;
626 BUG();
627 break;
628 }
629
Paul Mackerrasde56a942011-06-29 00:21:34 +0000630 return r;
631}
632
633int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
634 struct kvm_sregs *sregs)
635{
636 int i;
637
638 sregs->pvr = vcpu->arch.pvr;
639
640 memset(sregs, 0, sizeof(struct kvm_sregs));
641 for (i = 0; i < vcpu->arch.slb_max; i++) {
642 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
643 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
644 }
645
646 return 0;
647}
648
649int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
650 struct kvm_sregs *sregs)
651{
652 int i, j;
653
654 kvmppc_set_pvr(vcpu, sregs->pvr);
655
656 j = 0;
657 for (i = 0; i < vcpu->arch.slb_nr; i++) {
658 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
659 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
660 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
661 ++j;
662 }
663 }
664 vcpu->arch.slb_max = j;
665
666 return 0;
667}
668
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000669int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000670{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000671 int r = 0;
672 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +0000673
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000674 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000675 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000676 *val = get_reg_val(id, 0);
677 break;
678 case KVM_REG_PPC_DABR:
679 *val = get_reg_val(id, vcpu->arch.dabr);
680 break;
681 case KVM_REG_PPC_DSCR:
682 *val = get_reg_val(id, vcpu->arch.dscr);
683 break;
684 case KVM_REG_PPC_PURR:
685 *val = get_reg_val(id, vcpu->arch.purr);
686 break;
687 case KVM_REG_PPC_SPURR:
688 *val = get_reg_val(id, vcpu->arch.spurr);
689 break;
690 case KVM_REG_PPC_AMR:
691 *val = get_reg_val(id, vcpu->arch.amr);
692 break;
693 case KVM_REG_PPC_UAMOR:
694 *val = get_reg_val(id, vcpu->arch.uamor);
695 break;
696 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
697 i = id - KVM_REG_PPC_MMCR0;
698 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
699 break;
700 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
701 i = id - KVM_REG_PPC_PMC1;
702 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +0000703 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000704#ifdef CONFIG_VSX
705 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
706 if (cpu_has_feature(CPU_FTR_VSX)) {
707 /* VSX => FP reg i is stored in arch.vsr[2*i] */
708 long int i = id - KVM_REG_PPC_FPR0;
709 *val = get_reg_val(id, vcpu->arch.vsr[2 * i]);
710 } else {
711 /* let generic code handle it */
712 r = -EINVAL;
713 }
714 break;
715 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
716 if (cpu_has_feature(CPU_FTR_VSX)) {
717 long int i = id - KVM_REG_PPC_VSR0;
718 val->vsxval[0] = vcpu->arch.vsr[2 * i];
719 val->vsxval[1] = vcpu->arch.vsr[2 * i + 1];
720 } else {
721 r = -ENXIO;
722 }
723 break;
724#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000725 case KVM_REG_PPC_VPA_ADDR:
726 spin_lock(&vcpu->arch.vpa_update_lock);
727 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
728 spin_unlock(&vcpu->arch.vpa_update_lock);
729 break;
730 case KVM_REG_PPC_VPA_SLB:
731 spin_lock(&vcpu->arch.vpa_update_lock);
732 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
733 val->vpaval.length = vcpu->arch.slb_shadow.len;
734 spin_unlock(&vcpu->arch.vpa_update_lock);
735 break;
736 case KVM_REG_PPC_VPA_DTL:
737 spin_lock(&vcpu->arch.vpa_update_lock);
738 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
739 val->vpaval.length = vcpu->arch.dtl.len;
740 spin_unlock(&vcpu->arch.vpa_update_lock);
741 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000742 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000743 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000744 break;
745 }
746
747 return r;
748}
749
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000750int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000751{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000752 int r = 0;
753 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +0000754 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +0000755
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000756 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000757 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +0000758 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000759 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +0000760 r = -EINVAL;
761 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000762 case KVM_REG_PPC_DABR:
763 vcpu->arch.dabr = set_reg_val(id, *val);
764 break;
765 case KVM_REG_PPC_DSCR:
766 vcpu->arch.dscr = set_reg_val(id, *val);
767 break;
768 case KVM_REG_PPC_PURR:
769 vcpu->arch.purr = set_reg_val(id, *val);
770 break;
771 case KVM_REG_PPC_SPURR:
772 vcpu->arch.spurr = set_reg_val(id, *val);
773 break;
774 case KVM_REG_PPC_AMR:
775 vcpu->arch.amr = set_reg_val(id, *val);
776 break;
777 case KVM_REG_PPC_UAMOR:
778 vcpu->arch.uamor = set_reg_val(id, *val);
779 break;
780 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
781 i = id - KVM_REG_PPC_MMCR0;
782 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
783 break;
784 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
785 i = id - KVM_REG_PPC_PMC1;
786 vcpu->arch.pmc[i] = set_reg_val(id, *val);
787 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000788#ifdef CONFIG_VSX
789 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
790 if (cpu_has_feature(CPU_FTR_VSX)) {
791 /* VSX => FP reg i is stored in arch.vsr[2*i] */
792 long int i = id - KVM_REG_PPC_FPR0;
793 vcpu->arch.vsr[2 * i] = set_reg_val(id, *val);
794 } else {
795 /* let generic code handle it */
796 r = -EINVAL;
797 }
798 break;
799 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
800 if (cpu_has_feature(CPU_FTR_VSX)) {
801 long int i = id - KVM_REG_PPC_VSR0;
802 vcpu->arch.vsr[2 * i] = val->vsxval[0];
803 vcpu->arch.vsr[2 * i + 1] = val->vsxval[1];
804 } else {
805 r = -ENXIO;
806 }
807 break;
808#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000809 case KVM_REG_PPC_VPA_ADDR:
810 addr = set_reg_val(id, *val);
811 r = -EINVAL;
812 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
813 vcpu->arch.dtl.next_gpa))
814 break;
815 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
816 break;
817 case KVM_REG_PPC_VPA_SLB:
818 addr = val->vpaval.addr;
819 len = val->vpaval.length;
820 r = -EINVAL;
821 if (addr && !vcpu->arch.vpa.next_gpa)
822 break;
823 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
824 break;
825 case KVM_REG_PPC_VPA_DTL:
826 addr = val->vpaval.addr;
827 len = val->vpaval.length;
828 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +0000829 if (addr && (len < sizeof(struct dtl_entry) ||
830 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +0000831 break;
832 len -= len % sizeof(struct dtl_entry);
833 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
834 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000835 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000836 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000837 break;
838 }
839
840 return r;
841}
842
Paul Mackerrasde56a942011-06-29 00:21:34 +0000843int kvmppc_core_check_processor_compat(void)
844{
Paul Mackerras9e368f22011-06-29 00:40:08 +0000845 if (cpu_has_feature(CPU_FTR_HVMODE))
Paul Mackerrasde56a942011-06-29 00:21:34 +0000846 return 0;
847 return -EIO;
848}
849
850struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
851{
852 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000853 int err = -EINVAL;
854 int core;
855 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000856
Paul Mackerras371fefd2011-06-29 00:23:08 +0000857 core = id / threads_per_core;
858 if (core >= KVM_MAX_VCORES)
859 goto out;
860
861 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200862 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000863 if (!vcpu)
864 goto out;
865
866 err = kvm_vcpu_init(vcpu, kvm, id);
867 if (err)
868 goto free_vcpu;
869
870 vcpu->arch.shared = &vcpu->arch.shregs;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000871 vcpu->arch.mmcr[0] = MMCR0_FC;
872 vcpu->arch.ctrl = CTRL_RUNLATCH;
873 /* default to host PVR, since we can't spoof it */
874 vcpu->arch.pvr = mfspr(SPRN_PVR);
875 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000876 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000877 spin_lock_init(&vcpu->arch.tbacct_lock);
878 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000879
Paul Mackerrasde56a942011-06-29 00:21:34 +0000880 kvmppc_mmu_book3s_hv_init(vcpu);
881
Paul Mackerras8455d792012-10-15 01:17:42 +0000882 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000883
884 init_waitqueue_head(&vcpu->arch.cpu_run);
885
886 mutex_lock(&kvm->lock);
887 vcore = kvm->arch.vcores[core];
888 if (!vcore) {
889 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
890 if (vcore) {
891 INIT_LIST_HEAD(&vcore->runnable_threads);
892 spin_lock_init(&vcore->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000893 init_waitqueue_head(&vcore->wq);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000894 vcore->preempt_tb = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000895 }
896 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000897 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000898 }
899 mutex_unlock(&kvm->lock);
900
901 if (!vcore)
902 goto free_vcpu;
903
904 spin_lock(&vcore->lock);
905 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000906 spin_unlock(&vcore->lock);
907 vcpu->arch.vcore = vcore;
908
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200909 vcpu->arch.cpu_type = KVM_CPU_3S_64;
910 kvmppc_sanity_check(vcpu);
911
Paul Mackerrasde56a942011-06-29 00:21:34 +0000912 return vcpu;
913
914free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200915 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000916out:
917 return ERR_PTR(err);
918}
919
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000920static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
921{
922 if (vpa->pinned_addr)
923 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
924 vpa->dirty);
925}
926
Paul Mackerrasde56a942011-06-29 00:21:34 +0000927void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
928{
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000929 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000930 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
931 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
932 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000933 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000934 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200935 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000936}
937
Paul Mackerras19ccb762011-07-23 17:42:46 +1000938static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000939{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000940 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000941
Paul Mackerras19ccb762011-07-23 17:42:46 +1000942 now = get_tb();
943 if (now > vcpu->arch.dec_expires) {
944 /* decrementer has already gone negative */
945 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -0600946 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000947 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000948 }
Paul Mackerras19ccb762011-07-23 17:42:46 +1000949 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
950 / tb_ticks_per_sec;
951 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
952 HRTIMER_MODE_REL);
953 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000954}
955
Paul Mackerras19ccb762011-07-23 17:42:46 +1000956static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000957{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000958 vcpu->arch.ceded = 0;
959 if (vcpu->arch.timer_running) {
960 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
961 vcpu->arch.timer_running = 0;
962 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000963}
964
965extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
966extern void xics_wake_cpu(int cpu);
967
968static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
969 struct kvm_vcpu *vcpu)
970{
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000971 u64 now;
972
Paul Mackerras371fefd2011-06-29 00:23:08 +0000973 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
974 return;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000975 spin_lock(&vcpu->arch.tbacct_lock);
976 now = mftb();
977 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
978 vcpu->arch.stolen_logged;
979 vcpu->arch.busy_preempt = now;
980 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
981 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +0000982 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000983 list_del(&vcpu->arch.run_list);
984}
985
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000986static int kvmppc_grab_hwthread(int cpu)
987{
988 struct paca_struct *tpaca;
989 long timeout = 1000;
990
991 tpaca = &paca[cpu];
992
993 /* Ensure the thread won't go into the kernel if it wakes */
994 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerras7b444c62012-10-15 01:16:14 +0000995 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000996
997 /*
998 * If the thread is already executing in the kernel (e.g. handling
999 * a stray interrupt), wait for it to get back to nap mode.
1000 * The smp_mb() is to ensure that our setting of hwthread_req
1001 * is visible before we look at hwthread_state, so if this
1002 * races with the code at system_reset_pSeries and the thread
1003 * misses our setting of hwthread_req, we are sure to see its
1004 * setting of hwthread_state, and vice versa.
1005 */
1006 smp_mb();
1007 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1008 if (--timeout <= 0) {
1009 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1010 return -EBUSY;
1011 }
1012 udelay(1);
1013 }
1014 return 0;
1015}
1016
1017static void kvmppc_release_hwthread(int cpu)
1018{
1019 struct paca_struct *tpaca;
1020
1021 tpaca = &paca[cpu];
1022 tpaca->kvm_hstate.hwthread_req = 0;
1023 tpaca->kvm_hstate.kvm_vcpu = NULL;
1024}
1025
Paul Mackerras371fefd2011-06-29 00:23:08 +00001026static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1027{
1028 int cpu;
1029 struct paca_struct *tpaca;
1030 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1031
Paul Mackerras19ccb762011-07-23 17:42:46 +10001032 if (vcpu->arch.timer_running) {
1033 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1034 vcpu->arch.timer_running = 0;
1035 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001036 cpu = vc->pcpu + vcpu->arch.ptid;
1037 tpaca = &paca[cpu];
1038 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1039 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001040 tpaca->kvm_hstate.napping = 0;
1041 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001042 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +00001043#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001044 if (vcpu->arch.ptid) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001045 xics_wake_cpu(cpu);
1046 ++vc->n_woken;
1047 }
1048#endif
1049}
1050
1051static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1052{
1053 int i;
1054
1055 HMT_low();
1056 i = 0;
1057 while (vc->nap_count < vc->n_woken) {
1058 if (++i >= 1000000) {
1059 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1060 vc->nap_count, vc->n_woken);
1061 break;
1062 }
1063 cpu_relax();
1064 }
1065 HMT_medium();
1066}
1067
1068/*
1069 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001070 * this core are off-line. Then grab the threads so they can't
1071 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001072 */
1073static int on_primary_thread(void)
1074{
1075 int cpu = smp_processor_id();
1076 int thr = cpu_thread_in_core(cpu);
1077
1078 if (thr)
1079 return 0;
1080 while (++thr < threads_per_core)
1081 if (cpu_online(cpu + thr))
1082 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001083
1084 /* Grab all hw threads so they can't go into the kernel */
1085 for (thr = 1; thr < threads_per_core; ++thr) {
1086 if (kvmppc_grab_hwthread(cpu + thr)) {
1087 /* Couldn't grab one; let the others go */
1088 do {
1089 kvmppc_release_hwthread(cpu + thr);
1090 } while (--thr > 0);
1091 return 0;
1092 }
1093 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001094 return 1;
1095}
1096
1097/*
1098 * Run a set of guest threads on a physical core.
1099 * Called with vc->lock held.
1100 */
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001101static void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001102{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001103 struct kvm_vcpu *vcpu, *vcpu0, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001104 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001105 u64 now;
Paul Mackerras081f3232012-06-01 20:20:24 +10001106 int ptid, i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001107 int srcu_idx;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001108 struct kvm_vcpu *vcpus_to_update[threads_per_core];
Paul Mackerrasde56a942011-06-29 00:21:34 +00001109
Paul Mackerras371fefd2011-06-29 00:23:08 +00001110 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +10001111 need_vpa_update = 0;
1112 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001113 if (signal_pending(vcpu->arch.run_task))
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001114 return;
1115 if (vcpu->arch.vpa.update_pending ||
1116 vcpu->arch.slb_shadow.update_pending ||
1117 vcpu->arch.dtl.update_pending)
1118 vcpus_to_update[need_vpa_update++] = vcpu;
Paul Mackerras081f3232012-06-01 20:20:24 +10001119 }
1120
1121 /*
1122 * Initialize *vc, in particular vc->vcore_state, so we can
1123 * drop the vcore lock if necessary.
1124 */
1125 vc->n_woken = 0;
1126 vc->nap_count = 0;
1127 vc->entry_exit_count = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001128 vc->vcore_state = VCORE_STARTING;
Paul Mackerras081f3232012-06-01 20:20:24 +10001129 vc->in_guest = 0;
1130 vc->napping_threads = 0;
1131
1132 /*
1133 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1134 * which can't be called with any spinlocks held.
1135 */
1136 if (need_vpa_update) {
1137 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001138 for (i = 0; i < need_vpa_update; ++i)
1139 kvmppc_update_vpas(vcpus_to_update[i]);
Paul Mackerras081f3232012-06-01 20:20:24 +10001140 spin_lock(&vc->lock);
1141 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001142
1143 /*
Paul Mackerras19ccb762011-07-23 17:42:46 +10001144 * Assign physical thread IDs, first to non-ceded vcpus
1145 * and then to ceded ones.
1146 */
1147 ptid = 0;
1148 vcpu0 = NULL;
1149 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1150 if (!vcpu->arch.ceded) {
1151 if (!ptid)
1152 vcpu0 = vcpu;
1153 vcpu->arch.ptid = ptid++;
1154 }
1155 }
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001156 if (!vcpu0)
1157 goto out; /* nothing to run; should never happen */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001158 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1159 if (vcpu->arch.ceded)
1160 vcpu->arch.ptid = ptid++;
1161
Paul Mackerras7b444c62012-10-15 01:16:14 +00001162 /*
1163 * Make sure we are running on thread 0, and that
1164 * secondary threads are offline.
1165 */
1166 if (threads_per_core > 1 && !on_primary_thread()) {
1167 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1168 vcpu->arch.ret = -EBUSY;
1169 goto out;
1170 }
1171
Paul Mackerras371fefd2011-06-29 00:23:08 +00001172 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001173 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001174 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001175 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001176 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001177
Paul Mackerras2f12f032012-10-15 01:17:17 +00001178 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001179 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001180 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001181
Paul Mackerras19ccb762011-07-23 17:42:46 +10001182 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001183
1184 srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
1185
Paul Mackerras19ccb762011-07-23 17:42:46 +10001186 __kvmppc_vcore_entry(NULL, vcpu0);
1187
Paul Mackerras371fefd2011-06-29 00:23:08 +00001188 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001189 /* disable sending of IPIs on virtual external irqs */
1190 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1191 vcpu->cpu = -1;
1192 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001193 if (vc->nap_count < vc->n_woken)
1194 kvmppc_wait_for_nap(vc);
Paul Mackerras2f12f032012-10-15 01:17:17 +00001195 for (i = 0; i < threads_per_core; ++i)
1196 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001197 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001198 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001199 spin_unlock(&vc->lock);
1200
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001201 srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
1202
Paul Mackerras371fefd2011-06-29 00:23:08 +00001203 /* make sure updates to secondary vcpu structs are visible now */
1204 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001205 kvm_guest_exit();
1206
1207 preempt_enable();
1208 kvm_resched(vcpu);
1209
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001210 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001211 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001212 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1213 /* cancel pending dec exception if dec is positive */
1214 if (now < vcpu->arch.dec_expires &&
1215 kvmppc_core_pending_dec(vcpu))
1216 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001217
1218 ret = RESUME_GUEST;
1219 if (vcpu->arch.trap)
1220 ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
1221 vcpu->arch.run_task);
1222
Paul Mackerras371fefd2011-06-29 00:23:08 +00001223 vcpu->arch.ret = ret;
1224 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001225
1226 if (vcpu->arch.ceded) {
1227 if (ret != RESUME_GUEST)
1228 kvmppc_end_cede(vcpu);
1229 else
1230 kvmppc_set_timer(vcpu);
1231 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001232 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001233
Paul Mackerrasde56a942011-06-29 00:21:34 +00001234 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001235 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001236 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1237 arch.run_list) {
1238 if (vcpu->arch.ret != RESUME_GUEST) {
1239 kvmppc_remove_runnable(vc, vcpu);
1240 wake_up(&vcpu->arch.cpu_run);
1241 }
1242 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001243}
1244
Paul Mackerras19ccb762011-07-23 17:42:46 +10001245/*
1246 * Wait for some other vcpu thread to execute us, and
1247 * wake us up when we need to handle something in the host.
1248 */
1249static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001250{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001251 DEFINE_WAIT(wait);
1252
Paul Mackerras19ccb762011-07-23 17:42:46 +10001253 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1254 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1255 schedule();
1256 finish_wait(&vcpu->arch.cpu_run, &wait);
1257}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001258
Paul Mackerras19ccb762011-07-23 17:42:46 +10001259/*
1260 * All the vcpus in this vcore are idle, so wait for a decrementer
1261 * or external interrupt to one of the vcpus. vc->lock is held.
1262 */
1263static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1264{
1265 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001266
1267 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1268 vc->vcore_state = VCORE_SLEEPING;
1269 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001270 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001271 finish_wait(&vc->wq, &wait);
1272 spin_lock(&vc->lock);
1273 vc->vcore_state = VCORE_INACTIVE;
1274}
1275
1276static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1277{
1278 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001279 struct kvmppc_vcore *vc;
1280 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001281
Paul Mackerras371fefd2011-06-29 00:23:08 +00001282 kvm_run->exit_reason = 0;
1283 vcpu->arch.ret = RESUME_GUEST;
1284 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001285 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001286
Paul Mackerras371fefd2011-06-29 00:23:08 +00001287 /*
1288 * Synchronize with other threads in this virtual core
1289 */
1290 vc = vcpu->arch.vcore;
1291 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001292 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001293 vcpu->arch.run_task = current;
1294 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001295 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10001296 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001297 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001298 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1299 ++vc->n_runnable;
1300
Paul Mackerras19ccb762011-07-23 17:42:46 +10001301 /*
1302 * This happens the first time this is called for a vcpu.
1303 * If the vcore is already running, we may be able to start
1304 * this thread straight away and have it join in.
1305 */
Paul Mackerras8455d792012-10-15 01:17:42 +00001306 if (!signal_pending(current)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001307 if (vc->vcore_state == VCORE_RUNNING &&
1308 VCORE_EXIT_COUNT(vc) == 0) {
1309 vcpu->arch.ptid = vc->n_runnable - 1;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001310 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001311 kvmppc_start_thread(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00001312 } else if (vc->vcore_state == VCORE_SLEEPING) {
1313 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001314 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001315
Paul Mackerras8455d792012-10-15 01:17:42 +00001316 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001317
1318 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1319 !signal_pending(current)) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001320 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001321 spin_unlock(&vc->lock);
1322 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1323 spin_lock(&vc->lock);
1324 continue;
1325 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001326 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1327 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001328 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001329 if (signal_pending(v->arch.run_task)) {
1330 kvmppc_remove_runnable(vc, v);
1331 v->stat.signal_exits++;
1332 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1333 v->arch.ret = -EINTR;
1334 wake_up(&v->arch.cpu_run);
1335 }
1336 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001337 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1338 break;
1339 vc->runner = vcpu;
1340 n_ceded = 0;
1341 list_for_each_entry(v, &vc->runnable_threads, arch.run_list)
1342 if (!v->arch.pending_exceptions)
1343 n_ceded += v->arch.ceded;
1344 if (n_ceded == vc->n_runnable)
1345 kvmppc_vcore_blocked(vc);
1346 else
1347 kvmppc_run_core(vc);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001348 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001349 }
1350
Paul Mackerras8455d792012-10-15 01:17:42 +00001351 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1352 (vc->vcore_state == VCORE_RUNNING ||
1353 vc->vcore_state == VCORE_EXITING)) {
1354 spin_unlock(&vc->lock);
1355 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1356 spin_lock(&vc->lock);
1357 }
1358
1359 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1360 kvmppc_remove_runnable(vc, vcpu);
1361 vcpu->stat.signal_exits++;
1362 kvm_run->exit_reason = KVM_EXIT_INTR;
1363 vcpu->arch.ret = -EINTR;
1364 }
1365
1366 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1367 /* Wake up some vcpu to run the core */
1368 v = list_first_entry(&vc->runnable_threads,
1369 struct kvm_vcpu, arch.run_list);
1370 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001371 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001372
Paul Mackerras19ccb762011-07-23 17:42:46 +10001373 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001374 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001375}
1376
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001377int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
1378{
1379 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001380 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001381
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001382 if (!vcpu->arch.sane) {
1383 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1384 return -EINVAL;
1385 }
1386
Scott Wood25051b52011-11-08 18:23:23 -06001387 kvmppc_core_prepare_to_enter(vcpu);
1388
Paul Mackerras19ccb762011-07-23 17:42:46 +10001389 /* No need to go into the guest when all we'll do is come back out */
1390 if (signal_pending(current)) {
1391 run->exit_reason = KVM_EXIT_INTR;
1392 return -EINTR;
1393 }
1394
Paul Mackerras32fad282012-05-04 02:32:53 +00001395 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1396 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1397 smp_mb();
1398
1399 /* On the first time here, set up HTAB and VRMA or RMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001400 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001401 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001402 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001403 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001404 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001405
1406 flush_fp_to_thread(current);
1407 flush_altivec_to_thread(current);
1408 flush_vsx_to_thread(current);
1409 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001410 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001411 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001412
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001413 do {
1414 r = kvmppc_run_vcpu(run, vcpu);
1415
1416 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1417 !(vcpu->arch.shregs.msr & MSR_PR)) {
1418 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001419 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001420 } else if (r == RESUME_PAGE_FAULT) {
1421 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1422 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1423 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1424 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001425 }
1426 } while (r == RESUME_GUEST);
Paul Mackerras32fad282012-05-04 02:32:53 +00001427
1428 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001429 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00001430 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001431 return r;
1432}
1433
David Gibson54738c02011-06-29 00:22:41 +00001434
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001435/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +00001436 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001437static inline int lpcr_rmls(unsigned long rma_size)
1438{
1439 switch (rma_size) {
1440 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001441 if (cpu_has_feature(CPU_FTR_ARCH_206))
1442 return 8; /* only supported on POWER7 */
1443 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001444 case 64ul << 20: /* 64 MB */
1445 return 3;
1446 case 128ul << 20: /* 128 MB */
1447 return 7;
1448 case 256ul << 20: /* 256 MB */
1449 return 4;
1450 case 1ul << 30: /* 1 GB */
1451 return 2;
1452 case 16ul << 30: /* 16 GB */
1453 return 1;
1454 case 256ul << 30: /* 256 GB */
1455 return 0;
1456 default:
1457 return -1;
1458 }
1459}
1460
1461static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1462{
Alexander Grafb4e70612012-01-16 16:50:10 +01001463 struct kvmppc_linear_info *ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001464 struct page *page;
1465
1466 if (vmf->pgoff >= ri->npages)
1467 return VM_FAULT_SIGBUS;
1468
1469 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1470 get_page(page);
1471 vmf->page = page;
1472 return 0;
1473}
1474
1475static const struct vm_operations_struct kvm_rma_vm_ops = {
1476 .fault = kvm_rma_fault,
1477};
1478
1479static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1480{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001481 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001482 vma->vm_ops = &kvm_rma_vm_ops;
1483 return 0;
1484}
1485
1486static int kvm_rma_release(struct inode *inode, struct file *filp)
1487{
Alexander Grafb4e70612012-01-16 16:50:10 +01001488 struct kvmppc_linear_info *ri = filp->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001489
1490 kvm_release_rma(ri);
1491 return 0;
1492}
1493
1494static struct file_operations kvm_rma_fops = {
1495 .mmap = kvm_rma_mmap,
1496 .release = kvm_rma_release,
1497};
1498
1499long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
1500{
Alexander Grafb4e70612012-01-16 16:50:10 +01001501 struct kvmppc_linear_info *ri;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001502 long fd;
1503
1504 ri = kvm_alloc_rma();
1505 if (!ri)
1506 return -ENOMEM;
1507
1508 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR);
1509 if (fd < 0)
1510 kvm_release_rma(ri);
1511
1512 ret->rma_size = ri->npages << PAGE_SHIFT;
1513 return fd;
1514}
1515
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001516static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1517 int linux_psize)
1518{
1519 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1520
1521 if (!def->shift)
1522 return;
1523 (*sps)->page_shift = def->shift;
1524 (*sps)->slb_enc = def->sllp;
1525 (*sps)->enc[0].page_shift = def->shift;
1526 (*sps)->enc[0].pte_enc = def->penc;
1527 (*sps)++;
1528}
1529
1530int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
1531{
1532 struct kvm_ppc_one_seg_page_size *sps;
1533
1534 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1535 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1536 info->flags |= KVM_PPC_1T_SEGMENTS;
1537 info->slb_size = mmu_slb_size;
1538
1539 /* We only support these sizes for now, and no muti-size segments */
1540 sps = &info->sps[0];
1541 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1542 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1543 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1544
1545 return 0;
1546}
1547
Paul Mackerras82ed3612011-12-15 02:03:22 +00001548/*
1549 * Get (and clear) the dirty memory log for a memory slot.
1550 */
1551int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1552{
1553 struct kvm_memory_slot *memslot;
1554 int r;
1555 unsigned long n;
1556
1557 mutex_lock(&kvm->slots_lock);
1558
1559 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07001560 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00001561 goto out;
1562
1563 memslot = id_to_memslot(kvm->memslots, log->slot);
1564 r = -ENOENT;
1565 if (!memslot->dirty_bitmap)
1566 goto out;
1567
1568 n = kvm_dirty_bitmap_bytes(memslot);
1569 memset(memslot->dirty_bitmap, 0, n);
1570
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001571 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00001572 if (r)
1573 goto out;
1574
1575 r = -EFAULT;
1576 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1577 goto out;
1578
1579 r = 0;
1580out:
1581 mutex_unlock(&kvm->slots_lock);
1582 return r;
1583}
1584
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001585static void unpin_slot(struct kvm_memory_slot *memslot)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001586{
1587 unsigned long *physp;
1588 unsigned long j, npages, pfn;
1589 struct page *page;
1590
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001591 physp = memslot->arch.slot_phys;
1592 npages = memslot->npages;
1593 if (!physp)
1594 return;
1595 for (j = 0; j < npages; j++) {
1596 if (!(physp[j] & KVMPPC_GOT_PAGE))
1597 continue;
1598 pfn = physp[j] >> PAGE_SHIFT;
1599 page = pfn_to_page(pfn);
1600 SetPageDirty(page);
1601 put_page(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001602 }
1603}
1604
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001605void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1606 struct kvm_memory_slot *dont)
1607{
1608 if (!dont || free->arch.rmap != dont->arch.rmap) {
1609 vfree(free->arch.rmap);
1610 free->arch.rmap = NULL;
1611 }
1612 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1613 unpin_slot(free);
1614 vfree(free->arch.slot_phys);
1615 free->arch.slot_phys = NULL;
1616 }
1617}
1618
1619int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1620 unsigned long npages)
1621{
1622 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1623 if (!slot->arch.rmap)
1624 return -ENOMEM;
1625 slot->arch.slot_phys = NULL;
1626
1627 return 0;
1628}
1629
1630int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1631 struct kvm_memory_slot *memslot,
1632 struct kvm_userspace_memory_region *mem)
1633{
1634 unsigned long *phys;
1635
1636 /* Allocate a slot_phys array if needed */
1637 phys = memslot->arch.slot_phys;
1638 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1639 phys = vzalloc(memslot->npages * sizeof(unsigned long));
1640 if (!phys)
1641 return -ENOMEM;
1642 memslot->arch.slot_phys = phys;
1643 }
1644
1645 return 0;
1646}
1647
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001648void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001649 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001650 const struct kvm_memory_slot *old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001651{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001652 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1653 struct kvm_memory_slot *memslot;
1654
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001655 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001656 /*
1657 * If modifying a memslot, reset all the rmap dirty bits.
1658 * If this is a new memslot, we don't need to do anything
1659 * since the rmap array starts out as all zeroes,
1660 * i.e. no pages are dirty.
1661 */
1662 memslot = id_to_memslot(kvm->memslots, mem->slot);
1663 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1664 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001665}
1666
Paul Mackerras32fad282012-05-04 02:32:53 +00001667static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001668{
1669 int err = 0;
1670 struct kvm *kvm = vcpu->kvm;
Alexander Grafb4e70612012-01-16 16:50:10 +01001671 struct kvmppc_linear_info *ri = NULL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001672 unsigned long hva;
1673 struct kvm_memory_slot *memslot;
1674 struct vm_area_struct *vma;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001675 unsigned long lpcr, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001676 unsigned long psize, porder;
1677 unsigned long rma_size;
1678 unsigned long rmls;
1679 unsigned long *physp;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001680 unsigned long i, npages;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001681 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001682
1683 mutex_lock(&kvm->lock);
1684 if (kvm->arch.rma_setup_done)
1685 goto out; /* another vcpu beat us to it */
1686
Paul Mackerras32fad282012-05-04 02:32:53 +00001687 /* Allocate hashed page table (if not done already) and reset it */
1688 if (!kvm->arch.hpt_virt) {
1689 err = kvmppc_alloc_hpt(kvm, NULL);
1690 if (err) {
1691 pr_err("KVM: Couldn't alloc HPT\n");
1692 goto out;
1693 }
1694 }
1695
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001696 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001697 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001698 memslot = gfn_to_memslot(kvm, 0);
1699
1700 /* We must have some memory at 0 by now */
1701 err = -EINVAL;
1702 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001703 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001704
1705 /* Look up the VMA for the start of this memory slot */
1706 hva = memslot->userspace_addr;
1707 down_read(&current->mm->mmap_sem);
1708 vma = find_vma(current->mm, hva);
1709 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
1710 goto up_out;
1711
1712 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001713 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001714
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001715 /* Is this one of our preallocated RMAs? */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001716 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
1717 hva == vma->vm_start)
1718 ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001719
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001720 up_read(&current->mm->mmap_sem);
1721
1722 if (!ri) {
1723 /* On POWER7, use VRMA; on PPC970, give up */
1724 err = -EPERM;
1725 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1726 pr_err("KVM: CPU requires an RMO\n");
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001727 goto out_srcu;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001728 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001729
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001730 /* We can handle 4k, 64k or 16M pages in the VRMA */
1731 err = -EINVAL;
1732 if (!(psize == 0x1000 || psize == 0x10000 ||
1733 psize == 0x1000000))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001734 goto out_srcu;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001735
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001736 /* Update VRMASD field in the LPCR */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001737 senc = slb_pgsize_encoding(psize);
Paul Mackerras697d3892011-12-12 12:36:37 +00001738 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1739 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001740 lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
1741 lpcr |= senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001742 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001743
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001744 /* Create HPTEs in the hash page table for the VRMA */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001745 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001746
1747 } else {
1748 /* Set up to use an RMO region */
1749 rma_size = ri->npages;
1750 if (rma_size > memslot->npages)
1751 rma_size = memslot->npages;
1752 rma_size <<= PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001753 rmls = lpcr_rmls(rma_size);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001754 err = -EINVAL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001755 if (rmls < 0) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001756 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001757 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001758 }
1759 atomic_inc(&ri->use_count);
1760 kvm->arch.rma = ri;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001761
1762 /* Update LPCR and RMOR */
1763 lpcr = kvm->arch.lpcr;
1764 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1765 /* PPC970; insert RMLS value (split field) in HID4 */
1766 lpcr &= ~((1ul << HID4_RMLS0_SH) |
1767 (3ul << HID4_RMLS2_SH));
1768 lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
1769 ((rmls & 3) << HID4_RMLS2_SH);
1770 /* RMOR is also in HID4 */
1771 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
1772 << HID4_RMOR_SH;
1773 } else {
1774 /* POWER7 */
1775 lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
1776 lpcr |= rmls << LPCR_RMLS_SH;
1777 kvm->arch.rmor = kvm->arch.rma->base_pfn << PAGE_SHIFT;
1778 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001779 kvm->arch.lpcr = lpcr;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001780 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001781 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001782
1783 /* Initialize phys addrs of pages in RMO */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001784 npages = ri->npages;
1785 porder = __ilog2(npages);
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001786 physp = memslot->arch.slot_phys;
1787 if (physp) {
1788 if (npages > memslot->npages)
1789 npages = memslot->npages;
1790 spin_lock(&kvm->arch.slot_phys_lock);
1791 for (i = 0; i < npages; ++i)
1792 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
1793 porder;
1794 spin_unlock(&kvm->arch.slot_phys_lock);
1795 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001796 }
1797
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001798 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
1799 smp_wmb();
1800 kvm->arch.rma_setup_done = 1;
1801 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001802 out_srcu:
1803 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001804 out:
1805 mutex_unlock(&kvm->lock);
1806 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001807
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001808 up_out:
1809 up_read(&current->mm->mmap_sem);
1810 goto out;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001811}
1812
1813int kvmppc_core_init_vm(struct kvm *kvm)
1814{
Paul Mackerras32fad282012-05-04 02:32:53 +00001815 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001816
Paul Mackerras32fad282012-05-04 02:32:53 +00001817 /* Allocate the guest's logical partition ID */
1818
1819 lpid = kvmppc_alloc_lpid();
1820 if (lpid < 0)
1821 return -ENOMEM;
1822 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001823
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001824 /*
1825 * Since we don't flush the TLB when tearing down a VM,
1826 * and this lpid might have previously been used,
1827 * make sure we flush on each core before running the new VM.
1828 */
1829 cpumask_setall(&kvm->arch.need_tlb_flush);
1830
David Gibson54738c02011-06-29 00:22:41 +00001831 INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001832
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001833 kvm->arch.rma = NULL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001834
Paul Mackerras9e368f22011-06-29 00:40:08 +00001835 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001836
Paul Mackerras9e368f22011-06-29 00:40:08 +00001837 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1838 /* PPC970; HID4 is effectively the LPCR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001839 kvm->arch.host_lpid = 0;
1840 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
1841 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
1842 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
1843 ((lpid & 0xf) << HID4_LPID5_SH);
1844 } else {
1845 /* POWER7; init LPCR for virtual RMA mode */
1846 kvm->arch.host_lpid = mfspr(SPRN_LPID);
1847 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
1848 lpcr &= LPCR_PECE | LPCR_LPES;
1849 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
Paul Mackerras697d3892011-12-12 12:36:37 +00001850 LPCR_VPM0 | LPCR_VPM1;
1851 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
1852 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerras9e368f22011-06-29 00:40:08 +00001853 }
1854 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001855
Paul Mackerras342d3db2011-12-12 12:38:05 +00001856 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001857 spin_lock_init(&kvm->arch.slot_phys_lock);
Paul Mackerras512691d2012-10-15 01:15:41 +00001858
1859 /*
1860 * Don't allow secondary CPU threads to come online
1861 * while any KVM VMs exist.
1862 */
1863 inhibit_secondary_onlining();
1864
David Gibson54738c02011-06-29 00:22:41 +00001865 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001866}
1867
1868void kvmppc_core_destroy_vm(struct kvm *kvm)
1869{
Paul Mackerras512691d2012-10-15 01:15:41 +00001870 uninhibit_secondary_onlining();
1871
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001872 if (kvm->arch.rma) {
1873 kvm_release_rma(kvm->arch.rma);
1874 kvm->arch.rma = NULL;
1875 }
1876
Paul Mackerrasde56a942011-06-29 00:21:34 +00001877 kvmppc_free_hpt(kvm);
David Gibson54738c02011-06-29 00:22:41 +00001878 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
Paul Mackerrasde56a942011-06-29 00:21:34 +00001879}
1880
1881/* These are stubs for now */
1882void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
1883{
1884}
1885
1886/* We don't need to emulate any privileged instructions or dcbz */
1887int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
1888 unsigned int inst, int *advance)
1889{
1890 return EMULATE_FAIL;
1891}
1892
Alexander Graf54771e62012-05-04 14:55:12 +02001893int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001894{
1895 return EMULATE_FAIL;
1896}
1897
Alexander Graf54771e62012-05-04 14:55:12 +02001898int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001899{
1900 return EMULATE_FAIL;
1901}
1902
1903static int kvmppc_book3s_hv_init(void)
1904{
1905 int r;
1906
1907 r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1908
1909 if (r)
1910 return r;
1911
1912 r = kvmppc_mmu_hv_init();
1913
1914 return r;
1915}
1916
1917static void kvmppc_book3s_hv_exit(void)
1918{
1919 kvm_exit();
1920}
1921
1922module_init(kvmppc_book3s_hv_init);
1923module_exit(kvmppc_book3s_hv_exit);