2 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
3 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
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>
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>
36 #include <asm/cputable.h>
37 #include <asm/cacheflush.h>
38 #include <asm/tlbflush.h>
39 #include <asm/uaccess.h>
41 #include <asm/kvm_ppc.h>
42 #include <asm/kvm_book3s.h>
43 #include <asm/mmu_context.h>
44 #include <asm/lppaca.h>
45 #include <asm/processor.h>
46 #include <asm/cputhreads.h>
48 #include <asm/hvcall.h>
49 #include <asm/switch_to.h>
51 #include <linux/gfp.h>
52 #include <linux/vmalloc.h>
53 #include <linux/highmem.h>
54 #include <linux/hugetlb.h>
56 /* #define EXIT_DEBUG */
57 /* #define EXIT_DEBUG_SIMPLE */
58 /* #define EXIT_DEBUG_INT */
60 /* Used to indicate that a guest page fault needs to be handled */
61 #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
63 /* Used as a "null" value for timebase values */
64 #define TB_NIL (~(u64)0)
66 static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
67 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
69 void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
73 wait_queue_head_t *wqp;
75 wqp = kvm_arch_vcpu_wq(vcpu);
76 if (waitqueue_active(wqp)) {
77 wake_up_interruptible(wqp);
78 ++vcpu->stat.halt_wakeup;
83 /* CPU points to the first thread of the core */
84 if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
85 int real_cpu = cpu + vcpu->arch.ptid;
86 if (paca[real_cpu].kvm_hstate.xics_phys)
87 xics_wake_cpu(real_cpu);
88 else if (cpu_online(cpu))
89 smp_send_reschedule(cpu);
95 * We use the vcpu_load/put functions to measure stolen time.
96 * Stolen time is counted as time when either the vcpu is able to
97 * run as part of a virtual core, but the task running the vcore
98 * is preempted or sleeping, or when the vcpu needs something done
99 * in the kernel by the task running the vcpu, but that task is
100 * preempted or sleeping. Those two things have to be counted
101 * separately, since one of the vcpu tasks will take on the job
102 * of running the core, and the other vcpu tasks in the vcore will
103 * sleep waiting for it to do that, but that sleep shouldn't count
106 * Hence we accumulate stolen time when the vcpu can run as part of
107 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
108 * needs its task to do other things in the kernel (for example,
109 * service a page fault) in busy_stolen. We don't accumulate
110 * stolen time for a vcore when it is inactive, or for a vcpu
111 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
112 * a misnomer; it means that the vcpu task is not executing in
113 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
114 * the kernel. We don't have any way of dividing up that time
115 * between time that the vcpu is genuinely stopped, time that
116 * the task is actively working on behalf of the vcpu, and time
117 * that the task is preempted, so we don't count any of it as
120 * Updates to busy_stolen are protected by arch.tbacct_lock;
121 * updates to vc->stolen_tb are protected by the arch.tbacct_lock
122 * of the vcpu that has taken responsibility for running the vcore
123 * (i.e. vc->runner). The stolen times are measured in units of
124 * timebase ticks. (Note that the != TB_NIL checks below are
125 * purely defensive; they should never fail.)
128 void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
130 struct kvmppc_vcore *vc = vcpu->arch.vcore;
132 spin_lock(&vcpu->arch.tbacct_lock);
133 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
134 vc->preempt_tb != TB_NIL) {
135 vc->stolen_tb += mftb() - vc->preempt_tb;
136 vc->preempt_tb = TB_NIL;
138 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
139 vcpu->arch.busy_preempt != TB_NIL) {
140 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
141 vcpu->arch.busy_preempt = TB_NIL;
143 spin_unlock(&vcpu->arch.tbacct_lock);
146 void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
148 struct kvmppc_vcore *vc = vcpu->arch.vcore;
150 spin_lock(&vcpu->arch.tbacct_lock);
151 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
152 vc->preempt_tb = mftb();
153 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
154 vcpu->arch.busy_preempt = mftb();
155 spin_unlock(&vcpu->arch.tbacct_lock);
158 void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
160 vcpu->arch.shregs.msr = msr;
161 kvmppc_end_cede(vcpu);
164 void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
166 vcpu->arch.pvr = pvr;
169 void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
173 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
174 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
175 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
176 for (r = 0; r < 16; ++r)
177 pr_err("r%2d = %.16lx r%d = %.16lx\n",
178 r, kvmppc_get_gpr(vcpu, r),
179 r+16, kvmppc_get_gpr(vcpu, r+16));
180 pr_err("ctr = %.16lx lr = %.16lx\n",
181 vcpu->arch.ctr, vcpu->arch.lr);
182 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
183 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
184 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
185 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
186 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
187 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
188 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
189 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
190 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
191 pr_err("fault dar = %.16lx dsisr = %.8x\n",
192 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
193 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
194 for (r = 0; r < vcpu->arch.slb_max; ++r)
195 pr_err(" ESID = %.16llx VSID = %.16llx\n",
196 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
197 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
198 vcpu->kvm->arch.lpcr, vcpu->kvm->arch.sdr1,
199 vcpu->arch.last_inst);
202 struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
205 struct kvm_vcpu *v, *ret = NULL;
207 mutex_lock(&kvm->lock);
208 kvm_for_each_vcpu(r, v, kvm) {
209 if (v->vcpu_id == id) {
214 mutex_unlock(&kvm->lock);
218 static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
220 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
221 vpa->yield_count = 1;
224 static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
225 unsigned long addr, unsigned long len)
227 /* check address is cacheline aligned */
228 if (addr & (L1_CACHE_BYTES - 1))
230 spin_lock(&vcpu->arch.vpa_update_lock);
231 if (v->next_gpa != addr || v->len != len) {
233 v->len = addr ? len : 0;
234 v->update_pending = 1;
236 spin_unlock(&vcpu->arch.vpa_update_lock);
240 /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
249 static int vpa_is_registered(struct kvmppc_vpa *vpap)
251 if (vpap->update_pending)
252 return vpap->next_gpa != 0;
253 return vpap->pinned_addr != NULL;
256 static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
258 unsigned long vcpuid, unsigned long vpa)
260 struct kvm *kvm = vcpu->kvm;
261 unsigned long len, nb;
263 struct kvm_vcpu *tvcpu;
266 struct kvmppc_vpa *vpap;
268 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
272 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
273 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
274 subfunc == H_VPA_REG_SLB) {
275 /* Registering new area - address must be cache-line aligned */
276 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
279 /* convert logical addr to kernel addr and read length */
280 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
283 if (subfunc == H_VPA_REG_VPA)
284 len = ((struct reg_vpa *)va)->length.hword;
286 len = ((struct reg_vpa *)va)->length.word;
287 kvmppc_unpin_guest_page(kvm, va, vpa, false);
290 if (len > nb || len < sizeof(struct reg_vpa))
299 spin_lock(&tvcpu->arch.vpa_update_lock);
302 case H_VPA_REG_VPA: /* register VPA */
303 if (len < sizeof(struct lppaca))
305 vpap = &tvcpu->arch.vpa;
309 case H_VPA_REG_DTL: /* register DTL */
310 if (len < sizeof(struct dtl_entry))
312 len -= len % sizeof(struct dtl_entry);
314 /* Check that they have previously registered a VPA */
316 if (!vpa_is_registered(&tvcpu->arch.vpa))
319 vpap = &tvcpu->arch.dtl;
323 case H_VPA_REG_SLB: /* register SLB shadow buffer */
324 /* Check that they have previously registered a VPA */
326 if (!vpa_is_registered(&tvcpu->arch.vpa))
329 vpap = &tvcpu->arch.slb_shadow;
333 case H_VPA_DEREG_VPA: /* deregister VPA */
334 /* Check they don't still have a DTL or SLB buf registered */
336 if (vpa_is_registered(&tvcpu->arch.dtl) ||
337 vpa_is_registered(&tvcpu->arch.slb_shadow))
340 vpap = &tvcpu->arch.vpa;
344 case H_VPA_DEREG_DTL: /* deregister DTL */
345 vpap = &tvcpu->arch.dtl;
349 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
350 vpap = &tvcpu->arch.slb_shadow;
356 vpap->next_gpa = vpa;
358 vpap->update_pending = 1;
361 spin_unlock(&tvcpu->arch.vpa_update_lock);
366 static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
368 struct kvm *kvm = vcpu->kvm;
374 * We need to pin the page pointed to by vpap->next_gpa,
375 * but we can't call kvmppc_pin_guest_page under the lock
376 * as it does get_user_pages() and down_read(). So we
377 * have to drop the lock, pin the page, then get the lock
378 * again and check that a new area didn't get registered
382 gpa = vpap->next_gpa;
383 spin_unlock(&vcpu->arch.vpa_update_lock);
387 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
388 spin_lock(&vcpu->arch.vpa_update_lock);
389 if (gpa == vpap->next_gpa)
391 /* sigh... unpin that one and try again */
393 kvmppc_unpin_guest_page(kvm, va, gpa, false);
396 vpap->update_pending = 0;
397 if (va && nb < vpap->len) {
399 * If it's now too short, it must be that userspace
400 * has changed the mappings underlying guest memory,
401 * so unregister the region.
403 kvmppc_unpin_guest_page(kvm, va, gpa, false);
406 if (vpap->pinned_addr)
407 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
410 vpap->pinned_addr = va;
413 vpap->pinned_end = va + vpap->len;
416 static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
418 if (!(vcpu->arch.vpa.update_pending ||
419 vcpu->arch.slb_shadow.update_pending ||
420 vcpu->arch.dtl.update_pending))
423 spin_lock(&vcpu->arch.vpa_update_lock);
424 if (vcpu->arch.vpa.update_pending) {
425 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
426 if (vcpu->arch.vpa.pinned_addr)
427 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
429 if (vcpu->arch.dtl.update_pending) {
430 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
431 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
432 vcpu->arch.dtl_index = 0;
434 if (vcpu->arch.slb_shadow.update_pending)
435 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
436 spin_unlock(&vcpu->arch.vpa_update_lock);
440 * Return the accumulated stolen time for the vcore up until `now'.
441 * The caller should hold the vcore lock.
443 static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
448 * If we are the task running the vcore, then since we hold
449 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
450 * can't be updated, so we don't need the tbacct_lock.
451 * If the vcore is inactive, it can't become active (since we
452 * hold the vcore lock), so the vcpu load/put functions won't
453 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
455 if (vc->vcore_state != VCORE_INACTIVE &&
456 vc->runner->arch.run_task != current) {
457 spin_lock(&vc->runner->arch.tbacct_lock);
459 if (vc->preempt_tb != TB_NIL)
460 p += now - vc->preempt_tb;
461 spin_unlock(&vc->runner->arch.tbacct_lock);
468 static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
469 struct kvmppc_vcore *vc)
471 struct dtl_entry *dt;
473 unsigned long stolen;
474 unsigned long core_stolen;
477 dt = vcpu->arch.dtl_ptr;
478 vpa = vcpu->arch.vpa.pinned_addr;
480 core_stolen = vcore_stolen_time(vc, now);
481 stolen = core_stolen - vcpu->arch.stolen_logged;
482 vcpu->arch.stolen_logged = core_stolen;
483 spin_lock(&vcpu->arch.tbacct_lock);
484 stolen += vcpu->arch.busy_stolen;
485 vcpu->arch.busy_stolen = 0;
486 spin_unlock(&vcpu->arch.tbacct_lock);
489 memset(dt, 0, sizeof(struct dtl_entry));
490 dt->dispatch_reason = 7;
491 dt->processor_id = vc->pcpu + vcpu->arch.ptid;
493 dt->enqueue_to_dispatch_time = stolen;
494 dt->srr0 = kvmppc_get_pc(vcpu);
495 dt->srr1 = vcpu->arch.shregs.msr;
497 if (dt == vcpu->arch.dtl.pinned_end)
498 dt = vcpu->arch.dtl.pinned_addr;
499 vcpu->arch.dtl_ptr = dt;
500 /* order writing *dt vs. writing vpa->dtl_idx */
502 vpa->dtl_idx = ++vcpu->arch.dtl_index;
503 vcpu->arch.dtl.dirty = true;
506 int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
508 unsigned long req = kvmppc_get_gpr(vcpu, 3);
509 unsigned long target, ret = H_SUCCESS;
510 struct kvm_vcpu *tvcpu;
515 idx = srcu_read_lock(&vcpu->kvm->srcu);
516 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
517 kvmppc_get_gpr(vcpu, 5),
518 kvmppc_get_gpr(vcpu, 6),
519 kvmppc_get_gpr(vcpu, 7));
520 srcu_read_unlock(&vcpu->kvm->srcu, idx);
525 target = kvmppc_get_gpr(vcpu, 4);
526 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
531 tvcpu->arch.prodded = 1;
533 if (vcpu->arch.ceded) {
534 if (waitqueue_active(&vcpu->wq)) {
535 wake_up_interruptible(&vcpu->wq);
536 vcpu->stat.halt_wakeup++;
543 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
544 kvmppc_get_gpr(vcpu, 5),
545 kvmppc_get_gpr(vcpu, 6));
548 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
551 rc = kvmppc_rtas_hcall(vcpu);
558 /* Send the error out to userspace via KVM_RUN */
567 if (kvmppc_xics_enabled(vcpu)) {
568 ret = kvmppc_xics_hcall(vcpu, req);
574 kvmppc_set_gpr(vcpu, 3, ret);
575 vcpu->arch.hcall_needed = 0;
579 static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
580 struct task_struct *tsk)
584 vcpu->stat.sum_exits++;
586 run->exit_reason = KVM_EXIT_UNKNOWN;
587 run->ready_for_interrupt_injection = 1;
588 switch (vcpu->arch.trap) {
589 /* We're good on these - the host merely wanted to get our attention */
590 case BOOK3S_INTERRUPT_HV_DECREMENTER:
591 vcpu->stat.dec_exits++;
594 case BOOK3S_INTERRUPT_EXTERNAL:
595 vcpu->stat.ext_intr_exits++;
598 case BOOK3S_INTERRUPT_PERFMON:
601 case BOOK3S_INTERRUPT_MACHINE_CHECK:
603 * Deliver a machine check interrupt to the guest.
604 * We have to do this, even if the host has handled the
605 * machine check, because machine checks use SRR0/1 and
606 * the interrupt might have trashed guest state in them.
608 kvmppc_book3s_queue_irqprio(vcpu,
609 BOOK3S_INTERRUPT_MACHINE_CHECK);
612 case BOOK3S_INTERRUPT_PROGRAM:
616 * Normally program interrupts are delivered directly
617 * to the guest by the hardware, but we can get here
618 * as a result of a hypervisor emulation interrupt
619 * (e40) getting turned into a 700 by BML RTAS.
621 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
622 kvmppc_core_queue_program(vcpu, flags);
626 case BOOK3S_INTERRUPT_SYSCALL:
628 /* hcall - punt to userspace */
631 if (vcpu->arch.shregs.msr & MSR_PR) {
632 /* sc 1 from userspace - reflect to guest syscall */
633 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
637 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
638 for (i = 0; i < 9; ++i)
639 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
640 run->exit_reason = KVM_EXIT_PAPR_HCALL;
641 vcpu->arch.hcall_needed = 1;
646 * We get these next two if the guest accesses a page which it thinks
647 * it has mapped but which is not actually present, either because
648 * it is for an emulated I/O device or because the corresonding
649 * host page has been paged out. Any other HDSI/HISI interrupts
650 * have been handled already.
652 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
653 r = RESUME_PAGE_FAULT;
655 case BOOK3S_INTERRUPT_H_INST_STORAGE:
656 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
657 vcpu->arch.fault_dsisr = 0;
658 r = RESUME_PAGE_FAULT;
661 * This occurs if the guest executes an illegal instruction.
662 * We just generate a program interrupt to the guest, since
663 * we don't emulate any guest instructions at this stage.
665 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
666 kvmppc_core_queue_program(vcpu, 0x80000);
670 kvmppc_dump_regs(vcpu);
671 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
672 vcpu->arch.trap, kvmppc_get_pc(vcpu),
673 vcpu->arch.shregs.msr);
682 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
683 struct kvm_sregs *sregs)
687 memset(sregs, 0, sizeof(struct kvm_sregs));
688 sregs->pvr = vcpu->arch.pvr;
689 for (i = 0; i < vcpu->arch.slb_max; i++) {
690 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
691 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
697 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
698 struct kvm_sregs *sregs)
702 kvmppc_set_pvr(vcpu, sregs->pvr);
705 for (i = 0; i < vcpu->arch.slb_nr; i++) {
706 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
707 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
708 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
712 vcpu->arch.slb_max = j;
717 int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
723 case KVM_REG_PPC_HIOR:
724 *val = get_reg_val(id, 0);
726 case KVM_REG_PPC_DABR:
727 *val = get_reg_val(id, vcpu->arch.dabr);
729 case KVM_REG_PPC_DSCR:
730 *val = get_reg_val(id, vcpu->arch.dscr);
732 case KVM_REG_PPC_PURR:
733 *val = get_reg_val(id, vcpu->arch.purr);
735 case KVM_REG_PPC_SPURR:
736 *val = get_reg_val(id, vcpu->arch.spurr);
738 case KVM_REG_PPC_AMR:
739 *val = get_reg_val(id, vcpu->arch.amr);
741 case KVM_REG_PPC_UAMOR:
742 *val = get_reg_val(id, vcpu->arch.uamor);
744 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
745 i = id - KVM_REG_PPC_MMCR0;
746 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
748 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
749 i = id - KVM_REG_PPC_PMC1;
750 *val = get_reg_val(id, vcpu->arch.pmc[i]);
752 case KVM_REG_PPC_SIAR:
753 *val = get_reg_val(id, vcpu->arch.siar);
755 case KVM_REG_PPC_SDAR:
756 *val = get_reg_val(id, vcpu->arch.sdar);
759 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
760 if (cpu_has_feature(CPU_FTR_VSX)) {
761 /* VSX => FP reg i is stored in arch.vsr[2*i] */
762 long int i = id - KVM_REG_PPC_FPR0;
763 *val = get_reg_val(id, vcpu->arch.vsr[2 * i]);
765 /* let generic code handle it */
769 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
770 if (cpu_has_feature(CPU_FTR_VSX)) {
771 long int i = id - KVM_REG_PPC_VSR0;
772 val->vsxval[0] = vcpu->arch.vsr[2 * i];
773 val->vsxval[1] = vcpu->arch.vsr[2 * i + 1];
778 #endif /* CONFIG_VSX */
779 case KVM_REG_PPC_VPA_ADDR:
780 spin_lock(&vcpu->arch.vpa_update_lock);
781 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
782 spin_unlock(&vcpu->arch.vpa_update_lock);
784 case KVM_REG_PPC_VPA_SLB:
785 spin_lock(&vcpu->arch.vpa_update_lock);
786 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
787 val->vpaval.length = vcpu->arch.slb_shadow.len;
788 spin_unlock(&vcpu->arch.vpa_update_lock);
790 case KVM_REG_PPC_VPA_DTL:
791 spin_lock(&vcpu->arch.vpa_update_lock);
792 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
793 val->vpaval.length = vcpu->arch.dtl.len;
794 spin_unlock(&vcpu->arch.vpa_update_lock);
804 int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
808 unsigned long addr, len;
811 case KVM_REG_PPC_HIOR:
812 /* Only allow this to be set to zero */
813 if (set_reg_val(id, *val))
816 case KVM_REG_PPC_DABR:
817 vcpu->arch.dabr = set_reg_val(id, *val);
819 case KVM_REG_PPC_DSCR:
820 vcpu->arch.dscr = set_reg_val(id, *val);
822 case KVM_REG_PPC_PURR:
823 vcpu->arch.purr = set_reg_val(id, *val);
825 case KVM_REG_PPC_SPURR:
826 vcpu->arch.spurr = set_reg_val(id, *val);
828 case KVM_REG_PPC_AMR:
829 vcpu->arch.amr = set_reg_val(id, *val);
831 case KVM_REG_PPC_UAMOR:
832 vcpu->arch.uamor = set_reg_val(id, *val);
834 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
835 i = id - KVM_REG_PPC_MMCR0;
836 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
838 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
839 i = id - KVM_REG_PPC_PMC1;
840 vcpu->arch.pmc[i] = set_reg_val(id, *val);
842 case KVM_REG_PPC_SIAR:
843 vcpu->arch.siar = set_reg_val(id, *val);
845 case KVM_REG_PPC_SDAR:
846 vcpu->arch.sdar = set_reg_val(id, *val);
849 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
850 if (cpu_has_feature(CPU_FTR_VSX)) {
851 /* VSX => FP reg i is stored in arch.vsr[2*i] */
852 long int i = id - KVM_REG_PPC_FPR0;
853 vcpu->arch.vsr[2 * i] = set_reg_val(id, *val);
855 /* let generic code handle it */
859 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
860 if (cpu_has_feature(CPU_FTR_VSX)) {
861 long int i = id - KVM_REG_PPC_VSR0;
862 vcpu->arch.vsr[2 * i] = val->vsxval[0];
863 vcpu->arch.vsr[2 * i + 1] = val->vsxval[1];
868 #endif /* CONFIG_VSX */
869 case KVM_REG_PPC_VPA_ADDR:
870 addr = set_reg_val(id, *val);
872 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
873 vcpu->arch.dtl.next_gpa))
875 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
877 case KVM_REG_PPC_VPA_SLB:
878 addr = val->vpaval.addr;
879 len = val->vpaval.length;
881 if (addr && !vcpu->arch.vpa.next_gpa)
883 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
885 case KVM_REG_PPC_VPA_DTL:
886 addr = val->vpaval.addr;
887 len = val->vpaval.length;
889 if (addr && (len < sizeof(struct dtl_entry) ||
890 !vcpu->arch.vpa.next_gpa))
892 len -= len % sizeof(struct dtl_entry);
893 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
903 int kvmppc_core_check_processor_compat(void)
905 if (cpu_has_feature(CPU_FTR_HVMODE))
910 struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
912 struct kvm_vcpu *vcpu;
915 struct kvmppc_vcore *vcore;
917 core = id / threads_per_core;
918 if (core >= KVM_MAX_VCORES)
922 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
926 err = kvm_vcpu_init(vcpu, kvm, id);
930 vcpu->arch.shared = &vcpu->arch.shregs;
931 vcpu->arch.mmcr[0] = MMCR0_FC;
932 vcpu->arch.ctrl = CTRL_RUNLATCH;
933 /* default to host PVR, since we can't spoof it */
934 vcpu->arch.pvr = mfspr(SPRN_PVR);
935 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
936 spin_lock_init(&vcpu->arch.vpa_update_lock);
937 spin_lock_init(&vcpu->arch.tbacct_lock);
938 vcpu->arch.busy_preempt = TB_NIL;
940 kvmppc_mmu_book3s_hv_init(vcpu);
942 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
944 init_waitqueue_head(&vcpu->arch.cpu_run);
946 mutex_lock(&kvm->lock);
947 vcore = kvm->arch.vcores[core];
949 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
951 INIT_LIST_HEAD(&vcore->runnable_threads);
952 spin_lock_init(&vcore->lock);
953 init_waitqueue_head(&vcore->wq);
954 vcore->preempt_tb = TB_NIL;
956 kvm->arch.vcores[core] = vcore;
957 kvm->arch.online_vcores++;
959 mutex_unlock(&kvm->lock);
964 spin_lock(&vcore->lock);
965 ++vcore->num_threads;
966 spin_unlock(&vcore->lock);
967 vcpu->arch.vcore = vcore;
969 vcpu->arch.cpu_type = KVM_CPU_3S_64;
970 kvmppc_sanity_check(vcpu);
975 kmem_cache_free(kvm_vcpu_cache, vcpu);
980 static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
982 if (vpa->pinned_addr)
983 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
987 void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
989 spin_lock(&vcpu->arch.vpa_update_lock);
990 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
991 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
992 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
993 spin_unlock(&vcpu->arch.vpa_update_lock);
994 kvm_vcpu_uninit(vcpu);
995 kmem_cache_free(kvm_vcpu_cache, vcpu);
998 static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
1000 unsigned long dec_nsec, now;
1003 if (now > vcpu->arch.dec_expires) {
1004 /* decrementer has already gone negative */
1005 kvmppc_core_queue_dec(vcpu);
1006 kvmppc_core_prepare_to_enter(vcpu);
1009 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1011 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1013 vcpu->arch.timer_running = 1;
1016 static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
1018 vcpu->arch.ceded = 0;
1019 if (vcpu->arch.timer_running) {
1020 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1021 vcpu->arch.timer_running = 0;
1025 extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1027 static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1028 struct kvm_vcpu *vcpu)
1032 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1034 spin_lock(&vcpu->arch.tbacct_lock);
1036 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1037 vcpu->arch.stolen_logged;
1038 vcpu->arch.busy_preempt = now;
1039 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1040 spin_unlock(&vcpu->arch.tbacct_lock);
1042 list_del(&vcpu->arch.run_list);
1045 static int kvmppc_grab_hwthread(int cpu)
1047 struct paca_struct *tpaca;
1048 long timeout = 1000;
1052 /* Ensure the thread won't go into the kernel if it wakes */
1053 tpaca->kvm_hstate.hwthread_req = 1;
1054 tpaca->kvm_hstate.kvm_vcpu = NULL;
1057 * If the thread is already executing in the kernel (e.g. handling
1058 * a stray interrupt), wait for it to get back to nap mode.
1059 * The smp_mb() is to ensure that our setting of hwthread_req
1060 * is visible before we look at hwthread_state, so if this
1061 * races with the code at system_reset_pSeries and the thread
1062 * misses our setting of hwthread_req, we are sure to see its
1063 * setting of hwthread_state, and vice versa.
1066 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1067 if (--timeout <= 0) {
1068 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1076 static void kvmppc_release_hwthread(int cpu)
1078 struct paca_struct *tpaca;
1081 tpaca->kvm_hstate.hwthread_req = 0;
1082 tpaca->kvm_hstate.kvm_vcpu = NULL;
1085 static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1088 struct paca_struct *tpaca;
1089 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1091 if (vcpu->arch.timer_running) {
1092 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1093 vcpu->arch.timer_running = 0;
1095 cpu = vc->pcpu + vcpu->arch.ptid;
1097 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1098 tpaca->kvm_hstate.kvm_vcore = vc;
1099 tpaca->kvm_hstate.napping = 0;
1100 vcpu->cpu = vc->pcpu;
1102 #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
1103 if (vcpu->arch.ptid) {
1110 static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1116 while (vc->nap_count < vc->n_woken) {
1117 if (++i >= 1000000) {
1118 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1119 vc->nap_count, vc->n_woken);
1128 * Check that we are on thread 0 and that any other threads in
1129 * this core are off-line. Then grab the threads so they can't
1132 static int on_primary_thread(void)
1134 int cpu = smp_processor_id();
1135 int thr = cpu_thread_in_core(cpu);
1139 while (++thr < threads_per_core)
1140 if (cpu_online(cpu + thr))
1143 /* Grab all hw threads so they can't go into the kernel */
1144 for (thr = 1; thr < threads_per_core; ++thr) {
1145 if (kvmppc_grab_hwthread(cpu + thr)) {
1146 /* Couldn't grab one; let the others go */
1148 kvmppc_release_hwthread(cpu + thr);
1149 } while (--thr > 0);
1157 * Run a set of guest threads on a physical core.
1158 * Called with vc->lock held.
1160 static void kvmppc_run_core(struct kvmppc_vcore *vc)
1162 struct kvm_vcpu *vcpu, *vcpu0, *vnext;
1165 int ptid, i, need_vpa_update;
1167 struct kvm_vcpu *vcpus_to_update[threads_per_core];
1169 /* don't start if any threads have a signal pending */
1170 need_vpa_update = 0;
1171 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1172 if (signal_pending(vcpu->arch.run_task))
1174 if (vcpu->arch.vpa.update_pending ||
1175 vcpu->arch.slb_shadow.update_pending ||
1176 vcpu->arch.dtl.update_pending)
1177 vcpus_to_update[need_vpa_update++] = vcpu;
1181 * Initialize *vc, in particular vc->vcore_state, so we can
1182 * drop the vcore lock if necessary.
1186 vc->entry_exit_count = 0;
1187 vc->vcore_state = VCORE_STARTING;
1189 vc->napping_threads = 0;
1192 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1193 * which can't be called with any spinlocks held.
1195 if (need_vpa_update) {
1196 spin_unlock(&vc->lock);
1197 for (i = 0; i < need_vpa_update; ++i)
1198 kvmppc_update_vpas(vcpus_to_update[i]);
1199 spin_lock(&vc->lock);
1203 * Assign physical thread IDs, first to non-ceded vcpus
1204 * and then to ceded ones.
1208 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1209 if (!vcpu->arch.ceded) {
1212 vcpu->arch.ptid = ptid++;
1216 goto out; /* nothing to run; should never happen */
1217 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1218 if (vcpu->arch.ceded)
1219 vcpu->arch.ptid = ptid++;
1222 * Make sure we are running on thread 0, and that
1223 * secondary threads are offline.
1225 if (threads_per_core > 1 && !on_primary_thread()) {
1226 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1227 vcpu->arch.ret = -EBUSY;
1231 vc->pcpu = smp_processor_id();
1232 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1233 kvmppc_start_thread(vcpu);
1234 kvmppc_create_dtl_entry(vcpu, vc);
1237 vc->vcore_state = VCORE_RUNNING;
1239 spin_unlock(&vc->lock);
1243 srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
1245 __kvmppc_vcore_entry(NULL, vcpu0);
1247 spin_lock(&vc->lock);
1248 /* disable sending of IPIs on virtual external irqs */
1249 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1251 /* wait for secondary threads to finish writing their state to memory */
1252 if (vc->nap_count < vc->n_woken)
1253 kvmppc_wait_for_nap(vc);
1254 for (i = 0; i < threads_per_core; ++i)
1255 kvmppc_release_hwthread(vc->pcpu + i);
1256 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
1257 vc->vcore_state = VCORE_EXITING;
1258 spin_unlock(&vc->lock);
1260 srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
1262 /* make sure updates to secondary vcpu structs are visible now */
1269 spin_lock(&vc->lock);
1271 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1272 /* cancel pending dec exception if dec is positive */
1273 if (now < vcpu->arch.dec_expires &&
1274 kvmppc_core_pending_dec(vcpu))
1275 kvmppc_core_dequeue_dec(vcpu);
1278 if (vcpu->arch.trap)
1279 ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
1280 vcpu->arch.run_task);
1282 vcpu->arch.ret = ret;
1283 vcpu->arch.trap = 0;
1285 if (vcpu->arch.ceded) {
1286 if (ret != RESUME_GUEST)
1287 kvmppc_end_cede(vcpu);
1289 kvmppc_set_timer(vcpu);
1294 vc->vcore_state = VCORE_INACTIVE;
1295 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1297 if (vcpu->arch.ret != RESUME_GUEST) {
1298 kvmppc_remove_runnable(vc, vcpu);
1299 wake_up(&vcpu->arch.cpu_run);
1305 * Wait for some other vcpu thread to execute us, and
1306 * wake us up when we need to handle something in the host.
1308 static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
1312 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1313 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1315 finish_wait(&vcpu->arch.cpu_run, &wait);
1319 * All the vcpus in this vcore are idle, so wait for a decrementer
1320 * or external interrupt to one of the vcpus. vc->lock is held.
1322 static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1326 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1327 vc->vcore_state = VCORE_SLEEPING;
1328 spin_unlock(&vc->lock);
1330 finish_wait(&vc->wq, &wait);
1331 spin_lock(&vc->lock);
1332 vc->vcore_state = VCORE_INACTIVE;
1335 static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1338 struct kvmppc_vcore *vc;
1339 struct kvm_vcpu *v, *vn;
1341 kvm_run->exit_reason = 0;
1342 vcpu->arch.ret = RESUME_GUEST;
1343 vcpu->arch.trap = 0;
1344 kvmppc_update_vpas(vcpu);
1347 * Synchronize with other threads in this virtual core
1349 vc = vcpu->arch.vcore;
1350 spin_lock(&vc->lock);
1351 vcpu->arch.ceded = 0;
1352 vcpu->arch.run_task = current;
1353 vcpu->arch.kvm_run = kvm_run;
1354 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
1355 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
1356 vcpu->arch.busy_preempt = TB_NIL;
1357 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1361 * This happens the first time this is called for a vcpu.
1362 * If the vcore is already running, we may be able to start
1363 * this thread straight away and have it join in.
1365 if (!signal_pending(current)) {
1366 if (vc->vcore_state == VCORE_RUNNING &&
1367 VCORE_EXIT_COUNT(vc) == 0) {
1368 vcpu->arch.ptid = vc->n_runnable - 1;
1369 kvmppc_create_dtl_entry(vcpu, vc);
1370 kvmppc_start_thread(vcpu);
1371 } else if (vc->vcore_state == VCORE_SLEEPING) {
1377 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1378 !signal_pending(current)) {
1379 if (vc->vcore_state != VCORE_INACTIVE) {
1380 spin_unlock(&vc->lock);
1381 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1382 spin_lock(&vc->lock);
1385 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1387 kvmppc_core_prepare_to_enter(v);
1388 if (signal_pending(v->arch.run_task)) {
1389 kvmppc_remove_runnable(vc, v);
1390 v->stat.signal_exits++;
1391 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1392 v->arch.ret = -EINTR;
1393 wake_up(&v->arch.cpu_run);
1396 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1400 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
1401 if (!v->arch.pending_exceptions)
1402 n_ceded += v->arch.ceded;
1406 if (n_ceded == vc->n_runnable)
1407 kvmppc_vcore_blocked(vc);
1409 kvmppc_run_core(vc);
1413 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1414 (vc->vcore_state == VCORE_RUNNING ||
1415 vc->vcore_state == VCORE_EXITING)) {
1416 spin_unlock(&vc->lock);
1417 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1418 spin_lock(&vc->lock);
1421 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1422 kvmppc_remove_runnable(vc, vcpu);
1423 vcpu->stat.signal_exits++;
1424 kvm_run->exit_reason = KVM_EXIT_INTR;
1425 vcpu->arch.ret = -EINTR;
1428 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1429 /* Wake up some vcpu to run the core */
1430 v = list_first_entry(&vc->runnable_threads,
1431 struct kvm_vcpu, arch.run_list);
1432 wake_up(&v->arch.cpu_run);
1435 spin_unlock(&vc->lock);
1436 return vcpu->arch.ret;
1439 int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
1444 if (!vcpu->arch.sane) {
1445 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1449 kvmppc_core_prepare_to_enter(vcpu);
1451 /* No need to go into the guest when all we'll do is come back out */
1452 if (signal_pending(current)) {
1453 run->exit_reason = KVM_EXIT_INTR;
1457 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1458 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1461 /* On the first time here, set up HTAB and VRMA or RMA */
1462 if (!vcpu->kvm->arch.rma_setup_done) {
1463 r = kvmppc_hv_setup_htab_rma(vcpu);
1468 flush_fp_to_thread(current);
1469 flush_altivec_to_thread(current);
1470 flush_vsx_to_thread(current);
1471 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
1472 vcpu->arch.pgdir = current->mm->pgd;
1473 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1476 r = kvmppc_run_vcpu(run, vcpu);
1478 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1479 !(vcpu->arch.shregs.msr & MSR_PR)) {
1480 r = kvmppc_pseries_do_hcall(vcpu);
1481 kvmppc_core_prepare_to_enter(vcpu);
1482 } else if (r == RESUME_PAGE_FAULT) {
1483 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1484 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1485 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1486 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1488 } while (r == RESUME_GUEST);
1491 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
1492 atomic_dec(&vcpu->kvm->arch.vcpus_running);
1497 /* Work out RMLS (real mode limit selector) field value for a given RMA size.
1498 Assumes POWER7 or PPC970. */
1499 static inline int lpcr_rmls(unsigned long rma_size)
1502 case 32ul << 20: /* 32 MB */
1503 if (cpu_has_feature(CPU_FTR_ARCH_206))
1504 return 8; /* only supported on POWER7 */
1506 case 64ul << 20: /* 64 MB */
1508 case 128ul << 20: /* 128 MB */
1510 case 256ul << 20: /* 256 MB */
1512 case 1ul << 30: /* 1 GB */
1514 case 16ul << 30: /* 16 GB */
1516 case 256ul << 30: /* 256 GB */
1523 static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1526 struct kvm_rma_info *ri = vma->vm_file->private_data;
1528 if (vmf->pgoff >= kvm_rma_pages)
1529 return VM_FAULT_SIGBUS;
1531 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1537 static const struct vm_operations_struct kvm_rma_vm_ops = {
1538 .fault = kvm_rma_fault,
1541 static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1543 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
1544 vma->vm_ops = &kvm_rma_vm_ops;
1548 static int kvm_rma_release(struct inode *inode, struct file *filp)
1550 struct kvm_rma_info *ri = filp->private_data;
1552 kvm_release_rma(ri);
1556 static const struct file_operations kvm_rma_fops = {
1557 .mmap = kvm_rma_mmap,
1558 .release = kvm_rma_release,
1561 long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
1564 struct kvm_rma_info *ri;
1566 * Only do this on PPC970 in HV mode
1568 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
1569 !cpu_has_feature(CPU_FTR_ARCH_201))
1575 ri = kvm_alloc_rma();
1579 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
1581 kvm_release_rma(ri);
1583 ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
1587 static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1590 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1594 (*sps)->page_shift = def->shift;
1595 (*sps)->slb_enc = def->sllp;
1596 (*sps)->enc[0].page_shift = def->shift;
1598 * Only return base page encoding. We don't want to return
1599 * all the supporting pte_enc, because our H_ENTER doesn't
1600 * support MPSS yet. Once they do, we can start passing all
1601 * support pte_enc here
1603 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
1607 int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
1609 struct kvm_ppc_one_seg_page_size *sps;
1611 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1612 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1613 info->flags |= KVM_PPC_1T_SEGMENTS;
1614 info->slb_size = mmu_slb_size;
1616 /* We only support these sizes for now, and no muti-size segments */
1617 sps = &info->sps[0];
1618 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1619 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1620 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1626 * Get (and clear) the dirty memory log for a memory slot.
1628 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1630 struct kvm_memory_slot *memslot;
1634 mutex_lock(&kvm->slots_lock);
1637 if (log->slot >= KVM_USER_MEM_SLOTS)
1640 memslot = id_to_memslot(kvm->memslots, log->slot);
1642 if (!memslot->dirty_bitmap)
1645 n = kvm_dirty_bitmap_bytes(memslot);
1646 memset(memslot->dirty_bitmap, 0, n);
1648 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
1653 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1658 mutex_unlock(&kvm->slots_lock);
1662 static void unpin_slot(struct kvm_memory_slot *memslot)
1664 unsigned long *physp;
1665 unsigned long j, npages, pfn;
1668 physp = memslot->arch.slot_phys;
1669 npages = memslot->npages;
1672 for (j = 0; j < npages; j++) {
1673 if (!(physp[j] & KVMPPC_GOT_PAGE))
1675 pfn = physp[j] >> PAGE_SHIFT;
1676 page = pfn_to_page(pfn);
1682 void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1683 struct kvm_memory_slot *dont)
1685 if (!dont || free->arch.rmap != dont->arch.rmap) {
1686 vfree(free->arch.rmap);
1687 free->arch.rmap = NULL;
1689 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1691 vfree(free->arch.slot_phys);
1692 free->arch.slot_phys = NULL;
1696 int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1697 unsigned long npages)
1699 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1700 if (!slot->arch.rmap)
1702 slot->arch.slot_phys = NULL;
1707 int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1708 struct kvm_memory_slot *memslot,
1709 struct kvm_userspace_memory_region *mem)
1711 unsigned long *phys;
1713 /* Allocate a slot_phys array if needed */
1714 phys = memslot->arch.slot_phys;
1715 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1716 phys = vzalloc(memslot->npages * sizeof(unsigned long));
1719 memslot->arch.slot_phys = phys;
1725 void kvmppc_core_commit_memory_region(struct kvm *kvm,
1726 struct kvm_userspace_memory_region *mem,
1727 const struct kvm_memory_slot *old)
1729 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1730 struct kvm_memory_slot *memslot;
1732 if (npages && old->npages) {
1734 * If modifying a memslot, reset all the rmap dirty bits.
1735 * If this is a new memslot, we don't need to do anything
1736 * since the rmap array starts out as all zeroes,
1737 * i.e. no pages are dirty.
1739 memslot = id_to_memslot(kvm->memslots, mem->slot);
1740 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1744 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
1747 struct kvm *kvm = vcpu->kvm;
1748 struct kvm_rma_info *ri = NULL;
1750 struct kvm_memory_slot *memslot;
1751 struct vm_area_struct *vma;
1752 unsigned long lpcr, senc;
1753 unsigned long psize, porder;
1754 unsigned long rma_size;
1756 unsigned long *physp;
1757 unsigned long i, npages;
1760 mutex_lock(&kvm->lock);
1761 if (kvm->arch.rma_setup_done)
1762 goto out; /* another vcpu beat us to it */
1764 /* Allocate hashed page table (if not done already) and reset it */
1765 if (!kvm->arch.hpt_virt) {
1766 err = kvmppc_alloc_hpt(kvm, NULL);
1768 pr_err("KVM: Couldn't alloc HPT\n");
1773 /* Look up the memslot for guest physical address 0 */
1774 srcu_idx = srcu_read_lock(&kvm->srcu);
1775 memslot = gfn_to_memslot(kvm, 0);
1777 /* We must have some memory at 0 by now */
1779 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
1782 /* Look up the VMA for the start of this memory slot */
1783 hva = memslot->userspace_addr;
1784 down_read(¤t->mm->mmap_sem);
1785 vma = find_vma(current->mm, hva);
1786 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
1789 psize = vma_kernel_pagesize(vma);
1790 porder = __ilog2(psize);
1792 /* Is this one of our preallocated RMAs? */
1793 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
1794 hva == vma->vm_start)
1795 ri = vma->vm_file->private_data;
1797 up_read(¤t->mm->mmap_sem);
1800 /* On POWER7, use VRMA; on PPC970, give up */
1802 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1803 pr_err("KVM: CPU requires an RMO\n");
1807 /* We can handle 4k, 64k or 16M pages in the VRMA */
1809 if (!(psize == 0x1000 || psize == 0x10000 ||
1810 psize == 0x1000000))
1813 /* Update VRMASD field in the LPCR */
1814 senc = slb_pgsize_encoding(psize);
1815 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1816 (VRMA_VSID << SLB_VSID_SHIFT_1T);
1817 lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
1818 lpcr |= senc << (LPCR_VRMASD_SH - 4);
1819 kvm->arch.lpcr = lpcr;
1821 /* Create HPTEs in the hash page table for the VRMA */
1822 kvmppc_map_vrma(vcpu, memslot, porder);
1825 /* Set up to use an RMO region */
1826 rma_size = kvm_rma_pages;
1827 if (rma_size > memslot->npages)
1828 rma_size = memslot->npages;
1829 rma_size <<= PAGE_SHIFT;
1830 rmls = lpcr_rmls(rma_size);
1832 if ((long)rmls < 0) {
1833 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
1836 atomic_inc(&ri->use_count);
1839 /* Update LPCR and RMOR */
1840 lpcr = kvm->arch.lpcr;
1841 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1842 /* PPC970; insert RMLS value (split field) in HID4 */
1843 lpcr &= ~((1ul << HID4_RMLS0_SH) |
1844 (3ul << HID4_RMLS2_SH));
1845 lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
1846 ((rmls & 3) << HID4_RMLS2_SH);
1847 /* RMOR is also in HID4 */
1848 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
1852 lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
1853 lpcr |= rmls << LPCR_RMLS_SH;
1854 kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
1856 kvm->arch.lpcr = lpcr;
1857 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
1858 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
1860 /* Initialize phys addrs of pages in RMO */
1861 npages = kvm_rma_pages;
1862 porder = __ilog2(npages);
1863 physp = memslot->arch.slot_phys;
1865 if (npages > memslot->npages)
1866 npages = memslot->npages;
1867 spin_lock(&kvm->arch.slot_phys_lock);
1868 for (i = 0; i < npages; ++i)
1869 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
1871 spin_unlock(&kvm->arch.slot_phys_lock);
1875 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
1877 kvm->arch.rma_setup_done = 1;
1880 srcu_read_unlock(&kvm->srcu, srcu_idx);
1882 mutex_unlock(&kvm->lock);
1886 up_read(¤t->mm->mmap_sem);
1890 int kvmppc_core_init_vm(struct kvm *kvm)
1892 unsigned long lpcr, lpid;
1894 /* Allocate the guest's logical partition ID */
1896 lpid = kvmppc_alloc_lpid();
1899 kvm->arch.lpid = lpid;
1902 * Since we don't flush the TLB when tearing down a VM,
1903 * and this lpid might have previously been used,
1904 * make sure we flush on each core before running the new VM.
1906 cpumask_setall(&kvm->arch.need_tlb_flush);
1908 INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
1909 INIT_LIST_HEAD(&kvm->arch.rtas_tokens);
1911 kvm->arch.rma = NULL;
1913 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
1915 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1916 /* PPC970; HID4 is effectively the LPCR */
1917 kvm->arch.host_lpid = 0;
1918 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
1919 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
1920 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
1921 ((lpid & 0xf) << HID4_LPID5_SH);
1923 /* POWER7; init LPCR for virtual RMA mode */
1924 kvm->arch.host_lpid = mfspr(SPRN_LPID);
1925 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
1926 lpcr &= LPCR_PECE | LPCR_LPES;
1927 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
1928 LPCR_VPM0 | LPCR_VPM1;
1929 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
1930 (VRMA_VSID << SLB_VSID_SHIFT_1T);
1932 kvm->arch.lpcr = lpcr;
1934 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
1935 spin_lock_init(&kvm->arch.slot_phys_lock);
1938 * Don't allow secondary CPU threads to come online
1939 * while any KVM VMs exist.
1941 inhibit_secondary_onlining();
1946 void kvmppc_core_destroy_vm(struct kvm *kvm)
1948 uninhibit_secondary_onlining();
1950 if (kvm->arch.rma) {
1951 kvm_release_rma(kvm->arch.rma);
1952 kvm->arch.rma = NULL;
1955 kvmppc_rtas_tokens_free(kvm);
1957 kvmppc_free_hpt(kvm);
1958 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
1961 /* These are stubs for now */
1962 void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
1966 /* We don't need to emulate any privileged instructions or dcbz */
1967 int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
1968 unsigned int inst, int *advance)
1970 return EMULATE_FAIL;
1973 int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
1975 return EMULATE_FAIL;
1978 int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
1980 return EMULATE_FAIL;
1983 static int kvmppc_book3s_hv_init(void)
1987 r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1992 r = kvmppc_mmu_hv_init();
1997 static void kvmppc_book3s_hv_exit(void)
2002 module_init(kvmppc_book3s_hv_init);
2003 module_exit(kvmppc_book3s_hv_exit);