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>
34 #include <linux/miscdevice.h>
37 #include <asm/cputable.h>
38 #include <asm/cache.h>
39 #include <asm/cacheflush.h>
40 #include <asm/tlbflush.h>
41 #include <asm/uaccess.h>
43 #include <asm/kvm_ppc.h>
44 #include <asm/kvm_book3s.h>
45 #include <asm/mmu_context.h>
46 #include <asm/lppaca.h>
47 #include <asm/processor.h>
48 #include <asm/cputhreads.h>
50 #include <asm/hvcall.h>
51 #include <asm/switch_to.h>
53 #include <linux/gfp.h>
54 #include <linux/vmalloc.h>
55 #include <linux/highmem.h>
56 #include <linux/hugetlb.h>
57 #include <linux/module.h>
61 #define CREATE_TRACE_POINTS
64 /* #define EXIT_DEBUG */
65 /* #define EXIT_DEBUG_SIMPLE */
66 /* #define EXIT_DEBUG_INT */
68 /* Used to indicate that a guest page fault needs to be handled */
69 #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
71 /* Used as a "null" value for timebase values */
72 #define TB_NIL (~(u64)0)
74 static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
76 #if defined(CONFIG_PPC_64K_PAGES)
77 #define MPP_BUFFER_ORDER 0
78 #elif defined(CONFIG_PPC_4K_PAGES)
79 #define MPP_BUFFER_ORDER 3
83 static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
84 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
86 static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
90 wait_queue_head_t *wqp;
92 wqp = kvm_arch_vcpu_wq(vcpu);
93 if (waitqueue_active(wqp)) {
94 wake_up_interruptible(wqp);
95 ++vcpu->stat.halt_wakeup;
100 /* CPU points to the first thread of the core */
101 if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
102 #ifdef CONFIG_PPC_ICP_NATIVE
103 int real_cpu = cpu + vcpu->arch.ptid;
104 if (paca[real_cpu].kvm_hstate.xics_phys)
105 xics_wake_cpu(real_cpu);
109 smp_send_reschedule(cpu);
115 * We use the vcpu_load/put functions to measure stolen time.
116 * Stolen time is counted as time when either the vcpu is able to
117 * run as part of a virtual core, but the task running the vcore
118 * is preempted or sleeping, or when the vcpu needs something done
119 * in the kernel by the task running the vcpu, but that task is
120 * preempted or sleeping. Those two things have to be counted
121 * separately, since one of the vcpu tasks will take on the job
122 * of running the core, and the other vcpu tasks in the vcore will
123 * sleep waiting for it to do that, but that sleep shouldn't count
126 * Hence we accumulate stolen time when the vcpu can run as part of
127 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
128 * needs its task to do other things in the kernel (for example,
129 * service a page fault) in busy_stolen. We don't accumulate
130 * stolen time for a vcore when it is inactive, or for a vcpu
131 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
132 * a misnomer; it means that the vcpu task is not executing in
133 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
134 * the kernel. We don't have any way of dividing up that time
135 * between time that the vcpu is genuinely stopped, time that
136 * the task is actively working on behalf of the vcpu, and time
137 * that the task is preempted, so we don't count any of it as
140 * Updates to busy_stolen are protected by arch.tbacct_lock;
141 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
142 * lock. The stolen times are measured in units of timebase ticks.
143 * (Note that the != TB_NIL checks below are purely defensive;
144 * they should never fail.)
147 static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
149 struct kvmppc_vcore *vc = vcpu->arch.vcore;
153 * We can test vc->runner without taking the vcore lock,
154 * because only this task ever sets vc->runner to this
155 * vcpu, and once it is set to this vcpu, only this task
156 * ever sets it to NULL.
158 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE) {
159 spin_lock_irqsave(&vc->stoltb_lock, flags);
160 if (vc->preempt_tb != TB_NIL) {
161 vc->stolen_tb += mftb() - vc->preempt_tb;
162 vc->preempt_tb = TB_NIL;
164 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
166 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
167 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
168 vcpu->arch.busy_preempt != TB_NIL) {
169 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
170 vcpu->arch.busy_preempt = TB_NIL;
172 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
175 static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
177 struct kvmppc_vcore *vc = vcpu->arch.vcore;
180 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE) {
181 spin_lock_irqsave(&vc->stoltb_lock, flags);
182 vc->preempt_tb = mftb();
183 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
185 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
186 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
187 vcpu->arch.busy_preempt = mftb();
188 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
191 static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
193 vcpu->arch.shregs.msr = msr;
194 kvmppc_end_cede(vcpu);
197 void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
199 vcpu->arch.pvr = pvr;
202 int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
204 unsigned long pcr = 0;
205 struct kvmppc_vcore *vc = vcpu->arch.vcore;
208 switch (arch_compat) {
211 * If an arch bit is set in PCR, all the defined
212 * higher-order arch bits also have to be set.
214 pcr = PCR_ARCH_206 | PCR_ARCH_205;
226 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
227 /* POWER7 can't emulate POWER8 */
228 if (!(pcr & PCR_ARCH_206))
230 pcr &= ~PCR_ARCH_206;
234 spin_lock(&vc->lock);
235 vc->arch_compat = arch_compat;
237 spin_unlock(&vc->lock);
242 void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
246 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
247 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
248 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
249 for (r = 0; r < 16; ++r)
250 pr_err("r%2d = %.16lx r%d = %.16lx\n",
251 r, kvmppc_get_gpr(vcpu, r),
252 r+16, kvmppc_get_gpr(vcpu, r+16));
253 pr_err("ctr = %.16lx lr = %.16lx\n",
254 vcpu->arch.ctr, vcpu->arch.lr);
255 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
256 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
257 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
258 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
259 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
260 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
261 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
262 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
263 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
264 pr_err("fault dar = %.16lx dsisr = %.8x\n",
265 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
266 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
267 for (r = 0; r < vcpu->arch.slb_max; ++r)
268 pr_err(" ESID = %.16llx VSID = %.16llx\n",
269 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
270 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
271 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
272 vcpu->arch.last_inst);
275 struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
278 struct kvm_vcpu *v, *ret = NULL;
280 mutex_lock(&kvm->lock);
281 kvm_for_each_vcpu(r, v, kvm) {
282 if (v->vcpu_id == id) {
287 mutex_unlock(&kvm->lock);
291 static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
293 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
294 vpa->yield_count = cpu_to_be32(1);
297 static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
298 unsigned long addr, unsigned long len)
300 /* check address is cacheline aligned */
301 if (addr & (L1_CACHE_BYTES - 1))
303 spin_lock(&vcpu->arch.vpa_update_lock);
304 if (v->next_gpa != addr || v->len != len) {
306 v->len = addr ? len : 0;
307 v->update_pending = 1;
309 spin_unlock(&vcpu->arch.vpa_update_lock);
313 /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
322 static int vpa_is_registered(struct kvmppc_vpa *vpap)
324 if (vpap->update_pending)
325 return vpap->next_gpa != 0;
326 return vpap->pinned_addr != NULL;
329 static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
331 unsigned long vcpuid, unsigned long vpa)
333 struct kvm *kvm = vcpu->kvm;
334 unsigned long len, nb;
336 struct kvm_vcpu *tvcpu;
339 struct kvmppc_vpa *vpap;
341 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
345 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
346 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
347 subfunc == H_VPA_REG_SLB) {
348 /* Registering new area - address must be cache-line aligned */
349 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
352 /* convert logical addr to kernel addr and read length */
353 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
356 if (subfunc == H_VPA_REG_VPA)
357 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
359 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
360 kvmppc_unpin_guest_page(kvm, va, vpa, false);
363 if (len > nb || len < sizeof(struct reg_vpa))
372 spin_lock(&tvcpu->arch.vpa_update_lock);
375 case H_VPA_REG_VPA: /* register VPA */
376 if (len < sizeof(struct lppaca))
378 vpap = &tvcpu->arch.vpa;
382 case H_VPA_REG_DTL: /* register DTL */
383 if (len < sizeof(struct dtl_entry))
385 len -= len % sizeof(struct dtl_entry);
387 /* Check that they have previously registered a VPA */
389 if (!vpa_is_registered(&tvcpu->arch.vpa))
392 vpap = &tvcpu->arch.dtl;
396 case H_VPA_REG_SLB: /* register SLB shadow buffer */
397 /* Check that they have previously registered a VPA */
399 if (!vpa_is_registered(&tvcpu->arch.vpa))
402 vpap = &tvcpu->arch.slb_shadow;
406 case H_VPA_DEREG_VPA: /* deregister VPA */
407 /* Check they don't still have a DTL or SLB buf registered */
409 if (vpa_is_registered(&tvcpu->arch.dtl) ||
410 vpa_is_registered(&tvcpu->arch.slb_shadow))
413 vpap = &tvcpu->arch.vpa;
417 case H_VPA_DEREG_DTL: /* deregister DTL */
418 vpap = &tvcpu->arch.dtl;
422 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
423 vpap = &tvcpu->arch.slb_shadow;
429 vpap->next_gpa = vpa;
431 vpap->update_pending = 1;
434 spin_unlock(&tvcpu->arch.vpa_update_lock);
439 static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
441 struct kvm *kvm = vcpu->kvm;
447 * We need to pin the page pointed to by vpap->next_gpa,
448 * but we can't call kvmppc_pin_guest_page under the lock
449 * as it does get_user_pages() and down_read(). So we
450 * have to drop the lock, pin the page, then get the lock
451 * again and check that a new area didn't get registered
455 gpa = vpap->next_gpa;
456 spin_unlock(&vcpu->arch.vpa_update_lock);
460 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
461 spin_lock(&vcpu->arch.vpa_update_lock);
462 if (gpa == vpap->next_gpa)
464 /* sigh... unpin that one and try again */
466 kvmppc_unpin_guest_page(kvm, va, gpa, false);
469 vpap->update_pending = 0;
470 if (va && nb < vpap->len) {
472 * If it's now too short, it must be that userspace
473 * has changed the mappings underlying guest memory,
474 * so unregister the region.
476 kvmppc_unpin_guest_page(kvm, va, gpa, false);
479 if (vpap->pinned_addr)
480 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
483 vpap->pinned_addr = va;
486 vpap->pinned_end = va + vpap->len;
489 static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
491 if (!(vcpu->arch.vpa.update_pending ||
492 vcpu->arch.slb_shadow.update_pending ||
493 vcpu->arch.dtl.update_pending))
496 spin_lock(&vcpu->arch.vpa_update_lock);
497 if (vcpu->arch.vpa.update_pending) {
498 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
499 if (vcpu->arch.vpa.pinned_addr)
500 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
502 if (vcpu->arch.dtl.update_pending) {
503 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
504 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
505 vcpu->arch.dtl_index = 0;
507 if (vcpu->arch.slb_shadow.update_pending)
508 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
509 spin_unlock(&vcpu->arch.vpa_update_lock);
513 * Return the accumulated stolen time for the vcore up until `now'.
514 * The caller should hold the vcore lock.
516 static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
521 spin_lock_irqsave(&vc->stoltb_lock, flags);
523 if (vc->vcore_state != VCORE_INACTIVE &&
524 vc->preempt_tb != TB_NIL)
525 p += now - vc->preempt_tb;
526 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
530 static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
531 struct kvmppc_vcore *vc)
533 struct dtl_entry *dt;
535 unsigned long stolen;
536 unsigned long core_stolen;
539 dt = vcpu->arch.dtl_ptr;
540 vpa = vcpu->arch.vpa.pinned_addr;
542 core_stolen = vcore_stolen_time(vc, now);
543 stolen = core_stolen - vcpu->arch.stolen_logged;
544 vcpu->arch.stolen_logged = core_stolen;
545 spin_lock_irq(&vcpu->arch.tbacct_lock);
546 stolen += vcpu->arch.busy_stolen;
547 vcpu->arch.busy_stolen = 0;
548 spin_unlock_irq(&vcpu->arch.tbacct_lock);
551 memset(dt, 0, sizeof(struct dtl_entry));
552 dt->dispatch_reason = 7;
553 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
554 dt->timebase = cpu_to_be64(now + vc->tb_offset);
555 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
556 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
557 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
559 if (dt == vcpu->arch.dtl.pinned_end)
560 dt = vcpu->arch.dtl.pinned_addr;
561 vcpu->arch.dtl_ptr = dt;
562 /* order writing *dt vs. writing vpa->dtl_idx */
564 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
565 vcpu->arch.dtl.dirty = true;
568 static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
570 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
572 if ((!vcpu->arch.vcore->arch_compat) &&
573 cpu_has_feature(CPU_FTR_ARCH_207S))
578 static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
579 unsigned long resource, unsigned long value1,
580 unsigned long value2)
583 case H_SET_MODE_RESOURCE_SET_CIABR:
584 if (!kvmppc_power8_compatible(vcpu))
589 return H_UNSUPPORTED_FLAG_START;
590 /* Guests can't breakpoint the hypervisor */
591 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
593 vcpu->arch.ciabr = value1;
595 case H_SET_MODE_RESOURCE_SET_DAWR:
596 if (!kvmppc_power8_compatible(vcpu))
599 return H_UNSUPPORTED_FLAG_START;
600 if (value2 & DABRX_HYP)
602 vcpu->arch.dawr = value1;
603 vcpu->arch.dawrx = value2;
610 static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
612 struct kvmppc_vcore *vcore = target->arch.vcore;
615 * We expect to have been called by the real mode handler
616 * (kvmppc_rm_h_confer()) which would have directly returned
617 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
618 * have useful work to do and should not confer) so we don't
622 spin_lock(&vcore->lock);
623 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
624 vcore->vcore_state != VCORE_INACTIVE)
625 target = vcore->runner;
626 spin_unlock(&vcore->lock);
628 return kvm_vcpu_yield_to(target);
631 static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
634 struct lppaca *lppaca;
636 spin_lock(&vcpu->arch.vpa_update_lock);
637 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
639 yield_count = be32_to_cpu(lppaca->yield_count);
640 spin_unlock(&vcpu->arch.vpa_update_lock);
644 int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
646 unsigned long req = kvmppc_get_gpr(vcpu, 3);
647 unsigned long target, ret = H_SUCCESS;
649 struct kvm_vcpu *tvcpu;
652 if (req <= MAX_HCALL_OPCODE &&
653 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
660 target = kvmppc_get_gpr(vcpu, 4);
661 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
666 tvcpu->arch.prodded = 1;
668 if (vcpu->arch.ceded) {
669 if (waitqueue_active(&vcpu->wq)) {
670 wake_up_interruptible(&vcpu->wq);
671 vcpu->stat.halt_wakeup++;
676 target = kvmppc_get_gpr(vcpu, 4);
679 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
684 yield_count = kvmppc_get_gpr(vcpu, 5);
685 if (kvmppc_get_yield_count(tvcpu) != yield_count)
687 kvm_arch_vcpu_yield_to(tvcpu);
690 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
691 kvmppc_get_gpr(vcpu, 5),
692 kvmppc_get_gpr(vcpu, 6));
695 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
698 idx = srcu_read_lock(&vcpu->kvm->srcu);
699 rc = kvmppc_rtas_hcall(vcpu);
700 srcu_read_unlock(&vcpu->kvm->srcu, idx);
707 /* Send the error out to userspace via KVM_RUN */
709 case H_LOGICAL_CI_LOAD:
710 ret = kvmppc_h_logical_ci_load(vcpu);
711 if (ret == H_TOO_HARD)
714 case H_LOGICAL_CI_STORE:
715 ret = kvmppc_h_logical_ci_store(vcpu);
716 if (ret == H_TOO_HARD)
720 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
721 kvmppc_get_gpr(vcpu, 5),
722 kvmppc_get_gpr(vcpu, 6),
723 kvmppc_get_gpr(vcpu, 7));
724 if (ret == H_TOO_HARD)
733 if (kvmppc_xics_enabled(vcpu)) {
734 ret = kvmppc_xics_hcall(vcpu, req);
740 kvmppc_set_gpr(vcpu, 3, ret);
741 vcpu->arch.hcall_needed = 0;
745 static int kvmppc_hcall_impl_hv(unsigned long cmd)
753 case H_LOGICAL_CI_LOAD:
754 case H_LOGICAL_CI_STORE:
755 #ifdef CONFIG_KVM_XICS
766 /* See if it's in the real-mode table */
767 return kvmppc_hcall_impl_hv_realmode(cmd);
770 static int kvmppc_emulate_debug_inst(struct kvm_run *run,
771 struct kvm_vcpu *vcpu)
775 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
778 * Fetch failed, so return to guest and
779 * try executing it again.
784 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
785 run->exit_reason = KVM_EXIT_DEBUG;
786 run->debug.arch.address = kvmppc_get_pc(vcpu);
789 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
794 static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
795 struct task_struct *tsk)
799 vcpu->stat.sum_exits++;
801 run->exit_reason = KVM_EXIT_UNKNOWN;
802 run->ready_for_interrupt_injection = 1;
803 switch (vcpu->arch.trap) {
804 /* We're good on these - the host merely wanted to get our attention */
805 case BOOK3S_INTERRUPT_HV_DECREMENTER:
806 vcpu->stat.dec_exits++;
809 case BOOK3S_INTERRUPT_EXTERNAL:
810 case BOOK3S_INTERRUPT_H_DOORBELL:
811 vcpu->stat.ext_intr_exits++;
814 /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
815 case BOOK3S_INTERRUPT_HMI:
816 case BOOK3S_INTERRUPT_PERFMON:
819 case BOOK3S_INTERRUPT_MACHINE_CHECK:
821 * Deliver a machine check interrupt to the guest.
822 * We have to do this, even if the host has handled the
823 * machine check, because machine checks use SRR0/1 and
824 * the interrupt might have trashed guest state in them.
826 kvmppc_book3s_queue_irqprio(vcpu,
827 BOOK3S_INTERRUPT_MACHINE_CHECK);
830 case BOOK3S_INTERRUPT_PROGRAM:
834 * Normally program interrupts are delivered directly
835 * to the guest by the hardware, but we can get here
836 * as a result of a hypervisor emulation interrupt
837 * (e40) getting turned into a 700 by BML RTAS.
839 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
840 kvmppc_core_queue_program(vcpu, flags);
844 case BOOK3S_INTERRUPT_SYSCALL:
846 /* hcall - punt to userspace */
849 /* hypercall with MSR_PR has already been handled in rmode,
850 * and never reaches here.
853 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
854 for (i = 0; i < 9; ++i)
855 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
856 run->exit_reason = KVM_EXIT_PAPR_HCALL;
857 vcpu->arch.hcall_needed = 1;
862 * We get these next two if the guest accesses a page which it thinks
863 * it has mapped but which is not actually present, either because
864 * it is for an emulated I/O device or because the corresonding
865 * host page has been paged out. Any other HDSI/HISI interrupts
866 * have been handled already.
868 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
869 r = RESUME_PAGE_FAULT;
871 case BOOK3S_INTERRUPT_H_INST_STORAGE:
872 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
873 vcpu->arch.fault_dsisr = 0;
874 r = RESUME_PAGE_FAULT;
877 * This occurs if the guest executes an illegal instruction.
878 * If the guest debug is disabled, generate a program interrupt
879 * to the guest. If guest debug is enabled, we need to check
880 * whether the instruction is a software breakpoint instruction.
881 * Accordingly return to Guest or Host.
883 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
884 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
885 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
886 swab32(vcpu->arch.emul_inst) :
887 vcpu->arch.emul_inst;
888 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
889 r = kvmppc_emulate_debug_inst(run, vcpu);
891 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
896 * This occurs if the guest (kernel or userspace), does something that
897 * is prohibited by HFSCR. We just generate a program interrupt to
900 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
901 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
905 kvmppc_dump_regs(vcpu);
906 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
907 vcpu->arch.trap, kvmppc_get_pc(vcpu),
908 vcpu->arch.shregs.msr);
909 run->hw.hardware_exit_reason = vcpu->arch.trap;
917 static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
918 struct kvm_sregs *sregs)
922 memset(sregs, 0, sizeof(struct kvm_sregs));
923 sregs->pvr = vcpu->arch.pvr;
924 for (i = 0; i < vcpu->arch.slb_max; i++) {
925 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
926 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
932 static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
933 struct kvm_sregs *sregs)
937 /* Only accept the same PVR as the host's, since we can't spoof it */
938 if (sregs->pvr != vcpu->arch.pvr)
942 for (i = 0; i < vcpu->arch.slb_nr; i++) {
943 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
944 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
945 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
949 vcpu->arch.slb_max = j;
954 static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
957 struct kvm *kvm = vcpu->kvm;
958 struct kvmppc_vcore *vc = vcpu->arch.vcore;
961 mutex_lock(&kvm->lock);
962 spin_lock(&vc->lock);
964 * If ILE (interrupt little-endian) has changed, update the
965 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
967 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
968 struct kvm_vcpu *vcpu;
971 kvm_for_each_vcpu(i, vcpu, kvm) {
972 if (vcpu->arch.vcore != vc)
974 if (new_lpcr & LPCR_ILE)
975 vcpu->arch.intr_msr |= MSR_LE;
977 vcpu->arch.intr_msr &= ~MSR_LE;
982 * Userspace can only modify DPFD (default prefetch depth),
983 * ILE (interrupt little-endian) and TC (translation control).
984 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
986 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
987 if (cpu_has_feature(CPU_FTR_ARCH_207S))
990 /* Broken 32-bit version of LPCR must not clear top bits */
993 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
994 spin_unlock(&vc->lock);
995 mutex_unlock(&kvm->lock);
998 static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
999 union kvmppc_one_reg *val)
1005 case KVM_REG_PPC_DEBUG_INST:
1006 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
1008 case KVM_REG_PPC_HIOR:
1009 *val = get_reg_val(id, 0);
1011 case KVM_REG_PPC_DABR:
1012 *val = get_reg_val(id, vcpu->arch.dabr);
1014 case KVM_REG_PPC_DABRX:
1015 *val = get_reg_val(id, vcpu->arch.dabrx);
1017 case KVM_REG_PPC_DSCR:
1018 *val = get_reg_val(id, vcpu->arch.dscr);
1020 case KVM_REG_PPC_PURR:
1021 *val = get_reg_val(id, vcpu->arch.purr);
1023 case KVM_REG_PPC_SPURR:
1024 *val = get_reg_val(id, vcpu->arch.spurr);
1026 case KVM_REG_PPC_AMR:
1027 *val = get_reg_val(id, vcpu->arch.amr);
1029 case KVM_REG_PPC_UAMOR:
1030 *val = get_reg_val(id, vcpu->arch.uamor);
1032 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
1033 i = id - KVM_REG_PPC_MMCR0;
1034 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
1036 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1037 i = id - KVM_REG_PPC_PMC1;
1038 *val = get_reg_val(id, vcpu->arch.pmc[i]);
1040 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1041 i = id - KVM_REG_PPC_SPMC1;
1042 *val = get_reg_val(id, vcpu->arch.spmc[i]);
1044 case KVM_REG_PPC_SIAR:
1045 *val = get_reg_val(id, vcpu->arch.siar);
1047 case KVM_REG_PPC_SDAR:
1048 *val = get_reg_val(id, vcpu->arch.sdar);
1050 case KVM_REG_PPC_SIER:
1051 *val = get_reg_val(id, vcpu->arch.sier);
1053 case KVM_REG_PPC_IAMR:
1054 *val = get_reg_val(id, vcpu->arch.iamr);
1056 case KVM_REG_PPC_PSPB:
1057 *val = get_reg_val(id, vcpu->arch.pspb);
1059 case KVM_REG_PPC_DPDES:
1060 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1062 case KVM_REG_PPC_DAWR:
1063 *val = get_reg_val(id, vcpu->arch.dawr);
1065 case KVM_REG_PPC_DAWRX:
1066 *val = get_reg_val(id, vcpu->arch.dawrx);
1068 case KVM_REG_PPC_CIABR:
1069 *val = get_reg_val(id, vcpu->arch.ciabr);
1071 case KVM_REG_PPC_CSIGR:
1072 *val = get_reg_val(id, vcpu->arch.csigr);
1074 case KVM_REG_PPC_TACR:
1075 *val = get_reg_val(id, vcpu->arch.tacr);
1077 case KVM_REG_PPC_TCSCR:
1078 *val = get_reg_val(id, vcpu->arch.tcscr);
1080 case KVM_REG_PPC_PID:
1081 *val = get_reg_val(id, vcpu->arch.pid);
1083 case KVM_REG_PPC_ACOP:
1084 *val = get_reg_val(id, vcpu->arch.acop);
1086 case KVM_REG_PPC_WORT:
1087 *val = get_reg_val(id, vcpu->arch.wort);
1089 case KVM_REG_PPC_VPA_ADDR:
1090 spin_lock(&vcpu->arch.vpa_update_lock);
1091 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1092 spin_unlock(&vcpu->arch.vpa_update_lock);
1094 case KVM_REG_PPC_VPA_SLB:
1095 spin_lock(&vcpu->arch.vpa_update_lock);
1096 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1097 val->vpaval.length = vcpu->arch.slb_shadow.len;
1098 spin_unlock(&vcpu->arch.vpa_update_lock);
1100 case KVM_REG_PPC_VPA_DTL:
1101 spin_lock(&vcpu->arch.vpa_update_lock);
1102 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1103 val->vpaval.length = vcpu->arch.dtl.len;
1104 spin_unlock(&vcpu->arch.vpa_update_lock);
1106 case KVM_REG_PPC_TB_OFFSET:
1107 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1109 case KVM_REG_PPC_LPCR:
1110 case KVM_REG_PPC_LPCR_64:
1111 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1113 case KVM_REG_PPC_PPR:
1114 *val = get_reg_val(id, vcpu->arch.ppr);
1116 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1117 case KVM_REG_PPC_TFHAR:
1118 *val = get_reg_val(id, vcpu->arch.tfhar);
1120 case KVM_REG_PPC_TFIAR:
1121 *val = get_reg_val(id, vcpu->arch.tfiar);
1123 case KVM_REG_PPC_TEXASR:
1124 *val = get_reg_val(id, vcpu->arch.texasr);
1126 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1127 i = id - KVM_REG_PPC_TM_GPR0;
1128 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1130 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1133 i = id - KVM_REG_PPC_TM_VSR0;
1135 for (j = 0; j < TS_FPRWIDTH; j++)
1136 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1138 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1139 val->vval = vcpu->arch.vr_tm.vr[i-32];
1145 case KVM_REG_PPC_TM_CR:
1146 *val = get_reg_val(id, vcpu->arch.cr_tm);
1148 case KVM_REG_PPC_TM_LR:
1149 *val = get_reg_val(id, vcpu->arch.lr_tm);
1151 case KVM_REG_PPC_TM_CTR:
1152 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1154 case KVM_REG_PPC_TM_FPSCR:
1155 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1157 case KVM_REG_PPC_TM_AMR:
1158 *val = get_reg_val(id, vcpu->arch.amr_tm);
1160 case KVM_REG_PPC_TM_PPR:
1161 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1163 case KVM_REG_PPC_TM_VRSAVE:
1164 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1166 case KVM_REG_PPC_TM_VSCR:
1167 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1168 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1172 case KVM_REG_PPC_TM_DSCR:
1173 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1175 case KVM_REG_PPC_TM_TAR:
1176 *val = get_reg_val(id, vcpu->arch.tar_tm);
1179 case KVM_REG_PPC_ARCH_COMPAT:
1180 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1190 static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1191 union kvmppc_one_reg *val)
1195 unsigned long addr, len;
1198 case KVM_REG_PPC_HIOR:
1199 /* Only allow this to be set to zero */
1200 if (set_reg_val(id, *val))
1203 case KVM_REG_PPC_DABR:
1204 vcpu->arch.dabr = set_reg_val(id, *val);
1206 case KVM_REG_PPC_DABRX:
1207 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1209 case KVM_REG_PPC_DSCR:
1210 vcpu->arch.dscr = set_reg_val(id, *val);
1212 case KVM_REG_PPC_PURR:
1213 vcpu->arch.purr = set_reg_val(id, *val);
1215 case KVM_REG_PPC_SPURR:
1216 vcpu->arch.spurr = set_reg_val(id, *val);
1218 case KVM_REG_PPC_AMR:
1219 vcpu->arch.amr = set_reg_val(id, *val);
1221 case KVM_REG_PPC_UAMOR:
1222 vcpu->arch.uamor = set_reg_val(id, *val);
1224 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
1225 i = id - KVM_REG_PPC_MMCR0;
1226 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1228 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1229 i = id - KVM_REG_PPC_PMC1;
1230 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1232 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1233 i = id - KVM_REG_PPC_SPMC1;
1234 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1236 case KVM_REG_PPC_SIAR:
1237 vcpu->arch.siar = set_reg_val(id, *val);
1239 case KVM_REG_PPC_SDAR:
1240 vcpu->arch.sdar = set_reg_val(id, *val);
1242 case KVM_REG_PPC_SIER:
1243 vcpu->arch.sier = set_reg_val(id, *val);
1245 case KVM_REG_PPC_IAMR:
1246 vcpu->arch.iamr = set_reg_val(id, *val);
1248 case KVM_REG_PPC_PSPB:
1249 vcpu->arch.pspb = set_reg_val(id, *val);
1251 case KVM_REG_PPC_DPDES:
1252 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1254 case KVM_REG_PPC_DAWR:
1255 vcpu->arch.dawr = set_reg_val(id, *val);
1257 case KVM_REG_PPC_DAWRX:
1258 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1260 case KVM_REG_PPC_CIABR:
1261 vcpu->arch.ciabr = set_reg_val(id, *val);
1262 /* Don't allow setting breakpoints in hypervisor code */
1263 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1264 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1266 case KVM_REG_PPC_CSIGR:
1267 vcpu->arch.csigr = set_reg_val(id, *val);
1269 case KVM_REG_PPC_TACR:
1270 vcpu->arch.tacr = set_reg_val(id, *val);
1272 case KVM_REG_PPC_TCSCR:
1273 vcpu->arch.tcscr = set_reg_val(id, *val);
1275 case KVM_REG_PPC_PID:
1276 vcpu->arch.pid = set_reg_val(id, *val);
1278 case KVM_REG_PPC_ACOP:
1279 vcpu->arch.acop = set_reg_val(id, *val);
1281 case KVM_REG_PPC_WORT:
1282 vcpu->arch.wort = set_reg_val(id, *val);
1284 case KVM_REG_PPC_VPA_ADDR:
1285 addr = set_reg_val(id, *val);
1287 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1288 vcpu->arch.dtl.next_gpa))
1290 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1292 case KVM_REG_PPC_VPA_SLB:
1293 addr = val->vpaval.addr;
1294 len = val->vpaval.length;
1296 if (addr && !vcpu->arch.vpa.next_gpa)
1298 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1300 case KVM_REG_PPC_VPA_DTL:
1301 addr = val->vpaval.addr;
1302 len = val->vpaval.length;
1304 if (addr && (len < sizeof(struct dtl_entry) ||
1305 !vcpu->arch.vpa.next_gpa))
1307 len -= len % sizeof(struct dtl_entry);
1308 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1310 case KVM_REG_PPC_TB_OFFSET:
1311 /* round up to multiple of 2^24 */
1312 vcpu->arch.vcore->tb_offset =
1313 ALIGN(set_reg_val(id, *val), 1UL << 24);
1315 case KVM_REG_PPC_LPCR:
1316 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1318 case KVM_REG_PPC_LPCR_64:
1319 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
1321 case KVM_REG_PPC_PPR:
1322 vcpu->arch.ppr = set_reg_val(id, *val);
1324 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1325 case KVM_REG_PPC_TFHAR:
1326 vcpu->arch.tfhar = set_reg_val(id, *val);
1328 case KVM_REG_PPC_TFIAR:
1329 vcpu->arch.tfiar = set_reg_val(id, *val);
1331 case KVM_REG_PPC_TEXASR:
1332 vcpu->arch.texasr = set_reg_val(id, *val);
1334 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1335 i = id - KVM_REG_PPC_TM_GPR0;
1336 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1338 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1341 i = id - KVM_REG_PPC_TM_VSR0;
1343 for (j = 0; j < TS_FPRWIDTH; j++)
1344 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1346 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1347 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1352 case KVM_REG_PPC_TM_CR:
1353 vcpu->arch.cr_tm = set_reg_val(id, *val);
1355 case KVM_REG_PPC_TM_LR:
1356 vcpu->arch.lr_tm = set_reg_val(id, *val);
1358 case KVM_REG_PPC_TM_CTR:
1359 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1361 case KVM_REG_PPC_TM_FPSCR:
1362 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1364 case KVM_REG_PPC_TM_AMR:
1365 vcpu->arch.amr_tm = set_reg_val(id, *val);
1367 case KVM_REG_PPC_TM_PPR:
1368 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1370 case KVM_REG_PPC_TM_VRSAVE:
1371 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1373 case KVM_REG_PPC_TM_VSCR:
1374 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1375 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1379 case KVM_REG_PPC_TM_DSCR:
1380 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1382 case KVM_REG_PPC_TM_TAR:
1383 vcpu->arch.tar_tm = set_reg_val(id, *val);
1386 case KVM_REG_PPC_ARCH_COMPAT:
1387 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1397 static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1399 struct kvmppc_vcore *vcore;
1401 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1406 INIT_LIST_HEAD(&vcore->runnable_threads);
1407 spin_lock_init(&vcore->lock);
1408 spin_lock_init(&vcore->stoltb_lock);
1409 init_waitqueue_head(&vcore->wq);
1410 vcore->preempt_tb = TB_NIL;
1411 vcore->lpcr = kvm->arch.lpcr;
1412 vcore->first_vcpuid = core * threads_per_subcore;
1415 vcore->mpp_buffer_is_valid = false;
1417 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1418 vcore->mpp_buffer = (void *)__get_free_pages(
1419 GFP_KERNEL|__GFP_ZERO,
1425 static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1428 struct kvm_vcpu *vcpu;
1431 struct kvmppc_vcore *vcore;
1433 core = id / threads_per_subcore;
1434 if (core >= KVM_MAX_VCORES)
1438 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
1442 err = kvm_vcpu_init(vcpu, kvm, id);
1446 vcpu->arch.shared = &vcpu->arch.shregs;
1447 #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1449 * The shared struct is never shared on HV,
1450 * so we can always use host endianness
1452 #ifdef __BIG_ENDIAN__
1453 vcpu->arch.shared_big_endian = true;
1455 vcpu->arch.shared_big_endian = false;
1458 vcpu->arch.mmcr[0] = MMCR0_FC;
1459 vcpu->arch.ctrl = CTRL_RUNLATCH;
1460 /* default to host PVR, since we can't spoof it */
1461 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
1462 spin_lock_init(&vcpu->arch.vpa_update_lock);
1463 spin_lock_init(&vcpu->arch.tbacct_lock);
1464 vcpu->arch.busy_preempt = TB_NIL;
1465 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
1467 kvmppc_mmu_book3s_hv_init(vcpu);
1469 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
1471 init_waitqueue_head(&vcpu->arch.cpu_run);
1473 mutex_lock(&kvm->lock);
1474 vcore = kvm->arch.vcores[core];
1476 vcore = kvmppc_vcore_create(kvm, core);
1477 kvm->arch.vcores[core] = vcore;
1478 kvm->arch.online_vcores++;
1480 mutex_unlock(&kvm->lock);
1485 spin_lock(&vcore->lock);
1486 ++vcore->num_threads;
1487 spin_unlock(&vcore->lock);
1488 vcpu->arch.vcore = vcore;
1489 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
1491 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1492 kvmppc_sanity_check(vcpu);
1497 kmem_cache_free(kvm_vcpu_cache, vcpu);
1499 return ERR_PTR(err);
1502 static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1504 if (vpa->pinned_addr)
1505 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1509 static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
1511 spin_lock(&vcpu->arch.vpa_update_lock);
1512 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1513 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1514 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
1515 spin_unlock(&vcpu->arch.vpa_update_lock);
1516 kvm_vcpu_uninit(vcpu);
1517 kmem_cache_free(kvm_vcpu_cache, vcpu);
1520 static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1522 /* Indicate we want to get back into the guest */
1526 static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
1528 unsigned long dec_nsec, now;
1531 if (now > vcpu->arch.dec_expires) {
1532 /* decrementer has already gone negative */
1533 kvmppc_core_queue_dec(vcpu);
1534 kvmppc_core_prepare_to_enter(vcpu);
1537 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1539 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1541 vcpu->arch.timer_running = 1;
1544 static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
1546 vcpu->arch.ceded = 0;
1547 if (vcpu->arch.timer_running) {
1548 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1549 vcpu->arch.timer_running = 0;
1553 extern void __kvmppc_vcore_entry(void);
1555 static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1556 struct kvm_vcpu *vcpu)
1560 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1562 spin_lock_irq(&vcpu->arch.tbacct_lock);
1564 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1565 vcpu->arch.stolen_logged;
1566 vcpu->arch.busy_preempt = now;
1567 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1568 spin_unlock_irq(&vcpu->arch.tbacct_lock);
1570 list_del(&vcpu->arch.run_list);
1573 static int kvmppc_grab_hwthread(int cpu)
1575 struct paca_struct *tpaca;
1576 long timeout = 10000;
1580 /* Ensure the thread won't go into the kernel if it wakes */
1581 tpaca->kvm_hstate.hwthread_req = 1;
1582 tpaca->kvm_hstate.kvm_vcpu = NULL;
1585 * If the thread is already executing in the kernel (e.g. handling
1586 * a stray interrupt), wait for it to get back to nap mode.
1587 * The smp_mb() is to ensure that our setting of hwthread_req
1588 * is visible before we look at hwthread_state, so if this
1589 * races with the code at system_reset_pSeries and the thread
1590 * misses our setting of hwthread_req, we are sure to see its
1591 * setting of hwthread_state, and vice versa.
1594 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1595 if (--timeout <= 0) {
1596 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1604 static void kvmppc_release_hwthread(int cpu)
1606 struct paca_struct *tpaca;
1609 tpaca->kvm_hstate.hwthread_req = 0;
1610 tpaca->kvm_hstate.kvm_vcpu = NULL;
1613 static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1616 struct paca_struct *tpaca;
1617 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1619 if (vcpu->arch.timer_running) {
1620 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1621 vcpu->arch.timer_running = 0;
1623 cpu = vc->pcpu + vcpu->arch.ptid;
1625 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1626 tpaca->kvm_hstate.kvm_vcore = vc;
1627 tpaca->kvm_hstate.ptid = vcpu->arch.ptid;
1628 vcpu->cpu = vc->pcpu;
1630 #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
1631 if (cpu != smp_processor_id()) {
1633 if (vcpu->arch.ptid)
1639 static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1645 while (vc->nap_count < vc->n_woken) {
1646 if (++i >= 1000000) {
1647 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1648 vc->nap_count, vc->n_woken);
1657 * Check that we are on thread 0 and that any other threads in
1658 * this core are off-line. Then grab the threads so they can't
1661 static int on_primary_thread(void)
1663 int cpu = smp_processor_id();
1666 /* Are we on a primary subcore? */
1667 if (cpu_thread_in_subcore(cpu))
1671 while (++thr < threads_per_subcore)
1672 if (cpu_online(cpu + thr))
1675 /* Grab all hw threads so they can't go into the kernel */
1676 for (thr = 1; thr < threads_per_subcore; ++thr) {
1677 if (kvmppc_grab_hwthread(cpu + thr)) {
1678 /* Couldn't grab one; let the others go */
1680 kvmppc_release_hwthread(cpu + thr);
1681 } while (--thr > 0);
1688 static void kvmppc_start_saving_l2_cache(struct kvmppc_vcore *vc)
1690 phys_addr_t phy_addr, mpp_addr;
1692 phy_addr = (phys_addr_t)virt_to_phys(vc->mpp_buffer);
1693 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1695 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_ABORT);
1696 logmpp(mpp_addr | PPC_LOGMPP_LOG_L2);
1698 vc->mpp_buffer_is_valid = true;
1701 static void kvmppc_start_restoring_l2_cache(const struct kvmppc_vcore *vc)
1703 phys_addr_t phy_addr, mpp_addr;
1705 phy_addr = virt_to_phys(vc->mpp_buffer);
1706 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1708 /* We must abort any in-progress save operations to ensure
1709 * the table is valid so that prefetch engine knows when to
1710 * stop prefetching. */
1711 logmpp(mpp_addr | PPC_LOGMPP_LOG_ABORT);
1712 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_WHOLE_TABLE);
1716 * Run a set of guest threads on a physical core.
1717 * Called with vc->lock held.
1719 static void kvmppc_run_core(struct kvmppc_vcore *vc)
1721 struct kvm_vcpu *vcpu, *vnext;
1724 int i, need_vpa_update;
1726 struct kvm_vcpu *vcpus_to_update[threads_per_core];
1728 /* don't start if any threads have a signal pending */
1729 need_vpa_update = 0;
1730 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1731 if (signal_pending(vcpu->arch.run_task))
1733 if (vcpu->arch.vpa.update_pending ||
1734 vcpu->arch.slb_shadow.update_pending ||
1735 vcpu->arch.dtl.update_pending)
1736 vcpus_to_update[need_vpa_update++] = vcpu;
1740 * Initialize *vc, in particular vc->vcore_state, so we can
1741 * drop the vcore lock if necessary.
1745 vc->entry_exit_count = 0;
1746 vc->preempt_tb = TB_NIL;
1747 vc->vcore_state = VCORE_STARTING;
1749 vc->napping_threads = 0;
1750 vc->conferring_threads = 0;
1753 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1754 * which can't be called with any spinlocks held.
1756 if (need_vpa_update) {
1757 spin_unlock(&vc->lock);
1758 for (i = 0; i < need_vpa_update; ++i)
1759 kvmppc_update_vpas(vcpus_to_update[i]);
1760 spin_lock(&vc->lock);
1764 * Make sure we are running on primary threads, and that secondary
1765 * threads are offline. Also check if the number of threads in this
1766 * guest are greater than the current system threads per guest.
1768 if ((threads_per_core > 1) &&
1769 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
1770 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1771 vcpu->arch.ret = -EBUSY;
1776 vc->pcpu = smp_processor_id();
1777 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1778 kvmppc_start_thread(vcpu);
1779 kvmppc_create_dtl_entry(vcpu, vc);
1780 trace_kvm_guest_enter(vcpu);
1783 /* Set this explicitly in case thread 0 doesn't have a vcpu */
1784 get_paca()->kvm_hstate.kvm_vcore = vc;
1785 get_paca()->kvm_hstate.ptid = 0;
1787 vc->vcore_state = VCORE_RUNNING;
1790 trace_kvmppc_run_core(vc, 0);
1792 spin_unlock(&vc->lock);
1796 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
1798 if (vc->mpp_buffer_is_valid)
1799 kvmppc_start_restoring_l2_cache(vc);
1801 __kvmppc_vcore_entry();
1803 spin_lock(&vc->lock);
1806 kvmppc_start_saving_l2_cache(vc);
1808 /* disable sending of IPIs on virtual external irqs */
1809 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1811 /* wait for secondary threads to finish writing their state to memory */
1812 if (vc->nap_count < vc->n_woken)
1813 kvmppc_wait_for_nap(vc);
1814 for (i = 0; i < threads_per_subcore; ++i)
1815 kvmppc_release_hwthread(vc->pcpu + i);
1816 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
1817 vc->vcore_state = VCORE_EXITING;
1818 spin_unlock(&vc->lock);
1820 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
1822 /* make sure updates to secondary vcpu structs are visible now */
1829 spin_lock(&vc->lock);
1831 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1832 /* cancel pending dec exception if dec is positive */
1833 if (now < vcpu->arch.dec_expires &&
1834 kvmppc_core_pending_dec(vcpu))
1835 kvmppc_core_dequeue_dec(vcpu);
1837 trace_kvm_guest_exit(vcpu);
1840 if (vcpu->arch.trap)
1841 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1842 vcpu->arch.run_task);
1844 vcpu->arch.ret = ret;
1845 vcpu->arch.trap = 0;
1847 if (vcpu->arch.ceded) {
1848 if (!is_kvmppc_resume_guest(ret))
1849 kvmppc_end_cede(vcpu);
1851 kvmppc_set_timer(vcpu);
1856 vc->vcore_state = VCORE_INACTIVE;
1857 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1859 if (!is_kvmppc_resume_guest(vcpu->arch.ret)) {
1860 kvmppc_remove_runnable(vc, vcpu);
1861 wake_up(&vcpu->arch.cpu_run);
1865 trace_kvmppc_run_core(vc, 1);
1869 * Wait for some other vcpu thread to execute us, and
1870 * wake us up when we need to handle something in the host.
1872 static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
1876 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1877 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1879 finish_wait(&vcpu->arch.cpu_run, &wait);
1883 * All the vcpus in this vcore are idle, so wait for a decrementer
1884 * or external interrupt to one of the vcpus. vc->lock is held.
1886 static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1888 struct kvm_vcpu *vcpu;
1893 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1896 * Check one last time for pending exceptions and ceded state after
1897 * we put ourselves on the wait queue
1899 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1900 if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded) {
1907 finish_wait(&vc->wq, &wait);
1911 vc->vcore_state = VCORE_SLEEPING;
1912 trace_kvmppc_vcore_blocked(vc, 0);
1913 spin_unlock(&vc->lock);
1915 finish_wait(&vc->wq, &wait);
1916 spin_lock(&vc->lock);
1917 vc->vcore_state = VCORE_INACTIVE;
1918 trace_kvmppc_vcore_blocked(vc, 1);
1921 static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1924 struct kvmppc_vcore *vc;
1925 struct kvm_vcpu *v, *vn;
1927 trace_kvmppc_run_vcpu_enter(vcpu);
1929 kvm_run->exit_reason = 0;
1930 vcpu->arch.ret = RESUME_GUEST;
1931 vcpu->arch.trap = 0;
1932 kvmppc_update_vpas(vcpu);
1935 * Synchronize with other threads in this virtual core
1937 vc = vcpu->arch.vcore;
1938 spin_lock(&vc->lock);
1939 vcpu->arch.ceded = 0;
1940 vcpu->arch.run_task = current;
1941 vcpu->arch.kvm_run = kvm_run;
1942 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
1943 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
1944 vcpu->arch.busy_preempt = TB_NIL;
1945 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1949 * This happens the first time this is called for a vcpu.
1950 * If the vcore is already running, we may be able to start
1951 * this thread straight away and have it join in.
1953 if (!signal_pending(current)) {
1954 if (vc->vcore_state == VCORE_RUNNING &&
1955 VCORE_EXIT_COUNT(vc) == 0) {
1956 kvmppc_create_dtl_entry(vcpu, vc);
1957 kvmppc_start_thread(vcpu);
1958 trace_kvm_guest_enter(vcpu);
1959 } else if (vc->vcore_state == VCORE_SLEEPING) {
1965 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1966 !signal_pending(current)) {
1967 if (vc->vcore_state != VCORE_INACTIVE) {
1968 spin_unlock(&vc->lock);
1969 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1970 spin_lock(&vc->lock);
1973 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1975 kvmppc_core_prepare_to_enter(v);
1976 if (signal_pending(v->arch.run_task)) {
1977 kvmppc_remove_runnable(vc, v);
1978 v->stat.signal_exits++;
1979 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1980 v->arch.ret = -EINTR;
1981 wake_up(&v->arch.cpu_run);
1984 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1988 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
1989 if (!v->arch.pending_exceptions)
1990 n_ceded += v->arch.ceded;
1994 if (n_ceded == vc->n_runnable)
1995 kvmppc_vcore_blocked(vc);
1997 kvmppc_run_core(vc);
2001 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2002 (vc->vcore_state == VCORE_RUNNING ||
2003 vc->vcore_state == VCORE_EXITING)) {
2004 spin_unlock(&vc->lock);
2005 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
2006 spin_lock(&vc->lock);
2009 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2010 kvmppc_remove_runnable(vc, vcpu);
2011 vcpu->stat.signal_exits++;
2012 kvm_run->exit_reason = KVM_EXIT_INTR;
2013 vcpu->arch.ret = -EINTR;
2016 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
2017 /* Wake up some vcpu to run the core */
2018 v = list_first_entry(&vc->runnable_threads,
2019 struct kvm_vcpu, arch.run_list);
2020 wake_up(&v->arch.cpu_run);
2023 trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
2024 spin_unlock(&vc->lock);
2025 return vcpu->arch.ret;
2028 static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
2033 if (!vcpu->arch.sane) {
2034 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2038 kvmppc_core_prepare_to_enter(vcpu);
2040 /* No need to go into the guest when all we'll do is come back out */
2041 if (signal_pending(current)) {
2042 run->exit_reason = KVM_EXIT_INTR;
2046 atomic_inc(&vcpu->kvm->arch.vcpus_running);
2047 /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */
2050 /* On the first time here, set up HTAB and VRMA */
2051 if (!vcpu->kvm->arch.hpte_setup_done) {
2052 r = kvmppc_hv_setup_htab_rma(vcpu);
2057 flush_fp_to_thread(current);
2058 flush_altivec_to_thread(current);
2059 flush_vsx_to_thread(current);
2060 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
2061 vcpu->arch.pgdir = current->mm->pgd;
2062 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
2065 r = kvmppc_run_vcpu(run, vcpu);
2067 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
2068 !(vcpu->arch.shregs.msr & MSR_PR)) {
2069 trace_kvm_hcall_enter(vcpu);
2070 r = kvmppc_pseries_do_hcall(vcpu);
2071 trace_kvm_hcall_exit(vcpu, r);
2072 kvmppc_core_prepare_to_enter(vcpu);
2073 } else if (r == RESUME_PAGE_FAULT) {
2074 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2075 r = kvmppc_book3s_hv_page_fault(run, vcpu,
2076 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
2077 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
2079 } while (is_kvmppc_resume_guest(r));
2082 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
2083 atomic_dec(&vcpu->kvm->arch.vcpus_running);
2087 static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2090 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2094 (*sps)->page_shift = def->shift;
2095 (*sps)->slb_enc = def->sllp;
2096 (*sps)->enc[0].page_shift = def->shift;
2097 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
2099 * Add 16MB MPSS support if host supports it
2101 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2102 (*sps)->enc[1].page_shift = 24;
2103 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2108 static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2109 struct kvm_ppc_smmu_info *info)
2111 struct kvm_ppc_one_seg_page_size *sps;
2113 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2114 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2115 info->flags |= KVM_PPC_1T_SEGMENTS;
2116 info->slb_size = mmu_slb_size;
2118 /* We only support these sizes for now, and no muti-size segments */
2119 sps = &info->sps[0];
2120 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2121 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2122 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2128 * Get (and clear) the dirty memory log for a memory slot.
2130 static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2131 struct kvm_dirty_log *log)
2133 struct kvm_memory_slot *memslot;
2137 mutex_lock(&kvm->slots_lock);
2140 if (log->slot >= KVM_USER_MEM_SLOTS)
2143 memslot = id_to_memslot(kvm->memslots, log->slot);
2145 if (!memslot->dirty_bitmap)
2148 n = kvm_dirty_bitmap_bytes(memslot);
2149 memset(memslot->dirty_bitmap, 0, n);
2151 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
2156 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2161 mutex_unlock(&kvm->slots_lock);
2165 static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2166 struct kvm_memory_slot *dont)
2168 if (!dont || free->arch.rmap != dont->arch.rmap) {
2169 vfree(free->arch.rmap);
2170 free->arch.rmap = NULL;
2174 static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
2175 unsigned long npages)
2177 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
2178 if (!slot->arch.rmap)
2184 static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
2185 struct kvm_memory_slot *memslot,
2186 struct kvm_userspace_memory_region *mem)
2191 static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
2192 struct kvm_userspace_memory_region *mem,
2193 const struct kvm_memory_slot *old)
2195 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
2196 struct kvm_memory_slot *memslot;
2198 if (npages && old->npages) {
2200 * If modifying a memslot, reset all the rmap dirty bits.
2201 * If this is a new memslot, we don't need to do anything
2202 * since the rmap array starts out as all zeroes,
2203 * i.e. no pages are dirty.
2205 memslot = id_to_memslot(kvm->memslots, mem->slot);
2206 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
2211 * Update LPCR values in kvm->arch and in vcores.
2212 * Caller must hold kvm->lock.
2214 void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
2219 if ((kvm->arch.lpcr & mask) == lpcr)
2222 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2224 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2225 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2228 spin_lock(&vc->lock);
2229 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2230 spin_unlock(&vc->lock);
2231 if (++cores_done >= kvm->arch.online_vcores)
2236 static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2241 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
2244 struct kvm *kvm = vcpu->kvm;
2246 struct kvm_memory_slot *memslot;
2247 struct vm_area_struct *vma;
2248 unsigned long lpcr = 0, senc;
2249 unsigned long psize, porder;
2252 mutex_lock(&kvm->lock);
2253 if (kvm->arch.hpte_setup_done)
2254 goto out; /* another vcpu beat us to it */
2256 /* Allocate hashed page table (if not done already) and reset it */
2257 if (!kvm->arch.hpt_virt) {
2258 err = kvmppc_alloc_hpt(kvm, NULL);
2260 pr_err("KVM: Couldn't alloc HPT\n");
2265 /* Look up the memslot for guest physical address 0 */
2266 srcu_idx = srcu_read_lock(&kvm->srcu);
2267 memslot = gfn_to_memslot(kvm, 0);
2269 /* We must have some memory at 0 by now */
2271 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
2274 /* Look up the VMA for the start of this memory slot */
2275 hva = memslot->userspace_addr;
2276 down_read(¤t->mm->mmap_sem);
2277 vma = find_vma(current->mm, hva);
2278 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2281 psize = vma_kernel_pagesize(vma);
2282 porder = __ilog2(psize);
2284 up_read(¤t->mm->mmap_sem);
2286 /* We can handle 4k, 64k or 16M pages in the VRMA */
2288 if (!(psize == 0x1000 || psize == 0x10000 ||
2289 psize == 0x1000000))
2292 /* Update VRMASD field in the LPCR */
2293 senc = slb_pgsize_encoding(psize);
2294 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2295 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2296 /* the -4 is to account for senc values starting at 0x10 */
2297 lpcr = senc << (LPCR_VRMASD_SH - 4);
2299 /* Create HPTEs in the hash page table for the VRMA */
2300 kvmppc_map_vrma(vcpu, memslot, porder);
2302 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
2304 /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */
2306 kvm->arch.hpte_setup_done = 1;
2309 srcu_read_unlock(&kvm->srcu, srcu_idx);
2311 mutex_unlock(&kvm->lock);
2315 up_read(¤t->mm->mmap_sem);
2319 static int kvmppc_core_init_vm_hv(struct kvm *kvm)
2321 unsigned long lpcr, lpid;
2323 /* Allocate the guest's logical partition ID */
2325 lpid = kvmppc_alloc_lpid();
2328 kvm->arch.lpid = lpid;
2331 * Since we don't flush the TLB when tearing down a VM,
2332 * and this lpid might have previously been used,
2333 * make sure we flush on each core before running the new VM.
2335 cpumask_setall(&kvm->arch.need_tlb_flush);
2337 /* Start out with the default set of hcalls enabled */
2338 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
2339 sizeof(kvm->arch.enabled_hcalls));
2341 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
2343 /* Init LPCR for virtual RMA mode */
2344 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2345 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2346 lpcr &= LPCR_PECE | LPCR_LPES;
2347 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
2348 LPCR_VPM0 | LPCR_VPM1;
2349 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2350 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2351 /* On POWER8 turn on online bit to enable PURR/SPURR */
2352 if (cpu_has_feature(CPU_FTR_ARCH_207S))
2354 kvm->arch.lpcr = lpcr;
2357 * Track that we now have a HV mode VM active. This blocks secondary
2358 * CPU threads from coming online.
2360 kvm_hv_vm_activated();
2365 static void kvmppc_free_vcores(struct kvm *kvm)
2369 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2370 if (kvm->arch.vcores[i] && kvm->arch.vcores[i]->mpp_buffer) {
2371 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2372 free_pages((unsigned long)vc->mpp_buffer,
2375 kfree(kvm->arch.vcores[i]);
2377 kvm->arch.online_vcores = 0;
2380 static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
2382 kvm_hv_vm_deactivated();
2384 kvmppc_free_vcores(kvm);
2386 kvmppc_free_hpt(kvm);
2389 /* We don't need to emulate any privileged instructions or dcbz */
2390 static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2391 unsigned int inst, int *advance)
2393 return EMULATE_FAIL;
2396 static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2399 return EMULATE_FAIL;
2402 static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2405 return EMULATE_FAIL;
2408 static int kvmppc_core_check_processor_compat_hv(void)
2410 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
2411 !cpu_has_feature(CPU_FTR_ARCH_206))
2416 static long kvm_arch_vm_ioctl_hv(struct file *filp,
2417 unsigned int ioctl, unsigned long arg)
2419 struct kvm *kvm __maybe_unused = filp->private_data;
2420 void __user *argp = (void __user *)arg;
2425 case KVM_PPC_ALLOCATE_HTAB: {
2429 if (get_user(htab_order, (u32 __user *)argp))
2431 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2435 if (put_user(htab_order, (u32 __user *)argp))
2441 case KVM_PPC_GET_HTAB_FD: {
2442 struct kvm_get_htab_fd ghf;
2445 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2447 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2459 * List of hcall numbers to enable by default.
2460 * For compatibility with old userspace, we enable by default
2461 * all hcalls that were implemented before the hcall-enabling
2462 * facility was added. Note this list should not include H_RTAS.
2464 static unsigned int default_hcall_list[] = {
2478 #ifdef CONFIG_KVM_XICS
2489 static void init_default_hcalls(void)
2494 for (i = 0; default_hcall_list[i]; ++i) {
2495 hcall = default_hcall_list[i];
2496 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
2497 __set_bit(hcall / 4, default_enabled_hcalls);
2501 static struct kvmppc_ops kvm_ops_hv = {
2502 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2503 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2504 .get_one_reg = kvmppc_get_one_reg_hv,
2505 .set_one_reg = kvmppc_set_one_reg_hv,
2506 .vcpu_load = kvmppc_core_vcpu_load_hv,
2507 .vcpu_put = kvmppc_core_vcpu_put_hv,
2508 .set_msr = kvmppc_set_msr_hv,
2509 .vcpu_run = kvmppc_vcpu_run_hv,
2510 .vcpu_create = kvmppc_core_vcpu_create_hv,
2511 .vcpu_free = kvmppc_core_vcpu_free_hv,
2512 .check_requests = kvmppc_core_check_requests_hv,
2513 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2514 .flush_memslot = kvmppc_core_flush_memslot_hv,
2515 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2516 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2517 .unmap_hva = kvm_unmap_hva_hv,
2518 .unmap_hva_range = kvm_unmap_hva_range_hv,
2519 .age_hva = kvm_age_hva_hv,
2520 .test_age_hva = kvm_test_age_hva_hv,
2521 .set_spte_hva = kvm_set_spte_hva_hv,
2522 .mmu_destroy = kvmppc_mmu_destroy_hv,
2523 .free_memslot = kvmppc_core_free_memslot_hv,
2524 .create_memslot = kvmppc_core_create_memslot_hv,
2525 .init_vm = kvmppc_core_init_vm_hv,
2526 .destroy_vm = kvmppc_core_destroy_vm_hv,
2527 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2528 .emulate_op = kvmppc_core_emulate_op_hv,
2529 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2530 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2531 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2532 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
2533 .hcall_implemented = kvmppc_hcall_impl_hv,
2536 static int kvmppc_book3s_init_hv(void)
2540 * FIXME!! Do we need to check on all cpus ?
2542 r = kvmppc_core_check_processor_compat_hv();
2546 kvm_ops_hv.owner = THIS_MODULE;
2547 kvmppc_hv_ops = &kvm_ops_hv;
2549 init_default_hcalls();
2551 r = kvmppc_mmu_hv_init();
2555 static void kvmppc_book3s_exit_hv(void)
2557 kvmppc_hv_ops = NULL;
2560 module_init(kvmppc_book3s_init_hv);
2561 module_exit(kvmppc_book3s_exit_hv);
2562 MODULE_LICENSE("GPL");
2563 MODULE_ALIAS_MISCDEV(KVM_MINOR);
2564 MODULE_ALIAS("devname:kvm");