blob: b1e3b9c1326a013a8df44f27795dd5af22b826fb [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>
Paul Mackerrasde56a942011-06-29 00:21:34 +000048#include <linux/gfp.h>
49#include <linux/sched.h>
50#include <linux/vmalloc.h>
51#include <linux/highmem.h>
52
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000053/*
54 * For now, limit memory to 64GB and require it to be large pages.
55 * This value is chosen because it makes the ram_pginfo array be
56 * 64kB in size, which is about as large as we want to be trying
57 * to allocate with kmalloc.
58 */
59#define MAX_MEM_ORDER 36
60
61#define LARGE_PAGE_ORDER 24 /* 16MB pages */
62
Paul Mackerrasde56a942011-06-29 00:21:34 +000063/* #define EXIT_DEBUG */
64/* #define EXIT_DEBUG_SIMPLE */
65/* #define EXIT_DEBUG_INT */
66
Paul Mackerras19ccb762011-07-23 17:42:46 +100067static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
68
Paul Mackerrasde56a942011-06-29 00:21:34 +000069void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
70{
71 local_paca->kvm_hstate.kvm_vcpu = vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +000072 local_paca->kvm_hstate.kvm_vcore = vcpu->arch.vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +000073}
74
75void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
76{
77}
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
145static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
146 unsigned long flags,
147 unsigned long vcpuid, unsigned long vpa)
148{
149 struct kvm *kvm = vcpu->kvm;
150 unsigned long pg_index, ra, len;
151 unsigned long pg_offset;
152 void *va;
153 struct kvm_vcpu *tvcpu;
154
155 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
156 if (!tvcpu)
157 return H_PARAMETER;
158
159 flags >>= 63 - 18;
160 flags &= 7;
161 if (flags == 0 || flags == 4)
162 return H_PARAMETER;
163 if (flags < 4) {
164 if (vpa & 0x7f)
165 return H_PARAMETER;
166 /* registering new area; convert logical addr to real */
167 pg_index = vpa >> kvm->arch.ram_porder;
168 pg_offset = vpa & (kvm->arch.ram_psize - 1);
169 if (pg_index >= kvm->arch.ram_npages)
170 return H_PARAMETER;
171 if (kvm->arch.ram_pginfo[pg_index].pfn == 0)
172 return H_PARAMETER;
173 ra = kvm->arch.ram_pginfo[pg_index].pfn << PAGE_SHIFT;
174 ra |= pg_offset;
175 va = __va(ra);
176 if (flags <= 1)
177 len = *(unsigned short *)(va + 4);
178 else
179 len = *(unsigned int *)(va + 4);
180 if (pg_offset + len > kvm->arch.ram_psize)
181 return H_PARAMETER;
182 switch (flags) {
183 case 1: /* register VPA */
184 if (len < 640)
185 return H_PARAMETER;
186 tvcpu->arch.vpa = va;
187 init_vpa(vcpu, va);
188 break;
189 case 2: /* register DTL */
190 if (len < 48)
191 return H_PARAMETER;
192 if (!tvcpu->arch.vpa)
193 return H_RESOURCE;
194 len -= len % 48;
195 tvcpu->arch.dtl = va;
196 tvcpu->arch.dtl_end = va + len;
197 break;
198 case 3: /* register SLB shadow buffer */
199 if (len < 8)
200 return H_PARAMETER;
201 if (!tvcpu->arch.vpa)
202 return H_RESOURCE;
203 tvcpu->arch.slb_shadow = va;
204 len = (len - 16) / 16;
205 tvcpu->arch.slb_shadow = va;
206 break;
207 }
208 } else {
209 switch (flags) {
210 case 5: /* unregister VPA */
211 if (tvcpu->arch.slb_shadow || tvcpu->arch.dtl)
212 return H_RESOURCE;
213 tvcpu->arch.vpa = NULL;
214 break;
215 case 6: /* unregister DTL */
216 tvcpu->arch.dtl = NULL;
217 break;
218 case 7: /* unregister SLB shadow buffer */
219 tvcpu->arch.slb_shadow = NULL;
220 break;
221 }
222 }
223 return H_SUCCESS;
224}
225
226int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
227{
228 unsigned long req = kvmppc_get_gpr(vcpu, 3);
229 unsigned long target, ret = H_SUCCESS;
230 struct kvm_vcpu *tvcpu;
231
232 switch (req) {
233 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000234 break;
235 case H_PROD:
236 target = kvmppc_get_gpr(vcpu, 4);
237 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
238 if (!tvcpu) {
239 ret = H_PARAMETER;
240 break;
241 }
242 tvcpu->arch.prodded = 1;
243 smp_mb();
244 if (vcpu->arch.ceded) {
245 if (waitqueue_active(&vcpu->wq)) {
246 wake_up_interruptible(&vcpu->wq);
247 vcpu->stat.halt_wakeup++;
248 }
249 }
250 break;
251 case H_CONFER:
252 break;
253 case H_REGISTER_VPA:
254 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
255 kvmppc_get_gpr(vcpu, 5),
256 kvmppc_get_gpr(vcpu, 6));
257 break;
258 default:
259 return RESUME_HOST;
260 }
261 kvmppc_set_gpr(vcpu, 3, ret);
262 vcpu->arch.hcall_needed = 0;
263 return RESUME_GUEST;
264}
265
Paul Mackerrasde56a942011-06-29 00:21:34 +0000266static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
267 struct task_struct *tsk)
268{
269 int r = RESUME_HOST;
270
271 vcpu->stat.sum_exits++;
272
273 run->exit_reason = KVM_EXIT_UNKNOWN;
274 run->ready_for_interrupt_injection = 1;
275 switch (vcpu->arch.trap) {
276 /* We're good on these - the host merely wanted to get our attention */
277 case BOOK3S_INTERRUPT_HV_DECREMENTER:
278 vcpu->stat.dec_exits++;
279 r = RESUME_GUEST;
280 break;
281 case BOOK3S_INTERRUPT_EXTERNAL:
282 vcpu->stat.ext_intr_exits++;
283 r = RESUME_GUEST;
284 break;
285 case BOOK3S_INTERRUPT_PERFMON:
286 r = RESUME_GUEST;
287 break;
288 case BOOK3S_INTERRUPT_PROGRAM:
289 {
290 ulong flags;
291 /*
292 * Normally program interrupts are delivered directly
293 * to the guest by the hardware, but we can get here
294 * as a result of a hypervisor emulation interrupt
295 * (e40) getting turned into a 700 by BML RTAS.
296 */
297 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
298 kvmppc_core_queue_program(vcpu, flags);
299 r = RESUME_GUEST;
300 break;
301 }
302 case BOOK3S_INTERRUPT_SYSCALL:
303 {
304 /* hcall - punt to userspace */
305 int i;
306
307 if (vcpu->arch.shregs.msr & MSR_PR) {
308 /* sc 1 from userspace - reflect to guest syscall */
309 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
310 r = RESUME_GUEST;
311 break;
312 }
313 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
314 for (i = 0; i < 9; ++i)
315 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
316 run->exit_reason = KVM_EXIT_PAPR_HCALL;
317 vcpu->arch.hcall_needed = 1;
318 r = RESUME_HOST;
319 break;
320 }
321 /*
322 * We get these next two if the guest does a bad real-mode access,
323 * as we have enabled VRMA (virtualized real mode area) mode in the
324 * LPCR. We just generate an appropriate DSI/ISI to the guest.
325 */
326 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
327 vcpu->arch.shregs.dsisr = vcpu->arch.fault_dsisr;
328 vcpu->arch.shregs.dar = vcpu->arch.fault_dar;
329 kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_DATA_STORAGE, 0);
330 r = RESUME_GUEST;
331 break;
332 case BOOK3S_INTERRUPT_H_INST_STORAGE:
333 kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_INST_STORAGE,
334 0x08000000);
335 r = RESUME_GUEST;
336 break;
337 /*
338 * This occurs if the guest executes an illegal instruction.
339 * We just generate a program interrupt to the guest, since
340 * we don't emulate any guest instructions at this stage.
341 */
342 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
343 kvmppc_core_queue_program(vcpu, 0x80000);
344 r = RESUME_GUEST;
345 break;
346 default:
347 kvmppc_dump_regs(vcpu);
348 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
349 vcpu->arch.trap, kvmppc_get_pc(vcpu),
350 vcpu->arch.shregs.msr);
351 r = RESUME_HOST;
352 BUG();
353 break;
354 }
355
Paul Mackerrasde56a942011-06-29 00:21:34 +0000356 return r;
357}
358
359int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
360 struct kvm_sregs *sregs)
361{
362 int i;
363
364 sregs->pvr = vcpu->arch.pvr;
365
366 memset(sregs, 0, sizeof(struct kvm_sregs));
367 for (i = 0; i < vcpu->arch.slb_max; i++) {
368 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
369 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
370 }
371
372 return 0;
373}
374
375int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
376 struct kvm_sregs *sregs)
377{
378 int i, j;
379
380 kvmppc_set_pvr(vcpu, sregs->pvr);
381
382 j = 0;
383 for (i = 0; i < vcpu->arch.slb_nr; i++) {
384 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
385 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
386 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
387 ++j;
388 }
389 }
390 vcpu->arch.slb_max = j;
391
392 return 0;
393}
394
395int kvmppc_core_check_processor_compat(void)
396{
Paul Mackerras9e368f22011-06-29 00:40:08 +0000397 if (cpu_has_feature(CPU_FTR_HVMODE))
Paul Mackerrasde56a942011-06-29 00:21:34 +0000398 return 0;
399 return -EIO;
400}
401
402struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
403{
404 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000405 int err = -EINVAL;
406 int core;
407 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000408
Paul Mackerras371fefd2011-06-29 00:23:08 +0000409 core = id / threads_per_core;
410 if (core >= KVM_MAX_VCORES)
411 goto out;
412
413 err = -ENOMEM;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000414 vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
415 if (!vcpu)
416 goto out;
417
418 err = kvm_vcpu_init(vcpu, kvm, id);
419 if (err)
420 goto free_vcpu;
421
422 vcpu->arch.shared = &vcpu->arch.shregs;
423 vcpu->arch.last_cpu = -1;
424 vcpu->arch.mmcr[0] = MMCR0_FC;
425 vcpu->arch.ctrl = CTRL_RUNLATCH;
426 /* default to host PVR, since we can't spoof it */
427 vcpu->arch.pvr = mfspr(SPRN_PVR);
428 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
429
Paul Mackerrasde56a942011-06-29 00:21:34 +0000430 kvmppc_mmu_book3s_hv_init(vcpu);
431
Paul Mackerras371fefd2011-06-29 00:23:08 +0000432 /*
Paul Mackerras19ccb762011-07-23 17:42:46 +1000433 * We consider the vcpu stopped until we see the first run ioctl for it.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000434 */
Paul Mackerras19ccb762011-07-23 17:42:46 +1000435 vcpu->arch.state = KVMPPC_VCPU_STOPPED;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000436
437 init_waitqueue_head(&vcpu->arch.cpu_run);
438
439 mutex_lock(&kvm->lock);
440 vcore = kvm->arch.vcores[core];
441 if (!vcore) {
442 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
443 if (vcore) {
444 INIT_LIST_HEAD(&vcore->runnable_threads);
445 spin_lock_init(&vcore->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000446 init_waitqueue_head(&vcore->wq);
Paul Mackerras371fefd2011-06-29 00:23:08 +0000447 }
448 kvm->arch.vcores[core] = vcore;
449 }
450 mutex_unlock(&kvm->lock);
451
452 if (!vcore)
453 goto free_vcpu;
454
455 spin_lock(&vcore->lock);
456 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000457 spin_unlock(&vcore->lock);
458 vcpu->arch.vcore = vcore;
459
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200460 vcpu->arch.cpu_type = KVM_CPU_3S_64;
461 kvmppc_sanity_check(vcpu);
462
Paul Mackerrasde56a942011-06-29 00:21:34 +0000463 return vcpu;
464
465free_vcpu:
466 kfree(vcpu);
467out:
468 return ERR_PTR(err);
469}
470
471void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
472{
473 kvm_vcpu_uninit(vcpu);
474 kfree(vcpu);
475}
476
Paul Mackerras19ccb762011-07-23 17:42:46 +1000477static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000478{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000479 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000480
Paul Mackerras19ccb762011-07-23 17:42:46 +1000481 now = get_tb();
482 if (now > vcpu->arch.dec_expires) {
483 /* decrementer has already gone negative */
484 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -0600485 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000486 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000487 }
Paul Mackerras19ccb762011-07-23 17:42:46 +1000488 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
489 / tb_ticks_per_sec;
490 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
491 HRTIMER_MODE_REL);
492 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000493}
494
Paul Mackerras19ccb762011-07-23 17:42:46 +1000495static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000496{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000497 vcpu->arch.ceded = 0;
498 if (vcpu->arch.timer_running) {
499 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
500 vcpu->arch.timer_running = 0;
501 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000502}
503
504extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
505extern void xics_wake_cpu(int cpu);
506
507static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
508 struct kvm_vcpu *vcpu)
509{
510 struct kvm_vcpu *v;
511
512 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
513 return;
514 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
515 --vc->n_runnable;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000516 ++vc->n_busy;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000517 /* decrement the physical thread id of each following vcpu */
518 v = vcpu;
519 list_for_each_entry_continue(v, &vc->runnable_threads, arch.run_list)
520 --v->arch.ptid;
521 list_del(&vcpu->arch.run_list);
522}
523
524static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
525{
526 int cpu;
527 struct paca_struct *tpaca;
528 struct kvmppc_vcore *vc = vcpu->arch.vcore;
529
Paul Mackerras19ccb762011-07-23 17:42:46 +1000530 if (vcpu->arch.timer_running) {
531 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
532 vcpu->arch.timer_running = 0;
533 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000534 cpu = vc->pcpu + vcpu->arch.ptid;
535 tpaca = &paca[cpu];
536 tpaca->kvm_hstate.kvm_vcpu = vcpu;
537 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000538 tpaca->kvm_hstate.napping = 0;
539 vcpu->cpu = vc->pcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000540 smp_wmb();
Michael Neuling251da032011-11-10 16:03:20 +0000541#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000542 if (vcpu->arch.ptid) {
543 tpaca->cpu_start = 0x80;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000544 wmb();
545 xics_wake_cpu(cpu);
546 ++vc->n_woken;
547 }
548#endif
549}
550
551static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
552{
553 int i;
554
555 HMT_low();
556 i = 0;
557 while (vc->nap_count < vc->n_woken) {
558 if (++i >= 1000000) {
559 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
560 vc->nap_count, vc->n_woken);
561 break;
562 }
563 cpu_relax();
564 }
565 HMT_medium();
566}
567
568/*
569 * Check that we are on thread 0 and that any other threads in
570 * this core are off-line.
571 */
572static int on_primary_thread(void)
573{
574 int cpu = smp_processor_id();
575 int thr = cpu_thread_in_core(cpu);
576
577 if (thr)
578 return 0;
579 while (++thr < threads_per_core)
580 if (cpu_online(cpu + thr))
581 return 0;
582 return 1;
583}
584
585/*
586 * Run a set of guest threads on a physical core.
587 * Called with vc->lock held.
588 */
589static int kvmppc_run_core(struct kvmppc_vcore *vc)
590{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000591 struct kvm_vcpu *vcpu, *vcpu0, *vnext;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000592 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000593 u64 now;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000594 int ptid;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000595
Paul Mackerras371fefd2011-06-29 00:23:08 +0000596 /* don't start if any threads have a signal pending */
597 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
598 if (signal_pending(vcpu->arch.run_task))
599 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000600
601 /*
602 * Make sure we are running on thread 0, and that
603 * secondary threads are offline.
604 * XXX we should also block attempts to bring any
605 * secondary threads online.
606 */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000607 if (threads_per_core > 1 && !on_primary_thread()) {
608 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
609 vcpu->arch.ret = -EBUSY;
610 goto out;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000611 }
612
Paul Mackerras19ccb762011-07-23 17:42:46 +1000613 /*
614 * Assign physical thread IDs, first to non-ceded vcpus
615 * and then to ceded ones.
616 */
617 ptid = 0;
618 vcpu0 = NULL;
619 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
620 if (!vcpu->arch.ceded) {
621 if (!ptid)
622 vcpu0 = vcpu;
623 vcpu->arch.ptid = ptid++;
624 }
625 }
626 if (!vcpu0)
627 return 0; /* nothing to run */
628 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
629 if (vcpu->arch.ceded)
630 vcpu->arch.ptid = ptid++;
631
Paul Mackerras371fefd2011-06-29 00:23:08 +0000632 vc->n_woken = 0;
633 vc->nap_count = 0;
634 vc->entry_exit_count = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000635 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000636 vc->in_guest = 0;
637 vc->pcpu = smp_processor_id();
Paul Mackerras19ccb762011-07-23 17:42:46 +1000638 vc->napping_threads = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000639 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
640 kvmppc_start_thread(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +0000641
642 preempt_disable();
Paul Mackerras19ccb762011-07-23 17:42:46 +1000643 spin_unlock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000644
Paul Mackerras19ccb762011-07-23 17:42:46 +1000645 kvm_guest_enter();
646 __kvmppc_vcore_entry(NULL, vcpu0);
647
Paul Mackerras371fefd2011-06-29 00:23:08 +0000648 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000649 /* disable sending of IPIs on virtual external irqs */
650 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
651 vcpu->cpu = -1;
652 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000653 if (vc->nap_count < vc->n_woken)
654 kvmppc_wait_for_nap(vc);
655 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +1000656 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000657 spin_unlock(&vc->lock);
658
659 /* make sure updates to secondary vcpu structs are visible now */
660 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +0000661 kvm_guest_exit();
662
663 preempt_enable();
664 kvm_resched(vcpu);
665
666 now = get_tb();
Paul Mackerras371fefd2011-06-29 00:23:08 +0000667 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
668 /* cancel pending dec exception if dec is positive */
669 if (now < vcpu->arch.dec_expires &&
670 kvmppc_core_pending_dec(vcpu))
671 kvmppc_core_dequeue_dec(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000672
673 ret = RESUME_GUEST;
674 if (vcpu->arch.trap)
675 ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
676 vcpu->arch.run_task);
677
Paul Mackerras371fefd2011-06-29 00:23:08 +0000678 vcpu->arch.ret = ret;
679 vcpu->arch.trap = 0;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000680
681 if (vcpu->arch.ceded) {
682 if (ret != RESUME_GUEST)
683 kvmppc_end_cede(vcpu);
684 else
685 kvmppc_set_timer(vcpu);
686 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000687 }
Paul Mackerrasde56a942011-06-29 00:21:34 +0000688
Paul Mackerras371fefd2011-06-29 00:23:08 +0000689 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000690 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +1000691 vc->vcore_state = VCORE_INACTIVE;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000692 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
693 arch.run_list) {
694 if (vcpu->arch.ret != RESUME_GUEST) {
695 kvmppc_remove_runnable(vc, vcpu);
696 wake_up(&vcpu->arch.cpu_run);
697 }
698 }
699
700 return 1;
701}
702
Paul Mackerras19ccb762011-07-23 17:42:46 +1000703/*
704 * Wait for some other vcpu thread to execute us, and
705 * wake us up when we need to handle something in the host.
706 */
707static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000708{
Paul Mackerras371fefd2011-06-29 00:23:08 +0000709 DEFINE_WAIT(wait);
710
Paul Mackerras19ccb762011-07-23 17:42:46 +1000711 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
712 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
713 schedule();
714 finish_wait(&vcpu->arch.cpu_run, &wait);
715}
Paul Mackerras371fefd2011-06-29 00:23:08 +0000716
Paul Mackerras19ccb762011-07-23 17:42:46 +1000717/*
718 * All the vcpus in this vcore are idle, so wait for a decrementer
719 * or external interrupt to one of the vcpus. vc->lock is held.
720 */
721static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
722{
723 DEFINE_WAIT(wait);
724 struct kvm_vcpu *v;
725 int all_idle = 1;
726
727 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
728 vc->vcore_state = VCORE_SLEEPING;
729 spin_unlock(&vc->lock);
730 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
731 if (!v->arch.ceded || v->arch.pending_exceptions) {
732 all_idle = 0;
733 break;
734 }
735 }
736 if (all_idle)
737 schedule();
738 finish_wait(&vc->wq, &wait);
739 spin_lock(&vc->lock);
740 vc->vcore_state = VCORE_INACTIVE;
741}
742
743static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
744{
745 int n_ceded;
746 int prev_state;
747 struct kvmppc_vcore *vc;
748 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +0000749
Paul Mackerras371fefd2011-06-29 00:23:08 +0000750 kvm_run->exit_reason = 0;
751 vcpu->arch.ret = RESUME_GUEST;
752 vcpu->arch.trap = 0;
753
Paul Mackerras371fefd2011-06-29 00:23:08 +0000754 /*
755 * Synchronize with other threads in this virtual core
756 */
757 vc = vcpu->arch.vcore;
758 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000759 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000760 vcpu->arch.run_task = current;
761 vcpu->arch.kvm_run = kvm_run;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000762 prev_state = vcpu->arch.state;
763 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000764 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
765 ++vc->n_runnable;
766
Paul Mackerras19ccb762011-07-23 17:42:46 +1000767 /*
768 * This happens the first time this is called for a vcpu.
769 * If the vcore is already running, we may be able to start
770 * this thread straight away and have it join in.
771 */
772 if (prev_state == KVMPPC_VCPU_STOPPED) {
773 if (vc->vcore_state == VCORE_RUNNING &&
774 VCORE_EXIT_COUNT(vc) == 0) {
775 vcpu->arch.ptid = vc->n_runnable - 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000776 kvmppc_start_thread(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000777 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000778
Paul Mackerras19ccb762011-07-23 17:42:46 +1000779 } else if (prev_state == KVMPPC_VCPU_BUSY_IN_HOST)
780 --vc->n_busy;
781
782 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
783 !signal_pending(current)) {
784 if (vc->n_busy || vc->vcore_state != VCORE_INACTIVE) {
785 spin_unlock(&vc->lock);
786 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
787 spin_lock(&vc->lock);
788 continue;
789 }
790 n_ceded = 0;
791 list_for_each_entry(v, &vc->runnable_threads, arch.run_list)
792 n_ceded += v->arch.ceded;
793 if (n_ceded == vc->n_runnable)
794 kvmppc_vcore_blocked(vc);
795 else
796 kvmppc_run_core(vc);
797
798 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
799 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -0600800 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000801 if (signal_pending(v->arch.run_task)) {
802 kvmppc_remove_runnable(vc, v);
803 v->stat.signal_exits++;
804 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
805 v->arch.ret = -EINTR;
806 wake_up(&v->arch.cpu_run);
807 }
808 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000809 }
810
Paul Mackerras19ccb762011-07-23 17:42:46 +1000811 if (signal_pending(current)) {
812 if (vc->vcore_state == VCORE_RUNNING ||
813 vc->vcore_state == VCORE_EXITING) {
814 spin_unlock(&vc->lock);
815 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
816 spin_lock(&vc->lock);
817 }
818 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
819 kvmppc_remove_runnable(vc, vcpu);
820 vcpu->stat.signal_exits++;
821 kvm_run->exit_reason = KVM_EXIT_INTR;
822 vcpu->arch.ret = -EINTR;
823 }
824 }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000825
Paul Mackerras19ccb762011-07-23 17:42:46 +1000826 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +0000827 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000828}
829
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000830int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
831{
832 int r;
833
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200834 if (!vcpu->arch.sane) {
835 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
836 return -EINVAL;
837 }
838
Scott Wood25051b5a2011-11-08 18:23:23 -0600839 kvmppc_core_prepare_to_enter(vcpu);
840
Paul Mackerras19ccb762011-07-23 17:42:46 +1000841 /* No need to go into the guest when all we'll do is come back out */
842 if (signal_pending(current)) {
843 run->exit_reason = KVM_EXIT_INTR;
844 return -EINTR;
845 }
846
847 /* On PPC970, check that we have an RMA region */
848 if (!vcpu->kvm->arch.rma && cpu_has_feature(CPU_FTR_ARCH_201))
849 return -EPERM;
850
851 flush_fp_to_thread(current);
852 flush_altivec_to_thread(current);
853 flush_vsx_to_thread(current);
854 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
855
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000856 do {
857 r = kvmppc_run_vcpu(run, vcpu);
858
859 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
860 !(vcpu->arch.shregs.msr & MSR_PR)) {
861 r = kvmppc_pseries_do_hcall(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -0600862 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000863 }
864 } while (r == RESUME_GUEST);
865 return r;
866}
867
David Gibson54738c02011-06-29 00:22:41 +0000868static long kvmppc_stt_npages(unsigned long window_size)
869{
870 return ALIGN((window_size >> SPAPR_TCE_SHIFT)
871 * sizeof(u64), PAGE_SIZE) / PAGE_SIZE;
872}
873
874static void release_spapr_tce_table(struct kvmppc_spapr_tce_table *stt)
875{
876 struct kvm *kvm = stt->kvm;
877 int i;
878
879 mutex_lock(&kvm->lock);
880 list_del(&stt->list);
881 for (i = 0; i < kvmppc_stt_npages(stt->window_size); i++)
882 __free_page(stt->pages[i]);
883 kfree(stt);
884 mutex_unlock(&kvm->lock);
885
886 kvm_put_kvm(kvm);
887}
888
889static int kvm_spapr_tce_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
890{
891 struct kvmppc_spapr_tce_table *stt = vma->vm_file->private_data;
892 struct page *page;
893
894 if (vmf->pgoff >= kvmppc_stt_npages(stt->window_size))
895 return VM_FAULT_SIGBUS;
896
897 page = stt->pages[vmf->pgoff];
898 get_page(page);
899 vmf->page = page;
900 return 0;
901}
902
903static const struct vm_operations_struct kvm_spapr_tce_vm_ops = {
904 .fault = kvm_spapr_tce_fault,
905};
906
907static int kvm_spapr_tce_mmap(struct file *file, struct vm_area_struct *vma)
908{
909 vma->vm_ops = &kvm_spapr_tce_vm_ops;
910 return 0;
911}
912
913static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)
914{
915 struct kvmppc_spapr_tce_table *stt = filp->private_data;
916
917 release_spapr_tce_table(stt);
918 return 0;
919}
920
921static struct file_operations kvm_spapr_tce_fops = {
922 .mmap = kvm_spapr_tce_mmap,
923 .release = kvm_spapr_tce_release,
924};
925
926long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
927 struct kvm_create_spapr_tce *args)
928{
929 struct kvmppc_spapr_tce_table *stt = NULL;
930 long npages;
931 int ret = -ENOMEM;
932 int i;
933
934 /* Check this LIOBN hasn't been previously allocated */
935 list_for_each_entry(stt, &kvm->arch.spapr_tce_tables, list) {
936 if (stt->liobn == args->liobn)
937 return -EBUSY;
938 }
939
940 npages = kvmppc_stt_npages(args->window_size);
941
942 stt = kzalloc(sizeof(*stt) + npages* sizeof(struct page *),
943 GFP_KERNEL);
944 if (!stt)
945 goto fail;
946
947 stt->liobn = args->liobn;
948 stt->window_size = args->window_size;
949 stt->kvm = kvm;
950
951 for (i = 0; i < npages; i++) {
952 stt->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
953 if (!stt->pages[i])
954 goto fail;
955 }
956
957 kvm_get_kvm(kvm);
958
959 mutex_lock(&kvm->lock);
960 list_add(&stt->list, &kvm->arch.spapr_tce_tables);
961
962 mutex_unlock(&kvm->lock);
963
964 return anon_inode_getfd("kvm-spapr-tce", &kvm_spapr_tce_fops,
965 stt, O_RDWR);
966
967fail:
968 if (stt) {
969 for (i = 0; i < npages; i++)
970 if (stt->pages[i])
971 __free_page(stt->pages[i]);
972
973 kfree(stt);
974 }
975 return ret;
976}
977
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000978/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +0000979 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000980static inline int lpcr_rmls(unsigned long rma_size)
981{
982 switch (rma_size) {
983 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000984 if (cpu_has_feature(CPU_FTR_ARCH_206))
985 return 8; /* only supported on POWER7 */
986 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000987 case 64ul << 20: /* 64 MB */
988 return 3;
989 case 128ul << 20: /* 128 MB */
990 return 7;
991 case 256ul << 20: /* 256 MB */
992 return 4;
993 case 1ul << 30: /* 1 GB */
994 return 2;
995 case 16ul << 30: /* 16 GB */
996 return 1;
997 case 256ul << 30: /* 256 GB */
998 return 0;
999 default:
1000 return -1;
1001 }
1002}
1003
1004static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1005{
1006 struct kvmppc_rma_info *ri = vma->vm_file->private_data;
1007 struct page *page;
1008
1009 if (vmf->pgoff >= ri->npages)
1010 return VM_FAULT_SIGBUS;
1011
1012 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1013 get_page(page);
1014 vmf->page = page;
1015 return 0;
1016}
1017
1018static const struct vm_operations_struct kvm_rma_vm_ops = {
1019 .fault = kvm_rma_fault,
1020};
1021
1022static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1023{
1024 vma->vm_flags |= VM_RESERVED;
1025 vma->vm_ops = &kvm_rma_vm_ops;
1026 return 0;
1027}
1028
1029static int kvm_rma_release(struct inode *inode, struct file *filp)
1030{
1031 struct kvmppc_rma_info *ri = filp->private_data;
1032
1033 kvm_release_rma(ri);
1034 return 0;
1035}
1036
1037static struct file_operations kvm_rma_fops = {
1038 .mmap = kvm_rma_mmap,
1039 .release = kvm_rma_release,
1040};
1041
1042long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
1043{
1044 struct kvmppc_rma_info *ri;
1045 long fd;
1046
1047 ri = kvm_alloc_rma();
1048 if (!ri)
1049 return -ENOMEM;
1050
1051 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR);
1052 if (fd < 0)
1053 kvm_release_rma(ri);
1054
1055 ret->rma_size = ri->npages << PAGE_SHIFT;
1056 return fd;
1057}
1058
1059static struct page *hva_to_page(unsigned long addr)
1060{
1061 struct page *page[1];
1062 int npages;
1063
1064 might_sleep();
1065
1066 npages = get_user_pages_fast(addr, 1, 1, page);
1067
1068 if (unlikely(npages != 1))
1069 return 0;
1070
1071 return page[0];
1072}
1073
Paul Mackerrasde56a942011-06-29 00:21:34 +00001074int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1075 struct kvm_userspace_memory_region *mem)
1076{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001077 unsigned long psize, porder;
1078 unsigned long i, npages, totalpages;
1079 unsigned long pg_ix;
1080 struct kvmppc_pginfo *pginfo;
1081 unsigned long hva;
1082 struct kvmppc_rma_info *ri = NULL;
1083 struct page *page;
1084
1085 /* For now, only allow 16MB pages */
1086 porder = LARGE_PAGE_ORDER;
1087 psize = 1ul << porder;
1088 if ((mem->memory_size & (psize - 1)) ||
1089 (mem->guest_phys_addr & (psize - 1))) {
1090 pr_err("bad memory_size=%llx @ %llx\n",
1091 mem->memory_size, mem->guest_phys_addr);
1092 return -EINVAL;
1093 }
1094
1095 npages = mem->memory_size >> porder;
1096 totalpages = (mem->guest_phys_addr + mem->memory_size) >> porder;
1097
1098 /* More memory than we have space to track? */
1099 if (totalpages > (1ul << (MAX_MEM_ORDER - LARGE_PAGE_ORDER)))
1100 return -EINVAL;
1101
1102 /* Do we already have an RMA registered? */
1103 if (mem->guest_phys_addr == 0 && kvm->arch.rma)
1104 return -EINVAL;
1105
1106 if (totalpages > kvm->arch.ram_npages)
1107 kvm->arch.ram_npages = totalpages;
1108
1109 /* Is this one of our preallocated RMAs? */
1110 if (mem->guest_phys_addr == 0) {
1111 struct vm_area_struct *vma;
1112
1113 down_read(&current->mm->mmap_sem);
1114 vma = find_vma(current->mm, mem->userspace_addr);
1115 if (vma && vma->vm_file &&
1116 vma->vm_file->f_op == &kvm_rma_fops &&
1117 mem->userspace_addr == vma->vm_start)
1118 ri = vma->vm_file->private_data;
1119 up_read(&current->mm->mmap_sem);
Paul Mackerras9e368f22011-06-29 00:40:08 +00001120 if (!ri && cpu_has_feature(CPU_FTR_ARCH_201)) {
1121 pr_err("CPU requires an RMO\n");
1122 return -EINVAL;
1123 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001124 }
1125
1126 if (ri) {
1127 unsigned long rma_size;
1128 unsigned long lpcr;
1129 long rmls;
1130
1131 rma_size = ri->npages << PAGE_SHIFT;
1132 if (rma_size > mem->memory_size)
1133 rma_size = mem->memory_size;
1134 rmls = lpcr_rmls(rma_size);
1135 if (rmls < 0) {
1136 pr_err("Can't use RMA of 0x%lx bytes\n", rma_size);
1137 return -EINVAL;
1138 }
1139 atomic_inc(&ri->use_count);
1140 kvm->arch.rma = ri;
1141 kvm->arch.n_rma_pages = rma_size >> porder;
Paul Mackerras9e368f22011-06-29 00:40:08 +00001142
1143 /* Update LPCR and RMOR */
1144 lpcr = kvm->arch.lpcr;
1145 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1146 /* PPC970; insert RMLS value (split field) in HID4 */
1147 lpcr &= ~((1ul << HID4_RMLS0_SH) |
1148 (3ul << HID4_RMLS2_SH));
1149 lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
1150 ((rmls & 3) << HID4_RMLS2_SH);
1151 /* RMOR is also in HID4 */
1152 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
1153 << HID4_RMOR_SH;
1154 } else {
1155 /* POWER7 */
1156 lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
1157 lpcr |= rmls << LPCR_RMLS_SH;
1158 kvm->arch.rmor = kvm->arch.rma->base_pfn << PAGE_SHIFT;
1159 }
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001160 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001161 pr_info("Using RMO at %lx size %lx (LPCR = %lx)\n",
1162 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
1163 }
1164
1165 pg_ix = mem->guest_phys_addr >> porder;
1166 pginfo = kvm->arch.ram_pginfo + pg_ix;
1167 for (i = 0; i < npages; ++i, ++pg_ix) {
1168 if (ri && pg_ix < kvm->arch.n_rma_pages) {
1169 pginfo[i].pfn = ri->base_pfn +
1170 (pg_ix << (porder - PAGE_SHIFT));
1171 continue;
1172 }
1173 hva = mem->userspace_addr + (i << porder);
1174 page = hva_to_page(hva);
1175 if (!page) {
1176 pr_err("oops, no pfn for hva %lx\n", hva);
1177 goto err;
1178 }
1179 /* Check it's a 16MB page */
1180 if (!PageHead(page) ||
1181 compound_order(page) != (LARGE_PAGE_ORDER - PAGE_SHIFT)) {
1182 pr_err("page at %lx isn't 16MB (o=%d)\n",
1183 hva, compound_order(page));
1184 goto err;
1185 }
1186 pginfo[i].pfn = page_to_pfn(page);
1187 }
1188
Paul Mackerrasde56a942011-06-29 00:21:34 +00001189 return 0;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001190
1191 err:
1192 return -EINVAL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001193}
1194
1195void kvmppc_core_commit_memory_region(struct kvm *kvm,
1196 struct kvm_userspace_memory_region *mem)
1197{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001198 if (mem->guest_phys_addr == 0 && mem->memory_size != 0 &&
1199 !kvm->arch.rma)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001200 kvmppc_map_vrma(kvm, mem);
1201}
1202
1203int kvmppc_core_init_vm(struct kvm *kvm)
1204{
1205 long r;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001206 unsigned long npages = 1ul << (MAX_MEM_ORDER - LARGE_PAGE_ORDER);
1207 long err = -ENOMEM;
1208 unsigned long lpcr;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001209
1210 /* Allocate hashed page table */
1211 r = kvmppc_alloc_hpt(kvm);
David Gibson54738c02011-06-29 00:22:41 +00001212 if (r)
1213 return r;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001214
David Gibson54738c02011-06-29 00:22:41 +00001215 INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001216
1217 kvm->arch.ram_pginfo = kzalloc(npages * sizeof(struct kvmppc_pginfo),
1218 GFP_KERNEL);
1219 if (!kvm->arch.ram_pginfo) {
1220 pr_err("kvmppc_core_init_vm: couldn't alloc %lu bytes\n",
1221 npages * sizeof(struct kvmppc_pginfo));
1222 goto out_free;
1223 }
1224
1225 kvm->arch.ram_npages = 0;
1226 kvm->arch.ram_psize = 1ul << LARGE_PAGE_ORDER;
1227 kvm->arch.ram_porder = LARGE_PAGE_ORDER;
1228 kvm->arch.rma = NULL;
1229 kvm->arch.n_rma_pages = 0;
1230
Paul Mackerras9e368f22011-06-29 00:40:08 +00001231 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001232
Paul Mackerras9e368f22011-06-29 00:40:08 +00001233 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1234 /* PPC970; HID4 is effectively the LPCR */
1235 unsigned long lpid = kvm->arch.lpid;
1236 kvm->arch.host_lpid = 0;
1237 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
1238 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
1239 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
1240 ((lpid & 0xf) << HID4_LPID5_SH);
1241 } else {
1242 /* POWER7; init LPCR for virtual RMA mode */
1243 kvm->arch.host_lpid = mfspr(SPRN_LPID);
1244 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
1245 lpcr &= LPCR_PECE | LPCR_LPES;
1246 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
1247 LPCR_VPM0 | LPCR_VRMA_L;
1248 }
1249 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001250
David Gibson54738c02011-06-29 00:22:41 +00001251 return 0;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001252
1253 out_free:
1254 kvmppc_free_hpt(kvm);
1255 return err;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001256}
1257
1258void kvmppc_core_destroy_vm(struct kvm *kvm)
1259{
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001260 struct kvmppc_pginfo *pginfo;
1261 unsigned long i;
1262
1263 if (kvm->arch.ram_pginfo) {
1264 pginfo = kvm->arch.ram_pginfo;
1265 kvm->arch.ram_pginfo = NULL;
1266 for (i = kvm->arch.n_rma_pages; i < kvm->arch.ram_npages; ++i)
1267 if (pginfo[i].pfn)
1268 put_page(pfn_to_page(pginfo[i].pfn));
1269 kfree(pginfo);
1270 }
1271 if (kvm->arch.rma) {
1272 kvm_release_rma(kvm->arch.rma);
1273 kvm->arch.rma = NULL;
1274 }
1275
Paul Mackerrasde56a942011-06-29 00:21:34 +00001276 kvmppc_free_hpt(kvm);
David Gibson54738c02011-06-29 00:22:41 +00001277 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
Paul Mackerrasde56a942011-06-29 00:21:34 +00001278}
1279
1280/* These are stubs for now */
1281void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
1282{
1283}
1284
1285/* We don't need to emulate any privileged instructions or dcbz */
1286int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
1287 unsigned int inst, int *advance)
1288{
1289 return EMULATE_FAIL;
1290}
1291
1292int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
1293{
1294 return EMULATE_FAIL;
1295}
1296
1297int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
1298{
1299 return EMULATE_FAIL;
1300}
1301
1302static int kvmppc_book3s_hv_init(void)
1303{
1304 int r;
1305
1306 r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1307
1308 if (r)
1309 return r;
1310
1311 r = kvmppc_mmu_hv_init();
1312
1313 return r;
1314}
1315
1316static void kvmppc_book3s_hv_exit(void)
1317{
1318 kvm_exit();
1319}
1320
1321module_init(kvmppc_book3s_hv_init);
1322module_exit(kvmppc_book3s_hv_exit);