blob: 36eb95cc48ae7516ed981c8713119cd888966747 [file] [log] [blame]
Paul Mackerrasde56a942011-06-29 00:21:34 +00001/*
2 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
3 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
4 *
5 * Authors:
6 * Paul Mackerras <paulus@au1.ibm.com>
7 * Alexander Graf <agraf@suse.de>
8 * Kevin Wolf <mail@kevin-wolf.de>
9 *
10 * Description: KVM functions specific to running on Book 3S
11 * processors in hypervisor mode (specifically POWER7 and later).
12 *
13 * This file is derived from arch/powerpc/kvm/book3s.c,
14 * by Alexander Graf <agraf@suse.de>.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License, version 2, as
18 * published by the Free Software Foundation.
19 */
20
21#include <linux/kvm_host.h>
22#include <linux/err.h>
23#include <linux/slab.h>
24#include <linux/preempt.h>
25#include <linux/sched.h>
26#include <linux/delay.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040027#include <linux/export.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000028#include <linux/fs.h>
29#include <linux/anon_inodes.h>
30#include <linux/cpumask.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000031#include <linux/spinlock.h>
32#include <linux/page-flags.h>
Paul Mackerras2c9097e2012-09-11 13:27:01 +000033#include <linux/srcu.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000034
35#include <asm/reg.h>
36#include <asm/cputable.h>
37#include <asm/cacheflush.h>
38#include <asm/tlbflush.h>
39#include <asm/uaccess.h>
40#include <asm/io.h>
41#include <asm/kvm_ppc.h>
42#include <asm/kvm_book3s.h>
43#include <asm/mmu_context.h>
44#include <asm/lppaca.h>
45#include <asm/processor.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000046#include <asm/cputhreads.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000047#include <asm/page.h>
Michael Neulingde1d9242011-11-09 20:39:49 +000048#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010049#include <asm/switch_to.h>
Paul Mackerras512691d2012-10-15 01:15:41 +000050#include <asm/smp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000051#include <linux/gfp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000052#include <linux/vmalloc.h>
53#include <linux/highmem.h>
Paul Mackerrasc77162d2011-12-12 12:31:00 +000054#include <linux/hugetlb.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000055
56/* #define EXIT_DEBUG */
57/* #define EXIT_DEBUG_SIMPLE */
58/* #define EXIT_DEBUG_INT */
59
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000060/* Used to indicate that a guest page fault needs to be handled */
61#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
62
Paul Mackerrasc7b67672012-10-15 01:18:07 +000063/* Used as a "null" value for timebase values */
64#define TB_NIL (~(u64)0)
65
Paul Mackerras19ccb762011-07-23 17:42:46 +100066static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +000067static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +100068
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +000069void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
70{
71 int me;
72 int cpu = vcpu->cpu;
73 wait_queue_head_t *wqp;
74
75 wqp = kvm_arch_vcpu_wq(vcpu);
76 if (waitqueue_active(wqp)) {
77 wake_up_interruptible(wqp);
78 ++vcpu->stat.halt_wakeup;
79 }
80
81 me = get_cpu();
82
83 /* CPU points to the first thread of the core */
84 if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
85 int real_cpu = cpu + vcpu->arch.ptid;
86 if (paca[real_cpu].kvm_hstate.xics_phys)
87 xics_wake_cpu(real_cpu);
88 else if (cpu_online(cpu))
89 smp_send_reschedule(cpu);
90 }
91 put_cpu();
92}
93
Paul Mackerrasc7b67672012-10-15 01:18:07 +000094/*
95 * We use the vcpu_load/put functions to measure stolen time.
96 * Stolen time is counted as time when either the vcpu is able to
97 * run as part of a virtual core, but the task running the vcore
98 * is preempted or sleeping, or when the vcpu needs something done
99 * in the kernel by the task running the vcpu, but that task is
100 * preempted or sleeping. Those two things have to be counted
101 * separately, since one of the vcpu tasks will take on the job
102 * of running the core, and the other vcpu tasks in the vcore will
103 * sleep waiting for it to do that, but that sleep shouldn't count
104 * as stolen time.
105 *
106 * Hence we accumulate stolen time when the vcpu can run as part of
107 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
108 * needs its task to do other things in the kernel (for example,
109 * service a page fault) in busy_stolen. We don't accumulate
110 * stolen time for a vcore when it is inactive, or for a vcpu
111 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
112 * a misnomer; it means that the vcpu task is not executing in
113 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
114 * the kernel. We don't have any way of dividing up that time
115 * between time that the vcpu is genuinely stopped, time that
116 * the task is actively working on behalf of the vcpu, and time
117 * that the task is preempted, so we don't count any of it as
118 * stolen.
119 *
120 * Updates to busy_stolen are protected by arch.tbacct_lock;
121 * updates to vc->stolen_tb are protected by the arch.tbacct_lock
122 * of the vcpu that has taken responsibility for running the vcore
123 * (i.e. vc->runner). The stolen times are measured in units of
124 * timebase ticks. (Note that the != TB_NIL checks below are
125 * purely defensive; they should never fail.)
126 */
127
Paul Mackerrasde56a942011-06-29 00:21:34 +0000128void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
129{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000130 struct kvmppc_vcore *vc = vcpu->arch.vcore;
131
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000132 spin_lock(&vcpu->arch.tbacct_lock);
133 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
134 vc->preempt_tb != TB_NIL) {
Paul Mackerras0456ec42012-02-03 00:56:21 +0000135 vc->stolen_tb += mftb() - vc->preempt_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000136 vc->preempt_tb = TB_NIL;
137 }
138 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
139 vcpu->arch.busy_preempt != TB_NIL) {
140 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
141 vcpu->arch.busy_preempt = TB_NIL;
142 }
143 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000144}
145
146void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
147{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000148 struct kvmppc_vcore *vc = vcpu->arch.vcore;
149
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000150 spin_lock(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000151 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
152 vc->preempt_tb = mftb();
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000153 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
154 vcpu->arch.busy_preempt = mftb();
155 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000156}
157
Paul Mackerrasde56a942011-06-29 00:21:34 +0000158void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
159{
160 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000161 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000162}
163
164void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
165{
166 vcpu->arch.pvr = pvr;
167}
168
169void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
170{
171 int r;
172
173 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
174 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
175 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
176 for (r = 0; r < 16; ++r)
177 pr_err("r%2d = %.16lx r%d = %.16lx\n",
178 r, kvmppc_get_gpr(vcpu, r),
179 r+16, kvmppc_get_gpr(vcpu, r+16));
180 pr_err("ctr = %.16lx lr = %.16lx\n",
181 vcpu->arch.ctr, vcpu->arch.lr);
182 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
183 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
184 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
185 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
186 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
187 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
188 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
189 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
190 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
191 pr_err("fault dar = %.16lx dsisr = %.8x\n",
192 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
193 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
194 for (r = 0; r < vcpu->arch.slb_max; ++r)
195 pr_err(" ESID = %.16llx VSID = %.16llx\n",
196 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
197 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000198 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000199 vcpu->arch.last_inst);
200}
201
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000202struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
203{
204 int r;
205 struct kvm_vcpu *v, *ret = NULL;
206
207 mutex_lock(&kvm->lock);
208 kvm_for_each_vcpu(r, v, kvm) {
209 if (v->vcpu_id == id) {
210 ret = v;
211 break;
212 }
213 }
214 mutex_unlock(&kvm->lock);
215 return ret;
216}
217
218static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
219{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000220 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000221 vpa->yield_count = 1;
222}
223
Paul Mackerras55b665b2012-09-25 20:33:06 +0000224static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
225 unsigned long addr, unsigned long len)
226{
227 /* check address is cacheline aligned */
228 if (addr & (L1_CACHE_BYTES - 1))
229 return -EINVAL;
230 spin_lock(&vcpu->arch.vpa_update_lock);
231 if (v->next_gpa != addr || v->len != len) {
232 v->next_gpa = addr;
233 v->len = addr ? len : 0;
234 v->update_pending = 1;
235 }
236 spin_unlock(&vcpu->arch.vpa_update_lock);
237 return 0;
238}
239
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000240/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
241struct reg_vpa {
242 u32 dummy;
243 union {
244 u16 hword;
245 u32 word;
246 } length;
247};
248
249static int vpa_is_registered(struct kvmppc_vpa *vpap)
250{
251 if (vpap->update_pending)
252 return vpap->next_gpa != 0;
253 return vpap->pinned_addr != NULL;
254}
255
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000256static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
257 unsigned long flags,
258 unsigned long vcpuid, unsigned long vpa)
259{
260 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000261 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000262 void *va;
263 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000264 int err;
265 int subfunc;
266 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000267
268 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
269 if (!tvcpu)
270 return H_PARAMETER;
271
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000272 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
273 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
274 subfunc == H_VPA_REG_SLB) {
275 /* Registering new area - address must be cache-line aligned */
276 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000277 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000278
279 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000280 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
281 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000282 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000283 if (subfunc == H_VPA_REG_VPA)
284 len = ((struct reg_vpa *)va)->length.hword;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000285 else
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000286 len = ((struct reg_vpa *)va)->length.word;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000287 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000288
289 /* Check length */
290 if (len > nb || len < sizeof(struct reg_vpa))
291 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000292 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000293 vpa = 0;
294 len = 0;
295 }
296
297 err = H_PARAMETER;
298 vpap = NULL;
299 spin_lock(&tvcpu->arch.vpa_update_lock);
300
301 switch (subfunc) {
302 case H_VPA_REG_VPA: /* register VPA */
303 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000304 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000305 vpap = &tvcpu->arch.vpa;
306 err = 0;
307 break;
308
309 case H_VPA_REG_DTL: /* register DTL */
310 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000311 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000312 len -= len % sizeof(struct dtl_entry);
313
314 /* Check that they have previously registered a VPA */
315 err = H_RESOURCE;
316 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000317 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000318
319 vpap = &tvcpu->arch.dtl;
320 err = 0;
321 break;
322
323 case H_VPA_REG_SLB: /* register SLB shadow buffer */
324 /* Check that they have previously registered a VPA */
325 err = H_RESOURCE;
326 if (!vpa_is_registered(&tvcpu->arch.vpa))
327 break;
328
329 vpap = &tvcpu->arch.slb_shadow;
330 err = 0;
331 break;
332
333 case H_VPA_DEREG_VPA: /* deregister VPA */
334 /* Check they don't still have a DTL or SLB buf registered */
335 err = H_RESOURCE;
336 if (vpa_is_registered(&tvcpu->arch.dtl) ||
337 vpa_is_registered(&tvcpu->arch.slb_shadow))
338 break;
339
340 vpap = &tvcpu->arch.vpa;
341 err = 0;
342 break;
343
344 case H_VPA_DEREG_DTL: /* deregister DTL */
345 vpap = &tvcpu->arch.dtl;
346 err = 0;
347 break;
348
349 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
350 vpap = &tvcpu->arch.slb_shadow;
351 err = 0;
352 break;
353 }
354
355 if (vpap) {
356 vpap->next_gpa = vpa;
357 vpap->len = len;
358 vpap->update_pending = 1;
359 }
360
361 spin_unlock(&tvcpu->arch.vpa_update_lock);
362
363 return err;
364}
365
Paul Mackerras081f3232012-06-01 20:20:24 +1000366static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000367{
Paul Mackerras081f3232012-06-01 20:20:24 +1000368 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000369 void *va;
370 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000371 unsigned long gpa;
372
373 /*
374 * We need to pin the page pointed to by vpap->next_gpa,
375 * but we can't call kvmppc_pin_guest_page under the lock
376 * as it does get_user_pages() and down_read(). So we
377 * have to drop the lock, pin the page, then get the lock
378 * again and check that a new area didn't get registered
379 * in the meantime.
380 */
381 for (;;) {
382 gpa = vpap->next_gpa;
383 spin_unlock(&vcpu->arch.vpa_update_lock);
384 va = NULL;
385 nb = 0;
386 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000387 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000388 spin_lock(&vcpu->arch.vpa_update_lock);
389 if (gpa == vpap->next_gpa)
390 break;
391 /* sigh... unpin that one and try again */
392 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000393 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000394 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000395
396 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000397 if (va && nb < vpap->len) {
398 /*
399 * If it's now too short, it must be that userspace
400 * has changed the mappings underlying guest memory,
401 * so unregister the region.
402 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000403 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000404 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000405 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000406 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000407 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
408 vpap->dirty);
409 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000410 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000411 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000412 if (va)
413 vpap->pinned_end = va + vpap->len;
414}
Paul Mackerras93e60242011-12-12 12:28:55 +0000415
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000416static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
417{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000418 if (!(vcpu->arch.vpa.update_pending ||
419 vcpu->arch.slb_shadow.update_pending ||
420 vcpu->arch.dtl.update_pending))
421 return;
422
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000423 spin_lock(&vcpu->arch.vpa_update_lock);
424 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000425 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000426 if (vcpu->arch.vpa.pinned_addr)
427 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000428 }
429 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000430 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000431 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
432 vcpu->arch.dtl_index = 0;
433 }
434 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000435 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000436 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000437}
438
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000439/*
440 * Return the accumulated stolen time for the vcore up until `now'.
441 * The caller should hold the vcore lock.
442 */
443static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
444{
445 u64 p;
446
447 /*
448 * If we are the task running the vcore, then since we hold
449 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
450 * can't be updated, so we don't need the tbacct_lock.
451 * If the vcore is inactive, it can't become active (since we
452 * hold the vcore lock), so the vcpu load/put functions won't
453 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
454 */
455 if (vc->vcore_state != VCORE_INACTIVE &&
456 vc->runner->arch.run_task != current) {
457 spin_lock(&vc->runner->arch.tbacct_lock);
458 p = vc->stolen_tb;
459 if (vc->preempt_tb != TB_NIL)
460 p += now - vc->preempt_tb;
461 spin_unlock(&vc->runner->arch.tbacct_lock);
462 } else {
463 p = vc->stolen_tb;
464 }
465 return p;
466}
467
Paul Mackerras0456ec42012-02-03 00:56:21 +0000468static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
469 struct kvmppc_vcore *vc)
470{
471 struct dtl_entry *dt;
472 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000473 unsigned long stolen;
474 unsigned long core_stolen;
475 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000476
477 dt = vcpu->arch.dtl_ptr;
478 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000479 now = mftb();
480 core_stolen = vcore_stolen_time(vc, now);
481 stolen = core_stolen - vcpu->arch.stolen_logged;
482 vcpu->arch.stolen_logged = core_stolen;
483 spin_lock(&vcpu->arch.tbacct_lock);
484 stolen += vcpu->arch.busy_stolen;
485 vcpu->arch.busy_stolen = 0;
486 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000487 if (!dt || !vpa)
488 return;
489 memset(dt, 0, sizeof(struct dtl_entry));
490 dt->dispatch_reason = 7;
491 dt->processor_id = vc->pcpu + vcpu->arch.ptid;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000492 dt->timebase = now + vc->tb_offset;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000493 dt->enqueue_to_dispatch_time = stolen;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000494 dt->srr0 = kvmppc_get_pc(vcpu);
495 dt->srr1 = vcpu->arch.shregs.msr;
496 ++dt;
497 if (dt == vcpu->arch.dtl.pinned_end)
498 dt = vcpu->arch.dtl.pinned_addr;
499 vcpu->arch.dtl_ptr = dt;
500 /* order writing *dt vs. writing vpa->dtl_idx */
501 smp_wmb();
502 vpa->dtl_idx = ++vcpu->arch.dtl_index;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000503 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000504}
505
506int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
507{
508 unsigned long req = kvmppc_get_gpr(vcpu, 3);
509 unsigned long target, ret = H_SUCCESS;
510 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000511 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000512
513 switch (req) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000514 case H_ENTER:
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000515 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000516 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
517 kvmppc_get_gpr(vcpu, 5),
518 kvmppc_get_gpr(vcpu, 6),
519 kvmppc_get_gpr(vcpu, 7));
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000520 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000521 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000522 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000523 break;
524 case H_PROD:
525 target = kvmppc_get_gpr(vcpu, 4);
526 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
527 if (!tvcpu) {
528 ret = H_PARAMETER;
529 break;
530 }
531 tvcpu->arch.prodded = 1;
532 smp_mb();
533 if (vcpu->arch.ceded) {
534 if (waitqueue_active(&vcpu->wq)) {
535 wake_up_interruptible(&vcpu->wq);
536 vcpu->stat.halt_wakeup++;
537 }
538 }
539 break;
540 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +1000541 target = kvmppc_get_gpr(vcpu, 4);
542 if (target == -1)
543 break;
544 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
545 if (!tvcpu) {
546 ret = H_PARAMETER;
547 break;
548 }
549 kvm_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000550 break;
551 case H_REGISTER_VPA:
552 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
553 kvmppc_get_gpr(vcpu, 5),
554 kvmppc_get_gpr(vcpu, 6));
555 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000556 case H_RTAS:
557 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
558 return RESUME_HOST;
559
560 rc = kvmppc_rtas_hcall(vcpu);
561
562 if (rc == -ENOENT)
563 return RESUME_HOST;
564 else if (rc == 0)
565 break;
566
567 /* Send the error out to userspace via KVM_RUN */
568 return rc;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000569
570 case H_XIRR:
571 case H_CPPR:
572 case H_EOI:
573 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000574 case H_IPOLL:
575 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000576 if (kvmppc_xics_enabled(vcpu)) {
577 ret = kvmppc_xics_hcall(vcpu, req);
578 break;
579 } /* fallthrough */
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000580 default:
581 return RESUME_HOST;
582 }
583 kvmppc_set_gpr(vcpu, 3, ret);
584 vcpu->arch.hcall_needed = 0;
585 return RESUME_GUEST;
586}
587
Paul Mackerrasde56a942011-06-29 00:21:34 +0000588static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
589 struct task_struct *tsk)
590{
591 int r = RESUME_HOST;
592
593 vcpu->stat.sum_exits++;
594
595 run->exit_reason = KVM_EXIT_UNKNOWN;
596 run->ready_for_interrupt_injection = 1;
597 switch (vcpu->arch.trap) {
598 /* We're good on these - the host merely wanted to get our attention */
599 case BOOK3S_INTERRUPT_HV_DECREMENTER:
600 vcpu->stat.dec_exits++;
601 r = RESUME_GUEST;
602 break;
603 case BOOK3S_INTERRUPT_EXTERNAL:
604 vcpu->stat.ext_intr_exits++;
605 r = RESUME_GUEST;
606 break;
607 case BOOK3S_INTERRUPT_PERFMON:
608 r = RESUME_GUEST;
609 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000610 case BOOK3S_INTERRUPT_MACHINE_CHECK:
611 /*
612 * Deliver a machine check interrupt to the guest.
613 * We have to do this, even if the host has handled the
614 * machine check, because machine checks use SRR0/1 and
615 * the interrupt might have trashed guest state in them.
616 */
617 kvmppc_book3s_queue_irqprio(vcpu,
618 BOOK3S_INTERRUPT_MACHINE_CHECK);
619 r = RESUME_GUEST;
620 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000621 case BOOK3S_INTERRUPT_PROGRAM:
622 {
623 ulong flags;
624 /*
625 * Normally program interrupts are delivered directly
626 * to the guest by the hardware, but we can get here
627 * as a result of a hypervisor emulation interrupt
628 * (e40) getting turned into a 700 by BML RTAS.
629 */
630 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
631 kvmppc_core_queue_program(vcpu, flags);
632 r = RESUME_GUEST;
633 break;
634 }
635 case BOOK3S_INTERRUPT_SYSCALL:
636 {
637 /* hcall - punt to userspace */
638 int i;
639
640 if (vcpu->arch.shregs.msr & MSR_PR) {
641 /* sc 1 from userspace - reflect to guest syscall */
642 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
643 r = RESUME_GUEST;
644 break;
645 }
646 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
647 for (i = 0; i < 9; ++i)
648 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
649 run->exit_reason = KVM_EXIT_PAPR_HCALL;
650 vcpu->arch.hcall_needed = 1;
651 r = RESUME_HOST;
652 break;
653 }
654 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000655 * We get these next two if the guest accesses a page which it thinks
656 * it has mapped but which is not actually present, either because
657 * it is for an emulated I/O device or because the corresonding
658 * host page has been paged out. Any other HDSI/HISI interrupts
659 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000660 */
661 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000662 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000663 break;
664 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000665 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
666 vcpu->arch.fault_dsisr = 0;
667 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000668 break;
669 /*
670 * This occurs if the guest executes an illegal instruction.
671 * We just generate a program interrupt to the guest, since
672 * we don't emulate any guest instructions at this stage.
673 */
674 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
675 kvmppc_core_queue_program(vcpu, 0x80000);
676 r = RESUME_GUEST;
677 break;
678 default:
679 kvmppc_dump_regs(vcpu);
680 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
681 vcpu->arch.trap, kvmppc_get_pc(vcpu),
682 vcpu->arch.shregs.msr);
683 r = RESUME_HOST;
684 BUG();
685 break;
686 }
687
Paul Mackerrasde56a942011-06-29 00:21:34 +0000688 return r;
689}
690
691int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
Aneesh Kumar K.V87916442013-08-22 17:08:39 +0530692 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000693{
694 int i;
695
Paul Mackerrasde56a942011-06-29 00:21:34 +0000696 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +0530697 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000698 for (i = 0; i < vcpu->arch.slb_max; i++) {
699 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
700 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
701 }
702
703 return 0;
704}
705
706int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
Aneesh Kumar K.V87916442013-08-22 17:08:39 +0530707 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000708{
709 int i, j;
710
711 kvmppc_set_pvr(vcpu, sregs->pvr);
712
713 j = 0;
714 for (i = 0; i < vcpu->arch.slb_nr; i++) {
715 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
716 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
717 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
718 ++j;
719 }
720 }
721 vcpu->arch.slb_max = j;
722
723 return 0;
724}
725
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000726static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr)
727{
728 struct kvmppc_vcore *vc = vcpu->arch.vcore;
729 u64 mask;
730
731 spin_lock(&vc->lock);
732 /*
733 * Userspace can only modify DPFD (default prefetch depth),
734 * ILE (interrupt little-endian) and TC (translation control).
735 */
736 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
737 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
738 spin_unlock(&vc->lock);
739}
740
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000741int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000742{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000743 int r = 0;
744 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +0000745
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000746 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000747 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000748 *val = get_reg_val(id, 0);
749 break;
750 case KVM_REG_PPC_DABR:
751 *val = get_reg_val(id, vcpu->arch.dabr);
752 break;
753 case KVM_REG_PPC_DSCR:
754 *val = get_reg_val(id, vcpu->arch.dscr);
755 break;
756 case KVM_REG_PPC_PURR:
757 *val = get_reg_val(id, vcpu->arch.purr);
758 break;
759 case KVM_REG_PPC_SPURR:
760 *val = get_reg_val(id, vcpu->arch.spurr);
761 break;
762 case KVM_REG_PPC_AMR:
763 *val = get_reg_val(id, vcpu->arch.amr);
764 break;
765 case KVM_REG_PPC_UAMOR:
766 *val = get_reg_val(id, vcpu->arch.uamor);
767 break;
768 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
769 i = id - KVM_REG_PPC_MMCR0;
770 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
771 break;
772 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
773 i = id - KVM_REG_PPC_PMC1;
774 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +0000775 break;
Paul Mackerras14941782013-09-06 13:11:18 +1000776 case KVM_REG_PPC_SIAR:
777 *val = get_reg_val(id, vcpu->arch.siar);
778 break;
779 case KVM_REG_PPC_SDAR:
780 *val = get_reg_val(id, vcpu->arch.sdar);
781 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000782#ifdef CONFIG_VSX
783 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
784 if (cpu_has_feature(CPU_FTR_VSX)) {
785 /* VSX => FP reg i is stored in arch.vsr[2*i] */
786 long int i = id - KVM_REG_PPC_FPR0;
787 *val = get_reg_val(id, vcpu->arch.vsr[2 * i]);
788 } else {
789 /* let generic code handle it */
790 r = -EINVAL;
791 }
792 break;
793 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
794 if (cpu_has_feature(CPU_FTR_VSX)) {
795 long int i = id - KVM_REG_PPC_VSR0;
796 val->vsxval[0] = vcpu->arch.vsr[2 * i];
797 val->vsxval[1] = vcpu->arch.vsr[2 * i + 1];
798 } else {
799 r = -ENXIO;
800 }
801 break;
802#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000803 case KVM_REG_PPC_VPA_ADDR:
804 spin_lock(&vcpu->arch.vpa_update_lock);
805 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
806 spin_unlock(&vcpu->arch.vpa_update_lock);
807 break;
808 case KVM_REG_PPC_VPA_SLB:
809 spin_lock(&vcpu->arch.vpa_update_lock);
810 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
811 val->vpaval.length = vcpu->arch.slb_shadow.len;
812 spin_unlock(&vcpu->arch.vpa_update_lock);
813 break;
814 case KVM_REG_PPC_VPA_DTL:
815 spin_lock(&vcpu->arch.vpa_update_lock);
816 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
817 val->vpaval.length = vcpu->arch.dtl.len;
818 spin_unlock(&vcpu->arch.vpa_update_lock);
819 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000820 case KVM_REG_PPC_TB_OFFSET:
821 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
822 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000823 case KVM_REG_PPC_LPCR:
824 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
825 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000826 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000827 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000828 break;
829 }
830
831 return r;
832}
833
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000834int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000835{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000836 int r = 0;
837 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +0000838 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +0000839
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000840 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000841 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +0000842 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000843 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +0000844 r = -EINVAL;
845 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000846 case KVM_REG_PPC_DABR:
847 vcpu->arch.dabr = set_reg_val(id, *val);
848 break;
849 case KVM_REG_PPC_DSCR:
850 vcpu->arch.dscr = set_reg_val(id, *val);
851 break;
852 case KVM_REG_PPC_PURR:
853 vcpu->arch.purr = set_reg_val(id, *val);
854 break;
855 case KVM_REG_PPC_SPURR:
856 vcpu->arch.spurr = set_reg_val(id, *val);
857 break;
858 case KVM_REG_PPC_AMR:
859 vcpu->arch.amr = set_reg_val(id, *val);
860 break;
861 case KVM_REG_PPC_UAMOR:
862 vcpu->arch.uamor = set_reg_val(id, *val);
863 break;
864 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
865 i = id - KVM_REG_PPC_MMCR0;
866 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
867 break;
868 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
869 i = id - KVM_REG_PPC_PMC1;
870 vcpu->arch.pmc[i] = set_reg_val(id, *val);
871 break;
Paul Mackerras14941782013-09-06 13:11:18 +1000872 case KVM_REG_PPC_SIAR:
873 vcpu->arch.siar = set_reg_val(id, *val);
874 break;
875 case KVM_REG_PPC_SDAR:
876 vcpu->arch.sdar = set_reg_val(id, *val);
877 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000878#ifdef CONFIG_VSX
879 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
880 if (cpu_has_feature(CPU_FTR_VSX)) {
881 /* VSX => FP reg i is stored in arch.vsr[2*i] */
882 long int i = id - KVM_REG_PPC_FPR0;
883 vcpu->arch.vsr[2 * i] = set_reg_val(id, *val);
884 } else {
885 /* let generic code handle it */
886 r = -EINVAL;
887 }
888 break;
889 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
890 if (cpu_has_feature(CPU_FTR_VSX)) {
891 long int i = id - KVM_REG_PPC_VSR0;
892 vcpu->arch.vsr[2 * i] = val->vsxval[0];
893 vcpu->arch.vsr[2 * i + 1] = val->vsxval[1];
894 } else {
895 r = -ENXIO;
896 }
897 break;
898#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000899 case KVM_REG_PPC_VPA_ADDR:
900 addr = set_reg_val(id, *val);
901 r = -EINVAL;
902 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
903 vcpu->arch.dtl.next_gpa))
904 break;
905 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
906 break;
907 case KVM_REG_PPC_VPA_SLB:
908 addr = val->vpaval.addr;
909 len = val->vpaval.length;
910 r = -EINVAL;
911 if (addr && !vcpu->arch.vpa.next_gpa)
912 break;
913 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
914 break;
915 case KVM_REG_PPC_VPA_DTL:
916 addr = val->vpaval.addr;
917 len = val->vpaval.length;
918 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +0000919 if (addr && (len < sizeof(struct dtl_entry) ||
920 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +0000921 break;
922 len -= len % sizeof(struct dtl_entry);
923 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
924 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000925 case KVM_REG_PPC_TB_OFFSET:
926 /* round up to multiple of 2^24 */
927 vcpu->arch.vcore->tb_offset =
928 ALIGN(set_reg_val(id, *val), 1UL << 24);
929 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000930 case KVM_REG_PPC_LPCR:
931 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val));
932 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000933 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000934 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000935 break;
936 }
937
938 return r;
939}
940
Paul Mackerrasde56a942011-06-29 00:21:34 +0000941int kvmppc_core_check_processor_compat(void)
942{
Paul Mackerras9e368f22011-06-29 00:40:08 +0000943 if (cpu_has_feature(CPU_FTR_HVMODE))
Paul Mackerrasde56a942011-06-29 00:21:34 +0000944 return 0;
945 return -EIO;
946}
947
948struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
949{
950 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000951 int err = -EINVAL;
952 int core;
953 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000954
Paul Mackerras371fefd2011-06-29 00:23:08 +0000955 core = id / threads_per_core;
956 if (core >= KVM_MAX_VCORES)
957 goto out;
958
959 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200960 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000961 if (!vcpu)
962 goto out;
963
964 err = kvm_vcpu_init(vcpu, kvm, id);
965 if (err)
966 goto free_vcpu;
967
968 vcpu->arch.shared = &vcpu->arch.shregs;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000969 vcpu->arch.mmcr[0] = MMCR0_FC;
970 vcpu->arch.ctrl = CTRL_RUNLATCH;
971 /* default to host PVR, since we can't spoof it */
972 vcpu->arch.pvr = mfspr(SPRN_PVR);
973 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000974 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000975 spin_lock_init(&vcpu->arch.tbacct_lock);
976 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000977
Paul Mackerrasde56a942011-06-29 00:21:34 +0000978 kvmppc_mmu_book3s_hv_init(vcpu);
979
Paul Mackerras8455d792012-10-15 01:17:42 +0000980 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000981
982 init_waitqueue_head(&vcpu->arch.cpu_run);
983
984 mutex_lock(&kvm->lock);
985 vcore = kvm->arch.vcores[core];
986 if (!vcore) {
987 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
988 if (vcore) {
989 INIT_LIST_HEAD(&vcore->runnable_threads);
990 spin_lock_init(&vcore->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000991 init_waitqueue_head(&vcore->wq);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000992 vcore->preempt_tb = TB_NIL;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000993 vcore->lpcr = kvm->arch.lpcr;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000994 }
995 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000996 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000997 }
998 mutex_unlock(&kvm->lock);
999
1000 if (!vcore)
1001 goto free_vcpu;
1002
1003 spin_lock(&vcore->lock);
1004 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001005 spin_unlock(&vcore->lock);
1006 vcpu->arch.vcore = vcore;
1007
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001008 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1009 kvmppc_sanity_check(vcpu);
1010
Paul Mackerrasde56a942011-06-29 00:21:34 +00001011 return vcpu;
1012
1013free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001014 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001015out:
1016 return ERR_PTR(err);
1017}
1018
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001019static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1020{
1021 if (vpa->pinned_addr)
1022 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1023 vpa->dirty);
1024}
1025
Paul Mackerrasde56a942011-06-29 00:21:34 +00001026void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1027{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001028 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001029 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1030 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1031 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001032 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001033 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001034 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001035}
1036
Paul Mackerras19ccb762011-07-23 17:42:46 +10001037static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001038{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001039 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001040
Paul Mackerras19ccb762011-07-23 17:42:46 +10001041 now = get_tb();
1042 if (now > vcpu->arch.dec_expires) {
1043 /* decrementer has already gone negative */
1044 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001045 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001046 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001047 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001048 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1049 / tb_ticks_per_sec;
1050 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1051 HRTIMER_MODE_REL);
1052 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001053}
1054
Paul Mackerras19ccb762011-07-23 17:42:46 +10001055static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001056{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001057 vcpu->arch.ceded = 0;
1058 if (vcpu->arch.timer_running) {
1059 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1060 vcpu->arch.timer_running = 0;
1061 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001062}
1063
1064extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001065
1066static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1067 struct kvm_vcpu *vcpu)
1068{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001069 u64 now;
1070
Paul Mackerras371fefd2011-06-29 00:23:08 +00001071 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1072 return;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001073 spin_lock(&vcpu->arch.tbacct_lock);
1074 now = mftb();
1075 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1076 vcpu->arch.stolen_logged;
1077 vcpu->arch.busy_preempt = now;
1078 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1079 spin_unlock(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001080 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001081 list_del(&vcpu->arch.run_list);
1082}
1083
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001084static int kvmppc_grab_hwthread(int cpu)
1085{
1086 struct paca_struct *tpaca;
1087 long timeout = 1000;
1088
1089 tpaca = &paca[cpu];
1090
1091 /* Ensure the thread won't go into the kernel if it wakes */
1092 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001093 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001094
1095 /*
1096 * If the thread is already executing in the kernel (e.g. handling
1097 * a stray interrupt), wait for it to get back to nap mode.
1098 * The smp_mb() is to ensure that our setting of hwthread_req
1099 * is visible before we look at hwthread_state, so if this
1100 * races with the code at system_reset_pSeries and the thread
1101 * misses our setting of hwthread_req, we are sure to see its
1102 * setting of hwthread_state, and vice versa.
1103 */
1104 smp_mb();
1105 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1106 if (--timeout <= 0) {
1107 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1108 return -EBUSY;
1109 }
1110 udelay(1);
1111 }
1112 return 0;
1113}
1114
1115static void kvmppc_release_hwthread(int cpu)
1116{
1117 struct paca_struct *tpaca;
1118
1119 tpaca = &paca[cpu];
1120 tpaca->kvm_hstate.hwthread_req = 0;
1121 tpaca->kvm_hstate.kvm_vcpu = NULL;
1122}
1123
Paul Mackerras371fefd2011-06-29 00:23:08 +00001124static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1125{
1126 int cpu;
1127 struct paca_struct *tpaca;
1128 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1129
Paul Mackerras19ccb762011-07-23 17:42:46 +10001130 if (vcpu->arch.timer_running) {
1131 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1132 vcpu->arch.timer_running = 0;
1133 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001134 cpu = vc->pcpu + vcpu->arch.ptid;
1135 tpaca = &paca[cpu];
1136 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1137 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001138 tpaca->kvm_hstate.napping = 0;
1139 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001140 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +00001141#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001142 if (vcpu->arch.ptid) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001143 xics_wake_cpu(cpu);
1144 ++vc->n_woken;
1145 }
1146#endif
1147}
1148
1149static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1150{
1151 int i;
1152
1153 HMT_low();
1154 i = 0;
1155 while (vc->nap_count < vc->n_woken) {
1156 if (++i >= 1000000) {
1157 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1158 vc->nap_count, vc->n_woken);
1159 break;
1160 }
1161 cpu_relax();
1162 }
1163 HMT_medium();
1164}
1165
1166/*
1167 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001168 * this core are off-line. Then grab the threads so they can't
1169 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001170 */
1171static int on_primary_thread(void)
1172{
1173 int cpu = smp_processor_id();
1174 int thr = cpu_thread_in_core(cpu);
1175
1176 if (thr)
1177 return 0;
1178 while (++thr < threads_per_core)
1179 if (cpu_online(cpu + thr))
1180 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001181
1182 /* Grab all hw threads so they can't go into the kernel */
1183 for (thr = 1; thr < threads_per_core; ++thr) {
1184 if (kvmppc_grab_hwthread(cpu + thr)) {
1185 /* Couldn't grab one; let the others go */
1186 do {
1187 kvmppc_release_hwthread(cpu + thr);
1188 } while (--thr > 0);
1189 return 0;
1190 }
1191 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001192 return 1;
1193}
1194
1195/*
1196 * Run a set of guest threads on a physical core.
1197 * Called with vc->lock held.
1198 */
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001199static void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001200{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001201 struct kvm_vcpu *vcpu, *vcpu0, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001202 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001203 u64 now;
Paul Mackerras081f3232012-06-01 20:20:24 +10001204 int ptid, i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001205 int srcu_idx;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001206 struct kvm_vcpu *vcpus_to_update[threads_per_core];
Paul Mackerrasde56a942011-06-29 00:21:34 +00001207
Paul Mackerras371fefd2011-06-29 00:23:08 +00001208 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +10001209 need_vpa_update = 0;
1210 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001211 if (signal_pending(vcpu->arch.run_task))
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001212 return;
1213 if (vcpu->arch.vpa.update_pending ||
1214 vcpu->arch.slb_shadow.update_pending ||
1215 vcpu->arch.dtl.update_pending)
1216 vcpus_to_update[need_vpa_update++] = vcpu;
Paul Mackerras081f3232012-06-01 20:20:24 +10001217 }
1218
1219 /*
1220 * Initialize *vc, in particular vc->vcore_state, so we can
1221 * drop the vcore lock if necessary.
1222 */
1223 vc->n_woken = 0;
1224 vc->nap_count = 0;
1225 vc->entry_exit_count = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001226 vc->vcore_state = VCORE_STARTING;
Paul Mackerras081f3232012-06-01 20:20:24 +10001227 vc->in_guest = 0;
1228 vc->napping_threads = 0;
1229
1230 /*
1231 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1232 * which can't be called with any spinlocks held.
1233 */
1234 if (need_vpa_update) {
1235 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001236 for (i = 0; i < need_vpa_update; ++i)
1237 kvmppc_update_vpas(vcpus_to_update[i]);
Paul Mackerras081f3232012-06-01 20:20:24 +10001238 spin_lock(&vc->lock);
1239 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001240
1241 /*
Paul Mackerras19ccb762011-07-23 17:42:46 +10001242 * Assign physical thread IDs, first to non-ceded vcpus
1243 * and then to ceded ones.
1244 */
1245 ptid = 0;
1246 vcpu0 = NULL;
1247 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1248 if (!vcpu->arch.ceded) {
1249 if (!ptid)
1250 vcpu0 = vcpu;
1251 vcpu->arch.ptid = ptid++;
1252 }
1253 }
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001254 if (!vcpu0)
1255 goto out; /* nothing to run; should never happen */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001256 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1257 if (vcpu->arch.ceded)
1258 vcpu->arch.ptid = ptid++;
1259
Paul Mackerras7b444c62012-10-15 01:16:14 +00001260 /*
1261 * Make sure we are running on thread 0, and that
1262 * secondary threads are offline.
1263 */
1264 if (threads_per_core > 1 && !on_primary_thread()) {
1265 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1266 vcpu->arch.ret = -EBUSY;
1267 goto out;
1268 }
1269
Paul Mackerras371fefd2011-06-29 00:23:08 +00001270 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001271 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001272 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001273 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001274 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001275
Paul Mackerras2f12f032012-10-15 01:17:17 +00001276 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001277 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001278 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001279
Paul Mackerras19ccb762011-07-23 17:42:46 +10001280 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001281
1282 srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
1283
Paul Mackerras19ccb762011-07-23 17:42:46 +10001284 __kvmppc_vcore_entry(NULL, vcpu0);
1285
Paul Mackerras371fefd2011-06-29 00:23:08 +00001286 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001287 /* disable sending of IPIs on virtual external irqs */
1288 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1289 vcpu->cpu = -1;
1290 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001291 if (vc->nap_count < vc->n_woken)
1292 kvmppc_wait_for_nap(vc);
Paul Mackerras2f12f032012-10-15 01:17:17 +00001293 for (i = 0; i < threads_per_core; ++i)
1294 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001295 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001296 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001297 spin_unlock(&vc->lock);
1298
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001299 srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
1300
Paul Mackerras371fefd2011-06-29 00:23:08 +00001301 /* make sure updates to secondary vcpu structs are visible now */
1302 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001303 kvm_guest_exit();
1304
1305 preempt_enable();
1306 kvm_resched(vcpu);
1307
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001308 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001309 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001310 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1311 /* cancel pending dec exception if dec is positive */
1312 if (now < vcpu->arch.dec_expires &&
1313 kvmppc_core_pending_dec(vcpu))
1314 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001315
1316 ret = RESUME_GUEST;
1317 if (vcpu->arch.trap)
1318 ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
1319 vcpu->arch.run_task);
1320
Paul Mackerras371fefd2011-06-29 00:23:08 +00001321 vcpu->arch.ret = ret;
1322 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001323
1324 if (vcpu->arch.ceded) {
1325 if (ret != RESUME_GUEST)
1326 kvmppc_end_cede(vcpu);
1327 else
1328 kvmppc_set_timer(vcpu);
1329 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001330 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001331
Paul Mackerrasde56a942011-06-29 00:21:34 +00001332 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001333 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001334 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1335 arch.run_list) {
1336 if (vcpu->arch.ret != RESUME_GUEST) {
1337 kvmppc_remove_runnable(vc, vcpu);
1338 wake_up(&vcpu->arch.cpu_run);
1339 }
1340 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001341}
1342
Paul Mackerras19ccb762011-07-23 17:42:46 +10001343/*
1344 * Wait for some other vcpu thread to execute us, and
1345 * wake us up when we need to handle something in the host.
1346 */
1347static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001348{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001349 DEFINE_WAIT(wait);
1350
Paul Mackerras19ccb762011-07-23 17:42:46 +10001351 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1352 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1353 schedule();
1354 finish_wait(&vcpu->arch.cpu_run, &wait);
1355}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001356
Paul Mackerras19ccb762011-07-23 17:42:46 +10001357/*
1358 * All the vcpus in this vcore are idle, so wait for a decrementer
1359 * or external interrupt to one of the vcpus. vc->lock is held.
1360 */
1361static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1362{
1363 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001364
1365 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1366 vc->vcore_state = VCORE_SLEEPING;
1367 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001368 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001369 finish_wait(&vc->wq, &wait);
1370 spin_lock(&vc->lock);
1371 vc->vcore_state = VCORE_INACTIVE;
1372}
1373
1374static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1375{
1376 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001377 struct kvmppc_vcore *vc;
1378 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001379
Paul Mackerras371fefd2011-06-29 00:23:08 +00001380 kvm_run->exit_reason = 0;
1381 vcpu->arch.ret = RESUME_GUEST;
1382 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001383 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001384
Paul Mackerras371fefd2011-06-29 00:23:08 +00001385 /*
1386 * Synchronize with other threads in this virtual core
1387 */
1388 vc = vcpu->arch.vcore;
1389 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001390 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001391 vcpu->arch.run_task = current;
1392 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001393 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10001394 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001395 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001396 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1397 ++vc->n_runnable;
1398
Paul Mackerras19ccb762011-07-23 17:42:46 +10001399 /*
1400 * This happens the first time this is called for a vcpu.
1401 * If the vcore is already running, we may be able to start
1402 * this thread straight away and have it join in.
1403 */
Paul Mackerras8455d792012-10-15 01:17:42 +00001404 if (!signal_pending(current)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001405 if (vc->vcore_state == VCORE_RUNNING &&
1406 VCORE_EXIT_COUNT(vc) == 0) {
1407 vcpu->arch.ptid = vc->n_runnable - 1;
Paul Mackerras2f12f032012-10-15 01:17:17 +00001408 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001409 kvmppc_start_thread(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00001410 } else if (vc->vcore_state == VCORE_SLEEPING) {
1411 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001412 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001413
Paul Mackerras8455d792012-10-15 01:17:42 +00001414 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001415
1416 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1417 !signal_pending(current)) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001418 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10001419 spin_unlock(&vc->lock);
1420 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1421 spin_lock(&vc->lock);
1422 continue;
1423 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001424 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1425 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001426 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001427 if (signal_pending(v->arch.run_task)) {
1428 kvmppc_remove_runnable(vc, v);
1429 v->stat.signal_exits++;
1430 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1431 v->arch.ret = -EINTR;
1432 wake_up(&v->arch.cpu_run);
1433 }
1434 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001435 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1436 break;
1437 vc->runner = vcpu;
1438 n_ceded = 0;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001439 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
Paul Mackerras8455d792012-10-15 01:17:42 +00001440 if (!v->arch.pending_exceptions)
1441 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00001442 else
1443 v->arch.ceded = 0;
1444 }
Paul Mackerras8455d792012-10-15 01:17:42 +00001445 if (n_ceded == vc->n_runnable)
1446 kvmppc_vcore_blocked(vc);
1447 else
1448 kvmppc_run_core(vc);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001449 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001450 }
1451
Paul Mackerras8455d792012-10-15 01:17:42 +00001452 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1453 (vc->vcore_state == VCORE_RUNNING ||
1454 vc->vcore_state == VCORE_EXITING)) {
1455 spin_unlock(&vc->lock);
1456 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1457 spin_lock(&vc->lock);
1458 }
1459
1460 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1461 kvmppc_remove_runnable(vc, vcpu);
1462 vcpu->stat.signal_exits++;
1463 kvm_run->exit_reason = KVM_EXIT_INTR;
1464 vcpu->arch.ret = -EINTR;
1465 }
1466
1467 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1468 /* Wake up some vcpu to run the core */
1469 v = list_first_entry(&vc->runnable_threads,
1470 struct kvm_vcpu, arch.run_list);
1471 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001472 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001473
Paul Mackerras19ccb762011-07-23 17:42:46 +10001474 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001475 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001476}
1477
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001478int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
1479{
1480 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001481 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001482
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001483 if (!vcpu->arch.sane) {
1484 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1485 return -EINVAL;
1486 }
1487
Scott Wood25051b52011-11-08 18:23:23 -06001488 kvmppc_core_prepare_to_enter(vcpu);
1489
Paul Mackerras19ccb762011-07-23 17:42:46 +10001490 /* No need to go into the guest when all we'll do is come back out */
1491 if (signal_pending(current)) {
1492 run->exit_reason = KVM_EXIT_INTR;
1493 return -EINTR;
1494 }
1495
Paul Mackerras32fad282012-05-04 02:32:53 +00001496 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1497 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1498 smp_mb();
1499
1500 /* On the first time here, set up HTAB and VRMA or RMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001501 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001502 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001503 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001504 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001505 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001506
1507 flush_fp_to_thread(current);
1508 flush_altivec_to_thread(current);
1509 flush_vsx_to_thread(current);
1510 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001511 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001512 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001513
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001514 do {
1515 r = kvmppc_run_vcpu(run, vcpu);
1516
1517 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1518 !(vcpu->arch.shregs.msr & MSR_PR)) {
1519 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001520 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001521 } else if (r == RESUME_PAGE_FAULT) {
1522 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1523 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1524 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1525 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001526 }
1527 } while (r == RESUME_GUEST);
Paul Mackerras32fad282012-05-04 02:32:53 +00001528
1529 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001530 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00001531 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001532 return r;
1533}
1534
David Gibson54738c02011-06-29 00:22:41 +00001535
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001536/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +00001537 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001538static inline int lpcr_rmls(unsigned long rma_size)
1539{
1540 switch (rma_size) {
1541 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001542 if (cpu_has_feature(CPU_FTR_ARCH_206))
1543 return 8; /* only supported on POWER7 */
1544 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001545 case 64ul << 20: /* 64 MB */
1546 return 3;
1547 case 128ul << 20: /* 128 MB */
1548 return 7;
1549 case 256ul << 20: /* 256 MB */
1550 return 4;
1551 case 1ul << 30: /* 1 GB */
1552 return 2;
1553 case 16ul << 30: /* 16 GB */
1554 return 1;
1555 case 256ul << 30: /* 256 GB */
1556 return 0;
1557 default:
1558 return -1;
1559 }
1560}
1561
1562static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1563{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001564 struct page *page;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301565 struct kvm_rma_info *ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001566
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301567 if (vmf->pgoff >= kvm_rma_pages)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001568 return VM_FAULT_SIGBUS;
1569
1570 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1571 get_page(page);
1572 vmf->page = page;
1573 return 0;
1574}
1575
1576static const struct vm_operations_struct kvm_rma_vm_ops = {
1577 .fault = kvm_rma_fault,
1578};
1579
1580static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1581{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001582 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001583 vma->vm_ops = &kvm_rma_vm_ops;
1584 return 0;
1585}
1586
1587static int kvm_rma_release(struct inode *inode, struct file *filp)
1588{
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301589 struct kvm_rma_info *ri = filp->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001590
1591 kvm_release_rma(ri);
1592 return 0;
1593}
1594
Al Viro75ef9de2013-04-04 19:09:41 -04001595static const struct file_operations kvm_rma_fops = {
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001596 .mmap = kvm_rma_mmap,
1597 .release = kvm_rma_release,
1598};
1599
1600long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
1601{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001602 long fd;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301603 struct kvm_rma_info *ri;
1604 /*
1605 * Only do this on PPC970 in HV mode
1606 */
1607 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
1608 !cpu_has_feature(CPU_FTR_ARCH_201))
1609 return -EINVAL;
1610
1611 if (!kvm_rma_pages)
1612 return -EINVAL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001613
1614 ri = kvm_alloc_rma();
1615 if (!ri)
1616 return -ENOMEM;
1617
Yann Droneaud2f84d5e2013-08-24 22:14:08 +02001618 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001619 if (fd < 0)
1620 kvm_release_rma(ri);
1621
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301622 ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001623 return fd;
1624}
1625
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001626static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1627 int linux_psize)
1628{
1629 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1630
1631 if (!def->shift)
1632 return;
1633 (*sps)->page_shift = def->shift;
1634 (*sps)->slb_enc = def->sllp;
1635 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00001636 /*
1637 * Only return base page encoding. We don't want to return
1638 * all the supporting pte_enc, because our H_ENTER doesn't
1639 * support MPSS yet. Once they do, we can start passing all
1640 * support pte_enc here
1641 */
1642 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001643 (*sps)++;
1644}
1645
1646int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
1647{
1648 struct kvm_ppc_one_seg_page_size *sps;
1649
1650 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1651 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1652 info->flags |= KVM_PPC_1T_SEGMENTS;
1653 info->slb_size = mmu_slb_size;
1654
1655 /* We only support these sizes for now, and no muti-size segments */
1656 sps = &info->sps[0];
1657 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1658 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1659 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1660
1661 return 0;
1662}
1663
Paul Mackerras82ed3612011-12-15 02:03:22 +00001664/*
1665 * Get (and clear) the dirty memory log for a memory slot.
1666 */
1667int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1668{
1669 struct kvm_memory_slot *memslot;
1670 int r;
1671 unsigned long n;
1672
1673 mutex_lock(&kvm->slots_lock);
1674
1675 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07001676 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00001677 goto out;
1678
1679 memslot = id_to_memslot(kvm->memslots, log->slot);
1680 r = -ENOENT;
1681 if (!memslot->dirty_bitmap)
1682 goto out;
1683
1684 n = kvm_dirty_bitmap_bytes(memslot);
1685 memset(memslot->dirty_bitmap, 0, n);
1686
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001687 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00001688 if (r)
1689 goto out;
1690
1691 r = -EFAULT;
1692 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1693 goto out;
1694
1695 r = 0;
1696out:
1697 mutex_unlock(&kvm->slots_lock);
1698 return r;
1699}
1700
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001701static void unpin_slot(struct kvm_memory_slot *memslot)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001702{
1703 unsigned long *physp;
1704 unsigned long j, npages, pfn;
1705 struct page *page;
1706
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001707 physp = memslot->arch.slot_phys;
1708 npages = memslot->npages;
1709 if (!physp)
1710 return;
1711 for (j = 0; j < npages; j++) {
1712 if (!(physp[j] & KVMPPC_GOT_PAGE))
1713 continue;
1714 pfn = physp[j] >> PAGE_SHIFT;
1715 page = pfn_to_page(pfn);
1716 SetPageDirty(page);
1717 put_page(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001718 }
1719}
1720
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001721void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1722 struct kvm_memory_slot *dont)
1723{
1724 if (!dont || free->arch.rmap != dont->arch.rmap) {
1725 vfree(free->arch.rmap);
1726 free->arch.rmap = NULL;
1727 }
1728 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1729 unpin_slot(free);
1730 vfree(free->arch.slot_phys);
1731 free->arch.slot_phys = NULL;
1732 }
1733}
1734
1735int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1736 unsigned long npages)
1737{
1738 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1739 if (!slot->arch.rmap)
1740 return -ENOMEM;
1741 slot->arch.slot_phys = NULL;
1742
1743 return 0;
1744}
1745
1746int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1747 struct kvm_memory_slot *memslot,
1748 struct kvm_userspace_memory_region *mem)
1749{
1750 unsigned long *phys;
1751
1752 /* Allocate a slot_phys array if needed */
1753 phys = memslot->arch.slot_phys;
1754 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1755 phys = vzalloc(memslot->npages * sizeof(unsigned long));
1756 if (!phys)
1757 return -ENOMEM;
1758 memslot->arch.slot_phys = phys;
1759 }
1760
1761 return 0;
1762}
1763
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001764void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001765 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001766 const struct kvm_memory_slot *old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001767{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001768 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1769 struct kvm_memory_slot *memslot;
1770
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001771 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001772 /*
1773 * If modifying a memslot, reset all the rmap dirty bits.
1774 * If this is a new memslot, we don't need to do anything
1775 * since the rmap array starts out as all zeroes,
1776 * i.e. no pages are dirty.
1777 */
1778 memslot = id_to_memslot(kvm->memslots, mem->slot);
1779 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1780 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001781}
1782
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001783/*
1784 * Update LPCR values in kvm->arch and in vcores.
1785 * Caller must hold kvm->lock.
1786 */
1787void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
1788{
1789 long int i;
1790 u32 cores_done = 0;
1791
1792 if ((kvm->arch.lpcr & mask) == lpcr)
1793 return;
1794
1795 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
1796
1797 for (i = 0; i < KVM_MAX_VCORES; ++i) {
1798 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
1799 if (!vc)
1800 continue;
1801 spin_lock(&vc->lock);
1802 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
1803 spin_unlock(&vc->lock);
1804 if (++cores_done >= kvm->arch.online_vcores)
1805 break;
1806 }
1807}
1808
Paul Mackerras32fad282012-05-04 02:32:53 +00001809static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001810{
1811 int err = 0;
1812 struct kvm *kvm = vcpu->kvm;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301813 struct kvm_rma_info *ri = NULL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001814 unsigned long hva;
1815 struct kvm_memory_slot *memslot;
1816 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001817 unsigned long lpcr = 0, senc;
1818 unsigned long lpcr_mask = 0;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001819 unsigned long psize, porder;
1820 unsigned long rma_size;
1821 unsigned long rmls;
1822 unsigned long *physp;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001823 unsigned long i, npages;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001824 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001825
1826 mutex_lock(&kvm->lock);
1827 if (kvm->arch.rma_setup_done)
1828 goto out; /* another vcpu beat us to it */
1829
Paul Mackerras32fad282012-05-04 02:32:53 +00001830 /* Allocate hashed page table (if not done already) and reset it */
1831 if (!kvm->arch.hpt_virt) {
1832 err = kvmppc_alloc_hpt(kvm, NULL);
1833 if (err) {
1834 pr_err("KVM: Couldn't alloc HPT\n");
1835 goto out;
1836 }
1837 }
1838
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001839 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001840 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001841 memslot = gfn_to_memslot(kvm, 0);
1842
1843 /* We must have some memory at 0 by now */
1844 err = -EINVAL;
1845 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001846 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001847
1848 /* Look up the VMA for the start of this memory slot */
1849 hva = memslot->userspace_addr;
1850 down_read(&current->mm->mmap_sem);
1851 vma = find_vma(current->mm, hva);
1852 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
1853 goto up_out;
1854
1855 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001856 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001857
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001858 /* Is this one of our preallocated RMAs? */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001859 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
1860 hva == vma->vm_start)
1861 ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001862
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001863 up_read(&current->mm->mmap_sem);
1864
1865 if (!ri) {
1866 /* On POWER7, use VRMA; on PPC970, give up */
1867 err = -EPERM;
1868 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1869 pr_err("KVM: CPU requires an RMO\n");
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001870 goto out_srcu;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001871 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001872
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001873 /* We can handle 4k, 64k or 16M pages in the VRMA */
1874 err = -EINVAL;
1875 if (!(psize == 0x1000 || psize == 0x10000 ||
1876 psize == 0x1000000))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001877 goto out_srcu;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001878
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001879 /* Update VRMASD field in the LPCR */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001880 senc = slb_pgsize_encoding(psize);
Paul Mackerras697d3892011-12-12 12:36:37 +00001881 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1882 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001883 lpcr_mask = LPCR_VRMASD;
1884 /* the -4 is to account for senc values starting at 0x10 */
1885 lpcr = senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001886
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001887 /* Create HPTEs in the hash page table for the VRMA */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001888 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001889
1890 } else {
1891 /* Set up to use an RMO region */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301892 rma_size = kvm_rma_pages;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001893 if (rma_size > memslot->npages)
1894 rma_size = memslot->npages;
1895 rma_size <<= PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001896 rmls = lpcr_rmls(rma_size);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001897 err = -EINVAL;
Chen Gang5d226ae2013-07-22 14:32:35 +08001898 if ((long)rmls < 0) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001899 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001900 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001901 }
1902 atomic_inc(&ri->use_count);
1903 kvm->arch.rma = ri;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001904
1905 /* Update LPCR and RMOR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001906 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1907 /* PPC970; insert RMLS value (split field) in HID4 */
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001908 lpcr_mask = (1ul << HID4_RMLS0_SH) |
1909 (3ul << HID4_RMLS2_SH) | HID4_RMOR;
1910 lpcr = ((rmls >> 2) << HID4_RMLS0_SH) |
Paul Mackerras9e368f22011-06-29 00:40:08 +00001911 ((rmls & 3) << HID4_RMLS2_SH);
1912 /* RMOR is also in HID4 */
1913 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
1914 << HID4_RMOR_SH;
1915 } else {
1916 /* POWER7 */
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001917 lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS;
1918 lpcr = rmls << LPCR_RMLS_SH;
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301919 kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001920 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001921 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001922 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001923
1924 /* Initialize phys addrs of pages in RMO */
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +05301925 npages = kvm_rma_pages;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001926 porder = __ilog2(npages);
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001927 physp = memslot->arch.slot_phys;
1928 if (physp) {
1929 if (npages > memslot->npages)
1930 npages = memslot->npages;
1931 spin_lock(&kvm->arch.slot_phys_lock);
1932 for (i = 0; i < npages; ++i)
1933 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
1934 porder;
1935 spin_unlock(&kvm->arch.slot_phys_lock);
1936 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001937 }
1938
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001939 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
1940
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001941 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
1942 smp_wmb();
1943 kvm->arch.rma_setup_done = 1;
1944 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001945 out_srcu:
1946 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001947 out:
1948 mutex_unlock(&kvm->lock);
1949 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001950
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001951 up_out:
1952 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08001953 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001954}
1955
1956int kvmppc_core_init_vm(struct kvm *kvm)
1957{
Paul Mackerras32fad282012-05-04 02:32:53 +00001958 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001959
Paul Mackerras32fad282012-05-04 02:32:53 +00001960 /* Allocate the guest's logical partition ID */
1961
1962 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08001963 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00001964 return -ENOMEM;
1965 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001966
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001967 /*
1968 * Since we don't flush the TLB when tearing down a VM,
1969 * and this lpid might have previously been used,
1970 * make sure we flush on each core before running the new VM.
1971 */
1972 cpumask_setall(&kvm->arch.need_tlb_flush);
1973
David Gibson54738c02011-06-29 00:22:41 +00001974 INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001975 INIT_LIST_HEAD(&kvm->arch.rtas_tokens);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001976
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001977 kvm->arch.rma = NULL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001978
Paul Mackerras9e368f22011-06-29 00:40:08 +00001979 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001980
Paul Mackerras9e368f22011-06-29 00:40:08 +00001981 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1982 /* PPC970; HID4 is effectively the LPCR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001983 kvm->arch.host_lpid = 0;
1984 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
1985 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
1986 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
1987 ((lpid & 0xf) << HID4_LPID5_SH);
1988 } else {
1989 /* POWER7; init LPCR for virtual RMA mode */
1990 kvm->arch.host_lpid = mfspr(SPRN_LPID);
1991 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
1992 lpcr &= LPCR_PECE | LPCR_LPES;
1993 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
Paul Mackerras697d3892011-12-12 12:36:37 +00001994 LPCR_VPM0 | LPCR_VPM1;
1995 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
1996 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerras9e368f22011-06-29 00:40:08 +00001997 }
1998 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001999
Paul Mackerras342d3db2011-12-12 12:38:05 +00002000 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002001 spin_lock_init(&kvm->arch.slot_phys_lock);
Paul Mackerras512691d2012-10-15 01:15:41 +00002002
2003 /*
2004 * Don't allow secondary CPU threads to come online
2005 * while any KVM VMs exist.
2006 */
2007 inhibit_secondary_onlining();
2008
David Gibson54738c02011-06-29 00:22:41 +00002009 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002010}
2011
2012void kvmppc_core_destroy_vm(struct kvm *kvm)
2013{
Paul Mackerras512691d2012-10-15 01:15:41 +00002014 uninhibit_secondary_onlining();
2015
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002016 if (kvm->arch.rma) {
2017 kvm_release_rma(kvm->arch.rma);
2018 kvm->arch.rma = NULL;
2019 }
2020
Michael Ellerman8e591cb2013-04-17 20:30:00 +00002021 kvmppc_rtas_tokens_free(kvm);
2022
Paul Mackerrasde56a942011-06-29 00:21:34 +00002023 kvmppc_free_hpt(kvm);
David Gibson54738c02011-06-29 00:22:41 +00002024 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
Paul Mackerrasde56a942011-06-29 00:21:34 +00002025}
2026
2027/* These are stubs for now */
2028void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
2029{
2030}
2031
2032/* We don't need to emulate any privileged instructions or dcbz */
2033int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
2034 unsigned int inst, int *advance)
2035{
2036 return EMULATE_FAIL;
2037}
2038
Alexander Graf54771e62012-05-04 14:55:12 +02002039int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002040{
2041 return EMULATE_FAIL;
2042}
2043
Alexander Graf54771e62012-05-04 14:55:12 +02002044int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002045{
2046 return EMULATE_FAIL;
2047}
2048
2049static int kvmppc_book3s_hv_init(void)
2050{
2051 int r;
2052
2053 r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
2054
2055 if (r)
2056 return r;
2057
2058 r = kvmppc_mmu_hv_init();
2059
2060 return r;
2061}
2062
2063static void kvmppc_book3s_hv_exit(void)
2064{
2065 kvm_exit();
2066}
2067
2068module_init(kvmppc_book3s_hv_init);
2069module_exit(kvmppc_book3s_hv_exit);