Lynx framebuffers multidomain implementation.
[linux/elbrus.git] / arch / powerpc / kvm / book3s_hv.c
blob0019281510da08583249c0bd604bdf66e08fec82
1 /*
2 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
3 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
5 * Authors:
6 * Paul Mackerras <paulus@au1.ibm.com>
7 * Alexander Graf <agraf@suse.de>
8 * Kevin Wolf <mail@kevin-wolf.de>
10 * Description: KVM functions specific to running on Book 3S
11 * processors in hypervisor mode (specifically POWER7 and later).
13 * This file is derived from arch/powerpc/kvm/book3s.c,
14 * by Alexander Graf <agraf@suse.de>.
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.
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>
27 #include <linux/export.h>
28 #include <linux/fs.h>
29 #include <linux/anon_inodes.h>
30 #include <linux/cpumask.h>
31 #include <linux/spinlock.h>
32 #include <linux/page-flags.h>
33 #include <linux/srcu.h>
34 #include <linux/miscdevice.h>
36 #include <asm/reg.h>
37 #include <asm/cputable.h>
38 #include <asm/cacheflush.h>
39 #include <asm/tlbflush.h>
40 #include <asm/uaccess.h>
41 #include <asm/io.h>
42 #include <asm/kvm_ppc.h>
43 #include <asm/kvm_book3s.h>
44 #include <asm/mmu_context.h>
45 #include <asm/lppaca.h>
46 #include <asm/processor.h>
47 #include <asm/cputhreads.h>
48 #include <asm/page.h>
49 #include <asm/hvcall.h>
50 #include <asm/switch_to.h>
51 #include <asm/smp.h>
52 #include <linux/gfp.h>
53 #include <linux/vmalloc.h>
54 #include <linux/highmem.h>
55 #include <linux/hugetlb.h>
56 #include <linux/module.h>
58 #include "book3s.h"
60 /* #define EXIT_DEBUG */
61 /* #define EXIT_DEBUG_SIMPLE */
62 /* #define EXIT_DEBUG_INT */
64 /* Used to indicate that a guest page fault needs to be handled */
65 #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
67 /* Used as a "null" value for timebase values */
68 #define TB_NIL (~(u64)0)
70 static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
71 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
73 static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
75 int me;
76 int cpu = vcpu->cpu;
77 struct swait_head *wqp;
79 wqp = kvm_arch_vcpu_wq(vcpu);
80 if (swaitqueue_active(wqp)) {
81 swait_wake_interruptible(wqp);
82 ++vcpu->stat.halt_wakeup;
85 me = get_cpu();
87 /* CPU points to the first thread of the core */
88 if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
89 #ifdef CONFIG_PPC_ICP_NATIVE
90 int real_cpu = cpu + vcpu->arch.ptid;
91 if (paca[real_cpu].kvm_hstate.xics_phys)
92 xics_wake_cpu(real_cpu);
93 else
94 #endif
95 if (cpu_online(cpu))
96 smp_send_reschedule(cpu);
98 put_cpu();
102 * We use the vcpu_load/put functions to measure stolen time.
103 * Stolen time is counted as time when either the vcpu is able to
104 * run as part of a virtual core, but the task running the vcore
105 * is preempted or sleeping, or when the vcpu needs something done
106 * in the kernel by the task running the vcpu, but that task is
107 * preempted or sleeping. Those two things have to be counted
108 * separately, since one of the vcpu tasks will take on the job
109 * of running the core, and the other vcpu tasks in the vcore will
110 * sleep waiting for it to do that, but that sleep shouldn't count
111 * as stolen time.
113 * Hence we accumulate stolen time when the vcpu can run as part of
114 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
115 * needs its task to do other things in the kernel (for example,
116 * service a page fault) in busy_stolen. We don't accumulate
117 * stolen time for a vcore when it is inactive, or for a vcpu
118 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
119 * a misnomer; it means that the vcpu task is not executing in
120 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
121 * the kernel. We don't have any way of dividing up that time
122 * between time that the vcpu is genuinely stopped, time that
123 * the task is actively working on behalf of the vcpu, and time
124 * that the task is preempted, so we don't count any of it as
125 * stolen.
127 * Updates to busy_stolen are protected by arch.tbacct_lock;
128 * updates to vc->stolen_tb are protected by the arch.tbacct_lock
129 * of the vcpu that has taken responsibility for running the vcore
130 * (i.e. vc->runner). The stolen times are measured in units of
131 * timebase ticks. (Note that the != TB_NIL checks below are
132 * purely defensive; they should never fail.)
135 static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
137 struct kvmppc_vcore *vc = vcpu->arch.vcore;
138 unsigned long flags;
140 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
141 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
142 vc->preempt_tb != TB_NIL) {
143 vc->stolen_tb += mftb() - vc->preempt_tb;
144 vc->preempt_tb = TB_NIL;
146 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
147 vcpu->arch.busy_preempt != TB_NIL) {
148 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
149 vcpu->arch.busy_preempt = TB_NIL;
151 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
154 static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
156 struct kvmppc_vcore *vc = vcpu->arch.vcore;
157 unsigned long flags;
159 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
160 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
161 vc->preempt_tb = mftb();
162 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
163 vcpu->arch.busy_preempt = mftb();
164 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
167 static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
169 vcpu->arch.shregs.msr = msr;
170 kvmppc_end_cede(vcpu);
173 void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
175 vcpu->arch.pvr = pvr;
178 int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
180 unsigned long pcr = 0;
181 struct kvmppc_vcore *vc = vcpu->arch.vcore;
183 if (arch_compat) {
184 if (!cpu_has_feature(CPU_FTR_ARCH_206))
185 return -EINVAL; /* 970 has no compat mode support */
187 switch (arch_compat) {
188 case PVR_ARCH_205:
190 * If an arch bit is set in PCR, all the defined
191 * higher-order arch bits also have to be set.
193 pcr = PCR_ARCH_206 | PCR_ARCH_205;
194 break;
195 case PVR_ARCH_206:
196 case PVR_ARCH_206p:
197 pcr = PCR_ARCH_206;
198 break;
199 case PVR_ARCH_207:
200 break;
201 default:
202 return -EINVAL;
205 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
206 /* POWER7 can't emulate POWER8 */
207 if (!(pcr & PCR_ARCH_206))
208 return -EINVAL;
209 pcr &= ~PCR_ARCH_206;
213 spin_lock(&vc->lock);
214 vc->arch_compat = arch_compat;
215 vc->pcr = pcr;
216 spin_unlock(&vc->lock);
218 return 0;
221 void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
223 int r;
225 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
226 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
227 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
228 for (r = 0; r < 16; ++r)
229 pr_err("r%2d = %.16lx r%d = %.16lx\n",
230 r, kvmppc_get_gpr(vcpu, r),
231 r+16, kvmppc_get_gpr(vcpu, r+16));
232 pr_err("ctr = %.16lx lr = %.16lx\n",
233 vcpu->arch.ctr, vcpu->arch.lr);
234 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
235 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
236 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
237 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
238 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
239 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
240 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
241 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
242 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
243 pr_err("fault dar = %.16lx dsisr = %.8x\n",
244 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
245 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
246 for (r = 0; r < vcpu->arch.slb_max; ++r)
247 pr_err(" ESID = %.16llx VSID = %.16llx\n",
248 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
249 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
250 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
251 vcpu->arch.last_inst);
254 struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
256 int r;
257 struct kvm_vcpu *v, *ret = NULL;
259 mutex_lock(&kvm->lock);
260 kvm_for_each_vcpu(r, v, kvm) {
261 if (v->vcpu_id == id) {
262 ret = v;
263 break;
266 mutex_unlock(&kvm->lock);
267 return ret;
270 static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
272 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
273 vpa->yield_count = 1;
276 static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
277 unsigned long addr, unsigned long len)
279 /* check address is cacheline aligned */
280 if (addr & (L1_CACHE_BYTES - 1))
281 return -EINVAL;
282 spin_lock(&vcpu->arch.vpa_update_lock);
283 if (v->next_gpa != addr || v->len != len) {
284 v->next_gpa = addr;
285 v->len = addr ? len : 0;
286 v->update_pending = 1;
288 spin_unlock(&vcpu->arch.vpa_update_lock);
289 return 0;
292 /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
293 struct reg_vpa {
294 u32 dummy;
295 union {
296 u16 hword;
297 u32 word;
298 } length;
301 static int vpa_is_registered(struct kvmppc_vpa *vpap)
303 if (vpap->update_pending)
304 return vpap->next_gpa != 0;
305 return vpap->pinned_addr != NULL;
308 static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
309 unsigned long flags,
310 unsigned long vcpuid, unsigned long vpa)
312 struct kvm *kvm = vcpu->kvm;
313 unsigned long len, nb;
314 void *va;
315 struct kvm_vcpu *tvcpu;
316 int err;
317 int subfunc;
318 struct kvmppc_vpa *vpap;
320 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
321 if (!tvcpu)
322 return H_PARAMETER;
324 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
325 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
326 subfunc == H_VPA_REG_SLB) {
327 /* Registering new area - address must be cache-line aligned */
328 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
329 return H_PARAMETER;
331 /* convert logical addr to kernel addr and read length */
332 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
333 if (va == NULL)
334 return H_PARAMETER;
335 if (subfunc == H_VPA_REG_VPA)
336 len = ((struct reg_vpa *)va)->length.hword;
337 else
338 len = ((struct reg_vpa *)va)->length.word;
339 kvmppc_unpin_guest_page(kvm, va, vpa, false);
341 /* Check length */
342 if (len > nb || len < sizeof(struct reg_vpa))
343 return H_PARAMETER;
344 } else {
345 vpa = 0;
346 len = 0;
349 err = H_PARAMETER;
350 vpap = NULL;
351 spin_lock(&tvcpu->arch.vpa_update_lock);
353 switch (subfunc) {
354 case H_VPA_REG_VPA: /* register VPA */
355 if (len < sizeof(struct lppaca))
356 break;
357 vpap = &tvcpu->arch.vpa;
358 err = 0;
359 break;
361 case H_VPA_REG_DTL: /* register DTL */
362 if (len < sizeof(struct dtl_entry))
363 break;
364 len -= len % sizeof(struct dtl_entry);
366 /* Check that they have previously registered a VPA */
367 err = H_RESOURCE;
368 if (!vpa_is_registered(&tvcpu->arch.vpa))
369 break;
371 vpap = &tvcpu->arch.dtl;
372 err = 0;
373 break;
375 case H_VPA_REG_SLB: /* register SLB shadow buffer */
376 /* Check that they have previously registered a VPA */
377 err = H_RESOURCE;
378 if (!vpa_is_registered(&tvcpu->arch.vpa))
379 break;
381 vpap = &tvcpu->arch.slb_shadow;
382 err = 0;
383 break;
385 case H_VPA_DEREG_VPA: /* deregister VPA */
386 /* Check they don't still have a DTL or SLB buf registered */
387 err = H_RESOURCE;
388 if (vpa_is_registered(&tvcpu->arch.dtl) ||
389 vpa_is_registered(&tvcpu->arch.slb_shadow))
390 break;
392 vpap = &tvcpu->arch.vpa;
393 err = 0;
394 break;
396 case H_VPA_DEREG_DTL: /* deregister DTL */
397 vpap = &tvcpu->arch.dtl;
398 err = 0;
399 break;
401 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
402 vpap = &tvcpu->arch.slb_shadow;
403 err = 0;
404 break;
407 if (vpap) {
408 vpap->next_gpa = vpa;
409 vpap->len = len;
410 vpap->update_pending = 1;
413 spin_unlock(&tvcpu->arch.vpa_update_lock);
415 return err;
418 static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
420 struct kvm *kvm = vcpu->kvm;
421 void *va;
422 unsigned long nb;
423 unsigned long gpa;
426 * We need to pin the page pointed to by vpap->next_gpa,
427 * but we can't call kvmppc_pin_guest_page under the lock
428 * as it does get_user_pages() and down_read(). So we
429 * have to drop the lock, pin the page, then get the lock
430 * again and check that a new area didn't get registered
431 * in the meantime.
433 for (;;) {
434 gpa = vpap->next_gpa;
435 spin_unlock(&vcpu->arch.vpa_update_lock);
436 va = NULL;
437 nb = 0;
438 if (gpa)
439 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
440 spin_lock(&vcpu->arch.vpa_update_lock);
441 if (gpa == vpap->next_gpa)
442 break;
443 /* sigh... unpin that one and try again */
444 if (va)
445 kvmppc_unpin_guest_page(kvm, va, gpa, false);
448 vpap->update_pending = 0;
449 if (va && nb < vpap->len) {
451 * If it's now too short, it must be that userspace
452 * has changed the mappings underlying guest memory,
453 * so unregister the region.
455 kvmppc_unpin_guest_page(kvm, va, gpa, false);
456 va = NULL;
458 if (vpap->pinned_addr)
459 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
460 vpap->dirty);
461 vpap->gpa = gpa;
462 vpap->pinned_addr = va;
463 vpap->dirty = false;
464 if (va)
465 vpap->pinned_end = va + vpap->len;
468 static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
470 if (!(vcpu->arch.vpa.update_pending ||
471 vcpu->arch.slb_shadow.update_pending ||
472 vcpu->arch.dtl.update_pending))
473 return;
475 spin_lock(&vcpu->arch.vpa_update_lock);
476 if (vcpu->arch.vpa.update_pending) {
477 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
478 if (vcpu->arch.vpa.pinned_addr)
479 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
481 if (vcpu->arch.dtl.update_pending) {
482 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
483 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
484 vcpu->arch.dtl_index = 0;
486 if (vcpu->arch.slb_shadow.update_pending)
487 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
488 spin_unlock(&vcpu->arch.vpa_update_lock);
492 * Return the accumulated stolen time for the vcore up until `now'.
493 * The caller should hold the vcore lock.
495 static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
497 u64 p;
500 * If we are the task running the vcore, then since we hold
501 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
502 * can't be updated, so we don't need the tbacct_lock.
503 * If the vcore is inactive, it can't become active (since we
504 * hold the vcore lock), so the vcpu load/put functions won't
505 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
507 if (vc->vcore_state != VCORE_INACTIVE &&
508 vc->runner->arch.run_task != current) {
509 spin_lock_irq(&vc->runner->arch.tbacct_lock);
510 p = vc->stolen_tb;
511 if (vc->preempt_tb != TB_NIL)
512 p += now - vc->preempt_tb;
513 spin_unlock_irq(&vc->runner->arch.tbacct_lock);
514 } else {
515 p = vc->stolen_tb;
517 return p;
520 static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
521 struct kvmppc_vcore *vc)
523 struct dtl_entry *dt;
524 struct lppaca *vpa;
525 unsigned long stolen;
526 unsigned long core_stolen;
527 u64 now;
529 dt = vcpu->arch.dtl_ptr;
530 vpa = vcpu->arch.vpa.pinned_addr;
531 now = mftb();
532 core_stolen = vcore_stolen_time(vc, now);
533 stolen = core_stolen - vcpu->arch.stolen_logged;
534 vcpu->arch.stolen_logged = core_stolen;
535 spin_lock_irq(&vcpu->arch.tbacct_lock);
536 stolen += vcpu->arch.busy_stolen;
537 vcpu->arch.busy_stolen = 0;
538 spin_unlock_irq(&vcpu->arch.tbacct_lock);
539 if (!dt || !vpa)
540 return;
541 memset(dt, 0, sizeof(struct dtl_entry));
542 dt->dispatch_reason = 7;
543 dt->processor_id = vc->pcpu + vcpu->arch.ptid;
544 dt->timebase = now + vc->tb_offset;
545 dt->enqueue_to_dispatch_time = stolen;
546 dt->srr0 = kvmppc_get_pc(vcpu);
547 dt->srr1 = vcpu->arch.shregs.msr;
548 ++dt;
549 if (dt == vcpu->arch.dtl.pinned_end)
550 dt = vcpu->arch.dtl.pinned_addr;
551 vcpu->arch.dtl_ptr = dt;
552 /* order writing *dt vs. writing vpa->dtl_idx */
553 smp_wmb();
554 vpa->dtl_idx = ++vcpu->arch.dtl_index;
555 vcpu->arch.dtl.dirty = true;
558 int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
560 unsigned long req = kvmppc_get_gpr(vcpu, 3);
561 unsigned long target, ret = H_SUCCESS;
562 struct kvm_vcpu *tvcpu;
563 int idx, rc;
565 switch (req) {
566 case H_ENTER:
567 idx = srcu_read_lock(&vcpu->kvm->srcu);
568 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
569 kvmppc_get_gpr(vcpu, 5),
570 kvmppc_get_gpr(vcpu, 6),
571 kvmppc_get_gpr(vcpu, 7));
572 srcu_read_unlock(&vcpu->kvm->srcu, idx);
573 break;
574 case H_CEDE:
575 break;
576 case H_PROD:
577 target = kvmppc_get_gpr(vcpu, 4);
578 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
579 if (!tvcpu) {
580 ret = H_PARAMETER;
581 break;
583 tvcpu->arch.prodded = 1;
584 smp_mb();
585 if (vcpu->arch.ceded) {
586 if (swaitqueue_active(&vcpu->wq)) {
587 swait_wake_interruptible(&vcpu->wq);
588 vcpu->stat.halt_wakeup++;
591 break;
592 case H_CONFER:
593 target = kvmppc_get_gpr(vcpu, 4);
594 if (target == -1)
595 break;
596 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
597 if (!tvcpu) {
598 ret = H_PARAMETER;
599 break;
601 kvm_vcpu_yield_to(tvcpu);
602 break;
603 case H_REGISTER_VPA:
604 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
605 kvmppc_get_gpr(vcpu, 5),
606 kvmppc_get_gpr(vcpu, 6));
607 break;
608 case H_RTAS:
609 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
610 return RESUME_HOST;
612 idx = srcu_read_lock(&vcpu->kvm->srcu);
613 rc = kvmppc_rtas_hcall(vcpu);
614 srcu_read_unlock(&vcpu->kvm->srcu, idx);
616 if (rc == -ENOENT)
617 return RESUME_HOST;
618 else if (rc == 0)
619 break;
621 /* Send the error out to userspace via KVM_RUN */
622 return rc;
624 case H_XIRR:
625 case H_CPPR:
626 case H_EOI:
627 case H_IPI:
628 case H_IPOLL:
629 case H_XIRR_X:
630 if (kvmppc_xics_enabled(vcpu)) {
631 ret = kvmppc_xics_hcall(vcpu, req);
632 break;
633 } /* fallthrough */
634 default:
635 return RESUME_HOST;
637 kvmppc_set_gpr(vcpu, 3, ret);
638 vcpu->arch.hcall_needed = 0;
639 return RESUME_GUEST;
642 static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
643 struct task_struct *tsk)
645 int r = RESUME_HOST;
647 vcpu->stat.sum_exits++;
649 run->exit_reason = KVM_EXIT_UNKNOWN;
650 run->ready_for_interrupt_injection = 1;
651 switch (vcpu->arch.trap) {
652 /* We're good on these - the host merely wanted to get our attention */
653 case BOOK3S_INTERRUPT_HV_DECREMENTER:
654 vcpu->stat.dec_exits++;
655 r = RESUME_GUEST;
656 break;
657 case BOOK3S_INTERRUPT_EXTERNAL:
658 case BOOK3S_INTERRUPT_H_DOORBELL:
659 vcpu->stat.ext_intr_exits++;
660 r = RESUME_GUEST;
661 break;
662 case BOOK3S_INTERRUPT_PERFMON:
663 r = RESUME_GUEST;
664 break;
665 case BOOK3S_INTERRUPT_MACHINE_CHECK:
667 * Deliver a machine check interrupt to the guest.
668 * We have to do this, even if the host has handled the
669 * machine check, because machine checks use SRR0/1 and
670 * the interrupt might have trashed guest state in them.
672 kvmppc_book3s_queue_irqprio(vcpu,
673 BOOK3S_INTERRUPT_MACHINE_CHECK);
674 r = RESUME_GUEST;
675 break;
676 case BOOK3S_INTERRUPT_PROGRAM:
678 ulong flags;
680 * Normally program interrupts are delivered directly
681 * to the guest by the hardware, but we can get here
682 * as a result of a hypervisor emulation interrupt
683 * (e40) getting turned into a 700 by BML RTAS.
685 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
686 kvmppc_core_queue_program(vcpu, flags);
687 r = RESUME_GUEST;
688 break;
690 case BOOK3S_INTERRUPT_SYSCALL:
692 /* hcall - punt to userspace */
693 int i;
695 /* hypercall with MSR_PR has already been handled in rmode,
696 * and never reaches here.
699 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
700 for (i = 0; i < 9; ++i)
701 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
702 run->exit_reason = KVM_EXIT_PAPR_HCALL;
703 vcpu->arch.hcall_needed = 1;
704 r = RESUME_HOST;
705 break;
708 * We get these next two if the guest accesses a page which it thinks
709 * it has mapped but which is not actually present, either because
710 * it is for an emulated I/O device or because the corresonding
711 * host page has been paged out. Any other HDSI/HISI interrupts
712 * have been handled already.
714 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
715 r = RESUME_PAGE_FAULT;
716 break;
717 case BOOK3S_INTERRUPT_H_INST_STORAGE:
718 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
719 vcpu->arch.fault_dsisr = 0;
720 r = RESUME_PAGE_FAULT;
721 break;
723 * This occurs if the guest executes an illegal instruction.
724 * We just generate a program interrupt to the guest, since
725 * we don't emulate any guest instructions at this stage.
727 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
728 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
729 r = RESUME_GUEST;
730 break;
732 * This occurs if the guest (kernel or userspace), does something that
733 * is prohibited by HFSCR. We just generate a program interrupt to
734 * the guest.
736 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
737 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
738 r = RESUME_GUEST;
739 break;
740 default:
741 kvmppc_dump_regs(vcpu);
742 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
743 vcpu->arch.trap, kvmppc_get_pc(vcpu),
744 vcpu->arch.shregs.msr);
745 run->hw.hardware_exit_reason = vcpu->arch.trap;
746 r = RESUME_HOST;
747 break;
750 return r;
753 static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
754 struct kvm_sregs *sregs)
756 int i;
758 memset(sregs, 0, sizeof(struct kvm_sregs));
759 sregs->pvr = vcpu->arch.pvr;
760 for (i = 0; i < vcpu->arch.slb_max; i++) {
761 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
762 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
765 return 0;
768 static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
769 struct kvm_sregs *sregs)
771 int i, j;
773 kvmppc_set_pvr_hv(vcpu, sregs->pvr);
775 j = 0;
776 for (i = 0; i < vcpu->arch.slb_nr; i++) {
777 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
778 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
779 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
780 ++j;
783 vcpu->arch.slb_max = j;
785 return 0;
788 static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr)
790 struct kvmppc_vcore *vc = vcpu->arch.vcore;
791 u64 mask;
793 spin_lock(&vc->lock);
795 * If ILE (interrupt little-endian) has changed, update the
796 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
798 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
799 struct kvm *kvm = vcpu->kvm;
800 struct kvm_vcpu *vcpu;
801 int i;
803 mutex_lock(&kvm->lock);
804 kvm_for_each_vcpu(i, vcpu, kvm) {
805 if (vcpu->arch.vcore != vc)
806 continue;
807 if (new_lpcr & LPCR_ILE)
808 vcpu->arch.intr_msr |= MSR_LE;
809 else
810 vcpu->arch.intr_msr &= ~MSR_LE;
812 mutex_unlock(&kvm->lock);
816 * Userspace can only modify DPFD (default prefetch depth),
817 * ILE (interrupt little-endian) and TC (translation control).
818 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
820 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
821 if (cpu_has_feature(CPU_FTR_ARCH_207S))
822 mask |= LPCR_AIL;
823 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
824 spin_unlock(&vc->lock);
827 static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
828 union kvmppc_one_reg *val)
830 int r = 0;
831 long int i;
833 switch (id) {
834 case KVM_REG_PPC_HIOR:
835 *val = get_reg_val(id, 0);
836 break;
837 case KVM_REG_PPC_DABR:
838 *val = get_reg_val(id, vcpu->arch.dabr);
839 break;
840 case KVM_REG_PPC_DABRX:
841 *val = get_reg_val(id, vcpu->arch.dabrx);
842 break;
843 case KVM_REG_PPC_DSCR:
844 *val = get_reg_val(id, vcpu->arch.dscr);
845 break;
846 case KVM_REG_PPC_PURR:
847 *val = get_reg_val(id, vcpu->arch.purr);
848 break;
849 case KVM_REG_PPC_SPURR:
850 *val = get_reg_val(id, vcpu->arch.spurr);
851 break;
852 case KVM_REG_PPC_AMR:
853 *val = get_reg_val(id, vcpu->arch.amr);
854 break;
855 case KVM_REG_PPC_UAMOR:
856 *val = get_reg_val(id, vcpu->arch.uamor);
857 break;
858 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
859 i = id - KVM_REG_PPC_MMCR0;
860 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
861 break;
862 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
863 i = id - KVM_REG_PPC_PMC1;
864 *val = get_reg_val(id, vcpu->arch.pmc[i]);
865 break;
866 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
867 i = id - KVM_REG_PPC_SPMC1;
868 *val = get_reg_val(id, vcpu->arch.spmc[i]);
869 break;
870 case KVM_REG_PPC_SIAR:
871 *val = get_reg_val(id, vcpu->arch.siar);
872 break;
873 case KVM_REG_PPC_SDAR:
874 *val = get_reg_val(id, vcpu->arch.sdar);
875 break;
876 case KVM_REG_PPC_SIER:
877 *val = get_reg_val(id, vcpu->arch.sier);
878 break;
879 case KVM_REG_PPC_IAMR:
880 *val = get_reg_val(id, vcpu->arch.iamr);
881 break;
882 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
883 case KVM_REG_PPC_TFHAR:
884 *val = get_reg_val(id, vcpu->arch.tfhar);
885 break;
886 case KVM_REG_PPC_TFIAR:
887 *val = get_reg_val(id, vcpu->arch.tfiar);
888 break;
889 case KVM_REG_PPC_TEXASR:
890 *val = get_reg_val(id, vcpu->arch.texasr);
891 break;
892 #endif
893 case KVM_REG_PPC_FSCR:
894 *val = get_reg_val(id, vcpu->arch.fscr);
895 break;
896 case KVM_REG_PPC_PSPB:
897 *val = get_reg_val(id, vcpu->arch.pspb);
898 break;
899 case KVM_REG_PPC_EBBHR:
900 *val = get_reg_val(id, vcpu->arch.ebbhr);
901 break;
902 case KVM_REG_PPC_EBBRR:
903 *val = get_reg_val(id, vcpu->arch.ebbrr);
904 break;
905 case KVM_REG_PPC_BESCR:
906 *val = get_reg_val(id, vcpu->arch.bescr);
907 break;
908 case KVM_REG_PPC_TAR:
909 *val = get_reg_val(id, vcpu->arch.tar);
910 break;
911 case KVM_REG_PPC_DPDES:
912 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
913 break;
914 case KVM_REG_PPC_DAWR:
915 *val = get_reg_val(id, vcpu->arch.dawr);
916 break;
917 case KVM_REG_PPC_DAWRX:
918 *val = get_reg_val(id, vcpu->arch.dawrx);
919 break;
920 case KVM_REG_PPC_CIABR:
921 *val = get_reg_val(id, vcpu->arch.ciabr);
922 break;
923 case KVM_REG_PPC_IC:
924 *val = get_reg_val(id, vcpu->arch.ic);
925 break;
926 case KVM_REG_PPC_VTB:
927 *val = get_reg_val(id, vcpu->arch.vtb);
928 break;
929 case KVM_REG_PPC_CSIGR:
930 *val = get_reg_val(id, vcpu->arch.csigr);
931 break;
932 case KVM_REG_PPC_TACR:
933 *val = get_reg_val(id, vcpu->arch.tacr);
934 break;
935 case KVM_REG_PPC_TCSCR:
936 *val = get_reg_val(id, vcpu->arch.tcscr);
937 break;
938 case KVM_REG_PPC_PID:
939 *val = get_reg_val(id, vcpu->arch.pid);
940 break;
941 case KVM_REG_PPC_ACOP:
942 *val = get_reg_val(id, vcpu->arch.acop);
943 break;
944 case KVM_REG_PPC_WORT:
945 *val = get_reg_val(id, vcpu->arch.wort);
946 break;
947 case KVM_REG_PPC_VPA_ADDR:
948 spin_lock(&vcpu->arch.vpa_update_lock);
949 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
950 spin_unlock(&vcpu->arch.vpa_update_lock);
951 break;
952 case KVM_REG_PPC_VPA_SLB:
953 spin_lock(&vcpu->arch.vpa_update_lock);
954 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
955 val->vpaval.length = vcpu->arch.slb_shadow.len;
956 spin_unlock(&vcpu->arch.vpa_update_lock);
957 break;
958 case KVM_REG_PPC_VPA_DTL:
959 spin_lock(&vcpu->arch.vpa_update_lock);
960 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
961 val->vpaval.length = vcpu->arch.dtl.len;
962 spin_unlock(&vcpu->arch.vpa_update_lock);
963 break;
964 case KVM_REG_PPC_TB_OFFSET:
965 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
966 break;
967 case KVM_REG_PPC_LPCR:
968 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
969 break;
970 case KVM_REG_PPC_PPR:
971 *val = get_reg_val(id, vcpu->arch.ppr);
972 break;
973 case KVM_REG_PPC_ARCH_COMPAT:
974 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
975 break;
976 default:
977 r = -EINVAL;
978 break;
981 return r;
984 static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
985 union kvmppc_one_reg *val)
987 int r = 0;
988 long int i;
989 unsigned long addr, len;
991 switch (id) {
992 case KVM_REG_PPC_HIOR:
993 /* Only allow this to be set to zero */
994 if (set_reg_val(id, *val))
995 r = -EINVAL;
996 break;
997 case KVM_REG_PPC_DABR:
998 vcpu->arch.dabr = set_reg_val(id, *val);
999 break;
1000 case KVM_REG_PPC_DABRX:
1001 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1002 break;
1003 case KVM_REG_PPC_DSCR:
1004 vcpu->arch.dscr = set_reg_val(id, *val);
1005 break;
1006 case KVM_REG_PPC_PURR:
1007 vcpu->arch.purr = set_reg_val(id, *val);
1008 break;
1009 case KVM_REG_PPC_SPURR:
1010 vcpu->arch.spurr = set_reg_val(id, *val);
1011 break;
1012 case KVM_REG_PPC_AMR:
1013 vcpu->arch.amr = set_reg_val(id, *val);
1014 break;
1015 case KVM_REG_PPC_UAMOR:
1016 vcpu->arch.uamor = set_reg_val(id, *val);
1017 break;
1018 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
1019 i = id - KVM_REG_PPC_MMCR0;
1020 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1021 break;
1022 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1023 i = id - KVM_REG_PPC_PMC1;
1024 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1025 break;
1026 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1027 i = id - KVM_REG_PPC_SPMC1;
1028 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1029 break;
1030 case KVM_REG_PPC_SIAR:
1031 vcpu->arch.siar = set_reg_val(id, *val);
1032 break;
1033 case KVM_REG_PPC_SDAR:
1034 vcpu->arch.sdar = set_reg_val(id, *val);
1035 break;
1036 case KVM_REG_PPC_SIER:
1037 vcpu->arch.sier = set_reg_val(id, *val);
1038 break;
1039 case KVM_REG_PPC_IAMR:
1040 vcpu->arch.iamr = set_reg_val(id, *val);
1041 break;
1042 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1043 case KVM_REG_PPC_TFHAR:
1044 vcpu->arch.tfhar = set_reg_val(id, *val);
1045 break;
1046 case KVM_REG_PPC_TFIAR:
1047 vcpu->arch.tfiar = set_reg_val(id, *val);
1048 break;
1049 case KVM_REG_PPC_TEXASR:
1050 vcpu->arch.texasr = set_reg_val(id, *val);
1051 break;
1052 #endif
1053 case KVM_REG_PPC_FSCR:
1054 vcpu->arch.fscr = set_reg_val(id, *val);
1055 break;
1056 case KVM_REG_PPC_PSPB:
1057 vcpu->arch.pspb = set_reg_val(id, *val);
1058 break;
1059 case KVM_REG_PPC_EBBHR:
1060 vcpu->arch.ebbhr = set_reg_val(id, *val);
1061 break;
1062 case KVM_REG_PPC_EBBRR:
1063 vcpu->arch.ebbrr = set_reg_val(id, *val);
1064 break;
1065 case KVM_REG_PPC_BESCR:
1066 vcpu->arch.bescr = set_reg_val(id, *val);
1067 break;
1068 case KVM_REG_PPC_TAR:
1069 vcpu->arch.tar = set_reg_val(id, *val);
1070 break;
1071 case KVM_REG_PPC_DPDES:
1072 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1073 break;
1074 case KVM_REG_PPC_DAWR:
1075 vcpu->arch.dawr = set_reg_val(id, *val);
1076 break;
1077 case KVM_REG_PPC_DAWRX:
1078 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1079 break;
1080 case KVM_REG_PPC_CIABR:
1081 vcpu->arch.ciabr = set_reg_val(id, *val);
1082 /* Don't allow setting breakpoints in hypervisor code */
1083 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1084 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1085 break;
1086 case KVM_REG_PPC_IC:
1087 vcpu->arch.ic = set_reg_val(id, *val);
1088 break;
1089 case KVM_REG_PPC_VTB:
1090 vcpu->arch.vtb = set_reg_val(id, *val);
1091 break;
1092 case KVM_REG_PPC_CSIGR:
1093 vcpu->arch.csigr = set_reg_val(id, *val);
1094 break;
1095 case KVM_REG_PPC_TACR:
1096 vcpu->arch.tacr = set_reg_val(id, *val);
1097 break;
1098 case KVM_REG_PPC_TCSCR:
1099 vcpu->arch.tcscr = set_reg_val(id, *val);
1100 break;
1101 case KVM_REG_PPC_PID:
1102 vcpu->arch.pid = set_reg_val(id, *val);
1103 break;
1104 case KVM_REG_PPC_ACOP:
1105 vcpu->arch.acop = set_reg_val(id, *val);
1106 break;
1107 case KVM_REG_PPC_WORT:
1108 vcpu->arch.wort = set_reg_val(id, *val);
1109 break;
1110 case KVM_REG_PPC_VPA_ADDR:
1111 addr = set_reg_val(id, *val);
1112 r = -EINVAL;
1113 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1114 vcpu->arch.dtl.next_gpa))
1115 break;
1116 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1117 break;
1118 case KVM_REG_PPC_VPA_SLB:
1119 addr = val->vpaval.addr;
1120 len = val->vpaval.length;
1121 r = -EINVAL;
1122 if (addr && !vcpu->arch.vpa.next_gpa)
1123 break;
1124 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1125 break;
1126 case KVM_REG_PPC_VPA_DTL:
1127 addr = val->vpaval.addr;
1128 len = val->vpaval.length;
1129 r = -EINVAL;
1130 if (addr && (len < sizeof(struct dtl_entry) ||
1131 !vcpu->arch.vpa.next_gpa))
1132 break;
1133 len -= len % sizeof(struct dtl_entry);
1134 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1135 break;
1136 case KVM_REG_PPC_TB_OFFSET:
1137 /* round up to multiple of 2^24 */
1138 vcpu->arch.vcore->tb_offset =
1139 ALIGN(set_reg_val(id, *val), 1UL << 24);
1140 break;
1141 case KVM_REG_PPC_LPCR:
1142 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val));
1143 break;
1144 case KVM_REG_PPC_PPR:
1145 vcpu->arch.ppr = set_reg_val(id, *val);
1146 break;
1147 case KVM_REG_PPC_ARCH_COMPAT:
1148 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1149 break;
1150 default:
1151 r = -EINVAL;
1152 break;
1155 return r;
1158 static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1159 unsigned int id)
1161 struct kvm_vcpu *vcpu;
1162 int err = -EINVAL;
1163 int core;
1164 struct kvmppc_vcore *vcore;
1166 core = id / threads_per_core;
1167 if (core >= KVM_MAX_VCORES)
1168 goto out;
1170 err = -ENOMEM;
1171 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
1172 if (!vcpu)
1173 goto out;
1175 err = kvm_vcpu_init(vcpu, kvm, id);
1176 if (err)
1177 goto free_vcpu;
1179 vcpu->arch.shared = &vcpu->arch.shregs;
1180 vcpu->arch.mmcr[0] = MMCR0_FC;
1181 vcpu->arch.ctrl = CTRL_RUNLATCH;
1182 /* default to host PVR, since we can't spoof it */
1183 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
1184 spin_lock_init(&vcpu->arch.vpa_update_lock);
1185 spin_lock_init(&vcpu->arch.tbacct_lock);
1186 vcpu->arch.busy_preempt = TB_NIL;
1187 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
1189 kvmppc_mmu_book3s_hv_init(vcpu);
1191 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
1193 init_waitqueue_head(&vcpu->arch.cpu_run);
1195 mutex_lock(&kvm->lock);
1196 vcore = kvm->arch.vcores[core];
1197 if (!vcore) {
1198 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1199 if (vcore) {
1200 INIT_LIST_HEAD(&vcore->runnable_threads);
1201 spin_lock_init(&vcore->lock);
1202 init_swait_head(&vcore->wq);
1203 vcore->preempt_tb = TB_NIL;
1204 vcore->lpcr = kvm->arch.lpcr;
1205 vcore->first_vcpuid = core * threads_per_core;
1206 vcore->kvm = kvm;
1208 kvm->arch.vcores[core] = vcore;
1209 kvm->arch.online_vcores++;
1211 mutex_unlock(&kvm->lock);
1213 if (!vcore)
1214 goto free_vcpu;
1216 spin_lock(&vcore->lock);
1217 ++vcore->num_threads;
1218 spin_unlock(&vcore->lock);
1219 vcpu->arch.vcore = vcore;
1220 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
1222 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1223 kvmppc_sanity_check(vcpu);
1225 return vcpu;
1227 free_vcpu:
1228 kmem_cache_free(kvm_vcpu_cache, vcpu);
1229 out:
1230 return ERR_PTR(err);
1233 static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1235 if (vpa->pinned_addr)
1236 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1237 vpa->dirty);
1240 static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
1242 spin_lock(&vcpu->arch.vpa_update_lock);
1243 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1244 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1245 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
1246 spin_unlock(&vcpu->arch.vpa_update_lock);
1247 kvm_vcpu_uninit(vcpu);
1248 kmem_cache_free(kvm_vcpu_cache, vcpu);
1251 static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1253 /* Indicate we want to get back into the guest */
1254 return 1;
1257 static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
1259 unsigned long dec_nsec, now;
1261 now = get_tb();
1262 if (now > vcpu->arch.dec_expires) {
1263 /* decrementer has already gone negative */
1264 kvmppc_core_queue_dec(vcpu);
1265 kvmppc_core_prepare_to_enter(vcpu);
1266 return;
1268 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1269 / tb_ticks_per_sec;
1270 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1271 HRTIMER_MODE_REL);
1272 vcpu->arch.timer_running = 1;
1275 static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
1277 vcpu->arch.ceded = 0;
1278 if (vcpu->arch.timer_running) {
1279 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1280 vcpu->arch.timer_running = 0;
1284 extern void __kvmppc_vcore_entry(void);
1286 static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1287 struct kvm_vcpu *vcpu)
1289 u64 now;
1291 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1292 return;
1293 spin_lock_irq(&vcpu->arch.tbacct_lock);
1294 now = mftb();
1295 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1296 vcpu->arch.stolen_logged;
1297 vcpu->arch.busy_preempt = now;
1298 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1299 spin_unlock_irq(&vcpu->arch.tbacct_lock);
1300 --vc->n_runnable;
1301 list_del(&vcpu->arch.run_list);
1304 static int kvmppc_grab_hwthread(int cpu)
1306 struct paca_struct *tpaca;
1307 long timeout = 1000;
1309 tpaca = &paca[cpu];
1311 /* Ensure the thread won't go into the kernel if it wakes */
1312 tpaca->kvm_hstate.hwthread_req = 1;
1313 tpaca->kvm_hstate.kvm_vcpu = NULL;
1316 * If the thread is already executing in the kernel (e.g. handling
1317 * a stray interrupt), wait for it to get back to nap mode.
1318 * The smp_mb() is to ensure that our setting of hwthread_req
1319 * is visible before we look at hwthread_state, so if this
1320 * races with the code at system_reset_pSeries and the thread
1321 * misses our setting of hwthread_req, we are sure to see its
1322 * setting of hwthread_state, and vice versa.
1324 smp_mb();
1325 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1326 if (--timeout <= 0) {
1327 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1328 return -EBUSY;
1330 udelay(1);
1332 return 0;
1335 static void kvmppc_release_hwthread(int cpu)
1337 struct paca_struct *tpaca;
1339 tpaca = &paca[cpu];
1340 tpaca->kvm_hstate.hwthread_req = 0;
1341 tpaca->kvm_hstate.kvm_vcpu = NULL;
1344 static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1346 int cpu;
1347 struct paca_struct *tpaca;
1348 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1350 if (vcpu->arch.timer_running) {
1351 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1352 vcpu->arch.timer_running = 0;
1354 cpu = vc->pcpu + vcpu->arch.ptid;
1355 tpaca = &paca[cpu];
1356 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1357 tpaca->kvm_hstate.kvm_vcore = vc;
1358 tpaca->kvm_hstate.ptid = vcpu->arch.ptid;
1359 vcpu->cpu = vc->pcpu;
1360 smp_wmb();
1361 #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
1362 if (cpu != smp_processor_id()) {
1363 xics_wake_cpu(cpu);
1364 if (vcpu->arch.ptid)
1365 ++vc->n_woken;
1367 #endif
1370 static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1372 int i;
1374 HMT_low();
1375 i = 0;
1376 while (vc->nap_count < vc->n_woken) {
1377 if (++i >= 1000000) {
1378 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1379 vc->nap_count, vc->n_woken);
1380 break;
1382 cpu_relax();
1384 HMT_medium();
1388 * Check that we are on thread 0 and that any other threads in
1389 * this core are off-line. Then grab the threads so they can't
1390 * enter the kernel.
1392 static int on_primary_thread(void)
1394 int cpu = smp_processor_id();
1395 int thr = cpu_thread_in_core(cpu);
1397 if (thr)
1398 return 0;
1399 while (++thr < threads_per_core)
1400 if (cpu_online(cpu + thr))
1401 return 0;
1403 /* Grab all hw threads so they can't go into the kernel */
1404 for (thr = 1; thr < threads_per_core; ++thr) {
1405 if (kvmppc_grab_hwthread(cpu + thr)) {
1406 /* Couldn't grab one; let the others go */
1407 do {
1408 kvmppc_release_hwthread(cpu + thr);
1409 } while (--thr > 0);
1410 return 0;
1413 return 1;
1417 * Run a set of guest threads on a physical core.
1418 * Called with vc->lock held.
1420 static void kvmppc_run_core(struct kvmppc_vcore *vc)
1422 struct kvm_vcpu *vcpu, *vnext;
1423 long ret;
1424 u64 now;
1425 int i, need_vpa_update;
1426 int srcu_idx;
1427 struct kvm_vcpu *vcpus_to_update[threads_per_core];
1429 /* don't start if any threads have a signal pending */
1430 need_vpa_update = 0;
1431 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1432 if (signal_pending(vcpu->arch.run_task))
1433 return;
1434 if (vcpu->arch.vpa.update_pending ||
1435 vcpu->arch.slb_shadow.update_pending ||
1436 vcpu->arch.dtl.update_pending)
1437 vcpus_to_update[need_vpa_update++] = vcpu;
1441 * Initialize *vc, in particular vc->vcore_state, so we can
1442 * drop the vcore lock if necessary.
1444 vc->n_woken = 0;
1445 vc->nap_count = 0;
1446 vc->entry_exit_count = 0;
1447 vc->vcore_state = VCORE_STARTING;
1448 vc->in_guest = 0;
1449 vc->napping_threads = 0;
1452 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1453 * which can't be called with any spinlocks held.
1455 if (need_vpa_update) {
1456 spin_unlock(&vc->lock);
1457 for (i = 0; i < need_vpa_update; ++i)
1458 kvmppc_update_vpas(vcpus_to_update[i]);
1459 spin_lock(&vc->lock);
1463 * Make sure we are running on thread 0, and that
1464 * secondary threads are offline.
1466 if (threads_per_core > 1 && !on_primary_thread()) {
1467 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1468 vcpu->arch.ret = -EBUSY;
1469 goto out;
1472 vc->pcpu = smp_processor_id();
1473 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1474 kvmppc_start_thread(vcpu);
1475 kvmppc_create_dtl_entry(vcpu, vc);
1478 /* Set this explicitly in case thread 0 doesn't have a vcpu */
1479 get_paca()->kvm_hstate.kvm_vcore = vc;
1480 get_paca()->kvm_hstate.ptid = 0;
1482 vc->vcore_state = VCORE_RUNNING;
1483 preempt_disable();
1484 spin_unlock(&vc->lock);
1486 kvm_guest_enter();
1488 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
1490 __kvmppc_vcore_entry();
1492 spin_lock(&vc->lock);
1493 /* disable sending of IPIs on virtual external irqs */
1494 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1495 vcpu->cpu = -1;
1496 /* wait for secondary threads to finish writing their state to memory */
1497 if (vc->nap_count < vc->n_woken)
1498 kvmppc_wait_for_nap(vc);
1499 for (i = 0; i < threads_per_core; ++i)
1500 kvmppc_release_hwthread(vc->pcpu + i);
1501 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
1502 vc->vcore_state = VCORE_EXITING;
1503 spin_unlock(&vc->lock);
1505 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
1507 /* make sure updates to secondary vcpu structs are visible now */
1508 smp_mb();
1509 kvm_guest_exit();
1511 preempt_enable();
1512 cond_resched();
1514 spin_lock(&vc->lock);
1515 now = get_tb();
1516 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1517 /* cancel pending dec exception if dec is positive */
1518 if (now < vcpu->arch.dec_expires &&
1519 kvmppc_core_pending_dec(vcpu))
1520 kvmppc_core_dequeue_dec(vcpu);
1522 ret = RESUME_GUEST;
1523 if (vcpu->arch.trap)
1524 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1525 vcpu->arch.run_task);
1527 vcpu->arch.ret = ret;
1528 vcpu->arch.trap = 0;
1530 if (vcpu->arch.ceded) {
1531 if (ret != RESUME_GUEST)
1532 kvmppc_end_cede(vcpu);
1533 else
1534 kvmppc_set_timer(vcpu);
1538 out:
1539 vc->vcore_state = VCORE_INACTIVE;
1540 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1541 arch.run_list) {
1542 if (vcpu->arch.ret != RESUME_GUEST) {
1543 kvmppc_remove_runnable(vc, vcpu);
1544 wake_up(&vcpu->arch.cpu_run);
1550 * Wait for some other vcpu thread to execute us, and
1551 * wake us up when we need to handle something in the host.
1553 static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
1555 DEFINE_WAIT(wait);
1557 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1558 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1559 schedule();
1560 finish_wait(&vcpu->arch.cpu_run, &wait);
1564 * All the vcpus in this vcore are idle, so wait for a decrementer
1565 * or external interrupt to one of the vcpus. vc->lock is held.
1567 static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1569 DEFINE_SWAITER(wait);
1571 swait_prepare(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1572 vc->vcore_state = VCORE_SLEEPING;
1573 spin_unlock(&vc->lock);
1574 schedule();
1575 swait_finish(&vc->wq, &wait);
1576 spin_lock(&vc->lock);
1577 vc->vcore_state = VCORE_INACTIVE;
1580 static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1582 int n_ceded;
1583 struct kvmppc_vcore *vc;
1584 struct kvm_vcpu *v, *vn;
1586 kvm_run->exit_reason = 0;
1587 vcpu->arch.ret = RESUME_GUEST;
1588 vcpu->arch.trap = 0;
1589 kvmppc_update_vpas(vcpu);
1592 * Synchronize with other threads in this virtual core
1594 vc = vcpu->arch.vcore;
1595 spin_lock(&vc->lock);
1596 vcpu->arch.ceded = 0;
1597 vcpu->arch.run_task = current;
1598 vcpu->arch.kvm_run = kvm_run;
1599 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
1600 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
1601 vcpu->arch.busy_preempt = TB_NIL;
1602 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1603 ++vc->n_runnable;
1606 * This happens the first time this is called for a vcpu.
1607 * If the vcore is already running, we may be able to start
1608 * this thread straight away and have it join in.
1610 if (!signal_pending(current)) {
1611 if (vc->vcore_state == VCORE_RUNNING &&
1612 VCORE_EXIT_COUNT(vc) == 0) {
1613 kvmppc_create_dtl_entry(vcpu, vc);
1614 kvmppc_start_thread(vcpu);
1615 } else if (vc->vcore_state == VCORE_SLEEPING) {
1616 swait_wake(&vc->wq);
1621 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1622 !signal_pending(current)) {
1623 if (vc->vcore_state != VCORE_INACTIVE) {
1624 spin_unlock(&vc->lock);
1625 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1626 spin_lock(&vc->lock);
1627 continue;
1629 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1630 arch.run_list) {
1631 kvmppc_core_prepare_to_enter(v);
1632 if (signal_pending(v->arch.run_task)) {
1633 kvmppc_remove_runnable(vc, v);
1634 v->stat.signal_exits++;
1635 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1636 v->arch.ret = -EINTR;
1637 wake_up(&v->arch.cpu_run);
1640 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1641 break;
1642 vc->runner = vcpu;
1643 n_ceded = 0;
1644 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
1645 if (!v->arch.pending_exceptions)
1646 n_ceded += v->arch.ceded;
1647 else
1648 v->arch.ceded = 0;
1650 if (n_ceded == vc->n_runnable)
1651 kvmppc_vcore_blocked(vc);
1652 else
1653 kvmppc_run_core(vc);
1654 vc->runner = NULL;
1657 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1658 (vc->vcore_state == VCORE_RUNNING ||
1659 vc->vcore_state == VCORE_EXITING)) {
1660 spin_unlock(&vc->lock);
1661 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1662 spin_lock(&vc->lock);
1665 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1666 kvmppc_remove_runnable(vc, vcpu);
1667 vcpu->stat.signal_exits++;
1668 kvm_run->exit_reason = KVM_EXIT_INTR;
1669 vcpu->arch.ret = -EINTR;
1672 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1673 /* Wake up some vcpu to run the core */
1674 v = list_first_entry(&vc->runnable_threads,
1675 struct kvm_vcpu, arch.run_list);
1676 wake_up(&v->arch.cpu_run);
1679 spin_unlock(&vc->lock);
1680 return vcpu->arch.ret;
1683 static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
1685 int r;
1686 int srcu_idx;
1688 if (!vcpu->arch.sane) {
1689 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1690 return -EINVAL;
1693 kvmppc_core_prepare_to_enter(vcpu);
1695 /* No need to go into the guest when all we'll do is come back out */
1696 if (signal_pending(current)) {
1697 run->exit_reason = KVM_EXIT_INTR;
1698 return -EINTR;
1701 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1702 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1703 smp_mb();
1705 /* On the first time here, set up HTAB and VRMA or RMA */
1706 if (!vcpu->kvm->arch.rma_setup_done) {
1707 r = kvmppc_hv_setup_htab_rma(vcpu);
1708 if (r)
1709 goto out;
1712 flush_fp_to_thread(current);
1713 flush_altivec_to_thread(current);
1714 flush_vsx_to_thread(current);
1715 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
1716 vcpu->arch.pgdir = current->mm->pgd;
1717 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1719 do {
1720 r = kvmppc_run_vcpu(run, vcpu);
1722 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1723 !(vcpu->arch.shregs.msr & MSR_PR)) {
1724 r = kvmppc_pseries_do_hcall(vcpu);
1725 kvmppc_core_prepare_to_enter(vcpu);
1726 } else if (r == RESUME_PAGE_FAULT) {
1727 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1728 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1729 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1730 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1732 } while (r == RESUME_GUEST);
1734 out:
1735 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
1736 atomic_dec(&vcpu->kvm->arch.vcpus_running);
1737 return r;
1741 /* Work out RMLS (real mode limit selector) field value for a given RMA size.
1742 Assumes POWER7 or PPC970. */
1743 static inline int lpcr_rmls(unsigned long rma_size)
1745 switch (rma_size) {
1746 case 32ul << 20: /* 32 MB */
1747 if (cpu_has_feature(CPU_FTR_ARCH_206))
1748 return 8; /* only supported on POWER7 */
1749 return -1;
1750 case 64ul << 20: /* 64 MB */
1751 return 3;
1752 case 128ul << 20: /* 128 MB */
1753 return 7;
1754 case 256ul << 20: /* 256 MB */
1755 return 4;
1756 case 1ul << 30: /* 1 GB */
1757 return 2;
1758 case 16ul << 30: /* 16 GB */
1759 return 1;
1760 case 256ul << 30: /* 256 GB */
1761 return 0;
1762 default:
1763 return -1;
1767 static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1769 struct page *page;
1770 struct kvm_rma_info *ri = vma->vm_file->private_data;
1772 if (vmf->pgoff >= kvm_rma_pages)
1773 return VM_FAULT_SIGBUS;
1775 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1776 get_page(page);
1777 vmf->page = page;
1778 return 0;
1781 static const struct vm_operations_struct kvm_rma_vm_ops = {
1782 .fault = kvm_rma_fault,
1785 static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1787 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
1788 vma->vm_ops = &kvm_rma_vm_ops;
1789 return 0;
1792 static int kvm_rma_release(struct inode *inode, struct file *filp)
1794 struct kvm_rma_info *ri = filp->private_data;
1796 kvm_release_rma(ri);
1797 return 0;
1800 static const struct file_operations kvm_rma_fops = {
1801 .mmap = kvm_rma_mmap,
1802 .release = kvm_rma_release,
1805 static long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
1806 struct kvm_allocate_rma *ret)
1808 long fd;
1809 struct kvm_rma_info *ri;
1811 * Only do this on PPC970 in HV mode
1813 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
1814 !cpu_has_feature(CPU_FTR_ARCH_201))
1815 return -EINVAL;
1817 if (!kvm_rma_pages)
1818 return -EINVAL;
1820 ri = kvm_alloc_rma();
1821 if (!ri)
1822 return -ENOMEM;
1824 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
1825 if (fd < 0)
1826 kvm_release_rma(ri);
1828 ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
1829 return fd;
1832 static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1833 int linux_psize)
1835 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1837 if (!def->shift)
1838 return;
1839 (*sps)->page_shift = def->shift;
1840 (*sps)->slb_enc = def->sllp;
1841 (*sps)->enc[0].page_shift = def->shift;
1843 * Only return base page encoding. We don't want to return
1844 * all the supporting pte_enc, because our H_ENTER doesn't
1845 * support MPSS yet. Once they do, we can start passing all
1846 * support pte_enc here
1848 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
1849 (*sps)++;
1852 static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
1853 struct kvm_ppc_smmu_info *info)
1855 struct kvm_ppc_one_seg_page_size *sps;
1857 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1858 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1859 info->flags |= KVM_PPC_1T_SEGMENTS;
1860 info->slb_size = mmu_slb_size;
1862 /* We only support these sizes for now, and no muti-size segments */
1863 sps = &info->sps[0];
1864 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1865 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1866 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1868 return 0;
1872 * Get (and clear) the dirty memory log for a memory slot.
1874 static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
1875 struct kvm_dirty_log *log)
1877 struct kvm_memory_slot *memslot;
1878 int r;
1879 unsigned long n;
1881 mutex_lock(&kvm->slots_lock);
1883 r = -EINVAL;
1884 if (log->slot >= KVM_USER_MEM_SLOTS)
1885 goto out;
1887 memslot = id_to_memslot(kvm->memslots, log->slot);
1888 r = -ENOENT;
1889 if (!memslot->dirty_bitmap)
1890 goto out;
1892 n = kvm_dirty_bitmap_bytes(memslot);
1893 memset(memslot->dirty_bitmap, 0, n);
1895 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
1896 if (r)
1897 goto out;
1899 r = -EFAULT;
1900 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1901 goto out;
1903 r = 0;
1904 out:
1905 mutex_unlock(&kvm->slots_lock);
1906 return r;
1909 static void unpin_slot(struct kvm_memory_slot *memslot)
1911 unsigned long *physp;
1912 unsigned long j, npages, pfn;
1913 struct page *page;
1915 physp = memslot->arch.slot_phys;
1916 npages = memslot->npages;
1917 if (!physp)
1918 return;
1919 for (j = 0; j < npages; j++) {
1920 if (!(physp[j] & KVMPPC_GOT_PAGE))
1921 continue;
1922 pfn = physp[j] >> PAGE_SHIFT;
1923 page = pfn_to_page(pfn);
1924 SetPageDirty(page);
1925 put_page(page);
1929 static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
1930 struct kvm_memory_slot *dont)
1932 if (!dont || free->arch.rmap != dont->arch.rmap) {
1933 vfree(free->arch.rmap);
1934 free->arch.rmap = NULL;
1936 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1937 unpin_slot(free);
1938 vfree(free->arch.slot_phys);
1939 free->arch.slot_phys = NULL;
1943 static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
1944 unsigned long npages)
1946 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1947 if (!slot->arch.rmap)
1948 return -ENOMEM;
1949 slot->arch.slot_phys = NULL;
1951 return 0;
1954 static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
1955 struct kvm_memory_slot *memslot,
1956 struct kvm_userspace_memory_region *mem)
1958 unsigned long *phys;
1960 /* Allocate a slot_phys array if needed */
1961 phys = memslot->arch.slot_phys;
1962 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1963 phys = vzalloc(memslot->npages * sizeof(unsigned long));
1964 if (!phys)
1965 return -ENOMEM;
1966 memslot->arch.slot_phys = phys;
1969 return 0;
1972 static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
1973 struct kvm_userspace_memory_region *mem,
1974 const struct kvm_memory_slot *old)
1976 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1977 struct kvm_memory_slot *memslot;
1979 if (npages && old->npages) {
1981 * If modifying a memslot, reset all the rmap dirty bits.
1982 * If this is a new memslot, we don't need to do anything
1983 * since the rmap array starts out as all zeroes,
1984 * i.e. no pages are dirty.
1986 memslot = id_to_memslot(kvm->memslots, mem->slot);
1987 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1992 * Update LPCR values in kvm->arch and in vcores.
1993 * Caller must hold kvm->lock.
1995 void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
1997 long int i;
1998 u32 cores_done = 0;
2000 if ((kvm->arch.lpcr & mask) == lpcr)
2001 return;
2003 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2005 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2006 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2007 if (!vc)
2008 continue;
2009 spin_lock(&vc->lock);
2010 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2011 spin_unlock(&vc->lock);
2012 if (++cores_done >= kvm->arch.online_vcores)
2013 break;
2017 static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2019 return;
2022 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
2024 int err = 0;
2025 struct kvm *kvm = vcpu->kvm;
2026 struct kvm_rma_info *ri = NULL;
2027 unsigned long hva;
2028 struct kvm_memory_slot *memslot;
2029 struct vm_area_struct *vma;
2030 unsigned long lpcr = 0, senc;
2031 unsigned long lpcr_mask = 0;
2032 unsigned long psize, porder;
2033 unsigned long rma_size;
2034 unsigned long rmls;
2035 unsigned long *physp;
2036 unsigned long i, npages;
2037 int srcu_idx;
2039 mutex_lock(&kvm->lock);
2040 if (kvm->arch.rma_setup_done)
2041 goto out; /* another vcpu beat us to it */
2043 /* Allocate hashed page table (if not done already) and reset it */
2044 if (!kvm->arch.hpt_virt) {
2045 err = kvmppc_alloc_hpt(kvm, NULL);
2046 if (err) {
2047 pr_err("KVM: Couldn't alloc HPT\n");
2048 goto out;
2052 /* Look up the memslot for guest physical address 0 */
2053 srcu_idx = srcu_read_lock(&kvm->srcu);
2054 memslot = gfn_to_memslot(kvm, 0);
2056 /* We must have some memory at 0 by now */
2057 err = -EINVAL;
2058 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
2059 goto out_srcu;
2061 /* Look up the VMA for the start of this memory slot */
2062 hva = memslot->userspace_addr;
2063 down_read(&current->mm->mmap_sem);
2064 vma = find_vma(current->mm, hva);
2065 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2066 goto up_out;
2068 psize = vma_kernel_pagesize(vma);
2069 porder = __ilog2(psize);
2071 /* Is this one of our preallocated RMAs? */
2072 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
2073 hva == vma->vm_start)
2074 ri = vma->vm_file->private_data;
2076 up_read(&current->mm->mmap_sem);
2078 if (!ri) {
2079 /* On POWER7, use VRMA; on PPC970, give up */
2080 err = -EPERM;
2081 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2082 pr_err("KVM: CPU requires an RMO\n");
2083 goto out_srcu;
2086 /* We can handle 4k, 64k or 16M pages in the VRMA */
2087 err = -EINVAL;
2088 if (!(psize == 0x1000 || psize == 0x10000 ||
2089 psize == 0x1000000))
2090 goto out_srcu;
2092 /* Update VRMASD field in the LPCR */
2093 senc = slb_pgsize_encoding(psize);
2094 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2095 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2096 lpcr_mask = LPCR_VRMASD;
2097 /* the -4 is to account for senc values starting at 0x10 */
2098 lpcr = senc << (LPCR_VRMASD_SH - 4);
2100 /* Create HPTEs in the hash page table for the VRMA */
2101 kvmppc_map_vrma(vcpu, memslot, porder);
2103 } else {
2104 /* Set up to use an RMO region */
2105 rma_size = kvm_rma_pages;
2106 if (rma_size > memslot->npages)
2107 rma_size = memslot->npages;
2108 rma_size <<= PAGE_SHIFT;
2109 rmls = lpcr_rmls(rma_size);
2110 err = -EINVAL;
2111 if ((long)rmls < 0) {
2112 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
2113 goto out_srcu;
2115 atomic_inc(&ri->use_count);
2116 kvm->arch.rma = ri;
2118 /* Update LPCR and RMOR */
2119 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2120 /* PPC970; insert RMLS value (split field) in HID4 */
2121 lpcr_mask = (1ul << HID4_RMLS0_SH) |
2122 (3ul << HID4_RMLS2_SH) | HID4_RMOR;
2123 lpcr = ((rmls >> 2) << HID4_RMLS0_SH) |
2124 ((rmls & 3) << HID4_RMLS2_SH);
2125 /* RMOR is also in HID4 */
2126 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
2127 << HID4_RMOR_SH;
2128 } else {
2129 /* POWER7 */
2130 lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS;
2131 lpcr = rmls << LPCR_RMLS_SH;
2132 kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
2134 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
2135 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
2137 /* Initialize phys addrs of pages in RMO */
2138 npages = kvm_rma_pages;
2139 porder = __ilog2(npages);
2140 physp = memslot->arch.slot_phys;
2141 if (physp) {
2142 if (npages > memslot->npages)
2143 npages = memslot->npages;
2144 spin_lock(&kvm->arch.slot_phys_lock);
2145 for (i = 0; i < npages; ++i)
2146 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
2147 porder;
2148 spin_unlock(&kvm->arch.slot_phys_lock);
2152 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
2154 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
2155 smp_wmb();
2156 kvm->arch.rma_setup_done = 1;
2157 err = 0;
2158 out_srcu:
2159 srcu_read_unlock(&kvm->srcu, srcu_idx);
2160 out:
2161 mutex_unlock(&kvm->lock);
2162 return err;
2164 up_out:
2165 up_read(&current->mm->mmap_sem);
2166 goto out_srcu;
2169 static int kvmppc_core_init_vm_hv(struct kvm *kvm)
2171 unsigned long lpcr, lpid;
2173 /* Allocate the guest's logical partition ID */
2175 lpid = kvmppc_alloc_lpid();
2176 if ((long)lpid < 0)
2177 return -ENOMEM;
2178 kvm->arch.lpid = lpid;
2181 * Since we don't flush the TLB when tearing down a VM,
2182 * and this lpid might have previously been used,
2183 * make sure we flush on each core before running the new VM.
2185 cpumask_setall(&kvm->arch.need_tlb_flush);
2187 kvm->arch.rma = NULL;
2189 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
2191 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2192 /* PPC970; HID4 is effectively the LPCR */
2193 kvm->arch.host_lpid = 0;
2194 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
2195 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
2196 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
2197 ((lpid & 0xf) << HID4_LPID5_SH);
2198 } else {
2199 /* POWER7; init LPCR for virtual RMA mode */
2200 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2201 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2202 lpcr &= LPCR_PECE | LPCR_LPES;
2203 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
2204 LPCR_VPM0 | LPCR_VPM1;
2205 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2206 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2207 /* On POWER8 turn on online bit to enable PURR/SPURR */
2208 if (cpu_has_feature(CPU_FTR_ARCH_207S))
2209 lpcr |= LPCR_ONL;
2211 kvm->arch.lpcr = lpcr;
2213 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
2214 spin_lock_init(&kvm->arch.slot_phys_lock);
2217 * Don't allow secondary CPU threads to come online
2218 * while any KVM VMs exist.
2220 inhibit_secondary_onlining();
2222 return 0;
2225 static void kvmppc_free_vcores(struct kvm *kvm)
2227 long int i;
2229 for (i = 0; i < KVM_MAX_VCORES; ++i)
2230 kfree(kvm->arch.vcores[i]);
2231 kvm->arch.online_vcores = 0;
2234 static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
2236 uninhibit_secondary_onlining();
2238 kvmppc_free_vcores(kvm);
2239 if (kvm->arch.rma) {
2240 kvm_release_rma(kvm->arch.rma);
2241 kvm->arch.rma = NULL;
2244 kvmppc_free_hpt(kvm);
2247 /* We don't need to emulate any privileged instructions or dcbz */
2248 static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2249 unsigned int inst, int *advance)
2251 return EMULATE_FAIL;
2254 static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2255 ulong spr_val)
2257 return EMULATE_FAIL;
2260 static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2261 ulong *spr_val)
2263 return EMULATE_FAIL;
2266 static int kvmppc_core_check_processor_compat_hv(void)
2268 if (!cpu_has_feature(CPU_FTR_HVMODE))
2269 return -EIO;
2270 return 0;
2273 static long kvm_arch_vm_ioctl_hv(struct file *filp,
2274 unsigned int ioctl, unsigned long arg)
2276 struct kvm *kvm __maybe_unused = filp->private_data;
2277 void __user *argp = (void __user *)arg;
2278 long r;
2280 switch (ioctl) {
2282 case KVM_ALLOCATE_RMA: {
2283 struct kvm_allocate_rma rma;
2284 struct kvm *kvm = filp->private_data;
2286 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
2287 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
2288 r = -EFAULT;
2289 break;
2292 case KVM_PPC_ALLOCATE_HTAB: {
2293 u32 htab_order;
2295 r = -EFAULT;
2296 if (get_user(htab_order, (u32 __user *)argp))
2297 break;
2298 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2299 if (r)
2300 break;
2301 r = -EFAULT;
2302 if (put_user(htab_order, (u32 __user *)argp))
2303 break;
2304 r = 0;
2305 break;
2308 case KVM_PPC_GET_HTAB_FD: {
2309 struct kvm_get_htab_fd ghf;
2311 r = -EFAULT;
2312 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2313 break;
2314 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2315 break;
2318 default:
2319 r = -ENOTTY;
2322 return r;
2325 static struct kvmppc_ops kvm_ops_hv = {
2326 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2327 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2328 .get_one_reg = kvmppc_get_one_reg_hv,
2329 .set_one_reg = kvmppc_set_one_reg_hv,
2330 .vcpu_load = kvmppc_core_vcpu_load_hv,
2331 .vcpu_put = kvmppc_core_vcpu_put_hv,
2332 .set_msr = kvmppc_set_msr_hv,
2333 .vcpu_run = kvmppc_vcpu_run_hv,
2334 .vcpu_create = kvmppc_core_vcpu_create_hv,
2335 .vcpu_free = kvmppc_core_vcpu_free_hv,
2336 .check_requests = kvmppc_core_check_requests_hv,
2337 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2338 .flush_memslot = kvmppc_core_flush_memslot_hv,
2339 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2340 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2341 .unmap_hva = kvm_unmap_hva_hv,
2342 .unmap_hva_range = kvm_unmap_hva_range_hv,
2343 .age_hva = kvm_age_hva_hv,
2344 .test_age_hva = kvm_test_age_hva_hv,
2345 .set_spte_hva = kvm_set_spte_hva_hv,
2346 .mmu_destroy = kvmppc_mmu_destroy_hv,
2347 .free_memslot = kvmppc_core_free_memslot_hv,
2348 .create_memslot = kvmppc_core_create_memslot_hv,
2349 .init_vm = kvmppc_core_init_vm_hv,
2350 .destroy_vm = kvmppc_core_destroy_vm_hv,
2351 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2352 .emulate_op = kvmppc_core_emulate_op_hv,
2353 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2354 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2355 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2356 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
2359 static int kvmppc_book3s_init_hv(void)
2361 int r;
2363 * FIXME!! Do we need to check on all cpus ?
2365 r = kvmppc_core_check_processor_compat_hv();
2366 if (r < 0)
2367 return r;
2369 kvm_ops_hv.owner = THIS_MODULE;
2370 kvmppc_hv_ops = &kvm_ops_hv;
2372 r = kvmppc_mmu_hv_init();
2373 return r;
2376 static void kvmppc_book3s_exit_hv(void)
2378 kvmppc_hv_ops = NULL;
2381 module_init(kvmppc_book3s_init_hv);
2382 module_exit(kvmppc_book3s_exit_hv);
2383 MODULE_LICENSE("GPL");
2384 MODULE_ALIAS_MISCDEV(KVM_MINOR);
2385 MODULE_ALIAS("devname:kvm");