blob: 83e929e66f9d8cb583ccf5564c9e7f846164f5fc [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 Mackerrasde56a942011-06-29 00:21:34 +000033
34#include <asm/reg.h>
35#include <asm/cputable.h>
36#include <asm/cacheflush.h>
37#include <asm/tlbflush.h>
38#include <asm/uaccess.h>
39#include <asm/io.h>
40#include <asm/kvm_ppc.h>
41#include <asm/kvm_book3s.h>
42#include <asm/mmu_context.h>
43#include <asm/lppaca.h>
44#include <asm/processor.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000045#include <asm/cputhreads.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000046#include <asm/page.h>
Michael Neulingde1d9242011-11-09 20:39:49 +000047#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010048#include <asm/switch_to.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000049#include <linux/gfp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000050#include <linux/vmalloc.h>
51#include <linux/highmem.h>
Paul Mackerrasc77162d2011-12-12 12:31:00 +000052#include <linux/hugetlb.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000053
54/* #define EXIT_DEBUG */
55/* #define EXIT_DEBUG_SIMPLE */
56/* #define EXIT_DEBUG_INT */
57
Paul Mackerras19ccb762011-07-23 17:42:46 +100058static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +000059static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +100060
Paul Mackerrasde56a942011-06-29 00:21:34 +000061void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
62{
Paul Mackerras0456ec42012-02-03 00:56:21 +000063 struct kvmppc_vcore *vc = vcpu->arch.vcore;
64
Paul Mackerrasde56a942011-06-29 00:21:34 +000065 local_paca->kvm_hstate.kvm_vcpu = vcpu;
Paul Mackerras0456ec42012-02-03 00:56:21 +000066 local_paca->kvm_hstate.kvm_vcore = vc;
67 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
68 vc->stolen_tb += mftb() - vc->preempt_tb;
Paul Mackerrasde56a942011-06-29 00:21:34 +000069}
70
71void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
72{
Paul Mackerras0456ec42012-02-03 00:56:21 +000073 struct kvmppc_vcore *vc = vcpu->arch.vcore;
74
75 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
76 vc->preempt_tb = mftb();
Paul Mackerrasde56a942011-06-29 00:21:34 +000077}
78
Paul Mackerrasde56a942011-06-29 00:21:34 +000079void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
80{
81 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +100082 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +000083}
84
85void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
86{
87 vcpu->arch.pvr = pvr;
88}
89
90void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
91{
92 int r;
93
94 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
95 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
96 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
97 for (r = 0; r < 16; ++r)
98 pr_err("r%2d = %.16lx r%d = %.16lx\n",
99 r, kvmppc_get_gpr(vcpu, r),
100 r+16, kvmppc_get_gpr(vcpu, r+16));
101 pr_err("ctr = %.16lx lr = %.16lx\n",
102 vcpu->arch.ctr, vcpu->arch.lr);
103 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
104 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
105 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
106 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
107 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
108 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
109 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
110 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
111 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
112 pr_err("fault dar = %.16lx dsisr = %.8x\n",
113 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
114 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
115 for (r = 0; r < vcpu->arch.slb_max; ++r)
116 pr_err(" ESID = %.16llx VSID = %.16llx\n",
117 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
118 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000119 vcpu->kvm->arch.lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000120 vcpu->arch.last_inst);
121}
122
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000123struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
124{
125 int r;
126 struct kvm_vcpu *v, *ret = NULL;
127
128 mutex_lock(&kvm->lock);
129 kvm_for_each_vcpu(r, v, kvm) {
130 if (v->vcpu_id == id) {
131 ret = v;
132 break;
133 }
134 }
135 mutex_unlock(&kvm->lock);
136 return ret;
137}
138
139static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
140{
141 vpa->shared_proc = 1;
142 vpa->yield_count = 1;
143}
144
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000145/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
146struct reg_vpa {
147 u32 dummy;
148 union {
149 u16 hword;
150 u32 word;
151 } length;
152};
153
154static int vpa_is_registered(struct kvmppc_vpa *vpap)
155{
156 if (vpap->update_pending)
157 return vpap->next_gpa != 0;
158 return vpap->pinned_addr != NULL;
159}
160
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000161static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
162 unsigned long flags,
163 unsigned long vcpuid, unsigned long vpa)
164{
165 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000166 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000167 void *va;
168 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000169 int err;
170 int subfunc;
171 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000172
173 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
174 if (!tvcpu)
175 return H_PARAMETER;
176
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000177 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
178 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
179 subfunc == H_VPA_REG_SLB) {
180 /* Registering new area - address must be cache-line aligned */
181 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000182 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000183
184 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000185 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
186 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000187 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000188 if (subfunc == H_VPA_REG_VPA)
189 len = ((struct reg_vpa *)va)->length.hword;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000190 else
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000191 len = ((struct reg_vpa *)va)->length.word;
192 kvmppc_unpin_guest_page(kvm, va);
193
194 /* Check length */
195 if (len > nb || len < sizeof(struct reg_vpa))
196 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000197 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000198 vpa = 0;
199 len = 0;
200 }
201
202 err = H_PARAMETER;
203 vpap = NULL;
204 spin_lock(&tvcpu->arch.vpa_update_lock);
205
206 switch (subfunc) {
207 case H_VPA_REG_VPA: /* register VPA */
208 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000209 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000210 vpap = &tvcpu->arch.vpa;
211 err = 0;
212 break;
213
214 case H_VPA_REG_DTL: /* register DTL */
215 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000216 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000217 len -= len % sizeof(struct dtl_entry);
218
219 /* Check that they have previously registered a VPA */
220 err = H_RESOURCE;
221 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000222 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000223
224 vpap = &tvcpu->arch.dtl;
225 err = 0;
226 break;
227
228 case H_VPA_REG_SLB: /* register SLB shadow buffer */
229 /* Check that they have previously registered a VPA */
230 err = H_RESOURCE;
231 if (!vpa_is_registered(&tvcpu->arch.vpa))
232 break;
233
234 vpap = &tvcpu->arch.slb_shadow;
235 err = 0;
236 break;
237
238 case H_VPA_DEREG_VPA: /* deregister VPA */
239 /* Check they don't still have a DTL or SLB buf registered */
240 err = H_RESOURCE;
241 if (vpa_is_registered(&tvcpu->arch.dtl) ||
242 vpa_is_registered(&tvcpu->arch.slb_shadow))
243 break;
244
245 vpap = &tvcpu->arch.vpa;
246 err = 0;
247 break;
248
249 case H_VPA_DEREG_DTL: /* deregister DTL */
250 vpap = &tvcpu->arch.dtl;
251 err = 0;
252 break;
253
254 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
255 vpap = &tvcpu->arch.slb_shadow;
256 err = 0;
257 break;
258 }
259
260 if (vpap) {
261 vpap->next_gpa = vpa;
262 vpap->len = len;
263 vpap->update_pending = 1;
264 }
265
266 spin_unlock(&tvcpu->arch.vpa_update_lock);
267
268 return err;
269}
270
Paul Mackerras081f3232012-06-01 20:20:24 +1000271static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000272{
Paul Mackerras081f3232012-06-01 20:20:24 +1000273 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000274 void *va;
275 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000276 unsigned long gpa;
277
278 /*
279 * We need to pin the page pointed to by vpap->next_gpa,
280 * but we can't call kvmppc_pin_guest_page under the lock
281 * as it does get_user_pages() and down_read(). So we
282 * have to drop the lock, pin the page, then get the lock
283 * again and check that a new area didn't get registered
284 * in the meantime.
285 */
286 for (;;) {
287 gpa = vpap->next_gpa;
288 spin_unlock(&vcpu->arch.vpa_update_lock);
289 va = NULL;
290 nb = 0;
291 if (gpa)
292 va = kvmppc_pin_guest_page(kvm, vpap->next_gpa, &nb);
293 spin_lock(&vcpu->arch.vpa_update_lock);
294 if (gpa == vpap->next_gpa)
295 break;
296 /* sigh... unpin that one and try again */
297 if (va)
298 kvmppc_unpin_guest_page(kvm, va);
299 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000300
301 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000302 if (va && nb < vpap->len) {
303 /*
304 * If it's now too short, it must be that userspace
305 * has changed the mappings underlying guest memory,
306 * so unregister the region.
307 */
308 kvmppc_unpin_guest_page(kvm, va);
309 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000310 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000311 if (vpap->pinned_addr)
312 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr);
313 vpap->pinned_addr = va;
314 if (va)
315 vpap->pinned_end = va + vpap->len;
316}
Paul Mackerras93e60242011-12-12 12:28:55 +0000317
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000318static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
319{
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000320 spin_lock(&vcpu->arch.vpa_update_lock);
321 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000322 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000323 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
324 }
325 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000326 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000327 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
328 vcpu->arch.dtl_index = 0;
329 }
330 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000331 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000332 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000333}
334
Paul Mackerras0456ec42012-02-03 00:56:21 +0000335static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
336 struct kvmppc_vcore *vc)
337{
338 struct dtl_entry *dt;
339 struct lppaca *vpa;
340 unsigned long old_stolen;
341
342 dt = vcpu->arch.dtl_ptr;
343 vpa = vcpu->arch.vpa.pinned_addr;
344 old_stolen = vcpu->arch.stolen_logged;
345 vcpu->arch.stolen_logged = vc->stolen_tb;
346 if (!dt || !vpa)
347 return;
348 memset(dt, 0, sizeof(struct dtl_entry));
349 dt->dispatch_reason = 7;
350 dt->processor_id = vc->pcpu + vcpu->arch.ptid;
351 dt->timebase = mftb();
352 dt->enqueue_to_dispatch_time = vc->stolen_tb - old_stolen;
353 dt->srr0 = kvmppc_get_pc(vcpu);
354 dt->srr1 = vcpu->arch.shregs.msr;
355 ++dt;
356 if (dt == vcpu->arch.dtl.pinned_end)
357 dt = vcpu->arch.dtl.pinned_addr;
358 vcpu->arch.dtl_ptr = dt;
359 /* order writing *dt vs. writing vpa->dtl_idx */
360 smp_wmb();
361 vpa->dtl_idx = ++vcpu->arch.dtl_index;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000362}
363
364int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
365{
366 unsigned long req = kvmppc_get_gpr(vcpu, 3);
367 unsigned long target, ret = H_SUCCESS;
368 struct kvm_vcpu *tvcpu;
369
370 switch (req) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000371 case H_ENTER:
372 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
373 kvmppc_get_gpr(vcpu, 5),
374 kvmppc_get_gpr(vcpu, 6),
375 kvmppc_get_gpr(vcpu, 7));
376 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000377 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000378 break;
379 case H_PROD:
380 target = kvmppc_get_gpr(vcpu, 4);
381 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
382 if (!tvcpu) {
383 ret = H_PARAMETER;
384 break;
385 }
386 tvcpu->arch.prodded = 1;
387 smp_mb();
388 if (vcpu->arch.ceded) {
389 if (waitqueue_active(&vcpu->wq)) {
390 wake_up_interruptible(&vcpu->wq);
391 vcpu->stat.halt_wakeup++;
392 }
393 }
394 break;
395 case H_CONFER:
396 break;
397 case H_REGISTER_VPA:
398 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
399 kvmppc_get_gpr(vcpu, 5),
400 kvmppc_get_gpr(vcpu, 6));
401 break;
402 default:
403 return RESUME_HOST;
404 }
405 kvmppc_set_gpr(vcpu, 3, ret);
406 vcpu->arch.hcall_needed = 0;
407 return RESUME_GUEST;
408}
409
Paul Mackerrasde56a942011-06-29 00:21:34 +0000410static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
411 struct task_struct *tsk)
412{
413 int r = RESUME_HOST;
414
415 vcpu->stat.sum_exits++;
416
417 run->exit_reason = KVM_EXIT_UNKNOWN;
418 run->ready_for_interrupt_injection = 1;
419 switch (vcpu->arch.trap) {
420 /* We're good on these - the host merely wanted to get our attention */
421 case BOOK3S_INTERRUPT_HV_DECREMENTER:
422 vcpu->stat.dec_exits++;
423 r = RESUME_GUEST;
424 break;
425 case BOOK3S_INTERRUPT_EXTERNAL:
426 vcpu->stat.ext_intr_exits++;
427 r = RESUME_GUEST;
428 break;
429 case BOOK3S_INTERRUPT_PERFMON:
430 r = RESUME_GUEST;
431 break;
432 case BOOK3S_INTERRUPT_PROGRAM:
433 {
434 ulong flags;
435 /*
436 * Normally program interrupts are delivered directly
437 * to the guest by the hardware, but we can get here
438 * as a result of a hypervisor emulation interrupt
439 * (e40) getting turned into a 700 by BML RTAS.
440 */
441 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
442 kvmppc_core_queue_program(vcpu, flags);
443 r = RESUME_GUEST;
444 break;
445 }
446 case BOOK3S_INTERRUPT_SYSCALL:
447 {
448 /* hcall - punt to userspace */
449 int i;
450
451 if (vcpu->arch.shregs.msr & MSR_PR) {
452 /* sc 1 from userspace - reflect to guest syscall */
453 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
454 r = RESUME_GUEST;
455 break;
456 }
457 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
458 for (i = 0; i < 9; ++i)
459 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
460 run->exit_reason = KVM_EXIT_PAPR_HCALL;
461 vcpu->arch.hcall_needed = 1;
462 r = RESUME_HOST;
463 break;
464 }
465 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000466 * We get these next two if the guest accesses a page which it thinks
467 * it has mapped but which is not actually present, either because
468 * it is for an emulated I/O device or because the corresonding
469 * host page has been paged out. Any other HDSI/HISI interrupts
470 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000471 */
472 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras697d3892011-12-12 12:36:37 +0000473 r = kvmppc_book3s_hv_page_fault(run, vcpu,
474 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000475 break;
476 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras342d3db2011-12-12 12:38:05 +0000477 r = kvmppc_book3s_hv_page_fault(run, vcpu,
478 kvmppc_get_pc(vcpu), 0);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000479 break;
480 /*
481 * This occurs if the guest executes an illegal instruction.
482 * We just generate a program interrupt to the guest, since
483 * we don't emulate any guest instructions at this stage.
484 */
485 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
486 kvmppc_core_queue_program(vcpu, 0x80000);
487 r = RESUME_GUEST;
488 break;
489 default:
490 kvmppc_dump_regs(vcpu);
491 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
492 vcpu->arch.trap, kvmppc_get_pc(vcpu),
493 vcpu->arch.shregs.msr);
494 r = RESUME_HOST;
495 BUG();
496 break;
497 }
498
Paul Mackerrasde56a942011-06-29 00:21:34 +0000499 return r;
500}
501
502int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
503 struct kvm_sregs *sregs)
504{
505 int i;
506
507 sregs->pvr = vcpu->arch.pvr;
508
509 memset(sregs, 0, sizeof(struct kvm_sregs));
510 for (i = 0; i < vcpu->arch.slb_max; i++) {
511 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
512 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
513 }
514
515 return 0;
516}
517
518int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
519 struct kvm_sregs *sregs)
520{
521 int i, j;
522
523 kvmppc_set_pvr(vcpu, sregs->pvr);
524
525 j = 0;
526 for (i = 0; i < vcpu->arch.slb_nr; i++) {
527 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
528 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
529 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
530 ++j;
531 }
532 }
533 vcpu->arch.slb_max = j;
534
535 return 0;
536}
537
Paul Mackerras31f34382011-12-12 12:26:50 +0000538int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
539{
540 int r = -EINVAL;
541
542 switch (reg->id) {
543 case KVM_REG_PPC_HIOR:
544 r = put_user(0, (u64 __user *)reg->addr);
545 break;
546 default:
547 break;
548 }
549
550 return r;
551}
552
553int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
554{
555 int r = -EINVAL;
556
557 switch (reg->id) {
558 case KVM_REG_PPC_HIOR:
559 {
560 u64 hior;
561 /* Only allow this to be set to zero */
562 r = get_user(hior, (u64 __user *)reg->addr);
563 if (!r && (hior != 0))
564 r = -EINVAL;
565 break;
566 }
567 default:
568 break;
569 }
570
571 return r;
572}
573
Paul Mackerrasde56a942011-06-29 00:21:34 +0000574int kvmppc_core_check_processor_compat(void)
575{
Paul Mackerras9e368f22011-06-29 00:40:08 +0000576 if (cpu_has_feature(CPU_FTR_HVMODE))
Paul Mackerrasde56a942011-06-29 00:21:34 +0000577 return 0;
578 return -EIO;
579}
580
581struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
582{
583 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000584 int err = -EINVAL;
585 int core;
586 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000587
Paul Mackerras371fefd2011-06-29 00:23:08 +0000588 core = id / threads_per_core;
589 if (core >= KVM_MAX_VCORES)
590 goto out;
591
592 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200593 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000594 if (!vcpu)
595 goto out;
596
597 err = kvm_vcpu_init(vcpu, kvm, id);
598 if (err)
599 goto free_vcpu;
600
601 vcpu->arch.shared = &vcpu->arch.shregs;
602 vcpu->arch.last_cpu = -1;
603 vcpu->arch.mmcr[0] = MMCR0_FC;
604 vcpu->arch.ctrl = CTRL_RUNLATCH;
605 /* default to host PVR, since we can't spoof it */
606 vcpu->arch.pvr = mfspr(SPRN_PVR);
607 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000608 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000609
Paul Mackerrasde56a942011-06-29 00:21:34 +0000610 kvmppc_mmu_book3s_hv_init(vcpu);
611
Paul Mackerras371fefd2011-06-29 00:23:08 +0000612 /*
Paul Mackerras19ccb762011-07-23 17:42:46 +1000613 * We consider the vcpu stopped until we see the first run ioctl for it.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000614 */
Paul Mackerras19ccb762011-07-23 17:42:46 +1000615 vcpu->arch.state = KVMPPC_VCPU_STOPPED;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000616
617 init_waitqueue_head(&vcpu->arch.cpu_run);
618
619 mutex_lock(&kvm->lock);
620 vcore = kvm->arch.vcores[core];
621 if (!vcore) {
622 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
623 if (vcore) {
624 INIT_LIST_HEAD(&vcore->runnable_threads);
625 spin_lock_init(&vcore->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000626 init_waitqueue_head(&vcore->wq);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000627 vcore->preempt_tb = mftb();
Paul Mackerras371fefd2011-06-29 00:23:08 +0000628 }
629 kvm->arch.vcores[core] = vcore;
630 }
631 mutex_unlock(&kvm->lock);
632
633 if (!vcore)
634 goto free_vcpu;
635
636 spin_lock(&vcore->lock);
637 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000638 spin_unlock(&vcore->lock);
639 vcpu->arch.vcore = vcore;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000640 vcpu->arch.stolen_logged = vcore->stolen_tb;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000641
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200642 vcpu->arch.cpu_type = KVM_CPU_3S_64;
643 kvmppc_sanity_check(vcpu);
644
Paul Mackerrasde56a942011-06-29 00:21:34 +0000645 return vcpu;
646
647free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200648 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000649out:
650 return ERR_PTR(err);
651}
652
653void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
654{
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000655 spin_lock(&vcpu->arch.vpa_update_lock);
656 if (vcpu->arch.dtl.pinned_addr)
657 kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.dtl.pinned_addr);
658 if (vcpu->arch.slb_shadow.pinned_addr)
659 kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.slb_shadow.pinned_addr);
660 if (vcpu->arch.vpa.pinned_addr)
661 kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.vpa.pinned_addr);
662 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000663 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +0200664 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000665}
666
Paul Mackerras19ccb762011-07-23 17:42:46 +1000667static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000668{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000669 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000670
Paul Mackerras19ccb762011-07-23 17:42:46 +1000671 now = get_tb();
672 if (now > vcpu->arch.dec_expires) {
673 /* decrementer has already gone negative */
674 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -0600675 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000676 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000677 }
Paul Mackerras19ccb762011-07-23 17:42:46 +1000678 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
679 / tb_ticks_per_sec;
680 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
681 HRTIMER_MODE_REL);
682 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000683}
684
Paul Mackerras19ccb762011-07-23 17:42:46 +1000685static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000686{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000687 vcpu->arch.ceded = 0;
688 if (vcpu->arch.timer_running) {
689 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
690 vcpu->arch.timer_running = 0;
691 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000692}
693
694extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
695extern void xics_wake_cpu(int cpu);
696
697static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
698 struct kvm_vcpu *vcpu)
699{
700 struct kvm_vcpu *v;
701
702 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
703 return;
704 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
705 --vc->n_runnable;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000706 ++vc->n_busy;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000707 /* decrement the physical thread id of each following vcpu */
708 v = vcpu;
709 list_for_each_entry_continue(v, &vc->runnable_threads, arch.run_list)
710 --v->arch.ptid;
711 list_del(&vcpu->arch.run_list);
712}
713
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000714static int kvmppc_grab_hwthread(int cpu)
715{
716 struct paca_struct *tpaca;
717 long timeout = 1000;
718
719 tpaca = &paca[cpu];
720
721 /* Ensure the thread won't go into the kernel if it wakes */
722 tpaca->kvm_hstate.hwthread_req = 1;
723
724 /*
725 * If the thread is already executing in the kernel (e.g. handling
726 * a stray interrupt), wait for it to get back to nap mode.
727 * The smp_mb() is to ensure that our setting of hwthread_req
728 * is visible before we look at hwthread_state, so if this
729 * races with the code at system_reset_pSeries and the thread
730 * misses our setting of hwthread_req, we are sure to see its
731 * setting of hwthread_state, and vice versa.
732 */
733 smp_mb();
734 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
735 if (--timeout <= 0) {
736 pr_err("KVM: couldn't grab cpu %d\n", cpu);
737 return -EBUSY;
738 }
739 udelay(1);
740 }
741 return 0;
742}
743
744static void kvmppc_release_hwthread(int cpu)
745{
746 struct paca_struct *tpaca;
747
748 tpaca = &paca[cpu];
749 tpaca->kvm_hstate.hwthread_req = 0;
750 tpaca->kvm_hstate.kvm_vcpu = NULL;
751}
752
Paul Mackerras371fefd2011-06-29 00:23:08 +0000753static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
754{
755 int cpu;
756 struct paca_struct *tpaca;
757 struct kvmppc_vcore *vc = vcpu->arch.vcore;
758
Paul Mackerras19ccb762011-07-23 17:42:46 +1000759 if (vcpu->arch.timer_running) {
760 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
761 vcpu->arch.timer_running = 0;
762 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000763 cpu = vc->pcpu + vcpu->arch.ptid;
764 tpaca = &paca[cpu];
765 tpaca->kvm_hstate.kvm_vcpu = vcpu;
766 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000767 tpaca->kvm_hstate.napping = 0;
768 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000769 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +0000770#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000771 if (vcpu->arch.ptid) {
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000772 kvmppc_grab_hwthread(cpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +0000773 xics_wake_cpu(cpu);
774 ++vc->n_woken;
775 }
776#endif
777}
778
779static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
780{
781 int i;
782
783 HMT_low();
784 i = 0;
785 while (vc->nap_count < vc->n_woken) {
786 if (++i >= 1000000) {
787 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
788 vc->nap_count, vc->n_woken);
789 break;
790 }
791 cpu_relax();
792 }
793 HMT_medium();
794}
795
796/*
797 * Check that we are on thread 0 and that any other threads in
798 * this core are off-line.
799 */
800static int on_primary_thread(void)
801{
802 int cpu = smp_processor_id();
803 int thr = cpu_thread_in_core(cpu);
804
805 if (thr)
806 return 0;
807 while (++thr < threads_per_core)
808 if (cpu_online(cpu + thr))
809 return 0;
810 return 1;
811}
812
813/*
814 * Run a set of guest threads on a physical core.
815 * Called with vc->lock held.
816 */
817static int kvmppc_run_core(struct kvmppc_vcore *vc)
818{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000819 struct kvm_vcpu *vcpu, *vcpu0, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000820 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000821 u64 now;
Paul Mackerras081f3232012-06-01 20:20:24 +1000822 int ptid, i, need_vpa_update;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000823
Paul Mackerras371fefd2011-06-29 00:23:08 +0000824 /* don't start if any threads have a signal pending */
Paul Mackerras081f3232012-06-01 20:20:24 +1000825 need_vpa_update = 0;
826 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +0000827 if (signal_pending(vcpu->arch.run_task))
828 return 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000829 need_vpa_update |= vcpu->arch.vpa.update_pending |
830 vcpu->arch.slb_shadow.update_pending |
831 vcpu->arch.dtl.update_pending;
832 }
833
834 /*
835 * Initialize *vc, in particular vc->vcore_state, so we can
836 * drop the vcore lock if necessary.
837 */
838 vc->n_woken = 0;
839 vc->nap_count = 0;
840 vc->entry_exit_count = 0;
841 vc->vcore_state = VCORE_RUNNING;
842 vc->in_guest = 0;
843 vc->napping_threads = 0;
844
845 /*
846 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
847 * which can't be called with any spinlocks held.
848 */
849 if (need_vpa_update) {
850 spin_unlock(&vc->lock);
851 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
852 kvmppc_update_vpas(vcpu);
853 spin_lock(&vc->lock);
854 }
Paul Mackerrasde56a942011-06-29 00:21:34 +0000855
856 /*
857 * Make sure we are running on thread 0, and that
858 * secondary threads are offline.
859 * XXX we should also block attempts to bring any
860 * secondary threads online.
861 */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000862 if (threads_per_core > 1 && !on_primary_thread()) {
863 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
864 vcpu->arch.ret = -EBUSY;
865 goto out;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000866 }
867
Paul Mackerras19ccb762011-07-23 17:42:46 +1000868 /*
869 * Assign physical thread IDs, first to non-ceded vcpus
870 * and then to ceded ones.
871 */
872 ptid = 0;
873 vcpu0 = NULL;
874 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
875 if (!vcpu->arch.ceded) {
876 if (!ptid)
877 vcpu0 = vcpu;
878 vcpu->arch.ptid = ptid++;
879 }
880 }
881 if (!vcpu0)
882 return 0; /* nothing to run */
883 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
884 if (vcpu->arch.ceded)
885 vcpu->arch.ptid = ptid++;
886
Paul Mackerras0456ec42012-02-03 00:56:21 +0000887 vc->stolen_tb += mftb() - vc->preempt_tb;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000888 vc->pcpu = smp_processor_id();
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000889 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
Paul Mackerras371fefd2011-06-29 00:23:08 +0000890 kvmppc_start_thread(vcpu);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000891 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000892 }
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000893 /* Grab any remaining hw threads so they can't go into the kernel */
894 for (i = ptid; i < threads_per_core; ++i)
895 kvmppc_grab_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +0000896
897 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +1000898 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000899
Paul Mackerras19ccb762011-07-23 17:42:46 +1000900 kvm_guest_enter();
901 __kvmppc_vcore_entry(NULL, vcpu0);
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000902 for (i = 0; i < threads_per_core; ++i)
903 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000904
Paul Mackerras371fefd2011-06-29 00:23:08 +0000905 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000906 /* disable sending of IPIs on virtual external irqs */
907 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
908 vcpu->cpu = -1;
909 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000910 if (vc->nap_count < vc->n_woken)
911 kvmppc_wait_for_nap(vc);
912 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +1000913 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000914 spin_unlock(&vc->lock);
915
916 /* make sure updates to secondary vcpu structs are visible now */
917 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +0000918 kvm_guest_exit();
919
920 preempt_enable();
921 kvm_resched(vcpu);
922
923 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +0000924 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
925 /* cancel pending dec exception if dec is positive */
926 if (now < vcpu->arch.dec_expires &&
927 kvmppc_core_pending_dec(vcpu))
928 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000929
930 ret = RESUME_GUEST;
931 if (vcpu->arch.trap)
932 ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
933 vcpu->arch.run_task);
934
Paul Mackerras371fefd2011-06-29 00:23:08 +0000935 vcpu->arch.ret = ret;
936 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000937
938 if (vcpu->arch.ceded) {
939 if (ret != RESUME_GUEST)
940 kvmppc_end_cede(vcpu);
941 else
942 kvmppc_set_timer(vcpu);
943 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000944 }
Paul Mackerrasde56a942011-06-29 00:21:34 +0000945
Paul Mackerras371fefd2011-06-29 00:23:08 +0000946 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000947 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +1000948 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000949 vc->preempt_tb = mftb();
Paul Mackerras371fefd2011-06-29 00:23:08 +0000950 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
951 arch.run_list) {
952 if (vcpu->arch.ret != RESUME_GUEST) {
953 kvmppc_remove_runnable(vc, vcpu);
954 wake_up(&vcpu->arch.cpu_run);
955 }
956 }
957
958 return 1;
959}
960
Paul Mackerras19ccb762011-07-23 17:42:46 +1000961/*
962 * Wait for some other vcpu thread to execute us, and
963 * wake us up when we need to handle something in the host.
964 */
965static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000966{
Paul Mackerras371fefd2011-06-29 00:23:08 +0000967 DEFINE_WAIT(wait);
968
Paul Mackerras19ccb762011-07-23 17:42:46 +1000969 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
970 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
971 schedule();
972 finish_wait(&vcpu->arch.cpu_run, &wait);
973}
Paul Mackerras371fefd2011-06-29 00:23:08 +0000974
Paul Mackerras19ccb762011-07-23 17:42:46 +1000975/*
976 * All the vcpus in this vcore are idle, so wait for a decrementer
977 * or external interrupt to one of the vcpus. vc->lock is held.
978 */
979static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
980{
981 DEFINE_WAIT(wait);
982 struct kvm_vcpu *v;
983 int all_idle = 1;
984
985 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
986 vc->vcore_state = VCORE_SLEEPING;
987 spin_unlock(&vc->lock);
988 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
989 if (!v->arch.ceded || v->arch.pending_exceptions) {
990 all_idle = 0;
991 break;
992 }
993 }
994 if (all_idle)
995 schedule();
996 finish_wait(&vc->wq, &wait);
997 spin_lock(&vc->lock);
998 vc->vcore_state = VCORE_INACTIVE;
999}
1000
1001static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1002{
1003 int n_ceded;
1004 int prev_state;
1005 struct kvmppc_vcore *vc;
1006 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001007
Paul Mackerras371fefd2011-06-29 00:23:08 +00001008 kvm_run->exit_reason = 0;
1009 vcpu->arch.ret = RESUME_GUEST;
1010 vcpu->arch.trap = 0;
1011
Paul Mackerras371fefd2011-06-29 00:23:08 +00001012 /*
1013 * Synchronize with other threads in this virtual core
1014 */
1015 vc = vcpu->arch.vcore;
1016 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001017 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001018 vcpu->arch.run_task = current;
1019 vcpu->arch.kvm_run = kvm_run;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001020 prev_state = vcpu->arch.state;
1021 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001022 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1023 ++vc->n_runnable;
1024
Paul Mackerras19ccb762011-07-23 17:42:46 +10001025 /*
1026 * This happens the first time this is called for a vcpu.
1027 * If the vcore is already running, we may be able to start
1028 * this thread straight away and have it join in.
1029 */
1030 if (prev_state == KVMPPC_VCPU_STOPPED) {
1031 if (vc->vcore_state == VCORE_RUNNING &&
1032 VCORE_EXIT_COUNT(vc) == 0) {
1033 vcpu->arch.ptid = vc->n_runnable - 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001034 kvmppc_start_thread(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001035 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001036
Paul Mackerras19ccb762011-07-23 17:42:46 +10001037 } else if (prev_state == KVMPPC_VCPU_BUSY_IN_HOST)
1038 --vc->n_busy;
1039
1040 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1041 !signal_pending(current)) {
1042 if (vc->n_busy || vc->vcore_state != VCORE_INACTIVE) {
1043 spin_unlock(&vc->lock);
1044 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1045 spin_lock(&vc->lock);
1046 continue;
1047 }
Paul Mackerras0456ec42012-02-03 00:56:21 +00001048 vc->runner = vcpu;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001049 n_ceded = 0;
1050 list_for_each_entry(v, &vc->runnable_threads, arch.run_list)
1051 n_ceded += v->arch.ceded;
1052 if (n_ceded == vc->n_runnable)
1053 kvmppc_vcore_blocked(vc);
1054 else
1055 kvmppc_run_core(vc);
1056
1057 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1058 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06001059 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001060 if (signal_pending(v->arch.run_task)) {
1061 kvmppc_remove_runnable(vc, v);
1062 v->stat.signal_exits++;
1063 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1064 v->arch.ret = -EINTR;
1065 wake_up(&v->arch.cpu_run);
1066 }
1067 }
Paul Mackerras0456ec42012-02-03 00:56:21 +00001068 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001069 }
1070
Paul Mackerras19ccb762011-07-23 17:42:46 +10001071 if (signal_pending(current)) {
1072 if (vc->vcore_state == VCORE_RUNNING ||
1073 vc->vcore_state == VCORE_EXITING) {
1074 spin_unlock(&vc->lock);
1075 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1076 spin_lock(&vc->lock);
1077 }
1078 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1079 kvmppc_remove_runnable(vc, vcpu);
1080 vcpu->stat.signal_exits++;
1081 kvm_run->exit_reason = KVM_EXIT_INTR;
1082 vcpu->arch.ret = -EINTR;
1083 }
1084 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001085
Paul Mackerras19ccb762011-07-23 17:42:46 +10001086 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001087 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001088}
1089
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001090int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
1091{
1092 int r;
1093
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001094 if (!vcpu->arch.sane) {
1095 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1096 return -EINVAL;
1097 }
1098
Scott Wood25051b52011-11-08 18:23:23 -06001099 kvmppc_core_prepare_to_enter(vcpu);
1100
Paul Mackerras19ccb762011-07-23 17:42:46 +10001101 /* No need to go into the guest when all we'll do is come back out */
1102 if (signal_pending(current)) {
1103 run->exit_reason = KVM_EXIT_INTR;
1104 return -EINTR;
1105 }
1106
Paul Mackerras32fad282012-05-04 02:32:53 +00001107 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1108 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1109 smp_mb();
1110
1111 /* On the first time here, set up HTAB and VRMA or RMA */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001112 if (!vcpu->kvm->arch.rma_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00001113 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001114 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00001115 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001116 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001117
1118 flush_fp_to_thread(current);
1119 flush_altivec_to_thread(current);
1120 flush_vsx_to_thread(current);
1121 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001122 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001123
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001124 do {
1125 r = kvmppc_run_vcpu(run, vcpu);
1126
1127 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1128 !(vcpu->arch.shregs.msr & MSR_PR)) {
1129 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001130 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001131 }
1132 } while (r == RESUME_GUEST);
Paul Mackerras32fad282012-05-04 02:32:53 +00001133
1134 out:
1135 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001136 return r;
1137}
1138
David Gibson54738c02011-06-29 00:22:41 +00001139
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001140/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +00001141 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001142static inline int lpcr_rmls(unsigned long rma_size)
1143{
1144 switch (rma_size) {
1145 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001146 if (cpu_has_feature(CPU_FTR_ARCH_206))
1147 return 8; /* only supported on POWER7 */
1148 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001149 case 64ul << 20: /* 64 MB */
1150 return 3;
1151 case 128ul << 20: /* 128 MB */
1152 return 7;
1153 case 256ul << 20: /* 256 MB */
1154 return 4;
1155 case 1ul << 30: /* 1 GB */
1156 return 2;
1157 case 16ul << 30: /* 16 GB */
1158 return 1;
1159 case 256ul << 30: /* 256 GB */
1160 return 0;
1161 default:
1162 return -1;
1163 }
1164}
1165
1166static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1167{
Alexander Grafb4e70612012-01-16 16:50:10 +01001168 struct kvmppc_linear_info *ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001169 struct page *page;
1170
1171 if (vmf->pgoff >= ri->npages)
1172 return VM_FAULT_SIGBUS;
1173
1174 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1175 get_page(page);
1176 vmf->page = page;
1177 return 0;
1178}
1179
1180static const struct vm_operations_struct kvm_rma_vm_ops = {
1181 .fault = kvm_rma_fault,
1182};
1183
1184static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1185{
1186 vma->vm_flags |= VM_RESERVED;
1187 vma->vm_ops = &kvm_rma_vm_ops;
1188 return 0;
1189}
1190
1191static int kvm_rma_release(struct inode *inode, struct file *filp)
1192{
Alexander Grafb4e70612012-01-16 16:50:10 +01001193 struct kvmppc_linear_info *ri = filp->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001194
1195 kvm_release_rma(ri);
1196 return 0;
1197}
1198
1199static struct file_operations kvm_rma_fops = {
1200 .mmap = kvm_rma_mmap,
1201 .release = kvm_rma_release,
1202};
1203
1204long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
1205{
Alexander Grafb4e70612012-01-16 16:50:10 +01001206 struct kvmppc_linear_info *ri;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001207 long fd;
1208
1209 ri = kvm_alloc_rma();
1210 if (!ri)
1211 return -ENOMEM;
1212
1213 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR);
1214 if (fd < 0)
1215 kvm_release_rma(ri);
1216
1217 ret->rma_size = ri->npages << PAGE_SHIFT;
1218 return fd;
1219}
1220
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00001221static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1222 int linux_psize)
1223{
1224 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1225
1226 if (!def->shift)
1227 return;
1228 (*sps)->page_shift = def->shift;
1229 (*sps)->slb_enc = def->sllp;
1230 (*sps)->enc[0].page_shift = def->shift;
1231 (*sps)->enc[0].pte_enc = def->penc;
1232 (*sps)++;
1233}
1234
1235int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
1236{
1237 struct kvm_ppc_one_seg_page_size *sps;
1238
1239 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1240 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1241 info->flags |= KVM_PPC_1T_SEGMENTS;
1242 info->slb_size = mmu_slb_size;
1243
1244 /* We only support these sizes for now, and no muti-size segments */
1245 sps = &info->sps[0];
1246 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1247 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1248 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1249
1250 return 0;
1251}
1252
Paul Mackerras82ed3612011-12-15 02:03:22 +00001253/*
1254 * Get (and clear) the dirty memory log for a memory slot.
1255 */
1256int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1257{
1258 struct kvm_memory_slot *memslot;
1259 int r;
1260 unsigned long n;
1261
1262 mutex_lock(&kvm->slots_lock);
1263
1264 r = -EINVAL;
1265 if (log->slot >= KVM_MEMORY_SLOTS)
1266 goto out;
1267
1268 memslot = id_to_memslot(kvm->memslots, log->slot);
1269 r = -ENOENT;
1270 if (!memslot->dirty_bitmap)
1271 goto out;
1272
1273 n = kvm_dirty_bitmap_bytes(memslot);
1274 memset(memslot->dirty_bitmap, 0, n);
1275
1276 r = kvmppc_hv_get_dirty_log(kvm, memslot);
1277 if (r)
1278 goto out;
1279
1280 r = -EFAULT;
1281 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1282 goto out;
1283
1284 r = 0;
1285out:
1286 mutex_unlock(&kvm->slots_lock);
1287 return r;
1288}
1289
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001290static unsigned long slb_pgsize_encoding(unsigned long psize)
1291{
1292 unsigned long senc = 0;
1293
1294 if (psize > 0x1000) {
1295 senc = SLB_VSID_L;
1296 if (psize == 0x10000)
1297 senc |= SLB_VSID_LP_01;
1298 }
1299 return senc;
1300}
1301
Paul Mackerrasde56a942011-06-29 00:21:34 +00001302int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1303 struct kvm_userspace_memory_region *mem)
1304{
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001305 unsigned long npages;
Paul Mackerrasb2b2f162011-12-12 12:28:21 +00001306 unsigned long *phys;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001307
Paul Mackerrasb2b2f162011-12-12 12:28:21 +00001308 /* Allocate a slot_phys array */
Paul Mackerrasb2b2f162011-12-12 12:28:21 +00001309 phys = kvm->arch.slot_phys[mem->slot];
Paul Mackerras342d3db2011-12-12 12:38:05 +00001310 if (!kvm->arch.using_mmu_notifiers && !phys) {
1311 npages = mem->memory_size >> PAGE_SHIFT;
Paul Mackerrasb2b2f162011-12-12 12:28:21 +00001312 phys = vzalloc(npages * sizeof(unsigned long));
1313 if (!phys)
1314 return -ENOMEM;
1315 kvm->arch.slot_phys[mem->slot] = phys;
1316 kvm->arch.slot_npages[mem->slot] = npages;
1317 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001318
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001319 return 0;
1320}
1321
1322static void unpin_slot(struct kvm *kvm, int slot_id)
1323{
1324 unsigned long *physp;
1325 unsigned long j, npages, pfn;
1326 struct page *page;
1327
1328 physp = kvm->arch.slot_phys[slot_id];
1329 npages = kvm->arch.slot_npages[slot_id];
1330 if (physp) {
1331 spin_lock(&kvm->arch.slot_phys_lock);
1332 for (j = 0; j < npages; j++) {
1333 if (!(physp[j] & KVMPPC_GOT_PAGE))
1334 continue;
1335 pfn = physp[j] >> PAGE_SHIFT;
1336 page = pfn_to_page(pfn);
1337 SetPageDirty(page);
1338 put_page(page);
1339 }
1340 kvm->arch.slot_phys[slot_id] = NULL;
1341 spin_unlock(&kvm->arch.slot_phys_lock);
1342 vfree(physp);
1343 }
1344}
1345
1346void kvmppc_core_commit_memory_region(struct kvm *kvm,
1347 struct kvm_userspace_memory_region *mem)
1348{
1349}
1350
Paul Mackerras32fad282012-05-04 02:32:53 +00001351static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001352{
1353 int err = 0;
1354 struct kvm *kvm = vcpu->kvm;
Alexander Grafb4e70612012-01-16 16:50:10 +01001355 struct kvmppc_linear_info *ri = NULL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001356 unsigned long hva;
1357 struct kvm_memory_slot *memslot;
1358 struct vm_area_struct *vma;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001359 unsigned long lpcr, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001360 unsigned long psize, porder;
1361 unsigned long rma_size;
1362 unsigned long rmls;
1363 unsigned long *physp;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001364 unsigned long i, npages;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001365
1366 mutex_lock(&kvm->lock);
1367 if (kvm->arch.rma_setup_done)
1368 goto out; /* another vcpu beat us to it */
1369
Paul Mackerras32fad282012-05-04 02:32:53 +00001370 /* Allocate hashed page table (if not done already) and reset it */
1371 if (!kvm->arch.hpt_virt) {
1372 err = kvmppc_alloc_hpt(kvm, NULL);
1373 if (err) {
1374 pr_err("KVM: Couldn't alloc HPT\n");
1375 goto out;
1376 }
1377 }
1378
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001379 /* Look up the memslot for guest physical address 0 */
1380 memslot = gfn_to_memslot(kvm, 0);
1381
1382 /* We must have some memory at 0 by now */
1383 err = -EINVAL;
1384 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
1385 goto out;
1386
1387 /* Look up the VMA for the start of this memory slot */
1388 hva = memslot->userspace_addr;
1389 down_read(&current->mm->mmap_sem);
1390 vma = find_vma(current->mm, hva);
1391 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
1392 goto up_out;
1393
1394 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001395 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001396
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001397 /* Is this one of our preallocated RMAs? */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001398 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
1399 hva == vma->vm_start)
1400 ri = vma->vm_file->private_data;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001401
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001402 up_read(&current->mm->mmap_sem);
1403
1404 if (!ri) {
1405 /* On POWER7, use VRMA; on PPC970, give up */
1406 err = -EPERM;
1407 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1408 pr_err("KVM: CPU requires an RMO\n");
1409 goto out;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001410 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001411
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001412 /* We can handle 4k, 64k or 16M pages in the VRMA */
1413 err = -EINVAL;
1414 if (!(psize == 0x1000 || psize == 0x10000 ||
1415 psize == 0x1000000))
1416 goto out;
1417
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001418 /* Update VRMASD field in the LPCR */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001419 senc = slb_pgsize_encoding(psize);
Paul Mackerras697d3892011-12-12 12:36:37 +00001420 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1421 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001422 lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
1423 lpcr |= senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001424 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001425
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001426 /* Create HPTEs in the hash page table for the VRMA */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001427 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001428
1429 } else {
1430 /* Set up to use an RMO region */
1431 rma_size = ri->npages;
1432 if (rma_size > memslot->npages)
1433 rma_size = memslot->npages;
1434 rma_size <<= PAGE_SHIFT;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001435 rmls = lpcr_rmls(rma_size);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001436 err = -EINVAL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001437 if (rmls < 0) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001438 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
1439 goto out;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001440 }
1441 atomic_inc(&ri->use_count);
1442 kvm->arch.rma = ri;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001443
1444 /* Update LPCR and RMOR */
1445 lpcr = kvm->arch.lpcr;
1446 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1447 /* PPC970; insert RMLS value (split field) in HID4 */
1448 lpcr &= ~((1ul << HID4_RMLS0_SH) |
1449 (3ul << HID4_RMLS2_SH));
1450 lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
1451 ((rmls & 3) << HID4_RMLS2_SH);
1452 /* RMOR is also in HID4 */
1453 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
1454 << HID4_RMOR_SH;
1455 } else {
1456 /* POWER7 */
1457 lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
1458 lpcr |= rmls << LPCR_RMLS_SH;
1459 kvm->arch.rmor = kvm->arch.rma->base_pfn << PAGE_SHIFT;
1460 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001461 kvm->arch.lpcr = lpcr;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001462 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001463 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001464
1465 /* Initialize phys addrs of pages in RMO */
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001466 npages = ri->npages;
1467 porder = __ilog2(npages);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001468 physp = kvm->arch.slot_phys[memslot->id];
1469 spin_lock(&kvm->arch.slot_phys_lock);
1470 for (i = 0; i < npages; ++i)
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001471 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) + porder;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001472 spin_unlock(&kvm->arch.slot_phys_lock);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001473 }
1474
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001475 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
1476 smp_wmb();
1477 kvm->arch.rma_setup_done = 1;
1478 err = 0;
1479 out:
1480 mutex_unlock(&kvm->lock);
1481 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001482
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001483 up_out:
1484 up_read(&current->mm->mmap_sem);
1485 goto out;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001486}
1487
1488int kvmppc_core_init_vm(struct kvm *kvm)
1489{
Paul Mackerras32fad282012-05-04 02:32:53 +00001490 unsigned long lpcr, lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001491
Paul Mackerras32fad282012-05-04 02:32:53 +00001492 /* Allocate the guest's logical partition ID */
1493
1494 lpid = kvmppc_alloc_lpid();
1495 if (lpid < 0)
1496 return -ENOMEM;
1497 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001498
David Gibson54738c02011-06-29 00:22:41 +00001499 INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001500
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001501 kvm->arch.rma = NULL;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001502
Paul Mackerras9e368f22011-06-29 00:40:08 +00001503 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001504
Paul Mackerras9e368f22011-06-29 00:40:08 +00001505 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1506 /* PPC970; HID4 is effectively the LPCR */
Paul Mackerras9e368f22011-06-29 00:40:08 +00001507 kvm->arch.host_lpid = 0;
1508 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
1509 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
1510 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
1511 ((lpid & 0xf) << HID4_LPID5_SH);
1512 } else {
1513 /* POWER7; init LPCR for virtual RMA mode */
1514 kvm->arch.host_lpid = mfspr(SPRN_LPID);
1515 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
1516 lpcr &= LPCR_PECE | LPCR_LPES;
1517 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
Paul Mackerras697d3892011-12-12 12:36:37 +00001518 LPCR_VPM0 | LPCR_VPM1;
1519 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
1520 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerras9e368f22011-06-29 00:40:08 +00001521 }
1522 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001523
Paul Mackerras342d3db2011-12-12 12:38:05 +00001524 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001525 spin_lock_init(&kvm->arch.slot_phys_lock);
David Gibson54738c02011-06-29 00:22:41 +00001526 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001527}
1528
1529void kvmppc_core_destroy_vm(struct kvm *kvm)
1530{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001531 unsigned long i;
1532
Paul Mackerras342d3db2011-12-12 12:38:05 +00001533 if (!kvm->arch.using_mmu_notifiers)
1534 for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
1535 unpin_slot(kvm, i);
Paul Mackerrasb2b2f162011-12-12 12:28:21 +00001536
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001537 if (kvm->arch.rma) {
1538 kvm_release_rma(kvm->arch.rma);
1539 kvm->arch.rma = NULL;
1540 }
1541
Paul Mackerrasde56a942011-06-29 00:21:34 +00001542 kvmppc_free_hpt(kvm);
David Gibson54738c02011-06-29 00:22:41 +00001543 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
Paul Mackerrasde56a942011-06-29 00:21:34 +00001544}
1545
1546/* These are stubs for now */
1547void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
1548{
1549}
1550
1551/* We don't need to emulate any privileged instructions or dcbz */
1552int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
1553 unsigned int inst, int *advance)
1554{
1555 return EMULATE_FAIL;
1556}
1557
Alexander Graf54771e62012-05-04 14:55:12 +02001558int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001559{
1560 return EMULATE_FAIL;
1561}
1562
Alexander Graf54771e62012-05-04 14:55:12 +02001563int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001564{
1565 return EMULATE_FAIL;
1566}
1567
1568static int kvmppc_book3s_hv_init(void)
1569{
1570 int r;
1571
1572 r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1573
1574 if (r)
1575 return r;
1576
1577 r = kvmppc_mmu_hv_init();
1578
1579 return r;
1580}
1581
1582static void kvmppc_book3s_hv_exit(void)
1583{
1584 kvm_exit();
1585}
1586
1587module_init(kvmppc_book3s_hv_init);
1588module_exit(kvmppc_book3s_hv_exit);