blob: 9a15da76e56bed6aa702e5d0682abc1e6983867e [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 Mackerrasde56a942011-06-29 00:21:34 +000050#include <linux/gfp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000051#include <linux/vmalloc.h>
52#include <linux/highmem.h>
Paul Mackerrasc77162d2011-12-12 12:31:00 +000053#include <linux/hugetlb.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000054
55/* #define EXIT_DEBUG */
56/* #define EXIT_DEBUG_SIMPLE */
57/* #define EXIT_DEBUG_INT */
58
Paul Mackerras19ccb762011-07-23 17:42:46 +100059static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +000060static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +100061
Paul Mackerrasde56a942011-06-29 00:21:34 +000062void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
63{
Paul Mackerras0456ec42012-02-03 00:56:21 +000064 struct kvmppc_vcore *vc = vcpu->arch.vcore;
65
Paul Mackerrasde56a942011-06-29 00:21:34 +000066 local_paca->kvm_hstate.kvm_vcpu = vcpu;
Paul Mackerras0456ec42012-02-03 00:56:21 +000067 local_paca->kvm_hstate.kvm_vcore = vc;
68 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
69 vc->stolen_tb += mftb() - vc->preempt_tb;
Paul Mackerrasde56a942011-06-29 00:21:34 +000070}
71
72void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
73{
Paul Mackerras0456ec42012-02-03 00:56:21 +000074 struct kvmppc_vcore *vc = vcpu->arch.vcore;
75
76 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
77 vc->preempt_tb = mftb();
Paul Mackerrasde56a942011-06-29 00:21:34 +000078}
79
Paul Mackerrasde56a942011-06-29 00:21:34 +000080void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
81{
82 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +100083 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +000084}
85
86void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
87{
88 vcpu->arch.pvr = pvr;
89}
90
91void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
92{
93 int r;
94
95 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
96 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
97 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
98 for (r = 0; r < 16; ++r)
99 pr_err("r%2d = %.16lx r%d = %.16lx\n",
100 r, kvmppc_get_gpr(vcpu, r),
101 r+16, kvmppc_get_gpr(vcpu, r+16));
102 pr_err("ctr = %.16lx lr = %.16lx\n",
103 vcpu->arch.ctr, vcpu->arch.lr);
104 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
105 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
106 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
107 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
108 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
109 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
110 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
111 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
112 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
113 pr_err("fault dar = %.16lx dsisr = %.8x\n",
114 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
115 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
116 for (r = 0; r < vcpu->arch.slb_max; ++r)
117 pr_err(" ESID = %.16llx VSID = %.16llx\n",
118 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
119 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000120 vcpu->kvm->arch.lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000121 vcpu->arch.last_inst);
122}
123
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000124struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
125{
126 int r;
127 struct kvm_vcpu *v, *ret = NULL;
128
129 mutex_lock(&kvm->lock);
130 kvm_for_each_vcpu(r, v, kvm) {
131 if (v->vcpu_id == id) {
132 ret = v;
133 break;
134 }
135 }
136 mutex_unlock(&kvm->lock);
137 return ret;
138}
139
140static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
141{
142 vpa->shared_proc = 1;
143 vpa->yield_count = 1;
144}
145
Paul Mackerras55b665b2012-09-25 20:33:06 +0000146static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
147 unsigned long addr, unsigned long len)
148{
149 /* check address is cacheline aligned */
150 if (addr & (L1_CACHE_BYTES - 1))
151 return -EINVAL;
152 spin_lock(&vcpu->arch.vpa_update_lock);
153 if (v->next_gpa != addr || v->len != len) {
154 v->next_gpa = addr;
155 v->len = addr ? len : 0;
156 v->update_pending = 1;
157 }
158 spin_unlock(&vcpu->arch.vpa_update_lock);
159 return 0;
160}
161
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000162/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
163struct reg_vpa {
164 u32 dummy;
165 union {
166 u16 hword;
167 u32 word;
168 } length;
169};
170
171static int vpa_is_registered(struct kvmppc_vpa *vpap)
172{
173 if (vpap->update_pending)
174 return vpap->next_gpa != 0;
175 return vpap->pinned_addr != NULL;
176}
177
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000178static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
179 unsigned long flags,
180 unsigned long vcpuid, unsigned long vpa)
181{
182 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000183 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000184 void *va;
185 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000186 int err;
187 int subfunc;
188 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000189
190 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
191 if (!tvcpu)
192 return H_PARAMETER;
193
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000194 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
195 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
196 subfunc == H_VPA_REG_SLB) {
197 /* Registering new area - address must be cache-line aligned */
198 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000199 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000200
201 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000202 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
203 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000204 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000205 if (subfunc == H_VPA_REG_VPA)
206 len = ((struct reg_vpa *)va)->length.hword;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000207 else
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000208 len = ((struct reg_vpa *)va)->length.word;
209 kvmppc_unpin_guest_page(kvm, va);
210
211 /* Check length */
212 if (len > nb || len < sizeof(struct reg_vpa))
213 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000214 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000215 vpa = 0;
216 len = 0;
217 }
218
219 err = H_PARAMETER;
220 vpap = NULL;
221 spin_lock(&tvcpu->arch.vpa_update_lock);
222
223 switch (subfunc) {
224 case H_VPA_REG_VPA: /* register VPA */
225 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000226 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000227 vpap = &tvcpu->arch.vpa;
228 err = 0;
229 break;
230
231 case H_VPA_REG_DTL: /* register DTL */
232 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000233 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000234 len -= len % sizeof(struct dtl_entry);
235
236 /* Check that they have previously registered a VPA */
237 err = H_RESOURCE;
238 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000239 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000240
241 vpap = &tvcpu->arch.dtl;
242 err = 0;
243 break;
244
245 case H_VPA_REG_SLB: /* register SLB shadow buffer */
246 /* Check that they have previously registered a VPA */
247 err = H_RESOURCE;
248 if (!vpa_is_registered(&tvcpu->arch.vpa))
249 break;
250
251 vpap = &tvcpu->arch.slb_shadow;
252 err = 0;
253 break;
254
255 case H_VPA_DEREG_VPA: /* deregister VPA */
256 /* Check they don't still have a DTL or SLB buf registered */
257 err = H_RESOURCE;
258 if (vpa_is_registered(&tvcpu->arch.dtl) ||
259 vpa_is_registered(&tvcpu->arch.slb_shadow))
260 break;
261
262 vpap = &tvcpu->arch.vpa;
263 err = 0;
264 break;
265
266 case H_VPA_DEREG_DTL: /* deregister DTL */
267 vpap = &tvcpu->arch.dtl;
268 err = 0;
269 break;
270
271 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
272 vpap = &tvcpu->arch.slb_shadow;
273 err = 0;
274 break;
275 }
276
277 if (vpap) {
278 vpap->next_gpa = vpa;
279 vpap->len = len;
280 vpap->update_pending = 1;
281 }
282
283 spin_unlock(&tvcpu->arch.vpa_update_lock);
284
285 return err;
286}
287
Paul Mackerras081f3232012-06-01 20:20:24 +1000288static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000289{
Paul Mackerras081f3232012-06-01 20:20:24 +1000290 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000291 void *va;
292 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000293 unsigned long gpa;
294
295 /*
296 * We need to pin the page pointed to by vpap->next_gpa,
297 * but we can't call kvmppc_pin_guest_page under the lock
298 * as it does get_user_pages() and down_read(). So we
299 * have to drop the lock, pin the page, then get the lock
300 * again and check that a new area didn't get registered
301 * in the meantime.
302 */
303 for (;;) {
304 gpa = vpap->next_gpa;
305 spin_unlock(&vcpu->arch.vpa_update_lock);
306 va = NULL;
307 nb = 0;
308 if (gpa)
309 va = kvmppc_pin_guest_page(kvm, vpap->next_gpa, &nb);
310 spin_lock(&vcpu->arch.vpa_update_lock);
311 if (gpa == vpap->next_gpa)
312 break;
313 /* sigh... unpin that one and try again */
314 if (va)
315 kvmppc_unpin_guest_page(kvm, va);
316 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000317
318 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000319 if (va && nb < vpap->len) {
320 /*
321 * If it's now too short, it must be that userspace
322 * has changed the mappings underlying guest memory,
323 * so unregister the region.
324 */
325 kvmppc_unpin_guest_page(kvm, va);
326 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000327 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000328 if (vpap->pinned_addr)
329 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr);
330 vpap->pinned_addr = va;
331 if (va)
332 vpap->pinned_end = va + vpap->len;
333}
Paul Mackerras93e60242011-12-12 12:28:55 +0000334
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000335static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
336{
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000337 spin_lock(&vcpu->arch.vpa_update_lock);
338 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000339 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000340 if (vcpu->arch.vpa.pinned_addr)
341 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000342 }
343 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000344 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000345 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
346 vcpu->arch.dtl_index = 0;
347 }
348 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000349 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000350 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000351}
352
Paul Mackerras0456ec42012-02-03 00:56:21 +0000353static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
354 struct kvmppc_vcore *vc)
355{
356 struct dtl_entry *dt;
357 struct lppaca *vpa;
358 unsigned long old_stolen;
359
360 dt = vcpu->arch.dtl_ptr;
361 vpa = vcpu->arch.vpa.pinned_addr;
362 old_stolen = vcpu->arch.stolen_logged;
363 vcpu->arch.stolen_logged = vc->stolen_tb;
364 if (!dt || !vpa)
365 return;
366 memset(dt, 0, sizeof(struct dtl_entry));
367 dt->dispatch_reason = 7;
368 dt->processor_id = vc->pcpu + vcpu->arch.ptid;
369 dt->timebase = mftb();
370 dt->enqueue_to_dispatch_time = vc->stolen_tb - old_stolen;
371 dt->srr0 = kvmppc_get_pc(vcpu);
372 dt->srr1 = vcpu->arch.shregs.msr;
373 ++dt;
374 if (dt == vcpu->arch.dtl.pinned_end)
375 dt = vcpu->arch.dtl.pinned_addr;
376 vcpu->arch.dtl_ptr = dt;
377 /* order writing *dt vs. writing vpa->dtl_idx */
378 smp_wmb();
379 vpa->dtl_idx = ++vcpu->arch.dtl_index;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000380}
381
382int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
383{
384 unsigned long req = kvmppc_get_gpr(vcpu, 3);
385 unsigned long target, ret = H_SUCCESS;
386 struct kvm_vcpu *tvcpu;
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000387 int idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000388
389 switch (req) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000390 case H_ENTER:
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000391 idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000392 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
393 kvmppc_get_gpr(vcpu, 5),
394 kvmppc_get_gpr(vcpu, 6),
395 kvmppc_get_gpr(vcpu, 7));
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000396 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000397 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000398 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000399 break;
400 case H_PROD:
401 target = kvmppc_get_gpr(vcpu, 4);
402 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
403 if (!tvcpu) {
404 ret = H_PARAMETER;
405 break;
406 }
407 tvcpu->arch.prodded = 1;
408 smp_mb();
409 if (vcpu->arch.ceded) {
410 if (waitqueue_active(&vcpu->wq)) {
411 wake_up_interruptible(&vcpu->wq);
412 vcpu->stat.halt_wakeup++;
413 }
414 }
415 break;
416 case H_CONFER:
417 break;
418 case H_REGISTER_VPA:
419 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
420 kvmppc_get_gpr(vcpu, 5),
421 kvmppc_get_gpr(vcpu, 6));
422 break;
423 default:
424 return RESUME_HOST;
425 }
426 kvmppc_set_gpr(vcpu, 3, ret);
427 vcpu->arch.hcall_needed = 0;
428 return RESUME_GUEST;
429}
430
Paul Mackerrasde56a942011-06-29 00:21:34 +0000431static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
432 struct task_struct *tsk)
433{
434 int r = RESUME_HOST;
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000435 int srcu_idx;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000436
437 vcpu->stat.sum_exits++;
438
439 run->exit_reason = KVM_EXIT_UNKNOWN;
440 run->ready_for_interrupt_injection = 1;
441 switch (vcpu->arch.trap) {
442 /* We're good on these - the host merely wanted to get our attention */
443 case BOOK3S_INTERRUPT_HV_DECREMENTER:
444 vcpu->stat.dec_exits++;
445 r = RESUME_GUEST;
446 break;
447 case BOOK3S_INTERRUPT_EXTERNAL:
448 vcpu->stat.ext_intr_exits++;
449 r = RESUME_GUEST;
450 break;
451 case BOOK3S_INTERRUPT_PERFMON:
452 r = RESUME_GUEST;
453 break;
454 case BOOK3S_INTERRUPT_PROGRAM:
455 {
456 ulong flags;
457 /*
458 * Normally program interrupts are delivered directly
459 * to the guest by the hardware, but we can get here
460 * as a result of a hypervisor emulation interrupt
461 * (e40) getting turned into a 700 by BML RTAS.
462 */
463 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
464 kvmppc_core_queue_program(vcpu, flags);
465 r = RESUME_GUEST;
466 break;
467 }
468 case BOOK3S_INTERRUPT_SYSCALL:
469 {
470 /* hcall - punt to userspace */
471 int i;
472
473 if (vcpu->arch.shregs.msr & MSR_PR) {
474 /* sc 1 from userspace - reflect to guest syscall */
475 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
476 r = RESUME_GUEST;
477 break;
478 }
479 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
480 for (i = 0; i < 9; ++i)
481 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
482 run->exit_reason = KVM_EXIT_PAPR_HCALL;
483 vcpu->arch.hcall_needed = 1;
484 r = RESUME_HOST;
485 break;
486 }
487 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000488 * We get these next two if the guest accesses a page which it thinks
489 * it has mapped but which is not actually present, either because
490 * it is for an emulated I/O device or because the corresonding
491 * host page has been paged out. Any other HDSI/HISI interrupts
492 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000493 */
494 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000495 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerras697d3892011-12-12 12:36:37 +0000496 r = kvmppc_book3s_hv_page_fault(run, vcpu,
497 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000498 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000499 break;
500 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000501 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000502 r = kvmppc_book3s_hv_page_fault(run, vcpu,
503 kvmppc_get_pc(vcpu), 0);
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000504 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000505 break;
506 /*
507 * This occurs if the guest executes an illegal instruction.
508 * We just generate a program interrupt to the guest, since
509 * we don't emulate any guest instructions at this stage.
510 */
511 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
512 kvmppc_core_queue_program(vcpu, 0x80000);
513 r = RESUME_GUEST;
514 break;
515 default:
516 kvmppc_dump_regs(vcpu);
517 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
518 vcpu->arch.trap, kvmppc_get_pc(vcpu),
519 vcpu->arch.shregs.msr);
520 r = RESUME_HOST;
521 BUG();
522 break;
523 }
524
Paul Mackerrasde56a942011-06-29 00:21:34 +0000525 return r;
526}
527
528int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
529 struct kvm_sregs *sregs)
530{
531 int i;
532
533 sregs->pvr = vcpu->arch.pvr;
534
535 memset(sregs, 0, sizeof(struct kvm_sregs));
536 for (i = 0; i < vcpu->arch.slb_max; i++) {
537 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
538 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
539 }
540
541 return 0;
542}
543
544int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
545 struct kvm_sregs *sregs)
546{
547 int i, j;
548
549 kvmppc_set_pvr(vcpu, sregs->pvr);
550
551 j = 0;
552 for (i = 0; i < vcpu->arch.slb_nr; i++) {
553 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
554 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
555 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
556 ++j;
557 }
558 }
559 vcpu->arch.slb_max = j;
560
561 return 0;
562}
563
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000564int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000565{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000566 int r = 0;
567 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +0000568
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000569 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000570 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000571 *val = get_reg_val(id, 0);
572 break;
573 case KVM_REG_PPC_DABR:
574 *val = get_reg_val(id, vcpu->arch.dabr);
575 break;
576 case KVM_REG_PPC_DSCR:
577 *val = get_reg_val(id, vcpu->arch.dscr);
578 break;
579 case KVM_REG_PPC_PURR:
580 *val = get_reg_val(id, vcpu->arch.purr);
581 break;
582 case KVM_REG_PPC_SPURR:
583 *val = get_reg_val(id, vcpu->arch.spurr);
584 break;
585 case KVM_REG_PPC_AMR:
586 *val = get_reg_val(id, vcpu->arch.amr);
587 break;
588 case KVM_REG_PPC_UAMOR:
589 *val = get_reg_val(id, vcpu->arch.uamor);
590 break;
591 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
592 i = id - KVM_REG_PPC_MMCR0;
593 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
594 break;
595 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
596 i = id - KVM_REG_PPC_PMC1;
597 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +0000598 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000599#ifdef CONFIG_VSX
600 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
601 if (cpu_has_feature(CPU_FTR_VSX)) {
602 /* VSX => FP reg i is stored in arch.vsr[2*i] */
603 long int i = id - KVM_REG_PPC_FPR0;
604 *val = get_reg_val(id, vcpu->arch.vsr[2 * i]);
605 } else {
606 /* let generic code handle it */
607 r = -EINVAL;
608 }
609 break;
610 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
611 if (cpu_has_feature(CPU_FTR_VSX)) {
612 long int i = id - KVM_REG_PPC_VSR0;
613 val->vsxval[0] = vcpu->arch.vsr[2 * i];
614 val->vsxval[1] = vcpu->arch.vsr[2 * i + 1];
615 } else {
616 r = -ENXIO;
617 }
618 break;
619#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000620 case KVM_REG_PPC_VPA_ADDR:
621 spin_lock(&vcpu->arch.vpa_update_lock);
622 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
623 spin_unlock(&vcpu->arch.vpa_update_lock);
624 break;
625 case KVM_REG_PPC_VPA_SLB:
626 spin_lock(&vcpu->arch.vpa_update_lock);
627 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
628 val->vpaval.length = vcpu->arch.slb_shadow.len;
629 spin_unlock(&vcpu->arch.vpa_update_lock);
630 break;
631 case KVM_REG_PPC_VPA_DTL:
632 spin_lock(&vcpu->arch.vpa_update_lock);
633 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
634 val->vpaval.length = vcpu->arch.dtl.len;
635 spin_unlock(&vcpu->arch.vpa_update_lock);
636 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000637 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000638 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000639 break;
640 }
641
642 return r;
643}
644
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000645int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +0000646{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000647 int r = 0;
648 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +0000649 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +0000650
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000651 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +0000652 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +0000653 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000654 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +0000655 r = -EINVAL;
656 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000657 case KVM_REG_PPC_DABR:
658 vcpu->arch.dabr = set_reg_val(id, *val);
659 break;
660 case KVM_REG_PPC_DSCR:
661 vcpu->arch.dscr = set_reg_val(id, *val);
662 break;
663 case KVM_REG_PPC_PURR:
664 vcpu->arch.purr = set_reg_val(id, *val);
665 break;
666 case KVM_REG_PPC_SPURR:
667 vcpu->arch.spurr = set_reg_val(id, *val);
668 break;
669 case KVM_REG_PPC_AMR:
670 vcpu->arch.amr = set_reg_val(id, *val);
671 break;
672 case KVM_REG_PPC_UAMOR:
673 vcpu->arch.uamor = set_reg_val(id, *val);
674 break;
675 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
676 i = id - KVM_REG_PPC_MMCR0;
677 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
678 break;
679 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
680 i = id - KVM_REG_PPC_PMC1;
681 vcpu->arch.pmc[i] = set_reg_val(id, *val);
682 break;
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +0000683#ifdef CONFIG_VSX
684 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
685 if (cpu_has_feature(CPU_FTR_VSX)) {
686 /* VSX => FP reg i is stored in arch.vsr[2*i] */
687 long int i = id - KVM_REG_PPC_FPR0;
688 vcpu->arch.vsr[2 * i] = set_reg_val(id, *val);
689 } else {
690 /* let generic code handle it */
691 r = -EINVAL;
692 }
693 break;
694 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
695 if (cpu_has_feature(CPU_FTR_VSX)) {
696 long int i = id - KVM_REG_PPC_VSR0;
697 vcpu->arch.vsr[2 * i] = val->vsxval[0];
698 vcpu->arch.vsr[2 * i + 1] = val->vsxval[1];
699 } else {
700 r = -ENXIO;
701 }
702 break;
703#endif /* CONFIG_VSX */
Paul Mackerras55b665b2012-09-25 20:33:06 +0000704 case KVM_REG_PPC_VPA_ADDR:
705 addr = set_reg_val(id, *val);
706 r = -EINVAL;
707 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
708 vcpu->arch.dtl.next_gpa))
709 break;
710 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
711 break;
712 case KVM_REG_PPC_VPA_SLB:
713 addr = val->vpaval.addr;
714 len = val->vpaval.length;
715 r = -EINVAL;
716 if (addr && !vcpu->arch.vpa.next_gpa)
717 break;
718 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
719 break;
720 case KVM_REG_PPC_VPA_DTL:
721 addr = val->vpaval.addr;
722 len = val->vpaval.length;
723 r = -EINVAL;
724 if (len < sizeof(struct dtl_entry))
725 break;
726 if (addr && !vcpu->arch.vpa.next_gpa)
727 break;
728 len -= len % sizeof(struct dtl_entry);
729 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
730 break;
Paul Mackerras31f34382011-12-12 12:26:50 +0000731 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000732 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +0000733 break;
734 }
735
736 return r;
737}
738
Paul Mackerrasde56a942011-06-29 00:21:34 +0000739int kvmppc_core_check_processor_compat(void)
740{
Paul Mackerras9e368f22011-06-29 00:40:08 +0000741 if (cpu_has_feature(CPU_FTR_HVMODE))
Paul Mackerrasde56a942011-06-29 00:21:34 +0000742 return 0;
743 return -EIO;
744}
745
746struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
747{
748 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000749 int err = -EINVAL;
750 int core;
751 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000752
Paul Mackerras371fefd2011-06-29 00:23:08 +0000753 core = id / threads_per_core;
754 if (core >= KVM_MAX_VCORES)
755 goto out;
756
757 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200758 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000759 if (!vcpu)
760 goto out;
761
762 err = kvm_vcpu_init(vcpu, kvm, id);
763 if (err)
764 goto free_vcpu;
765
766 vcpu->arch.shared = &vcpu->arch.shregs;
767 vcpu->arch.last_cpu = -1;
768 vcpu->arch.mmcr[0] = MMCR0_FC;
769 vcpu->arch.ctrl = CTRL_RUNLATCH;
770 /* default to host PVR, since we can't spoof it */
771 vcpu->arch.pvr = mfspr(SPRN_PVR);
772 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000773 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000774
Paul Mackerrasde56a942011-06-29 00:21:34 +0000775 kvmppc_mmu_book3s_hv_init(vcpu);
776
Paul Mackerras371fefd2011-06-29 00:23:08 +0000777 /*
Paul Mackerras19ccb762011-07-23 17:42:46 +1000778 * We consider the vcpu stopped until we see the first run ioctl for it.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000779 */
Paul Mackerras19ccb762011-07-23 17:42:46 +1000780 vcpu->arch.state = KVMPPC_VCPU_STOPPED;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000781
782 init_waitqueue_head(&vcpu->arch.cpu_run);
783
784 mutex_lock(&kvm->lock);
785 vcore = kvm->arch.vcores[core];
786 if (!vcore) {
787 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
788 if (vcore) {
789 INIT_LIST_HEAD(&vcore->runnable_threads);
790 spin_lock_init(&vcore->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000791 init_waitqueue_head(&vcore->wq);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000792 vcore->preempt_tb = mftb();
Paul Mackerras371fefd2011-06-29 00:23:08 +0000793 }
794 kvm->arch.vcores[core] = vcore;
795 }
796 mutex_unlock(&kvm->lock);
797
798 if (!vcore)
799 goto free_vcpu;
800
801 spin_lock(&vcore->lock);
802 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000803 spin_unlock(&vcore->lock);
804 vcpu->arch.vcore = vcore;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000805 vcpu->arch.stolen_logged = vcore->stolen_tb;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000806
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200807 vcpu->arch.cpu_type = KVM_CPU_3S_64;
808 kvmppc_sanity_check(vcpu);
809
Paul Mackerrasde56a942011-06-29 00:21:34 +0000810 return vcpu;
811
812free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200813 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000814out:
815 return ERR_PTR(err);
816}
817
818void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
819{
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000820 spin_lock(&vcpu->arch.vpa_update_lock);
821 if (vcpu->arch.dtl.pinned_addr)
822 kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.dtl.pinned_addr);
823 if (vcpu->arch.slb_shadow.pinned_addr)
824 kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.slb_shadow.pinned_addr);
825 if (vcpu->arch.vpa.pinned_addr)
826 kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.vpa.pinned_addr);
827 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000828 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200829 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000830}
831
Paul Mackerras19ccb762011-07-23 17:42:46 +1000832static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000833{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000834 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000835
Paul Mackerras19ccb762011-07-23 17:42:46 +1000836 now = get_tb();
837 if (now > vcpu->arch.dec_expires) {
838 /* decrementer has already gone negative */
839 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -0600840 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000841 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000842 }
Paul Mackerras19ccb762011-07-23 17:42:46 +1000843 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
844 / tb_ticks_per_sec;
845 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
846 HRTIMER_MODE_REL);
847 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000848}
849
Paul Mackerras19ccb762011-07-23 17:42:46 +1000850static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000851{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000852 vcpu->arch.ceded = 0;
853 if (vcpu->arch.timer_running) {
854 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
855 vcpu->arch.timer_running = 0;
856 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000857}
858
859extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
860extern void xics_wake_cpu(int cpu);
861
862static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
863 struct kvm_vcpu *vcpu)
864{
Paul Mackerras371fefd2011-06-29 00:23:08 +0000865 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
866 return;
867 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
868 --vc->n_runnable;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000869 ++vc->n_busy;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000870 list_del(&vcpu->arch.run_list);
871}
872
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000873static int kvmppc_grab_hwthread(int cpu)
874{
875 struct paca_struct *tpaca;
876 long timeout = 1000;
877
878 tpaca = &paca[cpu];
879
880 /* Ensure the thread won't go into the kernel if it wakes */
881 tpaca->kvm_hstate.hwthread_req = 1;
882
883 /*
884 * If the thread is already executing in the kernel (e.g. handling
885 * a stray interrupt), wait for it to get back to nap mode.
886 * The smp_mb() is to ensure that our setting of hwthread_req
887 * is visible before we look at hwthread_state, so if this
888 * races with the code at system_reset_pSeries and the thread
889 * misses our setting of hwthread_req, we are sure to see its
890 * setting of hwthread_state, and vice versa.
891 */
892 smp_mb();
893 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
894 if (--timeout <= 0) {
895 pr_err("KVM: couldn't grab cpu %d\n", cpu);
896 return -EBUSY;
897 }
898 udelay(1);
899 }
900 return 0;
901}
902
903static void kvmppc_release_hwthread(int cpu)
904{
905 struct paca_struct *tpaca;
906
907 tpaca = &paca[cpu];
908 tpaca->kvm_hstate.hwthread_req = 0;
909 tpaca->kvm_hstate.kvm_vcpu = NULL;
910}
911
Paul Mackerras371fefd2011-06-29 00:23:08 +0000912static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
913{
914 int cpu;
915 struct paca_struct *tpaca;
916 struct kvmppc_vcore *vc = vcpu->arch.vcore;
917
Paul Mackerras19ccb762011-07-23 17:42:46 +1000918 if (vcpu->arch.timer_running) {
919 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
920 vcpu->arch.timer_running = 0;
921 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000922 cpu = vc->pcpu + vcpu->arch.ptid;
923 tpaca = &paca[cpu];
924 tpaca->kvm_hstate.kvm_vcpu = vcpu;
925 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000926 tpaca->kvm_hstate.napping = 0;
927 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000928 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +0000929#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000930 if (vcpu->arch.ptid) {
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000931 kvmppc_grab_hwthread(cpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +0000932 xics_wake_cpu(cpu);
933 ++vc->n_woken;
934 }
935#endif
936}
937
938static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
939{
940 int i;
941
942 HMT_low();
943 i = 0;
944 while (vc->nap_count < vc->n_woken) {
945 if (++i >= 1000000) {
946 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
947 vc->nap_count, vc->n_woken);
948 break;
949 }
950 cpu_relax();
951 }
952 HMT_medium();
953}
954
955/*
956 * Check that we are on thread 0 and that any other threads in
957 * this core are off-line.
958 */
959static int on_primary_thread(void)
960{
961 int cpu = smp_processor_id();
962 int thr = cpu_thread_in_core(cpu);
963
964 if (thr)
965 return 0;
966 while (++thr < threads_per_core)
967 if (cpu_online(cpu + thr))
968 return 0;
969 return 1;
970}
971
972/*
973 * Run a set of guest threads on a physical core.
974 * Called with vc->lock held.
975 */
976static int kvmppc_run_core(struct kvmppc_vcore *vc)
977{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000978 struct kvm_vcpu *vcpu, *vcpu0, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000979 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000980 u64 now;
Paul Mackerras081f3232012-06-01 20:20:24 +1000981 int ptid, i, need_vpa_update;
Paul Mackerras2c9097e2012-09-11 13:27:01 +0000982 int srcu_idx;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000983
Paul Mackerras371fefd2011-06-29 00:23:08 +0000984 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +1000985 need_vpa_update = 0;
986 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +0000987 if (signal_pending(vcpu->arch.run_task))
988 return 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000989 need_vpa_update |= vcpu->arch.vpa.update_pending |
990 vcpu->arch.slb_shadow.update_pending |
991 vcpu->arch.dtl.update_pending;
992 }
993
994 /*
995 * Initialize *vc, in particular vc->vcore_state, so we can
996 * drop the vcore lock if necessary.
997 */
998 vc->n_woken = 0;
999 vc->nap_count = 0;
1000 vc->entry_exit_count = 0;
1001 vc->vcore_state = VCORE_RUNNING;
1002 vc->in_guest = 0;
1003 vc->napping_threads = 0;
1004
1005 /*
1006 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1007 * which can't be called with any spinlocks held.
1008 */
1009 if (need_vpa_update) {
1010 spin_unlock(&vc->lock);
1011 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1012 kvmppc_update_vpas(vcpu);
1013 spin_lock(&vc->lock);
1014 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001015
1016 /*
1017 * Make sure we are running on thread 0, and that
1018 * secondary threads are offline.
1019 * XXX we should also block attempts to bring any
1020 * secondary threads online.
1021 */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001022 if (threads_per_core > 1 && !on_primary_thread()) {
1023 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1024 vcpu->arch.ret = -EBUSY;
1025 goto out;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001026 }
1027
Paul Mackerras19ccb762011-07-23 17:42:46 +10001028 /*
1029 * Assign physical thread IDs, first to non-ceded vcpus
1030 * and then to ceded ones.
1031 */
1032 ptid = 0;
1033 vcpu0 = NULL;
1034 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1035 if (!vcpu->arch.ceded) {
1036 if (!ptid)
1037 vcpu0 = vcpu;
1038 vcpu->arch.ptid = ptid++;
1039 }
1040 }
1041 if (!vcpu0)
1042 return 0; /* nothing to run */
1043 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1044 if (vcpu->arch.ceded)
1045 vcpu->arch.ptid = ptid++;
1046
Paul Mackerras0456ec42012-02-03 00:56:21 +00001047 vc->stolen_tb += mftb() - vc->preempt_tb;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001048 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001049 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +00001050 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +00001051 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001052 }
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001053 /* Grab any remaining hw threads so they can't go into the kernel */
1054 for (i = ptid; i < threads_per_core; ++i)
1055 kvmppc_grab_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001056
1057 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +10001058 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001059
Paul Mackerras19ccb762011-07-23 17:42:46 +10001060 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001061
1062 srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
1063
Paul Mackerras19ccb762011-07-23 17:42:46 +10001064 __kvmppc_vcore_entry(NULL, vcpu0);
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001065 for (i = 0; i < threads_per_core; ++i)
1066 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001067
Paul Mackerras371fefd2011-06-29 00:23:08 +00001068 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001069 /* disable sending of IPIs on virtual external irqs */
1070 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1071 vcpu->cpu = -1;
1072 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001073 if (vc->nap_count < vc->n_woken)
1074 kvmppc_wait_for_nap(vc);
1075 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001076 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001077 spin_unlock(&vc->lock);
1078
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001079 srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
1080
Paul Mackerras371fefd2011-06-29 00:23:08 +00001081 /* make sure updates to secondary vcpu structs are visible now */
1082 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00001083 kvm_guest_exit();
1084
1085 preempt_enable();
1086 kvm_resched(vcpu);
1087
1088 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001089 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1090 /* cancel pending dec exception if dec is positive */
1091 if (now < vcpu->arch.dec_expires &&
1092 kvmppc_core_pending_dec(vcpu))
1093 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001094
1095 ret = RESUME_GUEST;
1096 if (vcpu->arch.trap)
1097 ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
1098 vcpu->arch.run_task);
1099
Paul Mackerras371fefd2011-06-29 00:23:08 +00001100 vcpu->arch.ret = ret;
1101 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001102
1103 if (vcpu->arch.ceded) {
1104 if (ret != RESUME_GUEST)
1105 kvmppc_end_cede(vcpu);
1106 else
1107 kvmppc_set_timer(vcpu);
1108 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001109 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001110
Paul Mackerras371fefd2011-06-29 00:23:08 +00001111 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001112 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001113 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras0456ec42012-02-03 00:56:21 +00001114 vc->preempt_tb = mftb();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001115 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1116 arch.run_list) {
1117 if (vcpu->arch.ret != RESUME_GUEST) {
1118 kvmppc_remove_runnable(vc, vcpu);
1119 wake_up(&vcpu->arch.cpu_run);
1120 }
1121 }
1122
1123 return 1;
1124}
1125
Paul Mackerras19ccb762011-07-23 17:42:46 +10001126/*
1127 * Wait for some other vcpu thread to execute us, and
1128 * wake us up when we need to handle something in the host.
1129 */
1130static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001131{
Paul Mackerras371fefd2011-06-29 00:23:08 +00001132 DEFINE_WAIT(wait);
1133
Paul Mackerras19ccb762011-07-23 17:42:46 +10001134 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1135 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1136 schedule();
1137 finish_wait(&vcpu->arch.cpu_run, &wait);
1138}
Paul Mackerras371fefd2011-06-29 00:23:08 +00001139
Paul Mackerras19ccb762011-07-23 17:42:46 +10001140/*
1141 * All the vcpus in this vcore are idle, so wait for a decrementer
1142 * or external interrupt to one of the vcpus. vc->lock is held.
1143 */
1144static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1145{
1146 DEFINE_WAIT(wait);
1147 struct kvm_vcpu *v;
1148 int all_idle = 1;
1149
1150 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1151 vc->vcore_state = VCORE_SLEEPING;
1152 spin_unlock(&vc->lock);
1153 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
1154 if (!v->arch.ceded || v->arch.pending_exceptions) {
1155 all_idle = 0;
1156 break;
1157 }
1158 }
1159 if (all_idle)
1160 schedule();
1161 finish_wait(&vc->wq, &wait);
1162 spin_lock(&vc->lock);
1163 vc->vcore_state = VCORE_INACTIVE;
1164}
1165
1166static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1167{
1168 int n_ceded;
1169 int prev_state;
1170 struct kvmppc_vcore *vc;
1171 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001172
Paul Mackerras371fefd2011-06-29 00:23:08 +00001173 kvm_run->exit_reason = 0;
1174 vcpu->arch.ret = RESUME_GUEST;
1175 vcpu->arch.trap = 0;
1176
Paul Mackerras371fefd2011-06-29 00:23:08 +00001177 /*
1178 * Synchronize with other threads in this virtual core
1179 */
1180 vc = vcpu->arch.vcore;
1181 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001182 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001183 vcpu->arch.run_task = current;
1184 vcpu->arch.kvm_run = kvm_run;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001185 prev_state = vcpu->arch.state;
1186 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001187 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1188 ++vc->n_runnable;
1189
Paul Mackerras19ccb762011-07-23 17:42:46 +10001190 /*
1191 * This happens the first time this is called for a vcpu.
1192 * If the vcore is already running, we may be able to start
1193 * this thread straight away and have it join in.
1194 */
1195 if (prev_state == KVMPPC_VCPU_STOPPED) {
1196 if (vc->vcore_state == VCORE_RUNNING &&
1197 VCORE_EXIT_COUNT(vc) == 0) {
1198 vcpu->arch.ptid = vc->n_runnable - 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001199 kvmppc_start_thread(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001200 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001201
Paul Mackerras19ccb762011-07-23 17:42:46 +10001202 } else if (prev_state == KVMPPC_VCPU_BUSY_IN_HOST)
1203 --vc->n_busy;
1204
1205 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1206 !signal_pending(current)) {
1207 if (vc->n_busy || vc->vcore_state != VCORE_INACTIVE) {
1208 spin_unlock(&vc->lock);
1209 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1210 spin_lock(&vc->lock);
1211 continue;
1212 }
Paul Mackerras0456ec42012-02-03 00:56:21 +00001213 vc->runner = vcpu;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001214 n_ceded = 0;
1215 list_for_each_entry(v, &vc->runnable_threads, arch.run_list)
1216 n_ceded += v->arch.ceded;
1217 if (n_ceded == vc->n_runnable)
1218 kvmppc_vcore_blocked(vc);
1219 else
1220 kvmppc_run_core(vc);
1221
1222 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1223 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001224 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001225 if (signal_pending(v->arch.run_task)) {
1226 kvmppc_remove_runnable(vc, v);
1227 v->stat.signal_exits++;
1228 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1229 v->arch.ret = -EINTR;
1230 wake_up(&v->arch.cpu_run);
1231 }
1232 }
Paul Mackerras0456ec42012-02-03 00:56:21 +00001233 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001234 }
1235
Paul Mackerras19ccb762011-07-23 17:42:46 +10001236 if (signal_pending(current)) {
1237 if (vc->vcore_state == VCORE_RUNNING ||
1238 vc->vcore_state == VCORE_EXITING) {
1239 spin_unlock(&vc->lock);
1240 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1241 spin_lock(&vc->lock);
1242 }
1243 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1244 kvmppc_remove_runnable(vc, vcpu);
1245 vcpu->stat.signal_exits++;
1246 kvm_run->exit_reason = KVM_EXIT_INTR;
1247 vcpu->arch.ret = -EINTR;
1248 }
1249 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001250
Paul Mackerras19ccb762011-07-23 17:42:46 +10001251 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001252 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001253}
1254
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001255int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
1256{
1257 int r;
1258
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001259 if (!vcpu->arch.sane) {
1260 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1261 return -EINVAL;
1262 }
1263
Scott Wood25051b52011-11-08 18:23:23 -06001264 kvmppc_core_prepare_to_enter(vcpu);
1265
Paul Mackerras19ccb762011-07-23 17:42:46 +10001266 /* No need to go into the guest when all we'll do is come back out */
1267 if (signal_pending(current)) {
1268 run->exit_reason = KVM_EXIT_INTR;
1269 return -EINTR;
1270 }
1271
Paul Mackerras32fad282012-05-04 02:32:53 +00001272 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1273 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1274 smp_mb();
1275
1276 /* On the first time here, set up HTAB and VRMA or RMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001277 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001278 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001279 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001280 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001281 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001282
1283 flush_fp_to_thread(current);
1284 flush_altivec_to_thread(current);
1285 flush_vsx_to_thread(current);
1286 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001287 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001288
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001289 do {
1290 r = kvmppc_run_vcpu(run, vcpu);
1291
1292 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1293 !(vcpu->arch.shregs.msr & MSR_PR)) {
1294 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001295 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001296 }
1297 } while (r == RESUME_GUEST);
Paul Mackerras32fad282012-05-04 02:32:53 +00001298
1299 out:
1300 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001301 return r;
1302}
1303
David Gibson54738c02011-06-29 00:22:41 +00001304
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001305/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +00001306 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001307static inline int lpcr_rmls(unsigned long rma_size)
1308{
1309 switch (rma_size) {
1310 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001311 if (cpu_has_feature(CPU_FTR_ARCH_206))
1312 return 8; /* only supported on POWER7 */
1313 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001314 case 64ul << 20: /* 64 MB */
1315 return 3;
1316 case 128ul << 20: /* 128 MB */
1317 return 7;
1318 case 256ul << 20: /* 256 MB */
1319 return 4;
1320 case 1ul << 30: /* 1 GB */
1321 return 2;
1322 case 16ul << 30: /* 16 GB */
1323 return 1;
1324 case 256ul << 30: /* 256 GB */
1325 return 0;
1326 default:
1327 return -1;
1328 }
1329}
1330
1331static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1332{
Alexander Grafb4e70612012-01-16 16:50:10 +01001333 struct kvmppc_linear_info *ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001334 struct page *page;
1335
1336 if (vmf->pgoff >= ri->npages)
1337 return VM_FAULT_SIGBUS;
1338
1339 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1340 get_page(page);
1341 vmf->page = page;
1342 return 0;
1343}
1344
1345static const struct vm_operations_struct kvm_rma_vm_ops = {
1346 .fault = kvm_rma_fault,
1347};
1348
1349static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1350{
1351 vma->vm_flags |= VM_RESERVED;
1352 vma->vm_ops = &kvm_rma_vm_ops;
1353 return 0;
1354}
1355
1356static int kvm_rma_release(struct inode *inode, struct file *filp)
1357{
Alexander Grafb4e70612012-01-16 16:50:10 +01001358 struct kvmppc_linear_info *ri = filp->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001359
1360 kvm_release_rma(ri);
1361 return 0;
1362}
1363
1364static struct file_operations kvm_rma_fops = {
1365 .mmap = kvm_rma_mmap,
1366 .release = kvm_rma_release,
1367};
1368
1369long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
1370{
Alexander Grafb4e70612012-01-16 16:50:10 +01001371 struct kvmppc_linear_info *ri;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001372 long fd;
1373
1374 ri = kvm_alloc_rma();
1375 if (!ri)
1376 return -ENOMEM;
1377
1378 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR);
1379 if (fd < 0)
1380 kvm_release_rma(ri);
1381
1382 ret->rma_size = ri->npages << PAGE_SHIFT;
1383 return fd;
1384}
1385
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001386static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1387 int linux_psize)
1388{
1389 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1390
1391 if (!def->shift)
1392 return;
1393 (*sps)->page_shift = def->shift;
1394 (*sps)->slb_enc = def->sllp;
1395 (*sps)->enc[0].page_shift = def->shift;
1396 (*sps)->enc[0].pte_enc = def->penc;
1397 (*sps)++;
1398}
1399
1400int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
1401{
1402 struct kvm_ppc_one_seg_page_size *sps;
1403
1404 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1405 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1406 info->flags |= KVM_PPC_1T_SEGMENTS;
1407 info->slb_size = mmu_slb_size;
1408
1409 /* We only support these sizes for now, and no muti-size segments */
1410 sps = &info->sps[0];
1411 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1412 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1413 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1414
1415 return 0;
1416}
1417
Paul Mackerras82ed3612011-12-15 02:03:22 +00001418/*
1419 * Get (and clear) the dirty memory log for a memory slot.
1420 */
1421int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1422{
1423 struct kvm_memory_slot *memslot;
1424 int r;
1425 unsigned long n;
1426
1427 mutex_lock(&kvm->slots_lock);
1428
1429 r = -EINVAL;
1430 if (log->slot >= KVM_MEMORY_SLOTS)
1431 goto out;
1432
1433 memslot = id_to_memslot(kvm->memslots, log->slot);
1434 r = -ENOENT;
1435 if (!memslot->dirty_bitmap)
1436 goto out;
1437
1438 n = kvm_dirty_bitmap_bytes(memslot);
1439 memset(memslot->dirty_bitmap, 0, n);
1440
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001441 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00001442 if (r)
1443 goto out;
1444
1445 r = -EFAULT;
1446 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1447 goto out;
1448
1449 r = 0;
1450out:
1451 mutex_unlock(&kvm->slots_lock);
1452 return r;
1453}
1454
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001455static unsigned long slb_pgsize_encoding(unsigned long psize)
1456{
1457 unsigned long senc = 0;
1458
1459 if (psize > 0x1000) {
1460 senc = SLB_VSID_L;
1461 if (psize == 0x10000)
1462 senc |= SLB_VSID_LP_01;
1463 }
1464 return senc;
1465}
1466
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001467static void unpin_slot(struct kvm_memory_slot *memslot)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001468{
1469 unsigned long *physp;
1470 unsigned long j, npages, pfn;
1471 struct page *page;
1472
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001473 physp = memslot->arch.slot_phys;
1474 npages = memslot->npages;
1475 if (!physp)
1476 return;
1477 for (j = 0; j < npages; j++) {
1478 if (!(physp[j] & KVMPPC_GOT_PAGE))
1479 continue;
1480 pfn = physp[j] >> PAGE_SHIFT;
1481 page = pfn_to_page(pfn);
1482 SetPageDirty(page);
1483 put_page(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001484 }
1485}
1486
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001487void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1488 struct kvm_memory_slot *dont)
1489{
1490 if (!dont || free->arch.rmap != dont->arch.rmap) {
1491 vfree(free->arch.rmap);
1492 free->arch.rmap = NULL;
1493 }
1494 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1495 unpin_slot(free);
1496 vfree(free->arch.slot_phys);
1497 free->arch.slot_phys = NULL;
1498 }
1499}
1500
1501int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1502 unsigned long npages)
1503{
1504 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1505 if (!slot->arch.rmap)
1506 return -ENOMEM;
1507 slot->arch.slot_phys = NULL;
1508
1509 return 0;
1510}
1511
1512int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1513 struct kvm_memory_slot *memslot,
1514 struct kvm_userspace_memory_region *mem)
1515{
1516 unsigned long *phys;
1517
1518 /* Allocate a slot_phys array if needed */
1519 phys = memslot->arch.slot_phys;
1520 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1521 phys = vzalloc(memslot->npages * sizeof(unsigned long));
1522 if (!phys)
1523 return -ENOMEM;
1524 memslot->arch.slot_phys = phys;
1525 }
1526
1527 return 0;
1528}
1529
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001530void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001531 struct kvm_userspace_memory_region *mem,
1532 struct kvm_memory_slot old)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001533{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001534 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1535 struct kvm_memory_slot *memslot;
1536
1537 if (npages && old.npages) {
1538 /*
1539 * If modifying a memslot, reset all the rmap dirty bits.
1540 * If this is a new memslot, we don't need to do anything
1541 * since the rmap array starts out as all zeroes,
1542 * i.e. no pages are dirty.
1543 */
1544 memslot = id_to_memslot(kvm->memslots, mem->slot);
1545 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1546 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001547}
1548
Paul Mackerras32fad282012-05-04 02:32:53 +00001549static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001550{
1551 int err = 0;
1552 struct kvm *kvm = vcpu->kvm;
Alexander Grafb4e70612012-01-16 16:50:10 +01001553 struct kvmppc_linear_info *ri = NULL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001554 unsigned long hva;
1555 struct kvm_memory_slot *memslot;
1556 struct vm_area_struct *vma;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001557 unsigned long lpcr, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001558 unsigned long psize, porder;
1559 unsigned long rma_size;
1560 unsigned long rmls;
1561 unsigned long *physp;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001562 unsigned long i, npages;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001563 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001564
1565 mutex_lock(&kvm->lock);
1566 if (kvm->arch.rma_setup_done)
1567 goto out; /* another vcpu beat us to it */
1568
Paul Mackerras32fad282012-05-04 02:32:53 +00001569 /* Allocate hashed page table (if not done already) and reset it */
1570 if (!kvm->arch.hpt_virt) {
1571 err = kvmppc_alloc_hpt(kvm, NULL);
1572 if (err) {
1573 pr_err("KVM: Couldn't alloc HPT\n");
1574 goto out;
1575 }
1576 }
1577
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001578 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001579 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001580 memslot = gfn_to_memslot(kvm, 0);
1581
1582 /* We must have some memory at 0 by now */
1583 err = -EINVAL;
1584 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001585 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001586
1587 /* Look up the VMA for the start of this memory slot */
1588 hva = memslot->userspace_addr;
1589 down_read(&current->mm->mmap_sem);
1590 vma = find_vma(current->mm, hva);
1591 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
1592 goto up_out;
1593
1594 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001595 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001596
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001597 /* Is this one of our preallocated RMAs? */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001598 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
1599 hva == vma->vm_start)
1600 ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001601
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001602 up_read(&current->mm->mmap_sem);
1603
1604 if (!ri) {
1605 /* On POWER7, use VRMA; on PPC970, give up */
1606 err = -EPERM;
1607 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1608 pr_err("KVM: CPU requires an RMO\n");
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001609 goto out_srcu;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001610 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001611
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001612 /* We can handle 4k, 64k or 16M pages in the VRMA */
1613 err = -EINVAL;
1614 if (!(psize == 0x1000 || psize == 0x10000 ||
1615 psize == 0x1000000))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001616 goto out_srcu;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001617
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001618 /* Update VRMASD field in the LPCR */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001619 senc = slb_pgsize_encoding(psize);
Paul Mackerras697d3892011-12-12 12:36:37 +00001620 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1621 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001622 lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
1623 lpcr |= senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001624 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001625
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001626 /* Create HPTEs in the hash page table for the VRMA */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001627 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001628
1629 } else {
1630 /* Set up to use an RMO region */
1631 rma_size = ri->npages;
1632 if (rma_size > memslot->npages)
1633 rma_size = memslot->npages;
1634 rma_size <<= PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001635 rmls = lpcr_rmls(rma_size);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001636 err = -EINVAL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001637 if (rmls < 0) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001638 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001639 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001640 }
1641 atomic_inc(&ri->use_count);
1642 kvm->arch.rma = ri;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001643
1644 /* Update LPCR and RMOR */
1645 lpcr = kvm->arch.lpcr;
1646 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1647 /* PPC970; insert RMLS value (split field) in HID4 */
1648 lpcr &= ~((1ul << HID4_RMLS0_SH) |
1649 (3ul << HID4_RMLS2_SH));
1650 lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
1651 ((rmls & 3) << HID4_RMLS2_SH);
1652 /* RMOR is also in HID4 */
1653 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
1654 << HID4_RMOR_SH;
1655 } else {
1656 /* POWER7 */
1657 lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
1658 lpcr |= rmls << LPCR_RMLS_SH;
1659 kvm->arch.rmor = kvm->arch.rma->base_pfn << PAGE_SHIFT;
1660 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001661 kvm->arch.lpcr = lpcr;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001662 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001663 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001664
1665 /* Initialize phys addrs of pages in RMO */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001666 npages = ri->npages;
1667 porder = __ilog2(npages);
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001668 physp = memslot->arch.slot_phys;
1669 if (physp) {
1670 if (npages > memslot->npages)
1671 npages = memslot->npages;
1672 spin_lock(&kvm->arch.slot_phys_lock);
1673 for (i = 0; i < npages; ++i)
1674 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
1675 porder;
1676 spin_unlock(&kvm->arch.slot_phys_lock);
1677 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001678 }
1679
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001680 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
1681 smp_wmb();
1682 kvm->arch.rma_setup_done = 1;
1683 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001684 out_srcu:
1685 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001686 out:
1687 mutex_unlock(&kvm->lock);
1688 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001689
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001690 up_out:
1691 up_read(&current->mm->mmap_sem);
1692 goto out;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001693}
1694
1695int kvmppc_core_init_vm(struct kvm *kvm)
1696{
Paul Mackerras32fad282012-05-04 02:32:53 +00001697 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001698
Paul Mackerras32fad282012-05-04 02:32:53 +00001699 /* Allocate the guest's logical partition ID */
1700
1701 lpid = kvmppc_alloc_lpid();
1702 if (lpid < 0)
1703 return -ENOMEM;
1704 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001705
David Gibson54738c02011-06-29 00:22:41 +00001706 INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001707
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001708 kvm->arch.rma = NULL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001709
Paul Mackerras9e368f22011-06-29 00:40:08 +00001710 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001711
Paul Mackerras9e368f22011-06-29 00:40:08 +00001712 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1713 /* PPC970; HID4 is effectively the LPCR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001714 kvm->arch.host_lpid = 0;
1715 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
1716 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
1717 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
1718 ((lpid & 0xf) << HID4_LPID5_SH);
1719 } else {
1720 /* POWER7; init LPCR for virtual RMA mode */
1721 kvm->arch.host_lpid = mfspr(SPRN_LPID);
1722 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
1723 lpcr &= LPCR_PECE | LPCR_LPES;
1724 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
Paul Mackerras697d3892011-12-12 12:36:37 +00001725 LPCR_VPM0 | LPCR_VPM1;
1726 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
1727 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerras9e368f22011-06-29 00:40:08 +00001728 }
1729 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001730
Paul Mackerras342d3db2011-12-12 12:38:05 +00001731 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001732 spin_lock_init(&kvm->arch.slot_phys_lock);
David Gibson54738c02011-06-29 00:22:41 +00001733 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001734}
1735
1736void kvmppc_core_destroy_vm(struct kvm *kvm)
1737{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001738 if (kvm->arch.rma) {
1739 kvm_release_rma(kvm->arch.rma);
1740 kvm->arch.rma = NULL;
1741 }
1742
Paul Mackerrasde56a942011-06-29 00:21:34 +00001743 kvmppc_free_hpt(kvm);
David Gibson54738c02011-06-29 00:22:41 +00001744 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
Paul Mackerrasde56a942011-06-29 00:21:34 +00001745}
1746
1747/* These are stubs for now */
1748void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
1749{
1750}
1751
1752/* We don't need to emulate any privileged instructions or dcbz */
1753int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
1754 unsigned int inst, int *advance)
1755{
1756 return EMULATE_FAIL;
1757}
1758
Alexander Graf54771e62012-05-04 14:55:12 +02001759int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001760{
1761 return EMULATE_FAIL;
1762}
1763
Alexander Graf54771e62012-05-04 14:55:12 +02001764int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001765{
1766 return EMULATE_FAIL;
1767}
1768
1769static int kvmppc_book3s_hv_init(void)
1770{
1771 int r;
1772
1773 r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1774
1775 if (r)
1776 return r;
1777
1778 r = kvmppc_mmu_hv_init();
1779
1780 return r;
1781}
1782
1783static void kvmppc_book3s_hv_exit(void)
1784{
1785 kvm_exit();
1786}
1787
1788module_init(kvmppc_book3s_hv_init);
1789module_exit(kvmppc_book3s_hv_exit);