blob: c5ddf048e19ef7595dbc8831bb7022b12d0bbc05 [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 Mackerras19ccb762011-07-23 17:42:46 +100060static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +000061static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +100062
Paul Mackerrasde56a942011-06-29 00:21:34 +000063void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
64{
Paul Mackerras0456ec42012-02-03 00:56:21 +000065 struct kvmppc_vcore *vc = vcpu->arch.vcore;
66
Paul Mackerrasde56a942011-06-29 00:21:34 +000067 local_paca->kvm_hstate.kvm_vcpu = vcpu;
Paul Mackerras0456ec42012-02-03 00:56:21 +000068 local_paca->kvm_hstate.kvm_vcore = vc;
69 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
70 vc->stolen_tb += mftb() - vc->preempt_tb;
Paul Mackerrasde56a942011-06-29 00:21:34 +000071}
72
73void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
74{
Paul Mackerras0456ec42012-02-03 00:56:21 +000075 struct kvmppc_vcore *vc = vcpu->arch.vcore;
76
77 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
78 vc->preempt_tb = mftb();
Paul Mackerrasde56a942011-06-29 00:21:34 +000079}
80
Paul Mackerrasde56a942011-06-29 00:21:34 +000081void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
82{
83 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +100084 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +000085}
86
87void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
88{
89 vcpu->arch.pvr = pvr;
90}
91
92void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
93{
94 int r;
95
96 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
97 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
98 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
99 for (r = 0; r < 16; ++r)
100 pr_err("r%2d = %.16lx r%d = %.16lx\n",
101 r, kvmppc_get_gpr(vcpu, r),
102 r+16, kvmppc_get_gpr(vcpu, r+16));
103 pr_err("ctr = %.16lx lr = %.16lx\n",
104 vcpu->arch.ctr, vcpu->arch.lr);
105 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
106 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
107 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
108 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
109 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
110 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
111 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
112 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
113 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
114 pr_err("fault dar = %.16lx dsisr = %.8x\n",
115 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
116 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
117 for (r = 0; r < vcpu->arch.slb_max; ++r)
118 pr_err(" ESID = %.16llx VSID = %.16llx\n",
119 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
120 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000121 vcpu->kvm->arch.lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000122 vcpu->arch.last_inst);
123}
124
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000125struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
126{
127 int r;
128 struct kvm_vcpu *v, *ret = NULL;
129
130 mutex_lock(&kvm->lock);
131 kvm_for_each_vcpu(r, v, kvm) {
132 if (v->vcpu_id == id) {
133 ret = v;
134 break;
135 }
136 }
137 mutex_unlock(&kvm->lock);
138 return ret;
139}
140
141static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
142{
143 vpa->shared_proc = 1;
144 vpa->yield_count = 1;
145}
146
Paul Mackerras55b665b2012-09-25 20:33:06 +0000147static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
148 unsigned long addr, unsigned long len)
149{
150 /* check address is cacheline aligned */
151 if (addr & (L1_CACHE_BYTES - 1))
152 return -EINVAL;
153 spin_lock(&vcpu->arch.vpa_update_lock);
154 if (v->next_gpa != addr || v->len != len) {
155 v->next_gpa = addr;
156 v->len = addr ? len : 0;
157 v->update_pending = 1;
158 }
159 spin_unlock(&vcpu->arch.vpa_update_lock);
160 return 0;
161}
162
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000163/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
164struct reg_vpa {
165 u32 dummy;
166 union {
167 u16 hword;
168 u32 word;
169 } length;
170};
171
172static int vpa_is_registered(struct kvmppc_vpa *vpap)
173{
174 if (vpap->update_pending)
175 return vpap->next_gpa != 0;
176 return vpap->pinned_addr != NULL;
177}
178
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000179static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
180 unsigned long flags,
181 unsigned long vcpuid, unsigned long vpa)
182{
183 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000184 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000185 void *va;
186 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000187 int err;
188 int subfunc;
189 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000190
191 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
192 if (!tvcpu)
193 return H_PARAMETER;
194
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000195 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
196 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
197 subfunc == H_VPA_REG_SLB) {
198 /* Registering new area - address must be cache-line aligned */
199 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000200 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000201
202 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000203 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
204 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000205 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000206 if (subfunc == H_VPA_REG_VPA)
207 len = ((struct reg_vpa *)va)->length.hword;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000208 else
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000209 len = ((struct reg_vpa *)va)->length.word;
210 kvmppc_unpin_guest_page(kvm, va);
211
212 /* Check length */
213 if (len > nb || len < sizeof(struct reg_vpa))
214 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000215 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000216 vpa = 0;
217 len = 0;
218 }
219
220 err = H_PARAMETER;
221 vpap = NULL;
222 spin_lock(&tvcpu->arch.vpa_update_lock);
223
224 switch (subfunc) {
225 case H_VPA_REG_VPA: /* register VPA */
226 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000227 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000228 vpap = &tvcpu->arch.vpa;
229 err = 0;
230 break;
231
232 case H_VPA_REG_DTL: /* register DTL */
233 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000234 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000235 len -= len % sizeof(struct dtl_entry);
236
237 /* Check that they have previously registered a VPA */
238 err = H_RESOURCE;
239 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000240 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000241
242 vpap = &tvcpu->arch.dtl;
243 err = 0;
244 break;
245
246 case H_VPA_REG_SLB: /* register SLB shadow buffer */
247 /* Check that they have previously registered a VPA */
248 err = H_RESOURCE;
249 if (!vpa_is_registered(&tvcpu->arch.vpa))
250 break;
251
252 vpap = &tvcpu->arch.slb_shadow;
253 err = 0;
254 break;
255
256 case H_VPA_DEREG_VPA: /* deregister VPA */
257 /* Check they don't still have a DTL or SLB buf registered */
258 err = H_RESOURCE;
259 if (vpa_is_registered(&tvcpu->arch.dtl) ||
260 vpa_is_registered(&tvcpu->arch.slb_shadow))
261 break;
262
263 vpap = &tvcpu->arch.vpa;
264 err = 0;
265 break;
266
267 case H_VPA_DEREG_DTL: /* deregister DTL */
268 vpap = &tvcpu->arch.dtl;
269 err = 0;
270 break;
271
272 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
273 vpap = &tvcpu->arch.slb_shadow;
274 err = 0;
275 break;
276 }
277
278 if (vpap) {
279 vpap->next_gpa = vpa;
280 vpap->len = len;
281 vpap->update_pending = 1;
282 }
283
284 spin_unlock(&tvcpu->arch.vpa_update_lock);
285
286 return err;
287}
288
Paul Mackerras081f3232012-06-01 20:20:24 +1000289static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000290{
Paul Mackerras081f3232012-06-01 20:20:24 +1000291 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000292 void *va;
293 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000294 unsigned long gpa;
295
296 /*
297 * We need to pin the page pointed to by vpap->next_gpa,
298 * but we can't call kvmppc_pin_guest_page under the lock
299 * as it does get_user_pages() and down_read(). So we
300 * have to drop the lock, pin the page, then get the lock
301 * again and check that a new area didn't get registered
302 * in the meantime.
303 */
304 for (;;) {
305 gpa = vpap->next_gpa;
306 spin_unlock(&vcpu->arch.vpa_update_lock);
307 va = NULL;
308 nb = 0;
309 if (gpa)
310 va = kvmppc_pin_guest_page(kvm, vpap->next_gpa, &nb);
311 spin_lock(&vcpu->arch.vpa_update_lock);
312 if (gpa == vpap->next_gpa)
313 break;
314 /* sigh... unpin that one and try again */
315 if (va)
316 kvmppc_unpin_guest_page(kvm, va);
317 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000318
319 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000320 if (va && nb < vpap->len) {
321 /*
322 * If it's now too short, it must be that userspace
323 * has changed the mappings underlying guest memory,
324 * so unregister the region.
325 */
326 kvmppc_unpin_guest_page(kvm, va);
327 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000328 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000329 if (vpap->pinned_addr)
330 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr);
331 vpap->pinned_addr = va;
332 if (va)
333 vpap->pinned_end = va + vpap->len;
334}
Paul Mackerras93e60242011-12-12 12:28:55 +0000335
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000336static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
337{
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000338 spin_lock(&vcpu->arch.vpa_update_lock);
339 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000340 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000341 if (vcpu->arch.vpa.pinned_addr)
342 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000343 }
344 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000345 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000346 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
347 vcpu->arch.dtl_index = 0;
348 }
349 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000350 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000351 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000352}
353
Paul Mackerras0456ec42012-02-03 00:56:21 +0000354static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
355 struct kvmppc_vcore *vc)
356{
357 struct dtl_entry *dt;
358 struct lppaca *vpa;
359 unsigned long old_stolen;
360
361 dt = vcpu->arch.dtl_ptr;
362 vpa = vcpu->arch.vpa.pinned_addr;
363 old_stolen = vcpu->arch.stolen_logged;
364 vcpu->arch.stolen_logged = vc->stolen_tb;
365 if (!dt || !vpa)
366 return;
367 memset(dt, 0, sizeof(struct dtl_entry));
368 dt->dispatch_reason = 7;
369 dt->processor_id = vc->pcpu + vcpu->arch.ptid;
370 dt->timebase = mftb();
371 dt->enqueue_to_dispatch_time = vc->stolen_tb - old_stolen;
372 dt->srr0 = kvmppc_get_pc(vcpu);
373 dt->srr1 = vcpu->arch.shregs.msr;
374 ++dt;
375 if (dt == vcpu->arch.dtl.pinned_end)
376 dt = vcpu->arch.dtl.pinned_addr;
377 vcpu->arch.dtl_ptr = dt;
378 /* order writing *dt vs. writing vpa->dtl_idx */
379 smp_wmb();
380 vpa->dtl_idx = ++vcpu->arch.dtl_index;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000381}
382
383int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
384{
385 unsigned long req = kvmppc_get_gpr(vcpu, 3);
386 unsigned long target, ret = H_SUCCESS;
387 struct kvm_vcpu *tvcpu;
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000388 int idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000389
390 switch (req) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000391 case H_ENTER:
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000392 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000393 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
394 kvmppc_get_gpr(vcpu, 5),
395 kvmppc_get_gpr(vcpu, 6),
396 kvmppc_get_gpr(vcpu, 7));
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000397 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000398 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000399 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000400 break;
401 case H_PROD:
402 target = kvmppc_get_gpr(vcpu, 4);
403 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
404 if (!tvcpu) {
405 ret = H_PARAMETER;
406 break;
407 }
408 tvcpu->arch.prodded = 1;
409 smp_mb();
410 if (vcpu->arch.ceded) {
411 if (waitqueue_active(&vcpu->wq)) {
412 wake_up_interruptible(&vcpu->wq);
413 vcpu->stat.halt_wakeup++;
414 }
415 }
416 break;
417 case H_CONFER:
418 break;
419 case H_REGISTER_VPA:
420 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
421 kvmppc_get_gpr(vcpu, 5),
422 kvmppc_get_gpr(vcpu, 6));
423 break;
424 default:
425 return RESUME_HOST;
426 }
427 kvmppc_set_gpr(vcpu, 3, ret);
428 vcpu->arch.hcall_needed = 0;
429 return RESUME_GUEST;
430}
431
Paul Mackerrasde56a942011-06-29 00:21:34 +0000432static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
433 struct task_struct *tsk)
434{
435 int r = RESUME_HOST;
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000436 int srcu_idx;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000437
438 vcpu->stat.sum_exits++;
439
440 run->exit_reason = KVM_EXIT_UNKNOWN;
441 run->ready_for_interrupt_injection = 1;
442 switch (vcpu->arch.trap) {
443 /* We're good on these - the host merely wanted to get our attention */
444 case BOOK3S_INTERRUPT_HV_DECREMENTER:
445 vcpu->stat.dec_exits++;
446 r = RESUME_GUEST;
447 break;
448 case BOOK3S_INTERRUPT_EXTERNAL:
449 vcpu->stat.ext_intr_exits++;
450 r = RESUME_GUEST;
451 break;
452 case BOOK3S_INTERRUPT_PERFMON:
453 r = RESUME_GUEST;
454 break;
455 case BOOK3S_INTERRUPT_PROGRAM:
456 {
457 ulong flags;
458 /*
459 * Normally program interrupts are delivered directly
460 * to the guest by the hardware, but we can get here
461 * as a result of a hypervisor emulation interrupt
462 * (e40) getting turned into a 700 by BML RTAS.
463 */
464 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
465 kvmppc_core_queue_program(vcpu, flags);
466 r = RESUME_GUEST;
467 break;
468 }
469 case BOOK3S_INTERRUPT_SYSCALL:
470 {
471 /* hcall - punt to userspace */
472 int i;
473
474 if (vcpu->arch.shregs.msr & MSR_PR) {
475 /* sc 1 from userspace - reflect to guest syscall */
476 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
477 r = RESUME_GUEST;
478 break;
479 }
480 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
481 for (i = 0; i < 9; ++i)
482 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
483 run->exit_reason = KVM_EXIT_PAPR_HCALL;
484 vcpu->arch.hcall_needed = 1;
485 r = RESUME_HOST;
486 break;
487 }
488 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000489 * We get these next two if the guest accesses a page which it thinks
490 * it has mapped but which is not actually present, either because
491 * it is for an emulated I/O device or because the corresonding
492 * host page has been paged out. Any other HDSI/HISI interrupts
493 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000494 */
495 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000496 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerras697d3892011-12-12 12:36:37 +0000497 r = kvmppc_book3s_hv_page_fault(run, vcpu,
498 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000499 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000500 break;
501 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000502 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000503 r = kvmppc_book3s_hv_page_fault(run, vcpu,
504 kvmppc_get_pc(vcpu), 0);
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000505 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000506 break;
507 /*
508 * This occurs if the guest executes an illegal instruction.
509 * We just generate a program interrupt to the guest, since
510 * we don't emulate any guest instructions at this stage.
511 */
512 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
513 kvmppc_core_queue_program(vcpu, 0x80000);
514 r = RESUME_GUEST;
515 break;
516 default:
517 kvmppc_dump_regs(vcpu);
518 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
519 vcpu->arch.trap, kvmppc_get_pc(vcpu),
520 vcpu->arch.shregs.msr);
521 r = RESUME_HOST;
522 BUG();
523 break;
524 }
525
Paul Mackerrasde56a942011-06-29 00:21:34 +0000526 return r;
527}
528
529int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
530 struct kvm_sregs *sregs)
531{
532 int i;
533
534 sregs->pvr = vcpu->arch.pvr;
535
536 memset(sregs, 0, sizeof(struct kvm_sregs));
537 for (i = 0; i < vcpu->arch.slb_max; i++) {
538 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
539 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
540 }
541
542 return 0;
543}
544
545int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
546 struct kvm_sregs *sregs)
547{
548 int i, j;
549
550 kvmppc_set_pvr(vcpu, sregs->pvr);
551
552 j = 0;
553 for (i = 0; i < vcpu->arch.slb_nr; i++) {
554 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
555 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
556 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
557 ++j;
558 }
559 }
560 vcpu->arch.slb_max = j;
561
562 return 0;
563}
564
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000565int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000566{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000567 int r = 0;
568 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +0000569
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000570 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000571 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000572 *val = get_reg_val(id, 0);
573 break;
574 case KVM_REG_PPC_DABR:
575 *val = get_reg_val(id, vcpu->arch.dabr);
576 break;
577 case KVM_REG_PPC_DSCR:
578 *val = get_reg_val(id, vcpu->arch.dscr);
579 break;
580 case KVM_REG_PPC_PURR:
581 *val = get_reg_val(id, vcpu->arch.purr);
582 break;
583 case KVM_REG_PPC_SPURR:
584 *val = get_reg_val(id, vcpu->arch.spurr);
585 break;
586 case KVM_REG_PPC_AMR:
587 *val = get_reg_val(id, vcpu->arch.amr);
588 break;
589 case KVM_REG_PPC_UAMOR:
590 *val = get_reg_val(id, vcpu->arch.uamor);
591 break;
592 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
593 i = id - KVM_REG_PPC_MMCR0;
594 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
595 break;
596 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
597 i = id - KVM_REG_PPC_PMC1;
598 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +0000599 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000600#ifdef CONFIG_VSX
601 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
602 if (cpu_has_feature(CPU_FTR_VSX)) {
603 /* VSX => FP reg i is stored in arch.vsr[2*i] */
604 long int i = id - KVM_REG_PPC_FPR0;
605 *val = get_reg_val(id, vcpu->arch.vsr[2 * i]);
606 } else {
607 /* let generic code handle it */
608 r = -EINVAL;
609 }
610 break;
611 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
612 if (cpu_has_feature(CPU_FTR_VSX)) {
613 long int i = id - KVM_REG_PPC_VSR0;
614 val->vsxval[0] = vcpu->arch.vsr[2 * i];
615 val->vsxval[1] = vcpu->arch.vsr[2 * i + 1];
616 } else {
617 r = -ENXIO;
618 }
619 break;
620#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000621 case KVM_REG_PPC_VPA_ADDR:
622 spin_lock(&vcpu->arch.vpa_update_lock);
623 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
624 spin_unlock(&vcpu->arch.vpa_update_lock);
625 break;
626 case KVM_REG_PPC_VPA_SLB:
627 spin_lock(&vcpu->arch.vpa_update_lock);
628 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
629 val->vpaval.length = vcpu->arch.slb_shadow.len;
630 spin_unlock(&vcpu->arch.vpa_update_lock);
631 break;
632 case KVM_REG_PPC_VPA_DTL:
633 spin_lock(&vcpu->arch.vpa_update_lock);
634 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
635 val->vpaval.length = vcpu->arch.dtl.len;
636 spin_unlock(&vcpu->arch.vpa_update_lock);
637 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000638 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000639 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000640 break;
641 }
642
643 return r;
644}
645
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000646int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000647{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000648 int r = 0;
649 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +0000650 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +0000651
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000652 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000653 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +0000654 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000655 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +0000656 r = -EINVAL;
657 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000658 case KVM_REG_PPC_DABR:
659 vcpu->arch.dabr = set_reg_val(id, *val);
660 break;
661 case KVM_REG_PPC_DSCR:
662 vcpu->arch.dscr = set_reg_val(id, *val);
663 break;
664 case KVM_REG_PPC_PURR:
665 vcpu->arch.purr = set_reg_val(id, *val);
666 break;
667 case KVM_REG_PPC_SPURR:
668 vcpu->arch.spurr = set_reg_val(id, *val);
669 break;
670 case KVM_REG_PPC_AMR:
671 vcpu->arch.amr = set_reg_val(id, *val);
672 break;
673 case KVM_REG_PPC_UAMOR:
674 vcpu->arch.uamor = set_reg_val(id, *val);
675 break;
676 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
677 i = id - KVM_REG_PPC_MMCR0;
678 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
679 break;
680 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
681 i = id - KVM_REG_PPC_PMC1;
682 vcpu->arch.pmc[i] = set_reg_val(id, *val);
683 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000684#ifdef CONFIG_VSX
685 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
686 if (cpu_has_feature(CPU_FTR_VSX)) {
687 /* VSX => FP reg i is stored in arch.vsr[2*i] */
688 long int i = id - KVM_REG_PPC_FPR0;
689 vcpu->arch.vsr[2 * i] = set_reg_val(id, *val);
690 } else {
691 /* let generic code handle it */
692 r = -EINVAL;
693 }
694 break;
695 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
696 if (cpu_has_feature(CPU_FTR_VSX)) {
697 long int i = id - KVM_REG_PPC_VSR0;
698 vcpu->arch.vsr[2 * i] = val->vsxval[0];
699 vcpu->arch.vsr[2 * i + 1] = val->vsxval[1];
700 } else {
701 r = -ENXIO;
702 }
703 break;
704#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000705 case KVM_REG_PPC_VPA_ADDR:
706 addr = set_reg_val(id, *val);
707 r = -EINVAL;
708 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
709 vcpu->arch.dtl.next_gpa))
710 break;
711 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
712 break;
713 case KVM_REG_PPC_VPA_SLB:
714 addr = val->vpaval.addr;
715 len = val->vpaval.length;
716 r = -EINVAL;
717 if (addr && !vcpu->arch.vpa.next_gpa)
718 break;
719 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
720 break;
721 case KVM_REG_PPC_VPA_DTL:
722 addr = val->vpaval.addr;
723 len = val->vpaval.length;
724 r = -EINVAL;
725 if (len < sizeof(struct dtl_entry))
726 break;
727 if (addr && !vcpu->arch.vpa.next_gpa)
728 break;
729 len -= len % sizeof(struct dtl_entry);
730 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
731 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000732 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000733 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000734 break;
735 }
736
737 return r;
738}
739
Paul Mackerrasde56a942011-06-29 00:21:34 +0000740int kvmppc_core_check_processor_compat(void)
741{
Paul Mackerras9e368f22011-06-29 00:40:08 +0000742 if (cpu_has_feature(CPU_FTR_HVMODE))
Paul Mackerrasde56a942011-06-29 00:21:34 +0000743 return 0;
744 return -EIO;
745}
746
747struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
748{
749 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000750 int err = -EINVAL;
751 int core;
752 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000753
Paul Mackerras371fefd2011-06-29 00:23:08 +0000754 core = id / threads_per_core;
755 if (core >= KVM_MAX_VCORES)
756 goto out;
757
758 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200759 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000760 if (!vcpu)
761 goto out;
762
763 err = kvm_vcpu_init(vcpu, kvm, id);
764 if (err)
765 goto free_vcpu;
766
767 vcpu->arch.shared = &vcpu->arch.shregs;
768 vcpu->arch.last_cpu = -1;
769 vcpu->arch.mmcr[0] = MMCR0_FC;
770 vcpu->arch.ctrl = CTRL_RUNLATCH;
771 /* default to host PVR, since we can't spoof it */
772 vcpu->arch.pvr = mfspr(SPRN_PVR);
773 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000774 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000775
Paul Mackerrasde56a942011-06-29 00:21:34 +0000776 kvmppc_mmu_book3s_hv_init(vcpu);
777
Paul Mackerras371fefd2011-06-29 00:23:08 +0000778 /*
Paul Mackerras19ccb762011-07-23 17:42:46 +1000779 * We consider the vcpu stopped until we see the first run ioctl for it.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000780 */
Paul Mackerras19ccb762011-07-23 17:42:46 +1000781 vcpu->arch.state = KVMPPC_VCPU_STOPPED;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000782
783 init_waitqueue_head(&vcpu->arch.cpu_run);
784
785 mutex_lock(&kvm->lock);
786 vcore = kvm->arch.vcores[core];
787 if (!vcore) {
788 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
789 if (vcore) {
790 INIT_LIST_HEAD(&vcore->runnable_threads);
791 spin_lock_init(&vcore->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000792 init_waitqueue_head(&vcore->wq);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000793 vcore->preempt_tb = mftb();
Paul Mackerras371fefd2011-06-29 00:23:08 +0000794 }
795 kvm->arch.vcores[core] = vcore;
796 }
797 mutex_unlock(&kvm->lock);
798
799 if (!vcore)
800 goto free_vcpu;
801
802 spin_lock(&vcore->lock);
803 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000804 spin_unlock(&vcore->lock);
805 vcpu->arch.vcore = vcore;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000806 vcpu->arch.stolen_logged = vcore->stolen_tb;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000807
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200808 vcpu->arch.cpu_type = KVM_CPU_3S_64;
809 kvmppc_sanity_check(vcpu);
810
Paul Mackerrasde56a942011-06-29 00:21:34 +0000811 return vcpu;
812
813free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200814 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000815out:
816 return ERR_PTR(err);
817}
818
819void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
820{
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000821 spin_lock(&vcpu->arch.vpa_update_lock);
822 if (vcpu->arch.dtl.pinned_addr)
823 kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.dtl.pinned_addr);
824 if (vcpu->arch.slb_shadow.pinned_addr)
825 kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.slb_shadow.pinned_addr);
826 if (vcpu->arch.vpa.pinned_addr)
827 kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.vpa.pinned_addr);
828 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000829 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200830 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000831}
832
Paul Mackerras19ccb762011-07-23 17:42:46 +1000833static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000834{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000835 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000836
Paul Mackerras19ccb762011-07-23 17:42:46 +1000837 now = get_tb();
838 if (now > vcpu->arch.dec_expires) {
839 /* decrementer has already gone negative */
840 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -0600841 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000842 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000843 }
Paul Mackerras19ccb762011-07-23 17:42:46 +1000844 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
845 / tb_ticks_per_sec;
846 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
847 HRTIMER_MODE_REL);
848 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000849}
850
Paul Mackerras19ccb762011-07-23 17:42:46 +1000851static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000852{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000853 vcpu->arch.ceded = 0;
854 if (vcpu->arch.timer_running) {
855 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
856 vcpu->arch.timer_running = 0;
857 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000858}
859
860extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
861extern void xics_wake_cpu(int cpu);
862
863static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
864 struct kvm_vcpu *vcpu)
865{
Paul Mackerras371fefd2011-06-29 00:23:08 +0000866 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
867 return;
868 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
869 --vc->n_runnable;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000870 ++vc->n_busy;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000871 list_del(&vcpu->arch.run_list);
872}
873
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000874static int kvmppc_grab_hwthread(int cpu)
875{
876 struct paca_struct *tpaca;
877 long timeout = 1000;
878
879 tpaca = &paca[cpu];
880
881 /* Ensure the thread won't go into the kernel if it wakes */
882 tpaca->kvm_hstate.hwthread_req = 1;
883
884 /*
885 * If the thread is already executing in the kernel (e.g. handling
886 * a stray interrupt), wait for it to get back to nap mode.
887 * The smp_mb() is to ensure that our setting of hwthread_req
888 * is visible before we look at hwthread_state, so if this
889 * races with the code at system_reset_pSeries and the thread
890 * misses our setting of hwthread_req, we are sure to see its
891 * setting of hwthread_state, and vice versa.
892 */
893 smp_mb();
894 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
895 if (--timeout <= 0) {
896 pr_err("KVM: couldn't grab cpu %d\n", cpu);
897 return -EBUSY;
898 }
899 udelay(1);
900 }
901 return 0;
902}
903
904static void kvmppc_release_hwthread(int cpu)
905{
906 struct paca_struct *tpaca;
907
908 tpaca = &paca[cpu];
909 tpaca->kvm_hstate.hwthread_req = 0;
910 tpaca->kvm_hstate.kvm_vcpu = NULL;
911}
912
Paul Mackerras371fefd2011-06-29 00:23:08 +0000913static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
914{
915 int cpu;
916 struct paca_struct *tpaca;
917 struct kvmppc_vcore *vc = vcpu->arch.vcore;
918
Paul Mackerras19ccb762011-07-23 17:42:46 +1000919 if (vcpu->arch.timer_running) {
920 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
921 vcpu->arch.timer_running = 0;
922 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000923 cpu = vc->pcpu + vcpu->arch.ptid;
924 tpaca = &paca[cpu];
925 tpaca->kvm_hstate.kvm_vcpu = vcpu;
926 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000927 tpaca->kvm_hstate.napping = 0;
928 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000929 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +0000930#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000931 if (vcpu->arch.ptid) {
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000932 kvmppc_grab_hwthread(cpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +0000933 xics_wake_cpu(cpu);
934 ++vc->n_woken;
935 }
936#endif
937}
938
939static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
940{
941 int i;
942
943 HMT_low();
944 i = 0;
945 while (vc->nap_count < vc->n_woken) {
946 if (++i >= 1000000) {
947 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
948 vc->nap_count, vc->n_woken);
949 break;
950 }
951 cpu_relax();
952 }
953 HMT_medium();
954}
955
956/*
957 * Check that we are on thread 0 and that any other threads in
958 * this core are off-line.
959 */
960static int on_primary_thread(void)
961{
962 int cpu = smp_processor_id();
963 int thr = cpu_thread_in_core(cpu);
964
965 if (thr)
966 return 0;
967 while (++thr < threads_per_core)
968 if (cpu_online(cpu + thr))
969 return 0;
970 return 1;
971}
972
973/*
974 * Run a set of guest threads on a physical core.
975 * Called with vc->lock held.
976 */
977static int kvmppc_run_core(struct kvmppc_vcore *vc)
978{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000979 struct kvm_vcpu *vcpu, *vcpu0, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000980 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000981 u64 now;
Paul Mackerras081f3232012-06-01 20:20:24 +1000982 int ptid, i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000983 int srcu_idx;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000984
Paul Mackerras371fefd2011-06-29 00:23:08 +0000985 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +1000986 need_vpa_update = 0;
987 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +0000988 if (signal_pending(vcpu->arch.run_task))
989 return 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000990 need_vpa_update |= vcpu->arch.vpa.update_pending |
991 vcpu->arch.slb_shadow.update_pending |
992 vcpu->arch.dtl.update_pending;
993 }
994
995 /*
996 * Initialize *vc, in particular vc->vcore_state, so we can
997 * drop the vcore lock if necessary.
998 */
999 vc->n_woken = 0;
1000 vc->nap_count = 0;
1001 vc->entry_exit_count = 0;
1002 vc->vcore_state = VCORE_RUNNING;
1003 vc->in_guest = 0;
1004 vc->napping_threads = 0;
1005
1006 /*
1007 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1008 * which can't be called with any spinlocks held.
1009 */
1010 if (need_vpa_update) {
1011 spin_unlock(&vc->lock);
1012 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1013 kvmppc_update_vpas(vcpu);
1014 spin_lock(&vc->lock);
1015 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001016
1017 /*
1018 * Make sure we are running on thread 0, and that
1019 * secondary threads are offline.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001020 */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001021 if (threads_per_core > 1 && !on_primary_thread()) {
1022 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1023 vcpu->arch.ret = -EBUSY;
1024 goto out;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001025 }
1026
Paul Mackerras19ccb762011-07-23 17:42:46 +10001027 /*
1028 * Assign physical thread IDs, first to non-ceded vcpus
1029 * and then to ceded ones.
1030 */
1031 ptid = 0;
1032 vcpu0 = NULL;
1033 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1034 if (!vcpu->arch.ceded) {
1035 if (!ptid)
1036 vcpu0 = vcpu;
1037 vcpu->arch.ptid = ptid++;
1038 }
1039 }
1040 if (!vcpu0)
1041 return 0; /* nothing to run */
1042 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1043 if (vcpu->arch.ceded)
1044 vcpu->arch.ptid = ptid++;
1045
Paul Mackerras0456ec42012-02-03 00:56:21 +00001046 vc->stolen_tb += mftb() - vc->preempt_tb;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001047 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001048 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001049 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001050 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001051 }
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001052 /* Grab any remaining hw threads so they can't go into the kernel */
1053 for (i = ptid; i < threads_per_core; ++i)
1054 kvmppc_grab_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001055
1056 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001057 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001058
Paul Mackerras19ccb762011-07-23 17:42:46 +10001059 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001060
1061 srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
1062
Paul Mackerras19ccb762011-07-23 17:42:46 +10001063 __kvmppc_vcore_entry(NULL, vcpu0);
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001064 for (i = 0; i < threads_per_core; ++i)
1065 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001066
Paul Mackerras371fefd2011-06-29 00:23:08 +00001067 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001068 /* disable sending of IPIs on virtual external irqs */
1069 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1070 vcpu->cpu = -1;
1071 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001072 if (vc->nap_count < vc->n_woken)
1073 kvmppc_wait_for_nap(vc);
1074 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001075 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001076 spin_unlock(&vc->lock);
1077
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001078 srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
1079
Paul Mackerras371fefd2011-06-29 00:23:08 +00001080 /* make sure updates to secondary vcpu structs are visible now */
1081 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001082 kvm_guest_exit();
1083
1084 preempt_enable();
1085 kvm_resched(vcpu);
1086
1087 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001088 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1089 /* cancel pending dec exception if dec is positive */
1090 if (now < vcpu->arch.dec_expires &&
1091 kvmppc_core_pending_dec(vcpu))
1092 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001093
1094 ret = RESUME_GUEST;
1095 if (vcpu->arch.trap)
1096 ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
1097 vcpu->arch.run_task);
1098
Paul Mackerras371fefd2011-06-29 00:23:08 +00001099 vcpu->arch.ret = ret;
1100 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001101
1102 if (vcpu->arch.ceded) {
1103 if (ret != RESUME_GUEST)
1104 kvmppc_end_cede(vcpu);
1105 else
1106 kvmppc_set_timer(vcpu);
1107 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001108 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001109
Paul Mackerras371fefd2011-06-29 00:23:08 +00001110 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001111 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001112 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras0456ec42012-02-03 00:56:21 +00001113 vc->preempt_tb = mftb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001114 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1115 arch.run_list) {
1116 if (vcpu->arch.ret != RESUME_GUEST) {
1117 kvmppc_remove_runnable(vc, vcpu);
1118 wake_up(&vcpu->arch.cpu_run);
1119 }
1120 }
1121
1122 return 1;
1123}
1124
Paul Mackerras19ccb762011-07-23 17:42:46 +10001125/*
1126 * Wait for some other vcpu thread to execute us, and
1127 * wake us up when we need to handle something in the host.
1128 */
1129static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001130{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001131 DEFINE_WAIT(wait);
1132
Paul Mackerras19ccb762011-07-23 17:42:46 +10001133 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1134 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1135 schedule();
1136 finish_wait(&vcpu->arch.cpu_run, &wait);
1137}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001138
Paul Mackerras19ccb762011-07-23 17:42:46 +10001139/*
1140 * All the vcpus in this vcore are idle, so wait for a decrementer
1141 * or external interrupt to one of the vcpus. vc->lock is held.
1142 */
1143static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1144{
1145 DEFINE_WAIT(wait);
1146 struct kvm_vcpu *v;
1147 int all_idle = 1;
1148
1149 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1150 vc->vcore_state = VCORE_SLEEPING;
1151 spin_unlock(&vc->lock);
1152 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
1153 if (!v->arch.ceded || v->arch.pending_exceptions) {
1154 all_idle = 0;
1155 break;
1156 }
1157 }
1158 if (all_idle)
1159 schedule();
1160 finish_wait(&vc->wq, &wait);
1161 spin_lock(&vc->lock);
1162 vc->vcore_state = VCORE_INACTIVE;
1163}
1164
1165static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1166{
1167 int n_ceded;
1168 int prev_state;
1169 struct kvmppc_vcore *vc;
1170 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001171
Paul Mackerras371fefd2011-06-29 00:23:08 +00001172 kvm_run->exit_reason = 0;
1173 vcpu->arch.ret = RESUME_GUEST;
1174 vcpu->arch.trap = 0;
1175
Paul Mackerras371fefd2011-06-29 00:23:08 +00001176 /*
1177 * Synchronize with other threads in this virtual core
1178 */
1179 vc = vcpu->arch.vcore;
1180 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001181 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001182 vcpu->arch.run_task = current;
1183 vcpu->arch.kvm_run = kvm_run;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001184 prev_state = vcpu->arch.state;
1185 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001186 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1187 ++vc->n_runnable;
1188
Paul Mackerras19ccb762011-07-23 17:42:46 +10001189 /*
1190 * This happens the first time this is called for a vcpu.
1191 * If the vcore is already running, we may be able to start
1192 * this thread straight away and have it join in.
1193 */
1194 if (prev_state == KVMPPC_VCPU_STOPPED) {
1195 if (vc->vcore_state == VCORE_RUNNING &&
1196 VCORE_EXIT_COUNT(vc) == 0) {
1197 vcpu->arch.ptid = vc->n_runnable - 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001198 kvmppc_start_thread(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001199 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001200
Paul Mackerras19ccb762011-07-23 17:42:46 +10001201 } else if (prev_state == KVMPPC_VCPU_BUSY_IN_HOST)
1202 --vc->n_busy;
1203
1204 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1205 !signal_pending(current)) {
1206 if (vc->n_busy || vc->vcore_state != VCORE_INACTIVE) {
1207 spin_unlock(&vc->lock);
1208 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1209 spin_lock(&vc->lock);
1210 continue;
1211 }
Paul Mackerras0456ec42012-02-03 00:56:21 +00001212 vc->runner = vcpu;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001213 n_ceded = 0;
1214 list_for_each_entry(v, &vc->runnable_threads, arch.run_list)
1215 n_ceded += v->arch.ceded;
1216 if (n_ceded == vc->n_runnable)
1217 kvmppc_vcore_blocked(vc);
1218 else
1219 kvmppc_run_core(vc);
1220
1221 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1222 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001223 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001224 if (signal_pending(v->arch.run_task)) {
1225 kvmppc_remove_runnable(vc, v);
1226 v->stat.signal_exits++;
1227 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1228 v->arch.ret = -EINTR;
1229 wake_up(&v->arch.cpu_run);
1230 }
1231 }
Paul Mackerras0456ec42012-02-03 00:56:21 +00001232 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001233 }
1234
Paul Mackerras19ccb762011-07-23 17:42:46 +10001235 if (signal_pending(current)) {
1236 if (vc->vcore_state == VCORE_RUNNING ||
1237 vc->vcore_state == VCORE_EXITING) {
1238 spin_unlock(&vc->lock);
1239 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1240 spin_lock(&vc->lock);
1241 }
1242 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1243 kvmppc_remove_runnable(vc, vcpu);
1244 vcpu->stat.signal_exits++;
1245 kvm_run->exit_reason = KVM_EXIT_INTR;
1246 vcpu->arch.ret = -EINTR;
1247 }
1248 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001249
Paul Mackerras19ccb762011-07-23 17:42:46 +10001250 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001251 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001252}
1253
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001254int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
1255{
1256 int r;
1257
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001258 if (!vcpu->arch.sane) {
1259 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1260 return -EINVAL;
1261 }
1262
Scott Wood25051b52011-11-08 18:23:23 -06001263 kvmppc_core_prepare_to_enter(vcpu);
1264
Paul Mackerras19ccb762011-07-23 17:42:46 +10001265 /* No need to go into the guest when all we'll do is come back out */
1266 if (signal_pending(current)) {
1267 run->exit_reason = KVM_EXIT_INTR;
1268 return -EINTR;
1269 }
1270
Paul Mackerras32fad282012-05-04 02:32:53 +00001271 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1272 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1273 smp_mb();
1274
1275 /* On the first time here, set up HTAB and VRMA or RMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001276 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001277 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001278 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001279 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001280 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001281
1282 flush_fp_to_thread(current);
1283 flush_altivec_to_thread(current);
1284 flush_vsx_to_thread(current);
1285 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001286 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001287
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001288 do {
1289 r = kvmppc_run_vcpu(run, vcpu);
1290
1291 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1292 !(vcpu->arch.shregs.msr & MSR_PR)) {
1293 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001294 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001295 }
1296 } while (r == RESUME_GUEST);
Paul Mackerras32fad282012-05-04 02:32:53 +00001297
1298 out:
1299 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001300 return r;
1301}
1302
David Gibson54738c02011-06-29 00:22:41 +00001303
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001304/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +00001305 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001306static inline int lpcr_rmls(unsigned long rma_size)
1307{
1308 switch (rma_size) {
1309 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001310 if (cpu_has_feature(CPU_FTR_ARCH_206))
1311 return 8; /* only supported on POWER7 */
1312 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001313 case 64ul << 20: /* 64 MB */
1314 return 3;
1315 case 128ul << 20: /* 128 MB */
1316 return 7;
1317 case 256ul << 20: /* 256 MB */
1318 return 4;
1319 case 1ul << 30: /* 1 GB */
1320 return 2;
1321 case 16ul << 30: /* 16 GB */
1322 return 1;
1323 case 256ul << 30: /* 256 GB */
1324 return 0;
1325 default:
1326 return -1;
1327 }
1328}
1329
1330static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1331{
Alexander Grafb4e70612012-01-16 16:50:10 +01001332 struct kvmppc_linear_info *ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001333 struct page *page;
1334
1335 if (vmf->pgoff >= ri->npages)
1336 return VM_FAULT_SIGBUS;
1337
1338 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1339 get_page(page);
1340 vmf->page = page;
1341 return 0;
1342}
1343
1344static const struct vm_operations_struct kvm_rma_vm_ops = {
1345 .fault = kvm_rma_fault,
1346};
1347
1348static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1349{
1350 vma->vm_flags |= VM_RESERVED;
1351 vma->vm_ops = &kvm_rma_vm_ops;
1352 return 0;
1353}
1354
1355static int kvm_rma_release(struct inode *inode, struct file *filp)
1356{
Alexander Grafb4e70612012-01-16 16:50:10 +01001357 struct kvmppc_linear_info *ri = filp->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001358
1359 kvm_release_rma(ri);
1360 return 0;
1361}
1362
1363static struct file_operations kvm_rma_fops = {
1364 .mmap = kvm_rma_mmap,
1365 .release = kvm_rma_release,
1366};
1367
1368long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
1369{
Alexander Grafb4e70612012-01-16 16:50:10 +01001370 struct kvmppc_linear_info *ri;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001371 long fd;
1372
1373 ri = kvm_alloc_rma();
1374 if (!ri)
1375 return -ENOMEM;
1376
1377 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR);
1378 if (fd < 0)
1379 kvm_release_rma(ri);
1380
1381 ret->rma_size = ri->npages << PAGE_SHIFT;
1382 return fd;
1383}
1384
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001385static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1386 int linux_psize)
1387{
1388 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1389
1390 if (!def->shift)
1391 return;
1392 (*sps)->page_shift = def->shift;
1393 (*sps)->slb_enc = def->sllp;
1394 (*sps)->enc[0].page_shift = def->shift;
1395 (*sps)->enc[0].pte_enc = def->penc;
1396 (*sps)++;
1397}
1398
1399int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
1400{
1401 struct kvm_ppc_one_seg_page_size *sps;
1402
1403 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1404 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1405 info->flags |= KVM_PPC_1T_SEGMENTS;
1406 info->slb_size = mmu_slb_size;
1407
1408 /* We only support these sizes for now, and no muti-size segments */
1409 sps = &info->sps[0];
1410 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1411 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1412 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1413
1414 return 0;
1415}
1416
Paul Mackerras82ed3612011-12-15 02:03:22 +00001417/*
1418 * Get (and clear) the dirty memory log for a memory slot.
1419 */
1420int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1421{
1422 struct kvm_memory_slot *memslot;
1423 int r;
1424 unsigned long n;
1425
1426 mutex_lock(&kvm->slots_lock);
1427
1428 r = -EINVAL;
1429 if (log->slot >= KVM_MEMORY_SLOTS)
1430 goto out;
1431
1432 memslot = id_to_memslot(kvm->memslots, log->slot);
1433 r = -ENOENT;
1434 if (!memslot->dirty_bitmap)
1435 goto out;
1436
1437 n = kvm_dirty_bitmap_bytes(memslot);
1438 memset(memslot->dirty_bitmap, 0, n);
1439
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001440 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00001441 if (r)
1442 goto out;
1443
1444 r = -EFAULT;
1445 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1446 goto out;
1447
1448 r = 0;
1449out:
1450 mutex_unlock(&kvm->slots_lock);
1451 return r;
1452}
1453
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001454static unsigned long slb_pgsize_encoding(unsigned long psize)
1455{
1456 unsigned long senc = 0;
1457
1458 if (psize > 0x1000) {
1459 senc = SLB_VSID_L;
1460 if (psize == 0x10000)
1461 senc |= SLB_VSID_LP_01;
1462 }
1463 return senc;
1464}
1465
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001466static void unpin_slot(struct kvm_memory_slot *memslot)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001467{
1468 unsigned long *physp;
1469 unsigned long j, npages, pfn;
1470 struct page *page;
1471
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001472 physp = memslot->arch.slot_phys;
1473 npages = memslot->npages;
1474 if (!physp)
1475 return;
1476 for (j = 0; j < npages; j++) {
1477 if (!(physp[j] & KVMPPC_GOT_PAGE))
1478 continue;
1479 pfn = physp[j] >> PAGE_SHIFT;
1480 page = pfn_to_page(pfn);
1481 SetPageDirty(page);
1482 put_page(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001483 }
1484}
1485
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001486void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1487 struct kvm_memory_slot *dont)
1488{
1489 if (!dont || free->arch.rmap != dont->arch.rmap) {
1490 vfree(free->arch.rmap);
1491 free->arch.rmap = NULL;
1492 }
1493 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1494 unpin_slot(free);
1495 vfree(free->arch.slot_phys);
1496 free->arch.slot_phys = NULL;
1497 }
1498}
1499
1500int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1501 unsigned long npages)
1502{
1503 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1504 if (!slot->arch.rmap)
1505 return -ENOMEM;
1506 slot->arch.slot_phys = NULL;
1507
1508 return 0;
1509}
1510
1511int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1512 struct kvm_memory_slot *memslot,
1513 struct kvm_userspace_memory_region *mem)
1514{
1515 unsigned long *phys;
1516
1517 /* Allocate a slot_phys array if needed */
1518 phys = memslot->arch.slot_phys;
1519 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1520 phys = vzalloc(memslot->npages * sizeof(unsigned long));
1521 if (!phys)
1522 return -ENOMEM;
1523 memslot->arch.slot_phys = phys;
1524 }
1525
1526 return 0;
1527}
1528
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001529void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001530 struct kvm_userspace_memory_region *mem,
1531 struct kvm_memory_slot old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001532{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001533 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1534 struct kvm_memory_slot *memslot;
1535
1536 if (npages && old.npages) {
1537 /*
1538 * If modifying a memslot, reset all the rmap dirty bits.
1539 * If this is a new memslot, we don't need to do anything
1540 * since the rmap array starts out as all zeroes,
1541 * i.e. no pages are dirty.
1542 */
1543 memslot = id_to_memslot(kvm->memslots, mem->slot);
1544 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1545 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001546}
1547
Paul Mackerras32fad282012-05-04 02:32:53 +00001548static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001549{
1550 int err = 0;
1551 struct kvm *kvm = vcpu->kvm;
Alexander Grafb4e70612012-01-16 16:50:10 +01001552 struct kvmppc_linear_info *ri = NULL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001553 unsigned long hva;
1554 struct kvm_memory_slot *memslot;
1555 struct vm_area_struct *vma;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001556 unsigned long lpcr, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001557 unsigned long psize, porder;
1558 unsigned long rma_size;
1559 unsigned long rmls;
1560 unsigned long *physp;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001561 unsigned long i, npages;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001562 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001563
1564 mutex_lock(&kvm->lock);
1565 if (kvm->arch.rma_setup_done)
1566 goto out; /* another vcpu beat us to it */
1567
Paul Mackerras32fad282012-05-04 02:32:53 +00001568 /* Allocate hashed page table (if not done already) and reset it */
1569 if (!kvm->arch.hpt_virt) {
1570 err = kvmppc_alloc_hpt(kvm, NULL);
1571 if (err) {
1572 pr_err("KVM: Couldn't alloc HPT\n");
1573 goto out;
1574 }
1575 }
1576
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001577 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001578 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001579 memslot = gfn_to_memslot(kvm, 0);
1580
1581 /* We must have some memory at 0 by now */
1582 err = -EINVAL;
1583 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001584 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001585
1586 /* Look up the VMA for the start of this memory slot */
1587 hva = memslot->userspace_addr;
1588 down_read(&current->mm->mmap_sem);
1589 vma = find_vma(current->mm, hva);
1590 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
1591 goto up_out;
1592
1593 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001594 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001595
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001596 /* Is this one of our preallocated RMAs? */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001597 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
1598 hva == vma->vm_start)
1599 ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001600
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001601 up_read(&current->mm->mmap_sem);
1602
1603 if (!ri) {
1604 /* On POWER7, use VRMA; on PPC970, give up */
1605 err = -EPERM;
1606 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1607 pr_err("KVM: CPU requires an RMO\n");
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001608 goto out_srcu;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001609 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001610
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001611 /* We can handle 4k, 64k or 16M pages in the VRMA */
1612 err = -EINVAL;
1613 if (!(psize == 0x1000 || psize == 0x10000 ||
1614 psize == 0x1000000))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001615 goto out_srcu;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001616
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001617 /* Update VRMASD field in the LPCR */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001618 senc = slb_pgsize_encoding(psize);
Paul Mackerras697d3892011-12-12 12:36:37 +00001619 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1620 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001621 lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
1622 lpcr |= senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001623 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001624
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001625 /* Create HPTEs in the hash page table for the VRMA */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001626 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001627
1628 } else {
1629 /* Set up to use an RMO region */
1630 rma_size = ri->npages;
1631 if (rma_size > memslot->npages)
1632 rma_size = memslot->npages;
1633 rma_size <<= PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001634 rmls = lpcr_rmls(rma_size);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001635 err = -EINVAL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001636 if (rmls < 0) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001637 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001638 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001639 }
1640 atomic_inc(&ri->use_count);
1641 kvm->arch.rma = ri;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001642
1643 /* Update LPCR and RMOR */
1644 lpcr = kvm->arch.lpcr;
1645 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1646 /* PPC970; insert RMLS value (split field) in HID4 */
1647 lpcr &= ~((1ul << HID4_RMLS0_SH) |
1648 (3ul << HID4_RMLS2_SH));
1649 lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
1650 ((rmls & 3) << HID4_RMLS2_SH);
1651 /* RMOR is also in HID4 */
1652 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
1653 << HID4_RMOR_SH;
1654 } else {
1655 /* POWER7 */
1656 lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
1657 lpcr |= rmls << LPCR_RMLS_SH;
1658 kvm->arch.rmor = kvm->arch.rma->base_pfn << PAGE_SHIFT;
1659 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001660 kvm->arch.lpcr = lpcr;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001661 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001662 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001663
1664 /* Initialize phys addrs of pages in RMO */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001665 npages = ri->npages;
1666 porder = __ilog2(npages);
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001667 physp = memslot->arch.slot_phys;
1668 if (physp) {
1669 if (npages > memslot->npages)
1670 npages = memslot->npages;
1671 spin_lock(&kvm->arch.slot_phys_lock);
1672 for (i = 0; i < npages; ++i)
1673 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
1674 porder;
1675 spin_unlock(&kvm->arch.slot_phys_lock);
1676 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001677 }
1678
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001679 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
1680 smp_wmb();
1681 kvm->arch.rma_setup_done = 1;
1682 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001683 out_srcu:
1684 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001685 out:
1686 mutex_unlock(&kvm->lock);
1687 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001688
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001689 up_out:
1690 up_read(&current->mm->mmap_sem);
1691 goto out;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001692}
1693
1694int kvmppc_core_init_vm(struct kvm *kvm)
1695{
Paul Mackerras32fad282012-05-04 02:32:53 +00001696 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001697
Paul Mackerras32fad282012-05-04 02:32:53 +00001698 /* Allocate the guest's logical partition ID */
1699
1700 lpid = kvmppc_alloc_lpid();
1701 if (lpid < 0)
1702 return -ENOMEM;
1703 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001704
David Gibson54738c02011-06-29 00:22:41 +00001705 INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001706
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001707 kvm->arch.rma = NULL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001708
Paul Mackerras9e368f22011-06-29 00:40:08 +00001709 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001710
Paul Mackerras9e368f22011-06-29 00:40:08 +00001711 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1712 /* PPC970; HID4 is effectively the LPCR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001713 kvm->arch.host_lpid = 0;
1714 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
1715 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
1716 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
1717 ((lpid & 0xf) << HID4_LPID5_SH);
1718 } else {
1719 /* POWER7; init LPCR for virtual RMA mode */
1720 kvm->arch.host_lpid = mfspr(SPRN_LPID);
1721 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
1722 lpcr &= LPCR_PECE | LPCR_LPES;
1723 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
Paul Mackerras697d3892011-12-12 12:36:37 +00001724 LPCR_VPM0 | LPCR_VPM1;
1725 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
1726 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerras9e368f22011-06-29 00:40:08 +00001727 }
1728 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001729
Paul Mackerras342d3db2011-12-12 12:38:05 +00001730 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001731 spin_lock_init(&kvm->arch.slot_phys_lock);
Paul Mackerras512691d2012-10-15 01:15:41 +00001732
1733 /*
1734 * Don't allow secondary CPU threads to come online
1735 * while any KVM VMs exist.
1736 */
1737 inhibit_secondary_onlining();
1738
David Gibson54738c02011-06-29 00:22:41 +00001739 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001740}
1741
1742void kvmppc_core_destroy_vm(struct kvm *kvm)
1743{
Paul Mackerras512691d2012-10-15 01:15:41 +00001744 uninhibit_secondary_onlining();
1745
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001746 if (kvm->arch.rma) {
1747 kvm_release_rma(kvm->arch.rma);
1748 kvm->arch.rma = NULL;
1749 }
1750
Paul Mackerrasde56a942011-06-29 00:21:34 +00001751 kvmppc_free_hpt(kvm);
David Gibson54738c02011-06-29 00:22:41 +00001752 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
Paul Mackerrasde56a942011-06-29 00:21:34 +00001753}
1754
1755/* These are stubs for now */
1756void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
1757{
1758}
1759
1760/* We don't need to emulate any privileged instructions or dcbz */
1761int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
1762 unsigned int inst, int *advance)
1763{
1764 return EMULATE_FAIL;
1765}
1766
Alexander Graf54771e62012-05-04 14:55:12 +02001767int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001768{
1769 return EMULATE_FAIL;
1770}
1771
Alexander Graf54771e62012-05-04 14:55:12 +02001772int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001773{
1774 return EMULATE_FAIL;
1775}
1776
1777static int kvmppc_book3s_hv_init(void)
1778{
1779 int r;
1780
1781 r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1782
1783 if (r)
1784 return r;
1785
1786 r = kvmppc_mmu_hv_init();
1787
1788 return r;
1789}
1790
1791static void kvmppc_book3s_hv_exit(void)
1792{
1793 kvm_exit();
1794}
1795
1796module_init(kvmppc_book3s_hv_init);
1797module_exit(kvmppc_book3s_hv_exit);